blob: 2072488206aa1f62f4cec0f24f5fa3e15f1f9df5 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsJITInfo.cpp - Implement the Mips JIT Interface ----------------===//
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +00002//
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 Mips target.
11//
12//===----------------------------------------------------------------------===//
13
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000014#include "MipsJITInfo.h"
15#include "MipsInstrInfo.h"
16#include "MipsRelocations.h"
17#include "MipsSubtarget.h"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000018#include "llvm/CodeGen/JITCodeEmitter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Function.h"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000022#include "llvm/Support/Memory.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000024#include <cstdlib>
25using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "jit"
28
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000029
30void MipsJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
Akira Hatanaka0820f0c2012-08-01 02:29:24 +000031 unsigned NewAddr = (intptr_t)New;
32 unsigned OldAddr = (intptr_t)Old;
33 const unsigned NopInstr = 0x0;
34
35 // If the functions are in the same memory segment, insert PC-region branch.
36 if ((NewAddr & 0xF0000000) == ((OldAddr + 4) & 0xF0000000)) {
37 unsigned *OldInstruction = (unsigned *)Old;
38 *OldInstruction = 0x08000000;
39 unsigned JTargetAddr = NewAddr & 0x0FFFFFFC;
40
41 JTargetAddr >>= 2;
42 *OldInstruction |= JTargetAddr;
43
44 // Insert a NOP.
45 OldInstruction++;
46 *OldInstruction = NopInstr;
47
48 sys::Memory::InvalidateInstructionCache(Old, 2 * 4);
49 } else {
50 // We need to clear hint bits from the instruction, in case it is 'jr ra'.
51 const unsigned HintMask = 0xFFFFF83F, ReturnSequence = 0x03e00008;
52 unsigned* CurrentInstr = (unsigned*)Old;
53 unsigned CurrInstrHintClear = (*CurrentInstr) & HintMask;
54 unsigned* NextInstr = CurrentInstr + 1;
55 unsigned NextInstrHintClear = (*NextInstr) & HintMask;
56
57 // Do absolute jump if there are 2 or more instructions before return from
58 // the old function.
59 if ((CurrInstrHintClear != ReturnSequence) &&
60 (NextInstrHintClear != ReturnSequence)) {
61 const unsigned LuiT0Instr = 0x3c080000, AddiuT0Instr = 0x25080000;
62 const unsigned JrT0Instr = 0x01000008;
63 // lui t0, high 16 bit of the NewAddr
64 (*(CurrentInstr++)) = LuiT0Instr | ((NewAddr & 0xffff0000) >> 16);
65 // addiu t0, t0, low 16 bit of the NewAddr
66 (*(CurrentInstr++)) = AddiuT0Instr | (NewAddr & 0x0000ffff);
67 // jr t0
68 (*(CurrentInstr++)) = JrT0Instr;
69 (*CurrentInstr) = NopInstr;
70
71 sys::Memory::InvalidateInstructionCache(Old, 4 * 4);
72 } else {
73 // Unsupported case
74 report_fatal_error("MipsJITInfo::replaceMachineCodeForFunction");
75 }
76 }
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000077}
78
79/// JITCompilerFunction - This contains the address of the JIT function used to
80/// compile a function lazily.
81static TargetJITInfo::JITCompilerFn JITCompilerFunction;
82
83// Get the ASMPREFIX for the current host. This is often '_'.
84#ifndef __USER_LABEL_PREFIX__
85#define __USER_LABEL_PREFIX__
86#endif
87#define GETASMPREFIX2(X) #X
88#define GETASMPREFIX(X) GETASMPREFIX2(X)
89#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
90
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +000091// CompilationCallback stub - We can't use a C function with inline assembly in
92// it, because the prolog/epilog inserted by GCC won't work for us. Instead,
93// write our own wrapper, which does things our way, so we have complete control
94// over register saving and restoring. This code saves registers, calls
95// MipsCompilationCallbackC and restores registers.
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +000096extern "C" {
97#if defined (__mips__)
98void MipsCompilationCallback();
99
100 asm(
101 ".text\n"
102 ".align 2\n"
103 ".globl " ASMPREFIX "MipsCompilationCallback\n"
104 ASMPREFIX "MipsCompilationCallback:\n"
105 ".ent " ASMPREFIX "MipsCompilationCallback\n"
Bruno Cardoso Lopesc0ecd1f2011-10-25 17:30:47 +0000106 ".frame $sp, 32, $ra\n"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000107 ".set noreorder\n"
108 ".cpload $t9\n"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000109
Bruno Cardoso Lopesc0ecd1f2011-10-25 17:30:47 +0000110 "addiu $sp, $sp, -64\n"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000111 ".cprestore 16\n"
112
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000113 // Save argument registers a0, a1, a2, a3, f12, f14 since they may contain
114 // stuff for the real target function right now. We have to act as if this
115 // whole compilation callback doesn't exist as far as the caller is
116 // concerned. We also need to save the ra register since it contains the
117 // original return address, and t8 register since it contains the address
118 // of the end of function stub.
119 "sw $a0, 20($sp)\n"
120 "sw $a1, 24($sp)\n"
121 "sw $a2, 28($sp)\n"
122 "sw $a3, 32($sp)\n"
123 "sw $ra, 36($sp)\n"
124 "sw $t8, 40($sp)\n"
Bruno Cardoso Lopesc0ecd1f2011-10-25 17:30:47 +0000125 "sdc1 $f12, 48($sp)\n"
126 "sdc1 $f14, 56($sp)\n"
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000127
128 // t8 points at the end of function stub. Pass the beginning of the stub
129 // to the MipsCompilationCallbackC.
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000130 "addiu $a0, $t8, -16\n"
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000131 "jal " ASMPREFIX "MipsCompilationCallbackC\n"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000132 "nop\n"
133
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000134 // Restore registers.
135 "lw $a0, 20($sp)\n"
136 "lw $a1, 24($sp)\n"
137 "lw $a2, 28($sp)\n"
138 "lw $a3, 32($sp)\n"
139 "lw $ra, 36($sp)\n"
140 "lw $t8, 40($sp)\n"
Bruno Cardoso Lopesc0ecd1f2011-10-25 17:30:47 +0000141 "ldc1 $f12, 48($sp)\n"
142 "ldc1 $f14, 56($sp)\n"
143 "addiu $sp, $sp, 64\n"
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000144
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000145 // Jump to the (newly modified) stub to invoke the real function.
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000146 "addiu $t8, $t8, -16\n"
147 "jr $t8\n"
148 "nop\n"
149
150 ".set reorder\n"
151 ".end " ASMPREFIX "MipsCompilationCallback\n"
152 );
153#else // host != Mips
154 void MipsCompilationCallback() {
155 llvm_unreachable(
156 "Cannot call MipsCompilationCallback() on a non-Mips arch!");
157 }
158#endif
159}
160
161/// MipsCompilationCallbackC - This is the target-specific function invoked
162/// by the function stub when we did not know the real target of a call.
163/// This function must locate the start of the stub or call site and pass
164/// it into the JIT compiler function.
165extern "C" void MipsCompilationCallbackC(intptr_t StubAddr) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000166 // Get the address of the compiled code for this function.
167 intptr_t NewVal = (intptr_t) JITCompilerFunction((void*) StubAddr);
168
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000169 // Rewrite the function stub so that we don't end up here every time we
170 // execute the call. We're replacing the first four instructions of the
171 // stub with code that jumps to the compiled function:
172 // lui $t9, %hi(NewVal)
173 // addiu $t9, $t9, %lo(NewVal)
174 // jr $t9
175 // nop
176
177 int Hi = ((unsigned)NewVal & 0xffff0000) >> 16;
178 if ((NewVal & 0x8000) != 0)
179 Hi++;
180 int Lo = (int)(NewVal & 0xffff);
181
182 *(intptr_t *)(StubAddr) = 0xf << 26 | 25 << 16 | Hi;
183 *(intptr_t *)(StubAddr + 4) = 9 << 26 | 25 << 21 | 25 << 16 | Lo;
184 *(intptr_t *)(StubAddr + 8) = 25 << 21 | 8;
185 *(intptr_t *)(StubAddr + 12) = 0;
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000186
187 sys::Memory::InvalidateInstructionCache((void*) StubAddr, 16);
188}
189
190TargetJITInfo::LazyResolverFn MipsJITInfo::getLazyResolverFunction(
191 JITCompilerFn F) {
192 JITCompilerFunction = F;
193 return MipsCompilationCallback;
194}
195
196TargetJITInfo::StubLayout MipsJITInfo::getStubLayout() {
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000197 // The stub contains 4 4-byte instructions, aligned at 4 bytes. See
198 // emitFunctionStub for details.
199 StubLayout Result = { 4*4, 4 };
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000200 return Result;
201}
202
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000203void *MipsJITInfo::emitFunctionStub(const Function *F, void *Fn,
204 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000205 JCE.emitAlignment(4);
206 void *Addr = (void*) (JCE.getCurrentPCValue());
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000207 if (!sys::Memory::setRangeWritable(Addr, 16))
208 llvm_unreachable("ERROR: Unable to mark stub writable.");
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000209
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000210 intptr_t EmittedAddr;
211 if (Fn != (void*)(intptr_t)MipsCompilationCallback)
212 EmittedAddr = (intptr_t)Fn;
213 else
214 EmittedAddr = (intptr_t)MipsCompilationCallback;
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000215
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000216
217 int Hi = ((unsigned)EmittedAddr & 0xffff0000) >> 16;
218 if ((EmittedAddr & 0x8000) != 0)
219 Hi++;
220 int Lo = (int)(EmittedAddr & 0xffff);
221
Petar Jovanovicc467c942013-07-24 13:02:35 +0000222 // lui $t9, %hi(EmittedAddr)
223 // addiu $t9, $t9, %lo(EmittedAddr)
224 // jalr $t8, $t9
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000225 // nop
Akira Hatanaka4c128502012-12-03 23:11:12 +0000226 if (IsLittleEndian) {
227 JCE.emitWordLE(0xf << 26 | 25 << 16 | Hi);
228 JCE.emitWordLE(9 << 26 | 25 << 21 | 25 << 16 | Lo);
229 JCE.emitWordLE(25 << 21 | 24 << 11 | 9);
230 JCE.emitWordLE(0);
231 } else {
232 JCE.emitWordBE(0xf << 26 | 25 << 16 | Hi);
233 JCE.emitWordBE(9 << 26 | 25 << 21 | 25 << 16 | Lo);
234 JCE.emitWordBE(25 << 21 | 24 << 11 | 9);
235 JCE.emitWordBE(0);
236 }
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000237
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000238 sys::Memory::InvalidateInstructionCache(Addr, 16);
239 if (!sys::Memory::setRangeExecutable(Addr, 16))
240 llvm_unreachable("ERROR: Unable to mark stub executable.");
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000241
242 return Addr;
243}
244
245/// relocate - Before the JIT can run a block of code that has been emitted,
246/// it must rewrite the code to contain the actual addresses of any
247/// referenced global symbols.
248void MipsJITInfo::relocate(void *Function, MachineRelocation *MR,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000249 unsigned NumRelocs, unsigned char *GOTBase) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000250 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
251
252 void *RelocPos = (char*) Function + MR->getMachineCodeOffset();
253 intptr_t ResultPtr = (intptr_t) MR->getResultPointer();
254
255 switch ((Mips::RelocationType) MR->getRelocationType()) {
Bruno Cardoso Lopesd5b28342011-12-30 21:04:30 +0000256 case Mips::reloc_mips_pc16:
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000257 ResultPtr = (((ResultPtr - (intptr_t) RelocPos) - 4) >> 2) & 0xffff;
258 *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
259 break;
260
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000261 case Mips::reloc_mips_26:
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000262 ResultPtr = (ResultPtr & 0x0fffffff) >> 2;
263 *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000264 break;
265
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +0000266 case Mips::reloc_mips_hi:
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000267 ResultPtr = ResultPtr >> 16;
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000268 if ((((intptr_t) (MR->getResultPointer()) & 0xffff) >> 15) == 1) {
269 ResultPtr += 1;
270 }
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000271 *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000272 break;
273
Bruno Cardoso Lopes71133fe2011-11-08 12:47:11 +0000274 case Mips::reloc_mips_lo: {
275 // Addend is needed for unaligned load/store instructions, where offset
276 // for the second load/store in the expanded instruction sequence must
277 // be modified by +1 or +3. Otherwise, Addend is 0.
278 int Addend = *((unsigned*) RelocPos) & 0xffff;
279 ResultPtr = (ResultPtr + Addend) & 0xffff;
280 *((unsigned*) RelocPos) &= 0xffff0000;
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000281 *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
282 break;
Bruno Cardoso Lopes71133fe2011-11-08 12:47:11 +0000283 }
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000284 }
285 }
286}