blob: 0c36457913761cad0deee2a8746bcbf59eb0d89f [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"
Edwin Török4d9756a2009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
pingbakddfd8692008-11-08 18:59:02 +000034#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000035#include "llvm/Support/FormattedStream.h"
pingbakddfd8692008-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 Dunbarfe5939f2009-07-15 20:24:03 +000040#include "llvm/Target/TargetRegistry.h"
pingbakddfd8692008-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 Wendling4f405312009-02-24 08:30:20 +000051 class VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
pingbakddfd8692008-11-08 18:59:02 +000052 std::set<std::string> FnStubs, GVStubs;
Bill Wendling4f405312009-02-24 08:30:20 +000053 public:
David Greene302008d2009-07-14 20:18:05 +000054 explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000055 const TargetAsmInfo *T, bool V) :
56 AsmPrinter(O, TM, T, V) {}
pingbakddfd8692008-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 Michelc899a122009-01-26 22:33:37 +000095
pingbakddfd8692008-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 Michelc899a122009-01-26 22:33:37 +0000100
101
pingbakddfd8692008-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 Michelc899a122009-01-26 22:33:37 +0000120
pingbakddfd8692008-11-08 18:59:02 +0000121 void
Scott Michel06eabde2008-12-27 04:51:36 +0000122 printShufAddr(const MachineInstr *MI, unsigned OpNo)
pingbakddfd8692008-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 Michelc899a122009-01-26 22:33:37 +0000148
pingbakddfd8692008-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 Michel06eabde2008-12-27 04:51:36 +0000188 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
pingbakddfd8692008-11-08 18:59:02 +0000189 {
Chris Lattner4c4fb3a2009-01-21 18:38:18 +0000190 assert(MI->getOperand(OpNo).isImm() &&
191 "printDFormAddr first operand is not immediate");
pingbakddfd8692008-11-08 18:59:02 +0000192 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michelffca41d2008-11-20 05:01:09 +0000193 int16_t value16 = int16_t(value);
194 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
pingbakddfd8692008-11-08 18:59:02 +0000195 && "Invalid dform s10 offset argument");
Scott Michel06eabde2008-12-27 04:51:36 +0000196 O << (value16 & ~0xf) << "(";
pingbakddfd8692008-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 Michel61895fe2008-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 << ".+";
pingbakddfd8692008-11-08 18:59:02 +0000236 printOp(MI->getOperand(OpNo));
pingbakddfd8692008-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 {
Edwin Törökbd448e32009-07-14 16:55:14 +0000269 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
pingbakddfd8692008-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 Michelc630c412008-11-24 17:11:17 +0000276 assert((value >= 0 && value <= 32)
pingbakddfd8692008-11-08 18:59:02 +0000277 && "Invalid negated immediate rotate 7-bit argument");
278 O << -value;
279 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000280 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
pingbakddfd8692008-11-08 18:59:02 +0000281 }
282 }
283
284 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
285 //! Assembly printer cleanup after function has been emitted
286 virtual bool doFinalization(Module &M) = 0;
287 };
288
289 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000290 class VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
Devang Patelaa1e8432009-01-08 23:40:34 +0000291 DwarfWriter *DW;
Bill Wendling4f405312009-02-24 08:30:20 +0000292 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000293 explicit LinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000294 const TargetAsmInfo *T, bool V)
295 : SPUAsmPrinter(O, TM, T, 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:
Edwin Török4d9756a2009-07-08 20:53:28 +0000324 llvm_report_error("printOp() does not handle immediate values");
pingbakddfd8692008-11-08 18:59:02 +0000325 return;
326
327 case MachineOperand::MO_MachineBasicBlock:
328 printBasicBlockLabel(MO.getMBB());
329 return;
330 case MachineOperand::MO_JumpTableIndex:
331 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
332 << '_' << MO.getIndex();
333 return;
334 case MachineOperand::MO_ConstantPoolIndex:
335 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
336 << '_' << MO.getIndex();
337 return;
338 case MachineOperand::MO_ExternalSymbol:
339 // Computing the address of an external symbol, not calling it.
340 if (TM.getRelocationModel() != Reloc::Static) {
341 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
342 GVStubs.insert(Name);
343 O << "L" << Name << "$non_lazy_ptr";
344 return;
345 }
346 O << TAI->getGlobalPrefix() << MO.getSymbolName();
347 return;
348 case MachineOperand::MO_GlobalAddress: {
349 // Computing the address of a global symbol, not calling it.
350 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000351 std::string Name = Mang->getMangledName(GV);
pingbakddfd8692008-11-08 18:59:02 +0000352
353 // External or weakly linked global variables need non-lazily-resolved
354 // stubs
355 if (TM.getRelocationModel() != Reloc::Static) {
356 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
357 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
358 GVStubs.insert(Name);
359 O << "L" << Name << "$non_lazy_ptr";
360 return;
361 }
362 }
363 O << Name;
pingbakddfd8692008-11-08 18:59:02 +0000364 return;
365 }
366
367 default:
368 O << "<unknown operand type: " << MO.getType() << ">";
369 return;
370 }
371}
372
373/// PrintAsmOperand - Print out an operand for an inline asm expression.
374///
375bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michelc899a122009-01-26 22:33:37 +0000376 unsigned AsmVariant,
pingbakddfd8692008-11-08 18:59:02 +0000377 const char *ExtraCode) {
378 // Does this asm operand have a single letter operand modifier?
379 if (ExtraCode && ExtraCode[0]) {
380 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michelc899a122009-01-26 22:33:37 +0000381
pingbakddfd8692008-11-08 18:59:02 +0000382 switch (ExtraCode[0]) {
383 default: return true; // Unknown modifier.
Scott Michelc899a122009-01-26 22:33:37 +0000384 case 'L': // Write second word of DImode reference.
pingbakddfd8692008-11-08 18:59:02 +0000385 // Verify that this operand has two consecutive registers.
386 if (!MI->getOperand(OpNo).isReg() ||
387 OpNo+1 == MI->getNumOperands() ||
388 !MI->getOperand(OpNo+1).isReg())
389 return true;
390 ++OpNo; // Return the high-part.
391 break;
392 }
393 }
Scott Michelc899a122009-01-26 22:33:37 +0000394
pingbakddfd8692008-11-08 18:59:02 +0000395 printOperand(MI, OpNo);
396 return false;
397}
398
399bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
400 unsigned OpNo,
Scott Michelc899a122009-01-26 22:33:37 +0000401 unsigned AsmVariant,
pingbakddfd8692008-11-08 18:59:02 +0000402 const char *ExtraCode) {
403 if (ExtraCode && ExtraCode[0])
404 return true; // Unknown modifier.
405 printMemRegReg(MI, OpNo);
406 return false;
407}
408
409/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
410/// to the current output stream.
411///
412void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
413 ++EmittedInsts;
414 printInstruction(MI);
415}
416
417/// runOnMachineFunction - This uses the printMachineInstruction()
418/// method to print assembly for each instruction.
419///
420bool
421LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
422{
Bill Wendling4f405312009-02-24 08:30:20 +0000423 this->MF = &MF;
424
pingbakddfd8692008-11-08 18:59:02 +0000425 SetupMachineFunction(MF);
426 O << "\n\n";
Scott Michelc899a122009-01-26 22:33:37 +0000427
pingbakddfd8692008-11-08 18:59:02 +0000428 // Print out constants referenced by the function
429 EmitConstantPool(MF.getConstantPool());
430
431 // Print out labels for the function.
432 const Function *F = MF.getFunction();
433
434 SwitchToSection(TAI->SectionForGlobal(F));
Bill Wendling25a8ae32009-06-30 22:38:32 +0000435 EmitAlignment(MF.getAlignment(), F);
pingbakddfd8692008-11-08 18:59:02 +0000436
437 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000438 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000439 case Function::PrivateLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000440 case Function::InternalLinkage: // Symbols default to internal.
441 break;
442 case Function::ExternalLinkage:
443 O << "\t.global\t" << CurrentFnName << "\n"
444 << "\t.type\t" << CurrentFnName << ", @function\n";
445 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000446 case Function::WeakAnyLinkage:
447 case Function::WeakODRLinkage:
448 case Function::LinkOnceAnyLinkage:
449 case Function::LinkOnceODRLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000450 O << "\t.global\t" << CurrentFnName << "\n";
451 O << "\t.weak_definition\t" << CurrentFnName << "\n";
452 break;
453 }
454 O << CurrentFnName << ":\n";
455
456 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000457 DW->BeginFunction(&MF);
pingbakddfd8692008-11-08 18:59:02 +0000458
459 // Print out code for the function.
460 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
461 I != E; ++I) {
462 // Print a label for the basic block.
463 if (I != MF.begin()) {
464 printBasicBlockLabel(I, true, true);
465 O << '\n';
466 }
467 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
468 II != E; ++II) {
469 // Print the assembly for the instruction.
470 printMachineInstruction(II);
471 }
472 }
473
474 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
475
476 // Print out jump tables referenced by the function.
477 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Scott Michelc899a122009-01-26 22:33:37 +0000478
pingbakddfd8692008-11-08 18:59:02 +0000479 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000480 DW->EndFunction(&MF);
Scott Michelc899a122009-01-26 22:33:37 +0000481
pingbakddfd8692008-11-08 18:59:02 +0000482 // We didn't modify anything.
483 return false;
484}
485
486
487bool LinuxAsmPrinter::doInitialization(Module &M) {
488 bool Result = AsmPrinter::doInitialization(M);
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000489 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patel1a454842009-06-19 21:54:26 +0000490 SwitchToTextSection("\t.text");
pingbakddfd8692008-11-08 18:59:02 +0000491 return Result;
492}
493
494/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000495/// Don't print things like \\n or \\0.
David Greene302008d2009-07-14 20:18:05 +0000496static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
pingbakddfd8692008-11-08 18:59:02 +0000497 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
498 Name != E; ++Name)
499 if (isprint(*Name))
500 OS << *Name;
501}
502
503/*!
504 Emit a global variable according to its section, alignment, etc.
Scott Michelc899a122009-01-26 22:33:37 +0000505
pingbakddfd8692008-11-08 18:59:02 +0000506 \note This code was shamelessly copied from the PowerPC's assembly printer,
507 which sort of screams for some kind of refactorization of common code.
508 */
509void LinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
510 const TargetData *TD = TM.getTargetData();
511
512 if (!GVar->hasInitializer())
513 return;
514
515 // Check to see if this is a special global used by LLVM, if so, emit it.
516 if (EmitSpecialLLVMGlobal(GVar))
517 return;
518
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000519 std::string name = Mang->getMangledName(GVar);
pingbakddfd8692008-11-08 18:59:02 +0000520
521 printVisibility(name, GVar->getVisibility());
522
523 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000524 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000525 return;
pingbakddfd8692008-11-08 18:59:02 +0000526 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000527 unsigned Size = TD->getTypeAllocSize(Type);
pingbakddfd8692008-11-08 18:59:02 +0000528 unsigned Align = TD->getPreferredAlignmentLog(GVar);
529
530 SwitchToSection(TAI->SectionForGlobal(GVar));
531
532 if (C->isNullValue() && /* FIXME: Verify correct */
533 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000534 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000535 GVar->isWeakForLinker())) {
pingbakddfd8692008-11-08 18:59:02 +0000536 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
537
538 if (GVar->hasExternalLinkage()) {
539 O << "\t.global " << name << '\n';
540 O << "\t.type " << name << ", @object\n";
541 O << name << ":\n";
542 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000543 } else if (GVar->hasLocalLinkage()) {
pingbakddfd8692008-11-08 18:59:02 +0000544 O << TAI->getLCOMMDirective() << name << ',' << Size;
545 } else {
546 O << ".comm " << name << ',' << Size;
547 }
548 O << "\t\t" << TAI->getCommentString() << " '";
549 PrintUnmangledNameSafely(GVar, O);
550 O << "'\n";
551 return;
552 }
553
554 switch (GVar->getLinkage()) {
555 // Should never be seen for the CellSPU platform...
Duncan Sands19d161f2009-03-07 15:45:40 +0000556 case GlobalValue::LinkOnceAnyLinkage:
557 case GlobalValue::LinkOnceODRLinkage:
558 case GlobalValue::WeakAnyLinkage:
559 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000560 case GlobalValue::CommonLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000561 O << "\t.global " << name << '\n'
562 << "\t.type " << name << ", @object\n"
563 << "\t.weak " << name << '\n';
564 break;
565 case GlobalValue::AppendingLinkage:
566 // FIXME: appending linkage variables should go into a section of
567 // their name or something. For now, just emit them as external.
568 case GlobalValue::ExternalLinkage:
569 // If external or appending, declare as a global symbol
570 O << "\t.global " << name << '\n'
571 << "\t.type " << name << ", @object\n";
572 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000573 case GlobalValue::PrivateLinkage:
pingbakddfd8692008-11-08 18:59:02 +0000574 case GlobalValue::InternalLinkage:
575 break;
576 default:
Edwin Török4d9756a2009-07-08 20:53:28 +0000577 llvm_report_error("Unknown linkage type!");
pingbakddfd8692008-11-08 18:59:02 +0000578 }
579
580 EmitAlignment(Align, GVar);
581 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
582 PrintUnmangledNameSafely(GVar, O);
583 O << "'\n";
584
pingbakddfd8692008-11-08 18:59:02 +0000585 EmitGlobalConstant(C);
586 O << '\n';
587}
588
589bool LinuxAsmPrinter::doFinalization(Module &M) {
590 // Print out module-level global variables here.
591 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
592 I != E; ++I)
593 printModuleLevelGV(I);
594
pingbakddfd8692008-11-08 18:59:02 +0000595 return AsmPrinter::doFinalization(M);
596}
597
598/// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
599/// assembly code for a MachineFunction to the given output stream, in a format
600/// that the Linux SPU assembler can deal with.
601///
David Greene302008d2009-07-14 20:18:05 +0000602FunctionPass *llvm::createSPUAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000603 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000604 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000605 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
pingbakddfd8692008-11-08 18:59:02 +0000606}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000607
Anton Korobeynikovbab832e2009-06-19 19:36:55 +0000608namespace {
609 static struct Register {
610 Register() {
611 SPUTargetMachine::registerAsmPrinter(createSPUAsmPrinterPass);
612 }
613 } Registrator;
614}
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000615
616// Force static initialization.
617extern "C" void LLVMInitializeCellSPUAsmPrinter() {
618 extern Target TheCellSPUTarget;
619 TargetRegistry::RegisterAsmPrinter(TheCellSPUTarget, createSPUAsmPrinterPass);
620}