1 /*
2 +----------------------------------------------------------------------+
3 | Zend JIT |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | https://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Dmitry Stogov <dmitry@php.net> |
16 | Xinchen Hui <laruence@php.net> |
17 | Hao Sun <hao.sun@arm.com> |
18 +----------------------------------------------------------------------+
19 */
20
21 #ifndef ZEND_JIT_INTERNAL_H
22 #define ZEND_JIT_INTERNAL_H
23
24 /* Address Encoding */
25 typedef uintptr_t zend_jit_addr;
26
27 #define IS_CONST_ZVAL 0
28 #define IS_MEM_ZVAL 1
29 #define IS_REG 2 /* CPU register or PHP SSA variable number (for IR JIT) */
30 #define IS_REF_ZVAL 3 /* IR reference */
31
32 #define _ZEND_ADDR_MODE_MASK 0x3
33 #define _ZEND_ADDR_REG_SHIFT 2
34 #define _ZEND_ADDR_REG_MASK 0x3f /* no more than 64 registers */
35 #define _ZEND_ADDR_OFFSET_SHIFT 8
36
37 #define ZEND_ADDR_CONST_ZVAL(zv) \
38 (((zend_jit_addr)(uintptr_t)(zv)) | IS_CONST_ZVAL)
39 #define ZEND_ADDR_MEM_ZVAL(reg, offset) \
40 ((((zend_jit_addr)(uintptr_t)(offset)) << _ZEND_ADDR_OFFSET_SHIFT) | \
41 (((zend_jit_addr)(uintptr_t)(reg)) << _ZEND_ADDR_REG_SHIFT) | \
42 IS_MEM_ZVAL)
43 #define ZEND_ADDR_REG(reg) \
44 ((((zend_jit_addr)(uintptr_t)(reg)) << _ZEND_ADDR_REG_SHIFT) | \
45 IS_REG)
46
47 #define Z_MODE(addr) (((addr) & _ZEND_ADDR_MODE_MASK))
48 #define Z_ZV(addr) ((zval*)(addr))
49 #define Z_OFFSET(addr) ((uint32_t)((addr)>>_ZEND_ADDR_OFFSET_SHIFT))
50 #define Z_REG(addr) ((zend_reg)(((addr)>>_ZEND_ADDR_REG_SHIFT) & _ZEND_ADDR_REG_MASK))
51
52 #define ZEND_ADDR_REF_ZVAL(ref) \
53 ((((zend_jit_addr)(uintptr_t)(ref)) << _ZEND_ADDR_REG_SHIFT) | \
54 IS_REF_ZVAL)
55
56 #define Z_SSA_VAR(addr) ((addr)>>_ZEND_ADDR_REG_SHIFT)
57 #define Z_IR_REF(addr) ((addr)>>_ZEND_ADDR_REG_SHIFT)
58
59 #define Z_STORE(addr) \
60 ((jit->ra && jit->ra[Z_SSA_VAR(addr)].ref) ? \
61 (jit->ra[Z_SSA_VAR(addr)].flags & ZREG_STORE) : \
62 0)
63 #define Z_LOAD(addr) \
64 ((jit->ra && jit->ra[Z_SSA_VAR(addr)].ref) ? \
65 (jit->ra[Z_SSA_VAR(addr)].flags & ZREG_LOAD) : \
66 0)
67
68 #if ZEND_USE_ABS_CONST_ADDR
69 # define OP_ADDR(opline, type, op) \
70 (((opline)->type == IS_CONST) ? \
71 ZEND_ADDR_CONST_ZVAL((opline)->op.zv) : \
72 ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline)->op.var))
73 #else
74 # define OP_ADDR(opline, type, op) \
75 (((opline)->type == IS_CONST) ? \
76 ZEND_ADDR_CONST_ZVAL(RT_CONSTANT(opline, (opline)->op)) : \
77 ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline)->op.var))
78 #endif
79
80 #define OP_REG_ADDR(opline, ssa_op, type, op, _ssa_op) \
81 ((ctx.ra && (ssa_op)->_ssa_op >= 0 && ctx.ra[(ssa_op)->_ssa_op].ref) ? \
82 ZEND_ADDR_REG((ssa_op)->_ssa_op) : \
83 OP_ADDR(opline, type, op))
84
85 #define OP1_ADDR() \
86 OP_ADDR(opline, op1_type, op1)
87 #define OP2_ADDR() \
88 OP_ADDR(opline, op2_type, op2)
89 #define RES_ADDR() \
90 OP_ADDR(opline, result_type, result)
91 #define OP1_DATA_ADDR() \
92 OP_ADDR(opline + 1, op1_type, op1)
93
94 #define OP1_REG_ADDR() \
95 OP_REG_ADDR(opline, ssa_op, op1_type, op1, op1_use)
96 #define OP2_REG_ADDR() \
97 OP_REG_ADDR(opline, ssa_op, op2_type, op2, op2_use)
98 #define RES_REG_ADDR() \
99 OP_REG_ADDR(opline, ssa_op, result_type, result, result_def)
100 #define OP1_DATA_REG_ADDR() \
101 OP_REG_ADDR(opline + 1, ssa_op + 1, op1_type, op1, op1_use)
102
103 #define OP1_DEF_REG_ADDR() \
104 OP_REG_ADDR(opline, ssa_op, op1_type, op1, op1_def)
105 #define OP2_DEF_REG_ADDR() \
106 OP_REG_ADDR(opline, ssa_op, op2_type, op2, op2_def)
107 #define RES_USE_REG_ADDR() \
108 OP_REG_ADDR(opline, ssa_op, result_type, result, result_use)
109 #define OP1_DATA_DEF_REG_ADDR() \
110 OP_REG_ADDR(opline + 1, ssa_op + 1, op1_type, op1, op1_def)
111
zend_jit_same_addr(zend_jit_addr addr1,zend_jit_addr addr2)112 static zend_always_inline bool zend_jit_same_addr(zend_jit_addr addr1, zend_jit_addr addr2)
113 {
114 if (addr1 == addr2) {
115 return 1;
116 } else if (Z_MODE(addr1) == IS_REG && Z_MODE(addr2) == IS_REG) {
117 return Z_SSA_VAR(addr1) == Z_SSA_VAR(addr2);
118 } else if (Z_MODE(addr1) == IS_REF_ZVAL && Z_MODE(addr2) == IS_REF_ZVAL) {
119 return Z_IR_REF(addr1) == Z_IR_REF(addr2);
120 }
121 return 0;
122 }
123
124 typedef struct _zend_jit_op_array_extension {
125 zend_func_info func_info;
126 const zend_op_array *op_array;
127 const void *orig_handler;
128 } zend_jit_op_array_extension;
129
130 /* Profiler */
131 extern zend_ulong zend_jit_profile_counter;
132 extern int zend_jit_profile_counter_rid;
133
134 #define ZEND_COUNTER_INFO(op_array) \
135 ZEND_OP_ARRAY_EXTENSION(op_array, zend_jit_profile_counter_rid)
136
137 /* Hot Counters */
138
139 #define ZEND_HOT_COUNTERS_COUNT 128
140
141 extern int16_t zend_jit_hot_counters[ZEND_HOT_COUNTERS_COUNT];
142
zend_jit_hash(const void * ptr)143 static zend_always_inline zend_long zend_jit_hash(const void *ptr)
144 {
145 uintptr_t x;
146
147 x = (uintptr_t)ptr >> 3;
148 #if SIZEOF_SIZE_T == 4
149 x = ((x >> 16) ^ x) * 0x45d9f3b;
150 x = ((x >> 16) ^ x) * 0x45d9f3b;
151 x = (x >> 16) ^ x;
152 #elif SIZEOF_SIZE_T == 8
153 x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
154 x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
155 x = x ^ (x >> 31);
156 #endif
157 return x;
158 }
159
160 void ZEND_FASTCALL zend_jit_hot_func(zend_execute_data *execute_data, const zend_op *opline);
161
162 typedef struct _zend_jit_op_array_hot_extension {
163 zend_func_info func_info;
164 const zend_op_array *op_array;
165 int16_t *counter;
166 const void *orig_handlers[1];
167 } zend_jit_op_array_hot_extension;
168
169 #define zend_jit_op_array_hash(op_array) \
170 zend_jit_hash((op_array)->opcodes)
171
172 extern const zend_op *zend_jit_halt_op;
173
174 #ifdef HAVE_GCC_GLOBAL_REGS
175 # define EXECUTE_DATA_D void
176 # define EXECUTE_DATA_C
177 # define EXECUTE_DATA_DC
178 # define EXECUTE_DATA_CC
179 # define OPLINE_D void
180 # define OPLINE_C
181 # define OPLINE_DC
182 # define OPLINE_CC
183 # define ZEND_OPCODE_HANDLER_RET void
184 # define ZEND_OPCODE_HANDLER_ARGS EXECUTE_DATA_D
185 # define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU
186 # define ZEND_OPCODE_HANDLER_ARGS_DC
187 # define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC
188 # define ZEND_OPCODE_RETURN() return
189 # define ZEND_OPCODE_TAIL_CALL(handler) do { \
190 handler(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU); \
191 return; \
192 } while(0)
193 # define ZEND_OPCODE_TAIL_CALL_EX(handler, arg) do { \
194 handler(arg ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC); \
195 return; \
196 } while(0)
197 #else
198 # define EXECUTE_DATA_D zend_execute_data* execute_data
199 # define EXECUTE_DATA_C execute_data
200 # define EXECUTE_DATA_DC , EXECUTE_DATA_D
201 # define EXECUTE_DATA_CC , EXECUTE_DATA_C
202 # define OPLINE_D const zend_op* opline
203 # define OPLINE_C opline
204 # define OPLINE_DC , OPLINE_D
205 # define OPLINE_CC , OPLINE_C
206 # define ZEND_OPCODE_HANDLER_RET int
207 # define ZEND_OPCODE_HANDLER_ARGS EXECUTE_DATA_D
208 # define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU EXECUTE_DATA_C
209 # define ZEND_OPCODE_HANDLER_ARGS_DC EXECUTE_DATA_DC
210 # define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC EXECUTE_DATA_CC
211 # define ZEND_OPCODE_RETURN() return 0
212 # define ZEND_OPCODE_TAIL_CALL(handler) do { \
213 return handler(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU); \
214 } while(0)
215 # define ZEND_OPCODE_TAIL_CALL_EX(handler, arg) do { \
216 return handler(arg ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC); \
217 } while(0)
218 #endif
219
220 /* VM handlers */
221 typedef ZEND_OPCODE_HANDLER_RET (ZEND_FASTCALL *zend_vm_opcode_handler_t)(ZEND_OPCODE_HANDLER_ARGS);
222
223 /* VM helpers */
224 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_leave_nested_func_helper(uint32_t call_info EXECUTE_DATA_DC);
225 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_leave_top_func_helper(uint32_t call_info EXECUTE_DATA_DC);
226 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_leave_func_helper(EXECUTE_DATA_D);
227
228 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_profile_helper(ZEND_OPCODE_HANDLER_ARGS);
229
230 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_func_counter_helper(ZEND_OPCODE_HANDLER_ARGS);
231 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_loop_counter_helper(ZEND_OPCODE_HANDLER_ARGS);
232
233 void ZEND_FASTCALL zend_jit_copy_extra_args_helper(EXECUTE_DATA_D);
234 bool ZEND_FASTCALL zend_jit_deprecated_helper(OPLINE_D);
235 void ZEND_FASTCALL zend_jit_undefined_long_key(EXECUTE_DATA_D);
236 void ZEND_FASTCALL zend_jit_undefined_long_key_ex(zend_long key EXECUTE_DATA_DC);
237 void ZEND_FASTCALL zend_jit_undefined_string_key(EXECUTE_DATA_D);
238
239 zend_constant* ZEND_FASTCALL zend_jit_get_constant(const zval *key, uint32_t flags);
240 zend_constant* ZEND_FASTCALL zend_jit_check_constant(const zval *key);
241
242 /* Tracer */
243 #define zend_jit_opline_hash(opline) \
244 zend_jit_hash(opline)
245
246 #define ZEND_JIT_TRACE_STOP(_) \
247 _(LOOP, "loop") \
248 _(RECURSIVE_CALL, "recursive call") \
249 _(RECURSIVE_RET, "recursive return") \
250 _(RETURN, "return") \
251 _(LINK, "link to another trace") \
252 _(INTERPRETER, "exit to VM interpreter") \
253 _(TRAMPOLINE, "trampoline call") \
254 _(PROP_HOOK_CALL, "property hook call") \
255 _(BAD_FUNC, "bad function call") \
256 /* compilation and linking successful */ \
257 _(COMPILED, "compiled") \
258 _(ALREADY_DONE, "already prcessed") \
259 /* failures */ \
260 _(ERROR, "error") /* not used */ \
261 _(NOT_SUPPORTED, "not supported instructions") \
262 _(EXCEPTION, "exception") \
263 _(TOO_LONG, "trace too long") \
264 _(TOO_DEEP, "trace too deep") \
265 _(TOO_DEEP_RET, "trace too deep return") \
266 _(DEEP_RECURSION, "deep recursion") \
267 _(LOOP_UNROLL, "loop unroll limit reached") \
268 _(LOOP_EXIT, "exit from loop") \
269 _(RECURSION_EXIT, "return from recursive function") \
270 _(BLACK_LIST, "trace blacklisted") \
271 _(INNER_LOOP, "inner loop") /* trace it */ \
272 _(COMPILED_LOOP, "compiled loop") \
273 _(COMPILER_ERROR, "JIT compilation error") \
274 /* no recoverable error (blacklist immediately) */ \
275 _(NO_SHM, "insufficient shared memory") \
276 _(TOO_MANY_TRACES, "too many traces") \
277 _(TOO_MANY_CHILDREN, "too many side traces") \
278 _(TOO_MANY_EXITS, "too many side exits") \
279
280 #define ZEND_JIT_TRACE_STOP_NAME(name, description) \
281 ZEND_JIT_TRACE_STOP_ ## name,
282
283 typedef enum _zend_jit_trace_stop {
284 ZEND_JIT_TRACE_STOP(ZEND_JIT_TRACE_STOP_NAME)
285 ZEND_JIT_TRACE_HALT = 0x40
286 } zend_jit_trace_stop;
287
288 #define ZEND_JIT_TRACE_STOP_OK(ret) \
289 (ret < ZEND_JIT_TRACE_STOP_COMPILED)
290
291 #define ZEND_JIT_TRACE_STOP_DONE(ret) \
292 (ret < ZEND_JIT_TRACE_STOP_ERROR)
293
294 #define ZEND_JIT_TRACE_STOP_REPEAT(ret) \
295 (ret == ZEND_JIT_TRACE_STOP_INNER_LOOP)
296
297 #define ZEND_JIT_TRACE_STOP_MAY_RECOVER(ret) \
298 (ret <= ZEND_JIT_TRACE_STOP_COMPILER_ERROR)
299
300 #define ZEND_JIT_TRACE_START_MASK 0xf
301
302 #define ZEND_JIT_TRACE_START_LOOP (1<<0)
303 #define ZEND_JIT_TRACE_START_ENTER (1<<1)
304 #define ZEND_JIT_TRACE_START_RETURN (1<<2)
305 #define ZEND_JIT_TRACE_START_SIDE (1<<3) /* used for side traces */
306
307 #define ZEND_JIT_TRACE_JITED (1<<4)
308 #define ZEND_JIT_TRACE_BLACKLISTED (1<<5)
309 #define ZEND_JIT_TRACE_UNSUPPORTED (1<<6)
310
311 #define ZEND_JIT_TRACE_SUPPORTED 0
312
313 #define ZEND_JIT_EXIT_JITED (1<<0)
314 #define ZEND_JIT_EXIT_BLACKLISTED (1<<1)
315 #define ZEND_JIT_EXIT_TO_VM (1<<2) /* exit to VM without attempt to create a side trace */
316 #define ZEND_JIT_EXIT_RESTORE_CALL (1<<3) /* deoptimizer should restore EX(call) chain */
317 #define ZEND_JIT_EXIT_POLYMORPHISM (1<<4) /* exit because of polymorphic call */
318 #define ZEND_JIT_EXIT_FREE_OP1 (1<<5)
319 #define ZEND_JIT_EXIT_FREE_OP2 (1<<6)
320 #define ZEND_JIT_EXIT_PACKED_GUARD (1<<7)
321 #define ZEND_JIT_EXIT_CLOSURE_CALL (1<<8) /* exit because of polymorphic INIT_DYNAMIC_CALL call */
322 #define ZEND_JIT_EXIT_METHOD_CALL (1<<9) /* exit because of polymorphic INIT_METHOD_CALL call */
323 #define ZEND_JIT_EXIT_INVALIDATE (1<<10) /* invalidate current trace */
324
325 #define ZEND_JIT_EXIT_FIXED (1U<<31) /* the exit_info can't be changed by zend_jit_snapshot_handler() */
326
327 typedef union _zend_op_trace_info {
328 zend_op dummy; /* the size of this structure must be the same as zend_op */
329 struct {
330 const void *orig_handler;
331 const void *call_handler;
332 int16_t *counter;
333 uint8_t trace_flags;
334 };
335 } zend_op_trace_info;
336
337 typedef struct _zend_jit_op_array_trace_extension {
338 zend_func_info func_info;
339 const zend_op_array *op_array;
340 size_t offset; /* offset from "zend_op" to corresponding "op_info" */
341 zend_op_trace_info trace_info[1];
342 } zend_jit_op_array_trace_extension;
343
344 #define ZEND_OP_TRACE_INFO(opline, offset) \
345 ((zend_op_trace_info*)(((char*)opline) + offset))
346
347 /* Recorder */
348 typedef enum _zend_jit_trace_op {
349 ZEND_JIT_TRACE_VM,
350 ZEND_JIT_TRACE_OP1_TYPE,
351 ZEND_JIT_TRACE_OP2_TYPE,
352 ZEND_JIT_TRACE_VAL_INFO,
353 ZEND_JIT_TRACE_INIT_CALL,
354 ZEND_JIT_TRACE_DO_ICALL,
355 ZEND_JIT_TRACE_ENTER,
356 ZEND_JIT_TRACE_BACK,
357 ZEND_JIT_TRACE_END,
358 ZEND_JIT_TRACE_START,
359 } zend_jit_trace_op;
360
361 #define IS_UNKNOWN 255 /* may be used for zend_jit_trace_rec.op?_type */
362 #define IS_TRACE_PACKED (1<<4)
363 #define IS_TRACE_REFERENCE (1<<5)
364 #define IS_TRACE_INDIRECT (1<<6)
365
366 #define IS_TRACE_TYPE_MASK 0xf
367
368 #define ZEND_JIT_TRACE_FAKE_INIT_CALL 0x00000100
369 #define ZEND_JIT_TRACE_RETURN_VALUE_USED 0x00000100
370
371 #define ZEND_JIT_TRACE_MAX_SSA_VAR 0x7ffffe
372 #define ZEND_JIT_TRACE_SSA_VAR_SHIFT 9
373
374 #define ZEND_JIT_TRACE_FAKE_LEVEL_MASK 0xffff0000
375 #define ZEND_JIT_TRACE_FAKE_LEVEL_SHIFT 16
376
377 #define ZEND_JIT_TRACE_FAKE_LEVEL(info) \
378 (((info) & ZEND_JIT_TRACE_FAKE_LEVEL_MASK) >> ZEND_JIT_TRACE_FAKE_LEVEL_SHIFT)
379
380 #define ZEND_JIT_TRACE_FAKE_INFO(level) \
381 (((level) << ZEND_JIT_TRACE_FAKE_LEVEL_SHIFT) | ZEND_JIT_TRACE_FAKE_INIT_CALL)
382
383 #define ZEND_JIT_TRACE_NUM_ARGS_INFO(count) \
384 ((count) << ZEND_JIT_TRACE_FAKE_LEVEL_SHIFT)
385
386 #define ZEND_JIT_TRACE_NUM_ARGS(info) \
387 (((info) & ZEND_JIT_TRACE_FAKE_LEVEL_MASK) >> ZEND_JIT_TRACE_FAKE_LEVEL_SHIFT)
388
389 #define ZEND_JIT_TRACE_SET_FIRST_SSA_VAR(_info, var) do { \
390 _info |= (var << ZEND_JIT_TRACE_SSA_VAR_SHIFT); \
391 } while (0)
392 #define ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(_info) \
393 (_info >> ZEND_JIT_TRACE_SSA_VAR_SHIFT)
394
395 struct _zend_jit_trace_rec {
396 union {
397 struct { ZEND_ENDIAN_LOHI(
398 uint8_t op, /* zend_jit_trace_op */
399 union {
400 struct {
401 uint8_t op1_type;/* recorded zval op1_type for ZEND_JIT_TRACE_VM */
402 uint8_t op2_type;/* recorded zval op2_type for ZEND_JIT_TRACE_VM */
403 uint8_t op3_type;/* recorded zval for op_data.op1_type for ZEND_JIT_TRACE_VM */
404 };
405 struct {
406 uint8_t start; /* ZEND_JIT_TRACE_START_MASK for ZEND_JIT_TRACE_START/END */
407 uint8_t stop; /* zend_jit_trace_stop for ZEND_JIT_TRACE_START/END */
408 uint8_t level; /* recursive return level for ZEND_JIT_TRACE_START */
409 };
410 })
411 };
412 uint32_t last;
413 uint32_t info; /* "first_ssa_var" for ZEND_JIT_TRACE_ENTER and ZEND_JIT_TRACE_BACK,
414 * "return_value_used" for ZEND_JIT_TRACE_ENTER,
415 * "fake" for ZEND_JIT_TRACE_INIT_CALL */
416 };
417 union {
418 const void *ptr;
419 const zend_function *func;
420 const zend_op_array *op_array;
421 const zend_op *opline;
422 const zend_class_entry *ce;
423 };
424 };
425
426 #define ZEND_JIT_TRACE_START_REC_SIZE 2
427
428 typedef struct _zend_jit_trace_exit_info {
429 const zend_op *opline; /* opline where VM should continue execution */
430 const zend_op_array *op_array;
431 uint32_t flags; /* set of ZEND_JIT_EXIT_... */
432 uint32_t stack_size;
433 uint32_t stack_offset;
434 int32_t poly_func_ref;
435 int32_t poly_this_ref;
436 int8_t poly_func_reg;
437 int8_t poly_this_reg;
438 } zend_jit_trace_exit_info;
439
440 typedef struct _zend_jit_trace_stack {
441 union {
442 uint32_t info;
443 struct {
444 uint8_t type; /* variable type (for type inference) */
445 uint8_t mem_type; /* stack slot type (for eliminate dead type store) */
446 int8_t reg;
447 uint8_t flags;
448 };
449 };
450 int32_t ref;
451 } zend_jit_trace_stack;
452
453 #define STACK_VAR(_stack, _slot) \
454 ((int32_t*)(_stack))[_slot]
455 #define SET_STACK_VAR(_stack, _slot, _ssa_var) do { \
456 ((int32_t*)(_stack))[_slot] = _ssa_var; \
457 } while (0)
458
459 #define CLEAR_STACK_REF(_stack, _slot) do { \
460 (_stack)[_slot].ref = IR_UNUSED; \
461 (_stack)[_slot].flags = 0; \
462 } while (0)
463 #define STACK_REF(_stack, _slot) \
464 (_stack)[_slot].ref
465 #define SET_STACK_REF(_stack, _slot, _ref) do { \
466 (_stack)[_slot].ref = (_ref); \
467 (_stack)[_slot].flags = 0; \
468 } while (0)
469 #define SET_STACK_REF_EX(_stack, _slot, _ref, _flags) do { \
470 (_stack)[_slot].ref = (_ref); \
471 (_stack)[_slot].flags = _flags; \
472 } while (0)
473
474 #define STACK_INFO(_stack, _slot) \
475 (_stack)[_slot].info
476 #define STACK_TYPE(_stack, _slot) \
477 (_stack)[_slot].type
478 #define STACK_MEM_TYPE(_stack, _slot) \
479 (_stack)[_slot].mem_type
480 #define STACK_REG(_stack, _slot) \
481 (_stack)[_slot].reg
482 #define STACK_FLAGS(_stack, _slot) \
483 (_stack)[_slot].flags
484 #define SET_STACK_INFO(_stack, _slot, _info) do { \
485 (_stack)[_slot].info = _info; \
486 } while (0)
487 #define SET_STACK_TYPE(_stack, _slot, _type, _set_mem_type) do { \
488 uint8_t __type = (_type); \
489 (_stack)[_slot].type = __type; \
490 if (_set_mem_type) { \
491 (_stack)[_slot].mem_type = __type; \
492 } \
493 (_stack)[_slot].reg = ZREG_NONE; \
494 (_stack)[_slot].flags = 0; \
495 CLEAR_STACK_REF(_stack, _slot); \
496 } while (0)
497 #define SET_STACK_REG(_stack, _slot, _reg) do { \
498 (_stack)[_slot].reg = _reg; \
499 (_stack)[_slot].flags = 0; \
500 } while (0)
501 #define SET_STACK_REG_EX(_stack, _slot, _reg, _flags) do { \
502 (_stack)[_slot].reg = _reg; \
503 (_stack)[_slot].flags = _flags; \
504 } while (0)
505 #define RESET_STACK_MEM_TYPE(_stack, _slot) do { \
506 (_stack)[_slot].mem_type = IS_UNKNOWN; \
507 } while (0)
508
509 /* trace info flags */
510 #define ZEND_JIT_TRACE_CHECK_INTERRUPT (1<<0)
511 #define ZEND_JIT_TRACE_LOOP (1<<1)
512 #define ZEND_JIT_TRACE_USES_INITIAL_IP (1<<2)
513
514 typedef union _zend_jit_exit_const {
515 int64_t i;
516 double d;
517 } zend_jit_exit_const;
518
519 typedef struct _zend_jit_trace_info {
520 uint32_t id; /* trace id */
521 uint32_t root; /* root trace id or self id for root traces */
522 uint32_t parent; /* parent trace id or 0 for root traces */
523 uint32_t link; /* link trace id or self id for loop) */
524 uint32_t exit_count; /* number of side exits */
525 uint32_t child_count; /* number of side traces for root traces */
526 uint32_t code_size; /* size of native code */
527 uint32_t exit_counters; /* offset in exit counters array */
528 uint32_t stack_map_size;
529 uint32_t flags; /* See ZEND_JIT_TRACE_... defines above */
530 uint32_t polymorphism; /* Counter of polymorphic calls */
531 uint32_t jmp_table_size;/* number of jmp_table slots */
532 const zend_op_array *op_array; /* function */
533 const zend_op *opline; /* first opline */
534 const void *code_start; /* address of native code */
535 zend_jit_trace_exit_info *exit_info; /* info about side exits */
536 zend_jit_trace_stack *stack_map;
537 //uint32_t loop_offset;
538 uint32_t consts_count; /* number of side exits */
539 zend_jit_exit_const *constants;
540 } zend_jit_trace_info;
541
542 struct _zend_jit_trace_stack_frame {
543 zend_jit_trace_stack_frame *call;
544 zend_jit_trace_stack_frame *prev;
545 const zend_function *func;
546 const zend_op *call_opline;
547 zend_class_entry *ce; /* $this */
548 uint32_t call_level;
549 uint32_t _info;
550 int used_stack;
551 int old_checked_stack;
552 int old_peek_checked_stack;
553 zend_jit_trace_stack stack[1];
554 };
555
556 #define TRACE_FRAME_SHIFT_NUM_ARGS 16
557 #define TRACE_FRAME_MAX_NUM_ARGS 32767
558
559 #define TRACE_FRAME_MASK_NUM_ARGS 0xffff0000
560 #define TRACE_FRAME_MASK_NESTED 0x00000001
561 #define TRACE_FRAME_MASK_LAST_SEND_BY_REF 0x00000002
562 #define TRACE_FRAME_MASK_LAST_SEND_BY_VAL 0x00000004
563 #define TRACE_FRAME_MASK_RETURN_VALUE_USED 0x00000008
564 #define TRACE_FRAME_MASK_RETURN_VALUE_UNUSED 0x00000010
565 #define TRACE_FRAME_MASK_THIS_CHECKED 0x00000020
566 #define TRACE_FRAME_MASK_UNKNOWN_RETURN 0x00000040
567 #define TRACE_FRAME_MASK_NO_NEED_RELEASE_THIS 0x00000080
568 #define TRACE_FRAME_MASK_THIS_CLASS_CHECKED 0x00000100
569 #define TRACE_FRAME_MASK_CLOSURE_CALL 0x00000200
570 #define TRACE_FRAME_MASK_ALWAYS_RELEASE_THIS 0x00000400
571
572
573 #define TRACE_FRAME_INIT(frame, _func, _flags, num_args) do { \
574 zend_jit_trace_stack_frame *_frame = (frame); \
575 _frame->call = NULL; \
576 _frame->prev = NULL; \
577 _frame->func = (const zend_function*)_func; \
578 _frame->call_opline = NULL; \
579 _frame->call_level = 0; \
580 _frame->_info = (((uint32_t)(num_args)) << TRACE_FRAME_SHIFT_NUM_ARGS) & TRACE_FRAME_MASK_NUM_ARGS; \
581 _frame->_info |= _flags; \
582 } while (0)
583
584 #define TRACE_FRAME_RETURN_SSA_VAR(frame) \
585 ((int)(frame)->_info)
586 #define TRACE_FRAME_NUM_ARGS(frame) \
587 ((int)((frame)->_info) >> TRACE_FRAME_SHIFT_NUM_ARGS)
588 #define TRACE_FRAME_IS_NESTED(frame) \
589 ((frame)->_info & TRACE_FRAME_MASK_NESTED)
590 #define TRACE_FRAME_IS_LAST_SEND_BY_REF(frame) \
591 ((frame)->_info & TRACE_FRAME_MASK_LAST_SEND_BY_REF)
592 #define TRACE_FRAME_IS_LAST_SEND_BY_VAL(frame) \
593 ((frame)->_info & TRACE_FRAME_MASK_LAST_SEND_BY_VAL)
594 #define TRACE_FRAME_IS_RETURN_VALUE_USED(frame) \
595 ((frame)->_info & TRACE_FRAME_MASK_RETURN_VALUE_USED)
596 #define TRACE_FRAME_IS_RETURN_VALUE_UNUSED(frame) \
597 ((frame)->_info & TRACE_FRAME_MASK_RETURN_VALUE_UNUSED)
598 #define TRACE_FRAME_IS_THIS_CHECKED(frame) \
599 ((frame)->_info & TRACE_FRAME_MASK_THIS_CHECKED)
600 #define TRACE_FRAME_IS_UNKNOWN_RETURN(frame) \
601 ((frame)->_info & TRACE_FRAME_MASK_UNKNOWN_RETURN)
602 #define TRACE_FRAME_NO_NEED_RELEASE_THIS(frame) \
603 ((frame)->_info & TRACE_FRAME_MASK_NO_NEED_RELEASE_THIS)
604 #define TRACE_FRAME_IS_THIS_CLASS_CHECKED(frame) \
605 ((frame)->_info & TRACE_FRAME_MASK_THIS_CLASS_CHECKED)
606 #define TRACE_FRAME_IS_CLOSURE_CALL(frame) \
607 ((frame)->_info & TRACE_FRAME_MASK_CLOSURE_CALL)
608 #define TRACE_FRAME_ALWAYS_RELEASE_THIS(frame) \
609 ((frame)->_info & TRACE_FRAME_MASK_ALWAYS_RELEASE_THIS)
610
611 #define TRACE_FRAME_SET_UNKNOWN_NUM_ARGS(frame) do { \
612 (frame)->_info |= (0xffffu << TRACE_FRAME_SHIFT_NUM_ARGS); \
613 } while (0)
614 #define TRACE_FRAME_SET_RETURN_SSA_VAR(frame, var) do { \
615 (frame)->_info = var; \
616 } while (0)
617 #define TRACE_FRAME_SET_LAST_SEND_BY_REF(frame) do { \
618 (frame)->_info |= TRACE_FRAME_MASK_LAST_SEND_BY_REF; \
619 (frame)->_info &= ~TRACE_FRAME_MASK_LAST_SEND_BY_VAL; \
620 } while (0)
621 #define TRACE_FRAME_SET_LAST_SEND_BY_VAL(frame) do { \
622 (frame)->_info |= TRACE_FRAME_MASK_LAST_SEND_BY_VAL; \
623 (frame)->_info &= ~TRACE_FRAME_MASK_LAST_SEND_BY_REF; \
624 } while (0)
625 #define TRACE_FRAME_SET_LAST_SEND_UNKNOWN(frame) do { \
626 (frame)->_info &= ~TRACE_FRAME_MASK_LAST_SEND_BY_VAL; \
627 (frame)->_info &= ~TRACE_FRAME_MASK_LAST_SEND_BY_REF; \
628 } while (0)
629 #define TRACE_FRAME_SET_RETURN_VALUE_USED(frame) do { \
630 (frame)->_info |= TRACE_FRAME_MASK_RETURN_VALUE_USED; \
631 (frame)->_info &= ~TRACE_FRAME_MASK_RETURN_VALUE_UNUSED; \
632 } while (0)
633 #define TRACE_FRAME_SET_RETURN_VALUE_UNUSED(frame) do { \
634 (frame)->_info |= TRACE_FRAME_MASK_RETURN_VALUE_UNUSED; \
635 (frame)->_info &= ~TRACE_FRAME_MASK_RETURN_VALUE_USED; \
636 } while (0)
637 #define TRACE_FRAME_SET_THIS_CHECKED(frame) do { \
638 (frame)->_info |= TRACE_FRAME_MASK_THIS_CHECKED; \
639 } while (0)
640 #define TRACE_FRAME_SET_NO_NEED_RELEASE_THIS(frame) do { \
641 (frame)->_info |= TRACE_FRAME_MASK_NO_NEED_RELEASE_THIS; \
642 } while (0)
643 #define TRACE_FRAME_SET_THIS_CLASS_CHECKED(frame) do { \
644 (frame)->_info |= TRACE_FRAME_MASK_THIS_CLASS_CHECKED; \
645 } while (0)
646 #define TRACE_FRAME_SET_CLOSURE_CALL(frame) do { \
647 (frame)->_info |= TRACE_FRAME_MASK_CLOSURE_CALL; \
648 } while (0)
649 #define TRACE_FRAME_SET_ALWAYS_RELEASE_THIS(frame) do { \
650 (frame)->_info |= TRACE_FRAME_MASK_ALWAYS_RELEASE_THIS; \
651 } while (0)
652
653 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_func_trace_helper(ZEND_OPCODE_HANDLER_ARGS);
654 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_ret_trace_helper(ZEND_OPCODE_HANDLER_ARGS);
655 ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_jit_loop_trace_helper(ZEND_OPCODE_HANDLER_ARGS);
656
657 int ZEND_FASTCALL zend_jit_trace_hot_root(zend_execute_data *execute_data, const zend_op *opline);
658 zend_jit_trace_stop ZEND_FASTCALL zend_jit_trace_execute(zend_execute_data *execute_data,
659 const zend_op *opline,
660 zend_jit_trace_rec *trace_buffer,
661 uint8_t start,
662 uint32_t is_megamorphc,
663 int ret_depth);
664
zend_jit_trace_get_exit_opline(zend_jit_trace_rec * trace,const zend_op * opline,bool * exit_if_true)665 static zend_always_inline const zend_op* zend_jit_trace_get_exit_opline(zend_jit_trace_rec *trace, const zend_op *opline, bool *exit_if_true)
666 {
667 if (trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END) {
668 if (trace->opline == opline + 1) {
669 /* not taken branch */
670 *exit_if_true = opline->opcode == ZEND_JMPNZ;
671 return OP_JMP_ADDR(opline, opline->op2);
672 } else if (trace->opline == OP_JMP_ADDR(opline, opline->op2)) {
673 /* taken branch */
674 *exit_if_true = opline->opcode == ZEND_JMPZ;
675 return opline + 1;
676 } else {
677 ZEND_UNREACHABLE();
678 }
679 } else {
680 ZEND_UNREACHABLE();
681 }
682 *exit_if_true = 0;
683 return NULL;
684 }
685
zend_jit_may_be_polymorphic_call(const zend_op * opline)686 static zend_always_inline bool zend_jit_may_be_polymorphic_call(const zend_op *opline)
687 {
688 if (opline->opcode == ZEND_INIT_FCALL
689 || opline->opcode == ZEND_INIT_FCALL_BY_NAME
690 || opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME
691 || opline->opcode == ZEND_INIT_PARENT_PROPERTY_HOOK_CALL) {
692 return 0;
693 } else if (opline->opcode == ZEND_INIT_METHOD_CALL
694 || opline->opcode == ZEND_INIT_DYNAMIC_CALL) {
695 return 1;
696 } else if (opline->opcode == ZEND_INIT_STATIC_METHOD_CALL) {
697 return (opline->op1_type != IS_CONST || opline->op2_type != IS_CONST);
698 } else if (opline->opcode == ZEND_INIT_USER_CALL) {
699 return (opline->op2_type != IS_CONST);
700 } else if (opline->opcode == ZEND_NEW) {
701 return (opline->op1_type != IS_CONST);
702 } else {
703 ZEND_UNREACHABLE();
704 return 0;
705 }
706 }
707
708 /* Instruction cache flush */
709 #ifndef JIT_CACHE_FLUSH
710 # if ZEND_JIT_TARGET_ARM64
711 # if ((defined(__GNUC__) && ZEND_GCC_VERSION >= 4003) || __has_builtin(__builtin___clear_cache))
712 # define JIT_CACHE_FLUSH(from, to) __builtin___clear_cache((char*)(from), (char*)(to))
713 # else
714 # error "Missing builtin to flush instruction cache for AArch64"
715 # endif
716 # else /* Not required to implement on archs with unified caches */
717 # define JIT_CACHE_FLUSH(from, to)
718 # endif
719 #endif /* !JIT_CACHE_FLUSH */
720
721 /* bit helpers */
722
zend_long_is_power_of_two(zend_long x)723 static zend_always_inline bool zend_long_is_power_of_two(zend_long x)
724 {
725 return (x > 0) && !(x & (x - 1));
726 }
727
728 /* from http://aggregate.org/MAGIC/ */
ones32(uint32_t x)729 static zend_always_inline uint32_t ones32(uint32_t x)
730 {
731 x -= ((x >> 1) & 0x55555555);
732 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
733 x = (((x >> 4) + x) & 0x0f0f0f0f);
734 x += (x >> 8);
735 x += (x >> 16);
736 return x & 0x0000003f;
737 }
738
floor_log2(uint32_t x)739 static zend_always_inline uint32_t floor_log2(uint32_t x)
740 {
741 ZEND_ASSERT(x != 0);
742 x |= (x >> 1);
743 x |= (x >> 2);
744 x |= (x >> 4);
745 x |= (x >> 8);
746 x |= (x >> 16);
747 return ones32(x) - 1;
748 }
749
is_power_of_two(uint32_t x)750 static zend_always_inline bool is_power_of_two(uint32_t x)
751 {
752 return !(x & (x - 1)) && x != 0;
753 }
754
has_concrete_type(uint32_t value_type)755 static zend_always_inline bool has_concrete_type(uint32_t value_type)
756 {
757 return is_power_of_two (value_type & (MAY_BE_ANY|MAY_BE_UNDEF));
758 }
759
concrete_type(uint32_t value_type)760 static zend_always_inline uint32_t concrete_type(uint32_t value_type)
761 {
762 return floor_log2(value_type & (MAY_BE_ANY|MAY_BE_UNDEF));
763 }
764
is_signed(double d)765 static zend_always_inline bool is_signed(double d)
766 {
767 return (((unsigned char*)&d)[sizeof(double)-1] & 0x80) != 0;
768 }
769
770 #endif /* ZEND_JIT_INTERNAL_H */
771