blob: e0d2401e71f8fef102f8030419ec983d0b77bf6c [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"
Scott Michel73655bc2008-11-08 18:59:02 +000022#include "llvm/CodeGen/AsmPrinter.h"
Scott Michel73655bc2008-11-08 18:59:02 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000026#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000027#include "llvm/Target/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Scott Michel73655bc2008-11-08 18:59:02 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetOptions.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000032#include "llvm/Target/TargetRegistry.h"
Scott Michel73655bc2008-11-08 18:59:02 +000033#include "llvm/ADT/StringExtras.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/FormattedStream.h"
Scott Michel73655bc2008-11-08 18:59:02 +000036using namespace llvm;
37
38namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000039 class SPUAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000040 public:
David Greene71847812009-07-14 20:18:05 +000041 explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner56591ab2010-02-02 23:37:42 +000042 MCContext &Ctx, MCStreamer &Streamer,
43 const MCAsmInfo *T) :
44 AsmPrinter(O, TM, Ctx, Streamer, T) {}
Scott Michel73655bc2008-11-08 18:59:02 +000045
46 virtual const char *getPassName() const {
47 return "STI CBEA SPU Assembly Printer";
48 }
49
50 SPUTargetMachine &getTM() {
51 return static_cast<SPUTargetMachine&>(TM);
52 }
53
54 /// printInstruction - This method is automatically generated by tablegen
Chris Lattner05af2612009-09-13 20:08:00 +000055 /// from the instruction set description.
Chris Lattner41aefdc2009-08-08 01:32:19 +000056 void printInstruction(const MachineInstr *MI);
Chris Lattnerd95148f2009-09-13 20:19:22 +000057 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000058
Scott Michel73655bc2008-11-08 18:59:02 +000059
Chris Lattner745ec062010-01-28 01:48:52 +000060 void EmitInstruction(const MachineInstr *MI) {
61 printInstruction(MI);
Chris Lattner8e089a92010-02-10 00:36:00 +000062 OutStreamer.AddBlankLine();
Chris Lattner745ec062010-01-28 01:48:52 +000063 }
Scott Michel73655bc2008-11-08 18:59:02 +000064 void printOp(const MachineOperand &MO);
65
66 /// printRegister - Print register according to target requirements.
67 ///
68 void printRegister(const MachineOperand &MO, bool R0AsZero) {
69 unsigned RegNo = MO.getReg();
70 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
71 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000072 O << getRegisterName(RegNo);
Scott Michel73655bc2008-11-08 18:59:02 +000073 }
74
75 void printOperand(const MachineInstr *MI, unsigned OpNo) {
76 const MachineOperand &MO = MI->getOperand(OpNo);
77 if (MO.isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +000078 O << getRegisterName(MO.getReg());
Scott Michel73655bc2008-11-08 18:59:02 +000079 } else if (MO.isImm()) {
80 O << MO.getImm();
81 } else {
82 printOp(MO);
83 }
84 }
Scott Michel9de57a92009-01-26 22:33:37 +000085
Scott Michel73655bc2008-11-08 18:59:02 +000086 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
87 unsigned AsmVariant, const char *ExtraCode);
88 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
89 unsigned AsmVariant, const char *ExtraCode);
Scott Michel9de57a92009-01-26 22:33:37 +000090
91
Scott Michel73655bc2008-11-08 18:59:02 +000092 void
93 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
94 {
95 int value = MI->getOperand(OpNo).getImm();
96 value = (value << (32 - 7)) >> (32 - 7);
97
98 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
99 && "Invalid s7 argument");
100 O << value;
101 }
102
103 void
104 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
105 {
106 unsigned int value = MI->getOperand(OpNo).getImm();
107 assert(value < (1 << 8) && "Invalid u7 argument");
108 O << value;
109 }
Scott Michel9de57a92009-01-26 22:33:37 +0000110
Scott Michel73655bc2008-11-08 18:59:02 +0000111 void
Scott Michelf0569be2008-12-27 04:51:36 +0000112 printShufAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000113 {
114 char value = MI->getOperand(OpNo).getImm();
115 O << (int) value;
116 O << "(";
117 printOperand(MI, OpNo+1);
118 O << ")";
119 }
120
121 void
122 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
123 {
124 O << (short) MI->getOperand(OpNo).getImm();
125 }
126
127 void
128 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
129 {
130 O << (unsigned short)MI->getOperand(OpNo).getImm();
131 }
132
133 void
134 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
135 {
136 O << (unsigned)MI->getOperand(OpNo).getImm();
137 }
Scott Michel9de57a92009-01-26 22:33:37 +0000138
Scott Michel73655bc2008-11-08 18:59:02 +0000139 void
140 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
141 // When used as the base register, r0 reads constant zero rather than
142 // the value contained in the register. For this reason, the darwin
143 // assembler requires that we print r0 as 0 (no r) when used as the base.
144 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner762ccea2009-09-13 20:31:40 +0000145 O << getRegisterName(MO.getReg()) << ", ";
Scott Michel73655bc2008-11-08 18:59:02 +0000146 printOperand(MI, OpNo+1);
147 }
148
149 void
150 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
151 {
152 unsigned int value = MI->getOperand(OpNo).getImm();
153 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
154 O << value;
155 }
156
157 void
158 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
159 {
160 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
161 >> 16);
162 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
163 && "Invalid s10 argument");
164 O << value;
165 }
166
167 void
168 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
169 {
170 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
171 >> 16);
172 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
173 O << value;
174 }
175
176 void
Scott Michelf0569be2008-12-27 04:51:36 +0000177 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000178 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000179 assert(MI->getOperand(OpNo).isImm() &&
180 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000181 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000182 int16_t value16 = int16_t(value);
183 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000184 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000185 O << (value16 & ~0xf) << "(";
Scott Michel73655bc2008-11-08 18:59:02 +0000186 printOperand(MI, OpNo+1);
187 O << ")";
188 }
189
190 void
191 printAddr256K(const MachineInstr *MI, unsigned OpNo)
192 {
193 /* Note: operand 1 is an offset or symbol name. */
194 if (MI->getOperand(OpNo).isImm()) {
195 printS16ImmOperand(MI, OpNo);
196 } else {
197 printOp(MI->getOperand(OpNo));
198 if (MI->getOperand(OpNo+1).isImm()) {
199 int displ = int(MI->getOperand(OpNo+1).getImm());
200 if (displ > 0)
201 O << "+" << displ;
202 else if (displ < 0)
203 O << displ;
204 }
205 }
206 }
207
208 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
209 printOp(MI->getOperand(OpNo));
210 }
211
212 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
Scott Michelaedc6372008-12-10 00:15:19 +0000213 // Used to generate a ".-<target>", but it turns out that the assembler
214 // really wants the target.
215 //
216 // N.B.: This operand is used for call targets. Branch hints are another
217 // animal entirely.
218 printOp(MI->getOperand(OpNo));
219 }
220
221 void printHBROperand(const MachineInstr *MI, unsigned OpNo) {
222 // HBR operands are generated in front of branches, hence, the
223 // program counter plus the target.
224 O << ".+";
Scott Michel73655bc2008-11-08 18:59:02 +0000225 printOp(MI->getOperand(OpNo));
Scott Michel73655bc2008-11-08 18:59:02 +0000226 }
227
228 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
229 if (MI->getOperand(OpNo).isImm()) {
230 printS16ImmOperand(MI, OpNo);
231 } else {
232 printOp(MI->getOperand(OpNo));
233 O << "@h";
234 }
235 }
236
237 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
238 if (MI->getOperand(OpNo).isImm()) {
239 printS16ImmOperand(MI, OpNo);
240 } else {
241 printOp(MI->getOperand(OpNo));
242 O << "@l";
243 }
244 }
245
246 /// Print local store address
247 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
248 printOp(MI->getOperand(OpNo));
249 }
250
251 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
252 if (MI->getOperand(OpNo).isImm()) {
253 int value = (int) MI->getOperand(OpNo).getImm();
254 assert((value >= 0 && value < 16)
255 && "Invalid negated immediate rotate 7-bit argument");
256 O << -value;
257 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000258 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000259 }
260 }
261
262 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
263 if (MI->getOperand(OpNo).isImm()) {
264 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel104de432008-11-24 17:11:17 +0000265 assert((value >= 0 && value <= 32)
Scott Michel73655bc2008-11-08 18:59:02 +0000266 && "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 }
Scott Michel73655bc2008-11-08 18:59:02 +0000272 };
Scott Michel73655bc2008-11-08 18:59:02 +0000273} // end of anonymous namespace
274
275// Include the auto-generated portion of the assembly writer
276#include "SPUGenAsmWriter.inc"
277
278void SPUAsmPrinter::printOp(const MachineOperand &MO) {
279 switch (MO.getType()) {
280 case MachineOperand::MO_Immediate:
Torok Edwindac237e2009-07-08 20:53:28 +0000281 llvm_report_error("printOp() does not handle immediate values");
Scott Michel73655bc2008-11-08 18:59:02 +0000282 return;
283
284 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000285 O << *MO.getMBB()->getSymbol(OutContext);
Scott Michel73655bc2008-11-08 18:59:02 +0000286 return;
287 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000288 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000289 << '_' << MO.getIndex();
290 return;
291 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000292 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000293 << '_' << MO.getIndex();
294 return;
295 case MachineOperand::MO_ExternalSymbol:
296 // Computing the address of an external symbol, not calling it.
297 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner12164412010-01-16 00:21:18 +0000298 O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
299 << "$non_lazy_ptr";
Scott Michel73655bc2008-11-08 18:59:02 +0000300 return;
301 }
Chris Lattner10b318b2010-01-17 21:43:43 +0000302 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Scott Michel73655bc2008-11-08 18:59:02 +0000303 return;
Chris Lattner12164412010-01-16 00:21:18 +0000304 case MachineOperand::MO_GlobalAddress:
Scott Michel73655bc2008-11-08 18:59:02 +0000305 // External or weakly linked global variables need non-lazily-resolved
306 // stubs
307 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner12164412010-01-16 00:21:18 +0000308 GlobalValue *GV = MO.getGlobal();
Scott Michel73655bc2008-11-08 18:59:02 +0000309 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
310 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000311 O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Scott Michel73655bc2008-11-08 18:59:02 +0000312 return;
313 }
314 }
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000315 O << *Mang->getSymbol(MO.getGlobal());
Scott Michel73655bc2008-11-08 18:59:02 +0000316 return;
Scott Michel73655bc2008-11-08 18:59:02 +0000317 default:
318 O << "<unknown operand type: " << MO.getType() << ">";
319 return;
320 }
321}
322
323/// PrintAsmOperand - Print out an operand for an inline asm expression.
324///
325bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000326 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000327 const char *ExtraCode) {
328 // Does this asm operand have a single letter operand modifier?
329 if (ExtraCode && ExtraCode[0]) {
330 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000331
Scott Michel73655bc2008-11-08 18:59:02 +0000332 switch (ExtraCode[0]) {
333 default: return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000334 case 'L': // Write second word of DImode reference.
Scott Michel73655bc2008-11-08 18:59:02 +0000335 // Verify that this operand has two consecutive registers.
336 if (!MI->getOperand(OpNo).isReg() ||
337 OpNo+1 == MI->getNumOperands() ||
338 !MI->getOperand(OpNo+1).isReg())
339 return true;
340 ++OpNo; // Return the high-part.
341 break;
342 }
343 }
Scott Michel9de57a92009-01-26 22:33:37 +0000344
Scott Michel73655bc2008-11-08 18:59:02 +0000345 printOperand(MI, OpNo);
346 return false;
347}
348
349bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
350 unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000351 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000352 const char *ExtraCode) {
353 if (ExtraCode && ExtraCode[0])
354 return true; // Unknown modifier.
355 printMemRegReg(MI, OpNo);
356 return false;
357}
358
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000359// Force static initialization.
360extern "C" void LLVMInitializeCellSPUAsmPrinter() {
Chris Lattner08acebc2010-01-28 01:50:22 +0000361 RegisterAsmPrinter<SPUAsmPrinter> X(TheCellSPUTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000362}