blob: 605e6319d037927eefdde3a52ff62a5477f805fe [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"
Evan Chenga65854f2008-12-05 01:06:39 +000046#include "llvm/ADT/StringSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047using namespace llvm;
48
49STATISTIC(EmittedInsts, "Number of machine instrs printed");
50
51namespace {
52 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
Evan Chenga65854f2008-12-05 01:06:39 +000053 StringSet<> FnStubs, GVStubs, HiddenGVStubs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 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 {
Devang Patelaa1e8432009-01-08 23:40:34 +0000295 DwarfWriter *DW;
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000296 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
Owen Anderson847b99b2008-08-21 00:14:44 +0000298 PPCLinuxAsmPrinter(raw_ostream &O, PPCTargetMachine &TM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 const TargetAsmInfo *T)
Devang Patelaa1e8432009-01-08 23:40:34 +0000300 : PPCAsmPrinter(O, TM, T), DW(0), MMI(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 }
302
303 virtual const char *getPassName() const {
304 return "Linux PPC Assembly Printer";
305 }
306
307 bool runOnMachineFunction(MachineFunction &F);
308 bool doInitialization(Module &M);
309 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 void getAnalysisUsage(AnalysisUsage &AU) const {
312 AU.setPreservesAll();
313 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000314 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 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
Devang Patelaa1e8432009-01-08 23:40:34 +0000325 DwarfWriter *DW;
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000326 MachineModuleInfo *MMI;
Devang Patelaa1e8432009-01-08 23:40:34 +0000327 raw_ostream &OS;
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)
Devang Patelaa1e8432009-01-08 23:40:34 +0000330 : PPCAsmPrinter(O, TM, T), DW(0), MMI(0), OS(O) {
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>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000344 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 PPCAsmPrinter::getAnalysisUsage(AU);
346 }
347
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000348 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 };
350} // end of anonymous namespace
351
352// Include the auto-generated portion of the assembly writer
353#include "PPCGenAsmWriter.inc"
354
355void PPCAsmPrinter::printOp(const MachineOperand &MO) {
356 switch (MO.getType()) {
357 case MachineOperand::MO_Immediate:
358 cerr << "printOp() does not handle immediate values\n";
359 abort();
360 return;
361
362 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000363 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 return;
365 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000366 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000367 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 // FIXME: PIC relocation model
369 return;
370 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000371 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000372 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 return;
374 case MachineOperand::MO_ExternalSymbol:
375 // Computing the address of an external symbol, not calling it.
376 if (TM.getRelocationModel() != Reloc::Static) {
377 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
378 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000379 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 return;
381 }
382 O << TAI->getGlobalPrefix() << MO.getSymbolName();
383 return;
384 case MachineOperand::MO_GlobalAddress: {
385 // Computing the address of a global symbol, not calling it.
386 GlobalValue *GV = MO.getGlobal();
387 std::string Name = Mang->getValueName(GV);
388
389 // External or weakly linked global variables need non-lazily-resolved stubs
390 if (TM.getRelocationModel() != Reloc::Static) {
Evan Chenga65854f2008-12-05 01:06:39 +0000391 if (GV->isDeclaration() || GV->mayBeOverridden()) {
392 if (GV->hasHiddenVisibility()) {
393 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
394 O << Name;
395 else {
396 HiddenGVStubs.insert(Name);
397 printSuffixedName(Name, "$non_lazy_ptr");
398 }
399 } else {
400 GVStubs.insert(Name);
401 printSuffixedName(Name, "$non_lazy_ptr");
402 }
Dale Johannesencaf11182008-05-16 20:09:25 +0000403 if (GV->hasExternalWeakLinkage())
404 ExtWeakSymbols.insert(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 return;
406 }
407 }
408 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000409
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000410 printOffset(MO.getOffset());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000411
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 if (GV->hasExternalWeakLinkage())
413 ExtWeakSymbols.insert(GV);
414 return;
415 }
416
417 default:
418 O << "<unknown operand type: " << MO.getType() << ">";
419 return;
420 }
421}
422
423/// EmitExternalGlobal - In this case we need to use the indirect symbol.
424///
425void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
426 std::string Name = getGlobalLinkName(GV);
427 if (TM.getRelocationModel() != Reloc::Static) {
Evan Chenga65854f2008-12-05 01:06:39 +0000428 if (GV->hasHiddenVisibility())
429 HiddenGVStubs.insert(Name);
430 else
431 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000432 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 return;
434 }
435 O << Name;
436}
437
438/// PrintAsmOperand - Print out an operand for an inline asm expression.
439///
440bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000441 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 const char *ExtraCode) {
443 // Does this asm operand have a single letter operand modifier?
444 if (ExtraCode && ExtraCode[0]) {
445 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000446
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 switch (ExtraCode[0]) {
448 default: return true; // Unknown modifier.
449 case 'c': // Don't print "$" before a global var name or constant.
450 // PPC never has a prefix.
451 printOperand(MI, OpNo);
452 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000453 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000455 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000457 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 return true;
459 ++OpNo; // Return the high-part.
460 break;
461 case 'I':
462 // Write 'i' if an integer constant, otherwise nothing. Used to print
463 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000464 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 O << "i";
466 return false;
467 }
468 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 printOperand(MI, OpNo);
471 return false;
472}
473
474bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000475 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 const char *ExtraCode) {
477 if (ExtraCode && ExtraCode[0])
478 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000479 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 printMemRegReg(MI, OpNo);
481 else
482 printMemRegImm(MI, OpNo);
483 return false;
484}
485
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000486void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 const char *Modifier) {
488 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
489 unsigned Code = MI->getOperand(OpNo).getImm();
490 if (!strcmp(Modifier, "cc")) {
491 switch ((PPC::Predicate)Code) {
492 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
493 case PPC::PRED_LT: O << "lt"; return;
494 case PPC::PRED_LE: O << "le"; return;
495 case PPC::PRED_EQ: O << "eq"; return;
496 case PPC::PRED_GE: O << "ge"; return;
497 case PPC::PRED_GT: O << "gt"; return;
498 case PPC::PRED_NE: O << "ne"; return;
499 case PPC::PRED_UN: O << "un"; return;
500 case PPC::PRED_NU: O << "nu"; return;
501 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000502
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 } else {
504 assert(!strcmp(Modifier, "reg") &&
505 "Need to specify 'cc' or 'reg' as predicate op modifier!");
506 // Don't print the register for 'always'.
507 if (Code == PPC::PRED_ALWAYS) return;
508 printOperand(MI, OpNo+1);
509 }
510}
511
512
513/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
514/// the current output stream.
515///
516void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
517 ++EmittedInsts;
518
519 // Check for slwi/srwi mnemonics.
520 if (MI->getOpcode() == PPC::RLWINM) {
521 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000522 unsigned char SH = MI->getOperand(2).getImm();
523 unsigned char MB = MI->getOperand(3).getImm();
524 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000526 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 }
528 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000529 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 SH = 32-SH;
531 }
532 if (FoundMnemonic) {
533 printOperand(MI, 0);
534 O << ", ";
535 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000536 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 return;
538 }
539 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
540 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000541 O << "\tmr ";
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 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 return;
547 }
548 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000549 unsigned char SH = MI->getOperand(2).getImm();
550 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
552 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000553 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 printOperand(MI, 0);
555 O << ", ";
556 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000557 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 return;
559 }
560 }
561
562 if (printInstruction(MI))
563 return; // Printer was automatically generated
564
565 assert(0 && "Unhandled instruction in asm writer!");
566 abort();
567 return;
568}
569
570/// runOnMachineFunction - This uses the printMachineInstruction()
571/// method to print assembly for each instruction.
572///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000573bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574
575 SetupMachineFunction(MF);
576 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000577
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 // Print out constants referenced by the function
579 EmitConstantPool(MF.getConstantPool());
580
581 // Print out labels for the function.
582 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000583 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000584
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 switch (F->getLinkage()) {
586 default: assert(0 && "Unknown linkage type!");
587 case Function::InternalLinkage: // Symbols default to internal.
588 break;
589 case Function::ExternalLinkage:
590 O << "\t.global\t" << CurrentFnName << '\n'
591 << "\t.type\t" << CurrentFnName << ", @function\n";
592 break;
593 case Function::WeakLinkage:
594 case Function::LinkOnceLinkage:
595 O << "\t.global\t" << CurrentFnName << '\n';
596 O << "\t.weak\t" << CurrentFnName << '\n';
597 break;
598 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000599
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000600 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000601
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 EmitAlignment(2, F);
603 O << CurrentFnName << ":\n";
604
605 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000606 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607
608 // Print out code for the function.
609 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
610 I != E; ++I) {
611 // Print a label for the basic block.
612 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000613 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 O << '\n';
615 }
616 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
617 II != E; ++II) {
618 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 printMachineInstruction(II);
620 }
621 }
622
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000623 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624
625 // Print out jump tables referenced by the function.
626 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000627
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000628 SwitchToSection(TAI->SectionForGlobal(F));
629
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000631 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000632
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000633 O.flush();
634
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 // We didn't modify anything.
636 return false;
637}
638
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000639bool PPCLinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000640 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000641
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000642 // Emit initial debug information.
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000643 MMI = getAnalysisToUpdate<MachineModuleInfo>();
644 assert(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +0000645 DW = getAnalysisToUpdate<DwarfWriter>();
646 assert(DW && "DwarfWriter is not available");
647 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000648
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 // GNU as handles section names wrapped in quotes
650 Mang->setUseQuotes(true);
651
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000652 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000653
Dan Gohman4a558a32007-07-25 19:33:14 +0000654 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655}
656
Chris Lattner2b638a72008-02-15 19:04:54 +0000657/// PrintUnmangledNameSafely - Print out the printable characters in the name.
658/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +0000659static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Chris Lattner2b638a72008-02-15 19:04:54 +0000660 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
661 Name != E; ++Name)
662 if (isprint(*Name))
663 OS << *Name;
664}
665
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000666void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 const TargetData *TD = TM.getTargetData();
668
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000669 if (!GVar->hasInitializer())
670 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000671
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000672 // Check to see if this is a special global used by LLVM, if so, emit it.
673 if (EmitSpecialLLVMGlobal(GVar))
674 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000676 std::string name = Mang->getValueName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000678 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000679
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000680 Constant *C = GVar->getInitializer();
681 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000682 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000683 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000685 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000686
687 if (C->isNullValue() && /* FIXME: Verify correct */
688 !GVar->hasSection() &&
689 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000690 GVar->mayBeOverridden())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000692
693 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 O << "\t.global " << name << '\n';
695 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000696 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000697 O << "\t.zero " << Size << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000698 } else if (GVar->hasInternalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000699 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000701 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 }
Chris Lattner2b638a72008-02-15 19:04:54 +0000703 O << "\t\t" << TAI->getCommentString() << " '";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000704 PrintUnmangledNameSafely(GVar, O);
Chris Lattner2b638a72008-02-15 19:04:54 +0000705 O << "'\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000706 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 }
708
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000709 switch (GVar->getLinkage()) {
710 case GlobalValue::LinkOnceLinkage:
711 case GlobalValue::WeakLinkage:
712 case GlobalValue::CommonLinkage:
713 O << "\t.global " << name << '\n'
714 << "\t.type " << name << ", @object\n"
715 << "\t.weak " << name << '\n';
716 break;
717 case GlobalValue::AppendingLinkage:
718 // FIXME: appending linkage variables should go into a section of
719 // their name or something. For now, just emit them as external.
720 case GlobalValue::ExternalLinkage:
721 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000722 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000723 << "\t.type " << name << ", @object\n";
724 // FALL THROUGH
725 case GlobalValue::InternalLinkage:
726 break;
727 default:
728 cerr << "Unknown linkage type!";
729 abort();
730 }
731
732 EmitAlignment(Align, GVar);
733 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
734 PrintUnmangledNameSafely(GVar, O);
735 O << "'\n";
736
737 // If the initializer is a extern weak symbol, remember to emit the weak
738 // reference!
739 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
740 if (GV->hasExternalWeakLinkage())
741 ExtWeakSymbols.insert(GV);
742
743 EmitGlobalConstant(C);
744 O << '\n';
745}
746
747bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
748 // Print out module-level global variables here.
749 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
750 I != E; ++I)
751 printModuleLevelGV(I);
752
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 // TODO
754
755 // Emit initial debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000756 DW->EndModule();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Dan Gohman4a558a32007-07-25 19:33:14 +0000758 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759}
760
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761/// runOnMachineFunction - This uses the printMachineInstruction()
762/// method to print assembly for each instruction.
763///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000764bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 SetupMachineFunction(MF);
766 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000767
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 // Print out constants referenced by the function
769 EmitConstantPool(MF.getConstantPool());
770
771 // Print out labels for the function.
772 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000773 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000774
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 switch (F->getLinkage()) {
776 default: assert(0 && "Unknown linkage type!");
777 case Function::InternalLinkage: // Symbols default to internal.
778 break;
779 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000780 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 break;
782 case Function::WeakLinkage:
783 case Function::LinkOnceLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000784 O << "\t.globl\t" << CurrentFnName << '\n';
785 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 break;
787 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000788
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000789 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000790
Devang Patel93698d92008-10-01 23:18:38 +0000791 EmitAlignment(F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 O << CurrentFnName << ":\n";
793
794 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000795 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796
Bill Wendling36ccaea2008-01-26 06:51:24 +0000797 // If the function is empty, then we need to emit *something*. Otherwise, the
798 // function's label might be associated with something that it wasn't meant to
799 // be associated with. We emit a noop in this situation.
800 MachineFunction::iterator I = MF.begin();
801
Bill Wendlingb5880a72008-01-26 09:03:52 +0000802 if (++I == MF.end() && MF.front().empty())
803 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 // Print out code for the function.
806 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
807 I != E; ++I) {
808 // Print a label for the basic block.
809 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000810 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 O << '\n';
812 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000813 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
814 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 printMachineInstruction(II);
817 }
818 }
819
820 // Print out jump tables referenced by the function.
821 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000822
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000824 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000825
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 // We didn't modify anything.
827 return false;
828}
829
830
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000831bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000832 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000833 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 "ppc",
835 "ppc601",
836 "ppc602",
837 "ppc603",
838 "ppc7400",
839 "ppc750",
840 "ppc970",
841 "ppc64"
842 };
843
844 unsigned Directive = Subtarget.getDarwinDirective();
845 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
846 Directive = PPC::DIR_970;
847 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
848 Directive = PPC::DIR_7400;
849 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
850 Directive = PPC::DIR_64;
851 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000852 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000853
Dan Gohman4a558a32007-07-25 19:33:14 +0000854 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000855
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000856 // Emit initial debug information.
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000857 // We need this for Personality functions.
858 // AsmPrinter::doInitialization should have done this analysis.
859 MMI = getAnalysisToUpdate<MachineModuleInfo>();
860 assert(MMI);
Devang Patelaa1e8432009-01-08 23:40:34 +0000861 DW = getAnalysisToUpdate<DwarfWriter>();
862 assert(DW && "DwarfWriter is not available");
863 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000864
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 // Darwin wants symbols to be quoted if they have complex names.
866 Mang->setUseQuotes(true);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000867
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868 // Prime text sections so they are adjacent. This reduces the likelihood a
869 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000870 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 "pure_instructions");
872 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000873 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 "pure_instructions,32");
875 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000876 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 "pure_instructions,16");
878 }
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000879 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000880
Dan Gohman4a558a32007-07-25 19:33:14 +0000881 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882}
883
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000884void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
885 const TargetData *TD = TM.getTargetData();
886
887 if (!GVar->hasInitializer())
888 return; // External global require no code
889
890 // Check to see if this is a special global used by LLVM, if so, emit it.
891 if (EmitSpecialLLVMGlobal(GVar)) {
892 if (TM.getRelocationModel() == Reloc::Static) {
893 if (GVar->getName() == "llvm.global_ctors")
894 O << ".reference .constructors_used\n";
895 else if (GVar->getName() == "llvm.global_dtors")
896 O << ".reference .destructors_used\n";
897 }
898 return;
899 }
900
901 std::string name = Mang->getValueName(GVar);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000902
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000903 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000904
905 Constant *C = GVar->getInitializer();
906 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000907 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000908 unsigned Align = TD->getPreferredAlignmentLog(GVar);
909
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000910 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000911
912 if (C->isNullValue() && /* FIXME: Verify correct */
913 !GVar->hasSection() &&
914 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000915 GVar->mayBeOverridden())) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000916 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
917
918 if (GVar->hasExternalLinkage()) {
919 O << "\t.globl " << name << '\n';
920 O << "\t.zerofill __DATA, __common, " << name << ", "
921 << Size << ", " << Align;
922 } else if (GVar->hasInternalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000923 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000924 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000925 O << "\t.globl " << name << '\n'
926 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000927 EmitAlignment(Align, GVar);
928 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
929 PrintUnmangledNameSafely(GVar, O);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000930 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000931 EmitGlobalConstant(C);
932 return;
933 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000934 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000935 // Darwin 9 and above support aligned common data.
936 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000937 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000938 }
939 O << "\t\t" << TAI->getCommentString() << " '";
940 PrintUnmangledNameSafely(GVar, O);
941 O << "'\n";
942 return;
943 }
944
945 switch (GVar->getLinkage()) {
946 case GlobalValue::LinkOnceLinkage:
947 case GlobalValue::WeakLinkage:
948 case GlobalValue::CommonLinkage:
949 O << "\t.globl " << name << '\n'
950 << "\t.weak_definition " << name << '\n';
951 break;
952 case GlobalValue::AppendingLinkage:
953 // FIXME: appending linkage variables should go into a section of
954 // their name or something. For now, just emit them as external.
955 case GlobalValue::ExternalLinkage:
956 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000957 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000958 // FALL THROUGH
959 case GlobalValue::InternalLinkage:
960 break;
961 default:
962 cerr << "Unknown linkage type!";
963 abort();
964 }
965
966 EmitAlignment(Align, GVar);
967 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
968 PrintUnmangledNameSafely(GVar, O);
969 O << "'\n";
970
971 // If the initializer is a extern weak symbol, remember to emit the weak
972 // reference!
973 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
974 if (GV->hasExternalWeakLinkage())
975 ExtWeakSymbols.insert(GV);
976
977 EmitGlobalConstant(C);
978 O << '\n';
979}
980
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000981bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 const TargetData *TD = TM.getTargetData();
983
984 // Print out module-level global variables here.
985 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000986 I != E; ++I)
987 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988
989 bool isPPC64 = TD->getPointerSizeInBits() == 64;
990
991 // Output stubs for dynamically-linked functions
992 if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Chenga65854f2008-12-05 01:06:39 +0000993 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000995 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 "pure_instructions,32");
997 EmitAlignment(4);
Evan Chenga65854f2008-12-05 01:06:39 +0000998 const char *p = i->getKeyData();
999 bool hasQuote = p[0]=='\"';
Dale Johannesena21b5202008-05-19 21:38:18 +00001000 printSuffixedName(p, "$stub");
1001 O << ":\n";
Evan Chenga65854f2008-12-05 01:06:39 +00001002 O << "\t.indirect_symbol " << p << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 O << "\tmflr r0\n";
Evan Chenga65854f2008-12-05 01:06:39 +00001004 O << "\tbcl 20,31,";
1005 if (hasQuote)
1006 O << "\"L0$" << &p[1];
1007 else
1008 O << "L0$" << p;
1009 O << '\n';
1010 if (hasQuote)
1011 O << "\"L0$" << &p[1];
1012 else
1013 O << "L0$" << p;
1014 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 O << "\tmflr r11\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001016 O << "\taddis r11,r11,ha16(";
1017 printSuffixedName(p, "$lazy_ptr");
Evan Chenga65854f2008-12-05 01:06:39 +00001018 O << "-";
1019 if (hasQuote)
1020 O << "\"L0$" << &p[1];
1021 else
1022 O << "L0$" << p;
1023 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024 O << "\tmtlr r0\n";
1025 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001026 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001028 O << "\tlwzu r12,lo16(";
1029 printSuffixedName(p, "$lazy_ptr");
Evan Chenga65854f2008-12-05 01:06:39 +00001030 O << "-";
1031 if (hasQuote)
1032 O << "\"L0$" << &p[1];
1033 else
1034 O << "L0$" << p;
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";
Evan Chenga65854f2008-12-05 01:06:39 +00001041 O << "\t.indirect_symbol " << p << '\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 } else {
Evan Chenga65854f2008-12-05 01:06:39 +00001048 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001050 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 "pure_instructions,16");
1052 EmitAlignment(4);
Evan Chenga65854f2008-12-05 01:06:39 +00001053 const char *p = i->getKeyData();
Dale Johannesena21b5202008-05-19 21:38:18 +00001054 printSuffixedName(p, "$stub");
1055 O << ":\n";
Evan Chenga65854f2008-12-05 01:06:39 +00001056 O << "\t.indirect_symbol " << p << '\n';
Dale Johannesena21b5202008-05-19 21:38:18 +00001057 O << "\tlis r11,ha16(";
1058 printSuffixedName(p, "$lazy_ptr");
1059 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001061 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001063 O << "\tlwzu r12,lo16(";
1064 printSuffixedName(p, "$lazy_ptr");
1065 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 O << "\tmtctr r12\n";
1067 O << "\tbctr\n";
1068 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001069 printSuffixedName(p, "$lazy_ptr");
1070 O << ":\n";
Evan Chenga65854f2008-12-05 01:06:39 +00001071 O << "\t.indirect_symbol " << p << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 if (isPPC64)
1073 O << "\t.quad dyld_stub_binding_helper\n";
1074 else
1075 O << "\t.long dyld_stub_binding_helper\n";
1076 }
1077 }
1078
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001079 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080
Dale Johannesen85535762008-04-02 00:25:04 +00001081 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001082 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001083 // Only referenced functions get into the Personalities list.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001084 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1085
1086 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1087 E = Personalities.end(); I != E; ++I)
1088 if (*I) GVStubs.insert("_" + (*I)->getName());
1089 }
1090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001091 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001092 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 SwitchToDataSection(".non_lazy_symbol_pointer");
Evan Chenga65854f2008-12-05 01:06:39 +00001094 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1095 i != e; ++i) {
1096 std::string p = i->getKeyData();
Dale Johannesena21b5202008-05-19 21:38:18 +00001097 printSuffixedName(p, "$non_lazy_ptr");
1098 O << ":\n";
Evan Chenga65854f2008-12-05 01:06:39 +00001099 O << "\t.indirect_symbol " << p << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 if (isPPC64)
1101 O << "\t.quad\t0\n";
1102 else
1103 O << "\t.long\t0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 }
1105 }
1106
Evan Chenga65854f2008-12-05 01:06:39 +00001107 if (!HiddenGVStubs.empty()) {
1108 SwitchToSection(TAI->getDataSection());
1109 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1110 i != e; ++i) {
1111 std::string p = i->getKeyData();
1112 EmitAlignment(isPPC64 ? 3 : 2);
1113 printSuffixedName(p, "$non_lazy_ptr");
1114 O << ":\n";
1115 if (isPPC64)
1116 O << "\t.quad\t";
1117 else
1118 O << "\t.long\t";
1119 O << p << '\n';
1120 }
1121 }
1122
1123
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 // Emit initial debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +00001125 DW->EndModule();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126
1127 // Funny Darwin hack: This flag tells the linker that no global symbols
1128 // contain code that falls through to other global symbols (e.g. the obvious
1129 // implementation of multiple entry points). If this doesn't occur, the
1130 // linker can safely perform dead code stripping. Since LLVM never generates
1131 // code that does this, it is always safe to set.
1132 O << "\t.subsections_via_symbols\n";
1133
Dan Gohman4a558a32007-07-25 19:33:14 +00001134 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001135}
1136
1137
1138
1139/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1140/// for a MachineFunction to the given output stream, in a format that the
1141/// Darwin assembler can deal with.
1142///
Owen Anderson847b99b2008-08-21 00:14:44 +00001143FunctionPass *llvm::createPPCAsmPrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 PPCTargetMachine &tm) {
1145 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1146
1147 if (Subtarget->isDarwin()) {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001148 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001149 } else {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001150 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001151 }
1152}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001153
1154namespace {
1155 static struct Register {
1156 Register() {
1157 PPCTargetMachine::registerAsmPrinter(createPPCAsmPrinterPass);
1158 }
1159 } Registrator;
1160}
Oscar Fuentes4f012352008-11-15 21:36:30 +00001161
1162extern "C" int PowerPCAsmPrinterForceLink;
1163int PowerPCAsmPrinterForceLink = 0;