-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallstack.cpp
150 lines (135 loc) · 3.31 KB
/
callstack.cpp
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 EMSCRIPTEN
#include "callstack.h"
#ifdef __GNUC__
#pragma GCC optimize ("O0")
#endif
#ifdef _MSC_VER
#pragma optimize( "", off )
#endif
void callstack::begin(std::function<void()> fn)
{
m_main = fn;
if (!m_main)
{
m_main = [](){};
}
m_state = CS_SUSPENDED;
// run _begin, which will do all of the following:
// - set stack pointers to the member stack.
// - store jump buffer which will execute m_main when longjmp'd to
// - longjmp back here and return.
if(!setjmp(m_env_external))
{
_begin();
}
}
bool callstack::resume()
{
verify_state_on_resume();
m_state = CS_ACTIVE;
switch (setjmp(m_env_external))
{
case CS_TERMINATE:
{
m_state = CS_INACTIVE;
return false;
}
case CS_YIELD:
{
m_state = CS_SUSPENDED;
return true;
}
case CS_CATCH:
{
m_state = CS_ERROR;
return false;
}
default:
{
// current context is saved in setjmp,
// so switch to its context.
longjmp(m_env_internal, CS_RESUME);
}
}
}
void callstack::yield()
{
verify_state_on_yield();
if (setjmp(m_env_internal) == 0)
{
// return to external
longjmp(m_env_external, CS_YIELD);
}
}
_CALLSTACK_H_NOINLINE
void callstack::_begin() noexcept
{
static callstack* volatile s_cs{ this };
// step 1: set stack pointer registers to secondary stack ------------------------
// macros: see https://sourceforge.net/p/predef/wiki/Architectures/
#if defined(__GNUC__) || defined(__clang__)
#if defined(__i386__)
#define PEGGML_ARCH_DEFINED
// x86
asm volatile(
"movl %0, %%esp\n\t"
"movl %%esp, %%ebp"
: /* No outputs. */
: "rm" (m_stack_base)
);
#elif defined(__x86_64__)
#define PEGGML_ARCH_DEFINED
// x86_64
asm volatile(
"movq %0, %%rsp\n\t"
"movq %%rsp, %%rbp"
: /* No outputs. */
: "rm" (m_stack_base)
);
#endif
#elif defined(_MSC_VER)
#if defined(_M_IX86)
#define PEGGML_ARCH_DEFINED
// MSVC doesn't support X64 inline asm
__asm{
mov m_stack_base, esp
mov esp, ebp
};
#endif
#endif
#ifndef PEGGML_ARCH_DEFINED
#error "compiler or architecture not supported. Consider adding support -- it's just two lines of asm."
#else
#undef PEGGML_ARCH_DEFINED
// step 2: begin function execution on secondary stack ------------------------
s_cs->__begin();
#endif
}
_CALLSTACK_H_NOINLINE
void callstack::__begin()
{
if (setjmp(m_env_internal) == 0)
{
longjmp(m_env_external, 1);
}
else
{
bool error = false;
try
{
m_main();
}
catch (const std::exception& e)
{
error = true;
m_error_what = e.what();
}
catch (...)
{
error = true;
m_error_what = "(unknown exception type)";
}
longjmp(m_env_external, error ? CS_CATCH : CS_TERMINATE);
}
}
#endif