blob: da29572676153c26983554917b4637042387fc48 [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"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Misha Brukman05794492004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling381802f2008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattner0b4bad52010-01-13 19:00:57 +000035#include "llvm/MC/MCContext.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000036#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000037#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000038#include "llvm/MC/MCSymbol.h"
Chris Lattnera87dea42009-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 Brukman5dfe3a92004-06-21 16:55:25 +000044#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000045#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000046#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000048#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000049#include "llvm/Support/FormattedStream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000050#include "llvm/ADT/Statistic.h"
51#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 +000056STATISTIC(EmittedInsts, "Number of machine instrs printed");
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057
Chris Lattner95b2c7d2006-12-19 22:59:26 +000058namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000059 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000060 protected:
Chris Lattner9ec8fac2009-07-15 02:28:57 +000061 struct FnStubInfo {
Chris Lattner0b4bad52010-01-13 19:00:57 +000062 std::string StubName, LazyPtrName, AnonSymbolName;
63 MCSymbol *StubSym, *LazyPtrSym, *AnonSymbolSym;
Chris Lattner9ec8fac2009-07-15 02:28:57 +000064
Chris Lattner0b4bad52010-01-13 19:00:57 +000065 FnStubInfo() {
66 StubSym = LazyPtrSym = AnonSymbolSym = 0;
67 }
Chris Lattner9ec8fac2009-07-15 02:28:57 +000068
69 void Init(const GlobalValue *GV, Mangler *Mang) {
70 // Already initialized.
Chris Lattner0b4bad52010-01-13 19:00:57 +000071 if (!StubName.empty()) return;
Chris Lattner59b34fe2010-01-13 19:05:36 +000072
73 // Get the names.
Chris Lattner0b4bad52010-01-13 19:00:57 +000074 StubName = Mang->getMangledName(GV, "$stub", true);
75 LazyPtrName = Mang->getMangledName(GV, "$lazy_ptr", true);
76 AnonSymbolName = Mang->getMangledName(GV, "$stub$tmp", true);
Chris Lattner9ec8fac2009-07-15 02:28:57 +000077 }
78
Chris Lattner0b4bad52010-01-13 19:00:57 +000079 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattneree9250b2010-01-13 07:56:59 +000080 assert(!GVName.empty());
Chris Lattner0b4bad52010-01-13 19:00:57 +000081 if (StubSym != 0) return; // Already initialized.
Chris Lattneree9250b2010-01-13 07:56:59 +000082 // Get the names for the external symbol name.
Chris Lattner48130352010-01-13 06:38:18 +000083 SmallString<128> TmpStr;
Chris Lattneree9250b2010-01-13 07:56:59 +000084 Mang->getNameWithPrefix(TmpStr, GVName + "$stub", Mangler::Private);
Chris Lattner0b4bad52010-01-13 19:00:57 +000085 StubSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner59b34fe2010-01-13 19:05:36 +000086 TmpStr.erase(TmpStr.end()-5, TmpStr.end()); // Remove $stub
87
88 TmpStr += "$lazy_ptr";
Chris Lattner0b4bad52010-01-13 19:00:57 +000089 LazyPtrSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner59b34fe2010-01-13 19:05:36 +000090 TmpStr.erase(TmpStr.end()-9, TmpStr.end()); // Remove $lazy_ptr
Chris Lattner48130352010-01-13 06:38:18 +000091
Chris Lattner59b34fe2010-01-13 19:05:36 +000092 TmpStr += "$stub$tmp";
Chris Lattner0b4bad52010-01-13 19:00:57 +000093 AnonSymbolSym = Ctx.GetOrCreateSymbol(TmpStr.str());
94 }
95
96 void printStub(raw_ostream &OS, const MCAsmInfo *MAI) const {
97 if (StubSym)
98 StubSym->print(OS, MAI);
99 else
100 OS << StubName;
101 }
102 void printLazyPtr(raw_ostream &OS, const MCAsmInfo *MAI) const {
103 if (LazyPtrSym)
104 LazyPtrSym->print(OS, MAI);
105 else
106 OS << LazyPtrName;
107 }
108 void printAnonSymbol(raw_ostream &OS, const MCAsmInfo *MAI) const {
109 if (AnonSymbolSym)
110 AnonSymbolSym->print(OS, MAI);
111 else
112 OS << AnonSymbolName;
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000113 }
114 };
115
116 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000117 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Chris Lattnerdadceed2006-09-20 17:07:15 +0000118 const PPCSubtarget &Subtarget;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000119 uint64_t LabelID;
Bill Wendling57f0db82009-02-24 08:30:20 +0000120 public:
David Greene71847812009-07-14 20:18:05 +0000121 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +0000122 const MCAsmInfo *T, bool V)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000123 : AsmPrinter(O, TM, T, V),
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000124 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000125
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000126 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +0000127 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000128 }
129
Nate Begeman21e463b2005-10-16 05:39:50 +0000130 PPCTargetMachine &getTM() {
131 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +0000132 }
133
Nate Begemanadeb43d2005-07-20 22:42:00 +0000134 unsigned enumRegToMachineReg(unsigned enumReg) {
135 switch (enumReg) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000136 default: llvm_unreachable("Unhandled register!");
Nate Begemanadeb43d2005-07-20 22:42:00 +0000137 case PPC::CR0: return 0;
138 case PPC::CR1: return 1;
139 case PPC::CR2: return 2;
140 case PPC::CR3: return 3;
141 case PPC::CR4: return 4;
142 case PPC::CR5: return 5;
143 case PPC::CR6: return 6;
144 case PPC::CR7: return 7;
145 }
Torok Edwinc23197a2009-07-14 16:55:14 +0000146 llvm_unreachable(0);
Nate Begemanadeb43d2005-07-20 22:42:00 +0000147 }
148
Nate Begemane59bf592004-08-14 22:09:10 +0000149 /// printInstruction - This method is automatically generated by tablegen
150 /// from the instruction set description. This method returns true if the
151 /// machine instruction was sufficiently described to print it, otherwise it
152 /// returns false.
Chris Lattner41aefdc2009-08-08 01:32:19 +0000153 void printInstruction(const MachineInstr *MI);
Chris Lattnerd95148f2009-09-13 20:19:22 +0000154 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +0000155
Nate Begemane59bf592004-08-14 22:09:10 +0000156
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000157 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +0000158 void printOp(const MachineOperand &MO);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000159
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000160 /// stripRegisterPrefix - This method strips the character prefix from a
161 /// register name so that only the number is left. Used by for linux asm.
Jim Laskey40ee69f2006-12-20 21:33:34 +0000162 const char *stripRegisterPrefix(const char *RegName) {
163 switch (RegName[0]) {
164 case 'r':
165 case 'f':
166 case 'v': return RegName + 1;
Jim Laskey2aa14aa2006-12-20 21:35:00 +0000167 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000168 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000169
Jim Laskey40ee69f2006-12-20 21:33:34 +0000170 return RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000171 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000172
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000173 /// printRegister - Print register according to target requirements.
174 ///
175 void printRegister(const MachineOperand &MO, bool R0AsZero) {
176 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000177 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000178
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000179 // If we should use 0 for R0.
180 if (R0AsZero && RegNo == PPC::R0) {
181 O << "0";
182 return;
183 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000184
Chris Lattner762ccea2009-09-13 20:31:40 +0000185 const char *RegName = getRegisterName(RegNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000186 // Linux assembler (Others?) does not take register mnemonics.
187 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskey40ee69f2006-12-20 21:33:34 +0000188 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
189 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000190 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000191
Chris Lattner58873272006-02-01 22:38:46 +0000192 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000193 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmand735b802008-10-03 15:45:36 +0000194 if (MO.isReg()) {
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000195 printRegister(MO, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000196 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000197 O << MO.getImm();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000198 } else {
199 printOp(MO);
200 }
201 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000202
Chris Lattner58873272006-02-01 22:38:46 +0000203 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000204 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000205 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
206 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000207
208
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000209 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000210 char value = MI->getOperand(OpNo).getImm();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000211 value = (value << (32-5)) >> (32-5);
212 O << (int)value;
213 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000214 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000215 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattner12585ba2004-08-21 19:11:03 +0000216 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000217 O << (unsigned int)value;
218 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000219 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000220 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman07aada82004-08-30 02:28:06 +0000221 assert(value <= 63 && "Invalid u6imm argument!");
222 O << (unsigned int)value;
223 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000224 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000225 O << (short)MI->getOperand(OpNo).getImm();
Nate Begemaned428532004-09-04 05:00:00 +0000226 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000227 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000228 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000229 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000230 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000231 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000232 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000233 } else {
234 O << "lo16(";
235 printOp(MI->getOperand(OpNo));
236 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000237 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000238 else
239 O << ')';
240 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000241 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000242 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000243 // Branches can take an immediate operand. This is used by the branch
244 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmand735b802008-10-03 15:45:36 +0000245 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000246 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000247 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000248 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000249 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000250 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000251 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000252 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000253 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000254 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
255 GlobalValue *GV = MO.getGlobal();
Chris Lattner20674332009-07-02 16:08:53 +0000256 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattnera637c322005-12-16 00:22:14 +0000257 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000258 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
259 FnInfo.Init(GV, Mang);
Chris Lattner0b4bad52010-01-13 19:00:57 +0000260 FnInfo.printStub(O, MAI);
Chris Lattnera637c322005-12-16 00:22:14 +0000261 return;
262 }
263 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000264 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner48130352010-01-13 06:38:18 +0000265 SmallString<128> MangledName;
Chris Lattneree9250b2010-01-13 07:56:59 +0000266 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattner48130352010-01-13 06:38:18 +0000267 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner0b4bad52010-01-13 19:00:57 +0000268 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
269 FnInfo.printStub(O, MAI);
Chris Lattner9542f9712005-11-17 19:40:30 +0000270 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000271 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000272 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000273
Chris Lattner9542f9712005-11-17 19:40:30 +0000274 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000275 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000276 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000277 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begeman422b0ce2005-11-16 00:48:01 +0000278 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000279 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000280 O << "\"L" << getFunctionNumber() << "$pb\"\n";
281 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000282 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000283 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000284 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000285 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000286 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000287 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begeman2497e632005-07-21 20:44:43 +0000288 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000289 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000290 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000291 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000292 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000293 else
294 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000295 }
Nate Begemaned428532004-09-04 05:00:00 +0000296 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000297 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000298 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000299 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000300 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000301 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000302 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000303 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000304 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000305 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000306 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000307 else
308 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000309 }
310 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000311 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000312 unsigned CCReg = MI->getOperand(OpNo).getReg();
313 unsigned RegNo = enumRegToMachineReg(CCReg);
314 O << (0x80 >> RegNo);
315 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000316 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000317 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
318 printSymbolLo(MI, OpNo);
319 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000320 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner13feb582006-03-21 17:21:13 +0000321 MI->getOperand(OpNo+1).getReg() == PPC::R0)
322 O << "0";
323 else
324 printOperand(MI, OpNo+1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000325 O << ')';
326 }
Chris Lattnere5ba5802006-03-22 05:26:03 +0000327 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000328 if (MI->getOperand(OpNo).isImm())
Chris Lattnere5ba5802006-03-22 05:26:03 +0000329 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000330 else
Chris Lattnere5ba5802006-03-22 05:26:03 +0000331 printSymbolLo(MI, OpNo);
332 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000333 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattnere5ba5802006-03-22 05:26:03 +0000334 MI->getOperand(OpNo+1).getReg() == PPC::R0)
335 O << "0";
336 else
337 printOperand(MI, OpNo+1);
338 O << ')';
339 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000340
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000341 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000342 // When used as the base register, r0 reads constant zero rather than
343 // the value contained in the register. For this reason, the darwin
344 // assembler requires that we print r0 as 0 (no r) when used as the base.
345 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000346 printRegister(MO, true);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000347 O << ", ";
348 printOperand(MI, OpNo+1);
349 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000350
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000351 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
352 const MachineOperand &MO = MI->getOperand(OpNo);
353
354 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
355
356 GlobalValue *GV = MO.getGlobal();
357
358 std::string Name = Mang->getMangledName(GV);
359
360 // Map symbol -> label of TOC entry.
361 if (TOC.count(Name) == 0) {
362 std::string Label;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000363 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000364 Label += "C";
365 Label += utostr(LabelID++);
366
367 TOC[Name] = Label;
368 }
369
370 O << TOC[Name] << "@toc";
371 }
372
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000373 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000374 const char *Modifier);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000375
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000376 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000377 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000378
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000379 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000380 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000381 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000382 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +0000383 const MCAsmInfo *T, bool V)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000384 : PPCAsmPrinter(O, TM, T, V){}
Jim Laskeybf111822006-12-21 20:26:09 +0000385
386 virtual const char *getPassName() const {
387 return "Linux PPC Assembly Printer";
388 }
389
390 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000391 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000392
Jim Laskeybf111822006-12-21 20:26:09 +0000393 void getAnalysisUsage(AnalysisUsage &AU) const {
394 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000395 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000396 AU.addRequired<DwarfWriter>();
Jim Laskeybf111822006-12-21 20:26:09 +0000397 PPCAsmPrinter::getAnalysisUsage(AU);
398 }
399
Chris Lattner40bbebd2009-07-21 18:38:57 +0000400 void PrintGlobalVariable(const GlobalVariable *GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000401 };
402
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000403 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
404 /// OS X
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000405 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene71847812009-07-14 20:18:05 +0000406 formatted_raw_ostream &OS;
Bill Wendling57f0db82009-02-24 08:30:20 +0000407 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000408 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +0000409 const MCAsmInfo *T, bool V)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000410 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Nate Begemaned428532004-09-04 05:00:00 +0000411
412 virtual const char *getPassName() const {
413 return "Darwin PPC Assembly Printer";
414 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000415
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000416 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000417 bool doFinalization(Module &M);
Bob Wilson812209a2009-09-30 22:06:26 +0000418 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000419
Jim Laskeyb2efb852006-01-04 22:28:25 +0000420 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000421 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000422 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000423 AU.addRequired<DwarfWriter>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000424 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000425 }
426
Chris Lattner40bbebd2009-07-21 18:38:57 +0000427 void PrintGlobalVariable(const GlobalVariable *GVar);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428 };
429} // end of anonymous namespace
430
Nate Begemane59bf592004-08-14 22:09:10 +0000431// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000432#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000433
Chris Lattner9ba13e42005-11-17 19:25:59 +0000434void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000435 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000436 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000437 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman97a296f2004-07-21 20:11:11 +0000438
Nate Begeman37efe672006-04-22 18:53:45 +0000439 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000440 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000441 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000442 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000443 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000444 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000445 // FIXME: PIC relocation model
446 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000447 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000448 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000449 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000450 return;
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000451 case MachineOperand::MO_BlockAddress:
452 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
453 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000454 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000455 // Computing the address of an external symbol, not calling it.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000456 std::string Name(MAI->getGlobalPrefix());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000457 Name += MO.getSymbolName();
458
Evan Cheng4c1aa862006-02-22 20:19:42 +0000459 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000460 GVStubs[Name] = Name+"$non_lazy_ptr";
461 Name += "$non_lazy_ptr";
Chris Lattnera637c322005-12-16 00:22:14 +0000462 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000463 O << Name;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000464 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000465 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000466 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000467 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000468 GlobalValue *GV = MO.getGlobal();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000469 std::string Name;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000470
Nate Begemanfcf4a422004-10-17 23:01:34 +0000471 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000472 if (TM.getRelocationModel() != Reloc::Static &&
473 (GV->isDeclaration() || GV->isWeakForLinker())) {
474 if (!GV->hasHiddenVisibility()) {
475 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
476 GVStubs[Mang->getMangledName(GV)] = Name;
477 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
478 GV->hasAvailableExternallyLinkage()) {
479 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
480 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
481 } else {
482 Name = Mang->getMangledName(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000483 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000484 } else {
485 Name = Mang->getMangledName(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000486 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000487 O << Name;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000488
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000489 printOffset(MO.getOffset());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000490 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000491 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000492
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000493 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000494 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000495 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000496 }
497}
498
Chris Lattnere3f01572006-02-23 19:31:10 +0000499/// PrintAsmOperand - Print out an operand for an inline asm expression.
500///
501bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000502 unsigned AsmVariant,
Chris Lattnere3f01572006-02-23 19:31:10 +0000503 const char *ExtraCode) {
504 // Does this asm operand have a single letter operand modifier?
505 if (ExtraCode && ExtraCode[0]) {
506 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000507
Chris Lattnere3f01572006-02-23 19:31:10 +0000508 switch (ExtraCode[0]) {
509 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000510 case 'c': // Don't print "$" before a global var name or constant.
511 // PPC never has a prefix.
512 printOperand(MI, OpNo);
513 return false;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000514 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000515 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000516 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000517 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000518 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000519 return true;
520 ++OpNo; // Return the high-part.
521 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000522 case 'I':
523 // Write 'i' if an integer constant, otherwise nothing. Used to print
524 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000525 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000526 O << "i";
527 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000528 }
529 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000530
Chris Lattnere3f01572006-02-23 19:31:10 +0000531 printOperand(MI, OpNo);
532 return false;
533}
534
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000535// At the moment, all inline asm memory operands are a single register.
536// In any case, the output of this routine should always be just one
537// assembler operand.
538
Chris Lattner2c003e22006-02-24 20:27:40 +0000539bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000540 unsigned AsmVariant,
Chris Lattner2c003e22006-02-24 20:27:40 +0000541 const char *ExtraCode) {
542 if (ExtraCode && ExtraCode[0])
543 return true; // Unknown modifier.
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000544 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen4e68f882009-08-26 18:10:32 +0000545 O << "0(";
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000546 printOperand(MI, OpNo);
Dale Johannesen4e68f882009-08-26 18:10:32 +0000547 O << ")";
Chris Lattner2c003e22006-02-24 20:27:40 +0000548 return false;
549}
Chris Lattnere3f01572006-02-23 19:31:10 +0000550
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000551void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000552 const char *Modifier) {
553 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
554 unsigned Code = MI->getOperand(OpNo).getImm();
555 if (!strcmp(Modifier, "cc")) {
556 switch ((PPC::Predicate)Code) {
557 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
558 case PPC::PRED_LT: O << "lt"; return;
559 case PPC::PRED_LE: O << "le"; return;
560 case PPC::PRED_EQ: O << "eq"; return;
561 case PPC::PRED_GE: O << "ge"; return;
562 case PPC::PRED_GT: O << "gt"; return;
563 case PPC::PRED_NE: O << "ne"; return;
564 case PPC::PRED_UN: O << "un"; return;
565 case PPC::PRED_NU: O << "nu"; return;
566 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000567
Chris Lattneraf53a872006-11-04 05:27:39 +0000568 } else {
569 assert(!strcmp(Modifier, "reg") &&
570 "Need to specify 'cc' or 'reg' as predicate op modifier!");
571 // Don't print the register for 'always'.
572 if (Code == PPC::PRED_ALWAYS) return;
573 printOperand(MI, OpNo+1);
574 }
575}
576
577
Nate Begemane59bf592004-08-14 22:09:10 +0000578/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
579/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000580///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000581void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000582 ++EmittedInsts;
Chris Lattner634cca32009-09-09 20:34:59 +0000583
Devang Patelaf0e2722009-10-06 02:19:11 +0000584 processDebugLoc(MI, true);
Chris Lattner617742b2005-10-14 22:44:13 +0000585
Nate Begemanad5f65c2005-04-05 18:19:50 +0000586 // Check for slwi/srwi mnemonics.
Dale Johannesene89c5102010-01-06 02:20:18 +0000587 bool useSubstituteMnemonic = false;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000588 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000589 unsigned char SH = MI->getOperand(2).getImm();
590 unsigned char MB = MI->getOperand(3).getImm();
591 unsigned char ME = MI->getOperand(4).getImm();
Nate Begemanad5f65c2005-04-05 18:19:50 +0000592 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000593 O << "\tslwi "; useSubstituteMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000594 }
595 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000596 O << "\tsrwi "; useSubstituteMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000597 SH = 32-SH;
598 }
Dale Johannesene89c5102010-01-06 02:20:18 +0000599 if (useSubstituteMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000600 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000601 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000602 printOperand(MI, 1);
Dale Johannesene89c5102010-01-06 02:20:18 +0000603 O << ", " << (unsigned int)SH;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000604 }
Chris Lattnerb410dc92006-06-20 23:18:58 +0000605 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000606 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000607 useSubstituteMnemonic = true;
Nate Begeman5a804e32008-02-05 08:49:09 +0000608 O << "\tmr ";
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000609 printOperand(MI, 0);
610 O << ", ";
611 printOperand(MI, 1);
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000612 }
Chris Lattner566c1b12006-11-18 01:23:56 +0000613 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000614 unsigned char SH = MI->getOperand(2).getImm();
615 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner566c1b12006-11-18 01:23:56 +0000616 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
617 if (63-SH == ME) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000618 useSubstituteMnemonic = true;
Nate Begeman5a804e32008-02-05 08:49:09 +0000619 O << "\tsldi ";
Chris Lattner566c1b12006-11-18 01:23:56 +0000620 printOperand(MI, 0);
621 O << ", ";
622 printOperand(MI, 1);
Dale Johannesene89c5102010-01-06 02:20:18 +0000623 O << ", " << (unsigned int)SH;
Chris Lattner566c1b12006-11-18 01:23:56 +0000624 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000625 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000626
Dale Johannesene89c5102010-01-06 02:20:18 +0000627 if (!useSubstituteMnemonic)
628 printInstruction(MI);
629
David Greene1924aab2009-11-13 21:34:57 +0000630 if (VerboseAsm)
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000631 EmitComments(*MI);
632 O << '\n';
Devang Patelaf0e2722009-10-06 02:19:11 +0000633
634 processDebugLoc(MI, false);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000635}
636
Jim Laskeybf111822006-12-21 20:26:09 +0000637/// runOnMachineFunction - This uses the printMachineInstruction()
638/// method to print assembly for each instruction.
639///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000640bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000641 this->MF = &MF;
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000642
Jim Laskeybf111822006-12-21 20:26:09 +0000643 SetupMachineFunction(MF);
644 O << "\n\n";
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000645
Jim Laskeybf111822006-12-21 20:26:09 +0000646 // Print out constants referenced by the function
647 EmitConstantPool(MF.getConstantPool());
648
649 // Print out labels for the function.
650 const Function *F = MF.getFunction();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000651 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000652
Jim Laskeybf111822006-12-21 20:26:09 +0000653 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000654 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000655 case Function::PrivateLinkage:
Jim Laskeybf111822006-12-21 20:26:09 +0000656 case Function::InternalLinkage: // Symbols default to internal.
657 break;
658 case Function::ExternalLinkage:
659 O << "\t.global\t" << CurrentFnName << '\n'
660 << "\t.type\t" << CurrentFnName << ", @function\n";
661 break;
Dale Johannesena60e51f2009-08-24 01:03:42 +0000662 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000663 case Function::WeakAnyLinkage:
664 case Function::WeakODRLinkage:
665 case Function::LinkOnceAnyLinkage:
666 case Function::LinkOnceODRLinkage:
Jim Laskeybf111822006-12-21 20:26:09 +0000667 O << "\t.global\t" << CurrentFnName << '\n';
668 O << "\t.weak\t" << CurrentFnName << '\n';
669 break;
670 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000671
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000672 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000673
Bill Wendling20c568f2009-06-30 22:38:32 +0000674 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000675
676 if (Subtarget.isPPC64()) {
677 // Emit an official procedure descriptor.
678 // FIXME 64-bit SVR4: Use MCSection here?
679 O << "\t.section\t\".opd\",\"aw\"\n";
680 O << "\t.align 3\n";
681 O << CurrentFnName << ":\n";
682 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
683 O << "\t.previous\n";
684 O << ".L." << CurrentFnName << ":\n";
685 } else {
686 O << CurrentFnName << ":\n";
687 }
Jim Laskeybf111822006-12-21 20:26:09 +0000688
689 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000690 DW->BeginFunction(&MF);
Jim Laskeybf111822006-12-21 20:26:09 +0000691
692 // Print out code for the function.
693 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
694 I != E; ++I) {
695 // Print a label for the basic block.
696 if (I != MF.begin()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000697 EmitBasicBlockStart(I);
Jim Laskeybf111822006-12-21 20:26:09 +0000698 }
699 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
700 II != E; ++II) {
701 // Print the assembly for the instruction.
Jim Laskeybf111822006-12-21 20:26:09 +0000702 printMachineInstruction(II);
703 }
704 }
705
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000706 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Jim Laskeybf111822006-12-21 20:26:09 +0000707
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000708 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesenf94a3272008-12-03 19:33:10 +0000709
Jim Laskeybf111822006-12-21 20:26:09 +0000710 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000711 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000712
Bill Wendlingb1ec31d2009-11-09 21:20:14 +0000713 // Print out jump tables referenced by the function.
714 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
715
Jim Laskeybf111822006-12-21 20:26:09 +0000716 // We didn't modify anything.
717 return false;
718}
719
Chris Lattner40bbebd2009-07-21 18:38:57 +0000720void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Jim Laskeybf111822006-12-21 20:26:09 +0000721 const TargetData *TD = TM.getTargetData();
722
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000723 if (!GVar->hasInitializer())
724 return; // External global require no code
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000725
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000726 // Check to see if this is a special global used by LLVM, if so, emit it.
727 if (EmitSpecialLLVMGlobal(GVar))
728 return;
Chris Lattner70d41072007-01-14 06:37:54 +0000729
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000730 std::string name = Mang->getMangledName(GVar);
Chris Lattner70d41072007-01-14 06:37:54 +0000731
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000732 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000733
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000734 Constant *C = GVar->getInitializer();
735 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000736 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000737 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000738
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000739 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
740 TM));
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000741
742 if (C->isNullValue() && /* FIXME: Verify correct */
743 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000744 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000745 GVar->isWeakForLinker())) {
Jim Laskeybf111822006-12-21 20:26:09 +0000746 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000747
748 if (GVar->hasExternalLinkage()) {
Jim Laskeybf111822006-12-21 20:26:09 +0000749 O << "\t.global " << name << '\n';
750 O << "\t.type " << name << ", @object\n";
Nick Lewyckye2b90522007-07-25 03:48:45 +0000751 O << name << ":\n";
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000752 O << "\t.zero " << Size << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000753 } else if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000754 O << MAI->getLCOMMDirective() << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000755 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000756 O << ".comm " << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000757 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000758 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000759 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000760 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000761 O << "'";
762 }
763 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000764 return;
Jim Laskeybf111822006-12-21 20:26:09 +0000765 }
766
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000767 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000768 case GlobalValue::LinkOnceAnyLinkage:
769 case GlobalValue::LinkOnceODRLinkage:
770 case GlobalValue::WeakAnyLinkage:
771 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000772 case GlobalValue::CommonLinkage:
Dale Johannesena60e51f2009-08-24 01:03:42 +0000773 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000774 O << "\t.global " << name << '\n'
775 << "\t.type " << name << ", @object\n"
776 << "\t.weak " << name << '\n';
777 break;
778 case GlobalValue::AppendingLinkage:
779 // FIXME: appending linkage variables should go into a section of
780 // their name or something. For now, just emit them as external.
781 case GlobalValue::ExternalLinkage:
782 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000783 O << "\t.global " << name << '\n'
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000784 << "\t.type " << name << ", @object\n";
785 // FALL THROUGH
786 case GlobalValue::InternalLinkage:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000787 case GlobalValue::PrivateLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000788 break;
789 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000790 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000791 }
792
793 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000794 O << name << ":";
795 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000796 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000797 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000798 O << "'";
799 }
800 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000801
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000802 EmitGlobalConstant(C);
803 O << '\n';
804}
805
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000806bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
807 const TargetData *TD = TM.getTargetData();
808
809 bool isPPC64 = TD->getPointerSizeInBits() == 64;
810
811 if (isPPC64 && !TOC.empty()) {
812 // FIXME 64-bit SVR4: Use MCSection here?
813 O << "\t.section\t\".toc\",\"aw\"\n";
814
815 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
816 I != E; ++I) {
817 O << I->second << ":\n";
818 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
819 }
820 }
821
822 return AsmPrinter::doFinalization(M);
823}
Jim Laskeybf111822006-12-21 20:26:09 +0000824
Nate Begemaned428532004-09-04 05:00:00 +0000825/// runOnMachineFunction - This uses the printMachineInstruction()
826/// method to print assembly for each instruction.
827///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000828bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000829 this->MF = &MF;
830
Chris Lattner8b8b9512005-11-21 07:51:23 +0000831 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000832 O << "\n\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000833
Nate Begemaned428532004-09-04 05:00:00 +0000834 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000835 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000836
837 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000838 const Function *F = MF.getFunction();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000839 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000840
Chris Lattnera637c322005-12-16 00:22:14 +0000841 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000842 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng1902a122009-01-25 06:32:01 +0000843 case Function::PrivateLinkage:
Chris Lattnera637c322005-12-16 00:22:14 +0000844 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnera637c322005-12-16 00:22:14 +0000845 break;
846 case Function::ExternalLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000847 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000848 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000849 case Function::WeakAnyLinkage:
850 case Function::WeakODRLinkage:
851 case Function::LinkOnceAnyLinkage:
852 case Function::LinkOnceODRLinkage:
Dale Johannesena60e51f2009-08-24 01:03:42 +0000853 case Function::LinkerPrivateLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000854 O << "\t.globl\t" << CurrentFnName << '\n';
855 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000856 break;
857 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000858
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000859 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000860
Bill Wendling20c568f2009-06-30 22:38:32 +0000861 EmitAlignment(MF.getAlignment(), F);
Nate Begemaned428532004-09-04 05:00:00 +0000862 O << CurrentFnName << ":\n";
863
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000864 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000865 DW->BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000866
Bill Wendling381802f2008-01-26 06:51:24 +0000867 // If the function is empty, then we need to emit *something*. Otherwise, the
868 // function's label might be associated with something that it wasn't meant to
869 // be associated with. We emit a noop in this situation.
870 MachineFunction::iterator I = MF.begin();
871
Bill Wendling824a7212008-01-26 09:03:52 +0000872 if (++I == MF.end() && MF.front().empty())
873 O << "\tnop\n";
Bill Wendling381802f2008-01-26 06:51:24 +0000874
Nate Begemaned428532004-09-04 05:00:00 +0000875 // Print out code for the function.
876 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
877 I != E; ++I) {
878 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000879 if (I != MF.begin()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000880 EmitBasicBlockStart(I);
Chris Lattner1db1adb2005-08-21 19:09:33 +0000881 }
Bill Wendling381802f2008-01-26 06:51:24 +0000882 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
883 II != IE; ++II) {
Nate Begemaned428532004-09-04 05:00:00 +0000884 // Print the assembly for the instruction.
Nate Begemaned428532004-09-04 05:00:00 +0000885 printMachineInstruction(II);
886 }
887 }
Nate Begemaned428532004-09-04 05:00:00 +0000888
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000889 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000890 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000891
Bill Wendling2718e432009-11-09 21:45:26 +0000892 // Print out jump tables referenced by the function.
893 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
894
Nate Begemaned428532004-09-04 05:00:00 +0000895 // We didn't modify anything.
896 return false;
897}
898
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000899
Bob Wilson812209a2009-09-30 22:06:26 +0000900void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000901 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000902 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000903 "ppc",
904 "ppc601",
905 "ppc602",
906 "ppc603",
907 "ppc7400",
908 "ppc750",
909 "ppc970",
910 "ppc64"
911 };
912
913 unsigned Directive = Subtarget.getDarwinDirective();
914 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
915 Directive = PPC::DIR_970;
916 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
917 Directive = PPC::DIR_7400;
918 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
919 Directive = PPC::DIR_64;
920 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000921 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000922
Jim Laskey2ada0852006-11-28 18:21:52 +0000923 // Prime text sections so they are adjacent. This reduces the likelihood a
924 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattner2dfddee2009-08-03 22:52:21 +0000925 TargetLoweringObjectFileMachO &TLOFMacho =
926 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000927 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskey2ada0852006-11-28 18:21:52 +0000928 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000929 OutStreamer.SwitchSection(
930 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
931 MCSectionMachO::S_SYMBOL_STUBS |
932 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
933 32, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000934 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000935 OutStreamer.SwitchSection(
936 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
937 MCSectionMachO::S_SYMBOL_STUBS |
938 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
939 16, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000940 }
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000941 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman18ed0292005-07-21 01:25:49 +0000942}
943
Chris Lattner40bbebd2009-07-21 18:38:57 +0000944void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000945 const TargetData *TD = TM.getTargetData();
946
947 if (!GVar->hasInitializer())
948 return; // External global require no code
949
950 // Check to see if this is a special global used by LLVM, if so, emit it.
951 if (EmitSpecialLLVMGlobal(GVar)) {
952 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000953 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000954 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000955 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000956 O << ".reference .destructors_used\n";
957 }
958 return;
959 }
960
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000961 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000962 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000963
964 Constant *C = GVar->getInitializer();
965 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000966 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000967 unsigned Align = TD->getPreferredAlignmentLog(GVar);
968
Chris Lattnera87dea42009-07-31 18:48:30 +0000969 const MCSection *TheSection =
Chris Lattnere53a6002009-07-29 05:09:30 +0000970 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000971 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000972
Chris Lattner3b24c012009-08-04 05:35:56 +0000973 /// FIXME: Drive this off the section!
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000974 if (C->isNullValue() && /* FIXME: Verify correct */
975 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000976 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000977 GVar->isWeakForLinker()) &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000978 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000979 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000980 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
981
982 if (GVar->hasExternalLinkage()) {
983 O << "\t.globl " << name << '\n';
984 O << "\t.zerofill __DATA, __common, " << name << ", "
985 << Size << ", " << Align;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000986 } else if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000987 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000988 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000989 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000990 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000991 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000992 O << name << ":";
993 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000994 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000995 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000996 }
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000997 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000998 EmitGlobalConstant(C);
999 return;
1000 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001001 O << ".comm " << name << ',' << Size;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001002 // Darwin 9 and above support aligned common data.
1003 if (Subtarget.isDarwin9())
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001004 O << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001005 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001006 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001007 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohmancf20ac42009-08-13 01:36:44 +00001008 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +00001009 O << "'";
1010 }
1011 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001012 return;
1013 }
1014
1015 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +00001016 case GlobalValue::LinkOnceAnyLinkage:
1017 case GlobalValue::LinkOnceODRLinkage:
1018 case GlobalValue::WeakAnyLinkage:
1019 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00001020 case GlobalValue::CommonLinkage:
Dale Johannesena60e51f2009-08-24 01:03:42 +00001021 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001022 O << "\t.globl " << name << '\n'
1023 << "\t.weak_definition " << name << '\n';
1024 break;
1025 case GlobalValue::AppendingLinkage:
1026 // FIXME: appending linkage variables should go into a section of
1027 // their name or something. For now, just emit them as external.
1028 case GlobalValue::ExternalLinkage:
1029 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001030 O << "\t.globl " << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001031 // FALL THROUGH
1032 case GlobalValue::InternalLinkage:
Evan Cheng1902a122009-01-25 06:32:01 +00001033 case GlobalValue::PrivateLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001034 break;
1035 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001036 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001037 }
1038
1039 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001040 O << name << ":";
1041 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001042 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohmancf20ac42009-08-13 01:36:44 +00001043 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +00001044 O << "'";
1045 }
1046 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001047
Anton Korobeynikovbc331a82008-08-08 18:23:25 +00001048 EmitGlobalConstant(C);
1049 O << '\n';
1050}
1051
Anton Korobeynikov34da1272008-08-08 18:22:59 +00001052bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +00001053 const TargetData *TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001054
Chris Lattner7e097e42006-06-27 01:02:25 +00001055 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1056
Chris Lattner2dfddee2009-08-03 22:52:21 +00001057 // Darwin/PPC always uses mach-o.
1058 TargetLoweringObjectFileMachO &TLOFMacho =
1059 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1060
Chris Lattnerff4bc462009-08-10 01:39:42 +00001061
1062 const MCSection *LSPSection = 0;
1063 if (!FnStubs.empty()) // .lazy_symbol_pointer
1064 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1065
1066
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001067 // Output stubs for dynamically-linked functions
Chris Lattnerea56f102009-07-15 00:55:58 +00001068 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner2dfddee2009-08-03 22:52:21 +00001069 const MCSection *StubSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +00001070 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1071 MCSectionMachO::S_SYMBOL_STUBS |
1072 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1073 32, SectionKind::getText());
Chris Lattner0b4bad52010-01-13 19:00:57 +00001074 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +00001075 I != E; ++I) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001076 OutStreamer.SwitchSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +00001077 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001078 const FnStubInfo &Info = I->second;
Chris Lattner0b4bad52010-01-13 19:00:57 +00001079 Info.printStub(O, MAI);
1080 O << ":\n";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001081 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +00001082 O << "\tmflr r0\n";
Chris Lattner0b4bad52010-01-13 19:00:57 +00001083 O << "\tbcl 20,31,";
1084 Info.printAnonSymbol(O, MAI);
1085 O << '\n';
1086 Info.printAnonSymbol(O, MAI);
1087 O << ":\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001088 O << "\tmflr r11\n";
Chris Lattner0b4bad52010-01-13 19:00:57 +00001089 O << "\taddis r11,r11,ha16(";
1090 Info.printLazyPtr(O, MAI);
1091 O << '-';
1092 Info.printAnonSymbol(O, MAI);
Evan Chengae94e592008-12-05 01:06:39 +00001093 O << ")\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001094 O << "\tmtlr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001095 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner0b4bad52010-01-13 19:00:57 +00001096 Info.printLazyPtr(O, MAI);
1097 O << '-';
1098 Info.printAnonSymbol(O, MAI);
1099 O << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001100 O << "\tmtctr r12\n";
1101 O << "\tbctr\n";
Chris Lattnerfe2fe702009-07-16 01:23:26 +00001102
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001103 OutStreamer.SwitchSection(LSPSection);
Chris Lattner0b4bad52010-01-13 19:00:57 +00001104 Info.printLazyPtr(O, MAI);
1105 O << ":\n";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001106 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001107 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001108 }
Chris Lattnerea56f102009-07-15 00:55:58 +00001109 } else if (!FnStubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001110 const MCSection *StubSection =
1111 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1112 MCSectionMachO::S_SYMBOL_STUBS |
1113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1114 16, SectionKind::getText());
Chris Lattner2dfddee2009-08-03 22:52:21 +00001115
Chris Lattner9ec8fac2009-07-15 02:28:57 +00001116 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +00001117 I != E; ++I) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001118 OutStreamer.SwitchSection(StubSection);
Chris Lattnerdeea4162005-12-13 04:33:58 +00001119 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001120 const FnStubInfo &Info = I->second;
Chris Lattner0b4bad52010-01-13 19:00:57 +00001121 Info.printStub(O, MAI);
1122 O << ":\n";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001123 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner0b4bad52010-01-13 19:00:57 +00001124 O << "\tlis r11,ha16(";
1125 Info.printLazyPtr(O, MAI);
1126 O << ")\n";
Chris Lattner1a87c052009-07-15 02:36:21 +00001127 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner0b4bad52010-01-13 19:00:57 +00001128 Info.printLazyPtr(O, MAI);
1129 O << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001130 O << "\tmtctr r12\n";
1131 O << "\tbctr\n";
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001132 OutStreamer.SwitchSection(LSPSection);
Chris Lattner0b4bad52010-01-13 19:00:57 +00001133 Info.printLazyPtr(O, MAI);
1134 O << ":\n";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001135 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001136 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +00001137 }
Misha Brukman46fd00a2004-06-24 23:04:11 +00001138 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001139
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001140 O << '\n';
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001141
Chris Lattner33adcfb2009-08-22 21:43:10 +00001142 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001143 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001144 // Only referenced functions get into the Personalities list.
Chris Lattner0de1fc42009-06-24 19:09:55 +00001145 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001146 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +00001147 E = Personalities.end(); I != E; ++I) {
1148 if (*I)
1149 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner0f6c8f22009-07-15 01:16:38 +00001150 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +00001151 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001152 }
1153
Chris Lattner2dfddee2009-08-03 22:52:21 +00001154 // Output macho stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001155 if (!GVStubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001156 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001157 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnere1e8a822009-08-10 17:58:51 +00001158 EmitAlignment(isPPC64 ? 3 : 2);
1159
Chris Lattner8f831cb2009-07-15 01:14:44 +00001160 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1161 E = GVStubs.end(); I != E; ++I) {
1162 O << I->second << ":\n";
1163 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1164 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001165 }
Nate Begemane59bf592004-08-14 22:09:10 +00001166 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001167
Evan Chengae94e592008-12-05 01:06:39 +00001168 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001169 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +00001170 EmitAlignment(isPPC64 ? 3 : 2);
1171 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1172 E = HiddenGVStubs.end(); I != E; ++I) {
1173 O << I->second << ":\n";
1174 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chengae94e592008-12-05 01:06:39 +00001175 }
1176 }
1177
Chris Lattner5b0ac992005-11-01 00:12:36 +00001178 // Funny Darwin hack: This flag tells the linker that no global symbols
1179 // contain code that falls through to other global symbols (e.g. the obvious
1180 // implementation of multiple entry points). If this doesn't occur, the
1181 // linker can safely perform dead code stripping. Since LLVM never generates
1182 // code that does this, it is always safe to set.
Chris Lattner74cd3b72009-10-19 18:03:08 +00001183 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Chris Lattner5b0ac992005-11-01 00:12:36 +00001184
Dan Gohmanb8275a32007-07-25 19:33:14 +00001185 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001186}
Nate Begemaned428532004-09-04 05:00:00 +00001187
Chris Lattner4da1c822006-09-20 17:12:19 +00001188
1189
Jim Laskeyb608a4d2006-12-20 20:56:46 +00001190/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1191/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +00001192/// Darwin assembler can deal with.
1193///
Daniel Dunbar78945782009-08-13 23:48:47 +00001194static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1195 TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +00001196 const MCAsmInfo *tai,
Daniel Dunbar78945782009-08-13 23:48:47 +00001197 bool verbose) {
Jim Laskeybf111822006-12-21 20:26:09 +00001198 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1199
Chris Lattner40bbebd2009-07-21 18:38:57 +00001200 if (Subtarget->isDarwin())
Daniel Dunbar67d894e2009-08-13 19:38:51 +00001201 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1202 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Chris Lattner4da1c822006-09-20 17:12:19 +00001203}
Anton Korobeynikov06be9972008-08-17 13:54:28 +00001204
Bob Wilsona96751f2009-06-23 23:59:40 +00001205// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001206extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001207 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001208 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1209}