blob: 01e9dac6d74ac6c98c211a6b818b385079f96cfb [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);
Dan Gohmand735b802008-10-03 15:45:36 +000084 if (MO.isReg()) {
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;
Dan Gohmand735b802008-10-03 15:45:36 +000087 } else if (MO.isImm()) {
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);
Dan Gohmand735b802008-10-03 15:45:36 +0000189 assert(MO.isImm() &&
190 "printMemRegImmS10 first operand is not immedate");
Scott Micheld976c212008-10-30 01:51:48 +0000191 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
192 assert((value >= -(1 << (9+4)) && value <= (1 << (9+4)) - 1)
193 && "Invalid dform s10 offset argument");
194 O << value << "(";
Scott Michel266bc8f2007-12-04 22:23:35 +0000195 printOperand(MI, OpNo+1);
196 O << ")";
197 }
198
199 void
200 printAddr256K(const MachineInstr *MI, unsigned OpNo)
201 {
Scott Michel053c1da2008-01-29 02:16:57 +0000202 /* Note: operand 1 is an offset or symbol name. */
Dan Gohmand735b802008-10-03 15:45:36 +0000203 if (MI->getOperand(OpNo).isImm()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000204 printS16ImmOperand(MI, OpNo);
205 } else {
206 printOp(MI->getOperand(OpNo));
Dan Gohmand735b802008-10-03 15:45:36 +0000207 if (MI->getOperand(OpNo+1).isImm()) {
Scott Michel053c1da2008-01-29 02:16:57 +0000208 int displ = int(MI->getOperand(OpNo+1).getImm());
209 if (displ > 0)
210 O << "+" << displ;
211 else if (displ < 0)
212 O << displ;
213 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000214 }
215 }
216
217 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
218 printOp(MI->getOperand(OpNo));
219 }
220
221 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
222 printOp(MI->getOperand(OpNo));
223 O << "-.";
224 }
225
226 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000227 if (MI->getOperand(OpNo).isImm()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000228 printS16ImmOperand(MI, OpNo);
229 } else {
230 printOp(MI->getOperand(OpNo));
231 O << "@h";
232 }
233 }
234
235 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000236 if (MI->getOperand(OpNo).isImm()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000237 printS16ImmOperand(MI, OpNo);
238 } else {
239 printOp(MI->getOperand(OpNo));
240 O << "@l";
241 }
242 }
243
244 /// Print local store address
245 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
246 printOp(MI->getOperand(OpNo));
247 }
248
249 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000250 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000251 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000252 assert((value >= 0 && value < 16)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000253 && "Invalid negated immediate rotate 7-bit argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000254 O << -value;
255 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000256 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel266bc8f2007-12-04 22:23:35 +0000257 }
258 }
259
260 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000261 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000262 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel266bc8f2007-12-04 22:23:35 +0000263 assert((value >= 0 && value < 32)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000264 && "Invalid negated immediate rotate 7-bit argument");
Scott Michel266bc8f2007-12-04 22:23:35 +0000265 O << -value;
266 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000267 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel266bc8f2007-12-04 22:23:35 +0000268 }
269 }
270
271 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
272 virtual bool doFinalization(Module &M) = 0;
273 };
274
275 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
276 struct VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
277
278 DwarfWriter DW;
Dale Johannesend0377242008-07-09 21:25:06 +0000279 MachineModuleInfo *MMI;
Scott Michel266bc8f2007-12-04 22:23:35 +0000280
Owen Andersoncb371882008-08-21 00:14:44 +0000281 LinuxAsmPrinter(raw_ostream &O, SPUTargetMachine &TM,
Scott Michel266bc8f2007-12-04 22:23:35 +0000282 const TargetAsmInfo *T) :
283 SPUAsmPrinter(O, TM, T),
Dale Johannesend0377242008-07-09 21:25:06 +0000284 DW(O, this, T),
285 MMI(0)
Scott Michel266bc8f2007-12-04 22:23:35 +0000286 { }
287
288 virtual const char *getPassName() const {
289 return "STI CBEA SPU Assembly Printer";
290 }
291
292 bool runOnMachineFunction(MachineFunction &F);
293 bool doInitialization(Module &M);
294 bool doFinalization(Module &M);
295
296 void getAnalysisUsage(AnalysisUsage &AU) const {
297 AU.setPreservesAll();
298 AU.addRequired<MachineModuleInfo>();
299 SPUAsmPrinter::getAnalysisUsage(AU);
300 }
301
Scott Michel266bc8f2007-12-04 22:23:35 +0000302 };
303} // end of anonymous namespace
304
305// Include the auto-generated portion of the assembly writer
306#include "SPUGenAsmWriter.inc"
307
308void SPUAsmPrinter::printOp(const MachineOperand &MO) {
309 switch (MO.getType()) {
310 case MachineOperand::MO_Immediate:
311 cerr << "printOp() does not handle immediate values\n";
312 abort();
313 return;
314
315 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000316 printBasicBlockLabel(MO.getMBB());
Scott Michel266bc8f2007-12-04 22:23:35 +0000317 return;
318 case MachineOperand::MO_JumpTableIndex:
319 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000320 << '_' << MO.getIndex();
Scott Michel266bc8f2007-12-04 22:23:35 +0000321 return;
322 case MachineOperand::MO_ConstantPoolIndex:
323 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000324 << '_' << MO.getIndex();
Scott Michel266bc8f2007-12-04 22:23:35 +0000325 return;
326 case MachineOperand::MO_ExternalSymbol:
327 // Computing the address of an external symbol, not calling it.
328 if (TM.getRelocationModel() != Reloc::Static) {
329 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
330 GVStubs.insert(Name);
331 O << "L" << Name << "$non_lazy_ptr";
332 return;
333 }
334 O << TAI->getGlobalPrefix() << MO.getSymbolName();
335 return;
336 case MachineOperand::MO_GlobalAddress: {
337 // Computing the address of a global symbol, not calling it.
338 GlobalValue *GV = MO.getGlobal();
339 std::string Name = Mang->getValueName(GV);
340
341 // External or weakly linked global variables need non-lazily-resolved
342 // stubs
343 if (TM.getRelocationModel() != Reloc::Static) {
344 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000345 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000346 GVStubs.insert(Name);
347 O << "L" << Name << "$non_lazy_ptr";
348 return;
349 }
350 }
351 O << Name;
352
353 if (GV->hasExternalWeakLinkage())
354 ExtWeakSymbols.insert(GV);
355 return;
356 }
357
358 default:
359 O << "<unknown operand type: " << MO.getType() << ">";
360 return;
361 }
362}
363
364/// PrintAsmOperand - Print out an operand for an inline asm expression.
365///
366bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
367 unsigned AsmVariant,
368 const char *ExtraCode) {
369 // Does this asm operand have a single letter operand modifier?
370 if (ExtraCode && ExtraCode[0]) {
371 if (ExtraCode[1] != 0) return true; // Unknown modifier.
372
373 switch (ExtraCode[0]) {
374 default: return true; // Unknown modifier.
375 case 'L': // Write second word of DImode reference.
376 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000377 if (!MI->getOperand(OpNo).isReg() ||
Scott Michel266bc8f2007-12-04 22:23:35 +0000378 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000379 !MI->getOperand(OpNo+1).isReg())
Scott Michel266bc8f2007-12-04 22:23:35 +0000380 return true;
381 ++OpNo; // Return the high-part.
382 break;
383 }
384 }
385
386 printOperand(MI, OpNo);
387 return false;
388}
389
390bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000391 unsigned OpNo,
Scott Michel266bc8f2007-12-04 22:23:35 +0000392 unsigned AsmVariant,
393 const char *ExtraCode) {
394 if (ExtraCode && ExtraCode[0])
395 return true; // Unknown modifier.
396 printMemRegReg(MI, OpNo);
397 return false;
398}
399
400/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
401/// to the current output stream.
402///
403void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
404 ++EmittedInsts;
405 printInstruction(MI);
406}
407
Scott Michel266bc8f2007-12-04 22:23:35 +0000408/// runOnMachineFunction - This uses the printMachineInstruction()
409/// method to print assembly for each instruction.
410///
411bool
412LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
413{
Scott Michel266bc8f2007-12-04 22:23:35 +0000414 SetupMachineFunction(MF);
415 O << "\n\n";
416
417 // Print out constants referenced by the function
418 EmitConstantPool(MF.getConstantPool());
419
420 // Print out labels for the function.
421 const Function *F = MF.getFunction();
422
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000423 SwitchToSection(TAI->SectionForGlobal(F));
Scott Michel266bc8f2007-12-04 22:23:35 +0000424 EmitAlignment(3, F);
425
426 switch (F->getLinkage()) {
427 default: assert(0 && "Unknown linkage type!");
428 case Function::InternalLinkage: // Symbols default to internal.
429 break;
430 case Function::ExternalLinkage:
431 O << "\t.global\t" << CurrentFnName << "\n"
432 << "\t.type\t" << CurrentFnName << ", @function\n";
433 break;
434 case Function::WeakLinkage:
435 case Function::LinkOnceLinkage:
436 O << "\t.global\t" << CurrentFnName << "\n";
437 O << "\t.weak_definition\t" << CurrentFnName << "\n";
438 break;
439 }
440 O << CurrentFnName << ":\n";
441
442 // Emit pre-function debug information.
443 DW.BeginFunction(&MF);
444
445 // Print out code for the function.
446 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
447 I != E; ++I) {
448 // Print a label for the basic block.
449 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000450 printBasicBlockLabel(I, true, true);
Scott Michel266bc8f2007-12-04 22:23:35 +0000451 O << '\n';
452 }
453 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
454 II != E; ++II) {
455 // Print the assembly for the instruction.
Scott Michel266bc8f2007-12-04 22:23:35 +0000456 printMachineInstruction(II);
457 }
458 }
459
460 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
461
462 // Print out jump tables referenced by the function.
463 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
464
465 // Emit post-function debug information.
Bill Wendlingd751c642008-09-26 00:28:12 +0000466 DW.EndFunction(&MF);
Scott Michel266bc8f2007-12-04 22:23:35 +0000467
468 // We didn't modify anything.
469 return false;
470}
471
472
473bool LinuxAsmPrinter::doInitialization(Module &M) {
474 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikov90a7b882008-09-24 22:11:42 +0000475 SwitchToTextSection("\t.text");
Scott Michel266bc8f2007-12-04 22:23:35 +0000476 // Emit initial debug information.
477 DW.BeginModule(&M);
Dale Johannesend0377242008-07-09 21:25:06 +0000478 MMI = getAnalysisToUpdate<MachineModuleInfo>();
479 DW.SetModuleInfo(MMI);
Scott Michel266bc8f2007-12-04 22:23:35 +0000480 return Result;
481}
482
483bool LinuxAsmPrinter::doFinalization(Module &M) {
484 const TargetData *TD = TM.getTargetData();
485
486 // Print out module-level global variables here.
487 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
488 I != E; ++I) {
489 if (!I->hasInitializer()) continue; // External global require no code
490
491 // Check to see if this is a special global used by LLVM, if so, emit it.
492 if (EmitSpecialLLVMGlobal(I))
493 continue;
494
495 std::string name = Mang->getValueName(I);
496 Constant *C = I->getInitializer();
497 unsigned Size = TD->getTypeStoreSize(C->getType());
498 unsigned Align = TD->getPreferredAlignmentLog(I);
499
500 if (C->isNullValue() && /* FIXME: Verify correct */
501 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000502 I->hasLinkOnceLinkage() || I->hasCommonLinkage() ||
Scott Michel266bc8f2007-12-04 22:23:35 +0000503 (I->hasExternalLinkage() && !I->hasSection()))) {
504 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
505 if (I->hasExternalLinkage()) {
506 // External linkage globals -> .bss section
507 // FIXME: Want to set the global variable's section so that
508 // SwitchToDataSection emits the ".section" directive
509 SwitchToDataSection("\t.section\t.bss", I);
510 O << "\t.global\t" << name << '\n';
511 O << "\t.align\t" << Align << '\n';
512 O << "\t.type\t" << name << ", @object\n";
513 O << "\t.size\t" << name << ", " << Size << '\n';
514 O << name << ":\n";
515 O << "\t.zero\t" << Size;
516 } else if (I->hasInternalLinkage()) {
517 SwitchToDataSection("\t.data", I);
Scott Michel053c1da2008-01-29 02:16:57 +0000518 O << ".local " << name << "\n";
519 O << TAI->getCOMMDirective() << name << "," << Size << "," << Align << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000520 } else {
521 SwitchToDataSection("\t.data", I);
522 O << ".comm " << name << "," << Size;
523 }
524 O << "\t\t# '" << I->getName() << "'\n";
525 } else {
526 switch (I->getLinkage()) {
527 case GlobalValue::LinkOnceLinkage:
528 case GlobalValue::WeakLinkage:
Dale Johannesenaafce772008-05-14 20:12:51 +0000529 case GlobalValue::CommonLinkage:
Scott Michel266bc8f2007-12-04 22:23:35 +0000530 O << "\t.global " << name << '\n'
531 << "\t.weak_definition " << name << '\n';
532 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
533 break;
534 case GlobalValue::AppendingLinkage:
535 // FIXME: appending linkage variables should go into a section of
536 // their name or something. For now, just emit them as external.
537 case GlobalValue::ExternalLinkage:
538 // If external or appending, declare as a global symbol
539 O << "\t.global " << name << "\n";
540 // FALL THROUGH
541 case GlobalValue::InternalLinkage:
542 if (I->isConstant()) {
543 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
544 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
Anton Korobeynikov90a7b882008-09-24 22:11:42 +0000545 SwitchToDataSection("\t.cstring", I);
Scott Michel266bc8f2007-12-04 22:23:35 +0000546 break;
547 }
548 }
549
550 SwitchToDataSection("\t.data", I);
551 break;
552 default:
553 cerr << "Unknown linkage type!";
554 abort();
555 }
556
557 EmitAlignment(Align, I);
558 O << name << ":\t\t\t\t# '" << I->getName() << "'\n";
559
560 // If the initializer is a extern weak symbol, remember to emit the weak
561 // reference!
562 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
563 if (GV->hasExternalWeakLinkage())
564 ExtWeakSymbols.insert(GV);
565
566 EmitGlobalConstant(C);
567 O << '\n';
568 }
569 }
570
571 // Output stubs for dynamically-linked functions
572 if (TM.getRelocationModel() == Reloc::PIC_) {
573 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
574 i != e; ++i) {
575 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
576 "pure_instructions,32");
577 EmitAlignment(4);
578 O << "L" << *i << "$stub:\n";
579 O << "\t.indirect_symbol " << *i << "\n";
580 O << "\tmflr r0\n";
581 O << "\tbcl 20,31,L0$" << *i << "\n";
582 O << "L0$" << *i << ":\n";
583 O << "\tmflr r11\n";
584 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
585 O << "\tmtlr r0\n";
586 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
587 O << "\tmtctr r12\n";
588 O << "\tbctr\n";
589 SwitchToDataSection(".lazy_symbol_pointer");
590 O << "L" << *i << "$lazy_ptr:\n";
591 O << "\t.indirect_symbol " << *i << "\n";
592 O << "\t.long dyld_stub_binding_helper\n";
593 }
594 } else {
595 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
596 i != e; ++i) {
597 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
598 "pure_instructions,16");
599 EmitAlignment(4);
600 O << "L" << *i << "$stub:\n";
601 O << "\t.indirect_symbol " << *i << "\n";
602 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
603 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
604 O << "\tmtctr r12\n";
605 O << "\tbctr\n";
606 SwitchToDataSection(".lazy_symbol_pointer");
607 O << "L" << *i << "$lazy_ptr:\n";
608 O << "\t.indirect_symbol " << *i << "\n";
609 O << "\t.long dyld_stub_binding_helper\n";
610 }
611 }
612
613 O << "\n";
614
615 // Output stubs for external and common global variables.
616 if (GVStubs.begin() != GVStubs.end()) {
617 SwitchToDataSection(".non_lazy_symbol_pointer");
618 for (std::set<std::string>::iterator I = GVStubs.begin(),
619 E = GVStubs.end(); I != E; ++I) {
620 O << "L" << *I << "$non_lazy_ptr:\n";
621 O << "\t.indirect_symbol " << *I << "\n";
622 O << "\t.long\t0\n";
623 }
624 }
625
626 // Emit initial debug information.
627 DW.EndModule();
628
629 // Emit ident information
Scott Michel86c041f2007-12-20 00:44:13 +0000630 O << "\t.ident\t\"(llvm 2.2+) STI CBEA Cell SPU backend\"\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000631
632 return AsmPrinter::doFinalization(M);
633}
634
635
636
637/// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
638/// assembly code for a MachineFunction to the given output stream, in a format
639/// that the Linux SPU assembler can deal with.
640///
Owen Andersoncb371882008-08-21 00:14:44 +0000641FunctionPass *llvm::createSPUAsmPrinterPass(raw_ostream &o,
Scott Michel266bc8f2007-12-04 22:23:35 +0000642 SPUTargetMachine &tm) {
643 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());
644}
645