blob: a4475036966b42404ef6294aa5597f532a8b4cb7 [file] [log] [blame]
Scott Michel73655bc2008-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 Patele4c0c0f2009-06-25 00:47:42 +000022#include "llvm/MDNode.h"
Scott Michel73655bc2008-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"
Torok Edwindac237e2009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Scott Michel73655bc2008-11-08 18:59:02 +000034#include "llvm/Support/Compiler.h"
David Greene71847812009-07-14 20:18:05 +000035#include "llvm/Support/FormattedStream.h"
Scott Michel73655bc2008-11-08 18:59:02 +000036#include "llvm/Target/TargetAsmInfo.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetOptions.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000040#include "llvm/Target/TargetRegistry.h"
Scott Michel73655bc2008-11-08 18:59:02 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/StringExtras.h"
43#include <set>
44using namespace llvm;
45
46namespace {
47 STATISTIC(EmittedInsts, "Number of machine instrs printed");
48
49 const std::string bss_section(".bss");
50
Bill Wendling57f0db82009-02-24 08:30:20 +000051 class VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
Scott Michel73655bc2008-11-08 18:59:02 +000052 std::set<std::string> FnStubs, GVStubs;
Bill Wendling57f0db82009-02-24 08:30:20 +000053 public:
David Greene71847812009-07-14 20:18:05 +000054 explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000055 const TargetAsmInfo *T, bool V) :
56 AsmPrinter(O, TM, T, V) {}
Scott Michel73655bc2008-11-08 18:59:02 +000057
58 virtual const char *getPassName() const {
59 return "STI CBEA SPU Assembly Printer";
60 }
61
62 SPUTargetMachine &getTM() {
63 return static_cast<SPUTargetMachine&>(TM);
64 }
65
66 /// printInstruction - This method is automatically generated by tablegen
67 /// from the instruction set description. This method returns true if the
68 /// machine instruction was sufficiently described to print it, otherwise it
69 /// returns false.
70 bool printInstruction(const MachineInstr *MI);
71
72 void printMachineInstruction(const MachineInstr *MI);
73 void printOp(const MachineOperand &MO);
74
75 /// printRegister - Print register according to target requirements.
76 ///
77 void printRegister(const MachineOperand &MO, bool R0AsZero) {
78 unsigned RegNo = MO.getReg();
79 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
80 "Not physreg??");
81 O << TM.getRegisterInfo()->get(RegNo).AsmName;
82 }
83
84 void printOperand(const MachineInstr *MI, unsigned OpNo) {
85 const MachineOperand &MO = MI->getOperand(OpNo);
86 if (MO.isReg()) {
87 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
88 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
89 } else if (MO.isImm()) {
90 O << MO.getImm();
91 } else {
92 printOp(MO);
93 }
94 }
Scott Michel9de57a92009-01-26 22:33:37 +000095
Scott Michel73655bc2008-11-08 18:59:02 +000096 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
97 unsigned AsmVariant, const char *ExtraCode);
98 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
99 unsigned AsmVariant, const char *ExtraCode);
Scott Michel9de57a92009-01-26 22:33:37 +0000100
101
Scott Michel73655bc2008-11-08 18:59:02 +0000102 void
103 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
104 {
105 int value = MI->getOperand(OpNo).getImm();
106 value = (value << (32 - 7)) >> (32 - 7);
107
108 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
109 && "Invalid s7 argument");
110 O << value;
111 }
112
113 void
114 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
115 {
116 unsigned int value = MI->getOperand(OpNo).getImm();
117 assert(value < (1 << 8) && "Invalid u7 argument");
118 O << value;
119 }
Scott Michel9de57a92009-01-26 22:33:37 +0000120
Scott Michel73655bc2008-11-08 18:59:02 +0000121 void
Scott Michelf0569be2008-12-27 04:51:36 +0000122 printShufAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000123 {
124 char value = MI->getOperand(OpNo).getImm();
125 O << (int) value;
126 O << "(";
127 printOperand(MI, OpNo+1);
128 O << ")";
129 }
130
131 void
132 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
133 {
134 O << (short) MI->getOperand(OpNo).getImm();
135 }
136
137 void
138 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
139 {
140 O << (unsigned short)MI->getOperand(OpNo).getImm();
141 }
142
143 void
144 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
145 {
146 O << (unsigned)MI->getOperand(OpNo).getImm();
147 }
Scott Michel9de57a92009-01-26 22:33:37 +0000148
Scott Michel73655bc2008-11-08 18:59:02 +0000149 void
150 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
151 // When used as the base register, r0 reads constant zero rather than
152 // the value contained in the register. For this reason, the darwin
153 // assembler requires that we print r0 as 0 (no r) when used as the base.
154 const MachineOperand &MO = MI->getOperand(OpNo);
155 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
156 O << ", ";
157 printOperand(MI, OpNo+1);
158 }
159
160 void
161 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
162 {
163 unsigned int value = MI->getOperand(OpNo).getImm();
164 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
165 O << value;
166 }
167
168 void
169 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
170 {
171 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
172 >> 16);
173 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
174 && "Invalid s10 argument");
175 O << value;
176 }
177
178 void
179 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
180 {
181 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
182 >> 16);
183 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
184 O << value;
185 }
186
187 void
Scott Michelf0569be2008-12-27 04:51:36 +0000188 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000189 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000190 assert(MI->getOperand(OpNo).isImm() &&
191 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000192 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000193 int16_t value16 = int16_t(value);
194 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000195 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000196 O << (value16 & ~0xf) << "(";
Scott Michel73655bc2008-11-08 18:59:02 +0000197 printOperand(MI, OpNo+1);
198 O << ")";
199 }
200
201 void
202 printAddr256K(const MachineInstr *MI, unsigned OpNo)
203 {
204 /* Note: operand 1 is an offset or symbol name. */
205 if (MI->getOperand(OpNo).isImm()) {
206 printS16ImmOperand(MI, OpNo);
207 } else {
208 printOp(MI->getOperand(OpNo));
209 if (MI->getOperand(OpNo+1).isImm()) {
210 int displ = int(MI->getOperand(OpNo+1).getImm());
211 if (displ > 0)
212 O << "+" << displ;
213 else if (displ < 0)
214 O << displ;
215 }
216 }
217 }
218
219 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
220 printOp(MI->getOperand(OpNo));
221 }
222
223 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
Scott Michelaedc6372008-12-10 00:15:19 +0000224 // Used to generate a ".-<target>", but it turns out that the assembler
225 // really wants the target.
226 //
227 // N.B.: This operand is used for call targets. Branch hints are another
228 // animal entirely.
229 printOp(MI->getOperand(OpNo));
230 }
231
232 void printHBROperand(const MachineInstr *MI, unsigned OpNo) {
233 // HBR operands are generated in front of branches, hence, the
234 // program counter plus the target.
235 O << ".+";
Scott Michel73655bc2008-11-08 18:59:02 +0000236 printOp(MI->getOperand(OpNo));
Scott Michel73655bc2008-11-08 18:59:02 +0000237 }
238
239 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
240 if (MI->getOperand(OpNo).isImm()) {
241 printS16ImmOperand(MI, OpNo);
242 } else {
243 printOp(MI->getOperand(OpNo));
244 O << "@h";
245 }
246 }
247
248 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
249 if (MI->getOperand(OpNo).isImm()) {
250 printS16ImmOperand(MI, OpNo);
251 } else {
252 printOp(MI->getOperand(OpNo));
253 O << "@l";
254 }
255 }
256
257 /// Print local store address
258 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
259 printOp(MI->getOperand(OpNo));
260 }
261
262 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
263 if (MI->getOperand(OpNo).isImm()) {
264 int value = (int) MI->getOperand(OpNo).getImm();
265 assert((value >= 0 && value < 16)
266 && "Invalid negated immediate rotate 7-bit argument");
267 O << -value;
268 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000269 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000270 }
271 }
272
273 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
274 if (MI->getOperand(OpNo).isImm()) {
275 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel104de432008-11-24 17:11:17 +0000276 assert((value >= 0 && value <= 32)
Scott Michel73655bc2008-11-08 18:59:02 +0000277 && "Invalid negated immediate rotate 7-bit argument");
278 O << -value;
279 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000280 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000281 }
282 }
283
284 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Scott Michel73655bc2008-11-08 18:59:02 +0000285 };
286
287 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
Bill Wendling57f0db82009-02-24 08:30:20 +0000288 class VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
Devang Pateleb3fc282009-01-08 23:40:34 +0000289 DwarfWriter *DW;
Bill Wendling57f0db82009-02-24 08:30:20 +0000290 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000291 explicit LinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000292 const TargetAsmInfo *T, bool V)
293 : SPUAsmPrinter(O, TM, T, V), DW(0) {}
Scott Michel73655bc2008-11-08 18:59:02 +0000294
295 virtual const char *getPassName() const {
296 return "STI CBEA SPU Assembly Printer";
297 }
Scott Michel9de57a92009-01-26 22:33:37 +0000298
Scott Michel73655bc2008-11-08 18:59:02 +0000299 bool runOnMachineFunction(MachineFunction &F);
300 bool doInitialization(Module &M);
Scott Michel9de57a92009-01-26 22:33:37 +0000301
Scott Michel73655bc2008-11-08 18:59:02 +0000302 void getAnalysisUsage(AnalysisUsage &AU) const {
303 AU.setPreservesAll();
304 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000305 AU.addRequired<DwarfWriter>();
Scott Michel73655bc2008-11-08 18:59:02 +0000306 SPUAsmPrinter::getAnalysisUsage(AU);
307 }
308
309 //! Emit a global variable according to its section and type
Chris Lattner40bbebd2009-07-21 18:38:57 +0000310 void PrintGlobalVariable(const GlobalVariable* GVar);
Scott Michel73655bc2008-11-08 18:59:02 +0000311 };
312} // end of anonymous namespace
313
314// Include the auto-generated portion of the assembly writer
315#include "SPUGenAsmWriter.inc"
316
317void SPUAsmPrinter::printOp(const MachineOperand &MO) {
318 switch (MO.getType()) {
319 case MachineOperand::MO_Immediate:
Torok Edwindac237e2009-07-08 20:53:28 +0000320 llvm_report_error("printOp() does not handle immediate values");
Scott Michel73655bc2008-11-08 18:59:02 +0000321 return;
322
323 case MachineOperand::MO_MachineBasicBlock:
324 printBasicBlockLabel(MO.getMBB());
325 return;
326 case MachineOperand::MO_JumpTableIndex:
327 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
328 << '_' << MO.getIndex();
329 return;
330 case MachineOperand::MO_ConstantPoolIndex:
331 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
332 << '_' << MO.getIndex();
333 return;
334 case MachineOperand::MO_ExternalSymbol:
335 // Computing the address of an external symbol, not calling it.
336 if (TM.getRelocationModel() != Reloc::Static) {
337 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
338 GVStubs.insert(Name);
339 O << "L" << Name << "$non_lazy_ptr";
340 return;
341 }
342 O << TAI->getGlobalPrefix() << MO.getSymbolName();
343 return;
344 case MachineOperand::MO_GlobalAddress: {
345 // Computing the address of a global symbol, not calling it.
346 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000347 std::string Name = Mang->getMangledName(GV);
Scott Michel73655bc2008-11-08 18:59:02 +0000348
349 // External or weakly linked global variables need non-lazily-resolved
350 // stubs
351 if (TM.getRelocationModel() != Reloc::Static) {
352 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
353 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
354 GVStubs.insert(Name);
355 O << "L" << Name << "$non_lazy_ptr";
356 return;
357 }
358 }
359 O << Name;
Scott Michel73655bc2008-11-08 18:59:02 +0000360 return;
361 }
362
363 default:
364 O << "<unknown operand type: " << MO.getType() << ">";
365 return;
366 }
367}
368
369/// PrintAsmOperand - Print out an operand for an inline asm expression.
370///
371bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000372 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000373 const char *ExtraCode) {
374 // Does this asm operand have a single letter operand modifier?
375 if (ExtraCode && ExtraCode[0]) {
376 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000377
Scott Michel73655bc2008-11-08 18:59:02 +0000378 switch (ExtraCode[0]) {
379 default: return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000380 case 'L': // Write second word of DImode reference.
Scott Michel73655bc2008-11-08 18:59:02 +0000381 // Verify that this operand has two consecutive registers.
382 if (!MI->getOperand(OpNo).isReg() ||
383 OpNo+1 == MI->getNumOperands() ||
384 !MI->getOperand(OpNo+1).isReg())
385 return true;
386 ++OpNo; // Return the high-part.
387 break;
388 }
389 }
Scott Michel9de57a92009-01-26 22:33:37 +0000390
Scott Michel73655bc2008-11-08 18:59:02 +0000391 printOperand(MI, OpNo);
392 return false;
393}
394
395bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
396 unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000397 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000398 const char *ExtraCode) {
399 if (ExtraCode && ExtraCode[0])
400 return true; // Unknown modifier.
401 printMemRegReg(MI, OpNo);
402 return false;
403}
404
405/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
406/// to the current output stream.
407///
408void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
409 ++EmittedInsts;
410 printInstruction(MI);
411}
412
413/// runOnMachineFunction - This uses the printMachineInstruction()
414/// method to print assembly for each instruction.
415///
416bool
417LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
418{
Bill Wendling57f0db82009-02-24 08:30:20 +0000419 this->MF = &MF;
420
Scott Michel73655bc2008-11-08 18:59:02 +0000421 SetupMachineFunction(MF);
422 O << "\n\n";
Scott Michel9de57a92009-01-26 22:33:37 +0000423
Scott Michel73655bc2008-11-08 18:59:02 +0000424 // Print out constants referenced by the function
425 EmitConstantPool(MF.getConstantPool());
426
427 // Print out labels for the function.
428 const Function *F = MF.getFunction();
429
430 SwitchToSection(TAI->SectionForGlobal(F));
Bill Wendling20c568f2009-06-30 22:38:32 +0000431 EmitAlignment(MF.getAlignment(), F);
Scott Michel73655bc2008-11-08 18:59:02 +0000432
433 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000434 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000435 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000436 case Function::LinkerPrivateLinkage:
Scott Michel73655bc2008-11-08 18:59:02 +0000437 case Function::InternalLinkage: // Symbols default to internal.
438 break;
439 case Function::ExternalLinkage:
440 O << "\t.global\t" << CurrentFnName << "\n"
441 << "\t.type\t" << CurrentFnName << ", @function\n";
442 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000443 case Function::WeakAnyLinkage:
444 case Function::WeakODRLinkage:
445 case Function::LinkOnceAnyLinkage:
446 case Function::LinkOnceODRLinkage:
Scott Michel73655bc2008-11-08 18:59:02 +0000447 O << "\t.global\t" << CurrentFnName << "\n";
448 O << "\t.weak_definition\t" << CurrentFnName << "\n";
449 break;
450 }
451 O << CurrentFnName << ":\n";
452
453 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000454 DW->BeginFunction(&MF);
Scott Michel73655bc2008-11-08 18:59:02 +0000455
456 // Print out code for the function.
457 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
458 I != E; ++I) {
459 // Print a label for the basic block.
460 if (I != MF.begin()) {
461 printBasicBlockLabel(I, true, true);
462 O << '\n';
463 }
464 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
465 II != E; ++II) {
466 // Print the assembly for the instruction.
467 printMachineInstruction(II);
468 }
469 }
470
471 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
472
473 // Print out jump tables referenced by the function.
474 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Scott Michel9de57a92009-01-26 22:33:37 +0000475
Scott Michel73655bc2008-11-08 18:59:02 +0000476 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000477 DW->EndFunction(&MF);
Scott Michel9de57a92009-01-26 22:33:37 +0000478
Scott Michel73655bc2008-11-08 18:59:02 +0000479 // We didn't modify anything.
480 return false;
481}
482
483
484bool LinuxAsmPrinter::doInitialization(Module &M) {
485 bool Result = AsmPrinter::doInitialization(M);
Duncan Sands1465d612009-01-28 13:14:17 +0000486 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patel14a55d92009-06-19 21:54:26 +0000487 SwitchToTextSection("\t.text");
Scott Michel73655bc2008-11-08 18:59:02 +0000488 return Result;
489}
490
491/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000492/// Don't print things like \\n or \\0.
David Greene71847812009-07-14 20:18:05 +0000493static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Scott Michel73655bc2008-11-08 18:59:02 +0000494 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
495 Name != E; ++Name)
496 if (isprint(*Name))
497 OS << *Name;
498}
499
500/*!
501 Emit a global variable according to its section, alignment, etc.
Scott Michel9de57a92009-01-26 22:33:37 +0000502
Scott Michel73655bc2008-11-08 18:59:02 +0000503 \note This code was shamelessly copied from the PowerPC's assembly printer,
504 which sort of screams for some kind of refactorization of common code.
505 */
Chris Lattner40bbebd2009-07-21 18:38:57 +0000506void LinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Scott Michel73655bc2008-11-08 18:59:02 +0000507 const TargetData *TD = TM.getTargetData();
508
509 if (!GVar->hasInitializer())
510 return;
511
512 // Check to see if this is a special global used by LLVM, if so, emit it.
513 if (EmitSpecialLLVMGlobal(GVar))
514 return;
515
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000516 std::string name = Mang->getMangledName(GVar);
Scott Michel73655bc2008-11-08 18:59:02 +0000517
518 printVisibility(name, GVar->getVisibility());
519
520 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000521 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000522 return;
Scott Michel73655bc2008-11-08 18:59:02 +0000523 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000524 unsigned Size = TD->getTypeAllocSize(Type);
Scott Michel73655bc2008-11-08 18:59:02 +0000525 unsigned Align = TD->getPreferredAlignmentLog(GVar);
526
527 SwitchToSection(TAI->SectionForGlobal(GVar));
528
529 if (C->isNullValue() && /* FIXME: Verify correct */
530 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000531 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000532 GVar->isWeakForLinker())) {
Scott Michel73655bc2008-11-08 18:59:02 +0000533 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
534
535 if (GVar->hasExternalLinkage()) {
536 O << "\t.global " << name << '\n';
537 O << "\t.type " << name << ", @object\n";
538 O << name << ":\n";
539 O << "\t.zero " << Size << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000540 } else if (GVar->hasLocalLinkage()) {
Scott Michel73655bc2008-11-08 18:59:02 +0000541 O << TAI->getLCOMMDirective() << name << ',' << Size;
542 } else {
543 O << ".comm " << name << ',' << Size;
544 }
545 O << "\t\t" << TAI->getCommentString() << " '";
546 PrintUnmangledNameSafely(GVar, O);
547 O << "'\n";
548 return;
549 }
550
551 switch (GVar->getLinkage()) {
552 // Should never be seen for the CellSPU platform...
Duncan Sands667d4b82009-03-07 15:45:40 +0000553 case GlobalValue::LinkOnceAnyLinkage:
554 case GlobalValue::LinkOnceODRLinkage:
555 case GlobalValue::WeakAnyLinkage:
556 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000557 case GlobalValue::CommonLinkage:
Scott Michel73655bc2008-11-08 18:59:02 +0000558 O << "\t.global " << name << '\n'
559 << "\t.type " << name << ", @object\n"
560 << "\t.weak " << name << '\n';
561 break;
562 case GlobalValue::AppendingLinkage:
563 // FIXME: appending linkage variables should go into a section of
564 // their name or something. For now, just emit them as external.
565 case GlobalValue::ExternalLinkage:
566 // If external or appending, declare as a global symbol
567 O << "\t.global " << name << '\n'
568 << "\t.type " << name << ", @object\n";
569 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000570 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000571 case GlobalValue::LinkerPrivateLinkage:
Scott Michel73655bc2008-11-08 18:59:02 +0000572 case GlobalValue::InternalLinkage:
573 break;
574 default:
Torok Edwindac237e2009-07-08 20:53:28 +0000575 llvm_report_error("Unknown linkage type!");
Scott Michel73655bc2008-11-08 18:59:02 +0000576 }
577
578 EmitAlignment(Align, GVar);
579 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
580 PrintUnmangledNameSafely(GVar, O);
581 O << "'\n";
582
Scott Michel73655bc2008-11-08 18:59:02 +0000583 EmitGlobalConstant(C);
584 O << '\n';
585}
586
Scott Michel73655bc2008-11-08 18:59:02 +0000587/// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
588/// assembly code for a MachineFunction to the given output stream, in a format
589/// that the Linux SPU assembler can deal with.
590///
David Greene71847812009-07-14 20:18:05 +0000591FunctionPass *llvm::createSPUAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000592 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +0000593 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000594 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Scott Michel73655bc2008-11-08 18:59:02 +0000595}
Douglas Gregor1555a232009-06-16 20:12:29 +0000596
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000597// Force static initialization.
598extern "C" void LLVMInitializeCellSPUAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000599 TargetRegistry::RegisterAsmPrinter(TheCellSPUTarget, createSPUAsmPrinterPass);
600}