blob: a4fcdf661b917e57e63723c8d51338020b1c26ef [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"
Devang Patele4c0c0f2009-06-25 00:47:42 +000027#include "llvm/MDNode.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000029#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000030#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Misha Brukman05794492004-06-24 17:31:42 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000033#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling381802f2008-01-26 06:51:24 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000035#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000036#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000039#include "llvm/Support/ErrorHandling.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000040#include "llvm/Support/Compiler.h"
David Greene71847812009-07-14 20:18:05 +000041#include "llvm/Support/FormattedStream.h"
Jim Laskey563321a2006-09-06 18:34:40 +000042#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000043#include "llvm/Target/TargetRegisterInfo.h"
Nate Begemand4c8bea2004-11-25 07:09:01 +000044#include "llvm/Target/TargetInstrInfo.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000045#include "llvm/Target/TargetOptions.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000046#include "llvm/Target/TargetRegistry.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000047#include "llvm/ADT/Statistic.h"
48#include "llvm/ADT/StringExtras.h"
Evan Chengae94e592008-12-05 01:06:39 +000049#include "llvm/ADT/StringSet.h"
Chris Lattnera3840792004-08-16 23:25:21 +000050using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000051
Chris Lattner95b2c7d2006-12-19 22:59:26 +000052STATISTIC(EmittedInsts, "Number of machine instrs printed");
Misha Brukman5dfe3a92004-06-21 16:55:25 +000053
Chris Lattner95b2c7d2006-12-19 22:59:26 +000054namespace {
Bill Wendling57f0db82009-02-24 08:30:20 +000055 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
56 protected:
Chris Lattner9ec8fac2009-07-15 02:28:57 +000057 struct FnStubInfo {
58 std::string Stub, LazyPtr, AnonSymbol;
59
60 FnStubInfo() {}
61
62 void Init(const GlobalValue *GV, Mangler *Mang) {
63 // Already initialized.
64 if (!Stub.empty()) return;
65 Stub = Mang->getMangledName(GV, "$stub", true);
66 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
67 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
68 }
69
70 void Init(const std::string &GV, Mangler *Mang) {
71 // Already initialized.
72 if (!Stub.empty()) return;
Bill Wendling3d10a5a2009-07-20 01:03:30 +000073 Stub = Mang->makeNameProper(GV + "$stub",
74 Mangler::PrivatePrefixTy);
75 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
76 Mangler::PrivatePrefixTy);
77 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
78 Mangler::PrivatePrefixTy);
Chris Lattner9ec8fac2009-07-15 02:28:57 +000079 }
80 };
81
82 StringMap<FnStubInfo> FnStubs;
Chris Lattner8f831cb2009-07-15 01:14:44 +000083 StringMap<std::string> GVStubs, HiddenGVStubs;
Chris Lattnerdadceed2006-09-20 17:07:15 +000084 const PPCSubtarget &Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000085 public:
David Greene71847812009-07-14 20:18:05 +000086 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000087 const TargetAsmInfo *T, bool V)
88 : AsmPrinter(O, TM, T, V),
Bill Wendling57f0db82009-02-24 08:30:20 +000089 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000090
Misha Brukman5dfe3a92004-06-21 16:55:25 +000091 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000092 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000093 }
94
Nate Begeman21e463b2005-10-16 05:39:50 +000095 PPCTargetMachine &getTM() {
96 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000097 }
98
Nate Begemanadeb43d2005-07-20 22:42:00 +000099 unsigned enumRegToMachineReg(unsigned enumReg) {
100 switch (enumReg) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000101 default: llvm_unreachable("Unhandled register!");
Nate Begemanadeb43d2005-07-20 22:42:00 +0000102 case PPC::CR0: return 0;
103 case PPC::CR1: return 1;
104 case PPC::CR2: return 2;
105 case PPC::CR3: return 3;
106 case PPC::CR4: return 4;
107 case PPC::CR5: return 5;
108 case PPC::CR6: return 6;
109 case PPC::CR7: return 7;
110 }
Torok Edwinc23197a2009-07-14 16:55:14 +0000111 llvm_unreachable(0);
Nate Begemanadeb43d2005-07-20 22:42:00 +0000112 }
113
Nate Begemane59bf592004-08-14 22:09:10 +0000114 /// printInstruction - This method is automatically generated by tablegen
115 /// from the instruction set description. This method returns true if the
116 /// machine instruction was sufficiently described to print it, otherwise it
117 /// returns false.
118 bool printInstruction(const MachineInstr *MI);
119
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000120 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +0000121 void printOp(const MachineOperand &MO);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000122
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000123 /// stripRegisterPrefix - This method strips the character prefix from a
124 /// register name so that only the number is left. Used by for linux asm.
Jim Laskey40ee69f2006-12-20 21:33:34 +0000125 const char *stripRegisterPrefix(const char *RegName) {
126 switch (RegName[0]) {
127 case 'r':
128 case 'f':
129 case 'v': return RegName + 1;
Jim Laskey2aa14aa2006-12-20 21:35:00 +0000130 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000131 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000132
Jim Laskey40ee69f2006-12-20 21:33:34 +0000133 return RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000134 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000135
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000136 /// printRegister - Print register according to target requirements.
137 ///
138 void printRegister(const MachineOperand &MO, bool R0AsZero) {
139 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000140 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000141
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000142 // If we should use 0 for R0.
143 if (R0AsZero && RegNo == PPC::R0) {
144 O << "0";
145 return;
146 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000147
Bill Wendling74ab84c2008-02-26 21:11:01 +0000148 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000149 // Linux assembler (Others?) does not take register mnemonics.
150 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskey40ee69f2006-12-20 21:33:34 +0000151 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
152 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000153 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000154
Chris Lattner58873272006-02-01 22:38:46 +0000155 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000156 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmand735b802008-10-03 15:45:36 +0000157 if (MO.isReg()) {
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000158 printRegister(MO, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000159 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000160 O << MO.getImm();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000161 } else {
162 printOp(MO);
163 }
164 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000165
Chris Lattner58873272006-02-01 22:38:46 +0000166 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000167 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000168 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
169 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000170
171
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000172 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000173 char value = MI->getOperand(OpNo).getImm();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000174 value = (value << (32-5)) >> (32-5);
175 O << (int)value;
176 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000177 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000178 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattner12585ba2004-08-21 19:11:03 +0000179 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000180 O << (unsigned int)value;
181 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000182 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000183 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman07aada82004-08-30 02:28:06 +0000184 assert(value <= 63 && "Invalid u6imm argument!");
185 O << (unsigned int)value;
186 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000187 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000188 O << (short)MI->getOperand(OpNo).getImm();
Nate Begemaned428532004-09-04 05:00:00 +0000189 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000190 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000191 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000192 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000193 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000194 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000195 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000196 } else {
197 O << "lo16(";
198 printOp(MI->getOperand(OpNo));
199 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000200 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000201 else
202 O << ')';
203 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000204 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000205 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000206 // Branches can take an immediate operand. This is used by the branch
207 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmand735b802008-10-03 15:45:36 +0000208 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000209 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000210 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000211 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000212 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000213 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000214 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000215 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000216 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000217 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
218 GlobalValue *GV = MO.getGlobal();
Chris Lattner20674332009-07-02 16:08:53 +0000219 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattnera637c322005-12-16 00:22:14 +0000220 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000221 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
222 FnInfo.Init(GV, Mang);
223 O << FnInfo.Stub;
Chris Lattnera637c322005-12-16 00:22:14 +0000224 return;
225 }
226 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000227 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000228 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
229 FnInfo.Init(MO.getSymbolName(), Mang);
230 O << FnInfo.Stub;
Chris Lattner9542f9712005-11-17 19:40:30 +0000231 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000232 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000233 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000234
Chris Lattner9542f9712005-11-17 19:40:30 +0000235 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000236 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000237 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000238 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begeman422b0ce2005-11-16 00:48:01 +0000239 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000240 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000243 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000244 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000245 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000246 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000247 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000248 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begeman2497e632005-07-21 20:44:43 +0000249 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000250 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000251 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000252 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000253 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000254 else
255 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000256 }
Nate Begemaned428532004-09-04 05:00:00 +0000257 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000258 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000259 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000260 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000261 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000262 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000263 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000264 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000265 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000266 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000267 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000268 else
269 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000270 }
271 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000272 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000273 unsigned CCReg = MI->getOperand(OpNo).getReg();
274 unsigned RegNo = enumRegToMachineReg(CCReg);
275 O << (0x80 >> RegNo);
276 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000277 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000278 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
279 printSymbolLo(MI, OpNo);
280 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000281 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner13feb582006-03-21 17:21:13 +0000282 MI->getOperand(OpNo+1).getReg() == PPC::R0)
283 O << "0";
284 else
285 printOperand(MI, OpNo+1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000286 O << ')';
287 }
Chris Lattnere5ba5802006-03-22 05:26:03 +0000288 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000289 if (MI->getOperand(OpNo).isImm())
Chris Lattnere5ba5802006-03-22 05:26:03 +0000290 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000291 else
Chris Lattnere5ba5802006-03-22 05:26:03 +0000292 printSymbolLo(MI, OpNo);
293 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000294 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattnere5ba5802006-03-22 05:26:03 +0000295 MI->getOperand(OpNo+1).getReg() == PPC::R0)
296 O << "0";
297 else
298 printOperand(MI, OpNo+1);
299 O << ')';
300 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000301
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000302 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000303 // When used as the base register, r0 reads constant zero rather than
304 // the value contained in the register. For this reason, the darwin
305 // assembler requires that we print r0 as 0 (no r) when used as the base.
306 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000307 printRegister(MO, true);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000308 O << ", ";
309 printOperand(MI, OpNo+1);
310 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000311
312 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000313 const char *Modifier);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000314
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000315 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000316 virtual bool doFinalization(Module &M) = 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);
Jim Laskeybf111822006-12-21 20:26:09 +0000333 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000334
Jim Laskeybf111822006-12-21 20:26:09 +0000335 void getAnalysisUsage(AnalysisUsage &AU) const {
336 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000337 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000338 AU.addRequired<DwarfWriter>();
Jim Laskeybf111822006-12-21 20:26:09 +0000339 PPCAsmPrinter::getAnalysisUsage(AU);
340 }
341
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000342 void printModuleLevelGV(const GlobalVariable* GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000343 };
344
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000345 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
346 /// OS X
Bill Wendling57f0db82009-02-24 08:30:20 +0000347 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene71847812009-07-14 20:18:05 +0000348 formatted_raw_ostream &OS;
Bill Wendling57f0db82009-02-24 08:30:20 +0000349 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000350 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000351 const TargetAsmInfo *T, bool V)
352 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Nate Begemaned428532004-09-04 05:00:00 +0000353
354 virtual const char *getPassName() const {
355 return "Darwin PPC Assembly Printer";
356 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000357
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000358 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000359 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000360 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000361
Jim Laskeyb2efb852006-01-04 22:28:25 +0000362 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000363 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000364 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000365 AU.addRequired<DwarfWriter>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000366 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000367 }
368
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000369 void printModuleLevelGV(const GlobalVariable* GVar);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000370 };
371} // end of anonymous namespace
372
Nate Begemane59bf592004-08-14 22:09:10 +0000373// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000374#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000375
Chris Lattner9ba13e42005-11-17 19:25:59 +0000376void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000377 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000378 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000379 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman97a296f2004-07-21 20:11:11 +0000380
Nate Begeman37efe672006-04-22 18:53:45 +0000381 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000382 printBasicBlockLabel(MO.getMBB());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000383 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000384 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000385 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000386 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000387 // FIXME: PIC relocation model
388 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000389 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000390 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000391 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000392 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000393 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000394 // Computing the address of an external symbol, not calling it.
Chris Lattner8f831cb2009-07-15 01:14:44 +0000395 std::string Name(TAI->getGlobalPrefix());
396 Name += MO.getSymbolName();
397
Evan Cheng4c1aa862006-02-22 20:19:42 +0000398 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000399 GVStubs[Name] = Name+"$non_lazy_ptr";
400 Name += "$non_lazy_ptr";
Chris Lattnera637c322005-12-16 00:22:14 +0000401 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000402 O << Name;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000403 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000404 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000405 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000406 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000407 GlobalValue *GV = MO.getGlobal();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000408 std::string Name;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000409
Nate Begemanfcf4a422004-10-17 23:01:34 +0000410 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000411 if (TM.getRelocationModel() != Reloc::Static &&
412 (GV->isDeclaration() || GV->isWeakForLinker())) {
413 if (!GV->hasHiddenVisibility()) {
414 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
415 GVStubs[Mang->getMangledName(GV)] = Name;
416 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
417 GV->hasAvailableExternallyLinkage()) {
418 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
419 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
420 } else {
421 Name = Mang->getMangledName(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000422 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000423 } else {
424 Name = Mang->getMangledName(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000425 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000426 O << Name;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000427
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000428 printOffset(MO.getOffset());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000429 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000430 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000431
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000432 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000433 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000434 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000435 }
436}
437
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000438/// EmitExternalGlobal - In this case we need to use the indirect symbol.
439///
440void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling7d16e852009-04-10 00:12:49 +0000441 std::string Name;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000442
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000443 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000444 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +0000445 } else {
446 Name = Mang->getMangledName(GV);
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000447 }
448 O << Name;
449}
450
Chris Lattnere3f01572006-02-23 19:31:10 +0000451/// PrintAsmOperand - Print out an operand for an inline asm expression.
452///
453bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000454 unsigned AsmVariant,
Chris Lattnere3f01572006-02-23 19:31:10 +0000455 const char *ExtraCode) {
456 // Does this asm operand have a single letter operand modifier?
457 if (ExtraCode && ExtraCode[0]) {
458 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000459
Chris Lattnere3f01572006-02-23 19:31:10 +0000460 switch (ExtraCode[0]) {
461 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000462 case 'c': // Don't print "$" before a global var name or constant.
463 // PPC never has a prefix.
464 printOperand(MI, OpNo);
465 return false;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000466 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000467 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000468 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000469 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000470 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000471 return true;
472 ++OpNo; // Return the high-part.
473 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000474 case 'I':
475 // Write 'i' if an integer constant, otherwise nothing. Used to print
476 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000477 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000478 O << "i";
479 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000480 }
481 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000482
Chris Lattnere3f01572006-02-23 19:31:10 +0000483 printOperand(MI, OpNo);
484 return false;
485}
486
Chris Lattner2c003e22006-02-24 20:27:40 +0000487bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000488 unsigned AsmVariant,
Chris Lattner2c003e22006-02-24 20:27:40 +0000489 const char *ExtraCode) {
490 if (ExtraCode && ExtraCode[0])
491 return true; // Unknown modifier.
Dan Gohmand735b802008-10-03 15:45:36 +0000492 if (MI->getOperand(OpNo).isReg())
Chris Lattner9aa28952007-02-01 00:39:08 +0000493 printMemRegReg(MI, OpNo);
494 else
495 printMemRegImm(MI, OpNo);
Chris Lattner2c003e22006-02-24 20:27:40 +0000496 return false;
497}
Chris Lattnere3f01572006-02-23 19:31:10 +0000498
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000499void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000500 const char *Modifier) {
501 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
502 unsigned Code = MI->getOperand(OpNo).getImm();
503 if (!strcmp(Modifier, "cc")) {
504 switch ((PPC::Predicate)Code) {
505 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
506 case PPC::PRED_LT: O << "lt"; return;
507 case PPC::PRED_LE: O << "le"; return;
508 case PPC::PRED_EQ: O << "eq"; return;
509 case PPC::PRED_GE: O << "ge"; return;
510 case PPC::PRED_GT: O << "gt"; return;
511 case PPC::PRED_NE: O << "ne"; return;
512 case PPC::PRED_UN: O << "un"; return;
513 case PPC::PRED_NU: O << "nu"; return;
514 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000515
Chris Lattneraf53a872006-11-04 05:27:39 +0000516 } else {
517 assert(!strcmp(Modifier, "reg") &&
518 "Need to specify 'cc' or 'reg' as predicate op modifier!");
519 // Don't print the register for 'always'.
520 if (Code == PPC::PRED_ALWAYS) return;
521 printOperand(MI, OpNo+1);
522 }
523}
524
525
Nate Begemane59bf592004-08-14 22:09:10 +0000526/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
527/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000528///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000529void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000530 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000531
Nate Begemanad5f65c2005-04-05 18:19:50 +0000532 // Check for slwi/srwi mnemonics.
533 if (MI->getOpcode() == PPC::RLWINM) {
534 bool FoundMnemonic = false;
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000535 unsigned char SH = MI->getOperand(2).getImm();
536 unsigned char MB = MI->getOperand(3).getImm();
537 unsigned char ME = MI->getOperand(4).getImm();
Nate Begemanad5f65c2005-04-05 18:19:50 +0000538 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000539 O << "\tslwi "; FoundMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000540 }
541 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000542 O << "\tsrwi "; FoundMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000543 SH = 32-SH;
544 }
545 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000546 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000547 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000548 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000549 O << ", " << (unsigned int)SH << '\n';
Nate Begemanad5f65c2005-04-05 18:19:50 +0000550 return;
551 }
Chris Lattnerb410dc92006-06-20 23:18:58 +0000552 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000553 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000554 O << "\tmr ";
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000555 printOperand(MI, 0);
556 O << ", ";
557 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000558 O << '\n';
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000559 return;
560 }
Chris Lattner566c1b12006-11-18 01:23:56 +0000561 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000562 unsigned char SH = MI->getOperand(2).getImm();
563 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner566c1b12006-11-18 01:23:56 +0000564 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
565 if (63-SH == ME) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000566 O << "\tsldi ";
Chris Lattner566c1b12006-11-18 01:23:56 +0000567 printOperand(MI, 0);
568 O << ", ";
569 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000570 O << ", " << (unsigned int)SH << '\n';
Chris Lattner566c1b12006-11-18 01:23:56 +0000571 return;
572 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000573 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000574
Nate Begemane59bf592004-08-14 22:09:10 +0000575 if (printInstruction(MI))
576 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000577
Torok Edwinc23197a2009-07-14 16:55:14 +0000578 llvm_unreachable("Unhandled instruction in asm writer!");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000579}
580
Jim Laskeybf111822006-12-21 20:26:09 +0000581/// runOnMachineFunction - This uses the printMachineInstruction()
582/// method to print assembly for each instruction.
583///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000584bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000585 this->MF = &MF;
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000586
Jim Laskeybf111822006-12-21 20:26:09 +0000587 SetupMachineFunction(MF);
588 O << "\n\n";
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000589
Jim Laskeybf111822006-12-21 20:26:09 +0000590 // Print out constants referenced by the function
591 EmitConstantPool(MF.getConstantPool());
592
593 // Print out labels for the function.
594 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000595 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000596
Jim Laskeybf111822006-12-21 20:26:09 +0000597 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000598 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000599 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000600 case Function::LinkerPrivateLinkage:
Jim Laskeybf111822006-12-21 20:26:09 +0000601 case Function::InternalLinkage: // Symbols default to internal.
602 break;
603 case Function::ExternalLinkage:
604 O << "\t.global\t" << CurrentFnName << '\n'
605 << "\t.type\t" << CurrentFnName << ", @function\n";
606 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000607 case Function::WeakAnyLinkage:
608 case Function::WeakODRLinkage:
609 case Function::LinkOnceAnyLinkage:
610 case Function::LinkOnceODRLinkage:
Jim Laskeybf111822006-12-21 20:26:09 +0000611 O << "\t.global\t" << CurrentFnName << '\n';
612 O << "\t.weak\t" << CurrentFnName << '\n';
613 break;
614 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000615
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000616 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000617
Bill Wendling20c568f2009-06-30 22:38:32 +0000618 EmitAlignment(MF.getAlignment(), F);
Jim Laskeybf111822006-12-21 20:26:09 +0000619 O << CurrentFnName << ":\n";
620
621 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000622 DW->BeginFunction(&MF);
Jim Laskeybf111822006-12-21 20:26:09 +0000623
624 // Print out code for the function.
625 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
626 I != E; ++I) {
627 // Print a label for the basic block.
628 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000629 printBasicBlockLabel(I, true, true);
Jim Laskeybf111822006-12-21 20:26:09 +0000630 O << '\n';
631 }
632 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
633 II != E; ++II) {
634 // Print the assembly for the instruction.
Jim Laskeybf111822006-12-21 20:26:09 +0000635 printMachineInstruction(II);
636 }
637 }
638
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000639 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Jim Laskeybf111822006-12-21 20:26:09 +0000640
641 // Print out jump tables referenced by the function.
642 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000643
Dale Johannesenf94a3272008-12-03 19:33:10 +0000644 SwitchToSection(TAI->SectionForGlobal(F));
645
Jim Laskeybf111822006-12-21 20:26:09 +0000646 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000647 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000648
Dan Gohmane5f4de42008-11-07 19:49:17 +0000649 O.flush();
650
Jim Laskeybf111822006-12-21 20:26:09 +0000651 // We didn't modify anything.
652 return false;
653}
654
Chris Lattnerec321b42008-02-15 19:04:54 +0000655/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000656/// Don't print things like \\n or \\0.
David Greene71847812009-07-14 20:18:05 +0000657static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Chris Lattnerec321b42008-02-15 19:04:54 +0000658 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
659 Name != E; ++Name)
660 if (isprint(*Name))
661 OS << *Name;
662}
663
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000664void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Jim Laskeybf111822006-12-21 20:26:09 +0000665 const TargetData *TD = TM.getTargetData();
666
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000667 if (!GVar->hasInitializer())
668 return; // External global require no code
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000669
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000670 // Check to see if this is a special global used by LLVM, if so, emit it.
671 if (EmitSpecialLLVMGlobal(GVar))
672 return;
Chris Lattner70d41072007-01-14 06:37:54 +0000673
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000674 std::string name = Mang->getMangledName(GVar);
Chris Lattner70d41072007-01-14 06:37:54 +0000675
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000676 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000677
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000678 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000679 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000680 return;
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000681 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000682 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000683 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000684
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000685 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000686
687 if (C->isNullValue() && /* FIXME: Verify correct */
688 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000689 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000690 GVar->isWeakForLinker())) {
Jim Laskeybf111822006-12-21 20:26:09 +0000691 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000692
693 if (GVar->hasExternalLinkage()) {
Jim Laskeybf111822006-12-21 20:26:09 +0000694 O << "\t.global " << name << '\n';
695 O << "\t.type " << name << ", @object\n";
Nick Lewyckye2b90522007-07-25 03:48:45 +0000696 O << name << ":\n";
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000697 O << "\t.zero " << Size << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000698 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000699 O << TAI->getLCOMMDirective() << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000700 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000701 O << ".comm " << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000702 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000703 if (VerboseAsm) {
704 O << "\t\t" << TAI->getCommentString() << " '";
705 PrintUnmangledNameSafely(GVar, O);
706 O << "'";
707 }
708 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000709 return;
Jim Laskeybf111822006-12-21 20:26:09 +0000710 }
711
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000712 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000713 case GlobalValue::LinkOnceAnyLinkage:
714 case GlobalValue::LinkOnceODRLinkage:
715 case GlobalValue::WeakAnyLinkage:
716 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000717 case GlobalValue::CommonLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000718 O << "\t.global " << name << '\n'
719 << "\t.type " << name << ", @object\n"
720 << "\t.weak " << name << '\n';
721 break;
722 case GlobalValue::AppendingLinkage:
723 // FIXME: appending linkage variables should go into a section of
724 // their name or something. For now, just emit them as external.
725 case GlobalValue::ExternalLinkage:
726 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000727 O << "\t.global " << name << '\n'
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000728 << "\t.type " << name << ", @object\n";
729 // FALL THROUGH
730 case GlobalValue::InternalLinkage:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000731 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000732 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000733 break;
734 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000735 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000736 }
737
738 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000739 O << name << ":";
740 if (VerboseAsm) {
741 O << "\t\t\t\t" << TAI->getCommentString() << " '";
742 PrintUnmangledNameSafely(GVar, O);
743 O << "'";
744 }
745 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000746
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000747 EmitGlobalConstant(C);
748 O << '\n';
749}
750
751bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
752 // Print out module-level global variables here.
753 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
754 I != E; ++I)
755 printModuleLevelGV(I);
756
Dan Gohmanb8275a32007-07-25 19:33:14 +0000757 return AsmPrinter::doFinalization(M);
Jim Laskeybf111822006-12-21 20:26:09 +0000758}
759
Nate Begemaned428532004-09-04 05:00:00 +0000760/// runOnMachineFunction - This uses the printMachineInstruction()
761/// method to print assembly for each instruction.
762///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000763bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000764 this->MF = &MF;
765
Chris Lattner8b8b9512005-11-21 07:51:23 +0000766 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000767 O << "\n\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000768
Nate Begemaned428532004-09-04 05:00:00 +0000769 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000770 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000771
772 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000773 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000774 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000775
Chris Lattnera637c322005-12-16 00:22:14 +0000776 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000777 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng1902a122009-01-25 06:32:01 +0000778 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000779 case Function::LinkerPrivateLinkage:
Chris Lattnera637c322005-12-16 00:22:14 +0000780 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnera637c322005-12-16 00:22:14 +0000781 break;
782 case Function::ExternalLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000783 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000784 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000785 case Function::WeakAnyLinkage:
786 case Function::WeakODRLinkage:
787 case Function::LinkOnceAnyLinkage:
788 case Function::LinkOnceODRLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000789 O << "\t.globl\t" << CurrentFnName << '\n';
790 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000791 break;
792 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000793
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000794 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000795
Bill Wendling20c568f2009-06-30 22:38:32 +0000796 EmitAlignment(MF.getAlignment(), F);
Nate Begemaned428532004-09-04 05:00:00 +0000797 O << CurrentFnName << ":\n";
798
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000799 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000800 DW->BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000801
Bill Wendling381802f2008-01-26 06:51:24 +0000802 // If the function is empty, then we need to emit *something*. Otherwise, the
803 // function's label might be associated with something that it wasn't meant to
804 // be associated with. We emit a noop in this situation.
805 MachineFunction::iterator I = MF.begin();
806
Bill Wendling824a7212008-01-26 09:03:52 +0000807 if (++I == MF.end() && MF.front().empty())
808 O << "\tnop\n";
Bill Wendling381802f2008-01-26 06:51:24 +0000809
Nate Begemaned428532004-09-04 05:00:00 +0000810 // Print out code for the function.
811 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
812 I != E; ++I) {
813 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000814 if (I != MF.begin()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000815 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000816 O << '\n';
Chris Lattner1db1adb2005-08-21 19:09:33 +0000817 }
Bill Wendling381802f2008-01-26 06:51:24 +0000818 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
819 II != IE; ++II) {
Nate Begemaned428532004-09-04 05:00:00 +0000820 // Print the assembly for the instruction.
Nate Begemaned428532004-09-04 05:00:00 +0000821 printMachineInstruction(II);
822 }
823 }
Nate Begemaned428532004-09-04 05:00:00 +0000824
Chris Lattnerfea13d32006-10-05 00:26:05 +0000825 // Print out jump tables referenced by the function.
Chris Lattner1da31ee2006-10-05 03:01:21 +0000826 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000827
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000828 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000829 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000830
Nate Begemaned428532004-09-04 05:00:00 +0000831 // We didn't modify anything.
832 return false;
833}
834
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000835
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000836bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000837 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000838 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000839 "ppc",
840 "ppc601",
841 "ppc602",
842 "ppc603",
843 "ppc7400",
844 "ppc750",
845 "ppc970",
846 "ppc64"
847 };
848
849 unsigned Directive = Subtarget.getDarwinDirective();
850 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
851 Directive = PPC::DIR_970;
852 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
853 Directive = PPC::DIR_7400;
854 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
855 Directive = PPC::DIR_64;
856 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000857 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000858
Dan Gohmanb8275a32007-07-25 19:33:14 +0000859 bool Result = AsmPrinter::doInitialization(M);
Devang Patel66b4d3b2009-06-20 01:00:07 +0000860 assert(MMI);
Dale Johannesen94618eb2008-07-09 20:43:39 +0000861
Jim Laskey2ada0852006-11-28 18:21:52 +0000862 // Prime text sections so they are adjacent. This reduces the likelihood a
863 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000864 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Jim Laskey2ada0852006-11-28 18:21:52 +0000865 "pure_instructions");
866 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000867 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Jim Laskey2ada0852006-11-28 18:21:52 +0000868 "pure_instructions,32");
869 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000870 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Jim Laskey2ada0852006-11-28 18:21:52 +0000871 "pure_instructions,16");
872 }
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000873 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000874
Dan Gohmanb8275a32007-07-25 19:33:14 +0000875 return Result;
Nate Begeman18ed0292005-07-21 01:25:49 +0000876}
877
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000878void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
879 const TargetData *TD = TM.getTargetData();
880
881 if (!GVar->hasInitializer())
882 return; // External global require no code
883
884 // Check to see if this is a special global used by LLVM, if so, emit it.
885 if (EmitSpecialLLVMGlobal(GVar)) {
886 if (TM.getRelocationModel() == Reloc::Static) {
887 if (GVar->getName() == "llvm.global_ctors")
888 O << ".reference .constructors_used\n";
889 else if (GVar->getName() == "llvm.global_dtors")
890 O << ".reference .destructors_used\n";
891 }
892 return;
893 }
894
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000895 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000896 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000897
898 Constant *C = GVar->getInitializer();
899 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000900 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000901 unsigned Align = TD->getPreferredAlignmentLog(GVar);
902
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000903 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000904
905 if (C->isNullValue() && /* FIXME: Verify correct */
906 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000907 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000908 GVar->isWeakForLinker()) &&
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000909 TAI->SectionKindForGlobal(GVar) != SectionKind::RODataMergeStr) {
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000910 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
911
912 if (GVar->hasExternalLinkage()) {
913 O << "\t.globl " << name << '\n';
914 O << "\t.zerofill __DATA, __common, " << name << ", "
915 << Size << ", " << Align;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000916 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000917 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000918 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000919 O << "\t.globl " << name << '\n'
920 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000921 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000922 O << name << ":";
923 if (VerboseAsm) {
924 O << "\t\t\t\t" << TAI->getCommentString() << " ";
925 PrintUnmangledNameSafely(GVar, O);
926 }
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000927 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000928 EmitGlobalConstant(C);
929 return;
930 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000931 O << ".comm " << name << ',' << Size;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000932 // Darwin 9 and above support aligned common data.
933 if (Subtarget.isDarwin9())
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000934 O << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000935 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000936 if (VerboseAsm) {
937 O << "\t\t" << TAI->getCommentString() << " '";
938 PrintUnmangledNameSafely(GVar, O);
939 O << "'";
940 }
941 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000942 return;
943 }
944
945 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000946 case GlobalValue::LinkOnceAnyLinkage:
947 case GlobalValue::LinkOnceODRLinkage:
948 case GlobalValue::WeakAnyLinkage:
949 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000950 case GlobalValue::CommonLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000951 O << "\t.globl " << name << '\n'
952 << "\t.weak_definition " << name << '\n';
953 break;
954 case GlobalValue::AppendingLinkage:
955 // FIXME: appending linkage variables should go into a section of
956 // their name or something. For now, just emit them as external.
957 case GlobalValue::ExternalLinkage:
958 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000959 O << "\t.globl " << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000960 // FALL THROUGH
961 case GlobalValue::InternalLinkage:
Evan Cheng1902a122009-01-25 06:32:01 +0000962 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000963 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000964 break;
965 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000966 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000967 }
968
969 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000970 O << name << ":";
971 if (VerboseAsm) {
972 O << "\t\t\t\t" << TAI->getCommentString() << " '";
973 PrintUnmangledNameSafely(GVar, O);
974 O << "'";
975 }
976 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000977
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000978 EmitGlobalConstant(C);
979 O << '\n';
980}
981
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000982bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000983 const TargetData *TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000984
985 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000986 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000987 I != E; ++I)
988 printModuleLevelGV(I);
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000989
Chris Lattner7e097e42006-06-27 01:02:25 +0000990 bool isPPC64 = TD->getPointerSizeInBits() == 64;
991
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000992 // Output stubs for dynamically-linked functions
Chris Lattnerea56f102009-07-15 00:55:58 +0000993 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000994 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000995 I != E; ++I) {
Chris Lattnerfe2fe702009-07-16 01:23:26 +0000996 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
997 "pure_instructions,32");
Chris Lattner7e097e42006-06-27 01:02:25 +0000998 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +0000999 const FnStubInfo &Info = I->second;
Chris Lattnerdefd3002009-07-15 02:33:19 +00001000 O << Info.Stub << ":\n";
1001 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +00001002 O << "\tmflr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001003 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1004 O << Info.AnonSymbol << ":\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001005 O << "\tmflr r11\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001006 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chengae94e592008-12-05 01:06:39 +00001007 O << ")\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001008 O << "\tmtlr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001009 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1010 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001011 O << "\tmtctr r12\n";
1012 O << "\tbctr\n";
Chris Lattnerfe2fe702009-07-16 01:23:26 +00001013
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001014 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdefd3002009-07-15 02:33:19 +00001015 O << Info.LazyPtr << ":\n";
1016 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001017 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001018 }
Chris Lattnerea56f102009-07-15 00:55:58 +00001019 } else if (!FnStubs.empty()) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +00001020 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +00001021 I != E; ++I) {
Chris Lattnerfe2fe702009-07-16 01:23:26 +00001022 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1023 "pure_instructions,16");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001024 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001025 const FnStubInfo &Info = I->second;
1026 O << Info.Stub << ":\n";
1027 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1028 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattner1a87c052009-07-15 02:36:21 +00001029 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001030 O << Info.LazyPtr << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001031 O << "\tmtctr r12\n";
1032 O << "\tbctr\n";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001033 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdefd3002009-07-15 02:33:19 +00001034 O << Info.LazyPtr << ":\n";
1035 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001036 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +00001037 }
Misha Brukman46fd00a2004-06-24 23:04:11 +00001038 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001039
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001040 O << '\n';
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001041
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001042 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001043 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001044 // Only referenced functions get into the Personalities list.
Chris Lattner0de1fc42009-06-24 19:09:55 +00001045 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001046 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +00001047 E = Personalities.end(); I != E; ++I) {
1048 if (*I)
1049 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner0f6c8f22009-07-15 01:16:38 +00001050 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +00001051 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001052 }
1053
Chris Lattnera637c322005-12-16 00:22:14 +00001054 // Output stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001055 if (!GVStubs.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001056 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner8f831cb2009-07-15 01:14:44 +00001057 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1058 E = GVStubs.end(); I != E; ++I) {
1059 O << I->second << ":\n";
1060 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1061 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001062 }
Nate Begemane59bf592004-08-14 22:09:10 +00001063 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001064
Evan Chengae94e592008-12-05 01:06:39 +00001065 if (!HiddenGVStubs.empty()) {
1066 SwitchToSection(TAI->getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +00001067 EmitAlignment(isPPC64 ? 3 : 2);
1068 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1069 E = HiddenGVStubs.end(); I != E; ++I) {
1070 O << I->second << ":\n";
1071 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chengae94e592008-12-05 01:06:39 +00001072 }
1073 }
1074
Chris Lattner5b0ac992005-11-01 00:12:36 +00001075 // Funny Darwin hack: This flag tells the linker that no global symbols
1076 // contain code that falls through to other global symbols (e.g. the obvious
1077 // implementation of multiple entry points). If this doesn't occur, the
1078 // linker can safely perform dead code stripping. Since LLVM never generates
1079 // code that does this, it is always safe to set.
Chris Lattner4da1c822006-09-20 17:12:19 +00001080 O << "\t.subsections_via_symbols\n";
Chris Lattner5b0ac992005-11-01 00:12:36 +00001081
Dan Gohmanb8275a32007-07-25 19:33:14 +00001082 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001083}
Nate Begemaned428532004-09-04 05:00:00 +00001084
Chris Lattner4da1c822006-09-20 17:12:19 +00001085
1086
Jim Laskeyb608a4d2006-12-20 20:56:46 +00001087/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1088/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +00001089/// Darwin assembler can deal with.
1090///
David Greene71847812009-07-14 20:18:05 +00001091FunctionPass *llvm::createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001092 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +00001093 bool verbose) {
Jim Laskeybf111822006-12-21 20:26:09 +00001094 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1095
1096 if (Subtarget->isDarwin()) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +00001097 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Jim Laskeybf111822006-12-21 20:26:09 +00001098 } else {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +00001099 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Jim Laskeybf111822006-12-21 20:26:09 +00001100 }
Chris Lattner4da1c822006-09-20 17:12:19 +00001101}
Anton Korobeynikov06be9972008-08-17 13:54:28 +00001102
Bob Wilsona96751f2009-06-23 23:59:40 +00001103// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001104extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001105 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1106
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001107 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1108}