14 #define DEBUG_TYPE "jit"
28 #if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
30 #elif defined(__i386__) || defined(i386) || defined(_M_IX86)
35 unsigned char *OldByte = (
unsigned char *)Old;
37 unsigned *OldWord = (
unsigned *)OldByte;
39 unsigned OldAddr = (
intptr_t)OldWord;
40 *OldWord = NewAddr - OldAddr - 4;
53 #ifndef __USER_LABEL_PREFIX__
54 #define __USER_LABEL_PREFIX__
56 #define GETASMPREFIX2(X) #X
57 #define GETASMPREFIX(X) GETASMPREFIX2(X)
58 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
63 # define SIZE(sym) ".size " #sym ", . - " #sym "\n"
64 # define TYPE_FUNCTION(sym) ".type " #sym ", @function\n"
67 # define TYPE_FUNCTION(sym)
73 #if defined(__APPLE__)
85 #if defined(X86_64_JIT)
92 ".globl " ASMPREFIX "X86CompilationCallback\n"
95 CFI(
".cfi_startproc\n")
98 CFI(
".cfi_def_cfa_offset 16\n")
99 CFI(
".cfi_offset %rbp, -16\n")
102 CFI(
".cfi_def_cfa_register %rbp\n")
105 CFI(
".cfi_rel_offset %rdi, 0\n")
107 CFI(
".cfi_rel_offset %rsi, 8\n")
109 CFI(
".cfi_rel_offset %rdx, 16\n")
111 CFI(
".cfi_rel_offset %rcx, 24\n")
113 CFI(
".cfi_rel_offset %r8, 32\n")
115 CFI(
".cfi_rel_offset %r9, 40\n")
121 "movaps %xmm0, (%rsp)\n"
122 "movaps %xmm1, 16(%rsp)\n"
123 "movaps %xmm2, 32(%rsp)\n"
124 "movaps %xmm3, 48(%rsp)\n"
125 "movaps %xmm4, 64(%rsp)\n"
126 "movaps %xmm5, 80(%rsp)\n"
127 "movaps %xmm6, 96(%rsp)\n"
128 "movaps %xmm7, 112(%rsp)\n"
130 #if defined(_WIN64) || defined(__CYGWIN__)
133 "movq 8(%rbp), %rdx\n"
134 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
138 "movq 8(%rbp), %rsi\n"
139 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
142 "movaps 112(%rsp), %xmm7\n"
143 "movaps 96(%rsp), %xmm6\n"
144 "movaps 80(%rsp), %xmm5\n"
145 "movaps 64(%rsp), %xmm4\n"
146 "movaps 48(%rsp), %xmm3\n"
147 "movaps 32(%rsp), %xmm2\n"
148 "movaps 16(%rsp), %xmm1\n"
149 "movaps (%rsp), %xmm0\n"
152 CFI(
".cfi_def_cfa_register %rsp\n")
155 CFI(
".cfi_adjust_cfa_offset 48\n")
157 CFI(
".cfi_adjust_cfa_offset -8\n")
158 CFI(
".cfi_restore %r9\n")
160 CFI(
".cfi_adjust_cfa_offset -8\n")
161 CFI(
".cfi_restore %r8\n")
163 CFI(
".cfi_adjust_cfa_offset -8\n")
164 CFI(
".cfi_restore %rcx\n")
166 CFI(
".cfi_adjust_cfa_offset -8\n")
167 CFI(
".cfi_restore %rdx\n")
169 CFI(
".cfi_adjust_cfa_offset -8\n")
170 CFI(
".cfi_restore %rsi\n")
172 CFI(
".cfi_adjust_cfa_offset -8\n")
173 CFI(
".cfi_restore %rdi\n")
176 CFI(
".cfi_adjust_cfa_offset -8\n")
177 CFI(
".cfi_restore %rbp\n")
179 CFI(
".cfi_endproc\n")
180 SIZE(X86CompilationCallback)
188 #elif defined (X86_32_JIT)
194 ".globl " ASMPREFIX "X86CompilationCallback\n"
197 CFI(
".cfi_startproc\n")
199 CFI(
".cfi_def_cfa_offset 8\n")
200 CFI(
".cfi_offset %ebp, -8\n")
202 CFI(
".cfi_def_cfa_register %ebp\n")
204 CFI(
".cfi_rel_offset %eax, 0\n")
206 CFI(
".cfi_rel_offset %edx, 4\n")
208 CFI(
".cfi_rel_offset %ecx, 8\n")
209 # if defined(__APPLE__)
213 "movl 4(%ebp), %eax\n"
214 "movl %eax, 4(%esp)\n"
215 "movl %ebp, (%esp)\n"
216 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
218 CFI(
".cfi_def_cfa_register %esp\n")
220 CFI(
".cfi_adjust_cfa_offset 12\n")
222 CFI(
".cfi_adjust_cfa_offset -4\n")
223 CFI(
".cfi_restore %ecx\n")
225 CFI(
".cfi_adjust_cfa_offset -4\n")
226 CFI(
".cfi_restore %edx\n")
228 CFI(
".cfi_adjust_cfa_offset -4\n")
229 CFI(
".cfi_restore %eax\n")
231 CFI(
".cfi_adjust_cfa_offset -4\n")
232 CFI(
".cfi_restore %ebp\n")
234 CFI(
".cfi_endproc\n")
235 SIZE(X86CompilationCallback)
239 void X86CompilationCallback_SSE(
void);
243 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
245 ASMPREFIX "X86CompilationCallback_SSE:\n"
246 CFI(
".cfi_startproc\n")
248 CFI(
".cfi_def_cfa_offset 8\n")
249 CFI(
".cfi_offset %ebp, -8\n")
251 CFI(
".cfi_def_cfa_register %ebp\n")
253 CFI(
".cfi_rel_offset %eax, 0\n")
255 CFI(
".cfi_rel_offset %edx, 4\n")
257 CFI(
".cfi_rel_offset %ecx, 8\n")
264 "movaps %xmm0, (%esp)\n"
265 "movaps %xmm1, 16(%esp)\n"
266 "movaps %xmm2, 32(%esp)\n"
267 "movaps %xmm3, 48(%esp)\n"
269 "movl 4(%ebp), %eax\n"
270 "movl %eax, 4(%esp)\n"
271 "movl %ebp, (%esp)\n"
272 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
274 "movaps 48(%esp), %xmm3\n"
275 CFI(
".cfi_restore %xmm3\n")
276 "movaps 32(%esp), %xmm2\n"
277 CFI(
".cfi_restore %xmm2\n")
278 "movaps 16(%esp), %xmm1\n"
279 CFI(
".cfi_restore %xmm1\n")
280 "movaps (%esp), %xmm0\n"
281 CFI(
".cfi_restore %xmm0\n")
283 CFI(
".cfi_def_cfa_register esp\n")
285 CFI(
".cfi_adjust_cfa_offset 12\n")
287 CFI(
".cfi_adjust_cfa_offset -4\n")
288 CFI(
".cfi_restore %ecx\n")
290 CFI(
".cfi_adjust_cfa_offset -4\n")
291 CFI(
".cfi_restore %edx\n")
293 CFI(
".cfi_adjust_cfa_offset -4\n")
294 CFI(
".cfi_restore %eax\n")
296 CFI(
".cfi_adjust_cfa_offset -4\n")
297 CFI(
".cfi_restore %ebp\n")
299 CFI(
".cfi_endproc\n")
300 SIZE(X86CompilationCallback_SSE)
314 mov eax, dword ptr [ebp+4]
315 mov dword ptr [esp+4], eax
316 mov dword ptr [esp], ebp
317 call LLVMX86CompilationCallback2
330 #else // Not an i386 host
332 llvm_unreachable(
"Cannot call X86CompilationCallback() on a non-x86 arch!");
345 intptr_t *RetAddrLoc = &StackPtr[1];
349 assert(*RetAddrLoc == RetAddr &&
350 "Could not find return address on the stack!");
353 bool isStub = ((
unsigned char*)RetAddr)[0] == 0xCE;
356 #if defined (X86_64_JIT)
363 DEBUG(
dbgs() <<
"In callback! Addr=" << (
void*)RetAddr
364 <<
" ESP=" << (
void*)StackPtr
365 <<
": Resolving call to function: "
366 << TheVM->getFunctionReferencedName((
void*)RetAddr) <<
"\n");
370 #if defined (X86_64_JIT)
371 assert(((
unsigned char*)RetAddr)[-2] == 0x41 &&
"Not a call instr!");
372 assert(((
unsigned char*)RetAddr)[-1] == 0xFF &&
"Not a call instr!");
374 assert(((
unsigned char*)RetAddr)[-1] == 0xE8 &&
"Not a call instr!");
381 #if defined (X86_64_JIT)
383 "X86-64 doesn't support rewriting non-stub lazy compilation calls:"
384 " the call instruction varies too much.");
394 #if defined (X86_64_JIT)
400 if (diff >= -2147483648LL && diff <= 2147483647LL) {
401 *(
unsigned char*)(RetAddr-0xc) = 0xE9;
402 *(
intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
404 *(
intptr_t *)(RetAddr - 0xa) = NewVal;
405 ((
unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
409 ((
unsigned char*)RetAddr)[-1] = 0xE9;
415 #if defined (X86_64_JIT)
429 #if defined (X86_32_JIT) && !defined (_MSC_VER)
431 return X86CompilationCallback_SSE;
445 #if defined (X86_64_JIT)
446 const unsigned Alignment = 8;
448 uint8_t *Cur = Buffer;
452 const unsigned Alignment = 4;
454 uint8_t *Cur = Buffer;
475 #if defined (X86_32_JIT) && !defined (_MSC_VER)
477 Target != (
void*)(
intptr_t)X86CompilationCallback_SSE);
484 #if defined (X86_64_JIT)
491 JCE.
emitByte(2 | (4 << 3) | (3 << 6));
499 #if defined (X86_64_JIT)
506 JCE.
emitByte(2 | (2 << 3) | (3 << 6));
523 #if defined(X86_64_JIT)
532 std::memcpy(reinterpret_cast<char*>(&Value), reinterpret_cast<char*>(Pos),
535 std::memcpy(reinterpret_cast<char*>(Pos), reinterpret_cast<char*>(&Value),
543 unsigned NumRelocs,
unsigned char* GOTBase) {
544 for (
unsigned i = 0; i != NumRelocs; ++i, ++MR) {
552 addUnaligned<unsigned>(RelocPos, ResultPtr);
559 addUnaligned<unsigned>(RelocPos, ResultPtr);
566 addUnaligned<unsigned>(RelocPos, ResultPtr);
569 addUnaligned<intptr_t>(RelocPos, ResultPtr);
576 #if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
void emitAlignment(unsigned Alignment)
static void addUnaligned(void *Pos, T Delta)
unsigned getRelocationType() const
static TargetJITInfo::JITCompilerFn JITCompilerFunction
virtual void * emitFunctionStub(const Function *F, void *Target, JITCodeEmitter &JCE)
void ValgrindDiscardTranslations(const void *Addr, size_t Len)
void *(* JITCompilerFn)(void *)
#define llvm_unreachable(msg)
intptr_t getConstantVal() const
virtual void * emitGlobalValueIndirectSym(const GlobalValue *GV, void *ptr, JITCodeEmitter &JCE)
static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y, unsigned len)
Generalized subtraction of 64-bit integer arrays.
virtual uintptr_t getPICJumpTableEntry(uintptr_t BB, uintptr_t JTBase)
static void emitWordLEInto(uint8_t *&Buf, uint32_t W)
virtual void replaceMachineCodeForFunction(void *Old, void *New)
intptr_t getMachineCodeOffset() const
#define LLVM_LIBRARY_VISIBILITY
void * getResultPointer() const
const STC & getSubtarget() const
virtual uintptr_t getCurrentPCValue() const
#define __msan_unpoison(p, size)
virtual StubLayout getStubLayout()
Returns the maximum size and alignment for a call stub on this target.
virtual void * allocIndirectGV(const GlobalValue *GV, const uint8_t *Buffer, size_t Size, unsigned Alignment)=0
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
X86JITInfo(X86TargetMachine &tm)
#define TsanIgnoreWritesEnd()
void emitWordLE(uint32_t W)
void X86CompilationCallback()
#define TsanIgnoreWritesBegin()
virtual char * allocateThreadLocalMemory(size_t size)
virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn)
getLazyResolverFunction - Expose the lazy resolver to the JIT.
#define TYPE_FUNCTION(sym)
LLVM Value Representation.
LLVM_ATTRIBUTE_USED LLVM_LIBRARY_VISIBILITY void LLVMX86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr)
virtual void relocate(void *Function, MachineRelocation *MR, unsigned NumRelocs, unsigned char *GOTBase)
#define LLVM_ATTRIBUTE_USED
Records the required size and alignment for a call stub in bytes.