blob: 7ea7e9ea6d56df3a7044f500ba8023337556229d [file] [log] [blame]
Chris Lattner7ddde322004-11-20 23:54:33 +00001//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
Chris Lattner7ddde322004-11-20 23:54:33 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner7ddde322004-11-20 23:54:33 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the JIT interfaces for the X86 target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jit"
15#include "X86JITInfo.h"
16#include "X86Relocations.h"
17#include "llvm/CodeGen/MachineCodeEmitter.h"
18#include "llvm/Config/alloca.h"
Nate Begemanbe136342005-05-20 21:29:24 +000019#include <cstdlib>
20#include <iostream>
Chris Lattner7ddde322004-11-20 23:54:33 +000021using namespace llvm;
22
Chris Lattner38f73732006-01-26 19:55:20 +000023#ifdef _MSC_VER
24 extern "C" void *_AddressOfReturnAddress(void);
25 #pragma intrinsic(_AddressOfReturnAddress)
26#endif
27
Chris Lattner7ddde322004-11-20 23:54:33 +000028void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
29 unsigned char *OldByte = (unsigned char *)Old;
30 *OldByte++ = 0xE9; // Emit JMP opcode.
31 unsigned *OldWord = (unsigned *)OldByte;
32 unsigned NewAddr = (intptr_t)New;
33 unsigned OldAddr = (intptr_t)OldWord;
34 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
35}
36
37
Chris Lattner7ddde322004-11-20 23:54:33 +000038/// JITCompilerFunction - This contains the address of the JIT function used to
39/// compile a function lazily.
40static TargetJITInfo::JITCompilerFn JITCompilerFunction;
41
Chris Lattner1030f722005-05-19 06:49:17 +000042// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
43// callee saved registers, for the fastcc calling convention.
Jeff Cohen8bc6f932005-05-20 01:35:39 +000044extern "C" {
Evan Cheng25ab6902006-09-08 06:48:29 +000045#if defined(__x86_64__)
46 // No need to save EAX/EDX for X86-64.
47 void X86CompilationCallback(void);
48 asm(
49 ".text\n"
50 ".align 8\n"
51 ".globl _X86CompilationCallback\n"
52 "_X86CompilationCallback:\n"
53 // Save RBP
54 "pushq %rbp\n"
55 // Save RSP
56 "movq %rsp, %rbp\n"
57 // Save all int arg registers
58 "pushq %rdi\n"
59 "pushq %rsi\n"
60 "pushq %rdx\n"
61 "pushq %rcx\n"
62 "pushq %r8\n"
63 "pushq %r9\n"
64 // Align stack on 16-byte boundary. ESP might not be properly aligned
65 // (8 byte) if this is called from an indirect stub.
66 "andq $-16, %rsp\n"
67 // Save all XMM arg registers
68 "subq $128, %rsp\n"
69 "movaps %xmm0, (%rsp)\n"
70 "movaps %xmm1, 16(%rsp)\n"
71 "movaps %xmm2, 32(%rsp)\n"
72 "movaps %xmm3, 48(%rsp)\n"
73 "movaps %xmm4, 64(%rsp)\n"
74 "movaps %xmm5, 80(%rsp)\n"
75 "movaps %xmm6, 96(%rsp)\n"
76 "movaps %xmm7, 112(%rsp)\n"
77 // JIT callee
78 "movq %rbp, %rdi\n" // Pass prev frame and return address
79 "movq 8(%rbp), %rsi\n"
80 "call _X86CompilationCallback2\n"
81 // Restore all XMM arg registers
82 "movaps 112(%rsp), %xmm7\n"
83 "movaps 96(%rsp), %xmm6\n"
84 "movaps 80(%rsp), %xmm5\n"
85 "movaps 64(%rsp), %xmm4\n"
86 "movaps 48(%rsp), %xmm3\n"
87 "movaps 32(%rsp), %xmm2\n"
88 "movaps 16(%rsp), %xmm1\n"
89 "movaps (%rsp), %xmm0\n"
90 // Restore RSP
91 "movq %rbp, %rsp\n"
92 // Restore all int arg registers
93 "subq $48, %rsp\n"
94 "popq %r9\n"
95 "popq %r8\n"
96 "popq %rcx\n"
97 "popq %rdx\n"
98 "popq %rsi\n"
99 "popq %rdi\n"
100 // Restore RBP
101 "popq %rbp\n"
102 "ret\n");
103#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
Chris Lattner1030f722005-05-19 06:49:17 +0000104#ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000105 void X86CompilationCallback(void);
106 asm(
107 ".text\n"
108 ".align 8\n"
Jeff Cohen10a59ce2006-04-29 18:41:44 +0000109#if defined(__CYGWIN__) || defined(__APPLE__) || defined(__MINGW32__)
Reid Spencer628214e2005-06-02 21:33:19 +0000110 ".globl _X86CompilationCallback\n"
111 "_X86CompilationCallback:\n"
112#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000113 ".globl X86CompilationCallback\n"
114 "X86CompilationCallback:\n"
Reid Spencer628214e2005-06-02 21:33:19 +0000115#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000116 "pushl %ebp\n"
117 "movl %esp, %ebp\n" // Standard prologue
Evan Chengda08d2c2006-06-24 08:36:10 +0000118#if FASTCC_NUM_INT_ARGS_INREGS > 0
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000119 "pushl %eax\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000120 "pushl %edx\n" // Save EAX/EDX
121#endif
122#if defined(__APPLE__)
123 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
124#endif
Evan Chengbe33dd92006-06-29 01:48:36 +0000125 "subl $16, %esp\n"
126 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
127 "movl %eax, 4(%esp)\n"
128 "movl %ebp, (%esp)\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000129#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__APPLE__)
Nate Begemanadd19dc2006-04-25 20:54:26 +0000130 "call _X86CompilationCallback2\n"
Reid Spencer628214e2005-06-02 21:33:19 +0000131#else
Evan Chengda08d2c2006-06-24 08:36:10 +0000132 "call X86CompilationCallback2\n"
133#endif
Evan Chengda08d2c2006-06-24 08:36:10 +0000134 "movl %ebp, %esp\n" // Restore ESP
Evan Chengda08d2c2006-06-24 08:36:10 +0000135#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengda08d2c2006-06-24 08:36:10 +0000136 "subl $8, %esp\n"
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000137 "popl %edx\n"
138 "popl %eax\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000139#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000140 "popl %ebp\n"
141 "ret\n");
Chris Lattner1030f722005-05-19 06:49:17 +0000142#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000143 void X86CompilationCallback2(void);
144
145 _declspec(naked) void X86CompilationCallback(void) {
146 __asm {
147 push eax
148 push edx
149 call X86CompilationCallback2
150 pop edx
151 pop eax
152 ret
153 }
154 }
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000155#endif // _MSC_VER
Chris Lattner1030f722005-05-19 06:49:17 +0000156
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000157#else // Not an i386 host
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000158 void X86CompilationCallback() {
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000159 std::cerr << "Cannot call X86CompilationCallback() on a non-x86 arch!\n";
Chris Lattner170fbcb2005-05-20 17:00:21 +0000160 abort();
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000161 }
Chris Lattner1030f722005-05-19 06:49:17 +0000162#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000163}
Chris Lattner1030f722005-05-19 06:49:17 +0000164
165/// X86CompilationCallback - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +0000166/// function stub when we did not know the real target of a call. This function
167/// must locate the start of the stub or call site and pass it into the JIT
168/// compiler function.
Chris Lattner7ddde322004-11-20 23:54:33 +0000169#ifdef _MSC_VER
Evan Chengbe33dd92006-06-29 01:48:36 +0000170extern "C" void X86CompilationCallback2() {
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000171 assert(sizeof(size_t) == 4); // FIXME: handle Win64
172 unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
173 RetAddrLoc += 3; // skip over ret addr, edx, eax
174 unsigned RetAddr = *RetAddrLoc;
Chris Lattner7ddde322004-11-20 23:54:33 +0000175#else
Evan Chengbe33dd92006-06-29 01:48:36 +0000176extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
177 intptr_t *RetAddrLoc = &StackPtr[1];
Chris Lattner7ddde322004-11-20 23:54:33 +0000178#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000179 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000180 "Could not find return address on the stack!");
181
182 // It's a stub if there is an interrupt marker after the call.
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
Chris Lattner7ddde322004-11-20 23:54:33 +0000184
185 // The call instruction should have pushed the return value onto the stack...
186 RetAddr -= 4; // Backtrack to the reference itself...
187
188#if 0
189 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
190 << " ESP=" << (void*)StackPtr
191 << ": Resolving call to function: "
192 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
193#endif
194
195 // Sanity check to make sure this really is a call instruction.
Evan Cheng25ab6902006-09-08 06:48:29 +0000196 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000197
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattner7ddde322004-11-20 23:54:33 +0000199
200 // Rewrite the call target... so that we don't end up here every time we
201 // execute the call.
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 *(unsigned *)RetAddr = (unsigned)(NewVal-RetAddr-4);
Chris Lattner7ddde322004-11-20 23:54:33 +0000203
204 if (isStub) {
205 // If this is a stub, rewrite the call into an unconditional branch
206 // instruction so that two return addresses are not pushed onto the stack
207 // when the requested function finally gets called. This also makes the
208 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 ((unsigned char*)RetAddr)[-1] = 0xE9;
Chris Lattner7ddde322004-11-20 23:54:33 +0000210 }
211
212 // Change the return address to reexecute the call instruction...
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000213 *RetAddrLoc -= 5;
Chris Lattner7ddde322004-11-20 23:54:33 +0000214}
215
Chris Lattner7ddde322004-11-20 23:54:33 +0000216TargetJITInfo::LazyResolverFn
217X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
218 JITCompilerFunction = F;
Chris Lattner1030f722005-05-19 06:49:17 +0000219 return X86CompilationCallback;
Chris Lattner7ddde322004-11-20 23:54:33 +0000220}
221
Chris Lattner90b1b452004-11-22 22:25:30 +0000222void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
Chris Lattner078d1822006-06-01 17:13:10 +0000223 // Note, we cast to intptr_t here to silence a -pedantic warning that
224 // complains about casting a function pointer to a normal pointer.
225 if (Fn != (void*)(intptr_t)X86CompilationCallback) {
Chris Lattner90b1b452004-11-22 22:25:30 +0000226 MCE.startFunctionStub(5);
227 MCE.emitByte(0xE9);
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000228 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000229 return MCE.finishFunctionStub(0);
230 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000231
Chris Lattner90b1b452004-11-22 22:25:30 +0000232 MCE.startFunctionStub(6);
233 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
234
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000235 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000236
237 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
238 return MCE.finishFunctionStub(0);
239}
Chris Lattner7ddde322004-11-20 23:54:33 +0000240
241/// relocate - Before the JIT can run a block of code that has been emitted,
242/// it must rewrite the code to contain the actual addresses of any
243/// referenced global symbols.
244void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth908bc862005-07-22 20:49:37 +0000245 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattner7ddde322004-11-20 23:54:33 +0000246 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
247 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
248 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
249 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000250 case X86::reloc_pcrel_word: {
Chris Lattner7ddde322004-11-20 23:54:33 +0000251 // PC relative relocation, add the relocated value to the value already in
252 // memory, after we adjust it for where the PC is.
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 ResultPtr = ResultPtr-(intptr_t)RelocPos-4-MR->getConstantVal();
254 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000255 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 }
Chris Lattner7ddde322004-11-20 23:54:33 +0000257 case X86::reloc_absolute_word:
258 // Absolute relocation, just add the relocated value to the value already
259 // in memory.
Evan Cheng25ab6902006-09-08 06:48:29 +0000260 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000261 break;
262 }
263 }
264}