1 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the pass that transforms the ARM machine instructions into
11 // relocatable machine code.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "arm-emitter"
16 #include "ARMInstrInfo.h"
17 #include "ARMSubtarget.h"
18 #include "ARMTargetMachine.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"
30 STATISTIC(NumEmitted, "Number of machine instructions emitted");
33 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
34 const ARMInstrInfo *II;
37 MachineCodeEmitter &MCE;
40 explicit Emitter(TargetMachine &tm, MachineCodeEmitter &mce)
41 : MachineFunctionPass((intptr_t)&ID), II(0), TD(0), TM(tm),
43 Emitter(TargetMachine &tm, MachineCodeEmitter &mce,
44 const ARMInstrInfo &ii, const TargetData &td)
45 : MachineFunctionPass((intptr_t)&ID), II(&ii), TD(&td), TM(tm),
48 bool runOnMachineFunction(MachineFunction &MF);
50 virtual const char *getPassName() const {
51 return "ARM Machine Code Emitter";
54 void emitInstruction(const MachineInstr &MI);
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);
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();
77 MCE.startFunction(MF);
78 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
80 MCE.StartMachineBasicBlock(MBB);
81 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
85 } while (MCE.finishFunction(MF));
90 void Emitter::emitInstruction(const MachineInstr &MI) {
91 NumEmitted++; // Keep track of the # of mi's emitted