blob: 930886663796f6b79aa9f562976649d1f9704586 [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 Lattner11d53c12010-03-13 20:55:24 +000042 MCStreamer &Streamer) :
43 AsmPrinter(O, TM, Streamer) {}
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 Lattner35c33bd2010-04-04 04:47:45 +000055 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
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) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000060 printInstruction(MI, O);
Chris Lattner8e089a92010-02-10 00:36:00 +000061 OutStreamer.AddBlankLine();
Chris Lattner745ec062010-01-28 01:48:52 +000062 }
Chris Lattner35c33bd2010-04-04 04:47:45 +000063 void printOp(const MachineOperand &MO, raw_ostream &OS);
Scott Michel73655bc2008-11-08 18:59:02 +000064
65 /// printRegister - Print register according to target requirements.
66 ///
Chris Lattner35c33bd2010-04-04 04:47:45 +000067 void printRegister(const MachineOperand &MO, bool R0AsZero, raw_ostream &O){
Scott Michel73655bc2008-11-08 18:59:02 +000068 unsigned RegNo = MO.getReg();
69 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
70 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000071 O << getRegisterName(RegNo);
Scott Michel73655bc2008-11-08 18:59:02 +000072 }
73
Chris Lattner35c33bd2010-04-04 04:47:45 +000074 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +000075 const MachineOperand &MO = MI->getOperand(OpNo);
76 if (MO.isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +000077 O << getRegisterName(MO.getReg());
Scott Michel73655bc2008-11-08 18:59:02 +000078 } else if (MO.isImm()) {
79 O << MO.getImm();
80 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +000081 printOp(MO, O);
Scott Michel73655bc2008-11-08 18:59:02 +000082 }
83 }
Scott Michel9de57a92009-01-26 22:33:37 +000084
Scott Michel73655bc2008-11-08 18:59:02 +000085 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000086 unsigned AsmVariant, const char *ExtraCode,
87 raw_ostream &O);
Scott Michel73655bc2008-11-08 18:59:02 +000088 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000089 unsigned AsmVariant, const char *ExtraCode,
90 raw_ostream &O);
Scott Michel9de57a92009-01-26 22:33:37 +000091
92
Scott Michel73655bc2008-11-08 18:59:02 +000093 void
Chris Lattner35c33bd2010-04-04 04:47:45 +000094 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +000095 {
96 int value = MI->getOperand(OpNo).getImm();
97 value = (value << (32 - 7)) >> (32 - 7);
98
99 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
100 && "Invalid s7 argument");
101 O << value;
102 }
103
104 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000105 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000106 {
107 unsigned int value = MI->getOperand(OpNo).getImm();
108 assert(value < (1 << 8) && "Invalid u7 argument");
109 O << value;
110 }
Scott Michel9de57a92009-01-26 22:33:37 +0000111
Scott Michel73655bc2008-11-08 18:59:02 +0000112 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000113 printShufAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000114 {
115 char value = MI->getOperand(OpNo).getImm();
116 O << (int) value;
117 O << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000118 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000119 O << ")";
120 }
121
122 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000123 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000124 {
125 O << (short) MI->getOperand(OpNo).getImm();
126 }
127
128 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000129 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000130 {
131 O << (unsigned short)MI->getOperand(OpNo).getImm();
132 }
133
134 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000135 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000136 {
137 O << (unsigned)MI->getOperand(OpNo).getImm();
138 }
Scott Michel9de57a92009-01-26 22:33:37 +0000139
Scott Michel73655bc2008-11-08 18:59:02 +0000140 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000141 printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000142 // When used as the base register, r0 reads constant zero rather than
143 // the value contained in the register. For this reason, the darwin
144 // assembler requires that we print r0 as 0 (no r) when used as the base.
145 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner762ccea2009-09-13 20:31:40 +0000146 O << getRegisterName(MO.getReg()) << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000147 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000148 }
149
150 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000151 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000152 {
153 unsigned int value = MI->getOperand(OpNo).getImm();
154 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
155 O << value;
156 }
157
158 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000159 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000160 {
161 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
162 >> 16);
163 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
164 && "Invalid s10 argument");
165 O << value;
166 }
167
168 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000169 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000170 {
171 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
172 >> 16);
173 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
174 O << value;
175 }
176
177 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000178 printDFormAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000179 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000180 assert(MI->getOperand(OpNo).isImm() &&
181 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000182 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000183 int16_t value16 = int16_t(value);
184 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000185 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000186 O << (value16 & ~0xf) << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000187 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000188 O << ")";
189 }
190
191 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000192 printAddr256K(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000193 {
194 /* Note: operand 1 is an offset or symbol name. */
195 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000196 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000197 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000198 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000199 if (MI->getOperand(OpNo+1).isImm()) {
200 int displ = int(MI->getOperand(OpNo+1).getImm());
201 if (displ > 0)
202 O << "+" << displ;
203 else if (displ < 0)
204 O << displ;
205 }
206 }
207 }
208
Chris Lattner35c33bd2010-04-04 04:47:45 +0000209 void printCallOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
210 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000211 }
212
Chris Lattner35c33bd2010-04-04 04:47:45 +0000213 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000214 // Used to generate a ".-<target>", but it turns out that the assembler
215 // really wants the target.
216 //
217 // N.B.: This operand is used for call targets. Branch hints are another
218 // animal entirely.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000219 printOp(MI->getOperand(OpNo), O);
Scott Michelaedc6372008-12-10 00:15:19 +0000220 }
221
Chris Lattner35c33bd2010-04-04 04:47:45 +0000222 void printHBROperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000223 // HBR operands are generated in front of branches, hence, the
224 // program counter plus the target.
225 O << ".+";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000226 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000227 }
228
Chris Lattner35c33bd2010-04-04 04:47:45 +0000229 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000230 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000231 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000232 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000233 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000234 O << "@h";
235 }
236 }
237
Chris Lattner35c33bd2010-04-04 04:47:45 +0000238 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000239 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000240 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000241 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000242 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000243 O << "@l";
244 }
245 }
246
247 /// Print local store address
Chris Lattner35c33bd2010-04-04 04:47:45 +0000248 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
249 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000250 }
251
Chris Lattner35c33bd2010-04-04 04:47:45 +0000252 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo,
253 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000254 if (MI->getOperand(OpNo).isImm()) {
255 int value = (int) MI->getOperand(OpNo).getImm();
256 assert((value >= 0 && value < 16)
257 && "Invalid negated immediate rotate 7-bit argument");
258 O << -value;
259 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000260 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000261 }
262 }
263
Chris Lattner35c33bd2010-04-04 04:47:45 +0000264 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O){
265 assert(MI->getOperand(OpNo).isImm() &&
266 "Invalid/non-immediate rotate amount in printRotateNeg7Imm");
267 int value = (int) MI->getOperand(OpNo).getImm();
268 assert((value >= 0 && value <= 32)
269 && "Invalid negated immediate rotate 7-bit argument");
270 O << -value;
Scott Michel73655bc2008-11-08 18:59:02 +0000271 }
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
Chris Lattner35c33bd2010-04-04 04:47:45 +0000278void SPUAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000279 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 Lattner1b2eb0e2010-03-13 21:04:28 +0000285 O << *MO.getMBB()->getSymbol();
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,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000327 const char *ExtraCode, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000328 // 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
Chris Lattner35c33bd2010-04-04 04:47:45 +0000345 printOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000346 return false;
347}
348
349bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000350 unsigned OpNo, unsigned AsmVariant,
351 const char *ExtraCode,
352 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000353 if (ExtraCode && ExtraCode[0])
354 return true; // Unknown modifier.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000355 printMemRegReg(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000356 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}