1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - rjump.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2002 Hacktarux *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 #include "api/m64p_types.h"
25 #include "api/callbacks.h"
26 #include "r4300/recomp.h"
27 #include "r4300/r4300.h"
28 #include "r4300/macros.h"
29 #include "r4300/ops.h"
30 #include "r4300/recomph.h"
33 # define ASM_NAME(name) asm(name)
35 # define ASM_NAME(name)
38 static long save_ebp ASM_NAME("save_ebp") = 0;
39 static long save_ebx ASM_NAME("save_ebx") = 0;
40 static long save_esi ASM_NAME("save_esi") = 0;
41 static long save_edi ASM_NAME("save_edi") = 0;
42 static long save_esp ASM_NAME("save_esp") = 0;
43 static long save_eip ASM_NAME("save_eip") = 0;
45 // that's where the dynarec will restart when going back from a C function
46 static unsigned long *return_address ASM_NAME("return_address");
56 if (PC->reg_cache_infos.need_map)
57 *return_address = (unsigned long) (PC->reg_cache_infos.jump_wrapper);
59 *return_address = (unsigned long) (actual->code + PC->local_addr);
62 #if defined(WIN32) && !defined(__GNUC__) /* this warning disable only works if placed outside of the scope of a function */
63 #pragma warning(disable:4731) /* frame pointer register 'ebp' modified by inline assembly code */
66 void dyna_start(void *code)
68 /* save the base and stack pointers */
69 /* make a call and a pop to retrieve the instruction pointer and save it too */
70 /* then call the code(), which should theoretically never return. */
71 /* When dyna_stop() sets the *return_address to the saved EIP, the emulator thread will come back here. */
72 /* It will jump to label 2, restore the base and stack pointers, and exit this function */
73 #if defined(WIN32) && !defined(__GNUC__)
89 mov return_address, esp
101 #elif defined(__GNUC__) && defined(__i386__)
103 /* for -fPIC (shared libraries) */
104 #if __GNUC_PREREQ (4, 7)
105 # define GET_PC_THUNK_STR(reg) "__x86.get_pc_thunk." #reg
107 # define GET_PC_THUNK_STR(reg) "__i686.get_pc_thunk." #reg
110 #define LOAD_EBX "call " GET_PC_THUNK_STR(bx) " \n" \
111 "addl $_GLOBAL_OFFSET_TABLE_, %%ebx \n"
113 /* for non-PIC binaries */
114 #define STORE_EBX "movl %%ebx, %[save_ebx] \n"
115 #define LOAD_EBX "movl %[save_ebx], %%ebx \n"
120 " movl %%ebp, %[save_ebp] \n"
121 " movl %%esp, %[save_esp] \n"
122 " movl %%esi, %[save_esi] \n"
123 " movl %%edi, %[save_edi] \n"
128 " movl %%eax, %[save_eip] \n"
130 " subl $16, %%esp \n" /* save 16 bytes of padding just in case */
131 " andl $-16, %%esp \n" /* align stack on 16-byte boundary for OSX */
132 " movl %%esp, %[return_address] \n"
133 " subl $4, %[return_address] \n"
135 " call *%[codeptr] \n"
138 " movl %[save_ebp], %%ebp \n"
139 " movl %[save_esp], %%esp \n"
140 " movl %[save_esi], %%esi \n"
141 " movl %[save_edi], %%edi \n"
142 : [save_ebp]"=m"(save_ebp), [save_esp]"=m"(save_esp), [save_ebx]"=m"(save_ebx), [save_esi]"=m"(save_esi), [save_edi]"=m"(save_edi), [save_eip]"=m"(save_eip), [return_address]"=m"(return_address)
144 : "eax", "ecx", "edx", "memory"
148 /* clear the registers so we don't return here a second time; that would be a bug */
149 /* this is also necessary to prevent compiler from optimizing out the static variables */
161 DebugMessage(M64MSG_WARNING, "instruction pointer is 0 at dyna_stop()");
164 *return_address = (unsigned long) save_eip;