blob: 817b746ec11d9cdbec28bb863c07140e7108f837 [file] [log] [blame]
Scott Michel266bc8f2007-12-04 22:23:35 +00001//===-- SPUAsmPrinter.cpp - Print machine instrs to Cell SPU assembly -------=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel266bc8f2007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Cell SPU assembly language. This printer
12// is the output mechanism used by `llc'.
13//
Scott Michel266bc8f2007-12-04 22:23:35 +000014//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asmprinter"
17#include "SPU.h"
18#include "SPUTargetMachine.h"
19#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
22#include "llvm/Assembly/Writer.h"
23#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/DwarfWriter.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/Support/Mangler.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000035#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetOptions.h"
37#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/StringExtras.h"
39#include <set>
40using namespace llvm;
41
42namespace {
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45 const std::string bss_section(".bss");
46
47 struct VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
48 std::set<std::string> FnStubs, GVStubs;
49
50 SPUAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T) :
51 AsmPrinter(O, TM, T)
52 {
53 }
54
55 virtual const char *getPassName() const {
56 return "STI CBEA SPU Assembly Printer";
57 }
58
59 SPUTargetMachine &getTM() {
60 return static_cast<SPUTargetMachine&>(TM);
61 }
62
63 /// printInstruction - This method is automatically generated by tablegen
64 /// from the instruction set description. This method returns true if the
65 /// machine instruction was sufficiently described to print it, otherwise it
66 /// returns false.
67 bool printInstruction(const MachineInstr *MI);
68
69 void printMachineInstruction(const MachineInstr *MI);
70 void printOp(const MachineOperand &MO);
71
72 /// printRegister - Print register according to target requirements.
73 ///
74 void printRegister(const MachineOperand &MO, bool R0AsZero) {
75 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000076 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
77 "Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000078 O << TM.getRegisterInfo()->get(RegNo).AsmName;
Scott Michel266bc8f2007-12-04 22:23:35 +000079 }
80
81 void printOperand(const MachineInstr *MI, unsigned OpNo) {
82 const MachineOperand &MO = MI->getOperand(OpNo);
83 if (MO.isRegister()) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000084 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000085 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Scott Michel266bc8f2007-12-04 22:23:35 +000086 } else if (MO.isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000087 O << MO.getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +000088 } else {
89 printOp(MO);
90 }
91 }
92
93 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
94 unsigned AsmVariant, const char *ExtraCode);
95 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
96 unsigned AsmVariant, const char *ExtraCode);
97
98
99 void
100 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
101 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000102 int value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000103 value = (value << (32 - 7)) >> (32 - 7);
104
105 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000106 && "Invalid s7 argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000107 O << value;
108 }
109
110 void
111 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
112 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000113 unsigned int value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000114 assert(value < (1 << 8) && "Invalid u7 argument");
115 O << value;
116 }
117
118 void
119 printMemRegImmS7(const MachineInstr *MI, unsigned OpNo)
120 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000121 char value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000122 O << (int) value;
123 O << "(";
124 printOperand(MI, OpNo+1);
125 O << ")";
126 }
127
128 void
129 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
130 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000131 O << (short) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000132 }
133
134 void
135 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
136 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000137 O << (unsigned short)MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000138 }
139
140 void
141 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
142 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000143 O << (unsigned)MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000144 }
145
146 void
147 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
148 // When used as the base register, r0 reads constant zero rather than
149 // the value contained in the register. For this reason, the darwin
150 // assembler requires that we print r0 as 0 (no r) when used as the base.
151 const MachineOperand &MO = MI->getOperand(OpNo);
Bill Wendling74ab84c2008-02-26 21:11:01 +0000152 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Scott Michel266bc8f2007-12-04 22:23:35 +0000153 O << ", ";
154 printOperand(MI, OpNo+1);
155 }
156
157 void
158 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
159 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000160 unsigned int value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000161 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
162 O << value;
163 }
164
165 void
166 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
167 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000168 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
Scott Michel266bc8f2007-12-04 22:23:35 +0000169 >> 16);
170 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
171 && "Invalid s10 argument");
172 O << value;
173 }
174
175 void
176 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
177 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000178 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
Scott Michel266bc8f2007-12-04 22:23:35 +0000179 >> 16);
180 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
181 O << value;
182 }
183
184 void
185 printMemRegImmS10(const MachineInstr *MI, unsigned OpNo)
186 {
187 const MachineOperand &MO = MI->getOperand(OpNo);
188 assert(MO.isImmediate()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000189 && "printMemRegImmS10 first operand is not immedate");
Scott Michel266bc8f2007-12-04 22:23:35 +0000190 printS10ImmOperand(MI, OpNo);
191 O << "(";
192 printOperand(MI, OpNo+1);
193 O << ")";
194 }
195
196 void
197 printAddr256K(const MachineInstr *MI, unsigned OpNo)
198 {
Scott Michel053c1da2008-01-29 02:16:57 +0000199 /* Note: operand 1 is an offset or symbol name. */
Scott Michel266bc8f2007-12-04 22:23:35 +0000200 if (MI->getOperand(OpNo).isImmediate()) {
201 printS16ImmOperand(MI, OpNo);
202 } else {
203 printOp(MI->getOperand(OpNo));
Scott Michel053c1da2008-01-29 02:16:57 +0000204 if (MI->getOperand(OpNo+1).isImmediate()) {
205 int displ = int(MI->getOperand(OpNo+1).getImm());
206 if (displ > 0)
207 O << "+" << displ;
208 else if (displ < 0)
209 O << displ;
210 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000211 }
212 }
213
214 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
215 printOp(MI->getOperand(OpNo));
216 }
217
218 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
219 printOp(MI->getOperand(OpNo));
220 O << "-.";
221 }
222
223 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
224 if (MI->getOperand(OpNo).isImmediate()) {
225 printS16ImmOperand(MI, OpNo);
226 } else {
227 printOp(MI->getOperand(OpNo));
228 O << "@h";
229 }
230 }
231
232 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
233 if (MI->getOperand(OpNo).isImmediate()) {
234 printS16ImmOperand(MI, OpNo);
235 } else {
236 printOp(MI->getOperand(OpNo));
237 O << "@l";
238 }
239 }
240
241 /// Print local store address
242 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
243 printOp(MI->getOperand(OpNo));
244 }
245
246 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
247 if (MI->getOperand(OpNo).isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000248 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000249 assert((value >= 0 && value < 16)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000250 && "Invalid negated immediate rotate 7-bit argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000251 O << -value;
252 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000253 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel266bc8f2007-12-04 22:23:35 +0000254 }
255 }
256
257 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
258 if (MI->getOperand(OpNo).isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000259 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000260 assert((value >= 0 && value < 32)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000261 && "Invalid negated immediate rotate 7-bit argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000262 O << -value;
263 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000264 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel266bc8f2007-12-04 22:23:35 +0000265 }
266 }
267
268 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
269 virtual bool doFinalization(Module &M) = 0;
270 };
271
272 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
273 struct VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
274
275 DwarfWriter DW;
Dale Johannesend0377242008-07-09 21:25:06 +0000276 MachineModuleInfo *MMI;
Scott Michel266bc8f2007-12-04 22:23:35 +0000277
278 LinuxAsmPrinter(std::ostream &O, SPUTargetMachine &TM,
279 const TargetAsmInfo *T) :
280 SPUAsmPrinter(O, TM, T),
Dale Johannesend0377242008-07-09 21:25:06 +0000281 DW(O, this, T),
282 MMI(0)
Scott Michel266bc8f2007-12-04 22:23:35 +0000283 { }
284
285 virtual const char *getPassName() const {
286 return "STI CBEA SPU Assembly Printer";
287 }
288
289 bool runOnMachineFunction(MachineFunction &F);
290 bool doInitialization(Module &M);
291 bool doFinalization(Module &M);
292
293 void getAnalysisUsage(AnalysisUsage &AU) const {
294 AU.setPreservesAll();
295 AU.addRequired<MachineModuleInfo>();
296 SPUAsmPrinter::getAnalysisUsage(AU);
297 }
298
299 /// getSectionForFunction - Return the section that we should emit the
300 /// specified function body into.
301 virtual std::string getSectionForFunction(const Function &F) const;
302 };
303} // end of anonymous namespace
304
305// Include the auto-generated portion of the assembly writer
306#include "SPUGenAsmWriter.inc"
307
308void SPUAsmPrinter::printOp(const MachineOperand &MO) {
309 switch (MO.getType()) {
310 case MachineOperand::MO_Immediate:
311 cerr << "printOp() does not handle immediate values\n";
312 abort();
313 return;
314
315 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000316 printBasicBlockLabel(MO.getMBB());
Scott Michel266bc8f2007-12-04 22:23:35 +0000317 return;
318 case MachineOperand::MO_JumpTableIndex:
319 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000320 << '_' << MO.getIndex();
Scott Michel266bc8f2007-12-04 22:23:35 +0000321 return;
322 case MachineOperand::MO_ConstantPoolIndex:
323 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000324 << '_' << MO.getIndex();
Scott Michel266bc8f2007-12-04 22:23:35 +0000325 return;
326 case MachineOperand::MO_ExternalSymbol:
327 // Computing the address of an external symbol, not calling it.
328 if (TM.getRelocationModel() != Reloc::Static) {
329 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
330 GVStubs.insert(Name);
331 O << "L" << Name << "$non_lazy_ptr";
332 return;
333 }
334 O << TAI->getGlobalPrefix() << MO.getSymbolName();
335 return;
336 case MachineOperand::MO_GlobalAddress: {
337 // Computing the address of a global symbol, not calling it.
338 GlobalValue *GV = MO.getGlobal();
339 std::string Name = Mang->getValueName(GV);
340
341 // External or weakly linked global variables need non-lazily-resolved
342 // stubs
343 if (TM.getRelocationModel() != Reloc::Static) {
344 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000345 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000346 GVStubs.insert(Name);
347 O << "L" << Name << "$non_lazy_ptr";
348 return;
349 }
350 }
351 O << Name;
352
353 if (GV->hasExternalWeakLinkage())
354 ExtWeakSymbols.insert(GV);
355 return;
356 }
357
358 default:
359 O << "<unknown operand type: " << MO.getType() << ">";
360 return;
361 }
362}
363
364/// PrintAsmOperand - Print out an operand for an inline asm expression.
365///
366bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
367 unsigned AsmVariant,
368 const char *ExtraCode) {
369 // Does this asm operand have a single letter operand modifier?
370 if (ExtraCode && ExtraCode[0]) {
371 if (ExtraCode[1] != 0) return true; // Unknown modifier.
372
373 switch (ExtraCode[0]) {
374 default: return true; // Unknown modifier.
375 case 'L': // Write second word of DImode reference.
376 // Verify that this operand has two consecutive registers.
377 if (!MI->getOperand(OpNo).isRegister() ||
378 OpNo+1 == MI->getNumOperands() ||
379 !MI->getOperand(OpNo+1).isRegister())
380 return true;
381 ++OpNo; // Return the high-part.
382 break;
383 }
384 }
385
386 printOperand(MI, OpNo);
387 return false;
388}
389
390bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000391 unsigned OpNo,
Scott Michel266bc8f2007-12-04 22:23:35 +0000392 unsigned AsmVariant,
393 const char *ExtraCode) {
394 if (ExtraCode && ExtraCode[0])
395 return true; // Unknown modifier.
396 printMemRegReg(MI, OpNo);
397 return false;
398}
399
400/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
401/// to the current output stream.
402///
403void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
404 ++EmittedInsts;
405 printInstruction(MI);
406}
407
408
409
410std::string LinuxAsmPrinter::getSectionForFunction(const Function &F) const {
411 switch (F.getLinkage()) {
412 default: assert(0 && "Unknown linkage type!");
413 case Function::ExternalLinkage:
414 case Function::InternalLinkage: return TAI->getTextSection();
415 case Function::WeakLinkage:
416 case Function::LinkOnceLinkage:
417 return ""; // Print nothing for the time being...
418 }
419}
420
421/// runOnMachineFunction - This uses the printMachineInstruction()
422/// method to print assembly for each instruction.
423///
424bool
425LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
426{
Scott Michel266bc8f2007-12-04 22:23:35 +0000427 SetupMachineFunction(MF);
428 O << "\n\n";
429
430 // Print out constants referenced by the function
431 EmitConstantPool(MF.getConstantPool());
432
433 // Print out labels for the function.
434 const Function *F = MF.getFunction();
435
436 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
437 EmitAlignment(3, F);
438
439 switch (F->getLinkage()) {
440 default: assert(0 && "Unknown linkage type!");
441 case Function::InternalLinkage: // Symbols default to internal.
442 break;
443 case Function::ExternalLinkage:
444 O << "\t.global\t" << CurrentFnName << "\n"
445 << "\t.type\t" << CurrentFnName << ", @function\n";
446 break;
447 case Function::WeakLinkage:
448 case Function::LinkOnceLinkage:
449 O << "\t.global\t" << CurrentFnName << "\n";
450 O << "\t.weak_definition\t" << CurrentFnName << "\n";
451 break;
452 }
453 O << CurrentFnName << ":\n";
454
455 // Emit pre-function debug information.
456 DW.BeginFunction(&MF);
457
458 // Print out code for the function.
459 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
460 I != E; ++I) {
461 // Print a label for the basic block.
462 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000463 printBasicBlockLabel(I, true, true);
Scott Michel266bc8f2007-12-04 22:23:35 +0000464 O << '\n';
465 }
466 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
467 II != E; ++II) {
468 // Print the assembly for the instruction.
Scott Michel266bc8f2007-12-04 22:23:35 +0000469 printMachineInstruction(II);
470 }
471 }
472
473 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
474
475 // Print out jump tables referenced by the function.
476 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
477
478 // Emit post-function debug information.
479 DW.EndFunction();
480
481 // We didn't modify anything.
482 return false;
483}
484
485
486bool LinuxAsmPrinter::doInitialization(Module &M) {
487 bool Result = AsmPrinter::doInitialization(M);
488 SwitchToTextSection(TAI->getTextSection());
489 // Emit initial debug information.
490 DW.BeginModule(&M);
Dale Johannesend0377242008-07-09 21:25:06 +0000491 MMI = getAnalysisToUpdate<MachineModuleInfo>();
492 DW.SetModuleInfo(MMI);
Scott Michel266bc8f2007-12-04 22:23:35 +0000493 return Result;
494}
495
496bool LinuxAsmPrinter::doFinalization(Module &M) {
497 const TargetData *TD = TM.getTargetData();
498
499 // Print out module-level global variables here.
500 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
501 I != E; ++I) {
502 if (!I->hasInitializer()) continue; // External global require no code
503
504 // Check to see if this is a special global used by LLVM, if so, emit it.
505 if (EmitSpecialLLVMGlobal(I))
506 continue;
507
508 std::string name = Mang->getValueName(I);
509 Constant *C = I->getInitializer();
510 unsigned Size = TD->getTypeStoreSize(C->getType());
511 unsigned Align = TD->getPreferredAlignmentLog(I);
512
513 if (C->isNullValue() && /* FIXME: Verify correct */
514 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000515 I->hasLinkOnceLinkage() || I->hasCommonLinkage() ||
Scott Michel266bc8f2007-12-04 22:23:35 +0000516 (I->hasExternalLinkage() && !I->hasSection()))) {
517 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
518 if (I->hasExternalLinkage()) {
519 // External linkage globals -> .bss section
520 // FIXME: Want to set the global variable's section so that
521 // SwitchToDataSection emits the ".section" directive
522 SwitchToDataSection("\t.section\t.bss", I);
523 O << "\t.global\t" << name << '\n';
524 O << "\t.align\t" << Align << '\n';
525 O << "\t.type\t" << name << ", @object\n";
526 O << "\t.size\t" << name << ", " << Size << '\n';
527 O << name << ":\n";
528 O << "\t.zero\t" << Size;
529 } else if (I->hasInternalLinkage()) {
530 SwitchToDataSection("\t.data", I);
Scott Michel053c1da2008-01-29 02:16:57 +0000531 O << ".local " << name << "\n";
532 O << TAI->getCOMMDirective() << name << "," << Size << "," << Align << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000533 } else {
534 SwitchToDataSection("\t.data", I);
535 O << ".comm " << name << "," << Size;
536 }
537 O << "\t\t# '" << I->getName() << "'\n";
538 } else {
539 switch (I->getLinkage()) {
540 case GlobalValue::LinkOnceLinkage:
541 case GlobalValue::WeakLinkage:
Dale Johannesenaafce772008-05-14 20:12:51 +0000542 case GlobalValue::CommonLinkage:
Scott Michel266bc8f2007-12-04 22:23:35 +0000543 O << "\t.global " << name << '\n'
544 << "\t.weak_definition " << name << '\n';
545 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
546 break;
547 case GlobalValue::AppendingLinkage:
548 // FIXME: appending linkage variables should go into a section of
549 // their name or something. For now, just emit them as external.
550 case GlobalValue::ExternalLinkage:
551 // If external or appending, declare as a global symbol
552 O << "\t.global " << name << "\n";
553 // FALL THROUGH
554 case GlobalValue::InternalLinkage:
555 if (I->isConstant()) {
556 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
557 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
558 SwitchToDataSection(TAI->getCStringSection(), I);
559 break;
560 }
561 }
562
563 SwitchToDataSection("\t.data", I);
564 break;
565 default:
566 cerr << "Unknown linkage type!";
567 abort();
568 }
569
570 EmitAlignment(Align, I);
571 O << name << ":\t\t\t\t# '" << I->getName() << "'\n";
572
573 // If the initializer is a extern weak symbol, remember to emit the weak
574 // reference!
575 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
576 if (GV->hasExternalWeakLinkage())
577 ExtWeakSymbols.insert(GV);
578
579 EmitGlobalConstant(C);
580 O << '\n';
581 }
582 }
583
584 // Output stubs for dynamically-linked functions
585 if (TM.getRelocationModel() == Reloc::PIC_) {
586 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
587 i != e; ++i) {
588 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
589 "pure_instructions,32");
590 EmitAlignment(4);
591 O << "L" << *i << "$stub:\n";
592 O << "\t.indirect_symbol " << *i << "\n";
593 O << "\tmflr r0\n";
594 O << "\tbcl 20,31,L0$" << *i << "\n";
595 O << "L0$" << *i << ":\n";
596 O << "\tmflr r11\n";
597 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
598 O << "\tmtlr r0\n";
599 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
600 O << "\tmtctr r12\n";
601 O << "\tbctr\n";
602 SwitchToDataSection(".lazy_symbol_pointer");
603 O << "L" << *i << "$lazy_ptr:\n";
604 O << "\t.indirect_symbol " << *i << "\n";
605 O << "\t.long dyld_stub_binding_helper\n";
606 }
607 } else {
608 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
609 i != e; ++i) {
610 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
611 "pure_instructions,16");
612 EmitAlignment(4);
613 O << "L" << *i << "$stub:\n";
614 O << "\t.indirect_symbol " << *i << "\n";
615 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
616 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
617 O << "\tmtctr r12\n";
618 O << "\tbctr\n";
619 SwitchToDataSection(".lazy_symbol_pointer");
620 O << "L" << *i << "$lazy_ptr:\n";
621 O << "\t.indirect_symbol " << *i << "\n";
622 O << "\t.long dyld_stub_binding_helper\n";
623 }
624 }
625
626 O << "\n";
627
628 // Output stubs for external and common global variables.
629 if (GVStubs.begin() != GVStubs.end()) {
630 SwitchToDataSection(".non_lazy_symbol_pointer");
631 for (std::set<std::string>::iterator I = GVStubs.begin(),
632 E = GVStubs.end(); I != E; ++I) {
633 O << "L" << *I << "$non_lazy_ptr:\n";
634 O << "\t.indirect_symbol " << *I << "\n";
635 O << "\t.long\t0\n";
636 }
637 }
638
639 // Emit initial debug information.
640 DW.EndModule();
641
642 // Emit ident information
Scott Michel86c041f2007-12-20 00:44:13 +0000643 O << "\t.ident\t\"(llvm 2.2+) STI CBEA Cell SPU backend\"\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000644
645 return AsmPrinter::doFinalization(M);
646}
647
648
649
650/// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
651/// assembly code for a MachineFunction to the given output stream, in a format
652/// that the Linux SPU assembler can deal with.
653///
654FunctionPass *llvm::createSPUAsmPrinterPass(std::ostream &o,
655 SPUTargetMachine &tm) {
656 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
657}
658