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 unsigned getCallTargetOpValue(const MachineInstr &MI,
77 unsigned getBranchTargetOpValue(const MachineInstr &MI,
80 void emitWord(unsigned Word);
82 unsigned getRelocation(const MachineInstr &MI,
83 const MachineOperand &MO) const;
85 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc) const;
86 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
87 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
88 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
90 } // end anonymous namespace.
92 char SparcCodeEmitter::ID = 0;
94 bool SparcCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
95 SparcTargetMachine &Target = static_cast<SparcTargetMachine &>(
96 const_cast<TargetMachine &>(MF.getTarget()));
98 JTI = Target.getJITInfo();
99 II = Target.getInstrInfo();
100 TD = Target.getDataLayout();
101 Subtarget = &TM.getSubtarget<SparcSubtarget> ();
102 MCPEs = &MF.getConstantPool()->getConstants();
103 JTI->Initialize(MF, IsPIC);
104 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
107 DEBUG(errs() << "JITTing function '"
108 << MF.getName() << "'\n");
109 MCE.startFunction(MF);
111 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
113 MCE.StartMachineBasicBlock(MBB);
114 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
115 E = MBB->instr_end(); I != E;)
116 emitInstruction(*I++, *MBB);
118 } while (MCE.finishFunction(MF));
123 void SparcCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
124 MachineBasicBlock &MBB) {
125 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
127 MCE.processDebugLoc(MI->getDebugLoc(), true);
131 switch (MI->getOpcode()) {
133 emitWord(getBinaryCodeForInstr(*MI));
136 case TargetOpcode::INLINEASM: {
137 // We allow inline assembler nodes with empty bodies - they can
138 // implicitly define registers, which is ok for JIT.
139 if (MI->getOperand(0).getSymbolName()[0]) {
140 report_fatal_error("JIT does not support inline asm!");
144 case TargetOpcode::PROLOG_LABEL:
145 case TargetOpcode::EH_LABEL: {
146 MCE.emitLabel(MI->getOperand(0).getMCSymbol());
149 case TargetOpcode::IMPLICIT_DEF:
150 case TargetOpcode::KILL: {
155 report_fatal_error("JIT does not support pseudo instruction GETPCX yet!");
160 MCE.processDebugLoc(MI->getDebugLoc(), false);
163 void SparcCodeEmitter::emitWord(unsigned Word) {
164 DEBUG(errs() << " 0x";
165 errs().write_hex(Word) << "\n");
166 MCE.emitWordBE(Word);
169 /// getMachineOpValue - Return binary encoding of operand. If the machine
170 /// operand requires relocation, record the relocation and return zero.
171 unsigned SparcCodeEmitter::getMachineOpValue(const MachineInstr &MI,
172 const MachineOperand &MO) const {
174 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
176 return static_cast<unsigned>(MO.getImm());
177 else if (MO.isGlobal())
178 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO));
179 else if (MO.isSymbol())
180 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
182 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
184 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
186 llvm_unreachable("Unable to encode MachineOperand!");
189 unsigned SparcCodeEmitter::getCallTargetOpValue(const MachineInstr &MI,
190 unsigned opIdx) const {
191 const MachineOperand MO = MI.getOperand(opIdx);
192 return getMachineOpValue(MI, MO);
195 unsigned SparcCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
196 unsigned opIdx) const {
197 const MachineOperand MO = MI.getOperand(opIdx);
198 return getMachineOpValue(MI, MO);
201 unsigned SparcCodeEmitter::getRelocation(const MachineInstr &MI,
202 const MachineOperand &MO) const {
204 unsigned TF = MO.getTargetFlags();
207 case SPII::MO_NO_FLAG: break;
208 case SPII::MO_LO: return SP::reloc_sparc_lo;
209 case SPII::MO_HI: return SP::reloc_sparc_hi;
214 case SPII::MO_HM: assert(0 && "FIXME: Implement Medium/Large code model.");
217 unsigned Opc = MI.getOpcode();
220 case SP::CALL: return SP::reloc_sparc_pc30;
223 case SP::FBCOND: return SP::reloc_sparc_pc22;
224 case SP::BPXCC: return SP::reloc_sparc_pc19;
226 llvm_unreachable("unknown reloc!");
229 void SparcCodeEmitter::emitGlobalAddress(const GlobalValue *GV,
230 unsigned Reloc) const {
231 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
232 const_cast<GlobalValue *>(GV), 0,
236 void SparcCodeEmitter::
237 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
238 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
242 void SparcCodeEmitter::
243 emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
244 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
245 Reloc, CPI, 0, false));
248 void SparcCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
249 unsigned Reloc) const {
250 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
255 /// createSparcJITCodeEmitterPass - Return a pass that emits the collected Sparc
256 /// code to the specified MCE object.
257 FunctionPass *llvm::createSparcJITCodeEmitterPass(SparcTargetMachine &TM,
258 JITCodeEmitter &JCE) {
259 return new SparcCodeEmitter(TM, JCE);
262 #include "SparcGenCodeEmitter.inc"