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