2006-02-05 13:50:24 +08:00
|
|
|
//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-02-05 13:50:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to GAS-format SPARC assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 06:59:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
2006-02-05 13:50:24 +08:00
|
|
|
#include "Sparc.h"
|
|
|
|
#include "SparcInstrInfo.h"
|
2009-06-19 23:48:10 +08:00
|
|
|
#include "SparcTargetMachine.h"
|
2013-04-14 12:35:19 +08:00
|
|
|
#include "MCTargetDesc/SparcBaseInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2009-08-23 04:48:53 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-02-10 08:36:00 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2009-09-14 01:14:04 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-04-04 16:18:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-20 06:59:26 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class SparcAsmPrinter : public AsmPrinter {
|
2009-02-24 16:30:20 +08:00
|
|
|
public:
|
2010-04-04 16:18:47 +08:00
|
|
|
explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
|
|
|
: AsmPrinter(TM, Streamer) {}
|
2006-02-05 13:50:24 +08:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Sparc Assembly Printer";
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
|
|
|
|
void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
|
2006-02-10 15:35:42 +08:00
|
|
|
const char *Modifier = 0);
|
2010-04-04 12:47:45 +08:00
|
|
|
void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
|
2006-02-05 13:50:24 +08:00
|
|
|
|
2010-01-28 09:48:52 +08:00
|
|
|
virtual 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 printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
|
2009-09-14 04:19:22 +08:00
|
|
|
static const char *getRegisterName(unsigned RegNo);
|
2009-09-14 04:08:00 +08:00
|
|
|
|
2008-10-10 18:15:03 +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-10-10 18:15:03 +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-09-16 01:46:24 +08:00
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
|
2013-06-05 02:33:25 +08:00
|
|
|
|
2010-02-18 02:52:56 +08:00
|
|
|
virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
|
|
|
|
const;
|
2006-02-05 13:50:24 +08:00
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
#include "SparcGenAsmWriter.inc"
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &O) {
|
2006-02-05 13:50:24 +08:00
|
|
|
const MachineOperand &MO = MI->getOperand (opNum);
|
2013-04-14 12:35:19 +08:00
|
|
|
unsigned TF = MO.getTargetFlags();
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify the target flags.
|
|
|
|
if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
|
|
|
|
if (MI->getOpcode() == SP::CALL)
|
|
|
|
assert(TF == SPII::MO_NO_FLAG &&
|
|
|
|
"Cannot handle target flags on call address");
|
|
|
|
else if (MI->getOpcode() == SP::SETHIi)
|
|
|
|
assert((TF == SPII::MO_HI || TF == SPII::MO_H44 || TF == SPII::MO_HH) &&
|
|
|
|
"Invalid target flags for address operand on sethi");
|
|
|
|
else
|
|
|
|
assert((TF == SPII::MO_LO || TF == SPII::MO_M44 || TF == SPII::MO_L44 ||
|
|
|
|
TF == SPII::MO_HM) &&
|
|
|
|
"Invalid target flags for small address operand");
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
2013-04-14 12:35:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool CloseParen = true;
|
|
|
|
switch (TF) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown target flags on operand");
|
|
|
|
case SPII::MO_NO_FLAG:
|
|
|
|
CloseParen = false;
|
|
|
|
break;
|
|
|
|
case SPII::MO_LO: O << "%lo("; break;
|
|
|
|
case SPII::MO_HI: O << "%hi("; break;
|
|
|
|
case SPII::MO_H44: O << "%h44("; break;
|
|
|
|
case SPII::MO_M44: O << "%m44("; break;
|
|
|
|
case SPII::MO_L44: O << "%l44("; break;
|
|
|
|
case SPII::MO_HH: O << "%hh("; break;
|
|
|
|
case SPII::MO_HM: O << "%hm("; break;
|
|
|
|
}
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
switch (MO.getType()) {
|
2006-05-05 02:05:43 +08:00
|
|
|
case MachineOperand::MO_Register:
|
2011-11-07 04:37:06 +08:00
|
|
|
O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
|
2006-02-05 13:50:24 +08:00
|
|
|
break;
|
|
|
|
|
2006-05-05 01:21:20 +08:00
|
|
|
case MachineOperand::MO_Immediate:
|
2007-12-31 04:49:49 +08:00
|
|
|
O << (int)MO.getImm();
|
2006-02-05 13:50:24 +08:00
|
|
|
break;
|
2006-04-23 02:53:45 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2010-03-14 05:04:28 +08:00
|
|
|
O << *MO.getMBB()->getSymbol();
|
2006-02-05 13:50:24 +08:00
|
|
|
return;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2010-03-13 05:19:23 +08:00
|
|
|
O << *Mang->getSymbol(MO.getGlobal());
|
2006-02-05 13:50:24 +08:00
|
|
|
break;
|
2013-06-03 13:58:33 +08:00
|
|
|
case MachineOperand::MO_BlockAddress:
|
|
|
|
O << GetBlockAddressSymbol(MO.getBlockAddress())->getName();
|
|
|
|
break;
|
2006-02-05 13:50:24 +08:00
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2009-08-23 05:43:10 +08:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
|
2007-12-31 07:10:15 +08:00
|
|
|
<< MO.getIndex();
|
2006-02-05 13:50:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("<unknown operand type>");
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
|
|
|
if (CloseParen) O << ")";
|
|
|
|
}
|
|
|
|
|
2006-02-10 15:35:42 +08:00
|
|
|
void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
|
2010-04-04 12:47:45 +08:00
|
|
|
raw_ostream &O, const char *Modifier) {
|
|
|
|
printOperand(MI, opNum, O);
|
2008-08-07 17:51:25 +08:00
|
|
|
|
2006-02-10 15:35:42 +08:00
|
|
|
// If this is an ADD operand, emit it like normal operands.
|
|
|
|
if (Modifier && !strcmp(Modifier, "arith")) {
|
|
|
|
O << ", ";
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, opNum+1, O);
|
2006-02-10 15:35:42 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-08-07 17:51:25 +08:00
|
|
|
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MI->getOperand(opNum+1).isReg() &&
|
2006-02-05 13:50:24 +08:00
|
|
|
MI->getOperand(opNum+1).getReg() == SP::G0)
|
|
|
|
return; // don't print "+%g0"
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MI->getOperand(opNum+1).isImm() &&
|
2007-12-31 04:49:49 +08:00
|
|
|
MI->getOperand(opNum+1).getImm() == 0)
|
2006-02-05 13:50:24 +08:00
|
|
|
return; // don't print "+0"
|
2008-08-07 17:51:25 +08:00
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
O << "+";
|
2013-04-14 12:35:19 +08:00
|
|
|
printOperand(MI, opNum+1, O);
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
|
|
|
|
raw_ostream &O) {
|
2009-09-16 01:46:24 +08:00
|
|
|
std::string operand = "";
|
|
|
|
const MachineOperand &MO = MI->getOperand(opNum);
|
|
|
|
switch (MO.getType()) {
|
2012-02-07 10:50:20 +08:00
|
|
|
default: llvm_unreachable("Operand is not a register");
|
2009-09-16 01:46:24 +08:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
|
|
|
|
"Operand is not a physical register ");
|
2013-06-05 02:33:25 +08:00
|
|
|
assert(MO.getReg() != SP::O7 &&
|
2011-01-12 11:52:59 +08:00
|
|
|
"%o7 is assigned as destination for getpcx!");
|
2011-11-07 04:37:06 +08:00
|
|
|
operand = "%" + StringRef(getRegisterName(MO.getReg())).lower();
|
2009-09-16 01:46:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-02-18 02:57:19 +08:00
|
|
|
unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
|
2010-01-27 08:20:02 +08:00
|
|
|
unsigned bbNum = MI->getParent()->getNumber();
|
2009-09-16 01:46:24 +08:00
|
|
|
|
2010-02-18 02:57:19 +08:00
|
|
|
O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
|
|
|
|
O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
|
2009-09-16 01:46:24 +08:00
|
|
|
|
|
|
|
O << "\t sethi\t"
|
2013-06-05 02:33:25 +08:00
|
|
|
<< "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
|
2011-01-12 11:52:59 +08:00
|
|
|
<< ")), " << operand << '\n' ;
|
2009-09-16 01:46:24 +08:00
|
|
|
|
2010-02-18 02:57:19 +08:00
|
|
|
O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
|
2013-06-05 02:33:25 +08:00
|
|
|
O << "\tor\t" << operand
|
2010-04-04 12:47:45 +08:00
|
|
|
<< ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
|
|
|
|
<< ")), " << operand << '\n';
|
2013-06-05 02:33:25 +08:00
|
|
|
O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
|
|
|
|
|
2009-09-16 01:46:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &O) {
|
2007-12-31 04:49:49 +08:00
|
|
|
int CC = (int)MI->getOperand(opNum).getImm();
|
2006-02-05 13:50:24 +08:00
|
|
|
O << SPARCCondCodeToString((SPCC::CondCodes)CC);
|
|
|
|
}
|
|
|
|
|
2008-10-10 18:15:03 +08:00
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
2010-04-04 13:29:35 +08:00
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
2008-10-11 04:29:50 +08:00
|
|
|
if (ExtraCode && ExtraCode[0]) {
|
|
|
|
if (ExtraCode[1] != 0) return true; // Unknown modifier.
|
|
|
|
|
|
|
|
switch (ExtraCode[0]) {
|
2012-06-26 21:49:27 +08:00
|
|
|
default:
|
|
|
|
// See if this is a generic print operand
|
|
|
|
return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
|
2008-10-11 04:29:50 +08:00
|
|
|
case 'r':
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-10 18:15:03 +08:00
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, OpNo, O);
|
2008-10-10 18:15:03 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
2010-04-04 13:29:35 +08:00
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
2008-10-10 18:15:03 +08:00
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier
|
|
|
|
|
|
|
|
O << '[';
|
2010-04-04 12:47:45 +08:00
|
|
|
printMemOperand(MI, OpNo, O);
|
2008-10-10 18:15:03 +08:00
|
|
|
O << ']';
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-17 04:12:29 +08:00
|
|
|
|
2010-02-18 02:52:56 +08:00
|
|
|
/// isBlockOnlyReachableByFallthough - Return true if the basic block has
|
|
|
|
/// exactly one predecessor and the control transfer mechanism between
|
|
|
|
/// the predecessor and this block is a fall-through.
|
2010-03-06 15:02:28 +08:00
|
|
|
///
|
|
|
|
/// This overrides AsmPrinter's implementation to handle delay slots.
|
|
|
|
bool SparcAsmPrinter::
|
|
|
|
isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
|
2010-02-18 02:52:56 +08:00
|
|
|
// If this is a landing pad, it isn't a fall through. If it has no preds,
|
|
|
|
// then nothing falls through to it.
|
|
|
|
if (MBB->isLandingPad() || MBB->pred_empty())
|
|
|
|
return false;
|
2013-06-05 02:33:25 +08:00
|
|
|
|
2010-02-18 02:52:56 +08:00
|
|
|
// If there isn't exactly one predecessor, it can't be a fall through.
|
|
|
|
MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
|
|
|
|
++PI2;
|
|
|
|
if (PI2 != MBB->pred_end())
|
|
|
|
return false;
|
2013-06-05 02:33:25 +08:00
|
|
|
|
2010-02-18 02:52:56 +08:00
|
|
|
// The predecessor has to be immediately before this block.
|
|
|
|
const MachineBasicBlock *Pred = *PI;
|
2013-06-05 02:33:25 +08:00
|
|
|
|
2010-02-18 02:52:56 +08:00
|
|
|
if (!Pred->isLayoutSuccessor(MBB))
|
|
|
|
return false;
|
2013-06-05 02:33:25 +08:00
|
|
|
|
2010-03-06 15:02:28 +08:00
|
|
|
// Check if the last terminator is an unconditional branch.
|
2010-02-18 02:52:56 +08:00
|
|
|
MachineBasicBlock::const_iterator I = Pred->end();
|
2011-12-07 15:15:52 +08:00
|
|
|
while (I != Pred->begin() && !(--I)->isTerminator())
|
2010-03-06 15:02:28 +08:00
|
|
|
; // Noop
|
2011-12-07 15:15:52 +08:00
|
|
|
return I == Pred->end() || !I->isBarrier();
|
2010-02-18 02:52:56 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 07:59:40 +08:00
|
|
|
// Force static initialization.
|
2013-06-05 02:33:25 +08:00
|
|
|
extern "C" void LLVMInitializeSparcAsmPrinter() {
|
2009-07-25 14:49:55 +08:00
|
|
|
RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
|
2010-02-04 14:34:01 +08:00
|
|
|
RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
|
2009-07-16 04:24:03 +08:00
|
|
|
}
|