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"
|
2004-03-04 06:00:41 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.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/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2006-09-06 18:34:40 +00:00
|
|
|
#include "llvm/Target/TargetAsmInfo.h"
|
2006-05-12 06:33:49 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-03-04 06:00:41 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Mangler.h"
|
2004-09-02 02:37:43 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2005-08-17 20:04:34 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2004-03-06 05:30:21 +00:00
|
|
|
#include <cctype>
|
2008-02-20 11:08:44 +00:00
|
|
|
#include <cstring>
|
2004-03-04 06:00:41 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 22:59:26 +00:00
|
|
|
STATISTIC(EmittedInsts, "Number of machine instrs printed");
|
2004-03-04 06:00:41 +00:00
|
|
|
|
2006-12-19 22:59:26 +00:00
|
|
|
namespace {
|
2006-09-06 18:34:40 +00:00
|
|
|
struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
|
2006-09-07 22:06:40 +00:00
|
|
|
SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
|
2006-09-06 18:34:40 +00:00
|
|
|
: AsmPrinter(O, TM, T) {
|
|
|
|
}
|
2004-03-04 06:00:41 +00:00
|
|
|
|
|
|
|
/// We name each basic block in a Function with a unique number, so
|
|
|
|
/// that we can consistently refer to them later. This is cleared
|
|
|
|
/// at the beginning of each call to runOnMachineFunction().
|
|
|
|
///
|
|
|
|
typedef std::map<const Value *, unsigned> ValueMapTy;
|
|
|
|
ValueMapTy NumberForBB;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-06-15 19:52:59 +00:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum);
|
2006-02-10 07:35:42 +00:00
|
|
|
void printMemOperand(const MachineInstr *MI, int opNum,
|
|
|
|
const char *Modifier = 0);
|
2006-02-05 05:50:24 +00:00
|
|
|
void printCCOperand(const MachineInstr *MI, int opNum);
|
2006-01-31 06:49:09 +00:00
|
|
|
|
2005-12-16 06:34:17 +00:00
|
|
|
bool printInstruction(const MachineInstr *MI); // autogenerated.
|
2005-04-21 23:30:14 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
2004-03-04 06:00:41 +00:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2006-02-05 05:50:24 +00:00
|
|
|
#include "SparcGenAsmWriter.inc"
|
2005-12-16 06:34:17 +00:00
|
|
|
|
2006-02-05 05:50:24 +00:00
|
|
|
/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
|
2004-03-04 06:00:41 +00:00
|
|
|
/// assembly code for a MachineFunction to the given output stream,
|
|
|
|
/// using the given target machine description. This should work
|
|
|
|
/// regardless of whether the function is in SSA form.
|
|
|
|
///
|
2006-02-05 05:50:24 +00:00
|
|
|
FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
|
|
|
|
TargetMachine &tm) {
|
2006-09-07 22:06:40 +00:00
|
|
|
return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2004-03-04 06:00:41 +00:00
|
|
|
}
|
|
|
|
|
2008-02-02 08:39:46 +00:00
|
|
|
/// runOnMachineFunction - This uses the printInstruction()
|
2004-03-04 06:00:41 +00:00
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
2006-02-05 05:50:24 +00:00
|
|
|
bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2005-12-17 07:04:29 +00:00
|
|
|
SetupMachineFunction(MF);
|
|
|
|
|
2005-12-17 07:11:43 +00:00
|
|
|
// Print out constants referenced by the function
|
|
|
|
EmitConstantPool(MF.getConstantPool());
|
|
|
|
|
2004-03-04 06:00:41 +00:00
|
|
|
// BBNumber is used here so that a given Printer will never give two
|
|
|
|
// BBs the same name. (If you have a better way, please let me know!)
|
|
|
|
static unsigned BBNumber = 0;
|
|
|
|
|
|
|
|
O << "\n\n";
|
|
|
|
// What's my mangled name?
|
|
|
|
CurrentFnName = Mang->getValueName(MF.getFunction());
|
|
|
|
|
2006-10-05 02:48:40 +00:00
|
|
|
// Print out the label for the function.
|
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
|
|
|
|
EmitAlignment(4, F);
|
2004-03-04 06:00:41 +00:00
|
|
|
O << "\t.globl\t" << CurrentFnName << "\n";
|
2004-03-16 22:52:04 +00:00
|
|
|
O << "\t.type\t" << CurrentFnName << ", #function\n";
|
2004-03-04 06:00:41 +00:00
|
|
|
O << CurrentFnName << ":\n";
|
|
|
|
|
|
|
|
// Number each basic block so that we can consistently refer to them
|
|
|
|
// in PC-relative references.
|
2006-10-05 02:48:40 +00:00
|
|
|
// FIXME: Why not use the MBB numbers?
|
2004-03-04 06:00:41 +00:00
|
|
|
NumberForBB.clear();
|
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
NumberForBB[I->getBasicBlock()] = BBNumber++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out code for the function.
|
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// Print a label for the basic block.
|
2006-05-02 05:37:32 +00:00
|
|
|
if (I != MF.begin()) {
|
2008-02-28 00:43:03 +00:00
|
|
|
printBasicBlockLabel(I, true, true);
|
2006-05-02 05:37:32 +00:00
|
|
|
O << '\n';
|
|
|
|
}
|
2004-03-04 06:00:41 +00:00
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
2005-04-22 18:06:01 +00:00
|
|
|
II != E; ++II) {
|
2004-03-04 06:00:41 +00:00
|
|
|
// Print the assembly for the instruction.
|
2005-12-17 06:54:41 +00:00
|
|
|
printInstruction(II);
|
2005-12-17 07:04:29 +00:00
|
|
|
++EmittedInsts;
|
2004-03-04 06:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-05 05:50:24 +00:00
|
|
|
void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
|
2004-06-15 19:52:59 +00:00
|
|
|
const MachineOperand &MO = MI->getOperand (opNum);
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo &RI = *TM.getRegisterInfo();
|
2004-06-15 19:52:59 +00:00
|
|
|
bool CloseParen = false;
|
2006-02-05 05:50:24 +00:00
|
|
|
if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
|
2004-06-15 19:52:59 +00:00
|
|
|
O << "%hi(";
|
|
|
|
CloseParen = true;
|
2006-02-05 05:50:24 +00:00
|
|
|
} else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
|
Don't print a label for the first MBB in a function.
Compile this:
%_2E_str_8 = external global [75 x sbyte]
implementation ; Functions:
declare int %printf(sbyte*, ...)
void %test()
%tmp.101 = call int (sbyte*, ...)* %printf( sbyte* getelementptr ([75 x sbyte]* %_2E_str_8, int 0, int 0) ) ; <int> [#uses=0]
unreachable
}
to this:
main_endif_2E_8:
save -96, %o6, %o6
sethi %hi(_2E_str_8), %l0
add %l0, %lo(_2E_str_8), %o0
call printf
nop
instead of this:
main_endif_2E_8:
save -96, %o6, %o6
sethi %hi(_2E_str_8), %l0
or %g0, %lo(_2E_str_8), %l1 ;; extra instruction
add %l1, %l0, %o0
call printf
nop
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25335 91177308-0d34-0410-b5e6-96231b3b80d8
2006-01-15 09:26:27 +00:00
|
|
|
&& !MO.isRegister() && !MO.isImmediate()) {
|
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:
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
|
2008-02-26 21:11:01 +00:00
|
|
|
O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
|
2004-03-05 08:39:09 +00:00
|
|
|
else
|
|
|
|
O << "%reg" << MO.getReg();
|
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:
|
2007-12-30 23:10:15 +00:00
|
|
|
printBasicBlockLabel(MO.getMBB());
|
2004-06-17 19:39:23 +00:00
|
|
|
return;
|
2004-03-05 08:39:09 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
O << Mang->getValueName(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:
|
2007-10-14 05:57:21 +00:00
|
|
|
O << TAI->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:
|
2005-04-21 23:30:14 +00:00
|
|
|
O << "<unknown operand type>"; abort (); break;
|
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,
|
|
|
|
const char *Modifier) {
|
2005-12-17 20:04:49 +00:00
|
|
|
printOperand(MI, opNum);
|
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 << ", ";
|
|
|
|
printOperand(MI, opNum+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-04 18:05:43 +00:00
|
|
|
if (MI->getOperand(opNum+1).isRegister() &&
|
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"
|
2006-05-04 18:05:43 +00:00
|
|
|
if (MI->getOperand(opNum+1).isImmediate() &&
|
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"
|
|
|
|
|
2005-12-17 20:04:49 +00:00
|
|
|
O << "+";
|
2006-05-04 18:05:43 +00:00
|
|
|
if (MI->getOperand(opNum+1).isGlobalAddress() ||
|
|
|
|
MI->getOperand(opNum+1).isConstantPoolIndex()) {
|
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(";
|
|
|
|
printOperand(MI, opNum+1);
|
|
|
|
O << ")";
|
|
|
|
} else {
|
|
|
|
printOperand(MI, opNum+1);
|
|
|
|
}
|
2005-12-17 20:04:49 +00:00
|
|
|
}
|
|
|
|
|
2006-02-05 05:50:24 +00:00
|
|
|
void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-17 20:04:49 +00:00
|
|
|
|
2006-02-05 05:50:24 +00:00
|
|
|
bool SparcAsmPrinter::doInitialization(Module &M) {
|
2004-03-04 06:00:41 +00:00
|
|
|
Mang = new Mangler(M);
|
|
|
|
return false; // success
|
|
|
|
}
|
|
|
|
|
2006-02-05 05:50:24 +00:00
|
|
|
bool SparcAsmPrinter::doFinalization(Module &M) {
|
2006-05-03 01:29:57 +00:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2004-03-04 06:00:41 +00:00
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
2006-03-09 06:14:35 +00:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I)
|
2004-03-04 06:00:41 +00:00
|
|
|
if (I->hasInitializer()) { // External global require no code
|
2006-03-09 06:14:35 +00:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
|
|
|
continue;
|
|
|
|
|
2004-03-04 06:00:41 +00:00
|
|
|
O << "\n\n";
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
2007-11-05 00:04:43 +00:00
|
|
|
unsigned Size = TD->getABITypeSize(C->getType());
|
2008-01-29 06:23:44 +00:00
|
|
|
unsigned Align = TD->getPreferredAlignment(I);
|
2004-03-04 06:00:41 +00:00
|
|
|
|
2005-04-21 23:30:14 +00:00
|
|
|
if (C->isNullValue() &&
|
2004-03-04 06:00:41 +00:00
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection(".data", I);
|
2004-03-04 06:00:41 +00:00
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2007-11-05 00:04:43 +00:00
|
|
|
O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType())
|
2007-01-20 22:35:55 +00:00
|
|
|
<< "," << Align;
|
2004-03-04 06:00:41 +00:00
|
|
|
O << "\n";
|
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
|
|
|
|
// Nonnull linkonce -> weak
|
|
|
|
O << "\t.weak " << name << "\n";
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection("", I);
|
2005-12-17 07:04:29 +00:00
|
|
|
O << "\t.section\t\".llvm.linkonce.d." << name
|
|
|
|
<< "\",\"aw\",@progbits\n";
|
2004-03-04 06:00:41 +00:00
|
|
|
break;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-04 06:00:41 +00:00
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// FIXME: appending linkage variables should go into a section of
|
|
|
|
// their name or something. For now, just emit them as external.
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
// If external or appending, declare as a global symbol
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
// FALL THROUGH
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
if (C->isNullValue())
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection(".bss", I);
|
2004-03-04 06:00:41 +00:00
|
|
|
else
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection(".data", I);
|
2004-03-04 06:00:41 +00:00
|
|
|
break;
|
2004-11-19 21:49:19 +00:00
|
|
|
case GlobalValue::GhostLinkage:
|
2006-12-07 22:21:48 +00:00
|
|
|
cerr << "Should not have any unmaterialized functions!\n";
|
2004-11-19 21:49:19 +00:00
|
|
|
abort();
|
2006-09-14 18:23:27 +00:00
|
|
|
case GlobalValue::DLLImportLinkage:
|
2006-12-07 22:21:48 +00:00
|
|
|
cerr << "DLLImport linkage is not supported by this target!\n";
|
2006-09-14 18:23:27 +00:00
|
|
|
abort();
|
|
|
|
case GlobalValue::DLLExportLinkage:
|
2006-12-07 22:21:48 +00:00
|
|
|
cerr << "DLLExport linkage is not supported by this target!\n";
|
2006-09-14 18:23:27 +00:00
|
|
|
abort();
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
2004-03-04 06:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
O << "\t.align " << Align << "\n";
|
2004-03-16 22:52:04 +00:00
|
|
|
O << "\t.type " << name << ",#object\n";
|
2004-03-04 06:00:41 +00:00
|
|
|
O << "\t.size " << name << "," << Size << "\n";
|
2006-12-06 06:13:25 +00:00
|
|
|
O << name << ":\n";
|
2005-12-17 07:17:08 +00:00
|
|
|
EmitGlobalConstant(C);
|
2004-03-04 06:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-25 19:33:14 +00:00
|
|
|
return AsmPrinter::doFinalization(M);
|
2004-03-04 06:00:41 +00:00
|
|
|
}
|