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"
|
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"
|
2010-03-12 21:19:23 +00:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2010-04-04 06:12:20 +00:00
|
|
|
#include "llvm/ADT/SmallString.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"
|
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);
|
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);
|
|
|
|
bool CloseParen = false;
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
|
2004-06-15 19:52:59 +00:00
|
|
|
O << "%hi(";
|
|
|
|
CloseParen = true;
|
2008-10-03 15:45:36 +00:00
|
|
|
} else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
|
|
|
|
!MO.isReg() && !MO.isImm()) {
|
2004-06-15 19:52:59 +00:00
|
|
|
O << "%lo(";
|
|
|
|
CloseParen = true;
|
|
|
|
}
|
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;
|
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 << "+";
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MI->getOperand(opNum+1).isGlobal() ||
|
|
|
|
MI->getOperand(opNum+1).isCPI()) {
|
Teach the addressing mode stuff to fold "%lo" into 'ri' addressing modes,
allowing us to compile this:
to this:
%G1 = external global int
%G2 = external global int
void %test() {
%X = load int* %G1
store int %X, int* %G2
ret void
}
test:
save -96, %sp, %sp
sethi %hi(G1), %l0
ld [%l0+%lo(G1)], %l0
sethi %hi(G2), %l1
st %l0, [%l1+%lo(G2)]
restore %g0, %g0, %g0
retl
nop
instead of this:
test:
save -96, %sp, %sp
sethi %hi(G1), %l0
or %g0, %lo(G1), %l1
ld [%l1+%l0], %l0
sethi %hi(G2), %l1
or %g0, %lo(G2), %l2
st %l0, [%l2+%l1]
restore %g0, %g0, %g0
retl
nop
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24812 91177308-0d34-0410-b5e6-96231b3b80d8
2005-12-18 02:27:00 +00:00
|
|
|
O << "%lo(";
|
2010-04-04 04:47:45 +00:00
|
|
|
printOperand(MI, opNum+1, O);
|
Teach the addressing mode stuff to fold "%lo" into 'ri' addressing modes,
allowing us to compile this:
to this:
%G1 = external global int
%G2 = external global int
void %test() {
%X = load int* %G1
store int %X, int* %G2
ret void
}
test:
save -96, %sp, %sp
sethi %hi(G1), %l0
ld [%l0+%lo(G1)], %l0
sethi %hi(G2), %l1
st %l0, [%l1+%lo(G2)]
restore %g0, %g0, %g0
retl
nop
instead of this:
test:
save -96, %sp, %sp
sethi %hi(G1), %l0
or %g0, %lo(G1), %l1
ld [%l1+%l0], %l0
sethi %hi(G2), %l1
or %g0, %lo(G2), %l2
st %l0, [%l2+%l1]
restore %g0, %g0, %g0
retl
nop
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24812 91177308-0d34-0410-b5e6-96231b3b80d8
2005-12-18 02:27:00 +00:00
|
|
|
O << ")";
|
|
|
|
} else {
|
2010-04-04 04:47:45 +00:00
|
|
|
printOperand(MI, opNum+1, O);
|
Teach the addressing mode stuff to fold "%lo" into 'ri' addressing modes,
allowing us to compile this:
to this:
%G1 = external global int
%G2 = external global int
void %test() {
%X = load int* %G1
store int %X, int* %G2
ret void
}
test:
save -96, %sp, %sp
sethi %hi(G1), %l0
ld [%l0+%lo(G1)], %l0
sethi %hi(G2), %l1
st %l0, [%l1+%lo(G2)]
restore %g0, %g0, %g0
retl
nop
instead of this:
test:
save -96, %sp, %sp
sethi %hi(G1), %l0
or %g0, %lo(G1), %l1
ld [%l1+%l0], %l0
sethi %hi(G2), %l1
or %g0, %lo(G2), %l2
st %l0, [%l2+%l1]
restore %g0, %g0, %g0
retl
nop
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24812 91177308-0d34-0410-b5e6-96231b3b80d8
2005-12-18 02:27:00 +00:00
|
|
|
}
|
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()) {
|
|
|
|
default: assert(0 && "Operand is not a register ");
|
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
|
|
|
|
"Operand is not a physical register ");
|
2011-01-12 03:52:59 +00:00
|
|
|
assert(MO.getReg() != SP::O7 &&
|
|
|
|
"%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"
|
2011-01-12 03:52:59 +00:00
|
|
|
<< "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
|
|
|
|
<< ")), " << operand << '\n' ;
|
2009-09-15 17:46:24 +00:00
|
|
|
|
2010-02-17 18:57:19 +00:00
|
|
|
O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
|
2009-09-15 17:46:24 +00:00
|
|
|
O << "\tor\t" << operand
|
2010-04-04 04:47:45 +00:00
|
|
|
<< ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
|
|
|
|
<< ")), " << operand << '\n';
|
2009-09-15 17:46:24 +00:00
|
|
|
O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
|
|
|
|
|
|
|
|
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]) {
|
|
|
|
default: return true; // Unknown modifier.
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// The predecessor has to be immediately before this block.
|
|
|
|
const MachineBasicBlock *Pred = *PI;
|
|
|
|
|
|
|
|
if (!Pred->isLayoutSuccessor(MBB))
|
|
|
|
return false;
|
|
|
|
|
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();
|
2010-03-06 07:02:28 +00:00
|
|
|
while (I != Pred->begin() && !(--I)->getDesc().isTerminator())
|
|
|
|
; // Noop
|
2010-02-17 18:52:56 +00:00
|
|
|
return I == Pred->end() || !I->getDesc().isBarrier();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-23 23:59:40 +00:00
|
|
|
// Force static initialization.
|
2009-07-15 20:24:03 +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
|
|
|
}
|