1 //===-- Sparc/SparcCodeEmitter.cpp - Convert Sparc Code to Machine Code ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // This file contains the pass that transforms the Sparc machine instructions
11 // into relocatable machine code.
13 //===---------------------------------------------------------------------===//
15 #define DEBUG_TYPE "jit"
17 #include "MCTargetDesc/SparcBaseInfo.h"
18 #include "SparcRelocations.h"
19 #include "SparcTargetMachine.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/JITCodeEmitter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/Support/Debug.h"
28 STATISTIC(NumEmitted, "Number of machine instructions emitted");
32 class SparcCodeEmitter : public MachineFunctionPass {
34 const SparcInstrInfo *II;
36 const SparcSubtarget *Subtarget;
39 const std::vector<MachineConstantPoolEntry> *MCPEs;
42 void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<MachineModuleInfo> ();
44 MachineFunctionPass::getAnalysisUsage(AU);
50 SparcCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
51 : MachineFunctionPass(ID), JTI(0), II(0), TD(0),
52 TM(tm), MCE(mce), MCPEs(0),
53 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
55 bool runOnMachineFunction(MachineFunction &MF);
57 virtual const char *getPassName() const {
58 return "Sparc Machine Code Emitter";
61 /// getBinaryCodeForInstr - This function, generated by the
62 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
63 /// machine instructions.
64 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
66 void emitInstruction(MachineBasicBlock::instr_iterator MI,
67 MachineBasicBlock &MBB);
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
72 unsigned getMachineOpValue(const MachineInstr &MI,
73 const MachineOperand &MO) const;
75 void emitWord(unsigned Word);
77 unsigned getRelocation(const MachineInstr &MI,
78 const MachineOperand &MO) const;
80 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc) const;
81 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
82 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
83 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
85 } // end anonymous namespace.
87 char SparcCodeEmitter::ID = 0;
89 bool SparcCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
90 SparcTargetMachine &Target = static_cast<SparcTargetMachine &>(
91 const_cast<TargetMachine &>(MF.getTarget()));
93 JTI = Target.getJITInfo();
94 II = Target.getInstrInfo();
95 TD = Target.getDataLayout();
96 Subtarget = &TM.getSubtarget<SparcSubtarget> ();
97 MCPEs = &MF.getConstantPool()->getConstants();
98 JTI->Initialize(MF, IsPIC);
99 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
102 DEBUG(errs() << "JITTing function '"
103 << MF.getName() << "'\n");
104 MCE.startFunction(MF);
106 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
108 MCE.StartMachineBasicBlock(MBB);
109 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
110 E = MBB->instr_end(); I != E;)
111 emitInstruction(*I++, *MBB);
113 } while (MCE.finishFunction(MF));
118 void SparcCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
119 MachineBasicBlock &MBB) {
120 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
122 MCE.processDebugLoc(MI->getDebugLoc(), true);
126 switch (MI->getOpcode()) {
128 emitWord(getBinaryCodeForInstr(*MI));
131 case TargetOpcode::INLINEASM: {
132 // We allow inline assembler nodes with empty bodies - they can
133 // implicitly define registers, which is ok for JIT.
134 if (MI->getOperand(0).getSymbolName()[0]) {
135 report_fatal_error("JIT does not support inline asm!");
139 case TargetOpcode::PROLOG_LABEL:
140 case TargetOpcode::EH_LABEL: {
141 MCE.emitLabel(MI->getOperand(0).getMCSymbol());
144 case TargetOpcode::IMPLICIT_DEF:
145 case TargetOpcode::KILL: {
150 report_fatal_error("JIT does not support pseudo instruction GETPCX yet!");
155 MCE.processDebugLoc(MI->getDebugLoc(), false);
158 void SparcCodeEmitter::emitWord(unsigned Word) {
159 DEBUG(errs() << " 0x";
160 errs().write_hex(Word) << "\n");
161 MCE.emitWordBE(Word);
164 /// getMachineOpValue - Return binary encoding of operand. If the machine
165 /// operand requires relocation, record the relocation and return zero.
166 unsigned SparcCodeEmitter::getMachineOpValue(const MachineInstr &MI,
167 const MachineOperand &MO) const {
169 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
171 return static_cast<unsigned>(MO.getImm());
172 else if (MO.isGlobal())
173 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO));
174 else if (MO.isSymbol())
175 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
177 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
179 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
181 llvm_unreachable("Unable to encode MachineOperand!");
184 unsigned SparcCodeEmitter::getRelocation(const MachineInstr &MI,
185 const MachineOperand &MO) const {
187 unsigned TF = MO.getTargetFlags();
190 case SPII::MO_NO_FLAG: break;
191 case SPII::MO_LO: return SP::reloc_sparc_lo;
192 case SPII::MO_HI: return SP::reloc_sparc_hi;
197 case SPII::MO_HM: assert(0 && "FIXME: Implement Medium/Large code model.");
200 unsigned Opc = MI.getOpcode();
203 case SP::CALL: return SP::reloc_sparc_pc30;
206 case SP::FBCOND: return SP::reloc_sparc_pc22;
207 case SP::BPXCC: return SP::reloc_sparc_pc19;
209 llvm_unreachable("unknown reloc!");
212 void SparcCodeEmitter::emitGlobalAddress(const GlobalValue *GV,
213 unsigned Reloc) const {
214 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
215 const_cast<GlobalValue *>(GV), 0,
219 void SparcCodeEmitter::
220 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
221 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
225 void SparcCodeEmitter::
226 emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
227 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
228 Reloc, CPI, 0, false));
231 void SparcCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
232 unsigned Reloc) const {
233 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
238 /// createSparcJITCodeEmitterPass - Return a pass that emits the collected Sparc
239 /// code to the specified MCE object.
240 FunctionPass *llvm::createSparcJITCodeEmitterPass(SparcTargetMachine &TM,
241 JITCodeEmitter &JCE) {
242 return new SparcCodeEmitter(TM, JCE);
245 #include "SparcGenCodeEmitter.inc"