1 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
3 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
11 // This file contains the pass that transforms the ARM machine instructions into
12 // relocatable machine code.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "arm-emitter"
17 #include "ARMInstrInfo.h"
18 #include "ARMSubtarget.h"
19 #include "ARMTargetMachine.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"
31 STATISTIC(NumEmitted, "Number of machine instructions emitted");
34 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
35 const ARMInstrInfo *II;
38 MachineCodeEmitter &MCE;
41 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
42 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
44 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
45 const ARMInstrInfo &ii, const TargetData &td)
46 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
49 bool runOnMachineFunction(MachineFunction &MF);
51 virtual const char *getPassName() const {
52 return "ARM Machine Code Emitter";
55 void emitInstruction(const MachineInstr &MI);
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);
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();
78 MCE.startFunction(MF);
79 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
81 MCE.StartMachineBasicBlock(MBB);
82 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
86 } while (MCE.finishFunction(MF));
91 void Emitter::emitInstruction(const MachineInstr &MI) {
92 NumEmitted++; // Keep track of the # of mi's emitted