blob: 6981fef3446e2e195411e441a4b695f7be698070 [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"
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"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000028#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000029#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000030#include "llvm/MC/MCSymbol.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Scott Michel73655bc2008-11-08 18:59:02 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetOptions.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetRegisterInfo.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000035#include "llvm/Target/TargetRegistry.h"
Scott Michel73655bc2008-11-08 18:59:02 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/StringExtras.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000038#include "llvm/Support/CommandLine.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/FormattedStream.h"
42#include "llvm/Support/Mangler.h"
43#include "llvm/Support/MathExtras.h"
Scott Michel73655bc2008-11-08 18:59:02 +000044#include <set>
45using namespace llvm;
46
47namespace {
48 STATISTIC(EmittedInsts, "Number of machine instrs printed");
49
50 const std::string bss_section(".bss");
51
Nick Lewycky6726b6d2009-10-25 06:33:48 +000052 class SPUAsmPrinter : public AsmPrinter {
Scott Michel73655bc2008-11-08 18:59:02 +000053 std::set<std::string> FnStubs, GVStubs;
Bill Wendling57f0db82009-02-24 08:30:20 +000054 public:
David Greene71847812009-07-14 20:18:05 +000055 explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +000056 const MCAsmInfo *T, bool V) :
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000057 AsmPrinter(O, TM, T, V) {}
Scott Michel73655bc2008-11-08 18:59:02 +000058
59 virtual const char *getPassName() const {
60 return "STI CBEA SPU Assembly Printer";
61 }
62
63 SPUTargetMachine &getTM() {
64 return static_cast<SPUTargetMachine&>(TM);
65 }
66
67 /// printInstruction - This method is automatically generated by tablegen
Chris Lattner05af2612009-09-13 20:08:00 +000068 /// from the instruction set description.
Chris Lattner41aefdc2009-08-08 01:32:19 +000069 void printInstruction(const MachineInstr *MI);
Chris Lattnerd95148f2009-09-13 20:19:22 +000070 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000071
Scott Michel73655bc2008-11-08 18:59:02 +000072
73 void printMachineInstruction(const MachineInstr *MI);
74 void printOp(const MachineOperand &MO);
75
76 /// printRegister - Print register according to target requirements.
77 ///
78 void printRegister(const MachineOperand &MO, bool R0AsZero) {
79 unsigned RegNo = MO.getReg();
80 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
81 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000082 O << getRegisterName(RegNo);
Scott Michel73655bc2008-11-08 18:59:02 +000083 }
84
85 void printOperand(const MachineInstr *MI, unsigned OpNo) {
86 const MachineOperand &MO = MI->getOperand(OpNo);
87 if (MO.isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +000088 O << getRegisterName(MO.getReg());
Scott Michel73655bc2008-11-08 18:59:02 +000089 } 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);
Chris Lattner762ccea2009-09-13 20:31:40 +0000155 O << getRegisterName(MO.getReg()) << ", ";
Scott Michel73655bc2008-11-08 18:59:02 +0000156 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 Michelf0569be2008-12-27 04:51:36 +0000187 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000188 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000189 assert(MI->getOperand(OpNo).isImm() &&
190 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000191 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000192 int16_t value16 = int16_t(value);
193 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000194 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000195 O << (value16 & ~0xf) << "(";
Scott Michel73655bc2008-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 Michelaedc6372008-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 << ".+";
Scott Michel73655bc2008-11-08 18:59:02 +0000235 printOp(MI->getOperand(OpNo));
Scott Michel73655bc2008-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 {
Torok Edwinc23197a2009-07-14 16:55:14 +0000268 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000269 }
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 Michel104de432008-11-24 17:11:17 +0000275 assert((value >= 0 && value <= 32)
Scott Michel73655bc2008-11-08 18:59:02 +0000276 && "Invalid negated immediate rotate 7-bit argument");
277 O << -value;
278 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000279 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000280 }
281 }
282
283 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Scott Michel73655bc2008-11-08 18:59:02 +0000284 };
285
286 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000287 class LinuxAsmPrinter : public SPUAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000288 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000289 explicit LinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +0000290 const MCAsmInfo *T, bool V)
Bob Wilsonb6e47422009-09-30 21:24:45 +0000291 : SPUAsmPrinter(O, TM, T, V) {}
Scott Michel73655bc2008-11-08 18:59:02 +0000292
293 virtual const char *getPassName() const {
294 return "STI CBEA SPU Assembly Printer";
295 }
Scott Michel9de57a92009-01-26 22:33:37 +0000296
Scott Michel73655bc2008-11-08 18:59:02 +0000297 bool runOnMachineFunction(MachineFunction &F);
Scott Michel9de57a92009-01-26 22:33:37 +0000298
Scott Michel73655bc2008-11-08 18:59:02 +0000299 void getAnalysisUsage(AnalysisUsage &AU) const {
300 AU.setPreservesAll();
301 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000302 AU.addRequired<DwarfWriter>();
Scott Michel73655bc2008-11-08 18:59:02 +0000303 SPUAsmPrinter::getAnalysisUsage(AU);
304 }
305
306 //! Emit a global variable according to its section and type
Chris Lattner40bbebd2009-07-21 18:38:57 +0000307 void PrintGlobalVariable(const GlobalVariable* GVar);
Scott Michel73655bc2008-11-08 18:59:02 +0000308 };
309} // end of anonymous namespace
310
311// Include the auto-generated portion of the assembly writer
312#include "SPUGenAsmWriter.inc"
313
314void SPUAsmPrinter::printOp(const MachineOperand &MO) {
315 switch (MO.getType()) {
316 case MachineOperand::MO_Immediate:
Torok Edwindac237e2009-07-08 20:53:28 +0000317 llvm_report_error("printOp() does not handle immediate values");
Scott Michel73655bc2008-11-08 18:59:02 +0000318 return;
319
320 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000321 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Scott Michel73655bc2008-11-08 18:59:02 +0000322 return;
323 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000324 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000325 << '_' << MO.getIndex();
326 return;
327 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000328 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000329 << '_' << MO.getIndex();
330 return;
331 case MachineOperand::MO_ExternalSymbol:
332 // Computing the address of an external symbol, not calling it.
333 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000334 std::string Name(MAI->getGlobalPrefix()); Name += MO.getSymbolName();
Scott Michel73655bc2008-11-08 18:59:02 +0000335 GVStubs.insert(Name);
336 O << "L" << Name << "$non_lazy_ptr";
337 return;
338 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000339 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Scott Michel73655bc2008-11-08 18:59:02 +0000340 return;
341 case MachineOperand::MO_GlobalAddress: {
342 // Computing the address of a global symbol, not calling it.
343 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000344 std::string Name = Mang->getMangledName(GV);
Scott Michel73655bc2008-11-08 18:59:02 +0000345
346 // External or weakly linked global variables need non-lazily-resolved
347 // stubs
348 if (TM.getRelocationModel() != Reloc::Static) {
349 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
350 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
351 GVStubs.insert(Name);
352 O << "L" << Name << "$non_lazy_ptr";
353 return;
354 }
355 }
356 O << Name;
Scott Michel73655bc2008-11-08 18:59:02 +0000357 return;
358 }
359
360 default:
361 O << "<unknown operand type: " << MO.getType() << ">";
362 return;
363 }
364}
365
366/// PrintAsmOperand - Print out an operand for an inline asm expression.
367///
368bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000369 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000370 const char *ExtraCode) {
371 // Does this asm operand have a single letter operand modifier?
372 if (ExtraCode && ExtraCode[0]) {
373 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000374
Scott Michel73655bc2008-11-08 18:59:02 +0000375 switch (ExtraCode[0]) {
376 default: return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000377 case 'L': // Write second word of DImode reference.
Scott Michel73655bc2008-11-08 18:59:02 +0000378 // Verify that this operand has two consecutive registers.
379 if (!MI->getOperand(OpNo).isReg() ||
380 OpNo+1 == MI->getNumOperands() ||
381 !MI->getOperand(OpNo+1).isReg())
382 return true;
383 ++OpNo; // Return the high-part.
384 break;
385 }
386 }
Scott Michel9de57a92009-01-26 22:33:37 +0000387
Scott Michel73655bc2008-11-08 18:59:02 +0000388 printOperand(MI, OpNo);
389 return false;
390}
391
392bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
393 unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000394 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000395 const char *ExtraCode) {
396 if (ExtraCode && ExtraCode[0])
397 return true; // Unknown modifier.
398 printMemRegReg(MI, OpNo);
399 return false;
400}
401
402/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
403/// to the current output stream.
404///
405void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
406 ++EmittedInsts;
Devang Patelaf0e2722009-10-06 02:19:11 +0000407 processDebugLoc(MI, true);
Scott Michel73655bc2008-11-08 18:59:02 +0000408 printInstruction(MI);
David Greene1924aab2009-11-13 21:34:57 +0000409 if (VerboseAsm)
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000410 EmitComments(*MI);
Devang Patelaf0e2722009-10-06 02:19:11 +0000411 processDebugLoc(MI, false);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000412 O << '\n';
Scott Michel73655bc2008-11-08 18:59:02 +0000413}
414
415/// runOnMachineFunction - This uses the printMachineInstruction()
416/// method to print assembly for each instruction.
417///
Chris Lattner634cca32009-09-09 20:34:59 +0000418bool LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
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
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000430 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
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:
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000440 O << "\t.global\t";
441 CurrentFnSym->print(O, MAI);
442 O << "\n" << "\t.type\t";
443 CurrentFnSym->print(O, MAI);
444 O << ", @function\n";
Scott Michel73655bc2008-11-08 18:59:02 +0000445 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000446 case Function::WeakAnyLinkage:
447 case Function::WeakODRLinkage:
448 case Function::LinkOnceAnyLinkage:
449 case Function::LinkOnceODRLinkage:
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000450 O << "\t.global\t";
451 CurrentFnSym->print(O, MAI);
452 O << "\n";
453 O << "\t.weak_definition\t";
454 CurrentFnSym->print(O, MAI);
455 O << "\n";
Scott Michel73655bc2008-11-08 18:59:02 +0000456 break;
457 }
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000458
459 CurrentFnSym->print(O, MAI);
460 O << ":\n";
Scott Michel73655bc2008-11-08 18:59:02 +0000461
462 // Emit pre-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000463 DW->BeginFunction(&MF);
Scott Michel73655bc2008-11-08 18:59:02 +0000464
465 // Print out code for the function.
466 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
467 I != E; ++I) {
468 // Print a label for the basic block.
469 if (I != MF.begin()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000470 EmitBasicBlockStart(I);
Scott Michel73655bc2008-11-08 18:59:02 +0000471 }
472 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
473 II != E; ++II) {
474 // Print the assembly for the instruction.
475 printMachineInstruction(II);
476 }
477 }
478
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000479 O << "\t.size\t";
480 CurrentFnSym->print(O, MAI);
481 O << ",.-";
482 CurrentFnSym->print(O, MAI);
483 O << "\n";
Scott Michel73655bc2008-11-08 18:59:02 +0000484
485 // Print out jump tables referenced by the function.
486 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Scott Michel9de57a92009-01-26 22:33:37 +0000487
Scott Michel73655bc2008-11-08 18:59:02 +0000488 // Emit post-function debug information.
Devang Pateleb3fc282009-01-08 23:40:34 +0000489 DW->EndFunction(&MF);
Scott Michel9de57a92009-01-26 22:33:37 +0000490
Scott Michel73655bc2008-11-08 18:59:02 +0000491 // We didn't modify anything.
492 return false;
493}
494
495
Scott Michel73655bc2008-11-08 18:59:02 +0000496/*!
497 Emit a global variable according to its section, alignment, etc.
Scott Michel9de57a92009-01-26 22:33:37 +0000498
Scott Michel73655bc2008-11-08 18:59:02 +0000499 \note This code was shamelessly copied from the PowerPC's assembly printer,
500 which sort of screams for some kind of refactorization of common code.
501 */
Chris Lattner40bbebd2009-07-21 18:38:57 +0000502void LinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Scott Michel73655bc2008-11-08 18:59:02 +0000503 const TargetData *TD = TM.getTargetData();
504
505 if (!GVar->hasInitializer())
506 return;
507
508 // Check to see if this is a special global used by LLVM, if so, emit it.
509 if (EmitSpecialLLVMGlobal(GVar))
510 return;
511
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000512 std::string name = Mang->getMangledName(GVar);
Scott Michel73655bc2008-11-08 18:59:02 +0000513
514 printVisibility(name, GVar->getVisibility());
515
516 Constant *C = GVar->getInitializer();
517 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000518 unsigned Size = TD->getTypeAllocSize(Type);
Scott Michel73655bc2008-11-08 18:59:02 +0000519 unsigned Align = TD->getPreferredAlignmentLog(GVar);
520
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000521 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
522 TM));
Scott Michel73655bc2008-11-08 18:59:02 +0000523
524 if (C->isNullValue() && /* FIXME: Verify correct */
525 !GVar->hasSection() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000526 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands667d4b82009-03-07 15:45:40 +0000527 GVar->isWeakForLinker())) {
Scott Michel73655bc2008-11-08 18:59:02 +0000528 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
529
530 if (GVar->hasExternalLinkage()) {
531 O << "\t.global " << name << '\n';
532 O << "\t.type " << name << ", @object\n";
533 O << name << ":\n";
534 O << "\t.zero " << Size << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000535 } else if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000536 O << MAI->getLCOMMDirective() << name << ',' << Size;
Scott Michel73655bc2008-11-08 18:59:02 +0000537 } else {
538 O << ".comm " << name << ',' << Size;
539 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000540 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000541 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Scott Michel73655bc2008-11-08 18:59:02 +0000542 O << "'\n";
543 return;
544 }
545
546 switch (GVar->getLinkage()) {
547 // Should never be seen for the CellSPU platform...
Duncan Sands667d4b82009-03-07 15:45:40 +0000548 case GlobalValue::LinkOnceAnyLinkage:
549 case GlobalValue::LinkOnceODRLinkage:
550 case GlobalValue::WeakAnyLinkage:
551 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000552 case GlobalValue::CommonLinkage:
Scott Michel73655bc2008-11-08 18:59:02 +0000553 O << "\t.global " << name << '\n'
554 << "\t.type " << name << ", @object\n"
555 << "\t.weak " << name << '\n';
556 break;
557 case GlobalValue::AppendingLinkage:
558 // FIXME: appending linkage variables should go into a section of
559 // their name or something. For now, just emit them as external.
560 case GlobalValue::ExternalLinkage:
561 // If external or appending, declare as a global symbol
562 O << "\t.global " << name << '\n'
563 << "\t.type " << name << ", @object\n";
564 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000565 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000566 case GlobalValue::LinkerPrivateLinkage:
Scott Michel73655bc2008-11-08 18:59:02 +0000567 case GlobalValue::InternalLinkage:
568 break;
569 default:
Torok Edwindac237e2009-07-08 20:53:28 +0000570 llvm_report_error("Unknown linkage type!");
Scott Michel73655bc2008-11-08 18:59:02 +0000571 }
572
573 EmitAlignment(Align, GVar);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000574 O << name << ":\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000575 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Scott Michel73655bc2008-11-08 18:59:02 +0000576 O << "'\n";
577
Scott Michel73655bc2008-11-08 18:59:02 +0000578 EmitGlobalConstant(C);
579 O << '\n';
580}
581
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000582// Force static initialization.
583extern "C" void LLVMInitializeCellSPUAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000584 RegisterAsmPrinter<LinuxAsmPrinter> X(TheCellSPUTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000585}