blob: 3ed7265840147f263d12f1713b57d29458201843 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
12// the output mechanism used by `llc'.
13//
14// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "asmprinter"
20#include "PPC.h"
21#include "PPCPredicates.h"
22#include "PPCTargetMachine.h"
23#include "PPCSubtarget.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
28#include "llvm/CodeGen/AsmPrinter.h"
29#include "llvm/CodeGen/DwarfWriter.h"
30#include "llvm/CodeGen/MachineModuleInfo.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/Mangler.h"
35#include "llvm/Support/MathExtras.h"
36#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/Compiler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000039#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000041#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetOptions.h"
44#include "llvm/ADT/Statistic.h"
45#include "llvm/ADT/StringExtras.h"
46#include <set>
47using namespace llvm;
48
49STATISTIC(EmittedInsts, "Number of machine instrs printed");
50
51namespace {
52 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
53 std::set<std::string> FnStubs, GVStubs;
54 const PPCSubtarget &Subtarget;
55
Owen Anderson847b99b2008-08-21 00:14:44 +000056 PPCAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) {
58 }
59
60 virtual const char *getPassName() const {
61 return "PowerPC Assembly Printer";
62 }
63
64 PPCTargetMachine &getTM() {
65 return static_cast<PPCTargetMachine&>(TM);
66 }
67
68 unsigned enumRegToMachineReg(unsigned enumReg) {
69 switch (enumReg) {
70 default: assert(0 && "Unhandled register!"); break;
71 case PPC::CR0: return 0;
72 case PPC::CR1: return 1;
73 case PPC::CR2: return 2;
74 case PPC::CR3: return 3;
75 case PPC::CR4: return 4;
76 case PPC::CR5: return 5;
77 case PPC::CR6: return 6;
78 case PPC::CR7: return 7;
79 }
80 abort();
81 }
82
83 /// printInstruction - This method is automatically generated by tablegen
84 /// from the instruction set description. This method returns true if the
85 /// machine instruction was sufficiently described to print it, otherwise it
86 /// returns false.
87 bool printInstruction(const MachineInstr *MI);
88
89 void printMachineInstruction(const MachineInstr *MI);
90 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +000091
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 /// stripRegisterPrefix - This method strips the character prefix from a
93 /// register name so that only the number is left. Used by for linux asm.
94 const char *stripRegisterPrefix(const char *RegName) {
95 switch (RegName[0]) {
96 case 'r':
97 case 'f':
98 case 'v': return RegName + 1;
99 case 'c': if (RegName[1] == 'r') return RegName + 2;
100 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 return RegName;
103 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 /// printRegister - Print register according to target requirements.
106 ///
107 void printRegister(const MachineOperand &MO, bool R0AsZero) {
108 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000109 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 // If we should use 0 for R0.
112 if (R0AsZero && RegNo == PPC::R0) {
113 O << "0";
114 return;
115 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000116
Bill Wendling8eeb9792008-02-26 21:11:01 +0000117 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // Linux assembler (Others?) does not take register mnemonics.
119 // FIXME - What about special registers used in mfspr/mtspr?
120 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
121 O << RegName;
122 }
123
124 void printOperand(const MachineInstr *MI, unsigned OpNo) {
125 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000126 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000128 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000129 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 } else {
131 printOp(MO);
132 }
133 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
136 unsigned AsmVariant, const char *ExtraCode);
137 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
138 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000139
140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000142 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 value = (value << (32-5)) >> (32-5);
144 O << (int)value;
145 }
146 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000147 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 assert(value <= 31 && "Invalid u5imm argument!");
149 O << (unsigned int)value;
150 }
151 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000152 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 assert(value <= 63 && "Invalid u6imm argument!");
154 O << (unsigned int)value;
155 }
156 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000157 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 }
159 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000160 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 }
162 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000163 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000164 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 } else {
166 O << "lo16(";
167 printOp(MI->getOperand(OpNo));
168 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000169 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 else
171 O << ')';
172 }
173 }
174 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
175 // Branches can take an immediate operand. This is used by the branch
176 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000177 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000178 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else {
180 printOp(MI->getOperand(OpNo));
181 }
182 }
183 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
184 const MachineOperand &MO = MI->getOperand(OpNo);
185 if (TM.getRelocationModel() != Reloc::Static) {
186 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
187 GlobalValue *GV = MO.getGlobal();
188 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000189 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 // Dynamically-resolved functions need a stub for the function.
191 std::string Name = Mang->getValueName(GV);
192 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000193 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 if (GV->hasExternalWeakLinkage())
195 ExtWeakSymbols.insert(GV);
196 return;
197 }
198 }
199 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
200 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
201 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000202 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 return;
204 }
205 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 printOp(MI->getOperand(OpNo));
208 }
209 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000210 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 }
212 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000213 O << "\"L" << getFunctionNumber() << "$pb\"\n";
214 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 }
216 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000217 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 printS16ImmOperand(MI, OpNo);
219 } else {
220 if (Subtarget.isDarwin()) O << "ha16(";
221 printOp(MI->getOperand(OpNo));
222 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000223 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 if (Subtarget.isDarwin())
225 O << ')';
226 else
227 O << "@ha";
228 }
229 }
230 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000231 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 printS16ImmOperand(MI, OpNo);
233 } else {
234 if (Subtarget.isDarwin()) O << "lo16(";
235 printOp(MI->getOperand(OpNo));
236 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000237 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 if (Subtarget.isDarwin())
239 O << ')';
240 else
241 O << "@l";
242 }
243 }
244 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
245 unsigned CCReg = MI->getOperand(OpNo).getReg();
246 unsigned RegNo = enumRegToMachineReg(CCReg);
247 O << (0x80 >> RegNo);
248 }
249 // The new addressing mode printers.
250 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
251 printSymbolLo(MI, OpNo);
252 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000253 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 MI->getOperand(OpNo+1).getReg() == PPC::R0)
255 O << "0";
256 else
257 printOperand(MI, OpNo+1);
258 O << ')';
259 }
260 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000261 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000263 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 printSymbolLo(MI, OpNo);
265 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000266 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 MI->getOperand(OpNo+1).getReg() == PPC::R0)
268 O << "0";
269 else
270 printOperand(MI, OpNo+1);
271 O << ')';
272 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
275 // When used as the base register, r0 reads constant zero rather than
276 // the value contained in the register. For this reason, the darwin
277 // assembler requires that we print r0 as 0 (no r) when used as the base.
278 const MachineOperand &MO = MI->getOperand(OpNo);
279 printRegister(MO, true);
280 O << ", ";
281 printOperand(MI, OpNo+1);
282 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000283
284 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
288 virtual bool doFinalization(Module &M) = 0;
289
290 virtual void EmitExternalGlobal(const GlobalVariable *GV);
291 };
292
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000293 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
294 struct VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
296 DwarfWriter DW;
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000297 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298
Owen Anderson847b99b2008-08-21 00:14:44 +0000299 PPCLinuxAsmPrinter(raw_ostream &O, PPCTargetMachine &TM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 const TargetAsmInfo *T)
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000301 : PPCAsmPrinter(O, TM, T), DW(O, this, T), MMI(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 }
303
304 virtual const char *getPassName() const {
305 return "Linux PPC Assembly Printer";
306 }
307
308 bool runOnMachineFunction(MachineFunction &F);
309 bool doInitialization(Module &M);
310 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 void getAnalysisUsage(AnalysisUsage &AU) const {
313 AU.setPreservesAll();
314 AU.addRequired<MachineModuleInfo>();
315 PPCAsmPrinter::getAnalysisUsage(AU);
316 }
317
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000318 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 };
320
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000321 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
322 /// OS X
323 struct VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 DwarfWriter DW;
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000326 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
Owen Anderson847b99b2008-08-21 00:14:44 +0000328 PPCDarwinAsmPrinter(raw_ostream &O, PPCTargetMachine &TM,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000329 const TargetAsmInfo *T)
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000330 : PPCAsmPrinter(O, TM, T), DW(O, this, T), MMI(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 }
332
333 virtual const char *getPassName() const {
334 return "Darwin PPC Assembly Printer";
335 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000336
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 bool runOnMachineFunction(MachineFunction &F);
338 bool doInitialization(Module &M);
339 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000340
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 void getAnalysisUsage(AnalysisUsage &AU) const {
342 AU.setPreservesAll();
343 AU.addRequired<MachineModuleInfo>();
344 PPCAsmPrinter::getAnalysisUsage(AU);
345 }
346
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000347 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 };
349} // end of anonymous namespace
350
351// Include the auto-generated portion of the assembly writer
352#include "PPCGenAsmWriter.inc"
353
354void PPCAsmPrinter::printOp(const MachineOperand &MO) {
355 switch (MO.getType()) {
356 case MachineOperand::MO_Immediate:
357 cerr << "printOp() does not handle immediate values\n";
358 abort();
359 return;
360
361 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000362 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 return;
364 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000365 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000366 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 // FIXME: PIC relocation model
368 return;
369 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000370 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000371 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 return;
373 case MachineOperand::MO_ExternalSymbol:
374 // Computing the address of an external symbol, not calling it.
375 if (TM.getRelocationModel() != Reloc::Static) {
376 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
377 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000378 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 return;
380 }
381 O << TAI->getGlobalPrefix() << MO.getSymbolName();
382 return;
383 case MachineOperand::MO_GlobalAddress: {
384 // Computing the address of a global symbol, not calling it.
385 GlobalValue *GV = MO.getGlobal();
386 std::string Name = Mang->getValueName(GV);
387
388 // External or weakly linked global variables need non-lazily-resolved stubs
389 if (TM.getRelocationModel() != Reloc::Static) {
Bill Wendlingfa13d342008-12-04 04:07:00 +0000390 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
391 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000393 printSuffixedName(Name, "$non_lazy_ptr");
Dale Johannesencaf11182008-05-16 20:09:25 +0000394 if (GV->hasExternalWeakLinkage())
395 ExtWeakSymbols.insert(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 return;
397 }
398 }
399 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000400
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000401 printOffset(MO.getOffset());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000402
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 if (GV->hasExternalWeakLinkage())
404 ExtWeakSymbols.insert(GV);
405 return;
406 }
407
408 default:
409 O << "<unknown operand type: " << MO.getType() << ">";
410 return;
411 }
412}
413
414/// EmitExternalGlobal - In this case we need to use the indirect symbol.
415///
416void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
417 std::string Name = getGlobalLinkName(GV);
418 if (TM.getRelocationModel() != Reloc::Static) {
419 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000420 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 return;
422 }
423 O << Name;
424}
425
426/// PrintAsmOperand - Print out an operand for an inline asm expression.
427///
428bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000429 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 const char *ExtraCode) {
431 // Does this asm operand have a single letter operand modifier?
432 if (ExtraCode && ExtraCode[0]) {
433 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 switch (ExtraCode[0]) {
436 default: return true; // Unknown modifier.
437 case 'c': // Don't print "$" before a global var name or constant.
438 // PPC never has a prefix.
439 printOperand(MI, OpNo);
440 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000441 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000443 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000445 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 return true;
447 ++OpNo; // Return the high-part.
448 break;
449 case 'I':
450 // Write 'i' if an integer constant, otherwise nothing. Used to print
451 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000452 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 O << "i";
454 return false;
455 }
456 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000457
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 printOperand(MI, OpNo);
459 return false;
460}
461
462bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000463 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 const char *ExtraCode) {
465 if (ExtraCode && ExtraCode[0])
466 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000467 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 printMemRegReg(MI, OpNo);
469 else
470 printMemRegImm(MI, OpNo);
471 return false;
472}
473
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000474void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 const char *Modifier) {
476 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
477 unsigned Code = MI->getOperand(OpNo).getImm();
478 if (!strcmp(Modifier, "cc")) {
479 switch ((PPC::Predicate)Code) {
480 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
481 case PPC::PRED_LT: O << "lt"; return;
482 case PPC::PRED_LE: O << "le"; return;
483 case PPC::PRED_EQ: O << "eq"; return;
484 case PPC::PRED_GE: O << "ge"; return;
485 case PPC::PRED_GT: O << "gt"; return;
486 case PPC::PRED_NE: O << "ne"; return;
487 case PPC::PRED_UN: O << "un"; return;
488 case PPC::PRED_NU: O << "nu"; return;
489 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000490
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 } else {
492 assert(!strcmp(Modifier, "reg") &&
493 "Need to specify 'cc' or 'reg' as predicate op modifier!");
494 // Don't print the register for 'always'.
495 if (Code == PPC::PRED_ALWAYS) return;
496 printOperand(MI, OpNo+1);
497 }
498}
499
500
501/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
502/// the current output stream.
503///
504void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
505 ++EmittedInsts;
506
507 // Check for slwi/srwi mnemonics.
508 if (MI->getOpcode() == PPC::RLWINM) {
509 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000510 unsigned char SH = MI->getOperand(2).getImm();
511 unsigned char MB = MI->getOperand(3).getImm();
512 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000514 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 }
516 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000517 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 SH = 32-SH;
519 }
520 if (FoundMnemonic) {
521 printOperand(MI, 0);
522 O << ", ";
523 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000524 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 return;
526 }
527 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
528 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000529 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 printOperand(MI, 0);
531 O << ", ";
532 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000533 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 return;
535 }
536 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000537 unsigned char SH = MI->getOperand(2).getImm();
538 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
540 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000541 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 printOperand(MI, 0);
543 O << ", ";
544 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000545 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 return;
547 }
548 }
549
550 if (printInstruction(MI))
551 return; // Printer was automatically generated
552
553 assert(0 && "Unhandled instruction in asm writer!");
554 abort();
555 return;
556}
557
558/// runOnMachineFunction - This uses the printMachineInstruction()
559/// method to print assembly for each instruction.
560///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000561bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562
563 SetupMachineFunction(MF);
564 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000565
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 // Print out constants referenced by the function
567 EmitConstantPool(MF.getConstantPool());
568
569 // Print out labels for the function.
570 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000571 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000572
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 switch (F->getLinkage()) {
574 default: assert(0 && "Unknown linkage type!");
575 case Function::InternalLinkage: // Symbols default to internal.
576 break;
577 case Function::ExternalLinkage:
578 O << "\t.global\t" << CurrentFnName << '\n'
579 << "\t.type\t" << CurrentFnName << ", @function\n";
580 break;
581 case Function::WeakLinkage:
582 case Function::LinkOnceLinkage:
583 O << "\t.global\t" << CurrentFnName << '\n';
584 O << "\t.weak\t" << CurrentFnName << '\n';
585 break;
586 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000587
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000588 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000589
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 EmitAlignment(2, F);
591 O << CurrentFnName << ":\n";
592
593 // Emit pre-function debug information.
594 DW.BeginFunction(&MF);
595
596 // Print out code for the function.
597 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
598 I != E; ++I) {
599 // Print a label for the basic block.
600 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000601 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 O << '\n';
603 }
604 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
605 II != E; ++II) {
606 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 printMachineInstruction(II);
608 }
609 }
610
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000611 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612
613 // Print out jump tables referenced by the function.
614 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000615
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000616 SwitchToSection(TAI->SectionForGlobal(F));
617
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 // Emit post-function debug information.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +0000619 DW.EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000620
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000621 O.flush();
622
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 // We didn't modify anything.
624 return false;
625}
626
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000627bool PPCLinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000628 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000629
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000630 // Emit initial debug information.
631 DW.BeginModule(&M);
632
633 // AsmPrinter::doInitialization should have done this analysis.
634 MMI = getAnalysisToUpdate<MachineModuleInfo>();
635 assert(MMI);
636 DW.SetModuleInfo(MMI);
637
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 // GNU as handles section names wrapped in quotes
639 Mang->setUseQuotes(true);
640
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000641 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000642
Dan Gohman4a558a32007-07-25 19:33:14 +0000643 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644}
645
Chris Lattner2b638a72008-02-15 19:04:54 +0000646/// PrintUnmangledNameSafely - Print out the printable characters in the name.
647/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +0000648static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Chris Lattner2b638a72008-02-15 19:04:54 +0000649 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
650 Name != E; ++Name)
651 if (isprint(*Name))
652 OS << *Name;
653}
654
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000655void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 const TargetData *TD = TM.getTargetData();
657
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000658 if (!GVar->hasInitializer())
659 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000660
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000661 // Check to see if this is a special global used by LLVM, if so, emit it.
662 if (EmitSpecialLLVMGlobal(GVar))
663 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000665 std::string name = Mang->getValueName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000667 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000668
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000669 Constant *C = GVar->getInitializer();
670 const Type *Type = C->getType();
671 unsigned Size = TD->getABITypeSize(Type);
672 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000674 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000675
676 if (C->isNullValue() && /* FIXME: Verify correct */
677 !GVar->hasSection() &&
678 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000679 GVar->mayBeOverridden())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000681
682 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 O << "\t.global " << name << '\n';
684 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000685 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000686 O << "\t.zero " << Size << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000687 } else if (GVar->hasInternalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000688 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000690 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 }
Chris Lattner2b638a72008-02-15 19:04:54 +0000692 O << "\t\t" << TAI->getCommentString() << " '";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000693 PrintUnmangledNameSafely(GVar, O);
Chris Lattner2b638a72008-02-15 19:04:54 +0000694 O << "'\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000695 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 }
697
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000698 switch (GVar->getLinkage()) {
699 case GlobalValue::LinkOnceLinkage:
700 case GlobalValue::WeakLinkage:
701 case GlobalValue::CommonLinkage:
702 O << "\t.global " << name << '\n'
703 << "\t.type " << name << ", @object\n"
704 << "\t.weak " << name << '\n';
705 break;
706 case GlobalValue::AppendingLinkage:
707 // FIXME: appending linkage variables should go into a section of
708 // their name or something. For now, just emit them as external.
709 case GlobalValue::ExternalLinkage:
710 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000711 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000712 << "\t.type " << name << ", @object\n";
713 // FALL THROUGH
714 case GlobalValue::InternalLinkage:
715 break;
716 default:
717 cerr << "Unknown linkage type!";
718 abort();
719 }
720
721 EmitAlignment(Align, GVar);
722 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
723 PrintUnmangledNameSafely(GVar, O);
724 O << "'\n";
725
726 // If the initializer is a extern weak symbol, remember to emit the weak
727 // reference!
728 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
729 if (GV->hasExternalWeakLinkage())
730 ExtWeakSymbols.insert(GV);
731
732 EmitGlobalConstant(C);
733 O << '\n';
734}
735
736bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
737 // Print out module-level global variables here.
738 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
739 I != E; ++I)
740 printModuleLevelGV(I);
741
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 // TODO
743
744 // Emit initial debug information.
745 DW.EndModule();
746
Dan Gohman4a558a32007-07-25 19:33:14 +0000747 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748}
749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750/// runOnMachineFunction - This uses the printMachineInstruction()
751/// method to print assembly for each instruction.
752///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000753bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 SetupMachineFunction(MF);
755 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 // Print out constants referenced by the function
758 EmitConstantPool(MF.getConstantPool());
759
760 // Print out labels for the function.
761 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000762 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 switch (F->getLinkage()) {
765 default: assert(0 && "Unknown linkage type!");
766 case Function::InternalLinkage: // Symbols default to internal.
767 break;
768 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000769 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 break;
771 case Function::WeakLinkage:
772 case Function::LinkOnceLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000773 O << "\t.globl\t" << CurrentFnName << '\n';
774 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 break;
776 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000777
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000778 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000779
Devang Patel93698d92008-10-01 23:18:38 +0000780 EmitAlignment(F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 O << CurrentFnName << ":\n";
782
783 // Emit pre-function debug information.
784 DW.BeginFunction(&MF);
785
Bill Wendling36ccaea2008-01-26 06:51:24 +0000786 // If the function is empty, then we need to emit *something*. Otherwise, the
787 // function's label might be associated with something that it wasn't meant to
788 // be associated with. We emit a noop in this situation.
789 MachineFunction::iterator I = MF.begin();
790
Bill Wendlingb5880a72008-01-26 09:03:52 +0000791 if (++I == MF.end() && MF.front().empty())
792 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000793
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 // Print out code for the function.
795 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
796 I != E; ++I) {
797 // Print a label for the basic block.
798 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000799 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 O << '\n';
801 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000802 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
803 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 printMachineInstruction(II);
806 }
807 }
808
809 // Print out jump tables referenced by the function.
810 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000811
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 // Emit post-function debug information.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +0000813 DW.EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 // We didn't modify anything.
816 return false;
817}
818
819
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000820bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000821 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000822 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 "ppc",
824 "ppc601",
825 "ppc602",
826 "ppc603",
827 "ppc7400",
828 "ppc750",
829 "ppc970",
830 "ppc64"
831 };
832
833 unsigned Directive = Subtarget.getDarwinDirective();
834 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
835 Directive = PPC::DIR_970;
836 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
837 Directive = PPC::DIR_7400;
838 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
839 Directive = PPC::DIR_64;
840 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000841 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000842
Dan Gohman4a558a32007-07-25 19:33:14 +0000843 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000844
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000845 // Emit initial debug information.
846 DW.BeginModule(&M);
847
848 // We need this for Personality functions.
849 // AsmPrinter::doInitialization should have done this analysis.
850 MMI = getAnalysisToUpdate<MachineModuleInfo>();
851 assert(MMI);
852 DW.SetModuleInfo(MMI);
853
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 // Darwin wants symbols to be quoted if they have complex names.
855 Mang->setUseQuotes(true);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 // Prime text sections so they are adjacent. This reduces the likelihood a
858 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000859 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 "pure_instructions");
861 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000862 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 "pure_instructions,32");
864 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000865 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 "pure_instructions,16");
867 }
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000868 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000869
Dan Gohman4a558a32007-07-25 19:33:14 +0000870 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871}
872
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000873void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
874 const TargetData *TD = TM.getTargetData();
875
876 if (!GVar->hasInitializer())
877 return; // External global require no code
878
879 // Check to see if this is a special global used by LLVM, if so, emit it.
880 if (EmitSpecialLLVMGlobal(GVar)) {
881 if (TM.getRelocationModel() == Reloc::Static) {
882 if (GVar->getName() == "llvm.global_ctors")
883 O << ".reference .constructors_used\n";
884 else if (GVar->getName() == "llvm.global_dtors")
885 O << ".reference .destructors_used\n";
886 }
887 return;
888 }
889
890 std::string name = Mang->getValueName(GVar);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000891
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000892 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000893
894 Constant *C = GVar->getInitializer();
895 const Type *Type = C->getType();
896 unsigned Size = TD->getABITypeSize(Type);
897 unsigned Align = TD->getPreferredAlignmentLog(GVar);
898
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000899 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000900
901 if (C->isNullValue() && /* FIXME: Verify correct */
902 !GVar->hasSection() &&
903 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000904 GVar->mayBeOverridden())) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000905 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
906
907 if (GVar->hasExternalLinkage()) {
908 O << "\t.globl " << name << '\n';
909 O << "\t.zerofill __DATA, __common, " << name << ", "
910 << Size << ", " << Align;
911 } else if (GVar->hasInternalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000912 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000913 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000914 O << "\t.globl " << name << '\n'
915 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000916 EmitAlignment(Align, GVar);
917 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
918 PrintUnmangledNameSafely(GVar, O);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000919 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000920 EmitGlobalConstant(C);
921 return;
922 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000923 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000924 // Darwin 9 and above support aligned common data.
925 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000926 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000927 }
928 O << "\t\t" << TAI->getCommentString() << " '";
929 PrintUnmangledNameSafely(GVar, O);
930 O << "'\n";
931 return;
932 }
933
934 switch (GVar->getLinkage()) {
935 case GlobalValue::LinkOnceLinkage:
936 case GlobalValue::WeakLinkage:
937 case GlobalValue::CommonLinkage:
938 O << "\t.globl " << name << '\n'
939 << "\t.weak_definition " << name << '\n';
940 break;
941 case GlobalValue::AppendingLinkage:
942 // FIXME: appending linkage variables should go into a section of
943 // their name or something. For now, just emit them as external.
944 case GlobalValue::ExternalLinkage:
945 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000946 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000947 // FALL THROUGH
948 case GlobalValue::InternalLinkage:
949 break;
950 default:
951 cerr << "Unknown linkage type!";
952 abort();
953 }
954
955 EmitAlignment(Align, GVar);
956 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
957 PrintUnmangledNameSafely(GVar, O);
958 O << "'\n";
959
960 // If the initializer is a extern weak symbol, remember to emit the weak
961 // reference!
962 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
963 if (GV->hasExternalWeakLinkage())
964 ExtWeakSymbols.insert(GV);
965
966 EmitGlobalConstant(C);
967 O << '\n';
968}
969
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000970bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 const TargetData *TD = TM.getTargetData();
972
973 // Print out module-level global variables here.
974 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000975 I != E; ++I)
976 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977
978 bool isPPC64 = TD->getPointerSizeInBits() == 64;
979
980 // Output stubs for dynamically-linked functions
981 if (TM.getRelocationModel() == Reloc::PIC_) {
982 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
983 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000984 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 "pure_instructions,32");
986 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +0000987 std::string p = *i;
988 std::string L0p = (p[0]=='\"') ? "\"L0$" + p.substr(1) : "L0$" + p ;
989 printSuffixedName(p, "$stub");
990 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000991 O << "\t.indirect_symbol " << *i << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 O << "\tmflr r0\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000993 O << "\tbcl 20,31," << L0p << '\n';
Dale Johannesena21b5202008-05-19 21:38:18 +0000994 O << L0p << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 O << "\tmflr r11\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000996 O << "\taddis r11,r11,ha16(";
997 printSuffixedName(p, "$lazy_ptr");
998 O << "-" << L0p << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 O << "\tmtlr r0\n";
1000 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001001 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001003 O << "\tlwzu r12,lo16(";
1004 printSuffixedName(p, "$lazy_ptr");
1005 O << "-" << L0p << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 O << "\tmtctr r12\n";
1007 O << "\tbctr\n";
1008 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001009 printSuffixedName(p, "$lazy_ptr");
1010 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001011 O << "\t.indirect_symbol " << *i << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 if (isPPC64)
1013 O << "\t.quad dyld_stub_binding_helper\n";
1014 else
1015 O << "\t.long dyld_stub_binding_helper\n";
1016 }
1017 } else {
1018 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1019 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001020 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 "pure_instructions,16");
1022 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +00001023 std::string p = *i;
1024 printSuffixedName(p, "$stub");
1025 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001026 O << "\t.indirect_symbol " << *i << '\n';
Dale Johannesena21b5202008-05-19 21:38:18 +00001027 O << "\tlis r11,ha16(";
1028 printSuffixedName(p, "$lazy_ptr");
1029 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001031 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001033 O << "\tlwzu r12,lo16(";
1034 printSuffixedName(p, "$lazy_ptr");
1035 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 O << "\tmtctr r12\n";
1037 O << "\tbctr\n";
1038 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001039 printSuffixedName(p, "$lazy_ptr");
1040 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001041 O << "\t.indirect_symbol " << *i << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 if (isPPC64)
1043 O << "\t.quad dyld_stub_binding_helper\n";
1044 else
1045 O << "\t.long dyld_stub_binding_helper\n";
1046 }
1047 }
1048
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001049 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050
Dale Johannesen85535762008-04-02 00:25:04 +00001051 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001052 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001053 // Only referenced functions get into the Personalities list.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001054 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1055
1056 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1057 E = Personalities.end(); I != E; ++I)
1058 if (*I) GVStubs.insert("_" + (*I)->getName());
1059 }
1060
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001062 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 SwitchToDataSection(".non_lazy_symbol_pointer");
1064 for (std::set<std::string>::iterator I = GVStubs.begin(),
1065 E = GVStubs.end(); I != E; ++I) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001066 std::string p = *I;
1067 printSuffixedName(p, "$non_lazy_ptr");
1068 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001069 O << "\t.indirect_symbol " << *I << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070 if (isPPC64)
1071 O << "\t.quad\t0\n";
1072 else
1073 O << "\t.long\t0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 }
1075 }
1076
1077 // Emit initial debug information.
1078 DW.EndModule();
1079
1080 // Funny Darwin hack: This flag tells the linker that no global symbols
1081 // contain code that falls through to other global symbols (e.g. the obvious
1082 // implementation of multiple entry points). If this doesn't occur, the
1083 // linker can safely perform dead code stripping. Since LLVM never generates
1084 // code that does this, it is always safe to set.
1085 O << "\t.subsections_via_symbols\n";
1086
Dan Gohman4a558a32007-07-25 19:33:14 +00001087 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088}
1089
1090
1091
1092/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1093/// for a MachineFunction to the given output stream, in a format that the
1094/// Darwin assembler can deal with.
1095///
Owen Anderson847b99b2008-08-21 00:14:44 +00001096FunctionPass *llvm::createPPCAsmPrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 PPCTargetMachine &tm) {
1098 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1099
1100 if (Subtarget->isDarwin()) {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001101 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102 } else {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001103 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 }
1105}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001106
1107namespace {
1108 static struct Register {
1109 Register() {
1110 PPCTargetMachine::registerAsmPrinter(createPPCAsmPrinterPass);
1111 }
1112 } Registrator;
1113}
Oscar Fuentes4f012352008-11-15 21:36:30 +00001114
1115extern "C" int PowerPCAsmPrinterForceLink;
1116int PowerPCAsmPrinterForceLink = 0;