blob: 98b4b4a560dc25c4ea0d2befaee66ff9682a6d67 [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);
90
91 /// 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 }
100
101 return RegName;
102 }
103
104 /// 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??");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 // If we should use 0 for R0.
111 if (R0AsZero && RegNo == PPC::R0) {
112 O << "0";
113 return;
114 }
115
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 }
133
134 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);
138
139
140 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 }
205
206 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 << '(';
252 if (MI->getOperand(OpNo+1).isRegister() &&
253 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);
262 else
263 printSymbolLo(MI, OpNo);
264 O << '(';
265 if (MI->getOperand(OpNo+1).isRegister() &&
266 MI->getOperand(OpNo+1).getReg() == PPC::R0)
267 O << "0";
268 else
269 printOperand(MI, OpNo+1);
270 O << ')';
271 }
272
273 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 }
282
283 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
284 const char *Modifier);
285
286 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
287 virtual bool doFinalization(Module &M) = 0;
288
289 virtual void EmitExternalGlobal(const GlobalVariable *GV);
290 };
291
292 /// LinuxAsmPrinter - PowerPC assembly printer, customized for Linux
293 struct VISIBILITY_HIDDEN LinuxAsmPrinter : public PPCAsmPrinter {
294
295 DwarfWriter DW;
296
297 LinuxAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
298 const TargetAsmInfo *T)
299 : PPCAsmPrinter(O, TM, T), DW(O, this, T) {
300 }
301
302 virtual const char *getPassName() const {
303 return "Linux PPC Assembly Printer";
304 }
305
306 bool runOnMachineFunction(MachineFunction &F);
307 bool doInitialization(Module &M);
308 bool doFinalization(Module &M);
309
310 void getAnalysisUsage(AnalysisUsage &AU) const {
311 AU.setPreservesAll();
312 AU.addRequired<MachineModuleInfo>();
313 PPCAsmPrinter::getAnalysisUsage(AU);
314 }
315
316 /// getSectionForFunction - Return the section that we should emit the
317 /// specified function body into.
318 virtual std::string getSectionForFunction(const Function &F) const;
319 };
320
321 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
322 /// X
323 struct VISIBILITY_HIDDEN DarwinAsmPrinter : public PPCAsmPrinter {
324
325 DwarfWriter DW;
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000326 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
328 DarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM,
329 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 }
336
337 bool runOnMachineFunction(MachineFunction &F);
338 bool doInitialization(Module &M);
339 bool doFinalization(Module &M);
340
341 void getAnalysisUsage(AnalysisUsage &AU) const {
342 AU.setPreservesAll();
343 AU.addRequired<MachineModuleInfo>();
344 PPCAsmPrinter::getAnalysisUsage(AU);
345 }
346
347 /// getSectionForFunction - Return the section that we should emit the
348 /// specified function body into.
349 virtual std::string getSectionForFunction(const Function &F) const;
350 };
351} // end of anonymous namespace
352
353// Include the auto-generated portion of the assembly writer
354#include "PPCGenAsmWriter.inc"
355
356void PPCAsmPrinter::printOp(const MachineOperand &MO) {
357 switch (MO.getType()) {
358 case MachineOperand::MO_Immediate:
359 cerr << "printOp() does not handle immediate values\n";
360 abort();
361 return;
362
363 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000364 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 return;
366 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000367 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000368 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 // FIXME: PIC relocation model
370 return;
371 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000372 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000373 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 return;
375 case MachineOperand::MO_ExternalSymbol:
376 // Computing the address of an external symbol, not calling it.
377 if (TM.getRelocationModel() != Reloc::Static) {
378 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
379 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000380 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 return;
382 }
383 O << TAI->getGlobalPrefix() << MO.getSymbolName();
384 return;
385 case MachineOperand::MO_GlobalAddress: {
386 // Computing the address of a global symbol, not calling it.
387 GlobalValue *GV = MO.getGlobal();
388 std::string Name = Mang->getValueName(GV);
389
390 // External or weakly linked global variables need non-lazily-resolved stubs
391 if (TM.getRelocationModel() != Reloc::Static) {
392 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000393 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000395 printSuffixedName(Name, "$non_lazy_ptr");
Dale Johannesencaf11182008-05-16 20:09:25 +0000396 if (GV->hasExternalWeakLinkage())
397 ExtWeakSymbols.insert(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 return;
399 }
400 }
401 O << Name;
402
403 if (MO.getOffset() > 0)
404 O << "+" << MO.getOffset();
405 else if (MO.getOffset() < 0)
406 O << MO.getOffset();
407
408 if (GV->hasExternalWeakLinkage())
409 ExtWeakSymbols.insert(GV);
410 return;
411 }
412
413 default:
414 O << "<unknown operand type: " << MO.getType() << ">";
415 return;
416 }
417}
418
419/// EmitExternalGlobal - In this case we need to use the indirect symbol.
420///
421void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
422 std::string Name = getGlobalLinkName(GV);
423 if (TM.getRelocationModel() != Reloc::Static) {
424 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000425 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 return;
427 }
428 O << Name;
429}
430
431/// PrintAsmOperand - Print out an operand for an inline asm expression.
432///
433bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
434 unsigned AsmVariant,
435 const char *ExtraCode) {
436 // Does this asm operand have a single letter operand modifier?
437 if (ExtraCode && ExtraCode[0]) {
438 if (ExtraCode[1] != 0) return true; // Unknown modifier.
439
440 switch (ExtraCode[0]) {
441 default: return true; // Unknown modifier.
442 case 'c': // Don't print "$" before a global var name or constant.
443 // PPC never has a prefix.
444 printOperand(MI, OpNo);
445 return false;
446 case 'L': // Write second word of DImode reference.
447 // Verify that this operand has two consecutive registers.
448 if (!MI->getOperand(OpNo).isRegister() ||
449 OpNo+1 == MI->getNumOperands() ||
450 !MI->getOperand(OpNo+1).isRegister())
451 return true;
452 ++OpNo; // Return the high-part.
453 break;
454 case 'I':
455 // Write 'i' if an integer constant, otherwise nothing. Used to print
456 // addi vs add, etc.
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000457 if (MI->getOperand(OpNo).isImmediate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 O << "i";
459 return false;
460 }
461 }
462
463 printOperand(MI, OpNo);
464 return false;
465}
466
467bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
468 unsigned AsmVariant,
469 const char *ExtraCode) {
470 if (ExtraCode && ExtraCode[0])
471 return true; // Unknown modifier.
472 if (MI->getOperand(OpNo).isRegister())
473 printMemRegReg(MI, OpNo);
474 else
475 printMemRegImm(MI, OpNo);
476 return false;
477}
478
479void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
480 const char *Modifier) {
481 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
482 unsigned Code = MI->getOperand(OpNo).getImm();
483 if (!strcmp(Modifier, "cc")) {
484 switch ((PPC::Predicate)Code) {
485 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
486 case PPC::PRED_LT: O << "lt"; return;
487 case PPC::PRED_LE: O << "le"; return;
488 case PPC::PRED_EQ: O << "eq"; return;
489 case PPC::PRED_GE: O << "ge"; return;
490 case PPC::PRED_GT: O << "gt"; return;
491 case PPC::PRED_NE: O << "ne"; return;
492 case PPC::PRED_UN: O << "un"; return;
493 case PPC::PRED_NU: O << "nu"; return;
494 }
495
496 } else {
497 assert(!strcmp(Modifier, "reg") &&
498 "Need to specify 'cc' or 'reg' as predicate op modifier!");
499 // Don't print the register for 'always'.
500 if (Code == PPC::PRED_ALWAYS) return;
501 printOperand(MI, OpNo+1);
502 }
503}
504
505
506/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
507/// the current output stream.
508///
509void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
510 ++EmittedInsts;
511
512 // Check for slwi/srwi mnemonics.
513 if (MI->getOpcode() == PPC::RLWINM) {
514 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000515 unsigned char SH = MI->getOperand(2).getImm();
516 unsigned char MB = MI->getOperand(3).getImm();
517 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000519 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 }
521 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000522 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 SH = 32-SH;
524 }
525 if (FoundMnemonic) {
526 printOperand(MI, 0);
527 O << ", ";
528 printOperand(MI, 1);
529 O << ", " << (unsigned int)SH << "\n";
530 return;
531 }
532 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
533 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000534 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 printOperand(MI, 0);
536 O << ", ";
537 printOperand(MI, 1);
538 O << "\n";
539 return;
540 }
541 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000542 unsigned char SH = MI->getOperand(2).getImm();
543 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
545 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000546 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 printOperand(MI, 0);
548 O << ", ";
549 printOperand(MI, 1);
550 O << ", " << (unsigned int)SH << "\n";
551 return;
552 }
553 }
554
555 if (printInstruction(MI))
556 return; // Printer was automatically generated
557
558 assert(0 && "Unhandled instruction in asm writer!");
559 abort();
560 return;
561}
562
563/// runOnMachineFunction - This uses the printMachineInstruction()
564/// method to print assembly for each instruction.
565///
566bool LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
567 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
568
569 SetupMachineFunction(MF);
570 O << "\n\n";
571
572 // Print out constants referenced by the function
573 EmitConstantPool(MF.getConstantPool());
574
575 // Print out labels for the function.
576 const Function *F = MF.getFunction();
577 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
578
579 switch (F->getLinkage()) {
580 default: assert(0 && "Unknown linkage type!");
581 case Function::InternalLinkage: // Symbols default to internal.
582 break;
583 case Function::ExternalLinkage:
584 O << "\t.global\t" << CurrentFnName << '\n'
585 << "\t.type\t" << CurrentFnName << ", @function\n";
586 break;
587 case Function::WeakLinkage:
588 case Function::LinkOnceLinkage:
589 O << "\t.global\t" << CurrentFnName << '\n';
590 O << "\t.weak\t" << CurrentFnName << '\n';
591 break;
592 }
593
594 if (F->hasHiddenVisibility())
595 if (const char *Directive = TAI->getHiddenDirective())
596 O << Directive << CurrentFnName << "\n";
597
598 EmitAlignment(2, F);
599 O << CurrentFnName << ":\n";
600
601 // Emit pre-function debug information.
602 DW.BeginFunction(&MF);
603
604 // Print out code for the function.
605 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
606 I != E; ++I) {
607 // Print a label for the basic block.
608 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000609 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 O << '\n';
611 }
612 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
613 II != E; ++II) {
614 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 printMachineInstruction(II);
616 }
617 }
618
619 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
620
621 // Print out jump tables referenced by the function.
622 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
623
624 // Emit post-function debug information.
625 DW.EndFunction();
626
627 // We didn't modify anything.
628 return false;
629}
630
631bool LinuxAsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000632 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633
634 // GNU as handles section names wrapped in quotes
635 Mang->setUseQuotes(true);
636
637 SwitchToTextSection(TAI->getTextSection());
638
639 // Emit initial debug information.
640 DW.BeginModule(&M);
Dan Gohman4a558a32007-07-25 19:33:14 +0000641 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642}
643
Chris Lattner2b638a72008-02-15 19:04:54 +0000644/// PrintUnmangledNameSafely - Print out the printable characters in the name.
645/// Don't print things like \n or \0.
646static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
647 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
648 Name != E; ++Name)
649 if (isprint(*Name))
650 OS << *Name;
651}
652
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653bool LinuxAsmPrinter::doFinalization(Module &M) {
654 const TargetData *TD = TM.getTargetData();
655
656 // Print out module-level global variables here.
657 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
658 I != E; ++I) {
659 if (!I->hasInitializer()) continue; // External global require no code
660
661 // Check to see if this is a special global used by LLVM, if so, emit it.
662 if (EmitSpecialLLVMGlobal(I))
663 continue;
664
665 std::string name = Mang->getValueName(I);
666
667 if (I->hasHiddenVisibility())
668 if (const char *Directive = TAI->getHiddenDirective())
669 O << Directive << name << "\n";
670
671 Constant *C = I->getInitializer();
Duncan Sands8157ef42007-11-05 00:04:43 +0000672 unsigned Size = TD->getABITypeSize(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 unsigned Align = TD->getPreferredAlignmentLog(I);
674
675 if (C->isNullValue() && /* FIXME: Verify correct */
Dale Johannesen49c44122008-05-14 20:12:51 +0000676 !I->hasSection() && (I->hasCommonLinkage() ||
677 I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng65c0fbc2007-09-21 00:41:19 +0000678 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
680 if (I->hasExternalLinkage()) {
681 O << "\t.global " << name << '\n';
682 O << "\t.type " << name << ", @object\n";
Nick Lewyckyc6583752007-11-04 17:32:10 +0000683 if (TAI->getBSSSection())
684 SwitchToDataSection(TAI->getBSSSection(), I);
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000685 O << name << ":\n";
686 O << "\t.zero " << Size << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 } else if (I->hasInternalLinkage()) {
688 SwitchToDataSection("\t.data", I);
689 O << TAI->getLCOMMDirective() << name << "," << Size;
690 } else {
691 SwitchToDataSection("\t.data", I);
692 O << ".comm " << name << "," << Size;
693 }
Chris Lattner2b638a72008-02-15 19:04:54 +0000694 O << "\t\t" << TAI->getCommentString() << " '";
695 PrintUnmangledNameSafely(I, O);
696 O << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 } else {
698 switch (I->getLinkage()) {
699 case GlobalValue::LinkOnceLinkage:
700 case GlobalValue::WeakLinkage:
Dale Johannesen49c44122008-05-14 20:12:51 +0000701 case GlobalValue::CommonLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 O << "\t.global " << name << '\n'
703 << "\t.type " << name << ", @object\n"
704 << "\t.weak " << name << '\n';
705 SwitchToDataSection("\t.data", I);
706 break;
707 case GlobalValue::AppendingLinkage:
708 // FIXME: appending linkage variables should go into a section of
709 // their name or something. For now, just emit them as external.
710 case GlobalValue::ExternalLinkage:
711 // If external or appending, declare as a global symbol
712 O << "\t.global " << name << "\n"
713 << "\t.type " << name << ", @object\n";
714 // FALL THROUGH
715 case GlobalValue::InternalLinkage:
716 if (I->isConstant()) {
717 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
718 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
719 SwitchToDataSection(TAI->getCStringSection(), I);
720 break;
721 }
722 }
723
724 // FIXME: special handling for ".ctors" & ".dtors" sections
725 if (I->hasSection() &&
726 (I->getSection() == ".ctors" ||
727 I->getSection() == ".dtors")) {
728 std::string SectionName = ".section " + I->getSection()
729 + ",\"aw\",@progbits";
730 SwitchToDataSection(SectionName.c_str());
731 } else {
732 if (I->isConstant() && TAI->getReadOnlySection())
733 SwitchToDataSection(TAI->getReadOnlySection(), I);
734 else
735 SwitchToDataSection(TAI->getDataSection(), I);
736 }
737 break;
738 default:
739 cerr << "Unknown linkage type!";
740 abort();
741 }
742
743 EmitAlignment(Align, I);
Chris Lattner2b638a72008-02-15 19:04:54 +0000744 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
745 PrintUnmangledNameSafely(I, O);
746 O << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747
748 // If the initializer is a extern weak symbol, remember to emit the weak
749 // reference!
750 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
751 if (GV->hasExternalWeakLinkage())
752 ExtWeakSymbols.insert(GV);
753
754 EmitGlobalConstant(C);
755 O << '\n';
756 }
757 }
758
759 // TODO
760
761 // Emit initial debug information.
762 DW.EndModule();
763
Dan Gohman4a558a32007-07-25 19:33:14 +0000764 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765}
766
767std::string LinuxAsmPrinter::getSectionForFunction(const Function &F) const {
768 switch (F.getLinkage()) {
769 default: assert(0 && "Unknown linkage type!");
770 case Function::ExternalLinkage:
771 case Function::InternalLinkage: return TAI->getTextSection();
772 case Function::WeakLinkage:
773 case Function::LinkOnceLinkage:
774 return ".text";
775 }
776}
777
778std::string DarwinAsmPrinter::getSectionForFunction(const Function &F) const {
779 switch (F.getLinkage()) {
780 default: assert(0 && "Unknown linkage type!");
781 case Function::ExternalLinkage:
782 case Function::InternalLinkage: return TAI->getTextSection();
783 case Function::WeakLinkage:
784 case Function::LinkOnceLinkage:
Dale Johannesen3c788322008-01-11 00:54:37 +0000785 return "\t.section __TEXT,__textcoal_nt,coalesced,pure_instructions";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 }
787}
788
789/// runOnMachineFunction - This uses the printMachineInstruction()
790/// method to print assembly for each instruction.
791///
792bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793
794 SetupMachineFunction(MF);
795 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000796
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 // Print out constants referenced by the function
798 EmitConstantPool(MF.getConstantPool());
799
800 // Print out labels for the function.
801 const Function *F = MF.getFunction();
802 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
803
804 switch (F->getLinkage()) {
805 default: assert(0 && "Unknown linkage type!");
806 case Function::InternalLinkage: // Symbols default to internal.
807 break;
808 case Function::ExternalLinkage:
809 O << "\t.globl\t" << CurrentFnName << "\n";
810 break;
811 case Function::WeakLinkage:
812 case Function::LinkOnceLinkage:
813 O << "\t.globl\t" << CurrentFnName << "\n";
814 O << "\t.weak_definition\t" << CurrentFnName << "\n";
815 break;
816 }
817
818 if (F->hasHiddenVisibility())
819 if (const char *Directive = TAI->getHiddenDirective())
820 O << Directive << CurrentFnName << "\n";
821
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000822 EmitAlignment(OptimizeForSize ? 2 : 4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 O << CurrentFnName << ":\n";
824
825 // Emit pre-function debug information.
826 DW.BeginFunction(&MF);
827
Bill Wendling36ccaea2008-01-26 06:51:24 +0000828 // If the function is empty, then we need to emit *something*. Otherwise, the
829 // function's label might be associated with something that it wasn't meant to
830 // be associated with. We emit a noop in this situation.
831 MachineFunction::iterator I = MF.begin();
832
Bill Wendlingb5880a72008-01-26 09:03:52 +0000833 if (++I == MF.end() && MF.front().empty())
834 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000835
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 // Print out code for the function.
837 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
838 I != E; ++I) {
839 // Print a label for the basic block.
840 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000841 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 O << '\n';
843 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000844 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
845 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 printMachineInstruction(II);
848 }
849 }
850
851 // Print out jump tables referenced by the function.
852 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
853
854 // Emit post-function debug information.
855 DW.EndFunction();
856
857 // We didn't modify anything.
858 return false;
859}
860
861
862bool DarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000863 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000864 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 "ppc",
866 "ppc601",
867 "ppc602",
868 "ppc603",
869 "ppc7400",
870 "ppc750",
871 "ppc970",
872 "ppc64"
873 };
874
875 unsigned Directive = Subtarget.getDarwinDirective();
876 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
877 Directive = PPC::DIR_970;
878 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
879 Directive = PPC::DIR_7400;
880 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
881 Directive = PPC::DIR_64;
882 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
883 O << "\t.machine " << CPUDirectives[Directive] << "\n";
884
Dan Gohman4a558a32007-07-25 19:33:14 +0000885 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000887 // Emit initial debug information.
888 DW.BeginModule(&M);
889
890 // We need this for Personality functions.
891 // AsmPrinter::doInitialization should have done this analysis.
892 MMI = getAnalysisToUpdate<MachineModuleInfo>();
893 assert(MMI);
894 DW.SetModuleInfo(MMI);
895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 // Darwin wants symbols to be quoted if they have complex names.
897 Mang->setUseQuotes(true);
898
899 // Prime text sections so they are adjacent. This reduces the likelihood a
900 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000901 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 "pure_instructions");
903 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000904 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 "pure_instructions,32");
906 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000907 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 "pure_instructions,16");
909 }
910 SwitchToTextSection(TAI->getTextSection());
911
Dan Gohman4a558a32007-07-25 19:33:14 +0000912 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913}
914
915bool DarwinAsmPrinter::doFinalization(Module &M) {
916 const TargetData *TD = TM.getTargetData();
917
918 // Print out module-level global variables here.
919 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
920 I != E; ++I) {
921 if (!I->hasInitializer()) continue; // External global require no code
922
923 // Check to see if this is a special global used by LLVM, if so, emit it.
924 if (EmitSpecialLLVMGlobal(I)) {
925 if (TM.getRelocationModel() == Reloc::Static) {
926 if (I->getName() == "llvm.global_ctors")
927 O << ".reference .constructors_used\n";
928 else if (I->getName() == "llvm.global_dtors")
929 O << ".reference .destructors_used\n";
930 }
931 continue;
932 }
933
934 std::string name = Mang->getValueName(I);
935
936 if (I->hasHiddenVisibility())
937 if (const char *Directive = TAI->getHiddenDirective())
938 O << Directive << name << "\n";
939
940 Constant *C = I->getInitializer();
941 const Type *Type = C->getType();
Duncan Sands8157ef42007-11-05 00:04:43 +0000942 unsigned Size = TD->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 unsigned Align = TD->getPreferredAlignmentLog(I);
944
945 if (C->isNullValue() && /* FIXME: Verify correct */
Dale Johannesen49c44122008-05-14 20:12:51 +0000946 !I->hasSection() && (I->hasCommonLinkage() ||
947 I->hasInternalLinkage() || I->hasWeakLinkage() ||
Dale Johannesen50085da2008-01-17 23:04:07 +0000948 I->hasLinkOnceLinkage() || I->hasExternalLinkage())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
950 if (I->hasExternalLinkage()) {
951 O << "\t.globl " << name << '\n';
952 O << "\t.zerofill __DATA, __common, " << name << ", "
953 << Size << ", " << Align;
954 } else if (I->hasInternalLinkage()) {
955 SwitchToDataSection("\t.data", I);
956 O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align;
Dale Johannesencaf11182008-05-16 20:09:25 +0000957 } else if (!I->hasCommonLinkage()) {
958 O << "\t.globl " << name << "\n"
959 << TAI->getWeakDefDirective() << name << "\n";
960 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
961 EmitAlignment(Align, I);
962 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
963 PrintUnmangledNameSafely(I, O);
964 O << "\n";
965 EmitGlobalConstant(C);
966 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 } else {
968 SwitchToDataSection("\t.data", I);
969 O << ".comm " << name << "," << Size;
Chris Lattner9b7677d2008-01-02 19:35:16 +0000970 // Darwin 9 and above support aligned common data.
971 if (Subtarget.isDarwin9())
972 O << "," << Align;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 }
Chris Lattner2b638a72008-02-15 19:04:54 +0000974 O << "\t\t" << TAI->getCommentString() << " '";
975 PrintUnmangledNameSafely(I, O);
976 O << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 } else {
978 switch (I->getLinkage()) {
979 case GlobalValue::LinkOnceLinkage:
980 case GlobalValue::WeakLinkage:
Dale Johannesen49c44122008-05-14 20:12:51 +0000981 case GlobalValue::CommonLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 O << "\t.globl " << name << '\n'
983 << "\t.weak_definition " << name << '\n';
Dale Johannesen1c365122008-05-24 00:10:20 +0000984 if (!I->isConstant())
985 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
986 else {
987 const ArrayType *AT = dyn_cast<ArrayType>(Type);
988 if (AT && AT->getElementType()==Type::Int8Ty)
989 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", I);
990 else
991 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
992 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 break;
994 case GlobalValue::AppendingLinkage:
995 // FIXME: appending linkage variables should go into a section of
996 // their name or something. For now, just emit them as external.
997 case GlobalValue::ExternalLinkage:
998 // If external or appending, declare as a global symbol
999 O << "\t.globl " << name << "\n";
1000 // FALL THROUGH
1001 case GlobalValue::InternalLinkage:
1002 if (I->isConstant()) {
1003 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
1004 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
1005 SwitchToDataSection(TAI->getCStringSection(), I);
1006 break;
1007 }
1008 }
Dale Johannesenae4f62f2008-01-23 00:58:14 +00001009 if (I->hasSection()) {
1010 // Honor all section names on Darwin; ObjC uses this
1011 std::string SectionName = ".section " + I->getSection();
1012 SwitchToDataSection(SectionName.c_str());
1013 } else if (!I->isConstant())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 SwitchToDataSection(TAI->getDataSection(), I);
1015 else {
1016 // Read-only data.
1017 bool HasReloc = C->ContainsRelocations();
1018 if (HasReloc &&
1019 TM.getRelocationModel() != Reloc::Static)
1020 SwitchToDataSection("\t.const_data\n");
1021 else if (!HasReloc && Size == 4 &&
1022 TAI->getFourByteConstantSection())
1023 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
1024 else if (!HasReloc && Size == 8 &&
1025 TAI->getEightByteConstantSection())
1026 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
1027 else if (!HasReloc && Size == 16 &&
1028 TAI->getSixteenByteConstantSection())
1029 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
1030 else if (TAI->getReadOnlySection())
1031 SwitchToDataSection(TAI->getReadOnlySection(), I);
1032 else
1033 SwitchToDataSection(TAI->getDataSection(), I);
1034 }
1035 break;
1036 default:
1037 cerr << "Unknown linkage type!";
1038 abort();
1039 }
1040
1041 EmitAlignment(Align, I);
Chris Lattner2b638a72008-02-15 19:04:54 +00001042 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
1043 PrintUnmangledNameSafely(I, O);
1044 O << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045
1046 // If the initializer is a extern weak symbol, remember to emit the weak
1047 // reference!
1048 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1049 if (GV->hasExternalWeakLinkage())
1050 ExtWeakSymbols.insert(GV);
1051
1052 EmitGlobalConstant(C);
1053 O << '\n';
1054 }
1055 }
1056
1057 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1058
1059 // Output stubs for dynamically-linked functions
1060 if (TM.getRelocationModel() == Reloc::PIC_) {
1061 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1062 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001063 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 "pure_instructions,32");
1065 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +00001066 std::string p = *i;
1067 std::string L0p = (p[0]=='\"') ? "\"L0$" + p.substr(1) : "L0$" + p ;
1068 printSuffixedName(p, "$stub");
1069 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070 O << "\t.indirect_symbol " << *i << "\n";
1071 O << "\tmflr r0\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001072 O << "\tbcl 20,31," << L0p << "\n";
1073 O << L0p << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 O << "\tmflr r11\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001075 O << "\taddis r11,r11,ha16(";
1076 printSuffixedName(p, "$lazy_ptr");
1077 O << "-" << L0p << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 O << "\tmtlr r0\n";
1079 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001080 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001082 O << "\tlwzu r12,lo16(";
1083 printSuffixedName(p, "$lazy_ptr");
1084 O << "-" << L0p << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 O << "\tmtctr r12\n";
1086 O << "\tbctr\n";
1087 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001088 printSuffixedName(p, "$lazy_ptr");
1089 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 O << "\t.indirect_symbol " << *i << "\n";
1091 if (isPPC64)
1092 O << "\t.quad dyld_stub_binding_helper\n";
1093 else
1094 O << "\t.long dyld_stub_binding_helper\n";
1095 }
1096 } else {
1097 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
1098 i != e; ++i) {
Dale Johannesen3c788322008-01-11 00:54:37 +00001099 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 "pure_instructions,16");
1101 EmitAlignment(4);
Dale Johannesena21b5202008-05-19 21:38:18 +00001102 std::string p = *i;
1103 printSuffixedName(p, "$stub");
1104 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 O << "\t.indirect_symbol " << *i << "\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001106 O << "\tlis r11,ha16(";
1107 printSuffixedName(p, "$lazy_ptr");
1108 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109 if (isPPC64)
Dale Johannesena21b5202008-05-19 21:38:18 +00001110 O << "\tldu r12,lo16(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 else
Dale Johannesena21b5202008-05-19 21:38:18 +00001112 O << "\tlwzu r12,lo16(";
1113 printSuffixedName(p, "$lazy_ptr");
1114 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 O << "\tmtctr r12\n";
1116 O << "\tbctr\n";
1117 SwitchToDataSection(".lazy_symbol_pointer");
Dale Johannesena21b5202008-05-19 21:38:18 +00001118 printSuffixedName(p, "$lazy_ptr");
1119 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001120 O << "\t.indirect_symbol " << *i << "\n";
1121 if (isPPC64)
1122 O << "\t.quad dyld_stub_binding_helper\n";
1123 else
1124 O << "\t.long dyld_stub_binding_helper\n";
1125 }
1126 }
1127
1128 O << "\n";
1129
Dale Johannesen85535762008-04-02 00:25:04 +00001130 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001131 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001132 // Only referenced functions get into the Personalities list.
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001133 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1134
1135 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1136 E = Personalities.end(); I != E; ++I)
1137 if (*I) GVStubs.insert("_" + (*I)->getName());
1138 }
1139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001141 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142 SwitchToDataSection(".non_lazy_symbol_pointer");
1143 for (std::set<std::string>::iterator I = GVStubs.begin(),
1144 E = GVStubs.end(); I != E; ++I) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001145 std::string p = *I;
1146 printSuffixedName(p, "$non_lazy_ptr");
1147 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 O << "\t.indirect_symbol " << *I << "\n";
1149 if (isPPC64)
1150 O << "\t.quad\t0\n";
1151 else
1152 O << "\t.long\t0\n";
1153
1154 }
1155 }
1156
1157 // Emit initial debug information.
1158 DW.EndModule();
1159
1160 // Funny Darwin hack: This flag tells the linker that no global symbols
1161 // contain code that falls through to other global symbols (e.g. the obvious
1162 // implementation of multiple entry points). If this doesn't occur, the
1163 // linker can safely perform dead code stripping. Since LLVM never generates
1164 // code that does this, it is always safe to set.
1165 O << "\t.subsections_via_symbols\n";
1166
Dan Gohman4a558a32007-07-25 19:33:14 +00001167 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168}
1169
1170
1171
1172/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1173/// for a MachineFunction to the given output stream, in a format that the
1174/// Darwin assembler can deal with.
1175///
1176FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o,
1177 PPCTargetMachine &tm) {
1178 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1179
1180 if (Subtarget->isDarwin()) {
1181 return new DarwinAsmPrinter(o, tm, tm.getTargetAsmInfo());
1182 } else {
1183 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
1184 }
1185}
1186