blob: 92736c2268c4b55b9e29d04d7b5452f2dcdf96ff [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 Lattnerf0144122009-07-28 03:13:23 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
Scott Michel73655bc2008-11-08 18:59:02 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetOptions.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000031#include "llvm/Target/TargetRegistry.h"
Scott Michel73655bc2008-11-08 18:59:02 +000032#include "llvm/ADT/StringExtras.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/FormattedStream.h"
Scott Michel73655bc2008-11-08 18:59:02 +000035using namespace llvm;
36
37namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000038 class SPUAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000039 public:
David Greene71847812009-07-14 20:18:05 +000040 explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner56591ab2010-02-02 23:37:42 +000041 MCContext &Ctx, MCStreamer &Streamer,
42 const MCAsmInfo *T) :
43 AsmPrinter(O, TM, Ctx, Streamer, T) {}
Scott Michel73655bc2008-11-08 18:59:02 +000044
45 virtual const char *getPassName() const {
46 return "STI CBEA SPU Assembly Printer";
47 }
48
49 SPUTargetMachine &getTM() {
50 return static_cast<SPUTargetMachine&>(TM);
51 }
52
53 /// printInstruction - This method is automatically generated by tablegen
Chris Lattner05af2612009-09-13 20:08:00 +000054 /// from the instruction set description.
Chris Lattner41aefdc2009-08-08 01:32:19 +000055 void printInstruction(const MachineInstr *MI);
Chris Lattnerd95148f2009-09-13 20:19:22 +000056 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000057
Scott Michel73655bc2008-11-08 18:59:02 +000058
Chris Lattner745ec062010-01-28 01:48:52 +000059 void EmitInstruction(const MachineInstr *MI) {
60 printInstruction(MI);
61 }
Scott Michel73655bc2008-11-08 18:59:02 +000062 void printOp(const MachineOperand &MO);
63
64 /// printRegister - Print register according to target requirements.
65 ///
66 void printRegister(const MachineOperand &MO, bool R0AsZero) {
67 unsigned RegNo = MO.getReg();
68 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
69 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000070 O << getRegisterName(RegNo);
Scott Michel73655bc2008-11-08 18:59:02 +000071 }
72
73 void printOperand(const MachineInstr *MI, unsigned OpNo) {
74 const MachineOperand &MO = MI->getOperand(OpNo);
75 if (MO.isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +000076 O << getRegisterName(MO.getReg());
Scott Michel73655bc2008-11-08 18:59:02 +000077 } else if (MO.isImm()) {
78 O << MO.getImm();
79 } else {
80 printOp(MO);
81 }
82 }
Scott Michel9de57a92009-01-26 22:33:37 +000083
Scott Michel73655bc2008-11-08 18:59:02 +000084 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
85 unsigned AsmVariant, const char *ExtraCode);
86 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
87 unsigned AsmVariant, const char *ExtraCode);
Scott Michel9de57a92009-01-26 22:33:37 +000088
89
Scott Michel73655bc2008-11-08 18:59:02 +000090 void
91 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
92 {
93 int value = MI->getOperand(OpNo).getImm();
94 value = (value << (32 - 7)) >> (32 - 7);
95
96 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
97 && "Invalid s7 argument");
98 O << value;
99 }
100
101 void
102 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
103 {
104 unsigned int value = MI->getOperand(OpNo).getImm();
105 assert(value < (1 << 8) && "Invalid u7 argument");
106 O << value;
107 }
Scott Michel9de57a92009-01-26 22:33:37 +0000108
Scott Michel73655bc2008-11-08 18:59:02 +0000109 void
Scott Michelf0569be2008-12-27 04:51:36 +0000110 printShufAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000111 {
112 char value = MI->getOperand(OpNo).getImm();
113 O << (int) value;
114 O << "(";
115 printOperand(MI, OpNo+1);
116 O << ")";
117 }
118
119 void
120 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
121 {
122 O << (short) MI->getOperand(OpNo).getImm();
123 }
124
125 void
126 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
127 {
128 O << (unsigned short)MI->getOperand(OpNo).getImm();
129 }
130
131 void
132 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
133 {
134 O << (unsigned)MI->getOperand(OpNo).getImm();
135 }
Scott Michel9de57a92009-01-26 22:33:37 +0000136
Scott Michel73655bc2008-11-08 18:59:02 +0000137 void
138 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
139 // When used as the base register, r0 reads constant zero rather than
140 // the value contained in the register. For this reason, the darwin
141 // assembler requires that we print r0 as 0 (no r) when used as the base.
142 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner762ccea2009-09-13 20:31:40 +0000143 O << getRegisterName(MO.getReg()) << ", ";
Scott Michel73655bc2008-11-08 18:59:02 +0000144 printOperand(MI, OpNo+1);
145 }
146
147 void
148 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
149 {
150 unsigned int value = MI->getOperand(OpNo).getImm();
151 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
152 O << value;
153 }
154
155 void
156 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
157 {
158 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
159 >> 16);
160 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
161 && "Invalid s10 argument");
162 O << value;
163 }
164
165 void
166 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
167 {
168 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
169 >> 16);
170 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
171 O << value;
172 }
173
174 void
Scott Michelf0569be2008-12-27 04:51:36 +0000175 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
Scott Michel73655bc2008-11-08 18:59:02 +0000176 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000177 assert(MI->getOperand(OpNo).isImm() &&
178 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000179 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000180 int16_t value16 = int16_t(value);
181 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000182 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000183 O << (value16 & ~0xf) << "(";
Scott Michel73655bc2008-11-08 18:59:02 +0000184 printOperand(MI, OpNo+1);
185 O << ")";
186 }
187
188 void
189 printAddr256K(const MachineInstr *MI, unsigned OpNo)
190 {
191 /* Note: operand 1 is an offset or symbol name. */
192 if (MI->getOperand(OpNo).isImm()) {
193 printS16ImmOperand(MI, OpNo);
194 } else {
195 printOp(MI->getOperand(OpNo));
196 if (MI->getOperand(OpNo+1).isImm()) {
197 int displ = int(MI->getOperand(OpNo+1).getImm());
198 if (displ > 0)
199 O << "+" << displ;
200 else if (displ < 0)
201 O << displ;
202 }
203 }
204 }
205
206 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
207 printOp(MI->getOperand(OpNo));
208 }
209
210 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
Scott Michelaedc6372008-12-10 00:15:19 +0000211 // Used to generate a ".-<target>", but it turns out that the assembler
212 // really wants the target.
213 //
214 // N.B.: This operand is used for call targets. Branch hints are another
215 // animal entirely.
216 printOp(MI->getOperand(OpNo));
217 }
218
219 void printHBROperand(const MachineInstr *MI, unsigned OpNo) {
220 // HBR operands are generated in front of branches, hence, the
221 // program counter plus the target.
222 O << ".+";
Scott Michel73655bc2008-11-08 18:59:02 +0000223 printOp(MI->getOperand(OpNo));
Scott Michel73655bc2008-11-08 18:59:02 +0000224 }
225
226 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
227 if (MI->getOperand(OpNo).isImm()) {
228 printS16ImmOperand(MI, OpNo);
229 } else {
230 printOp(MI->getOperand(OpNo));
231 O << "@h";
232 }
233 }
234
235 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
236 if (MI->getOperand(OpNo).isImm()) {
237 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) {
250 if (MI->getOperand(OpNo).isImm()) {
251 int value = (int) MI->getOperand(OpNo).getImm();
252 assert((value >= 0 && value < 16)
253 && "Invalid negated immediate rotate 7-bit argument");
254 O << -value;
255 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000256 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000257 }
258 }
259
260 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
261 if (MI->getOperand(OpNo).isImm()) {
262 int value = (int) MI->getOperand(OpNo).getImm();
Scott Michel104de432008-11-24 17:11:17 +0000263 assert((value >= 0 && value <= 32)
Scott Michel73655bc2008-11-08 18:59:02 +0000264 && "Invalid negated immediate rotate 7-bit argument");
265 O << -value;
266 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000267 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000268 }
269 }
Scott Michel73655bc2008-11-08 18:59:02 +0000270 };
Scott Michel73655bc2008-11-08 18:59:02 +0000271} // end of anonymous namespace
272
273// Include the auto-generated portion of the assembly writer
274#include "SPUGenAsmWriter.inc"
275
276void SPUAsmPrinter::printOp(const MachineOperand &MO) {
277 switch (MO.getType()) {
278 case MachineOperand::MO_Immediate:
Torok Edwindac237e2009-07-08 20:53:28 +0000279 llvm_report_error("printOp() does not handle immediate values");
Scott Michel73655bc2008-11-08 18:59:02 +0000280 return;
281
282 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000283 O << *MO.getMBB()->getSymbol(OutContext);
Scott Michel73655bc2008-11-08 18:59:02 +0000284 return;
285 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000286 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000287 << '_' << MO.getIndex();
288 return;
289 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000290 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000291 << '_' << MO.getIndex();
292 return;
293 case MachineOperand::MO_ExternalSymbol:
294 // Computing the address of an external symbol, not calling it.
295 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner12164412010-01-16 00:21:18 +0000296 O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
297 << "$non_lazy_ptr";
Scott Michel73655bc2008-11-08 18:59:02 +0000298 return;
299 }
Chris Lattner10b318b2010-01-17 21:43:43 +0000300 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Scott Michel73655bc2008-11-08 18:59:02 +0000301 return;
Chris Lattner12164412010-01-16 00:21:18 +0000302 case MachineOperand::MO_GlobalAddress:
Scott Michel73655bc2008-11-08 18:59:02 +0000303 // External or weakly linked global variables need non-lazily-resolved
304 // stubs
305 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner12164412010-01-16 00:21:18 +0000306 GlobalValue *GV = MO.getGlobal();
Scott Michel73655bc2008-11-08 18:59:02 +0000307 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
308 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000309 O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Scott Michel73655bc2008-11-08 18:59:02 +0000310 return;
311 }
312 }
Chris Lattner10b318b2010-01-17 21:43:43 +0000313 O << *GetGlobalValueSymbol(MO.getGlobal());
Scott Michel73655bc2008-11-08 18:59:02 +0000314 return;
Scott Michel73655bc2008-11-08 18:59:02 +0000315 default:
316 O << "<unknown operand type: " << MO.getType() << ">";
317 return;
318 }
319}
320
321/// PrintAsmOperand - Print out an operand for an inline asm expression.
322///
323bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000324 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000325 const char *ExtraCode) {
326 // Does this asm operand have a single letter operand modifier?
327 if (ExtraCode && ExtraCode[0]) {
328 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000329
Scott Michel73655bc2008-11-08 18:59:02 +0000330 switch (ExtraCode[0]) {
331 default: return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000332 case 'L': // Write second word of DImode reference.
Scott Michel73655bc2008-11-08 18:59:02 +0000333 // Verify that this operand has two consecutive registers.
334 if (!MI->getOperand(OpNo).isReg() ||
335 OpNo+1 == MI->getNumOperands() ||
336 !MI->getOperand(OpNo+1).isReg())
337 return true;
338 ++OpNo; // Return the high-part.
339 break;
340 }
341 }
Scott Michel9de57a92009-01-26 22:33:37 +0000342
Scott Michel73655bc2008-11-08 18:59:02 +0000343 printOperand(MI, OpNo);
344 return false;
345}
346
347bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
348 unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000349 unsigned AsmVariant,
Scott Michel73655bc2008-11-08 18:59:02 +0000350 const char *ExtraCode) {
351 if (ExtraCode && ExtraCode[0])
352 return true; // Unknown modifier.
353 printMemRegReg(MI, OpNo);
354 return false;
355}
356
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000357// Force static initialization.
358extern "C" void LLVMInitializeCellSPUAsmPrinter() {
Chris Lattner08acebc2010-01-28 01:50:22 +0000359 RegisterAsmPrinter<SPUAsmPrinter> X(TheCellSPUTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000360}