-
Notifications
You must be signed in to change notification settings - Fork 3
/
savestate.hpp
150 lines (141 loc) · 4.37 KB
/
savestate.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef CR_SAVESTATE_HPP
# define CR_SAVESTATE_HPP
# pragma once
namespace gnr
{
struct statebuf
{
void* sp, *label;
};
}
#if defined(__GNUC__)
# if defined(i386) || defined(__i386) || defined(__i386__)
# define clobber_all() asm volatile ("":::"eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc");
# elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
# define clobber_all() asm volatile ("":::"rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "cc");
# elif defined(__arm__)
# define clobber_all() asm volatile ("":::"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "lr", "cc");
# elif defined(__aarch64__)
# define clobber_all() asm volatile ("":::"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29", "x30", "cc");
# else
# error "unsupported architecture"
# endif
#else
# error "unsupported compiler"
#endif
#if defined(__GNUC__)
static inline bool __attribute__((always_inline)) savestate(
gnr::statebuf& ssb) noexcept
{
bool r;
# if defined(i386) || defined(__i386) || defined(__i386__)
asm (
"movl %%esp, %0\n\t" // store sp
"lea 1f, %%eax\n\t" // load label
"movl %%eax, %1\n\t" // store label
"movb $0, %2\n\t" // return false
"jmp 2f\n\t"
"1:"
"movb $1, %2\n\t" // return true
"2:"
: "=m" (ssb.sp), "=m" (ssb.label), "=r" (r)
:
: "eax", "memory"
);
# elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) ||\
defined(__x86_64)
asm (
"movq %%rsp, %0\n\t" // store sp
"lea 1f(%%rip), %%rax\n\t" // load label
"movq %%rax, %1\n\t" // store label
"movb $0, %2\n\t" // return false
"jmp 2f\n\t"
"1:"
"movb $1, %2\n\t" // return true
"2:"
: "=m" (ssb.sp), "=m" (ssb.label), "=r" (r)
:
: "rax", "memory"
);
# elif defined(__arm__)
asm (
"str sp, %0\n\t" // store sp
"ldr r0, =1f\n\t" // load label
"str r0, %1\n\t" // store label
"mov %2, $0\n\t" // store 0 into result
"b 2f\n\t"
"1:"
"mov %2, $1\n\t" // store 1 into result
"2:"
: "=m" (ssb.sp), "=m" (ssb.label), "=r" (r)
:
: "r0", "memory"
);
# elif defined(__aarch64__)
asm (
"mov x0, sp\n\t"
"str x0, %0\n\t" // store sp
"ldr x0, =1f\n\t" // load label
"str x0, %1\n\t" // store label
"mov %w2, #0\n\t" // store 0 into result
"b 2f\n\t"
"1:"
"mov %w2, #1\n\t" // store 1 into result
"2:"
: "=m" (ssb.sp), "=m" (ssb.label), "=r" (r)
:
: "x0", "memory"
);
# else
# error "unsupported architecture"
# endif
return r;
}
#else
# error "unsupported compiler"
#endif
#if defined(__GNUC__)
# if defined(i386) || defined(__i386) || defined(__i386__)
# define restorestate(SSB) \
asm ( \
"movl %0, %%esp\n\t" \
"jmp *%1" \
: \
: "m" (SSB.sp), "m" (SSB.label) \
); \
__builtin_unreachable();
# elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) ||\
defined(__x86_64)
# define restorestate(SSB) \
asm ( \
"movq %0, %%rsp\n\t" \
"jmp *%1" \
: \
: "m" (SSB.sp), "m" (SSB.label)\
); \
__builtin_unreachable();
# elif defined(__arm__)
# define restorestate(SSB) \
asm ( \
"mov sp, %0\n\t" \
"mov pc, %1" \
: \
: "r" (SSB.sp), "r" (SSB.label)\
); \
__builtin_unreachable();
# elif defined(__aarch64__)
# define restorestate(SSB) \
asm ( \
"mov sp, %0\n\t" \
"ret %1" \
: \
: "r" (SSB.sp), "r" (SSB.label)\
); \
__builtin_unreachable();
# else
# error "unsupported architecture"
# endif
#else
# error "unsupported compiler"
#endif
#endif // CR_SAVESTATE_HPP