2008-11-09 02:59:02 +08:00
|
|
|
//===-- SPUAsmPrinter.cpp - Print machine instrs to Cell SPU assembly -------=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to Cell SPU assembly language. This printer
|
|
|
|
// is the output mechanism used by `llc'.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asmprinter"
|
|
|
|
#include "SPU.h"
|
|
|
|
#include "SPUTargetMachine.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2009-08-19 13:49:37 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2009-08-23 04:48:53 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-09-14 01:14:04 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2010-03-13 05:19:23 +08:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2009-07-28 11:13:23 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2008-11-09 02:59:02 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2009-07-28 11:13:23 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-07-16 04:24:03 +08:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2010-04-04 14:12:20 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2008-11-09 02:59:02 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-08-19 13:49:37 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-04-04 16:18:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-11-09 02:59:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class SPUAsmPrinter : public AsmPrinter {
|
2009-02-24 16:30:20 +08:00
|
|
|
public:
|
2010-04-04 16:18:47 +08:00
|
|
|
explicit SPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) :
|
|
|
|
AsmPrinter(TM, Streamer) {}
|
2008-11-09 02:59:02 +08:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "STI CBEA SPU Assembly Printer";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// printInstruction - This method is automatically generated by tablegen
|
2009-09-14 04:08:00 +08:00
|
|
|
/// from the instruction set description.
|
2010-04-04 12:47:45 +08:00
|
|
|
void printInstruction(const MachineInstr *MI, raw_ostream &OS);
|
2009-09-14 04:19:22 +08:00
|
|
|
static const char *getRegisterName(unsigned RegNo);
|
2009-09-14 04:08:00 +08:00
|
|
|
|
2008-11-09 02:59:02 +08:00
|
|
|
|
2010-01-28 09:48:52 +08:00
|
|
|
void EmitInstruction(const MachineInstr *MI) {
|
2010-04-04 14:12:20 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream OS(Str);
|
|
|
|
printInstruction(MI, OS);
|
|
|
|
OutStreamer.EmitRawText(OS.str());
|
2010-01-28 09:48:52 +08:00
|
|
|
}
|
2010-04-04 12:47:45 +08:00
|
|
|
void printOp(const MachineOperand &MO, raw_ostream &OS);
|
2008-11-09 02:59:02 +08:00
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
if (MO.isReg()) {
|
2009-09-14 04:31:40 +08:00
|
|
|
O << getRegisterName(MO.getReg());
|
2008-11-09 02:59:02 +08:00
|
|
|
} else if (MO.isImm()) {
|
|
|
|
O << MO.getImm();
|
|
|
|
} else {
|
2010-04-04 12:47:45 +08:00
|
|
|
printOp(MO, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
}
|
|
|
|
}
|
2009-01-27 06:33:37 +08:00
|
|
|
|
2008-11-09 02:59:02 +08:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
2010-04-04 13:29:35 +08:00
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &O);
|
2008-11-09 02:59:02 +08:00
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
|
2010-04-04 13:29:35 +08:00
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &O);
|
2009-01-27 06:33:37 +08:00
|
|
|
|
|
|
|
|
2008-11-09 02:59:02 +08:00
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printU7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
unsigned int value = MI->getOperand(OpNo).getImm();
|
|
|
|
assert(value < (1 << 8) && "Invalid u7 argument");
|
|
|
|
O << value;
|
|
|
|
}
|
2009-01-27 06:33:37 +08:00
|
|
|
|
2008-11-09 02:59:02 +08:00
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printShufAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
char value = MI->getOperand(OpNo).getImm();
|
|
|
|
O << (int) value;
|
|
|
|
O << "(";
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, OpNo+1, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
O << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
O << (short) MI->getOperand(OpNo).getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
O << (unsigned short)MI->getOperand(OpNo).getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
// When used as the base register, r0 reads constant zero rather than
|
|
|
|
// the value contained in the register. For this reason, the darwin
|
|
|
|
// assembler requires that we print r0 as 0 (no r) when used as the base.
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
2009-09-14 04:31:40 +08:00
|
|
|
O << getRegisterName(MO.getReg()) << ", ";
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, OpNo+1, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printU18ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
unsigned int value = MI->getOperand(OpNo).getImm();
|
|
|
|
assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
|
|
|
|
O << value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printS10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
|
|
|
|
>> 16);
|
|
|
|
assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
|
|
|
|
&& "Invalid s10 argument");
|
|
|
|
O << value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printU10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
|
|
|
|
>> 16);
|
|
|
|
assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
|
|
|
|
O << value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printDFormAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
2009-01-22 02:38:18 +08:00
|
|
|
assert(MI->getOperand(OpNo).isImm() &&
|
|
|
|
"printDFormAddr first operand is not immediate");
|
2008-11-09 02:59:02 +08:00
|
|
|
int64_t value = int64_t(MI->getOperand(OpNo).getImm());
|
2008-11-20 13:01:09 +08:00
|
|
|
int16_t value16 = int16_t(value);
|
|
|
|
assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
|
2008-11-09 02:59:02 +08:00
|
|
|
&& "Invalid dform s10 offset argument");
|
2008-12-27 12:51:36 +08:00
|
|
|
O << (value16 & ~0xf) << "(";
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, OpNo+1, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
O << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-04 12:47:45 +08:00
|
|
|
printAddr256K(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
|
2008-11-09 02:59:02 +08:00
|
|
|
{
|
|
|
|
/* Note: operand 1 is an offset or symbol name. */
|
|
|
|
if (MI->getOperand(OpNo).isImm()) {
|
2010-04-04 12:47:45 +08:00
|
|
|
printS16ImmOperand(MI, OpNo, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
} else {
|
2010-04-04 12:47:45 +08:00
|
|
|
printOp(MI->getOperand(OpNo), O);
|
2008-11-09 02:59:02 +08:00
|
|
|
if (MI->getOperand(OpNo+1).isImm()) {
|
|
|
|
int displ = int(MI->getOperand(OpNo+1).getImm());
|
|
|
|
if (displ > 0)
|
|
|
|
O << "+" << displ;
|
|
|
|
else if (displ < 0)
|
|
|
|
O << displ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printCallOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
|
|
|
printOp(MI->getOperand(OpNo), O);
|
2008-11-09 02:59:02 +08:00
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
2008-12-10 08:15:19 +08:00
|
|
|
// Used to generate a ".-<target>", but it turns out that the assembler
|
|
|
|
// really wants the target.
|
|
|
|
//
|
|
|
|
// N.B.: This operand is used for call targets. Branch hints are another
|
|
|
|
// animal entirely.
|
2010-04-04 12:47:45 +08:00
|
|
|
printOp(MI->getOperand(OpNo), O);
|
2008-12-10 08:15:19 +08:00
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
if (MI->getOperand(OpNo).isImm()) {
|
2010-04-04 12:47:45 +08:00
|
|
|
printS16ImmOperand(MI, OpNo, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
} else {
|
2010-04-04 12:47:45 +08:00
|
|
|
printOp(MI->getOperand(OpNo), O);
|
2008-11-09 02:59:02 +08:00
|
|
|
O << "@h";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
if (MI->getOperand(OpNo).isImm()) {
|
2010-04-04 12:47:45 +08:00
|
|
|
printS16ImmOperand(MI, OpNo, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
} else {
|
2010-04-04 12:47:45 +08:00
|
|
|
printOp(MI->getOperand(OpNo), O);
|
2008-11-09 02:59:02 +08:00
|
|
|
O << "@l";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print local store address
|
2010-04-04 12:47:45 +08:00
|
|
|
void printSymbolLSA(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
|
|
|
|
printOp(MI->getOperand(OpNo), O);
|
2008-11-09 02:59:02 +08:00
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
if (MI->getOperand(OpNo).isImm()) {
|
|
|
|
int value = (int) MI->getOperand(OpNo).getImm();
|
|
|
|
assert((value >= 0 && value < 16)
|
|
|
|
&& "Invalid negated immediate rotate 7-bit argument");
|
|
|
|
O << -value;
|
|
|
|
} else {
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
|
2008-11-09 02:59:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O){
|
|
|
|
assert(MI->getOperand(OpNo).isImm() &&
|
|
|
|
"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
|
|
|
|
int value = (int) MI->getOperand(OpNo).getImm();
|
|
|
|
assert((value >= 0 && value <= 32)
|
|
|
|
&& "Invalid negated immediate rotate 7-bit argument");
|
|
|
|
O << -value;
|
2008-11-09 02:59:02 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
// Include the auto-generated portion of the assembly writer
|
|
|
|
#include "SPUGenAsmWriter.inc"
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void SPUAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_Immediate:
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("printOp() does not handle immediate values");
|
2008-11-09 02:59:02 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2010-03-14 05:04:28 +08:00
|
|
|
O << *MO.getMBB()->getSymbol();
|
2008-11-09 02:59:02 +08:00
|
|
|
return;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
2009-08-23 05:43:10 +08:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
|
2008-11-09 02:59:02 +08:00
|
|
|
<< '_' << MO.getIndex();
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2009-08-23 05:43:10 +08:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
|
2008-11-09 02:59:02 +08:00
|
|
|
<< '_' << MO.getIndex();
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
// Computing the address of an external symbol, not calling it.
|
|
|
|
if (TM.getRelocationModel() != Reloc::Static) {
|
2010-01-16 08:21:18 +08:00
|
|
|
O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
|
|
|
|
<< "$non_lazy_ptr";
|
2008-11-09 02:59:02 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-01-18 05:43:43 +08:00
|
|
|
O << *GetExternalSymbolSymbol(MO.getSymbolName());
|
2008-11-09 02:59:02 +08:00
|
|
|
return;
|
2010-01-16 08:21:18 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2008-11-09 02:59:02 +08:00
|
|
|
// External or weakly linked global variables need non-lazily-resolved
|
|
|
|
// stubs
|
|
|
|
if (TM.getRelocationModel() != Reloc::Static) {
|
2010-04-15 09:51:59 +08:00
|
|
|
const GlobalValue *GV = MO.getGlobal();
|
2008-11-09 02:59:02 +08:00
|
|
|
if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
|
|
|
|
GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
|
2010-01-18 05:43:43 +08:00
|
|
|
O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
|
2008-11-09 02:59:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-03-13 05:19:23 +08:00
|
|
|
O << *Mang->getSymbol(MO.getGlobal());
|
2008-11-09 02:59:02 +08:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
O << "<unknown operand type: " << MO.getType() << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
2009-01-27 06:33:37 +08:00
|
|
|
unsigned AsmVariant,
|
2010-04-04 13:29:35 +08:00
|
|
|
const char *ExtraCode, raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
// Does this asm operand have a single letter operand modifier?
|
|
|
|
if (ExtraCode && ExtraCode[0]) {
|
|
|
|
if (ExtraCode[1] != 0) return true; // Unknown modifier.
|
2009-01-27 06:33:37 +08:00
|
|
|
|
2008-11-09 02:59:02 +08:00
|
|
|
switch (ExtraCode[0]) {
|
|
|
|
default: return true; // Unknown modifier.
|
2009-01-27 06:33:37 +08:00
|
|
|
case 'L': // Write second word of DImode reference.
|
2008-11-09 02:59:02 +08:00
|
|
|
// Verify that this operand has two consecutive registers.
|
|
|
|
if (!MI->getOperand(OpNo).isReg() ||
|
|
|
|
OpNo+1 == MI->getNumOperands() ||
|
|
|
|
!MI->getOperand(OpNo+1).isReg())
|
|
|
|
return true;
|
|
|
|
++OpNo; // Return the high-part.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-27 06:33:37 +08:00
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, OpNo, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
2010-04-04 13:29:35 +08:00
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
2008-11-09 02:59:02 +08:00
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier.
|
2010-04-04 12:47:45 +08:00
|
|
|
printMemRegReg(MI, OpNo, O);
|
2008-11-09 02:59:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:24:03 +08:00
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeCellSPUAsmPrinter() {
|
2010-01-28 09:50:22 +08:00
|
|
|
RegisterAsmPrinter<SPUAsmPrinter> X(TheCellSPUTarget);
|
2009-07-16 04:24:03 +08:00
|
|
|
}
|