blob: c29f0d2bec716eeb1f1758207070b6d4be58b5bc [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"
39#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000040#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetOptions.h"
43#include "llvm/ADT/Statistic.h"
44#include "llvm/ADT/StringExtras.h"
45#include <set>
46using namespace llvm;
47
48STATISTIC(EmittedInsts, "Number of machine instrs printed");
49
50namespace {
51 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
52 std::set<std::string> FnStubs, GVStubs;
53 const PPCSubtarget &Subtarget;
54
55 PPCAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
56 : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) {
57 }
58
59 virtual const char *getPassName() const {
60 return "PowerPC Assembly Printer";
61 }
62
63 PPCTargetMachine &getTM() {
64 return static_cast<PPCTargetMachine&>(TM);
65 }
66
67 unsigned enumRegToMachineReg(unsigned enumReg) {
68 switch (enumReg) {
69 default: assert(0 && "Unhandled register!"); break;
70 case PPC::CR0: return 0;
71 case PPC::CR1: return 1;
72 case PPC::CR2: return 2;
73 case PPC::CR3: return 3;
74 case PPC::CR4: return 4;
75 case PPC::CR5: return 5;
76 case PPC::CR6: return 6;
77 case PPC::CR7: return 7;
78 }
79 abort();
80 }
81
82 /// printInstruction - This method is automatically generated by tablegen
83 /// from the instruction set description. This method returns true if the
84 /// machine instruction was sufficiently described to print it, otherwise it
85 /// returns false.
86 bool printInstruction(const MachineInstr *MI);
87
88 void printMachineInstruction(const MachineInstr *MI);
89 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +000090
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 /// stripRegisterPrefix - This method strips the character prefix from a
92 /// register name so that only the number is left. Used by for linux asm.
93 const char *stripRegisterPrefix(const char *RegName) {
94 switch (RegName[0]) {
95 case 'r':
96 case 'f':
97 case 'v': return RegName + 1;
98 case 'c': if (RegName[1] == 'r') return RegName + 2;
99 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000100
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 return RegName;
102 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 /// printRegister - Print register according to target requirements.
105 ///
106 void printRegister(const MachineOperand &MO, bool R0AsZero) {
107 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000108 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 // If we should use 0 for R0.
111 if (R0AsZero && RegNo == PPC::R0) {
112 O << "0";
113 return;
114 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000115
Bill Wendling8eeb9792008-02-26 21:11:01 +0000116 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 // Linux assembler (Others?) does not take register mnemonics.
118 // FIXME - What about special registers used in mfspr/mtspr?
119 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
120 O << RegName;
121 }
122
123 void printOperand(const MachineInstr *MI, unsigned OpNo) {
124 const MachineOperand &MO = MI->getOperand(OpNo);
125 if (MO.isRegister()) {
126 printRegister(MO, false);
127 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000128 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 } else {
130 printOp(MO);
131 }
132 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
135 unsigned AsmVariant, const char *ExtraCode);
136 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
137 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000138
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000141 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 value = (value << (32-5)) >> (32-5);
143 O << (int)value;
144 }
145 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000146 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 assert(value <= 31 && "Invalid u5imm argument!");
148 O << (unsigned int)value;
149 }
150 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000151 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 assert(value <= 63 && "Invalid u6imm argument!");
153 O << (unsigned int)value;
154 }
155 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000156 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 }
158 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000159 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 }
161 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
162 if (MI->getOperand(OpNo).isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000163 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 } else {
165 O << "lo16(";
166 printOp(MI->getOperand(OpNo));
167 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000168 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 else
170 O << ')';
171 }
172 }
173 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
174 // Branches can take an immediate operand. This is used by the branch
175 // selection pass to print $+8, an eight byte displacement from the PC.
176 if (MI->getOperand(OpNo).isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000177 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 } else {
179 printOp(MI->getOperand(OpNo));
180 }
181 }
182 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
183 const MachineOperand &MO = MI->getOperand(OpNo);
184 if (TM.getRelocationModel() != Reloc::Static) {
185 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
186 GlobalValue *GV = MO.getGlobal();
187 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000188 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 // Dynamically-resolved functions need a stub for the function.
190 std::string Name = Mang->getValueName(GV);
191 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000192 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 if (GV->hasExternalWeakLinkage())
194 ExtWeakSymbols.insert(GV);
195 return;
196 }
197 }
198 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
199 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
200 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000201 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 return;
203 }
204 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000205
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 printOp(MI->getOperand(OpNo));
207 }
208 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000209 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 }
211 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000212 O << "\"L" << getFunctionNumber() << "$pb\"\n";
213 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 }
215 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
216 if (MI->getOperand(OpNo).isImmediate()) {
217 printS16ImmOperand(MI, OpNo);
218 } else {
219 if (Subtarget.isDarwin()) O << "ha16(";
220 printOp(MI->getOperand(OpNo));
221 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000222 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 if (Subtarget.isDarwin())
224 O << ')';
225 else
226 O << "@ha";
227 }
228 }
229 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
230 if (MI->getOperand(OpNo).isImmediate()) {
231 printS16ImmOperand(MI, OpNo);
232 } else {
233 if (Subtarget.isDarwin()) O << "lo16(";
234 printOp(MI->getOperand(OpNo));
235 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000236 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 if (Subtarget.isDarwin())
238 O << ')';
239 else
240 O << "@l";
241 }
242 }
243 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
244 unsigned CCReg = MI->getOperand(OpNo).getReg();
245 unsigned RegNo = enumRegToMachineReg(CCReg);
246 O << (0x80 >> RegNo);
247 }
248 // The new addressing mode printers.
249 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
250 printSymbolLo(MI, OpNo);
251 O << '(';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000252 if (MI->getOperand(OpNo+1).isRegister() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 MI->getOperand(OpNo+1).getReg() == PPC::R0)
254 O << "0";
255 else
256 printOperand(MI, OpNo+1);
257 O << ')';
258 }
259 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
260 if (MI->getOperand(OpNo).isImmediate())
261 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000262 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 printSymbolLo(MI, OpNo);
264 O << '(';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000265 if (MI->getOperand(OpNo+1).isRegister() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 MI->getOperand(OpNo+1).getReg() == PPC::R0)
267 O << "0";
268 else
269 printOperand(MI, OpNo+1);
270 O << ')';
271 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
274 // When used as the base register, r0 reads constant zero rather than
275 // the value contained in the register. For this reason, the darwin
276 // assembler requires that we print r0 as 0 (no r) when used as the base.
277 const MachineOperand &MO = MI->getOperand(OpNo);
278 printRegister(MO, true);
279 O << ", ";
280 printOperand(MI, OpNo+1);
281 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000282
283 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000285
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
287 virtual bool doFinalization(Module &M) = 0;
288
289 virtual void EmitExternalGlobal(const GlobalVariable *GV);
290 };
291
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000292 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
293 struct VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
295 DwarfWriter DW;
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000296 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000298 PPCLinuxAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 const TargetAsmInfo *T)
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000300 : PPCAsmPrinter(O, TM, T), DW(O, this, T), 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>();
314 PPCAsmPrinter::getAnalysisUsage(AU);
315 }
316
317 /// getSectionForFunction - Return the section that we should emit the
318 /// specified function body into.
319 virtual std::string getSectionForFunction(const Function &F) const;
320 };
321
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000322 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
323 /// OS X
324 struct VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
325
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 DwarfWriter DW;
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000327 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000329 PPCDarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
330 const TargetAsmInfo *T)
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000331 : PPCAsmPrinter(O, TM, T), DW(O, this, T), MMI(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 }
333
334 virtual const char *getPassName() const {
335 return "Darwin PPC Assembly Printer";
336 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000337
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 bool runOnMachineFunction(MachineFunction &F);
339 bool doInitialization(Module &M);
340 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000341
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 void getAnalysisUsage(AnalysisUsage &AU) const {
343 AU.setPreservesAll();
344 AU.addRequired<MachineModuleInfo>();
345 PPCAsmPrinter::getAnalysisUsage(AU);
346 }
347
348 /// getSectionForFunction - Return the section that we should emit the
349 /// specified function body into.
350 virtual std::string getSectionForFunction(const Function &F) const;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000351 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 };
353} // end of anonymous namespace
354
355// Include the auto-generated portion of the assembly writer
356#include "PPCGenAsmWriter.inc"
357
358void PPCAsmPrinter::printOp(const MachineOperand &MO) {
359 switch (MO.getType()) {
360 case MachineOperand::MO_Immediate:
361 cerr << "printOp() does not handle immediate values\n";
362 abort();
363 return;
364
365 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000366 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 return;
368 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000369 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000370 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 // FIXME: PIC relocation model
372 return;
373 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000374 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000375 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 return;
377 case MachineOperand::MO_ExternalSymbol:
378 // Computing the address of an external symbol, not calling it.
379 if (TM.getRelocationModel() != Reloc::Static) {
380 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
381 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000382 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 return;
384 }
385 O << TAI->getGlobalPrefix() << MO.getSymbolName();
386 return;
387 case MachineOperand::MO_GlobalAddress: {
388 // Computing the address of a global symbol, not calling it.
389 GlobalValue *GV = MO.getGlobal();
390 std::string Name = Mang->getValueName(GV);
391
392 // External or weakly linked global variables need non-lazily-resolved stubs
393 if (TM.getRelocationModel() != Reloc::Static) {
394 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000395 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000397 printSuffixedName(Name, "$non_lazy_ptr");
Dale Johannesencaf11182008-05-16 20:09:25 +0000398 if (GV->hasExternalWeakLinkage())
399 ExtWeakSymbols.insert(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 return;
401 }
402 }
403 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 if (MO.getOffset() > 0)
406 O << "+" << MO.getOffset();
407 else if (MO.getOffset() < 0)
408 O << MO.getOffset();
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000409
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 if (GV->hasExternalWeakLinkage())
411 ExtWeakSymbols.insert(GV);
412 return;
413 }
414
415 default:
416 O << "<unknown operand type: " << MO.getType() << ">";
417 return;
418 }
419}
420
421/// EmitExternalGlobal - In this case we need to use the indirect symbol.
422///
423void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
424 std::string Name = getGlobalLinkName(GV);
425 if (TM.getRelocationModel() != Reloc::Static) {
426 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000427 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 return;
429 }
430 O << Name;
431}
432
433/// PrintAsmOperand - Print out an operand for an inline asm expression.
434///
435bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000436 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 const char *ExtraCode) {
438 // Does this asm operand have a single letter operand modifier?
439 if (ExtraCode && ExtraCode[0]) {
440 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000441
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 switch (ExtraCode[0]) {
443 default: return true; // Unknown modifier.
444 case 'c': // Don't print "$" before a global var name or constant.
445 // PPC never has a prefix.
446 printOperand(MI, OpNo);
447 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000448 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 // Verify that this operand has two consecutive registers.
450 if (!MI->getOperand(OpNo).isRegister() ||
451 OpNo+1 == MI->getNumOperands() ||
452 !MI->getOperand(OpNo+1).isRegister())
453 return true;
454 ++OpNo; // Return the high-part.
455 break;
456 case 'I':
457 // Write 'i' if an integer constant, otherwise nothing. Used to print
458 // addi vs add, etc.
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000459 if (MI->getOperand(OpNo).isImmediate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 O << "i";
461 return false;
462 }
463 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000464
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 printOperand(MI, OpNo);
466 return false;
467}
468
469bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000470 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 const char *ExtraCode) {
472 if (ExtraCode && ExtraCode[0])
473 return true; // Unknown modifier.
474 if (MI->getOperand(OpNo).isRegister())
475 printMemRegReg(MI, OpNo);
476 else
477 printMemRegImm(MI, OpNo);
478 return false;
479}
480
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000481void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 const char *Modifier) {
483 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
484 unsigned Code = MI->getOperand(OpNo).getImm();
485 if (!strcmp(Modifier, "cc")) {
486 switch ((PPC::Predicate)Code) {
487 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
488 case PPC::PRED_LT: O << "lt"; return;
489 case PPC::PRED_LE: O << "le"; return;
490 case PPC::PRED_EQ: O << "eq"; return;
491 case PPC::PRED_GE: O << "ge"; return;
492 case PPC::PRED_GT: O << "gt"; return;
493 case PPC::PRED_NE: O << "ne"; return;
494 case PPC::PRED_UN: O << "un"; return;
495 case PPC::PRED_NU: O << "nu"; return;
496 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000497
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 } else {
499 assert(!strcmp(Modifier, "reg") &&
500 "Need to specify 'cc' or 'reg' as predicate op modifier!");
501 // Don't print the register for 'always'.
502 if (Code == PPC::PRED_ALWAYS) return;
503 printOperand(MI, OpNo+1);
504 }
505}
506
507
508/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
509/// the current output stream.
510///
511void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
512 ++EmittedInsts;
513
514 // Check for slwi/srwi mnemonics.
515 if (MI->getOpcode() == PPC::RLWINM) {
516 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000517 unsigned char SH = MI->getOperand(2).getImm();
518 unsigned char MB = MI->getOperand(3).getImm();
519 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000521 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 }
523 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000524 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 SH = 32-SH;
526 }
527 if (FoundMnemonic) {
528 printOperand(MI, 0);
529 O << ", ";
530 printOperand(MI, 1);
531 O << ", " << (unsigned int)SH << "\n";
532 return;
533 }
534 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
535 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000536 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 printOperand(MI, 0);
538 O << ", ";
539 printOperand(MI, 1);
540 O << "\n";
541 return;
542 }
543 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000544 unsigned char SH = MI->getOperand(2).getImm();
545 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
547 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000548 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 printOperand(MI, 0);
550 O << ", ";
551 printOperand(MI, 1);
552 O << ", " << (unsigned int)SH << "\n";
553 return;
554 }
555 }
556
557 if (printInstruction(MI))
558 return; // Printer was automatically generated
559
560 assert(0 && "Unhandled instruction in asm writer!");
561 abort();
562 return;
563}
564
565/// runOnMachineFunction - This uses the printMachineInstruction()
566/// method to print assembly for each instruction.
567///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000568bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569
570 SetupMachineFunction(MF);
571 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000572
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 // Print out constants referenced by the function
574 EmitConstantPool(MF.getConstantPool());
575
576 // Print out labels for the function.
577 const Function *F = MF.getFunction();
578 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000579
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 switch (F->getLinkage()) {
581 default: assert(0 && "Unknown linkage type!");
582 case Function::InternalLinkage: // Symbols default to internal.
583 break;
584 case Function::ExternalLinkage:
585 O << "\t.global\t" << CurrentFnName << '\n'
586 << "\t.type\t" << CurrentFnName << ", @function\n";
587 break;
588 case Function::WeakLinkage:
589 case Function::LinkOnceLinkage:
590 O << "\t.global\t" << CurrentFnName << '\n';
591 O << "\t.weak\t" << CurrentFnName << '\n';
592 break;
593 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000594
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 if (F->hasHiddenVisibility())
596 if (const char *Directive = TAI->getHiddenDirective())
597 O << Directive << CurrentFnName << "\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000598
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 EmitAlignment(2, F);
600 O << CurrentFnName << ":\n";
601
602 // Emit pre-function debug information.
603 DW.BeginFunction(&MF);
604
605 // Print out code for the function.
606 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
607 I != E; ++I) {
608 // Print a label for the basic block.
609 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000610 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 O << '\n';
612 }
613 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
614 II != E; ++II) {
615 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 printMachineInstruction(II);
617 }
618 }
619
620 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
621
622 // Print out jump tables referenced by the function.
623 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000624
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 // Emit post-function debug information.
626 DW.EndFunction();
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 // We didn't modify anything.
629 return false;
630}
631
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000632bool PPCLinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000633 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000634
Dale Johannesen2f6aa072008-07-09 21:24:07 +0000635 // Emit initial debug information.
636 DW.BeginModule(&M);
637
638 // AsmPrinter::doInitialization should have done this analysis.
639 MMI = getAnalysisToUpdate<MachineModuleInfo>();
640 assert(MMI);
641 DW.SetModuleInfo(MMI);
642
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 // GNU as handles section names wrapped in quotes
644 Mang->setUseQuotes(true);
645
646 SwitchToTextSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000647
Dan Gohman4a558a32007-07-25 19:33:14 +0000648 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649}
650
Chris Lattner2b638a72008-02-15 19:04:54 +0000651/// PrintUnmangledNameSafely - Print out the printable characters in the name.
652/// Don't print things like \n or \0.
653static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
654 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
655 Name != E; ++Name)
656 if (isprint(*Name))
657 OS << *Name;
658}
659
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000660bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 const TargetData *TD = TM.getTargetData();
662
663 // Print out module-level global variables here.
664 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
665 I != E; ++I) {
666 if (!I->hasInitializer()) continue; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000667
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 // Check to see if this is a special global used by LLVM, if so, emit it.
669 if (EmitSpecialLLVMGlobal(I))
670 continue;
671
672 std::string name = Mang->getValueName(I);
673
674 if (I->hasHiddenVisibility())
675 if (const char *Directive = TAI->getHiddenDirective())
676 O << Directive << name << "\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 Constant *C = I->getInitializer();
Duncan Sands8157ef42007-11-05 00:04:43 +0000679 unsigned Size = TD->getABITypeSize(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 unsigned Align = TD->getPreferredAlignmentLog(I);
681
682 if (C->isNullValue() && /* FIXME: Verify correct */
Dale Johannesen49c44122008-05-14 20:12:51 +0000683 !I->hasSection() && (I->hasCommonLinkage() ||
684 I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng65c0fbc2007-09-21 00:41:19 +0000685 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
687 if (I->hasExternalLinkage()) {
688 O << "\t.global " << name << '\n';
689 O << "\t.type " << name << ", @object\n";
Nick Lewyckyc6583752007-11-04 17:32:10 +0000690 if (TAI->getBSSSection())
691 SwitchToDataSection(TAI->getBSSSection(), I);
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000692 O << name << ":\n";
693 O << "\t.zero " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 } else if (I->hasInternalLinkage()) {
695 SwitchToDataSection("\t.data", I);
696 O << TAI->getLCOMMDirective() << name << "," << Size;
697 } else {
698 SwitchToDataSection("\t.data", I);
699 O << ".comm " << name << "," << Size;
700 }
Chris Lattner2b638a72008-02-15 19:04:54 +0000701 O << "\t\t" << TAI->getCommentString() << " '";
702 PrintUnmangledNameSafely(I, O);
703 O << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 } else {
705 switch (I->getLinkage()) {
706 case GlobalValue::LinkOnceLinkage:
707 case GlobalValue::WeakLinkage:
Dale Johannesen49c44122008-05-14 20:12:51 +0000708 case GlobalValue::CommonLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 O << "\t.global " << name << '\n'
710 << "\t.type " << name << ", @object\n"
711 << "\t.weak " << name << '\n';
712 SwitchToDataSection("\t.data", I);
713 break;
714 case GlobalValue::AppendingLinkage:
715 // FIXME: appending linkage variables should go into a section of
716 // their name or something. For now, just emit them as external.
717 case GlobalValue::ExternalLinkage:
718 // If external or appending, declare as a global symbol
719 O << "\t.global " << name << "\n"
720 << "\t.type " << name << ", @object\n";
721 // FALL THROUGH
722 case GlobalValue::InternalLinkage:
723 if (I->isConstant()) {
724 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
725 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
726 SwitchToDataSection(TAI->getCStringSection(), I);
727 break;
728 }
729 }
730
731 // FIXME: special handling for ".ctors" & ".dtors" sections
732 if (I->hasSection() &&
733 (I->getSection() == ".ctors" ||
734 I->getSection() == ".dtors")) {
735 std::string SectionName = ".section " + I->getSection()
736 + ",\"aw\",@progbits";
737 SwitchToDataSection(SectionName.c_str());
738 } else {
739 if (I->isConstant() && TAI->getReadOnlySection())
740 SwitchToDataSection(TAI->getReadOnlySection(), I);
741 else
742 SwitchToDataSection(TAI->getDataSection(), I);
743 }
744 break;
745 default:
746 cerr << "Unknown linkage type!";
747 abort();
748 }
749
750 EmitAlignment(Align, I);
Chris Lattner2b638a72008-02-15 19:04:54 +0000751 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
752 PrintUnmangledNameSafely(I, O);
753 O << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754
755 // If the initializer is a extern weak symbol, remember to emit the weak
756 // reference!
757 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
758 if (GV->hasExternalWeakLinkage())
759 ExtWeakSymbols.insert(GV);
760
761 EmitGlobalConstant(C);
762 O << '\n';
763 }
764 }
765
766 // TODO
767
768 // Emit initial debug information.
769 DW.EndModule();
770
Dan Gohman4a558a32007-07-25 19:33:14 +0000771 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772}
773
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000774std::string PPCLinuxAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000775 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776}
777
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000778std::string PPCDarwinAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000779 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780}
781
782/// runOnMachineFunction - This uses the printMachineInstruction()
783/// method to print assembly for each instruction.
784///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000785bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786
787 SetupMachineFunction(MF);
788 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000789
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 // Print out constants referenced by the function
791 EmitConstantPool(MF.getConstantPool());
792
793 // Print out labels for the function.
794 const Function *F = MF.getFunction();
795 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000796
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 switch (F->getLinkage()) {
798 default: assert(0 && "Unknown linkage type!");
799 case Function::InternalLinkage: // Symbols default to internal.
800 break;
801 case Function::ExternalLinkage:
802 O << "\t.globl\t" << CurrentFnName << "\n";
803 break;
804 case Function::WeakLinkage:
805 case Function::LinkOnceLinkage:
806 O << "\t.globl\t" << CurrentFnName << "\n";
807 O << "\t.weak_definition\t" << CurrentFnName << "\n";
808 break;
809 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 if (F->hasHiddenVisibility())
812 if (const char *Directive = TAI->getHiddenDirective())
813 O << Directive << CurrentFnName << "\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000814
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000815 EmitAlignment(OptimizeForSize ? 2 : 4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 O << CurrentFnName << ":\n";
817
818 // Emit pre-function debug information.
819 DW.BeginFunction(&MF);
820
Bill Wendling36ccaea2008-01-26 06:51:24 +0000821 // If the function is empty, then we need to emit *something*. Otherwise, the
822 // function's label might be associated with something that it wasn't meant to
823 // be associated with. We emit a noop in this situation.
824 MachineFunction::iterator I = MF.begin();
825
Bill Wendlingb5880a72008-01-26 09:03:52 +0000826 if (++I == MF.end() && MF.front().empty())
827 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000828
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 // Print out code for the function.
830 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
831 I != E; ++I) {
832 // Print a label for the basic block.
833 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000834 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 O << '\n';
836 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000837 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
838 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 printMachineInstruction(II);
841 }
842 }
843
844 // Print out jump tables referenced by the function.
845 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000846
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 // Emit post-function debug information.
848 DW.EndFunction();
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 // We didn't modify anything.
851 return false;
852}
853
854
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000855bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000856 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000857 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 "ppc",
859 "ppc601",
860 "ppc602",
861 "ppc603",
862 "ppc7400",
863 "ppc750",
864 "ppc970",
865 "ppc64"
866 };
867
868 unsigned Directive = Subtarget.getDarwinDirective();
869 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
870 Directive = PPC::DIR_970;
871 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
872 Directive = PPC::DIR_7400;
873 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
874 Directive = PPC::DIR_64;
875 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
876 O << "\t.machine " << CPUDirectives[Directive] << "\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000877
Dan Gohman4a558a32007-07-25 19:33:14 +0000878 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000879
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000880 // Emit initial debug information.
881 DW.BeginModule(&M);
882
883 // We need this for Personality functions.
884 // AsmPrinter::doInitialization should have done this analysis.
885 MMI = getAnalysisToUpdate<MachineModuleInfo>();
886 assert(MMI);
887 DW.SetModuleInfo(MMI);
888
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 // Darwin wants symbols to be quoted if they have complex names.
890 Mang->setUseQuotes(true);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000891
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 // Prime text sections so they are adjacent. This reduces the likelihood a
893 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000894 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 "pure_instructions");
896 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000897 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 "pure_instructions,32");
899 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000900 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 "pure_instructions,16");
902 }
903 SwitchToTextSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000904
Dan Gohman4a558a32007-07-25 19:33:14 +0000905 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906}
907
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000908void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
909 const TargetData *TD = TM.getTargetData();
910
911 if (!GVar->hasInitializer())
912 return; // External global require no code
913
914 // Check to see if this is a special global used by LLVM, if so, emit it.
915 if (EmitSpecialLLVMGlobal(GVar)) {
916 if (TM.getRelocationModel() == Reloc::Static) {
917 if (GVar->getName() == "llvm.global_ctors")
918 O << ".reference .constructors_used\n";
919 else if (GVar->getName() == "llvm.global_dtors")
920 O << ".reference .destructors_used\n";
921 }
922 return;
923 }
924
925 std::string name = Mang->getValueName(GVar);
926 std::string SectionName = TAI->SectionForGlobal(GVar);
927
928 if (GVar->hasHiddenVisibility())
929 if (const char *Directive = TAI->getHiddenDirective())
930 O << Directive << name << "\n";
931
932 Constant *C = GVar->getInitializer();
933 const Type *Type = C->getType();
934 unsigned Size = TD->getABITypeSize(Type);
935 unsigned Align = TD->getPreferredAlignmentLog(GVar);
936
937 SwitchToDataSection(SectionName.c_str());
938
939 if (C->isNullValue() && /* FIXME: Verify correct */
940 !GVar->hasSection() &&
941 (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() ||
942 GVar->isWeakForLinker())) {
943 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
944
945 if (GVar->hasExternalLinkage()) {
946 O << "\t.globl " << name << '\n';
947 O << "\t.zerofill __DATA, __common, " << name << ", "
948 << Size << ", " << Align;
949 } else if (GVar->hasInternalLinkage()) {
950 O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align;
951 } else if (!GVar->hasCommonLinkage()) {
952 O << "\t.globl " << name << "\n"
953 << TAI->getWeakDefDirective() << name << "\n";
954 EmitAlignment(Align, GVar);
955 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
956 PrintUnmangledNameSafely(GVar, O);
957 O << "\n";
958 EmitGlobalConstant(C);
959 return;
960 } else {
961 O << ".comm " << name << "," << Size;
962 // Darwin 9 and above support aligned common data.
963 if (Subtarget.isDarwin9())
964 O << "," << Align;
965 }
966 O << "\t\t" << TAI->getCommentString() << " '";
967 PrintUnmangledNameSafely(GVar, O);
968 O << "'\n";
969 return;
970 }
971
972 switch (GVar->getLinkage()) {
973 case GlobalValue::LinkOnceLinkage:
974 case GlobalValue::WeakLinkage:
975 case GlobalValue::CommonLinkage:
976 O << "\t.globl " << name << '\n'
977 << "\t.weak_definition " << name << '\n';
978 break;
979 case GlobalValue::AppendingLinkage:
980 // FIXME: appending linkage variables should go into a section of
981 // their name or something. For now, just emit them as external.
982 case GlobalValue::ExternalLinkage:
983 // If external or appending, declare as a global symbol
984 O << "\t.globl " << name << "\n";
985 // FALL THROUGH
986 case GlobalValue::InternalLinkage:
987 break;
988 default:
989 cerr << "Unknown linkage type!";
990 abort();
991 }
992
993 EmitAlignment(Align, GVar);
994 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
995 PrintUnmangledNameSafely(GVar, O);
996 O << "'\n";
997
998 // If the initializer is a extern weak symbol, remember to emit the weak
999 // reference!
1000 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1001 if (GV->hasExternalWeakLinkage())
1002 ExtWeakSymbols.insert(GV);
1003
1004 EmitGlobalConstant(C);
1005 O << '\n';
1006}
1007
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001008bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 const TargetData *TD = TM.getTargetData();
1010
1011 // Print out module-level global variables here.
1012 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001013 I != E; ++I)
1014 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015
1016 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1017
1018 // Output stubs for dynamically-linked functions
1019 if (TM.getRelocationModel() == Reloc::PIC_) {
1020 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1021 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001022 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 "pure_instructions,32");
1024 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +00001025 std::string p = *i;
1026 std::string L0p = (p[0]=='\"') ? "\"L0$" + p.substr(1) : "L0$" + p ;
1027 printSuffixedName(p, "$stub");
1028 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 O << "\t.indirect_symbol " << *i << "\n";
1030 O << "\tmflr r0\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001031 O << "\tbcl 20,31," << L0p << "\n";
1032 O << L0p << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 O << "\tmflr r11\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001034 O << "\taddis r11,r11,ha16(";
1035 printSuffixedName(p, "$lazy_ptr");
1036 O << "-" << L0p << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 O << "\tmtlr r0\n";
1038 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001039 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001041 O << "\tlwzu r12,lo16(";
1042 printSuffixedName(p, "$lazy_ptr");
1043 O << "-" << L0p << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 O << "\tmtctr r12\n";
1045 O << "\tbctr\n";
1046 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001047 printSuffixedName(p, "$lazy_ptr");
1048 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 O << "\t.indirect_symbol " << *i << "\n";
1050 if (isPPC64)
1051 O << "\t.quad dyld_stub_binding_helper\n";
1052 else
1053 O << "\t.long dyld_stub_binding_helper\n";
1054 }
1055 } else {
1056 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1057 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001058 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 "pure_instructions,16");
1060 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +00001061 std::string p = *i;
1062 printSuffixedName(p, "$stub");
1063 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 O << "\t.indirect_symbol " << *i << "\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001065 O << "\tlis r11,ha16(";
1066 printSuffixedName(p, "$lazy_ptr");
1067 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001069 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001071 O << "\tlwzu r12,lo16(";
1072 printSuffixedName(p, "$lazy_ptr");
1073 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 O << "\tmtctr r12\n";
1075 O << "\tbctr\n";
1076 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001077 printSuffixedName(p, "$lazy_ptr");
1078 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 O << "\t.indirect_symbol " << *i << "\n";
1080 if (isPPC64)
1081 O << "\t.quad dyld_stub_binding_helper\n";
1082 else
1083 O << "\t.long dyld_stub_binding_helper\n";
1084 }
1085 }
1086
1087 O << "\n";
1088
Dale Johannesen85535762008-04-02 00:25:04 +00001089 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001090 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001091 // Only referenced functions get into the Personalities list.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001092 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1093
1094 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1095 E = Personalities.end(); I != E; ++I)
1096 if (*I) GVStubs.insert("_" + (*I)->getName());
1097 }
1098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001100 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 SwitchToDataSection(".non_lazy_symbol_pointer");
1102 for (std::set<std::string>::iterator I = GVStubs.begin(),
1103 E = GVStubs.end(); I != E; ++I) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001104 std::string p = *I;
1105 printSuffixedName(p, "$non_lazy_ptr");
1106 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107 O << "\t.indirect_symbol " << *I << "\n";
1108 if (isPPC64)
1109 O << "\t.quad\t0\n";
1110 else
1111 O << "\t.long\t0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 }
1113 }
1114
1115 // Emit initial debug information.
1116 DW.EndModule();
1117
1118 // Funny Darwin hack: This flag tells the linker that no global symbols
1119 // contain code that falls through to other global symbols (e.g. the obvious
1120 // implementation of multiple entry points). If this doesn't occur, the
1121 // linker can safely perform dead code stripping. Since LLVM never generates
1122 // code that does this, it is always safe to set.
1123 O << "\t.subsections_via_symbols\n";
1124
Dan Gohman4a558a32007-07-25 19:33:14 +00001125 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126}
1127
1128
1129
1130/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1131/// for a MachineFunction to the given output stream, in a format that the
1132/// Darwin assembler can deal with.
1133///
1134FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o,
1135 PPCTargetMachine &tm) {
1136 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1137
1138 if (Subtarget->isDarwin()) {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001139 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 } else {
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001141 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142 }
1143}