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 //===---------------------------------------------------------------------===//
16 #include "MCTargetDesc/SparcMCExpr.h"
17 #include "SparcRelocations.h"
18 #include "SparcTargetMachine.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/Support/Debug.h"
27 #define DEBUG_TYPE "jit"
29 STATISTIC(NumEmitted, "Number of machine instructions emitted");
33 class SparcCodeEmitter : public MachineFunctionPass {
35 const SparcInstrInfo *II;
37 const SparcSubtarget *Subtarget;
40 const std::vector<MachineConstantPoolEntry> *MCPEs;
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 AU.addRequired<MachineModuleInfo> ();
45 MachineFunctionPass::getAnalysisUsage(AU);
51 SparcCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
52 : MachineFunctionPass(ID), JTI(nullptr), II(nullptr), TD(nullptr),
53 TM(tm), MCE(mce), MCPEs(nullptr),
54 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
56 bool runOnMachineFunction(MachineFunction &MF) override;
58 const char *getPassName() const override {
59 return "Sparc Machine Code Emitter";
62 /// getBinaryCodeForInstr - This function, generated by the
63 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
64 /// machine instructions.
65 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
67 void emitInstruction(MachineBasicBlock::instr_iterator MI,
68 MachineBasicBlock &MBB);
71 /// getMachineOpValue - Return binary encoding of operand. If the machine
72 /// operand requires relocation, record the relocation and return zero.
73 unsigned getMachineOpValue(const MachineInstr &MI,
74 const MachineOperand &MO) const;
76 unsigned getCallTargetOpValue(const MachineInstr &MI,
78 unsigned getBranchTargetOpValue(const MachineInstr &MI,
80 unsigned getBranchPredTargetOpValue(const MachineInstr &MI,
82 unsigned getBranchOnRegTargetOpValue(const MachineInstr &MI,
85 void emitWord(unsigned Word);
87 unsigned getRelocation(const MachineInstr &MI,
88 const MachineOperand &MO) const;
90 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc) const;
91 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
92 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
93 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
95 } // end anonymous namespace.
97 char SparcCodeEmitter::ID = 0;
99 bool SparcCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
100 SparcTargetMachine &Target = static_cast<SparcTargetMachine &>(
101 const_cast<TargetMachine &>(MF.getTarget()));
103 JTI = Target.getSubtargetImpl()->getJITInfo();
104 II = Target.getSubtargetImpl()->getInstrInfo();
105 TD = Target.getSubtargetImpl()->getDataLayout();
106 Subtarget = &TM.getSubtarget<SparcSubtarget>();
107 MCPEs = &MF.getConstantPool()->getConstants();
108 JTI->Initialize(MF, IsPIC);
109 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
112 DEBUG(errs() << "JITTing function '"
113 << MF.getName() << "'\n");
114 MCE.startFunction(MF);
116 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
118 MCE.StartMachineBasicBlock(MBB);
119 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
120 E = MBB->instr_end(); I != E;)
121 emitInstruction(*I++, *MBB);
123 } while (MCE.finishFunction(MF));
128 void SparcCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
129 MachineBasicBlock &MBB) {
130 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
132 MCE.processDebugLoc(MI->getDebugLoc(), true);
136 switch (MI->getOpcode()) {
138 emitWord(getBinaryCodeForInstr(*MI));
141 case TargetOpcode::INLINEASM: {
142 // We allow inline assembler nodes with empty bodies - they can
143 // implicitly define registers, which is ok for JIT.
144 if (MI->getOperand(0).getSymbolName()[0]) {
145 report_fatal_error("JIT does not support inline asm!");
149 case TargetOpcode::CFI_INSTRUCTION:
151 case TargetOpcode::EH_LABEL: {
152 MCE.emitLabel(MI->getOperand(0).getMCSymbol());
155 case TargetOpcode::IMPLICIT_DEF:
156 case TargetOpcode::KILL: {
161 report_fatal_error("JIT does not support pseudo instruction GETPCX yet!");
166 MCE.processDebugLoc(MI->getDebugLoc(), false);
169 void SparcCodeEmitter::emitWord(unsigned Word) {
170 DEBUG(errs() << " 0x";
171 errs().write_hex(Word) << "\n");
172 MCE.emitWordBE(Word);
175 /// getMachineOpValue - Return binary encoding of operand. If the machine
176 /// operand requires relocation, record the relocation and return zero.
177 unsigned SparcCodeEmitter::getMachineOpValue(const MachineInstr &MI,
178 const MachineOperand &MO) const {
180 return TM.getSubtargetImpl()->getRegisterInfo()->getEncodingValue(
183 return static_cast<unsigned>(MO.getImm());
184 else if (MO.isGlobal())
185 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO));
186 else if (MO.isSymbol())
187 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
189 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
191 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
193 llvm_unreachable("Unable to encode MachineOperand!");
196 unsigned SparcCodeEmitter::getCallTargetOpValue(const MachineInstr &MI,
197 unsigned opIdx) const {
198 const MachineOperand MO = MI.getOperand(opIdx);
199 return getMachineOpValue(MI, MO);
202 unsigned SparcCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
203 unsigned opIdx) const {
204 const MachineOperand MO = MI.getOperand(opIdx);
205 return getMachineOpValue(MI, MO);
208 unsigned SparcCodeEmitter::getBranchPredTargetOpValue(const MachineInstr &MI,
209 unsigned opIdx) const {
210 const MachineOperand MO = MI.getOperand(opIdx);
211 return getMachineOpValue(MI, MO);
214 unsigned SparcCodeEmitter::getBranchOnRegTargetOpValue(const MachineInstr &MI,
215 unsigned opIdx) const {
216 const MachineOperand MO = MI.getOperand(opIdx);
217 return getMachineOpValue(MI, MO);
220 unsigned SparcCodeEmitter::getRelocation(const MachineInstr &MI,
221 const MachineOperand &MO) const {
223 unsigned TF = MO.getTargetFlags();
226 case SparcMCExpr::VK_Sparc_None: break;
227 case SparcMCExpr::VK_Sparc_LO: return SP::reloc_sparc_lo;
228 case SparcMCExpr::VK_Sparc_HI: return SP::reloc_sparc_hi;
229 case SparcMCExpr::VK_Sparc_H44: return SP::reloc_sparc_h44;
230 case SparcMCExpr::VK_Sparc_M44: return SP::reloc_sparc_m44;
231 case SparcMCExpr::VK_Sparc_L44: return SP::reloc_sparc_l44;
232 case SparcMCExpr::VK_Sparc_HH: return SP::reloc_sparc_hh;
233 case SparcMCExpr::VK_Sparc_HM: return SP::reloc_sparc_hm;
236 unsigned Opc = MI.getOpcode();
239 case SP::CALL: return SP::reloc_sparc_pc30;
242 case SP::FBCOND: return SP::reloc_sparc_pc22;
243 case SP::BPXCC: return SP::reloc_sparc_pc19;
245 llvm_unreachable("unknown reloc!");
248 void SparcCodeEmitter::emitGlobalAddress(const GlobalValue *GV,
249 unsigned Reloc) const {
250 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
251 const_cast<GlobalValue *>(GV), 0,
255 void SparcCodeEmitter::
256 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
257 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
261 void SparcCodeEmitter::
262 emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
263 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
264 Reloc, CPI, 0, false));
267 void SparcCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
268 unsigned Reloc) const {
269 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
274 /// createSparcJITCodeEmitterPass - Return a pass that emits the collected Sparc
275 /// code to the specified MCE object.
276 FunctionPass *llvm::createSparcJITCodeEmitterPass(SparcTargetMachine &TM,
277 JITCodeEmitter &JCE) {
278 return new SparcCodeEmitter(TM, JCE);
281 #include "SparcGenCodeEmitter.inc"