blob: 63454894bd7862baf8cbbc4a3977eea09e208159 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
12// the output mechanism used by `llc'.
13//
14// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "asmprinter"
20#include "PPC.h"
21#include "PPCPredicates.h"
22#include "PPCTargetMachine.h"
23#include "PPCSubtarget.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
28#include "llvm/CodeGen/AsmPrinter.h"
29#include "llvm/CodeGen/DwarfWriter.h"
30#include "llvm/CodeGen/MachineModuleInfo.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9bc87482010-01-13 19:00:57 +000035#include "llvm/MC/MCContext.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000036#include "llvm/MC/MCSectionMachO.h"
Chris Lattner73266f92009-08-19 05:49:37 +000037#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000038#include "llvm/MC/MCSymbol.h"
Chris Lattnere6ad12f2009-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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/Support/Mangler.h"
45#include "llvm/Support/MathExtras.h"
46#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000048#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000049#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050#include "llvm/ADT/Statistic.h"
51#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000052#include "llvm/ADT/StringSet.h"
Chris Lattnera38b2872010-01-13 06:38:18 +000053#include "llvm/ADT/SmallString.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054using namespace llvm;
55
56STATISTIC(EmittedInsts, "Number of machine instrs printed");
57
58namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000059 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +000060 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000061 struct FnStubInfo {
Chris Lattner1774fd32010-01-13 19:13:16 +000062 MCSymbol *Stub, *LazyPtr, *AnonSymbol;
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000063
Chris Lattner9bc87482010-01-13 19:00:57 +000064 FnStubInfo() {
Chris Lattner1774fd32010-01-13 19:13:16 +000065 Stub = LazyPtr = AnonSymbol = 0;
Chris Lattner9bc87482010-01-13 19:00:57 +000066 }
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000067
Chris Lattner1774fd32010-01-13 19:13:16 +000068 void Init(const GlobalValue *GV, Mangler *Mang, MCContext &Ctx) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000069 // Already initialized.
Chris Lattner1774fd32010-01-13 19:13:16 +000070 if (Stub != 0) return;
Chris Lattner53087e52010-01-13 19:05:36 +000071
72 // Get the names.
Chris Lattner1774fd32010-01-13 19:13:16 +000073 SmallString<128> TmpStr;
74 Mang->getNameWithPrefix(TmpStr, GV, true);
75 MakeSymbols(TmpStr, Ctx);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000076 }
77
Chris Lattner9bc87482010-01-13 19:00:57 +000078 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattner1774fd32010-01-13 19:13:16 +000079 assert(!GVName.empty() && "external symbol name shouldn't be empty");
80 if (Stub != 0) return; // Already initialized.
Chris Lattnerf80743e2010-01-13 07:56:59 +000081 // Get the names for the external symbol name.
Chris Lattnera38b2872010-01-13 06:38:18 +000082 SmallString<128> TmpStr;
Chris Lattner1774fd32010-01-13 19:13:16 +000083 Mang->getNameWithPrefix(TmpStr, GVName, Mangler::Private);
84 MakeSymbols(TmpStr, Ctx);
85 }
86
87 void MakeSymbols(SmallString<128> &TmpStr, MCContext &Ctx) {
88 TmpStr += "$stub";
89 Stub = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner53087e52010-01-13 19:05:36 +000090 TmpStr.erase(TmpStr.end()-5, TmpStr.end()); // Remove $stub
91
92 TmpStr += "$lazy_ptr";
Chris Lattner1774fd32010-01-13 19:13:16 +000093 LazyPtr = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner53087e52010-01-13 19:05:36 +000094 TmpStr.erase(TmpStr.end()-9, TmpStr.end()); // Remove $lazy_ptr
Chris Lattnera38b2872010-01-13 06:38:18 +000095
Chris Lattner53087e52010-01-13 19:05:36 +000096 TmpStr += "$stub$tmp";
Chris Lattner1774fd32010-01-13 19:13:16 +000097 AnonSymbol = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000098 }
99 };
100
101 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000102 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000104 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +0000105 public:
David Greene302008d2009-07-14 20:18:05 +0000106 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000107 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000108 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000109 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
111 virtual const char *getPassName() const {
112 return "PowerPC Assembly Printer";
113 }
114
115 PPCTargetMachine &getTM() {
116 return static_cast<PPCTargetMachine&>(TM);
117 }
118
119 unsigned enumRegToMachineReg(unsigned enumReg) {
120 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000121 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 case PPC::CR0: return 0;
123 case PPC::CR1: return 1;
124 case PPC::CR2: return 2;
125 case PPC::CR3: return 3;
126 case PPC::CR4: return 4;
127 case PPC::CR5: return 5;
128 case PPC::CR6: return 6;
129 case PPC::CR7: return 7;
130 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000131 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 }
133
134 /// printInstruction - This method is automatically generated by tablegen
135 /// from the instruction set description. This method returns true if the
136 /// machine instruction was sufficiently described to print it, otherwise it
137 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000138 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000139 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
142 void printMachineInstruction(const MachineInstr *MI);
143 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 /// stripRegisterPrefix - This method strips the character prefix from a
146 /// register name so that only the number is left. Used by for linux asm.
147 const char *stripRegisterPrefix(const char *RegName) {
148 switch (RegName[0]) {
149 case 'r':
150 case 'f':
151 case 'v': return RegName + 1;
152 case 'c': if (RegName[1] == 'r') return RegName + 2;
153 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 return RegName;
156 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 /// printRegister - Print register according to target requirements.
159 ///
160 void printRegister(const MachineOperand &MO, bool R0AsZero) {
161 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000162 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 // If we should use 0 for R0.
165 if (R0AsZero && RegNo == PPC::R0) {
166 O << "0";
167 return;
168 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000169
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000170 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 // Linux assembler (Others?) does not take register mnemonics.
172 // FIXME - What about special registers used in mfspr/mtspr?
173 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
174 O << RegName;
175 }
176
177 void printOperand(const MachineInstr *MI, unsigned OpNo) {
178 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000179 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000181 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000182 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 } else {
184 printOp(MO);
185 }
186 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
189 unsigned AsmVariant, const char *ExtraCode);
190 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
191 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000192
193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000195 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 value = (value << (32-5)) >> (32-5);
197 O << (int)value;
198 }
199 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000200 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 assert(value <= 31 && "Invalid u5imm argument!");
202 O << (unsigned int)value;
203 }
204 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000205 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 assert(value <= 63 && "Invalid u6imm argument!");
207 O << (unsigned int)value;
208 }
209 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000210 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 }
212 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000213 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 }
215 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000216 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000217 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 } else {
219 O << "lo16(";
220 printOp(MI->getOperand(OpNo));
221 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000222 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 else
224 O << ')';
225 }
226 }
227 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
228 // Branches can take an immediate operand. This is used by the branch
229 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000230 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000231 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 } else {
233 printOp(MI->getOperand(OpNo));
234 }
235 }
236 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
237 const MachineOperand &MO = MI->getOperand(OpNo);
238 if (TM.getRelocationModel() != Reloc::Static) {
239 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
240 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000241 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000243 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
Chris Lattner1774fd32010-01-13 19:13:16 +0000244 FnInfo.Init(GV, Mang, OutContext);
245 FnInfo.Stub->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 return;
247 }
248 }
249 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000250 SmallString<128> MangledName;
Chris Lattnerf80743e2010-01-13 07:56:59 +0000251 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattnera38b2872010-01-13 06:38:18 +0000252 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9bc87482010-01-13 19:00:57 +0000253 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
Chris Lattner1774fd32010-01-13 19:13:16 +0000254 FnInfo.Stub->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 return;
256 }
257 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 printOp(MI->getOperand(OpNo));
260 }
261 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000262 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 }
264 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000265 O << "\"L" << getFunctionNumber() << "$pb\"\n";
266 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 }
268 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000269 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 printS16ImmOperand(MI, OpNo);
271 } else {
272 if (Subtarget.isDarwin()) O << "ha16(";
273 printOp(MI->getOperand(OpNo));
274 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000275 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 if (Subtarget.isDarwin())
277 O << ')';
278 else
279 O << "@ha";
280 }
281 }
282 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000283 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 printS16ImmOperand(MI, OpNo);
285 } else {
286 if (Subtarget.isDarwin()) O << "lo16(";
287 printOp(MI->getOperand(OpNo));
288 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000289 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 if (Subtarget.isDarwin())
291 O << ')';
292 else
293 O << "@l";
294 }
295 }
296 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
297 unsigned CCReg = MI->getOperand(OpNo).getReg();
298 unsigned RegNo = enumRegToMachineReg(CCReg);
299 O << (0x80 >> RegNo);
300 }
301 // The new addressing mode printers.
302 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
303 printSymbolLo(MI, OpNo);
304 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000305 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 MI->getOperand(OpNo+1).getReg() == PPC::R0)
307 O << "0";
308 else
309 printOperand(MI, OpNo+1);
310 O << ')';
311 }
312 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000313 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000315 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 printSymbolLo(MI, OpNo);
317 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000318 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 MI->getOperand(OpNo+1).getReg() == PPC::R0)
320 O << "0";
321 else
322 printOperand(MI, OpNo+1);
323 O << ')';
324 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000325
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
327 // When used as the base register, r0 reads constant zero rather than
328 // the value contained in the register. For this reason, the darwin
329 // assembler requires that we print r0 as 0 (no r) when used as the base.
330 const MachineOperand &MO = MI->getOperand(OpNo);
331 printRegister(MO, true);
332 O << ", ";
333 printOperand(MI, OpNo+1);
334 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000335
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000336 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
337 const MachineOperand &MO = MI->getOperand(OpNo);
338
339 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
340
341 GlobalValue *GV = MO.getGlobal();
342
343 std::string Name = Mang->getMangledName(GV);
344
345 // Map symbol -> label of TOC entry.
346 if (TOC.count(Name) == 0) {
347 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000348 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000349 Label += "C";
350 Label += utostr(LabelID++);
351
352 TOC[Name] = Label;
353 }
354
355 O << TOC[Name] << "@toc";
356 }
357
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000358 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000360
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 };
363
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000364 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000365 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000366 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000367 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000368 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000369 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370
371 virtual const char *getPassName() const {
372 return "Linux PPC Assembly Printer";
373 }
374
375 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000376 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000377
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 void getAnalysisUsage(AnalysisUsage &AU) const {
379 AU.setPreservesAll();
380 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000381 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 PPCAsmPrinter::getAnalysisUsage(AU);
383 }
384
Chris Lattnerae982212009-07-21 18:38:57 +0000385 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 };
387
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000388 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
389 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000390 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000391 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000392 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000393 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000394 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000395 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396
397 virtual const char *getPassName() const {
398 return "Darwin PPC Assembly Printer";
399 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000400
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000403 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 void getAnalysisUsage(AnalysisUsage &AU) const {
406 AU.setPreservesAll();
407 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000408 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 PPCAsmPrinter::getAnalysisUsage(AU);
410 }
411
Chris Lattnerae982212009-07-21 18:38:57 +0000412 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 };
414} // end of anonymous namespace
415
416// Include the auto-generated portion of the assembly writer
417#include "PPCGenAsmWriter.inc"
418
419void PPCAsmPrinter::printOp(const MachineOperand &MO) {
420 switch (MO.getType()) {
421 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000422 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423
424 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000425 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 return;
427 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000428 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000429 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 // FIXME: PIC relocation model
431 return;
432 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000433 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000434 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000436 case MachineOperand::MO_BlockAddress:
437 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
438 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000439 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000441 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000442 Name += MO.getSymbolName();
443
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000445 GVStubs[Name] = Name+"$non_lazy_ptr";
446 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000448 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000450 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 case MachineOperand::MO_GlobalAddress: {
452 // Computing the address of a global symbol, not calling it.
453 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000454 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000457 if (TM.getRelocationModel() != Reloc::Static &&
458 (GV->isDeclaration() || GV->isWeakForLinker())) {
459 if (!GV->hasHiddenVisibility()) {
460 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
461 GVStubs[Mang->getMangledName(GV)] = Name;
462 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
463 GV->hasAvailableExternallyLinkage()) {
464 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
465 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
466 } else {
467 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000469 } else {
470 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 }
472 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000473
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000474 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 return;
476 }
477
478 default:
479 O << "<unknown operand type: " << MO.getType() << ">";
480 return;
481 }
482}
483
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484/// PrintAsmOperand - Print out an operand for an inline asm expression.
485///
486bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000487 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 const char *ExtraCode) {
489 // Does this asm operand have a single letter operand modifier?
490 if (ExtraCode && ExtraCode[0]) {
491 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000492
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 switch (ExtraCode[0]) {
494 default: return true; // Unknown modifier.
495 case 'c': // Don't print "$" before a global var name or constant.
496 // PPC never has a prefix.
497 printOperand(MI, OpNo);
498 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000499 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000501 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000503 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 return true;
505 ++OpNo; // Return the high-part.
506 break;
507 case 'I':
508 // Write 'i' if an integer constant, otherwise nothing. Used to print
509 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000510 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 O << "i";
512 return false;
513 }
514 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000515
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 printOperand(MI, OpNo);
517 return false;
518}
519
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000520// At the moment, all inline asm memory operands are a single register.
521// In any case, the output of this routine should always be just one
522// assembler operand.
523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000525 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 const char *ExtraCode) {
527 if (ExtraCode && ExtraCode[0])
528 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000529 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000530 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000531 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000532 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 return false;
534}
535
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000536void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 const char *Modifier) {
538 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
539 unsigned Code = MI->getOperand(OpNo).getImm();
540 if (!strcmp(Modifier, "cc")) {
541 switch ((PPC::Predicate)Code) {
542 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
543 case PPC::PRED_LT: O << "lt"; return;
544 case PPC::PRED_LE: O << "le"; return;
545 case PPC::PRED_EQ: O << "eq"; return;
546 case PPC::PRED_GE: O << "ge"; return;
547 case PPC::PRED_GT: O << "gt"; return;
548 case PPC::PRED_NE: O << "ne"; return;
549 case PPC::PRED_UN: O << "un"; return;
550 case PPC::PRED_NU: O << "nu"; return;
551 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000552
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 } else {
554 assert(!strcmp(Modifier, "reg") &&
555 "Need to specify 'cc' or 'reg' as predicate op modifier!");
556 // Don't print the register for 'always'.
557 if (Code == PPC::PRED_ALWAYS) return;
558 printOperand(MI, OpNo+1);
559 }
560}
561
562
563/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
564/// the current output stream.
565///
566void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
567 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000568
Devang Patel5450fc12009-10-06 02:19:11 +0000569 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570
571 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000572 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000574 unsigned char SH = MI->getOperand(2).getImm();
575 unsigned char MB = MI->getOperand(3).getImm();
576 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000578 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
580 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000581 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 SH = 32-SH;
583 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000584 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 printOperand(MI, 0);
586 O << ", ";
587 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000588 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 }
590 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
591 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000592 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000593 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 printOperand(MI, 0);
595 O << ", ";
596 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 }
598 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000599 unsigned char SH = MI->getOperand(2).getImm();
600 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
602 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000603 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000604 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 printOperand(MI, 0);
606 O << ", ";
607 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000608 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 }
610 }
611
Dale Johannesen760acc02010-01-06 02:20:18 +0000612 if (!useSubstituteMnemonic)
613 printInstruction(MI);
614
David Greeneca9b04b2009-11-13 21:34:57 +0000615 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000616 EmitComments(*MI);
617 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000618
619 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620}
621
622/// runOnMachineFunction - This uses the printMachineInstruction()
623/// method to print assembly for each instruction.
624///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000625bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000626 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627
628 SetupMachineFunction(MF);
629 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000630
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 // Print out constants referenced by the function
632 EmitConstantPool(MF.getConstantPool());
633
634 // Print out labels for the function.
635 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000636 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000637
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000639 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000640 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 case Function::InternalLinkage: // Symbols default to internal.
642 break;
643 case Function::ExternalLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000644 O << "\t.global\t";
645 CurrentFnSym->print(O, MAI);
646 O << '\n' << "\t.type\t";
647 CurrentFnSym->print(O, MAI);
648 O << ", @function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000650 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000651 case Function::WeakAnyLinkage:
652 case Function::WeakODRLinkage:
653 case Function::LinkOnceAnyLinkage:
654 case Function::LinkOnceODRLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000655 O << "\t.global\t";
656 CurrentFnSym->print(O, MAI);
657 O << '\n';
658 O << "\t.weak\t";
659 CurrentFnSym->print(O, MAI);
660 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 break;
662 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000663
Chris Lattner651adf32010-01-16 00:21:18 +0000664 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000665
Bill Wendling25a8ae32009-06-30 22:38:32 +0000666 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000667
668 if (Subtarget.isPPC64()) {
669 // Emit an official procedure descriptor.
Chris Lattner651adf32010-01-16 00:21:18 +0000670 // FIXME 64-bit SVR4: Use MCSection here!
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000671 O << "\t.section\t\".opd\",\"aw\"\n";
672 O << "\t.align 3\n";
Chris Lattner651adf32010-01-16 00:21:18 +0000673 CurrentFnSym->print(O, MAI);
674 O << ":\n";
675 O << "\t.quad .L.";
676 CurrentFnSym->print(O, MAI);
677 O << ",.TOC.@tocbase\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000678 O << "\t.previous\n";
Chris Lattner651adf32010-01-16 00:21:18 +0000679 O << ".L.";
680 CurrentFnSym->print(O, MAI);
681 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000682 } else {
Chris Lattner651adf32010-01-16 00:21:18 +0000683 CurrentFnSym->print(O, MAI);
684 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
687 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000688 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner2faa4ef2009-09-13 18:25:37 +0000695 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 }
697 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
698 II != E; ++II) {
699 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 printMachineInstruction(II);
701 }
702 }
703
Chris Lattner651adf32010-01-16 00:21:18 +0000704 O << "\t.size\t";
705 CurrentFnSym->print(O, MAI);
706 O << ",.-";
707 CurrentFnSym->print(O, MAI);
708 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
Chris Lattner73266f92009-08-19 05:49:37 +0000710 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000711
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000713 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000714
Bill Wendling6fb58e12009-11-09 21:20:14 +0000715 // Print out jump tables referenced by the function.
716 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
717
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 // We didn't modify anything.
719 return false;
720}
721
Chris Lattnerae982212009-07-21 18:38:57 +0000722void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 const TargetData *TD = TM.getTargetData();
724
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000725 if (!GVar->hasInitializer())
726 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000727
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000728 // Check to see if this is a special global used by LLVM, if so, emit it.
729 if (EmitSpecialLLVMGlobal(GVar))
730 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731
Chris Lattner22b24952010-01-16 01:12:01 +0000732 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733
Chris Lattner22b24952010-01-16 01:12:01 +0000734 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000735
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000736 Constant *C = GVar->getInitializer();
737 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000738 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000739 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740
Chris Lattner73266f92009-08-19 05:49:37 +0000741 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
742 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000743
744 if (C->isNullValue() && /* FIXME: Verify correct */
745 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000746 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000747 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000749
750 if (GVar->hasExternalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000751 O << "\t.global ";
752 GVarSym->print(O, MAI);
753 O << '\n';
754 O << "\t.type ";
755 GVarSym->print(O, MAI);
756 O << ", @object\n";
757 GVarSym->print(O, MAI);
758 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000759 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000760 } else if (GVar->hasLocalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000761 O << MAI->getLCOMMDirective();
762 GVarSym->print(O, MAI);
763 O << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 } else {
Chris Lattner22b24952010-01-16 01:12:01 +0000765 O << ".comm ";
766 GVarSym->print(O, MAI);
767 O << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 }
Evan Cheng11db8142009-03-24 00:17:40 +0000769 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000770 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000771 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000772 O << "'";
773 }
774 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000775 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 }
777
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000778 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000779 case GlobalValue::LinkOnceAnyLinkage:
780 case GlobalValue::LinkOnceODRLinkage:
781 case GlobalValue::WeakAnyLinkage:
782 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000783 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000784 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner22b24952010-01-16 01:12:01 +0000785 O << "\t.global ";
786 GVarSym->print(O, MAI);
787 O << "\n\t.type ";
788 GVarSym->print(O, MAI);
789 O << ", @object\n\t.weak ";
790 GVarSym->print(O, MAI);
791 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000792 break;
793 case GlobalValue::AppendingLinkage:
794 // FIXME: appending linkage variables should go into a section of
795 // their name or something. For now, just emit them as external.
796 case GlobalValue::ExternalLinkage:
797 // If external or appending, declare as a global symbol
Chris Lattner22b24952010-01-16 01:12:01 +0000798 O << "\t.global ";
799 GVarSym->print(O, MAI);
800 O << "\n\t.type ";
801 GVarSym->print(O, MAI);
802 O << ", @object\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000803 // FALL THROUGH
804 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000805 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000806 break;
807 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000808 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000809 }
810
811 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +0000812 GVarSym->print(O, MAI);
813 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +0000814 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000815 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000816 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000817 O << "'";
818 }
819 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000820
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000821 EmitGlobalConstant(C);
822 O << '\n';
823}
824
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000825bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
826 const TargetData *TD = TM.getTargetData();
827
828 bool isPPC64 = TD->getPointerSizeInBits() == 64;
829
830 if (isPPC64 && !TOC.empty()) {
831 // FIXME 64-bit SVR4: Use MCSection here?
832 O << "\t.section\t\".toc\",\"aw\"\n";
833
834 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
835 I != E; ++I) {
836 O << I->second << ":\n";
837 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
838 }
839 }
840
841 return AsmPrinter::doFinalization(M);
842}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844/// runOnMachineFunction - This uses the printMachineInstruction()
845/// method to print assembly for each instruction.
846///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000847bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000848 this->MF = &MF;
849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 SetupMachineFunction(MF);
851 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000852
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 // Print out constants referenced by the function
854 EmitConstantPool(MF.getConstantPool());
855
856 // Print out labels for the function.
857 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000858 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000859
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000861 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000862 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 case Function::InternalLinkage: // Symbols default to internal.
864 break;
865 case Function::ExternalLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000866 O << "\t.globl\t";
867 CurrentFnSym->print(O, MAI);
868 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000870 case Function::WeakAnyLinkage:
871 case Function::WeakODRLinkage:
872 case Function::LinkOnceAnyLinkage:
873 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000874 case Function::LinkerPrivateLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000875 O << "\t.globl\t";
876 CurrentFnSym->print(O, MAI);
877 O << '\n';
878 O << "\t.weak_definition\t";
879 CurrentFnSym->print(O, MAI);
880 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 break;
882 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000883
Chris Lattner651adf32010-01-16 00:21:18 +0000884 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000885
Bill Wendling25a8ae32009-06-30 22:38:32 +0000886 EmitAlignment(MF.getAlignment(), F);
Chris Lattner651adf32010-01-16 00:21:18 +0000887 CurrentFnSym->print(O, MAI);
888 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889
890 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000891 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892
Bill Wendling36ccaea2008-01-26 06:51:24 +0000893 // If the function is empty, then we need to emit *something*. Otherwise, the
894 // function's label might be associated with something that it wasn't meant to
895 // be associated with. We emit a noop in this situation.
896 MachineFunction::iterator I = MF.begin();
897
Bill Wendlingb5880a72008-01-26 09:03:52 +0000898 if (++I == MF.end() && MF.front().empty())
899 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000900
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 // Print out code for the function.
902 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
903 I != E; ++I) {
904 // Print a label for the basic block.
905 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000906 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000908 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
909 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 printMachineInstruction(II);
912 }
913 }
914
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000916 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000917
Bill Wendling5019fe02009-11-09 21:45:26 +0000918 // Print out jump tables referenced by the function.
919 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 // We didn't modify anything.
922 return false;
923}
924
925
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000926void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000927 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000928 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 "ppc",
930 "ppc601",
931 "ppc602",
932 "ppc603",
933 "ppc7400",
934 "ppc750",
935 "ppc970",
936 "ppc64"
937 };
938
939 unsigned Directive = Subtarget.getDarwinDirective();
940 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
941 Directive = PPC::DIR_970;
942 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
943 Directive = PPC::DIR_7400;
944 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
945 Directive = PPC::DIR_64;
946 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000947 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000948
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 // Prime text sections so they are adjacent. This reduces the likelihood a
950 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000951 TargetLoweringObjectFileMachO &TLOFMacho =
952 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000953 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000955 OutStreamer.SwitchSection(
956 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
957 MCSectionMachO::S_SYMBOL_STUBS |
958 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
959 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000961 OutStreamer.SwitchSection(
962 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
963 MCSectionMachO::S_SYMBOL_STUBS |
964 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
965 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 }
Chris Lattner73266f92009-08-19 05:49:37 +0000967 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968}
969
Chris Lattnerae982212009-07-21 18:38:57 +0000970void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000971 const TargetData *TD = TM.getTargetData();
972
973 if (!GVar->hasInitializer())
974 return; // External global require no code
975
976 // Check to see if this is a special global used by LLVM, if so, emit it.
977 if (EmitSpecialLLVMGlobal(GVar)) {
978 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000979 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000981 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000982 O << ".reference .destructors_used\n";
983 }
984 return;
985 }
986
Chris Lattner22b24952010-01-16 01:12:01 +0000987 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
988 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000989
990 Constant *C = GVar->getInitializer();
991 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000992 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000993 unsigned Align = TD->getPreferredAlignmentLog(GVar);
994
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000995 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000996 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000997 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000998
Chris Lattnerdb727932009-08-04 05:35:56 +0000999 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001000 if (C->isNullValue() && /* FIXME: Verify correct */
1001 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001002 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +00001003 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +00001004 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +00001005 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001006 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
1007
1008 if (GVar->hasExternalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001009 O << "\t.globl ";
1010 GVarSym->print(O, MAI);
1011 O << '\n';
1012 O << "\t.zerofill __DATA, __common, ";
1013 GVarSym->print(O, MAI);
1014 O << ", " << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001015 } else if (GVar->hasLocalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001016 O << MAI->getLCOMMDirective();
1017 GVarSym->print(O, MAI);
1018 O << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001019 } else if (!GVar->hasCommonLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001020 O << "\t.globl ";
1021 GVarSym->print(O, MAI);
1022 O << '\n' << MAI->getWeakDefDirective();
1023 GVarSym->print(O, MAI);
1024 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001025 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +00001026 GVarSym->print(O, MAI);
1027 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001028 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001029 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001030 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001031 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001032 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001033 EmitGlobalConstant(C);
1034 return;
1035 } else {
Chris Lattner22b24952010-01-16 01:12:01 +00001036 O << ".comm ";
1037 GVarSym->print(O, MAI);
1038 O << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001039 // Darwin 9 and above support aligned common data.
1040 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001041 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001042 }
Evan Cheng11db8142009-03-24 00:17:40 +00001043 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001044 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001045 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001046 O << "'";
1047 }
1048 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001049 return;
1050 }
1051
1052 switch (GVar->getLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001053 case GlobalValue::LinkOnceAnyLinkage:
1054 case GlobalValue::LinkOnceODRLinkage:
1055 case GlobalValue::WeakAnyLinkage:
1056 case GlobalValue::WeakODRLinkage:
1057 case GlobalValue::CommonLinkage:
1058 case GlobalValue::LinkerPrivateLinkage:
1059 O << "\t.globl ";
1060 GVarSym->print(O, MAI);
1061 O << "\n\t.weak_definition ";
1062 GVarSym->print(O, MAI);
1063 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001064 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001065 case GlobalValue::AppendingLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001066 // FIXME: appending linkage variables should go into a section of
1067 // their name or something. For now, just emit them as external.
Chris Lattner22b24952010-01-16 01:12:01 +00001068 case GlobalValue::ExternalLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001069 // If external or appending, declare as a global symbol
Chris Lattner22b24952010-01-16 01:12:01 +00001070 O << "\t.globl ";
1071 GVarSym->print(O, MAI);
1072 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001073 // FALL THROUGH
Chris Lattner22b24952010-01-16 01:12:01 +00001074 case GlobalValue::InternalLinkage:
1075 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001076 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001077 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001078 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001079 }
1080
1081 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +00001082 GVarSym->print(O, MAI);
1083 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001084 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001085 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001086 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001087 O << "'";
1088 }
1089 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001090
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001091 EmitGlobalConstant(C);
1092 O << '\n';
1093}
1094
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001095bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 const TargetData *TD = TM.getTargetData();
1097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1099
Chris Lattnerf4815552009-08-03 22:52:21 +00001100 // Darwin/PPC always uses mach-o.
1101 TargetLoweringObjectFileMachO &TLOFMacho =
1102 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1103
Chris Lattner72a676a2009-08-10 01:39:42 +00001104
1105 const MCSection *LSPSection = 0;
1106 if (!FnStubs.empty()) // .lazy_symbol_pointer
1107 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1108
1109
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001111 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001112 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001113 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1114 MCSectionMachO::S_SYMBOL_STUBS |
1115 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1116 32, SectionKind::getText());
Chris Lattner9bc87482010-01-13 19:00:57 +00001117 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001118 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001119 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001120 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001121 const FnStubInfo &Info = I->second;
Chris Lattner1774fd32010-01-13 19:13:16 +00001122 Info.Stub->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001123 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001124 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 O << "\tmflr r0\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001126 O << "\tbcl 20,31,";
Chris Lattner1774fd32010-01-13 19:13:16 +00001127 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001128 O << '\n';
Chris Lattner1774fd32010-01-13 19:13:16 +00001129 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001130 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 O << "\tmflr r11\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001132 O << "\taddis r11,r11,ha16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001133 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001134 O << '-';
Chris Lattner1774fd32010-01-13 19:13:16 +00001135 Info.AnonSymbol->print(O, MAI);
Evan Chenga65854f2008-12-05 01:06:39 +00001136 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001138 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001139 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001140 O << '-';
Chris Lattner1774fd32010-01-13 19:13:16 +00001141 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001142 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 O << "\tmtctr r12\n";
1144 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001145
Chris Lattner73266f92009-08-19 05:49:37 +00001146 OutStreamer.SwitchSection(LSPSection);
Chris Lattner1774fd32010-01-13 19:13:16 +00001147 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001148 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001149 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001150 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001151 }
Chris Lattner189198f2009-07-15 00:55:58 +00001152 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001153 const MCSection *StubSection =
1154 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1155 MCSectionMachO::S_SYMBOL_STUBS |
1156 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1157 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001158
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001159 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001160 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001161 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001163 const FnStubInfo &Info = I->second;
Chris Lattner1774fd32010-01-13 19:13:16 +00001164 Info.Stub->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001165 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001166 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner9bc87482010-01-13 19:00:57 +00001167 O << "\tlis r11,ha16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001168 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001169 O << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001170 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001171 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001172 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 O << "\tmtctr r12\n";
1174 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001175 OutStreamer.SwitchSection(LSPSection);
Chris Lattner1774fd32010-01-13 19:13:16 +00001176 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001177 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001178 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001179 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 }
1181 }
1182
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001183 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001185 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001186 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001187 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001188 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001189 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001190 E = Personalities.end(); I != E; ++I) {
1191 if (*I)
1192 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001193 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001194 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001195 }
1196
Chris Lattnerf4815552009-08-03 22:52:21 +00001197 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001198 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001199 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001200 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001201 EmitAlignment(isPPC64 ? 3 : 2);
1202
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001203 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1204 E = GVStubs.end(); I != E; ++I) {
1205 O << I->second << ":\n";
1206 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1207 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 }
1209 }
1210
Evan Chenga65854f2008-12-05 01:06:39 +00001211 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001212 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001213 EmitAlignment(isPPC64 ? 3 : 2);
1214 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1215 E = HiddenGVStubs.end(); I != E; ++I) {
1216 O << I->second << ":\n";
1217 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001218 }
1219 }
1220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 // Funny Darwin hack: This flag tells the linker that no global symbols
1222 // contain code that falls through to other global symbols (e.g. the obvious
1223 // implementation of multiple entry points). If this doesn't occur, the
1224 // linker can safely perform dead code stripping. Since LLVM never generates
1225 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001226 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001227
Dan Gohman4a558a32007-07-25 19:33:14 +00001228 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229}
1230
1231
1232
1233/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1234/// for a MachineFunction to the given output stream, in a format that the
1235/// Darwin assembler can deal with.
1236///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001237static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1238 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001239 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001240 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1242
Chris Lattnerae982212009-07-21 18:38:57 +00001243 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001244 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1245 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001247
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001248// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001249extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001250 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001251 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1252}