blob: 8d9e891116285123ceccd9f0ca0250ba290e5e5b [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"
Owen Andersoncb371882008-08-21 00:14:44 +000033#include "llvm/Support/raw_ostream.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000034#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000035#include "llvm/Target/TargetRegisterInfo.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/ADT/Statistic.h"
39#include "llvm/ADT/StringExtras.h"
40#include <set>
41using namespace llvm;
42
43namespace {
44 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
46 const std::string bss_section(".bss");
47
48 struct VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
49 std::set<std::string> FnStubs, GVStubs;
50
Owen Andersoncb371882008-08-21 00:14:44 +000051 SPUAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T) :
Scott Michel266bc8f2007-12-04 22:23:35 +000052 AsmPrinter(O, TM, T)
53 {
54 }
55
56 virtual const char *getPassName() const {
57 return "STI CBEA SPU Assembly Printer";
58 }
59
60 SPUTargetMachine &getTM() {
61 return static_cast<SPUTargetMachine&>(TM);
62 }
63
64 /// printInstruction - This method is automatically generated by tablegen
65 /// from the instruction set description. This method returns true if the
66 /// machine instruction was sufficiently described to print it, otherwise it
67 /// returns false.
68 bool printInstruction(const MachineInstr *MI);
69
70 void printMachineInstruction(const MachineInstr *MI);
71 void printOp(const MachineOperand &MO);
72
73 /// printRegister - Print register according to target requirements.
74 ///
75 void printRegister(const MachineOperand &MO, bool R0AsZero) {
76 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000077 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
78 "Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000079 O << TM.getRegisterInfo()->get(RegNo).AsmName;
Scott Michel266bc8f2007-12-04 22:23:35 +000080 }
81
82 void printOperand(const MachineInstr *MI, unsigned OpNo) {
83 const MachineOperand &MO = MI->getOperand(OpNo);
84 if (MO.isRegister()) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000085 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000086 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Scott Michel266bc8f2007-12-04 22:23:35 +000087 } else if (MO.isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000088 O << MO.getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +000089 } else {
90 printOp(MO);
91 }
92 }
93
94 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
95 unsigned AsmVariant, const char *ExtraCode);
96 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
97 unsigned AsmVariant, const char *ExtraCode);
98
99
100 void
101 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
102 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000103 int value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000104 value = (value << (32 - 7)) >> (32 - 7);
105
106 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000107 && "Invalid s7 argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000108 O << value;
109 }
110
111 void
112 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
113 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000114 unsigned int value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000115 assert(value < (1 << 8) && "Invalid u7 argument");
116 O << value;
117 }
118
119 void
120 printMemRegImmS7(const MachineInstr *MI, unsigned OpNo)
121 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000122 char value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000123 O << (int) value;
124 O << "(";
125 printOperand(MI, OpNo+1);
126 O << ")";
127 }
128
129 void
130 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
131 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000132 O << (short) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000133 }
134
135 void
136 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
137 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000138 O << (unsigned short)MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000139 }
140
141 void
142 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
143 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000144 O << (unsigned)MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000145 }
146
147 void
148 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
149 // When used as the base register, r0 reads constant zero rather than
150 // the value contained in the register. For this reason, the darwin
151 // assembler requires that we print r0 as 0 (no r) when used as the base.
152 const MachineOperand &MO = MI->getOperand(OpNo);
Bill Wendling74ab84c2008-02-26 21:11:01 +0000153 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Scott Michel266bc8f2007-12-04 22:23:35 +0000154 O << ", ";
155 printOperand(MI, OpNo+1);
156 }
157
158 void
159 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
160 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000161 unsigned int value = MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000162 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
163 O << value;
164 }
165
166 void
167 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
168 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000169 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
Scott Michel266bc8f2007-12-04 22:23:35 +0000170 >> 16);
171 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
172 && "Invalid s10 argument");
173 O << value;
174 }
175
176 void
177 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
178 {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000179 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
Scott Michel266bc8f2007-12-04 22:23:35 +0000180 >> 16);
181 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
182 O << value;
183 }
184
185 void
186 printMemRegImmS10(const MachineInstr *MI, unsigned OpNo)
187 {
188 const MachineOperand &MO = MI->getOperand(OpNo);
189 assert(MO.isImmediate()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000190 && "printMemRegImmS10 first operand is not immedate");
Scott Michel266bc8f2007-12-04 22:23:35 +0000191 printS10ImmOperand(MI, OpNo);
192 O << "(";
193 printOperand(MI, OpNo+1);
194 O << ")";
195 }
196
197 void
198 printAddr256K(const MachineInstr *MI, unsigned OpNo)
199 {
Scott Michel053c1da2008-01-29 02:16:57 +0000200 /* Note: operand 1 is an offset or symbol name. */
Scott Michel266bc8f2007-12-04 22:23:35 +0000201 if (MI->getOperand(OpNo).isImmediate()) {
202 printS16ImmOperand(MI, OpNo);
203 } else {
204 printOp(MI->getOperand(OpNo));
Scott Michel053c1da2008-01-29 02:16:57 +0000205 if (MI->getOperand(OpNo+1).isImmediate()) {
206 int displ = int(MI->getOperand(OpNo+1).getImm());
207 if (displ > 0)
208 O << "+" << displ;
209 else if (displ < 0)
210 O << displ;
211 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000212 }
213 }
214
215 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
216 printOp(MI->getOperand(OpNo));
217 }
218
219 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
220 printOp(MI->getOperand(OpNo));
221 O << "-.";
222 }
223
224 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
225 if (MI->getOperand(OpNo).isImmediate()) {
226 printS16ImmOperand(MI, OpNo);
227 } else {
228 printOp(MI->getOperand(OpNo));
229 O << "@h";
230 }
231 }
232
233 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
234 if (MI->getOperand(OpNo).isImmediate()) {
235 printS16ImmOperand(MI, OpNo);
236 } else {
237 printOp(MI->getOperand(OpNo));
238 O << "@l";
239 }
240 }
241
242 /// Print local store address
243 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
244 printOp(MI->getOperand(OpNo));
245 }
246
247 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
248 if (MI->getOperand(OpNo).isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000249 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000250 assert((value >= 0 && value < 16)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000251 && "Invalid negated immediate rotate 7-bit argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000252 O << -value;
253 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000254 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel266bc8f2007-12-04 22:23:35 +0000255 }
256 }
257
258 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
259 if (MI->getOperand(OpNo).isImmediate()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000260 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000261 assert((value >= 0 && value < 32)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000262 && "Invalid negated immediate rotate 7-bit argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000263 O << -value;
264 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000265 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel266bc8f2007-12-04 22:23:35 +0000266 }
267 }
268
269 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
270 virtual bool doFinalization(Module &M) = 0;
271 };
272
273 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
274 struct VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
275
276 DwarfWriter DW;
Dale Johannesend0377242008-07-09 21:25:06 +0000277 MachineModuleInfo *MMI;
Scott Michel266bc8f2007-12-04 22:23:35 +0000278
Owen Andersoncb371882008-08-21 00:14:44 +0000279 LinuxAsmPrinter(raw_ostream &O, SPUTargetMachine &TM,
Scott Michel266bc8f2007-12-04 22:23:35 +0000280 const TargetAsmInfo *T) :
281 SPUAsmPrinter(O, TM, T),
Dale Johannesend0377242008-07-09 21:25:06 +0000282 DW(O, this, T),
283 MMI(0)
Scott Michel266bc8f2007-12-04 22:23:35 +0000284 { }
285
286 virtual const char *getPassName() const {
287 return "STI CBEA SPU Assembly Printer";
288 }
289
290 bool runOnMachineFunction(MachineFunction &F);
291 bool doInitialization(Module &M);
292 bool doFinalization(Module &M);
293
294 void getAnalysisUsage(AnalysisUsage &AU) const {
295 AU.setPreservesAll();
296 AU.addRequired<MachineModuleInfo>();
297 SPUAsmPrinter::getAnalysisUsage(AU);
298 }
299
Scott Michel266bc8f2007-12-04 22:23:35 +0000300 };
301} // end of anonymous namespace
302
303// Include the auto-generated portion of the assembly writer
304#include "SPUGenAsmWriter.inc"
305
306void SPUAsmPrinter::printOp(const MachineOperand &MO) {
307 switch (MO.getType()) {
308 case MachineOperand::MO_Immediate:
309 cerr << "printOp() does not handle immediate values\n";
310 abort();
311 return;
312
313 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000314 printBasicBlockLabel(MO.getMBB());
Scott Michel266bc8f2007-12-04 22:23:35 +0000315 return;
316 case MachineOperand::MO_JumpTableIndex:
317 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000318 << '_' << MO.getIndex();
Scott Michel266bc8f2007-12-04 22:23:35 +0000319 return;
320 case MachineOperand::MO_ConstantPoolIndex:
321 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000322 << '_' << MO.getIndex();
Scott Michel266bc8f2007-12-04 22:23:35 +0000323 return;
324 case MachineOperand::MO_ExternalSymbol:
325 // Computing the address of an external symbol, not calling it.
326 if (TM.getRelocationModel() != Reloc::Static) {
327 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
328 GVStubs.insert(Name);
329 O << "L" << Name << "$non_lazy_ptr";
330 return;
331 }
332 O << TAI->getGlobalPrefix() << MO.getSymbolName();
333 return;
334 case MachineOperand::MO_GlobalAddress: {
335 // Computing the address of a global symbol, not calling it.
336 GlobalValue *GV = MO.getGlobal();
337 std::string Name = Mang->getValueName(GV);
338
339 // External or weakly linked global variables need non-lazily-resolved
340 // stubs
341 if (TM.getRelocationModel() != Reloc::Static) {
342 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000343 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000344 GVStubs.insert(Name);
345 O << "L" << Name << "$non_lazy_ptr";
346 return;
347 }
348 }
349 O << Name;
350
351 if (GV->hasExternalWeakLinkage())
352 ExtWeakSymbols.insert(GV);
353 return;
354 }
355
356 default:
357 O << "<unknown operand type: " << MO.getType() << ">";
358 return;
359 }
360}
361
362/// PrintAsmOperand - Print out an operand for an inline asm expression.
363///
364bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
365 unsigned AsmVariant,
366 const char *ExtraCode) {
367 // Does this asm operand have a single letter operand modifier?
368 if (ExtraCode && ExtraCode[0]) {
369 if (ExtraCode[1] != 0) return true; // Unknown modifier.
370
371 switch (ExtraCode[0]) {
372 default: return true; // Unknown modifier.
373 case 'L': // Write second word of DImode reference.
374 // Verify that this operand has two consecutive registers.
375 if (!MI->getOperand(OpNo).isRegister() ||
376 OpNo+1 == MI->getNumOperands() ||
377 !MI->getOperand(OpNo+1).isRegister())
378 return true;
379 ++OpNo; // Return the high-part.
380 break;
381 }
382 }
383
384 printOperand(MI, OpNo);
385 return false;
386}
387
388bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000389 unsigned OpNo,
Scott Michel266bc8f2007-12-04 22:23:35 +0000390 unsigned AsmVariant,
391 const char *ExtraCode) {
392 if (ExtraCode && ExtraCode[0])
393 return true; // Unknown modifier.
394 printMemRegReg(MI, OpNo);
395 return false;
396}
397
398/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
399/// to the current output stream.
400///
401void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
402 ++EmittedInsts;
403 printInstruction(MI);
404}
405
Scott Michel266bc8f2007-12-04 22:23:35 +0000406/// runOnMachineFunction - This uses the printMachineInstruction()
407/// method to print assembly for each instruction.
408///
409bool
410LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
411{
Scott Michel266bc8f2007-12-04 22:23:35 +0000412 SetupMachineFunction(MF);
413 O << "\n\n";
414
415 // Print out constants referenced by the function
416 EmitConstantPool(MF.getConstantPool());
417
418 // Print out labels for the function.
419 const Function *F = MF.getFunction();
420
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000421 SwitchToSection(TAI->SectionForGlobal(F));
Scott Michel266bc8f2007-12-04 22:23:35 +0000422 EmitAlignment(3, F);
423
424 switch (F->getLinkage()) {
425 default: assert(0 && "Unknown linkage type!");
426 case Function::InternalLinkage: // Symbols default to internal.
427 break;
428 case Function::ExternalLinkage:
429 O << "\t.global\t" << CurrentFnName << "\n"
430 << "\t.type\t" << CurrentFnName << ", @function\n";
431 break;
432 case Function::WeakLinkage:
433 case Function::LinkOnceLinkage:
434 O << "\t.global\t" << CurrentFnName << "\n";
435 O << "\t.weak_definition\t" << CurrentFnName << "\n";
436 break;
437 }
438 O << CurrentFnName << ":\n";
439
440 // Emit pre-function debug information.
441 DW.BeginFunction(&MF);
442
443 // Print out code for the function.
444 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
445 I != E; ++I) {
446 // Print a label for the basic block.
447 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000448 printBasicBlockLabel(I, true, true);
Scott Michel266bc8f2007-12-04 22:23:35 +0000449 O << '\n';
450 }
451 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
452 II != E; ++II) {
453 // Print the assembly for the instruction.
Scott Michel266bc8f2007-12-04 22:23:35 +0000454 printMachineInstruction(II);
455 }
456 }
457
458 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
459
460 // Print out jump tables referenced by the function.
461 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
462
463 // Emit post-function debug information.
Bill Wendlingd751c642008-09-26 00:28:12 +0000464 DW.EndFunction(&MF);
Scott Michel266bc8f2007-12-04 22:23:35 +0000465
466 // We didn't modify anything.
467 return false;
468}
469
470
471bool LinuxAsmPrinter::doInitialization(Module &M) {
472 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov90a7b882008-09-24 22:11:42 +0000473 SwitchToTextSection("\t.text");
Scott Michel266bc8f2007-12-04 22:23:35 +0000474 // Emit initial debug information.
475 DW.BeginModule(&M);
Dale Johannesend0377242008-07-09 21:25:06 +0000476 MMI = getAnalysisToUpdate<MachineModuleInfo>();
477 DW.SetModuleInfo(MMI);
Scott Michel266bc8f2007-12-04 22:23:35 +0000478 return Result;
479}
480
481bool LinuxAsmPrinter::doFinalization(Module &M) {
482 const TargetData *TD = TM.getTargetData();
483
484 // Print out module-level global variables here.
485 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
486 I != E; ++I) {
487 if (!I->hasInitializer()) continue; // External global require no code
488
489 // Check to see if this is a special global used by LLVM, if so, emit it.
490 if (EmitSpecialLLVMGlobal(I))
491 continue;
492
493 std::string name = Mang->getValueName(I);
494 Constant *C = I->getInitializer();
495 unsigned Size = TD->getTypeStoreSize(C->getType());
496 unsigned Align = TD->getPreferredAlignmentLog(I);
497
498 if (C->isNullValue() && /* FIXME: Verify correct */
499 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000500 I->hasLinkOnceLinkage() || I->hasCommonLinkage() ||
Scott Michel266bc8f2007-12-04 22:23:35 +0000501 (I->hasExternalLinkage() && !I->hasSection()))) {
502 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
503 if (I->hasExternalLinkage()) {
504 // External linkage globals -> .bss section
505 // FIXME: Want to set the global variable's section so that
506 // SwitchToDataSection emits the ".section" directive
507 SwitchToDataSection("\t.section\t.bss", I);
508 O << "\t.global\t" << name << '\n';
509 O << "\t.align\t" << Align << '\n';
510 O << "\t.type\t" << name << ", @object\n";
511 O << "\t.size\t" << name << ", " << Size << '\n';
512 O << name << ":\n";
513 O << "\t.zero\t" << Size;
514 } else if (I->hasInternalLinkage()) {
515 SwitchToDataSection("\t.data", I);
Scott Michel053c1da2008-01-29 02:16:57 +0000516 O << ".local " << name << "\n";
517 O << TAI->getCOMMDirective() << name << "," << Size << "," << Align << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000518 } else {
519 SwitchToDataSection("\t.data", I);
520 O << ".comm " << name << "," << Size;
521 }
522 O << "\t\t# '" << I->getName() << "'\n";
523 } else {
524 switch (I->getLinkage()) {
525 case GlobalValue::LinkOnceLinkage:
526 case GlobalValue::WeakLinkage:
Dale Johannesenaafce772008-05-14 20:12:51 +0000527 case GlobalValue::CommonLinkage:
Scott Michel266bc8f2007-12-04 22:23:35 +0000528 O << "\t.global " << name << '\n'
529 << "\t.weak_definition " << name << '\n';
530 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
531 break;
532 case GlobalValue::AppendingLinkage:
533 // FIXME: appending linkage variables should go into a section of
534 // their name or something. For now, just emit them as external.
535 case GlobalValue::ExternalLinkage:
536 // If external or appending, declare as a global symbol
537 O << "\t.global " << name << "\n";
538 // FALL THROUGH
539 case GlobalValue::InternalLinkage:
540 if (I->isConstant()) {
541 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
542 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
Anton Korobeynikov90a7b882008-09-24 22:11:42 +0000543 SwitchToDataSection("\t.cstring", I);
Scott Michel266bc8f2007-12-04 22:23:35 +0000544 break;
545 }
546 }
547
548 SwitchToDataSection("\t.data", I);
549 break;
550 default:
551 cerr << "Unknown linkage type!";
552 abort();
553 }
554
555 EmitAlignment(Align, I);
556 O << name << ":\t\t\t\t# '" << I->getName() << "'\n";
557
558 // If the initializer is a extern weak symbol, remember to emit the weak
559 // reference!
560 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
561 if (GV->hasExternalWeakLinkage())
562 ExtWeakSymbols.insert(GV);
563
564 EmitGlobalConstant(C);
565 O << '\n';
566 }
567 }
568
569 // Output stubs for dynamically-linked functions
570 if (TM.getRelocationModel() == Reloc::PIC_) {
571 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
572 i != e; ++i) {
573 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
574 "pure_instructions,32");
575 EmitAlignment(4);
576 O << "L" << *i << "$stub:\n";
577 O << "\t.indirect_symbol " << *i << "\n";
578 O << "\tmflr r0\n";
579 O << "\tbcl 20,31,L0$" << *i << "\n";
580 O << "L0$" << *i << ":\n";
581 O << "\tmflr r11\n";
582 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
583 O << "\tmtlr r0\n";
584 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
585 O << "\tmtctr r12\n";
586 O << "\tbctr\n";
587 SwitchToDataSection(".lazy_symbol_pointer");
588 O << "L" << *i << "$lazy_ptr:\n";
589 O << "\t.indirect_symbol " << *i << "\n";
590 O << "\t.long dyld_stub_binding_helper\n";
591 }
592 } else {
593 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
594 i != e; ++i) {
595 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
596 "pure_instructions,16");
597 EmitAlignment(4);
598 O << "L" << *i << "$stub:\n";
599 O << "\t.indirect_symbol " << *i << "\n";
600 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
601 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
602 O << "\tmtctr r12\n";
603 O << "\tbctr\n";
604 SwitchToDataSection(".lazy_symbol_pointer");
605 O << "L" << *i << "$lazy_ptr:\n";
606 O << "\t.indirect_symbol " << *i << "\n";
607 O << "\t.long dyld_stub_binding_helper\n";
608 }
609 }
610
611 O << "\n";
612
613 // Output stubs for external and common global variables.
614 if (GVStubs.begin() != GVStubs.end()) {
615 SwitchToDataSection(".non_lazy_symbol_pointer");
616 for (std::set<std::string>::iterator I = GVStubs.begin(),
617 E = GVStubs.end(); I != E; ++I) {
618 O << "L" << *I << "$non_lazy_ptr:\n";
619 O << "\t.indirect_symbol " << *I << "\n";
620 O << "\t.long\t0\n";
621 }
622 }
623
624 // Emit initial debug information.
625 DW.EndModule();
626
627 // Emit ident information
Scott Michel86c041f2007-12-20 00:44:13 +0000628 O << "\t.ident\t\"(llvm 2.2+) STI CBEA Cell SPU backend\"\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000629
630 return AsmPrinter::doFinalization(M);
631}
632
633
634
635/// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
636/// assembly code for a MachineFunction to the given output stream, in a format
637/// that the Linux SPU assembler can deal with.
638///
Owen Andersoncb371882008-08-21 00:14:44 +0000639FunctionPass *llvm::createSPUAsmPrinterPass(raw_ostream &o,
Scott Michel266bc8f2007-12-04 22:23:35 +0000640 SPUTargetMachine &tm) {
641 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
642}
643