//===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
//
// This file contains the pass that transforms the X86 machine instructions into
// actual executable machine code.
//
//===----------------------------------------------------------------------===//
+#define DEBUG_TYPE "jit"
#include "X86TargetMachine.h"
#include "X86.h"
#include "llvm/PassManager.h"
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
-#include "llvm/Value.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/Function.h"
#include "Support/Debug.h"
#include "Support/Statistic.h"
#include "Config/alloca.h"
+using namespace llvm;
namespace {
Statistic<>
unsigned resolveFunctionReference(unsigned RetAddr);
};
- JITResolver *TheJITResolver;
+ static JITResolver &getResolver(MachineCodeEmitter &MCE) {
+ static JITResolver *TheJITResolver = 0;
+ if (TheJITResolver == 0)
+ TheJITResolver = new JITResolver(MCE);
+ return *TheJITResolver;
+ }
+}
+
+
+void *X86JITInfo::getJITStubForFunction(Function *F, MachineCodeEmitter &MCE) {
+ return (void*)((unsigned long)getResolver(MCE).getLazyResolver(F));
}
+void X86JITInfo::replaceMachineCodeForFunction (void *Old, void *New) {
+ char *OldByte = (char *) Old;
+ *OldByte++ = 0xE9; // Emit JMP opcode.
+ int32_t *OldWord = (int32_t *) OldByte;
+ int32_t NewAddr = (intptr_t) New;
+ int32_t OldAddr = (intptr_t) OldWord;
+ *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
+}
/// addFunctionReference - This method is called when we need to emit the
/// address of a function that has not yet been emitted, so we don't know the
// Sanity check to make sure this really is a call instruction...
assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
- unsigned NewVal = TheJITResolver->resolveFunctionReference(RetAddr);
+ JITResolver &JR = getResolver(*(MachineCodeEmitter*)0);
+ unsigned NewVal = JR.resolveFunctionReference(RetAddr);
// Rewrite the call target... so that we don't fault every time we execute
// the call.
}
/// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
-/// machine code emitted. This uses a MAchineCodeEmitter object to handle
+/// machine code emitted. This uses a MachineCodeEmitter object to handle
/// actually outputting the machine code and resolving things like the address
/// of functions. This method should returns true if machine code emission is
/// not supported.
///
-bool X86TargetMachine::addPassesToEmitMachineCode(PassManager &PM,
+bool X86TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
MachineCodeEmitter &MCE) {
PM.add(new Emitter(MCE));
+ // Delete machine code for this function
+ PM.add(createMachineCodeDeleter());
return false;
}
BasicBlockAddrs[MBB.getBasicBlock()] = Addr;
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
- emitInstruction(**I);
+ emitInstruction(*I);
}
// Get the address from the backend...
unsigned Address = MCE.getGlobalValueAddress(GV);
- // If the machine code emitter doesn't know what the address IS yet, we have
- // to take special measures.
- //
if (Address == 0) {
// FIXME: this is JIT specific!
- if (TheJITResolver == 0)
- TheJITResolver = new JITResolver(MCE);
- Address = TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
- (Function*)GV);
+ Address = getResolver(MCE).addFunctionReference(MCE.getCurrentPCValue(),
+ cast<Function>(GV));
}
emitMaybePCRelativeValue(Address, true);
}
//
if (Address == 0) {
// FIXME: this is JIT specific!
- if (TheJITResolver == 0)
- TheJITResolver = new JITResolver(MCE);
- Address = TheJITResolver->getLazyResolver((Function*)GV);
+ Address = getResolver(MCE).getLazyResolver((Function*)GV);
}
emitMaybePCRelativeValue(Address, false);
case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
return RegNo-X86::ST0;
default:
- assert(RegNo >= MRegisterInfo::FirstVirtualRegister &&
+ assert(MRegisterInfo::isVirtualRegister(RegNo) &&
"Unknown physical register!");
assert(0 && "Register allocator hasn't allocated reg correctly yet!");
return 0;
unsigned Opcode = MI.getOpcode();
const TargetInstrDescriptor &Desc = II->get(Opcode);
- // Emit instruction prefixes if neccesary
+ // Emit the repeat opcode prefix as needed.
+ if ((Desc.TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
+
+ // Emit instruction prefixes if necessary
if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);// Operand size...
switch (Desc.TSFlags & X86II::Op0Mask) {
case X86II::TB:
MCE.emitByte(0x0F); // Two-byte opcode prefix
break;
+ case X86II::REP: break; // already handled.
case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
MCE.emitByte(0xD8+
switch (Desc.TSFlags & X86II::FormMask) {
default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
case X86II::Pseudo:
- if (Opcode != X86::IMPLICIT_USE)
+ if (Opcode != X86::IMPLICIT_USE &&
+ Opcode != X86::IMPLICIT_DEF &&
+ Opcode != X86::FP_REG_KILL)
std::cerr << "X86 Machine Code Emitter: No 'form', not emitting: " << MI;
break;
case X86II::MRMDestReg: {
MCE.emitByte(BaseOpcode);
- MachineOperand &SrcOp = MI.getOperand(1+II->isTwoAddrInstr(Opcode));
- emitRegModRMByte(MI.getOperand(0).getReg(), getX86RegNum(SrcOp.getReg()));
- if (MI.getNumOperands() == 4)
- emitConstant(MI.getOperand(3).getImmedValue(), sizeOfPtr(Desc));
+ emitRegModRMByte(MI.getOperand(0).getReg(),
+ getX86RegNum(MI.getOperand(1).getReg()));
+ if (MI.getNumOperands() == 3)
+ emitConstant(MI.getOperand(2).getImmedValue(), sizeOfPtr(Desc));
break;
}
case X86II::MRMDestMem:
case X86II::MRMSrcReg:
MCE.emitByte(BaseOpcode);
- emitRegModRMByte(MI.getOperand(MI.getNumOperands()-1).getReg(),
+
+ emitRegModRMByte(MI.getOperand(1).getReg(),
getX86RegNum(MI.getOperand(0).getReg()));
+ if (MI.getNumOperands() == 3)
+ emitConstant(MI.getOperand(2).getImmedValue(), sizeOfPtr(Desc));
break;
case X86II::MRMSrcMem: