Start implementing MachineCodeEmitter
[oota-llvm.git] / lib / Target / X86 / X86CodeEmitter.cpp
1 //===-- X86/MachineCodeEmitter.cpp - Convert X86 code to machine code -----===//
2 //
3 // This file contains the pass that transforms the X86 machine instructions into
4 // actual executable machine code.
5 //
6 //===----------------------------------------------------------------------===//
7
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"
13
14 namespace {
15   struct Emitter : public FunctionPass {
16     TargetMachine &TM;
17     MachineCodeEmitter &MCE;
18
19     Emitter(TargetMachine &tm, MachineCodeEmitter &mce) : TM(tm), MCE(mce) {}
20
21     bool runOnFunction(Function &F);
22
23     void emitBasicBlock(MachineBasicBlock &MBB);
24     void emitInstruction(MachineInstr &MI);
25   };
26 }
27
28
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
33 /// not supported.
34 ///
35 bool X86TargetMachine::addPassesToEmitMachineCode(PassManager &PM,
36                                                   MachineCodeEmitter &MCE) {
37   PM.add(new Emitter(*this, MCE));
38   return false;
39 }
40
41 bool Emitter::runOnFunction(Function &F) {
42   MachineFunction &MF = MachineFunction::get(&F);
43
44   MCE.startFunction(MF);
45   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
46     emitBasicBlock(*I);
47   MCE.finishFunction(MF);
48   return false;
49 }
50
51 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
52   MCE.startBasicBlock(MBB);
53   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
54     emitInstruction(**I);
55 }
56
57 void Emitter::emitInstruction(MachineInstr &MI) {
58   unsigned Opcode = MI.getOpcode();
59   const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
60
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
64
65   switch (Desc.TSFlags & X86II::FormMask) {
66   case X86II::RawFrm:
67     ;
68   }
69 }