blob: 1ffd65265f17af334f52bfe71b5f8201d450bf7b [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb604b4d2004-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 Lattner07fad1c2004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukmane05203f2004-06-21 16:55:25 +000013//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman811f5c22004-06-29 17:13:26 +000016//
Misha Brukmane05203f2004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattner8c6a41e2006-11-17 22:10:59 +000021#include "PPCPredicates.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000022#include "PPCTargetMachine.h"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000023#include "PPCSubtarget.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000028#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb0609d92006-01-04 13:52:30 +000029#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey7c462762005-12-16 22:45:29 +000030#include "llvm/CodeGen/MachineDebugInfo.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000033#include "llvm/Support/Mangler.h"
Nate Begeman4d847042004-09-04 14:51:26 +000034#include "llvm/Support/MathExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000038#include "llvm/Target/TargetAsmInfo.h"
Nate Begemandd8f1d82004-10-26 06:02:38 +000039#include "llvm/Target/MRegisterInfo.h"
Nate Begeman3f76eb62004-11-25 07:09:01 +000040#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng5f997602006-02-18 00:08:58 +000041#include "llvm/Target/TargetOptions.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/StringExtras.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000044#include <set>
Chris Lattner0ced9052004-08-16 23:25:21 +000045using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000046
47namespace {
Chris Lattner700b8732006-12-06 17:46:33 +000048 Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
Misha Brukmane05203f2004-06-21 16:55:25 +000049
Jim Laskeya6211dc2006-09-06 18:34:40 +000050 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
Chris Lattner57575112005-12-16 00:22:14 +000051 std::set<std::string> FnStubs, GVStubs;
Chris Lattner8597a2f2006-09-20 17:07:15 +000052 const PPCSubtarget &Subtarget;
Evan Chengfa54c0b2006-12-01 07:56:37 +000053
Jim Laskey261779b2006-09-07 22:06:40 +000054 PPCAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Chris Lattner8597a2f2006-09-20 17:07:15 +000055 : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) {
56 }
Misha Brukmanb4402432005-04-21 23:30:14 +000057
Misha Brukmane05203f2004-06-21 16:55:25 +000058 virtual const char *getPassName() const {
Nate Begeman4bfceb12004-09-04 05:00:00 +000059 return "PowerPC Assembly Printer";
Misha Brukmane05203f2004-06-21 16:55:25 +000060 }
61
Nate Begeman6cca84e2005-10-16 05:39:50 +000062 PPCTargetMachine &getTM() {
63 return static_cast<PPCTargetMachine&>(TM);
Chris Lattner0ced9052004-08-16 23:25:21 +000064 }
65
Nate Begeman8465fe82005-07-20 22:42:00 +000066 unsigned enumRegToMachineReg(unsigned enumReg) {
67 switch (enumReg) {
68 default: assert(0 && "Unhandled register!"); break;
69 case PPC::CR0: return 0;
70 case PPC::CR1: return 1;
71 case PPC::CR2: return 2;
72 case PPC::CR3: return 3;
73 case PPC::CR4: return 4;
74 case PPC::CR5: return 5;
75 case PPC::CR6: return 6;
76 case PPC::CR7: return 7;
77 }
78 abort();
79 }
80
Nate Begeman0ad7f812004-08-14 22:09:10 +000081 /// printInstruction - This method is automatically generated by tablegen
82 /// from the instruction set description. This method returns true if the
83 /// machine instruction was sufficiently described to print it, otherwise it
84 /// returns false.
85 bool printInstruction(const MachineInstr *MI);
86
Misha Brukmane05203f2004-06-21 16:55:25 +000087 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner6ab87fa2005-11-17 19:25:59 +000088 void printOp(const MachineOperand &MO);
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000089
Chris Lattnerf7f05672006-02-01 22:38:46 +000090 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000091 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner10b71c02006-05-04 18:05:43 +000092 if (MO.isRegister()) {
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000093 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Chris Lattner956820d2005-08-22 22:00:02 +000094 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Chris Lattnerda2e56f2004-08-15 05:46:14 +000095 } else if (MO.isImmediate()) {
96 O << MO.getImmedValue();
Chris Lattnerec1cc1b2004-08-14 23:27:29 +000097 } else {
98 printOp(MO);
99 }
100 }
Chris Lattnerf7f05672006-02-01 22:38:46 +0000101
102 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner1bad2542006-02-23 19:31:10 +0000103 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner7674d902006-02-24 20:27:40 +0000104 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
105 unsigned AsmVariant, const char *ExtraCode);
106
Chris Lattnerf7f05672006-02-01 22:38:46 +0000107
Chris Lattner2771e2c2006-03-25 06:12:06 +0000108 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
109 char value = MI->getOperand(OpNo).getImmedValue();
110 value = (value << (32-5)) >> (32-5);
111 O << (int)value;
112 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000113 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000114 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattnerf7833ba2004-08-21 19:11:03 +0000115 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begeman3ad3ad42004-08-21 05:56:39 +0000116 O << (unsigned int)value;
117 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000118 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman143cf942004-08-30 02:28:06 +0000119 unsigned char value = MI->getOperand(OpNo).getImmedValue();
120 assert(value <= 63 && "Invalid u6imm argument!");
121 O << (unsigned int)value;
122 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000123 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000124 O << (short)MI->getOperand(OpNo).getImmedValue();
125 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000126 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner8a796852004-08-15 05:20:16 +0000127 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
128 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000129 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner30055b92006-11-16 21:45:30 +0000130 if (MI->getOperand(OpNo).isImmediate()) {
131 O << (short)(MI->getOperand(OpNo).getImmedValue()*4);
132 } else {
133 O << "lo16(";
134 printOp(MI->getOperand(OpNo));
135 if (TM.getRelocationModel() == Reloc::PIC_)
136 O << "-\"L" << getFunctionNumber() << "$pb\")";
137 else
138 O << ')';
139 }
Chris Lattner5a2fb972005-10-18 16:51:22 +0000140 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000141 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000142 // Branches can take an immediate operand. This is used by the branch
143 // selection pass to print $+8, an eight byte displacement from the PC.
144 if (MI->getOperand(OpNo).isImmediate()) {
Evan Chengd7572fb2006-08-25 21:54:44 +0000145 O << "$+" << MI->getOperand(OpNo).getImmedValue()*4;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000146 } else {
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000147 printOp(MI->getOperand(OpNo));
Nate Begeman4bfceb12004-09-04 05:00:00 +0000148 }
Nate Begeman61738782004-09-02 08:13:00 +0000149 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000150 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000151 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng73136df2006-02-22 20:19:42 +0000152 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner57575112005-12-16 00:22:14 +0000153 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
154 GlobalValue *GV = MO.getGlobal();
155 if (((GV->isExternal() || GV->hasWeakLinkage() ||
156 GV->hasLinkOnceLinkage()))) {
157 // Dynamically-resolved functions need a stub for the function.
158 std::string Name = Mang->getValueName(GV);
159 FnStubs.insert(Name);
160 O << "L" << Name << "$stub";
Evan Chengfa54c0b2006-12-01 07:56:37 +0000161 if (GV->hasExternalWeakLinkage())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000162 ExtWeakSymbols.insert(GV);
Chris Lattner57575112005-12-16 00:22:14 +0000163 return;
164 }
165 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000166 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000167 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
Chris Lattnercdde99902005-11-17 19:40:30 +0000168 FnStubs.insert(Name);
169 O << "L" << Name << "$stub";
170 return;
Chris Lattnercdde99902005-11-17 19:40:30 +0000171 }
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000172 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000173
174 printOp(MI->getOperand(OpNo));
Chris Lattnerbd9efdb2005-11-17 19:16:08 +0000175 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000176 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemana171f6b2005-11-16 00:48:01 +0000177 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
178 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000179 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner57575112005-12-16 00:22:14 +0000180 O << "\"L" << getFunctionNumber() << "$pb\"\n";
181 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begeman61738782004-09-02 08:13:00 +0000182 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000183 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begemana9443f22005-07-21 20:44:43 +0000184 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000185 printS16ImmOperand(MI, OpNo);
Nate Begemana9443f22005-07-21 20:44:43 +0000186 } else {
187 O << "ha16(";
188 printOp(MI->getOperand(OpNo));
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000189 if (TM.getRelocationModel() == Reloc::PIC_)
Chris Lattner57575112005-12-16 00:22:14 +0000190 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begemana9443f22005-07-21 20:44:43 +0000191 else
192 O << ')';
193 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000194 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000195 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000196 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000197 printS16ImmOperand(MI, OpNo);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000198 } else {
199 O << "lo16(";
Nate Begeman3f76eb62004-11-25 07:09:01 +0000200 printOp(MI->getOperand(OpNo));
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000201 if (TM.getRelocationModel() == Reloc::PIC_)
Chris Lattner57575112005-12-16 00:22:14 +0000202 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begemana9443f22005-07-21 20:44:43 +0000203 else
204 O << ')';
Nate Begeman4bfceb12004-09-04 05:00:00 +0000205 }
206 }
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000207 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman8465fe82005-07-20 22:42:00 +0000208 unsigned CCReg = MI->getOperand(OpNo).getReg();
209 unsigned RegNo = enumRegToMachineReg(CCReg);
210 O << (0x80 >> RegNo);
211 }
Chris Lattner7674d902006-02-24 20:27:40 +0000212 // The new addressing mode printers.
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000213 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
214 printSymbolLo(MI, OpNo);
215 O << '(';
Chris Lattner139eac52006-03-21 17:21:13 +0000216 if (MI->getOperand(OpNo+1).isRegister() &&
217 MI->getOperand(OpNo+1).getReg() == PPC::R0)
218 O << "0";
219 else
220 printOperand(MI, OpNo+1);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000221 O << ')';
222 }
Chris Lattner77373d12006-03-22 05:26:03 +0000223 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
224 if (MI->getOperand(OpNo).isImmediate())
225 printS16X4ImmOperand(MI, OpNo);
226 else
227 printSymbolLo(MI, OpNo);
228 O << '(';
229 if (MI->getOperand(OpNo+1).isRegister() &&
230 MI->getOperand(OpNo+1).getReg() == PPC::R0)
231 O << "0";
232 else
233 printOperand(MI, OpNo+1);
234 O << ')';
235 }
236
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000237 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc1263972005-12-19 23:40:42 +0000238 // When used as the base register, r0 reads constant zero rather than
239 // the value contained in the register. For this reason, the darwin
240 // assembler requires that we print r0 as 0 (no r) when used as the base.
241 const MachineOperand &MO = MI->getOperand(OpNo);
242 if (MO.getReg() == PPC::R0)
243 O << '0';
244 else
245 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000246 O << ", ";
247 printOperand(MI, OpNo+1);
248 }
249
Chris Lattner6be72602006-11-04 05:27:39 +0000250 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
251 const char *Modifier);
252
Misha Brukmanb4402432005-04-21 23:30:14 +0000253 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000254 virtual bool doFinalization(Module &M) = 0;
255 };
Misha Brukmanb4402432005-04-21 23:30:14 +0000256
Misha Brukman175fdd42004-09-05 02:42:44 +0000257 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
258 /// X
Chris Lattner996795b2006-06-28 23:17:24 +0000259 struct VISIBILITY_HIDDEN DarwinAsmPrinter : public PPCAsmPrinter {
Jim Laskeyb0609d92006-01-04 13:52:30 +0000260
Jim Laskeya6211dc2006-09-06 18:34:40 +0000261 DwarfWriter DW;
Nate Begeman4bfceb12004-09-04 05:00:00 +0000262
Jim Laskey261779b2006-09-07 22:06:40 +0000263 DarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
264 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000265 : PPCAsmPrinter(O, TM, T), DW(O, this, T) {
Nate Begeman4bfceb12004-09-04 05:00:00 +0000266 }
267
268 virtual const char *getPassName() const {
269 return "Darwin PPC Assembly Printer";
270 }
Chris Lattnere0f5f8e2005-12-09 18:24:29 +0000271
Misha Brukmanb4402432005-04-21 23:30:14 +0000272 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman15527112005-07-21 01:25:49 +0000273 bool doInitialization(Module &M);
Misha Brukmane05203f2004-06-21 16:55:25 +0000274 bool doFinalization(Module &M);
Jim Laskey219d5592006-01-04 22:28:25 +0000275
276 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000277 AU.setPreservesAll();
Jim Laskey219d5592006-01-04 22:28:25 +0000278 AU.addRequired<MachineDebugInfo>();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000279 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskey219d5592006-01-04 22:28:25 +0000280 }
281
Chris Lattner028d6632006-10-05 02:42:20 +0000282 /// getSectionForFunction - Return the section that we should emit the
283 /// specified function body into.
284 virtual std::string getSectionForFunction(const Function &F) const;
Misha Brukmane05203f2004-06-21 16:55:25 +0000285 };
286} // end of anonymous namespace
287
Nate Begeman0ad7f812004-08-14 22:09:10 +0000288// Include the auto-generated portion of the assembly writer
Chris Lattner0921e3b2005-10-14 23:37:35 +0000289#include "PPCGenAsmWriter.inc"
Nate Begeman0ad7f812004-08-14 22:09:10 +0000290
Chris Lattner6ab87fa2005-11-17 19:25:59 +0000291void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukmane05203f2004-06-21 16:55:25 +0000292 switch (MO.getType()) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000293 case MachineOperand::MO_Immediate:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000294 cerr << "printOp() does not handle immediate values\n";
Misha Brukman47d5a222004-07-28 00:00:48 +0000295 abort();
Misha Brukman8d75aa42004-07-21 20:11:11 +0000296 return;
297
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000298 case MachineOperand::MO_MachineBasicBlock:
299 printBasicBlockLabel(MO.getMachineBasicBlock());
Misha Brukmane05203f2004-06-21 16:55:25 +0000300 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000301 case MachineOperand::MO_JumpTableIndex:
Jim Laskeya6211dc2006-09-06 18:34:40 +0000302 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000303 << '_' << MO.getJumpTableIndex();
304 // FIXME: PIC relocation model
305 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000306 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskeya6211dc2006-09-06 18:34:40 +0000307 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner41cb1152005-11-21 06:47:58 +0000308 << '_' << MO.getConstantPoolIndex();
Misha Brukmane05203f2004-06-21 16:55:25 +0000309 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000310 case MachineOperand::MO_ExternalSymbol:
Chris Lattner57575112005-12-16 00:22:14 +0000311 // Computing the address of an external symbol, not calling it.
Evan Cheng73136df2006-02-22 20:19:42 +0000312 if (TM.getRelocationModel() != Reloc::Static) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000313 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
Chris Lattner57575112005-12-16 00:22:14 +0000314 GVStubs.insert(Name);
315 O << "L" << Name << "$non_lazy_ptr";
316 return;
317 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000318 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000319 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000320 case MachineOperand::MO_GlobalAddress: {
Chris Lattner57575112005-12-16 00:22:14 +0000321 // Computing the address of a global symbol, not calling it.
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000322 GlobalValue *GV = MO.getGlobal();
323 std::string Name = Mang->getValueName(GV);
Misha Brukman7dba17d2004-07-23 16:08:20 +0000324
Nate Begeman844186b2004-10-17 23:01:34 +0000325 // External or weakly linked global variables need non-lazily-resolved stubs
Evan Cheng73136df2006-02-22 20:19:42 +0000326 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner57575112005-12-16 00:22:14 +0000327 if (((GV->isExternal() || GV->hasWeakLinkage() ||
328 GV->hasLinkOnceLinkage()))) {
Nate Begeman3f76eb62004-11-25 07:09:01 +0000329 GVStubs.insert(Name);
Chris Lattner57575112005-12-16 00:22:14 +0000330 O << "L" << Name << "$non_lazy_ptr";
331 return;
332 }
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000333 }
Chris Lattnercdde99902005-11-17 19:40:30 +0000334 O << Name;
Evan Chengfa54c0b2006-12-01 07:56:37 +0000335
336 if (GV->hasExternalWeakLinkage())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000337 ExtWeakSymbols.insert(GV);
Misha Brukmane05203f2004-06-21 16:55:25 +0000338 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000339 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000340
Misha Brukmane05203f2004-06-21 16:55:25 +0000341 default:
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000342 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukmana2737582004-06-25 15:11:34 +0000343 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000344 }
345}
346
Chris Lattner1bad2542006-02-23 19:31:10 +0000347/// PrintAsmOperand - Print out an operand for an inline asm expression.
348///
349bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
350 unsigned AsmVariant,
351 const char *ExtraCode) {
352 // Does this asm operand have a single letter operand modifier?
353 if (ExtraCode && ExtraCode[0]) {
354 if (ExtraCode[1] != 0) return true; // Unknown modifier.
355
356 switch (ExtraCode[0]) {
357 default: return true; // Unknown modifier.
358 case 'L': // Write second word of DImode reference.
359 // Verify that this operand has two consecutive registers.
360 if (!MI->getOperand(OpNo).isRegister() ||
361 OpNo+1 == MI->getNumOperands() ||
362 !MI->getOperand(OpNo+1).isRegister())
363 return true;
364 ++OpNo; // Return the high-part.
365 break;
366 }
367 }
368
369 printOperand(MI, OpNo);
370 return false;
371}
372
Chris Lattner7674d902006-02-24 20:27:40 +0000373bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
374 unsigned AsmVariant,
375 const char *ExtraCode) {
376 if (ExtraCode && ExtraCode[0])
377 return true; // Unknown modifier.
378 printMemRegReg(MI, OpNo);
379 return false;
380}
Chris Lattner1bad2542006-02-23 19:31:10 +0000381
Chris Lattner6be72602006-11-04 05:27:39 +0000382void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
383 const char *Modifier) {
384 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
385 unsigned Code = MI->getOperand(OpNo).getImm();
386 if (!strcmp(Modifier, "cc")) {
387 switch ((PPC::Predicate)Code) {
388 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
389 case PPC::PRED_LT: O << "lt"; return;
390 case PPC::PRED_LE: O << "le"; return;
391 case PPC::PRED_EQ: O << "eq"; return;
392 case PPC::PRED_GE: O << "ge"; return;
393 case PPC::PRED_GT: O << "gt"; return;
394 case PPC::PRED_NE: O << "ne"; return;
395 case PPC::PRED_UN: O << "un"; return;
396 case PPC::PRED_NU: O << "nu"; return;
397 }
398
399 } else {
400 assert(!strcmp(Modifier, "reg") &&
401 "Need to specify 'cc' or 'reg' as predicate op modifier!");
402 // Don't print the register for 'always'.
403 if (Code == PPC::PRED_ALWAYS) return;
404 printOperand(MI, OpNo+1);
405 }
406}
407
408
Nate Begeman0ad7f812004-08-14 22:09:10 +0000409/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
410/// the current output stream.
Misha Brukmane05203f2004-06-21 16:55:25 +0000411///
Chris Lattner0921e3b2005-10-14 23:37:35 +0000412void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begeman0ad7f812004-08-14 22:09:10 +0000413 ++EmittedInsts;
Chris Lattner2121f3c2005-10-14 22:44:13 +0000414
Nate Begeman52441732005-04-05 18:19:50 +0000415 // Check for slwi/srwi mnemonics.
416 if (MI->getOpcode() == PPC::RLWINM) {
417 bool FoundMnemonic = false;
418 unsigned char SH = MI->getOperand(2).getImmedValue();
419 unsigned char MB = MI->getOperand(3).getImmedValue();
420 unsigned char ME = MI->getOperand(4).getImmedValue();
421 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
422 O << "slwi "; FoundMnemonic = true;
423 }
424 if (SH <= 31 && MB == (32-SH) && ME == 31) {
425 O << "srwi "; FoundMnemonic = true;
426 SH = 32-SH;
427 }
428 if (FoundMnemonic) {
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000429 printOperand(MI, 0);
Misha Brukmanb4402432005-04-21 23:30:14 +0000430 O << ", ";
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000431 printOperand(MI, 1);
Nate Begeman52441732005-04-05 18:19:50 +0000432 O << ", " << (unsigned int)SH << "\n";
433 return;
434 }
Chris Lattner52a956d2006-06-20 23:18:58 +0000435 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
Chris Lattnerf7b962d2006-02-08 06:56:40 +0000436 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
437 O << "mr ";
438 printOperand(MI, 0);
439 O << ", ";
440 printOperand(MI, 1);
441 O << "\n";
442 return;
443 }
Chris Lattner9ca15c82006-11-18 01:23:56 +0000444 } else if (MI->getOpcode() == PPC::RLDICR) {
445 unsigned char SH = MI->getOperand(2).getImmedValue();
446 unsigned char ME = MI->getOperand(3).getImmedValue();
447 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
448 if (63-SH == ME) {
449 O << "sldi ";
450 printOperand(MI, 0);
451 O << ", ";
452 printOperand(MI, 1);
453 O << ", " << (unsigned int)SH << "\n";
454 return;
455 }
Nate Begeman52441732005-04-05 18:19:50 +0000456 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000457
Nate Begeman0ad7f812004-08-14 22:09:10 +0000458 if (printInstruction(MI))
459 return; // Printer was automatically generated
Misha Brukmanb4402432005-04-21 23:30:14 +0000460
Nate Begeman4bfceb12004-09-04 05:00:00 +0000461 assert(0 && "Unhandled instruction in asm writer!");
462 abort();
Nate Begeman0ad7f812004-08-14 22:09:10 +0000463 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000464}
465
Chris Lattner028d6632006-10-05 02:42:20 +0000466
467
468std::string DarwinAsmPrinter::getSectionForFunction(const Function &F) const {
469 switch (F.getLinkage()) {
470 default: assert(0 && "Unknown linkage type!");
471 case Function::ExternalLinkage:
472 case Function::InternalLinkage: return TAI->getTextSection();
473 case Function::WeakLinkage:
474 case Function::LinkOnceLinkage:
475 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
476 }
477}
478
Nate Begeman4bfceb12004-09-04 05:00:00 +0000479/// runOnMachineFunction - This uses the printMachineInstruction()
480/// method to print assembly for each instruction.
481///
482bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000483 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
484
Chris Lattner99946fb2005-11-21 07:51:23 +0000485 SetupMachineFunction(MF);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000486 O << "\n\n";
Jim Laskey7c462762005-12-16 22:45:29 +0000487
Nate Begeman4bfceb12004-09-04 05:00:00 +0000488 // Print out constants referenced by the function
Chris Lattneref83ebd2005-11-21 08:26:15 +0000489 EmitConstantPool(MF.getConstantPool());
Nate Begeman4bfceb12004-09-04 05:00:00 +0000490
491 // Print out labels for the function.
Chris Lattner0aacd2a2005-11-14 18:52:46 +0000492 const Function *F = MF.getFunction();
Chris Lattner028d6632006-10-05 02:42:20 +0000493 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Chris Lattner0d236452006-10-05 00:35:50 +0000494
Chris Lattner57575112005-12-16 00:22:14 +0000495 switch (F->getLinkage()) {
496 default: assert(0 && "Unknown linkage type!");
497 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattner57575112005-12-16 00:22:14 +0000498 break;
499 case Function::ExternalLinkage:
Chris Lattner97d72c82005-10-28 18:44:07 +0000500 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner57575112005-12-16 00:22:14 +0000501 break;
502 case Function::WeakLinkage:
503 case Function::LinkOnceLinkage:
Chris Lattner57575112005-12-16 00:22:14 +0000504 O << "\t.globl\t" << CurrentFnName << "\n";
505 O << "\t.weak_definition\t" << CurrentFnName << "\n";
506 break;
507 }
Chris Lattner84fb09e2006-02-14 20:42:33 +0000508 EmitAlignment(4, F);
Nate Begeman4bfceb12004-09-04 05:00:00 +0000509 O << CurrentFnName << ":\n";
510
Jim Laskeyc0d65182006-04-07 20:44:42 +0000511 // Emit pre-function debug information.
Jim Laskeya7b2bd52006-06-23 12:51:53 +0000512 DW.BeginFunction(&MF);
Jim Laskeyc0d65182006-04-07 20:44:42 +0000513
Nate Begeman4bfceb12004-09-04 05:00:00 +0000514 // Print out code for the function.
515 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
516 I != E; ++I) {
517 // Print a label for the basic block.
Chris Lattner968aeb12005-08-21 19:09:33 +0000518 if (I != MF.begin()) {
Nate Begemanb9d4f832006-05-02 05:37:32 +0000519 printBasicBlockLabel(I, true);
520 O << '\n';
Chris Lattner968aeb12005-08-21 19:09:33 +0000521 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000522 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
523 II != E; ++II) {
524 // Print the assembly for the instruction.
525 O << "\t";
526 printMachineInstruction(II);
527 }
528 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000529
Chris Lattner41e22a52006-10-05 00:26:05 +0000530 // Print out jump tables referenced by the function.
Chris Lattnera6a570e2006-10-05 03:01:21 +0000531 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Chris Lattner41e22a52006-10-05 00:26:05 +0000532
Jim Laskeyb0609d92006-01-04 13:52:30 +0000533 // Emit post-function debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +0000534 DW.EndFunction();
Chris Lattneraad26a12006-10-05 00:24:46 +0000535
Nate Begeman4bfceb12004-09-04 05:00:00 +0000536 // We didn't modify anything.
537 return false;
538}
539
Misha Brukmane05203f2004-06-21 16:55:25 +0000540
Nate Begeman15527112005-07-21 01:25:49 +0000541bool DarwinAsmPrinter::doInitialization(Module &M) {
Jim Laskey59e7a772006-12-12 20:57:08 +0000542 static const char *CPUDirectives[] = {
543 "ppc",
544 "ppc601",
545 "ppc602",
546 "ppc603",
547 "ppc7400",
548 "ppc750",
549 "ppc970",
550 "ppc64"
551 };
552
553 unsigned Directive = Subtarget.getDarwinDirective();
554 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
555 Directive = PPC::DIR_970;
556 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
557 Directive = PPC::DIR_7400;
558 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
559 Directive = PPC::DIR_64;
560 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
561 O << "\t.machine " << CPUDirectives[Directive] << "\n";
Jim Laskey7c3cab92006-12-12 16:07:33 +0000562
Nate Begeman15527112005-07-21 01:25:49 +0000563 AsmPrinter::doInitialization(M);
Chris Lattner9eb7dfa2005-11-10 19:33:43 +0000564
565 // Darwin wants symbols to be quoted if they have complex names.
566 Mang->setUseQuotes(true);
Jim Laskeyb0609d92006-01-04 13:52:30 +0000567
Jim Laskeyec05b042006-11-28 18:21:52 +0000568 // Prime text sections so they are adjacent. This reduces the likelihood a
569 // large data or debug section causes a branch to exceed 16M limit.
570 SwitchToTextSection(".section __TEXT,__textcoal_nt,coalesced,"
571 "pure_instructions");
572 if (TM.getRelocationModel() == Reloc::PIC_) {
573 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
574 "pure_instructions,32");
575 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
576 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
577 "pure_instructions,16");
578 }
579 SwitchToTextSection(TAI->getTextSection());
580
Jim Laskeyb0609d92006-01-04 13:52:30 +0000581 // Emit initial debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +0000582 DW.BeginModule(&M);
Nate Begeman15527112005-07-21 01:25:49 +0000583 return false;
584}
585
Nate Begeman4bfceb12004-09-04 05:00:00 +0000586bool DarwinAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000587 const TargetData *TD = TM.getTargetData();
Misha Brukmane05203f2004-06-21 16:55:25 +0000588
589 // Print out module-level global variables here.
Chris Lattner1a4adc72005-11-14 19:00:30 +0000590 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattner54a11df2005-12-13 04:33:58 +0000591 I != E; ++I) {
592 if (!I->hasInitializer()) continue; // External global require no code
593
Chris Lattner87079882005-12-13 06:32:50 +0000594 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey313570f2006-03-07 22:00:35 +0000595 if (EmitSpecialLLVMGlobal(I))
Chris Lattner87079882005-12-13 06:32:50 +0000596 continue;
Chris Lattner54a11df2005-12-13 04:33:58 +0000597
Chris Lattner54a11df2005-12-13 04:33:58 +0000598 std::string name = Mang->getValueName(I);
599 Constant *C = I->getInitializer();
Owen Anderson20a631f2006-05-03 01:29:57 +0000600 unsigned Size = TD->getTypeSize(C->getType());
Devang Patel71b99292006-10-24 20:32:14 +0000601 unsigned Align = TD->getPreferredAlignmentLog(I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000602
603 if (C->isNullValue() && /* FIXME: Verify correct */
604 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Chris Lattnerb1345202006-02-14 22:18:23 +0000605 I->hasLinkOnceLinkage() ||
606 (I->hasExternalLinkage() && !I->hasSection()))) {
Chris Lattner54a11df2005-12-13 04:33:58 +0000607 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Chris Lattnerb1345202006-02-14 22:18:23 +0000608 if (I->hasExternalLinkage()) {
609 O << "\t.globl " << name << '\n';
610 O << "\t.zerofill __DATA, __common, " << name << ", "
611 << Size << ", " << Align;
612 } else if (I->hasInternalLinkage()) {
Chris Lattner28141342006-05-09 16:15:00 +0000613 SwitchToDataSection("\t.data", I);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000614 O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align;
Chris Lattnerb1345202006-02-14 22:18:23 +0000615 } else {
Chris Lattner28141342006-05-09 16:15:00 +0000616 SwitchToDataSection("\t.data", I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000617 O << ".comm " << name << "," << Size;
Chris Lattnerb1345202006-02-14 22:18:23 +0000618 }
Chris Lattner54a11df2005-12-13 04:33:58 +0000619 O << "\t\t; '" << I->getName() << "'\n";
620 } else {
621 switch (I->getLinkage()) {
Jim Laskey311622f2006-12-01 14:37:39 +0000622 case GlobalValue::LinkOnceLinkage:
Chris Lattner54a11df2005-12-13 04:33:58 +0000623 case GlobalValue::WeakLinkage:
Chris Lattner6b0325a2005-12-22 21:15:17 +0000624 O << "\t.globl " << name << '\n'
625 << "\t.weak_definition " << name << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000626 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000627 break;
628 case GlobalValue::AppendingLinkage:
629 // FIXME: appending linkage variables should go into a section of
630 // their name or something. For now, just emit them as external.
631 case GlobalValue::ExternalLinkage:
632 // If external or appending, declare as a global symbol
633 O << "\t.globl " << name << "\n";
634 // FALL THROUGH
635 case GlobalValue::InternalLinkage:
Evan Chengae8c29a2006-10-28 05:56:51 +0000636 if (I->isConstant()) {
Evan Chenge1e06c22006-10-26 21:48:57 +0000637 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Evan Chengae8c29a2006-10-28 05:56:51 +0000638 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
Evan Chenge1e06c22006-10-26 21:48:57 +0000639 SwitchToDataSection(TAI->getCStringSection(), I);
640 break;
641 }
642 }
643
Chris Lattner28141342006-05-09 16:15:00 +0000644 SwitchToDataSection("\t.data", I);
Chris Lattner54a11df2005-12-13 04:33:58 +0000645 break;
646 default:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000647 cerr << "Unknown linkage type!";
Chris Lattner54a11df2005-12-13 04:33:58 +0000648 abort();
649 }
650
651 EmitAlignment(Align, I);
652 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
Evan Cheng5fb2c762006-12-01 09:13:26 +0000653
654 // If the initializer is a extern weak symbol, remember to emit the weak
655 // reference!
656 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
657 if (GV->hasExternalWeakLinkage())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000658 ExtWeakSymbols.insert(GV);
Evan Cheng5fb2c762006-12-01 09:13:26 +0000659
Chris Lattner54a11df2005-12-13 04:33:58 +0000660 EmitGlobalConstant(C);
Chris Lattner9436aa72006-01-21 01:35:26 +0000661 O << '\n';
Chris Lattner54a11df2005-12-13 04:33:58 +0000662 }
663 }
Misha Brukmand4ac8182004-07-16 20:29:04 +0000664
Chris Lattner1df08392006-06-27 01:02:25 +0000665 bool isPPC64 = TD->getPointerSizeInBits() == 64;
666
Misha Brukmand4ac8182004-07-16 20:29:04 +0000667 // Output stubs for dynamically-linked functions
Chris Lattner9e56e5c2006-07-26 21:12:04 +0000668 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner54a11df2005-12-13 04:33:58 +0000669 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
670 i != e; ++i) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000671 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000672 "pure_instructions,32");
Chris Lattner1df08392006-06-27 01:02:25 +0000673 EmitAlignment(4);
Chris Lattner54a11df2005-12-13 04:33:58 +0000674 O << "L" << *i << "$stub:\n";
675 O << "\t.indirect_symbol " << *i << "\n";
676 O << "\tmflr r0\n";
677 O << "\tbcl 20,31,L0$" << *i << "\n";
678 O << "L0$" << *i << ":\n";
679 O << "\tmflr r11\n";
680 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
681 O << "\tmtlr r0\n";
Chris Lattner1df08392006-06-27 01:02:25 +0000682 if (isPPC64)
683 O << "\tldu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
684 else
685 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Chris Lattner54a11df2005-12-13 04:33:58 +0000686 O << "\tmtctr r12\n";
687 O << "\tbctr\n";
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000688 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattner54a11df2005-12-13 04:33:58 +0000689 O << "L" << *i << "$lazy_ptr:\n";
690 O << "\t.indirect_symbol " << *i << "\n";
Chris Lattner1df08392006-06-27 01:02:25 +0000691 if (isPPC64)
692 O << "\t.quad dyld_stub_binding_helper\n";
693 else
694 O << "\t.long dyld_stub_binding_helper\n";
Chris Lattner54a11df2005-12-13 04:33:58 +0000695 }
696 } else {
697 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
698 i != e; ++i) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000699 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000700 "pure_instructions,16");
Chris Lattner54a11df2005-12-13 04:33:58 +0000701 EmitAlignment(4);
702 O << "L" << *i << "$stub:\n";
703 O << "\t.indirect_symbol " << *i << "\n";
704 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
Chris Lattner1df08392006-06-27 01:02:25 +0000705 if (isPPC64)
706 O << "\tldu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
707 else
708 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
Chris Lattner54a11df2005-12-13 04:33:58 +0000709 O << "\tmtctr r12\n";
710 O << "\tbctr\n";
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000711 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattner54a11df2005-12-13 04:33:58 +0000712 O << "L" << *i << "$lazy_ptr:\n";
713 O << "\t.indirect_symbol " << *i << "\n";
Chris Lattner1df08392006-06-27 01:02:25 +0000714 if (isPPC64)
715 O << "\t.quad dyld_stub_binding_helper\n";
716 else
717 O << "\t.long dyld_stub_binding_helper\n";
Nate Begemana9443f22005-07-21 20:44:43 +0000718 }
Misha Brukmanf62ee7a2004-06-24 23:04:11 +0000719 }
Misha Brukmane05203f2004-06-21 16:55:25 +0000720
Misha Brukmand4ac8182004-07-16 20:29:04 +0000721 O << "\n";
722
Chris Lattner57575112005-12-16 00:22:14 +0000723 // Output stubs for external and common global variables.
724 if (GVStubs.begin() != GVStubs.end()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000725 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner57575112005-12-16 00:22:14 +0000726 for (std::set<std::string>::iterator I = GVStubs.begin(),
727 E = GVStubs.end(); I != E; ++I) {
728 O << "L" << *I << "$non_lazy_ptr:\n";
729 O << "\t.indirect_symbol " << *I << "\n";
Chris Lattner82ab3e22006-06-27 20:20:53 +0000730 if (isPPC64)
731 O << "\t.quad\t0\n";
732 else
733 O << "\t.long\t0\n";
734
Chris Lattner54a11df2005-12-13 04:33:58 +0000735 }
Nate Begeman0ad7f812004-08-14 22:09:10 +0000736 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000737
Jim Laskeyb9966022006-01-17 17:31:53 +0000738 // Emit initial debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +0000739 DW.EndModule();
Jim Laskeyb9966022006-01-17 17:31:53 +0000740
Chris Lattner7432ceef2005-11-01 00:12:36 +0000741 // Funny Darwin hack: This flag tells the linker that no global symbols
742 // contain code that falls through to other global symbols (e.g. the obvious
743 // implementation of multiple entry points). If this doesn't occur, the
744 // linker can safely perform dead code stripping. Since LLVM never generates
745 // code that does this, it is always safe to set.
Chris Lattnera81a75c2006-09-20 17:12:19 +0000746 O << "\t.subsections_via_symbols\n";
Chris Lattner7432ceef2005-11-01 00:12:36 +0000747
Chris Lattner0ced9052004-08-16 23:25:21 +0000748 AsmPrinter::doFinalization(M);
Misha Brukmane05203f2004-06-21 16:55:25 +0000749 return false; // success
750}
Nate Begeman4bfceb12004-09-04 05:00:00 +0000751
Chris Lattnera81a75c2006-09-20 17:12:19 +0000752
753
754/// createDarwinCodePrinterPass - Returns a pass that prints the PPC assembly
755/// code for a MachineFunction to the given output stream, in a format that the
756/// Darwin assembler can deal with.
757///
758FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o,
759 PPCTargetMachine &tm) {
760 return new DarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
761}
762