no email addrs in file headers
[oota-llvm.git] / lib / Target / ARM / ARMCodeEmitter.cpp
1 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the Raul Herbster and is distributed under the 
6 // University of Illinois Open Source License.  See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the pass that transforms the ARM machine instructions into
11 // relocatable machine code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "arm-emitter"
16 #include "ARMInstrInfo.h"
17 #include "ARMSubtarget.h"
18 #include "ARMTargetMachine.h"
19 #include "ARM.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/CodeGen/MachineCodeEmitter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/Function.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Support/Compiler.h"
28 using namespace llvm;
29
30 STATISTIC(NumEmitted, "Number of machine instructions emitted");
31
32 namespace {
33   class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
34     const ARMInstrInfo  *II;
35     const TargetData    *TD;
36     TargetMachine       &TM;
37     MachineCodeEmitter  &MCE;
38   public:
39     static char ID;
40     explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
41       : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm), 
42       MCE(mce) {}
43     Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
44             const ARMInstrInfo &ii, const TargetData &td)
45       : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm), 
46       MCE(mce) {}
47
48     bool runOnMachineFunction(MachineFunction &MF);
49
50     virtual const char *getPassName() const {
51       return "ARM Machine Code Emitter";
52     }
53
54     void emitInstruction(const MachineInstr &MI);
55
56   private:
57
58   };
59   char Emitter::ID = 0;
60 }
61
62 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
63 /// to the specified MCE object.
64 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
65                                              MachineCodeEmitter &MCE) {
66   return new Emitter(TM, MCE);
67 }
68
69 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
70   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
71           MF.getTarget().getRelocationModel() != Reloc::Static) &&
72          "JIT relocation model must be set to static or default!");
73   II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
74   TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
75
76   do {
77     MCE.startFunction(MF);
78     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 
79          MBB != E; ++MBB) {
80       MCE.StartMachineBasicBlock(MBB);
81       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
82            I != E; ++I)
83         emitInstruction(*I);
84     }
85   } while (MCE.finishFunction(MF));
86
87   return false;
88 }
89
90 void Emitter::emitInstruction(const MachineInstr &MI) {
91   NumEmitted++;  // Keep track of the # of mi's emitted
92 }