2004-07-26 18:45:48 +00:00
|
|
|
//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
2004-03-01 23:53:11 +00:00
|
|
|
// This file contains a printer that converts from our internal representation
|
2004-10-04 07:31:08 +00:00
|
|
|
// of machine-dependent LLVM code to Intel and AT&T format assembly
|
|
|
|
// language. This printer is the output mechanism used by `llc' and `lli
|
|
|
|
// -print-machineinstrs' on X86.
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "X86.h"
|
2004-03-09 03:35:34 +00:00
|
|
|
#include "X86TargetMachine.h"
|
2003-08-11 20:06:16 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
2004-08-16 23:16:06 +00:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2003-01-13 00:35:03 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2004-03-09 03:35:34 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2004-08-01 07:43:46 +00:00
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2003-08-11 20:06:16 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2003-07-24 20:20:44 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-02-14 06:00:36 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-10-04 07:31:08 +00:00
|
|
|
namespace {
|
|
|
|
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
|
|
|
|
enum AsmWriterFlavor { att, intel };
|
|
|
|
|
|
|
|
cl::opt<AsmWriterFlavor>
|
|
|
|
AsmWriterFlavor("x86-asm-syntax",
|
|
|
|
cl::desc("Choose style of code to emit from X86 backend:"),
|
|
|
|
cl::values(
|
|
|
|
clEnumVal(att, " Emit AT&T-style assembly"),
|
|
|
|
clEnumVal(intel, " Emit Intel-style assembly"),
|
|
|
|
clEnumValEnd),
|
|
|
|
cl::init(att));
|
|
|
|
|
|
|
|
struct X86SharedAsmPrinter : public AsmPrinter {
|
|
|
|
X86SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
|
|
|
|
: AsmPrinter(O, TM) { }
|
|
|
|
|
|
|
|
void printConstantPool(MachineConstantPool *MCP);
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
static bool isScale(const MachineOperand &MO) {
|
|
|
|
return MO.isImmediate() &&
|
|
|
|
(MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
|
|
|
|
MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMem(const MachineInstr *MI, unsigned Op) {
|
|
|
|
if (MI->getOperand(Op).isFrameIndex()) return true;
|
|
|
|
if (MI->getOperand(Op).isConstantPoolIndex()) return true;
|
|
|
|
return Op+4 <= MI->getNumOperands() &&
|
|
|
|
MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
|
2004-10-15 04:44:53 +00:00
|
|
|
MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
|
|
|
|
MI->getOperand(Op+3).isGlobalAddress());
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwitchSection - Switch to the specified section of the executable if we are
|
|
|
|
// not already in it!
|
|
|
|
//
|
|
|
|
static void SwitchSection(std::ostream &OS, std::string &CurSection,
|
|
|
|
const char *NewSection) {
|
|
|
|
if (CurSection != NewSection) {
|
|
|
|
CurSection = NewSection;
|
|
|
|
if (!CurSection.empty())
|
|
|
|
OS << "\t" << NewSection << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// printConstantPool - Print to the current output stream assembly
|
|
|
|
/// representations of the constants in the constant pool MCP. This is
|
|
|
|
/// used to print out constants which have been "spilled to memory" by
|
|
|
|
/// the code generator.
|
|
|
|
///
|
|
|
|
void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
|
|
|
|
const std::vector<Constant*> &CP = MCP->getConstants();
|
|
|
|
const TargetData &TD = TM.getTargetData();
|
|
|
|
|
|
|
|
if (CP.empty()) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
|
|
|
|
O << "\t.section .rodata\n";
|
|
|
|
emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
|
|
|
|
O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
|
|
|
|
<< *CP[i] << "\n";
|
|
|
|
emitGlobalConstant(CP[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X86SharedAsmPrinter::doFinalization(Module &M) {
|
|
|
|
const TargetData &TD = TM.getTargetData();
|
|
|
|
std::string CurSection;
|
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
|
|
|
for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
|
|
|
|
if (I->hasInitializer()) { // External global require no code
|
|
|
|
O << "\n\n";
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
|
|
|
unsigned Size = TD.getTypeSize(C->getType());
|
|
|
|
unsigned Align = TD.getTypeAlignmentShift(C->getType());
|
|
|
|
|
|
|
|
if (C->isNullValue() &&
|
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
|
|
|
|
SwitchSection(O, CurSection, ".data");
|
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
|
|
|
|
|
|
|
O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
|
|
|
|
<< "," << (1 << Align);
|
|
|
|
O << "\t\t# ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
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";
|
|
|
|
SwitchSection(O, CurSection, "");
|
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
|
|
|
|
break;
|
|
|
|
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())
|
|
|
|
SwitchSection(O, CurSection, ".bss");
|
|
|
|
else
|
|
|
|
SwitchSection(O, CurSection, ".data");
|
|
|
|
break;
|
2004-11-14 21:03:49 +00:00
|
|
|
case GlobalValue::GhostLinkage:
|
|
|
|
std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
|
|
|
|
abort();
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emitAlignment(Align);
|
|
|
|
O << "\t.type " << name << ",@object\n";
|
|
|
|
O << "\t.size " << name << "," << Size << "\n";
|
|
|
|
O << name << ":\t\t\t\t# ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << " = ";
|
|
|
|
WriteAsOperand(O, C, false, false, &M);
|
|
|
|
O << "\n";
|
|
|
|
emitGlobalConstant(C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AsmPrinter::doFinalization(M);
|
|
|
|
return false; // success
|
|
|
|
}
|
|
|
|
|
2002-10-29 22:37:54 +00:00
|
|
|
namespace {
|
2004-10-04 07:24:48 +00:00
|
|
|
struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
|
|
|
|
X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
|
|
|
|
: X86SharedAsmPrinter(O, TM) { }
|
2003-07-23 18:37:06 +00:00
|
|
|
|
2002-12-15 21:13:40 +00:00
|
|
|
virtual const char *getPassName() const {
|
2004-10-04 07:24:48 +00:00
|
|
|
return "X86 Intel-Style Assembly Printer";
|
2002-12-15 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2004-08-01 06:02:08 +00:00
|
|
|
/// printInstruction - This method is automatically generated by tablegen
|
|
|
|
/// from the instruction set description. This method returns true if the
|
|
|
|
/// machine instruction was sufficiently described to print it, otherwise it
|
|
|
|
/// returns false.
|
|
|
|
bool printInstruction(const MachineInstr *MI);
|
|
|
|
|
2004-08-01 07:43:46 +00:00
|
|
|
// This method is used by the tablegen'erated instruction printer.
|
2004-08-16 23:16:06 +00:00
|
|
|
void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
|
2004-08-11 02:25:00 +00:00
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
2004-08-01 08:12:41 +00:00
|
|
|
if (MO.getType() == MachineOperand::MO_MachineRegister) {
|
|
|
|
assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
|
|
|
|
// Bug Workaround: See note in Printer::doInitialization about %.
|
|
|
|
O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
|
|
|
|
} else {
|
|
|
|
printOp(MO);
|
|
|
|
}
|
2004-08-01 07:43:46 +00:00
|
|
|
}
|
|
|
|
|
2004-08-16 23:16:06 +00:00
|
|
|
void printCallOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MVT::ValueType VT) {
|
2004-08-11 06:59:12 +00:00
|
|
|
printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
|
|
|
|
}
|
|
|
|
|
2004-08-11 02:25:00 +00:00
|
|
|
void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MVT::ValueType VT) {
|
|
|
|
switch (VT) {
|
|
|
|
default: assert(0 && "Unknown arg size!");
|
|
|
|
case MVT::i8: O << "BYTE PTR "; break;
|
|
|
|
case MVT::i16: O << "WORD PTR "; break;
|
|
|
|
case MVT::i32:
|
|
|
|
case MVT::f32: O << "DWORD PTR "; break;
|
|
|
|
case MVT::i64:
|
|
|
|
case MVT::f64: O << "QWORD PTR "; break;
|
|
|
|
case MVT::f80: O << "XWORD PTR "; break;
|
|
|
|
}
|
|
|
|
printMemReference(MI, OpNo);
|
|
|
|
}
|
|
|
|
|
2003-07-24 20:20:44 +00:00
|
|
|
void printMachineInstruction(const MachineInstr *MI);
|
2004-06-29 19:28:53 +00:00
|
|
|
void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
|
2003-07-24 20:20:44 +00:00
|
|
|
void printMemReference(const MachineInstr *MI, unsigned Op);
|
2003-06-25 18:01:07 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
2003-06-19 19:32:32 +00:00
|
|
|
bool doInitialization(Module &M);
|
2002-10-29 22:37:54 +00:00
|
|
|
};
|
2003-06-27 00:00:48 +00:00
|
|
|
} // end of anonymous namespace
|
2002-10-29 22:37:54 +00:00
|
|
|
|
2004-08-01 06:02:08 +00:00
|
|
|
|
|
|
|
// Include the auto-generated portion of the assembly writer.
|
2004-10-03 20:36:57 +00:00
|
|
|
#include "X86GenIntelAsmWriter.inc"
|
2004-08-01 06:02:08 +00:00
|
|
|
|
|
|
|
|
2003-07-23 18:37:06 +00:00
|
|
|
/// runOnMachineFunction - This uses the printMachineInstruction()
|
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
2004-10-03 20:36:57 +00:00
|
|
|
bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2004-08-16 23:16:06 +00:00
|
|
|
setupMachineFunction(MF);
|
2003-08-03 23:37:09 +00:00
|
|
|
O << "\n\n";
|
2003-06-27 00:00:48 +00:00
|
|
|
|
2003-01-13 00:35:03 +00:00
|
|
|
// Print out constants referenced by the function
|
2003-06-25 18:01:07 +00:00
|
|
|
printConstantPool(MF.getConstantPool());
|
2003-01-13 00:35:03 +00:00
|
|
|
|
2002-11-14 22:32:30 +00:00
|
|
|
// Print out labels for the function.
|
2003-01-13 00:35:03 +00:00
|
|
|
O << "\t.text\n";
|
2004-08-17 19:25:42 +00:00
|
|
|
emitAlignment(4);
|
2003-06-27 00:00:48 +00:00
|
|
|
O << "\t.globl\t" << CurrentFnName << "\n";
|
2004-08-16 23:16:06 +00:00
|
|
|
O << "\t.type\t" << CurrentFnName << ", @function\n";
|
2003-06-27 00:00:48 +00:00
|
|
|
O << CurrentFnName << ":\n";
|
2002-10-29 22:37:54 +00:00
|
|
|
|
2002-11-14 22:32:30 +00:00
|
|
|
// Print out code for the function.
|
2002-12-28 20:25:38 +00:00
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
2004-11-13 23:27:11 +00:00
|
|
|
// Print a label for the basic block if there are any predecessors.
|
|
|
|
if (I->pred_begin() != I->pred_end())
|
|
|
|
O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
|
|
|
|
<< CommentString << " " << I->getBasicBlock()->getName() << "\n";
|
2002-12-28 20:25:38 +00:00
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
2004-06-29 19:28:53 +00:00
|
|
|
II != E; ++II) {
|
2002-12-28 20:25:38 +00:00
|
|
|
// Print the assembly for the instruction.
|
|
|
|
O << "\t";
|
2004-02-12 02:27:10 +00:00
|
|
|
printMachineInstruction(II);
|
2002-11-14 22:32:30 +00:00
|
|
|
}
|
2002-12-28 20:25:38 +00:00
|
|
|
}
|
2002-10-25 22:55:53 +00:00
|
|
|
|
2002-11-14 22:32:30 +00:00
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-29 22:37:54 +00:00
|
|
|
|
2004-10-03 20:36:57 +00:00
|
|
|
void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
|
2004-10-04 07:24:48 +00:00
|
|
|
bool elideOffsetKeyword /* = false */) {
|
2003-07-23 20:25:08 +00:00
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
2002-11-18 06:56:51 +00:00
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
2002-12-04 17:32:52 +00:00
|
|
|
if (Value *V = MO.getVRegValueOrNull()) {
|
2002-12-04 06:45:19 +00:00
|
|
|
O << "<" << V->getName() << ">";
|
|
|
|
return;
|
|
|
|
}
|
2003-01-13 00:35:03 +00:00
|
|
|
// FALLTHROUGH
|
2002-11-20 18:56:41 +00:00
|
|
|
case MachineOperand::MO_MachineRegister:
|
2004-02-15 21:37:17 +00:00
|
|
|
if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
|
2003-08-11 19:05:46 +00:00
|
|
|
// Bug Workaround: See note in Printer::doInitialization about %.
|
2003-08-13 18:15:15 +00:00
|
|
|
O << "%" << RI.get(MO.getReg()).Name;
|
|
|
|
else
|
2002-11-18 06:56:51 +00:00
|
|
|
O << "%reg" << MO.getReg();
|
|
|
|
return;
|
2002-11-21 02:00:20 +00:00
|
|
|
|
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
|
|
|
O << (int)MO.getImmedValue();
|
|
|
|
return;
|
2004-05-14 06:54:57 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
|
|
|
|
O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
|
|
|
|
<< "_" << MBBOp->getNumber () << "\t# "
|
|
|
|
<< MBBOp->getBasicBlock ()->getName ();
|
2002-12-01 23:25:59 +00:00
|
|
|
return;
|
2003-09-09 16:23:36 +00:00
|
|
|
}
|
2004-05-14 06:54:57 +00:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
|
|
|
|
abort ();
|
|
|
|
return;
|
2004-10-15 04:44:53 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
2003-07-31 17:38:52 +00:00
|
|
|
if (!elideOffsetKeyword)
|
|
|
|
O << "OFFSET ";
|
|
|
|
O << Mang->getValueName(MO.getGlobal());
|
2004-10-15 04:44:53 +00:00
|
|
|
int Offset = MO.getOffset();
|
|
|
|
if (Offset > 0)
|
|
|
|
O << " + " << Offset;
|
|
|
|
else if (Offset < 0)
|
|
|
|
O << " - " << -Offset;
|
2003-01-13 00:35:03 +00:00
|
|
|
return;
|
2004-10-15 04:44:53 +00:00
|
|
|
}
|
2003-01-13 00:35:03 +00:00
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2003-06-19 19:32:32 +00:00
|
|
|
O << MO.getSymbolName();
|
2003-01-13 00:35:03 +00:00
|
|
|
return;
|
2002-11-18 06:56:51 +00:00
|
|
|
default:
|
2003-06-25 18:01:07 +00:00
|
|
|
O << "<unknown operand type>"; return;
|
2002-11-18 06:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
|
2002-11-21 20:44:15 +00:00
|
|
|
assert(isMem(MI, Op) && "Invalid memory reference!");
|
2003-01-13 00:35:03 +00:00
|
|
|
|
2004-10-17 07:16:32 +00:00
|
|
|
const MachineOperand &BaseReg = MI->getOperand(Op);
|
|
|
|
int ScaleVal = MI->getOperand(Op+1).getImmedValue();
|
|
|
|
const MachineOperand &IndexReg = MI->getOperand(Op+2);
|
|
|
|
const MachineOperand &DispSpec = MI->getOperand(Op+3);
|
|
|
|
|
|
|
|
if (BaseReg.isFrameIndex()) {
|
|
|
|
O << "[frame slot #" << BaseReg.getFrameIndex();
|
|
|
|
if (DispSpec.getImmedValue())
|
|
|
|
O << " + " << DispSpec.getImmedValue();
|
2003-01-13 00:35:03 +00:00
|
|
|
O << "]";
|
|
|
|
return;
|
2004-10-17 07:16:32 +00:00
|
|
|
} else if (BaseReg.isConstantPoolIndex()) {
|
2003-06-27 00:00:48 +00:00
|
|
|
O << "[.CPI" << CurrentFnName << "_"
|
2004-10-17 07:16:32 +00:00
|
|
|
<< BaseReg.getConstantPoolIndex();
|
|
|
|
|
|
|
|
if (IndexReg.getReg()) {
|
|
|
|
O << " + ";
|
|
|
|
if (ScaleVal != 1)
|
|
|
|
O << ScaleVal << "*";
|
|
|
|
printOp(IndexReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DispSpec.getImmedValue())
|
|
|
|
O << " + " << DispSpec.getImmedValue();
|
2003-01-13 00:35:03 +00:00
|
|
|
O << "]";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-11-21 20:44:15 +00:00
|
|
|
O << "[";
|
|
|
|
bool NeedPlus = false;
|
|
|
|
if (BaseReg.getReg()) {
|
2004-10-15 04:44:53 +00:00
|
|
|
printOp(BaseReg, true);
|
2002-11-21 20:44:15 +00:00
|
|
|
NeedPlus = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IndexReg.getReg()) {
|
|
|
|
if (NeedPlus) O << " + ";
|
2002-12-28 20:25:38 +00:00
|
|
|
if (ScaleVal != 1)
|
|
|
|
O << ScaleVal << "*";
|
2003-07-23 20:25:08 +00:00
|
|
|
printOp(IndexReg);
|
2002-11-21 20:44:15 +00:00
|
|
|
NeedPlus = true;
|
|
|
|
}
|
|
|
|
|
2004-10-15 04:44:53 +00:00
|
|
|
if (DispSpec.isGlobalAddress()) {
|
2002-12-28 20:25:38 +00:00
|
|
|
if (NeedPlus)
|
2004-10-15 04:44:53 +00:00
|
|
|
O << " + ";
|
|
|
|
printOp(DispSpec, true);
|
|
|
|
} else {
|
|
|
|
int DispVal = DispSpec.getImmedValue();
|
|
|
|
if (DispVal) {
|
|
|
|
if (NeedPlus)
|
|
|
|
if (DispVal > 0)
|
|
|
|
O << " + ";
|
|
|
|
else {
|
|
|
|
O << " - ";
|
|
|
|
DispVal = -DispVal;
|
|
|
|
}
|
|
|
|
O << DispVal;
|
|
|
|
}
|
2002-11-21 20:44:15 +00:00
|
|
|
}
|
|
|
|
O << "]";
|
|
|
|
}
|
|
|
|
|
2004-04-08 20:31:47 +00:00
|
|
|
|
2003-07-23 18:37:06 +00:00
|
|
|
/// printMachineInstruction -- Print out a single X86 LLVM instruction
|
2003-07-23 20:25:08 +00:00
|
|
|
/// MI in Intel syntax to the current output stream.
|
2003-06-27 00:00:48 +00:00
|
|
|
///
|
2004-10-03 20:36:57 +00:00
|
|
|
void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
|
2004-08-01 06:02:08 +00:00
|
|
|
++EmittedInsts;
|
2004-08-11 06:09:55 +00:00
|
|
|
|
2004-08-11 07:02:04 +00:00
|
|
|
// Call the autogenerated instruction printer routines.
|
2004-10-04 07:31:08 +00:00
|
|
|
printInstruction(MI);
|
2002-10-25 22:55:53 +00:00
|
|
|
}
|
2003-06-19 19:32:32 +00:00
|
|
|
|
2004-10-03 20:36:57 +00:00
|
|
|
bool X86IntelAsmPrinter::doInitialization(Module &M) {
|
2004-08-16 23:16:06 +00:00
|
|
|
AsmPrinter::doInitialization(M);
|
2003-08-11 19:35:26 +00:00
|
|
|
// Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
|
|
|
|
//
|
|
|
|
// Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
|
|
|
|
// instruction as a reference to the register named sp, and if you try to
|
|
|
|
// reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
|
|
|
|
// before being looked up in the symbol table. This creates spurious
|
|
|
|
// `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
|
|
|
|
// mode, and decorate all register names with percent signs.
|
2003-08-11 20:04:57 +00:00
|
|
|
O << "\t.intel_syntax\n";
|
2004-08-16 23:16:06 +00:00
|
|
|
return false;
|
2003-06-19 19:32:32 +00:00
|
|
|
}
|
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
|
|
|
|
X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
|
|
|
|
: X86SharedAsmPrinter(O, TM) { }
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "X86 AT&T-Style Assembly Printer";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// printInstruction - This method is automatically generated by tablegen
|
|
|
|
/// from the instruction set description. This method returns true if the
|
|
|
|
/// machine instruction was sufficiently described to print it, otherwise it
|
|
|
|
/// returns false.
|
|
|
|
bool printInstruction(const MachineInstr *MI);
|
|
|
|
|
|
|
|
// This method is used by the tablegen'erated instruction printer.
|
|
|
|
void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
|
|
|
|
printOp(MI->getOperand(OpNo));
|
|
|
|
}
|
|
|
|
|
|
|
|
void printCallOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MVT::ValueType VT) {
|
|
|
|
printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
|
|
|
|
}
|
|
|
|
|
|
|
|
void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MVT::ValueType VT) {
|
|
|
|
printMemReference(MI, OpNo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printMachineInstruction(const MachineInstr *MI);
|
|
|
|
void printOp(const MachineOperand &MO, bool isCallOperand = false);
|
|
|
|
void printMemReference(const MachineInstr *MI, unsigned Op);
|
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
// Include the auto-generated portion of the assembly writer.
|
|
|
|
#include "X86GenATTAsmWriter.inc"
|
|
|
|
|
|
|
|
|
|
|
|
/// runOnMachineFunction - This uses the printMachineInstruction()
|
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
|
|
|
bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
setupMachineFunction(MF);
|
|
|
|
O << "\n\n";
|
|
|
|
|
|
|
|
// Print out constants referenced by the function
|
|
|
|
printConstantPool(MF.getConstantPool());
|
|
|
|
|
|
|
|
// Print out labels for the function.
|
|
|
|
O << "\t.text\n";
|
|
|
|
emitAlignment(4);
|
|
|
|
O << "\t.globl\t" << CurrentFnName << "\n";
|
|
|
|
O << "\t.type\t" << CurrentFnName << ", @function\n";
|
|
|
|
O << CurrentFnName << ":\n";
|
|
|
|
|
|
|
|
// 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.
|
2004-11-13 23:27:11 +00:00
|
|
|
if (I->pred_begin() != I->pred_end())
|
|
|
|
O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
|
|
|
|
<< CommentString << " " << I->getBasicBlock()->getName() << "\n";
|
2004-10-04 07:24:48 +00:00
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
|
|
|
II != E; ++II) {
|
|
|
|
// Print the assembly for the instruction.
|
|
|
|
O << "\t";
|
|
|
|
printMachineInstruction(II);
|
|
|
|
}
|
2003-09-09 16:23:36 +00:00
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
|
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
2003-07-11 21:57:01 +00:00
|
|
|
}
|
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
|
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
|
|
|
assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
|
|
|
|
"Virtual registers should not make it this far!");
|
|
|
|
O << '%';
|
|
|
|
for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
|
|
|
|
O << (char)tolower(*Name);
|
|
|
|
return;
|
2003-09-09 16:23:36 +00:00
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
|
|
|
O << '$' << (int)MO.getImmedValue();
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
|
|
|
|
O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
|
|
|
|
<< "_" << MBBOp->getNumber () << "\t# "
|
|
|
|
<< MBBOp->getBasicBlock ()->getName ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
|
|
|
|
abort ();
|
|
|
|
return;
|
2004-10-15 04:44:53 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
2004-10-04 07:24:48 +00:00
|
|
|
if (!isCallOp) O << '$';
|
|
|
|
O << Mang->getValueName(MO.getGlobal());
|
2004-10-15 04:44:53 +00:00
|
|
|
int Offset = MO.getOffset();
|
|
|
|
if (Offset > 0)
|
|
|
|
O << "+" << Offset;
|
|
|
|
else if (Offset < 0)
|
|
|
|
O << Offset;
|
2004-10-04 07:24:48 +00:00
|
|
|
return;
|
2004-10-15 04:44:53 +00:00
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
if (!isCallOp) O << '$';
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
O << "<unknown operand type>"; return;
|
|
|
|
}
|
|
|
|
}
|
2003-09-09 16:23:36 +00:00
|
|
|
|
2004-10-04 07:24:48 +00:00
|
|
|
void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
|
|
|
|
assert(isMem(MI, Op) && "Invalid memory reference!");
|
2003-09-09 16:23:36 +00:00
|
|
|
|
2004-10-17 07:16:32 +00:00
|
|
|
const MachineOperand &BaseReg = MI->getOperand(Op);
|
|
|
|
int ScaleVal = MI->getOperand(Op+1).getImmedValue();
|
|
|
|
const MachineOperand &IndexReg = MI->getOperand(Op+2);
|
|
|
|
const MachineOperand &DispSpec = MI->getOperand(Op+3);
|
|
|
|
|
|
|
|
if (BaseReg.isFrameIndex()) {
|
|
|
|
O << "[frame slot #" << BaseReg.getFrameIndex();
|
|
|
|
if (DispSpec.getImmedValue())
|
|
|
|
O << " + " << DispSpec.getImmedValue();
|
2004-10-04 07:24:48 +00:00
|
|
|
O << "]";
|
|
|
|
return;
|
2004-10-17 07:16:32 +00:00
|
|
|
} else if (BaseReg.isConstantPoolIndex()) {
|
2004-10-04 07:24:48 +00:00
|
|
|
O << ".CPI" << CurrentFnName << "_"
|
2004-10-17 07:16:32 +00:00
|
|
|
<< BaseReg.getConstantPoolIndex();
|
|
|
|
if (DispSpec.getImmedValue())
|
|
|
|
O << "+" << DispSpec.getImmedValue();
|
|
|
|
if (IndexReg.getReg()) {
|
|
|
|
O << "(,";
|
|
|
|
printOp(IndexReg);
|
|
|
|
if (ScaleVal != 1)
|
|
|
|
O << "," << ScaleVal;
|
|
|
|
O << ")";
|
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-09-09 16:23:36 +00:00
|
|
|
|
2004-10-15 04:44:53 +00:00
|
|
|
if (DispSpec.isGlobalAddress()) {
|
|
|
|
printOp(DispSpec, true);
|
|
|
|
} else {
|
|
|
|
int DispVal = DispSpec.getImmedValue();
|
|
|
|
if (DispVal)
|
|
|
|
O << DispVal;
|
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
|
2004-10-15 04:44:53 +00:00
|
|
|
if (IndexReg.getReg() || BaseReg.getReg()) {
|
|
|
|
O << "(";
|
|
|
|
if (BaseReg.getReg())
|
|
|
|
printOp(BaseReg);
|
2004-10-04 07:24:48 +00:00
|
|
|
|
2004-10-15 04:44:53 +00:00
|
|
|
if (IndexReg.getReg()) {
|
|
|
|
O << ",";
|
|
|
|
printOp(IndexReg);
|
|
|
|
if (ScaleVal != 1)
|
|
|
|
O << "," << ScaleVal;
|
|
|
|
}
|
2004-10-04 07:24:48 +00:00
|
|
|
|
2004-10-15 04:44:53 +00:00
|
|
|
O << ")";
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// printMachineInstruction -- Print out a single X86 LLVM instruction
|
|
|
|
/// MI in Intel syntax to the current output stream.
|
|
|
|
///
|
|
|
|
void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
|
|
|
|
++EmittedInsts;
|
|
|
|
// Call the autogenerated instruction printer routines.
|
2004-10-04 07:31:08 +00:00
|
|
|
printInstruction(MI);
|
2004-10-04 07:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
|
|
|
|
/// for a MachineFunction to the given output stream, using the given target
|
|
|
|
/// machine description.
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
|
|
|
|
switch (AsmWriterFlavor) {
|
|
|
|
default: assert(0 && "Unknown asm flavor!");
|
|
|
|
case intel:
|
|
|
|
return new X86IntelAsmPrinter(o, tm);
|
|
|
|
case att:
|
|
|
|
return new X86ATTAsmPrinter(o, tm);
|
|
|
|
}
|
2003-06-19 19:32:32 +00:00
|
|
|
}
|