ALL: Huge upstream synch + PerRom DelaySI & CountPerOp parameters
[mupen64plus-pandora.git] / source / mupen64plus-core / src / r4300 / x86 / rjump.c
CommitLineData
451ab91e 1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - rjump.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2002 Hacktarux *
5 * *
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. *
10 * *
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. *
15 * *
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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
22#include <stdlib.h>
23
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"
31
32 #ifdef __GNUC__
33# define ASM_NAME(name) asm(name)
34#else
35# define ASM_NAME(name)
36#endif
37
38static long save_ebp ASM_NAME("save_ebp") = 0;
39static long save_ebx ASM_NAME("save_ebx") = 0;
40static long save_esi ASM_NAME("save_esi") = 0;
41static long save_edi ASM_NAME("save_edi") = 0;
42static long save_esp ASM_NAME("save_esp") = 0;
43static long save_eip ASM_NAME("save_eip") = 0;
44
45// that's where the dynarec will restart when going back from a C function
46static unsigned long *return_address ASM_NAME("return_address");
47
48void dyna_jump()
49{
50 if (stop == 1)
51 {
52 dyna_stop();
53 return;
54 }
55
56 if (PC->reg_cache_infos.need_map)
57 *return_address = (unsigned long) (PC->reg_cache_infos.jump_wrapper);
58 else
59 *return_address = (unsigned long) (actual->code + PC->local_addr);
60}
61
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 */
64#endif
65
66void dyna_start(void *code)
67{
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__)
74 __asm
75 {
76 mov save_ebp, ebp
77 mov save_esp, esp
78 mov save_ebx, ebx
79 mov save_esi, esi
80 mov save_edi, edi
81 call point1
82 jmp point2
83 point1:
84 pop eax
85 mov save_eip, eax
86
87 sub esp, 0x10
88 and esp, 0xfffffff0
89 mov return_address, esp
90 sub return_address, 4
91
92 mov eax, code
93 call eax
94 point2:
95 mov ebp, save_ebp
96 mov esp, save_esp
97 mov ebx, save_ebx
98 mov esi, save_esi
99 mov edi, save_edi
100 }
101#elif defined(__GNUC__) && defined(__i386__)
102 #if defined(__PIC__)
103 /* for -fPIC (shared libraries) */
104 #if __GNUC_PREREQ (4, 7)
105 # define GET_PC_THUNK_STR(reg) "__x86.get_pc_thunk." #reg
106 #else
107 # define GET_PC_THUNK_STR(reg) "__i686.get_pc_thunk." #reg
108 #endif
109 #define STORE_EBX
110 #define LOAD_EBX "call " GET_PC_THUNK_STR(bx) " \n" \
111 "addl $_GLOBAL_OFFSET_TABLE_, %%ebx \n"
112 #else
113 /* for non-PIC binaries */
114 #define STORE_EBX "movl %%ebx, %[save_ebx] \n"
115 #define LOAD_EBX "movl %[save_ebx], %%ebx \n"
116 #endif
117
118 asm volatile
119 (STORE_EBX
120 " movl %%ebp, %[save_ebp] \n"
121 " movl %%esp, %[save_esp] \n"
122 " movl %%esi, %[save_esi] \n"
123 " movl %%edi, %[save_edi] \n"
124 " call 1f \n"
125 " jmp 2f \n"
126 "1: \n"
127 " popl %%eax \n"
128 " movl %%eax, %[save_eip] \n"
129
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"
134
135 " call *%[codeptr] \n"
136 "2: \n"
137 LOAD_EBX
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)
143 : [codeptr]"r"(code)
144 : "eax", "ecx", "edx", "memory"
145 );
146#endif
147
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 */
150 save_edi=0;
151 save_esi=0;
152 save_ebx=0;
153 save_ebp=0;
154 save_esp=0;
155 save_eip=0;
156}
157
158void dyna_stop()
159{
160 if (save_eip == 0)
161 DebugMessage(M64MSG_WARNING, "instruction pointer is 0 at dyna_stop()");
162 else
163 {
164 *return_address = (unsigned long) save_eip;
165 }
166}
167