blob: 93019d067cb05a61d27dd65b3bd3019f76a3abb9 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb604b4d2004-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 Lattner07fad1c2004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukmane05203f2004-06-21 16:55:25 +000013//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman811f5c22004-06-29 17:13:26 +000016//
Misha Brukmane05203f2004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattner8c6a41e2006-11-17 22:10:59 +000021#include "PPCPredicates.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000022#include "PPCTargetMachine.h"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000023#include "PPCSubtarget.h"
Misha Brukmane05203f2004-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 Lattner0ced9052004-08-16 23:25:21 +000028#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb0609d92006-01-04 13:52:30 +000029#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskeyc56315c2007-01-26 21:22:28 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling50794832008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9148f362010-01-13 19:00:57 +000035#include "llvm/MC/MCContext.h"
Chris Lattner6c203912009-08-10 18:15:01 +000036#include "llvm/MC/MCSectionMachO.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000037#include "llvm/MC/MCStreamer.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000038#include "llvm/MC/MCSymbol.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
40#include "llvm/Target/TargetRegisterInfo.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetOptions.h"
43#include "llvm/Target/TargetRegistry.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000044#include "llvm/Support/Mangler.h"
Nate Begeman4d847042004-09-04 14:51:26 +000045#include "llvm/Support/MathExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000046#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000048#include "llvm/Support/ErrorHandling.h"
David Greenea31f96c2009-07-14 20:18:05 +000049#include "llvm/Support/FormattedStream.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000050#include "llvm/ADT/Statistic.h"
51#include "llvm/ADT/StringExtras.h"
Evan Cheng2a03c7e2008-12-05 01:06:39 +000052#include "llvm/ADT/StringSet.h"
Chris Lattner209aeca2010-01-13 06:38:18 +000053#include "llvm/ADT/SmallString.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000054using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000055
Chris Lattner1ef9cd42006-12-19 22:59:26 +000056STATISTIC(EmittedInsts, "Number of machine instrs printed");
Misha Brukmane05203f2004-06-21 16:55:25 +000057
Chris Lattner1ef9cd42006-12-19 22:59:26 +000058namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000059 class PPCAsmPrinter : public AsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000060 protected:
Chris Lattnere8382ac2009-07-15 02:28:57 +000061 struct FnStubInfo {
Chris Lattner9148f362010-01-13 19:00:57 +000062 std::string StubName, LazyPtrName, AnonSymbolName;
63 MCSymbol *StubSym, *LazyPtrSym, *AnonSymbolSym;
Chris Lattnere8382ac2009-07-15 02:28:57 +000064
Chris Lattner9148f362010-01-13 19:00:57 +000065 FnStubInfo() {
66 StubSym = LazyPtrSym = AnonSymbolSym = 0;
67 }
Chris Lattnere8382ac2009-07-15 02:28:57 +000068
69 void Init(const GlobalValue *GV, Mangler *Mang) {
70 // Already initialized.
Chris Lattner9148f362010-01-13 19:00:57 +000071 if (!StubName.empty()) return;
72 StubName = Mang->getMangledName(GV, "$stub", true);
73 LazyPtrName = Mang->getMangledName(GV, "$lazy_ptr", true);
74 AnonSymbolName = Mang->getMangledName(GV, "$stub$tmp", true);
Chris Lattnere8382ac2009-07-15 02:28:57 +000075 }
76
Chris Lattner9148f362010-01-13 19:00:57 +000077 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattnerf0a401f2010-01-13 07:56:59 +000078 assert(!GVName.empty());
Chris Lattner9148f362010-01-13 19:00:57 +000079 if (StubSym != 0) return; // Already initialized.
Chris Lattnerf0a401f2010-01-13 07:56:59 +000080 // Get the names for the external symbol name.
Chris Lattner209aeca2010-01-13 06:38:18 +000081 SmallString<128> TmpStr;
Chris Lattnerf0a401f2010-01-13 07:56:59 +000082 Mang->getNameWithPrefix(TmpStr, GVName + "$stub", Mangler::Private);
Chris Lattner9148f362010-01-13 19:00:57 +000083 StubSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner209aeca2010-01-13 06:38:18 +000084 TmpStr.clear();
85
Chris Lattnerf0a401f2010-01-13 07:56:59 +000086 Mang->getNameWithPrefix(TmpStr, GVName + "$lazy_ptr", Mangler::Private);
Chris Lattner9148f362010-01-13 19:00:57 +000087 LazyPtrSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner209aeca2010-01-13 06:38:18 +000088 TmpStr.clear();
89
Chris Lattnerf0a401f2010-01-13 07:56:59 +000090 Mang->getNameWithPrefix(TmpStr, GVName + "$stub$tmp", Mangler::Private);
Chris Lattner9148f362010-01-13 19:00:57 +000091 AnonSymbolSym = Ctx.GetOrCreateSymbol(TmpStr.str());
92 }
93
94 void printStub(raw_ostream &OS, const MCAsmInfo *MAI) const {
95 if (StubSym)
96 StubSym->print(OS, MAI);
97 else
98 OS << StubName;
99 }
100 void printLazyPtr(raw_ostream &OS, const MCAsmInfo *MAI) const {
101 if (LazyPtrSym)
102 LazyPtrSym->print(OS, MAI);
103 else
104 OS << LazyPtrName;
105 }
106 void printAnonSymbol(raw_ostream &OS, const MCAsmInfo *MAI) const {
107 if (AnonSymbolSym)
108 AnonSymbolSym->print(OS, MAI);
109 else
110 OS << AnonSymbolName;
Chris Lattnere8382ac2009-07-15 02:28:57 +0000111 }
112 };
113
114 StringMap<FnStubInfo> FnStubs;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000115 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Chris Lattner8597a2f2006-09-20 17:07:15 +0000116 const PPCSubtarget &Subtarget;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000117 uint64_t LabelID;
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000118 public:
David Greenea31f96c2009-07-14 20:18:05 +0000119 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner7b26fce2009-08-22 20:48:53 +0000120 const MCAsmInfo *T, bool V)
Daniel Dunbar75c12e12009-07-01 01:48:54 +0000121 : AsmPrinter(O, TM, T, V),
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000122 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Misha Brukmanb4402432005-04-21 23:30:14 +0000123
Misha Brukmane05203f2004-06-21 16:55:25 +0000124 virtual const char *getPassName() const {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000125 return "PowerPC Assembly Printer";
Misha Brukmane05203f2004-06-21 16:55:25 +0000126 }
127
Nate Begeman6cca84e2005-10-16 05:39:50 +0000128 PPCTargetMachine &getTM() {
129 return static_cast<PPCTargetMachine&>(TM);
Chris Lattner0ced9052004-08-16 23:25:21 +0000130 }
131
Nate Begeman8465fe82005-07-20 22:42:00 +0000132 unsigned enumRegToMachineReg(unsigned enumReg) {
133 switch (enumReg) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000134 default: llvm_unreachable("Unhandled register!");
Nate Begeman8465fe82005-07-20 22:42:00 +0000135 case PPC::CR0: return 0;
136 case PPC::CR1: return 1;
137 case PPC::CR2: return 2;
138 case PPC::CR3: return 3;
139 case PPC::CR4: return 4;
140 case PPC::CR5: return 5;
141 case PPC::CR6: return 6;
142 case PPC::CR7: return 7;
143 }
Torok Edwinfbcc6632009-07-14 16:55:14 +0000144 llvm_unreachable(0);
Nate Begeman8465fe82005-07-20 22:42:00 +0000145 }
146
Nate Begeman0ad7f812004-08-14 22:09:10 +0000147 /// printInstruction - This method is automatically generated by tablegen
148 /// from the instruction set description. This method returns true if the
149 /// machine instruction was sufficiently described to print it, otherwise it
150 /// returns false.
Chris Lattnerb94284b2009-08-08 01:32:19 +0000151 void printInstruction(const MachineInstr *MI);
Chris Lattnerad10b3b2009-09-13 20:19:22 +0000152 static const char *getRegisterName(unsigned RegNo);
Chris Lattner06c5eed2009-09-13 20:08:00 +0000153
Nate Begeman0ad7f812004-08-14 22:09:10 +0000154
Misha Brukmane05203f2004-06-21 16:55:25 +0000155 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000156 void printOp(const MachineOperand &MO);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000157
Jim Laskey41621a72006-12-20 20:56:46 +0000158 /// stripRegisterPrefix - This method strips the character prefix from a
159 /// register name so that only the number is left. Used by for linux asm.
Jim Laskeyc2c861d2006-12-20 21:33:34 +0000160 const char *stripRegisterPrefix(const char *RegName) {
161 switch (RegName[0]) {
162 case 'r':
163 case 'f':
164 case 'v': return RegName + 1;
Jim Laskey4c90a6d2006-12-20 21:35:00 +0000165 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskey41621a72006-12-20 20:56:46 +0000166 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000167
Jim Laskeyc2c861d2006-12-20 21:33:34 +0000168 return RegName;
Jim Laskey41621a72006-12-20 20:56:46 +0000169 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000170
Jim Laskey41621a72006-12-20 20:56:46 +0000171 /// printRegister - Print register according to target requirements.
172 ///
173 void printRegister(const MachineOperand &MO, bool R0AsZero) {
174 unsigned RegNo = MO.getReg();
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000175 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000176
Jim Laskey41621a72006-12-20 20:56:46 +0000177 // If we should use 0 for R0.
178 if (R0AsZero && RegNo == PPC::R0) {
179 O << "0";
180 return;
181 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000182
Chris Lattnereb2cc682009-09-13 20:31:40 +0000183 const char *RegName = getRegisterName(RegNo);
Jim Laskey41621a72006-12-20 20:56:46 +0000184 // Linux assembler (Others?) does not take register mnemonics.
185 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskeyc2c861d2006-12-20 21:33:34 +0000186 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
187 O << RegName;
Jim Laskey41621a72006-12-20 20:56:46 +0000188 }
Chris Lattnerec1cc1b2004-08-14 23:27:29 +0000189
Chris Lattnerf7f05672006-02-01 22:38:46 +0000190 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnerec1cc1b2004-08-14 23:27:29 +0000191 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000192 if (MO.isReg()) {
Jim Laskey41621a72006-12-20 20:56:46 +0000193 printRegister(MO, false);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000194 } else if (MO.isImm()) {
Chris Lattner5c463782007-12-30 20:49:49 +0000195 O << MO.getImm();
Chris Lattnerec1cc1b2004-08-14 23:27:29 +0000196 } else {
197 printOp(MO);
198 }
199 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000200
Chris Lattnerf7f05672006-02-01 22:38:46 +0000201 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner1bad2542006-02-23 19:31:10 +0000202 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner7674d902006-02-24 20:27:40 +0000203 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
204 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000205
206
Chris Lattner2771e2c2006-03-25 06:12:06 +0000207 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5c463782007-12-30 20:49:49 +0000208 char value = MI->getOperand(OpNo).getImm();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000209 value = (value << (32-5)) >> (32-5);
210 O << (int)value;
211 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000212 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5c463782007-12-30 20:49:49 +0000213 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattnerf7833ba2004-08-21 19:11:03 +0000214 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000215 O << (unsigned int)value;
216 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000217 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5c463782007-12-30 20:49:49 +0000218 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman143cf942004-08-30 02:28:06 +0000219 assert(value <= 63 && "Invalid u6imm argument!");
220 O << (unsigned int)value;
221 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000222 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5c463782007-12-30 20:49:49 +0000223 O << (short)MI->getOperand(OpNo).getImm();
Nate Begeman4bfceb12004-09-04 05:00:00 +0000224 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000225 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5c463782007-12-30 20:49:49 +0000226 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner8a796852004-08-15 05:20:16 +0000227 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000228 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000229 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner5c463782007-12-30 20:49:49 +0000230 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner30055b92006-11-16 21:45:30 +0000231 } else {
232 O << "lo16(";
233 printOp(MI->getOperand(OpNo));
234 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Chengcdf36092007-10-14 05:57:21 +0000235 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner30055b92006-11-16 21:45:30 +0000236 else
237 O << ')';
238 }
Chris Lattner5a2fb972005-10-18 16:51:22 +0000239 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000240 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000241 // Branches can take an immediate operand. This is used by the branch
242 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000243 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner5c463782007-12-30 20:49:49 +0000244 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000245 } else {
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000246 printOp(MI->getOperand(OpNo));
Nate Begeman4bfceb12004-09-04 05:00:00 +0000247 }
Nate Begeman61738782004-09-02 08:13:00 +0000248 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000249 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000250 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng73136df2006-02-22 20:19:42 +0000251 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner57575112005-12-16 00:22:14 +0000252 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
253 GlobalValue *GV = MO.getGlobal();
Chris Lattnerccfb5102009-07-02 16:08:53 +0000254 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattner57575112005-12-16 00:22:14 +0000255 // Dynamically-resolved functions need a stub for the function.
Chris Lattnere8382ac2009-07-15 02:28:57 +0000256 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
257 FnInfo.Init(GV, Mang);
Chris Lattner9148f362010-01-13 19:00:57 +0000258 FnInfo.printStub(O, MAI);
Chris Lattner57575112005-12-16 00:22:14 +0000259 return;
260 }
261 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000262 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner209aeca2010-01-13 06:38:18 +0000263 SmallString<128> MangledName;
Chris Lattnerf0a401f2010-01-13 07:56:59 +0000264 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattner209aeca2010-01-13 06:38:18 +0000265 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9148f362010-01-13 19:00:57 +0000266 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
267 FnInfo.printStub(O, MAI);
Chris Lattnercdde99902005-11-17 19:40:30 +0000268 return;
Chris Lattnercdde99902005-11-17 19:40:30 +0000269 }
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000270 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000271
Chris Lattnercdde99902005-11-17 19:40:30 +0000272 printOp(MI->getOperand(OpNo));
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000273 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000274 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner5c463782007-12-30 20:49:49 +0000275 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begemana171f6b2005-11-16 00:48:01 +0000276 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000277 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Chengcdf36092007-10-14 05:57:21 +0000278 O << "\"L" << getFunctionNumber() << "$pb\"\n";
279 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begeman61738782004-09-02 08:13:00 +0000280 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000281 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000282 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000283 printS16ImmOperand(MI, OpNo);
Nate Begemana9443f22005-07-21 20:44:43 +0000284 } else {
Nick Lewyckye6049c22007-03-03 05:29:51 +0000285 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begemana9443f22005-07-21 20:44:43 +0000286 printOp(MI->getOperand(OpNo));
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000287 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Chengcdf36092007-10-14 05:57:21 +0000288 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckye6049c22007-03-03 05:29:51 +0000289 if (Subtarget.isDarwin())
Nate Begemana9443f22005-07-21 20:44:43 +0000290 O << ')';
Nick Lewyckye6049c22007-03-03 05:29:51 +0000291 else
292 O << "@ha";
Nate Begemana9443f22005-07-21 20:44:43 +0000293 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000294 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000295 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000296 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000297 printS16ImmOperand(MI, OpNo);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000298 } else {
Nick Lewyckye6049c22007-03-03 05:29:51 +0000299 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begeman3f76eb62004-11-25 07:09:01 +0000300 printOp(MI->getOperand(OpNo));
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000301 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Chengcdf36092007-10-14 05:57:21 +0000302 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckye6049c22007-03-03 05:29:51 +0000303 if (Subtarget.isDarwin())
Nate Begemana9443f22005-07-21 20:44:43 +0000304 O << ')';
Nick Lewyckye6049c22007-03-03 05:29:51 +0000305 else
306 O << "@l";
Nate Begeman4bfceb12004-09-04 05:00:00 +0000307 }
308 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000309 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman8465fe82005-07-20 22:42:00 +0000310 unsigned CCReg = MI->getOperand(OpNo).getReg();
311 unsigned RegNo = enumRegToMachineReg(CCReg);
312 O << (0x80 >> RegNo);
313 }
Chris Lattner7674d902006-02-24 20:27:40 +0000314 // The new addressing mode printers.
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000315 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
316 printSymbolLo(MI, OpNo);
317 O << '(';
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000318 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner139eac52006-03-21 17:21:13 +0000319 MI->getOperand(OpNo+1).getReg() == PPC::R0)
320 O << "0";
321 else
322 printOperand(MI, OpNo+1);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000323 O << ')';
324 }
Chris Lattner77373d12006-03-22 05:26:03 +0000325 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000326 if (MI->getOperand(OpNo).isImm())
Chris Lattner77373d12006-03-22 05:26:03 +0000327 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000328 else
Chris Lattner77373d12006-03-22 05:26:03 +0000329 printSymbolLo(MI, OpNo);
330 O << '(';
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000331 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner77373d12006-03-22 05:26:03 +0000332 MI->getOperand(OpNo+1).getReg() == PPC::R0)
333 O << "0";
334 else
335 printOperand(MI, OpNo+1);
336 O << ')';
337 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000338
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000339 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc1263972005-12-19 23:40:42 +0000340 // When used as the base register, r0 reads constant zero rather than
341 // the value contained in the register. For this reason, the darwin
342 // assembler requires that we print r0 as 0 (no r) when used as the base.
343 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskey41621a72006-12-20 20:56:46 +0000344 printRegister(MO, true);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000345 O << ", ";
346 printOperand(MI, OpNo+1);
347 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000348
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000349 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
350 const MachineOperand &MO = MI->getOperand(OpNo);
351
352 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
353
354 GlobalValue *GV = MO.getGlobal();
355
356 std::string Name = Mang->getMangledName(GV);
357
358 // Map symbol -> label of TOC entry.
359 if (TOC.count(Name) == 0) {
360 std::string Label;
Chris Lattnere9a75a62009-08-22 21:43:10 +0000361 Label += MAI->getPrivateGlobalPrefix();
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000362 Label += "C";
363 Label += utostr(LabelID++);
364
365 TOC[Name] = Label;
366 }
367
368 O << TOC[Name] << "@toc";
369 }
370
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000371 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner6be72602006-11-04 05:27:39 +0000372 const char *Modifier);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000373
Misha Brukmanb4402432005-04-21 23:30:14 +0000374 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000375 };
Misha Brukmanb4402432005-04-21 23:30:14 +0000376
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000377 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky02d5f772009-10-25 06:33:48 +0000378 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000379 public:
Daniel Dunbare8338102009-07-15 20:24:03 +0000380 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner7b26fce2009-08-22 20:48:53 +0000381 const MCAsmInfo *T, bool V)
Daniel Dunbar75c12e12009-07-01 01:48:54 +0000382 : PPCAsmPrinter(O, TM, T, V){}
Jim Laskey28663c72006-12-21 20:26:09 +0000383
384 virtual const char *getPassName() const {
385 return "Linux PPC Assembly Printer";
386 }
387
388 bool runOnMachineFunction(MachineFunction &F);
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000389 bool doFinalization(Module &M);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000390
Jim Laskey28663c72006-12-21 20:26:09 +0000391 void getAnalysisUsage(AnalysisUsage &AU) const {
392 AU.setPreservesAll();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000393 AU.addRequired<MachineModuleInfo>();
Devang Patelf6466682009-01-08 23:40:34 +0000394 AU.addRequired<DwarfWriter>();
Jim Laskey28663c72006-12-21 20:26:09 +0000395 PPCAsmPrinter::getAnalysisUsage(AU);
396 }
397
Chris Lattner100865e2009-07-21 18:38:57 +0000398 void PrintGlobalVariable(const GlobalVariable *GVar);
Jim Laskey28663c72006-12-21 20:26:09 +0000399 };
400
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000401 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
402 /// OS X
Nick Lewycky02d5f772009-10-25 06:33:48 +0000403 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greenea31f96c2009-07-14 20:18:05 +0000404 formatted_raw_ostream &OS;
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000405 public:
Daniel Dunbare8338102009-07-15 20:24:03 +0000406 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner7b26fce2009-08-22 20:48:53 +0000407 const MCAsmInfo *T, bool V)
Daniel Dunbar75c12e12009-07-01 01:48:54 +0000408 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Nate Begeman4bfceb12004-09-04 05:00:00 +0000409
410 virtual const char *getPassName() const {
411 return "Darwin PPC Assembly Printer";
412 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000413
Misha Brukmanb4402432005-04-21 23:30:14 +0000414 bool runOnMachineFunction(MachineFunction &F);
Misha Brukmane05203f2004-06-21 16:55:25 +0000415 bool doFinalization(Module &M);
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000416 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000417
Jim Laskey219d5592006-01-04 22:28:25 +0000418 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000419 AU.setPreservesAll();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000420 AU.addRequired<MachineModuleInfo>();
Devang Patelf6466682009-01-08 23:40:34 +0000421 AU.addRequired<DwarfWriter>();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000422 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskey219d5592006-01-04 22:28:25 +0000423 }
424
Chris Lattner100865e2009-07-21 18:38:57 +0000425 void PrintGlobalVariable(const GlobalVariable *GVar);
Misha Brukmane05203f2004-06-21 16:55:25 +0000426 };
427} // end of anonymous namespace
428
Nate Begeman0ad7f812004-08-14 22:09:10 +0000429// Include the auto-generated portion of the assembly writer
Chris Lattner0921e3b2005-10-14 23:37:35 +0000430#include "PPCGenAsmWriter.inc"
Nate Begeman0ad7f812004-08-14 22:09:10 +0000431
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000432void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukmane05203f2004-06-21 16:55:25 +0000433 switch (MO.getType()) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000434 case MachineOperand::MO_Immediate:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000435 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman8d75aa42004-07-21 20:11:11 +0000436
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000437 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner4cd44982009-09-13 17:14:04 +0000438 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Misha Brukmane05203f2004-06-21 16:55:25 +0000439 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000440 case MachineOperand::MO_JumpTableIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000441 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattnera5bb3702007-12-30 23:10:15 +0000442 << '_' << MO.getIndex();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000443 // FIXME: PIC relocation model
444 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000445 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000446 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattnera5bb3702007-12-30 23:10:15 +0000447 << '_' << MO.getIndex();
Misha Brukmane05203f2004-06-21 16:55:25 +0000448 return;
Bob Wilsonf84f7102009-11-04 21:31:18 +0000449 case MachineOperand::MO_BlockAddress:
450 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
451 return;
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000452 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner57575112005-12-16 00:22:14 +0000453 // Computing the address of an external symbol, not calling it.
Chris Lattnere9a75a62009-08-22 21:43:10 +0000454 std::string Name(MAI->getGlobalPrefix());
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000455 Name += MO.getSymbolName();
456
Evan Cheng73136df2006-02-22 20:19:42 +0000457 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000458 GVStubs[Name] = Name+"$non_lazy_ptr";
459 Name += "$non_lazy_ptr";
Chris Lattner57575112005-12-16 00:22:14 +0000460 }
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000461 O << Name;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000462 return;
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000463 }
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000464 case MachineOperand::MO_GlobalAddress: {
Chris Lattner57575112005-12-16 00:22:14 +0000465 // Computing the address of a global symbol, not calling it.
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000466 GlobalValue *GV = MO.getGlobal();
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000467 std::string Name;
Misha Brukman7dba17d2004-07-23 16:08:20 +0000468
Nate Begeman844186b2004-10-17 23:01:34 +0000469 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000470 if (TM.getRelocationModel() != Reloc::Static &&
471 (GV->isDeclaration() || GV->isWeakForLinker())) {
472 if (!GV->hasHiddenVisibility()) {
473 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
474 GVStubs[Mang->getMangledName(GV)] = Name;
475 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
476 GV->hasAvailableExternallyLinkage()) {
477 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
478 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
479 } else {
480 Name = Mang->getMangledName(GV);
Chris Lattner57575112005-12-16 00:22:14 +0000481 }
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000482 } else {
483 Name = Mang->getMangledName(GV);
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000484 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000485 O << Name;
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000486
Anton Korobeynikovbff4b372008-11-22 16:15:34 +0000487 printOffset(MO.getOffset());
Misha Brukmane05203f2004-06-21 16:55:25 +0000488 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000489 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000490
Misha Brukmane05203f2004-06-21 16:55:25 +0000491 default:
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000492 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukmana2737582004-06-25 15:11:34 +0000493 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000494 }
495}
496
Chris Lattner1bad2542006-02-23 19:31:10 +0000497/// PrintAsmOperand - Print out an operand for an inline asm expression.
498///
499bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000500 unsigned AsmVariant,
Chris Lattner1bad2542006-02-23 19:31:10 +0000501 const char *ExtraCode) {
502 // Does this asm operand have a single letter operand modifier?
503 if (ExtraCode && ExtraCode[0]) {
504 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000505
Chris Lattner1bad2542006-02-23 19:31:10 +0000506 switch (ExtraCode[0]) {
507 default: return true; // Unknown modifier.
Chris Lattner48518542007-01-25 02:52:50 +0000508 case 'c': // Don't print "$" before a global var name or constant.
509 // PPC never has a prefix.
510 printOperand(MI, OpNo);
511 return false;
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000512 case 'L': // Write second word of DImode reference.
Chris Lattner1bad2542006-02-23 19:31:10 +0000513 // Verify that this operand has two consecutive registers.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000514 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattner1bad2542006-02-23 19:31:10 +0000515 OpNo+1 == MI->getNumOperands() ||
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000516 !MI->getOperand(OpNo+1).isReg())
Chris Lattner1bad2542006-02-23 19:31:10 +0000517 return true;
518 ++OpNo; // Return the high-part.
519 break;
Chris Lattnercb35c612007-04-24 22:51:03 +0000520 case 'I':
521 // Write 'i' if an integer constant, otherwise nothing. Used to print
522 // addi vs add, etc.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000523 if (MI->getOperand(OpNo).isImm())
Chris Lattnercb35c612007-04-24 22:51:03 +0000524 O << "i";
525 return false;
Chris Lattner1bad2542006-02-23 19:31:10 +0000526 }
527 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000528
Chris Lattner1bad2542006-02-23 19:31:10 +0000529 printOperand(MI, OpNo);
530 return false;
531}
532
Dale Johannesen4a50e682009-08-18 00:18:39 +0000533// At the moment, all inline asm memory operands are a single register.
534// In any case, the output of this routine should always be just one
535// assembler operand.
536
Chris Lattner7674d902006-02-24 20:27:40 +0000537bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000538 unsigned AsmVariant,
Chris Lattner7674d902006-02-24 20:27:40 +0000539 const char *ExtraCode) {
540 if (ExtraCode && ExtraCode[0])
541 return true; // Unknown modifier.
Dale Johannesen4a50e682009-08-18 00:18:39 +0000542 assert (MI->getOperand(OpNo).isReg());
Dale Johannesenf582ac72009-08-26 18:10:32 +0000543 O << "0(";
Dale Johannesen4a50e682009-08-18 00:18:39 +0000544 printOperand(MI, OpNo);
Dale Johannesenf582ac72009-08-26 18:10:32 +0000545 O << ")";
Chris Lattner7674d902006-02-24 20:27:40 +0000546 return false;
547}
Chris Lattner1bad2542006-02-23 19:31:10 +0000548
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000549void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner6be72602006-11-04 05:27:39 +0000550 const char *Modifier) {
551 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
552 unsigned Code = MI->getOperand(OpNo).getImm();
553 if (!strcmp(Modifier, "cc")) {
554 switch ((PPC::Predicate)Code) {
555 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
556 case PPC::PRED_LT: O << "lt"; return;
557 case PPC::PRED_LE: O << "le"; return;
558 case PPC::PRED_EQ: O << "eq"; return;
559 case PPC::PRED_GE: O << "ge"; return;
560 case PPC::PRED_GT: O << "gt"; return;
561 case PPC::PRED_NE: O << "ne"; return;
562 case PPC::PRED_UN: O << "un"; return;
563 case PPC::PRED_NU: O << "nu"; return;
564 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000565
Chris Lattner6be72602006-11-04 05:27:39 +0000566 } else {
567 assert(!strcmp(Modifier, "reg") &&
568 "Need to specify 'cc' or 'reg' as predicate op modifier!");
569 // Don't print the register for 'always'.
570 if (Code == PPC::PRED_ALWAYS) return;
571 printOperand(MI, OpNo+1);
572 }
573}
574
575
Nate Begeman0ad7f812004-08-14 22:09:10 +0000576/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
577/// the current output stream.
Misha Brukmane05203f2004-06-21 16:55:25 +0000578///
Chris Lattner0921e3b2005-10-14 23:37:35 +0000579void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begeman0ad7f812004-08-14 22:09:10 +0000580 ++EmittedInsts;
Chris Lattner716a8c92009-09-09 20:34:59 +0000581
Devang Patel051454a2009-10-06 02:19:11 +0000582 processDebugLoc(MI, true);
Chris Lattner2121f3c2005-10-14 22:44:13 +0000583
Nate Begeman52441732005-04-05 18:19:50 +0000584 // Check for slwi/srwi mnemonics.
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000585 bool useSubstituteMnemonic = false;
Nate Begeman52441732005-04-05 18:19:50 +0000586 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattner5c463782007-12-30 20:49:49 +0000587 unsigned char SH = MI->getOperand(2).getImm();
588 unsigned char MB = MI->getOperand(3).getImm();
589 unsigned char ME = MI->getOperand(4).getImm();
Nate Begeman52441732005-04-05 18:19:50 +0000590 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000591 O << "\tslwi "; useSubstituteMnemonic = true;
Nate Begeman52441732005-04-05 18:19:50 +0000592 }
593 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000594 O << "\tsrwi "; useSubstituteMnemonic = true;
Nate Begeman52441732005-04-05 18:19:50 +0000595 SH = 32-SH;
596 }
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000597 if (useSubstituteMnemonic) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000598 printOperand(MI, 0);
Misha Brukmanb4402432005-04-21 23:30:14 +0000599 O << ", ";
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000600 printOperand(MI, 1);
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000601 O << ", " << (unsigned int)SH;
Nate Begeman52441732005-04-05 18:19:50 +0000602 }
Chris Lattner52a956d2006-06-20 23:18:58 +0000603 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattnerf7b962d2006-02-08 06:56:40 +0000604 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000605 useSubstituteMnemonic = true;
Nate Begemanf3c89be32008-02-05 08:49:09 +0000606 O << "\tmr ";
Chris Lattnerf7b962d2006-02-08 06:56:40 +0000607 printOperand(MI, 0);
608 O << ", ";
609 printOperand(MI, 1);
Chris Lattnerf7b962d2006-02-08 06:56:40 +0000610 }
Chris Lattner9ca15c82006-11-18 01:23:56 +0000611 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner5c463782007-12-30 20:49:49 +0000612 unsigned char SH = MI->getOperand(2).getImm();
613 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner9ca15c82006-11-18 01:23:56 +0000614 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
615 if (63-SH == ME) {
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000616 useSubstituteMnemonic = true;
Nate Begemanf3c89be32008-02-05 08:49:09 +0000617 O << "\tsldi ";
Chris Lattner9ca15c82006-11-18 01:23:56 +0000618 printOperand(MI, 0);
619 O << ", ";
620 printOperand(MI, 1);
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000621 O << ", " << (unsigned int)SH;
Chris Lattner9ca15c82006-11-18 01:23:56 +0000622 }
Nate Begeman52441732005-04-05 18:19:50 +0000623 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000624
Dale Johannesen3257b4c2010-01-06 02:20:18 +0000625 if (!useSubstituteMnemonic)
626 printInstruction(MI);
627
David Greene659c1a92009-11-13 21:34:57 +0000628 if (VerboseAsm)
Chris Lattner321bc992009-09-09 23:14:36 +0000629 EmitComments(*MI);
630 O << '\n';
Devang Patel051454a2009-10-06 02:19:11 +0000631
632 processDebugLoc(MI, false);
Misha Brukmane05203f2004-06-21 16:55:25 +0000633}
634
Jim Laskey28663c72006-12-21 20:26:09 +0000635/// runOnMachineFunction - This uses the printMachineInstruction()
636/// method to print assembly for each instruction.
637///
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000638bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000639 this->MF = &MF;
Chris Lattner028d6632006-10-05 02:42:20 +0000640
Jim Laskey28663c72006-12-21 20:26:09 +0000641 SetupMachineFunction(MF);
642 O << "\n\n";
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000643
Jim Laskey28663c72006-12-21 20:26:09 +0000644 // Print out constants referenced by the function
645 EmitConstantPool(MF.getConstantPool());
646
647 // Print out labels for the function.
648 const Function *F = MF.getFunction();
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000649 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000650
Jim Laskey28663c72006-12-21 20:26:09 +0000651 switch (F->getLinkage()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000652 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindola6de96a12009-01-15 20:18:42 +0000653 case Function::PrivateLinkage:
Jim Laskey28663c72006-12-21 20:26:09 +0000654 case Function::InternalLinkage: // Symbols default to internal.
655 break;
656 case Function::ExternalLinkage:
657 O << "\t.global\t" << CurrentFnName << '\n'
658 << "\t.type\t" << CurrentFnName << ", @function\n";
659 break;
Dale Johannesen6bbeda42009-08-24 01:03:42 +0000660 case Function::LinkerPrivateLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +0000661 case Function::WeakAnyLinkage:
662 case Function::WeakODRLinkage:
663 case Function::LinkOnceAnyLinkage:
664 case Function::LinkOnceODRLinkage:
Jim Laskey28663c72006-12-21 20:26:09 +0000665 O << "\t.global\t" << CurrentFnName << '\n';
666 O << "\t.weak\t" << CurrentFnName << '\n';
667 break;
668 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000669
Anton Korobeynikoved473292008-08-08 18:25:07 +0000670 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000671
Bill Wendling31ceb1b2009-06-30 22:38:32 +0000672 EmitAlignment(MF.getAlignment(), F);
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000673
674 if (Subtarget.isPPC64()) {
675 // Emit an official procedure descriptor.
676 // FIXME 64-bit SVR4: Use MCSection here?
677 O << "\t.section\t\".opd\",\"aw\"\n";
678 O << "\t.align 3\n";
679 O << CurrentFnName << ":\n";
680 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
681 O << "\t.previous\n";
682 O << ".L." << CurrentFnName << ":\n";
683 } else {
684 O << CurrentFnName << ":\n";
685 }
Jim Laskey28663c72006-12-21 20:26:09 +0000686
687 // Emit pre-function debug information.
Devang Patelf6466682009-01-08 23:40:34 +0000688 DW->BeginFunction(&MF);
Jim Laskey28663c72006-12-21 20:26:09 +0000689
690 // Print out code for the function.
691 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
692 I != E; ++I) {
693 // Print a label for the basic block.
694 if (I != MF.begin()) {
Chris Lattner1d04aa72009-09-13 18:25:37 +0000695 EmitBasicBlockStart(I);
Jim Laskey28663c72006-12-21 20:26:09 +0000696 }
697 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
698 II != E; ++II) {
699 // Print the assembly for the instruction.
Jim Laskey28663c72006-12-21 20:26:09 +0000700 printMachineInstruction(II);
701 }
702 }
703
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000704 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Jim Laskey28663c72006-12-21 20:26:09 +0000705
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000706 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesen5d84f682008-12-03 19:33:10 +0000707
Jim Laskey28663c72006-12-21 20:26:09 +0000708 // Emit post-function debug information.
Devang Patelf6466682009-01-08 23:40:34 +0000709 DW->EndFunction(&MF);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000710
Bill Wendling7307bff2009-11-09 21:20:14 +0000711 // Print out jump tables referenced by the function.
712 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
713
Jim Laskey28663c72006-12-21 20:26:09 +0000714 // We didn't modify anything.
715 return false;
716}
717
Chris Lattner100865e2009-07-21 18:38:57 +0000718void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Jim Laskey28663c72006-12-21 20:26:09 +0000719 const TargetData *TD = TM.getTargetData();
720
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000721 if (!GVar->hasInitializer())
722 return; // External global require no code
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000723
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000724 // Check to see if this is a special global used by LLVM, if so, emit it.
725 if (EmitSpecialLLVMGlobal(GVar))
726 return;
Chris Lattner3c84b552007-01-14 06:37:54 +0000727
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000728 std::string name = Mang->getMangledName(GVar);
Chris Lattner3c84b552007-01-14 06:37:54 +0000729
Anton Korobeynikoved473292008-08-08 18:25:07 +0000730 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000731
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000732 Constant *C = GVar->getInitializer();
733 const Type *Type = C->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000734 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000735 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Jim Laskey28663c72006-12-21 20:26:09 +0000736
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000737 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
738 TM));
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000739
740 if (C->isNullValue() && /* FIXME: Verify correct */
741 !GVar->hasSection() &&
Rafael Espindola6de96a12009-01-15 20:18:42 +0000742 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands12da8ce2009-03-07 15:45:40 +0000743 GVar->isWeakForLinker())) {
Jim Laskey28663c72006-12-21 20:26:09 +0000744 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000745
746 if (GVar->hasExternalLinkage()) {
Jim Laskey28663c72006-12-21 20:26:09 +0000747 O << "\t.global " << name << '\n';
748 O << "\t.type " << name << ", @object\n";
Nick Lewycky5805c462007-07-25 03:48:45 +0000749 O << name << ":\n";
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000750 O << "\t.zero " << Size << '\n';
Rafael Espindola6de96a12009-01-15 20:18:42 +0000751 } else if (GVar->hasLocalLinkage()) {
Chris Lattnere9a75a62009-08-22 21:43:10 +0000752 O << MAI->getLCOMMDirective() << name << ',' << Size;
Jim Laskey28663c72006-12-21 20:26:09 +0000753 } else {
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000754 O << ".comm " << name << ',' << Size;
Jim Laskey28663c72006-12-21 20:26:09 +0000755 }
Evan Chenga774a992009-03-24 00:17:40 +0000756 if (VerboseAsm) {
Chris Lattnere9a75a62009-08-22 21:43:10 +0000757 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohmanef3d4572009-08-13 01:36:44 +0000758 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chenga774a992009-03-24 00:17:40 +0000759 O << "'";
760 }
761 O << '\n';
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000762 return;
Jim Laskey28663c72006-12-21 20:26:09 +0000763 }
764
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000765 switch (GVar->getLinkage()) {
Duncan Sands12da8ce2009-03-07 15:45:40 +0000766 case GlobalValue::LinkOnceAnyLinkage:
767 case GlobalValue::LinkOnceODRLinkage:
768 case GlobalValue::WeakAnyLinkage:
769 case GlobalValue::WeakODRLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +0000770 case GlobalValue::CommonLinkage:
Dale Johannesen6bbeda42009-08-24 01:03:42 +0000771 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000772 O << "\t.global " << name << '\n'
773 << "\t.type " << name << ", @object\n"
774 << "\t.weak " << name << '\n';
775 break;
776 case GlobalValue::AppendingLinkage:
777 // FIXME: appending linkage variables should go into a section of
778 // their name or something. For now, just emit them as external.
779 case GlobalValue::ExternalLinkage:
780 // If external or appending, declare as a global symbol
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000781 O << "\t.global " << name << '\n'
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000782 << "\t.type " << name << ", @object\n";
783 // FALL THROUGH
784 case GlobalValue::InternalLinkage:
Rafael Espindola6de96a12009-01-15 20:18:42 +0000785 case GlobalValue::PrivateLinkage:
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000786 break;
787 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000788 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000789 }
790
791 EmitAlignment(Align, GVar);
Evan Chenga774a992009-03-24 00:17:40 +0000792 O << name << ":";
793 if (VerboseAsm) {
Chris Lattnere9a75a62009-08-22 21:43:10 +0000794 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohmanef3d4572009-08-13 01:36:44 +0000795 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chenga774a992009-03-24 00:17:40 +0000796 O << "'";
797 }
798 O << '\n';
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000799
Anton Korobeynikovc9ad17c2008-08-08 18:23:49 +0000800 EmitGlobalConstant(C);
801 O << '\n';
802}
803
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000804bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
805 const TargetData *TD = TM.getTargetData();
806
807 bool isPPC64 = TD->getPointerSizeInBits() == 64;
808
809 if (isPPC64 && !TOC.empty()) {
810 // FIXME 64-bit SVR4: Use MCSection here?
811 O << "\t.section\t\".toc\",\"aw\"\n";
812
813 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
814 I != E; ++I) {
815 O << I->second << ":\n";
816 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
817 }
818 }
819
820 return AsmPrinter::doFinalization(M);
821}
Jim Laskey28663c72006-12-21 20:26:09 +0000822
Nate Begeman4bfceb12004-09-04 05:00:00 +0000823/// runOnMachineFunction - This uses the printMachineInstruction()
824/// method to print assembly for each instruction.
825///
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000826bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000827 this->MF = &MF;
828
Chris Lattner99946fb2005-11-21 07:51:23 +0000829 SetupMachineFunction(MF);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000830 O << "\n\n";
Dale Johannesen763e1102007-11-20 23:24:42 +0000831
Nate Begeman4bfceb12004-09-04 05:00:00 +0000832 // Print out constants referenced by the function
Chris Lattneref83ebd2005-11-21 08:26:15 +0000833 EmitConstantPool(MF.getConstantPool());
Nate Begeman4bfceb12004-09-04 05:00:00 +0000834
835 // Print out labels for the function.
Chris Lattner0aacd2a2005-11-14 18:52:46 +0000836 const Function *F = MF.getFunction();
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000837 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000838
Chris Lattner57575112005-12-16 00:22:14 +0000839 switch (F->getLinkage()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000840 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng1c7c0192009-01-25 06:32:01 +0000841 case Function::PrivateLinkage:
Chris Lattner57575112005-12-16 00:22:14 +0000842 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattner57575112005-12-16 00:22:14 +0000843 break;
844 case Function::ExternalLinkage:
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000845 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner57575112005-12-16 00:22:14 +0000846 break;
Duncan Sands12da8ce2009-03-07 15:45:40 +0000847 case Function::WeakAnyLinkage:
848 case Function::WeakODRLinkage:
849 case Function::LinkOnceAnyLinkage:
850 case Function::LinkOnceODRLinkage:
Dale Johannesen6bbeda42009-08-24 01:03:42 +0000851 case Function::LinkerPrivateLinkage:
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000852 O << "\t.globl\t" << CurrentFnName << '\n';
853 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Chris Lattner57575112005-12-16 00:22:14 +0000854 break;
855 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000856
Anton Korobeynikoved473292008-08-08 18:25:07 +0000857 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000858
Bill Wendling31ceb1b2009-06-30 22:38:32 +0000859 EmitAlignment(MF.getAlignment(), F);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000860 O << CurrentFnName << ":\n";
861
Jim Laskeyc0d65182006-04-07 20:44:42 +0000862 // Emit pre-function debug information.
Devang Patelf6466682009-01-08 23:40:34 +0000863 DW->BeginFunction(&MF);
Jim Laskeyc0d65182006-04-07 20:44:42 +0000864
Bill Wendling50794832008-01-26 06:51:24 +0000865 // If the function is empty, then we need to emit *something*. Otherwise, the
866 // function's label might be associated with something that it wasn't meant to
867 // be associated with. We emit a noop in this situation.
868 MachineFunction::iterator I = MF.begin();
869
Bill Wendling1a17ef02008-01-26 09:03:52 +0000870 if (++I == MF.end() && MF.front().empty())
871 O << "\tnop\n";
Bill Wendling50794832008-01-26 06:51:24 +0000872
Nate Begeman4bfceb12004-09-04 05:00:00 +0000873 // Print out code for the function.
874 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
875 I != E; ++I) {
876 // Print a label for the basic block.
Chris Lattner968aeb12005-08-21 19:09:33 +0000877 if (I != MF.begin()) {
Chris Lattner1d04aa72009-09-13 18:25:37 +0000878 EmitBasicBlockStart(I);
Chris Lattner968aeb12005-08-21 19:09:33 +0000879 }
Bill Wendling50794832008-01-26 06:51:24 +0000880 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
881 II != IE; ++II) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000882 // Print the assembly for the instruction.
Nate Begeman4bfceb12004-09-04 05:00:00 +0000883 printMachineInstruction(II);
884 }
885 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000886
Jim Laskeyb0609d92006-01-04 13:52:30 +0000887 // Emit post-function debug information.
Devang Patelf6466682009-01-08 23:40:34 +0000888 DW->EndFunction(&MF);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000889
Bill Wendling787997f2009-11-09 21:45:26 +0000890 // Print out jump tables referenced by the function.
891 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
892
Nate Begeman4bfceb12004-09-04 05:00:00 +0000893 // We didn't modify anything.
894 return false;
895}
896
Misha Brukmane05203f2004-06-21 16:55:25 +0000897
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000898void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmanbdc24ad2008-03-25 21:45:14 +0000899 static const char *const CPUDirectives[] = {
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +0000900 "",
Jim Laskey59e7a772006-12-12 20:57:08 +0000901 "ppc",
902 "ppc601",
903 "ppc602",
904 "ppc603",
905 "ppc7400",
906 "ppc750",
907 "ppc970",
908 "ppc64"
909 };
910
911 unsigned Directive = Subtarget.getDarwinDirective();
912 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
913 Directive = PPC::DIR_970;
914 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
915 Directive = PPC::DIR_7400;
916 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
917 Directive = PPC::DIR_64;
918 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000919 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000920
Jim Laskeyec05b042006-11-28 18:21:52 +0000921 // Prime text sections so they are adjacent. This reduces the likelihood a
922 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattner661710c2009-08-03 22:52:21 +0000923 TargetLoweringObjectFileMachO &TLOFMacho =
924 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000925 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskeyec05b042006-11-28 18:21:52 +0000926 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000927 OutStreamer.SwitchSection(
928 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
929 MCSectionMachO::S_SYMBOL_STUBS |
930 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
931 32, SectionKind::getText()));
Jim Laskeyec05b042006-11-28 18:21:52 +0000932 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000933 OutStreamer.SwitchSection(
934 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
935 MCSectionMachO::S_SYMBOL_STUBS |
936 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
937 16, SectionKind::getText()));
Jim Laskeyec05b042006-11-28 18:21:52 +0000938 }
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000939 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman15527112005-07-21 01:25:49 +0000940}
941
Chris Lattner100865e2009-07-21 18:38:57 +0000942void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000943 const TargetData *TD = TM.getTargetData();
944
945 if (!GVar->hasInitializer())
946 return; // External global require no code
947
948 // Check to see if this is a special global used by LLVM, if so, emit it.
949 if (EmitSpecialLLVMGlobal(GVar)) {
950 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000951 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000952 O << ".reference .constructors_used\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000953 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000954 O << ".reference .destructors_used\n";
955 }
956 return;
957 }
958
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000959 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikoved473292008-08-08 18:25:07 +0000960 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000961
962 Constant *C = GVar->getInitializer();
963 const Type *Type = C->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000964 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000965 unsigned Align = TD->getPreferredAlignmentLog(GVar);
966
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000967 const MCSection *TheSection =
Chris Lattner50343292009-07-29 05:09:30 +0000968 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000969 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000970
Chris Lattnerb58dc1c2009-08-04 05:35:56 +0000971 /// FIXME: Drive this off the section!
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000972 if (C->isNullValue() && /* FIXME: Verify correct */
973 !GVar->hasSection() &&
Rafael Espindola6de96a12009-01-15 20:18:42 +0000974 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands12da8ce2009-03-07 15:45:40 +0000975 GVar->isWeakForLinker()) &&
Chris Lattner1553fde2009-07-24 04:08:17 +0000976 // Don't put things that should go in the cstring section into "comm".
Chris Lattner149465e2009-07-27 05:32:16 +0000977 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000978 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
979
980 if (GVar->hasExternalLinkage()) {
981 O << "\t.globl " << name << '\n';
982 O << "\t.zerofill __DATA, __common, " << name << ", "
983 << Size << ", " << Align;
Rafael Espindola6de96a12009-01-15 20:18:42 +0000984 } else if (GVar->hasLocalLinkage()) {
Chris Lattnere9a75a62009-08-22 21:43:10 +0000985 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000986 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000987 O << "\t.globl " << name << '\n'
Chris Lattnere9a75a62009-08-22 21:43:10 +0000988 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000989 EmitAlignment(Align, GVar);
Evan Chenga774a992009-03-24 00:17:40 +0000990 O << name << ":";
991 if (VerboseAsm) {
Chris Lattnere9a75a62009-08-22 21:43:10 +0000992 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohmanef3d4572009-08-13 01:36:44 +0000993 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chenga774a992009-03-24 00:17:40 +0000994 }
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000995 O << '\n';
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +0000996 EmitGlobalConstant(C);
997 return;
998 } else {
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +0000999 O << ".comm " << name << ',' << Size;
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001000 // Darwin 9 and above support aligned common data.
1001 if (Subtarget.isDarwin9())
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +00001002 O << ',' << Align;
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001003 }
Evan Chenga774a992009-03-24 00:17:40 +00001004 if (VerboseAsm) {
Chris Lattnere9a75a62009-08-22 21:43:10 +00001005 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohmanef3d4572009-08-13 01:36:44 +00001006 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chenga774a992009-03-24 00:17:40 +00001007 O << "'";
1008 }
1009 O << '\n';
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001010 return;
1011 }
1012
1013 switch (GVar->getLinkage()) {
Duncan Sands12da8ce2009-03-07 15:45:40 +00001014 case GlobalValue::LinkOnceAnyLinkage:
1015 case GlobalValue::LinkOnceODRLinkage:
1016 case GlobalValue::WeakAnyLinkage:
1017 case GlobalValue::WeakODRLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00001018 case GlobalValue::CommonLinkage:
Dale Johannesen6bbeda42009-08-24 01:03:42 +00001019 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001020 O << "\t.globl " << name << '\n'
1021 << "\t.weak_definition " << name << '\n';
1022 break;
1023 case GlobalValue::AppendingLinkage:
1024 // FIXME: appending linkage variables should go into a section of
1025 // their name or something. For now, just emit them as external.
1026 case GlobalValue::ExternalLinkage:
1027 // If external or appending, declare as a global symbol
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +00001028 O << "\t.globl " << name << '\n';
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001029 // FALL THROUGH
1030 case GlobalValue::InternalLinkage:
Evan Cheng1c7c0192009-01-25 06:32:01 +00001031 case GlobalValue::PrivateLinkage:
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001032 break;
1033 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001034 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001035 }
1036
1037 EmitAlignment(Align, GVar);
Evan Chenga774a992009-03-24 00:17:40 +00001038 O << name << ":";
1039 if (VerboseAsm) {
Chris Lattnere9a75a62009-08-22 21:43:10 +00001040 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohmanef3d4572009-08-13 01:36:44 +00001041 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chenga774a992009-03-24 00:17:40 +00001042 O << "'";
1043 }
1044 O << '\n';
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001045
Anton Korobeynikov5b5d8bc2008-08-08 18:23:25 +00001046 EmitGlobalConstant(C);
1047 O << '\n';
1048}
1049
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +00001050bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001051 const TargetData *TD = TM.getTargetData();
Misha Brukmane05203f2004-06-21 16:55:25 +00001052
Chris Lattner1df08392006-06-27 01:02:25 +00001053 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1054
Chris Lattner661710c2009-08-03 22:52:21 +00001055 // Darwin/PPC always uses mach-o.
1056 TargetLoweringObjectFileMachO &TLOFMacho =
1057 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1058
Chris Lattnercb307a272009-08-10 01:39:42 +00001059
1060 const MCSection *LSPSection = 0;
1061 if (!FnStubs.empty()) // .lazy_symbol_pointer
1062 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1063
1064
Misha Brukmand4ac8182004-07-16 20:29:04 +00001065 // Output stubs for dynamically-linked functions
Chris Lattner0b1ca0a2009-07-15 00:55:58 +00001066 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner661710c2009-08-03 22:52:21 +00001067 const MCSection *StubSection =
Chris Lattnercb307a272009-08-10 01:39:42 +00001068 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1069 MCSectionMachO::S_SYMBOL_STUBS |
1070 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1071 32, SectionKind::getText());
Chris Lattner9148f362010-01-13 19:00:57 +00001072 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001073 I != E; ++I) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001074 OutStreamer.SwitchSection(StubSection);
Chris Lattner1df08392006-06-27 01:02:25 +00001075 EmitAlignment(4);
Chris Lattner9a066ca2009-07-15 02:33:19 +00001076 const FnStubInfo &Info = I->second;
Chris Lattner9148f362010-01-13 19:00:57 +00001077 Info.printStub(O, MAI);
1078 O << ":\n";
Chris Lattner9a066ca2009-07-15 02:33:19 +00001079 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner54a11df2005-12-13 04:33:58 +00001080 O << "\tmflr r0\n";
Chris Lattner9148f362010-01-13 19:00:57 +00001081 O << "\tbcl 20,31,";
1082 Info.printAnonSymbol(O, MAI);
1083 O << '\n';
1084 Info.printAnonSymbol(O, MAI);
1085 O << ":\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001086 O << "\tmflr r11\n";
Chris Lattner9148f362010-01-13 19:00:57 +00001087 O << "\taddis r11,r11,ha16(";
1088 Info.printLazyPtr(O, MAI);
1089 O << '-';
1090 Info.printAnonSymbol(O, MAI);
Evan Cheng2a03c7e2008-12-05 01:06:39 +00001091 O << ")\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001092 O << "\tmtlr r0\n";
Chris Lattner7d1f9542009-07-15 02:56:53 +00001093 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner9148f362010-01-13 19:00:57 +00001094 Info.printLazyPtr(O, MAI);
1095 O << '-';
1096 Info.printAnonSymbol(O, MAI);
1097 O << ")(r11)\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001098 O << "\tmtctr r12\n";
1099 O << "\tbctr\n";
Chris Lattner86085a52009-07-16 01:23:26 +00001100
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001101 OutStreamer.SwitchSection(LSPSection);
Chris Lattner9148f362010-01-13 19:00:57 +00001102 Info.printLazyPtr(O, MAI);
1103 O << ":\n";
Chris Lattner9a066ca2009-07-15 02:33:19 +00001104 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerd68df2a2009-07-15 02:36:21 +00001105 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001106 }
Chris Lattner0b1ca0a2009-07-15 00:55:58 +00001107 } else if (!FnStubs.empty()) {
Chris Lattnercb307a272009-08-10 01:39:42 +00001108 const MCSection *StubSection =
1109 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1110 MCSectionMachO::S_SYMBOL_STUBS |
1111 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1112 16, SectionKind::getText());
Chris Lattner661710c2009-08-03 22:52:21 +00001113
Chris Lattnere8382ac2009-07-15 02:28:57 +00001114 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001115 I != E; ++I) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001116 OutStreamer.SwitchSection(StubSection);
Chris Lattner54a11df2005-12-13 04:33:58 +00001117 EmitAlignment(4);
Chris Lattner9a066ca2009-07-15 02:33:19 +00001118 const FnStubInfo &Info = I->second;
Chris Lattner9148f362010-01-13 19:00:57 +00001119 Info.printStub(O, MAI);
1120 O << ":\n";
Chris Lattner9a066ca2009-07-15 02:33:19 +00001121 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner9148f362010-01-13 19:00:57 +00001122 O << "\tlis r11,ha16(";
1123 Info.printLazyPtr(O, MAI);
1124 O << ")\n";
Chris Lattnerd68df2a2009-07-15 02:36:21 +00001125 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner9148f362010-01-13 19:00:57 +00001126 Info.printLazyPtr(O, MAI);
1127 O << ")(r11)\n";
Chris Lattner54a11df2005-12-13 04:33:58 +00001128 O << "\tmtctr r12\n";
1129 O << "\tbctr\n";
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001130 OutStreamer.SwitchSection(LSPSection);
Chris Lattner9148f362010-01-13 19:00:57 +00001131 Info.printLazyPtr(O, MAI);
1132 O << ":\n";
Chris Lattner9a066ca2009-07-15 02:33:19 +00001133 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerd68df2a2009-07-15 02:36:21 +00001134 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Nate Begemana9443f22005-07-21 20:44:43 +00001135 }
Misha Brukmanf62ee7a2004-06-24 23:04:11 +00001136 }
Misha Brukmane05203f2004-06-21 16:55:25 +00001137
Anton Korobeynikovf1f8aa32008-08-08 18:24:10 +00001138 O << '\n';
Misha Brukmand4ac8182004-07-16 20:29:04 +00001139
Chris Lattnere9a75a62009-08-22 21:43:10 +00001140 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen763e1102007-11-20 23:24:42 +00001141 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesenfd967cf2008-04-02 00:25:04 +00001142 // Only referenced functions get into the Personalities list.
Chris Lattner1fd58882009-06-24 19:09:55 +00001143 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen763e1102007-11-20 23:24:42 +00001144 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001145 E = Personalities.end(); I != E; ++I) {
1146 if (*I)
1147 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner8da3ce32009-07-15 01:16:38 +00001148 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001149 }
Dale Johannesen763e1102007-11-20 23:24:42 +00001150 }
1151
Chris Lattner661710c2009-08-03 22:52:21 +00001152 // Output macho stubs for external and common global variables.
Dan Gohmanc731c972007-10-03 19:26:29 +00001153 if (!GVStubs.empty()) {
Chris Lattnercb307a272009-08-10 01:39:42 +00001154 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001155 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerec64b732009-08-10 17:58:51 +00001156 EmitAlignment(isPPC64 ? 3 : 2);
1157
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001158 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1159 E = GVStubs.end(); I != E; ++I) {
1160 O << I->second << ":\n";
1161 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1162 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Chris Lattner54a11df2005-12-13 04:33:58 +00001163 }
Nate Begeman0ad7f812004-08-14 22:09:10 +00001164 }
Misha Brukmanb4402432005-04-21 23:30:14 +00001165
Evan Cheng2a03c7e2008-12-05 01:06:39 +00001166 if (!HiddenGVStubs.empty()) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001167 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001168 EmitAlignment(isPPC64 ? 3 : 2);
1169 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1170 E = HiddenGVStubs.end(); I != E; ++I) {
1171 O << I->second << ":\n";
1172 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Cheng2a03c7e2008-12-05 01:06:39 +00001173 }
1174 }
1175
Chris Lattner7432ceef2005-11-01 00:12:36 +00001176 // Funny Darwin hack: This flag tells the linker that no global symbols
1177 // contain code that falls through to other global symbols (e.g. the obvious
1178 // implementation of multiple entry points). If this doesn't occur, the
1179 // linker can safely perform dead code stripping. Since LLVM never generates
1180 // code that does this, it is always safe to set.
Chris Lattnere6da1822009-10-19 18:03:08 +00001181 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Chris Lattner7432ceef2005-11-01 00:12:36 +00001182
Dan Gohmancf0a5342007-07-25 19:33:14 +00001183 return AsmPrinter::doFinalization(M);
Misha Brukmane05203f2004-06-21 16:55:25 +00001184}
Nate Begeman4bfceb12004-09-04 05:00:00 +00001185
Chris Lattnera81a75c2006-09-20 17:12:19 +00001186
1187
Jim Laskey41621a72006-12-20 20:56:46 +00001188/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1189/// for a MachineFunction to the given output stream, in a format that the
Chris Lattnera81a75c2006-09-20 17:12:19 +00001190/// Darwin assembler can deal with.
1191///
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +00001192static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1193 TargetMachine &tm,
Chris Lattner7b26fce2009-08-22 20:48:53 +00001194 const MCAsmInfo *tai,
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +00001195 bool verbose) {
Jim Laskey28663c72006-12-21 20:26:09 +00001196 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1197
Chris Lattner100865e2009-07-21 18:38:57 +00001198 if (Subtarget->isDarwin())
Daniel Dunbar95f58462009-08-13 19:38:51 +00001199 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1200 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Chris Lattnera81a75c2006-09-20 17:12:19 +00001201}
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +00001202
Bob Wilson5a495fe2009-06-23 23:59:40 +00001203// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +00001204extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbare8338102009-07-15 20:24:03 +00001205 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbare8338102009-07-15 20:24:03 +00001206 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1207}