opnsense-src/contrib/llvm/lib/Target/CellSPU/SPUAsmPrinter.cpp

335 lines
11 KiB
C++
Raw Normal View History

2009-06-02 13:52:33 -04: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-10-14 13:57:32 -04:00
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCSymbol.h"
2010-03-16 12:51:38 -04:00
#include "llvm/Target/Mangler.h"
2009-10-14 13:57:32 -04:00
#include "llvm/Target/TargetLoweringObjectFile.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetOptions.h"
2009-10-14 13:57:32 -04:00
#include "llvm/Target/TargetRegisterInfo.h"
2010-04-06 11:52:58 -04:00
#include "llvm/ADT/SmallString.h"
2009-06-02 13:52:33 -04:00
#include "llvm/ADT/StringExtras.h"
2009-10-14 13:57:32 -04:00
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TargetRegistry.h"
2010-04-06 11:52:58 -04:00
#include "llvm/Support/raw_ostream.h"
2009-06-02 13:52:33 -04:00
using namespace llvm;
namespace {
2009-11-04 09:58:56 -05:00
class SPUAsmPrinter : public AsmPrinter {
2009-06-02 13:52:33 -04:00
public:
2010-04-06 11:52:58 -04:00
explicit SPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) :
AsmPrinter(TM, Streamer) {}
2009-06-02 13:52:33 -04:00
virtual const char *getPassName() const {
return "STI CBEA SPU Assembly Printer";
}
/// printInstruction - This method is automatically generated by tablegen
2009-10-14 13:57:32 -04:00
/// from the instruction set description.
2010-04-06 11:52:58 -04:00
void printInstruction(const MachineInstr *MI, raw_ostream &OS);
2009-10-14 13:57:32 -04:00
static const char *getRegisterName(unsigned RegNo);
2009-06-02 13:52:33 -04:00
2010-02-16 04:30:23 -05:00
void EmitInstruction(const MachineInstr *MI) {
2010-04-06 11:52:58 -04:00
SmallString<128> Str;
raw_svector_ostream OS(Str);
printInstruction(MI, OS);
OutStreamer.EmitRawText(OS.str());
2010-02-16 04:30:23 -05:00
}
2010-04-06 11:52:58 -04:00
void printOp(const MachineOperand &MO, raw_ostream &OS);
2009-06-02 13:52:33 -04:00
2010-04-06 11:52:58 -04:00
void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
2009-06-02 13:52:33 -04:00
const MachineOperand &MO = MI->getOperand(OpNo);
if (MO.isReg()) {
2009-10-14 13:57:32 -04:00
O << getRegisterName(MO.getReg());
2009-06-02 13:52:33 -04:00
} else if (MO.isImm()) {
O << MO.getImm();
} else {
2010-04-06 11:52:58 -04:00
printOp(MO, O);
2009-06-02 13:52:33 -04:00
}
}
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
2010-04-06 11:52:58 -04:00
unsigned AsmVariant, const char *ExtraCode,
raw_ostream &O);
2009-06-02 13:52:33 -04:00
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
2010-04-06 11:52:58 -04:00
unsigned AsmVariant, const char *ExtraCode,
raw_ostream &O);
2009-06-02 13:52:33 -04:00
void
2010-04-06 11:52:58 -04:00
printU7ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
unsigned int value = MI->getOperand(OpNo).getImm();
assert(value < (1 << 8) && "Invalid u7 argument");
O << value;
}
void
2010-04-06 11:52:58 -04:00
printShufAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
char value = MI->getOperand(OpNo).getImm();
O << (int) value;
O << "(";
2010-04-06 11:52:58 -04:00
printOperand(MI, OpNo+1, O);
2009-06-02 13:52:33 -04:00
O << ")";
}
void
2010-04-06 11:52:58 -04:00
printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
O << (short) MI->getOperand(OpNo).getImm();
}
void
2010-04-06 11:52:58 -04:00
printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
O << (unsigned short)MI->getOperand(OpNo).getImm();
}
void
2010-04-06 11:52:58 -04:00
printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
2009-06-02 13:52:33 -04: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-10-14 13:57:32 -04:00
O << getRegisterName(MO.getReg()) << ", ";
2010-04-06 11:52:58 -04:00
printOperand(MI, OpNo+1, O);
2009-06-02 13:52:33 -04:00
}
void
2010-04-06 11:52:58 -04:00
printU18ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
unsigned int value = MI->getOperand(OpNo).getImm();
assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
O << value;
}
void
2010-04-06 11:52:58 -04:00
printS10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04: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-06 11:52:58 -04:00
printU10ImmOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
>> 16);
assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
O << value;
}
void
2010-04-06 11:52:58 -04:00
printDFormAddr(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
assert(MI->getOperand(OpNo).isImm() &&
"printDFormAddr first operand is not immediate");
int64_t value = int64_t(MI->getOperand(OpNo).getImm());
int16_t value16 = int16_t(value);
assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
&& "Invalid dform s10 offset argument");
O << (value16 & ~0xf) << "(";
2010-04-06 11:52:58 -04:00
printOperand(MI, OpNo+1, O);
2009-06-02 13:52:33 -04:00
O << ")";
}
void
2010-04-06 11:52:58 -04:00
printAddr256K(const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
2009-06-02 13:52:33 -04:00
{
/* Note: operand 1 is an offset or symbol name. */
if (MI->getOperand(OpNo).isImm()) {
2010-04-06 11:52:58 -04:00
printS16ImmOperand(MI, OpNo, O);
2009-06-02 13:52:33 -04:00
} else {
2010-04-06 11:52:58 -04:00
printOp(MI->getOperand(OpNo), O);
2009-06-02 13:52:33 -04: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-06 11:52:58 -04:00
void printCallOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
printOp(MI->getOperand(OpNo), O);
2009-06-02 13:52:33 -04:00
}
void printHBROperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
printOp(MI->getOperand(OpNo), O);
}
2010-04-06 11:52:58 -04:00
void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
2009-06-02 13:52:33 -04: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-06 11:52:58 -04:00
printOp(MI->getOperand(OpNo), O);
2009-06-02 13:52:33 -04:00
}
2010-04-06 11:52:58 -04:00
void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
2009-06-02 13:52:33 -04:00
if (MI->getOperand(OpNo).isImm()) {
2010-04-06 11:52:58 -04:00
printS16ImmOperand(MI, OpNo, O);
2009-06-02 13:52:33 -04:00
} else {
2010-04-06 11:52:58 -04:00
printOp(MI->getOperand(OpNo), O);
2009-06-02 13:52:33 -04:00
O << "@h";
}
}
2010-04-06 11:52:58 -04:00
void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
2009-06-02 13:52:33 -04:00
if (MI->getOperand(OpNo).isImm()) {
2010-04-06 11:52:58 -04:00
printS16ImmOperand(MI, OpNo, O);
2009-06-02 13:52:33 -04:00
} else {
2010-04-06 11:52:58 -04:00
printOp(MI->getOperand(OpNo), O);
2009-06-02 13:52:33 -04:00
O << "@l";
}
}
/// Print local store address
2010-04-06 11:52:58 -04:00
void printSymbolLSA(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
printOp(MI->getOperand(OpNo), O);
2009-06-02 13:52:33 -04:00
}
2010-04-06 11:52:58 -04:00
void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo,
raw_ostream &O) {
2009-06-02 13:52:33 -04: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-10-14 13:57:32 -04:00
llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
2009-06-02 13:52:33 -04:00
}
}
2010-04-06 11:52:58 -04: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;
2009-06-02 13:52:33 -04:00
}
};
} // end of anonymous namespace
// Include the auto-generated portion of the assembly writer
#include "SPUGenAsmWriter.inc"
2010-04-06 11:52:58 -04:00
void SPUAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
2009-06-02 13:52:33 -04:00
switch (MO.getType()) {
case MachineOperand::MO_Immediate:
2010-05-04 12:11:02 -04:00
report_fatal_error("printOp() does not handle immediate values");
2009-06-02 13:52:33 -04:00
return;
case MachineOperand::MO_MachineBasicBlock:
2010-03-16 12:51:38 -04:00
O << *MO.getMBB()->getSymbol();
2009-06-02 13:52:33 -04:00
return;
case MachineOperand::MO_JumpTableIndex:
2009-10-14 13:57:32 -04:00
O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
2009-06-02 13:52:33 -04:00
<< '_' << MO.getIndex();
return;
case MachineOperand::MO_ConstantPoolIndex:
2009-10-14 13:57:32 -04:00
O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
2009-06-02 13:52:33 -04: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-23 06:09:33 -05:00
O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
<< "$non_lazy_ptr";
2009-06-02 13:52:33 -04:00
return;
}
2010-01-23 06:09:33 -05:00
O << *GetExternalSymbolSymbol(MO.getSymbolName());
2009-06-02 13:52:33 -04:00
return;
2010-01-23 06:09:33 -05:00
case MachineOperand::MO_GlobalAddress:
2009-06-02 13:52:33 -04:00
// External or weakly linked global variables need non-lazily-resolved
// stubs
if (TM.getRelocationModel() != Reloc::Static) {
2010-05-04 12:11:02 -04:00
const GlobalValue *GV = MO.getGlobal();
2009-06-02 13:52:33 -04:00
if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
2010-01-23 06:09:33 -05:00
O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
2009-06-02 13:52:33 -04:00
return;
}
}
2010-03-16 12:51:38 -04:00
O << *Mang->getSymbol(MO.getGlobal());
2009-06-02 13:52:33 -04:00
return;
case MachineOperand::MO_MCSymbol:
O << *(MO.getMCSymbol());
return;
2009-06-02 13:52:33 -04:00
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,
unsigned AsmVariant,
2010-04-06 11:52:58 -04:00
const char *ExtraCode, raw_ostream &O) {
2009-06-02 13:52:33 -04:00
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0) return true; // Unknown modifier.
switch (ExtraCode[0]) {
default: return true; // Unknown modifier.
case 'L': // Write second word of DImode reference.
// 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;
}
}
2010-04-06 11:52:58 -04:00
printOperand(MI, OpNo, O);
2009-06-02 13:52:33 -04:00
return false;
}
bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
2010-04-06 11:52:58 -04:00
unsigned OpNo, unsigned AsmVariant,
const char *ExtraCode,
raw_ostream &O) {
2009-06-02 13:52:33 -04:00
if (ExtraCode && ExtraCode[0])
return true; // Unknown modifier.
2010-04-06 11:52:58 -04:00
printMemRegReg(MI, OpNo, O);
2009-06-02 13:52:33 -04:00
return false;
}
2009-06-27 06:44:33 -04:00
// Force static initialization.
2009-10-14 13:57:32 -04:00
extern "C" void LLVMInitializeCellSPUAsmPrinter() {
2010-02-16 04:30:23 -05:00
RegisterAsmPrinter<SPUAsmPrinter> X(TheCellSPUTarget);
2009-06-22 04:08:12 -04:00
}