blob: ec47a392081ca3dc4a995a09aa239bd56ef8f80a [file] [log] [blame]
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +00001//===-- SparcJITInfo.cpp - Implement the Sparc JIT Interface --------------===//
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 implements the JIT interfaces for the Sparc target.
11//
12//===----------------------------------------------------------------------===//
13#define DEBUG_TYPE "jit"
14#include "SparcJITInfo.h"
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +000015#include "Sparc.h"
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000016#include "SparcRelocations.h"
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +000017#include "llvm/ADT/SmallVector.h"
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000018#include "llvm/CodeGen/JITCodeEmitter.h"
19#include "llvm/Support/Memory.h"
20
21using namespace llvm;
22
23/// JITCompilerFunction - This contains the address of the JIT function used to
24/// compile a function lazily.
25static TargetJITInfo::JITCompilerFn JITCompilerFunction;
26
27extern "C" void SparcCompilationCallback();
28
29extern "C" {
30#if defined (__sparc__)
31 asm(
32 ".text\n"
33 "\t.align 4\n"
34 "\t.global SparcCompilationCallback\n"
35 "\t.type SparcCompilationCallback, #function\n"
36 "SparcCompilationCallback:\n"
37 // Save current register window.
38 "\tsave %sp, -192, %sp\n"
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +000039 // stubaddr is in %g1.
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000040 "\tcall SparcCompilationCallbackC\n"
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +000041 "\t mov %g1, %o0\n"
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000042 // restore original register window and
43 // copy %o0 to %g1
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +000044 "\trestore %o0, 0, %g1\n"
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000045 // call the new stub
46 "\tjmp %g1\n"
47 "\t nop\n"
48 "\t.size SparcCompilationCallback, .-SparcCompilationCallback"
49 );
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000050#else
51 void SparcCompilationCallback() {
52 llvm_unreachable(
53 "Cannot call SparcCompilationCallback() on a non-sparc arch!");
54 }
55#endif
56}
57
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +000058
59#define SETHI_INST(imm, rd) (0x01000000 | ((rd) << 25) | ((imm) & 0x3FFFFF))
60#define JMP_INST(rs1, imm, rd) (0x80000000 | ((rd) << 25) | (0x38 << 19) \
61 | ((rs1) << 14) | (1 << 13) | ((imm) & 0x1FFF))
62#define NOP_INST SETHI_INST(0, 0)
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +000063#define OR_INST_I(rs1, imm, rd) (0x80000000 | ((rd) << 25) | (0x02 << 19) \
64 | ((rs1) << 14) | (1 << 13) | ((imm) & 0x1FFF))
65#define OR_INST_R(rs1, rs2, rd) (0x80000000 | ((rd) << 25) | (0x02 << 19) \
66 | ((rs1) << 14) | (0 << 13) | ((rs2) & 0x1F))
67#define RDPC_INST(rd) (0x80000000 | ((rd) << 25) | (0x28 << 19) \
68 | (5 << 14))
69#define LDX_INST(rs1, imm, rd) (0xC0000000 | ((rd) << 25) | (0x0B << 19) \
70 | ((rs1) << 14) | (1 << 13) | ((imm) & 0x1FFF))
71#define SLLX_INST(rs1, imm, rd) (0x80000000 | ((rd) << 25) | (0x25 << 19) \
72 | ((rs1) << 14) | (3 << 12) | ((imm) & 0x3F))
73#define SUB_INST(rs1, imm, rd) (0x80000000 | ((rd) << 25) | (0x04 << 19) \
74 | ((rs1) << 14) | (1 << 13) | ((imm) & 0x1FFF))
75#define XOR_INST(rs1, imm, rd) (0x80000000 | ((rd) << 25) | (0x03 << 19) \
76 | ((rs1) << 14) | (1 << 13) | ((imm) & 0x1FFF))
77#define BA_INST(tgt) (0x10800000 | ((tgt) & 0x3FFFFF))
78
79// Emit instructions to jump to Addr and store the starting address of
80// the instructions emitted in the scratch register.
81static void emitInstrForIndirectJump(intptr_t Addr,
82 unsigned scratch,
83 SmallVectorImpl<uint32_t> &Insts) {
84
85 if (isInt<13>(Addr)) {
86 // Emit: jmpl %g0+Addr, <scratch>
87 // nop
88 Insts.push_back(JMP_INST(0, LO10(Addr), scratch));
89 Insts.push_back(NOP_INST);
90 return;
91 }
92
93 if (isUInt<32>(Addr)) {
94 // Emit: sethi %hi(Addr), scratch
95 // jmpl scratch+%lo(Addr), scratch
96 // sub scratch, 4, scratch
97 Insts.push_back(SETHI_INST(HI22(Addr), scratch));
98 Insts.push_back(JMP_INST(scratch, LO10(Addr), scratch));
99 Insts.push_back(SUB_INST(scratch, 4, scratch));
100 return;
101 }
102
103 if (Addr < 0 && isInt<33>(Addr)) {
104 // Emit: sethi %hix(Addr), scratch)
105 // xor scratch, %lox(Addr), scratch
106 // jmpl scratch+0, scratch
107 // sub scratch, 8, scratch
108 Insts.push_back(SETHI_INST(HIX22(Addr), scratch));
109 Insts.push_back(XOR_INST(scratch, LOX10(Addr), scratch));
110 Insts.push_back(JMP_INST(scratch, 0, scratch));
111 Insts.push_back(SUB_INST(scratch, 8, scratch));
112 return;
113 }
114
115 // Emit: rd %pc, scratch
116 // ldx [scratch+16], scratch
117 // jmpl scratch+0, scratch
118 // sub scratch, 8, scratch
119 // <Addr: 8 byte>
120 Insts.push_back(RDPC_INST(scratch));
121 Insts.push_back(LDX_INST(scratch, 16, scratch));
122 Insts.push_back(JMP_INST(scratch, 0, scratch));
123 Insts.push_back(SUB_INST(scratch, 8, scratch));
124 Insts.push_back((uint32_t)(((int64_t)Addr) >> 32) & 0xffffffff);
125 Insts.push_back((uint32_t)(Addr & 0xffffffff));
126
127 // Instruction sequence without rdpc instruction
128 // 7 instruction and 2 scratch register
129 // Emit: sethi %hh(Addr), scratch
130 // or scratch, %hm(Addr), scratch
131 // sllx scratch, 32, scratch
132 // sethi %hi(Addr), scratch2
133 // or scratch, scratch2, scratch
134 // jmpl scratch+%lo(Addr), scratch
135 // sub scratch, 20, scratch
136 // Insts.push_back(SETHI_INST(HH22(Addr), scratch));
137 // Insts.push_back(OR_INST_I(scratch, HM10(Addr), scratch));
138 // Insts.push_back(SLLX_INST(scratch, 32, scratch));
139 // Insts.push_back(SETHI_INST(HI22(Addr), scratch2));
140 // Insts.push_back(OR_INST_R(scratch, scratch2, scratch));
141 // Insts.push_back(JMP_INST(scratch, LO10(Addr), scratch));
142 // Insts.push_back(SUB_INST(scratch, 20, scratch));
143}
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000144
145extern "C" void *SparcCompilationCallbackC(intptr_t StubAddr) {
146 // Get the address of the compiled code for this function.
147 intptr_t NewVal = (intptr_t) JITCompilerFunction((void*) StubAddr);
148
149 // Rewrite the function stub so that we don't end up here every time we
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000150 // execute the call. We're replacing the stub instructions with code
151 // that jumps to the compiled function:
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000152
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000153 SmallVector<uint32_t, 8> Insts;
154 intptr_t diff = (NewVal - StubAddr) >> 2;
155 if (isInt<22>(diff)) {
156 // Use branch instruction to jump
157 Insts.push_back(BA_INST(diff));
158 Insts.push_back(NOP_INST);
159 } else {
160 // Otherwise, use indirect jump to the compiled function
161 emitInstrForIndirectJump(NewVal, 1, Insts);
162 }
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000163
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000164 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
165 *(uint32_t *)(StubAddr + i*4) = Insts[i];
166
167 sys::Memory::InvalidateInstructionCache((void*) StubAddr, Insts.size() * 4);
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000168 return (void*)StubAddr;
169}
170
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000171
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000172void SparcJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
173 assert(0 && "FIXME: Implement SparcJITInfo::replaceMachineCodeForFunction");
174}
175
176
177TargetJITInfo::StubLayout SparcJITInfo::getStubLayout() {
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000178 // The stub contains maximum of 4 4-byte instructions and 8 bytes for address,
179 // aligned at 32 bytes.
180 // See emitFunctionStub and emitInstrForIndirectJump for details.
181 StubLayout Result = { 4*4 + 8, 32 };
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000182 return Result;
183}
184
185void *SparcJITInfo::emitFunctionStub(const Function *F, void *Fn,
186 JITCodeEmitter &JCE)
187{
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000188 JCE.emitAlignment(32);
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000189 void *Addr = (void*) (JCE.getCurrentPCValue());
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000190
191 intptr_t CurrentAddr = (intptr_t)Addr;
192 intptr_t EmittedAddr;
193 SmallVector<uint32_t, 8> Insts;
194 if (Fn != (void*)(intptr_t)SparcCompilationCallback) {
195 EmittedAddr = (intptr_t)Fn;
196 intptr_t diff = (EmittedAddr - CurrentAddr) >> 2;
197 if (isInt<22>(diff)) {
198 Insts.push_back(BA_INST(diff));
199 Insts.push_back(NOP_INST);
200 }
201 } else {
202 EmittedAddr = (intptr_t)SparcCompilationCallback;
203 }
204
205 if (Insts.size() == 0)
206 emitInstrForIndirectJump(EmittedAddr, 1, Insts);
207
208
209 if (!sys::Memory::setRangeWritable(Addr, 4 * Insts.size()))
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000210 llvm_unreachable("ERROR: Unable to mark stub writable.");
211
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000212 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
213 JCE.emitWordBE(Insts[i]);
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000214
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000215 sys::Memory::InvalidateInstructionCache(Addr, 4 * Insts.size());
216 if (!sys::Memory::setRangeExecutable(Addr, 4 * Insts.size()))
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000217 llvm_unreachable("ERROR: Unable to mark stub executable.");
218
219 return Addr;
220}
221
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000222
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000223TargetJITInfo::LazyResolverFn
224SparcJITInfo::getLazyResolverFunction(JITCompilerFn F) {
225 JITCompilerFunction = F;
226 return SparcCompilationCallback;
227}
228
229/// relocate - Before the JIT can run a block of code that has been emitted,
230/// it must rewrite the code to contain the actual addresses of any
231/// referenced global symbols.
232void SparcJITInfo::relocate(void *Function, MachineRelocation *MR,
233 unsigned NumRelocs, unsigned char *GOTBase) {
234 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
235 void *RelocPos = (char*) Function + MR->getMachineCodeOffset();
236 intptr_t ResultPtr = (intptr_t) MR->getResultPointer();
237
238 switch ((SP::RelocationType) MR->getRelocationType()) {
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000239 case SP::reloc_sparc_hi:
240 ResultPtr = (ResultPtr >> 10) & 0x3fffff;
241 break;
242
243 case SP::reloc_sparc_lo:
244 ResultPtr = (ResultPtr & 0x3ff);
245 break;
246
247 case SP::reloc_sparc_pc30:
248 ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x3fffffff;
249 break;
250
251 case SP::reloc_sparc_pc22:
252 ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x3fffff;
253 break;
254
255 case SP::reloc_sparc_pc19:
256 ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x7ffff;
257 break;
Venkatraman Govindarajudc3bcc12014-01-24 07:10:19 +0000258
259 case SP::reloc_sparc_h44:
260 ResultPtr = (ResultPtr >> 22) & 0x3fffff;
261 break;
262
263 case SP::reloc_sparc_m44:
264 ResultPtr = (ResultPtr >> 12) & 0x3ff;
265 break;
266
267 case SP::reloc_sparc_l44:
268 ResultPtr = (ResultPtr & 0xfff);
269 break;
270
271 case SP::reloc_sparc_hh:
272 ResultPtr = (((int64_t)ResultPtr) >> 42) & 0x3fffff;
273 break;
274
275 case SP::reloc_sparc_hm:
276 ResultPtr = (((int64_t)ResultPtr) >> 32) & 0x3ff;
277 break;
278
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +0000279 }
280 *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
281 }
282}