1 //===-- X86/MachineCodeEmitter.cpp - Convert X86 code to machine code -----===//
3 // This file contains the pass that transforms the X86 machine instructions into
4 // actual executable machine code.
6 //===----------------------------------------------------------------------===//
8 #include "X86TargetMachine.h"
9 #include "llvm/PassManager.h"
10 #include "llvm/CodeGen/MachineCodeEmitter.h"
11 #include "llvm/CodeGen/MachineFunction.h"
12 #include "llvm/CodeGen/MachineInstr.h"
15 struct Emitter : public FunctionPass {
17 const X86InstrInfo ⅈ
18 MachineCodeEmitter &MCE;
20 Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce)
21 : TM(tm), II(TM.getInstrInfo()), MCE(mce) {}
23 bool runOnFunction(Function &F);
25 void emitBasicBlock(MachineBasicBlock &MBB);
26 void emitInstruction(MachineInstr &MI);
31 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
32 /// machine code emitted. This uses a MAchineCodeEmitter object to handle
33 /// actually outputting the machine code and resolving things like the address
34 /// of functions. This method should returns true if machine code emission is
37 bool X86TargetMachine::addPassesToEmitMachineCode(PassManager &PM,
38 MachineCodeEmitter &MCE) {
39 PM.add(new Emitter(*this, MCE));
43 bool Emitter::runOnFunction(Function &F) {
44 MachineFunction &MF = MachineFunction::get(&F);
46 MCE.startFunction(MF);
47 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
49 MCE.finishFunction(MF);
53 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
54 MCE.startBasicBlock(MBB);
55 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
59 void Emitter::emitInstruction(MachineInstr &MI) {
60 unsigned Opcode = MI.getOpcode();
61 const MachineInstrDescriptor &Desc = II.get(Opcode);
63 // Emit instruction prefixes if neccesary
64 if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);// Operand size...
65 if (Desc.TSFlags & X86II::TB) MCE.emitByte(0x0F);// Two-byte opcode prefix
67 switch (Desc.TSFlags & X86II::FormMask) {
69 MCE.emitByte(II.getBaseOpcodeFor(Opcode));
71 if (MI.getNumOperands() == 1) {
72 assert(MI.getOperand(0).getType() == MachineOperand::MO_PCRelativeDisp);
73 MCE.emitPCRelativeDisp(MI.getOperand(0).getVRegValue());