blob: d8451bd1b82b1a4b2613d44494e072c6e70480f8 [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);
126 if (MO.isRegister()) {
127 printRegister(MO, false);
128 } else if (MO.isImmediate()) {
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) {
163 if (MI->getOperand(OpNo).isImmediate()) {
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.
177 if (MI->getOperand(OpNo).isImmediate()) {
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) {
217 if (MI->getOperand(OpNo).isImmediate()) {
218 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) {
231 if (MI->getOperand(OpNo).isImmediate()) {
232 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 << '(';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000253 if (MI->getOperand(OpNo+1).isRegister() &&
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) {
261 if (MI->getOperand(OpNo).isImmediate())
262 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 << '(';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000266 if (MI->getOperand(OpNo+1).isRegister() &&
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) {
390 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000391 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 if (MO.getOffset() > 0)
402 O << "+" << MO.getOffset();
403 else if (MO.getOffset() < 0)
404 O << MO.getOffset();
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000405
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 if (GV->hasExternalWeakLinkage())
407 ExtWeakSymbols.insert(GV);
408 return;
409 }
410
411 default:
412 O << "<unknown operand type: " << MO.getType() << ">";
413 return;
414 }
415}
416
417/// EmitExternalGlobal - In this case we need to use the indirect symbol.
418///
419void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
420 std::string Name = getGlobalLinkName(GV);
421 if (TM.getRelocationModel() != Reloc::Static) {
422 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000423 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 return;
425 }
426 O << Name;
427}
428
429/// PrintAsmOperand - Print out an operand for an inline asm expression.
430///
431bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000432 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 const char *ExtraCode) {
434 // Does this asm operand have a single letter operand modifier?
435 if (ExtraCode && ExtraCode[0]) {
436 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000437
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 switch (ExtraCode[0]) {
439 default: return true; // Unknown modifier.
440 case 'c': // Don't print "$" before a global var name or constant.
441 // PPC never has a prefix.
442 printOperand(MI, OpNo);
443 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000444 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 // Verify that this operand has two consecutive registers.
446 if (!MI->getOperand(OpNo).isRegister() ||
447 OpNo+1 == MI->getNumOperands() ||
448 !MI->getOperand(OpNo+1).isRegister())
449 return true;
450 ++OpNo; // Return the high-part.
451 break;
452 case 'I':
453 // Write 'i' if an integer constant, otherwise nothing. Used to print
454 // addi vs add, etc.
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000455 if (MI->getOperand(OpNo).isImmediate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 O << "i";
457 return false;
458 }
459 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000460
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 printOperand(MI, OpNo);
462 return false;
463}
464
465bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000466 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 const char *ExtraCode) {
468 if (ExtraCode && ExtraCode[0])
469 return true; // Unknown modifier.
470 if (MI->getOperand(OpNo).isRegister())
471 printMemRegReg(MI, OpNo);
472 else
473 printMemRegImm(MI, OpNo);
474 return false;
475}
476
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000477void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 const char *Modifier) {
479 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
480 unsigned Code = MI->getOperand(OpNo).getImm();
481 if (!strcmp(Modifier, "cc")) {
482 switch ((PPC::Predicate)Code) {
483 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
484 case PPC::PRED_LT: O << "lt"; return;
485 case PPC::PRED_LE: O << "le"; return;
486 case PPC::PRED_EQ: O << "eq"; return;
487 case PPC::PRED_GE: O << "ge"; return;
488 case PPC::PRED_GT: O << "gt"; return;
489 case PPC::PRED_NE: O << "ne"; return;
490 case PPC::PRED_UN: O << "un"; return;
491 case PPC::PRED_NU: O << "nu"; return;
492 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000493
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 } else {
495 assert(!strcmp(Modifier, "reg") &&
496 "Need to specify 'cc' or 'reg' as predicate op modifier!");
497 // Don't print the register for 'always'.
498 if (Code == PPC::PRED_ALWAYS) return;
499 printOperand(MI, OpNo+1);
500 }
501}
502
503
504/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
505/// the current output stream.
506///
507void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
508 ++EmittedInsts;
509
510 // Check for slwi/srwi mnemonics.
511 if (MI->getOpcode() == PPC::RLWINM) {
512 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000513 unsigned char SH = MI->getOperand(2).getImm();
514 unsigned char MB = MI->getOperand(3).getImm();
515 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000517 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 }
519 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000520 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 SH = 32-SH;
522 }
523 if (FoundMnemonic) {
524 printOperand(MI, 0);
525 O << ", ";
526 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000527 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 return;
529 }
530 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
531 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000532 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 printOperand(MI, 0);
534 O << ", ";
535 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000536 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 return;
538 }
539 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000540 unsigned char SH = MI->getOperand(2).getImm();
541 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
543 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000544 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 printOperand(MI, 0);
546 O << ", ";
547 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000548 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 return;
550 }
551 }
552
553 if (printInstruction(MI))
554 return; // Printer was automatically generated
555
556 assert(0 && "Unhandled instruction in asm writer!");
557 abort();
558 return;
559}
560
561/// runOnMachineFunction - This uses the printMachineInstruction()
562/// method to print assembly for each instruction.
563///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000564bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565
566 SetupMachineFunction(MF);
567 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000568
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 // Print out constants referenced by the function
570 EmitConstantPool(MF.getConstantPool());
571
572 // Print out labels for the function.
573 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000574 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000575
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 switch (F->getLinkage()) {
577 default: assert(0 && "Unknown linkage type!");
578 case Function::InternalLinkage: // Symbols default to internal.
579 break;
580 case Function::ExternalLinkage:
581 O << "\t.global\t" << CurrentFnName << '\n'
582 << "\t.type\t" << CurrentFnName << ", @function\n";
583 break;
584 case Function::WeakLinkage:
585 case Function::LinkOnceLinkage:
586 O << "\t.global\t" << CurrentFnName << '\n';
587 O << "\t.weak\t" << CurrentFnName << '\n';
588 break;
589 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000590
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000591 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000592
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 EmitAlignment(2, F);
594 O << CurrentFnName << ":\n";
595
596 // Emit pre-function debug information.
597 DW.BeginFunction(&MF);
598
599 // Print out code for the function.
600 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
601 I != E; ++I) {
602 // Print a label for the basic block.
603 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000604 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 O << '\n';
606 }
607 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
608 II != E; ++II) {
609 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 printMachineInstruction(II);
611 }
612 }
613
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000614 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615
616 // Print out jump tables referenced by the function.
617 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000618
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 // Emit post-function debug information.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +0000620 DW.EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000621
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 // We didn't modify anything.
623 return false;
624}
625
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000626bool PPCLinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000627 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000628
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000629 // Emit initial debug information.
630 DW.BeginModule(&M);
631
632 // AsmPrinter::doInitialization should have done this analysis.
633 MMI = getAnalysisToUpdate<MachineModuleInfo>();
634 assert(MMI);
635 DW.SetModuleInfo(MMI);
636
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 // GNU as handles section names wrapped in quotes
638 Mang->setUseQuotes(true);
639
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000640 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000641
Dan Gohman4a558a32007-07-25 19:33:14 +0000642 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643}
644
Chris Lattner2b638a72008-02-15 19:04:54 +0000645/// PrintUnmangledNameSafely - Print out the printable characters in the name.
646/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +0000647static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Chris Lattner2b638a72008-02-15 19:04:54 +0000648 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
649 Name != E; ++Name)
650 if (isprint(*Name))
651 OS << *Name;
652}
653
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000654void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 const TargetData *TD = TM.getTargetData();
656
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000657 if (!GVar->hasInitializer())
658 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000659
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000660 // Check to see if this is a special global used by LLVM, if so, emit it.
661 if (EmitSpecialLLVMGlobal(GVar))
662 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000664 std::string name = Mang->getValueName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000666 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000667
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000668 Constant *C = GVar->getInitializer();
669 const Type *Type = C->getType();
670 unsigned Size = TD->getABITypeSize(Type);
671 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000673 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000674
675 if (C->isNullValue() && /* FIXME: Verify correct */
676 !GVar->hasSection() &&
677 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000678 GVar->mayBeOverridden())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000680
681 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 O << "\t.global " << name << '\n';
683 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000684 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000685 O << "\t.zero " << Size << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000686 } else if (GVar->hasInternalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000687 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000689 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 }
Chris Lattner2b638a72008-02-15 19:04:54 +0000691 O << "\t\t" << TAI->getCommentString() << " '";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000692 PrintUnmangledNameSafely(GVar, O);
Chris Lattner2b638a72008-02-15 19:04:54 +0000693 O << "'\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000694 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 }
696
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000697 switch (GVar->getLinkage()) {
698 case GlobalValue::LinkOnceLinkage:
699 case GlobalValue::WeakLinkage:
700 case GlobalValue::CommonLinkage:
701 O << "\t.global " << name << '\n'
702 << "\t.type " << name << ", @object\n"
703 << "\t.weak " << name << '\n';
704 break;
705 case GlobalValue::AppendingLinkage:
706 // FIXME: appending linkage variables should go into a section of
707 // their name or something. For now, just emit them as external.
708 case GlobalValue::ExternalLinkage:
709 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000710 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000711 << "\t.type " << name << ", @object\n";
712 // FALL THROUGH
713 case GlobalValue::InternalLinkage:
714 break;
715 default:
716 cerr << "Unknown linkage type!";
717 abort();
718 }
719
720 EmitAlignment(Align, GVar);
721 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
722 PrintUnmangledNameSafely(GVar, O);
723 O << "'\n";
724
725 // If the initializer is a extern weak symbol, remember to emit the weak
726 // reference!
727 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
728 if (GV->hasExternalWeakLinkage())
729 ExtWeakSymbols.insert(GV);
730
731 EmitGlobalConstant(C);
732 O << '\n';
733}
734
735bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
736 // Print out module-level global variables here.
737 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
738 I != E; ++I)
739 printModuleLevelGV(I);
740
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 // TODO
742
743 // Emit initial debug information.
744 DW.EndModule();
745
Dan Gohman4a558a32007-07-25 19:33:14 +0000746 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747}
748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749/// runOnMachineFunction - This uses the printMachineInstruction()
750/// method to print assembly for each instruction.
751///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000752bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 SetupMachineFunction(MF);
754 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 // Print out constants referenced by the function
757 EmitConstantPool(MF.getConstantPool());
758
759 // Print out labels for the function.
760 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000761 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000762
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 switch (F->getLinkage()) {
764 default: assert(0 && "Unknown linkage type!");
765 case Function::InternalLinkage: // Symbols default to internal.
766 break;
767 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000768 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 break;
770 case Function::WeakLinkage:
771 case Function::LinkOnceLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000772 O << "\t.globl\t" << CurrentFnName << '\n';
773 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 break;
775 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000776
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000777 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000778
Devang Patel93698d92008-10-01 23:18:38 +0000779 EmitAlignment(F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 O << CurrentFnName << ":\n";
781
782 // Emit pre-function debug information.
783 DW.BeginFunction(&MF);
784
Bill Wendling36ccaea2008-01-26 06:51:24 +0000785 // If the function is empty, then we need to emit *something*. Otherwise, the
786 // function's label might be associated with something that it wasn't meant to
787 // be associated with. We emit a noop in this situation.
788 MachineFunction::iterator I = MF.begin();
789
Bill Wendlingb5880a72008-01-26 09:03:52 +0000790 if (++I == MF.end() && MF.front().empty())
791 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000792
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793 // Print out code for the function.
794 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
795 I != E; ++I) {
796 // Print a label for the basic block.
797 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000798 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 O << '\n';
800 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000801 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
802 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 printMachineInstruction(II);
805 }
806 }
807
808 // Print out jump tables referenced by the function.
809 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 // Emit post-function debug information.
Bill Wendlingb22ae7d2008-09-26 00:28:12 +0000812 DW.EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000813
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 // We didn't modify anything.
815 return false;
816}
817
818
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000819bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000820 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000821 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 "ppc",
823 "ppc601",
824 "ppc602",
825 "ppc603",
826 "ppc7400",
827 "ppc750",
828 "ppc970",
829 "ppc64"
830 };
831
832 unsigned Directive = Subtarget.getDarwinDirective();
833 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
834 Directive = PPC::DIR_970;
835 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
836 Directive = PPC::DIR_7400;
837 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
838 Directive = PPC::DIR_64;
839 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000840 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000841
Dan Gohman4a558a32007-07-25 19:33:14 +0000842 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000843
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000844 // Emit initial debug information.
845 DW.BeginModule(&M);
846
847 // We need this for Personality functions.
848 // AsmPrinter::doInitialization should have done this analysis.
849 MMI = getAnalysisToUpdate<MachineModuleInfo>();
850 assert(MMI);
851 DW.SetModuleInfo(MMI);
852
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 // Darwin wants symbols to be quoted if they have complex names.
854 Mang->setUseQuotes(true);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 // Prime text sections so they are adjacent. This reduces the likelihood a
857 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000858 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 "pure_instructions");
860 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000861 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 "pure_instructions,32");
863 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000864 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 "pure_instructions,16");
866 }
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000867 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000868
Dan Gohman4a558a32007-07-25 19:33:14 +0000869 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870}
871
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000872void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
873 const TargetData *TD = TM.getTargetData();
874
875 if (!GVar->hasInitializer())
876 return; // External global require no code
877
878 // Check to see if this is a special global used by LLVM, if so, emit it.
879 if (EmitSpecialLLVMGlobal(GVar)) {
880 if (TM.getRelocationModel() == Reloc::Static) {
881 if (GVar->getName() == "llvm.global_ctors")
882 O << ".reference .constructors_used\n";
883 else if (GVar->getName() == "llvm.global_dtors")
884 O << ".reference .destructors_used\n";
885 }
886 return;
887 }
888
889 std::string name = Mang->getValueName(GVar);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000890
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000891 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000892
893 Constant *C = GVar->getInitializer();
894 const Type *Type = C->getType();
895 unsigned Size = TD->getABITypeSize(Type);
896 unsigned Align = TD->getPreferredAlignmentLog(GVar);
897
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000898 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000899
900 if (C->isNullValue() && /* FIXME: Verify correct */
901 !GVar->hasSection() &&
902 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000903 GVar->mayBeOverridden())) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000904 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
905
906 if (GVar->hasExternalLinkage()) {
907 O << "\t.globl " << name << '\n';
908 O << "\t.zerofill __DATA, __common, " << name << ", "
909 << Size << ", " << Align;
910 } else if (GVar->hasInternalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000911 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000912 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000913 O << "\t.globl " << name << '\n'
914 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000915 EmitAlignment(Align, GVar);
916 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
917 PrintUnmangledNameSafely(GVar, O);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000918 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000919 EmitGlobalConstant(C);
920 return;
921 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000922 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000923 // Darwin 9 and above support aligned common data.
924 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000925 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000926 }
927 O << "\t\t" << TAI->getCommentString() << " '";
928 PrintUnmangledNameSafely(GVar, O);
929 O << "'\n";
930 return;
931 }
932
933 switch (GVar->getLinkage()) {
934 case GlobalValue::LinkOnceLinkage:
935 case GlobalValue::WeakLinkage:
936 case GlobalValue::CommonLinkage:
937 O << "\t.globl " << name << '\n'
938 << "\t.weak_definition " << name << '\n';
939 break;
940 case GlobalValue::AppendingLinkage:
941 // FIXME: appending linkage variables should go into a section of
942 // their name or something. For now, just emit them as external.
943 case GlobalValue::ExternalLinkage:
944 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000945 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000946 // FALL THROUGH
947 case GlobalValue::InternalLinkage:
948 break;
949 default:
950 cerr << "Unknown linkage type!";
951 abort();
952 }
953
954 EmitAlignment(Align, GVar);
955 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
956 PrintUnmangledNameSafely(GVar, O);
957 O << "'\n";
958
959 // If the initializer is a extern weak symbol, remember to emit the weak
960 // reference!
961 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
962 if (GV->hasExternalWeakLinkage())
963 ExtWeakSymbols.insert(GV);
964
965 EmitGlobalConstant(C);
966 O << '\n';
967}
968
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000969bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 const TargetData *TD = TM.getTargetData();
971
972 // Print out module-level global variables here.
973 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000974 I != E; ++I)
975 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976
977 bool isPPC64 = TD->getPointerSizeInBits() == 64;
978
979 // Output stubs for dynamically-linked functions
980 if (TM.getRelocationModel() == Reloc::PIC_) {
981 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
982 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000983 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 "pure_instructions,32");
985 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +0000986 std::string p = *i;
987 std::string L0p = (p[0]=='\"') ? "\"L0$" + p.substr(1) : "L0$" + p ;
988 printSuffixedName(p, "$stub");
989 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000990 O << "\t.indirect_symbol " << *i << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 O << "\tmflr r0\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000992 O << "\tbcl 20,31," << L0p << '\n';
Dale Johannesena21b5202008-05-19 21:38:18 +0000993 O << L0p << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 O << "\tmflr r11\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000995 O << "\taddis r11,r11,ha16(";
996 printSuffixedName(p, "$lazy_ptr");
997 O << "-" << L0p << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 O << "\tmtlr r0\n";
999 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001000 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001002 O << "\tlwzu r12,lo16(";
1003 printSuffixedName(p, "$lazy_ptr");
1004 O << "-" << L0p << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 O << "\tmtctr r12\n";
1006 O << "\tbctr\n";
1007 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001008 printSuffixedName(p, "$lazy_ptr");
1009 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001010 O << "\t.indirect_symbol " << *i << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 if (isPPC64)
1012 O << "\t.quad dyld_stub_binding_helper\n";
1013 else
1014 O << "\t.long dyld_stub_binding_helper\n";
1015 }
1016 } else {
1017 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1018 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001019 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 "pure_instructions,16");
1021 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +00001022 std::string p = *i;
1023 printSuffixedName(p, "$stub");
1024 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001025 O << "\t.indirect_symbol " << *i << '\n';
Dale Johannesena21b5202008-05-19 21:38:18 +00001026 O << "\tlis r11,ha16(";
1027 printSuffixedName(p, "$lazy_ptr");
1028 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001030 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001032 O << "\tlwzu r12,lo16(";
1033 printSuffixedName(p, "$lazy_ptr");
1034 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 O << "\tmtctr r12\n";
1036 O << "\tbctr\n";
1037 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001038 printSuffixedName(p, "$lazy_ptr");
1039 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001040 O << "\t.indirect_symbol " << *i << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 if (isPPC64)
1042 O << "\t.quad dyld_stub_binding_helper\n";
1043 else
1044 O << "\t.long dyld_stub_binding_helper\n";
1045 }
1046 }
1047
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001048 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049
Dale Johannesen85535762008-04-02 00:25:04 +00001050 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001051 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001052 // Only referenced functions get into the Personalities list.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001053 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1054
1055 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1056 E = Personalities.end(); I != E; ++I)
1057 if (*I) GVStubs.insert("_" + (*I)->getName());
1058 }
1059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001061 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 SwitchToDataSection(".non_lazy_symbol_pointer");
1063 for (std::set<std::string>::iterator I = GVStubs.begin(),
1064 E = GVStubs.end(); I != E; ++I) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001065 std::string p = *I;
1066 printSuffixedName(p, "$non_lazy_ptr");
1067 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001068 O << "\t.indirect_symbol " << *I << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 if (isPPC64)
1070 O << "\t.quad\t0\n";
1071 else
1072 O << "\t.long\t0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 }
1074 }
1075
1076 // Emit initial debug information.
1077 DW.EndModule();
1078
1079 // Funny Darwin hack: This flag tells the linker that no global symbols
1080 // contain code that falls through to other global symbols (e.g. the obvious
1081 // implementation of multiple entry points). If this doesn't occur, the
1082 // linker can safely perform dead code stripping. Since LLVM never generates
1083 // code that does this, it is always safe to set.
1084 O << "\t.subsections_via_symbols\n";
1085
Dan Gohman4a558a32007-07-25 19:33:14 +00001086 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087}
1088
1089
1090
1091/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1092/// for a MachineFunction to the given output stream, in a format that the
1093/// Darwin assembler can deal with.
1094///
Owen Anderson847b99b2008-08-21 00:14:44 +00001095FunctionPass *llvm::createPPCAsmPrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 PPCTargetMachine &tm) {
1097 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1098
1099 if (Subtarget->isDarwin()) {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001100 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 } else {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001102 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 }
1104}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001105
1106namespace {
1107 static struct Register {
1108 Register() {
1109 PPCTargetMachine::registerAsmPrinter(createPPCAsmPrinterPass);
1110 }
1111 } Registrator;
1112}