| 1 |
|
| /* |
| 2 |
|
| * morpher_jit.c - JIT compilation backends for morpher formula expressions |
| 3 |
|
| * |
| 4 |
|
| * Backend priority: |
| 5 |
|
| * 1. TCC (libtcc) - full C compiler, native -O0 speed |
| 6 |
|
| * 2. Hand-rolled x86-64 - walks tinyexpr tree, emits SSE2 |
| 7 |
|
| * 3. NULL (caller falls back to tinyexpr interpretation) |
| 8 |
|
| */ |
| 9 |
|
| |
| 10 |
|
| #include "morpher_jit.h" |
| 11 |
|
| #include "tinyexpr.h" |
| 12 |
|
| #include <stdlib.h> |
| 13 |
|
| #include <string.h> |
| 14 |
|
| #include <stdint.h> |
| 15 |
|
| #include <stdio.h> |
| 16 |
|
| #include <math.h> |
| 17 |
|
| |
| 18 |
|
| /* ================================================================== */ |
| 19 |
|
| /* TCC backend (loaded via dlopen at runtime) */ |
| 20 |
|
| /* ================================================================== */ |
| 21 |
|
| |
| 22 |
|
| #if defined(__unix__) || defined(__APPLE__) |
| 23 |
|
| #include <dlfcn.h> |
| 24 |
|
| #include <unistd.h> |
| 25 |
|
| |
| 26 |
|
| /* TCC API types (from libtcc.h) */ |
| 27 |
|
| typedef struct TCCState TCCState; |
| 28 |
|
| #define TCC_OUTPUT_MEMORY 1 |
| 29 |
|
| #define TCC_RELOCATE_AUTO ((void*)1) |
| 30 |
|
| |
| 31 |
|
| /* Function pointer types for TCC API */ |
| 32 |
|
| typedef TCCState *(*tcc_new_fn)(void); |
| 33 |
|
| typedef void (*tcc_delete_fn)(TCCState *); |
| 34 |
|
| typedef void (*tcc_set_error_func_fn)(TCCState *, void *, void (*)(void *, const char *)); |
| 35 |
|
| typedef int (*tcc_set_output_type_fn)(TCCState *, int); |
| 36 |
|
| typedef int (*tcc_compile_string_fn)(TCCState *, const char *); |
| 37 |
|
| typedef int (*tcc_add_symbol_fn)(TCCState *, const char *, const void *); |
| 38 |
|
| typedef int (*tcc_relocate_fn)(TCCState *, void *); |
| 39 |
|
| typedef void *(*tcc_get_symbol_fn)(TCCState *, const char *); |
| 40 |
|
| typedef void (*tcc_set_lib_path_fn)(TCCState *, const char *); |
| 41 |
|
| |
| 42 |
|
| static struct { |
| 43 |
|
| void *handle; |
| 44 |
|
| int loaded; |
| 45 |
|
| int available; |
| 46 |
|
| tcc_new_fn new; |
| 47 |
|
| tcc_delete_fn delete; |
| 48 |
|
| tcc_set_error_func_fn set_error_func; |
| 49 |
|
| tcc_set_output_type_fn set_output_type; |
| 50 |
|
| tcc_compile_string_fn compile_string; |
| 51 |
|
| tcc_add_symbol_fn add_symbol; |
| 52 |
|
| tcc_relocate_fn relocate; |
| 53 |
|
| tcc_get_symbol_fn get_symbol; |
| 54 |
|
| tcc_set_lib_path_fn set_lib_path; |
| 55 |
|
| } tcc_api = {0}; |
| 56 |
|
| |
| 57 |
6 |
| static void tcc_load(void) { |
| 58 |
6 |
▶ | if (tcc_api.loaded) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tcc_api.loaded) return; |
5 |
1 |
6 |
|
| 59 |
1 |
| tcc_api.loaded = 1; |
| 60 |
|
| |
| 61 |
1 |
| tcc_api.handle = dlopen("libtcc.so", RTLD_LAZY); |
| 62 |
1 |
▶ | if (!tcc_api.handle) tcc_api.handle = dlopen("libtcc.so.0", RTLD_LAZY); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tcc_api.handle) tcc_api.handle = dlopen("libtcc.so.0", RTLD_LAZY); |
1 |
0 |
1 |
|
| 63 |
1 |
▶ | if (!tcc_api.handle) tcc_api.handle = dlopen("libtcc.so.1", RTLD_LAZY); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tcc_api.handle) tcc_api.handle = dlopen("libtcc.so.1", RTLD_LAZY); |
1 |
0 |
1 |
|
| 64 |
1 |
▶ | if (!tcc_api.handle) tcc_api.handle = dlopen("libtcc.dylib", RTLD_LAZY); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tcc_api.handle) tcc_api.handle = dlopen("libtcc.dylib", RTLD_LAZY); |
1 |
0 |
1 |
|
| 65 |
1 |
▶ | if (!tcc_api.handle) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tcc_api.handle) return; |
1 |
0 |
1 |
|
| 66 |
|
| |
| 67 |
0 |
| tcc_api.new = (tcc_new_fn)dlsym(tcc_api.handle, "tcc_new"); |
| 68 |
0 |
| tcc_api.delete = (tcc_delete_fn)dlsym(tcc_api.handle, "tcc_delete"); |
| 69 |
0 |
| tcc_api.set_error_func = (tcc_set_error_func_fn)dlsym(tcc_api.handle, "tcc_set_error_func"); |
| 70 |
0 |
| tcc_api.set_output_type = (tcc_set_output_type_fn)dlsym(tcc_api.handle, "tcc_set_output_type"); |
| 71 |
0 |
| tcc_api.compile_string = (tcc_compile_string_fn)dlsym(tcc_api.handle, "tcc_compile_string"); |
| 72 |
0 |
| tcc_api.add_symbol = (tcc_add_symbol_fn)dlsym(tcc_api.handle, "tcc_add_symbol"); |
| 73 |
0 |
| tcc_api.relocate = (tcc_relocate_fn)dlsym(tcc_api.handle, "tcc_relocate"); |
| 74 |
0 |
| tcc_api.get_symbol = (tcc_get_symbol_fn)dlsym(tcc_api.handle, "tcc_get_symbol"); |
| 75 |
0 |
| tcc_api.set_lib_path = (tcc_set_lib_path_fn)dlsym(tcc_api.handle, "tcc_set_lib_path"); |
| 76 |
|
| |
| 77 |
0 |
▶ | tcc_api.available = tcc_api.new && tcc_api.delete && tcc_api.compile_string |
Branch: 2 branches
| Branch | True | False | Total |
| tcc_api.available = tcc_api.new && tcc_api.delete && tcc_api.compile_string |
0 |
0 |
0 |
| tcc_api.available = tcc_api.new && tcc_api.delete && tcc_api.compile_string |
0 |
0 |
0 |
|
| 78 |
0 |
▶ | && tcc_api.add_symbol && tcc_api.relocate && tcc_api.get_symbol |
Branch: 3 branches
| Branch | True | False | Total |
| && tcc_api.add_symbol && tcc_api.relocate && tcc_api.get_symbol |
0 |
0 |
0 |
| && tcc_api.add_symbol && tcc_api.relocate && tcc_api.get_symbol |
0 |
0 |
0 |
| && tcc_api.add_symbol && tcc_api.relocate && tcc_api.get_symbol |
0 |
0 |
0 |
|
| 79 |
0 |
▶ | && tcc_api.set_output_type; |
Branch: 2 branches
| Branch | True | False | Total |
| && tcc_api.set_output_type; |
0 |
0 |
0 |
| && tcc_api.set_output_type; |
0 |
0 |
0 |
|
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
0 |
| static void tcc_error_cb(void *opaque, const char *msg) { |
| 83 |
|
| (void)opaque; (void)msg; |
| 84 |
0 |
| } |
| 85 |
|
| |
| 86 |
6 |
| static jit_morpher *jit_compile_tcc(const char *formula) { |
| 87 |
|
| TCCState *s; |
| 88 |
|
| jit_morpher *jit; |
| 89 |
|
| char src[2048]; |
| 90 |
|
| int len; |
| 91 |
6 |
| double dummy_t = 0; |
| 92 |
6 |
| te_variable vars[] = {{"t", &dummy_t, TE_VARIABLE, NULL}}; |
| 93 |
|
| int err; |
| 94 |
|
| te_expr *validation; |
| 95 |
|
| |
| 96 |
6 |
| tcc_load(); |
| 97 |
6 |
▶ | if (!tcc_api.available) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tcc_api.available) return NULL; |
6 |
0 |
6 |
|
| 98 |
|
| |
| 99 |
|
| /* Validate formula through tinyexpr first to prevent code injection. |
| 100 |
|
| * TCC compiles arbitrary C, so we must ensure the formula is a pure |
| 101 |
|
| * math expression before embedding it in generated source code. */ |
| 102 |
0 |
| validation = te_compile(formula, vars, 1, &err); |
| 103 |
0 |
▶ | if (!validation) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!validation) return NULL; |
0 |
0 |
0 |
|
| 104 |
0 |
| te_free(validation); |
| 105 |
|
| |
| 106 |
0 |
| len = snprintf(src, sizeof(src), |
| 107 |
|
| "double sin(double); double cos(double); double tan(double);\n" |
| 108 |
|
| "double asin(double); double acos(double); double atan(double);\n" |
| 109 |
|
| "double atan2(double,double);\n" |
| 110 |
|
| "double sinh(double); double cosh(double); double tanh(double);\n" |
| 111 |
|
| "double exp(double); double log(double); double log10(double); double log2(double);\n" |
| 112 |
|
| "double pow(double,double); double sqrt(double); double fabs(double);\n" |
| 113 |
|
| "double ceil(double); double floor(double); double fmod(double,double);\n" |
| 114 |
|
| "double round(double); double cbrt(double); double hypot(double,double);\n" |
| 115 |
|
| "double morpher(double t) { return (%s); }\n", |
| 116 |
|
| formula); |
| 117 |
0 |
▶ | if (len < 0 || (size_t)len >= sizeof(src)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len < 0 || (size_t)len >= sizeof(src)) |
0 |
0 |
0 |
| if (len < 0 || (size_t)len >= sizeof(src)) |
0 |
0 |
0 |
|
| 118 |
0 |
| return NULL; |
| 119 |
|
| |
| 120 |
0 |
| s = tcc_api.new(); |
| 121 |
0 |
▶ | if (!s) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return NULL; |
0 |
0 |
0 |
|
| 122 |
|
| |
| 123 |
0 |
▶ | if (tcc_api.set_error_func) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tcc_api.set_error_func) |
0 |
0 |
0 |
|
| 124 |
0 |
| tcc_api.set_error_func(s, NULL, tcc_error_cb); |
| 125 |
|
| |
| 126 |
|
| /* Probe for libtcc1.a in common locations */ |
| 127 |
0 |
▶ | if (tcc_api.set_lib_path) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tcc_api.set_lib_path) { |
0 |
0 |
0 |
|
| 128 |
|
| static const char *probe_paths[] = { |
| 129 |
|
| "/usr/lib/x86_64-linux-gnu/tcc", |
| 130 |
|
| "/usr/lib/tcc", |
| 131 |
|
| "/usr/local/lib/tcc", |
| 132 |
|
| "/usr/lib64/tcc", |
| 133 |
|
| NULL |
| 134 |
|
| }; |
| 135 |
|
| int i; |
| 136 |
0 |
▶ | for (i = 0; probe_paths[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; probe_paths[i]; i++) { |
0 |
0 |
0 |
|
| 137 |
|
| char buf[256]; |
| 138 |
0 |
| snprintf(buf, sizeof(buf), "%s/libtcc1.a", probe_paths[i]); |
| 139 |
0 |
▶ | if (access(buf, R_OK) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (access(buf, R_OK) == 0) { |
0 |
0 |
0 |
|
| 140 |
0 |
| tcc_api.set_lib_path(s, probe_paths[i]); |
| 141 |
0 |
| break; |
| 142 |
|
| } |
| 143 |
|
| } |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
0 |
| tcc_api.set_output_type(s, TCC_OUTPUT_MEMORY); |
| 147 |
|
| |
| 148 |
|
| /* Provide math symbols so TCC doesn't need libc headers */ |
| 149 |
0 |
| tcc_api.add_symbol(s, "sin", (const void *)sin); |
| 150 |
0 |
| tcc_api.add_symbol(s, "cos", (const void *)cos); |
| 151 |
0 |
| tcc_api.add_symbol(s, "tan", (const void *)tan); |
| 152 |
0 |
| tcc_api.add_symbol(s, "asin", (const void *)asin); |
| 153 |
0 |
| tcc_api.add_symbol(s, "acos", (const void *)acos); |
| 154 |
0 |
| tcc_api.add_symbol(s, "atan", (const void *)atan); |
| 155 |
0 |
| tcc_api.add_symbol(s, "atan2", (const void *)atan2); |
| 156 |
0 |
| tcc_api.add_symbol(s, "sinh", (const void *)sinh); |
| 157 |
0 |
| tcc_api.add_symbol(s, "cosh", (const void *)cosh); |
| 158 |
0 |
| tcc_api.add_symbol(s, "tanh", (const void *)tanh); |
| 159 |
0 |
| tcc_api.add_symbol(s, "exp", (const void *)exp); |
| 160 |
0 |
| tcc_api.add_symbol(s, "log", (const void *)log); |
| 161 |
0 |
| tcc_api.add_symbol(s, "log10", (const void *)log10); |
| 162 |
0 |
| tcc_api.add_symbol(s, "log2", (const void *)log2); |
| 163 |
0 |
| tcc_api.add_symbol(s, "pow", (const void *)pow); |
| 164 |
0 |
| tcc_api.add_symbol(s, "sqrt", (const void *)sqrt); |
| 165 |
0 |
| tcc_api.add_symbol(s, "fabs", (const void *)fabs); |
| 166 |
0 |
| tcc_api.add_symbol(s, "ceil", (const void *)ceil); |
| 167 |
0 |
| tcc_api.add_symbol(s, "floor", (const void *)floor); |
| 168 |
0 |
| tcc_api.add_symbol(s, "fmod", (const void *)fmod); |
| 169 |
0 |
| tcc_api.add_symbol(s, "round", (const void *)round); |
| 170 |
0 |
| tcc_api.add_symbol(s, "cbrt", (const void *)cbrt); |
| 171 |
0 |
| tcc_api.add_symbol(s, "hypot", (const void *)hypot); |
| 172 |
|
| |
| 173 |
0 |
▶ | if (tcc_api.compile_string(s, src) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tcc_api.compile_string(s, src) == -1) { |
0 |
0 |
0 |
|
| 174 |
0 |
| tcc_api.delete(s); |
| 175 |
0 |
| return NULL; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
0 |
▶ | if (tcc_api.relocate(s, TCC_RELOCATE_AUTO) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tcc_api.relocate(s, TCC_RELOCATE_AUTO) == -1) { |
0 |
0 |
0 |
|
| 179 |
0 |
| tcc_api.delete(s); |
| 180 |
0 |
| return NULL; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
0 |
| jit = (jit_morpher *)malloc(sizeof(jit_morpher)); |
| 184 |
0 |
▶ | if (!jit) { tcc_api.delete(s); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!jit) { tcc_api.delete(s); return NULL; } |
0 |
0 |
0 |
|
| 185 |
|
| |
| 186 |
0 |
| jit->fn = (jit_morpher_fn)(uintptr_t)tcc_api.get_symbol(s, "morpher"); |
| 187 |
0 |
▶ | if (!jit->fn) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!jit->fn) { |
0 |
0 |
0 |
|
| 188 |
0 |
| tcc_api.delete(s); |
| 189 |
0 |
| free(jit); |
| 190 |
0 |
| return NULL; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
0 |
| jit->backend = JIT_BACKEND_TCC; |
| 194 |
0 |
| jit->state = s; |
| 195 |
0 |
| jit->state_size = 0; |
| 196 |
0 |
| return jit; |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
|
| #endif /* unix/apple */ |
| 200 |
|
| |
| 201 |
|
| /* ================================================================== */ |
| 202 |
|
| /* x86-64 hand-rolled JIT backend */ |
| 203 |
|
| /* ================================================================== */ |
| 204 |
|
| |
| 205 |
|
| #if defined(__x86_64__) || defined(_M_X64) |
| 206 |
|
| |
| 207 |
|
| #include <sys/mman.h> |
| 208 |
|
| #include <stdint.h> |
| 209 |
|
| |
| 210 |
|
| #ifndef MAP_ANONYMOUS |
| 211 |
|
| #define MAP_ANONYMOUS MAP_ANON |
| 212 |
|
| #endif |
| 213 |
|
| |
| 214 |
|
| #define JIT_PAGE 4096 |
| 215 |
|
| |
| 216 |
|
| typedef struct { |
| 217 |
|
| uint8_t *buf; |
| 218 |
|
| size_t pos; |
| 219 |
|
| size_t cap; |
| 220 |
|
| int stack_depth; |
| 221 |
|
| int ok; |
| 222 |
|
| } jit_ctx; |
| 223 |
|
| |
| 224 |
346 |
| static void jb(jit_ctx *j, uint8_t b) { |
| 225 |
346 |
▶ | if (j->pos < j->cap) j->buf[j->pos] = b; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j->pos < j->cap) j->buf[j->pos] = b; |
346 |
0 |
346 |
|
| 226 |
0 |
| else j->ok = 0; |
| 227 |
346 |
| j->pos++; |
| 228 |
346 |
| } |
| 229 |
|
| |
| 230 |
65 |
| static void jbs(jit_ctx *j, const uint8_t *b, int n) { |
| 231 |
|
| int i; |
| 232 |
349 |
▶ | for (i = 0; i < n; i++) jb(j, b[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) jb(j, b[i]); |
284 |
65 |
349 |
|
| 233 |
65 |
| } |
| 234 |
|
| |
| 235 |
8 |
| static void ju32(jit_ctx *j, uint32_t v) { |
| 236 |
8 |
| jb(j,v); jb(j,v>>8); jb(j,v>>16); jb(j,v>>24); |
| 237 |
8 |
| } |
| 238 |
|
| |
| 239 |
4 |
| static void ju64(jit_ctx *j, uint64_t v) { |
| 240 |
4 |
| ju32(j,(uint32_t)v); ju32(j,(uint32_t)(v>>32)); |
| 241 |
4 |
| } |
| 242 |
|
| |
| 243 |
|
| /* mov rax, imm64 */ |
| 244 |
4 |
| static void j_mov_rax(jit_ctx *j, uint64_t val) { |
| 245 |
4 |
| jb(j,0x48); jb(j,0xb8); ju64(j,val); |
| 246 |
4 |
| } |
| 247 |
|
| |
| 248 |
|
| /* movq xmm0, rax */ |
| 249 |
2 |
| static void j_movq_xmm0_rax(jit_ctx *j) { |
| 250 |
2 |
| static const uint8_t c[]={0x66,0x48,0x0f,0x6e,0xc0}; jbs(j,c,5); |
| 251 |
2 |
| } |
| 252 |
|
| |
| 253 |
|
| /* Load double constant into xmm0 */ |
| 254 |
2 |
| static void j_load_const(jit_ctx *j, double val) { |
| 255 |
|
| uint64_t bits; |
| 256 |
2 |
▶ | if (val == 0.0 && !signbit(val)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (val == 0.0 && !signbit(val)) { |
0 |
2 |
2 |
| if (val == 0.0 && !signbit(val)) { |
0 |
0 |
0 |
|
| 257 |
0 |
| static const uint8_t c[]={0x66,0x0f,0x57,0xc0}; jbs(j,c,4); /* xorpd xmm0,xmm0 */ |
| 258 |
0 |
| return; |
| 259 |
|
| } |
| 260 |
2 |
| memcpy(&bits, &val, 8); |
| 261 |
2 |
| j_mov_rax(j, bits); |
| 262 |
2 |
| j_movq_xmm0_rax(j); |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| /* movsd xmm0, [rbp-8] */ |
| 266 |
10 |
| static void j_load_t(jit_ctx *j) { |
| 267 |
10 |
| static const uint8_t c[]={0xf2,0x0f,0x10,0x45,0xf8}; jbs(j,c,5); |
| 268 |
10 |
| } |
| 269 |
|
| |
| 270 |
|
| /* push xmm0 to stack */ |
| 271 |
6 |
| static void j_push_xmm0(jit_ctx *j) { |
| 272 |
6 |
| static const uint8_t s[]={0x48,0x83,0xec,0x08}; jbs(j,s,4); |
| 273 |
6 |
| static const uint8_t m[]={0xf2,0x0f,0x11,0x04,0x24}; jbs(j,m,5); |
| 274 |
6 |
| j->stack_depth += 8; |
| 275 |
6 |
| } |
| 276 |
|
| |
| 277 |
|
| /* pop stack to xmm0 */ |
| 278 |
6 |
| static void j_pop_xmm0(jit_ctx *j) { |
| 279 |
6 |
| static const uint8_t m[]={0xf2,0x0f,0x10,0x04,0x24}; jbs(j,m,5); |
| 280 |
6 |
| static const uint8_t a[]={0x48,0x83,0xc4,0x08}; jbs(j,a,4); |
| 281 |
6 |
| j->stack_depth -= 8; |
| 282 |
6 |
| } |
| 283 |
|
| |
| 284 |
|
| /* movsd xmm1, xmm0 */ |
| 285 |
6 |
| static void j_xmm1_from_xmm0(jit_ctx *j) { |
| 286 |
6 |
| static const uint8_t c[]={0xf2,0x0f,0x10,0xc8}; jbs(j,c,4); |
| 287 |
6 |
| } |
| 288 |
|
| |
| 289 |
0 |
| static void j_addsd(jit_ctx *j) { static const uint8_t c[]={0xf2,0x0f,0x58,0xc1}; jbs(j,c,4); } |
| 290 |
0 |
| static void j_subsd(jit_ctx *j) { static const uint8_t c[]={0xf2,0x0f,0x5c,0xc1}; jbs(j,c,4); } |
| 291 |
5 |
| static void j_mulsd(jit_ctx *j) { static const uint8_t c[]={0xf2,0x0f,0x59,0xc1}; jbs(j,c,4); } |
| 292 |
0 |
| static void j_divsd(jit_ctx *j) { static const uint8_t c[]={0xf2,0x0f,0x5e,0xc1}; jbs(j,c,4); } |
| 293 |
|
| |
| 294 |
|
| /* Negate xmm0 via sign-bit flip */ |
| 295 |
0 |
| static void j_negate(jit_ctx *j) { |
| 296 |
0 |
| j_mov_rax(j, 0x8000000000000000ULL); |
| 297 |
0 |
| { static const uint8_t c[]={0x66,0x48,0x0f,0x6e,0xc8}; jbs(j,c,5); } /* movq xmm1,rax */ |
| 298 |
0 |
| { static const uint8_t c[]={0x66,0x0f,0x57,0xc1}; jbs(j,c,4); } /* xorpd xmm0,xmm1 */ |
| 299 |
0 |
| } |
| 300 |
|
| |
| 301 |
|
| /* Call function with 16-byte stack alignment */ |
| 302 |
2 |
| static void j_call(jit_ctx *j, const void *fn) { |
| 303 |
2 |
| int pad = (j->stack_depth % 16) != 0; |
| 304 |
2 |
▶ | if (pad) { static const uint8_t s[]={0x48,0x83,0xec,0x08}; jbs(j,s,4); j->stack_depth+=8; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad) { static const uint8_t s[]={0x48,0x83,0xec,0x08}; jbs(j,s,4); j->stack_depth+=8; } |
0 |
2 |
2 |
|
| 305 |
2 |
| j_mov_rax(j, (uint64_t)(uintptr_t)fn); |
| 306 |
2 |
| jb(j, 0xff); jb(j, 0xd0); |
| 307 |
2 |
▶ | if (pad) { static const uint8_t a[]={0x48,0x83,0xc4,0x08}; jbs(j,a,4); j->stack_depth-=8; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad) { static const uint8_t a[]={0x48,0x83,0xc4,0x08}; jbs(j,a,4); j->stack_depth-=8; } |
0 |
2 |
2 |
|
| 308 |
2 |
| } |
| 309 |
|
| |
| 310 |
|
| /* Recursive tree walker */ |
| 311 |
19 |
| static void j_emit(jit_ctx *j, const te_expr *n) { |
| 312 |
|
| int type, arity, op; |
| 313 |
19 |
▶ | if (!j->ok || !n) { j->ok = 0; return; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!j->ok || !n) { j->ok = 0; return; } |
19 |
0 |
19 |
| if (!j->ok || !n) { j->ok = 0; return; } |
0 |
19 |
19 |
|
| 314 |
|
| |
| 315 |
19 |
| type = n->type & 0x1f; |
| 316 |
19 |
▶ | arity = (type & (8|16)) ? (type & 7) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| arity = (type & (8|16)) ? (type & 7) : 0; |
7 |
12 |
19 |
|
| 317 |
|
| |
| 318 |
19 |
▶ | if (type == 1) { j_load_const(j, n->value); return; } /* TE_CONSTANT */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (type == 1) { j_load_const(j, n->value); return; } /* TE_CONSTANT */ |
2 |
17 |
19 |
|
| 319 |
17 |
▶ | if (type == 0) { j_load_t(j); return; } /* TE_VARIABLE */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (type == 0) { j_load_t(j); return; } /* TE_VARIABLE */ |
10 |
7 |
17 |
|
| 320 |
|
| |
| 321 |
7 |
▶ | if (arity == 0) { j_call(j, n->function); return; } /* TE_FUNCTION0 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (arity == 0) { j_call(j, n->function); return; } /* TE_FUNCTION0 */ |
0 |
7 |
7 |
|
| 322 |
|
| |
| 323 |
7 |
▶ | if (arity == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (arity == 1) { |
1 |
6 |
7 |
|
| 324 |
1 |
| op = te_identify_fn(n->function); |
| 325 |
1 |
| j_emit(j, n->parameters[0]); |
| 326 |
1 |
▶ | if (!j->ok) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!j->ok) return; |
0 |
1 |
1 |
|
| 327 |
1 |
▶ | if (op == TE_OP_NEGATE) j_negate(j); |
Branch: 1 branch
| Branch | True | False | Total |
| if (op == TE_OP_NEGATE) j_negate(j); |
0 |
1 |
1 |
|
| 328 |
1 |
| else j_call(j, n->function); |
| 329 |
1 |
| return; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
6 |
▶ | if (arity == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (arity == 2) { |
6 |
0 |
6 |
|
| 333 |
6 |
| op = te_identify_fn(n->function); |
| 334 |
6 |
| j_emit(j, n->parameters[0]); |
| 335 |
6 |
▶ | if (!j->ok) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!j->ok) return; |
0 |
6 |
6 |
|
| 336 |
6 |
| j_push_xmm0(j); |
| 337 |
6 |
| j_emit(j, n->parameters[1]); |
| 338 |
6 |
▶ | if (!j->ok) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!j->ok) return; |
0 |
6 |
6 |
|
| 339 |
6 |
| j_xmm1_from_xmm0(j); |
| 340 |
6 |
| j_pop_xmm0(j); |
| 341 |
6 |
| switch (op) { |
| 342 |
0 |
| case TE_OP_ADD: j_addsd(j); break; |
| 343 |
0 |
| case TE_OP_SUB: j_subsd(j); break; |
| 344 |
5 |
| case TE_OP_MUL: j_mulsd(j); break; |
| 345 |
0 |
| case TE_OP_DIV: j_divsd(j); break; |
| 346 |
1 |
| default: j_call(j, n->function); break; |
| 347 |
|
| } |
| 348 |
6 |
| return; |
| 349 |
|
| } |
| 350 |
|
| |
| 351 |
0 |
| j->ok = 0; /* arity 3+ unsupported */ |
| 352 |
|
| } |
| 353 |
|
| |
| 354 |
6 |
| static jit_morpher *jit_compile_x86(const char *formula) { |
| 355 |
6 |
| double dummy_t = 0; |
| 356 |
6 |
| te_variable vars[] = {{"t", &dummy_t, TE_VARIABLE, NULL}}; |
| 357 |
|
| int err; |
| 358 |
|
| te_expr *expr; |
| 359 |
|
| jit_ctx j; |
| 360 |
|
| void *code; |
| 361 |
|
| jit_morpher *jit; |
| 362 |
|
| |
| 363 |
6 |
| expr = te_compile(formula, vars, 1, &err); |
| 364 |
6 |
▶ | if (!expr) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!expr) return NULL; |
0 |
6 |
6 |
|
| 365 |
|
| |
| 366 |
6 |
| code = mmap(NULL, JIT_PAGE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 367 |
6 |
▶ | if (code == MAP_FAILED) { te_free(expr); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (code == MAP_FAILED) { te_free(expr); return NULL; } |
0 |
6 |
6 |
|
| 368 |
|
| |
| 369 |
6 |
| j.buf = (uint8_t *)code; |
| 370 |
6 |
| j.pos = 0; |
| 371 |
6 |
| j.cap = JIT_PAGE; |
| 372 |
6 |
| j.stack_depth = 0; |
| 373 |
6 |
| j.ok = 1; |
| 374 |
|
| |
| 375 |
|
| /* prologue */ |
| 376 |
6 |
| jb(&j, 0x55); /* push rbp */ |
| 377 |
6 |
| { static const uint8_t c[]={0x48,0x89,0xe5}; jbs(&j,c,3); } /* mov rbp,rsp */ |
| 378 |
6 |
| { static const uint8_t c[]={0x48,0x83,0xec,0x10}; jbs(&j,c,4); } /* sub rsp,16 */ |
| 379 |
6 |
| { static const uint8_t c[]={0xf2,0x0f,0x11,0x45,0xf8};jbs(&j,c,5); } /* movsd [rbp-8],xmm0 */ |
| 380 |
|
| |
| 381 |
6 |
| j_emit(&j, expr); |
| 382 |
|
| |
| 383 |
|
| /* epilogue */ |
| 384 |
6 |
| jb(&j, 0xc9); /* leave */ |
| 385 |
6 |
| jb(&j, 0xc3); /* ret */ |
| 386 |
|
| |
| 387 |
6 |
| te_free(expr); |
| 388 |
|
| |
| 389 |
6 |
▶ | if (!j.ok || j.pos > JIT_PAGE) { munmap(code, JIT_PAGE); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!j.ok || j.pos > JIT_PAGE) { munmap(code, JIT_PAGE); return NULL; } |
6 |
0 |
6 |
| if (!j.ok || j.pos > JIT_PAGE) { munmap(code, JIT_PAGE); return NULL; } |
0 |
6 |
6 |
|
| 390 |
6 |
▶ | if (mprotect(code, JIT_PAGE, PROT_READ|PROT_EXEC) != 0) { munmap(code, JIT_PAGE); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (mprotect(code, JIT_PAGE, PROT_READ|PROT_EXEC) != 0) { munmap(code, JIT_PAGE); return NULL; } |
0 |
6 |
6 |
|
| 391 |
|
| |
| 392 |
6 |
| jit = (jit_morpher *)malloc(sizeof(jit_morpher)); |
| 393 |
6 |
▶ | if (!jit) { munmap(code, JIT_PAGE); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!jit) { munmap(code, JIT_PAGE); return NULL; } |
0 |
6 |
6 |
|
| 394 |
|
| |
| 395 |
6 |
| jit->fn = (jit_morpher_fn)code; |
| 396 |
6 |
| jit->backend = JIT_BACKEND_X86; |
| 397 |
6 |
| jit->state = code; |
| 398 |
6 |
| jit->state_size = JIT_PAGE; |
| 399 |
6 |
| return jit; |
| 400 |
|
| } |
| 401 |
|
| |
| 402 |
|
| #endif /* x86-64 */ |
| 403 |
|
| |
| 404 |
|
| /* ================================================================== */ |
| 405 |
|
| /* Public API */ |
| 406 |
|
| /* ================================================================== */ |
| 407 |
|
| |
| 408 |
6 |
| jit_morpher *jit_morpher_compile(const char *formula) { |
| 409 |
6 |
| jit_morpher *jit = NULL; |
| 410 |
|
| |
| 411 |
|
| #if defined(__unix__) || defined(__APPLE__) |
| 412 |
6 |
| jit = jit_compile_tcc(formula); |
| 413 |
6 |
▶ | if (jit) return jit; |
Branch: 1 branch
| Branch | True | False | Total |
| if (jit) return jit; |
0 |
6 |
6 |
|
| 414 |
|
| #endif |
| 415 |
|
| |
| 416 |
|
| #if defined(__x86_64__) || defined(_M_X64) |
| 417 |
6 |
| jit = jit_compile_x86(formula); |
| 418 |
6 |
▶ | if (jit) return jit; |
Branch: 1 branch
| Branch | True | False | Total |
| if (jit) return jit; |
6 |
0 |
6 |
|
| 419 |
|
| #endif |
| 420 |
|
| |
| 421 |
|
| (void)formula; |
| 422 |
0 |
| return NULL; |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
272 |
| void jit_morpher_free(jit_morpher *jit) { |
| 426 |
272 |
▶ | if (!jit) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!jit) return; |
266 |
6 |
272 |
|
| 427 |
6 |
| switch (jit->backend) { |
| 428 |
0 |
| case JIT_BACKEND_TCC: |
| 429 |
|
| #if defined(__unix__) || defined(__APPLE__) |
| 430 |
0 |
▶ | if (jit->state && tcc_api.available) |
Branch: 2 branches
| Branch | True | False | Total |
| if (jit->state && tcc_api.available) |
0 |
0 |
0 |
| if (jit->state && tcc_api.available) |
0 |
0 |
0 |
|
| 431 |
0 |
| tcc_api.delete((TCCState *)jit->state); |
| 432 |
|
| #endif |
| 433 |
0 |
| break; |
| 434 |
|
| #if defined(__x86_64__) || defined(_M_X64) |
| 435 |
6 |
| case JIT_BACKEND_X86: |
| 436 |
6 |
▶ | if (jit->state) munmap(jit->state, jit->state_size); |
Branch: 1 branch
| Branch | True | False | Total |
| if (jit->state) munmap(jit->state, jit->state_size); |
6 |
0 |
6 |
|
| 437 |
6 |
| break; |
| 438 |
|
| #endif |
| 439 |
0 |
| default: |
| 440 |
0 |
| break; |
| 441 |
|
| } |
| 442 |
6 |
| free(jit); |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
1 |
| const char *jit_backend_name(const jit_morpher *jit) { |
| 446 |
1 |
▶ | if (!jit) return "none"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!jit) return "none"; |
0 |
1 |
1 |
|
| 447 |
1 |
| switch (jit->backend) { |
| 448 |
0 |
| case JIT_BACKEND_TCC: return "tcc"; |
| 449 |
1 |
| case JIT_BACKEND_X86: return "x86"; |
| 450 |
0 |
| default: return "none"; |
| 451 |
|
| } |
| 452 |
|
| } |