add support for external symbols + X86::MOVPC32r.

llvm-svn: 79175
This commit is contained in:
Chris Lattner 2009-08-16 04:28:14 +00:00
parent d56fca4708
commit 8a4c35dd18
2 changed files with 123 additions and 59 deletions

View File

@ -52,12 +52,28 @@ static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
//===----------------------------------------------------------------------===//
void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
// FIXME: the actual label generated doesn't matter here! Just mangle in
// something unique (the function number) with Private prefix.
if (Subtarget->isTargetDarwin())
O << "\"L" << getFunctionNumber() << "$pb\"";
else if (Subtarget->isTargetELF())
else {
assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
else
llvm_unreachable("Don't know how to print PIC label!");
}
}
MCSymbol *X86ATTAsmPrinter::GetPICBaseSymbol() {
// FIXME: the actual label generated doesn't matter here! Just mangle in
// something unique (the function number) with Private prefix.
std::string Name;
if (Subtarget->isTargetDarwin()) {
Name = "L" + utostr(getFunctionNumber())+"$pb";
} else {
assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Name = ".Lllvm$" + utostr(getFunctionNumber())+".$piclabel";
}
return OutContext.GetOrCreateSymbol(Name);
}
static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
@ -690,7 +706,6 @@ static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
/// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
/// MCOperand.
MCOperand X86ATTAsmPrinter::LowerGlobalAddressOperand(const MachineOperand &MO){
//OutContext
const GlobalValue *GV = MO.getGlobal();
const char *Suffix = "";
@ -724,72 +739,121 @@ MCOperand X86ATTAsmPrinter::LowerGlobalAddressOperand(const MachineOperand &MO){
return MCOperand::CreateMCValue(MCValue::get(Sym, 0, MO.getOffset()));
}
MCOperand X86ATTAsmPrinter::
LowerExternalSymbolOperand(const MachineOperand &MO){
std::string Name = Mang->makeNameProper(MO.getSymbolName());
if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
FnStubs[Name+"$stub"] = Name;
Name += "$stub";
}
MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
return MCOperand::CreateMCValue(MCValue::get(Sym, 0, MO.getOffset()));
}
/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
/// AT&T syntax to the current output stream.
///
void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
++EmittedInsts;
if (NewAsmPrinter) {
if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
O << "\t";
printInlineAsm(MI);
return;
} else if (MI->isLabel()) {
printLabel(MI);
return;
} else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
printDeclare(MI);
return;
} else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
printImplicitDef(MI);
return;
}
if (!NewAsmPrinter) {
// Call the autogenerated instruction printer routines.
printInstruction(MI);
return;
}
MCInst TmpInst;
switch (MI->getOpcode()) {
case TargetInstrInfo::DBG_LABEL:
case TargetInstrInfo::EH_LABEL:
case TargetInstrInfo::GC_LABEL:
printLabel(MI);
return;
case TargetInstrInfo::INLINEASM:
O << '\t';
printInlineAsm(MI);
return;
case TargetInstrInfo::DECLARE:
printDeclare(MI);
return;
case TargetInstrInfo::IMPLICIT_DEF:
printImplicitDef(MI);
return;
case X86::MOVPC32r: {
// This is a pseudo op for a two instruction sequence with a label, which
// looks like:
// call "L1$pb"
// "L1$pb":
// popl %esi
O << "NEW: ";
MCInst TmpInst;
// Emit the call.
MCSymbol *PICBase = GetPICBaseSymbol();
TmpInst.setOpcode(X86::CALLpcrel32);
TmpInst.addOperand(MCOperand::CreateMCValue(MCValue::get(PICBase)));
printInstruction(&TmpInst);
// Emit the label.
OutStreamer.EmitLabel(PICBase);
TmpInst.setOpcode(MI->getOpcode());
// popl $reg
TmpInst.setOpcode(X86::POP32r);
TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
printInstruction(&TmpInst);
O << "OLD: ";
// Call the autogenerated instruction printer routines.
printInstruction(MI);
return;
}
}
O << "NEW: ";
TmpInst.setOpcode(MI->getOpcode());
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
MCOperand MCOp;
switch (MO.getType()) {
default:
O.flush();
errs() << "Cannot lower operand #" << i << " of :" << *MI;
llvm_unreachable("Unimp");
case MachineOperand::MO_Register:
MCOp = MCOperand::CreateReg(MO.getReg());
break;
case MachineOperand::MO_Immediate:
MCOp = MCOperand::CreateImm(MO.getImm());
break;
case MachineOperand::MO_MachineBasicBlock:
MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
MO.getMBB()->getNumber());
break;
case MachineOperand::MO_GlobalAddress:
MCOp = LowerGlobalAddressOperand(MO);
break;
}
TmpInst.addOperand(MCOp);
}
switch (TmpInst.getOpcode()) {
case X86::LEA64_32r:
// Handle the 'subreg rewriting' for the lea64_32mem operand.
lower_lea64_32mem(&TmpInst, 1);
MCOperand MCOp;
switch (MO.getType()) {
default:
O.flush();
errs() << "Cannot lower operand #" << i << " of :" << *MI;
llvm_unreachable("Unimp");
case MachineOperand::MO_Register:
MCOp = MCOperand::CreateReg(MO.getReg());
break;
case MachineOperand::MO_Immediate:
MCOp = MCOperand::CreateImm(MO.getImm());
break;
case MachineOperand::MO_MachineBasicBlock:
MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
MO.getMBB()->getNumber());
break;
case MachineOperand::MO_GlobalAddress:
MCOp = LowerGlobalAddressOperand(MO);
break;
case MachineOperand::MO_ExternalSymbol:
MCOp = LowerExternalSymbolOperand(MO);
break;
}
// FIXME: Convert TmpInst.
printInstruction(&TmpInst);
O << "OLD: ";
TmpInst.addOperand(MCOp);
}
switch (TmpInst.getOpcode()) {
case X86::LEA64_32r:
// Handle the 'subreg rewriting' for the lea64_32mem operand.
lower_lea64_32mem(&TmpInst, 1);
break;
}
// FIXME: Convert TmpInst.
printInstruction(&TmpInst);
O << "OLD: ";
// Call the autogenerated instruction printer routines.
printInstruction(MI);
}
@ -825,9 +889,6 @@ void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
const MCSection *TheSection =
getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
SwitchToSection(TheSection);

View File

@ -31,6 +31,7 @@ class MCContext;
class MCInst;
class MCOperand;
class MCStreamer;
class MCSymbol;
class VISIBILITY_HIDDEN X86ATTAsmPrinter : public AsmPrinter {
const X86Subtarget *Subtarget;
@ -67,7 +68,9 @@ class VISIBILITY_HIDDEN X86ATTAsmPrinter : public AsmPrinter {
// New MCInst printing stuff.
void printInstruction(const MCInst *MI);
MCSymbol *GetPICBaseSymbol();
MCOperand LowerGlobalAddressOperand(const MachineOperand &MO);
MCOperand LowerExternalSymbolOperand(const MachineOperand &MO);
virtual void printMCInst(const MCInst *MI) { printInstruction(MI); }