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