blob: 8a0c767db58fdff264dc72813c05ea209b42fd67 [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 Lattnera87dea42009-07-31 18:48:30 +000034#include "llvm/MC/MCSection.h"
35#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetLoweringObjectFile.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegistry.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000041#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000042#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000043#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000045#include "llvm/Support/ErrorHandling.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000046#include "llvm/Support/Compiler.h"
David Greene71847812009-07-14 20:18:05 +000047#include "llvm/Support/FormattedStream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000048#include "llvm/ADT/Statistic.h"
49#include "llvm/ADT/StringExtras.h"
Evan Chengae94e592008-12-05 01:06:39 +000050#include "llvm/ADT/StringSet.h"
Chris Lattnera3840792004-08-16 23:25:21 +000051using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000052
Chris Lattner95b2c7d2006-12-19 22:59:26 +000053STATISTIC(EmittedInsts, "Number of machine instrs printed");
Misha Brukman5dfe3a92004-06-21 16:55:25 +000054
Chris Lattner95b2c7d2006-12-19 22:59:26 +000055namespace {
Bill Wendling57f0db82009-02-24 08:30:20 +000056 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
57 protected:
Chris Lattner9ec8fac2009-07-15 02:28:57 +000058 struct FnStubInfo {
59 std::string Stub, LazyPtr, AnonSymbol;
60
61 FnStubInfo() {}
62
63 void Init(const GlobalValue *GV, Mangler *Mang) {
64 // Already initialized.
65 if (!Stub.empty()) return;
66 Stub = Mang->getMangledName(GV, "$stub", true);
67 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
68 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
69 }
70
71 void Init(const std::string &GV, Mangler *Mang) {
72 // Already initialized.
73 if (!Stub.empty()) return;
Bill Wendling3d10a5a2009-07-20 01:03:30 +000074 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendling5c0ba802009-07-20 19:41:27 +000075 Mangler::Private);
Bill Wendling3d10a5a2009-07-20 01:03:30 +000076 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendling5c0ba802009-07-20 19:41:27 +000077 Mangler::Private);
Bill Wendling3d10a5a2009-07-20 01:03:30 +000078 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendling5c0ba802009-07-20 19:41:27 +000079 Mangler::Private);
Chris Lattner9ec8fac2009-07-15 02:28:57 +000080 }
81 };
82
83 StringMap<FnStubInfo> FnStubs;
Chris Lattner8f831cb2009-07-15 01:14:44 +000084 StringMap<std::string> GVStubs, HiddenGVStubs;
Chris Lattnerdadceed2006-09-20 17:07:15 +000085 const PPCSubtarget &Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000086 public:
David Greene71847812009-07-14 20:18:05 +000087 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000088 const TargetAsmInfo *T, bool V)
89 : AsmPrinter(O, TM, T, V),
Bill Wendling57f0db82009-02-24 08:30:20 +000090 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000091
Misha Brukman5dfe3a92004-06-21 16:55:25 +000092 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000093 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 }
95
Nate Begeman21e463b2005-10-16 05:39:50 +000096 PPCTargetMachine &getTM() {
97 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000098 }
99
Nate Begemanadeb43d2005-07-20 22:42:00 +0000100 unsigned enumRegToMachineReg(unsigned enumReg) {
101 switch (enumReg) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000102 default: llvm_unreachable("Unhandled register!");
Nate Begemanadeb43d2005-07-20 22:42:00 +0000103 case PPC::CR0: return 0;
104 case PPC::CR1: return 1;
105 case PPC::CR2: return 2;
106 case PPC::CR3: return 3;
107 case PPC::CR4: return 4;
108 case PPC::CR5: return 5;
109 case PPC::CR6: return 6;
110 case PPC::CR7: return 7;
111 }
Torok Edwinc23197a2009-07-14 16:55:14 +0000112 llvm_unreachable(0);
Nate Begemanadeb43d2005-07-20 22:42:00 +0000113 }
114
Nate Begemane59bf592004-08-14 22:09:10 +0000115 /// printInstruction - This method is automatically generated by tablegen
116 /// from the instruction set description. This method returns true if the
117 /// machine instruction was sufficiently described to print it, otherwise it
118 /// returns false.
119 bool printInstruction(const MachineInstr *MI);
120
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000121 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +0000122 void printOp(const MachineOperand &MO);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000123
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000124 /// stripRegisterPrefix - This method strips the character prefix from a
125 /// register name so that only the number is left. Used by for linux asm.
Jim Laskey40ee69f2006-12-20 21:33:34 +0000126 const char *stripRegisterPrefix(const char *RegName) {
127 switch (RegName[0]) {
128 case 'r':
129 case 'f':
130 case 'v': return RegName + 1;
Jim Laskey2aa14aa2006-12-20 21:35:00 +0000131 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000132 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000133
Jim Laskey40ee69f2006-12-20 21:33:34 +0000134 return RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000135 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000136
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000137 /// printRegister - Print register according to target requirements.
138 ///
139 void printRegister(const MachineOperand &MO, bool R0AsZero) {
140 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000141 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000142
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000143 // If we should use 0 for R0.
144 if (R0AsZero && RegNo == PPC::R0) {
145 O << "0";
146 return;
147 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000148
Bill Wendling74ab84c2008-02-26 21:11:01 +0000149 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000150 // Linux assembler (Others?) does not take register mnemonics.
151 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskey40ee69f2006-12-20 21:33:34 +0000152 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
153 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000154 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000155
Chris Lattner58873272006-02-01 22:38:46 +0000156 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000157 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmand735b802008-10-03 15:45:36 +0000158 if (MO.isReg()) {
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000159 printRegister(MO, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000160 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000161 O << MO.getImm();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000162 } else {
163 printOp(MO);
164 }
165 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000166
Chris Lattner58873272006-02-01 22:38:46 +0000167 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000168 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000169 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
170 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000171
172
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000173 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000174 char value = MI->getOperand(OpNo).getImm();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000175 value = (value << (32-5)) >> (32-5);
176 O << (int)value;
177 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000178 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000179 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattner12585ba2004-08-21 19:11:03 +0000180 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000181 O << (unsigned int)value;
182 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000183 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000184 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman07aada82004-08-30 02:28:06 +0000185 assert(value <= 63 && "Invalid u6imm argument!");
186 O << (unsigned int)value;
187 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000188 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000189 O << (short)MI->getOperand(OpNo).getImm();
Nate Begemaned428532004-09-04 05:00:00 +0000190 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000191 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000192 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000193 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000194 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000195 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000196 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000197 } else {
198 O << "lo16(";
199 printOp(MI->getOperand(OpNo));
200 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000201 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000202 else
203 O << ')';
204 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000205 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000206 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000207 // Branches can take an immediate operand. This is used by the branch
208 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmand735b802008-10-03 15:45:36 +0000209 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000210 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000211 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000212 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000213 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000214 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000215 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000216 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000217 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000218 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
219 GlobalValue *GV = MO.getGlobal();
Chris Lattner20674332009-07-02 16:08:53 +0000220 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattnera637c322005-12-16 00:22:14 +0000221 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000222 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
223 FnInfo.Init(GV, Mang);
224 O << FnInfo.Stub;
Chris Lattnera637c322005-12-16 00:22:14 +0000225 return;
226 }
227 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000228 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000229 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
230 FnInfo.Init(MO.getSymbolName(), Mang);
231 O << FnInfo.Stub;
Chris Lattner9542f9712005-11-17 19:40:30 +0000232 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000233 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000234 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000235
Chris Lattner9542f9712005-11-17 19:40:30 +0000236 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000237 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000238 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000239 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begeman422b0ce2005-11-16 00:48:01 +0000240 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000241 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000242 O << "\"L" << getFunctionNumber() << "$pb\"\n";
243 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000244 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000245 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000246 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000247 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000248 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000249 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begeman2497e632005-07-21 20:44:43 +0000250 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000251 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000252 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000253 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000254 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000255 else
256 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000257 }
Nate Begemaned428532004-09-04 05:00:00 +0000258 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000259 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000260 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000261 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000262 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000263 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000264 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000265 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000266 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000267 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000268 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000269 else
270 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000271 }
272 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000273 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000274 unsigned CCReg = MI->getOperand(OpNo).getReg();
275 unsigned RegNo = enumRegToMachineReg(CCReg);
276 O << (0x80 >> RegNo);
277 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000278 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000279 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
280 printSymbolLo(MI, OpNo);
281 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000282 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner13feb582006-03-21 17:21:13 +0000283 MI->getOperand(OpNo+1).getReg() == PPC::R0)
284 O << "0";
285 else
286 printOperand(MI, OpNo+1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000287 O << ')';
288 }
Chris Lattnere5ba5802006-03-22 05:26:03 +0000289 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000290 if (MI->getOperand(OpNo).isImm())
Chris Lattnere5ba5802006-03-22 05:26:03 +0000291 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000292 else
Chris Lattnere5ba5802006-03-22 05:26:03 +0000293 printSymbolLo(MI, OpNo);
294 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000295 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattnere5ba5802006-03-22 05:26:03 +0000296 MI->getOperand(OpNo+1).getReg() == PPC::R0)
297 O << "0";
298 else
299 printOperand(MI, OpNo+1);
300 O << ')';
301 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000302
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000303 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000304 // When used as the base register, r0 reads constant zero rather than
305 // the value contained in the register. For this reason, the darwin
306 // assembler requires that we print r0 as 0 (no r) when used as the base.
307 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000308 printRegister(MO, true);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000309 O << ", ";
310 printOperand(MI, OpNo+1);
311 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000312
313 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000314 const char *Modifier);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000315
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000316 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000317
318 virtual void EmitExternalGlobal(const GlobalVariable *GV);
Nate Begemaned428532004-09-04 05:00:00 +0000319 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000320
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000321 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling57f0db82009-02-24 08:30:20 +0000322 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000323 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000324 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000325 const TargetAsmInfo *T, bool V)
326 : PPCAsmPrinter(O, TM, T, V){}
Jim Laskeybf111822006-12-21 20:26:09 +0000327
328 virtual const char *getPassName() const {
329 return "Linux PPC Assembly Printer";
330 }
331
332 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000333
Jim Laskeybf111822006-12-21 20:26:09 +0000334 void getAnalysisUsage(AnalysisUsage &AU) const {
335 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000336 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000337 AU.addRequired<DwarfWriter>();
Jim Laskeybf111822006-12-21 20:26:09 +0000338 PPCAsmPrinter::getAnalysisUsage(AU);
339 }
340
Chris Lattner40bbebd2009-07-21 18:38:57 +0000341 void PrintGlobalVariable(const GlobalVariable *GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000342 };
343
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000344 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
345 /// OS X
Bill Wendling57f0db82009-02-24 08:30:20 +0000346 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene71847812009-07-14 20:18:05 +0000347 formatted_raw_ostream &OS;
Bill Wendling57f0db82009-02-24 08:30:20 +0000348 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000349 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000350 const TargetAsmInfo *T, bool V)
351 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Nate Begemaned428532004-09-04 05:00:00 +0000352
353 virtual const char *getPassName() const {
354 return "Darwin PPC Assembly Printer";
355 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000356
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000357 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000358 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000359 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000360
Jim Laskeyb2efb852006-01-04 22:28:25 +0000361 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000362 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000363 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000364 AU.addRequired<DwarfWriter>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000365 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000366 }
367
Chris Lattner40bbebd2009-07-21 18:38:57 +0000368 void PrintGlobalVariable(const GlobalVariable *GVar);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000369 };
370} // end of anonymous namespace
371
Nate Begemane59bf592004-08-14 22:09:10 +0000372// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000373#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000374
Chris Lattner9ba13e42005-11-17 19:25:59 +0000375void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000376 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000377 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000378 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman97a296f2004-07-21 20:11:11 +0000379
Nate Begeman37efe672006-04-22 18:53:45 +0000380 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000381 printBasicBlockLabel(MO.getMBB());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000382 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000383 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000384 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000385 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000386 // FIXME: PIC relocation model
387 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000388 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000389 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000390 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000392 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000393 // Computing the address of an external symbol, not calling it.
Chris Lattner8f831cb2009-07-15 01:14:44 +0000394 std::string Name(TAI->getGlobalPrefix());
395 Name += MO.getSymbolName();
396
Evan Cheng4c1aa862006-02-22 20:19:42 +0000397 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000398 GVStubs[Name] = Name+"$non_lazy_ptr";
399 Name += "$non_lazy_ptr";
Chris Lattnera637c322005-12-16 00:22:14 +0000400 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000401 O << Name;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000402 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000403 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000404 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000405 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000406 GlobalValue *GV = MO.getGlobal();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000407 std::string Name;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000408
Nate Begemanfcf4a422004-10-17 23:01:34 +0000409 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000410 if (TM.getRelocationModel() != Reloc::Static &&
411 (GV->isDeclaration() || GV->isWeakForLinker())) {
412 if (!GV->hasHiddenVisibility()) {
413 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
414 GVStubs[Mang->getMangledName(GV)] = Name;
415 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
416 GV->hasAvailableExternallyLinkage()) {
417 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
418 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
419 } else {
420 Name = Mang->getMangledName(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000421 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000422 } else {
423 Name = Mang->getMangledName(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000424 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000425 O << Name;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000426
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000427 printOffset(MO.getOffset());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000429 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000430
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000431 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000432 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000433 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000434 }
435}
436
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000437/// EmitExternalGlobal - In this case we need to use the indirect symbol.
438///
439void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling7d16e852009-04-10 00:12:49 +0000440 std::string Name;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000441
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000442 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000443 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +0000444 } else {
445 Name = Mang->getMangledName(GV);
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000446 }
447 O << Name;
448}
449
Chris Lattnere3f01572006-02-23 19:31:10 +0000450/// PrintAsmOperand - Print out an operand for an inline asm expression.
451///
452bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000453 unsigned AsmVariant,
Chris Lattnere3f01572006-02-23 19:31:10 +0000454 const char *ExtraCode) {
455 // Does this asm operand have a single letter operand modifier?
456 if (ExtraCode && ExtraCode[0]) {
457 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000458
Chris Lattnere3f01572006-02-23 19:31:10 +0000459 switch (ExtraCode[0]) {
460 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000461 case 'c': // Don't print "$" before a global var name or constant.
462 // PPC never has a prefix.
463 printOperand(MI, OpNo);
464 return false;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000465 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000466 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000467 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000468 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000469 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000470 return true;
471 ++OpNo; // Return the high-part.
472 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000473 case 'I':
474 // Write 'i' if an integer constant, otherwise nothing. Used to print
475 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000476 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000477 O << "i";
478 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000479 }
480 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000481
Chris Lattnere3f01572006-02-23 19:31:10 +0000482 printOperand(MI, OpNo);
483 return false;
484}
485
Chris Lattner2c003e22006-02-24 20:27:40 +0000486bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000487 unsigned AsmVariant,
Chris Lattner2c003e22006-02-24 20:27:40 +0000488 const char *ExtraCode) {
489 if (ExtraCode && ExtraCode[0])
490 return true; // Unknown modifier.
Dan Gohmand735b802008-10-03 15:45:36 +0000491 if (MI->getOperand(OpNo).isReg())
Chris Lattner9aa28952007-02-01 00:39:08 +0000492 printMemRegReg(MI, OpNo);
493 else
494 printMemRegImm(MI, OpNo);
Chris Lattner2c003e22006-02-24 20:27:40 +0000495 return false;
496}
Chris Lattnere3f01572006-02-23 19:31:10 +0000497
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000498void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000499 const char *Modifier) {
500 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
501 unsigned Code = MI->getOperand(OpNo).getImm();
502 if (!strcmp(Modifier, "cc")) {
503 switch ((PPC::Predicate)Code) {
504 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
505 case PPC::PRED_LT: O << "lt"; return;
506 case PPC::PRED_LE: O << "le"; return;
507 case PPC::PRED_EQ: O << "eq"; return;
508 case PPC::PRED_GE: O << "ge"; return;
509 case PPC::PRED_GT: O << "gt"; return;
510 case PPC::PRED_NE: O << "ne"; return;
511 case PPC::PRED_UN: O << "un"; return;
512 case PPC::PRED_NU: O << "nu"; return;
513 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000514
Chris Lattneraf53a872006-11-04 05:27:39 +0000515 } else {
516 assert(!strcmp(Modifier, "reg") &&
517 "Need to specify 'cc' or 'reg' as predicate op modifier!");
518 // Don't print the register for 'always'.
519 if (Code == PPC::PRED_ALWAYS) return;
520 printOperand(MI, OpNo+1);
521 }
522}
523
524
Nate Begemane59bf592004-08-14 22:09:10 +0000525/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
526/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000527///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000528void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000529 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000530
Nate Begemanad5f65c2005-04-05 18:19:50 +0000531 // Check for slwi/srwi mnemonics.
532 if (MI->getOpcode() == PPC::RLWINM) {
533 bool FoundMnemonic = false;
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000534 unsigned char SH = MI->getOperand(2).getImm();
535 unsigned char MB = MI->getOperand(3).getImm();
536 unsigned char ME = MI->getOperand(4).getImm();
Nate Begemanad5f65c2005-04-05 18:19:50 +0000537 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000538 O << "\tslwi "; FoundMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000539 }
540 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000541 O << "\tsrwi "; FoundMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000542 SH = 32-SH;
543 }
544 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000545 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000546 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000547 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000548 O << ", " << (unsigned int)SH << '\n';
Nate Begemanad5f65c2005-04-05 18:19:50 +0000549 return;
550 }
Chris Lattnerb410dc92006-06-20 23:18:58 +0000551 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000552 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000553 O << "\tmr ";
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000554 printOperand(MI, 0);
555 O << ", ";
556 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000557 O << '\n';
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000558 return;
559 }
Chris Lattner566c1b12006-11-18 01:23:56 +0000560 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000561 unsigned char SH = MI->getOperand(2).getImm();
562 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner566c1b12006-11-18 01:23:56 +0000563 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
564 if (63-SH == ME) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000565 O << "\tsldi ";
Chris Lattner566c1b12006-11-18 01:23:56 +0000566 printOperand(MI, 0);
567 O << ", ";
568 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000569 O << ", " << (unsigned int)SH << '\n';
Chris Lattner566c1b12006-11-18 01:23:56 +0000570 return;
571 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000572 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000573
Chris Lattner2698cb62009-08-08 00:05:42 +0000574 printInstruction(MI);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000575}
576
Jim Laskeybf111822006-12-21 20:26:09 +0000577/// runOnMachineFunction - This uses the printMachineInstruction()
578/// method to print assembly for each instruction.
579///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000580bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000581 this->MF = &MF;
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000582
Jim Laskeybf111822006-12-21 20:26:09 +0000583 SetupMachineFunction(MF);
584 O << "\n\n";
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000585
Jim Laskeybf111822006-12-21 20:26:09 +0000586 // Print out constants referenced by the function
587 EmitConstantPool(MF.getConstantPool());
588
589 // Print out labels for the function.
590 const Function *F = MF.getFunction();
Chris Lattnere53a6002009-07-29 05:09:30 +0000591 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000592
Jim Laskeybf111822006-12-21 20:26:09 +0000593 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000594 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000595 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000596 case Function::LinkerPrivateLinkage:
Jim Laskeybf111822006-12-21 20:26:09 +0000597 case Function::InternalLinkage: // Symbols default to internal.
598 break;
599 case Function::ExternalLinkage:
600 O << "\t.global\t" << CurrentFnName << '\n'
601 << "\t.type\t" << CurrentFnName << ", @function\n";
602 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000603 case Function::WeakAnyLinkage:
604 case Function::WeakODRLinkage:
605 case Function::LinkOnceAnyLinkage:
606 case Function::LinkOnceODRLinkage:
Jim Laskeybf111822006-12-21 20:26:09 +0000607 O << "\t.global\t" << CurrentFnName << '\n';
608 O << "\t.weak\t" << CurrentFnName << '\n';
609 break;
610 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000611
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000612 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000613
Bill Wendling20c568f2009-06-30 22:38:32 +0000614 EmitAlignment(MF.getAlignment(), F);
Jim Laskeybf111822006-12-21 20:26:09 +0000615 O << CurrentFnName << ":\n";
616
617 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000618 DW->BeginFunction(&MF);
Jim Laskeybf111822006-12-21 20:26:09 +0000619
620 // Print out code for the function.
621 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
622 I != E; ++I) {
623 // Print a label for the basic block.
624 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000625 printBasicBlockLabel(I, true, true);
Jim Laskeybf111822006-12-21 20:26:09 +0000626 O << '\n';
627 }
628 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
629 II != E; ++II) {
630 // Print the assembly for the instruction.
Jim Laskeybf111822006-12-21 20:26:09 +0000631 printMachineInstruction(II);
632 }
633 }
634
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000635 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Jim Laskeybf111822006-12-21 20:26:09 +0000636
637 // Print out jump tables referenced by the function.
638 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000639
Chris Lattnere53a6002009-07-29 05:09:30 +0000640 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesenf94a3272008-12-03 19:33:10 +0000641
Jim Laskeybf111822006-12-21 20:26:09 +0000642 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000643 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000644
Jim Laskeybf111822006-12-21 20:26:09 +0000645 // We didn't modify anything.
646 return false;
647}
648
Chris Lattnerec321b42008-02-15 19:04:54 +0000649/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000650/// Don't print things like \\n or \\0.
Daniel Dunbar93b67e42009-07-26 07:49:05 +0000651static void PrintUnmangledNameSafely(const Value *V,
652 formatted_raw_ostream &OS) {
653 for (StringRef::iterator it = V->getName().begin(),
654 ie = V->getName().end(); it != ie; ++it)
655 if (isprint(*it))
656 OS << *it;
Chris Lattnerec321b42008-02-15 19:04:54 +0000657}
658
Chris Lattner40bbebd2009-07-21 18:38:57 +0000659void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Jim Laskeybf111822006-12-21 20:26:09 +0000660 const TargetData *TD = TM.getTargetData();
661
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000662 if (!GVar->hasInitializer())
663 return; // External global require no code
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000664
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000665 // Check to see if this is a special global used by LLVM, if so, emit it.
666 if (EmitSpecialLLVMGlobal(GVar))
667 return;
Chris Lattner70d41072007-01-14 06:37:54 +0000668
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000669 std::string name = Mang->getMangledName(GVar);
Chris Lattner70d41072007-01-14 06:37:54 +0000670
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000671 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000672
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000673 Constant *C = GVar->getInitializer();
674 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000675 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000676 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000677
Chris Lattnere53a6002009-07-29 05:09:30 +0000678 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000679
680 if (C->isNullValue() && /* FIXME: Verify correct */
681 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000682 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000683 GVar->isWeakForLinker())) {
Jim Laskeybf111822006-12-21 20:26:09 +0000684 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000685
686 if (GVar->hasExternalLinkage()) {
Jim Laskeybf111822006-12-21 20:26:09 +0000687 O << "\t.global " << name << '\n';
688 O << "\t.type " << name << ", @object\n";
Nick Lewyckye2b90522007-07-25 03:48:45 +0000689 O << name << ":\n";
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000690 O << "\t.zero " << Size << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000691 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000692 O << TAI->getLCOMMDirective() << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000693 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000694 O << ".comm " << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000695 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000696 if (VerboseAsm) {
697 O << "\t\t" << TAI->getCommentString() << " '";
698 PrintUnmangledNameSafely(GVar, O);
699 O << "'";
700 }
701 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000702 return;
Jim Laskeybf111822006-12-21 20:26:09 +0000703 }
704
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000705 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000706 case GlobalValue::LinkOnceAnyLinkage:
707 case GlobalValue::LinkOnceODRLinkage:
708 case GlobalValue::WeakAnyLinkage:
709 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000710 case GlobalValue::CommonLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000711 O << "\t.global " << name << '\n'
712 << "\t.type " << name << ", @object\n"
713 << "\t.weak " << name << '\n';
714 break;
715 case GlobalValue::AppendingLinkage:
716 // FIXME: appending linkage variables should go into a section of
717 // their name or something. For now, just emit them as external.
718 case GlobalValue::ExternalLinkage:
719 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000720 O << "\t.global " << name << '\n'
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000721 << "\t.type " << name << ", @object\n";
722 // FALL THROUGH
723 case GlobalValue::InternalLinkage:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000724 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000725 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000726 break;
727 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000728 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000729 }
730
731 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000732 O << name << ":";
733 if (VerboseAsm) {
734 O << "\t\t\t\t" << TAI->getCommentString() << " '";
735 PrintUnmangledNameSafely(GVar, O);
736 O << "'";
737 }
738 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000739
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000740 EmitGlobalConstant(C);
741 O << '\n';
742}
743
Jim Laskeybf111822006-12-21 20:26:09 +0000744
Nate Begemaned428532004-09-04 05:00:00 +0000745/// runOnMachineFunction - This uses the printMachineInstruction()
746/// method to print assembly for each instruction.
747///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000748bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000749 this->MF = &MF;
750
Chris Lattner8b8b9512005-11-21 07:51:23 +0000751 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000752 O << "\n\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000753
Nate Begemaned428532004-09-04 05:00:00 +0000754 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000755 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000756
757 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000758 const Function *F = MF.getFunction();
Chris Lattnere53a6002009-07-29 05:09:30 +0000759 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000760
Chris Lattnera637c322005-12-16 00:22:14 +0000761 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000762 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng1902a122009-01-25 06:32:01 +0000763 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000764 case Function::LinkerPrivateLinkage:
Chris Lattnera637c322005-12-16 00:22:14 +0000765 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnera637c322005-12-16 00:22:14 +0000766 break;
767 case Function::ExternalLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000768 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000769 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000770 case Function::WeakAnyLinkage:
771 case Function::WeakODRLinkage:
772 case Function::LinkOnceAnyLinkage:
773 case Function::LinkOnceODRLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000774 O << "\t.globl\t" << CurrentFnName << '\n';
775 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000776 break;
777 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000778
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000779 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000780
Bill Wendling20c568f2009-06-30 22:38:32 +0000781 EmitAlignment(MF.getAlignment(), F);
Nate Begemaned428532004-09-04 05:00:00 +0000782 O << CurrentFnName << ":\n";
783
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000784 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000785 DW->BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000786
Bill Wendling381802f2008-01-26 06:51:24 +0000787 // If the function is empty, then we need to emit *something*. Otherwise, the
788 // function's label might be associated with something that it wasn't meant to
789 // be associated with. We emit a noop in this situation.
790 MachineFunction::iterator I = MF.begin();
791
Bill Wendling824a7212008-01-26 09:03:52 +0000792 if (++I == MF.end() && MF.front().empty())
793 O << "\tnop\n";
Bill Wendling381802f2008-01-26 06:51:24 +0000794
Nate Begemaned428532004-09-04 05:00:00 +0000795 // Print out code for the function.
796 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
797 I != E; ++I) {
798 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000799 if (I != MF.begin()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000800 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000801 O << '\n';
Chris Lattner1db1adb2005-08-21 19:09:33 +0000802 }
Bill Wendling381802f2008-01-26 06:51:24 +0000803 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
804 II != IE; ++II) {
Nate Begemaned428532004-09-04 05:00:00 +0000805 // Print the assembly for the instruction.
Nate Begemaned428532004-09-04 05:00:00 +0000806 printMachineInstruction(II);
807 }
808 }
Nate Begemaned428532004-09-04 05:00:00 +0000809
Chris Lattnerfea13d32006-10-05 00:26:05 +0000810 // Print out jump tables referenced by the function.
Chris Lattner1da31ee2006-10-05 03:01:21 +0000811 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000812
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000813 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000814 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000815
Nate Begemaned428532004-09-04 05:00:00 +0000816 // We didn't modify anything.
817 return false;
818}
819
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000820
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000821bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000822 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000823 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000824 "ppc",
825 "ppc601",
826 "ppc602",
827 "ppc603",
828 "ppc7400",
829 "ppc750",
830 "ppc970",
831 "ppc64"
832 };
833
834 unsigned Directive = Subtarget.getDarwinDirective();
835 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
836 Directive = PPC::DIR_970;
837 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
838 Directive = PPC::DIR_7400;
839 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
840 Directive = PPC::DIR_64;
841 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000842 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000843
Dan Gohmanb8275a32007-07-25 19:33:14 +0000844 bool Result = AsmPrinter::doInitialization(M);
Devang Patel66b4d3b2009-06-20 01:00:07 +0000845 assert(MMI);
Dale Johannesen94618eb2008-07-09 20:43:39 +0000846
Jim Laskey2ada0852006-11-28 18:21:52 +0000847 // Prime text sections so they are adjacent. This reduces the likelihood a
848 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattner2dfddee2009-08-03 22:52:21 +0000849
850 TargetLoweringObjectFileMachO &TLOFMacho =
851 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
852 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__textcoal_nt,"
853 "coalesced,pure_instructions", true,
854 SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000855 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000856 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstu"
857 "b1,symbol_stubs,"
858 "pure_instructions,32", true,
859 SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000860 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000861 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1"
862 ",symbol_stubs,"
863 "pure_instructions,16", true,
864 SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000865 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000866 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000867
Dan Gohmanb8275a32007-07-25 19:33:14 +0000868 return Result;
Nate Begeman18ed0292005-07-21 01:25:49 +0000869}
870
Chris Lattner40bbebd2009-07-21 18:38:57 +0000871void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000872 const TargetData *TD = TM.getTargetData();
873
874 if (!GVar->hasInitializer())
875 return; // External global require no code
876
877 // Check to see if this is a special global used by LLVM, if so, emit it.
878 if (EmitSpecialLLVMGlobal(GVar)) {
879 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000880 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000881 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000882 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000883 O << ".reference .destructors_used\n";
884 }
885 return;
886 }
887
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000888 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000889 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000890
891 Constant *C = GVar->getInitializer();
892 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000893 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000894 unsigned Align = TD->getPreferredAlignmentLog(GVar);
895
Chris Lattnera87dea42009-07-31 18:48:30 +0000896 const MCSection *TheSection =
Chris Lattnere53a6002009-07-29 05:09:30 +0000897 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattnerfb3431a2009-07-24 03:49:17 +0000898 SwitchToSection(TheSection);
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000899
Chris Lattner3b24c012009-08-04 05:35:56 +0000900 /// FIXME: Drive this off the section!
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000901 if (C->isNullValue() && /* FIXME: Verify correct */
902 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000903 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000904 GVar->isWeakForLinker()) &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000905 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000906 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000907 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
908
909 if (GVar->hasExternalLinkage()) {
910 O << "\t.globl " << name << '\n';
911 O << "\t.zerofill __DATA, __common, " << name << ", "
912 << Size << ", " << Align;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000913 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000914 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000915 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000916 O << "\t.globl " << name << '\n'
917 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000918 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000919 O << name << ":";
920 if (VerboseAsm) {
921 O << "\t\t\t\t" << TAI->getCommentString() << " ";
922 PrintUnmangledNameSafely(GVar, O);
923 }
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000924 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000925 EmitGlobalConstant(C);
926 return;
927 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000928 O << ".comm " << name << ',' << Size;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000929 // Darwin 9 and above support aligned common data.
930 if (Subtarget.isDarwin9())
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000931 O << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000932 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000933 if (VerboseAsm) {
934 O << "\t\t" << TAI->getCommentString() << " '";
935 PrintUnmangledNameSafely(GVar, O);
936 O << "'";
937 }
938 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000939 return;
940 }
941
942 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000943 case GlobalValue::LinkOnceAnyLinkage:
944 case GlobalValue::LinkOnceODRLinkage:
945 case GlobalValue::WeakAnyLinkage:
946 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000947 case GlobalValue::CommonLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000948 O << "\t.globl " << name << '\n'
949 << "\t.weak_definition " << name << '\n';
950 break;
951 case GlobalValue::AppendingLinkage:
952 // FIXME: appending linkage variables should go into a section of
953 // their name or something. For now, just emit them as external.
954 case GlobalValue::ExternalLinkage:
955 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000956 O << "\t.globl " << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000957 // FALL THROUGH
958 case GlobalValue::InternalLinkage:
Evan Cheng1902a122009-01-25 06:32:01 +0000959 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000960 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000961 break;
962 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000963 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000964 }
965
966 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000967 O << name << ":";
968 if (VerboseAsm) {
969 O << "\t\t\t\t" << TAI->getCommentString() << " '";
970 PrintUnmangledNameSafely(GVar, O);
971 O << "'";
972 }
973 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000974
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000975 EmitGlobalConstant(C);
976 O << '\n';
977}
978
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000979bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000980 const TargetData *TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000981
Chris Lattner7e097e42006-06-27 01:02:25 +0000982 bool isPPC64 = TD->getPointerSizeInBits() == 64;
983
Chris Lattner2dfddee2009-08-03 22:52:21 +0000984 // Darwin/PPC always uses mach-o.
985 TargetLoweringObjectFileMachO &TLOFMacho =
986 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
987
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000988 // Output stubs for dynamically-linked functions
Chris Lattnerea56f102009-07-15 00:55:58 +0000989 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000990 const MCSection *StubSection =
991 TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstub1,"
992 "symbol_stubs,pure_instructions,32", true,
993 SectionKind::getText());
994 const MCSection *LSPSection =
995 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
996 SectionKind::getMetadata());
997
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000998 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000999 I != E; ++I) {
Chris Lattner2dfddee2009-08-03 22:52:21 +00001000 SwitchToSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +00001001 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001002 const FnStubInfo &Info = I->second;
Chris Lattnerdefd3002009-07-15 02:33:19 +00001003 O << Info.Stub << ":\n";
1004 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +00001005 O << "\tmflr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001006 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1007 O << Info.AnonSymbol << ":\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001008 O << "\tmflr r11\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001009 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chengae94e592008-12-05 01:06:39 +00001010 O << ")\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001011 O << "\tmtlr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001012 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1013 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001014 O << "\tmtctr r12\n";
1015 O << "\tbctr\n";
Chris Lattnerfe2fe702009-07-16 01:23:26 +00001016
Chris Lattner2dfddee2009-08-03 22:52:21 +00001017 SwitchToSection(LSPSection);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001018 O << Info.LazyPtr << ":\n";
1019 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001020 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001021 }
Chris Lattnerea56f102009-07-15 00:55:58 +00001022 } else if (!FnStubs.empty()) {
Chris Lattner2dfddee2009-08-03 22:52:21 +00001023 const MCSection *StubSection =
1024 TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1025 "pure_instructions,16", true,
1026 SectionKind::getText());
1027 const MCSection *LSPSection =
1028 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1029 SectionKind::getMetadata());
1030
Chris Lattner9ec8fac2009-07-15 02:28:57 +00001031 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +00001032 I != E; ++I) {
Chris Lattner2dfddee2009-08-03 22:52:21 +00001033 SwitchToSection(StubSection);
Chris Lattnerdeea4162005-12-13 04:33:58 +00001034 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001035 const FnStubInfo &Info = I->second;
1036 O << Info.Stub << ":\n";
1037 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1038 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattner1a87c052009-07-15 02:36:21 +00001039 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001040 O << Info.LazyPtr << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001041 O << "\tmtctr r12\n";
1042 O << "\tbctr\n";
Chris Lattner2dfddee2009-08-03 22:52:21 +00001043 SwitchToSection(LSPSection);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001044 O << Info.LazyPtr << ":\n";
1045 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001046 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +00001047 }
Misha Brukman46fd00a2004-06-24 23:04:11 +00001048 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001049
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001050 O << '\n';
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001051
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001052 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001053 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001054 // Only referenced functions get into the Personalities list.
Chris Lattner0de1fc42009-06-24 19:09:55 +00001055 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001056 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +00001057 E = Personalities.end(); I != E; ++I) {
1058 if (*I)
1059 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner0f6c8f22009-07-15 01:16:38 +00001060 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +00001061 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001062 }
1063
Chris Lattner2dfddee2009-08-03 22:52:21 +00001064 // Output macho stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001065 if (!GVStubs.empty()) {
Chris Lattner2dfddee2009-08-03 22:52:21 +00001066 const MCSection *TheSection =
1067 TLOFMacho.getMachOSection(".non_lazy_symbol_pointer", true,
1068 SectionKind::getMetadata());
1069 SwitchToSection(TheSection);
Chris Lattner8f831cb2009-07-15 01:14:44 +00001070 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1071 E = GVStubs.end(); I != E; ++I) {
1072 O << I->second << ":\n";
1073 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1074 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001075 }
Nate Begemane59bf592004-08-14 22:09:10 +00001076 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001077
Evan Chengae94e592008-12-05 01:06:39 +00001078 if (!HiddenGVStubs.empty()) {
Chris Lattnerf0144122009-07-28 03:13:23 +00001079 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +00001080 EmitAlignment(isPPC64 ? 3 : 2);
1081 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1082 E = HiddenGVStubs.end(); I != E; ++I) {
1083 O << I->second << ":\n";
1084 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chengae94e592008-12-05 01:06:39 +00001085 }
1086 }
1087
Chris Lattner5b0ac992005-11-01 00:12:36 +00001088 // Funny Darwin hack: This flag tells the linker that no global symbols
1089 // contain code that falls through to other global symbols (e.g. the obvious
1090 // implementation of multiple entry points). If this doesn't occur, the
1091 // linker can safely perform dead code stripping. Since LLVM never generates
1092 // code that does this, it is always safe to set.
Chris Lattner4da1c822006-09-20 17:12:19 +00001093 O << "\t.subsections_via_symbols\n";
Chris Lattner5b0ac992005-11-01 00:12:36 +00001094
Dan Gohmanb8275a32007-07-25 19:33:14 +00001095 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001096}
Nate Begemaned428532004-09-04 05:00:00 +00001097
Chris Lattner4da1c822006-09-20 17:12:19 +00001098
1099
Jim Laskeyb608a4d2006-12-20 20:56:46 +00001100/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1101/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +00001102/// Darwin assembler can deal with.
1103///
Daniel Dunbar0c795d62009-07-25 06:49:55 +00001104static FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001105 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +00001106 bool verbose) {
Jim Laskeybf111822006-12-21 20:26:09 +00001107 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1108
Chris Lattner40bbebd2009-07-21 18:38:57 +00001109 if (Subtarget->isDarwin())
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +00001110 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattner40bbebd2009-07-21 18:38:57 +00001111 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattner4da1c822006-09-20 17:12:19 +00001112}
Anton Korobeynikov06be9972008-08-17 13:54:28 +00001113
Bob Wilsona96751f2009-06-23 23:59:40 +00001114// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001115extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001116 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1117
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001118 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1119}