blob: 3e955310b513d4cebc33745667dcc6eb348317f0 [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
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 Lattner7ad07c42010-04-04 06:12:20 +000060 SmallString<128> Str;
61 raw_svector_ostream OS(Str);
62 printInstruction(MI, OS);
63 OutStreamer.EmitRawText(OS.str());
Chris Lattner745ec062010-01-28 01:48:52 +000064 }
Chris Lattner35c33bd2010-04-04 04:47:45 +000065 void printOp(const MachineOperand &MO, raw_ostream &OS);
Scott Michel73655bc2008-11-08 18:59:02 +000066
67 /// printRegister - Print register according to target requirements.
68 ///
Chris Lattner35c33bd2010-04-04 04:47:45 +000069 void printRegister(const MachineOperand &MO, bool R0AsZero, raw_ostream &O){
Scott Michel73655bc2008-11-08 18:59:02 +000070 unsigned RegNo = MO.getReg();
71 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
72 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000073 O << getRegisterName(RegNo);
Scott Michel73655bc2008-11-08 18:59:02 +000074 }
75
Chris Lattner35c33bd2010-04-04 04:47:45 +000076 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +000077 const MachineOperand &MO = MI->getOperand(OpNo);
78 if (MO.isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +000079 O << getRegisterName(MO.getReg());
Scott Michel73655bc2008-11-08 18:59:02 +000080 } else if (MO.isImm()) {
81 O << MO.getImm();
82 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +000083 printOp(MO, O);
Scott Michel73655bc2008-11-08 18:59:02 +000084 }
85 }
Scott Michel9de57a92009-01-26 22:33:37 +000086
Scott Michel73655bc2008-11-08 18:59:02 +000087 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000088 unsigned AsmVariant, const char *ExtraCode,
89 raw_ostream &O);
Scott Michel73655bc2008-11-08 18:59:02 +000090 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000091 unsigned AsmVariant, const char *ExtraCode,
92 raw_ostream &O);
Scott Michel9de57a92009-01-26 22:33:37 +000093
94
Scott Michel73655bc2008-11-08 18:59:02 +000095 void
Chris Lattner35c33bd2010-04-04 04:47:45 +000096 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +000097 {
98 int value = MI->getOperand(OpNo).getImm();
99 value = (value << (32 - 7)) >> (32 - 7);
100
101 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
102 && "Invalid s7 argument");
103 O << value;
104 }
105
106 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000107 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000108 {
109 unsigned int value = MI->getOperand(OpNo).getImm();
110 assert(value < (1 << 8) && "Invalid u7 argument");
111 O << value;
112 }
Scott Michel9de57a92009-01-26 22:33:37 +0000113
Scott Michel73655bc2008-11-08 18:59:02 +0000114 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000115 printShufAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000116 {
117 char value = MI->getOperand(OpNo).getImm();
118 O << (int) value;
119 O << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000120 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000121 O << ")";
122 }
123
124 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000125 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000126 {
127 O << (short) MI->getOperand(OpNo).getImm();
128 }
129
130 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000131 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000132 {
133 O << (unsigned short)MI->getOperand(OpNo).getImm();
134 }
135
136 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000137 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000138 {
139 O << (unsigned)MI->getOperand(OpNo).getImm();
140 }
Scott Michel9de57a92009-01-26 22:33:37 +0000141
Scott Michel73655bc2008-11-08 18:59:02 +0000142 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000143 printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000144 // When used as the base register, r0 reads constant zero rather than
145 // the value contained in the register. For this reason, the darwin
146 // assembler requires that we print r0 as 0 (no r) when used as the base.
147 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner762ccea2009-09-13 20:31:40 +0000148 O << getRegisterName(MO.getReg()) << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000149 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000150 }
151
152 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000153 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000154 {
155 unsigned int value = MI->getOperand(OpNo).getImm();
156 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
157 O << value;
158 }
159
160 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000161 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000162 {
163 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
164 >> 16);
165 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
166 && "Invalid s10 argument");
167 O << value;
168 }
169
170 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000171 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000172 {
173 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
174 >> 16);
175 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
176 O << value;
177 }
178
179 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000180 printDFormAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000181 {
Chris Lattner0f2d9952009-01-21 18:38:18 +0000182 assert(MI->getOperand(OpNo).isImm() &&
183 "printDFormAddr first operand is not immediate");
Scott Michel73655bc2008-11-08 18:59:02 +0000184 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
Scott Michel4379efc2008-11-20 05:01:09 +0000185 int16_t value16 = int16_t(value);
186 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
Scott Michel73655bc2008-11-08 18:59:02 +0000187 && "Invalid dform s10 offset argument");
Scott Michelf0569be2008-12-27 04:51:36 +0000188 O << (value16 & ~0xf) << "(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000189 printOperand(MI, OpNo+1, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000190 O << ")";
191 }
192
193 void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000194 printAddr256K(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
Scott Michel73655bc2008-11-08 18:59:02 +0000195 {
196 /* Note: operand 1 is an offset or symbol name. */
197 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000198 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000199 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000200 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000201 if (MI->getOperand(OpNo+1).isImm()) {
202 int displ = int(MI->getOperand(OpNo+1).getImm());
203 if (displ > 0)
204 O << "+" << displ;
205 else if (displ < 0)
206 O << displ;
207 }
208 }
209 }
210
Chris Lattner35c33bd2010-04-04 04:47:45 +0000211 void printCallOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
212 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000213 }
214
Chris Lattner35c33bd2010-04-04 04:47:45 +0000215 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000216 // Used to generate a ".-<target>", but it turns out that the assembler
217 // really wants the target.
218 //
219 // N.B.: This operand is used for call targets. Branch hints are another
220 // animal entirely.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000221 printOp(MI->getOperand(OpNo), O);
Scott Michelaedc6372008-12-10 00:15:19 +0000222 }
223
Chris Lattner35c33bd2010-04-04 04:47:45 +0000224 void printHBROperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michelaedc6372008-12-10 00:15:19 +0000225 // HBR operands are generated in front of branches, hence, the
226 // program counter plus the target.
227 O << ".+";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000228 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000229 }
230
Chris Lattner35c33bd2010-04-04 04:47:45 +0000231 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000232 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000233 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000234 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000235 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000236 O << "@h";
237 }
238 }
239
Chris Lattner35c33bd2010-04-04 04:47:45 +0000240 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000241 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000242 printS16ImmOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000243 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000244 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000245 O << "@l";
246 }
247 }
248
249 /// Print local store address
Chris Lattner35c33bd2010-04-04 04:47:45 +0000250 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
251 printOp(MI->getOperand(OpNo), O);
Scott Michel73655bc2008-11-08 18:59:02 +0000252 }
253
Chris Lattner35c33bd2010-04-04 04:47:45 +0000254 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo,
255 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000256 if (MI->getOperand(OpNo).isImm()) {
257 int value = (int) MI->getOperand(OpNo).getImm();
258 assert((value >= 0 && value < 16)
259 && "Invalid negated immediate rotate 7-bit argument");
260 O << -value;
261 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000262 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
Scott Michel73655bc2008-11-08 18:59:02 +0000263 }
264 }
265
Chris Lattner35c33bd2010-04-04 04:47:45 +0000266 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O){
267 assert(MI->getOperand(OpNo).isImm() &&
268 "Invalid/non-immediate rotate amount in printRotateNeg7Imm");
269 int value = (int) MI->getOperand(OpNo).getImm();
270 assert((value >= 0 && value <= 32)
271 && "Invalid negated immediate rotate 7-bit argument");
272 O << -value;
Scott Michel73655bc2008-11-08 18:59:02 +0000273 }
Scott Michel73655bc2008-11-08 18:59:02 +0000274 };
Scott Michel73655bc2008-11-08 18:59:02 +0000275} // end of anonymous namespace
276
277// Include the auto-generated portion of the assembly writer
278#include "SPUGenAsmWriter.inc"
279
Chris Lattner35c33bd2010-04-04 04:47:45 +0000280void SPUAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000281 switch (MO.getType()) {
282 case MachineOperand::MO_Immediate:
Chris Lattner75361b62010-04-07 22:58:41 +0000283 report_fatal_error("printOp() does not handle immediate values");
Scott Michel73655bc2008-11-08 18:59:02 +0000284 return;
285
286 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000287 O << *MO.getMBB()->getSymbol();
Scott Michel73655bc2008-11-08 18:59:02 +0000288 return;
289 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000290 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000291 << '_' << MO.getIndex();
292 return;
293 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000294 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Scott Michel73655bc2008-11-08 18:59:02 +0000295 << '_' << MO.getIndex();
296 return;
297 case MachineOperand::MO_ExternalSymbol:
298 // Computing the address of an external symbol, not calling it.
299 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner12164412010-01-16 00:21:18 +0000300 O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
301 << "$non_lazy_ptr";
Scott Michel73655bc2008-11-08 18:59:02 +0000302 return;
303 }
Chris Lattner10b318b2010-01-17 21:43:43 +0000304 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Scott Michel73655bc2008-11-08 18:59:02 +0000305 return;
Chris Lattner12164412010-01-16 00:21:18 +0000306 case MachineOperand::MO_GlobalAddress:
Scott Michel73655bc2008-11-08 18:59:02 +0000307 // External or weakly linked global variables need non-lazily-resolved
308 // stubs
309 if (TM.getRelocationModel() != Reloc::Static) {
Dan Gohman46510a72010-04-15 01:51:59 +0000310 const GlobalValue *GV = MO.getGlobal();
Scott Michel73655bc2008-11-08 18:59:02 +0000311 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
312 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000313 O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Scott Michel73655bc2008-11-08 18:59:02 +0000314 return;
315 }
316 }
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000317 O << *Mang->getSymbol(MO.getGlobal());
Scott Michel73655bc2008-11-08 18:59:02 +0000318 return;
Scott Michel73655bc2008-11-08 18:59:02 +0000319 default:
320 O << "<unknown operand type: " << MO.getType() << ">";
321 return;
322 }
323}
324
325/// PrintAsmOperand - Print out an operand for an inline asm expression.
326///
327bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Scott Michel9de57a92009-01-26 22:33:37 +0000328 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000329 const char *ExtraCode, raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000330 // Does this asm operand have a single letter operand modifier?
331 if (ExtraCode && ExtraCode[0]) {
332 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000333
Scott Michel73655bc2008-11-08 18:59:02 +0000334 switch (ExtraCode[0]) {
335 default: return true; // Unknown modifier.
Scott Michel9de57a92009-01-26 22:33:37 +0000336 case 'L': // Write second word of DImode reference.
Scott Michel73655bc2008-11-08 18:59:02 +0000337 // Verify that this operand has two consecutive registers.
338 if (!MI->getOperand(OpNo).isReg() ||
339 OpNo+1 == MI->getNumOperands() ||
340 !MI->getOperand(OpNo+1).isReg())
341 return true;
342 ++OpNo; // Return the high-part.
343 break;
344 }
345 }
Scott Michel9de57a92009-01-26 22:33:37 +0000346
Chris Lattner35c33bd2010-04-04 04:47:45 +0000347 printOperand(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000348 return false;
349}
350
351bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000352 unsigned OpNo, unsigned AsmVariant,
353 const char *ExtraCode,
354 raw_ostream &O) {
Scott Michel73655bc2008-11-08 18:59:02 +0000355 if (ExtraCode && ExtraCode[0])
356 return true; // Unknown modifier.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000357 printMemRegReg(MI, OpNo, O);
Scott Michel73655bc2008-11-08 18:59:02 +0000358 return false;
359}
360
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000361// Force static initialization.
362extern "C" void LLVMInitializeCellSPUAsmPrinter() {
Chris Lattner08acebc2010-01-28 01:50:22 +0000363 RegisterAsmPrinter<SPUAsmPrinter> X(TheCellSPUTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000364}