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 MachineCodeEmitter &MCE;
19 Emitter(TargetMachine &tm, MachineCodeEmitter &mce) : TM(tm), MCE(mce) {}
21 bool runOnFunction(Function &F);
23 void emitBasicBlock(MachineBasicBlock &MBB);
24 void emitInstruction(MachineInstr &MI);
29 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
30 /// machine code emitted. This uses a MAchineCodeEmitter object to handle
31 /// actually outputting the machine code and resolving things like the address
32 /// of functions. This method should returns true if machine code emission is
35 bool X86TargetMachine::addPassesToEmitMachineCode(PassManager &PM,
36 MachineCodeEmitter &MCE) {
37 PM.add(new Emitter(*this, MCE));
41 bool Emitter::runOnFunction(Function &F) {
42 MachineFunction &MF = MachineFunction::get(&F);
44 MCE.startFunction(MF);
45 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
47 MCE.finishFunction(MF);
51 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
52 MCE.startBasicBlock(MBB);
53 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
57 void Emitter::emitInstruction(MachineInstr &MI) {
58 unsigned Opcode = MI.getOpcode();
59 const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
61 // Emit instruction prefixes if neccesary
62 if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);// Operand size...
63 if (Desc.TSFlags & X86II::TB) MCE.emitByte(0x0F); // Two-byte opcode prefix
65 switch (Desc.TSFlags & X86II::FormMask) {