[CFLAA] LLVM_CONSTEXPR -> const
[oota-llvm.git] / lib / Target / X86 / X86JITInfo.cpp
1 //===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
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 X86 target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86JITInfo.h"
15 #include "X86Relocations.h"
16 #include "X86Subtarget.h"
17 #include "X86TargetMachine.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Valgrind.h"
23 #include <cstdlib>
24 #include <cstring>
25 using namespace llvm;
26
27 #define DEBUG_TYPE "jit"
28
29 // Determine the platform we're running on
30 #if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
31 # define X86_64_JIT
32 #elif defined(__i386__) || defined(i386) || defined(_M_IX86)
33 # define X86_32_JIT
34 #endif
35
36 // x86 is little-endian, and we can do unaligned memory accesses.
37 template<typename value_type>
38 static value_type read_x86(const void *memory) {
39   return support::endian::read<value_type, support::little, 1>(memory);
40 }
41
42 template<typename value_type>
43 static void write_x86(void *memory, value_type value) {
44   support::endian::write<value_type, support::little, 1>(memory, value);
45 }
46
47 void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
48   unsigned char *OldPtr = static_cast<unsigned char*>(Old);
49   write_x86<unsigned char>(OldPtr++, 0xE9); // Emit JMP opcode.
50   unsigned NewAddr = (intptr_t)New;
51   unsigned OldAddr = (intptr_t)OldPtr;
52   write_x86<unsigned>(
53       OldPtr, NewAddr - OldAddr - 4); // Emit PC-relative addr of New code.
54
55   // X86 doesn't need to invalidate the processor cache, so just invalidate
56   // Valgrind's cache directly.
57   sys::ValgrindDiscardTranslations(Old, 5);
58 }
59
60
61 /// JITCompilerFunction - This contains the address of the JIT function used to
62 /// compile a function lazily.
63 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
64
65 // Get the ASMPREFIX for the current host.  This is often '_'.
66 #ifndef __USER_LABEL_PREFIX__
67 #define __USER_LABEL_PREFIX__
68 #endif
69 #define GETASMPREFIX2(X) #X
70 #define GETASMPREFIX(X) GETASMPREFIX2(X)
71 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
72
73 // For ELF targets, use a .size and .type directive, to let tools
74 // know the extent of functions defined in assembler.
75 #if defined(__ELF__)
76 # define SIZE(sym) ".size " #sym ", . - " #sym "\n"
77 # define TYPE_FUNCTION(sym) ".type " #sym ", @function\n"
78 #else
79 # define SIZE(sym)
80 # define TYPE_FUNCTION(sym)
81 #endif
82
83 // Provide a convenient way for disabling usage of CFI directives.
84 // This is needed for old/broken assemblers (for example, gas on
85 // Darwin is pretty old and doesn't support these directives)
86 #if defined(__APPLE__)
87 # define CFI(x)
88 #else
89 // FIXME: Disable this until we really want to use it. Also, we will
90 //        need to add some workarounds for compilers, which support
91 //        only subset of these directives.
92 # define CFI(x)
93 #endif
94
95 // Provide a wrapper for LLVMX86CompilationCallback2 that saves non-traditional
96 // callee saved registers, for the fastcc calling convention.
97 extern "C" {
98 #if defined(X86_64_JIT)
99 # ifndef _MSC_VER
100   // No need to save EAX/EDX for X86-64.
101   void X86CompilationCallback(void);
102   asm(
103     ".text\n"
104     ".align 8\n"
105     ".globl " ASMPREFIX "X86CompilationCallback\n"
106     TYPE_FUNCTION(X86CompilationCallback)
107   ASMPREFIX "X86CompilationCallback:\n"
108     CFI(".cfi_startproc\n")
109     // Save RBP
110     "pushq   %rbp\n"
111     CFI(".cfi_def_cfa_offset 16\n")
112     CFI(".cfi_offset %rbp, -16\n")
113     // Save RSP
114     "movq    %rsp, %rbp\n"
115     CFI(".cfi_def_cfa_register %rbp\n")
116     // Save all int arg registers
117     "pushq   %rdi\n"
118     CFI(".cfi_rel_offset %rdi, 0\n")
119     "pushq   %rsi\n"
120     CFI(".cfi_rel_offset %rsi, 8\n")
121     "pushq   %rdx\n"
122     CFI(".cfi_rel_offset %rdx, 16\n")
123     "pushq   %rcx\n"
124     CFI(".cfi_rel_offset %rcx, 24\n")
125     "pushq   %r8\n"
126     CFI(".cfi_rel_offset %r8, 32\n")
127     "pushq   %r9\n"
128     CFI(".cfi_rel_offset %r9, 40\n")
129     // Align stack on 16-byte boundary. ESP might not be properly aligned
130     // (8 byte) if this is called from an indirect stub.
131     "andq    $-16, %rsp\n"
132     // Save all XMM arg registers
133     "subq    $128, %rsp\n"
134     "movaps  %xmm0, (%rsp)\n"
135     "movaps  %xmm1, 16(%rsp)\n"
136     "movaps  %xmm2, 32(%rsp)\n"
137     "movaps  %xmm3, 48(%rsp)\n"
138     "movaps  %xmm4, 64(%rsp)\n"
139     "movaps  %xmm5, 80(%rsp)\n"
140     "movaps  %xmm6, 96(%rsp)\n"
141     "movaps  %xmm7, 112(%rsp)\n"
142     // JIT callee
143 #if defined(_WIN64) || defined(__CYGWIN__)
144     "subq    $32, %rsp\n"
145     "movq    %rbp, %rcx\n"    // Pass prev frame and return address
146     "movq    8(%rbp), %rdx\n"
147     "call    " ASMPREFIX "LLVMX86CompilationCallback2\n"
148     "addq    $32, %rsp\n"
149 #else
150     "movq    %rbp, %rdi\n"    // Pass prev frame and return address
151     "movq    8(%rbp), %rsi\n"
152     "call    " ASMPREFIX "LLVMX86CompilationCallback2\n"
153 #endif
154     // Restore all XMM arg registers
155     "movaps  112(%rsp), %xmm7\n"
156     "movaps  96(%rsp), %xmm6\n"
157     "movaps  80(%rsp), %xmm5\n"
158     "movaps  64(%rsp), %xmm4\n"
159     "movaps  48(%rsp), %xmm3\n"
160     "movaps  32(%rsp), %xmm2\n"
161     "movaps  16(%rsp), %xmm1\n"
162     "movaps  (%rsp), %xmm0\n"
163     // Restore RSP
164     "movq    %rbp, %rsp\n"
165     CFI(".cfi_def_cfa_register %rsp\n")
166     // Restore all int arg registers
167     "subq    $48, %rsp\n"
168     CFI(".cfi_adjust_cfa_offset 48\n")
169     "popq    %r9\n"
170     CFI(".cfi_adjust_cfa_offset -8\n")
171     CFI(".cfi_restore %r9\n")
172     "popq    %r8\n"
173     CFI(".cfi_adjust_cfa_offset -8\n")
174     CFI(".cfi_restore %r8\n")
175     "popq    %rcx\n"
176     CFI(".cfi_adjust_cfa_offset -8\n")
177     CFI(".cfi_restore %rcx\n")
178     "popq    %rdx\n"
179     CFI(".cfi_adjust_cfa_offset -8\n")
180     CFI(".cfi_restore %rdx\n")
181     "popq    %rsi\n"
182     CFI(".cfi_adjust_cfa_offset -8\n")
183     CFI(".cfi_restore %rsi\n")
184     "popq    %rdi\n"
185     CFI(".cfi_adjust_cfa_offset -8\n")
186     CFI(".cfi_restore %rdi\n")
187     // Restore RBP
188     "popq    %rbp\n"
189     CFI(".cfi_adjust_cfa_offset -8\n")
190     CFI(".cfi_restore %rbp\n")
191     "ret\n"
192     CFI(".cfi_endproc\n")
193     SIZE(X86CompilationCallback)
194   );
195 # else
196   // No inline assembler support on this platform. The routine is in external
197   // file.
198   void X86CompilationCallback();
199
200 # endif
201 #elif defined (X86_32_JIT)
202 # ifndef _MSC_VER
203   void X86CompilationCallback(void);
204   asm(
205     ".text\n"
206     ".align 8\n"
207     ".globl " ASMPREFIX "X86CompilationCallback\n"
208     TYPE_FUNCTION(X86CompilationCallback)
209   ASMPREFIX "X86CompilationCallback:\n"
210     CFI(".cfi_startproc\n")
211     "pushl   %ebp\n"
212     CFI(".cfi_def_cfa_offset 8\n")
213     CFI(".cfi_offset %ebp, -8\n")
214     "movl    %esp, %ebp\n"    // Standard prologue
215     CFI(".cfi_def_cfa_register %ebp\n")
216     "pushl   %eax\n"
217     CFI(".cfi_rel_offset %eax, 0\n")
218     "pushl   %edx\n"          // Save EAX/EDX/ECX
219     CFI(".cfi_rel_offset %edx, 4\n")
220     "pushl   %ecx\n"
221     CFI(".cfi_rel_offset %ecx, 8\n")
222 #  if defined(__APPLE__)
223     "andl    $-16, %esp\n"    // Align ESP on 16-byte boundary
224 #  endif
225     "subl    $16, %esp\n"
226     "movl    4(%ebp), %eax\n" // Pass prev frame and return address
227     "movl    %eax, 4(%esp)\n"
228     "movl    %ebp, (%esp)\n"
229     "call    " ASMPREFIX "LLVMX86CompilationCallback2\n"
230     "movl    %ebp, %esp\n"    // Restore ESP
231     CFI(".cfi_def_cfa_register %esp\n")
232     "subl    $12, %esp\n"
233     CFI(".cfi_adjust_cfa_offset 12\n")
234     "popl    %ecx\n"
235     CFI(".cfi_adjust_cfa_offset -4\n")
236     CFI(".cfi_restore %ecx\n")
237     "popl    %edx\n"
238     CFI(".cfi_adjust_cfa_offset -4\n")
239     CFI(".cfi_restore %edx\n")
240     "popl    %eax\n"
241     CFI(".cfi_adjust_cfa_offset -4\n")
242     CFI(".cfi_restore %eax\n")
243     "popl    %ebp\n"
244     CFI(".cfi_adjust_cfa_offset -4\n")
245     CFI(".cfi_restore %ebp\n")
246     "ret\n"
247     CFI(".cfi_endproc\n")
248     SIZE(X86CompilationCallback)
249   );
250
251   // Same as X86CompilationCallback but also saves XMM argument registers.
252   void X86CompilationCallback_SSE(void);
253   asm(
254     ".text\n"
255     ".align 8\n"
256     ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
257     TYPE_FUNCTION(X86CompilationCallback_SSE)
258   ASMPREFIX "X86CompilationCallback_SSE:\n"
259     CFI(".cfi_startproc\n")
260     "pushl   %ebp\n"
261     CFI(".cfi_def_cfa_offset 8\n")
262     CFI(".cfi_offset %ebp, -8\n")
263     "movl    %esp, %ebp\n"    // Standard prologue
264     CFI(".cfi_def_cfa_register %ebp\n")
265     "pushl   %eax\n"
266     CFI(".cfi_rel_offset %eax, 0\n")
267     "pushl   %edx\n"          // Save EAX/EDX/ECX
268     CFI(".cfi_rel_offset %edx, 4\n")
269     "pushl   %ecx\n"
270     CFI(".cfi_rel_offset %ecx, 8\n")
271     "andl    $-16, %esp\n"    // Align ESP on 16-byte boundary
272     // Save all XMM arg registers
273     "subl    $64, %esp\n"
274     // FIXME: provide frame move information for xmm registers.
275     // This can be tricky, because CFA register is ebp (unaligned)
276     // and we need to produce offsets relative to it.
277     "movaps  %xmm0, (%esp)\n"
278     "movaps  %xmm1, 16(%esp)\n"
279     "movaps  %xmm2, 32(%esp)\n"
280     "movaps  %xmm3, 48(%esp)\n"
281     "subl    $16, %esp\n"
282     "movl    4(%ebp), %eax\n" // Pass prev frame and return address
283     "movl    %eax, 4(%esp)\n"
284     "movl    %ebp, (%esp)\n"
285     "call    " ASMPREFIX "LLVMX86CompilationCallback2\n"
286     "addl    $16, %esp\n"
287     "movaps  48(%esp), %xmm3\n"
288     CFI(".cfi_restore %xmm3\n")
289     "movaps  32(%esp), %xmm2\n"
290     CFI(".cfi_restore %xmm2\n")
291     "movaps  16(%esp), %xmm1\n"
292     CFI(".cfi_restore %xmm1\n")
293     "movaps  (%esp), %xmm0\n"
294     CFI(".cfi_restore %xmm0\n")
295     "movl    %ebp, %esp\n"    // Restore ESP
296     CFI(".cfi_def_cfa_register esp\n")
297     "subl    $12, %esp\n"
298     CFI(".cfi_adjust_cfa_offset 12\n")
299     "popl    %ecx\n"
300     CFI(".cfi_adjust_cfa_offset -4\n")
301     CFI(".cfi_restore %ecx\n")
302     "popl    %edx\n"
303     CFI(".cfi_adjust_cfa_offset -4\n")
304     CFI(".cfi_restore %edx\n")
305     "popl    %eax\n"
306     CFI(".cfi_adjust_cfa_offset -4\n")
307     CFI(".cfi_restore %eax\n")
308     "popl    %ebp\n"
309     CFI(".cfi_adjust_cfa_offset -4\n")
310     CFI(".cfi_restore %ebp\n")
311     "ret\n"
312     CFI(".cfi_endproc\n")
313     SIZE(X86CompilationCallback_SSE)
314   );
315 # else
316   void LLVMX86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
317
318   _declspec(naked) void X86CompilationCallback(void) {
319     __asm {
320       push  ebp
321       mov   ebp, esp
322       push  eax
323       push  edx
324       push  ecx
325       and   esp, -16
326       sub   esp, 16
327       mov   eax, dword ptr [ebp+4]
328       mov   dword ptr [esp+4], eax
329       mov   dword ptr [esp], ebp
330       call  LLVMX86CompilationCallback2
331       mov   esp, ebp
332       sub   esp, 12
333       pop   ecx
334       pop   edx
335       pop   eax
336       pop   ebp
337       ret
338     }
339   }
340
341 # endif // _MSC_VER
342
343 #else // Not an i386 host
344   void X86CompilationCallback() {
345     llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
346   }
347 #endif
348 }
349
350 /// This is the target-specific function invoked by the
351 /// function stub when we did not know the real target of a call.  This function
352 /// must locate the start of the stub or call site and pass it into the JIT
353 /// compiler function.
354 extern "C" {
355 LLVM_ATTRIBUTE_USED // Referenced from inline asm.
356 LLVM_LIBRARY_VISIBILITY void LLVMX86CompilationCallback2(intptr_t *StackPtr,
357                                                          intptr_t RetAddr) {
358   intptr_t *RetAddrLoc = &StackPtr[1];
359   // We are reading raw stack data here. Tell MemorySanitizer that it is
360   // sufficiently initialized.
361   __msan_unpoison(RetAddrLoc, sizeof(*RetAddrLoc));
362   assert(*RetAddrLoc == RetAddr &&
363          "Could not find return address on the stack!");
364
365   // It's a stub if there is an interrupt marker after the call.
366   unsigned char *RetAddrPtr = (unsigned char*)RetAddr;
367   bool isStub = read_x86<unsigned char>(RetAddrPtr) == 0xCE;
368
369   // The call instruction should have pushed the return value onto the stack...
370 #if defined (X86_64_JIT)
371   RetAddrPtr--;  // Backtrack to the reference itself...
372 #else
373   RetAddrPtr -= 4;  // Backtrack to the reference itself...
374 #endif
375
376 #if 0
377   DEBUG(dbgs() << "In callback! Addr=" << RetAddrPtr
378                << " ESP=" << (void*)StackPtr
379                << ": Resolving call to function: "
380                << TheVM->getFunctionReferencedName(RetAddrPtr) << "\n");
381 #endif
382
383   // Sanity check to make sure this really is a call instruction.
384 #if defined (X86_64_JIT)
385   assert(read_x86<unsigned char>(RetAddrPtr - 2) == 0x41 &&
386          "Not a call instr!");
387   assert(read_x86<unsigned char>(RetAddrPtr - 1) == 0xFF &&
388          "Not a call instr!");
389 #else
390   assert(read_x86<unsigned char>(RetAddrPtr - 1) == 0xE8 &&
391          "Not a call instr!");
392 #endif
393
394   intptr_t NewVal = (intptr_t)JITCompilerFunction(RetAddrPtr);
395
396   // Rewrite the call target... so that we don't end up here every time we
397   // execute the call.
398 #if defined (X86_64_JIT)
399   assert(isStub &&
400          "X86-64 doesn't support rewriting non-stub lazy compilation calls:"
401          " the call instruction varies too much.");
402 #else
403   write_x86<intptr_t>(RetAddrPtr, NewVal - (intptr_t)RetAddrPtr - 4);
404 #endif
405
406   if (isStub) {
407     // If this is a stub, rewrite the call into an unconditional branch
408     // instruction so that two return addresses are not pushed onto the stack
409     // when the requested function finally gets called.  This also makes the
410     // 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
411 #if defined (X86_64_JIT)
412     // If the target address is within 32-bit range of the stub, use a
413     // PC-relative branch instead of loading the actual address.  (This is
414     // considerably shorter than the 64-bit immediate load already there.)
415     // We assume here intptr_t is 64 bits.
416     intptr_t diff = NewVal - (intptr_t)RetAddrPtr + 7;
417     if (diff >= -2147483648LL && diff <= 2147483647LL) {
418       write_x86<unsigned char>(RetAddrPtr - 0xC, 0xE9);
419       write_x86<intptr_t>(RetAddrPtr - 0xB, diff & 0xffffffff);
420     } else {
421       write_x86<intptr_t>(RetAddrPtr - 0xA, NewVal);
422       write_x86<unsigned char>(RetAddrPtr, (2 | (4 << 3) | (3 << 6)));
423     }
424     sys::ValgrindDiscardTranslations(RetAddrPtr - 0xC, 0xd);
425 #else
426     write_x86<unsigned char>(RetAddrPtr - 1, 0xE9);
427     sys::ValgrindDiscardTranslations(RetAddrPtr - 1, 5);
428 #endif
429   }
430
431   // Change the return address to reexecute the call instruction...
432 #if defined (X86_64_JIT)
433   *RetAddrLoc -= 0xd;
434 #else
435   *RetAddrLoc -= 5;
436 #endif
437 }
438 }
439
440 TargetJITInfo::LazyResolverFn
441 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
442   TsanIgnoreWritesBegin();
443   JITCompilerFunction = F;
444   TsanIgnoreWritesEnd();
445
446 #if defined (X86_32_JIT) && !defined (_MSC_VER)
447 #if defined(__SSE__)
448   // SSE Callback should be called for SSE-enabled LLVM.
449   return X86CompilationCallback_SSE;
450 #else
451   if (useSSE)
452     return X86CompilationCallback_SSE;
453 #endif
454 #endif
455
456   return X86CompilationCallback;
457 }
458
459 X86JITInfo::X86JITInfo(bool UseSSE) {
460   useSSE = UseSSE;
461   useGOT = 0;
462   TLSOffset = nullptr;
463 }
464
465 void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
466                                              JITCodeEmitter &JCE) {
467 #if defined (X86_64_JIT)
468   const unsigned Alignment = 8;
469   uint8_t Buffer[8];
470   uint8_t *Cur = Buffer;
471   MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(intptr_t)ptr);
472   MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(((intptr_t)ptr) >> 32));
473 #else
474   const unsigned Alignment = 4;
475   uint8_t Buffer[4];
476   uint8_t *Cur = Buffer;
477   MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)ptr);
478 #endif
479   return JCE.allocIndirectGV(GV, Buffer, sizeof(Buffer), Alignment);
480 }
481
482 TargetJITInfo::StubLayout X86JITInfo::getStubLayout() {
483   // The 64-bit stub contains:
484   //   movabs r10 <- 8-byte-target-address  # 10 bytes
485   //   call|jmp *r10  # 3 bytes
486   // The 32-bit stub contains a 5-byte call|jmp.
487   // If the stub is a call to the compilation callback, an extra byte is added
488   // to mark it as a stub.
489   StubLayout Result = {14, 4};
490   return Result;
491 }
492
493 void *X86JITInfo::emitFunctionStub(const Function* F, void *Target,
494                                    JITCodeEmitter &JCE) {
495   // Note, we cast to intptr_t here to silence a -pedantic warning that
496   // complains about casting a function pointer to a normal pointer.
497 #if defined (X86_32_JIT) && !defined (_MSC_VER)
498   bool NotCC = (Target != (void*)(intptr_t)X86CompilationCallback &&
499                 Target != (void*)(intptr_t)X86CompilationCallback_SSE);
500 #else
501   bool NotCC = Target != (void*)(intptr_t)X86CompilationCallback;
502 #endif
503   JCE.emitAlignment(4);
504   void *Result = (void*)JCE.getCurrentPCValue();
505   if (NotCC) {
506 #if defined (X86_64_JIT)
507     JCE.emitByte(0x49);          // REX prefix
508     JCE.emitByte(0xB8+2);        // movabsq r10
509     JCE.emitWordLE((unsigned)(intptr_t)Target);
510     JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
511     JCE.emitByte(0x41);          // REX prefix
512     JCE.emitByte(0xFF);          // jmpq *r10
513     JCE.emitByte(2 | (4 << 3) | (3 << 6));
514 #else
515     JCE.emitByte(0xE9);
516     JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
517 #endif
518     return Result;
519   }
520
521 #if defined (X86_64_JIT)
522   JCE.emitByte(0x49);          // REX prefix
523   JCE.emitByte(0xB8+2);        // movabsq r10
524   JCE.emitWordLE((unsigned)(intptr_t)Target);
525   JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
526   JCE.emitByte(0x41);          // REX prefix
527   JCE.emitByte(0xFF);          // callq *r10
528   JCE.emitByte(2 | (2 << 3) | (3 << 6));
529 #else
530   JCE.emitByte(0xE8);   // Call with 32 bit pc-rel destination...
531
532   JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
533 #endif
534
535   // This used to use 0xCD, but that value is used by JITMemoryManager to
536   // initialize the buffer with garbage, which means it may follow a
537   // noreturn function call, confusing LLVMX86CompilationCallback2.  PR 4929.
538   JCE.emitByte(0xCE);   // Interrupt - Just a marker identifying the stub!
539   return Result;
540 }
541
542 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
543 /// specific basic block.
544 uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
545 #if defined(X86_64_JIT)
546   return BB - Entry;
547 #else
548   return BB - PICBase;
549 #endif
550 }
551
552 template<typename T> static void addUnaligned(void *Pos, T Delta) {
553   T Value;
554   std::memcpy(reinterpret_cast<char*>(&Value), reinterpret_cast<char*>(Pos),
555               sizeof(T));
556   Value += Delta;
557   std::memcpy(reinterpret_cast<char*>(Pos), reinterpret_cast<char*>(&Value),
558               sizeof(T));
559 }
560
561 /// relocate - Before the JIT can run a block of code that has been emitted,
562 /// it must rewrite the code to contain the actual addresses of any
563 /// referenced global symbols.
564 void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
565                           unsigned NumRelocs, unsigned char* GOTBase) {
566   for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
567     void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
568     intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
569     switch ((X86::RelocationType)MR->getRelocationType()) {
570     case X86::reloc_pcrel_word: {
571       // PC relative relocation, add the relocated value to the value already in
572       // memory, after we adjust it for where the PC is.
573       ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
574       addUnaligned<unsigned>(RelocPos, ResultPtr);
575       break;
576     }
577     case X86::reloc_picrel_word: {
578       // PIC base relative relocation, add the relocated value to the value
579       // already in memory, after we adjust it for where the PIC base is.
580       ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
581       addUnaligned<unsigned>(RelocPos, ResultPtr);
582       break;
583     }
584     case X86::reloc_absolute_word:
585     case X86::reloc_absolute_word_sext:
586       // Absolute relocation, just add the relocated value to the value already
587       // in memory.
588       addUnaligned<unsigned>(RelocPos, ResultPtr);
589       break;
590     case X86::reloc_absolute_dword:
591       addUnaligned<intptr_t>(RelocPos, ResultPtr);
592       break;
593     }
594   }
595 }
596
597 char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
598 #if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
599   TLSOffset -= size;
600   return TLSOffset;
601 #else
602   llvm_unreachable("Cannot allocate thread local storage on this arch!");
603 #endif
604 }