blob: 40404614b703e7d0b4d8dc076572d3bb3c969646 [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"
Chris Lattner7ad07c42010-04-04 06:12:20 +000033#include "llvm/ADT/SmallString.h"
Scott Michel73655bc2008-11-08 18:59:02 +000034#include "llvm/ADT/StringExtras.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000035#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000036#include "llvm/Support/raw_ostream.h"
Scott Michel73655bc2008-11-08 18:59:02 +000037using namespace llvm;
38
39namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000040 class SPUAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000041 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000042 explicit SPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) :
43 AsmPrinter(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
Scott Michel73655bc2008-11-08 18:59:02 +000049 /// printInstruction - This method is automatically generated by tablegen
Chris Lattner05af2612009-09-13 20:08:00 +000050 /// from the instruction set description.
Chris Lattner35c33bd2010-04-04 04:47:45 +000051 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
Chris Lattnerd95148f2009-09-13 20:19:22 +000052 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000053
Scott Michel73655bc2008-11-08 18:59:02 +000054
Chris Lattner745ec062010-01-28 01:48:52 +000055 void EmitInstruction(const MachineInstr *MI) {
Chris Lattner7ad07c42010-04-04 06:12:20 +000056 SmallString<128> Str;
57 raw_svector_ostream OS(Str);
58 printInstruction(MI, OS);
59 OutStreamer.EmitRawText(OS.str());
Chris Lattner745ec062010-01-28 01:48:52 +000060 }
Chris Lattner35c33bd2010-04-04 04:47:45 +000061 void printOp(const MachineOperand &MO, raw_ostream &OS);
Scott Michel73655bc2008-11-08 18:59:02 +000062
Chris Lattner35c33bd2010-04-04 04:47:45 +000063 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +000064 const MachineOperand &MO = MI->getOperand(OpNo);
65 if (MO.isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +000066 O << getRegisterName(MO.getReg());
Scott Michel73655bc2008-11-08 18:59:02 +000067 } else if (MO.isImm()) {
68 O << MO.getImm();
69 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +000070 printOp(MO, O);
Scott Michel73655bc2008-11-08 18:59:02 +000071 }
72 }
Scott Michel9de57a92009-01-26 22:33:37 +000073
Scott Michel73655bc2008-11-08 18:59:02 +000074 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000075 unsigned AsmVariant, const char *ExtraCode,
76 raw_ostream &O);
Scott Michel73655bc2008-11-08 18:59:02 +000077 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000078 unsigned AsmVariant, const char *ExtraCode,
79 raw_ostream &O);
Scott Michel9de57a92009-01-26 22:33:37 +000080
81
Scott Michel73655bc2008-11-08 18:59:02 +000082 void
Chris Lattner35c33bd2010-04-04 04:47:45 +000083 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +000084 {
85 unsigned int value = MI->getOperand(OpNo).getImm();
86 assert(value < (1 << 8) && "Invalid u7 argument");
87 O << value;
88 }
Scott Michel9de57a92009-01-26 22:33:37 +000089
Scott Michel73655bc2008-11-08 18:59:02 +000090 void
Chris Lattner35c33bd2010-04-04 04:47:45 +000091 printShufAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +000092 {
93 char value = MI->getOperand(OpNo).getImm();
94 O << (int) value;
95 O << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +000096 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +000097 O << ")";
98 }
99
100 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000101 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000102 {
103 O << (short) MI->getOperand(OpNo).getImm();
104 }
105
106 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000107 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000108 {
109 O << (unsigned short)MI->getOperand(OpNo).getImm();
110 }
111
112 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000113 printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000114 // When used as the base register, r0 reads constant zero rather than
115 // the value contained in the register. For this reason, the darwin
116 // assembler requires that we print r0 as 0 (no r) when used as the base.
117 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner762ccea2009-09-13 20:31:40 +0000118 O << getRegisterName(MO.getReg()) << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000119 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000120 }
121
122 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000123 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000124 {
125 unsigned int value = MI->getOperand(OpNo).getImm();
126 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
127 O << value;
128 }
129
130 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000131 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000132 {
133 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
134 >> 16);
135 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
136 && "Invalid s10 argument");
137 O << value;
138 }
139
140 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000141 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000142 {
143 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
144 >> 16);
145 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
146 O << value;
147 }
148
149 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000150 printDFormAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000151 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000152 assert(MI->getOperand(OpNo).isImm() &&
153 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000154 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000155 int16_t value16 = int16_t(value);
156 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000157 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000158 O << (value16 & ~0xf) << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000159 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000160 O << ")";
161 }
162
163 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000164 printAddr256K(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000165 {
166 /* Note: operand 1 is an offset or symbol name. */
167 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000168 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000169 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000170 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000171 if (MI->getOperand(OpNo+1).isImm()) {
172 int displ = int(MI->getOperand(OpNo+1).getImm());
173 if (displ > 0)
174 O << "+" << displ;
175 else if (displ < 0)
176 O << displ;
177 }
178 }
179 }
180
Chris Lattner35c33bd2010-04-04 04:47:45 +0000181 void printCallOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
182 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000183 }
184
Chris Lattner35c33bd2010-04-04 04:47:45 +0000185 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000186 // Used to generate a ".-<target>", but it turns out that the assembler
187 // really wants the target.
188 //
189 // N.B.: This operand is used for call targets. Branch hints are another
190 // animal entirely.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000191 printOp(MI->getOperand(OpNo), O);
Scott Michelaedc6372008-12-10 00:15:19 +0000192 }
193
Chris Lattner35c33bd2010-04-04 04:47:45 +0000194 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000195 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 O << "@h";
200 }
201 }
202
Chris Lattner35c33bd2010-04-04 04:47:45 +0000203 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000204 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000205 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000206 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000207 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000208 O << "@l";
209 }
210 }
211
212 /// Print local store address
Chris Lattner35c33bd2010-04-04 04:47:45 +0000213 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
214 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000215 }
216
Chris Lattner35c33bd2010-04-04 04:47:45 +0000217 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo,
218 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000219 if (MI->getOperand(OpNo).isImm()) {
220 int value = (int) MI->getOperand(OpNo).getImm();
221 assert((value >= 0 && value < 16)
222 && "Invalid negated immediate rotate 7-bit argument");
223 O << -value;
224 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000225 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000226 }
227 }
228
Chris Lattner35c33bd2010-04-04 04:47:45 +0000229 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O){
230 assert(MI->getOperand(OpNo).isImm() &&
231 "Invalid/non-immediate rotate amount in printRotateNeg7Imm");
232 int value = (int) MI->getOperand(OpNo).getImm();
233 assert((value >= 0 && value <= 32)
234 && "Invalid negated immediate rotate 7-bit argument");
235 O << -value;
Scott Michel73655bc2008-11-08 18:59:02 +0000236 }
Scott Michel73655bc2008-11-08 18:59:02 +0000237 };
Scott Michel73655bc2008-11-08 18:59:02 +0000238} // end of anonymous namespace
239
240// Include the auto-generated portion of the assembly writer
241#include "SPUGenAsmWriter.inc"
242
Chris Lattner35c33bd2010-04-04 04:47:45 +0000243void SPUAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000244 switch (MO.getType()) {
245 case MachineOperand::MO_Immediate:
Chris Lattner75361b62010-04-07 22:58:41 +0000246 report_fatal_error("printOp() does not handle immediate values");
Scott Michel73655bc2008-11-08 18:59:02 +0000247 return;
248
249 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000250 O << *MO.getMBB()->getSymbol();
Scott Michel73655bc2008-11-08 18:59:02 +0000251 return;
252 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000253 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000254 << '_' << MO.getIndex();
255 return;
256 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000257 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000258 << '_' << MO.getIndex();
259 return;
260 case MachineOperand::MO_ExternalSymbol:
261 // Computing the address of an external symbol, not calling it.
262 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner12164412010-01-16 00:21:18 +0000263 O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
264 << "$non_lazy_ptr";
Scott Michel73655bc2008-11-08 18:59:02 +0000265 return;
266 }
Chris Lattner10b318b2010-01-17 21:43:43 +0000267 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Scott Michel73655bc2008-11-08 18:59:02 +0000268 return;
Chris Lattner12164412010-01-16 00:21:18 +0000269 case MachineOperand::MO_GlobalAddress:
Scott Michel73655bc2008-11-08 18:59:02 +0000270 // External or weakly linked global variables need non-lazily-resolved
271 // stubs
272 if (TM.getRelocationModel() != Reloc::Static) {
Dan Gohman46510a72010-04-15 01:51:59 +0000273 const GlobalValue *GV = MO.getGlobal();
Scott Michel73655bc2008-11-08 18:59:02 +0000274 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
275 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000276 O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Scott Michel73655bc2008-11-08 18:59:02 +0000277 return;
278 }
279 }
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000280 O << *Mang->getSymbol(MO.getGlobal());
Scott Michel73655bc2008-11-08 18:59:02 +0000281 return;
Scott Michel73655bc2008-11-08 18:59:02 +0000282 default:
283 O << "<unknown operand type: " << MO.getType() << ">";
284 return;
285 }
286}
287
288/// PrintAsmOperand - Print out an operand for an inline asm expression.
289///
290bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000291 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000292 const char *ExtraCode, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000293 // Does this asm operand have a single letter operand modifier?
294 if (ExtraCode && ExtraCode[0]) {
295 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000296
Scott Michel73655bc2008-11-08 18:59:02 +0000297 switch (ExtraCode[0]) {
298 default: return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000299 case 'L': // Write second word of DImode reference.
Scott Michel73655bc2008-11-08 18:59:02 +0000300 // Verify that this operand has two consecutive registers.
301 if (!MI->getOperand(OpNo).isReg() ||
302 OpNo+1 == MI->getNumOperands() ||
303 !MI->getOperand(OpNo+1).isReg())
304 return true;
305 ++OpNo; // Return the high-part.
306 break;
307 }
308 }
Scott Michel9de57a92009-01-26 22:33:37 +0000309
Chris Lattner35c33bd2010-04-04 04:47:45 +0000310 printOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000311 return false;
312}
313
314bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000315 unsigned OpNo, unsigned AsmVariant,
316 const char *ExtraCode,
317 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000318 if (ExtraCode && ExtraCode[0])
319 return true; // Unknown modifier.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000320 printMemRegReg(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000321 return false;
322}
323
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000324// Force static initialization.
325extern "C" void LLVMInitializeCellSPUAsmPrinter() {
Chris Lattner08acebc2010-01-28 01:50:22 +0000326 RegisterAsmPrinter<SPUAsmPrinter> X(TheCellSPUTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000327}