blob: 97bacf2d31912832565decd242f8a46b9d68747e [file] [log] [blame]
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +00001//===-- Sparc/SparcCodeEmitter.cpp - Convert Sparc Code to Machine Code ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===---------------------------------------------------------------------===//
9//
10// This file contains the pass that transforms the Sparc machine instructions
11// into relocatable machine code.
12//
13//===---------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "jit"
16#include "Sparc.h"
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +000017#include "MCTargetDesc/SparcMCExpr.h"
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000018#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"
25
26using namespace llvm;
27
28STATISTIC(NumEmitted, "Number of machine instructions emitted");
29
30namespace {
31
32class SparcCodeEmitter : public MachineFunctionPass {
33 SparcJITInfo *JTI;
34 const SparcInstrInfo *II;
35 const DataLayout *TD;
36 const SparcSubtarget *Subtarget;
37 TargetMachine &TM;
38 JITCodeEmitter &MCE;
39 const std::vector<MachineConstantPoolEntry> *MCPEs;
40 bool IsPIC;
41
42 void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<MachineModuleInfo> ();
44 MachineFunctionPass::getAnalysisUsage(AU);
45 }
46
47 static char ID;
48
49public:
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_) {}
54
55 bool runOnMachineFunction(MachineFunction &MF);
56
57 virtual const char *getPassName() const {
58 return "Sparc Machine Code Emitter";
59 }
60
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;
65
66 void emitInstruction(MachineBasicBlock::instr_iterator MI,
67 MachineBasicBlock &MBB);
68
69private:
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;
74
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000075 unsigned getCallTargetOpValue(const MachineInstr &MI,
76 unsigned) const;
77 unsigned getBranchTargetOpValue(const MachineInstr &MI,
78 unsigned) const;
Venkatraman Govindarajuc86e0f32014-03-01 22:03:07 +000079 unsigned getBranchPredTargetOpValue(const MachineInstr &MI,
80 unsigned) const;
Venkatraman Govindarajub745e672014-03-02 09:46:56 +000081 unsigned getBranchOnRegTargetOpValue(const MachineInstr &MI,
82 unsigned) const;
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +000083
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000084 void emitWord(unsigned Word);
85
86 unsigned getRelocation(const MachineInstr &MI,
87 const MachineOperand &MO) const;
88
89 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc) const;
90 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
91 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
92 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
93};
94} // end anonymous namespace.
95
96char SparcCodeEmitter::ID = 0;
97
98bool SparcCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
99 SparcTargetMachine &Target = static_cast<SparcTargetMachine &>(
100 const_cast<TargetMachine &>(MF.getTarget()));
101
102 JTI = Target.getJITInfo();
103 II = Target.getInstrInfo();
104 TD = Target.getDataLayout();
105 Subtarget = &TM.getSubtarget<SparcSubtarget> ();
106 MCPEs = &MF.getConstantPool()->getConstants();
107 JTI->Initialize(MF, IsPIC);
108 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
109
110 do {
111 DEBUG(errs() << "JITTing function '"
112 << MF.getName() << "'\n");
113 MCE.startFunction(MF);
114
115 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
116 MBB != E; ++MBB){
117 MCE.StartMachineBasicBlock(MBB);
118 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
119 E = MBB->instr_end(); I != E;)
120 emitInstruction(*I++, *MBB);
121 }
122 } while (MCE.finishFunction(MF));
123
124 return false;
125}
126
127void SparcCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
128 MachineBasicBlock &MBB) {
129 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
130
131 MCE.processDebugLoc(MI->getDebugLoc(), true);
132
133 ++NumEmitted;
134
135 switch (MI->getOpcode()) {
136 default: {
137 emitWord(getBinaryCodeForInstr(*MI));
138 break;
139 }
140 case TargetOpcode::INLINEASM: {
141 // We allow inline assembler nodes with empty bodies - they can
142 // implicitly define registers, which is ok for JIT.
143 if (MI->getOperand(0).getSymbolName()[0]) {
144 report_fatal_error("JIT does not support inline asm!");
145 }
146 break;
147 }
148 case TargetOpcode::PROLOG_LABEL:
149 case TargetOpcode::EH_LABEL: {
150 MCE.emitLabel(MI->getOperand(0).getMCSymbol());
151 break;
152 }
153 case TargetOpcode::IMPLICIT_DEF:
154 case TargetOpcode::KILL: {
155 // Do nothing.
156 break;
157 }
158 case SP::GETPCX: {
159 report_fatal_error("JIT does not support pseudo instruction GETPCX yet!");
160 break;
161 }
162 }
163
164 MCE.processDebugLoc(MI->getDebugLoc(), false);
165}
166
167void SparcCodeEmitter::emitWord(unsigned Word) {
168 DEBUG(errs() << " 0x";
169 errs().write_hex(Word) << "\n");
170 MCE.emitWordBE(Word);
171}
172
173/// getMachineOpValue - Return binary encoding of operand. If the machine
174/// operand requires relocation, record the relocation and return zero.
175unsigned SparcCodeEmitter::getMachineOpValue(const MachineInstr &MI,
176 const MachineOperand &MO) const {
177 if (MO.isReg())
178 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
179 else if (MO.isImm())
180 return static_cast<unsigned>(MO.getImm());
181 else if (MO.isGlobal())
182 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO));
183 else if (MO.isSymbol())
184 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
185 else if (MO.isCPI())
186 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
187 else if (MO.isMBB())
188 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
189 else
190 llvm_unreachable("Unable to encode MachineOperand!");
191 return 0;
192}
Venkatraman Govindaraju5f1cce52014-01-05 02:13:48 +0000193unsigned SparcCodeEmitter::getCallTargetOpValue(const MachineInstr &MI,
194 unsigned opIdx) const {
195 const MachineOperand MO = MI.getOperand(opIdx);
196 return getMachineOpValue(MI, MO);
197}
198
199unsigned SparcCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
200 unsigned opIdx) const {
201 const MachineOperand MO = MI.getOperand(opIdx);
202 return getMachineOpValue(MI, MO);
203}
204
Venkatraman Govindarajuc86e0f32014-03-01 22:03:07 +0000205unsigned SparcCodeEmitter::getBranchPredTargetOpValue(const MachineInstr &MI,
206 unsigned opIdx) const {
207 const MachineOperand MO = MI.getOperand(opIdx);
208 return getMachineOpValue(MI, MO);
209}
210
Venkatraman Govindarajub745e672014-03-02 09:46:56 +0000211unsigned SparcCodeEmitter::getBranchOnRegTargetOpValue(const MachineInstr &MI,
212 unsigned opIdx) const {
213 const MachineOperand MO = MI.getOperand(opIdx);
214 return getMachineOpValue(MI, MO);
215}
216
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000217unsigned SparcCodeEmitter::getRelocation(const MachineInstr &MI,
218 const MachineOperand &MO) const {
219
220 unsigned TF = MO.getTargetFlags();
221 switch (TF) {
222 default:
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +0000223 case SparcMCExpr::VK_Sparc_None: break;
224 case SparcMCExpr::VK_Sparc_LO: return SP::reloc_sparc_lo;
225 case SparcMCExpr::VK_Sparc_HI: return SP::reloc_sparc_hi;
226 case SparcMCExpr::VK_Sparc_H44: return SP::reloc_sparc_h44;
227 case SparcMCExpr::VK_Sparc_M44: return SP::reloc_sparc_m44;
228 case SparcMCExpr::VK_Sparc_L44: return SP::reloc_sparc_l44;
229 case SparcMCExpr::VK_Sparc_HH: return SP::reloc_sparc_hh;
230 case SparcMCExpr::VK_Sparc_HM: return SP::reloc_sparc_hm;
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000231 }
232
233 unsigned Opc = MI.getOpcode();
234 switch (Opc) {
235 default: break;
236 case SP::CALL: return SP::reloc_sparc_pc30;
237 case SP::BA:
238 case SP::BCOND:
239 case SP::FBCOND: return SP::reloc_sparc_pc22;
240 case SP::BPXCC: return SP::reloc_sparc_pc19;
241 }
242 llvm_unreachable("unknown reloc!");
243}
244
245void SparcCodeEmitter::emitGlobalAddress(const GlobalValue *GV,
246 unsigned Reloc) const {
247 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
248 const_cast<GlobalValue *>(GV), 0,
249 true));
250}
251
252void SparcCodeEmitter::
253emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
254 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
255 Reloc, ES, 0, 0));
256}
257
258void SparcCodeEmitter::
259emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
260 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
261 Reloc, CPI, 0, false));
262}
263
264void SparcCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
265 unsigned Reloc) const {
266 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
267 Reloc, BB));
268}
269
270
271/// createSparcJITCodeEmitterPass - Return a pass that emits the collected Sparc
272/// code to the specified MCE object.
273FunctionPass *llvm::createSparcJITCodeEmitterPass(SparcTargetMachine &TM,
274 JITCodeEmitter &JCE) {
275 return new SparcCodeEmitter(TM, JCE);
276}
277
278#include "SparcGenCodeEmitter.inc"