-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathexpr_jit.dasc
366 lines (341 loc) · 8.94 KB
/
expr_jit.dasc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// clang-format off
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include "dynasm/dasm_proto.h"
#if !JIT
#error "JIT must be nonzero to use this header"
#endif
| .arch x64
#include "dynasm/dasm_x86.h"
#define JIT 1
struct expr;
static float (*expr_compile(struct expr *e, size_t *sz))();
static void expr_jit_release(struct expr *e);
#include "expr.h"
static int expr_compile_dynasm(struct expr *e, dasm_State **Dst);
static void expr_jit_release(struct expr *e) {
if (e->fn != NULL && e->jitsz > 0) {
munmap((void *) (uintptr_t) e->fn, e->jitsz);
e->fn = NULL;
e->jitsz = 0;
}
}
static float (*expr_compile(struct expr *e, size_t *sz))() {
int dasm_status;
size_t codesize;
dasm_State* d;
dasm_State** Dst = &d;
void* mem = NULL;
expr_jit_fn_t fn = NULL;
| .actionlist actions
| .globals glob
| .section code, imports
void *globals[glob_MAX];
dasm_init(&d, DASM_MAXSECTION);
dasm_setupglobal(&d, globals, glob_MAX);
dasm_setup(&d, actions);
| push rbp
| mov rbp, rsp
if (expr_compile_dynasm(e, &d) != 0) {
dasm_free(&d);
return NULL;
}
| mov rsp, rbp
| pop rbp
| ret
dasm_status = dasm_link(&d, &codesize);
if (dasm_status != DASM_S_OK) {
dasm_free(&d);
return NULL;
}
mem = mmap(NULL, codesize, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
fprintf(stderr, "mmap(): %d\n", errno);
dasm_free(&d);
return NULL;
}
dasm_encode(&d, mem);
dasm_free(&d);
if (mprotect(mem, codesize, PROT_EXEC | PROT_READ) != 0) {
fprintf(stderr, "mprotect(): %d\n", errno);
return NULL;
}
*(void**)(&fn) = mem;
if (sz != NULL) {
*sz = codesize;
}
return fn;
}
static int expr_compile_dynasm(struct expr *e, dasm_State **Dst) {
|.define ONE, 0x3f800000
|.macro CALL_EXTERN_WITH_LABEL, target, label
| .imports
| ->label:
| .dword (uint32_t)(uintptr_t)target
| .dword ((uint64_t)(uintptr_t)target >> 32)
| .code
| call qword [->label]
|.endmacro
|.macro CALL_EXTERN, target
| CALL_EXTERN_WITH_LABEL target, __imp__..target
|.endmacro
|.macro MOVIMM, reg, value
| push value
| movss reg, dword [rsp]
| add rsp, aword*1
|.endmacro
|.macro COMPARE, J1, J2
| ucomiss xmm1, xmm0
| J1 >1
| J2 >2
|1:
| xorps xmm0, xmm0
| jmp >3
|2:
| MOVIMM xmm0, ONE
|3:
|.endmacro
|.macro PUSH_XMM0
| sub rsp, 8
| movss dword [rsp], xmm0
|.endmacro
|.macro POP_XMM0
| movss xmm1, dword [rsp]
| add rsp, 8
|.endmacro
switch (e->type) {
case OP_UNARY_MINUS:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| xorps xmm1, xmm1
| subss xmm1, xmm0
| movss xmm0, xmm1
break;
case OP_UNARY_LOGICAL_NOT:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| xorps xmm1, xmm1
| COMPARE jp, je
break;
case OP_UNARY_BITWISE_NOT:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| cvtss2si eax, xmm0
| not eax
| cvtsi2ss xmm0, eax
break;
case OP_POWER:
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| POP_XMM0
| CALL_EXTERN powf
break;
case OP_MULTIPLY:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| mulss xmm0, xmm1
break;
case OP_DIVIDE:
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| POP_XMM0
| divss xmm0, xmm1
break;
case OP_REMAINDER:
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| POP_XMM0
| CALL_EXTERN fmodf
break;
case OP_PLUS:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| addss xmm0, xmm1
break;
case OP_MINUS:
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| POP_XMM0
| subss xmm0, xmm1
break;
case OP_SHL:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| CALL_EXTERN to_int
| push rax
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| CALL_EXTERN to_int
| mov ecx, eax
| pop rax
| shl eax, cl
| cvtsi2ss xmm0, eax
break;
case OP_SHR:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| CALL_EXTERN to_int
| push rax
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| CALL_EXTERN to_int
| mov ecx, eax
| pop rax
| shr eax, cl
| cvtsi2ss xmm0, eax
break;
case OP_LT:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| cmpss xmm1, xmm0, 1
| MOVIMM xmm0, ONE
| andps xmm0, xmm1
break;
case OP_LE:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| cmpss xmm1, xmm0, 2
| MOVIMM xmm0, ONE
| andps xmm0, xmm1
break;
case OP_GT:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| cmpss xmm0, xmm1, 1
| MOVIMM xmm1, ONE
| andps xmm0, xmm1
break;
case OP_GE:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| cmpss xmm0, xmm1, 2
| MOVIMM xmm1, ONE
| andps xmm0, xmm1
break;
case OP_EQ:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| COMPARE jp, je
break;
case OP_NE:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| PUSH_XMM0
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| POP_XMM0
| COMPARE jp, jne
break;
case OP_BITWISE_AND:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| CALL_EXTERN to_int
| push rax
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| CALL_EXTERN to_int
| mov ecx, eax
| pop rax
| and eax, ecx
| cvtsi2ss xmm0, eax
break;
case OP_BITWISE_OR:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| CALL_EXTERN to_int
| push rax
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| CALL_EXTERN to_int
| mov ecx, eax
| pop rax
| or eax, ecx
| cvtsi2ss xmm0, eax
break;
case OP_BITWISE_XOR:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| CALL_EXTERN to_int
| push rax
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| CALL_EXTERN to_int
| mov ecx, eax
| pop rax
| xor eax, ecx
| cvtsi2ss xmm0, eax
break;
case OP_LOGICAL_AND:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| xorps xmm2, xmm2
| ucomiss xmm0, xmm2
| jz >1
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| ucomiss xmm0, xmm2
| jnz >2
|1:
| xorps xmm0, xmm0
|2:
break;
case OP_LOGICAL_OR:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
| ucomiss xmm0, xmm0
| xorps xmm2, xmm2
| jp >1
| ucomiss xmm0, xmm2
| mov dl, 1
| setp al
| cmovne eax, edx
| test al, al
| jne >3
|1:
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
| movss xmm1, xmm0
| ucomiss xmm1, xmm2
| xorps xmm0, xmm0
| jp >2
| ucomiss xmm1, xmm0
| je >3
|2:
| movaps xmm0, xmm1
|3:
break;
case OP_ASSIGN:
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
if (vec_nth(&e->param.op.args, 0).type == OP_VAR) {
| mov64 rax, (uint64_t) e->param.op.args.buf[0].param.var.value
| movss dword [rax], xmm0
}
break;
case OP_COMMA:
expr_compile_dynasm(&e->param.op.args.buf[0], Dst);
expr_compile_dynasm(&e->param.op.args.buf[1], Dst);
break;
case OP_CONST:
| mov64 rax, (uint64_t) &e->param.num.value
| movss xmm0, dword [rax]
break;
case OP_VAR:
| mov64 rax, (uint64_t) (uintptr_t) e->param.var.value
| movss xmm0, dword [rax]
break;
case OP_FUNC:
| mov64 rdi, (uint64_t) e->param.func.f
| mov64 rsi, (uint64_t) &e->param.func.args
| mov64 rdx, (uint64_t) e->param.func.context
| mov64 rax, (uintptr_t) e->param.func.f->f
| call rax
break;
default:
return -1;
}
return 0;
}