blob: c2b6f38e33f250a8336ef74842a84d63e35c2ea4 [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,
86 unsigned AsmVariant, const char *ExtraCode);
87 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
88 unsigned AsmVariant, const char *ExtraCode);
Scott Michel9de57a92009-01-26 22:33:37 +000089
90
Scott Michel73655bc2008-11-08 18:59:02 +000091 void
Chris Lattner35c33bd2010-04-04 04:47:45 +000092 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +000093 {
94 int value = MI->getOperand(OpNo).getImm();
95 value = (value << (32 - 7)) >> (32 - 7);
96
97 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
98 && "Invalid s7 argument");
99 O << value;
100 }
101
102 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000103 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000104 {
105 unsigned int value = MI->getOperand(OpNo).getImm();
106 assert(value < (1 << 8) && "Invalid u7 argument");
107 O << value;
108 }
Scott Michel9de57a92009-01-26 22:33:37 +0000109
Scott Michel73655bc2008-11-08 18:59:02 +0000110 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000111 printShufAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000112 {
113 char value = MI->getOperand(OpNo).getImm();
114 O << (int) value;
115 O << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000116 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000117 O << ")";
118 }
119
120 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000121 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000122 {
123 O << (short) MI->getOperand(OpNo).getImm();
124 }
125
126 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000127 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000128 {
129 O << (unsigned short)MI->getOperand(OpNo).getImm();
130 }
131
132 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000133 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000134 {
135 O << (unsigned)MI->getOperand(OpNo).getImm();
136 }
Scott Michel9de57a92009-01-26 22:33:37 +0000137
Scott Michel73655bc2008-11-08 18:59:02 +0000138 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000139 printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000140 // When used as the base register, r0 reads constant zero rather than
141 // the value contained in the register. For this reason, the darwin
142 // assembler requires that we print r0 as 0 (no r) when used as the base.
143 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner762ccea2009-09-13 20:31:40 +0000144 O << getRegisterName(MO.getReg()) << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000145 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000146 }
147
148 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000149 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000150 {
151 unsigned int value = MI->getOperand(OpNo).getImm();
152 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
153 O << value;
154 }
155
156 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000157 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000158 {
159 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
160 >> 16);
161 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
162 && "Invalid s10 argument");
163 O << value;
164 }
165
166 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000167 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000168 {
169 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
170 >> 16);
171 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
172 O << value;
173 }
174
175 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000176 printDFormAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000177 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000178 assert(MI->getOperand(OpNo).isImm() &&
179 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000180 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000181 int16_t value16 = int16_t(value);
182 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000183 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000184 O << (value16 & ~0xf) << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000185 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000186 O << ")";
187 }
188
189 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000190 printAddr256K(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000191 {
192 /* Note: operand 1 is an offset or symbol name. */
193 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000194 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000195 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000196 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000197 if (MI->getOperand(OpNo+1).isImm()) {
198 int displ = int(MI->getOperand(OpNo+1).getImm());
199 if (displ > 0)
200 O << "+" << displ;
201 else if (displ < 0)
202 O << displ;
203 }
204 }
205 }
206
Chris Lattner35c33bd2010-04-04 04:47:45 +0000207 void printCallOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
208 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000209 }
210
Chris Lattner35c33bd2010-04-04 04:47:45 +0000211 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000212 // Used to generate a ".-<target>", but it turns out that the assembler
213 // really wants the target.
214 //
215 // N.B.: This operand is used for call targets. Branch hints are another
216 // animal entirely.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000217 printOp(MI->getOperand(OpNo), O);
Scott Michelaedc6372008-12-10 00:15:19 +0000218 }
219
Chris Lattner35c33bd2010-04-04 04:47:45 +0000220 void printHBROperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000221 // HBR operands are generated in front of branches, hence, the
222 // program counter plus the target.
223 O << ".+";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000224 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000225 }
226
Chris Lattner35c33bd2010-04-04 04:47:45 +0000227 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000228 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000229 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000230 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000231 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000232 O << "@h";
233 }
234 }
235
Chris Lattner35c33bd2010-04-04 04:47:45 +0000236 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000237 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000238 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000239 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000240 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000241 O << "@l";
242 }
243 }
244
245 /// Print local store address
Chris Lattner35c33bd2010-04-04 04:47:45 +0000246 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
247 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000248 }
249
Chris Lattner35c33bd2010-04-04 04:47:45 +0000250 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo,
251 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000252 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
Chris Lattner35c33bd2010-04-04 04:47:45 +0000262 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O){
263 assert(MI->getOperand(OpNo).isImm() &&
264 "Invalid/non-immediate rotate amount in printRotateNeg7Imm");
265 int value = (int) MI->getOperand(OpNo).getImm();
266 assert((value >= 0 && value <= 32)
267 && "Invalid negated immediate rotate 7-bit argument");
268 O << -value;
Scott Michel73655bc2008-11-08 18:59:02 +0000269 }
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
Chris Lattner35c33bd2010-04-04 04:47:45 +0000276void SPUAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000277 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 Lattner1b2eb0e2010-03-13 21:04:28 +0000283 O << *MO.getMBB()->getSymbol();
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 Lattnerd62f1b42010-03-12 21:19:23 +0000313 O << *Mang->getSymbol(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
Chris Lattner35c33bd2010-04-04 04:47:45 +0000343 printOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000344 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.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000353 printMemRegReg(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000354 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}