blob: a43bc0e6d9ffe48d39e7789523b8d297433ed1b9 [file] [log] [blame]
pingbakddfd8692008-11-08 18:59:02 +00001//===-- SPUAsmPrinter.cpp - Print machine instrs to Cell SPU assembly -------=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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//
14//===----------------------------------------------------------------------===//
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"
Devang Patelf667ab42009-06-25 00:47:42 +000022#include "llvm/MDNode.h"
pingbakddfd8692008-11-08 18:59:02 +000023#include "llvm/Assembly/Writer.h"
24#include "llvm/CodeGen/AsmPrinter.h"
25#include "llvm/CodeGen/DwarfWriter.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/Support/Mangler.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/raw_ostream.h"
35#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetRegisterInfo.h"
37#include "llvm/Target/TargetInstrInfo.h"
38#include "llvm/Target/TargetOptions.h"
39#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/StringExtras.h"
41#include <set>
42using namespace llvm;
43
44namespace {
45 STATISTIC(EmittedInsts, "Number of machine instrs printed");
46
47 const std::string bss_section(".bss");
48
Bill Wendling4f405312009-02-24 08:30:20 +000049 class VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
pingbakddfd8692008-11-08 18:59:02 +000050 std::set<std::string> FnStubs, GVStubs;
Bill Wendling4f405312009-02-24 08:30:20 +000051 public:
Bill Wendling58ed5d22009-04-29 00:15:41 +000052 explicit SPUAsmPrinter(raw_ostream &O, TargetMachine &TM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000053 const TargetAsmInfo *T, CodeGenOpt::Level OL,
54 bool V) :
Bill Wendling58ed5d22009-04-29 00:15:41 +000055 AsmPrinter(O, TM, T, OL, V) {}
pingbakddfd8692008-11-08 18:59:02 +000056
57 virtual const char *getPassName() const {
58 return "STI CBEA SPU Assembly Printer";
59 }
60
61 SPUTargetMachine &getTM() {
62 return static_cast<SPUTargetMachine&>(TM);
63 }
64
65 /// printInstruction - This method is automatically generated by tablegen
66 /// from the instruction set description. This method returns true if the
67 /// machine instruction was sufficiently described to print it, otherwise it
68 /// returns false.
69 bool printInstruction(const MachineInstr *MI);
70
71 void printMachineInstruction(const MachineInstr *MI);
72 void printOp(const MachineOperand &MO);
73
74 /// printRegister - Print register according to target requirements.
75 ///
76 void printRegister(const MachineOperand &MO, bool R0AsZero) {
77 unsigned RegNo = MO.getReg();
78 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
79 "Not physreg??");
80 O << TM.getRegisterInfo()->get(RegNo).AsmName;
81 }
82
83 void printOperand(const MachineInstr *MI, unsigned OpNo) {
84 const MachineOperand &MO = MI->getOperand(OpNo);
85 if (MO.isReg()) {
86 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
87 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
88 } else if (MO.isImm()) {
89 O << MO.getImm();
90 } else {
91 printOp(MO);
92 }
93 }
Scott Michelc899a122009-01-26 22:33:37 +000094
pingbakddfd8692008-11-08 18:59:02 +000095 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
96 unsigned AsmVariant, const char *ExtraCode);
97 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
98 unsigned AsmVariant, const char *ExtraCode);
Scott Michelc899a122009-01-26 22:33:37 +000099
100
pingbakddfd8692008-11-08 18:59:02 +0000101 void
102 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
103 {
104 int value = MI->getOperand(OpNo).getImm();
105 value = (value << (32 - 7)) >> (32 - 7);
106
107 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
108 && "Invalid s7 argument");
109 O << value;
110 }
111
112 void
113 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
114 {
115 unsigned int value = MI->getOperand(OpNo).getImm();
116 assert(value < (1 << 8) && "Invalid u7 argument");
117 O << value;
118 }
Scott Michelc899a122009-01-26 22:33:37 +0000119
pingbakddfd8692008-11-08 18:59:02 +0000120 void
Scott Michel06eabde2008-12-27 04:51:36 +0000121 printShufAddr(const MachineInstr *MI, unsigned OpNo)
pingbakddfd8692008-11-08 18:59:02 +0000122 {
123 char value = MI->getOperand(OpNo).getImm();
124 O << (int) value;
125 O << "(";
126 printOperand(MI, OpNo+1);
127 O << ")";
128 }
129
130 void
131 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
132 {
133 O << (short) MI->getOperand(OpNo).getImm();
134 }
135
136 void
137 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
138 {
139 O << (unsigned short)MI->getOperand(OpNo).getImm();
140 }
141
142 void
143 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
144 {
145 O << (unsigned)MI->getOperand(OpNo).getImm();
146 }
Scott Michelc899a122009-01-26 22:33:37 +0000147
pingbakddfd8692008-11-08 18:59:02 +0000148 void
149 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
150 // When used as the base register, r0 reads constant zero rather than
151 // the value contained in the register. For this reason, the darwin
152 // assembler requires that we print r0 as 0 (no r) when used as the base.
153 const MachineOperand &MO = MI->getOperand(OpNo);
154 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
155 O << ", ";
156 printOperand(MI, OpNo+1);
157 }
158
159 void
160 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
161 {
162 unsigned int value = MI->getOperand(OpNo).getImm();
163 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
164 O << value;
165 }
166
167 void
168 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
169 {
170 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
171 >> 16);
172 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
173 && "Invalid s10 argument");
174 O << value;
175 }
176
177 void
178 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
179 {
180 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
181 >> 16);
182 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
183 O << value;
184 }
185
186 void
Scott Michel06eabde2008-12-27 04:51:36 +0000187 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
pingbakddfd8692008-11-08 18:59:02 +0000188 {
Chris Lattner4c4fb3a2009-01-21 18:38:18 +0000189 assert(MI->getOperand(OpNo).isImm() &&
190 "printDFormAddr first operand is not immediate");
pingbakddfd8692008-11-08 18:59:02 +0000191 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michelffca41d2008-11-20 05:01:09 +0000192 int16_t value16 = int16_t(value);
193 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
pingbakddfd8692008-11-08 18:59:02 +0000194 && "Invalid dform s10 offset argument");
Scott Michel06eabde2008-12-27 04:51:36 +0000195 O << (value16 & ~0xf) << "(";
pingbakddfd8692008-11-08 18:59:02 +0000196 printOperand(MI, OpNo+1);
197 O << ")";
198 }
199
200 void
201 printAddr256K(const MachineInstr *MI, unsigned OpNo)
202 {
203 /* Note: operand 1 is an offset or symbol name. */
204 if (MI->getOperand(OpNo).isImm()) {
205 printS16ImmOperand(MI, OpNo);
206 } else {
207 printOp(MI->getOperand(OpNo));
208 if (MI->getOperand(OpNo+1).isImm()) {
209 int displ = int(MI->getOperand(OpNo+1).getImm());
210 if (displ > 0)
211 O << "+" << displ;
212 else if (displ < 0)
213 O << displ;
214 }
215 }
216 }
217
218 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
219 printOp(MI->getOperand(OpNo));
220 }
221
222 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
Scott Michel61895fe2008-12-10 00:15:19 +0000223 // Used to generate a ".-<target>", but it turns out that the assembler
224 // really wants the target.
225 //
226 // N.B.: This operand is used for call targets. Branch hints are another
227 // animal entirely.
228 printOp(MI->getOperand(OpNo));
229 }
230
231 void printHBROperand(const MachineInstr *MI, unsigned OpNo) {
232 // HBR operands are generated in front of branches, hence, the
233 // program counter plus the target.
234 O << ".+";
pingbakddfd8692008-11-08 18:59:02 +0000235 printOp(MI->getOperand(OpNo));
pingbakddfd8692008-11-08 18:59:02 +0000236 }
237
238 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
239 if (MI->getOperand(OpNo).isImm()) {
240 printS16ImmOperand(MI, OpNo);
241 } else {
242 printOp(MI->getOperand(OpNo));
243 O << "@h";
244 }
245 }
246
247 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
248 if (MI->getOperand(OpNo).isImm()) {
249 printS16ImmOperand(MI, OpNo);
250 } else {
251 printOp(MI->getOperand(OpNo));
252 O << "@l";
253 }
254 }
255
256 /// Print local store address
257 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
258 printOp(MI->getOperand(OpNo));
259 }
260
261 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
262 if (MI->getOperand(OpNo).isImm()) {
263 int value = (int) MI->getOperand(OpNo).getImm();
264 assert((value >= 0 && value < 16)
265 && "Invalid negated immediate rotate 7-bit argument");
266 O << -value;
267 } else {
268 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
269 }
270 }
271
272 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
273 if (MI->getOperand(OpNo).isImm()) {
274 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michelc630c412008-11-24 17:11:17 +0000275 assert((value >= 0 && value <= 32)
pingbakddfd8692008-11-08 18:59:02 +0000276 && "Invalid negated immediate rotate 7-bit argument");
277 O << -value;
278 } else {
279 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
280 }
281 }
282
283 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
284 //! Assembly printer cleanup after function has been emitted
285 virtual bool doFinalization(Module &M) = 0;
286 };
287
288 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000289 class VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
Devang Patelaa1e8432009-01-08 23:40:34 +0000290 DwarfWriter *DW;
Bill Wendling4f405312009-02-24 08:30:20 +0000291 public:
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000292 explicit LinuxAsmPrinter(raw_ostream &O, SPUTargetMachine &TM,
293 const TargetAsmInfo *T, CodeGenOpt::Level F,
294 bool V)
Devang Patel0e6763c2009-06-20 01:07:54 +0000295 : SPUAsmPrinter(O, TM, T, F, V), DW(0) {}
pingbakddfd8692008-11-08 18:59:02 +0000296
297 virtual const char *getPassName() const {
298 return "STI CBEA SPU Assembly Printer";
299 }
Scott Michelc899a122009-01-26 22:33:37 +0000300
pingbakddfd8692008-11-08 18:59:02 +0000301 bool runOnMachineFunction(MachineFunction &F);
302 bool doInitialization(Module &M);
303 //! Dump globals, perform cleanup after function emission
304 bool doFinalization(Module &M);
Scott Michelc899a122009-01-26 22:33:37 +0000305
pingbakddfd8692008-11-08 18:59:02 +0000306 void getAnalysisUsage(AnalysisUsage &AU) const {
307 AU.setPreservesAll();
308 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000309 AU.addRequired<DwarfWriter>();
pingbakddfd8692008-11-08 18:59:02 +0000310 SPUAsmPrinter::getAnalysisUsage(AU);
311 }
312
313 //! Emit a global variable according to its section and type
314 void printModuleLevelGV(const GlobalVariable* GVar);
315 };
316} // end of anonymous namespace
317
318// Include the auto-generated portion of the assembly writer
319#include "SPUGenAsmWriter.inc"
320
321void SPUAsmPrinter::printOp(const MachineOperand &MO) {
322 switch (MO.getType()) {
323 case MachineOperand::MO_Immediate:
324 cerr << "printOp() does not handle immediate values\n";
325 abort();
326 return;
327
328 case MachineOperand::MO_MachineBasicBlock:
329 printBasicBlockLabel(MO.getMBB());
330 return;
331 case MachineOperand::MO_JumpTableIndex:
332 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
333 << '_' << MO.getIndex();
334 return;
335 case MachineOperand::MO_ConstantPoolIndex:
336 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
337 << '_' << MO.getIndex();
338 return;
339 case MachineOperand::MO_ExternalSymbol:
340 // Computing the address of an external symbol, not calling it.
341 if (TM.getRelocationModel() != Reloc::Static) {
342 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
343 GVStubs.insert(Name);
344 O << "L" << Name << "$non_lazy_ptr";
345 return;
346 }
347 O << TAI->getGlobalPrefix() << MO.getSymbolName();
348 return;
349 case MachineOperand::MO_GlobalAddress: {
350 // Computing the address of a global symbol, not calling it.
351 GlobalValue *GV = MO.getGlobal();
352 std::string Name = Mang->getValueName(GV);
353
354 // External or weakly linked global variables need non-lazily-resolved
355 // stubs
356 if (TM.getRelocationModel() != Reloc::Static) {
357 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
358 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
359 GVStubs.insert(Name);
360 O << "L" << Name << "$non_lazy_ptr";
361 return;
362 }
363 }
364 O << Name;
pingbakddfd8692008-11-08 18:59:02 +0000365 return;
366 }
367
368 default:
369 O << "<unknown operand type: " << MO.getType() << ">";
370 return;
371 }
372}
373
374/// PrintAsmOperand - Print out an operand for an inline asm expression.
375///
376bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michelc899a122009-01-26 22:33:37 +0000377 unsigned AsmVariant,
pingbakddfd8692008-11-08 18:59:02 +0000378 const char *ExtraCode) {
379 // Does this asm operand have a single letter operand modifier?
380 if (ExtraCode && ExtraCode[0]) {
381 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michelc899a122009-01-26 22:33:37 +0000382
pingbakddfd8692008-11-08 18:59:02 +0000383 switch (ExtraCode[0]) {
384 default: return true; // Unknown modifier.
Scott Michelc899a122009-01-26 22:33:37 +0000385 case 'L': // Write second word of DImode reference.
pingbakddfd8692008-11-08 18:59:02 +0000386 // Verify that this operand has two consecutive registers.
387 if (!MI->getOperand(OpNo).isReg() ||
388 OpNo+1 == MI->getNumOperands() ||
389 !MI->getOperand(OpNo+1).isReg())
390 return true;
391 ++OpNo; // Return the high-part.
392 break;
393 }
394 }
Scott Michelc899a122009-01-26 22:33:37 +0000395
pingbakddfd8692008-11-08 18:59:02 +0000396 printOperand(MI, OpNo);
397 return false;
398}
399
400bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
401 unsigned OpNo,
Scott Michelc899a122009-01-26 22:33:37 +0000402 unsigned AsmVariant,
pingbakddfd8692008-11-08 18:59:02 +0000403 const char *ExtraCode) {
404 if (ExtraCode && ExtraCode[0])
405 return true; // Unknown modifier.
406 printMemRegReg(MI, OpNo);
407 return false;
408}
409
410/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
411/// to the current output stream.
412///
413void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
414 ++EmittedInsts;
415 printInstruction(MI);
416}
417
418/// runOnMachineFunction - This uses the printMachineInstruction()
419/// method to print assembly for each instruction.
420///
421bool
422LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
423{
Bill Wendling4f405312009-02-24 08:30:20 +0000424 this->MF = &MF;
425
pingbakddfd8692008-11-08 18:59:02 +0000426 SetupMachineFunction(MF);
427 O << "\n\n";
Scott Michelc899a122009-01-26 22:33:37 +0000428
pingbakddfd8692008-11-08 18:59:02 +0000429 // Print out constants referenced by the function
430 EmitConstantPool(MF.getConstantPool());
431
432 // Print out labels for the function.
433 const Function *F = MF.getFunction();
434
435 SwitchToSection(TAI->SectionForGlobal(F));
436 EmitAlignment(3, F);
437
438 switch (F->getLinkage()) {
439 default: assert(0 && "Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000440 case Function::PrivateLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000441 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;
Duncan Sands19d161f2009-03-07 15:45:40 +0000447 case Function::WeakAnyLinkage:
448 case Function::WeakODRLinkage:
449 case Function::LinkOnceAnyLinkage:
450 case Function::LinkOnceODRLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000451 O << "\t.global\t" << CurrentFnName << "\n";
452 O << "\t.weak_definition\t" << CurrentFnName << "\n";
453 break;
454 }
455 O << CurrentFnName << ":\n";
456
457 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000458 DW->BeginFunction(&MF);
pingbakddfd8692008-11-08 18:59:02 +0000459
460 // Print out code for the function.
461 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
462 I != E; ++I) {
463 // Print a label for the basic block.
464 if (I != MF.begin()) {
465 printBasicBlockLabel(I, true, true);
466 O << '\n';
467 }
468 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
469 II != E; ++II) {
470 // Print the assembly for the instruction.
471 printMachineInstruction(II);
472 }
473 }
474
475 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
476
477 // Print out jump tables referenced by the function.
478 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Scott Michelc899a122009-01-26 22:33:37 +0000479
pingbakddfd8692008-11-08 18:59:02 +0000480 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000481 DW->EndFunction(&MF);
Scott Michelc899a122009-01-26 22:33:37 +0000482
pingbakddfd8692008-11-08 18:59:02 +0000483 // We didn't modify anything.
484 return false;
485}
486
487
488bool LinuxAsmPrinter::doInitialization(Module &M) {
489 bool Result = AsmPrinter::doInitialization(M);
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000490 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patel1a454842009-06-19 21:54:26 +0000491 SwitchToTextSection("\t.text");
pingbakddfd8692008-11-08 18:59:02 +0000492 return Result;
493}
494
495/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000496/// Don't print things like \\n or \\0.
pingbakddfd8692008-11-08 18:59:02 +0000497static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
498 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
499 Name != E; ++Name)
500 if (isprint(*Name))
501 OS << *Name;
502}
503
504/*!
505 Emit a global variable according to its section, alignment, etc.
Scott Michelc899a122009-01-26 22:33:37 +0000506
pingbakddfd8692008-11-08 18:59:02 +0000507 \note This code was shamelessly copied from the PowerPC's assembly printer,
508 which sort of screams for some kind of refactorization of common code.
509 */
510void LinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
511 const TargetData *TD = TM.getTargetData();
512
513 if (!GVar->hasInitializer())
514 return;
515
516 // Check to see if this is a special global used by LLVM, if so, emit it.
517 if (EmitSpecialLLVMGlobal(GVar))
518 return;
519
520 std::string name = Mang->getValueName(GVar);
521
522 printVisibility(name, GVar->getVisibility());
523
524 Constant *C = GVar->getInitializer();
Devang Patelf667ab42009-06-25 00:47:42 +0000525 if (isa<MDNode>(C))
526 return;
pingbakddfd8692008-11-08 18:59:02 +0000527 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000528 unsigned Size = TD->getTypeAllocSize(Type);
pingbakddfd8692008-11-08 18:59:02 +0000529 unsigned Align = TD->getPreferredAlignmentLog(GVar);
530
531 SwitchToSection(TAI->SectionForGlobal(GVar));
532
533 if (C->isNullValue() && /* FIXME: Verify correct */
534 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000535 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000536 GVar->isWeakForLinker())) {
pingbakddfd8692008-11-08 18:59:02 +0000537 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
538
539 if (GVar->hasExternalLinkage()) {
540 O << "\t.global " << name << '\n';
541 O << "\t.type " << name << ", @object\n";
542 O << name << ":\n";
543 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000544 } else if (GVar->hasLocalLinkage()) {
pingbakddfd8692008-11-08 18:59:02 +0000545 O << TAI->getLCOMMDirective() << name << ',' << Size;
546 } else {
547 O << ".comm " << name << ',' << Size;
548 }
549 O << "\t\t" << TAI->getCommentString() << " '";
550 PrintUnmangledNameSafely(GVar, O);
551 O << "'\n";
552 return;
553 }
554
555 switch (GVar->getLinkage()) {
556 // Should never be seen for the CellSPU platform...
Duncan Sands19d161f2009-03-07 15:45:40 +0000557 case GlobalValue::LinkOnceAnyLinkage:
558 case GlobalValue::LinkOnceODRLinkage:
559 case GlobalValue::WeakAnyLinkage:
560 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000561 case GlobalValue::CommonLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000562 O << "\t.global " << name << '\n'
563 << "\t.type " << name << ", @object\n"
564 << "\t.weak " << name << '\n';
565 break;
566 case GlobalValue::AppendingLinkage:
567 // FIXME: appending linkage variables should go into a section of
568 // their name or something. For now, just emit them as external.
569 case GlobalValue::ExternalLinkage:
570 // If external or appending, declare as a global symbol
571 O << "\t.global " << name << '\n'
572 << "\t.type " << name << ", @object\n";
573 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000574 case GlobalValue::PrivateLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000575 case GlobalValue::InternalLinkage:
576 break;
577 default:
578 cerr << "Unknown linkage type!";
579 abort();
580 }
581
582 EmitAlignment(Align, GVar);
583 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
584 PrintUnmangledNameSafely(GVar, O);
585 O << "'\n";
586
pingbakddfd8692008-11-08 18:59:02 +0000587 EmitGlobalConstant(C);
588 O << '\n';
589}
590
591bool LinuxAsmPrinter::doFinalization(Module &M) {
592 // Print out module-level global variables here.
593 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
594 I != E; ++I)
595 printModuleLevelGV(I);
596
pingbakddfd8692008-11-08 18:59:02 +0000597 return AsmPrinter::doFinalization(M);
598}
599
600/// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
601/// assembly code for a MachineFunction to the given output stream, in a format
602/// that the Linux SPU assembler can deal with.
603///
604FunctionPass *llvm::createSPUAsmPrinterPass(raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +0000605 SPUTargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000606 CodeGenOpt::Level OptLevel,
607 bool verbose) {
Bill Wendling58ed5d22009-04-29 00:15:41 +0000608 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
pingbakddfd8692008-11-08 18:59:02 +0000609}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000610
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000611// Force static initialization.
612extern "C" void LLVMInitializeCellSPUAsmPrinter() { }
Anton Korobeynikovbab832e2009-06-19 19:36:55 +0000613
614namespace {
615 static struct Register {
616 Register() {
617 SPUTargetMachine::registerAsmPrinter(createSPUAsmPrinterPass);
618 }
619 } Registrator;
620}