2004-07-27 02:45:48 +08:00
|
|
|
//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
|
2003-10-21 03:43:21 +08: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-26 06:55:53 +08:00
|
|
|
//
|
2004-03-02 07:53:11 +08:00
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to Intel-format assembly language. This
|
|
|
|
// printer is the output mechanism used by `llc' and `lli -print-machineinstrs'
|
|
|
|
// on X86.
|
2002-10-26 06:55:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "X86.h"
|
2002-11-15 06:32:30 +08:00
|
|
|
#include "X86InstrInfo.h"
|
2004-03-09 11:35:34 +08:00
|
|
|
#include "X86TargetMachine.h"
|
2003-08-04 07:37:09 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2003-08-12 04:06:16 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
2004-08-17 07:16:06 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2004-03-09 11:35:34 +08:00
|
|
|
#include "llvm/CodeGen/MachineCodeEmitter.h"
|
2003-01-13 08:35:03 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2004-03-09 11:35:34 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-11-18 06:53:13 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2004-08-01 15:43:46 +08:00
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2003-08-12 04:06:16 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2003-07-25 04:20:44 +08:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2003-10-06 23:41:21 +08:00
|
|
|
#include "Support/Statistic.h"
|
2003-08-04 07:37:09 +08:00
|
|
|
#include "Support/StringExtras.h"
|
2003-08-12 03:35:26 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2004-02-14 14:00:36 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2002-10-30 06:37:54 +08:00
|
|
|
namespace {
|
2003-10-06 23:41:21 +08:00
|
|
|
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
|
|
|
|
|
2004-03-09 11:35:34 +08:00
|
|
|
struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
|
2004-07-27 02:48:58 +08:00
|
|
|
GasBugWorkaroundEmitter(std::ostream& o)
|
|
|
|
: O(o), OldFlags(O.flags()), firstByte(true) {
|
|
|
|
O << std::hex;
|
|
|
|
}
|
2004-03-09 11:35:34 +08:00
|
|
|
|
2004-07-27 02:48:58 +08:00
|
|
|
~GasBugWorkaroundEmitter() {
|
|
|
|
O.flags(OldFlags);
|
|
|
|
}
|
2004-03-09 11:35:34 +08:00
|
|
|
|
2004-07-27 02:48:58 +08:00
|
|
|
virtual void emitByte(unsigned char B) {
|
|
|
|
if (!firstByte) O << "\n\t";
|
|
|
|
firstByte = false;
|
|
|
|
O << ".byte 0x" << (unsigned) B;
|
|
|
|
}
|
2004-03-09 11:35:34 +08:00
|
|
|
|
2004-07-27 02:48:58 +08:00
|
|
|
// These should never be called
|
|
|
|
virtual void emitWord(unsigned W) { assert(0); }
|
|
|
|
virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
|
|
|
|
virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
|
|
|
|
virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
|
|
|
|
virtual uint64_t getCurrentPCValue() { abort(); }
|
|
|
|
virtual uint64_t forceCompilationOf(Function *F) { abort(); }
|
2004-03-09 11:35:34 +08:00
|
|
|
|
|
|
|
private:
|
2004-07-27 02:48:58 +08:00
|
|
|
std::ostream& O;
|
|
|
|
std::ios::fmtflags OldFlags;
|
|
|
|
bool firstByte;
|
2004-03-09 11:35:34 +08:00
|
|
|
};
|
|
|
|
|
2004-08-17 07:16:06 +08:00
|
|
|
struct X86AsmPrinter : public AsmPrinter {
|
|
|
|
X86AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) { }
|
2003-07-24 02:37:06 +08:00
|
|
|
|
2002-12-16 05:13:40 +08:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "X86 Assembly Printer";
|
|
|
|
}
|
|
|
|
|
2004-08-01 14:02:08 +08: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 15:43:46 +08:00
|
|
|
// This method is used by the tablegen'erated instruction printer.
|
2004-08-17 07:16:06 +08:00
|
|
|
void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
|
2004-08-11 10:25:00 +08:00
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
2004-08-01 16:12:41 +08: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 15:43:46 +08:00
|
|
|
}
|
|
|
|
|
2004-08-17 07:16:06 +08:00
|
|
|
void printCallOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MVT::ValueType VT) {
|
2004-08-11 14:59:12 +08:00
|
|
|
printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
|
|
|
|
}
|
|
|
|
|
2004-08-11 10:25:00 +08: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-25 04:20:44 +08:00
|
|
|
void printMachineInstruction(const MachineInstr *MI);
|
2004-06-30 03:28:53 +08:00
|
|
|
void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
|
2003-07-25 04:20:44 +08:00
|
|
|
void printMemReference(const MachineInstr *MI, unsigned Op);
|
|
|
|
void printConstantPool(MachineConstantPool *MCP);
|
2003-06-26 02:01:07 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
2003-06-20 03:32:32 +08:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool doFinalization(Module &M);
|
2002-10-30 06:37:54 +08:00
|
|
|
};
|
2003-06-27 08:00:48 +08:00
|
|
|
} // end of anonymous namespace
|
2002-10-30 06:37:54 +08:00
|
|
|
|
2003-07-24 02:37:06 +08:00
|
|
|
/// createX86CodePrinterPass - Returns a pass that prints the X86
|
2003-07-24 04:25:08 +08: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.
|
2002-11-18 06:53:13 +08:00
|
|
|
///
|
2004-02-14 14:00:36 +08:00
|
|
|
FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
|
2004-08-01 14:02:08 +08:00
|
|
|
return new X86AsmPrinter(o, tm);
|
2002-11-18 06:53:13 +08:00
|
|
|
}
|
|
|
|
|
2004-08-01 14:02:08 +08:00
|
|
|
|
|
|
|
// Include the auto-generated portion of the assembly writer.
|
|
|
|
#include "X86GenAsmWriter.inc"
|
|
|
|
|
|
|
|
|
2003-07-24 02:37:06 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
2004-08-01 14:02:08 +08:00
|
|
|
void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
|
2003-01-13 08:35:03 +08:00
|
|
|
const std::vector<Constant*> &CP = MCP->getConstants();
|
2003-07-24 04:25:08 +08:00
|
|
|
const TargetData &TD = TM.getTargetData();
|
2003-07-24 02:37:06 +08:00
|
|
|
|
2003-01-13 08:35:03 +08:00
|
|
|
if (CP.empty()) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
|
|
|
|
O << "\t.section .rodata\n";
|
2003-07-24 02:37:06 +08:00
|
|
|
O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
|
2003-06-27 02:02:30 +08:00
|
|
|
<< "\n";
|
2003-06-27 08:00:48 +08:00
|
|
|
O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
|
|
|
|
<< *CP[i] << "\n";
|
2003-11-04 04:19:49 +08:00
|
|
|
emitGlobalConstant(CP[i]);
|
2003-01-13 08:35:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-24 02:37:06 +08:00
|
|
|
/// runOnMachineFunction - This uses the printMachineInstruction()
|
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
2004-08-01 14:02:08 +08:00
|
|
|
bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2004-08-17 07:16:06 +08:00
|
|
|
setupMachineFunction(MF);
|
2003-08-04 07:37:09 +08:00
|
|
|
O << "\n\n";
|
2003-06-27 08:00:48 +08:00
|
|
|
|
2003-01-13 08:35:03 +08:00
|
|
|
// Print out constants referenced by the function
|
2003-06-26 02:01:07 +08:00
|
|
|
printConstantPool(MF.getConstantPool());
|
2003-01-13 08:35:03 +08:00
|
|
|
|
2002-11-15 06:32:30 +08:00
|
|
|
// Print out labels for the function.
|
2003-01-13 08:35:03 +08:00
|
|
|
O << "\t.text\n";
|
|
|
|
O << "\t.align 16\n";
|
2003-06-27 08:00:48 +08:00
|
|
|
O << "\t.globl\t" << CurrentFnName << "\n";
|
2004-08-17 07:16:06 +08:00
|
|
|
O << "\t.type\t" << CurrentFnName << ", @function\n";
|
2003-06-27 08:00:48 +08:00
|
|
|
O << CurrentFnName << ":\n";
|
2002-10-30 06:37:54 +08:00
|
|
|
|
2002-11-15 06:32:30 +08:00
|
|
|
// Print out code for the function.
|
2002-12-29 04:25:38 +08:00
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// Print a label for the basic block.
|
2004-05-14 14:54:57 +08:00
|
|
|
O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
|
2003-06-26 02:01:07 +08:00
|
|
|
<< I->getBasicBlock()->getName() << "\n";
|
2002-12-29 04:25:38 +08:00
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
2004-06-30 03:28:53 +08:00
|
|
|
II != E; ++II) {
|
2002-12-29 04:25:38 +08:00
|
|
|
// Print the assembly for the instruction.
|
|
|
|
O << "\t";
|
2004-02-12 10:27:10 +08:00
|
|
|
printMachineInstruction(II);
|
2002-11-15 06:32:30 +08:00
|
|
|
}
|
2002-12-29 04:25:38 +08:00
|
|
|
}
|
2002-10-26 06:55:53 +08:00
|
|
|
|
2002-11-15 06:32:30 +08:00
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-30 06:37:54 +08:00
|
|
|
|
2002-11-22 04:44:15 +08:00
|
|
|
static bool isScale(const MachineOperand &MO) {
|
2002-12-15 16:01:39 +08:00
|
|
|
return MO.isImmediate() &&
|
2003-06-26 02:01:07 +08:00
|
|
|
(MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
|
|
|
|
MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
|
2002-11-22 04:44:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMem(const MachineInstr *MI, unsigned Op) {
|
2003-01-13 08:35:03 +08:00
|
|
|
if (MI->getOperand(Op).isFrameIndex()) return true;
|
|
|
|
if (MI->getOperand(Op).isConstantPoolIndex()) return true;
|
2002-11-22 04:44:15 +08:00
|
|
|
return Op+4 <= MI->getNumOperands() &&
|
2004-08-11 15:02:04 +08:00
|
|
|
MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
|
|
|
|
MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
|
2002-11-22 04:44:15 +08:00
|
|
|
}
|
|
|
|
|
2003-08-12 03:05:46 +08:00
|
|
|
|
|
|
|
|
2004-08-01 14:02:08 +08:00
|
|
|
void X86AsmPrinter::printOp(const MachineOperand &MO,
|
|
|
|
bool elideOffsetKeyword /* = false */) {
|
2003-07-24 04:25:08 +08:00
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
2002-11-18 14:56:51 +08:00
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
2002-12-05 01:32:52 +08:00
|
|
|
if (Value *V = MO.getVRegValueOrNull()) {
|
2002-12-04 14:45:19 +08:00
|
|
|
O << "<" << V->getName() << ">";
|
|
|
|
return;
|
|
|
|
}
|
2003-01-13 08:35:03 +08:00
|
|
|
// FALLTHROUGH
|
2002-11-21 02:56:41 +08:00
|
|
|
case MachineOperand::MO_MachineRegister:
|
2004-02-16 05:37:17 +08:00
|
|
|
if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
|
2003-08-12 03:05:46 +08:00
|
|
|
// Bug Workaround: See note in Printer::doInitialization about %.
|
2003-08-14 02:15:15 +08:00
|
|
|
O << "%" << RI.get(MO.getReg()).Name;
|
|
|
|
else
|
2002-11-18 14:56:51 +08:00
|
|
|
O << "%reg" << MO.getReg();
|
|
|
|
return;
|
2002-11-21 10:00:20 +08:00
|
|
|
|
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
|
|
|
O << (int)MO.getImmedValue();
|
|
|
|
return;
|
2004-05-14 14:54:57 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
|
|
|
|
O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
|
|
|
|
<< "_" << MBBOp->getNumber () << "\t# "
|
|
|
|
<< MBBOp->getBasicBlock ()->getName ();
|
2002-12-02 07:25:59 +08:00
|
|
|
return;
|
2003-09-10 00:23:36 +08:00
|
|
|
}
|
2004-05-14 14:54:57 +08:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
|
|
|
|
abort ();
|
|
|
|
return;
|
2003-01-13 08:35:03 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2003-08-01 01:38:52 +08:00
|
|
|
if (!elideOffsetKeyword)
|
|
|
|
O << "OFFSET ";
|
|
|
|
O << Mang->getValueName(MO.getGlobal());
|
2003-01-13 08:35:03 +08:00
|
|
|
return;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2003-06-20 03:32:32 +08:00
|
|
|
O << MO.getSymbolName();
|
2003-01-13 08:35:03 +08:00
|
|
|
return;
|
2002-11-18 14:56:51 +08:00
|
|
|
default:
|
2003-06-26 02:01:07 +08:00
|
|
|
O << "<unknown operand type>"; return;
|
2002-11-18 14:56:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-01 14:02:08 +08:00
|
|
|
void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
|
2002-11-22 04:44:15 +08:00
|
|
|
assert(isMem(MI, Op) && "Invalid memory reference!");
|
2003-01-13 08:35:03 +08:00
|
|
|
|
|
|
|
if (MI->getOperand(Op).isFrameIndex()) {
|
|
|
|
O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
|
|
|
|
if (MI->getOperand(Op+3).getImmedValue())
|
|
|
|
O << " + " << MI->getOperand(Op+3).getImmedValue();
|
|
|
|
O << "]";
|
|
|
|
return;
|
|
|
|
} else if (MI->getOperand(Op).isConstantPoolIndex()) {
|
2003-06-27 08:00:48 +08:00
|
|
|
O << "[.CPI" << CurrentFnName << "_"
|
2003-06-27 02:02:30 +08:00
|
|
|
<< MI->getOperand(Op).getConstantPoolIndex();
|
2003-01-13 08:35:03 +08:00
|
|
|
if (MI->getOperand(Op+3).getImmedValue())
|
|
|
|
O << " + " << MI->getOperand(Op+3).getImmedValue();
|
|
|
|
O << "]";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-11-22 04:44:15 +08:00
|
|
|
const MachineOperand &BaseReg = MI->getOperand(Op);
|
2002-12-29 04:25:38 +08:00
|
|
|
int ScaleVal = MI->getOperand(Op+1).getImmedValue();
|
2002-11-22 04:44:15 +08:00
|
|
|
const MachineOperand &IndexReg = MI->getOperand(Op+2);
|
2002-12-29 04:25:38 +08:00
|
|
|
int DispVal = MI->getOperand(Op+3).getImmedValue();
|
2002-11-22 04:44:15 +08:00
|
|
|
|
|
|
|
O << "[";
|
|
|
|
bool NeedPlus = false;
|
|
|
|
if (BaseReg.getReg()) {
|
2003-07-24 04:25:08 +08:00
|
|
|
printOp(BaseReg);
|
2002-11-22 04:44:15 +08:00
|
|
|
NeedPlus = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IndexReg.getReg()) {
|
|
|
|
if (NeedPlus) O << " + ";
|
2002-12-29 04:25:38 +08:00
|
|
|
if (ScaleVal != 1)
|
|
|
|
O << ScaleVal << "*";
|
2003-07-24 04:25:08 +08:00
|
|
|
printOp(IndexReg);
|
2002-11-22 04:44:15 +08:00
|
|
|
NeedPlus = true;
|
|
|
|
}
|
|
|
|
|
2002-12-29 04:25:38 +08:00
|
|
|
if (DispVal) {
|
|
|
|
if (NeedPlus)
|
|
|
|
if (DispVal > 0)
|
2004-06-30 03:28:53 +08:00
|
|
|
O << " + ";
|
2002-12-29 04:25:38 +08:00
|
|
|
else {
|
2004-06-30 03:28:53 +08:00
|
|
|
O << " - ";
|
|
|
|
DispVal = -DispVal;
|
2002-12-29 04:25:38 +08:00
|
|
|
}
|
|
|
|
O << DispVal;
|
2002-11-22 04:44:15 +08:00
|
|
|
}
|
|
|
|
O << "]";
|
|
|
|
}
|
|
|
|
|
2004-04-09 04:31:47 +08:00
|
|
|
|
2003-07-24 02:37:06 +08:00
|
|
|
/// printMachineInstruction -- Print out a single X86 LLVM instruction
|
2003-07-24 04:25:08 +08:00
|
|
|
/// MI in Intel syntax to the current output stream.
|
2003-06-27 08:00:48 +08:00
|
|
|
///
|
2004-08-01 14:02:08 +08:00
|
|
|
void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
|
|
|
|
++EmittedInsts;
|
2004-08-11 14:09:55 +08:00
|
|
|
|
|
|
|
// gas bugs:
|
|
|
|
//
|
|
|
|
// The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
|
|
|
|
// by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
|
|
|
|
// [...]". Workaround: Output the raw opcode bytes instead of the instruction.
|
|
|
|
//
|
|
|
|
// The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
|
|
|
|
// in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
|
|
|
|
// [...]". Workaround: Output the raw opcode bytes instead of the instruction.
|
|
|
|
//
|
|
|
|
// gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
|
|
|
|
// saying "64 bit operations are only supported in 64 bit modes." libopcodes
|
|
|
|
// disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
|
|
|
|
// Output the raw opcode bytes instead of the instruction.
|
|
|
|
//
|
|
|
|
// gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
|
|
|
|
// saying "64 bit operations are only supported in 64 bit modes." libopcodes
|
|
|
|
// disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
|
|
|
|
// Output the raw opcode bytes instead of the instruction.
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case X86::FSTP80m:
|
|
|
|
case X86::FLD80m:
|
|
|
|
case X86::FILD64m:
|
|
|
|
case X86::FISTP64m:
|
|
|
|
GasBugWorkaroundEmitter gwe(O);
|
|
|
|
X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
|
|
|
|
O << "\t# ";
|
|
|
|
}
|
|
|
|
|
2004-08-11 15:02:04 +08:00
|
|
|
// Call the autogenerated instruction printer routines.
|
|
|
|
bool Handled = printInstruction(MI);
|
|
|
|
if (!Handled) {
|
|
|
|
MI->dump();
|
|
|
|
assert(0 && "Do not know how to print this instruction!");
|
|
|
|
abort();
|
2002-11-18 14:56:51 +08:00
|
|
|
}
|
2002-10-26 06:55:53 +08:00
|
|
|
}
|
2003-06-20 03:32:32 +08:00
|
|
|
|
2004-08-01 14:02:08 +08:00
|
|
|
bool X86AsmPrinter::doInitialization(Module &M) {
|
2004-08-17 07:16:06 +08:00
|
|
|
AsmPrinter::doInitialization(M);
|
2003-08-12 03:35:26 +08: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-12 04:04:57 +08:00
|
|
|
O << "\t.intel_syntax\n";
|
2004-08-17 07:16:06 +08:00
|
|
|
return false;
|
2003-06-20 03:32:32 +08:00
|
|
|
}
|
|
|
|
|
2003-09-10 00:23:36 +08: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";
|
|
|
|
}
|
2003-07-12 05:57:01 +08:00
|
|
|
}
|
|
|
|
|
2004-08-01 14:02:08 +08:00
|
|
|
bool X86AsmPrinter::doFinalization(Module &M) {
|
2003-07-24 04:25:08 +08:00
|
|
|
const TargetData &TD = TM.getTargetData();
|
2003-09-10 00:23:36 +08:00
|
|
|
std::string CurSection;
|
|
|
|
|
2003-06-26 02:01:07 +08:00
|
|
|
// Print out module-level global variables here.
|
2003-09-10 00:23:36 +08:00
|
|
|
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);
|
2003-06-26 02:01:07 +08:00
|
|
|
Constant *C = I->getInitializer();
|
2003-09-10 00:23:36 +08:00
|
|
|
unsigned Size = TD.getTypeSize(C->getType());
|
|
|
|
unsigned Align = TD.getTypeAlignment(C->getType());
|
|
|
|
|
|
|
|
if (C->isNullValue() &&
|
2003-10-17 02:29:00 +08:00
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
|
2003-09-10 00:23:36 +08:00
|
|
|
SwitchSection(O, CurSection, ".data");
|
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
|
|
|
|
|
|
|
O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
|
2003-08-04 07:37:09 +08:00
|
|
|
<< "," << (unsigned)TD.getTypeAlignment(C->getType());
|
|
|
|
O << "\t\t# ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << "\n";
|
2003-07-12 05:57:01 +08:00
|
|
|
} else {
|
2003-09-10 00:23:36 +08:00
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
2003-10-17 02:29:00 +08:00
|
|
|
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
|
2003-09-10 00:23:36 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
O << "\t.align " << Align << "\n";
|
2003-08-04 07:37:09 +08:00
|
|
|
O << "\t.type " << name << ",@object\n";
|
2003-09-10 00:23:36 +08:00
|
|
|
O << "\t.size " << name << "," << Size << "\n";
|
2003-08-04 07:37:09 +08:00
|
|
|
O << name << ":\t\t\t\t# ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << " = ";
|
|
|
|
WriteAsOperand(O, C, false, false, &M);
|
|
|
|
O << "\n";
|
2003-11-04 04:19:49 +08:00
|
|
|
emitGlobalConstant(C);
|
2003-07-12 05:57:01 +08:00
|
|
|
}
|
2003-06-26 02:01:07 +08:00
|
|
|
}
|
2003-09-10 00:23:36 +08:00
|
|
|
|
2004-08-17 07:16:06 +08:00
|
|
|
AsmPrinter::doFinalization(M);
|
2003-06-20 03:32:32 +08:00
|
|
|
return false; // success
|
|
|
|
}
|