blob: b74321c6a9fd27623aed0c33d89e135efe9c74e4 [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;
73 Stub = Mang->makeNameProper(GV+"$stub", true);
74 LazyPtr = Mang->makeNameProper(GV+"$lazy_ptr", true);
75 AnonSymbol = Mang->makeNameProper(GV+"$stub$tmp", true);
76 }
77 };
78
79 StringMap<FnStubInfo> FnStubs;
Chris Lattner8f831cb2009-07-15 01:14:44 +000080 StringMap<std::string> GVStubs, HiddenGVStubs;
Chris Lattnerdadceed2006-09-20 17:07:15 +000081 const PPCSubtarget &Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000082 public:
David Greene71847812009-07-14 20:18:05 +000083 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000084 const TargetAsmInfo *T, bool V)
85 : AsmPrinter(O, TM, T, V),
Bill Wendling57f0db82009-02-24 08:30:20 +000086 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000087
Misha Brukman5dfe3a92004-06-21 16:55:25 +000088 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000089 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000090 }
91
Nate Begeman21e463b2005-10-16 05:39:50 +000092 PPCTargetMachine &getTM() {
93 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000094 }
95
Nate Begemanadeb43d2005-07-20 22:42:00 +000096 unsigned enumRegToMachineReg(unsigned enumReg) {
97 switch (enumReg) {
Torok Edwinc23197a2009-07-14 16:55:14 +000098 default: llvm_unreachable("Unhandled register!");
Nate Begemanadeb43d2005-07-20 22:42:00 +000099 case PPC::CR0: return 0;
100 case PPC::CR1: return 1;
101 case PPC::CR2: return 2;
102 case PPC::CR3: return 3;
103 case PPC::CR4: return 4;
104 case PPC::CR5: return 5;
105 case PPC::CR6: return 6;
106 case PPC::CR7: return 7;
107 }
Torok Edwinc23197a2009-07-14 16:55:14 +0000108 llvm_unreachable(0);
Nate Begemanadeb43d2005-07-20 22:42:00 +0000109 }
110
Nate Begemane59bf592004-08-14 22:09:10 +0000111 /// printInstruction - This method is automatically generated by tablegen
112 /// from the instruction set description. This method returns true if the
113 /// machine instruction was sufficiently described to print it, otherwise it
114 /// returns false.
115 bool printInstruction(const MachineInstr *MI);
116
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000117 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +0000118 void printOp(const MachineOperand &MO);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000119
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000120 /// stripRegisterPrefix - This method strips the character prefix from a
121 /// register name so that only the number is left. Used by for linux asm.
Jim Laskey40ee69f2006-12-20 21:33:34 +0000122 const char *stripRegisterPrefix(const char *RegName) {
123 switch (RegName[0]) {
124 case 'r':
125 case 'f':
126 case 'v': return RegName + 1;
Jim Laskey2aa14aa2006-12-20 21:35:00 +0000127 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000128 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000129
Jim Laskey40ee69f2006-12-20 21:33:34 +0000130 return RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000131 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000132
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000133 /// printRegister - Print register according to target requirements.
134 ///
135 void printRegister(const MachineOperand &MO, bool R0AsZero) {
136 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000137 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000138
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000139 // If we should use 0 for R0.
140 if (R0AsZero && RegNo == PPC::R0) {
141 O << "0";
142 return;
143 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000144
Bill Wendling74ab84c2008-02-26 21:11:01 +0000145 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000146 // Linux assembler (Others?) does not take register mnemonics.
147 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskey40ee69f2006-12-20 21:33:34 +0000148 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
149 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000150 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000151
Chris Lattner58873272006-02-01 22:38:46 +0000152 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000153 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmand735b802008-10-03 15:45:36 +0000154 if (MO.isReg()) {
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000155 printRegister(MO, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000156 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000157 O << MO.getImm();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000158 } else {
159 printOp(MO);
160 }
161 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000162
Chris Lattner58873272006-02-01 22:38:46 +0000163 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000164 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000165 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
166 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000167
168
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000169 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000170 char value = MI->getOperand(OpNo).getImm();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000171 value = (value << (32-5)) >> (32-5);
172 O << (int)value;
173 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000174 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000175 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattner12585ba2004-08-21 19:11:03 +0000176 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000177 O << (unsigned int)value;
178 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000179 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000180 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman07aada82004-08-30 02:28:06 +0000181 assert(value <= 63 && "Invalid u6imm argument!");
182 O << (unsigned int)value;
183 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000184 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000185 O << (short)MI->getOperand(OpNo).getImm();
Nate Begemaned428532004-09-04 05:00:00 +0000186 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000187 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000188 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000189 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000190 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000191 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000192 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000193 } else {
194 O << "lo16(";
195 printOp(MI->getOperand(OpNo));
196 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000197 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000198 else
199 O << ')';
200 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000201 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000202 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000203 // Branches can take an immediate operand. This is used by the branch
204 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmand735b802008-10-03 15:45:36 +0000205 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000206 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000207 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000208 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000209 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000210 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000211 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000212 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000213 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000214 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
215 GlobalValue *GV = MO.getGlobal();
Chris Lattner20674332009-07-02 16:08:53 +0000216 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattnera637c322005-12-16 00:22:14 +0000217 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000218 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
219 FnInfo.Init(GV, Mang);
220 O << FnInfo.Stub;
Chris Lattnera637c322005-12-16 00:22:14 +0000221 return;
222 }
223 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000224 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000225 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
226 FnInfo.Init(MO.getSymbolName(), Mang);
227 O << FnInfo.Stub;
Chris Lattner9542f9712005-11-17 19:40:30 +0000228 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000229 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000230 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000231
Chris Lattner9542f9712005-11-17 19:40:30 +0000232 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000233 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000234 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000235 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begeman422b0ce2005-11-16 00:48:01 +0000236 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000237 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000238 O << "\"L" << getFunctionNumber() << "$pb\"\n";
239 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000240 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000241 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000242 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000243 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000244 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000245 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begeman2497e632005-07-21 20:44:43 +0000246 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000247 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000248 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000249 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000250 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000251 else
252 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000253 }
Nate Begemaned428532004-09-04 05:00:00 +0000254 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000255 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000256 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000257 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000258 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000259 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000260 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000261 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000262 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000263 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000264 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000265 else
266 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000267 }
268 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000269 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000270 unsigned CCReg = MI->getOperand(OpNo).getReg();
271 unsigned RegNo = enumRegToMachineReg(CCReg);
272 O << (0x80 >> RegNo);
273 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000274 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000275 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
276 printSymbolLo(MI, OpNo);
277 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000278 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner13feb582006-03-21 17:21:13 +0000279 MI->getOperand(OpNo+1).getReg() == PPC::R0)
280 O << "0";
281 else
282 printOperand(MI, OpNo+1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000283 O << ')';
284 }
Chris Lattnere5ba5802006-03-22 05:26:03 +0000285 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000286 if (MI->getOperand(OpNo).isImm())
Chris Lattnere5ba5802006-03-22 05:26:03 +0000287 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000288 else
Chris Lattnere5ba5802006-03-22 05:26:03 +0000289 printSymbolLo(MI, OpNo);
290 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000291 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattnere5ba5802006-03-22 05:26:03 +0000292 MI->getOperand(OpNo+1).getReg() == PPC::R0)
293 O << "0";
294 else
295 printOperand(MI, OpNo+1);
296 O << ')';
297 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000298
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000299 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000300 // When used as the base register, r0 reads constant zero rather than
301 // the value contained in the register. For this reason, the darwin
302 // assembler requires that we print r0 as 0 (no r) when used as the base.
303 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000304 printRegister(MO, true);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000305 O << ", ";
306 printOperand(MI, OpNo+1);
307 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000308
309 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000310 const char *Modifier);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000311
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000312 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000313 virtual bool doFinalization(Module &M) = 0;
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000314
315 virtual void EmitExternalGlobal(const GlobalVariable *GV);
Nate Begemaned428532004-09-04 05:00:00 +0000316 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000317
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000318 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling57f0db82009-02-24 08:30:20 +0000319 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000320 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000321 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000322 const TargetAsmInfo *T, bool V)
323 : PPCAsmPrinter(O, TM, T, V){}
Jim Laskeybf111822006-12-21 20:26:09 +0000324
325 virtual const char *getPassName() const {
326 return "Linux PPC Assembly Printer";
327 }
328
329 bool runOnMachineFunction(MachineFunction &F);
Jim Laskeybf111822006-12-21 20:26:09 +0000330 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000331
Jim Laskeybf111822006-12-21 20:26:09 +0000332 void getAnalysisUsage(AnalysisUsage &AU) const {
333 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000334 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000335 AU.addRequired<DwarfWriter>();
Jim Laskeybf111822006-12-21 20:26:09 +0000336 PPCAsmPrinter::getAnalysisUsage(AU);
337 }
338
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000339 void printModuleLevelGV(const GlobalVariable* GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000340 };
341
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000342 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
343 /// OS X
Bill Wendling57f0db82009-02-24 08:30:20 +0000344 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene71847812009-07-14 20:18:05 +0000345 formatted_raw_ostream &OS;
Bill Wendling57f0db82009-02-24 08:30:20 +0000346 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000347 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000348 const TargetAsmInfo *T, bool V)
349 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Nate Begemaned428532004-09-04 05:00:00 +0000350
351 virtual const char *getPassName() const {
352 return "Darwin PPC Assembly Printer";
353 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000354
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000355 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000356 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000357 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000358
Jim Laskeyb2efb852006-01-04 22:28:25 +0000359 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000360 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000361 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000362 AU.addRequired<DwarfWriter>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000363 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000364 }
365
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000366 void printModuleLevelGV(const GlobalVariable* GVar);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000367 };
368} // end of anonymous namespace
369
Nate Begemane59bf592004-08-14 22:09:10 +0000370// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000371#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000372
Chris Lattner9ba13e42005-11-17 19:25:59 +0000373void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000374 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000375 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000376 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman97a296f2004-07-21 20:11:11 +0000377
Nate Begeman37efe672006-04-22 18:53:45 +0000378 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000379 printBasicBlockLabel(MO.getMBB());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000380 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000381 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000382 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000383 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000384 // FIXME: PIC relocation model
385 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000386 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000387 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000388 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000390 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000391 // Computing the address of an external symbol, not calling it.
Chris Lattner8f831cb2009-07-15 01:14:44 +0000392 std::string Name(TAI->getGlobalPrefix());
393 Name += MO.getSymbolName();
394
Evan Cheng4c1aa862006-02-22 20:19:42 +0000395 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000396 GVStubs[Name] = Name+"$non_lazy_ptr";
397 Name += "$non_lazy_ptr";
Chris Lattnera637c322005-12-16 00:22:14 +0000398 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000399 O << Name;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000400 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000401 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000402 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000403 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000404 GlobalValue *GV = MO.getGlobal();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000405 std::string Name;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000406
Nate Begemanfcf4a422004-10-17 23:01:34 +0000407 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000408 if (TM.getRelocationModel() != Reloc::Static &&
409 (GV->isDeclaration() || GV->isWeakForLinker())) {
410 if (!GV->hasHiddenVisibility()) {
411 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
412 GVStubs[Mang->getMangledName(GV)] = Name;
413 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
414 GV->hasAvailableExternallyLinkage()) {
415 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
416 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
417 } else {
418 Name = Mang->getMangledName(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000419 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000420 } else {
421 Name = Mang->getMangledName(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000422 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000423 O << Name;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000424
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000425 printOffset(MO.getOffset());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000426 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000427 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000428
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000429 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000430 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000431 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000432 }
433}
434
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000435/// EmitExternalGlobal - In this case we need to use the indirect symbol.
436///
437void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling7d16e852009-04-10 00:12:49 +0000438 std::string Name;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000439
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000440 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner8f831cb2009-07-15 01:14:44 +0000441 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +0000442 } else {
443 Name = Mang->getMangledName(GV);
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000444 }
445 O << Name;
446}
447
Chris Lattnere3f01572006-02-23 19:31:10 +0000448/// PrintAsmOperand - Print out an operand for an inline asm expression.
449///
450bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000451 unsigned AsmVariant,
Chris Lattnere3f01572006-02-23 19:31:10 +0000452 const char *ExtraCode) {
453 // Does this asm operand have a single letter operand modifier?
454 if (ExtraCode && ExtraCode[0]) {
455 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000456
Chris Lattnere3f01572006-02-23 19:31:10 +0000457 switch (ExtraCode[0]) {
458 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000459 case 'c': // Don't print "$" before a global var name or constant.
460 // PPC never has a prefix.
461 printOperand(MI, OpNo);
462 return false;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000463 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000464 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000465 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000466 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000467 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000468 return true;
469 ++OpNo; // Return the high-part.
470 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000471 case 'I':
472 // Write 'i' if an integer constant, otherwise nothing. Used to print
473 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000474 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000475 O << "i";
476 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000477 }
478 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000479
Chris Lattnere3f01572006-02-23 19:31:10 +0000480 printOperand(MI, OpNo);
481 return false;
482}
483
Chris Lattner2c003e22006-02-24 20:27:40 +0000484bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000485 unsigned AsmVariant,
Chris Lattner2c003e22006-02-24 20:27:40 +0000486 const char *ExtraCode) {
487 if (ExtraCode && ExtraCode[0])
488 return true; // Unknown modifier.
Dan Gohmand735b802008-10-03 15:45:36 +0000489 if (MI->getOperand(OpNo).isReg())
Chris Lattner9aa28952007-02-01 00:39:08 +0000490 printMemRegReg(MI, OpNo);
491 else
492 printMemRegImm(MI, OpNo);
Chris Lattner2c003e22006-02-24 20:27:40 +0000493 return false;
494}
Chris Lattnere3f01572006-02-23 19:31:10 +0000495
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000496void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000497 const char *Modifier) {
498 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
499 unsigned Code = MI->getOperand(OpNo).getImm();
500 if (!strcmp(Modifier, "cc")) {
501 switch ((PPC::Predicate)Code) {
502 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
503 case PPC::PRED_LT: O << "lt"; return;
504 case PPC::PRED_LE: O << "le"; return;
505 case PPC::PRED_EQ: O << "eq"; return;
506 case PPC::PRED_GE: O << "ge"; return;
507 case PPC::PRED_GT: O << "gt"; return;
508 case PPC::PRED_NE: O << "ne"; return;
509 case PPC::PRED_UN: O << "un"; return;
510 case PPC::PRED_NU: O << "nu"; return;
511 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000512
Chris Lattneraf53a872006-11-04 05:27:39 +0000513 } else {
514 assert(!strcmp(Modifier, "reg") &&
515 "Need to specify 'cc' or 'reg' as predicate op modifier!");
516 // Don't print the register for 'always'.
517 if (Code == PPC::PRED_ALWAYS) return;
518 printOperand(MI, OpNo+1);
519 }
520}
521
522
Nate Begemane59bf592004-08-14 22:09:10 +0000523/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
524/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000525///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000526void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000527 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000528
Nate Begemanad5f65c2005-04-05 18:19:50 +0000529 // Check for slwi/srwi mnemonics.
530 if (MI->getOpcode() == PPC::RLWINM) {
531 bool FoundMnemonic = false;
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000532 unsigned char SH = MI->getOperand(2).getImm();
533 unsigned char MB = MI->getOperand(3).getImm();
534 unsigned char ME = MI->getOperand(4).getImm();
Nate Begemanad5f65c2005-04-05 18:19:50 +0000535 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000536 O << "\tslwi "; FoundMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000537 }
538 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000539 O << "\tsrwi "; FoundMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000540 SH = 32-SH;
541 }
542 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000543 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000544 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000545 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000546 O << ", " << (unsigned int)SH << '\n';
Nate Begemanad5f65c2005-04-05 18:19:50 +0000547 return;
548 }
Chris Lattnerb410dc92006-06-20 23:18:58 +0000549 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000550 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000551 O << "\tmr ";
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000552 printOperand(MI, 0);
553 O << ", ";
554 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000555 O << '\n';
Chris Lattner4d73a5a2006-02-08 06:56:40 +0000556 return;
557 }
Chris Lattner566c1b12006-11-18 01:23:56 +0000558 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000559 unsigned char SH = MI->getOperand(2).getImm();
560 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner566c1b12006-11-18 01:23:56 +0000561 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
562 if (63-SH == ME) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000563 O << "\tsldi ";
Chris Lattner566c1b12006-11-18 01:23:56 +0000564 printOperand(MI, 0);
565 O << ", ";
566 printOperand(MI, 1);
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000567 O << ", " << (unsigned int)SH << '\n';
Chris Lattner566c1b12006-11-18 01:23:56 +0000568 return;
569 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000570 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000571
Nate Begemane59bf592004-08-14 22:09:10 +0000572 if (printInstruction(MI))
573 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000574
Torok Edwinc23197a2009-07-14 16:55:14 +0000575 llvm_unreachable("Unhandled instruction in asm writer!");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000576}
577
Jim Laskeybf111822006-12-21 20:26:09 +0000578/// runOnMachineFunction - This uses the printMachineInstruction()
579/// method to print assembly for each instruction.
580///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000581bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000582 this->MF = &MF;
Chris Lattner9b7ce7d2006-10-05 02:42:20 +0000583
Jim Laskeybf111822006-12-21 20:26:09 +0000584 SetupMachineFunction(MF);
585 O << "\n\n";
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000586
Jim Laskeybf111822006-12-21 20:26:09 +0000587 // Print out constants referenced by the function
588 EmitConstantPool(MF.getConstantPool());
589
590 // Print out labels for the function.
591 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000592 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000593
Jim Laskeybf111822006-12-21 20:26:09 +0000594 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000595 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000596 case Function::PrivateLinkage:
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
Dale Johannesenf94a3272008-12-03 19:33:10 +0000640 SwitchToSection(TAI->SectionForGlobal(F));
641
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
Dan Gohmane5f4de42008-11-07 19:49:17 +0000645 O.flush();
646
Jim Laskeybf111822006-12-21 20:26:09 +0000647 // We didn't modify anything.
648 return false;
649}
650
Chris Lattnerec321b42008-02-15 19:04:54 +0000651/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000652/// Don't print things like \\n or \\0.
David Greene71847812009-07-14 20:18:05 +0000653static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Chris Lattnerec321b42008-02-15 19:04:54 +0000654 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
655 Name != E; ++Name)
656 if (isprint(*Name))
657 OS << *Name;
658}
659
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000660void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Jim Laskeybf111822006-12-21 20:26:09 +0000661 const TargetData *TD = TM.getTargetData();
662
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000663 if (!GVar->hasInitializer())
664 return; // External global require no code
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000665
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000666 // Check to see if this is a special global used by LLVM, if so, emit it.
667 if (EmitSpecialLLVMGlobal(GVar))
668 return;
Chris Lattner70d41072007-01-14 06:37:54 +0000669
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000670 std::string name = Mang->getMangledName(GVar);
Chris Lattner70d41072007-01-14 06:37:54 +0000671
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000672 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000673
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000674 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000675 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000676 return;
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000677 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000678 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000679 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Jim Laskeybf111822006-12-21 20:26:09 +0000680
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000681 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000682
683 if (C->isNullValue() && /* FIXME: Verify correct */
684 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000685 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000686 GVar->isWeakForLinker())) {
Jim Laskeybf111822006-12-21 20:26:09 +0000687 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000688
689 if (GVar->hasExternalLinkage()) {
Jim Laskeybf111822006-12-21 20:26:09 +0000690 O << "\t.global " << name << '\n';
691 O << "\t.type " << name << ", @object\n";
Nick Lewyckye2b90522007-07-25 03:48:45 +0000692 O << name << ":\n";
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000693 O << "\t.zero " << Size << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000694 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000695 O << TAI->getLCOMMDirective() << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000696 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000697 O << ".comm " << name << ',' << Size;
Jim Laskeybf111822006-12-21 20:26:09 +0000698 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000699 if (VerboseAsm) {
700 O << "\t\t" << TAI->getCommentString() << " '";
701 PrintUnmangledNameSafely(GVar, O);
702 O << "'";
703 }
704 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000705 return;
Jim Laskeybf111822006-12-21 20:26:09 +0000706 }
707
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000708 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000709 case GlobalValue::LinkOnceAnyLinkage:
710 case GlobalValue::LinkOnceODRLinkage:
711 case GlobalValue::WeakAnyLinkage:
712 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000713 case GlobalValue::CommonLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000714 O << "\t.global " << name << '\n'
715 << "\t.type " << name << ", @object\n"
716 << "\t.weak " << name << '\n';
717 break;
718 case GlobalValue::AppendingLinkage:
719 // FIXME: appending linkage variables should go into a section of
720 // their name or something. For now, just emit them as external.
721 case GlobalValue::ExternalLinkage:
722 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000723 O << "\t.global " << name << '\n'
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000724 << "\t.type " << name << ", @object\n";
725 // FALL THROUGH
726 case GlobalValue::InternalLinkage:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000727 case GlobalValue::PrivateLinkage:
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000728 break;
729 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000730 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000731 }
732
733 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000734 O << name << ":";
735 if (VerboseAsm) {
736 O << "\t\t\t\t" << TAI->getCommentString() << " '";
737 PrintUnmangledNameSafely(GVar, O);
738 O << "'";
739 }
740 O << '\n';
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000741
Anton Korobeynikov7396e592008-08-08 18:23:49 +0000742 EmitGlobalConstant(C);
743 O << '\n';
744}
745
746bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
747 // Print out module-level global variables here.
748 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
749 I != E; ++I)
750 printModuleLevelGV(I);
751
Dan Gohmanb8275a32007-07-25 19:33:14 +0000752 return AsmPrinter::doFinalization(M);
Jim Laskeybf111822006-12-21 20:26:09 +0000753}
754
Nate Begemaned428532004-09-04 05:00:00 +0000755/// runOnMachineFunction - This uses the printMachineInstruction()
756/// method to print assembly for each instruction.
757///
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000758bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000759 this->MF = &MF;
760
Chris Lattner8b8b9512005-11-21 07:51:23 +0000761 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000762 O << "\n\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000763
Nate Begemaned428532004-09-04 05:00:00 +0000764 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000765 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000766
767 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000768 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000769 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000770
Chris Lattnera637c322005-12-16 00:22:14 +0000771 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000772 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng1902a122009-01-25 06:32:01 +0000773 case Function::PrivateLinkage:
Chris Lattnera637c322005-12-16 00:22:14 +0000774 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnera637c322005-12-16 00:22:14 +0000775 break;
776 case Function::ExternalLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000777 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000778 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000779 case Function::WeakAnyLinkage:
780 case Function::WeakODRLinkage:
781 case Function::LinkOnceAnyLinkage:
782 case Function::LinkOnceODRLinkage:
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000783 O << "\t.globl\t" << CurrentFnName << '\n';
784 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000785 break;
786 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000787
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000788 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000789
Bill Wendling20c568f2009-06-30 22:38:32 +0000790 EmitAlignment(MF.getAlignment(), F);
Nate Begemaned428532004-09-04 05:00:00 +0000791 O << CurrentFnName << ":\n";
792
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000793 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000794 DW->BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000795
Bill Wendling381802f2008-01-26 06:51:24 +0000796 // If the function is empty, then we need to emit *something*. Otherwise, the
797 // function's label might be associated with something that it wasn't meant to
798 // be associated with. We emit a noop in this situation.
799 MachineFunction::iterator I = MF.begin();
800
Bill Wendling824a7212008-01-26 09:03:52 +0000801 if (++I == MF.end() && MF.front().empty())
802 O << "\tnop\n";
Bill Wendling381802f2008-01-26 06:51:24 +0000803
Nate Begemaned428532004-09-04 05:00:00 +0000804 // Print out code for the function.
805 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
806 I != E; ++I) {
807 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000808 if (I != MF.begin()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000809 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000810 O << '\n';
Chris Lattner1db1adb2005-08-21 19:09:33 +0000811 }
Bill Wendling381802f2008-01-26 06:51:24 +0000812 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
813 II != IE; ++II) {
Nate Begemaned428532004-09-04 05:00:00 +0000814 // Print the assembly for the instruction.
Nate Begemaned428532004-09-04 05:00:00 +0000815 printMachineInstruction(II);
816 }
817 }
Nate Begemaned428532004-09-04 05:00:00 +0000818
Chris Lattnerfea13d32006-10-05 00:26:05 +0000819 // Print out jump tables referenced by the function.
Chris Lattner1da31ee2006-10-05 03:01:21 +0000820 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000821
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000822 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000823 DW->EndFunction(&MF);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000824
Nate Begemaned428532004-09-04 05:00:00 +0000825 // We didn't modify anything.
826 return false;
827}
828
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000829
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000830bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000831 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000832 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000833 "ppc",
834 "ppc601",
835 "ppc602",
836 "ppc603",
837 "ppc7400",
838 "ppc750",
839 "ppc970",
840 "ppc64"
841 };
842
843 unsigned Directive = Subtarget.getDarwinDirective();
844 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
845 Directive = PPC::DIR_970;
846 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
847 Directive = PPC::DIR_7400;
848 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
849 Directive = PPC::DIR_64;
850 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000851 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000852
Dan Gohmanb8275a32007-07-25 19:33:14 +0000853 bool Result = AsmPrinter::doInitialization(M);
Devang Patel66b4d3b2009-06-20 01:00:07 +0000854 assert(MMI);
Dale Johannesen94618eb2008-07-09 20:43:39 +0000855
Jim Laskey2ada0852006-11-28 18:21:52 +0000856 // Prime text sections so they are adjacent. This reduces the likelihood a
857 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000858 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Jim Laskey2ada0852006-11-28 18:21:52 +0000859 "pure_instructions");
860 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000861 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Jim Laskey2ada0852006-11-28 18:21:52 +0000862 "pure_instructions,32");
863 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000864 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Jim Laskey2ada0852006-11-28 18:21:52 +0000865 "pure_instructions,16");
866 }
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000867 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000868
Dan Gohmanb8275a32007-07-25 19:33:14 +0000869 return Result;
Nate Begeman18ed0292005-07-21 01:25:49 +0000870}
871
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000872void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
873 const TargetData *TD = TM.getTargetData();
874
875 if (!GVar->hasInitializer())
876 return; // External global require no code
877
878 // Check to see if this is a special global used by LLVM, if so, emit it.
879 if (EmitSpecialLLVMGlobal(GVar)) {
880 if (TM.getRelocationModel() == Reloc::Static) {
881 if (GVar->getName() == "llvm.global_ctors")
882 O << ".reference .constructors_used\n";
883 else if (GVar->getName() == "llvm.global_dtors")
884 O << ".reference .destructors_used\n";
885 }
886 return;
887 }
888
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000889 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000890 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000891
892 Constant *C = GVar->getInitializer();
893 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000894 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000895 unsigned Align = TD->getPreferredAlignmentLog(GVar);
896
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000897 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000898
899 if (C->isNullValue() && /* FIXME: Verify correct */
900 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000901 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000902 GVar->isWeakForLinker()) &&
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000903 TAI->SectionKindForGlobal(GVar) != SectionKind::RODataMergeStr) {
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000904 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
905
906 if (GVar->hasExternalLinkage()) {
907 O << "\t.globl " << name << '\n';
908 O << "\t.zerofill __DATA, __common, " << name << ", "
909 << Size << ", " << Align;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000910 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000911 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000912 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000913 O << "\t.globl " << name << '\n'
914 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000915 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000916 O << name << ":";
917 if (VerboseAsm) {
918 O << "\t\t\t\t" << TAI->getCommentString() << " ";
919 PrintUnmangledNameSafely(GVar, O);
920 }
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000921 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000922 EmitGlobalConstant(C);
923 return;
924 } else {
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000925 O << ".comm " << name << ',' << Size;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000926 // Darwin 9 and above support aligned common data.
927 if (Subtarget.isDarwin9())
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000928 O << ',' << Align;
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000929 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000930 if (VerboseAsm) {
931 O << "\t\t" << TAI->getCommentString() << " '";
932 PrintUnmangledNameSafely(GVar, O);
933 O << "'";
934 }
935 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000936 return;
937 }
938
939 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000940 case GlobalValue::LinkOnceAnyLinkage:
941 case GlobalValue::LinkOnceODRLinkage:
942 case GlobalValue::WeakAnyLinkage:
943 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000944 case GlobalValue::CommonLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000945 O << "\t.globl " << name << '\n'
946 << "\t.weak_definition " << name << '\n';
947 break;
948 case GlobalValue::AppendingLinkage:
949 // FIXME: appending linkage variables should go into a section of
950 // their name or something. For now, just emit them as external.
951 case GlobalValue::ExternalLinkage:
952 // If external or appending, declare as a global symbol
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000953 O << "\t.globl " << name << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000954 // FALL THROUGH
955 case GlobalValue::InternalLinkage:
Evan Cheng1902a122009-01-25 06:32:01 +0000956 case GlobalValue::PrivateLinkage:
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000957 break;
958 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000959 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000960 }
961
962 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000963 O << name << ":";
964 if (VerboseAsm) {
965 O << "\t\t\t\t" << TAI->getCommentString() << " '";
966 PrintUnmangledNameSafely(GVar, O);
967 O << "'";
968 }
969 O << '\n';
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000970
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000971 EmitGlobalConstant(C);
972 O << '\n';
973}
974
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000975bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000976 const TargetData *TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000977
978 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000979 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovbc331a82008-08-08 18:23:25 +0000980 I != E; ++I)
981 printModuleLevelGV(I);
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000982
Chris Lattner7e097e42006-06-27 01:02:25 +0000983 bool isPPC64 = TD->getPointerSizeInBits() == 64;
984
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000985 // Output stubs for dynamically-linked functions
Chris Lattnerea56f102009-07-15 00:55:58 +0000986 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +0000987 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +0000988 I != E; ++I) {
Chris Lattnerfe2fe702009-07-16 01:23:26 +0000989 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
990 "pure_instructions,32");
Chris Lattner7e097e42006-06-27 01:02:25 +0000991 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +0000992 const FnStubInfo &Info = I->second;
Chris Lattnerdefd3002009-07-15 02:33:19 +0000993 O << Info.Stub << ":\n";
994 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +0000995 O << "\tmflr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +0000996 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
997 O << Info.AnonSymbol << ":\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000998 O << "\tmflr r11\n";
Chris Lattner392db3e2009-07-15 02:56:53 +0000999 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chengae94e592008-12-05 01:06:39 +00001000 O << ")\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001001 O << "\tmtlr r0\n";
Chris Lattner392db3e2009-07-15 02:56:53 +00001002 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1003 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001004 O << "\tmtctr r12\n";
1005 O << "\tbctr\n";
Chris Lattnerfe2fe702009-07-16 01:23:26 +00001006
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001007 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdefd3002009-07-15 02:33:19 +00001008 O << Info.LazyPtr << ":\n";
1009 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001010 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001011 }
Chris Lattnerea56f102009-07-15 00:55:58 +00001012 } else if (!FnStubs.empty()) {
Chris Lattner9ec8fac2009-07-15 02:28:57 +00001013 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner8f831cb2009-07-15 01:14:44 +00001014 I != E; ++I) {
Chris Lattnerfe2fe702009-07-16 01:23:26 +00001015 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1016 "pure_instructions,16");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001017 EmitAlignment(4);
Chris Lattnerdefd3002009-07-15 02:33:19 +00001018 const FnStubInfo &Info = I->second;
1019 O << Info.Stub << ":\n";
1020 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1021 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattner1a87c052009-07-15 02:36:21 +00001022 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdefd3002009-07-15 02:33:19 +00001023 O << Info.LazyPtr << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +00001024 O << "\tmtctr r12\n";
1025 O << "\tbctr\n";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001026 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdefd3002009-07-15 02:33:19 +00001027 O << Info.LazyPtr << ":\n";
1028 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +00001029 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +00001030 }
Misha Brukman46fd00a2004-06-24 23:04:11 +00001031 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001032
Anton Korobeynikov382f0022008-08-08 18:24:10 +00001033 O << '\n';
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001034
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001035 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001036 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001037 // Only referenced functions get into the Personalities list.
Chris Lattner0de1fc42009-06-24 19:09:55 +00001038 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001039 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +00001040 E = Personalities.end(); I != E; ++I) {
1041 if (*I)
1042 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner0f6c8f22009-07-15 01:16:38 +00001043 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner8f831cb2009-07-15 01:14:44 +00001044 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001045 }
1046
Chris Lattnera637c322005-12-16 00:22:14 +00001047 // Output stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001048 if (!GVStubs.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +00001049 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner8f831cb2009-07-15 01:14:44 +00001050 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1051 E = GVStubs.end(); I != E; ++I) {
1052 O << I->second << ":\n";
1053 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1054 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Chris Lattnerdeea4162005-12-13 04:33:58 +00001055 }
Nate Begemane59bf592004-08-14 22:09:10 +00001056 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001057
Evan Chengae94e592008-12-05 01:06:39 +00001058 if (!HiddenGVStubs.empty()) {
1059 SwitchToSection(TAI->getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +00001060 EmitAlignment(isPPC64 ? 3 : 2);
1061 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1062 E = HiddenGVStubs.end(); I != E; ++I) {
1063 O << I->second << ":\n";
1064 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chengae94e592008-12-05 01:06:39 +00001065 }
1066 }
1067
Chris Lattner5b0ac992005-11-01 00:12:36 +00001068 // Funny Darwin hack: This flag tells the linker that no global symbols
1069 // contain code that falls through to other global symbols (e.g. the obvious
1070 // implementation of multiple entry points). If this doesn't occur, the
1071 // linker can safely perform dead code stripping. Since LLVM never generates
1072 // code that does this, it is always safe to set.
Chris Lattner4da1c822006-09-20 17:12:19 +00001073 O << "\t.subsections_via_symbols\n";
Chris Lattner5b0ac992005-11-01 00:12:36 +00001074
Dan Gohmanb8275a32007-07-25 19:33:14 +00001075 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001076}
Nate Begemaned428532004-09-04 05:00:00 +00001077
Chris Lattner4da1c822006-09-20 17:12:19 +00001078
1079
Jim Laskeyb608a4d2006-12-20 20:56:46 +00001080/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1081/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +00001082/// Darwin assembler can deal with.
1083///
David Greene71847812009-07-14 20:18:05 +00001084FunctionPass *llvm::createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001085 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +00001086 bool verbose) {
Jim Laskeybf111822006-12-21 20:26:09 +00001087 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1088
1089 if (Subtarget->isDarwin()) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +00001090 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Jim Laskeybf111822006-12-21 20:26:09 +00001091 } else {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +00001092 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Jim Laskeybf111822006-12-21 20:26:09 +00001093 }
Chris Lattner4da1c822006-09-20 17:12:19 +00001094}
Anton Korobeynikov06be9972008-08-17 13:54:28 +00001095
Oscar Fuentes92adc192008-11-15 21:36:30 +00001096extern "C" int PowerPCAsmPrinterForceLink;
1097int PowerPCAsmPrinterForceLink = 0;
Douglas Gregor1555a232009-06-16 20:12:29 +00001098
Bob Wilsona96751f2009-06-23 23:59:40 +00001099// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001100extern "C" void LLVMInitializePowerPCAsmPrinter() {
1101 extern Target ThePPC32Target;
1102 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1103
1104 extern Target ThePPC64Target;
1105 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1106}