Coverage Report

Created: 2025-09-27 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/jit/zend_jit_ir.c
Line
Count
Source
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
 *  +----------------------------------------------------------------------+
17
 */
18
19
#include "Zend/zend_types.h"
20
#include "Zend/zend_type_info.h"
21
#include "jit/ir/ir.h"
22
#include "jit/ir/ir_builder.h"
23
#include "jit/tls/zend_jit_tls.h"
24
25
#if defined(IR_TARGET_X86)
26
# define IR_REG_SP            4 /* IR_REG_RSP */
27
# define IR_REG_FP            5 /* IR_REG_RBP */
28
# define ZREG_FP              6 /* IR_REG_RSI */
29
# define ZREG_IP              7 /* IR_REG_RDI */
30
# define ZREG_FIRST_FPR       8
31
# define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<6) | (1<<7)) /* all preserved registers */
32
# if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
33
#  error
34
# endif
35
#elif defined(IR_TARGET_X64)
36
0
# define IR_REG_SP            4 /* IR_REG_RSP */
37
0
# define IR_REG_FP            5 /* IR_REG_RBP */
38
# if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
39
/* Use the first two arg registers of the preserve_none calling convention for FP/IP
40
 * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/X86/X86CallingConv.td#L1029 */
41
0
#  define ZREG_FP             12 /* IR_REG_R12 */
42
0
#  define ZREG_IP             13 /* IR_REG_R13 */
43
# else
44
#  define ZREG_FP             14 /* IR_REG_R14 */
45
#  define ZREG_IP             15 /* IR_REG_R15 */
46
# endif
47
0
# define ZREG_FIRST_FPR      16
48
# if defined(_WIN64)
49
#  define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<6) | (1<<7) | (1<<12) | (1<<13) | (1<<14) | (1<<15))
50
/*
51
#  define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<6) | (1<<7) | (1<<12) | (1<<13) | (1<<14) | (1<<15) | \
52
                               (1<<(16+6)) | (1<<(16+7)) | (1<<(16+8)) | (1<<(16+9)) | (1<<(16+10)) | \
53
                               (1<<(16+11)) | (1<<(16+12)) | (1<<(16+13)) | (1<<(16+14)) | (1<<(16+15)))
54
*/
55
#  define IR_SHADOW_ARGS     32
56
# else
57
0
#  define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<12) | (1<<13) | (1<<14) | (1<<15)) /* all preserved registers */
58
# endif
59
#elif defined(IR_TARGET_AARCH64)
60
# define IR_REG_SP           31 /* IR_REG_RSP */
61
# define IR_REG_LR           30 /* IR_REG_X30 */
62
# define IR_REG_FP           29 /* IR_REG_X29 */
63
# if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
64
/* Use the first two arg registers of the preserve_none calling convention for FP/IP
65
 * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/AArch64/AArch64CallingConvention.td#L541 */
66
#  define ZREG_FP             20 /* IR_REG_X20 */
67
#  define ZREG_IP             21 /* IR_REG_X21 */
68
# else
69
#  define ZREG_FP             27 /* IR_REG_X27 */
70
#  define ZREG_IP             28 /* IR_REG_X28 */
71
# endif
72
# define ZREG_FIRST_FPR      32
73
# define IR_REGSET_PRESERVED ((1<<19) | (1<<20) | (1<<21) | (1<<22) | (1<<23) | \
74
                              (1<<24) | (1<<25) | (1<<26) | (1<<27) | (1<<28)) /* all preserved registers */
75
#else
76
# error "Unknown IR target"
77
#endif
78
79
0
#define ZREG_RX ZREG_IP
80
81
#define OPTIMIZE_FOR_SIZE 0
82
83
/* IR builder defines */
84
#undef  _ir_CTX
85
0
#define _ir_CTX                 (&jit->ctx)
86
87
#if GCC_GLOBAL_REGS
88
# define IR_OPCODE_HANDLER_RET IR_VOID
89
#else
90
# define IR_OPCODE_HANDLER_RET IR_ADDR
91
#endif
92
93
#undef  ir_CONST_ADDR
94
0
#define ir_CONST_ADDR(_addr)    jit_CONST_ADDR(jit, (uintptr_t)(_addr))
95
#define ir_CONST_FUNC(_addr)    jit_CONST_FUNC(jit, (uintptr_t)(_addr), 0)
96
0
#define ir_CONST_FC_FUNC(_addr) jit_CONST_FUNC(jit, (uintptr_t)(_addr), IR_FASTCALL_FUNC)
97
#define ir_CAST_FC_FUNC(_addr)  ir_fold2(_ir_CTX, IR_OPT(IR_PROTO, IR_ADDR), (_addr), \
98
  ir_proto_0(_ir_CTX, IR_FASTCALL_FUNC, IR_I32))
99
# define ir_CONST_OPCODE_HANDLER_FUNC(_addr) \
100
0
  jit_CONST_OPCODE_HANDLER_FUNC(jit, _addr)
101
# define ir_CAST_OPCODE_HANDLER_FUNC(_addr)  ir_fold2(_ir_CTX, IR_OPT(IR_PROTO, IR_ADDR), (_addr), \
102
  ir_proto_0(_ir_CTX, IR_FASTCALL_FUNC, IR_OPCODE_HANDLER_RET))
103
104
#define ir_CONST_FUNC_PROTO(_addr, _proto) \
105
  jit_CONST_FUNC_PROTO(jit, (uintptr_t)(_addr), (_proto))
106
107
#undef  ir_ADD_OFFSET
108
#define ir_ADD_OFFSET(_addr, _offset) \
109
0
  jit_ADD_OFFSET(jit, _addr, _offset)
110
111
#ifdef ZEND_ENABLE_ZVAL_LONG64
112
0
# define IR_LONG           IR_I64
113
0
# define ir_CONST_LONG     ir_CONST_I64
114
# define ir_UNARY_OP_L     ir_UNARY_OP_I64
115
0
# define ir_BINARY_OP_L    ir_BINARY_OP_I64
116
# define ir_ADD_L          ir_ADD_I64
117
0
# define ir_SUB_L          ir_SUB_I64
118
0
# define ir_MUL_L          ir_MUL_I64
119
0
# define ir_DIV_L          ir_DIV_I64
120
0
# define ir_MOD_L          ir_MOD_I64
121
# define ir_NEG_L          ir_NEG_I64
122
# define ir_ABS_L          ir_ABS_I64
123
# define ir_SEXT_L         ir_SEXT_I64
124
0
# define ir_ZEXT_L         ir_ZEXT_I64
125
# define ir_TRUNC_L        ir_TRUNC_I64
126
0
# define ir_BITCAST_L      ir_BITCAST_I64
127
# define ir_FP2L           ir_FP2I64
128
0
# define ir_ADD_OV_L       ir_ADD_OV_I64
129
0
# define ir_SUB_OV_L       ir_SUB_OV_I64
130
# define ir_MUL_OV_L       ir_MUL_OV_I64
131
# define ir_NOT_L          ir_NOT_I64
132
# define ir_OR_L           ir_OR_I64
133
0
# define ir_AND_L          ir_AND_I64
134
# define ir_XOR_L          ir_XOR_I64
135
0
# define ir_SHL_L          ir_SHL_I64
136
0
# define ir_SHR_L          ir_SHR_I64
137
0
# define ir_SAR_L          ir_SAR_I64
138
# define ir_ROL_L          ir_ROL_I64
139
# define ir_ROR_L          ir_ROR_I64
140
# define ir_MIN_L          ir_MIN_I64
141
# define ir_MAX_L          ir_MAX_I64
142
0
# define ir_LOAD_L         ir_LOAD_I64
143
#else
144
# define IR_LONG           IR_I32
145
# define ir_CONST_LONG     ir_CONST_I32
146
# define ir_UNARY_OP_L     ir_UNARY_OP_I32
147
# define ir_BINARY_OP_L    ir_BINARY_OP_I32
148
# define ir_ADD_L          ir_ADD_I32
149
# define ir_SUB_L          ir_SUB_I32
150
# define ir_MUL_L          ir_MUL_I32
151
# define ir_DIV_L          ir_DIV_I32
152
# define ir_MOD_L          ir_MOD_I32
153
# define ir_NEG_L          ir_NEG_I32
154
# define ir_ABS_L          ir_ABS_I32
155
# define ir_SEXT_L         ir_SEXT_I32
156
# define ir_ZEXT_L         ir_ZEXT_I32
157
# define ir_TRUNC_L        ir_TRUNC_I32
158
# define ir_BITCAST_L      ir_BITCAST_I32
159
# define ir_FP2L           ir_FP2I32
160
# define ir_ADD_OV_L       ir_ADD_OV_I32
161
# define ir_SUB_OV_L       ir_SUB_OV_I32
162
# define ir_MUL_OV_L       ir_MUL_OV_I32
163
# define ir_NOT_L          ir_NOT_I32
164
# define ir_OR_L           ir_OR_I32
165
# define ir_AND_L          ir_AND_I32
166
# define ir_XOR_L          ir_XOR_I32
167
# define ir_SHL_L          ir_SHL_I32
168
# define ir_SHR_L          ir_SHR_I32
169
# define ir_SAR_L          ir_SAR_I32
170
# define ir_ROL_L          ir_ROL_I32
171
# define ir_ROR_L          ir_ROR_I32
172
# define ir_MIN_L          ir_MIN_I32
173
# define ir_MAX_L          ir_MAX_I32
174
# define ir_LOAD_L         ir_LOAD_I32
175
#endif
176
177
/* A helper structure to collect IT rers for the following use in (MERGE/PHI)_N */
178
typedef struct _ir_refs {
179
  uint32_t count;
180
  uint32_t limit;
181
  ir_ref   refs[] ZEND_ELEMENT_COUNT(count);
182
} ir_refs;
183
184
#define ir_refs_size(_n)          (offsetof(ir_refs, refs) + sizeof(ir_ref) * (_n))
185
0
#define ir_refs_init(_name, _n)   _name = alloca(ir_refs_size(_n)); \
186
0
                                  do {_name->count = 0; _name->limit = (_n);} while (0)
187
188
static void ir_refs_add(ir_refs *refs, ir_ref ref)
189
0
{
190
0
  ir_ref *ptr;
191
192
0
  ZEND_ASSERT(refs->count < refs->limit);
193
0
  ptr = refs->refs;
194
0
  ptr[refs->count++] = ref;
195
0
}
196
197
static size_t zend_jit_trace_prologue_size = (size_t)-1;
198
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
199
static uint32_t allowed_opt_flags = 0;
200
static uint32_t default_mflags = 0;
201
#endif
202
static bool delayed_call_chain = false; // TODO: remove this var (use jit->delayed_call_level) ???
203
204
#ifdef ZTS
205
static size_t tsrm_ls_cache_tcb_offset = 0;
206
static size_t tsrm_tls_index = -1;
207
static size_t tsrm_tls_offset = -1;
208
209
# define EG_TLS_OFFSET(field) \
210
  (executor_globals_offset + offsetof(zend_executor_globals, field))
211
212
# define CG_TLS_OFFSET(field) \
213
  (compiler_globals_offset + offsetof(zend_compiler_globals, field))
214
215
# define jit_EG(_field) \
216
  ir_ADD_OFFSET(jit_TLS(jit), EG_TLS_OFFSET(_field))
217
218
# define jit_CG(_field) \
219
  ir_ADD_OFFSET(jit_TLS(jit), CG_TLS_OFFSET(_field))
220
221
#else
222
223
# define jit_EG(_field) \
224
0
  ir_CONST_ADDR(&EG(_field))
225
226
# define jit_CG(_field) \
227
  ir_CONST_ADDR(&CG(_field))
228
229
#endif
230
231
#define jit_CALL(_call, _field) \
232
0
  ir_ADD_OFFSET(_call, offsetof(zend_execute_data, _field))
233
234
#define jit_EX(_field) \
235
  jit_CALL(jit_FP(jit), _field)
236
237
#define jit_RX(_field) \
238
  jit_CALL(jit_IP(jit), _field)
239
240
#define JIT_STUBS(_) \
241
  _(exception_handler,              IR_SKIP_PROLOGUE) \
242
  _(exception_handler_undef,        IR_SKIP_PROLOGUE) \
243
  _(exception_handler_free_op2,     IR_SKIP_PROLOGUE) \
244
  _(exception_handler_free_op1_op2, IR_SKIP_PROLOGUE) \
245
  _(interrupt_handler,              IR_SKIP_PROLOGUE) \
246
  _(leave_function_handler,         IR_SKIP_PROLOGUE) \
247
  _(negative_shift,                 IR_SKIP_PROLOGUE) \
248
  _(mod_by_zero,                    IR_SKIP_PROLOGUE) \
249
  _(invalid_this,                   IR_SKIP_PROLOGUE) \
250
  _(undefined_function,             IR_SKIP_PROLOGUE) \
251
  _(throw_cannot_pass_by_ref,       IR_SKIP_PROLOGUE) \
252
  _(icall_throw,                    IR_SKIP_PROLOGUE) \
253
  _(leave_throw,                    IR_SKIP_PROLOGUE) \
254
  _(hybrid_runtime_jit,             IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
255
  _(hybrid_profile_jit,             IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
256
  _(hybrid_func_hot_counter,        IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
257
  _(hybrid_loop_hot_counter,        IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
258
  _(hybrid_func_trace_counter,      IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
259
  _(hybrid_ret_trace_counter,       IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
260
  _(hybrid_loop_trace_counter,      IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
261
  _(trace_halt,                     IR_SKIP_PROLOGUE) \
262
  _(trace_escape,                   IR_SKIP_PROLOGUE) \
263
  _(trace_exit,                     IR_SKIP_PROLOGUE) \
264
  _(undefined_offset,               IR_FUNCTION | IR_FASTCALL_FUNC) \
265
  _(undefined_key,                  IR_FUNCTION | IR_FASTCALL_FUNC) \
266
  _(cannot_add_element,             IR_FUNCTION | IR_FASTCALL_FUNC) \
267
  _(assign_const,                   IR_FUNCTION | IR_FASTCALL_FUNC) \
268
  _(assign_tmp,                     IR_FUNCTION | IR_FASTCALL_FUNC) \
269
  _(assign_var,                     IR_FUNCTION | IR_FASTCALL_FUNC) \
270
  _(assign_cv_noref,                IR_FUNCTION | IR_FASTCALL_FUNC) \
271
  _(assign_cv,                      IR_FUNCTION | IR_FASTCALL_FUNC) \
272
  _(new_array,                      IR_FUNCTION | IR_FASTCALL_FUNC) \
273
274
#define JIT_STUB_ID(name, flags) \
275
  jit_stub_ ## name,
276
277
#define JIT_STUB_FORWARD(name, flags) \
278
  static int zend_jit_ ## name ## _stub(zend_jit_ctx *jit);
279
280
#define JIT_STUB(name, flags) \
281
  {JIT_STUB_PREFIX #name, zend_jit_ ## name ## _stub, flags},
282
283
typedef enum _jit_stub_id {
284
  JIT_STUBS(JIT_STUB_ID)
285
  jit_last_stub
286
} jit_stub_id;
287
288
typedef struct _zend_jit_reg_var {
289
  ir_ref   ref;
290
  uint32_t flags;
291
} zend_jit_reg_var;
292
293
typedef struct _zend_jit_ctx {
294
  ir_ctx               ctx;
295
  const zend_op       *last_valid_opline;
296
  bool                 use_last_valid_opline;
297
  bool                 track_last_valid_opline;
298
  bool                 reuse_ip;
299
  uint32_t             delayed_call_level;
300
  int                  b;           /* current basic block number or -1 */
301
#ifdef ZTS
302
  ir_ref               tls;
303
#endif
304
  ir_ref               fp;
305
  ir_ref               poly_func_ref; /* restored from parent trace snapshot */
306
  ir_ref               poly_this_ref; /* restored from parent trace snapshot */
307
  ir_ref               trace_loop_ref;
308
  ir_ref               return_inputs;
309
  const zend_op_array *op_array;
310
  const zend_op_array *current_op_array;
311
  zend_ssa            *ssa;
312
  zend_string         *name;
313
  ir_ref              *bb_start_ref;      /* PHP BB -> IR ref mapping */
314
  ir_ref              *bb_predecessors;   /* PHP BB -> index in bb_edges -> IR refs of predessors */
315
  ir_ref              *bb_edges;
316
  zend_jit_trace_info *trace;
317
  zend_jit_reg_var    *ra;
318
  int                  delay_var;
319
  ir_refs             *delay_refs;
320
  ir_ref               eg_exception_addr;
321
  HashTable            addr_hash;
322
  ir_ref               stub_addr[jit_last_stub];
323
} zend_jit_ctx;
324
325
typedef int8_t zend_reg;
326
327
typedef struct _zend_jit_registers_buf {
328
#if defined(IR_TARGET_X64)
329
  uint64_t gpr[16]; /* general purpose integer register */
330
  double   fpr[16]; /* floating point registers */
331
#elif defined(IR_TARGET_X86)
332
  uint32_t gpr[8]; /* general purpose integer register */
333
  double   fpr[8]; /* floating point registers */
334
#elif defined (IR_TARGET_AARCH64)
335
  uint64_t gpr[32]; /* general purpose integer register */
336
  double   fpr[32]; /* floating point registers */
337
#else
338
# error "Unknown IR target"
339
#endif
340
} zend_jit_registers_buf;
341
342
/* Keep 32 exit points in a single code block */
343
0
#define ZEND_JIT_EXIT_POINTS_SPACING   4  // push byte + short jmp = bytes
344
0
#define ZEND_JIT_EXIT_POINTS_PER_GROUP 32 // number of continuous exit points
345
346
static uint32_t zend_jit_exit_point_by_addr(const void *addr);
347
int ZEND_FASTCALL zend_jit_trace_exit(uint32_t exit_num, zend_jit_registers_buf *regs);
348
349
static int zend_jit_assign_to_variable(zend_jit_ctx   *jit,
350
                                       const zend_op  *opline,
351
                                       zend_jit_addr   var_use_addr,
352
                                       zend_jit_addr   var_addr,
353
                                       uint32_t        var_info,
354
                                       uint32_t        var_def_info,
355
                                       uint8_t         val_type,
356
                                       zend_jit_addr   val_addr,
357
                                       uint32_t        val_info,
358
                                       zend_jit_addr   res_addr,
359
                                       zend_jit_addr   ref_addr,
360
                                       bool       check_exception);
361
362
static ir_ref jit_CONST_FUNC(zend_jit_ctx *jit, uintptr_t addr, uint16_t flags);
363
364
typedef struct _zend_jit_stub {
365
  const char *name;
366
  int (*stub)(zend_jit_ctx *jit);
367
  uint32_t flags;
368
} zend_jit_stub;
369
370
JIT_STUBS(JIT_STUB_FORWARD)
371
372
static const zend_jit_stub zend_jit_stubs[] = {
373
  JIT_STUBS(JIT_STUB)
374
};
375
376
#if defined(_WIN32) || defined(IR_TARGET_AARCH64)
377
/* We keep addresses in SHM to share them between sepaeate processes (on Windows) or to support veneers (on AArch64) */
378
static void** zend_jit_stub_handlers = NULL;
379
#else
380
static void* zend_jit_stub_handlers[sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0])];
381
#endif
382
383
#if defined(IR_TARGET_AARCH64)
384
385
# ifdef __FreeBSD__
386
/* https://github.com/freebsd/freebsd-src/blob/c52ca7dd09066648b1cc40f758289404d68ab886/libexec/rtld-elf/aarch64/reloc.c#L180-L184 */
387
typedef struct TLSDescriptor {
388
  void*   thunk;
389
  int     index;
390
  size_t  offset;
391
} TLSDescriptor;
392
# endif
393
394
#define IR_HAS_VENEERS (1U<<31) /* IR_RESERVED_FLAG_1 */
395
396
static const void *zend_jit_get_veneer(ir_ctx *ctx, const void *addr)
397
{
398
  int i, count = sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
399
400
  for (i = 0; i < count; i++) {
401
    if (zend_jit_stub_handlers[i] == addr) {
402
      return zend_jit_stub_handlers[count + i];
403
    }
404
  }
405
406
  if (((zend_jit_ctx*)ctx)->trace
407
   && (void*)addr >= dasm_buf && (void*)addr < dasm_end) {
408
    uint32_t exit_point = zend_jit_exit_point_by_addr(addr);
409
410
    if (exit_point != (uint32_t)-1) {
411
      zend_jit_trace_info *t = ((zend_jit_ctx*)ctx)->trace;
412
413
      ZEND_ASSERT(exit_point < t->exit_count);
414
      return (const void*)((char*)ctx->deoptimization_exits_base + (exit_point * 4));
415
    }
416
  }
417
418
  return NULL;
419
}
420
421
static bool zend_jit_set_veneer(ir_ctx *ctx, const void *addr, const void *veneer)
422
{
423
  int i, count = sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
424
  uint32_t exit_point = zend_jit_exit_point_by_addr(addr);
425
426
  if (exit_point != (uint32_t)-1) {
427
    return true;
428
  }
429
  for (i = 0; i < count; i++) {
430
    if (zend_jit_stub_handlers[i] == addr) {
431
      const void **ptr = (const void**)&zend_jit_stub_handlers[count + i];
432
      *ptr = veneer;
433
      ctx->flags2 |= IR_HAS_VENEERS;
434
#ifdef HAVE_CAPSTONE
435
      int64_t offset;
436
        if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
437
        const char *name = ir_disasm_find_symbol((uint64_t)(uintptr_t)addr, &offset);
438
439
        if (name && !offset) {
440
          if (strstr(name, "@veneer") == NULL) {
441
            char *new_name;
442
443
            zend_spprintf(&new_name, 0, "%s@veneer", name);
444
            ir_disasm_add_symbol(new_name, (uint64_t)(uintptr_t)veneer, 4);
445
            efree(new_name);
446
          } else {
447
            ir_disasm_add_symbol(name, (uint64_t)(uintptr_t)veneer, 4);
448
          }
449
        }
450
      }
451
#endif
452
      return true;
453
    }
454
  }
455
456
  return false;
457
}
458
459
static void zend_jit_commit_veneers(void)
460
{
461
  int i, count = sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
462
463
  for (i = 0; i < count; i++) {
464
    if (zend_jit_stub_handlers[count + i]) {
465
      zend_jit_stub_handlers[i] = zend_jit_stub_handlers[count + i];
466
      zend_jit_stub_handlers[count + i] = NULL;
467
    }
468
  }
469
}
470
#endif
471
472
static bool zend_jit_prefer_const_addr_load(zend_jit_ctx *jit, uintptr_t addr)
473
0
{
474
#if defined(IR_TARGET_X86)
475
  return false; /* always use immediate value */
476
#elif defined(IR_TARGET_X64)
477
  return addr > 0xffffffff; /* prefer loading long constant from memery */
478
#elif defined(IR_TARGET_AARCH64)
479
  return addr > 0xffff;
480
#else
481
# error "Unknown IR target"
482
#endif
483
0
}
484
485
static const char* zend_reg_name(int8_t reg)
486
0
{
487
0
  return ir_reg_name(reg, ir_reg_is_int(reg) ? IR_LONG : IR_DOUBLE);
488
0
}
489
490
/* IR helpers */
491
492
#ifdef ZTS
493
static void * ZEND_FASTCALL zend_jit_get_tsrm_ls_cache(void)
494
{
495
  return _tsrm_ls_cache;
496
}
497
498
static ir_ref jit_TLS(zend_jit_ctx *jit)
499
{
500
  ZEND_ASSERT(jit->ctx.control);
501
  if (jit->tls) {
502
    /* Emit "TLS" once for basic block */
503
    ir_insn *insn;
504
    ir_ref ref = jit->ctx.control;
505
506
    while (1) {
507
      if (ref == jit->tls) {
508
        return jit->tls;
509
      }
510
      insn = &jit->ctx.ir_base[ref];
511
      if (insn->op >= IR_START || insn->op == IR_CALL) {
512
        break;
513
      }
514
      ref = insn->op1;
515
    }
516
  }
517
518
  if (tsrm_ls_cache_tcb_offset == 0 && tsrm_tls_index == -1) {
519
    jit->tls = ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_get_tsrm_ls_cache));
520
  } else {
521
    jit->tls = ir_TLS(
522
        tsrm_ls_cache_tcb_offset ? tsrm_ls_cache_tcb_offset : tsrm_tls_index,
523
        tsrm_ls_cache_tcb_offset ? IR_NULL : tsrm_tls_offset);
524
  }
525
526
  return jit->tls;
527
}
528
#endif
529
530
static ir_ref jit_CONST_ADDR(zend_jit_ctx *jit, uintptr_t addr)
531
0
{
532
0
  ir_ref ref;
533
0
  zval *zv;
534
535
0
  if (addr == 0) {
536
0
    return IR_NULL;
537
0
  }
538
0
  zv = zend_hash_index_lookup(&jit->addr_hash, addr);
539
0
  if (Z_TYPE_P(zv) == IS_LONG) {
540
0
    ref = Z_LVAL_P(zv);
541
0
    ZEND_ASSERT(jit->ctx.ir_base[ref].opt == IR_OPT(IR_ADDR, IR_ADDR));
542
0
  } else {
543
0
    ref = ir_unique_const_addr(&jit->ctx, addr);
544
0
    ZVAL_LONG(zv, ref);
545
0
  }
546
0
  return ref;
547
0
}
548
549
static ir_ref jit_CONST_FUNC_PROTO(zend_jit_ctx *jit, uintptr_t addr, ir_ref proto)
550
0
{
551
0
  ir_ref ref;
552
0
  ir_insn *insn;
553
0
  zval *zv;
554
555
0
  ZEND_ASSERT(addr != 0);
556
0
  zv = zend_hash_index_lookup(&jit->addr_hash, addr);
557
0
  if (Z_TYPE_P(zv) == IS_LONG) {
558
0
    ref = Z_LVAL_P(zv);
559
0
    ZEND_ASSERT(jit->ctx.ir_base[ref].opt == IR_OPT(IR_FUNC_ADDR, IR_ADDR) && jit->ctx.ir_base[ref].proto == proto);
560
0
  } else {
561
0
    ref = ir_unique_const_addr(&jit->ctx, addr);
562
0
    insn = &jit->ctx.ir_base[ref];
563
0
    insn->optx = IR_OPT(IR_FUNC_ADDR, IR_ADDR);
564
0
    insn->proto = proto;
565
0
    ZVAL_LONG(zv, ref);
566
0
  }
567
0
  return ref;
568
0
}
569
570
static ir_ref jit_CONST_FUNC(zend_jit_ctx *jit, uintptr_t addr, uint16_t flags)
571
0
{
572
#if defined(IR_TARGET_X86)
573
  /* TODO: dummy prototype (only flags matter) ??? */
574
  ir_ref proto = flags ? ir_proto_0(&jit->ctx, flags, IR_I32) : 0;
575
#else
576
0
  ir_ref proto = 0;
577
0
#endif
578
579
0
  return jit_CONST_FUNC_PROTO(jit, addr, proto);
580
0
}
581
582
static ir_ref jit_CONST_OPCODE_HANDLER_FUNC(zend_jit_ctx *jit, zend_vm_opcode_handler_t handler)
583
0
{
584
0
  return jit_CONST_FUNC(jit, (uintptr_t)handler, IR_FASTCALL_FUNC);
585
0
}
586
587
static ir_ref jit_ADD_OFFSET(zend_jit_ctx *jit, ir_ref addr, uintptr_t offset)
588
0
{
589
0
  if (offset) {
590
0
    addr = ir_ADD_A(addr, ir_CONST_ADDR(offset));
591
0
  }
592
0
  return addr;
593
0
}
594
595
static ir_ref jit_EG_exception(zend_jit_ctx *jit)
596
0
{
597
#ifdef ZTS
598
  return jit_EG(exception);
599
#else
600
0
  ir_ref ref = jit->eg_exception_addr;
601
602
0
  if (UNEXPECTED(!ref)) {
603
0
    ref = ir_unique_const_addr(&jit->ctx, (uintptr_t)&EG(exception));
604
0
    jit->eg_exception_addr = ref;
605
0
  }
606
0
  return ref;
607
0
#endif
608
0
}
609
610
static ir_ref jit_STUB_ADDR(zend_jit_ctx *jit, jit_stub_id id)
611
0
{
612
0
  ir_ref ref = jit->stub_addr[id];
613
614
0
  if (UNEXPECTED(!ref)) {
615
0
    ref = ir_unique_const_addr(&jit->ctx, (uintptr_t)zend_jit_stub_handlers[id]);
616
0
    jit->stub_addr[id] = ref;
617
0
  }
618
0
  return ref;
619
0
}
620
621
static ir_ref jit_STUB_FUNC_ADDR(zend_jit_ctx *jit, jit_stub_id id, uint16_t flags)
622
0
{
623
0
  ir_ref ref = jit->stub_addr[id];
624
0
  ir_insn *insn;
625
626
0
  if (UNEXPECTED(!ref)) {
627
0
    ref = ir_unique_const_addr(&jit->ctx, (uintptr_t)zend_jit_stub_handlers[id]);
628
0
    insn = &jit->ctx.ir_base[ref];
629
0
    insn->optx = IR_OPT(IR_FUNC_ADDR, IR_ADDR);
630
#if defined(IR_TARGET_X86)
631
    /* TODO: dummy prototype (only flags matter) ??? */
632
    insn->proto = flags ? ir_proto_0(&jit->ctx, flags, IR_I32) : 0;
633
#else
634
0
    insn->proto = 0;
635
0
#endif
636
0
    jit->stub_addr[id] = ref;
637
0
  }
638
0
  return ref;
639
0
}
640
641
static void jit_SNAPSHOT(zend_jit_ctx *jit, ir_ref addr)
642
0
{
643
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && JIT_G(current_frame)) {
644
0
    const void *ptr = (const void*)jit->ctx.ir_base[addr].val.addr;
645
0
    const zend_op_array *op_array = &JIT_G(current_frame)->func->op_array;
646
0
    uint32_t stack_size = op_array->last_var + op_array->T;
647
648
0
    if (ptr == zend_jit_stub_handlers[jit_stub_exception_handler]
649
0
       || ptr == zend_jit_stub_handlers[jit_stub_exception_handler_undef]
650
0
       || ptr == zend_jit_stub_handlers[jit_stub_exception_handler_free_op1_op2]
651
0
       || ptr == zend_jit_stub_handlers[jit_stub_exception_handler_free_op2]
652
0
       || ptr == zend_jit_stub_handlers[jit_stub_interrupt_handler]
653
0
       || ptr == zend_jit_stub_handlers[jit_stub_leave_function_handler]
654
0
       || ptr == zend_jit_stub_handlers[jit_stub_negative_shift]
655
0
       || ptr == zend_jit_stub_handlers[jit_stub_mod_by_zero]
656
0
       || ptr == zend_jit_stub_handlers[jit_stub_invalid_this]
657
0
       || ptr == zend_jit_stub_handlers[jit_stub_undefined_function]
658
0
       || ptr == zend_jit_stub_handlers[jit_stub_throw_cannot_pass_by_ref]
659
0
       || ptr == zend_jit_stub_handlers[jit_stub_icall_throw]
660
0
       || ptr == zend_jit_stub_handlers[jit_stub_leave_throw]
661
0
       || ptr == zend_jit_stub_handlers[jit_stub_trace_halt]
662
0
       || ptr == zend_jit_stub_handlers[jit_stub_trace_escape]) {
663
      /* This is a GUARD that trigger exit through a stub code (without deoptimization) */
664
0
      return;
665
0
    }
666
667
    /* Check if we need snapshot entries for polymorphic method call */
668
0
    zend_jit_trace_info *t = jit->trace;
669
0
    uint32_t exit_point = 0, n = 0;
670
671
0
    if (addr < 0) {
672
      /* addr is not always the address of the *last* exit point,
673
       * so we can not optimize this to 'exit_point = t->exit_count-1' */
674
0
      exit_point = zend_jit_exit_point_by_addr(ptr);
675
0
      ZEND_ASSERT(exit_point != -1);
676
0
      if (t->exit_info[exit_point].flags & ZEND_JIT_EXIT_METHOD_CALL) {
677
0
        n = 2;
678
0
      }
679
0
    }
680
681
0
    if (stack_size || n) {
682
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
683
0
      uint32_t snapshot_size, i;
684
685
0
      snapshot_size = stack_size;
686
0
      while (snapshot_size > 0) {
687
0
        ir_ref ref = STACK_REF(stack, snapshot_size - 1);
688
689
0
        if (!ref || ref == IR_NULL || (STACK_FLAGS(stack, snapshot_size - 1) & (/*ZREG_LOAD|*/ZREG_STORE))) {
690
0
          snapshot_size--;
691
0
        } else {
692
0
          break;
693
0
        }
694
0
      }
695
0
      if (snapshot_size || n) {
696
0
        ir_ref snapshot;
697
698
0
        snapshot = ir_SNAPSHOT(snapshot_size + n);
699
0
        for (i = 0; i < snapshot_size; i++) {
700
0
          ir_ref ref = STACK_REF(stack, i);
701
702
0
          if (!ref || ref == IR_NULL || (STACK_FLAGS(stack, i) & (/*ZREG_LOAD|*/ZREG_STORE))) {
703
0
            ref = IR_UNUSED;
704
0
          }
705
0
          ir_SNAPSHOT_SET_OP(snapshot, i + 1, ref);
706
0
        }
707
0
        if (n) {
708
0
          ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 1, t->exit_info[exit_point].poly_func.ref);
709
0
          ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 2, t->exit_info[exit_point].poly_this.ref);
710
0
        }
711
0
      }
712
0
    }
713
0
  }
714
0
}
715
716
static int32_t _add_trace_const(zend_jit_trace_info *t, int64_t val)
717
0
{
718
0
  int32_t i;
719
720
0
  for (i = 0; i < t->consts_count; i++) {
721
0
    if (t->constants[i].i == val) {
722
0
      return i;
723
0
    }
724
0
  }
725
0
  ZEND_ASSERT(i < 0x7fffffff);
726
0
  t->consts_count = i + 1;
727
0
  t->constants = erealloc(t->constants, (i + 1) * sizeof(zend_jit_exit_const));
728
0
  t->constants[i].i = val;
729
0
  return i;
730
0
}
731
732
uint32_t zend_jit_duplicate_exit_point(ir_ctx *ctx, zend_jit_trace_info *t, uint32_t exit_point, ir_ref snapshot_ref)
733
0
{
734
0
  uint32_t stack_size, stack_offset;
735
0
  uint32_t new_exit_point = t->exit_count;
736
737
0
  if (new_exit_point >= ZEND_JIT_TRACE_MAX_EXITS) {
738
0
    ctx->status = -ZEND_JIT_TRACE_STOP_TOO_MANY_EXITS;
739
0
    return exit_point;
740
0
  }
741
742
0
  t->exit_count++;
743
0
  memcpy(&t->exit_info[new_exit_point], &t->exit_info[exit_point], sizeof(zend_jit_trace_exit_info));
744
0
  stack_size = t->exit_info[new_exit_point].stack_size;
745
0
  if (stack_size != 0) {
746
0
    stack_offset = t->stack_map_size;
747
0
    t->stack_map_size += stack_size;
748
    // TODO: reduce number of reallocations ???
749
0
    t->stack_map = erealloc(t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
750
0
    memcpy(t->stack_map + stack_offset, t->stack_map + t->exit_info[new_exit_point].stack_offset, stack_size * sizeof(zend_jit_trace_stack));
751
0
    t->exit_info[new_exit_point].stack_offset = stack_offset;
752
0
  }
753
0
  t->exit_info[new_exit_point].flags &= ~ZEND_JIT_EXIT_FIXED;
754
755
0
  return new_exit_point;
756
0
}
757
758
static void zend_jit_resolve_ref_snapshot(zend_jit_ref_snapshot *dest, ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snapshot, int op)
759
0
{
760
0
  int8_t *reg_ops = ctx->regs[snapshot_ref];
761
0
  ZEND_ASSERT(reg_ops[op] != ZREG_NONE);
762
763
0
  int8_t reg = reg_ops[op];
764
0
  int32_t offset;
765
766
0
  if (IR_REG_SPILLED(reg)) {
767
0
    reg = ((ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FP : IR_REG_SP) | IR_REG_SPILL_LOAD;
768
0
    offset = ir_get_spill_slot_offset(ctx, ir_insn_op(snapshot, op));
769
0
  } else {
770
0
    offset = 0;
771
0
  }
772
773
0
  dest->reg = reg;
774
0
  dest->offset = offset;
775
0
}
776
777
static bool zend_jit_ref_snapshot_equals(const zend_jit_ref_snapshot *a, const zend_jit_ref_snapshot *b)
778
0
{
779
0
  return a->reg == b->reg
780
0
    && (!IR_REG_SPILLED(a->reg) || (a->offset == b->offset));
781
0
}
782
783
void *zend_jit_snapshot_handler(ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snapshot, void *addr)
784
0
{
785
0
  zend_jit_trace_info *t = ((zend_jit_ctx*)ctx)->trace;
786
0
  uint32_t exit_point, exit_flags;
787
0
  ir_ref n = snapshot->inputs_count;
788
0
  ir_ref i;
789
790
0
  exit_point = zend_jit_exit_point_by_addr(addr);
791
0
  ZEND_ASSERT(exit_point < t->exit_count);
792
0
  exit_flags = t->exit_info[exit_point].flags;
793
794
0
  if (exit_flags & ZEND_JIT_EXIT_METHOD_CALL) {
795
0
    zend_jit_ref_snapshot func, this;
796
0
    zend_jit_resolve_ref_snapshot(&func, ctx, snapshot_ref, snapshot, n - 1);
797
0
    zend_jit_resolve_ref_snapshot(&this, ctx, snapshot_ref, snapshot, n);
798
799
0
    if ((exit_flags & ZEND_JIT_EXIT_FIXED)
800
0
     && (!zend_jit_ref_snapshot_equals(&t->exit_info[exit_point].poly_func, &func)
801
0
       || !zend_jit_ref_snapshot_equals(&t->exit_info[exit_point].poly_this, &this))) {
802
0
      exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
803
0
      addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
804
0
      exit_flags &= ~ZEND_JIT_EXIT_FIXED;
805
0
    }
806
0
    t->exit_info[exit_point].poly_func = func;
807
0
    t->exit_info[exit_point].poly_this = this;
808
0
    n -= 2;
809
0
  }
810
811
0
  for (i = 2; i <= n; i++) {
812
0
    ir_ref ref = ir_insn_op(snapshot, i);
813
814
0
    if (ref) {
815
0
      int8_t *reg_ops = ctx->regs[snapshot_ref];
816
0
      int8_t reg = reg_ops[i];
817
0
      ir_ref var = i - 2;
818
819
0
      ZEND_ASSERT(var < t->exit_info[exit_point].stack_size);
820
0
      if (t->stack_map[t->exit_info[exit_point].stack_offset + var].flags == ZREG_ZVAL_COPY) {
821
0
        ZEND_ASSERT(reg != ZREG_NONE);
822
0
        if ((exit_flags & ZEND_JIT_EXIT_FIXED)
823
0
         && t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != IR_REG_NUM(reg)) {
824
0
          exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
825
0
          addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
826
0
          exit_flags &= ~ZEND_JIT_EXIT_FIXED;
827
0
        }
828
0
        t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = IR_REG_NUM(reg);
829
0
      } else if (t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_CONST) {
830
0
        ZEND_ASSERT(t->stack_map[t->exit_info[exit_point].stack_offset + var].type == IS_LONG ||
831
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].type == IS_DOUBLE);
832
833
0
        if (ref > 0) {
834
0
          if (reg != ZREG_NONE) {
835
0
            if (reg & IR_REG_SPILL_LOAD) {
836
0
              ZEND_ASSERT(!(reg & IR_REG_SPILL_SPECIAL));
837
              /* spill slot on a CPU stack */
838
0
              if ((exit_flags & ZEND_JIT_EXIT_FIXED)
839
0
               && (t->stack_map[t->exit_info[exit_point].stack_offset + var].ref != ref
840
0
                || t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
841
0
                || !(t->stack_map[t->exit_info[exit_point].stack_offset + var].flags & ZREG_SPILL_SLOT))) {
842
0
                exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
843
0
                addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
844
0
                exit_flags &= ~ZEND_JIT_EXIT_FIXED;
845
0
              }
846
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].ref = ref;
847
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = ZREG_NONE;
848
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].flags |= ZREG_SPILL_SLOT;
849
0
            } else if (reg & IR_REG_SPILL_SPECIAL) {
850
              /* spill slot on a VM stack */
851
0
              if ((exit_flags & ZEND_JIT_EXIT_FIXED)
852
0
               && (t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
853
0
               || t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_TYPE_ONLY)) {
854
0
                exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
855
0
                addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
856
0
                exit_flags &= ~ZEND_JIT_EXIT_FIXED;
857
0
              }
858
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = ZREG_NONE;
859
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_TYPE_ONLY;
860
0
            } else {
861
0
              if ((exit_flags & ZEND_JIT_EXIT_FIXED)
862
0
               && t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != IR_REG_NUM(reg)) {
863
0
                exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
864
0
                addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
865
0
                exit_flags &= ~ZEND_JIT_EXIT_FIXED;
866
0
              }
867
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = IR_REG_NUM(reg);
868
0
            }
869
0
          } else {
870
0
            if ((exit_flags & ZEND_JIT_EXIT_FIXED)
871
0
             && (t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
872
0
             || t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_TYPE_ONLY)) {
873
0
              exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
874
0
              addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
875
0
              exit_flags &= ~ZEND_JIT_EXIT_FIXED;
876
0
            }
877
0
            t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_TYPE_ONLY;
878
0
          }
879
0
        } else if (!(exit_flags & ZEND_JIT_EXIT_FIXED)) {
880
0
          int32_t idx = _add_trace_const(t, ctx->ir_base[ref].val.i64);
881
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_CONST;
882
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].ref = idx;
883
0
        }
884
0
      }
885
0
    }
886
0
  }
887
0
  t->exit_info[exit_point].flags |= ZEND_JIT_EXIT_FIXED;
888
0
  return addr;
889
0
}
890
891
static void jit_SIDE_EXIT(zend_jit_ctx *jit, ir_ref addr)
892
0
{
893
0
  jit_SNAPSHOT(jit, addr);
894
0
  ir_IJMP(addr);
895
0
}
896
897
/* PHP JIT helpers */
898
899
static ir_ref jit_EMALLOC(zend_jit_ctx *jit, size_t size, const zend_op_array *op_array, const zend_op *opline)
900
0
{
901
0
#if ZEND_DEBUG
902
0
  return ir_CALL_5(IR_ADDR, ir_CONST_FC_FUNC(_emalloc),
903
0
    ir_CONST_ADDR(size),
904
0
    op_array->filename ? ir_CONST_ADDR(op_array->filename->val) : IR_NULL,
905
0
    ir_CONST_U32(opline ? opline->lineno : 0),
906
0
    IR_NULL,
907
0
    ir_CONST_U32(0));
908
#elif defined(HAVE_BUILTIN_CONSTANT_P)
909
  if (size > 24 && size <= 32) {
910
    return ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(_emalloc_32));
911
  } else {
912
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_emalloc), ir_CONST_ADDR(size));
913
  }
914
#else
915
  return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_emalloc), ir_CONST_ADDR(size));
916
#endif
917
0
}
918
919
static ir_ref jit_EFREE(zend_jit_ctx *jit, ir_ref ptr, size_t size, const zend_op_array *op_array, const zend_op *opline)
920
0
{
921
0
#if ZEND_DEBUG
922
0
  return ir_CALL_5(IR_ADDR, ir_CONST_FC_FUNC(_efree),
923
0
    ptr,
924
0
    op_array && op_array->filename ? ir_CONST_ADDR(op_array->filename->val) : IR_NULL,
925
0
    ir_CONST_U32(opline ? opline->lineno : 0),
926
0
    IR_NULL,
927
0
    ir_CONST_U32(0));
928
#elif defined(HAVE_BUILTIN_CONSTANT_P)
929
  if (size > 24 && size <= 32) {
930
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree_32), ptr);
931
  } else {
932
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree), ptr);
933
  }
934
#else
935
  return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree), ptr);
936
#endif
937
0
}
938
939
static ir_ref jit_FP(zend_jit_ctx *jit)
940
0
{
941
0
  ZEND_ASSERT(jit->ctx.control);
942
0
  if (jit->fp == IR_UNUSED) {
943
    /* Emit "RLOAD FP" once for basic block */
944
0
    jit->fp = ir_RLOAD_A(ZREG_FP);
945
0
  } else {
946
0
    ir_insn *insn;
947
0
    ir_ref ref = jit->ctx.control;
948
949
0
    while (1) {
950
0
      if (ref == jit->fp) {
951
0
        break;
952
0
      }
953
0
      insn = &jit->ctx.ir_base[ref];
954
0
      if (insn->op >= IR_START || insn->op == IR_CALL) {
955
0
        jit->fp = ir_RLOAD_A(ZREG_FP);
956
0
        break;
957
0
      }
958
0
      ref = insn->op1;
959
0
    }
960
0
  }
961
0
  return jit->fp;
962
0
}
963
964
static void jit_STORE_FP(zend_jit_ctx *jit, ir_ref ref)
965
0
{
966
0
  ir_RSTORE(ZREG_FP, ref);
967
0
  jit->fp = IR_UNUSED;
968
0
}
969
970
static ir_ref jit_IP(zend_jit_ctx *jit)
971
0
{
972
0
  return ir_RLOAD_A(ZREG_IP);
973
0
}
974
975
static void jit_STORE_IP(zend_jit_ctx *jit, ir_ref ref)
976
0
{
977
0
  ir_RSTORE(ZREG_IP, ref);
978
0
}
979
980
static ir_ref jit_IP32(zend_jit_ctx *jit)
981
0
{
982
0
  return ir_RLOAD_U32(ZREG_IP);
983
0
}
984
985
static void jit_LOAD_IP_ADDR(zend_jit_ctx *jit, const zend_op *target)
986
0
{
987
0
  jit_STORE_IP(jit, ir_CONST_ADDR(target));
988
0
}
989
990
static void zend_jit_track_last_valid_opline(zend_jit_ctx *jit)
991
0
{
992
0
  jit->use_last_valid_opline = false;
993
0
  jit->track_last_valid_opline = true;
994
0
}
995
996
static void zend_jit_use_last_valid_opline(zend_jit_ctx *jit)
997
0
{
998
0
  if (jit->track_last_valid_opline) {
999
0
    jit->use_last_valid_opline = true;
1000
0
    jit->track_last_valid_opline = false;
1001
0
  }
1002
0
}
1003
1004
static bool zend_jit_trace_uses_initial_ip(zend_jit_ctx *jit)
1005
0
{
1006
0
  return jit->use_last_valid_opline;
1007
0
}
1008
1009
static void zend_jit_set_last_valid_opline(zend_jit_ctx *jit, const zend_op *opline)
1010
0
{
1011
0
  if (!jit->reuse_ip) {
1012
0
    jit->track_last_valid_opline = true;
1013
0
    jit->last_valid_opline = opline;
1014
0
  }
1015
0
}
1016
1017
static void zend_jit_reset_last_valid_opline(zend_jit_ctx *jit)
1018
0
{
1019
0
  jit->track_last_valid_opline = false;
1020
0
  jit->last_valid_opline = NULL;
1021
0
}
1022
1023
static void zend_jit_start_reuse_ip(zend_jit_ctx *jit)
1024
0
{
1025
0
  zend_jit_reset_last_valid_opline(jit);
1026
0
  jit->reuse_ip = true;
1027
0
}
1028
1029
static int zend_jit_reuse_ip(zend_jit_ctx *jit)
1030
0
{
1031
0
  if (!jit->reuse_ip) {
1032
0
    zend_jit_start_reuse_ip(jit);
1033
    // RX = EX(call);
1034
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(call)));
1035
0
  }
1036
0
  return 1;
1037
0
}
1038
1039
static void zend_jit_stop_reuse_ip(zend_jit_ctx *jit)
1040
0
{
1041
0
  jit->reuse_ip = false;
1042
0
}
1043
1044
static int zend_jit_save_call_chain(zend_jit_ctx *jit, uint32_t call_level)
1045
0
{
1046
0
  ir_ref rx, call;
1047
1048
0
  if (call_level == 1) {
1049
    // JIT: call = NULL;
1050
0
    call = IR_NULL;
1051
0
  } else {
1052
    // JIT: call = EX(call);
1053
0
    call = ir_LOAD_A(jit_EX(call));
1054
0
  }
1055
1056
0
  rx = jit_IP(jit);
1057
1058
  // JIT: call->prev_execute_data = call;
1059
0
  ir_STORE(jit_CALL(rx, prev_execute_data), call);
1060
1061
  // JIT: EX(call) = call;
1062
0
  ir_STORE(jit_EX(call), rx);
1063
1064
0
  jit->delayed_call_level = 0;
1065
0
  delayed_call_chain = false;
1066
1067
0
  return 1;
1068
0
}
1069
1070
static int zend_jit_set_ip(zend_jit_ctx *jit, const zend_op *target)
1071
0
{
1072
0
  ir_ref ref;
1073
1074
0
  if (jit->delayed_call_level) {
1075
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
1076
0
      return 0;
1077
0
    }
1078
0
  }
1079
1080
0
  if (jit->last_valid_opline) {
1081
0
    zend_jit_use_last_valid_opline(jit);
1082
0
    if (jit->last_valid_opline != target) {
1083
0
      ref = jit_IP(jit);
1084
0
      if (target > jit->last_valid_opline) {
1085
0
        ref = ir_ADD_OFFSET(ref, (uintptr_t)target - (uintptr_t)jit->last_valid_opline);
1086
0
      } else {
1087
0
        ref = ir_SUB_A(ref, ir_CONST_ADDR((uintptr_t)jit->last_valid_opline - (uintptr_t)target));
1088
0
      }
1089
0
      jit_STORE_IP(jit, ref);
1090
0
    }
1091
0
  } else {
1092
0
    jit_STORE_IP(jit, ir_CONST_ADDR(target));
1093
0
  }
1094
0
  jit->reuse_ip = false;
1095
0
  zend_jit_set_last_valid_opline(jit, target);
1096
0
  return 1;
1097
0
}
1098
1099
static void jit_SET_EX_OPLINE(zend_jit_ctx *jit, const zend_op *target)
1100
0
{
1101
0
  if (jit->last_valid_opline == target) {
1102
0
    zend_jit_use_last_valid_opline(jit);
1103
    // EX(opline) = opline
1104
0
    ir_STORE(jit_EX(opline), jit_IP(jit));
1105
0
  } else {
1106
0
    ir_STORE(jit_EX(opline), ir_CONST_ADDR(target));
1107
0
  }
1108
0
}
1109
1110
static ir_ref jit_ZVAL_ADDR(zend_jit_ctx *jit, zend_jit_addr addr)
1111
0
{
1112
0
  if (Z_MODE(addr) == IS_MEM_ZVAL) {
1113
0
    ir_ref reg;
1114
1115
0
    if (Z_REG(addr) == ZREG_FP) {
1116
0
      reg = jit_FP(jit);
1117
0
    } else if (Z_REG(addr) == ZREG_RX) {
1118
0
      reg = jit_IP(jit);
1119
0
    } else {
1120
0
      ZEND_UNREACHABLE();
1121
0
    }
1122
0
    return ir_ADD_OFFSET(reg, Z_OFFSET(addr));
1123
0
  } else if (Z_MODE(addr) == IS_REF_ZVAL) {
1124
0
    return Z_IR_REF(addr);
1125
0
  } else {
1126
0
    ZEND_ASSERT(Z_MODE(addr) == IS_CONST_ZVAL);
1127
0
    return ir_CONST_ADDR(Z_ZV(addr));
1128
0
  }
1129
0
}
1130
1131
static ir_ref jit_Z_TYPE_ref(zend_jit_ctx *jit, ir_ref ref)
1132
0
{
1133
0
  return ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zval, u1.v.type)));
1134
0
}
1135
1136
static ir_ref jit_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr)
1137
0
{
1138
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1139
0
    return ir_CONST_U8(Z_TYPE_P(Z_ZV(addr)));
1140
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1141
0
    ir_ref reg;
1142
1143
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1144
0
    if (Z_REG(addr) == ZREG_FP) {
1145
0
      reg = jit_FP(jit);
1146
0
    } else if (Z_REG(addr) == ZREG_RX) {
1147
0
      reg = jit_IP(jit);
1148
0
    } else {
1149
0
      ZEND_UNREACHABLE();
1150
0
    }
1151
0
    return ir_LOAD_U8(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.v.type)));
1152
0
  } else {
1153
0
    return jit_Z_TYPE_ref(jit, jit_ZVAL_ADDR(jit, addr));
1154
0
  }
1155
0
}
1156
1157
static ir_ref jit_Z_TYPE_FLAGS_ref(zend_jit_ctx *jit, ir_ref ref)
1158
0
{
1159
0
  return ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zval, u1.v.type_flags)));
1160
0
}
1161
1162
static ir_ref jit_Z_TYPE_FLAGS(zend_jit_ctx *jit, zend_jit_addr addr)
1163
0
{
1164
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1165
0
    return ir_CONST_U8(Z_TYPE_FLAGS_P(Z_ZV(addr)));
1166
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1167
0
    ir_ref reg;
1168
1169
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1170
0
    if (Z_REG(addr) == ZREG_FP) {
1171
0
      reg = jit_FP(jit);
1172
0
    } else if (Z_REG(addr) == ZREG_RX) {
1173
0
      reg = jit_IP(jit);
1174
0
    } else {
1175
0
      ZEND_UNREACHABLE();
1176
0
    }
1177
0
    return ir_LOAD_U8(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.v.type_flags)));
1178
0
  } else {
1179
0
    return jit_Z_TYPE_FLAGS_ref(jit, jit_ZVAL_ADDR(jit, addr));
1180
0
  }
1181
0
}
1182
1183
static ir_ref jit_Z_TYPE_INFO_ref(zend_jit_ctx *jit, ir_ref ref)
1184
0
{
1185
0
  return ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zval, u1.type_info)));
1186
0
}
1187
1188
static ir_ref jit_Z_TYPE_INFO(zend_jit_ctx *jit, zend_jit_addr addr)
1189
0
{
1190
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1191
0
    return ir_CONST_U32(Z_TYPE_INFO_P(Z_ZV(addr)));
1192
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1193
0
    ir_ref reg;
1194
1195
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1196
0
    if (Z_REG(addr) == ZREG_FP) {
1197
0
      reg = jit_FP(jit);
1198
0
    } else if (Z_REG(addr) == ZREG_RX) {
1199
0
      reg = jit_IP(jit);
1200
0
    } else {
1201
0
      ZEND_UNREACHABLE();
1202
0
    }
1203
0
    return ir_LOAD_U32(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.type_info)));
1204
0
  } else {
1205
0
    return jit_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, addr));
1206
0
  }
1207
0
}
1208
1209
static void jit_set_Z_TYPE_INFO_ref(zend_jit_ctx *jit, ir_ref ref, ir_ref type_info)
1210
0
{
1211
0
  ir_STORE(ir_ADD_OFFSET(ref, offsetof(zval, u1.type_info)), type_info);
1212
0
}
1213
1214
static void jit_set_Z_TYPE_INFO_ex(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref type_info)
1215
0
{
1216
0
  if (Z_MODE(addr) == IS_MEM_ZVAL) {
1217
0
    ir_ref reg;
1218
1219
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1220
0
    if (Z_REG(addr) == ZREG_FP) {
1221
0
      reg = jit_FP(jit);
1222
0
    } else if (Z_REG(addr) == ZREG_RX) {
1223
0
      reg = jit_IP(jit);
1224
0
    } else {
1225
0
      ZEND_UNREACHABLE();
1226
0
    }
1227
0
    ir_STORE(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.type_info)), type_info);
1228
0
  } else {
1229
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, addr), type_info);
1230
0
  }
1231
0
}
1232
1233
static void jit_set_Z_TYPE_INFO(zend_jit_ctx *jit, zend_jit_addr addr, uint32_t type_info)
1234
0
{
1235
0
  if (type_info < IS_STRING
1236
0
   && Z_MODE(addr) == IS_MEM_ZVAL
1237
0
   && Z_REG(addr) == ZREG_FP
1238
0
   && JIT_G(current_frame)
1239
0
   && STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(addr))) == type_info) {
1240
    /* type is already set */
1241
0
    return;
1242
0
  }
1243
0
  jit_set_Z_TYPE_INFO_ex(jit, addr, ir_CONST_U32(type_info));
1244
0
}
1245
1246
static ir_ref jit_if_Z_TYPE_ref(zend_jit_ctx *jit, ir_ref ref, ir_ref type)
1247
0
{
1248
0
  return ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), type));
1249
0
}
1250
1251
static ir_ref jit_if_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type)
1252
0
{
1253
0
  ZEND_ASSERT(type != IS_UNDEF);
1254
0
  return ir_IF(ir_EQ(jit_Z_TYPE(jit, addr), ir_CONST_U8(type)));
1255
0
}
1256
1257
static ir_ref jit_if_not_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type)
1258
0
{
1259
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1260
1261
0
  if (type != IS_UNDEF) {
1262
0
    ref = ir_NE(ref, ir_CONST_U8(type));
1263
0
  }
1264
0
  return ir_IF(ref);
1265
0
}
1266
1267
static void jit_guard_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type, const void *exit_addr)
1268
0
{
1269
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1270
1271
0
  if (type != IS_UNDEF) {
1272
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(type)), ir_CONST_ADDR(exit_addr));
1273
0
  } else {
1274
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
1275
0
  }
1276
0
}
1277
1278
static void jit_guard_not_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type, const void *exit_addr)
1279
0
{
1280
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1281
1282
0
  if (type != IS_UNDEF) {
1283
0
    ref = ir_NE(ref, ir_CONST_U8(type));
1284
0
  }
1285
0
  ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
1286
0
}
1287
1288
static ir_ref jit_if_REFCOUNTED(zend_jit_ctx *jit, zend_jit_addr addr)
1289
0
{
1290
0
  return ir_IF(jit_Z_TYPE_FLAGS(jit, addr));
1291
0
}
1292
1293
static ir_ref jit_if_COLLECTABLE_ref(zend_jit_ctx *jit, ir_ref addr_ref)
1294
0
{
1295
0
  return ir_IF(ir_AND_U8(jit_Z_TYPE_FLAGS_ref(jit, addr_ref), ir_CONST_U8(IS_TYPE_COLLECTABLE)));
1296
0
}
1297
1298
static ir_ref jit_Z_LVAL_ref(zend_jit_ctx *jit, ir_ref ref)
1299
0
{
1300
0
  return ir_LOAD_L(ref);
1301
0
}
1302
1303
static ir_ref jit_Z_DVAL_ref(zend_jit_ctx *jit, ir_ref ref)
1304
0
{
1305
0
  return ir_LOAD_D(ref);
1306
0
}
1307
1308
static bool zend_jit_spilling_may_cause_conflict(zend_jit_ctx *jit, int var, ir_ref val)
1309
0
{
1310
0
  if (jit->ctx.ir_base[val].op == IR_RLOAD) {
1311
    /* Deoptimization */
1312
0
    return false;
1313
0
  }
1314
//  if (jit->ctx.ir_base[val].op == IR_LOAD
1315
//   && jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op == IR_ADD
1316
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op == IR_RLOAD
1317
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op2 == ZREG_FP
1318
//   && IR_IS_CONST_REF(jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2)
1319
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2].val.addr == (uintptr_t)EX_NUM_TO_VAR(jit->ssa->vars[var].var)) {
1320
//    /* LOAD from the same location (the LOAD is pinned) */
1321
//    // TODO: should be anti-dependent with the following stores ???
1322
//    return 0;
1323
//  }
1324
0
  if (jit->ssa->vars[var].var < jit->current_op_array->last_var) {
1325
    /* IS_CV */
1326
0
    if (jit->ctx.ir_base[val].op == IR_LOAD
1327
0
     && jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op == IR_ADD
1328
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op == IR_RLOAD
1329
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op2 == ZREG_FP
1330
0
     && IR_IS_CONST_REF(jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2)
1331
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2].val.addr != (uintptr_t)EX_NUM_TO_VAR(jit->ssa->vars[var].var)
1332
0
     && EX_VAR_TO_NUM(jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2].val.addr) < jit->current_op_array->last_var) {
1333
      /* binding between different CVs may cause spill conflict */
1334
0
      return true;
1335
0
    } else if (jit->ssa->vars[var].definition >= 0
1336
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_def == var
1337
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_use >= 0
1338
0
     && jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].no_val
1339
0
     && jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].definition_phi
1340
0
     && (jit->ssa->cfg.blocks[jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].definition_phi->block].flags & ZEND_BB_LOOP_HEADER)) {
1341
      /* Avoid moving spill store out of loop */
1342
0
      return true;
1343
0
    } else if (jit->ssa->vars[var].definition >= 0
1344
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_def == var
1345
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_use >= 0
1346
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op2_use >= 0
1347
0
     && jit->ra[jit->ssa->ops[jit->ssa->vars[var].definition].op2_use].ref == val) {
1348
      /* Avoid spill conflict between of ASSIGN.op1_def and ASSIGN.op1_use */
1349
0
      return true;
1350
0
    }
1351
0
    return false;
1352
0
  }
1353
0
  return true;
1354
0
}
1355
1356
static void zend_jit_def_reg(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref val)
1357
0
{
1358
0
  int var;
1359
1360
0
  ZEND_ASSERT(Z_MODE(addr) == IS_REG);
1361
0
  var = Z_SSA_VAR(addr);
1362
0
  if (var == jit->delay_var) {
1363
0
    ir_refs_add(jit->delay_refs, val);
1364
0
    return;
1365
0
  }
1366
0
  ZEND_ASSERT(jit->ra && jit->ra[var].ref == IR_NULL);
1367
1368
  /* Negative "var" has special meaning for IR */
1369
0
  if (val > 0) {
1370
0
    if (jit->ctx.binding) {
1371
0
      ir_ref old = ir_binding_find(&jit->ctx, val);
1372
0
      if (old && old != -EX_NUM_TO_VAR(jit->ssa->vars[var].var)) {
1373
0
        val = ir_emit2(&jit->ctx, IR_OPT(IR_COPY, jit->ctx.ir_base[val].type), val, 1);
1374
0
      }
1375
0
    }
1376
0
    if (!zend_jit_spilling_may_cause_conflict(jit, var, val)) {
1377
0
      val = ir_bind(&jit->ctx, -EX_NUM_TO_VAR(jit->ssa->vars[var].var), val);
1378
0
    }
1379
0
  }
1380
0
  jit->ra[var].ref = val;
1381
1382
0
  if (jit->ra[var].flags & ZREG_FORWARD) {
1383
0
    zend_ssa_phi *phi = jit->ssa->vars[var].phi_use_chain;
1384
0
    zend_basic_block *bb;
1385
0
    int n, j, *p;
1386
0
    ir_ref *q;
1387
1388
0
    jit->ra[var].flags &= ~ZREG_FORWARD;
1389
0
    while (phi != NULL) {
1390
0
      zend_ssa_phi *dst_phi = phi;
1391
0
      int src_var = var;
1392
1393
0
      if (dst_phi->pi >= 0) {
1394
0
        jit->ra[src_var].ref = val;
1395
0
        src_var = dst_phi->ssa_var;
1396
0
        if (!(jit->ra[src_var].flags & ZREG_FORWARD)) {
1397
0
          phi = zend_ssa_next_use_phi(jit->ssa, var, phi);
1398
0
          continue;
1399
0
        }
1400
0
        dst_phi = jit->ssa->vars[src_var].phi_use_chain;
1401
0
        ZEND_ASSERT(dst_phi != NULL && "reg forwarding");
1402
0
        ZEND_ASSERT(!zend_ssa_next_use_phi(jit->ssa, src_var, dst_phi) && "reg forwarding");
1403
0
        jit->ra[src_var].flags &= ~ZREG_FORWARD;
1404
0
      }
1405
1406
0
      if (jit->ra[dst_phi->ssa_var].ref > 0) {
1407
0
        ir_insn *phi_insn = &jit->ctx.ir_base[jit->ra[dst_phi->ssa_var].ref];
1408
0
        if (phi_insn->op == IR_PHI) {
1409
//          ZEND_ASSERT(ir_operands_count(ctx, phi_insn) == n + 1);
1410
0
          bb = &jit->ssa->cfg.blocks[dst_phi->block];
1411
0
          n = bb->predecessors_count;
1412
0
          for (j = 0, p = &dst_phi->sources[0], q = phi_insn->ops + 2; j < n; j++, p++, q++) {
1413
0
            if (*p == src_var) {
1414
0
              *q = val;
1415
0
            }
1416
0
          }
1417
0
        }
1418
0
      }
1419
1420
0
      phi = zend_ssa_next_use_phi(jit->ssa, var, phi);
1421
0
    }
1422
0
  }
1423
0
}
1424
1425
static ir_ref zend_jit_use_reg(zend_jit_ctx *jit, zend_jit_addr addr)
1426
0
{
1427
0
  int var = Z_SSA_VAR(addr);
1428
1429
0
  ZEND_ASSERT(Z_MODE(addr) == IS_REG);
1430
0
  ZEND_ASSERT(jit->ra && jit->ra[var].ref);
1431
0
  if (jit->ra[var].ref == IR_NULL) {
1432
0
    zend_jit_addr mem_addr;
1433
0
    ir_ref ref;
1434
1435
0
    ZEND_ASSERT(jit->ra[var].flags & ZREG_LOAD);
1436
0
    mem_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(jit->ssa->vars[var].var));
1437
0
    if ((jit->ssa->var_info[var].type & MAY_BE_ANY) == MAY_BE_LONG) {
1438
0
      ref = jit_Z_LVAL_ref(jit, jit_ZVAL_ADDR(jit, mem_addr));
1439
0
    } else if ((jit->ssa->var_info[var].type & MAY_BE_ANY) == MAY_BE_DOUBLE) {
1440
0
      ref = jit_Z_DVAL_ref(jit, jit_ZVAL_ADDR(jit, mem_addr));
1441
0
    } else {
1442
0
      ZEND_UNREACHABLE();
1443
0
    }
1444
0
    zend_jit_def_reg(jit, addr, ref);
1445
0
    return ref;
1446
0
  }
1447
0
  return jit->ra[Z_SSA_VAR(addr)].ref;
1448
0
}
1449
1450
static void zend_jit_gen_pi(zend_jit_ctx *jit, zend_ssa_phi *phi)
1451
0
{
1452
0
  int src_var = phi->sources[0];
1453
0
  int dst_var = phi->ssa_var;
1454
1455
0
  ZEND_ASSERT(phi->pi >= 0);
1456
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
1457
0
  ZEND_ASSERT(jit->ra[src_var].ref);
1458
1459
0
  if (jit->ra[src_var].ref == IR_NULL) {
1460
    /* Not defined yet */
1461
0
    if (jit->ssa->vars[dst_var].use_chain < 0
1462
0
     && jit->ssa->vars[dst_var].phi_use_chain) {
1463
0
      zend_ssa_phi *phi = jit->ssa->vars[dst_var].phi_use_chain;
1464
0
      if (!zend_ssa_next_use_phi(jit->ssa, dst_var, phi)) {
1465
        /* This is a Pi forwarded to Phi */
1466
0
        jit->ra[src_var].flags |= ZREG_FORWARD;
1467
0
        return;
1468
0
      }
1469
0
    }
1470
0
    ZEND_ASSERT(0 && "Not defined Pi source");
1471
0
  }
1472
  /* Reuse register */
1473
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var),
1474
0
    zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var)));
1475
0
}
1476
1477
static void zend_jit_gen_phi(zend_jit_ctx *jit, zend_ssa_phi *phi)
1478
0
{
1479
0
  int dst_var = phi->ssa_var;
1480
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[phi->block];
1481
0
  int n = bb->predecessors_count;
1482
0
  int i;
1483
0
  ir_type type = (jit->ssa->var_info[phi->ssa_var].type & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE;
1484
0
  ir_ref merge = jit->bb_start_ref[phi->block];
1485
0
  ir_ref ref;
1486
0
  ir_ref old_insns_count = jit->ctx.insns_count;
1487
0
  ir_ref same_src_ref = IR_UNUSED;
1488
0
  bool phi_inputs_are_the_same = true;
1489
1490
0
  ZEND_ASSERT(phi->pi < 0);
1491
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
1492
0
  ZEND_ASSERT(merge);
1493
0
  ZEND_ASSERT(jit->ctx.ir_base[merge].op == IR_MERGE || jit->ctx.ir_base[merge].op == IR_LOOP_BEGIN);
1494
0
  ZEND_ASSERT(n == jit->ctx.ir_base[merge].inputs_count);
1495
1496
0
  ref = ir_emit_N(&jit->ctx, IR_OPT(IR_PHI, type), n + 1);
1497
0
  ir_set_op(&jit->ctx, ref, 1, merge);
1498
1499
0
  for (i = 0; i < n; i++) {
1500
0
    int src_var = phi->sources[i];
1501
1502
0
    ZEND_ASSERT(jit->ra[src_var].ref);
1503
0
    if (jit->ra[src_var].ref == IR_NULL) {
1504
0
      jit->ra[src_var].flags |= ZREG_FORWARD;
1505
0
      phi_inputs_are_the_same = false;
1506
0
    } else {
1507
0
      ir_ref src_ref = zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var));
1508
0
      if (i == 0) {
1509
0
        same_src_ref = src_ref;
1510
0
      } else if (same_src_ref != src_ref) {
1511
0
        phi_inputs_are_the_same = false;
1512
0
      }
1513
0
      ir_set_op(&jit->ctx, ref, i + 2, src_ref);
1514
0
    }
1515
0
  }
1516
0
  if (phi_inputs_are_the_same) {
1517
0
    ref = same_src_ref;
1518
0
    jit->ctx.insns_count = old_insns_count;
1519
0
  }
1520
1521
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var), ref);
1522
0
}
1523
1524
static ir_ref jit_Z_LVAL(zend_jit_ctx *jit, zend_jit_addr addr)
1525
0
{
1526
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1527
0
    return ir_CONST_LONG(Z_LVAL_P(Z_ZV(addr)));
1528
0
  } else if (Z_MODE(addr) == IS_REG) {
1529
0
    return zend_jit_use_reg(jit, addr);
1530
0
  } else {
1531
0
    return jit_Z_LVAL_ref(jit, jit_ZVAL_ADDR(jit, addr));
1532
0
  }
1533
0
}
1534
1535
static void jit_set_Z_LVAL(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref lval)
1536
0
{
1537
0
  if (Z_MODE(addr) == IS_REG) {
1538
0
    zend_jit_def_reg(jit, addr, lval);
1539
0
  } else {
1540
0
    ir_STORE(jit_ZVAL_ADDR(jit, addr), lval);
1541
0
  }
1542
0
}
1543
1544
#if SIZEOF_ZEND_LONG == 4
1545
static ir_ref jit_Z_W2(zend_jit_ctx *jit, zend_jit_addr addr)
1546
{
1547
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1548
    return ir_CONST_U32((Z_ZV(addr))->value.ww.w2);
1549
  } else {
1550
    return ir_LOAD_L(ir_ADD_OFFSET(jit_ZVAL_ADDR(jit, addr), offsetof(zval, value.ww.w2)));
1551
  }
1552
}
1553
1554
static void jit_set_Z_W2(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref lval)
1555
{
1556
  ir_STORE(ir_ADD_OFFSET(jit_ZVAL_ADDR(jit, addr), offsetof(zval, value.ww.w2)), lval);
1557
}
1558
#endif
1559
1560
static ir_ref jit_Z_DVAL(zend_jit_ctx *jit, zend_jit_addr addr)
1561
0
{
1562
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1563
0
    return ir_CONST_DOUBLE(Z_DVAL_P(Z_ZV(addr)));
1564
0
  } else if (Z_MODE(addr) == IS_REG) {
1565
0
    return zend_jit_use_reg(jit, addr);
1566
0
  } else {
1567
0
    return jit_Z_DVAL_ref(jit, jit_ZVAL_ADDR(jit, addr));
1568
0
  }
1569
0
}
1570
1571
static void jit_set_Z_DVAL(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref dval)
1572
0
{
1573
0
  if (Z_MODE(addr) == IS_REG) {
1574
0
    zend_jit_def_reg(jit, addr, dval);
1575
0
  } else {
1576
0
    ir_STORE(jit_ZVAL_ADDR(jit, addr), dval);
1577
0
  }
1578
0
}
1579
1580
static ir_ref jit_Z_PTR_ref(zend_jit_ctx *jit, ir_ref ref)
1581
0
{
1582
0
  return ir_LOAD_A(ref);
1583
0
}
1584
1585
static ir_ref jit_Z_PTR(zend_jit_ctx *jit, zend_jit_addr addr)
1586
0
{
1587
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1588
0
    return ir_CONST_ADDR(Z_PTR_P(Z_ZV(addr)));
1589
0
  } else {
1590
0
    return jit_Z_PTR_ref(jit, jit_ZVAL_ADDR(jit, addr));
1591
0
  }
1592
0
}
1593
1594
static void jit_set_Z_PTR(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref ptr)
1595
0
{
1596
0
  ir_STORE(jit_ZVAL_ADDR(jit, addr), ptr);
1597
0
}
1598
1599
static ir_ref jit_GC_REFCOUNT(zend_jit_ctx *jit, ir_ref ref)
1600
0
{
1601
0
  return ir_LOAD_U32(ref);
1602
0
}
1603
1604
static void jit_set_GC_REFCOUNT(zend_jit_ctx *jit, ir_ref ref, uint32_t refcount)
1605
0
{
1606
0
  ir_STORE(ref, ir_CONST_U32(refcount));
1607
0
}
1608
1609
static void jit_GC_ADDREF(zend_jit_ctx *jit, ir_ref ref)
1610
0
{
1611
0
  ir_STORE(ref, ir_ADD_U32(ir_LOAD_U32(ref), ir_CONST_U32(1)));
1612
0
}
1613
1614
static void jit_GC_ADDREF2(zend_jit_ctx *jit, ir_ref ref)
1615
0
{
1616
0
  ir_ref counter = ir_LOAD_U32(ref);
1617
0
  ir_STORE(ref, ir_ADD_U32(counter, ir_CONST_U32(2)));
1618
0
}
1619
1620
static ir_ref jit_GC_DELREF(zend_jit_ctx *jit, ir_ref ref)
1621
0
{
1622
0
  ir_ref counter = ir_LOAD_U32(ref);
1623
0
  counter = ir_SUB_U32(counter, ir_CONST_U32(1));
1624
0
  ir_STORE(ref, counter);
1625
0
  return counter;
1626
0
}
1627
1628
static ir_ref jit_if_GC_MAY_NOT_LEAK(zend_jit_ctx *jit, ir_ref ref)
1629
0
{
1630
0
  return ir_IF(
1631
0
    ir_AND_U32(
1632
0
      ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_refcounted, gc.u.type_info))),
1633
0
      ir_CONST_U32(GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))));
1634
0
}
1635
1636
static void jit_ZVAL_COPY_CONST(zend_jit_ctx *jit, zend_jit_addr dst, uint32_t dst_info, uint32_t dst_def_info, zval *zv, bool addref)
1637
0
{
1638
0
  ir_ref ref = IR_UNUSED;
1639
1640
0
  if (Z_TYPE_P(zv) > IS_TRUE) {
1641
0
    if (Z_TYPE_P(zv) == IS_DOUBLE) {
1642
0
      jit_set_Z_DVAL(jit, dst, ir_CONST_DOUBLE(Z_DVAL_P(zv)));
1643
0
    } else if (Z_TYPE_P(zv) == IS_LONG && dst_def_info == MAY_BE_DOUBLE) {
1644
0
      jit_set_Z_DVAL(jit, dst, ir_CONST_DOUBLE((double)Z_LVAL_P(zv)));
1645
0
    } else if (Z_TYPE_P(zv) == IS_LONG) {
1646
0
      jit_set_Z_LVAL(jit, dst, ir_CONST_LONG(Z_LVAL_P(zv)));
1647
0
    } else {
1648
0
      ref = ir_CONST_ADDR(Z_PTR_P(zv));
1649
0
      jit_set_Z_PTR(jit, dst, ref);
1650
0
      if (addref && Z_REFCOUNTED_P(zv)) {
1651
0
        jit_GC_ADDREF(jit, ref);
1652
0
      }
1653
0
    }
1654
0
  }
1655
0
  if (Z_MODE(dst) != IS_REG) {
1656
0
    if (dst_def_info == MAY_BE_DOUBLE) {
1657
0
      if ((dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
1658
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
1659
0
      }
1660
0
    } else if (((dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (1<<Z_TYPE_P(zv))) || (dst_info & (MAY_BE_STRING|MAY_BE_ARRAY)) != 0) {
1661
0
      jit_set_Z_TYPE_INFO(jit, dst, Z_TYPE_INFO_P(zv));
1662
0
    }
1663
0
  }
1664
0
}
1665
1666
static ir_ref jit_if_TYPED_REF(zend_jit_ctx *jit, ir_ref ref)
1667
0
{
1668
0
  return ir_IF(ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_reference, sources.ptr))));
1669
0
}
1670
1671
static void jit_ZVAL_COPY(zend_jit_ctx *jit, zend_jit_addr dst, uint32_t dst_info, zend_jit_addr src, uint32_t src_info, bool addref)
1672
0
{
1673
0
  ir_ref ref = IR_UNUSED;
1674
1675
0
  if (src_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
1676
0
    if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_LONG) {
1677
0
      jit_set_Z_LVAL(jit, dst, jit_Z_LVAL(jit, src));
1678
0
    } else if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_DOUBLE) {
1679
0
      jit_set_Z_DVAL(jit, dst, jit_Z_DVAL(jit, src));
1680
0
    } else {
1681
#if SIZEOF_ZEND_LONG == 4
1682
      if (src_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) {
1683
        jit_set_Z_W2(jit, dst, jit_Z_W2(jit, src));
1684
      }
1685
#endif
1686
0
      ref = jit_Z_PTR(jit, src);
1687
0
      jit_set_Z_PTR(jit, dst, ref);
1688
0
    }
1689
0
  }
1690
0
  if (has_concrete_type(src_info & MAY_BE_ANY)
1691
0
   && (src_info & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))
1692
0
     && !(src_info & MAY_BE_GUARD)) {
1693
0
    if (Z_MODE(dst) != IS_REG
1694
0
     && (dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (src_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD))) {
1695
0
      uint8_t type = concrete_type(src_info);
1696
0
      jit_set_Z_TYPE_INFO(jit, dst, type);
1697
0
    }
1698
0
  } else {
1699
0
    ir_ref type = jit_Z_TYPE_INFO(jit, src);
1700
0
    jit_set_Z_TYPE_INFO_ex(jit, dst, type);
1701
0
    if (addref) {
1702
0
      if (src_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
1703
0
        ir_ref if_refcounted = IR_UNUSED;
1704
1705
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1706
0
          if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(0xff00)));
1707
0
          ir_IF_TRUE(if_refcounted);
1708
0
        }
1709
1710
0
        jit_GC_ADDREF(jit, ref);
1711
1712
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1713
0
          ir_MERGE_WITH_EMPTY_FALSE(if_refcounted);
1714
0
        }
1715
0
      }
1716
0
    }
1717
0
  }
1718
0
}
1719
1720
static void jit_ZVAL_COPY_2(zend_jit_ctx *jit, zend_jit_addr dst2, zend_jit_addr dst, uint32_t dst_info, zend_jit_addr src, uint32_t src_info, int addref)
1721
0
{
1722
0
  ir_ref ref = IR_UNUSED;
1723
1724
0
  if (src_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
1725
0
    if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_LONG) {
1726
0
      ref = jit_Z_LVAL(jit, src);
1727
0
      jit_set_Z_LVAL(jit, dst, ref);
1728
0
      jit_set_Z_LVAL(jit, dst2, ref);
1729
0
    } else if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_DOUBLE) {
1730
0
      ref = jit_Z_DVAL(jit, src);
1731
0
      jit_set_Z_DVAL(jit, dst, ref);
1732
0
      jit_set_Z_DVAL(jit, dst2, ref);
1733
0
    } else {
1734
#if SIZEOF_ZEND_LONG == 4
1735
      if (src_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) {
1736
        ref = jit_Z_W2(jit, src);
1737
        jit_set_Z_W2(jit, dst, ref);
1738
        jit_set_Z_W2(jit, dst2, ref);
1739
      }
1740
#endif
1741
0
      ref = jit_Z_PTR(jit, src);
1742
0
      jit_set_Z_PTR(jit, dst, ref);
1743
0
      jit_set_Z_PTR(jit, dst2, ref);
1744
0
    }
1745
0
  }
1746
0
  if (has_concrete_type(src_info & MAY_BE_ANY)
1747
0
   && (src_info & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))
1748
0
     && !(src_info & MAY_BE_GUARD)) {
1749
0
    uint8_t type = concrete_type(src_info);
1750
0
    ir_ref type_ref = ir_CONST_U32(type);
1751
1752
0
    if (Z_MODE(dst) != IS_REG
1753
0
     && (dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (src_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD))) {
1754
0
      jit_set_Z_TYPE_INFO_ex(jit, dst, type_ref);
1755
0
    }
1756
0
    if (Z_MODE(dst2) != IS_REG) {
1757
0
      jit_set_Z_TYPE_INFO_ex(jit, dst2, type_ref);
1758
0
    }
1759
0
  } else {
1760
0
    ir_ref type = jit_Z_TYPE_INFO(jit, src);
1761
0
    jit_set_Z_TYPE_INFO_ex(jit, dst, type);
1762
0
    jit_set_Z_TYPE_INFO_ex(jit, dst2, type);
1763
0
    if (addref) {
1764
0
      if (src_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
1765
0
        ir_ref if_refcounted = IR_UNUSED;
1766
1767
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1768
0
          if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(0xff00)));
1769
0
          ir_IF_TRUE(if_refcounted);
1770
0
        }
1771
1772
0
        if (addref == 2) {
1773
0
          jit_GC_ADDREF2(jit, ref);
1774
0
        } else {
1775
0
          jit_GC_ADDREF(jit, ref);
1776
0
        }
1777
1778
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1779
0
          ir_MERGE_WITH_EMPTY_FALSE(if_refcounted);
1780
0
        }
1781
0
      }
1782
0
    }
1783
0
  }
1784
0
}
1785
1786
static void jit_ZVAL_DTOR(zend_jit_ctx *jit, ir_ref ref, uint32_t op_info, const zend_op *opline)
1787
0
{
1788
0
  if (!((op_info) & MAY_BE_GUARD)
1789
0
   && has_concrete_type((op_info) & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1790
0
    uint8_t type = concrete_type((op_info) & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
1791
0
    if (type == IS_STRING && !ZEND_DEBUG) {
1792
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(_efree), ref);
1793
0
        return;
1794
0
    } else if (type == IS_ARRAY) {
1795
0
      if ((op_info) & (MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_REF)) {
1796
0
        if (opline && ((op_info) & (MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_REF))) {
1797
0
          jit_SET_EX_OPLINE(jit, opline);
1798
0
        }
1799
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_array_destroy), ref);
1800
0
      } else {
1801
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_array_free), ref);
1802
0
      }
1803
0
      return;
1804
0
    } else if (type == IS_OBJECT) {
1805
0
      if (opline) {
1806
0
        jit_SET_EX_OPLINE(jit, opline);
1807
0
      }
1808
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_objects_store_del), ref);
1809
0
      return;
1810
0
    }
1811
0
  }
1812
0
  if (opline) {
1813
0
    jit_SET_EX_OPLINE(jit, opline);
1814
0
  }
1815
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(rc_dtor_func), ref);
1816
0
}
1817
1818
static void jit_ZVAL_PTR_DTOR(zend_jit_ctx  *jit,
1819
                              zend_jit_addr  addr,
1820
                              uint32_t       op_info,
1821
                              bool           gc,
1822
                              const zend_op *opline)
1823
0
{
1824
0
    ir_ref ref, ref2;
1825
0
  ir_ref if_refcounted = IR_UNUSED;
1826
0
  ir_ref if_not_zero = IR_UNUSED;
1827
0
  ir_ref end_inputs = IR_UNUSED;
1828
1829
0
  if (op_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF|MAY_BE_GUARD)) {
1830
0
    if ((op_info) & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_INDIRECT|MAY_BE_GUARD)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1831
0
      if_refcounted = jit_if_REFCOUNTED(jit, addr);
1832
0
      ir_IF_FALSE(if_refcounted);
1833
0
      ir_END_list(end_inputs);
1834
0
      ir_IF_TRUE(if_refcounted);
1835
0
    }
1836
0
    ref = jit_Z_PTR(jit, addr);
1837
0
    ref2 = jit_GC_DELREF(jit, ref);
1838
1839
0
    if (((op_info) & MAY_BE_GUARD) || RC_MAY_BE_1(op_info)) {
1840
0
      if (((op_info) & MAY_BE_GUARD) || RC_MAY_BE_N(op_info)) {
1841
0
        if_not_zero = ir_IF(ref2);
1842
0
        ir_IF_FALSE(if_not_zero);
1843
0
      }
1844
      // zval_dtor_func(r);
1845
0
      jit_ZVAL_DTOR(jit, ref, op_info, opline);
1846
0
      if (if_not_zero) {
1847
0
        ir_END_list(end_inputs);
1848
0
        ir_IF_TRUE(if_not_zero);
1849
0
      }
1850
0
    }
1851
0
    if (gc && (((op_info) & MAY_BE_GUARD) || (RC_MAY_BE_N(op_info) && ((op_info) & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT)) != 0))) {
1852
0
      ir_ref if_may_not_leak;
1853
1854
0
      if ((op_info) & (MAY_BE_REF|MAY_BE_GUARD)) {
1855
0
        ir_ref if_ref, if_collectable;
1856
1857
0
        if_ref = jit_if_Z_TYPE(jit, addr, IS_REFERENCE);
1858
0
        ir_IF_TRUE(if_ref);
1859
1860
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
1861
1862
0
        if_collectable = jit_if_COLLECTABLE_ref(jit, ref2);
1863
0
        ir_IF_FALSE(if_collectable);
1864
0
        ir_END_list(end_inputs);
1865
0
        ir_IF_TRUE(if_collectable);
1866
1867
0
        ref2 = jit_Z_PTR_ref(jit, ref2);
1868
1869
0
        ir_MERGE_WITH_EMPTY_FALSE(if_ref);
1870
0
        ref = ir_PHI_2(IR_ADDR, ref2, ref);
1871
0
      }
1872
1873
0
      if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
1874
0
      ir_IF_TRUE(if_may_not_leak);
1875
0
      ir_END_list(end_inputs);
1876
0
      ir_IF_FALSE(if_may_not_leak);
1877
1878
0
      if (opline) {
1879
0
        jit_SET_EX_OPLINE(jit, opline);
1880
0
      }
1881
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
1882
0
    }
1883
1884
0
    if (end_inputs) {
1885
0
      ir_END_list(end_inputs);
1886
0
      ir_MERGE_list(end_inputs);
1887
0
    }
1888
0
  }
1889
0
}
1890
1891
static void jit_FREE_OP(zend_jit_ctx  *jit,
1892
                        uint8_t        op_type,
1893
                        znode_op       op,
1894
                        uint32_t       op_info,
1895
                        const zend_op *opline)
1896
0
{
1897
0
  if (op_type & (IS_VAR|IS_TMP_VAR)) {
1898
0
    jit_ZVAL_PTR_DTOR(jit,
1899
0
      ZEND_ADDR_MEM_ZVAL(ZREG_FP, op.var),
1900
0
      op_info, false, opline);
1901
0
  }
1902
0
}
1903
1904
static void jit_OBJ_RELEASE(zend_jit_ctx  *jit, ir_ref ref)
1905
0
{
1906
0
  ir_ref end_inputs = IR_UNUSED;
1907
0
    ir_ref if_not_zero, if_may_not_leak;
1908
1909
  // JIT: if (GC_DELREF(obj) == 0) {
1910
0
  if_not_zero = ir_IF(jit_GC_DELREF(jit, ref));
1911
0
  ir_IF_FALSE(if_not_zero);
1912
1913
  // JIT: zend_objects_store_del(obj)
1914
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_objects_store_del), ref);
1915
0
  ir_END_list(end_inputs);
1916
1917
0
  ir_IF_TRUE(if_not_zero);
1918
0
  if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
1919
1920
0
  ir_IF_TRUE(if_may_not_leak);
1921
0
  ir_END_list(end_inputs);
1922
1923
0
  ir_IF_FALSE(if_may_not_leak);
1924
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
1925
0
  ir_END_list(end_inputs);
1926
1927
0
  ir_MERGE_list(end_inputs);
1928
0
}
1929
1930
static void zend_jit_check_timeout(zend_jit_ctx *jit, const zend_op *opline, const void *exit_addr)
1931
0
{
1932
0
  ir_ref ref = ir_LOAD_U8(jit_EG(vm_interrupt));
1933
1934
0
  if (exit_addr) {
1935
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
1936
0
  } else if (!opline || jit->last_valid_opline == opline) {
1937
0
    ir_GUARD_NOT(ref, jit_STUB_ADDR(jit, jit_stub_interrupt_handler));
1938
0
  } else {
1939
0
    ir_ref if_timeout = ir_IF(ref);
1940
1941
0
    ir_IF_TRUE_cold(if_timeout);
1942
0
    jit_LOAD_IP_ADDR(jit, opline);
1943
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_interrupt_handler));
1944
0
    ir_IF_FALSE(if_timeout);
1945
0
  }
1946
0
}
1947
1948
static void zend_jit_vm_enter(zend_jit_ctx *jit, ir_ref to_opline)
1949
0
{
1950
  // ZEND_VM_ENTER()
1951
0
  ir_RETURN(ir_OR_A(to_opline, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)));
1952
0
}
1953
1954
static void zend_jit_vm_leave(zend_jit_ctx *jit, ir_ref to_opline)
1955
0
{
1956
0
  // ZEND_VM_LEAVE()
1957
0
  ir_RETURN(ir_OR_A(to_opline, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)));
1958
0
}
1959
1960
static void zend_jit_tailcall_handler(zend_jit_ctx *jit, ir_ref handler)
1961
0
{
1962
#if defined(IR_TARGET_X86)
1963
  if (!IR_IS_CONST_REF(handler)) {
1964
    handler = ir_CAST_OPCODE_HANDLER_FUNC(handler);
1965
  }
1966
#endif
1967
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
1968
0
    ir_TAILCALL(IR_OPCODE_HANDLER_RET, handler);
1969
0
  } else {
1970
0
    ir_TAILCALL_2(IR_ADDR, handler, jit_FP(jit), jit_IP(jit));
1971
0
  }
1972
0
}
1973
1974
/* stubs */
1975
1976
static int zend_jit_exception_handler_stub(zend_jit_ctx *jit)
1977
0
{
1978
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
1979
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(EG(exception_op));
1980
1981
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
1982
0
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
1983
0
  } else {
1984
0
    zend_vm_opcode_handler_t handler = EG(exception_op)->handler;
1985
1986
0
    if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
1987
0
      zend_jit_tailcall_handler(jit, ir_CONST_OPCODE_HANDLER_FUNC(handler));
1988
0
    } else {
1989
0
      ir_ref ref = ir_CALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
1990
0
      zend_jit_vm_enter(jit, ref);
1991
0
    }
1992
0
  }
1993
0
  return 1;
1994
0
}
1995
1996
static int zend_jit_exception_handler_undef_stub(zend_jit_ctx *jit)
1997
0
{
1998
0
  ir_ref ref, result_type, if_result_used;
1999
2000
0
  ref = jit_EG(opline_before_exception);
2001
0
  result_type = ir_LOAD_U8(ir_ADD_OFFSET(ir_LOAD_A(ref), offsetof(zend_op, result_type)));
2002
2003
0
  if_result_used = ir_IF(ir_AND_U8(result_type, ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2004
0
  ir_IF_TRUE(if_result_used);
2005
2006
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(ref), offsetof(zend_op, result.var)));
2007
0
  if (sizeof(void*) == 8) {
2008
0
    ref = ir_ZEXT_A(ref);
2009
0
  }
2010
0
  ir_STORE(ir_ADD_OFFSET(ir_ADD_A(jit_FP(jit), ref), offsetof(zval, u1.type_info)), ir_CONST_U32(IS_UNDEF));
2011
0
  ir_MERGE_WITH_EMPTY_FALSE(if_result_used);
2012
2013
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2014
2015
0
  return 1;
2016
0
}
2017
2018
static int zend_jit_exception_handler_free_op1_op2_stub(zend_jit_ctx *jit)
2019
0
{
2020
0
  ir_ref ref, if_dtor;
2021
0
  zend_jit_addr var_addr;
2022
2023
0
  ref = ir_LOAD_A(jit_EG(opline_before_exception));
2024
0
  if_dtor = ir_IF(ir_AND_U8(ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zend_op, op1_type))),
2025
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2026
0
  ir_IF_TRUE(if_dtor);
2027
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op1.var)));
2028
0
  if (sizeof(void*) == 8) {
2029
0
    ref = ir_ZEXT_A(ref);
2030
0
  }
2031
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2032
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
2033
0
  jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2034
0
  ir_MERGE_WITH_EMPTY_FALSE(if_dtor);
2035
2036
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
2037
2038
0
  return 1;
2039
0
}
2040
2041
static int zend_jit_exception_handler_free_op2_stub(zend_jit_ctx *jit)
2042
0
{
2043
0
  ir_ref ref, if_dtor;
2044
0
  zend_jit_addr var_addr;
2045
2046
0
  ref = ir_LOAD_A(jit_EG(opline_before_exception));
2047
0
  if_dtor = ir_IF(ir_AND_U8(ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zend_op, op2_type))),
2048
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2049
0
  ir_IF_TRUE(if_dtor);
2050
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.var)));
2051
0
  if (sizeof(void*) == 8) {
2052
0
    ref = ir_ZEXT_A(ref);
2053
0
  }
2054
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2055
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
2056
0
  jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2057
0
  ir_MERGE_WITH_EMPTY_FALSE(if_dtor);
2058
2059
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
2060
2061
0
  return 1;
2062
0
}
2063
2064
static int zend_jit_interrupt_handler_stub(zend_jit_ctx *jit)
2065
0
{
2066
0
  ir_ref if_timeout, if_exception;
2067
2068
  // EX(opline) = opline
2069
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2070
2071
0
  ir_STORE(jit_EG(vm_interrupt), ir_CONST_U8(0));
2072
0
  if_timeout = ir_IF(ir_EQ(ir_LOAD_U8(jit_EG(timed_out)), ir_CONST_U8(0)));
2073
0
  ir_IF_FALSE(if_timeout);
2074
0
  ir_CALL(IR_VOID, ir_CONST_FUNC(zend_timeout));
2075
0
  ir_MERGE_WITH_EMPTY_TRUE(if_timeout);
2076
2077
0
  if (zend_interrupt_function) {
2078
0
    ir_CALL_1(IR_VOID, ir_CONST_FUNC(zend_interrupt_function), jit_FP(jit));
2079
0
    if_exception = ir_IF(ir_LOAD_A(jit_EG(exception)));
2080
0
    ir_IF_TRUE(if_exception);
2081
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(zend_jit_exception_in_interrupt_handler_helper));
2082
0
    ir_MERGE_WITH_EMPTY_FALSE(if_exception);
2083
2084
0
    jit_STORE_FP(jit, ir_LOAD_A(jit_EG(current_execute_data)));
2085
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2086
0
  }
2087
2088
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2089
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2090
0
  } else {
2091
0
    zend_jit_vm_enter(jit, jit_IP(jit));
2092
0
  }
2093
0
  return 1;
2094
0
}
2095
2096
static int zend_jit_leave_function_handler_stub(zend_jit_ctx *jit)
2097
0
{
2098
0
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
2099
0
  ir_TAILCALL(IR_OPCODE_HANDLER_RET, ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_leave_func_helper_tailcall));
2100
0
  return 1;
2101
#else
2102
  ir_ref call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
2103
  ir_ref if_top = ir_IF(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_TOP)));
2104
2105
  ir_IF_FALSE(if_top);
2106
2107
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2108
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), call_info);
2109
    jit_STORE_IP(jit,
2110
      ir_LOAD_A(jit_EX(opline)));
2111
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2112
  } else if (GCC_GLOBAL_REGS) {
2113
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), call_info);
2114
  } else {
2115
    ir_TAILCALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), jit_FP(jit), jit_IP(jit), call_info);
2116
  }
2117
2118
  ir_IF_TRUE(if_top);
2119
2120
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2121
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), call_info);
2122
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2123
  } else if (GCC_GLOBAL_REGS) {
2124
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), call_info);
2125
  } else {
2126
    ir_TAILCALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), jit_FP(jit), jit_IP(jit), call_info);
2127
  }
2128
2129
  return 1;
2130
#endif /* ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL */
2131
0
}
2132
2133
static int zend_jit_negative_shift_stub(zend_jit_ctx *jit)
2134
0
{
2135
0
  ir_CALL_2(IR_VOID,
2136
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2137
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2138
0
    ir_CONST_ADDR(zend_ce_arithmetic_error),
2139
0
    ir_CONST_ADDR("Bit shift by negative number"));
2140
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op1_op2));
2141
0
  return 1;
2142
0
}
2143
2144
static int zend_jit_mod_by_zero_stub(zend_jit_ctx *jit)
2145
0
{
2146
0
  ir_CALL_2(IR_VOID,
2147
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2148
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2149
0
    ir_CONST_ADDR(zend_ce_division_by_zero_error),
2150
0
    ir_CONST_ADDR("Modulo by zero"));
2151
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op1_op2));
2152
0
  return 1;
2153
0
}
2154
2155
static int zend_jit_invalid_this_stub(zend_jit_ctx *jit)
2156
0
{
2157
0
  ir_CALL_2(IR_VOID,
2158
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2159
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2160
0
    IR_NULL,
2161
0
    ir_CONST_ADDR("Using $this when not in object context"));
2162
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
2163
0
  return 1;
2164
0
}
2165
2166
static int zend_jit_undefined_function_stub(zend_jit_ctx *jit)
2167
0
{
2168
  // JIT: load EX(opline)
2169
0
  ir_ref ref = ir_LOAD_A(jit_FP(jit));
2170
0
  ir_ref arg3 = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.constant)));
2171
2172
0
  if (sizeof(void*) == 8) {
2173
0
    arg3 = ir_LOAD_A(ir_ADD_A(ref, ir_SEXT_A(arg3)));
2174
0
  } else {
2175
0
    arg3 = ir_LOAD_A(arg3);
2176
0
  }
2177
0
  arg3 = ir_ADD_OFFSET(arg3, offsetof(zend_string, val));
2178
2179
0
  ir_CALL_3(IR_VOID,
2180
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2181
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2182
0
    IR_NULL,
2183
0
    ir_CONST_ADDR("Call to undefined function %s()"),
2184
0
    arg3);
2185
2186
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2187
2188
0
  return 1;
2189
0
}
2190
2191
static int zend_jit_throw_cannot_pass_by_ref_stub(zend_jit_ctx *jit)
2192
0
{
2193
0
  ir_ref opline, ref, rx, if_eq, if_tmp;
2194
2195
  // JIT: opline = EX(opline)
2196
0
  opline = ir_LOAD_A(jit_FP(jit));
2197
2198
  // JIT: ZVAL_UNDEF(ZEND_CALL_VAR(RX, opline->result.var))
2199
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, result.var)));
2200
0
  if (sizeof(void*) == 8) {
2201
0
    ref = ir_ZEXT_A(ref);
2202
0
  }
2203
0
  rx = jit_IP(jit);
2204
0
  jit_set_Z_TYPE_INFO_ref(jit, ir_ADD_A(rx, ref), ir_CONST_U32(IS_UNDEF));
2205
2206
  // last EX(call) frame may be delayed
2207
  // JIT: if (EX(call) == RX)
2208
0
  ref = ir_LOAD_A(jit_EX(call));
2209
0
  if_eq = ir_IF(ir_EQ(rx, ref));
2210
0
  ir_IF_FALSE(if_eq);
2211
2212
  // JIT: RX->prev_execute_data == EX(call)
2213
0
  ir_STORE(jit_CALL(rx, prev_execute_data), ref);
2214
2215
  // JIT: EX(call) = RX
2216
0
  ir_STORE(jit_EX(call), rx);
2217
0
  ir_MERGE_WITH_EMPTY_TRUE(if_eq);
2218
2219
  // JIT: IP = opline
2220
0
  jit_STORE_IP(jit, opline);
2221
2222
  // JIT: zend_cannot_pass_by_reference(opline->op2.num)
2223
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_cannot_pass_by_reference),
2224
0
    ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, op2.num))));
2225
2226
  // JIT: if (IP->op1_type == IS_TMP_VAR)
2227
0
  ref = ir_LOAD_U8(ir_ADD_OFFSET(jit_IP(jit), offsetof(zend_op, op1_type)));
2228
0
  if_tmp = ir_IF(ir_EQ(ref, ir_CONST_U8(IS_TMP_VAR)));
2229
0
  ir_IF_TRUE(if_tmp);
2230
2231
  // JIT: zval_ptr_dtor(EX_VAR(IP->op1.var))
2232
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_IP(jit), offsetof(zend_op, op1.var)));
2233
0
  if (sizeof(void*) == 8) {
2234
0
    ref = ir_ZEXT_A(ref);
2235
0
  }
2236
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2237
0
  jit_ZVAL_PTR_DTOR(jit,
2238
0
    ZEND_ADDR_REF_ZVAL(ref),
2239
0
    MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2240
0
  ir_MERGE_WITH_EMPTY_FALSE(if_tmp);
2241
2242
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2243
2244
0
  return 1;
2245
0
}
2246
2247
static int zend_jit_icall_throw_stub(zend_jit_ctx *jit)
2248
0
{
2249
0
  ir_ref ip, if_set;
2250
2251
  // JIT: zend_rethrow_exception(zend_execute_data *execute_data)
2252
  // JIT: if (EX(opline)->opcode != ZEND_HANDLE_EXCEPTION) {
2253
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2254
0
  ip = jit_IP(jit);
2255
0
  if_set = ir_IF(ir_EQ(ir_LOAD_U8(ir_ADD_OFFSET(ip, offsetof(zend_op, opcode))),
2256
0
    ir_CONST_U8(ZEND_HANDLE_EXCEPTION)));
2257
0
  ir_IF_FALSE(if_set);
2258
2259
  // JIT: EG(opline_before_exception) = opline;
2260
0
  ir_STORE(jit_EG(opline_before_exception), ip);
2261
0
  ir_MERGE_WITH_EMPTY_TRUE(if_set);
2262
2263
  // JIT: opline = EG(exception_op);
2264
0
  jit_STORE_IP(jit, jit_EG(exception_op));
2265
2266
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2267
2268
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2269
2270
0
  return 1;
2271
0
}
2272
2273
static int zend_jit_leave_throw_stub(zend_jit_ctx *jit)
2274
0
{
2275
0
  ir_ref ip, if_set;
2276
2277
  // JIT: if (opline->opcode != ZEND_HANDLE_EXCEPTION) {
2278
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2279
0
  ip = jit_IP(jit);
2280
0
  if_set = ir_IF(ir_EQ(ir_LOAD_U8(ir_ADD_OFFSET(ip, offsetof(zend_op, opcode))),
2281
0
    ir_CONST_U8(ZEND_HANDLE_EXCEPTION)));
2282
0
  ir_IF_FALSE(if_set);
2283
2284
  // JIT: EG(opline_before_exception) = opline;
2285
0
  ir_STORE(jit_EG(opline_before_exception), ip);
2286
0
  ir_MERGE_WITH_EMPTY_TRUE(if_set);
2287
2288
  // JIT: opline = EG(exception_op);
2289
0
  jit_STORE_IP(jit, jit_EG(exception_op));
2290
2291
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2292
0
    ir_STORE(jit_EX(opline), jit_IP(jit));
2293
2294
    // JIT: HANDLE_EXCEPTION()
2295
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2296
0
  } else {
2297
0
    zend_jit_vm_leave(jit, jit_IP(jit));
2298
0
  }
2299
2300
0
  return 1;
2301
0
}
2302
2303
static int zend_jit_hybrid_runtime_jit_stub(zend_jit_ctx *jit)
2304
0
{
2305
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) {
2306
0
    return 0;
2307
0
  }
2308
2309
0
  ir_CALL(IR_VOID, ir_CONST_OPCODE_HANDLER_FUNC(zend_runtime_jit));
2310
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2311
0
  return 1;
2312
0
}
2313
2314
static int zend_jit_hybrid_profile_jit_stub(zend_jit_ctx *jit)
2315
0
{
2316
0
  ir_ref addr, func, run_time_cache, jit_extension;
2317
2318
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) {
2319
0
    return 0;
2320
0
  }
2321
2322
0
  addr = ir_CONST_ADDR(&zend_jit_profile_counter),
2323
0
  ir_STORE(addr, ir_ADD_L(ir_LOAD_L(addr), ir_CONST_LONG(1)));
2324
2325
0
  func = ir_LOAD_A(jit_EX(func));
2326
0
  run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
2327
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2328
2329
0
  if (zend_jit_profile_counter_rid) {
2330
0
    addr = ir_ADD_OFFSET(run_time_cache, zend_jit_profile_counter_rid * sizeof(void*));
2331
0
  } else {
2332
0
    addr = run_time_cache;
2333
0
  }
2334
0
  ir_STORE(addr, ir_ADD_L(ir_LOAD_L(addr), ir_CONST_LONG(1)));
2335
2336
0
  addr = ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_extension, orig_handler));
2337
0
  ir_IJMP(ir_LOAD_A(addr));
2338
2339
0
  return 1;
2340
0
}
2341
2342
static int _zend_jit_hybrid_hot_counter_stub(zend_jit_ctx *jit, uint32_t cost)
2343
0
{
2344
0
  ir_ref func, jit_extension, addr, ref, if_overflow;
2345
0
2346
0
  func = ir_LOAD_A(jit_EX(func));
2347
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2348
0
  addr = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_hot_extension, counter)));
2349
0
  ref = ir_SUB_I16(ir_LOAD_I16(addr), ir_CONST_I16(cost));
2350
0
  ir_STORE(addr, ref);
2351
0
  if_overflow = ir_IF(ir_LE(ref, ir_CONST_I16(0)));
2352
0
2353
0
  ir_IF_TRUE_cold(if_overflow);
2354
0
  ir_STORE(addr, ir_CONST_I16(ZEND_JIT_COUNTER_INIT));
2355
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_hot_func),
2356
0
    jit_FP(jit),
2357
0
    jit_IP(jit));
2358
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2359
0
2360
0
  ir_IF_FALSE(if_overflow);
2361
0
  ref = ir_SUB_A(jit_IP(jit),
2362
0
    ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, opcodes))));
2363
0
  ref = ir_DIV_A(ref, ir_CONST_ADDR(sizeof(zend_op) / sizeof(void*)));
2364
0
2365
0
  addr = ir_ADD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_hot_extension, orig_handlers)),
2366
0
    ref);
2367
0
  ir_IJMP(ir_LOAD_A(addr));
2368
0
2369
0
  return 1;
2370
0
}
2371
2372
static int zend_jit_hybrid_func_hot_counter_stub(zend_jit_ctx *jit)
2373
0
{
2374
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_func)) {
2375
0
    return 0;
2376
0
  }
2377
2378
0
  return _zend_jit_hybrid_hot_counter_stub(jit,
2379
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
2380
0
}
2381
2382
static int zend_jit_hybrid_loop_hot_counter_stub(zend_jit_ctx *jit)
2383
0
{
2384
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_loop)) {
2385
0
    return 0;
2386
0
  }
2387
2388
0
  return _zend_jit_hybrid_hot_counter_stub(jit,
2389
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
2390
0
}
2391
2392
static ir_ref _zend_jit_orig_opline_handler(zend_jit_ctx *jit, ir_ref offset)
2393
0
{
2394
0
  ir_ref addr = ir_ADD_A(offset, jit_IP(jit));
2395
2396
0
  return ir_LOAD_A(addr);
2397
0
}
2398
2399
static ir_ref zend_jit_orig_opline_handler(zend_jit_ctx *jit)
2400
0
{
2401
0
  ir_ref func, jit_extension, offset;
2402
2403
0
  func = ir_LOAD_A(jit_EX(func));
2404
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2405
0
  offset = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_trace_extension, offset)));
2406
0
  return _zend_jit_orig_opline_handler(jit, offset);
2407
0
}
2408
2409
static int _zend_jit_hybrid_trace_counter_stub(zend_jit_ctx *jit, uint32_t cost)
2410
0
{
2411
0
  ir_ref func, jit_extension, offset, addr, ref, if_overflow, ret, if_halt;
2412
0
2413
0
  func = ir_LOAD_A(jit_EX(func));
2414
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2415
0
  offset = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_trace_extension, offset)));
2416
0
  addr = ir_LOAD_A(ir_ADD_OFFSET(ir_ADD_A(offset, jit_IP(jit)), offsetof(zend_op_trace_info, counter)));
2417
0
  ref = ir_SUB_I16(ir_LOAD_I16(addr), ir_CONST_I16(cost));
2418
0
  ir_STORE(addr, ref);
2419
0
  if_overflow = ir_IF(ir_LE(ref, ir_CONST_I16(0)));
2420
0
2421
0
  ir_IF_TRUE_cold(if_overflow);
2422
0
  ir_STORE(addr, ir_CONST_I16(ZEND_JIT_COUNTER_INIT));
2423
0
  ret = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_trace_hot_root),
2424
0
    jit_FP(jit),
2425
0
    jit_IP(jit));
2426
0
  if_halt = ir_IF(ir_LT(ret, ir_CONST_I32(0)));
2427
0
  ir_IF_FALSE(if_halt);
2428
0
2429
0
  ref = jit_EG(current_execute_data);
2430
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2431
0
  ref = ir_LOAD_A(jit_EX(opline));
2432
0
  jit_STORE_IP(jit, ref);
2433
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2434
0
2435
0
  ir_IF_FALSE(if_overflow);
2436
0
  ir_IJMP(_zend_jit_orig_opline_handler(jit, offset));
2437
0
2438
0
  ir_IF_TRUE(if_halt);
2439
0
  ir_IJMP(ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_halt_op->handler));
2440
0
2441
0
  return 1;
2442
0
}
2443
2444
static int zend_jit_hybrid_func_trace_counter_stub(zend_jit_ctx *jit)
2445
0
{
2446
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_func)) {
2447
0
    return 0;
2448
0
  }
2449
2450
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2451
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
2452
0
}
2453
2454
static int zend_jit_hybrid_ret_trace_counter_stub(zend_jit_ctx *jit)
2455
0
{
2456
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_return)) {
2457
0
    return 0;
2458
0
  }
2459
2460
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2461
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_return) - 1) / JIT_G(hot_return)));
2462
0
}
2463
2464
static int zend_jit_hybrid_loop_trace_counter_stub(zend_jit_ctx *jit)
2465
0
{
2466
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_loop)) {
2467
0
    return 0;
2468
0
  }
2469
2470
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2471
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
2472
0
}
2473
2474
static int zend_jit_trace_halt_stub(zend_jit_ctx *jit)
2475
0
{
2476
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2477
0
    ir_TAILCALL(IR_VOID, ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_halt_op->handler));
2478
0
  } else if (GCC_GLOBAL_REGS) {
2479
0
    jit_STORE_IP(jit, IR_NULL);
2480
0
    ir_RETURN(IR_VOID);
2481
0
  } else {
2482
0
    ir_RETURN(ir_CONST_ADDR(ZEND_VM_ENTER_BIT)); // ZEND_VM_RETURN
2483
0
  }
2484
0
  return 1;
2485
0
}
2486
2487
static int zend_jit_trace_escape_stub(zend_jit_ctx *jit)
2488
0
{
2489
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2490
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2491
0
  } else {
2492
0
    zend_jit_vm_enter(jit, jit_IP(jit));
2493
0
  }
2494
2495
0
  return 1;
2496
0
}
2497
2498
static int zend_jit_trace_exit_stub(zend_jit_ctx *jit)
2499
0
{
2500
0
  ir_ref ref, ret, if_zero, addr;
2501
2502
  // EX(opline) = opline
2503
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2504
2505
0
  ret = ir_EXITCALL(ir_CONST_FC_FUNC(zend_jit_trace_exit));
2506
2507
0
  if_zero = ir_IF(ir_EQ(ret, ir_CONST_I32(0)));
2508
2509
0
  ir_IF_TRUE(if_zero);
2510
2511
0
  ref = jit_EG(current_execute_data);
2512
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2513
0
  ref = ir_LOAD_A(jit_EX(opline));
2514
0
  jit_STORE_IP(jit, ref);
2515
2516
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2517
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2518
0
  } else {
2519
0
    zend_jit_vm_enter(jit, ref);
2520
0
  }
2521
2522
0
  ir_IF_FALSE(if_zero);
2523
2524
0
  ir_GUARD(ir_GE(ret, ir_CONST_I32(0)), jit_STUB_ADDR(jit, jit_stub_trace_halt));
2525
2526
0
  ref = jit_EG(current_execute_data);
2527
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2528
2529
0
  ref = ir_LOAD_A(jit_EX(opline));
2530
0
  jit_STORE_IP(jit, ref);
2531
2532
  // check for interrupt (try to avoid this ???)
2533
0
  zend_jit_check_timeout(jit, NULL, NULL);
2534
2535
0
  addr = zend_jit_orig_opline_handler(jit);
2536
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2537
0
    zend_jit_tailcall_handler(jit, addr);
2538
0
  } else {
2539
#if defined(IR_TARGET_X86)
2540
    addr = ir_CAST_OPCODE_HANDLER_FUNC(addr);
2541
#endif
2542
0
    ref = ir_CALL_2(IR_ADDR, addr, jit_FP(jit), jit_IP(jit));
2543
0
    zend_jit_vm_enter(jit, ref);
2544
0
  }
2545
2546
0
  return 1;
2547
0
}
2548
2549
static int zend_jit_undefined_offset_stub(zend_jit_ctx *jit)
2550
0
{
2551
0
  if (GCC_GLOBAL_REGS) {
2552
0
    ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key));
2553
0
  } else {
2554
0
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key), jit_FP(jit));
2555
0
  }
2556
2557
0
  return 1;
2558
0
}
2559
2560
static int zend_jit_undefined_key_stub(zend_jit_ctx *jit)
2561
0
{
2562
0
  if (GCC_GLOBAL_REGS) {
2563
0
    ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_string_key));
2564
0
  } else {
2565
0
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_string_key), jit_FP(jit));
2566
0
  }
2567
2568
0
  return 1;
2569
0
}
2570
2571
static int zend_jit_cannot_add_element_stub(zend_jit_ctx *jit)
2572
0
{
2573
0
  ir_ref opline = ir_LOAD_A(jit_EX(opline));
2574
0
  ir_ref ref, if_result_used;
2575
2576
0
  if_result_used = ir_IF(ir_AND_U8(
2577
0
    ir_LOAD_U8(ir_ADD_OFFSET(opline, offsetof(zend_op, result_type))),
2578
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2579
0
  ir_IF_TRUE(if_result_used);
2580
2581
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, result.var)));
2582
0
  if (sizeof(void*) == 8) {
2583
0
    ref = ir_ZEXT_A(ref);
2584
0
  }
2585
0
  jit_set_Z_TYPE_INFO_ref(jit, ir_ADD_A(jit_FP(jit), ref), ir_CONST_U32(IS_UNDEF));
2586
0
  ir_MERGE_WITH_EMPTY_FALSE(if_result_used);
2587
2588
0
  ir_CALL_2(IR_VOID,
2589
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2590
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2591
0
    IR_NULL,
2592
0
    ir_CONST_ADDR("Cannot add element to the array as the next element is already occupied"));
2593
0
  ir_RETURN(IR_VOID);
2594
2595
0
  return 1;
2596
0
}
2597
2598
static int zend_jit_assign_const_stub(zend_jit_ctx *jit)
2599
0
{
2600
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2601
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2602
2603
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2604
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2605
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN;
2606
2607
0
  if (!zend_jit_assign_to_variable(
2608
0
      jit, NULL,
2609
0
      var_addr, var_addr, -1, -1,
2610
0
      IS_CONST, val_addr, val_info,
2611
0
      0, 0, false)) {
2612
0
    return 0;
2613
0
  }
2614
0
  ir_RETURN(IR_VOID);
2615
0
  return 1;
2616
0
}
2617
2618
static int zend_jit_assign_tmp_stub(zend_jit_ctx *jit)
2619
0
{
2620
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2621
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2622
2623
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2624
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2625
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN;
2626
2627
0
  if (!zend_jit_assign_to_variable(
2628
0
      jit, NULL,
2629
0
      var_addr, var_addr, -1, -1,
2630
0
      IS_TMP_VAR, val_addr, val_info,
2631
0
      0, 0, false)) {
2632
0
    return 0;
2633
0
  }
2634
0
  ir_RETURN(IR_VOID);
2635
0
  return 1;
2636
0
}
2637
2638
static int zend_jit_assign_var_stub(zend_jit_ctx *jit)
2639
0
{
2640
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2641
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2642
2643
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2644
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2645
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF;
2646
2647
0
  if (!zend_jit_assign_to_variable(
2648
0
      jit, NULL,
2649
0
      var_addr, var_addr, -1, -1,
2650
0
      IS_VAR, val_addr, val_info,
2651
0
      0, 0, false)) {
2652
0
    return 0;
2653
0
  }
2654
0
  ir_RETURN(IR_VOID);
2655
0
  return 1;
2656
0
}
2657
2658
static int zend_jit_assign_cv_noref_stub(zend_jit_ctx *jit)
2659
0
{
2660
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2661
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2662
2663
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2664
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2665
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN/*|MAY_BE_UNDEF*/;
2666
2667
0
  if (!zend_jit_assign_to_variable(
2668
0
      jit, NULL,
2669
0
      var_addr, var_addr, -1, -1,
2670
0
      IS_CV, val_addr, val_info,
2671
0
      0, 0, false)) {
2672
0
    return 0;
2673
0
  }
2674
0
  ir_RETURN(IR_VOID);
2675
0
  return 1;
2676
0
}
2677
2678
static int zend_jit_new_array_stub(zend_jit_ctx *jit)
2679
0
{
2680
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2681
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2682
0
  ir_ref ref = ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(_zend_new_array_0));
2683
2684
0
  jit_set_Z_PTR(jit, var_addr, ref);
2685
0
  jit_set_Z_TYPE_INFO(jit, var_addr, IS_ARRAY_EX);
2686
0
  ir_RETURN(ref);
2687
0
  return 1;
2688
0
}
2689
2690
static int zend_jit_assign_cv_stub(zend_jit_ctx *jit)
2691
0
{
2692
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2693
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2694
2695
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2696
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2697
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF/*|MAY_BE_UNDEF*/;
2698
2699
0
  if (!zend_jit_assign_to_variable(
2700
0
      jit, NULL,
2701
0
      var_addr, var_addr, -1, -1,
2702
0
      IS_CV, val_addr, val_info,
2703
0
      0, 0, false)) {
2704
0
    return 0;
2705
0
  }
2706
0
  ir_RETURN(IR_VOID);
2707
0
  return 1;
2708
0
}
2709
2710
static void zend_jit_init_ctx(zend_jit_ctx *jit, uint32_t flags)
2711
0
{
2712
#if defined (__CET__) && (__CET__ & 1) != 0
2713
  flags |= IR_GEN_ENDBR;
2714
#endif
2715
0
  flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN;
2716
2717
0
  ir_init(&jit->ctx, flags, 256, 1024);
2718
0
  jit->ctx.ret_type = -1;
2719
2720
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
2721
0
  jit->ctx.mflags |= default_mflags;
2722
0
  if (JIT_G(opt_flags) & allowed_opt_flags & ZEND_JIT_CPU_AVX) {
2723
0
    jit->ctx.mflags |= IR_X86_AVX;
2724
0
  }
2725
#elif defined(IR_TARGET_AARCH64)
2726
  jit->ctx.get_veneer = zend_jit_get_veneer;
2727
  jit->ctx.set_veneer = zend_jit_set_veneer;
2728
#endif
2729
2730
0
  jit->ctx.fixed_regset = (1<<ZREG_FP) | (1<<ZREG_IP);
2731
0
  if (!(flags & IR_FUNCTION)) {
2732
0
    jit->ctx.flags |= IR_NO_STACK_COMBINE;
2733
0
    if (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2734
0
      jit->ctx.flags |= IR_FUNCTION;
2735
      /* Stack must be 16 byte aligned */
2736
      /* TODO: select stack size ??? */
2737
0
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
2738
# if defined(IR_TARGET_AARCH64)
2739
      /* Must save LR */
2740
      jit->ctx.flags |= IR_USE_FRAME_POINTER;
2741
      /* Same as HYBRID VM */
2742
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 4; /* 4 spill slots */
2743
# else
2744
      /* Same as HYBRID VM, plus 1 slot for re-alignment (caller pushes return address, frame is not aligned on entry) */
2745
0
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 5; /* 5 spill slots (8 bytes) or 10 spill slots (4 bytes) */
2746
0
# endif
2747
#elif defined(IR_TARGET_AARCH64)
2748
      jit->ctx.flags |= IR_USE_FRAME_POINTER;
2749
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 16; /* 10 saved registers and 6 spill slots (8 bytes) */
2750
#elif defined(_WIN64)
2751
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 11; /* 8 saved registers and 3 spill slots (8 bytes) */
2752
#elif defined(IR_TARGET_X86_64)
2753
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 9;  /* 6 saved registers and 3 spill slots (8 bytes) */
2754
#else /* IR_TARGET_x86 */
2755
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 11; /* 4 saved registers and 7 spill slots (4 bytes) */
2756
#endif
2757
      /* JIT-ed code is called only from execute_ex, which takes care
2758
       * of saving ZREG_FP, ZREG_IP when GCC_GLOBAL_REGS is 1, so we don't
2759
       * have to save them.
2760
       */
2761
0
      if (GCC_GLOBAL_REGS) {
2762
0
        jit->ctx.fixed_save_regset = IR_REGSET_PRESERVED & ~((1<<ZREG_FP) | (1<<ZREG_IP));
2763
0
      } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2764
        /* The only preserved register on x86 is RBP:
2765
         * https://github.com/llvm/llvm-project/blob/a414877a7a5f000d01370acb1162eb1dea87f48c/llvm/lib/Target/X86/X86RegisterInfo.cpp#L319
2766
         * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/X86/X86CallingConv.td#L1183
2767
         * On AArch64 it's LR, FP:
2768
         * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/AArch64/AArch64CallingConvention.td#L681
2769
         *
2770
         * Add them to the fixed_regset to prevent usage or these regs.
2771
         * It's cheaper to not use them than to save them.
2772
         */
2773
0
#if defined(IR_TARGET_X64)
2774
0
        jit->ctx.fixed_regset |= (1<<IR_REG_FP);
2775
#elif defined(IR_TARGET_AARCH64)
2776
        jit->ctx.fixed_regset |= (1<<IR_REG_FP) | (1<<IR_REG_LR);
2777
#else
2778
        ZEND_UNREACHABLE();
2779
#endif
2780
0
      } else {
2781
0
        jit->ctx.fixed_save_regset = IR_REGSET_PRESERVED;
2782
//#ifdef _WIN64
2783
//        jit->ctx.fixed_save_regset &= 0xffff; // TODO: don't save FP registers ???
2784
//#endif
2785
0
      }
2786
#ifdef _WIN64
2787
      jit->ctx.fixed_call_stack_size = 16 + IR_SHADOW_ARGS;
2788
#else
2789
0
      jit->ctx.fixed_call_stack_size = 16;
2790
0
#endif
2791
0
    } else {
2792
#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE
2793
      jit->ctx.fixed_stack_red_zone = ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE;
2794
      if (jit->ctx.fixed_stack_red_zone > 16) {
2795
        jit->ctx.fixed_stack_frame_size = jit->ctx.fixed_stack_red_zone - 16;
2796
        jit->ctx.fixed_call_stack_size = 16;
2797
      }
2798
      jit->ctx.flags |= IR_MERGE_EMPTY_ENTRIES;
2799
#else
2800
0
      jit->ctx.fixed_stack_red_zone = 0;
2801
0
      jit->ctx.fixed_stack_frame_size = 32; /* 4 spill slots (8 bytes) or 8 spill slots (4 bytes) */
2802
0
      jit->ctx.fixed_call_stack_size = 16;
2803
0
#endif
2804
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
2805
0
      jit->ctx.fixed_regset |= (1<<IR_REG_FP); /* prevent %rbp (%r5) usage */
2806
0
#endif
2807
0
    }
2808
0
  }
2809
2810
0
  jit->ctx.snapshot_create = (ir_snapshot_create_t)jit_SNAPSHOT;
2811
2812
0
  jit->op_array = NULL;
2813
0
  jit->current_op_array = NULL;
2814
0
  jit->ssa = NULL;
2815
0
  jit->name = NULL;
2816
0
  jit->last_valid_opline = NULL;
2817
0
  jit->use_last_valid_opline = false;
2818
0
  jit->track_last_valid_opline = false;
2819
0
  jit->reuse_ip = false;
2820
0
  jit->delayed_call_level = 0;
2821
0
  delayed_call_chain = false;
2822
0
  jit->b = -1;
2823
#ifdef ZTS
2824
  jit->tls = IR_UNUSED;
2825
#endif
2826
0
  jit->fp = IR_UNUSED;
2827
0
  jit->poly_func_ref = IR_UNUSED;
2828
0
  jit->poly_this_ref = IR_UNUSED;
2829
0
  jit->trace_loop_ref = IR_UNUSED;
2830
0
  jit->return_inputs = IR_UNUSED;
2831
0
  jit->bb_start_ref = NULL;
2832
0
  jit->bb_predecessors = NULL;
2833
0
  jit->bb_edges = NULL;
2834
0
  jit->trace = NULL;
2835
0
  jit->ra = NULL;
2836
0
  jit->delay_var = -1;
2837
0
  jit->delay_refs = NULL;
2838
0
  jit->eg_exception_addr = 0;
2839
0
  zend_hash_init(&jit->addr_hash, 64, NULL, NULL, 0);
2840
0
  memset(jit->stub_addr, 0, sizeof(jit->stub_addr));
2841
2842
0
  ir_START();
2843
0
}
2844
2845
static int zend_jit_free_ctx(zend_jit_ctx *jit)
2846
0
{
2847
0
  if (jit->name) {
2848
0
    zend_string_release(jit->name);
2849
0
  }
2850
0
  zend_hash_destroy(&jit->addr_hash);
2851
0
  ir_free(&jit->ctx);
2852
0
  return 1;
2853
0
}
2854
2855
static void *zend_jit_ir_compile(ir_ctx *ctx, size_t *size, const char *name)
2856
0
{
2857
0
  void *entry;
2858
0
  ir_code_buffer code_buffer;
2859
2860
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_SRC) {
2861
0
    if (name) fprintf(stderr, "%s: ; after folding\n", name);
2862
0
    ir_save(ctx, 0, stderr);
2863
0
  }
2864
2865
0
#if ZEND_DEBUG
2866
0
  ir_check(ctx);
2867
0
#endif
2868
2869
0
  ir_build_def_use_lists(ctx);
2870
2871
0
#if ZEND_DEBUG
2872
0
  ir_check(ctx);
2873
0
#endif
2874
2875
0
#if 1
2876
0
  ir_sccp(ctx);
2877
0
#endif
2878
2879
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_SCCP) {
2880
0
    if (name) fprintf(stderr, "%s: ; after SCCP\n", name);
2881
0
    ir_save(ctx, 0, stderr);
2882
0
  }
2883
2884
0
  ir_build_cfg(ctx);
2885
0
  ir_build_dominators_tree(ctx);
2886
0
  ir_find_loops(ctx);
2887
2888
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_CFG) {
2889
0
    if (name) fprintf(stderr, "%s: ; after CFG\n", name);
2890
0
    ir_save(ctx, IR_SAVE_CFG, stderr);
2891
0
  }
2892
2893
0
  ir_gcm(ctx);
2894
2895
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_GCM) {
2896
0
    if (name) fprintf(stderr, "%s: ; after GCM\n", name);
2897
0
    ir_save(ctx, IR_SAVE_CFG|IR_SAVE_CFG_MAP, stderr);
2898
0
  }
2899
2900
0
  ir_schedule(ctx);
2901
2902
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE) {
2903
0
    if (name) fprintf(stderr, "%s: ; after schedule\n", name);
2904
0
    ir_save(ctx, IR_SAVE_CFG, stderr);
2905
0
  }
2906
2907
0
  ir_match(ctx);
2908
0
#if !defined(IR_TARGET_AARCH64)
2909
0
  ctx->flags &= ~IR_USE_FRAME_POINTER; /* don't use FRAME_POINTER even with ALLOCA, TODO: cleanup this ??? */
2910
0
#endif
2911
0
  ir_assign_virtual_registers(ctx);
2912
0
  ir_compute_live_ranges(ctx);
2913
0
  ir_coalesce(ctx);
2914
0
  ir_reg_alloc(ctx);
2915
2916
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_REGS) {
2917
0
    if (name) fprintf(stderr, "%s: ; after register allocation\n", name);
2918
0
    ir_save(ctx, IR_SAVE_CFG|IR_SAVE_RULES|IR_SAVE_REGS, stderr);
2919
0
    ir_dump_live_ranges(ctx, stderr);
2920
0
  }
2921
2922
0
  ir_schedule_blocks(ctx);
2923
2924
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_IR_FINAL|ZEND_JIT_DEBUG_IR_CODEGEN)) {
2925
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_CODEGEN) {
2926
0
      if (name) fprintf(stderr, "%s: ; codegen\n", name);
2927
0
      ir_dump_codegen(ctx, stderr);
2928
0
    } else {
2929
0
      if (name) fprintf(stderr, "%s: ; final\n", name);
2930
0
      ir_save(ctx, IR_SAVE_CFG|IR_SAVE_RULES|IR_SAVE_REGS, stderr);
2931
0
    }
2932
0
  }
2933
2934
0
#if ZEND_DEBUG
2935
0
  ir_check(ctx);
2936
0
#endif
2937
2938
0
  code_buffer.start = dasm_buf;
2939
0
  code_buffer.end = dasm_end;
2940
0
  code_buffer.pos = *dasm_ptr;
2941
0
  ctx->code_buffer = &code_buffer;
2942
2943
0
  entry = ir_emit_code(ctx, size);
2944
2945
0
  *dasm_ptr = code_buffer.pos;
2946
2947
#if defined(IR_TARGET_AARCH64)
2948
  if (ctx->flags2 & IR_HAS_VENEERS) {
2949
    zend_jit_commit_veneers();
2950
  }
2951
#endif
2952
2953
0
  return entry;
2954
0
}
2955
2956
static void zend_jit_setup_stubs(void)
2957
0
{
2958
0
  zend_jit_ctx jit;
2959
0
  void *entry;
2960
0
  size_t size;
2961
0
  uint32_t i;
2962
2963
0
  for (i = 0; i < sizeof(zend_jit_stubs)/sizeof(zend_jit_stubs[0]); i++) {
2964
0
    zend_jit_init_ctx(&jit, zend_jit_stubs[i].flags);
2965
2966
0
    if (!zend_jit_stubs[i].stub(&jit)) {
2967
0
      zend_jit_free_ctx(&jit);
2968
0
      zend_jit_stub_handlers[i] = NULL;
2969
0
      continue;
2970
0
    }
2971
2972
0
    entry = zend_jit_ir_compile(&jit.ctx, &size, zend_jit_stubs[i].name);
2973
0
    if (!entry) {
2974
0
      zend_jit_free_ctx(&jit);
2975
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: could not compile stub");
2976
0
    }
2977
2978
0
    zend_jit_stub_handlers[i] = entry;
2979
2980
0
    if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
2981
#ifdef HAVE_CAPSTONE
2982
      if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
2983
        ir_disasm_add_symbol(zend_jit_stubs[i].name, (uintptr_t)entry, size);
2984
      }
2985
      if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_STUBS) {
2986
        ir_disasm(zend_jit_stubs[i].name,
2987
          entry, size, (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_ADDR) != 0, &jit.ctx, stderr);
2988
      }
2989
#endif
2990
0
#ifndef _WIN32
2991
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
2992
//        ir_mem_unprotect(entry, size);
2993
0
        ir_gdb_register(zend_jit_stubs[i].name, entry, size, 0, 0);
2994
//        ir_mem_protect(entry, size);
2995
0
      }
2996
2997
0
      if (JIT_G(debug) & (ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
2998
0
        ir_perf_map_register(zend_jit_stubs[i].name, entry, size);
2999
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3000
0
          ir_perf_jitdump_register(zend_jit_stubs[i].name, entry, size);
3001
0
        }
3002
0
      }
3003
0
#endif
3004
0
    }
3005
0
    zend_jit_free_ctx(&jit);
3006
0
  }
3007
0
}
3008
3009
#define REGISTER_HELPER(n)  \
3010
  ir_disasm_add_symbol(#n, (uint64_t)(uintptr_t)n, sizeof(void*));
3011
#define REGISTER_DATA(n)  \
3012
  ir_disasm_add_symbol(#n, (uint64_t)(uintptr_t)&n, sizeof(n));
3013
3014
static void zend_jit_setup_disasm(void)
3015
0
{
3016
#ifdef HAVE_CAPSTONE
3017
  ir_disasm_init();
3018
3019
  if (zend_vm_kind() == ZEND_VM_KIND_HYBRID) {
3020
    zend_op opline;
3021
3022
    memset(&opline, 0, sizeof(opline));
3023
3024
    opline.opcode = ZEND_DO_UCALL;
3025
    opline.result_type = IS_UNUSED;
3026
    zend_vm_set_opcode_handler(&opline);
3027
    ir_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3028
3029
    opline.opcode = ZEND_DO_UCALL;
3030
    opline.result_type = IS_VAR;
3031
    zend_vm_set_opcode_handler(&opline);
3032
    ir_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3033
3034
    opline.opcode = ZEND_DO_FCALL_BY_NAME;
3035
    opline.result_type = IS_UNUSED;
3036
    zend_vm_set_opcode_handler(&opline);
3037
    ir_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3038
3039
    opline.opcode = ZEND_DO_FCALL_BY_NAME;
3040
    opline.result_type = IS_VAR;
3041
    zend_vm_set_opcode_handler(&opline);
3042
    ir_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3043
3044
    opline.opcode = ZEND_DO_FCALL;
3045
    opline.result_type = IS_UNUSED;
3046
    zend_vm_set_opcode_handler(&opline);
3047
    ir_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3048
3049
    opline.opcode = ZEND_DO_FCALL;
3050
    opline.result_type = IS_VAR;
3051
    zend_vm_set_opcode_handler(&opline);
3052
    ir_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3053
3054
    opline.opcode = ZEND_RETURN;
3055
    opline.op1_type = IS_CONST;
3056
    zend_vm_set_opcode_handler(&opline);
3057
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_CONST_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3058
3059
    opline.opcode = ZEND_RETURN;
3060
    opline.op1_type = IS_TMP_VAR;
3061
    zend_vm_set_opcode_handler(&opline);
3062
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_TMP_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3063
3064
    opline.opcode = ZEND_RETURN;
3065
    opline.op1_type = IS_VAR;
3066
    zend_vm_set_opcode_handler(&opline);
3067
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_VAR_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3068
3069
    opline.opcode = ZEND_RETURN;
3070
    opline.op1_type = IS_CV;
3071
    zend_vm_set_opcode_handler(&opline);
3072
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_CV_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3073
3074
    ir_disasm_add_symbol("ZEND_HYBRID_HALT_LABEL", (uint64_t)(uintptr_t)zend_jit_halt_op->handler, sizeof(void*));
3075
  }
3076
3077
  REGISTER_DATA(zend_jit_profile_counter);
3078
3079
  REGISTER_HELPER(zend_runtime_jit);
3080
  REGISTER_HELPER(zend_jit_hot_func);
3081
  REGISTER_HELPER(zend_jit_trace_hot_root);
3082
  REGISTER_HELPER(zend_jit_trace_exit);
3083
3084
  REGISTER_HELPER(zend_jit_array_free);
3085
  REGISTER_HELPER(zend_jit_undefined_op_helper);
3086
  REGISTER_HELPER(zend_jit_pre_inc_typed_ref);
3087
  REGISTER_HELPER(zend_jit_pre_dec_typed_ref);
3088
  REGISTER_HELPER(zend_jit_post_inc_typed_ref);
3089
  REGISTER_HELPER(zend_jit_post_dec_typed_ref);
3090
  REGISTER_HELPER(zend_jit_pre_inc);
3091
  REGISTER_HELPER(zend_jit_pre_dec);
3092
  REGISTER_HELPER(zend_jit_add_arrays_helper);
3093
  REGISTER_HELPER(zend_jit_fast_assign_concat_helper);
3094
  REGISTER_HELPER(zend_jit_fast_concat_helper);
3095
  REGISTER_HELPER(zend_jit_fast_concat_tmp_helper);
3096
  REGISTER_HELPER(zend_jit_assign_op_to_typed_ref_tmp);
3097
  REGISTER_HELPER(zend_jit_assign_op_to_typed_ref);
3098
  REGISTER_HELPER(zend_jit_assign_const_to_typed_ref);
3099
  REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref);
3100
  REGISTER_HELPER(zend_jit_assign_var_to_typed_ref);
3101
  REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref);
3102
  REGISTER_HELPER(zend_jit_assign_const_to_typed_ref2);
3103
  REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref2);
3104
  REGISTER_HELPER(zend_jit_assign_var_to_typed_ref2);
3105
  REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref2);
3106
  REGISTER_HELPER(zend_jit_check_constant);
3107
  REGISTER_HELPER(zend_jit_get_constant);
3108
  REGISTER_HELPER(zend_jit_int_extend_stack_helper);
3109
  REGISTER_HELPER(zend_jit_extend_stack_helper);
3110
  REGISTER_HELPER(zend_jit_init_func_run_time_cache_helper);
3111
  REGISTER_HELPER(zend_jit_find_func_helper);
3112
  REGISTER_HELPER(zend_jit_find_ns_func_helper);
3113
  REGISTER_HELPER(zend_jit_jmp_frameless_helper);
3114
  REGISTER_HELPER(zend_jit_unref_helper);
3115
  REGISTER_HELPER(zend_jit_invalid_method_call);
3116
  REGISTER_HELPER(zend_jit_invalid_method_call_tmp);
3117
  REGISTER_HELPER(zend_jit_find_method_helper);
3118
  REGISTER_HELPER(zend_jit_find_method_tmp_helper);
3119
  REGISTER_HELPER(zend_jit_push_static_method_call_frame);
3120
  REGISTER_HELPER(zend_jit_push_static_method_call_frame_tmp);
3121
  REGISTER_HELPER(zend_jit_find_class_helper);
3122
  REGISTER_HELPER(zend_jit_find_static_method_helper);
3123
  REGISTER_HELPER(zend_jit_push_this_method_call_frame);
3124
  REGISTER_HELPER(zend_jit_free_trampoline_helper);
3125
  REGISTER_HELPER(zend_jit_verify_return_slow);
3126
  REGISTER_HELPER(zend_jit_deprecated_helper);
3127
  REGISTER_HELPER(zend_jit_undefined_long_key);
3128
  REGISTER_HELPER(zend_jit_undefined_long_key_ex);
3129
  REGISTER_HELPER(zend_jit_undefined_string_key);
3130
  REGISTER_HELPER(zend_jit_copy_extra_args_helper);
3131
  REGISTER_HELPER(zend_jit_copy_extra_args_helper_no_skip_recv);
3132
  REGISTER_HELPER(zend_jit_vm_stack_free_args_helper);
3133
  REGISTER_HELPER(zend_free_extra_named_params);
3134
  REGISTER_HELPER(zend_jit_free_call_frame);
3135
  REGISTER_HELPER(zend_jit_exception_in_interrupt_handler_helper);
3136
  REGISTER_HELPER(zend_jit_verify_arg_slow);
3137
  REGISTER_HELPER(zend_missing_arg_error);
3138
  REGISTER_HELPER(zend_jit_only_vars_by_reference);
3139
  REGISTER_HELPER(zend_jit_leave_func_helper);
3140
  REGISTER_HELPER(zend_jit_leave_nested_func_helper);
3141
  REGISTER_HELPER(zend_jit_leave_top_func_helper);
3142
  REGISTER_HELPER(zend_jit_fetch_global_helper);
3143
  REGISTER_HELPER(zend_jit_hash_index_lookup_rw_no_packed);
3144
  REGISTER_HELPER(zend_jit_hash_index_lookup_rw);
3145
  REGISTER_HELPER(zend_jit_hash_lookup_rw);
3146
  REGISTER_HELPER(zend_jit_symtable_find);
3147
  REGISTER_HELPER(zend_jit_symtable_lookup_w);
3148
  REGISTER_HELPER(zend_jit_symtable_lookup_rw);
3149
  REGISTER_HELPER(zend_jit_fetch_dim_r_helper);
3150
  REGISTER_HELPER(zend_jit_fetch_dim_is_helper);
3151
  REGISTER_HELPER(zend_jit_fetch_dim_isset_helper);
3152
  REGISTER_HELPER(zend_jit_fetch_dim_rw_helper);
3153
  REGISTER_HELPER(zend_jit_fetch_dim_w_helper);
3154
  REGISTER_HELPER(zend_jit_fetch_dim_str_offset_r_helper);
3155
  REGISTER_HELPER(zend_jit_fetch_dim_str_r_helper);
3156
  REGISTER_HELPER(zend_jit_fetch_dim_str_is_helper);
3157
  REGISTER_HELPER(zend_jit_fetch_dim_obj_r_helper);
3158
  REGISTER_HELPER(zend_jit_fetch_dim_obj_is_helper);
3159
  REGISTER_HELPER(zend_jit_invalid_array_access);
3160
  REGISTER_HELPER(zend_jit_zval_array_dup);
3161
  REGISTER_HELPER(zend_jit_prepare_assign_dim_ref);
3162
  REGISTER_HELPER(zend_jit_fetch_dim_obj_w_helper);
3163
  REGISTER_HELPER(zend_jit_fetch_dim_obj_rw_helper);
3164
  REGISTER_HELPER(zend_jit_isset_dim_helper);
3165
  REGISTER_HELPER(zend_jit_assign_dim_helper);
3166
  REGISTER_HELPER(zend_jit_assign_dim_op_helper);
3167
  REGISTER_HELPER(zend_jit_fetch_obj_w_slow);
3168
  REGISTER_HELPER(zend_jit_fetch_obj_r_slow);
3169
  REGISTER_HELPER(zend_jit_fetch_obj_r_slow_ex);
3170
  REGISTER_HELPER(zend_jit_fetch_obj_is_slow);
3171
  REGISTER_HELPER(zend_jit_fetch_obj_is_slow_ex);
3172
  REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic);
3173
  REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic_ex);
3174
  REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic);
3175
  REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic_ex);
3176
  REGISTER_HELPER(zend_jit_check_array_promotion);
3177
  REGISTER_HELPER(zend_jit_create_typed_ref);
3178
  REGISTER_HELPER(zend_jit_invalid_property_write);
3179
  REGISTER_HELPER(zend_jit_invalid_property_read);
3180
  REGISTER_HELPER(zend_jit_extract_helper);
3181
  REGISTER_HELPER(zend_jit_invalid_property_assign);
3182
  REGISTER_HELPER(zend_jit_assign_to_typed_prop);
3183
  REGISTER_HELPER(zend_jit_assign_obj_helper);
3184
  REGISTER_HELPER(zend_jit_invalid_property_assign_op);
3185
  REGISTER_HELPER(zend_jit_assign_op_to_typed_prop);
3186
  REGISTER_HELPER(zend_jit_assign_obj_op_helper);
3187
  REGISTER_HELPER(zend_jit_invalid_property_incdec);
3188
  REGISTER_HELPER(zend_jit_inc_typed_prop);
3189
  REGISTER_HELPER(zend_jit_dec_typed_prop);
3190
  REGISTER_HELPER(zend_jit_pre_inc_typed_prop);
3191
  REGISTER_HELPER(zend_jit_post_inc_typed_prop);
3192
  REGISTER_HELPER(zend_jit_pre_dec_typed_prop);
3193
  REGISTER_HELPER(zend_jit_post_dec_typed_prop);
3194
  REGISTER_HELPER(zend_jit_pre_inc_obj_helper);
3195
  REGISTER_HELPER(zend_jit_post_inc_obj_helper);
3196
  REGISTER_HELPER(zend_jit_pre_dec_obj_helper);
3197
  REGISTER_HELPER(zend_jit_post_dec_obj_helper);
3198
  REGISTER_HELPER(zend_jit_uninit_static_prop);
3199
  REGISTER_HELPER(zend_jit_rope_end);
3200
  REGISTER_HELPER(zend_fcall_interrupt);
3201
3202
#ifndef ZTS
3203
  REGISTER_DATA(EG(current_execute_data));
3204
  REGISTER_DATA(EG(exception));
3205
  REGISTER_DATA(EG(opline_before_exception));
3206
  REGISTER_DATA(EG(vm_interrupt));
3207
  REGISTER_DATA(EG(timed_out));
3208
  REGISTER_DATA(EG(uninitialized_zval));
3209
  REGISTER_DATA(EG(zend_constants));
3210
  REGISTER_DATA(EG(jit_trace_num));
3211
  REGISTER_DATA(EG(vm_stack_top));
3212
  REGISTER_DATA(EG(vm_stack_end));
3213
  REGISTER_DATA(EG(exception_op));
3214
  REGISTER_DATA(EG(symbol_table));
3215
3216
  REGISTER_DATA(CG(map_ptr_base));
3217
#else /* ZTS */
3218
  REGISTER_HELPER(zend_jit_get_tsrm_ls_cache);
3219
#endif
3220
#endif
3221
0
}
3222
3223
static void zend_jit_calc_trace_prologue_size(void)
3224
0
{
3225
0
  zend_jit_ctx jit_ctx;
3226
0
  zend_jit_ctx *jit = &jit_ctx;
3227
0
  void *entry;
3228
0
  size_t size;
3229
3230
0
  zend_jit_init_ctx(jit, (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) ? 0 : IR_START_BR_TARGET);
3231
3232
0
  if (!GCC_GLOBAL_REGS) {
3233
0
    if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
3234
0
      ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
3235
0
      ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
3236
0
      jit_STORE_FP(jit, execute_data_ref);
3237
0
      jit_STORE_IP(jit, opline_ref);
3238
0
    }
3239
0
    jit->ctx.flags |= IR_FASTCALL_FUNC;
3240
0
  }
3241
3242
0
  ir_UNREACHABLE();
3243
3244
0
  entry = zend_jit_ir_compile(&jit->ctx, &size, "JIT$trace_prologue");
3245
0
  zend_jit_free_ctx(jit);
3246
3247
0
  if (!entry) {
3248
0
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: could not compile prologue");
3249
0
  }
3250
3251
0
  zend_jit_trace_prologue_size = size;
3252
0
}
3253
3254
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
3255
static uintptr_t zend_jit_hybrid_vm_sp_adj = 0;
3256
3257
typedef struct _Unwind_Context _Unwind_Context;
3258
typedef int (*_Unwind_Trace_Fn)(_Unwind_Context *, void *);
3259
extern int _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
3260
extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
3261
3262
typedef struct _zend_jit_unwind_arg {
3263
  int cnt;
3264
  uintptr_t cfa[3];
3265
} zend_jit_unwind_arg;
3266
3267
static int zend_jit_unwind_cb(_Unwind_Context *ctx, void *a)
3268
0
{
3269
0
  zend_jit_unwind_arg *arg = (zend_jit_unwind_arg*)a;
3270
0
  arg->cfa[arg->cnt] = _Unwind_GetCFA(ctx);
3271
0
  arg->cnt++;
3272
0
  if (arg->cnt == 3) {
3273
0
    return 5; // _URC_END_OF_STACK
3274
0
  }
3275
0
  return 0; // _URC_NO_REASON;
3276
0
}
3277
3278
static void ZEND_FASTCALL zend_jit_touch_vm_stack_data(void *vm_stack_data)
3279
0
{
3280
0
  zend_jit_unwind_arg arg;
3281
0
3282
0
  memset(&arg, 0, sizeof(arg));
3283
0
  _Unwind_Backtrace(zend_jit_unwind_cb, &arg);
3284
0
  if (arg.cnt == 3) {
3285
0
    zend_jit_hybrid_vm_sp_adj = arg.cfa[2] - arg.cfa[1];
3286
0
  }
3287
0
}
3288
3289
extern void (ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data);
3290
3291
static zend_never_inline void zend_jit_set_sp_adj_vm(void)
3292
0
{
3293
0
  void (ZEND_FASTCALL *orig_zend_touch_vm_stack_data)(void *);
3294
0
3295
0
  orig_zend_touch_vm_stack_data = zend_touch_vm_stack_data;
3296
0
  zend_touch_vm_stack_data = zend_jit_touch_vm_stack_data;
3297
0
  execute_ex(NULL);                                        // set sp_adj[SP_ADJ_VM]
3298
0
  zend_touch_vm_stack_data = orig_zend_touch_vm_stack_data;
3299
0
}
3300
#endif
3301
3302
#ifdef _WIN64
3303
/*
3304
 * We use a single unwind entry for the whole JIT buffer.
3305
 * This works, because all the JIT-ed PHP functions have the same "fixed stack frame".
3306
 */
3307
static PRUNTIME_FUNCTION zend_jit_uw_func = NULL;
3308
3309
#ifdef ZEND_JIT_RT_UNWINDER
3310
static PRUNTIME_FUNCTION zend_jit_unwind_callback(DWORD64 pc, PVOID context)
3311
{
3312
  return zend_jit_uw_func;
3313
}
3314
#endif
3315
3316
static void zend_jit_setup_unwinder(void)
3317
{
3318
  /* Hardcoded SEH unwind data for JIT-ed PHP functions with "fixed stack frame" */
3319
  static const unsigned char uw_data[] = {
3320
    0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
3321
    0x10, // UBYTE Size of prolog
3322
    0x09, // UBYTE Count of unwind codes
3323
    0x00, // UBYTE: 4 Frame Register, UBYTE: 4 Frame Register offset (scaled)
3324
    // USHORT * n Unwind codes array
3325
    0x10, 0x82, // c: subq $0x48, %rsp
3326
    0x0c, 0xf0, // a: pushq %r15
3327
    0x0a, 0xe0, // 8: pushq %r14
3328
    0x08, 0xd0, // 6: pushq %r13
3329
    0x06, 0xc0, // 4: pushq %r12
3330
    0x04, 0x70, // 3: pushq %rdi
3331
    0x03, 0x60, // 2: pushq %rsi
3332
    0x02, 0x50, // 1: pushq %rbp
3333
    0x01, 0x30, // 0: pushq %rbx
3334
    0x00, 0x00,
3335
  };
3336
  static const unsigned char uw_data_exitcall[] = {
3337
    0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
3338
    0x10, // UBYTE Size of prolog
3339
    0x0a, // UBYTE Count of unwind codes
3340
    0x00, // UBYTE: 4 Frame Register, UBYTE: 4 Frame Register offset (scaled)
3341
    // USHORT * n Unwind codes array
3342
    0x10, 0x01, 0x2f, 0x00, // c: subq 376, %rsp ; 0x48 + 32+16*8+16*8+8+8
3343
    0x0c, 0xf0, // a: pushq %r15
3344
    0x0a, 0xe0, // 8: pushq %r14
3345
    0x08, 0xd0, // 6: pushq %r13
3346
    0x06, 0xc0, // 4: pushq %r12
3347
    0x04, 0x70, // 3: pushq %rdi
3348
    0x03, 0x60, // 2: pushq %rsi
3349
    0x02, 0x50, // 1: pushq %rbp
3350
    0x01, 0x30, // 0: pushq %rbx
3351
  };
3352
3353
  zend_jit_uw_func = (PRUNTIME_FUNCTION)*dasm_ptr;
3354
  *dasm_ptr = (char*)*dasm_ptr + ZEND_MM_ALIGNED_SIZE_EX(sizeof(RUNTIME_FUNCTION) * 4 +
3355
    sizeof(uw_data) + sizeof(uw_data_exitcall) + sizeof(uw_data), 16);
3356
3357
  zend_jit_uw_func[0].BeginAddress = 0;
3358
  zend_jit_uw_func[1].BeginAddress = (uintptr_t)zend_jit_stub_handlers[jit_stub_trace_exit] - (uintptr_t)dasm_buf;
3359
  zend_jit_uw_func[2].BeginAddress = (uintptr_t)zend_jit_stub_handlers[jit_stub_undefined_offset] - (uintptr_t)dasm_buf;
3360
3361
  zend_jit_uw_func[0].EndAddress = zend_jit_uw_func[1].BeginAddress;
3362
  zend_jit_uw_func[1].EndAddress = zend_jit_uw_func[2].BeginAddress;
3363
  zend_jit_uw_func[2].EndAddress = (uintptr_t)dasm_end - (uintptr_t)dasm_buf;
3364
3365
  zend_jit_uw_func[0].UnwindData = (uintptr_t)zend_jit_uw_func  - (uintptr_t)dasm_buf + sizeof(RUNTIME_FUNCTION) * 4;
3366
  zend_jit_uw_func[1].UnwindData = zend_jit_uw_func[0].UnwindData + sizeof(uw_data);
3367
  zend_jit_uw_func[2].UnwindData = zend_jit_uw_func[1].UnwindData + sizeof(uw_data_exitcall);
3368
3369
  memcpy((char*)dasm_buf + zend_jit_uw_func[0].UnwindData, uw_data, sizeof(uw_data));
3370
  memcpy((char*)dasm_buf + zend_jit_uw_func[1].UnwindData, uw_data_exitcall, sizeof(uw_data_exitcall));
3371
  memcpy((char*)dasm_buf + zend_jit_uw_func[2].UnwindData, uw_data, sizeof(uw_data));
3372
3373
#ifdef ZEND_JIT_RT_UNWINDER
3374
  RtlInstallFunctionTableCallback(
3375
    (uintptr_t)dasm_buf | 3,
3376
    (uintptr_t) dasm_buf,
3377
    (uintptr_t)dasm_end - (uintptr_t)dasm_buf,
3378
    zend_jit_unwind_callback,
3379
    NULL,
3380
    NULL);
3381
#else
3382
  RtlAddFunctionTable(zend_jit_uw_func, 3, (uintptr_t)dasm_buf);
3383
#endif
3384
}
3385
#endif
3386
3387
static void zend_jit_setup(bool reattached)
3388
0
{
3389
#if defined(IR_TARGET_X86)
3390
  if (!zend_cpu_supports_sse2()) {
3391
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: CPU doesn't support SSE2");
3392
  }
3393
#endif
3394
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
3395
0
  allowed_opt_flags = 0;
3396
0
  if (zend_cpu_supports_avx()) {
3397
0
    allowed_opt_flags |= ZEND_JIT_CPU_AVX;
3398
0
  }
3399
# if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
3400
  if (zend_cpu_supports_cldemote()) {
3401
    default_mflags |= IR_X86_CLDEMOTE;
3402
  }
3403
# endif
3404
0
#endif
3405
3406
#ifdef ZTS
3407
  zend_result result = zend_jit_resolve_tsrm_ls_cache_offsets(
3408
    &tsrm_ls_cache_tcb_offset,
3409
    &tsrm_tls_index,
3410
    &tsrm_tls_offset
3411
  );
3412
  if (result == FAILURE) {
3413
    zend_accel_error(ACCEL_LOG_INFO,
3414
        "Could not get _tsrm_ls_cache offsets, will fallback to runtime resolution");
3415
  }
3416
#endif
3417
3418
0
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
3419
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
3420
0
    zend_jit_set_sp_adj_vm(); // set zend_jit_hybrid_vm_sp_adj
3421
0
  }
3422
0
#endif
3423
3424
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
3425
0
    zend_jit_setup_disasm();
3426
0
  }
3427
3428
0
#ifndef _WIN32
3429
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3430
0
    ir_perf_jitdump_open();
3431
0
  }
3432
3433
0
#endif
3434
0
  zend_long debug = JIT_G(debug);
3435
0
  if (!(debug & ZEND_JIT_DEBUG_ASM_STUBS)) {
3436
0
    JIT_G(debug) &= ~(ZEND_JIT_DEBUG_IR_SRC|ZEND_JIT_DEBUG_IR_FINAL|
3437
0
      ZEND_JIT_DEBUG_IR_CODEGEN|
3438
0
      ZEND_JIT_DEBUG_IR_AFTER_SCCP|ZEND_JIT_DEBUG_IR_AFTER_CFG|ZEND_JIT_DEBUG_IR_AFTER_GCM|
3439
0
      ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE|ZEND_JIT_DEBUG_IR_AFTER_REGS);
3440
0
  }
3441
3442
0
  zend_jit_calc_trace_prologue_size();
3443
0
  if (!reattached) {
3444
0
    zend_jit_setup_stubs();
3445
0
  }
3446
0
  JIT_G(debug) = debug;
3447
3448
#ifdef _WIN64
3449
  zend_jit_setup_unwinder();
3450
#endif
3451
0
}
3452
3453
static void zend_jit_shutdown_ir(void)
3454
0
{
3455
0
#ifndef _WIN32
3456
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3457
0
    ir_perf_jitdump_close();
3458
0
  }
3459
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
3460
0
    ir_gdb_unregister_all();
3461
0
  }
3462
0
#endif
3463
#ifdef HAVE_CAPSTONE
3464
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
3465
    ir_disasm_free();
3466
  }
3467
#endif
3468
0
}
3469
3470
/* PHP control flow reconstruction helpers */
3471
static ir_ref jit_IF_ex(zend_jit_ctx *jit, ir_ref condition, ir_ref true_block)
3472
0
{
3473
0
  ir_ref ref = ir_IF(condition);
3474
  /* op3 is used as a temporary storage for PHP BB number to reconstruct PHP control flow.
3475
   *
3476
   * It's used in jit_IF_TRUE_FALSE_ex() to select IF_TRUE or IF_FALSE instructions
3477
   * to start target block
3478
   */
3479
0
  ir_set_op(&jit->ctx, ref, 3, true_block);
3480
0
  return ref;
3481
0
}
3482
3483
static void jit_IF_TRUE_FALSE_ex(zend_jit_ctx *jit, ir_ref if_ref, ir_ref true_block)
3484
0
{
3485
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3486
0
  ZEND_ASSERT(if_ref);
3487
0
  ZEND_ASSERT(jit->ctx.ir_base[if_ref].op == IR_IF);
3488
0
  ZEND_ASSERT(jit->ctx.ir_base[if_ref].op3);
3489
0
  if (jit->ctx.ir_base[if_ref].op3 == true_block) {
3490
0
    ir_IF_TRUE(if_ref);
3491
0
  } else {
3492
0
    ir_IF_FALSE(if_ref);
3493
0
  }
3494
0
}
3495
3496
static void zend_jit_case_start(zend_jit_ctx *jit, int switch_b, int case_b, ir_ref switch_ref);
3497
3498
static void _zend_jit_add_predecessor_ref(zend_jit_ctx *jit, int b, int pred, ir_ref ref)
3499
0
{
3500
0
  int i, *p;
3501
0
  zend_basic_block *bb;
3502
0
  ir_ref *r, header;
3503
3504
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3505
0
  bb = &jit->ssa->cfg.blocks[b];
3506
0
  p = &jit->ssa->cfg.predecessors[bb->predecessor_offset];
3507
0
  r = &jit->bb_edges[jit->bb_predecessors[b]];
3508
0
  for (i = 0; i < bb->predecessors_count; i++, p++, r++) {
3509
0
    if (*p == pred) {
3510
0
      ZEND_ASSERT(*r == IR_UNUSED || *r == ref);
3511
0
      header = jit->bb_start_ref[b];
3512
0
      if (header) {
3513
        /* this is back edge */
3514
0
        ZEND_ASSERT(jit->ctx.ir_base[header].op == IR_LOOP_BEGIN);
3515
0
        if (jit->ctx.ir_base[ref].op == IR_END) {
3516
0
          jit->ctx.ir_base[ref].op = IR_LOOP_END;
3517
0
        } else if (jit->ctx.ir_base[ref].op == IR_IF) {
3518
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3519
0
          ref = ir_LOOP_END();
3520
0
        } else if (jit->ctx.ir_base[ref].op == IR_SWITCH) {
3521
0
          zend_jit_case_start(jit, pred, b, ref);
3522
0
          ref = ir_LOOP_END();
3523
0
        } else if (jit->ctx.ir_base[ref].op == IR_UNREACHABLE) {
3524
0
          ir_BEGIN(ref);
3525
0
          ref = ir_LOOP_END();
3526
0
        } else {
3527
0
          ZEND_UNREACHABLE();
3528
0
        }
3529
0
        ir_MERGE_SET_OP(header, i + 1, ref);
3530
0
      }
3531
0
      *r = ref;
3532
0
      return;
3533
0
    }
3534
0
  }
3535
0
  ZEND_UNREACHABLE();
3536
0
}
3537
3538
static void _zend_jit_merge_smart_branch_inputs(zend_jit_ctx *jit,
3539
                                                uint32_t      true_label,
3540
                                                uint32_t      false_label,
3541
                                                ir_ref        true_inputs,
3542
                                                ir_ref        false_inputs)
3543
0
{
3544
0
  ir_ref true_path = IR_UNUSED, false_path = IR_UNUSED;
3545
3546
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3547
0
  if (true_inputs) {
3548
0
    ZEND_ASSERT(jit->ctx.ir_base[true_inputs].op == IR_END);
3549
0
    if (!jit->ctx.ir_base[true_inputs].op2) {
3550
0
      true_path = true_inputs;
3551
0
    } else {
3552
0
      ir_MERGE_list(true_inputs);
3553
0
      true_path = ir_END();
3554
0
    }
3555
0
  }
3556
0
  if (false_inputs) {
3557
0
    ZEND_ASSERT(jit->ctx.ir_base[false_inputs].op == IR_END);
3558
0
    if (!jit->ctx.ir_base[false_inputs].op2) {
3559
0
      false_path = false_inputs;
3560
0
    } else {
3561
0
      ir_MERGE_list(false_inputs);
3562
0
      false_path = ir_END();
3563
0
    }
3564
0
  }
3565
3566
0
  if (true_label == false_label && true_path && false_path) {
3567
0
    ir_MERGE_2(true_path, false_path);
3568
0
    _zend_jit_add_predecessor_ref(jit, true_label, jit->b, ir_END());
3569
0
  } else if (!true_path && !false_path) {
3570
    /* dead code */
3571
0
    true_path = ir_END();
3572
0
    _zend_jit_add_predecessor_ref(jit, true_label, jit->b, true_path);
3573
0
  } else {
3574
0
    if (true_path) {
3575
0
      _zend_jit_add_predecessor_ref(jit, true_label, jit->b, true_path);
3576
0
    }
3577
0
    if (false_path) {
3578
0
      _zend_jit_add_predecessor_ref(jit, false_label, jit->b, false_path);
3579
0
    }
3580
0
  }
3581
3582
0
  jit->b = -1;
3583
0
}
3584
3585
static void _zend_jit_fix_merges(zend_jit_ctx *jit)
3586
0
{
3587
0
  int i, count;
3588
0
  ir_ref j, k, n, *p, *q, *r;
3589
0
  ir_ref ref;
3590
0
  ir_insn *insn, *phi;
3591
3592
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3593
0
  count = jit->ssa->cfg.blocks_count;
3594
0
  for (i = 0, p = jit->bb_start_ref; i < count; i++, p++) {
3595
0
    ref = *p;
3596
0
    if (ref) {
3597
0
      insn = &jit->ctx.ir_base[ref];
3598
0
      if (insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN) {
3599
0
        n = insn->inputs_count;
3600
        /* Remove IS_UNUSED inputs */
3601
0
        for (j = k = 0, q = r = insn->ops + 1; j < n; j++, q++) {
3602
0
          if (*q) {
3603
0
            if (q != r) {
3604
0
              *r = *q;
3605
0
              phi = insn + 1 + (n >> 2);
3606
0
              while (phi->op == IR_PI) {
3607
0
                phi++;
3608
0
              }
3609
0
              while (phi->op == IR_PHI) {
3610
0
                ir_insn_set_op(phi, k + 2, ir_insn_op(phi, j + 2));
3611
0
                phi += 1 + ((n + 1) >> 2);
3612
0
              }
3613
0
            }
3614
0
            k++;
3615
0
            r++;
3616
0
          }
3617
0
        }
3618
0
        if (k != n) {
3619
0
          ir_ref n2, k2;
3620
3621
0
          if (k <= 1) {
3622
0
            insn->op = IR_BEGIN;
3623
0
            insn->inputs_count = 0;
3624
0
          } else {
3625
0
            insn->inputs_count = k;
3626
0
          }
3627
0
          n2 = 1 + (n >> 2);
3628
0
          k2 = 1 + (k >> 2);
3629
0
          while (k2 != n2) {
3630
0
            (insn+k2)->optx = IR_NOP;
3631
0
            k2++;
3632
0
          }
3633
0
          phi = insn + 1 + (n >> 2);
3634
0
          while (phi->op == IR_PI) {
3635
0
            phi++;
3636
0
          }
3637
0
          while (phi->op == IR_PHI) {
3638
0
            if (k <= 1) {
3639
0
              phi->op = IR_COPY;
3640
0
              phi->op1 = phi->op2;
3641
0
              phi->op2 = 1;
3642
0
              phi->inputs_count = 0;
3643
0
            } else {
3644
0
              phi->inputs_count = k + 1;
3645
0
            }
3646
0
            n2 = 1 + ((n + 1) >> 2);
3647
0
            k2 = 1 + ((k + 1) >> 2);
3648
0
            while (k2 != n2) {
3649
0
              (phi+k2)->optx = IR_NOP;
3650
0
              k2++;
3651
0
            }
3652
0
            phi += 1 + ((n + 1) >> 2);
3653
0
          }
3654
0
        }
3655
0
      }
3656
0
    }
3657
0
  }
3658
0
}
3659
3660
static void zend_jit_case_start(zend_jit_ctx *jit, int switch_b, int case_b, ir_ref switch_ref)
3661
0
{
3662
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[switch_b];
3663
0
  const zend_op *opline = &jit->op_array->opcodes[bb->start + bb->len - 1];
3664
3665
0
  if (opline->opcode == ZEND_SWITCH_LONG
3666
0
   || opline->opcode == ZEND_SWITCH_STRING
3667
0
   || opline->opcode == ZEND_MATCH) {
3668
0
    HashTable *jumptable = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
3669
0
    const zend_op *default_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
3670
0
    int default_b = jit->ssa->cfg.map[default_opline - jit->op_array->opcodes];
3671
0
    zval *zv;
3672
0
    ir_ref list = IR_UNUSED, idx;
3673
0
    bool first = true;
3674
3675
0
    ZEND_HASH_FOREACH_VAL(jumptable, zv) {
3676
0
      const zend_op *target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
3677
0
      int b = jit->ssa->cfg.map[target - jit->op_array->opcodes];
3678
3679
0
      if (b == case_b) {
3680
0
        if (!first) {
3681
0
          ir_END_list(list);
3682
0
        }
3683
0
        if (HT_IS_PACKED(jumptable)) {
3684
0
          idx = ir_CONST_LONG(zv - jumptable->arPacked);
3685
0
        } else {
3686
0
          idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
3687
0
        }
3688
0
        ir_CASE_VAL(switch_ref, idx);
3689
0
        first = false;
3690
0
      }
3691
0
    } ZEND_HASH_FOREACH_END();
3692
0
    if (default_b == case_b) {
3693
0
      if (!first) {
3694
0
        ir_END_list(list);
3695
0
      }
3696
0
      if (jit->ctx.ir_base[switch_ref].op3) {
3697
        /* op3 may contain a list of additional "default" path inputs for MATCH */
3698
0
        ir_ref ref = jit->ctx.ir_base[switch_ref].op3;
3699
0
        jit->ctx.ir_base[switch_ref].op3 = IS_UNDEF;
3700
0
        ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_END);
3701
0
        ir_ref end = ref;
3702
0
        while (jit->ctx.ir_base[end].op2) {
3703
0
          ZEND_ASSERT(jit->ctx.ir_base[end].op == IR_END);
3704
0
          end = jit->ctx.ir_base[end].op2;
3705
0
        }
3706
0
        jit->ctx.ir_base[end].op2 = list;
3707
0
        list = ref;
3708
0
      }
3709
0
      ir_CASE_DEFAULT(switch_ref);
3710
0
    }
3711
0
    if (list) {
3712
0
      ir_END_list(list);
3713
0
      ir_MERGE_list(list);
3714
0
    }
3715
0
  } else {
3716
0
    ZEND_UNREACHABLE();
3717
0
  }
3718
0
}
3719
3720
static int zend_jit_bb_start(zend_jit_ctx *jit, int b)
3721
0
{
3722
0
  zend_basic_block *bb;
3723
0
  int i, n, *p, pred;
3724
0
  ir_ref ref, bb_start;
3725
3726
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3727
0
  ZEND_ASSERT(b < jit->ssa->cfg.blocks_count);
3728
0
  bb = &jit->ssa->cfg.blocks[b];
3729
0
  ZEND_ASSERT((bb->flags & ZEND_BB_REACHABLE) != 0);
3730
0
  n = bb->predecessors_count;
3731
3732
0
  if (n == 0) {
3733
    /* pass */
3734
0
    ZEND_ASSERT(jit->ctx.control);
3735
0
#if ZEND_DEBUG
3736
0
    ref = jit->ctx.control;
3737
0
    ir_insn *insn = &jit->ctx.ir_base[ref];
3738
0
    while (insn->op >= IR_CALL && insn->op <= IR_TRAP) {
3739
0
      ref = insn->op1;
3740
0
      insn = &jit->ctx.ir_base[ref];
3741
0
    }
3742
0
    ZEND_ASSERT(insn->op == IR_START);
3743
0
    ZEND_ASSERT(ref == 1);
3744
0
#endif
3745
0
    bb_start = 1;
3746
0
    if (jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY) {
3747
      /* prvent END/BEGIN merging */
3748
0
      jit->ctx.control = ir_emit1(&jit->ctx, IR_BEGIN, ir_END());
3749
0
      bb_start = jit->ctx.control;
3750
0
    }
3751
0
  } else if (n == 1) {
3752
0
    ZEND_ASSERT(!jit->ctx.control);
3753
0
    pred = jit->ssa->cfg.predecessors[bb->predecessor_offset];
3754
0
    ref = jit->bb_edges[jit->bb_predecessors[b]];
3755
0
    if (ref == IR_UNUSED) {
3756
0
      if (!jit->ctx.control) {
3757
0
        ir_BEGIN(IR_UNUSED); /* unreachable block */
3758
0
      }
3759
0
    } else {
3760
0
      ir_op op = jit->ctx.ir_base[ref].op;
3761
3762
0
      if (op == IR_IF) {
3763
0
        if (!jit->ctx.control) {
3764
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3765
0
        } else {
3766
0
          ir_ref entry_path = ir_END();
3767
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3768
0
          ir_MERGE_WITH(entry_path);
3769
0
        }
3770
0
      } else if (op == IR_SWITCH) {
3771
0
        zend_jit_case_start(jit, pred, b, ref);
3772
0
      } else {
3773
0
        if (!jit->ctx.control) {
3774
0
          ZEND_ASSERT(op == IR_END || op == IR_UNREACHABLE || op == IR_RETURN);
3775
0
          if ((jit->ssa->cfg.blocks[b].flags & ZEND_BB_RECV_ENTRY)
3776
0
           && (jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY)) {
3777
            /* prvent END/BEGIN merging */
3778
0
            jit->ctx.control = ir_emit1(&jit->ctx, IR_BEGIN, ref);
3779
0
          } else {
3780
0
            ir_BEGIN(ref);
3781
0
          }
3782
0
        } else {
3783
0
          ir_MERGE_WITH(ref);
3784
0
        }
3785
0
      }
3786
0
    }
3787
0
    bb_start = jit->ctx.control;
3788
0
  } else {
3789
0
    int forward_edges_count = 0;
3790
0
    int back_edges_count = 0;
3791
0
    ir_ref *pred_refs;
3792
0
    ir_ref entry_path = IR_UNUSED;
3793
0
    ALLOCA_FLAG(use_heap);
3794
3795
0
    ZEND_ASSERT(!jit->ctx.control);
3796
0
    if (jit->ctx.control) {
3797
0
      entry_path = ir_END();
3798
0
    }
3799
0
    pred_refs = (ir_ref *)do_alloca(sizeof(ir_ref) * n, use_heap);
3800
0
    for (i = 0, p = jit->ssa->cfg.predecessors + bb->predecessor_offset; i < n; p++, i++) {
3801
0
      pred = *p;
3802
0
      if (jit->bb_start_ref[pred]) {
3803
        /* forward edge */
3804
0
        forward_edges_count++;
3805
0
        ref = jit->bb_edges[jit->bb_predecessors[b] + i];
3806
0
        if (ref == IR_UNUSED) {
3807
          /* dead edge */
3808
0
          pred_refs[i] = IR_UNUSED;
3809
0
        } else {
3810
0
          ir_op op = jit->ctx.ir_base[ref].op;
3811
3812
0
          if (op == IR_IF) {
3813
0
            jit_IF_TRUE_FALSE_ex(jit, ref, b);
3814
0
            pred_refs[i] = ir_END();
3815
0
          } else if (op == IR_SWITCH) {
3816
0
            zend_jit_case_start(jit, pred, b, ref);
3817
0
            pred_refs[i] = ir_END();
3818
0
          } else {
3819
0
            ZEND_ASSERT(op == IR_END || op == IR_UNREACHABLE || op == IR_RETURN);
3820
0
            pred_refs[i] = ref;
3821
0
          }
3822
0
        }
3823
0
      } else {
3824
        /* backward edge */
3825
0
        back_edges_count++;
3826
0
        pred_refs[i] = IR_UNUSED;
3827
0
      }
3828
0
    }
3829
3830
0
    if (bb->flags & ZEND_BB_LOOP_HEADER) {
3831
0
      ZEND_ASSERT(back_edges_count != 0);
3832
0
      ZEND_ASSERT(forward_edges_count != 0);
3833
0
      ir_MERGE_N(n, pred_refs);
3834
0
      jit->ctx.ir_base[jit->ctx.control].op = IR_LOOP_BEGIN;
3835
0
      bb_start = jit->ctx.control;
3836
0
      if (entry_path) {
3837
0
        ir_MERGE_WITH(entry_path);
3838
0
      }
3839
0
    } else {
3840
//      ZEND_ASSERT(back_edges_count != 0);
3841
      /* edges from exceptional blocks may be counted as back edges */
3842
0
      ir_MERGE_N(n, pred_refs);
3843
0
      bb_start = jit->ctx.control;
3844
0
      if (entry_path) {
3845
0
        ir_MERGE_WITH(entry_path);
3846
0
      }
3847
0
    }
3848
0
    free_alloca(pred_refs, use_heap);
3849
0
  }
3850
0
  jit->b = b;
3851
0
  jit->bb_start_ref[b] = bb_start;
3852
3853
0
  if ((bb->flags & ZEND_BB_ENTRY) || (bb->idom >= 0 && jit->bb_start_ref[bb->idom] < jit->ctx.fold_cse_limit)) {
3854
0
    jit->ctx.fold_cse_limit = bb_start;
3855
0
  }
3856
3857
0
  return 1;
3858
0
}
3859
3860
static int zend_jit_bb_end(zend_jit_ctx *jit, int b)
3861
0
{
3862
0
  int succ;
3863
0
  zend_basic_block *bb;
3864
3865
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3866
0
  if (jit->b != b) {
3867
0
    return 1;
3868
0
  }
3869
3870
0
  bb = &jit->ssa->cfg.blocks[b];
3871
0
  ZEND_ASSERT(bb->successors_count != 0);
3872
0
  if (bb->successors_count == 1) {
3873
0
    succ = bb->successors[0];
3874
0
  } else {
3875
0
    const zend_op *opline = &jit->op_array->opcodes[bb->start + bb->len - 1];
3876
3877
    /* Use only the following successor of SWITCH and FE_RESET_R */
3878
0
    ZEND_ASSERT(opline->opcode == ZEND_SWITCH_LONG
3879
0
     || opline->opcode == ZEND_SWITCH_STRING
3880
0
     || opline->opcode == ZEND_MATCH
3881
0
     || opline->opcode == ZEND_FE_RESET_R);
3882
0
    succ = b + 1;
3883
0
  }
3884
0
  _zend_jit_add_predecessor_ref(jit, succ, b, ir_END());
3885
0
  jit->b = -1;
3886
0
  return 1;
3887
0
}
3888
3889
static int jit_CMP_IP(zend_jit_ctx *jit, ir_op op, const zend_op *next_opline)
3890
0
{
3891
0
  ir_ref ref;
3892
3893
0
#if 1
3894
0
  ref = jit_IP32(jit);
3895
0
  ref = ir_CMP_OP(op, ref, ir_CONST_U32((uint32_t)(uintptr_t)next_opline));
3896
#else
3897
  ref = jit_IP(jit);
3898
  ref = ir_CMP_OP(op, ref, ir_CONST_ADDR(next_opline));
3899
#endif
3900
0
  return ref;
3901
0
}
3902
3903
static int zend_jit_jmp_frameless(
3904
  zend_jit_ctx *jit,
3905
  const zend_op *opline,
3906
  const void *exit_addr,
3907
  zend_jmp_fl_result guard
3908
0
) {
3909
0
  ir_ref ref, if_ref, cache_result, function_result, phi_result, cache_slot_ref;
3910
0
  zend_basic_block *bb;
3911
3912
  // JIT: CACHED_PTR(opline->extended_value)
3913
0
  cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value);
3914
0
  cache_result = ir_LOAD_L(cache_slot_ref);
3915
3916
  // JIT: if (UNEXPECTED(!result))
3917
0
  if_ref = ir_IF(cache_result);
3918
0
  ir_IF_FALSE_cold(if_ref);
3919
0
  zval *func_name_zv = RT_CONSTANT(opline, opline->op1);
3920
0
  function_result = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_jit_jmp_frameless_helper),
3921
0
    ir_CONST_ADDR(func_name_zv),
3922
0
    cache_slot_ref);
3923
0
  ir_MERGE_WITH_EMPTY_TRUE(if_ref);
3924
3925
0
  phi_result = ir_PHI_2(IR_LONG, function_result, cache_result);
3926
3927
0
  if (exit_addr) {
3928
0
    ir_GUARD(ir_EQ(phi_result, ir_CONST_LONG(guard)), ir_CONST_ADDR(exit_addr));
3929
0
  } else {
3930
0
    ZEND_ASSERT(jit->b >= 0);
3931
0
    bb = &jit->ssa->cfg.blocks[jit->b];
3932
    // JIT: if (result == ZEND_JMP_FL_HIT)
3933
0
    ref = jit_IF_ex(jit, ir_EQ(phi_result, ir_CONST_LONG(ZEND_JMP_FL_HIT)), bb->successors[0]);
3934
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
3935
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
3936
0
    jit->b = -1;
3937
0
  }
3938
3939
0
  return 1;
3940
0
}
3941
3942
static int zend_jit_cond_jmp(zend_jit_ctx *jit, const zend_op *next_opline, int target_block)
3943
0
{
3944
0
  ir_ref ref;
3945
0
  zend_basic_block *bb;
3946
3947
0
  ZEND_ASSERT(jit->b >= 0);
3948
0
  bb = &jit->ssa->cfg.blocks[jit->b];
3949
3950
0
  ZEND_ASSERT(bb->successors_count == 2);
3951
0
  if (bb->successors[0] == bb->successors[1]) {
3952
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
3953
0
    jit->b = -1;
3954
0
    zend_jit_set_last_valid_opline(jit, next_opline);
3955
0
    return 1;
3956
0
  }
3957
3958
0
  ref = jit_IF_ex(jit, jit_CMP_IP(jit, IR_NE, next_opline), target_block);
3959
3960
0
  _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
3961
0
  _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
3962
3963
0
  jit->b = -1;
3964
0
  zend_jit_set_last_valid_opline(jit, next_opline);
3965
3966
0
  return 1;
3967
0
}
3968
3969
static int zend_jit_set_cond(zend_jit_ctx *jit, const zend_op *next_opline, uint32_t var)
3970
0
{
3971
0
  ir_ref ref;
3972
3973
0
  ref = ir_ADD_U32(ir_ZEXT_U32(jit_CMP_IP(jit, IR_EQ, next_opline)), ir_CONST_U32(IS_FALSE));
3974
3975
  // EX_VAR(var) = ...
3976
0
  ir_STORE(ir_ADD_OFFSET(jit_FP(jit), var + offsetof(zval, u1.type_info)), ref);
3977
3978
0
  zend_jit_reset_last_valid_opline(jit);
3979
0
  return zend_jit_set_ip(jit, next_opline - 1);
3980
0
}
3981
3982
/* PHP JIT handlers */
3983
static void zend_jit_check_exception(zend_jit_ctx *jit)
3984
0
{
3985
0
  ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
3986
0
    jit_STUB_ADDR(jit, jit_stub_exception_handler));
3987
0
}
3988
3989
static void zend_jit_check_exception_undef_result(zend_jit_ctx *jit, const zend_op *opline)
3990
0
{
3991
0
  ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
3992
0
    jit_STUB_ADDR(jit,
3993
0
      (opline->result_type & (IS_TMP_VAR|IS_VAR)) ? jit_stub_exception_handler_undef : jit_stub_exception_handler));
3994
0
}
3995
3996
static void zend_jit_type_check_undef(zend_jit_ctx  *jit,
3997
                                      ir_ref         type,
3998
                                      uint32_t       var,
3999
                                      const zend_op *opline,
4000
                                      bool           check_exception,
4001
                                      bool           in_cold_path,
4002
                                      bool           undef_result)
4003
0
{
4004
0
  ir_ref if_def = ir_IF(type);
4005
4006
0
  if (!in_cold_path) {
4007
0
    ir_IF_FALSE_cold(if_def);
4008
0
  } else {
4009
0
    ir_IF_FALSE(if_def);
4010
0
  }
4011
0
  if (opline) {
4012
0
    jit_SET_EX_OPLINE(jit, opline);
4013
0
  }
4014
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(var));
4015
0
  if (check_exception) {
4016
0
    if (undef_result) {
4017
0
      zend_jit_check_exception_undef_result(jit, opline);
4018
0
    } else {
4019
0
      zend_jit_check_exception(jit);
4020
0
    }
4021
0
  }
4022
0
  ir_MERGE_WITH_EMPTY_TRUE(if_def);
4023
0
}
4024
4025
static ir_ref zend_jit_zval_check_undef(zend_jit_ctx  *jit,
4026
                                        ir_ref         ref,
4027
                                        uint32_t       var,
4028
                                        const zend_op *opline,
4029
                                        bool           check_exception)
4030
0
{
4031
0
  ir_ref if_def, ref2;
4032
4033
0
  if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
4034
0
  ir_IF_FALSE_cold(if_def);
4035
4036
0
  if (opline) {
4037
0
    jit_SET_EX_OPLINE(jit, opline);
4038
0
  }
4039
4040
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(var));
4041
4042
0
  if (check_exception) {
4043
0
    zend_jit_check_exception(jit);
4044
0
  }
4045
4046
0
  ref2 = jit_EG(uninitialized_zval);
4047
4048
0
  ir_MERGE_WITH_EMPTY_TRUE(if_def);
4049
4050
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
4051
0
}
4052
4053
static void zend_jit_recv_entry(zend_jit_ctx *jit, int b)
4054
0
{
4055
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[b];
4056
0
  int pred;
4057
0
  ir_ref ref;
4058
4059
0
  ZEND_ASSERT(bb->predecessors_count > 0);
4060
4061
0
  pred = jit->bb_predecessors[b];
4062
0
  ref = jit->bb_edges[pred];
4063
4064
0
  ZEND_ASSERT(ref);
4065
0
  ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_END);
4066
4067
  /* Insert a MERGE block with additional ENTRY input between predecessor and this one */
4068
0
  ir_ENTRY(ref, bb->start);
4069
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4070
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4071
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4072
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4073
0
    jit_STORE_FP(jit, 2);
4074
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4075
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4076
0
    jit_STORE_IP(jit, 3);
4077
0
  }
4078
4079
0
  ir_MERGE_WITH(ref);
4080
0
  jit->bb_edges[pred] = ir_END();
4081
0
}
4082
4083
static void zend_jit_osr_entry(zend_jit_ctx *jit, int b)
4084
0
{
4085
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[b];
4086
0
  ir_ref ref = ir_END();
4087
4088
  /* Insert a MERGE block with additional ENTRY input between predecessor and this one */
4089
0
  ir_ENTRY(ref, bb->start);
4090
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4091
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4092
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4093
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4094
0
    jit_STORE_FP(jit, 2);
4095
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4096
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4097
0
    jit_STORE_IP(jit, 3);
4098
0
  }
4099
4100
0
  ir_MERGE_WITH(ref);
4101
0
}
4102
4103
static ir_ref zend_jit_continue_entry(zend_jit_ctx *jit, ir_ref src, unsigned int label)
4104
0
{
4105
0
  ir_ENTRY(src, label);
4106
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4107
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4108
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4109
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4110
0
    jit_STORE_FP(jit, 2);
4111
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4112
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4113
0
    jit_STORE_IP(jit, 3);
4114
0
  }
4115
0
  return ir_END();
4116
0
}
4117
4118
static int zend_jit_handler(zend_jit_ctx *jit, const zend_op *opline, int may_throw)
4119
0
{
4120
0
  zend_jit_set_ip(jit, opline);
4121
0
  if (GCC_GLOBAL_REGS) {
4122
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4123
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
4124
0
  } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
4125
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4126
0
    ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
4127
0
    jit_STORE_IP(jit, ip);
4128
0
  } else {
4129
0
    zend_vm_opcode_handler_t handler = opline->handler;
4130
0
    ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
4131
0
    jit_STORE_IP(jit, ip);
4132
0
  }
4133
0
  if (may_throw) {
4134
0
    zend_jit_check_exception(jit);
4135
0
  }
4136
  /* Skip the following OP_DATA */
4137
0
  switch (opline->opcode) {
4138
0
    case ZEND_ASSIGN_DIM:
4139
0
    case ZEND_ASSIGN_OBJ:
4140
0
    case ZEND_ASSIGN_STATIC_PROP:
4141
0
    case ZEND_ASSIGN_DIM_OP:
4142
0
    case ZEND_ASSIGN_OBJ_OP:
4143
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
4144
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
4145
0
    case ZEND_ASSIGN_OBJ_REF:
4146
0
    case ZEND_FRAMELESS_ICALL_3:
4147
0
    case ZEND_DECLARE_ATTRIBUTED_CONST:
4148
0
      zend_jit_set_last_valid_opline(jit, opline + 2);
4149
0
      break;
4150
0
    default:
4151
0
      zend_jit_set_last_valid_opline(jit, opline + 1);
4152
0
      break;
4153
0
  }
4154
0
  return 1;
4155
0
}
4156
4157
static int zend_jit_tail_handler(zend_jit_ctx *jit, const zend_op *opline)
4158
0
{
4159
0
  ir_ref ref;
4160
0
  zend_basic_block *bb;
4161
4162
0
  zend_jit_set_ip(jit, opline);
4163
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
4164
0
    if (opline->opcode == ZEND_DO_UCALL ||
4165
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
4166
0
        opline->opcode == ZEND_DO_FCALL ||
4167
0
        opline->opcode == ZEND_RETURN) {
4168
4169
      /* Use inlined HYBRID VM handler */
4170
0
      zend_vm_opcode_handler_t handler = opline->handler;
4171
0
      ir_TAILCALL(IR_VOID, ir_CONST_FUNC(handler));
4172
0
    } else {
4173
0
      zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4174
0
      ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
4175
0
      ref = ir_LOAD_A(jit_IP(jit));
4176
0
      ir_TAILCALL(IR_VOID, ref);
4177
0
    }
4178
0
  } else {
4179
0
    zend_vm_opcode_handler_t handler = opline->handler;
4180
0
    if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
4181
0
      zend_jit_tailcall_handler(jit, ir_CONST_OPCODE_HANDLER_FUNC(handler));
4182
0
    } else if ((jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY)
4183
0
     && (opline->opcode == ZEND_CATCH
4184
0
      || opline->opcode == ZEND_FAST_CALL
4185
0
      || opline->opcode == ZEND_FAST_RET
4186
0
      || opline->opcode == ZEND_MATCH_ERROR
4187
0
      || opline->opcode == ZEND_THROW
4188
0
      || opline->opcode == ZEND_VERIFY_NEVER_TYPE)) {
4189
0
      ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
4190
0
      zend_jit_vm_enter(jit, ip);
4191
0
    } else {
4192
0
      ir_TAILCALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
4193
0
    }
4194
0
  }
4195
0
  if (jit->b >= 0) {
4196
0
    bb = &jit->ssa->cfg.blocks[jit->b];
4197
0
    if (bb->successors_count > 0
4198
0
     && (opline->opcode == ZEND_DO_FCALL
4199
0
      || opline->opcode == ZEND_DO_UCALL
4200
0
      || opline->opcode == ZEND_DO_FCALL_BY_NAME
4201
0
      || opline->opcode == ZEND_INCLUDE_OR_EVAL
4202
0
      || opline->opcode == ZEND_GENERATOR_CREATE
4203
0
      || opline->opcode == ZEND_YIELD
4204
0
      || opline->opcode == ZEND_YIELD_FROM
4205
0
      || opline->opcode == ZEND_FAST_CALL)) {
4206
      /* Add a fake control edge from UNREACHABLE to the following ENTRY */
4207
0
      int succ;
4208
4209
0
      if (bb->successors_count == 1) {
4210
0
        succ = bb->successors[0];
4211
0
        ZEND_ASSERT(jit->ssa->cfg.blocks[succ].flags & ZEND_BB_ENTRY);
4212
0
      } else {
4213
        /* Use only the following successor of FAST_CALL */
4214
0
        ZEND_ASSERT(opline->opcode == ZEND_FAST_CALL);
4215
0
        succ = jit->b + 1;
4216
        /* we need an entry */
4217
0
        jit->ssa->cfg.blocks[succ].flags |= ZEND_BB_ENTRY;
4218
0
      }
4219
0
      ref = jit->ctx.insns_count - 1;
4220
0
      ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_UNREACHABLE || jit->ctx.ir_base[ref].op == IR_RETURN);
4221
0
      ref = zend_jit_continue_entry(jit, ref, jit->ssa->cfg.blocks[succ].start);
4222
0
      _zend_jit_add_predecessor_ref(jit, succ, jit->b, ref);
4223
0
    }
4224
0
    jit->b = -1;
4225
0
    zend_jit_reset_last_valid_opline(jit);
4226
0
  }
4227
0
  return 1;
4228
0
}
4229
4230
static int zend_jit_call(zend_jit_ctx *jit, const zend_op *opline, unsigned int next_block)
4231
0
{
4232
0
  return zend_jit_tail_handler(jit, opline);
4233
0
}
4234
4235
static int zend_jit_spill_store(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info, bool set_type)
4236
0
{
4237
0
  ZEND_ASSERT(Z_MODE(src) == IS_REG);
4238
0
  ZEND_ASSERT(Z_MODE(dst) == IS_MEM_ZVAL);
4239
4240
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4241
0
    jit_set_Z_LVAL(jit, dst, zend_jit_use_reg(jit, src));
4242
0
    if (set_type &&
4243
0
        (Z_REG(dst) != ZREG_FP ||
4244
0
         !JIT_G(current_frame) ||
4245
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4246
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4247
0
    }
4248
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4249
0
    jit_set_Z_DVAL(jit, dst, zend_jit_use_reg(jit, src));
4250
0
    if (set_type &&
4251
0
        (Z_REG(dst) != ZREG_FP ||
4252
0
         !JIT_G(current_frame) ||
4253
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4254
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4255
0
    }
4256
0
  } else {
4257
0
    ZEND_UNREACHABLE();
4258
0
  }
4259
0
  return 1;
4260
0
}
4261
4262
static int zend_jit_spill_store_inv(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4263
0
{
4264
0
  ZEND_ASSERT(Z_MODE(src) == IS_REG);
4265
0
  ZEND_ASSERT(Z_MODE(dst) == IS_MEM_ZVAL);
4266
4267
0
  if (Z_LOAD(src) || Z_STORE(src)) {
4268
    /* it's not necessary to store register if it was previously loaded or already stored */
4269
0
    return 1;
4270
0
  }
4271
4272
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4273
0
    jit_set_Z_LVAL(jit, dst, zend_jit_use_reg(jit, src));
4274
0
    if (Z_REG(dst) != ZREG_FP || !JIT_G(current_frame)) {
4275
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4276
0
    } else if (STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG) {
4277
      /* invalidate memory type */
4278
0
      STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) = IS_UNKNOWN;
4279
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4280
0
    }
4281
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4282
0
    jit_set_Z_DVAL(jit, dst, zend_jit_use_reg(jit, src));
4283
0
    if (Z_REG(dst) != ZREG_FP || !JIT_G(current_frame)) {
4284
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4285
0
    } else if (STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE) {
4286
      /* invalidate memory type */
4287
0
      STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) = IS_UNKNOWN;
4288
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4289
0
    }
4290
0
  } else {
4291
0
    ZEND_UNREACHABLE();
4292
0
  }
4293
0
  return 1;
4294
0
}
4295
4296
static int zend_jit_load_reg(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4297
0
{
4298
0
  ZEND_ASSERT(Z_MODE(src) == IS_MEM_ZVAL);
4299
0
  ZEND_ASSERT(Z_MODE(dst) == IS_REG);
4300
4301
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4302
0
    zend_jit_def_reg(jit, dst, jit_Z_LVAL(jit, src));
4303
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4304
0
    zend_jit_def_reg(jit, dst, jit_Z_DVAL(jit, src));
4305
0
  } else {
4306
0
    ZEND_UNREACHABLE();
4307
0
  }
4308
0
  return 1;
4309
0
}
4310
4311
static int zend_jit_store_var(zend_jit_ctx *jit, uint32_t info, int var, int ssa_var, bool set_type)
4312
0
{
4313
0
  zend_jit_addr src = ZEND_ADDR_REG(ssa_var);
4314
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4315
4316
0
  return zend_jit_spill_store(jit, src, dst, info, set_type);
4317
0
}
4318
4319
static int zend_jit_store_ref(zend_jit_ctx *jit, uint32_t info, int var, int32_t src, bool set_type)
4320
0
{
4321
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4322
4323
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4324
0
    jit_set_Z_LVAL(jit, dst, src);
4325
0
    if (set_type &&
4326
0
        (Z_REG(dst) != ZREG_FP ||
4327
0
         !JIT_G(current_frame) ||
4328
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4329
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4330
0
    }
4331
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4332
0
    jit_set_Z_DVAL(jit, dst, src);
4333
0
    if (set_type &&
4334
0
        (Z_REG(dst) != ZREG_FP ||
4335
0
         !JIT_G(current_frame) ||
4336
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4337
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4338
0
    }
4339
0
  } else {
4340
0
    ZEND_UNREACHABLE();
4341
0
  }
4342
0
  return 1;
4343
0
}
4344
4345
static ir_ref zend_jit_deopt_rload(zend_jit_ctx *jit, ir_type type, int32_t reg)
4346
0
{
4347
0
  ir_ref ref = jit->ctx.control;
4348
0
  ir_insn *insn;
4349
4350
0
  while (1) {
4351
0
    insn = &jit->ctx.ir_base[ref];
4352
0
    if (insn->op == IR_RLOAD && insn->op2 == reg) {
4353
0
      ZEND_ASSERT(insn->type == type);
4354
0
      return ref;
4355
0
    } else if (insn->op == IR_START) {
4356
0
      break;
4357
0
    }
4358
0
    ref = insn->op1;
4359
0
  }
4360
0
  return ir_RLOAD(type, reg);
4361
0
}
4362
4363
/* Same as zend_jit_deopt_rload(), but 'reg' may be spilled on C stack */
4364
static ir_ref zend_jit_deopt_rload_spilled(zend_jit_ctx *jit, ir_type type, int8_t reg, int32_t offset)
4365
0
{
4366
0
  ZEND_ASSERT(reg >= 0);
4367
4368
0
  if (IR_REG_SPILLED(reg)) {
4369
0
    return ir_LOAD(type, ir_ADD_OFFSET(zend_jit_deopt_rload(jit, type, IR_REG_NUM(reg)), offset));
4370
0
  } else {
4371
0
    return zend_jit_deopt_rload(jit, type, reg);
4372
0
  }
4373
0
}
4374
4375
static int zend_jit_store_const_long(zend_jit_ctx *jit, int var, zend_long val)
4376
0
{
4377
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4378
0
  ir_ref src = ir_CONST_LONG(val);
4379
4380
0
  if (jit->ra && jit->ra[var].ref == IR_NULL) {
4381
0
    zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4382
0
  }
4383
0
  jit_set_Z_LVAL(jit, dst, src);
4384
0
  jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4385
0
  return 1;
4386
0
}
4387
4388
static int zend_jit_store_const_double(zend_jit_ctx *jit, int var, double val)
4389
0
{
4390
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4391
0
  ir_ref src = ir_CONST_DOUBLE(val);
4392
4393
0
  if (jit->ra && jit->ra[var].ref == IR_NULL) {
4394
0
    zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4395
0
  }
4396
0
  jit_set_Z_DVAL(jit, dst, src);
4397
0
  jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4398
0
  return 1;
4399
0
}
4400
4401
static int zend_jit_store_type(zend_jit_ctx *jit, int var, uint8_t type)
4402
0
{
4403
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4404
4405
0
  ZEND_ASSERT(type <= IS_DOUBLE);
4406
0
  jit_set_Z_TYPE_INFO(jit, dst, type);
4407
0
  return 1;
4408
0
}
4409
4410
static int zend_jit_store_reg(zend_jit_ctx *jit, uint32_t info, int var, int8_t reg, bool in_mem, bool set_type)
4411
0
{
4412
0
  zend_jit_addr src;
4413
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4414
0
  ir_type type;
4415
4416
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4417
0
    type = IR_LONG;
4418
0
    src = zend_jit_deopt_rload(jit, type, reg);
4419
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4420
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4421
0
    } else if (!in_mem) {
4422
0
      jit_set_Z_LVAL(jit, dst, src);
4423
0
      if (set_type &&
4424
0
          (Z_REG(dst) != ZREG_FP ||
4425
0
           !JIT_G(current_frame) ||
4426
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4427
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4428
0
      }
4429
0
    }
4430
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4431
0
    type = IR_DOUBLE;
4432
0
    src = zend_jit_deopt_rload(jit, type, reg);
4433
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4434
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4435
0
    } else if (!in_mem) {
4436
0
      jit_set_Z_DVAL(jit, dst, src);
4437
0
      if (set_type &&
4438
0
          (Z_REG(dst) != ZREG_FP ||
4439
0
           !JIT_G(current_frame) ||
4440
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4441
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4442
0
      }
4443
0
    }
4444
0
  } else {
4445
0
    ZEND_UNREACHABLE();
4446
0
  }
4447
0
  return 1;
4448
0
}
4449
4450
static int zend_jit_store_spill_slot(zend_jit_ctx *jit, uint32_t info, int var, int8_t reg, int32_t offset, bool set_type)
4451
0
{
4452
0
  zend_jit_addr src;
4453
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4454
4455
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4456
0
    src = ir_LOAD_L(ir_ADD_OFFSET(ir_RLOAD_A(reg), offset));
4457
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4458
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4459
0
    } else {
4460
0
      jit_set_Z_LVAL(jit, dst, src);
4461
0
      if (set_type &&
4462
0
          (Z_REG(dst) != ZREG_FP ||
4463
0
           !JIT_G(current_frame) ||
4464
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4465
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4466
0
      }
4467
0
    }
4468
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4469
0
    src = ir_LOAD_D(ir_ADD_OFFSET(ir_RLOAD_A(reg), offset));
4470
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4471
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4472
0
    } else {
4473
0
      jit_set_Z_DVAL(jit, dst, src);
4474
0
      if (set_type &&
4475
0
          (Z_REG(dst) != ZREG_FP ||
4476
0
           !JIT_G(current_frame) ||
4477
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4478
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4479
0
      }
4480
0
    }
4481
0
  } else {
4482
0
    ZEND_UNREACHABLE();
4483
0
  }
4484
0
  return 1;
4485
0
}
4486
4487
static int zend_jit_store_var_type(zend_jit_ctx *jit, int var, uint32_t type)
4488
0
{
4489
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4490
4491
0
  jit_set_Z_TYPE_INFO(jit, dst, type);
4492
0
  return 1;
4493
0
}
4494
4495
static int zend_jit_zval_try_addref(zend_jit_ctx *jit, zend_jit_addr var_addr)
4496
0
{
4497
0
  ir_ref if_refcounted, end1;
4498
4499
0
  if_refcounted = jit_if_REFCOUNTED(jit, var_addr);
4500
0
  ir_IF_FALSE(if_refcounted);
4501
0
  end1 = ir_END();
4502
0
  ir_IF_TRUE(if_refcounted);
4503
0
  jit_GC_ADDREF(jit, jit_Z_PTR(jit, var_addr));
4504
0
  ir_MERGE_WITH(end1);
4505
0
  return 1;
4506
0
}
4507
4508
static int zend_jit_store_var_if_necessary(zend_jit_ctx *jit, int var, zend_jit_addr src, uint32_t info)
4509
0
{
4510
0
  if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
4511
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4512
0
    return zend_jit_spill_store(jit, src, dst, info, true);
4513
0
  }
4514
0
  return 1;
4515
0
}
4516
4517
static int zend_jit_store_var_if_necessary_ex(zend_jit_ctx *jit, int var, zend_jit_addr src, uint32_t info, zend_jit_addr old, uint32_t old_info)
4518
0
{
4519
0
  if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
4520
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4521
0
    bool set_type = true;
4522
4523
0
    if ((info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) ==
4524
0
        (old_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF))) {
4525
0
      if (Z_MODE(old) != IS_REG || Z_LOAD(old) || Z_STORE(old)) {
4526
0
        if (JIT_G(current_frame)) {
4527
0
          uint32_t mem_type = STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var));
4528
4529
0
          if (mem_type != IS_UNKNOWN
4530
0
           && (info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == (1 << mem_type)) {
4531
0
            set_type = false;
4532
0
          }
4533
0
        } else {
4534
0
          set_type = false;
4535
0
        }
4536
0
      }
4537
0
    }
4538
0
    return zend_jit_spill_store(jit, src, dst, info, set_type);
4539
0
  }
4540
0
  return 1;
4541
0
}
4542
4543
static int zend_jit_load_var(zend_jit_ctx *jit, uint32_t info, int var, int ssa_var)
4544
0
{
4545
0
  zend_jit_addr src = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4546
0
  zend_jit_addr dst = ZEND_ADDR_REG(ssa_var);
4547
4548
0
  return zend_jit_load_reg(jit, src, dst, info);
4549
0
}
4550
4551
static int zend_jit_invalidate_var_if_necessary(zend_jit_ctx *jit, uint8_t op_type, zend_jit_addr addr, znode_op op)
4552
0
{
4553
0
  if ((op_type & (IS_TMP_VAR|IS_VAR)) && Z_MODE(addr) == IS_REG && !Z_LOAD(addr) && !Z_STORE(addr)) {
4554
    /* Invalidate operand type to prevent incorrect destuction by exception_handler_free_op1_op2() */
4555
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op.var);
4556
0
    jit_set_Z_TYPE_INFO(jit, dst, IS_UNDEF);
4557
0
  }
4558
0
  return 1;
4559
0
}
4560
4561
static int zend_jit_update_regs(zend_jit_ctx *jit, uint32_t var, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4562
0
{
4563
0
  if (!zend_jit_same_addr(src, dst)) {
4564
0
    if (Z_MODE(src) == IS_REG) {
4565
0
      if (Z_MODE(dst) == IS_REG) {
4566
0
        zend_jit_def_reg(jit, dst, zend_jit_use_reg(jit, src));
4567
0
        if (!Z_LOAD(src) && !Z_STORE(src) && Z_STORE(dst)) {
4568
0
          zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4569
4570
0
          if (!zend_jit_spill_store(jit, dst, var_addr, info,
4571
0
              JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4572
0
              JIT_G(current_frame) == NULL ||
4573
0
              STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4574
0
              (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4575
0
          )) {
4576
0
            return 0;
4577
0
          }
4578
0
        }
4579
0
      } else if (Z_MODE(dst) == IS_MEM_ZVAL) {
4580
0
        if (!Z_LOAD(src) && !Z_STORE(src)) {
4581
0
          if (!zend_jit_spill_store(jit, src, dst, info,
4582
0
              JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4583
0
              JIT_G(current_frame) == NULL ||
4584
0
              STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4585
0
              (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4586
0
          )) {
4587
0
            return 0;
4588
0
          }
4589
0
        }
4590
0
      } else {
4591
0
        ZEND_UNREACHABLE();
4592
0
      }
4593
0
    } else if (Z_MODE(src) == IS_MEM_ZVAL) {
4594
0
      if (Z_MODE(dst) == IS_REG) {
4595
0
        if (!zend_jit_load_reg(jit, src, dst, info)) {
4596
0
          return 0;
4597
0
        }
4598
0
      } else {
4599
0
        ZEND_UNREACHABLE();
4600
0
      }
4601
0
    } else {
4602
0
      ZEND_UNREACHABLE();
4603
0
    }
4604
0
  } else if (Z_MODE(dst) == IS_REG && Z_STORE(dst)) {
4605
0
    dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4606
0
    if (!zend_jit_spill_store(jit, src, dst, info,
4607
0
        JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4608
0
        JIT_G(current_frame) == NULL ||
4609
0
        STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4610
0
        (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4611
0
    )) {
4612
0
      return 0;
4613
0
    }
4614
0
  }
4615
0
  return 1;
4616
0
}
4617
4618
struct jit_observer_fcall_is_unobserved_data {
4619
  ir_ref if_unobserved;
4620
  ir_ref ir_end_inputs;
4621
};
4622
4623
0
static struct jit_observer_fcall_is_unobserved_data jit_observer_fcall_is_unobserved_start(zend_jit_ctx *jit, const zend_function *func, ir_ref *observer_handler, ir_ref rx, ir_ref func_ref) {
4624
0
  ir_ref run_time_cache;
4625
0
  struct jit_observer_fcall_is_unobserved_data data = { .ir_end_inputs = IR_UNUSED };
4626
0
  if (func) {
4627
0
    ZEND_ASSERT((func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0);
4628
0
  } else {
4629
    // JIT: if (function->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) {
4630
0
    ZEND_ASSERT(rx != IR_UNUSED);
4631
0
    ir_ref if_trampoline_or_generator = ir_IF(ir_AND_U32(
4632
0
      ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
4633
0
      ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)));
4634
0
    ir_IF_TRUE(if_trampoline_or_generator);
4635
0
    ir_END_list(data.ir_end_inputs);
4636
0
    ir_IF_FALSE(if_trampoline_or_generator);
4637
0
  }
4638
0
  if (func && (func->common.fn_flags & ZEND_ACC_CLOSURE) == 0 && ZEND_MAP_PTR_IS_OFFSET(func->common.run_time_cache)) {
4639
    // JIT: ZEND_MAP_PTR_GET_IMM(func->common.runtime_cache)
4640
0
    run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_CG(map_ptr_base)), (uintptr_t)ZEND_MAP_PTR(func->common.run_time_cache)));
4641
0
#ifndef ZTS
4642
0
  } else if (func && rx == IS_UNUSED) { // happens for internal functions only
4643
0
    ZEND_ASSERT(!ZEND_USER_CODE(func->type));
4644
0
    run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_CONST_ADDR(func), offsetof(zend_op_array, run_time_cache__ptr)));
4645
0
#endif
4646
0
  } else {
4647
    // Closures may be duplicated and have a different runtime cache. Use the regular run_time_cache access pattern for these
4648
0
    if (func && ZEND_USER_CODE(func->type)) { // not a closure and definitely not an internal function
4649
0
      run_time_cache = ir_LOAD_A(jit_CALL(rx, run_time_cache));
4650
0
    } else {
4651
      // JIT: ZEND_MAP_PTR_GET(func->common.runtime_cache)
4652
0
      run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_CALL(rx, func)), offsetof(zend_op_array, run_time_cache__ptr)));
4653
0
      ir_ref if_odd = ir_IF(ir_AND_A(run_time_cache, ir_CONST_ADDR(1)));
4654
0
      ir_IF_TRUE(if_odd);
4655
4656
0
      ir_ref run_time_cache2 = ir_LOAD_A(ir_ADD_A(run_time_cache, ir_LOAD_A(jit_CG(map_ptr_base))));
4657
4658
0
      ir_ref if_odd_end = ir_END();
4659
0
      ir_IF_FALSE(if_odd);
4660
4661
      // JIT: if (func->common.runtime_cache != NULL) {
4662
0
      ir_ref if_rt_cache = ir_IF(ir_EQ(run_time_cache, IR_NULL));
4663
0
      ir_IF_TRUE(if_rt_cache);
4664
0
      ir_END_list(data.ir_end_inputs);
4665
0
      ir_IF_FALSE(if_rt_cache);
4666
4667
0
      ir_MERGE_WITH(if_odd_end);
4668
0
      run_time_cache = ir_PHI_2(IR_ADDR, run_time_cache, run_time_cache2);
4669
0
    }
4670
0
  }
4671
  // JIT: observer_handler = runtime_cache + ZEND_OBSERVER_HANDLE(function)
4672
0
  if (func) {
4673
0
    *observer_handler = ir_ADD_OFFSET(run_time_cache, ZEND_OBSERVER_HANDLE(func) * sizeof(void *));
4674
0
  } else {
4675
    // JIT: (func->type == ZEND_INTERNAL_FUNCTION ? zend_observer_fcall_internal_function_extension : zend_observer_fcall_op_array_extension) * sizeof(void *)
4676
0
    ir_ref tmp = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_function, type)));
4677
0
    ir_ref if_internal_func = ir_IF(ir_AND_U8(tmp, ir_CONST_U8(ZEND_INTERNAL_FUNCTION)));
4678
0
    ir_IF_TRUE(if_internal_func);
4679
4680
0
    ir_ref observer_handler_internal = ir_ADD_OFFSET(run_time_cache, zend_observer_fcall_internal_function_extension * sizeof(void *));
4681
4682
0
    ir_ref if_internal_func_end = ir_END();
4683
0
    ir_IF_FALSE(if_internal_func);
4684
4685
0
    ir_ref observer_handler_user = ir_ADD_OFFSET(run_time_cache, zend_observer_fcall_op_array_extension * sizeof(void *));
4686
4687
0
    ir_MERGE_WITH(if_internal_func_end);
4688
0
    *observer_handler = ir_PHI_2(IR_ADDR, observer_handler_internal, observer_handler_user);
4689
0
  }
4690
4691
  // JIT: if (*observer_handler == ZEND_OBSERVER_NONE_OBSERVED) {
4692
0
  data.if_unobserved = ir_IF(ir_EQ(ir_LOAD_A(*observer_handler), ir_CONST_ADDR(ZEND_OBSERVER_NONE_OBSERVED)));
4693
0
  ir_IF_FALSE(data.if_unobserved);
4694
0
  return data;
4695
0
}
4696
4697
/* For frameless the true branch of if_unobserved is used and this function not called. */
4698
0
static void jit_observer_fcall_is_unobserved_end(zend_jit_ctx *jit, struct jit_observer_fcall_is_unobserved_data *data) {
4699
0
  ir_END_list(data->ir_end_inputs);
4700
0
  ir_IF_TRUE(data->if_unobserved);
4701
0
  ir_END_list(data->ir_end_inputs);
4702
0
  ir_MERGE_list(data->ir_end_inputs);
4703
0
}
4704
4705
0
static void jit_observer_fcall_begin(zend_jit_ctx *jit, ir_ref rx, ir_ref observer_handler) {
4706
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_observer_fcall_begin_prechecked), rx, observer_handler);
4707
0
}
4708
4709
0
static void jit_observer_fcall_end(zend_jit_ctx *jit, ir_ref rx, ir_ref res_ref) {
4710
  // JIT: if (execute_data == EG(current_observed_frame)) {
4711
0
  ir_ref has_end_observer = ir_IF(ir_EQ(rx, ir_LOAD_A(jit_EG(current_observed_frame))));
4712
0
  ir_IF_TRUE(has_end_observer);
4713
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_observer_fcall_end_prechecked),
4714
0
    rx, res_ref);
4715
0
  ir_MERGE_WITH_EMPTY_FALSE(has_end_observer);
4716
0
}
4717
4718
static int zend_jit_inc_dec(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, uint32_t op1_def_info, zend_jit_addr op1_def_addr, uint32_t res_use_info, uint32_t res_info, zend_jit_addr res_addr, int may_overflow, int may_throw)
4719
0
{
4720
0
  ir_ref if_long = IR_UNUSED;
4721
0
  ir_ref op1_lval_ref = IR_UNUSED;
4722
0
  ir_ref ref;
4723
0
  ir_op op;
4724
4725
0
  if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)-MAY_BE_LONG)) {
4726
0
    if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
4727
0
    ir_IF_TRUE(if_long);
4728
0
  }
4729
0
  if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
4730
0
    op1_lval_ref = jit_Z_LVAL(jit, op1_addr);
4731
0
    jit_set_Z_LVAL(jit, res_addr, op1_lval_ref);
4732
0
    if (Z_MODE(res_addr) != IS_REG) {
4733
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4734
0
    }
4735
0
  }
4736
0
  if (Z_MODE(op1_def_addr) == IS_MEM_ZVAL
4737
0
   && Z_MODE(op1_addr) == IS_REG
4738
0
   && !Z_LOAD(op1_addr)
4739
0
   && !Z_STORE(op1_addr)) {
4740
0
    jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_LONG);
4741
0
  }
4742
0
  if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4743
0
    op = may_overflow ? IR_ADD_OV : IR_ADD;
4744
0
  } else {
4745
0
    op = may_overflow ? IR_SUB_OV : IR_SUB;
4746
0
  }
4747
0
  if (!op1_lval_ref) {
4748
0
    op1_lval_ref = jit_Z_LVAL(jit, op1_addr);
4749
0
  }
4750
0
  ref = ir_BINARY_OP_L(op, op1_lval_ref, ir_CONST_LONG(1));
4751
0
  if (op1_def_info & MAY_BE_LONG) {
4752
0
    jit_set_Z_LVAL(jit, op1_def_addr, ref);
4753
0
  }
4754
0
  if (may_overflow &&
4755
0
      (((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)) ||
4756
0
       ((opline->result_type != IS_UNUSED && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD))))) {
4757
0
    int32_t exit_point;
4758
0
    const void *exit_addr;
4759
0
    zend_jit_trace_stack *stack;
4760
0
    uint32_t old_op1_info, old_res_info = 0;
4761
4762
0
    stack = JIT_G(current_frame)->stack;
4763
0
    old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
4764
0
    SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_DOUBLE, 0);
4765
0
    if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4766
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4767
0
    } else {
4768
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4769
0
    }
4770
0
    if (opline->result_type != IS_UNUSED) {
4771
0
      old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
4772
0
      if (opline->opcode == ZEND_PRE_INC) {
4773
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
4774
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4775
0
      } else if (opline->opcode == ZEND_PRE_DEC) {
4776
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
4777
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4778
0
      } else if (opline->opcode == ZEND_POST_INC) {
4779
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4780
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_LONG(ZEND_LONG_MAX));
4781
0
      } else if (opline->opcode == ZEND_POST_DEC) {
4782
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4783
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_LONG(ZEND_LONG_MIN));
4784
0
      }
4785
0
    }
4786
4787
0
    exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
4788
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4789
0
    ir_GUARD_NOT(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
4790
4791
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4792
0
        opline->result_type != IS_UNUSED) {
4793
0
      jit_set_Z_LVAL(jit, res_addr, ref);
4794
0
      if (Z_MODE(res_addr) != IS_REG) {
4795
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4796
0
      }
4797
0
    }
4798
4799
0
    SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
4800
0
    if (opline->result_type != IS_UNUSED) {
4801
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
4802
0
    }
4803
0
  } else if (may_overflow) {
4804
0
    ir_ref if_overflow;
4805
0
    ir_ref merge_inputs = IR_UNUSED;
4806
4807
0
    if (((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_DOUBLE|MAY_BE_GUARD))
4808
0
     || (opline->result_type != IS_UNUSED && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) ==  (MAY_BE_DOUBLE|MAY_BE_GUARD))) {
4809
0
      int32_t exit_point;
4810
0
      const void *exit_addr;
4811
0
      zend_jit_trace_stack *stack;
4812
0
      uint32_t old_res_info = 0, old_op1_info = 0;
4813
4814
0
      stack = JIT_G(current_frame)->stack;
4815
0
      if (opline->result_type != IS_UNUSED) {
4816
0
        old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
4817
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4818
0
        if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) {
4819
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ref);
4820
0
        } else {
4821
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), op1_lval_ref);
4822
0
        }
4823
0
      }
4824
0
      old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
4825
0
      SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_LONG, 0);
4826
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ref);
4827
4828
0
      exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
4829
0
      exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4830
0
      ir_GUARD(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
4831
4832
0
      if (opline->result_type != IS_UNUSED) {
4833
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
4834
0
      }
4835
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
4836
0
    } else {
4837
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
4838
0
      ir_IF_FALSE(if_overflow);
4839
0
      if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4840
0
          opline->result_type != IS_UNUSED) {
4841
0
        jit_set_Z_LVAL(jit, res_addr, ref);
4842
0
        if (Z_MODE(res_addr) != IS_REG) {
4843
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4844
0
        }
4845
0
      }
4846
0
      ir_END_list(merge_inputs);
4847
4848
      /* overflow => cold path */
4849
0
      ir_IF_TRUE_cold(if_overflow);
4850
0
    }
4851
4852
0
    if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4853
0
      if (Z_MODE(op1_def_addr) == IS_REG) {
4854
0
        jit_set_Z_DVAL(jit, op1_def_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4855
0
      } else {
4856
#if SIZEOF_ZEND_LONG == 4
4857
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0));
4858
        jit_set_Z_W2(jit, op1_def_addr, ir_CONST_U32(0x41e00000));
4859
#else
4860
0
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0x43e0000000000000));
4861
0
#endif
4862
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_DOUBLE);
4863
0
      }
4864
0
    } else {
4865
0
      if (Z_MODE(op1_def_addr) == IS_REG) {
4866
0
        jit_set_Z_DVAL(jit, op1_def_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4867
0
      } else {
4868
#if SIZEOF_ZEND_LONG == 4
4869
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0x00200000));
4870
        jit_set_Z_W2(jit, op1_def_addr, ir_CONST_U32(0xc1e00000));
4871
#else
4872
0
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0xc3e0000000000000));
4873
0
#endif
4874
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_DOUBLE);
4875
0
      }
4876
0
    }
4877
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4878
0
        opline->result_type != IS_UNUSED) {
4879
0
      if (opline->opcode == ZEND_PRE_INC) {
4880
0
        if (Z_MODE(res_addr) == IS_REG) {
4881
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4882
0
        } else {
4883
#if SIZEOF_ZEND_LONG == 4
4884
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
4885
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
4886
#else
4887
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
4888
0
#endif
4889
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
4890
0
        }
4891
0
      } else {
4892
0
        if (Z_MODE(res_addr) == IS_REG) {
4893
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4894
0
        } else {
4895
#if SIZEOF_ZEND_LONG == 4
4896
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
4897
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
4898
#else
4899
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
4900
0
#endif
4901
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
4902
0
        }
4903
0
      }
4904
0
    }
4905
4906
0
    if (merge_inputs) {
4907
0
      ir_END_list(merge_inputs);
4908
0
      ir_MERGE_list(merge_inputs);
4909
0
    }
4910
0
  } else {
4911
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4912
0
        opline->result_type != IS_UNUSED) {
4913
0
      jit_set_Z_LVAL(jit, res_addr, ref);
4914
0
      if (Z_MODE(res_addr) != IS_REG) {
4915
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4916
0
      }
4917
0
    }
4918
0
  }
4919
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
4920
0
    ir_ref merge_inputs = ir_END();
4921
4922
    /* !is_long => cold path */
4923
0
    ir_IF_FALSE_cold(if_long);
4924
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
4925
0
      jit_SET_EX_OPLINE(jit, opline);
4926
0
      if (op1_info & MAY_BE_UNDEF) {
4927
0
        ir_ref if_def;
4928
4929
0
        if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
4930
0
        ir_IF_FALSE_cold(if_def);
4931
4932
        // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
4933
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
4934
4935
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_NULL);
4936
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def);
4937
4938
0
        op1_info |= MAY_BE_NULL;
4939
0
      }
4940
4941
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
4942
4943
0
      if (op1_info & MAY_BE_REF) {
4944
0
        ir_ref if_ref, if_typed, func, ref2, arg2;
4945
4946
0
        if_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
4947
0
        ir_IF_TRUE(if_ref);
4948
0
        ref2 = jit_Z_PTR_ref(jit, ref);
4949
4950
0
        if_typed = jit_if_TYPED_REF(jit, ref2);
4951
0
        ir_IF_TRUE(if_typed);
4952
4953
0
        if (RETURN_VALUE_USED(opline)) {
4954
0
          ZEND_ASSERT(Z_MODE(res_addr) != IS_REG);
4955
0
          arg2 = jit_ZVAL_ADDR(jit, res_addr);
4956
0
        } else {
4957
0
          arg2 = IR_NULL;
4958
0
        }
4959
0
        if (opline->opcode == ZEND_PRE_INC) {
4960
0
          func = ir_CONST_FC_FUNC(zend_jit_pre_inc_typed_ref);
4961
0
        } else if (opline->opcode == ZEND_PRE_DEC) {
4962
0
          func = ir_CONST_FC_FUNC(zend_jit_pre_dec_typed_ref);
4963
0
        } else if (opline->opcode == ZEND_POST_INC) {
4964
0
          func = ir_CONST_FC_FUNC(zend_jit_post_inc_typed_ref);
4965
0
        } else if (opline->opcode == ZEND_POST_DEC) {
4966
0
          func = ir_CONST_FC_FUNC(zend_jit_post_dec_typed_ref);
4967
0
        } else {
4968
0
          ZEND_UNREACHABLE();
4969
0
        }
4970
4971
0
        ir_CALL_2(IR_VOID, func, ref2, arg2);
4972
0
        zend_jit_check_exception(jit);
4973
0
        ir_END_list(merge_inputs);
4974
4975
0
        ir_IF_FALSE(if_typed);
4976
0
        ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
4977
0
        ir_MERGE_WITH_EMPTY_FALSE(if_ref);
4978
0
        ref = ir_PHI_2(IR_ADDR, ref2, ref);
4979
0
      }
4980
4981
0
      if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
4982
0
        jit_ZVAL_COPY(jit,
4983
0
          res_addr,
4984
0
          res_use_info,
4985
0
          ZEND_ADDR_REF_ZVAL(ref), op1_info, true);
4986
0
      }
4987
0
      if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4988
0
        if (opline->opcode == ZEND_PRE_INC && opline->result_type != IS_UNUSED) {
4989
0
          ir_ref arg2 = jit_ZVAL_ADDR(jit, res_addr);
4990
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_inc), ref, arg2);
4991
0
        } else {
4992
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(increment_function), ref);
4993
0
        }
4994
0
      } else {
4995
0
        if (opline->opcode == ZEND_PRE_DEC && opline->result_type != IS_UNUSED) {
4996
0
          ir_ref arg2 = jit_ZVAL_ADDR(jit, res_addr);
4997
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_dec), ref, arg2);
4998
0
        } else {
4999
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(decrement_function), ref);
5000
0
        }
5001
0
      }
5002
0
      if (may_throw) {
5003
0
        zend_jit_check_exception(jit);
5004
0
      }
5005
0
    } else {
5006
0
      ref = jit_Z_DVAL(jit, op1_addr);
5007
0
      if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
5008
0
        jit_set_Z_DVAL(jit, res_addr, ref);
5009
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5010
0
      }
5011
0
      if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
5012
0
        op = IR_ADD;
5013
0
      } else {
5014
0
        op = IR_SUB;
5015
0
      }
5016
0
      ref = ir_BINARY_OP_D(op, ref, ir_CONST_DOUBLE(1.0));
5017
0
      jit_set_Z_DVAL(jit, op1_def_addr, ref);
5018
0
      if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
5019
0
          opline->result_type != IS_UNUSED) {
5020
0
        jit_set_Z_DVAL(jit, res_addr, ref);
5021
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5022
0
      }
5023
0
    }
5024
0
    ir_END_list(merge_inputs);
5025
0
    ir_MERGE_list(merge_inputs);
5026
0
  }
5027
0
  if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_def_addr, op1_def_info, op1_addr, op1_info)) {
5028
0
    return 0;
5029
0
  }
5030
0
  if (opline->result_type != IS_UNUSED) {
5031
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
5032
0
      return 0;
5033
0
    }
5034
0
  }
5035
0
  return 1;
5036
0
}
5037
5038
static int zend_jit_math_long_long(zend_jit_ctx   *jit,
5039
                                   const zend_op  *opline,
5040
                                   uint8_t         opcode,
5041
                                   zend_jit_addr   op1_addr,
5042
                                   zend_jit_addr   op2_addr,
5043
                                   zend_jit_addr   res_addr,
5044
                                   uint32_t        res_info,
5045
                                   uint32_t        res_use_info,
5046
                                   int             may_overflow)
5047
0
{
5048
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5049
0
  ir_op op;
5050
0
  ir_ref op1, op2, ref, if_overflow = IR_UNUSED;
5051
5052
0
  if (opcode == ZEND_ADD) {
5053
0
    op = may_overflow ? IR_ADD_OV : IR_ADD;
5054
0
  } else if (opcode == ZEND_SUB) {
5055
0
    op = may_overflow ? IR_SUB_OV : IR_SUB;
5056
0
  } else if (opcode == ZEND_MUL) {
5057
0
    op = may_overflow ? IR_MUL_OV : IR_MUL;
5058
0
  } else {
5059
0
    ZEND_UNREACHABLE();
5060
0
  }
5061
0
  op1 = jit_Z_LVAL(jit, op1_addr);
5062
0
  op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5063
0
  ref = ir_BINARY_OP_L(op, op1, op2);
5064
5065
0
  if (may_overflow) {
5066
0
    if (res_info & MAY_BE_GUARD) {
5067
0
      if ((res_info & MAY_BE_ANY) == MAY_BE_LONG) {
5068
0
        zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
5069
0
        uint32_t old_res_info;
5070
0
        int32_t exit_point;
5071
0
        const void *exit_addr;
5072
5073
0
        if (opline->opcode == ZEND_ADD
5074
0
         && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5075
0
          old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5076
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
5077
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
5078
0
          exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
5079
0
          SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
5080
0
        } else if (opline->opcode == ZEND_SUB
5081
0
         && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5082
0
          old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5083
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
5084
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
5085
0
          exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
5086
0
          SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
5087
0
        } else {
5088
0
          exit_point = zend_jit_trace_get_exit_point(opline, 0);
5089
0
        }
5090
5091
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5092
0
        if (!exit_addr) {
5093
0
          return 0;
5094
0
        }
5095
0
        ir_GUARD_NOT(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
5096
0
        may_overflow = 0;
5097
0
      } else if ((res_info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
5098
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
5099
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5100
5101
0
        if (!exit_addr) {
5102
0
          return 0;
5103
0
        }
5104
0
        ir_GUARD(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
5105
0
      } else {
5106
0
        ZEND_UNREACHABLE();
5107
0
      }
5108
0
    } else {
5109
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
5110
0
      ir_IF_FALSE(if_overflow);
5111
0
    }
5112
0
  }
5113
5114
0
  if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5115
0
    jit_set_Z_LVAL(jit, res_addr, ref);
5116
5117
0
    if (Z_MODE(res_addr) != IS_REG) {
5118
0
      if (!zend_jit_same_addr(op1_addr, res_addr)) {
5119
0
        if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_LONG) {
5120
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
5121
0
        }
5122
0
      }
5123
0
    }
5124
0
  }
5125
5126
0
  if (may_overflow) {
5127
0
    ir_ref fast_path = IR_UNUSED;
5128
5129
0
    if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5130
0
      fast_path = ir_END();
5131
0
      ir_IF_TRUE_cold(if_overflow);
5132
0
    }
5133
0
    if (opcode == ZEND_ADD) {
5134
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5135
0
        if (Z_MODE(res_addr) == IS_REG) {
5136
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
5137
0
        } else {
5138
#if SIZEOF_ZEND_LONG == 4
5139
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
5140
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
5141
#else
5142
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
5143
0
#endif
5144
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5145
0
        }
5146
0
        if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5147
0
          ir_MERGE_WITH(fast_path);
5148
0
        }
5149
0
        return 1;
5150
0
      }
5151
0
      op = IR_ADD;
5152
0
    } else if (opcode == ZEND_SUB) {
5153
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5154
0
        if (Z_MODE(res_addr) == IS_REG) {
5155
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
5156
0
        } else {
5157
#if SIZEOF_ZEND_LONG == 4
5158
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
5159
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
5160
#else
5161
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
5162
0
#endif
5163
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5164
0
        }
5165
0
        if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5166
0
          ir_MERGE_WITH(fast_path);
5167
0
        }
5168
0
        return 1;
5169
0
      }
5170
0
      op = IR_SUB;
5171
0
    } else if (opcode == ZEND_MUL) {
5172
0
      op = IR_MUL;
5173
0
    } else {
5174
0
      ZEND_UNREACHABLE();
5175
0
    }
5176
0
#if 1
5177
    /* reload */
5178
0
    op1 = jit_Z_LVAL(jit, op1_addr);
5179
0
    op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5180
0
#endif
5181
0
#if 1
5182
    /* disable CSE */
5183
0
    ir_ref old_cse_limit = jit->ctx.fold_cse_limit;
5184
0
    jit->ctx.fold_cse_limit = 0x7fffffff;
5185
0
#endif
5186
0
    op1 = ir_INT2D(op1);
5187
0
    op2 = ir_INT2D(op2);
5188
0
#if 1
5189
0
    jit->ctx.fold_cse_limit = old_cse_limit;
5190
0
#endif
5191
0
    ref = ir_BINARY_OP_D(op, op1, op2);
5192
0
    jit_set_Z_DVAL(jit, res_addr, ref);
5193
0
    if (Z_MODE(res_addr) != IS_REG) {
5194
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5195
0
    }
5196
0
    if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5197
0
      ir_MERGE_WITH(fast_path);
5198
0
    }
5199
0
  }
5200
5201
0
  return 1;
5202
0
}
5203
5204
static int zend_jit_math_long_double(zend_jit_ctx   *jit,
5205
                                     uint8_t         opcode,
5206
                                     zend_jit_addr   op1_addr,
5207
                                     zend_jit_addr   op2_addr,
5208
                                     zend_jit_addr   res_addr,
5209
                                     uint32_t        res_use_info)
5210
0
{
5211
0
  ir_op op;
5212
0
  ir_ref op1, op2, ref;
5213
5214
0
  if (opcode == ZEND_ADD) {
5215
0
    op = IR_ADD;
5216
0
  } else if (opcode == ZEND_SUB) {
5217
0
    op = IR_SUB;
5218
0
  } else if (opcode == ZEND_MUL) {
5219
0
    op = IR_MUL;
5220
0
  } else if (opcode == ZEND_DIV) {
5221
0
    op = IR_DIV;
5222
0
  } else {
5223
0
    ZEND_UNREACHABLE();
5224
0
  }
5225
0
  op1 = jit_Z_LVAL(jit, op1_addr);
5226
0
  op2 = jit_Z_DVAL(jit, op2_addr);
5227
0
  ref = ir_BINARY_OP_D(op, ir_INT2D(op1), op2);
5228
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5229
5230
0
  if (Z_MODE(res_addr) != IS_REG) {
5231
0
    if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5232
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5233
0
    }
5234
0
  }
5235
0
  return 1;
5236
0
}
5237
5238
static int zend_jit_math_double_long(zend_jit_ctx   *jit,
5239
                                     uint8_t         opcode,
5240
                                     zend_jit_addr   op1_addr,
5241
                                     zend_jit_addr   op2_addr,
5242
                                     zend_jit_addr   res_addr,
5243
                                     uint32_t        res_use_info)
5244
0
{
5245
0
  ir_op op;
5246
0
  ir_ref op1, op2, ref;
5247
5248
0
  if (opcode == ZEND_ADD) {
5249
0
    op = IR_ADD;
5250
0
  } else if (opcode == ZEND_SUB) {
5251
0
    op = IR_SUB;
5252
0
  } else if (opcode == ZEND_MUL) {
5253
0
    op = IR_MUL;
5254
0
  } else if (opcode == ZEND_DIV) {
5255
0
    op = IR_DIV;
5256
0
  } else {
5257
0
    ZEND_UNREACHABLE();
5258
0
  }
5259
0
  op1 = jit_Z_DVAL(jit, op1_addr);
5260
0
  op2 = jit_Z_LVAL(jit, op2_addr);
5261
0
  ref = ir_BINARY_OP_D(op, op1, ir_INT2D(op2));
5262
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5263
5264
0
  if (Z_MODE(res_addr) != IS_REG) {
5265
0
    if (!zend_jit_same_addr(op1_addr, res_addr)) {
5266
0
      if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5267
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5268
0
      }
5269
0
    }
5270
0
  }
5271
0
  return 1;
5272
0
}
5273
5274
static int zend_jit_math_double_double(zend_jit_ctx   *jit,
5275
                                       uint8_t         opcode,
5276
                                       zend_jit_addr   op1_addr,
5277
                                       zend_jit_addr   op2_addr,
5278
                                       zend_jit_addr   res_addr,
5279
                                       uint32_t        res_use_info)
5280
0
{
5281
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5282
0
  ir_op op;
5283
0
  ir_ref op1, op2, ref;
5284
5285
0
  if (opcode == ZEND_ADD) {
5286
0
    op = IR_ADD;
5287
0
  } else if (opcode == ZEND_SUB) {
5288
0
    op = IR_SUB;
5289
0
  } else if (opcode == ZEND_MUL) {
5290
0
    op = IR_MUL;
5291
0
  } else if (opcode == ZEND_DIV) {
5292
0
    op = IR_DIV;
5293
0
  } else {
5294
0
    ZEND_UNREACHABLE();
5295
0
  }
5296
0
  op1 = jit_Z_DVAL(jit, op1_addr);
5297
0
  op2 = (same_ops) ? op1 : jit_Z_DVAL(jit, op2_addr);
5298
0
  ref = ir_BINARY_OP_D(op, op1, op2);
5299
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5300
5301
0
  if (Z_MODE(res_addr) != IS_REG) {
5302
0
    if (!zend_jit_same_addr(op1_addr, res_addr)) {
5303
0
      if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5304
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5305
0
      }
5306
0
    }
5307
0
  }
5308
0
  return 1;
5309
0
}
5310
5311
static int zend_jit_math_helper(zend_jit_ctx   *jit,
5312
                                const zend_op  *opline,
5313
                                uint8_t         opcode,
5314
                                uint8_t         op1_type,
5315
                                znode_op        op1,
5316
                                zend_jit_addr   op1_addr,
5317
                                uint32_t        op1_info,
5318
                                uint8_t         op2_type,
5319
                                znode_op        op2,
5320
                                zend_jit_addr   op2_addr,
5321
                                uint32_t        op2_info,
5322
                                uint32_t        res_var,
5323
                                zend_jit_addr   res_addr,
5324
                                uint32_t        res_info,
5325
                                uint32_t        res_use_info,
5326
                                int             may_overflow,
5327
                                int             may_throw)
5328
0
{
5329
0
  ir_ref if_op1_long = IR_UNUSED;
5330
0
  ir_ref if_op1_double = IR_UNUSED;
5331
0
  ir_ref if_op2_double = IR_UNUSED;
5332
0
  ir_ref if_op1_long_op2_long = IR_UNUSED;
5333
0
  ir_ref if_op1_long_op2_double = IR_UNUSED;
5334
0
  ir_ref if_op1_double_op2_double = IR_UNUSED;
5335
0
  ir_ref if_op1_double_op2_long = IR_UNUSED;
5336
0
  ir_ref slow_inputs = IR_UNUSED;
5337
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5338
0
  ir_refs *end_inputs;
5339
0
  ir_refs *res_inputs;
5340
5341
0
  ir_refs_init(end_inputs, 6);
5342
0
  ir_refs_init(res_inputs, 6);
5343
5344
0
  if (Z_MODE(op1_addr) == IS_REG) {
5345
0
    if (!has_concrete_type(op2_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
5346
      /* Force load */
5347
0
      zend_jit_use_reg(jit, op1_addr);
5348
0
    }
5349
0
  } else if (Z_MODE(op2_addr) == IS_REG) {
5350
0
    if (!has_concrete_type(op1_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
5351
      /* Force load */
5352
0
      zend_jit_use_reg(jit, op2_addr);
5353
0
    }
5354
0
  }
5355
5356
0
  if (Z_MODE(res_addr) == IS_REG) {
5357
0
    jit->delay_var = Z_SSA_VAR(res_addr);
5358
0
    jit->delay_refs = res_inputs;
5359
0
  }
5360
5361
0
  if ((res_info & MAY_BE_GUARD) && (res_info & MAY_BE_LONG) && (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG)) {
5362
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_LONG)) {
5363
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5364
0
      ir_IF_TRUE(if_op1_long);
5365
0
    }
5366
0
    if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_LONG))) {
5367
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5368
0
      ir_IF_TRUE(if_op1_long_op2_long);
5369
0
    }
5370
0
    if (!zend_jit_math_long_long(jit, opline, opcode, op1_addr, op2_addr, res_addr, res_info, res_use_info, may_overflow)) {
5371
0
      return 0;
5372
0
    }
5373
0
    ir_refs_add(end_inputs, ir_END());
5374
0
    if (if_op1_long) {
5375
0
      ir_IF_FALSE_cold(if_op1_long);
5376
0
      ir_END_list(slow_inputs);
5377
0
    }
5378
0
    if (if_op1_long_op2_long) {
5379
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
5380
0
      ir_END_list(slow_inputs);
5381
0
    }
5382
0
  } else if ((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (res_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
5383
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_LONG)) {
5384
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5385
0
      ir_IF_TRUE(if_op1_long);
5386
0
    }
5387
0
    if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_LONG))) {
5388
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5389
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
5390
0
      if (op2_info & MAY_BE_DOUBLE) {
5391
0
        if (op2_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5392
0
          if_op1_long_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5393
0
          ir_IF_FALSE_cold(if_op1_long_op2_double);
5394
0
          ir_END_list(slow_inputs);
5395
0
          ir_IF_TRUE(if_op1_long_op2_double);
5396
0
        }
5397
0
        if (!zend_jit_math_long_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5398
0
          return 0;
5399
0
        }
5400
0
        ir_refs_add(end_inputs, ir_END());
5401
0
      } else {
5402
0
        ir_END_list(slow_inputs);
5403
0
      }
5404
0
      ir_IF_TRUE(if_op1_long_op2_long);
5405
0
    }
5406
0
    if (!zend_jit_math_long_long(jit, opline, opcode, op1_addr, op2_addr, res_addr, res_info, res_use_info, may_overflow)) {
5407
0
      return 0;
5408
0
    }
5409
0
    ir_refs_add(end_inputs, ir_END());
5410
5411
0
    if (if_op1_long) {
5412
0
      ir_IF_FALSE_cold(if_op1_long);
5413
0
    }
5414
5415
0
    if (op1_info & MAY_BE_DOUBLE) {
5416
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5417
0
        if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5418
0
        ir_IF_FALSE_cold(if_op1_double);
5419
0
        ir_END_list(slow_inputs);
5420
0
        ir_IF_TRUE(if_op1_double);
5421
0
      }
5422
0
      if (op2_info & MAY_BE_DOUBLE) {
5423
0
        if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5424
0
          if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5425
0
          ir_IF_TRUE(if_op1_double_op2_double);
5426
0
        }
5427
0
        if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5428
0
          return 0;
5429
0
        }
5430
0
        ir_refs_add(end_inputs, ir_END());
5431
0
        if (if_op1_double_op2_double) {
5432
0
          ir_IF_FALSE_cold(if_op1_double_op2_double);
5433
0
        }
5434
0
      }
5435
0
      if (!same_ops) {
5436
0
        if (op2_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5437
0
          if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5438
0
          ir_IF_FALSE_cold(if_op1_double_op2_long);
5439
0
          ir_END_list(slow_inputs);
5440
0
          ir_IF_TRUE(if_op1_double_op2_long);
5441
0
        }
5442
0
        if (!zend_jit_math_double_long(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5443
0
          return 0;
5444
0
        }
5445
0
        ir_refs_add(end_inputs, ir_END());
5446
0
      } else if (if_op1_double_op2_double) {
5447
0
        ir_END_list(slow_inputs);
5448
0
      }
5449
0
    } else if (if_op1_long) {
5450
0
      ir_END_list(slow_inputs);
5451
0
    }
5452
0
  } else if ((op1_info & MAY_BE_DOUBLE) &&
5453
0
             !(op1_info & MAY_BE_LONG) &&
5454
0
             (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
5455
0
             (res_info & MAY_BE_DOUBLE)) {
5456
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_DOUBLE)) {
5457
0
      if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5458
0
      ir_IF_FALSE_cold(if_op1_double);
5459
0
      ir_END_list(slow_inputs);
5460
0
      ir_IF_TRUE(if_op1_double);
5461
0
    }
5462
0
    if (op2_info & MAY_BE_DOUBLE) {
5463
0
      if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5464
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5465
0
        ir_IF_TRUE(if_op1_double_op2_double);
5466
0
      }
5467
0
      if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5468
0
        return 0;
5469
0
      }
5470
0
      ir_refs_add(end_inputs, ir_END());
5471
0
      if (if_op1_double_op2_double) {
5472
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
5473
0
      }
5474
0
    }
5475
0
    if (!same_ops && (op2_info & MAY_BE_LONG)) {
5476
0
      if (op2_info & (MAY_BE_ANY-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
5477
0
        if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5478
0
        ir_IF_FALSE_cold(if_op1_double_op2_long);
5479
0
        ir_END_list(slow_inputs);
5480
0
        ir_IF_TRUE(if_op1_double_op2_long);
5481
0
      }
5482
0
      if (!zend_jit_math_double_long(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5483
0
        return 0;
5484
0
      }
5485
0
      ir_refs_add(end_inputs, ir_END());
5486
0
    } else if (if_op1_double_op2_double) {
5487
0
      ir_END_list(slow_inputs);
5488
0
    }
5489
0
  } else if ((op2_info & MAY_BE_DOUBLE) &&
5490
0
             !(op2_info & MAY_BE_LONG) &&
5491
0
             (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
5492
0
             (res_info & MAY_BE_DOUBLE)) {
5493
0
    if (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE)) {
5494
0
      if_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5495
0
      ir_IF_FALSE_cold(if_op2_double);
5496
0
      ir_END_list(slow_inputs);
5497
0
      ir_IF_TRUE(if_op2_double);
5498
0
    }
5499
0
    if (op1_info & MAY_BE_DOUBLE) {
5500
0
      if (!same_ops && (op1_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5501
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5502
0
        ir_IF_TRUE(if_op1_double_op2_double);
5503
0
      }
5504
0
      if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5505
0
        return 0;
5506
0
      }
5507
0
      ir_refs_add(end_inputs, ir_END());
5508
0
      if (if_op1_double_op2_double) {
5509
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
5510
0
      }
5511
0
    }
5512
0
    if (!same_ops && (op1_info & MAY_BE_LONG)) {
5513
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
5514
0
        if_op1_long_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5515
0
        ir_IF_FALSE_cold(if_op1_long_op2_double);
5516
0
        ir_END_list(slow_inputs);
5517
0
        ir_IF_TRUE(if_op1_long_op2_double);
5518
0
      }
5519
0
      if (!zend_jit_math_long_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5520
0
        return 0;
5521
0
      }
5522
0
      ir_refs_add(end_inputs, ir_END());
5523
0
    } else if (if_op1_double_op2_double) {
5524
0
      ir_END_list(slow_inputs);
5525
0
    }
5526
0
  }
5527
5528
0
  if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
5529
0
    (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE)))) {
5530
0
    ir_ref func, arg1, arg2, arg3;
5531
5532
0
    if (slow_inputs) {
5533
0
      ir_MERGE_list(slow_inputs);
5534
0
    }
5535
5536
0
    if (Z_MODE(op1_addr) == IS_REG) {
5537
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op1.var);
5538
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
5539
0
        return 0;
5540
0
      }
5541
0
      op1_addr = real_addr;
5542
0
    }
5543
0
    if (Z_MODE(op2_addr) == IS_REG) {
5544
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op2.var);
5545
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
5546
0
        return 0;
5547
0
      }
5548
0
      op2_addr = real_addr;
5549
0
    }
5550
0
    if (Z_MODE(res_addr) == IS_REG) {
5551
0
      arg1 = jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var));
5552
0
    } else {
5553
0
      arg1 = jit_ZVAL_ADDR(jit, res_addr);
5554
0
    }
5555
0
    arg2 = jit_ZVAL_ADDR(jit, op1_addr);
5556
0
    arg3 = jit_ZVAL_ADDR(jit, op2_addr);
5557
0
    jit_SET_EX_OPLINE(jit, opline);
5558
0
    if (opcode == ZEND_ADD) {
5559
0
      func = ir_CONST_FC_FUNC(add_function);
5560
0
    } else if (opcode == ZEND_SUB) {
5561
0
      func = ir_CONST_FC_FUNC(sub_function);
5562
0
    } else if (opcode == ZEND_MUL) {
5563
0
      func = ir_CONST_FC_FUNC(mul_function);
5564
0
    } else if (opcode == ZEND_DIV) {
5565
0
      func = ir_CONST_FC_FUNC(div_function);
5566
0
    } else {
5567
0
      ZEND_UNREACHABLE();
5568
0
    }
5569
0
    ir_CALL_3(IR_VOID, func, arg1, arg2, arg3);
5570
5571
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
5572
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
5573
5574
0
    if (may_throw) {
5575
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
5576
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
5577
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
5578
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
5579
0
        zend_jit_check_exception_undef_result(jit, opline);
5580
0
      } else {
5581
0
        zend_jit_check_exception(jit);
5582
0
      }
5583
0
    }
5584
0
    if (Z_MODE(res_addr) == IS_REG) {
5585
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var);
5586
0
      if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
5587
0
        return 0;
5588
0
      }
5589
0
    }
5590
0
    ir_refs_add(end_inputs, ir_END());
5591
0
  }
5592
5593
0
  if (end_inputs->count) {
5594
0
    ir_MERGE_N(end_inputs->count, end_inputs->refs);
5595
0
  }
5596
5597
0
  if (Z_MODE(res_addr) == IS_REG) {
5598
0
    ZEND_ASSERT(jit->delay_refs == res_inputs);
5599
0
    ZEND_ASSERT(end_inputs->count == res_inputs->count);
5600
0
    jit->delay_var = -1;
5601
0
    jit->delay_refs = NULL;
5602
0
    if (res_inputs->count == 1) {
5603
0
      zend_jit_def_reg(jit, res_addr, res_inputs->refs[0]);
5604
0
    } else {
5605
0
      ir_ref phi = ir_PHI_N((res_info & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE, res_inputs->count, res_inputs->refs);
5606
0
      zend_jit_def_reg(jit, res_addr, phi);
5607
0
    }
5608
0
  }
5609
5610
0
  return 1;
5611
0
}
5612
5613
static int zend_jit_math(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, uint32_t op2_info, zend_jit_addr op2_addr, uint32_t res_use_info, uint32_t res_info, zend_jit_addr res_addr, int may_overflow, int may_throw)
5614
0
{
5615
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
5616
5617
0
  if (!zend_jit_math_helper(jit, opline, opline->opcode, opline->op1_type, opline->op1, op1_addr, op1_info, opline->op2_type, opline->op2, op2_addr, op2_info, opline->result.var, res_addr, res_info, res_use_info, may_overflow, may_throw)) {
5618
0
    return 0;
5619
0
  }
5620
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
5621
0
    return 0;
5622
0
  }
5623
0
  return 1;
5624
0
}
5625
5626
static int zend_jit_add_arrays(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, uint32_t op2_info, zend_jit_addr op2_addr, zend_jit_addr res_addr)
5627
0
{
5628
0
  ir_ref ref;
5629
0
  ir_ref arg1 = jit_Z_PTR(jit, op1_addr);
5630
0
  ir_ref arg2 = jit_Z_PTR(jit, op2_addr);
5631
5632
0
  ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_add_arrays_helper), arg1, arg2);
5633
0
  jit_set_Z_PTR(jit, res_addr, ref);
5634
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_ARRAY_EX);
5635
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
5636
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
5637
0
  return 1;
5638
0
}
5639
5640
static int zend_jit_long_math_helper(zend_jit_ctx   *jit,
5641
                                     const zend_op  *opline,
5642
                                     uint8_t         opcode,
5643
                                     uint8_t         op1_type,
5644
                                     znode_op        op1,
5645
                                     zend_jit_addr   op1_addr,
5646
                                     uint32_t        op1_info,
5647
                                     zend_ssa_range *op1_range,
5648
                                     uint8_t         op2_type,
5649
                                     znode_op        op2,
5650
                                     zend_jit_addr   op2_addr,
5651
                                     uint32_t        op2_info,
5652
                                     zend_ssa_range *op2_range,
5653
                                     uint32_t        res_var,
5654
                                     zend_jit_addr   res_addr,
5655
                                     uint32_t        res_info,
5656
                                     uint32_t        res_use_info,
5657
                                     int             may_throw)
5658
0
{
5659
0
  ir_ref ref = IR_UNUSED;
5660
0
  ir_ref if_long1 = IR_UNUSED;
5661
0
  ir_ref if_long2 = IR_UNUSED;
5662
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5663
0
  ir_refs *res_inputs;
5664
5665
0
  ir_refs_init(res_inputs, 2);
5666
5667
0
  if (Z_MODE(op1_addr) == IS_REG
5668
0
   && Z_LOAD(op1_addr)
5669
0
   && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
5670
    /* Force load */
5671
0
    zend_jit_use_reg(jit, op1_addr);
5672
0
  }
5673
0
  if (Z_MODE(op2_addr) == IS_REG
5674
0
   && Z_LOAD(op2_addr)
5675
0
   && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
5676
    /* Force load */
5677
0
    zend_jit_use_reg(jit, op2_addr);
5678
0
  }
5679
5680
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
5681
0
    if_long1 = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5682
0
    ir_IF_TRUE(if_long1);
5683
0
  }
5684
0
  if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
5685
0
    if_long2 = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5686
0
    ir_IF_TRUE(if_long2);
5687
0
  }
5688
5689
0
  if (opcode == ZEND_SL) {
5690
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5691
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5692
5693
0
      if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
5694
0
        if (EXPECTED(op2_lval > 0)) {
5695
0
          ref = ir_CONST_LONG(0);
5696
0
        } else {
5697
0
          zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5698
0
          zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5699
0
          jit_SET_EX_OPLINE(jit, opline);
5700
0
          ir_GUARD(IR_FALSE, jit_STUB_ADDR(jit, jit_stub_negative_shift));
5701
0
          if (Z_MODE(res_addr) == IS_REG) {
5702
0
            ref = ir_CONST_LONG(0); // dead code
5703
0
          }
5704
0
        }
5705
0
      } else {
5706
0
        ref = ir_SHL_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5707
0
      }
5708
0
    } else {
5709
0
      ref = jit_Z_LVAL(jit, op2_addr);
5710
0
      if (!op2_range ||
5711
0
           op2_range->min < 0 ||
5712
0
           op2_range->max >= SIZEOF_ZEND_LONG * 8) {
5713
5714
0
        ir_ref if_wrong, cold_path, ref2, if_ok;
5715
0
        ir_ref op1_ref = jit_Z_LVAL(jit, op1_addr);
5716
5717
0
        if_wrong = ir_IF(ir_UGT(ref, ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1)));
5718
0
        ir_IF_TRUE_cold(if_wrong);
5719
0
        if_ok = ir_IF(ir_GE(ref, ir_CONST_LONG(0)));
5720
0
        ir_IF_FALSE(if_ok);
5721
0
        jit_SET_EX_OPLINE(jit, opline);
5722
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5723
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5724
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_negative_shift));
5725
0
        ir_IF_TRUE(if_ok);
5726
0
        ref2 = ir_CONST_LONG(0);
5727
0
        cold_path = ir_END();
5728
0
        ir_IF_FALSE(if_wrong);
5729
0
        ref = ir_SHL_L(op1_ref, ref);
5730
0
        ir_MERGE_WITH(cold_path);
5731
0
        ref = ir_PHI_2(IR_LONG, ref, ref2);
5732
0
      } else {
5733
0
        ref = ir_SHL_L(jit_Z_LVAL(jit, op1_addr), ref);
5734
0
      }
5735
0
    }
5736
0
  } else if (opcode == ZEND_SR) {
5737
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5738
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5739
5740
0
      if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
5741
0
        if (EXPECTED(op2_lval > 0)) {
5742
0
          ref = ir_SAR_L(
5743
0
            jit_Z_LVAL(jit, op1_addr),
5744
0
            ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1));
5745
0
        } else {
5746
0
          zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5747
0
          zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5748
0
          jit_SET_EX_OPLINE(jit, opline);
5749
0
          ir_GUARD(IR_FALSE, jit_STUB_ADDR(jit, jit_stub_negative_shift));
5750
0
          if (Z_MODE(res_addr) == IS_REG) {
5751
0
            ref = ir_CONST_LONG(0); // dead code
5752
0
          }
5753
0
        }
5754
0
      } else {
5755
0
        ref = ir_SAR_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5756
0
      }
5757
0
    } else {
5758
0
      ref = jit_Z_LVAL(jit, op2_addr);
5759
0
      if (!op2_range ||
5760
0
           op2_range->min < 0 ||
5761
0
           op2_range->max >= SIZEOF_ZEND_LONG * 8) {
5762
5763
0
        ir_ref if_wrong, cold_path, ref2, if_ok;
5764
5765
0
        if_wrong = ir_IF(ir_UGT(ref, ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1)));
5766
0
        ir_IF_TRUE_cold(if_wrong);
5767
0
        if_ok = ir_IF(ir_GE(ref, ir_CONST_LONG(0)));
5768
0
        ir_IF_FALSE(if_ok);
5769
0
        jit_SET_EX_OPLINE(jit, opline);
5770
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5771
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5772
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_negative_shift));
5773
0
        ir_IF_TRUE(if_ok);
5774
0
        ref2 = ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1);
5775
0
        cold_path = ir_END();
5776
0
        ir_IF_FALSE(if_wrong);
5777
0
        ir_MERGE_WITH(cold_path);
5778
0
        ref = ir_PHI_2(IR_LONG, ref, ref2);
5779
0
      }
5780
0
      ref = ir_SAR_L(jit_Z_LVAL(jit, op1_addr), ref);
5781
0
    }
5782
0
  } else if (opcode == ZEND_MOD) {
5783
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5784
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5785
5786
0
      if (op2_lval == 0) {
5787
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5788
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5789
0
        jit_SET_EX_OPLINE(jit, opline);
5790
0
        ir_GUARD(IR_FALSE,  jit_STUB_ADDR(jit, jit_stub_mod_by_zero));
5791
0
        if (Z_MODE(res_addr) == IS_REG) {
5792
0
          ref = ir_CONST_LONG(0); // dead code
5793
0
        }
5794
0
      } else if (zend_long_is_power_of_two(op2_lval) && op1_range && op1_range->min >= 0) {
5795
0
        ref = ir_AND_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval - 1));
5796
0
      } else {
5797
0
        ref = ir_MOD_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5798
0
      }
5799
0
    } else {
5800
0
      ir_ref zero_path = 0;
5801
0
      ir_ref op1_ref = jit_Z_LVAL(jit, op1_addr);
5802
5803
0
      ref = jit_Z_LVAL(jit, op2_addr);
5804
0
      if ((op2_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) || !op2_range || (op2_range->min <= 0 && op2_range->max >= 0)) {
5805
0
        ir_ref if_ok = ir_IF(ref);
5806
0
        ir_IF_FALSE(if_ok);
5807
0
        jit_SET_EX_OPLINE(jit, opline);
5808
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5809
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5810
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_mod_by_zero));
5811
0
        ir_IF_TRUE(if_ok);
5812
0
      }
5813
5814
      /* Prevent overflow error/crash if op1 == LONG_MIN and op2 == -1 */
5815
0
      if (!op2_range || (op2_range->min <= -1 && op2_range->max >= -1)) {
5816
0
        ir_ref if_minus_one = ir_IF(ir_EQ(ref, ir_CONST_LONG(-1)));
5817
0
        ir_IF_TRUE_cold(if_minus_one);
5818
0
        zero_path = ir_END();
5819
0
        ir_IF_FALSE(if_minus_one);
5820
0
      }
5821
0
      ref = ir_MOD_L(op1_ref, ref);
5822
5823
0
      if (zero_path) {
5824
0
        ir_MERGE_WITH(zero_path);
5825
0
        ref = ir_PHI_2(IR_LONG, ref, ir_CONST_LONG(0));
5826
0
      }
5827
0
    }
5828
0
  } else {
5829
0
    ir_op op;
5830
0
    ir_ref op1, op2;
5831
5832
0
    if (opcode == ZEND_BW_OR) {
5833
0
      op = IR_OR;
5834
0
    } else if (opcode == ZEND_BW_AND) {
5835
0
      op = IR_AND;
5836
0
    } else if (opcode == ZEND_BW_XOR) {
5837
0
      op = IR_XOR;
5838
0
    } else {
5839
0
      ZEND_UNREACHABLE();
5840
0
    }
5841
0
    op1 = jit_Z_LVAL(jit, op1_addr);
5842
0
    op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5843
0
    ref = ir_BINARY_OP_L(op, op1, op2);
5844
0
  }
5845
5846
0
  if (ref) {
5847
0
    if (Z_MODE(res_addr) == IS_REG
5848
0
     && ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))
5849
0
      || (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)))) {
5850
0
      jit->delay_var = Z_SSA_VAR(res_addr);
5851
0
      jit->delay_refs = res_inputs;
5852
0
    }
5853
0
    jit_set_Z_LVAL(jit, res_addr, ref);
5854
0
    if (Z_MODE(res_addr) != IS_REG) {
5855
0
      if (!zend_jit_same_addr(op1_addr, res_addr)) {
5856
0
        if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_LONG) {
5857
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
5858
0
        }
5859
0
      }
5860
0
    }
5861
0
  }
5862
5863
0
  if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) ||
5864
0
    (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
5865
0
    ir_ref fast_path = ir_END();
5866
0
    ir_ref func, arg1, arg2, arg3;
5867
5868
0
    if (if_long2 && if_long1) {
5869
0
      ir_ref ref;
5870
0
      ir_IF_FALSE_cold(if_long2);
5871
0
      ref = ir_END();
5872
0
      ir_IF_FALSE_cold(if_long1);
5873
0
      ir_MERGE_2(ref, ir_END());
5874
0
    } else if (if_long1) {
5875
0
      ir_IF_FALSE_cold(if_long1);
5876
0
    } else if (if_long2) {
5877
0
      ir_IF_FALSE_cold(if_long2);
5878
0
    }
5879
5880
0
    if (op1_info & MAY_BE_UNDEF) {
5881
0
      ir_ref if_def, ref, ref2;
5882
5883
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
5884
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
5885
0
      ir_IF_FALSE_cold(if_def);
5886
5887
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
5888
0
      jit_SET_EX_OPLINE(jit, opline);
5889
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
5890
5891
0
      ref2 = jit_EG(uninitialized_zval);
5892
0
      ir_MERGE_WITH_EMPTY_TRUE(if_def);
5893
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
5894
0
      op1_addr = ZEND_ADDR_REF_ZVAL(ref);
5895
0
    }
5896
5897
0
    if (op2_info & MAY_BE_UNDEF) {
5898
0
      ir_ref if_def, ref, ref2;
5899
5900
0
      ref = jit_ZVAL_ADDR(jit, op2_addr);
5901
0
      if_def = jit_if_not_Z_TYPE(jit, op2_addr, IS_UNDEF);
5902
0
      ir_IF_FALSE_cold(if_def);
5903
5904
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op2.var)));
5905
0
      jit_SET_EX_OPLINE(jit, opline);
5906
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op2.var));
5907
5908
0
      ref2 = jit_EG(uninitialized_zval);
5909
0
      ir_MERGE_WITH_EMPTY_TRUE(if_def);
5910
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
5911
0
      op2_addr = ZEND_ADDR_REF_ZVAL(ref);
5912
0
    }
5913
5914
0
    if (Z_MODE(op1_addr) == IS_REG) {
5915
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op1.var);
5916
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
5917
0
        return 0;
5918
0
      }
5919
0
      op1_addr = real_addr;
5920
0
    }
5921
0
    if (Z_MODE(op2_addr) == IS_REG) {
5922
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op2.var);
5923
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
5924
0
        return 0;
5925
0
      }
5926
0
      op2_addr = real_addr;
5927
0
    }
5928
0
    if (Z_MODE(res_addr) == IS_REG) {
5929
0
      arg1 = jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var));
5930
0
    } else {
5931
0
      arg1 = jit_ZVAL_ADDR(jit, res_addr);
5932
0
    }
5933
0
    arg2 = jit_ZVAL_ADDR(jit, op1_addr);
5934
0
    arg3 = jit_ZVAL_ADDR(jit, op2_addr);
5935
0
    jit_SET_EX_OPLINE(jit, opline);
5936
0
    if (opcode == ZEND_BW_OR) {
5937
0
      func = ir_CONST_FC_FUNC(bitwise_or_function);
5938
0
    } else if (opcode == ZEND_BW_AND) {
5939
0
      func = ir_CONST_FC_FUNC(bitwise_and_function);
5940
0
    } else if (opcode == ZEND_BW_XOR) {
5941
0
      func = ir_CONST_FC_FUNC(bitwise_xor_function);
5942
0
    } else if (opcode == ZEND_SL) {
5943
0
      func = ir_CONST_FC_FUNC(shift_left_function);
5944
0
    } else if (opcode == ZEND_SR) {
5945
0
      func = ir_CONST_FC_FUNC(shift_right_function);
5946
0
    } else if (opcode == ZEND_MOD) {
5947
0
      func = ir_CONST_FC_FUNC(mod_function);
5948
0
    } else {
5949
0
      ZEND_UNREACHABLE();
5950
0
    }
5951
0
    ir_CALL_3(IR_VOID, func, arg1, arg2, arg3);
5952
5953
0
    if (op1_addr == res_addr && (op2_info & MAY_BE_RCN)) {
5954
      /* compound assignment may decrement "op2" refcount */
5955
0
      op2_info |= MAY_BE_RC1;
5956
0
    }
5957
5958
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
5959
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
5960
5961
0
    if (may_throw) {
5962
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
5963
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
5964
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
5965
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
5966
0
        zend_jit_check_exception_undef_result(jit, opline);
5967
0
      } else {
5968
0
        zend_jit_check_exception(jit);
5969
0
      }
5970
0
    }
5971
5972
0
    if (Z_MODE(res_addr) == IS_REG) {
5973
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var);
5974
0
      if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
5975
0
        return 0;
5976
0
      }
5977
0
    }
5978
5979
0
    ir_MERGE_2(fast_path, ir_END());
5980
5981
0
    if (Z_MODE(res_addr) == IS_REG) {
5982
0
      ZEND_ASSERT(jit->delay_refs == res_inputs);
5983
0
      ZEND_ASSERT(res_inputs->count == 2);
5984
0
      jit->delay_var = -1;
5985
0
      jit->delay_refs = NULL;
5986
0
      if (res_inputs->count == 1) {
5987
0
        zend_jit_def_reg(jit, res_addr, res_inputs->refs[0]);
5988
0
      } else {
5989
0
        ir_ref phi = ir_PHI_N(IR_LONG, res_inputs->count, res_inputs->refs);
5990
0
        zend_jit_def_reg(jit, res_addr, phi);
5991
0
      }
5992
0
    }
5993
0
  }
5994
5995
0
  return 1;
5996
0
}
5997
5998
static int zend_jit_long_math(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_ssa_range *op1_range, zend_jit_addr op1_addr, uint32_t op2_info, zend_ssa_range *op2_range, zend_jit_addr op2_addr, uint32_t res_use_info, uint32_t res_info, zend_jit_addr res_addr, int may_throw)
5999
0
{
6000
0
  ZEND_ASSERT((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG));
6001
6002
0
  if (!zend_jit_long_math_helper(jit, opline, opline->opcode,
6003
0
      opline->op1_type, opline->op1, op1_addr, op1_info, op1_range,
6004
0
      opline->op2_type, opline->op2, op2_addr, op2_info, op2_range,
6005
0
      opline->result.var, res_addr, res_info, res_use_info, may_throw)) {
6006
0
    return 0;
6007
0
  }
6008
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6009
0
    return 0;
6010
0
  }
6011
0
  return 1;
6012
0
}
6013
6014
static int zend_jit_concat_helper(zend_jit_ctx   *jit,
6015
                                  const zend_op  *opline,
6016
                                  uint8_t         op1_type,
6017
                                  znode_op        op1,
6018
                                  zend_jit_addr   op1_addr,
6019
                                  uint32_t        op1_info,
6020
                                  uint8_t         op2_type,
6021
                                  znode_op        op2,
6022
                                  zend_jit_addr   op2_addr,
6023
                                  uint32_t        op2_info,
6024
                                  zend_jit_addr   res_addr,
6025
                                  int             may_throw)
6026
0
{
6027
0
  ir_ref if_op1_string = IR_UNUSED;
6028
0
  ir_ref if_op2_string = IR_UNUSED;
6029
0
  ir_ref fast_path = IR_UNUSED;
6030
6031
0
  if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6032
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) {
6033
0
      if_op1_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
6034
0
      ir_IF_TRUE(if_op1_string);
6035
0
    }
6036
0
    if (op2_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) {
6037
0
      if_op2_string = jit_if_Z_TYPE(jit, op2_addr, IS_STRING);
6038
0
      ir_IF_TRUE(if_op2_string);
6039
0
    }
6040
0
    if (zend_jit_same_addr(op1_addr, res_addr)) {
6041
0
      ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6042
0
      ir_ref arg2 = jit_ZVAL_ADDR(jit, op2_addr);
6043
6044
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_assign_concat_helper), arg1, arg2);
6045
      /* concatenation with itself may reduce refcount */
6046
0
      op2_info |= MAY_BE_RC1;
6047
0
    } else {
6048
0
      ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6049
0
      ir_ref arg2 = jit_ZVAL_ADDR(jit, op1_addr);
6050
0
      ir_ref arg3 = jit_ZVAL_ADDR(jit, op2_addr);
6051
6052
0
      if (op1_type == IS_CV || op1_type == IS_CONST) {
6053
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_concat_helper), arg1, arg2, arg3);
6054
0
      } else {
6055
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_concat_tmp_helper), arg1, arg2, arg3);
6056
0
      }
6057
0
    }
6058
    /* concatenation with empty string may increase refcount */
6059
0
    op2_info |= MAY_BE_RCN;
6060
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, opline);
6061
0
    if (if_op1_string || if_op2_string) {
6062
0
      fast_path = ir_END();
6063
0
    }
6064
0
  }
6065
0
  if ((op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) ||
6066
0
      (op2_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING))) {
6067
0
    if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6068
0
      if (if_op1_string && if_op2_string) {
6069
0
        ir_IF_FALSE(if_op1_string);
6070
0
        ir_MERGE_WITH_EMPTY_FALSE(if_op2_string);
6071
0
      } else if (if_op1_string) {
6072
0
        ir_IF_FALSE_cold(if_op1_string);
6073
0
      } else if (if_op2_string) {
6074
0
        ir_IF_FALSE_cold(if_op2_string);
6075
0
      }
6076
0
    }
6077
0
    ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6078
0
    ir_ref arg2 = jit_ZVAL_ADDR(jit, op1_addr);
6079
0
    ir_ref arg3 = jit_ZVAL_ADDR(jit, op2_addr);
6080
6081
0
    jit_SET_EX_OPLINE(jit, opline);
6082
0
    ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(concat_function), arg1, arg2, arg3);
6083
    /* concatenation with empty string may increase refcount */
6084
0
    op1_info |= MAY_BE_RCN;
6085
0
    op2_info |= MAY_BE_RCN;
6086
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
6087
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
6088
0
    if (may_throw) {
6089
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
6090
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
6091
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
6092
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
6093
0
        zend_jit_check_exception_undef_result(jit, opline);
6094
0
      } else {
6095
0
        zend_jit_check_exception(jit);
6096
0
      }
6097
0
    }
6098
0
    if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6099
0
      ir_MERGE_WITH(fast_path);
6100
0
    }
6101
0
  }
6102
0
  return 1;
6103
0
}
6104
6105
static int zend_jit_concat(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint32_t op2_info, zend_jit_addr res_addr, int may_throw)
6106
0
{
6107
0
  zend_jit_addr op1_addr, op2_addr;
6108
6109
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
6110
0
  ZEND_ASSERT((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING));
6111
6112
0
  op1_addr = OP1_ADDR();
6113
0
  op2_addr = OP2_ADDR();
6114
6115
0
  return zend_jit_concat_helper(jit, opline, opline->op1_type, opline->op1, op1_addr, op1_info, opline->op2_type, opline->op2, op2_addr, op2_info, res_addr, may_throw);
6116
0
}
6117
6118
static int zend_jit_assign_op(zend_jit_ctx   *jit,
6119
                              const zend_op  *opline,
6120
                              uint32_t        op1_info,
6121
                              zend_jit_addr   op1_addr,
6122
                              zend_ssa_range *op1_range,
6123
                              uint32_t        op1_def_info,
6124
                              zend_jit_addr   op1_def_addr,
6125
                              uint32_t        op1_mem_info,
6126
                              uint32_t        op2_info,
6127
                              zend_jit_addr   op2_addr,
6128
                              zend_ssa_range *op2_range,
6129
                              int             may_overflow,
6130
                              int             may_throw)
6131
0
{
6132
0
  int result = 1;
6133
0
  ir_ref slow_path = IR_UNUSED;
6134
6135
0
  ZEND_ASSERT(opline->op1_type == IS_CV && opline->result_type == IS_UNUSED);
6136
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
6137
6138
0
  if (op1_info & MAY_BE_REF) {
6139
0
    ir_ref ref, ref2, arg2, op1_noref_path;
6140
0
    ir_ref if_op1_ref = IR_UNUSED;
6141
0
    ir_ref if_op1_typed = IR_UNUSED;
6142
0
    binary_op_type binary_op = get_binary_op(opline->extended_value);
6143
6144
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
6145
0
    if_op1_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
6146
0
    ir_IF_FALSE(if_op1_ref);
6147
0
    op1_noref_path = ir_END();
6148
0
    ir_IF_TRUE(if_op1_ref);
6149
0
    ref2 = jit_Z_PTR_ref(jit, ref);
6150
6151
0
    if_op1_typed = jit_if_TYPED_REF(jit, ref2);
6152
0
    ir_IF_TRUE_cold(if_op1_typed);
6153
6154
0
    if (Z_MODE(op2_addr) == IS_REG) {
6155
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
6156
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
6157
0
        return 0;
6158
0
      }
6159
0
      arg2 = jit_ZVAL_ADDR(jit, real_addr);
6160
0
    } else {
6161
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
6162
0
    }
6163
0
    jit_SET_EX_OPLINE(jit, opline);
6164
0
    if ((opline->op2_type & (IS_TMP_VAR|IS_VAR))
6165
0
     && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
6166
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref_tmp),
6167
0
        ref2, arg2, ir_CONST_FC_FUNC(binary_op));
6168
0
    } else {
6169
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
6170
0
        ref2, arg2, ir_CONST_FC_FUNC(binary_op));
6171
0
    }
6172
0
    zend_jit_check_exception(jit);
6173
0
    slow_path = ir_END();
6174
6175
0
    ir_IF_FALSE(if_op1_typed);
6176
0
    ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
6177
6178
0
    ir_MERGE_WITH(op1_noref_path);
6179
0
    ref = ir_PHI_2(IR_ADDR, ref2, ref);
6180
0
    ZEND_ASSERT(op1_addr == op1_def_addr);
6181
0
    op1_def_addr = op1_addr = ZEND_ADDR_REF_ZVAL(ref);
6182
0
  }
6183
6184
0
  switch (opline->extended_value) {
6185
0
    case ZEND_ADD:
6186
0
    case ZEND_SUB:
6187
0
    case ZEND_MUL:
6188
0
    case ZEND_DIV:
6189
0
      result = zend_jit_math_helper(jit, opline, opline->extended_value, opline->op1_type, opline->op1, op1_addr, op1_info, opline->op2_type, opline->op2, op2_addr, op2_info, opline->op1.var, op1_def_addr, op1_def_info, op1_mem_info, may_overflow, may_throw);
6190
0
      break;
6191
0
    case ZEND_BW_OR:
6192
0
    case ZEND_BW_AND:
6193
0
    case ZEND_BW_XOR:
6194
0
    case ZEND_SL:
6195
0
    case ZEND_SR:
6196
0
    case ZEND_MOD:
6197
0
      result = zend_jit_long_math_helper(jit, opline, opline->extended_value,
6198
0
        opline->op1_type, opline->op1, op1_addr, op1_info, op1_range,
6199
0
        opline->op2_type, opline->op2, op2_addr, op2_info, op2_range,
6200
0
        opline->op1.var, op1_def_addr, op1_def_info, op1_mem_info, may_throw);
6201
0
      break;
6202
0
    case ZEND_CONCAT:
6203
0
      result = zend_jit_concat_helper(jit, opline, opline->op1_type, opline->op1, op1_addr, op1_info, opline->op2_type, opline->op2, op2_addr, op2_info, op1_def_addr, may_throw);
6204
0
      break;
6205
0
    default:
6206
0
      ZEND_UNREACHABLE();
6207
0
  }
6208
6209
0
  if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_def_addr, op1_def_info, op1_addr, op1_info)) {
6210
0
    return 0;
6211
0
  }
6212
6213
0
  if (op1_info & MAY_BE_REF) {
6214
0
    ir_MERGE_WITH(slow_path);
6215
0
  }
6216
6217
0
  return result;
6218
0
}
6219
6220
static ir_ref jit_ZVAL_DEREF_ref(zend_jit_ctx *jit, ir_ref ref)
6221
0
{
6222
0
  ir_ref if_ref, ref2;
6223
6224
0
  if_ref = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_REFERENCE)));
6225
0
  ir_IF_TRUE(if_ref);
6226
0
  ref2 = ir_ADD_OFFSET(jit_Z_PTR_ref(jit, ref), offsetof(zend_reference, val));
6227
0
  ir_MERGE_WITH_EMPTY_FALSE(if_ref);
6228
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
6229
0
}
6230
6231
static zend_jit_addr jit_ZVAL_DEREF(zend_jit_ctx *jit, zend_jit_addr addr)
6232
0
{
6233
0
  ir_ref ref = jit_ZVAL_ADDR(jit, addr);
6234
0
  ref = jit_ZVAL_DEREF_ref(jit, ref);
6235
0
  return ZEND_ADDR_REF_ZVAL(ref);
6236
0
}
6237
6238
static ir_ref jit_ZVAL_INDIRECT_DEREF_ref(zend_jit_ctx *jit, ir_ref ref)
6239
0
{
6240
0
  ir_ref if_ref, ref2;
6241
6242
0
  if_ref = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_INDIRECT)));
6243
0
  ir_IF_TRUE(if_ref);
6244
0
  ref2 = jit_Z_PTR_ref(jit, ref);
6245
0
  ir_MERGE_WITH_EMPTY_FALSE(if_ref);
6246
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
6247
0
}
6248
6249
static zend_jit_addr jit_ZVAL_INDIRECT_DEREF(zend_jit_ctx *jit, zend_jit_addr addr)
6250
0
{
6251
0
  ir_ref ref = jit_ZVAL_ADDR(jit, addr);
6252
0
  ref = jit_ZVAL_INDIRECT_DEREF_ref(jit, ref);
6253
0
  return ZEND_ADDR_REF_ZVAL(ref);
6254
0
}
6255
6256
static int zend_jit_simple_assign(zend_jit_ctx   *jit,
6257
                                  const zend_op  *opline,
6258
                                  zend_jit_addr   var_addr,
6259
                                  uint32_t        var_info,
6260
                                  uint32_t        var_def_info,
6261
                                  uint8_t         val_type,
6262
                                  zend_jit_addr   val_addr,
6263
                                  uint32_t        val_info,
6264
                                  zend_jit_addr   res_addr,
6265
                                  bool            check_exception)
6266
0
{
6267
0
  ir_ref end_inputs = IR_UNUSED;
6268
6269
0
  if (Z_MODE(val_addr) == IS_CONST_ZVAL) {
6270
0
    zval *zv = Z_ZV(val_addr);
6271
6272
0
    if (!res_addr) {
6273
0
      jit_ZVAL_COPY_CONST(jit,
6274
0
        var_addr,
6275
0
        var_info, var_def_info,
6276
0
        zv, true);
6277
0
    } else {
6278
0
      jit_ZVAL_COPY_CONST(jit,
6279
0
        var_addr,
6280
0
        var_info, var_def_info,
6281
0
        zv, true);
6282
0
      jit_ZVAL_COPY_CONST(jit,
6283
0
        res_addr,
6284
0
        -1, var_def_info,
6285
0
        zv, true);
6286
0
    }
6287
0
  } else {
6288
0
    if (val_info & MAY_BE_UNDEF) {
6289
0
      ir_ref if_def, ret;
6290
6291
0
      if_def = jit_if_not_Z_TYPE(jit, val_addr, IS_UNDEF);
6292
0
      ir_IF_FALSE_cold(if_def);
6293
6294
0
      jit_set_Z_TYPE_INFO(jit, var_addr, IS_NULL);
6295
0
      if (res_addr) {
6296
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
6297
0
      }
6298
0
      jit_SET_EX_OPLINE(jit, opline);
6299
6300
0
      ZEND_ASSERT(Z_MODE(val_addr) == IS_MEM_ZVAL);
6301
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
6302
0
      ret = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(Z_OFFSET(val_addr)));
6303
6304
0
      if (check_exception) {
6305
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
6306
0
      }
6307
6308
0
      ir_END_list(end_inputs);
6309
0
      ir_IF_TRUE(if_def);
6310
0
    }
6311
0
    if (val_info & MAY_BE_REF) {
6312
0
      if (val_type == IS_CV) {
6313
0
        ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
6314
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
6315
0
        val_addr = ZEND_ADDR_REF_ZVAL(ref);
6316
0
      } else {
6317
0
        ir_ref ref, type, if_ref, ref2, refcount, if_not_zero;
6318
6319
0
        ref = jit_ZVAL_ADDR(jit, val_addr);
6320
0
        type = jit_Z_TYPE_ref(jit, ref);
6321
0
        if_ref = ir_IF(ir_EQ(type, ir_CONST_U8(IS_REFERENCE)));
6322
6323
0
        ir_IF_TRUE_cold(if_ref);
6324
0
        ref = jit_Z_PTR_ref(jit, ref);
6325
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
6326
0
        if (!res_addr) {
6327
0
          jit_ZVAL_COPY(jit,
6328
0
            var_addr,
6329
0
            var_info,
6330
0
            ZEND_ADDR_REF_ZVAL(ref2), val_info, true);
6331
0
        } else {
6332
0
          jit_ZVAL_COPY_2(jit,
6333
0
            res_addr,
6334
0
            var_addr,
6335
0
            var_info,
6336
0
            ZEND_ADDR_REF_ZVAL(ref2), val_info, 2);
6337
0
        }
6338
6339
0
        refcount = jit_GC_DELREF(jit, ref);
6340
0
        if_not_zero = ir_IF(refcount);
6341
0
        ir_IF_FALSE(if_not_zero);
6342
        // TODO: instead of dtor() call and ADDREF above, we may call efree() and move addref at "true" path ???
6343
        // This is related to GH-10168 (keep this before GH-10168 is completely closed)
6344
        // jit_EFREE(jit, ref, sizeof(zend_reference), NULL, NULL);
6345
0
        jit_ZVAL_DTOR(jit, ref, val_info, opline);
6346
0
        ir_END_list(end_inputs);
6347
0
        ir_IF_TRUE(if_not_zero);
6348
0
        ir_END_list(end_inputs);
6349
6350
0
        ir_IF_FALSE(if_ref);
6351
0
      }
6352
0
    }
6353
6354
0
    if (!res_addr) {
6355
0
      jit_ZVAL_COPY(jit,
6356
0
        var_addr,
6357
0
        var_info,
6358
0
        val_addr, val_info, val_type == IS_CV);
6359
0
    } else {
6360
0
      jit_ZVAL_COPY_2(jit,
6361
0
        res_addr,
6362
0
        var_addr,
6363
0
        var_info,
6364
0
        val_addr, val_info, val_type == IS_CV ? 2 : 1);
6365
0
    }
6366
0
  }
6367
6368
0
  if (end_inputs) {
6369
0
    ir_END_list(end_inputs);
6370
0
    ir_MERGE_list(end_inputs);
6371
0
  }
6372
6373
0
  return 1;
6374
0
}
6375
6376
static int zend_jit_assign_to_variable_call(zend_jit_ctx   *jit,
6377
                                            const zend_op  *opline,
6378
                                            zend_jit_addr   __var_use_addr,
6379
                                            zend_jit_addr   var_addr,
6380
                                            uint32_t        __var_info,
6381
                                            uint32_t        __var_def_info,
6382
                                            uint8_t         val_type,
6383
                                            zend_jit_addr   val_addr,
6384
                                            uint32_t        val_info,
6385
                                            zend_jit_addr   __res_addr,
6386
                                            bool       __check_exception)
6387
0
{
6388
0
  jit_stub_id func;
6389
0
  ir_ref undef_path = IR_UNUSED;
6390
6391
0
  if (val_info & MAY_BE_UNDEF) {
6392
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
6393
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
6394
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
6395
6396
0
      if (!exit_addr) {
6397
0
        return 0;
6398
0
      }
6399
6400
0
      jit_guard_not_Z_TYPE(jit, val_addr, IS_UNDEF, exit_addr);
6401
0
    } else {
6402
0
      ir_ref if_def;
6403
6404
0
      ZEND_ASSERT(Z_MODE(val_addr) == IS_MEM_ZVAL && Z_REG(val_addr) == ZREG_FP);
6405
0
      if_def = ir_IF(jit_Z_TYPE(jit, val_addr));
6406
0
      ir_IF_FALSE_cold(if_def);
6407
0
      jit_SET_EX_OPLINE(jit, opline);
6408
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(Z_OFFSET(val_addr)));
6409
6410
0
      ir_CALL_2(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_assign_const, IR_FASTCALL_FUNC),
6411
0
        jit_ZVAL_ADDR(jit, var_addr),
6412
0
        jit_EG(uninitialized_zval));
6413
6414
0
      undef_path = ir_END();
6415
0
      ir_IF_TRUE(if_def);
6416
0
    }
6417
0
  }
6418
6419
0
  if (!(val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {
6420
0
    func = jit_stub_assign_tmp;
6421
0
  } else if (val_type == IS_CONST) {
6422
0
    func = jit_stub_assign_const;
6423
0
  } else if (val_type == IS_TMP_VAR) {
6424
0
    func = jit_stub_assign_tmp;
6425
0
  } else if (val_type == IS_VAR) {
6426
0
    if (!(val_info & MAY_BE_REF)) {
6427
0
      func = jit_stub_assign_tmp;
6428
0
    } else {
6429
0
      func = jit_stub_assign_var;
6430
0
    }
6431
0
  } else if (val_type == IS_CV) {
6432
0
    if (!(val_info & MAY_BE_REF)) {
6433
0
      func = jit_stub_assign_cv_noref;
6434
0
    } else {
6435
0
      func = jit_stub_assign_cv;
6436
0
    }
6437
0
  } else {
6438
0
    ZEND_UNREACHABLE();
6439
0
  }
6440
6441
0
  if (opline) {
6442
0
    jit_SET_EX_OPLINE(jit, opline);
6443
0
  }
6444
6445
0
  ir_CALL_2(IR_VOID, jit_STUB_FUNC_ADDR(jit, func, IR_FASTCALL_FUNC),
6446
0
    jit_ZVAL_ADDR(jit, var_addr),
6447
0
    jit_ZVAL_ADDR(jit, val_addr));
6448
6449
0
  if (undef_path) {
6450
0
    ir_MERGE_WITH(undef_path);
6451
0
  }
6452
6453
0
  return 1;
6454
0
}
6455
6456
static int zend_jit_assign_to_variable(zend_jit_ctx   *jit,
6457
                                       const zend_op  *opline,
6458
                                       zend_jit_addr   var_use_addr,
6459
                                       zend_jit_addr   var_addr,
6460
                                       uint32_t        var_info,
6461
                                       uint32_t        var_def_info,
6462
                                       uint8_t         val_type,
6463
                                       zend_jit_addr   val_addr,
6464
                                       uint32_t        val_info,
6465
                                       zend_jit_addr   res_addr,
6466
                                       zend_jit_addr   ref_addr,
6467
                                       bool       check_exception)
6468
0
{
6469
0
  ir_ref if_refcounted = IR_UNUSED;
6470
0
  ir_ref simple_inputs = IR_UNUSED;
6471
0
  bool done = false;
6472
0
  zend_jit_addr real_res_addr = 0;
6473
0
  ir_refs *end_inputs;
6474
0
  ir_refs *res_inputs;
6475
6476
0
  ir_refs_init(end_inputs, 6);
6477
0
  ir_refs_init(res_inputs, 6);
6478
6479
0
  if (Z_MODE(val_addr) == IS_REG && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
6480
    /* Force load */
6481
0
    zend_jit_use_reg(jit, val_addr);
6482
0
  }
6483
6484
0
  if (Z_MODE(var_addr) == IS_REG) {
6485
0
    jit->delay_var = Z_SSA_VAR(var_addr);
6486
0
    jit->delay_refs = res_inputs;
6487
0
    if (Z_MODE(res_addr) == IS_REG) {
6488
0
      real_res_addr = res_addr;
6489
0
      res_addr = 0;
6490
0
    }
6491
0
  } else if (Z_MODE(res_addr) == IS_REG) {
6492
0
    jit->delay_var = Z_SSA_VAR(res_addr);
6493
0
    jit->delay_refs = res_inputs;
6494
0
  }
6495
6496
0
  if ((var_info & MAY_BE_REF) || ref_addr) {
6497
0
    ir_ref ref = 0, if_ref = 0, ref2, arg2, if_typed, non_ref_path;
6498
0
    uintptr_t func;
6499
6500
0
    if (!ref_addr) {
6501
0
      ref = jit_ZVAL_ADDR(jit, var_use_addr);
6502
0
      if_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
6503
0
      ir_IF_TRUE(if_ref);
6504
0
      ref2 = jit_Z_PTR_ref(jit, ref);
6505
0
    } else {
6506
0
      ref2 = jit_ZVAL_ADDR(jit, ref_addr);
6507
0
    }
6508
0
    if_typed = jit_if_TYPED_REF(jit, ref2);
6509
0
    ir_IF_TRUE_cold(if_typed);
6510
0
    jit_SET_EX_OPLINE(jit, opline);
6511
0
    if (Z_MODE(val_addr) == IS_REG) {
6512
0
      zend_jit_addr real_addr;
6513
6514
0
      if (opline->opcode == ZEND_ASSIGN_DIM || opline->opcode == ZEND_ASSIGN_OBJ) {
6515
0
        real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
6516
0
      } else {
6517
0
        ZEND_ASSERT(opline->opcode == ZEND_ASSIGN);
6518
0
        real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
6519
0
      }
6520
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
6521
0
        return 0;
6522
0
      }
6523
0
      arg2 = jit_ZVAL_ADDR(jit, real_addr);
6524
0
    } else {
6525
0
      arg2 = jit_ZVAL_ADDR(jit, val_addr);
6526
0
    }
6527
0
    if (!res_addr) {
6528
0
      if (val_type == IS_CONST) {
6529
0
        func = (uintptr_t)zend_jit_assign_const_to_typed_ref;
6530
0
      } else if (val_type == IS_TMP_VAR) {
6531
0
        func = (uintptr_t)zend_jit_assign_tmp_to_typed_ref;
6532
0
      } else if (val_type == IS_VAR) {
6533
0
        func = (uintptr_t)zend_jit_assign_var_to_typed_ref;
6534
0
      } else if (val_type == IS_CV) {
6535
0
        func = (uintptr_t)zend_jit_assign_cv_to_typed_ref;
6536
0
      } else {
6537
0
        ZEND_UNREACHABLE();
6538
0
      }
6539
0
      ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(func), ref2, arg2);
6540
0
    } else {
6541
0
      if (val_type == IS_CONST) {
6542
0
        func = (uintptr_t)zend_jit_assign_const_to_typed_ref2;
6543
0
      } else if (val_type == IS_TMP_VAR) {
6544
0
        func = (uintptr_t)zend_jit_assign_tmp_to_typed_ref2;
6545
0
      } else if (val_type == IS_VAR) {
6546
0
        func = (uintptr_t)zend_jit_assign_var_to_typed_ref2;
6547
0
      } else if (val_type == IS_CV) {
6548
0
        func = (uintptr_t)zend_jit_assign_cv_to_typed_ref2;
6549
0
      } else {
6550
0
        ZEND_UNREACHABLE();
6551
0
      }
6552
0
      ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(func), ref2, arg2, jit_ZVAL_ADDR(jit, res_addr));
6553
0
    }
6554
0
    if (check_exception) {
6555
0
      zend_jit_check_exception(jit);
6556
0
    }
6557
0
    ir_refs_add(end_inputs, ir_END());
6558
6559
0
    if (!ref_addr) {
6560
0
      ir_IF_FALSE(if_ref);
6561
0
      non_ref_path = ir_END();
6562
0
      ir_IF_FALSE(if_typed);
6563
0
      ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
6564
0
      ir_MERGE_WITH(non_ref_path);
6565
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
6566
0
      var_addr = var_use_addr = ZEND_ADDR_REF_ZVAL(ref);
6567
0
    } else {
6568
0
      ir_IF_FALSE(if_typed);
6569
0
    }
6570
0
  }
6571
6572
0
  if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
6573
0
    ir_ref ref, counter, if_not_zero;
6574
6575
0
    if (var_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
6576
0
      if_refcounted = jit_if_REFCOUNTED(jit, var_use_addr);
6577
0
      ir_IF_FALSE(if_refcounted);
6578
0
      ir_END_list(simple_inputs);
6579
0
      ir_IF_TRUE_cold(if_refcounted);
6580
0
    } else if (RC_MAY_BE_1(var_info)) {
6581
0
      done = true;
6582
0
    }
6583
0
    ref = jit_Z_PTR(jit, var_use_addr);
6584
0
    if (RC_MAY_BE_1(var_info)) {
6585
0
      if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, var_def_info, val_type, val_addr, val_info, res_addr, false)) {
6586
0
        return 0;
6587
0
      }
6588
0
      counter = jit_GC_DELREF(jit, ref);
6589
6590
0
      if_not_zero = ir_IF(counter);
6591
0
      ir_IF_FALSE(if_not_zero);
6592
0
      jit_ZVAL_DTOR(jit, ref, var_info, opline);
6593
0
      if (check_exception) {
6594
0
        zend_jit_check_exception(jit);
6595
0
      }
6596
0
      ir_refs_add(end_inputs, ir_END());
6597
0
      ir_IF_TRUE(if_not_zero);
6598
0
      if (RC_MAY_BE_N(var_info) && (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT)) != 0) {
6599
0
        ir_ref if_may_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
6600
0
        ir_IF_FALSE(if_may_leak);
6601
0
        if (opline) {
6602
0
          jit_SET_EX_OPLINE(jit, opline);
6603
0
        }
6604
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
6605
6606
0
        if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6607
0
          ZEND_ASSERT(jit->delay_refs == res_inputs);
6608
0
          ZEND_ASSERT(res_inputs->count > 0);
6609
0
          ir_refs_add(res_inputs, res_inputs->refs[res_inputs->count - 1]);
6610
0
        }
6611
0
        if (check_exception && (val_info & MAY_BE_UNDEF)) {
6612
0
          zend_jit_check_exception(jit);
6613
0
        }
6614
0
        ir_refs_add(end_inputs, ir_END());
6615
0
        ir_IF_TRUE(if_may_leak);
6616
0
      }
6617
0
      if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6618
0
        ZEND_ASSERT(jit->delay_refs == res_inputs);
6619
0
        ZEND_ASSERT(res_inputs->count > 0);
6620
0
        ir_refs_add(res_inputs, res_inputs->refs[res_inputs->count - 1]);
6621
0
      }
6622
0
      if (check_exception && (val_info & MAY_BE_UNDEF)) {
6623
0
        zend_jit_check_exception(jit);
6624
0
      }
6625
0
      ir_refs_add(end_inputs, ir_END());
6626
0
    } else /* if (RC_MAY_BE_N(var_info)) */ {
6627
0
      jit_GC_DELREF(jit, ref);
6628
0
      if (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT)) {
6629
0
        ir_ref if_may_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
6630
0
        ir_IF_FALSE(if_may_leak);
6631
0
        if (opline) {
6632
0
          jit_SET_EX_OPLINE(jit, opline);
6633
0
        }
6634
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
6635
0
        ir_END_list(simple_inputs);
6636
0
        ir_IF_TRUE(if_may_leak);
6637
0
      }
6638
0
      ir_END_list(simple_inputs);
6639
0
    }
6640
0
  }
6641
6642
0
  if (simple_inputs) {
6643
0
    ir_MERGE_list(simple_inputs);
6644
0
  }
6645
6646
0
  if (!done) {
6647
0
    if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, var_def_info, val_type, val_addr, val_info, res_addr, check_exception)) {
6648
0
      return 0;
6649
0
    }
6650
0
    if (end_inputs->count) {
6651
0
      ir_refs_add(end_inputs, ir_END());
6652
0
    }
6653
0
  }
6654
6655
0
  if (end_inputs->count) {
6656
0
    ir_MERGE_N(end_inputs->count, end_inputs->refs);
6657
0
  }
6658
6659
0
  if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6660
0
    ir_ref phi;
6661
6662
0
    ZEND_ASSERT(jit->delay_refs == res_inputs);
6663
0
    ZEND_ASSERT(end_inputs->count == res_inputs->count || (end_inputs->count == 0 && res_inputs->count == 1));
6664
0
    jit->delay_var = -1;
6665
0
    jit->delay_refs = NULL;
6666
0
    if (res_inputs->count == 1) {
6667
0
      phi = res_inputs->refs[0];
6668
0
    } else {
6669
0
      phi = ir_PHI_N((var_def_info & MAY_BE_LONG & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE,
6670
0
        res_inputs->count, res_inputs->refs);
6671
0
    }
6672
0
    if (Z_MODE(var_addr) == IS_REG) {
6673
0
      if ((var_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) || ref_addr) {
6674
0
        phi = ir_emit2(&jit->ctx, IR_OPT(IR_COPY, jit->ctx.ir_base[phi].type), phi, 1);
6675
0
      }
6676
0
      zend_jit_def_reg(jit, var_addr, phi);
6677
0
      if (real_res_addr) {
6678
0
        if (var_def_info & MAY_BE_LONG) {
6679
0
          jit_set_Z_LVAL(jit, real_res_addr, jit_Z_LVAL(jit, var_addr));
6680
0
        } else {
6681
0
          jit_set_Z_DVAL(jit, real_res_addr, jit_Z_DVAL(jit, var_addr));
6682
0
        }
6683
0
      }
6684
0
    } else {
6685
0
      zend_jit_def_reg(jit, res_addr, phi);
6686
0
    }
6687
0
  }
6688
6689
0
  return 1;
6690
0
}
6691
6692
static int zend_jit_qm_assign(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, zend_jit_addr op1_def_addr, uint32_t res_use_info, uint32_t res_info, zend_jit_addr res_addr)
6693
0
{
6694
0
  if (op1_addr != op1_def_addr) {
6695
0
    if (!zend_jit_update_regs(jit, opline->op1.var, op1_addr, op1_def_addr, op1_info)) {
6696
0
      return 0;
6697
0
    }
6698
0
    if (Z_MODE(op1_def_addr) == IS_REG && Z_MODE(op1_addr) != IS_REG) {
6699
0
      op1_addr = op1_def_addr;
6700
0
    }
6701
0
  }
6702
6703
0
  if (!zend_jit_simple_assign(jit, opline, res_addr, res_use_info, res_info, opline->op1_type, op1_addr, op1_info, 0, true)) {
6704
0
    return 0;
6705
0
  }
6706
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6707
0
    return 0;
6708
0
  }
6709
0
  return 1;
6710
0
}
6711
6712
static int zend_jit_assign(zend_jit_ctx  *jit,
6713
                           const zend_op *opline,
6714
                           uint32_t       op1_info,
6715
                           zend_jit_addr  op1_use_addr,
6716
                           uint32_t       op1_def_info,
6717
                           zend_jit_addr  op1_addr,
6718
                           uint32_t       op2_info,
6719
                           zend_jit_addr  op2_addr,
6720
                           zend_jit_addr  op2_def_addr,
6721
                           uint32_t       res_info,
6722
                           zend_jit_addr  res_addr,
6723
                           zend_jit_addr  ref_addr,
6724
                           int            may_throw)
6725
0
{
6726
0
  ZEND_ASSERT(opline->op1_type == IS_CV);
6727
6728
0
  if (op2_addr != op2_def_addr) {
6729
0
    if (!zend_jit_update_regs(jit, opline->op2.var, op2_addr, op2_def_addr, op2_info)) {
6730
0
      return 0;
6731
0
    }
6732
0
    if (Z_MODE(op2_def_addr) == IS_REG && Z_MODE(op2_addr) != IS_REG) {
6733
0
      op2_addr = op2_def_addr;
6734
0
    }
6735
0
  }
6736
6737
0
  if (Z_MODE(op1_addr) != IS_REG
6738
0
   && Z_MODE(op1_use_addr) == IS_REG
6739
0
   && !Z_LOAD(op1_use_addr)
6740
0
   && !Z_STORE(op1_use_addr)) {
6741
    /* Force type update */
6742
0
    op1_info |= MAY_BE_UNDEF;
6743
0
  }
6744
0
  if (!zend_jit_assign_to_variable(jit, opline, op1_use_addr, op1_addr, op1_info, op1_def_info,
6745
0
      opline->op2_type, op2_addr, op2_info, res_addr, ref_addr, may_throw)) {
6746
0
    return 0;
6747
0
  }
6748
0
  if (Z_MODE(op1_addr) == IS_REG) {
6749
0
    if (Z_STORE(op1_addr)) {
6750
0
      if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_addr, op1_def_info, op1_use_addr, op1_info)) {
6751
0
        return 0;
6752
0
      }
6753
0
    } else if ((op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
6754
0
      && Z_MODE(op1_use_addr) == IS_MEM_ZVAL
6755
0
      && Z_REG(op1_use_addr) == ZREG_FP
6756
0
      && EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)) < jit->current_op_array->last_var) {
6757
      /* We have to update type of CV because it may be captured by exception backtrace or released on RETURN */
6758
0
      if ((op1_def_info & MAY_BE_ANY) == MAY_BE_LONG) {
6759
0
        jit_set_Z_TYPE_INFO(jit, op1_use_addr, IS_LONG);
6760
0
        if (JIT_G(current_frame)) {
6761
0
          SET_STACK_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)), IS_LONG, 1);
6762
0
        }
6763
0
      } else if ((op1_def_info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
6764
0
        jit_set_Z_TYPE_INFO(jit, op1_use_addr, IS_DOUBLE);
6765
0
        if (JIT_G(current_frame)) {
6766
0
          SET_STACK_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)), IS_DOUBLE, 1);
6767
0
        }
6768
0
      } else {
6769
0
        ZEND_UNREACHABLE();
6770
0
      }
6771
0
    }
6772
0
  }
6773
0
  if (opline->result_type != IS_UNUSED) {
6774
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6775
0
      return 0;
6776
0
    }
6777
0
  }
6778
6779
0
  return 1;
6780
0
}
6781
6782
static ir_op zend_jit_cmp_op(const zend_op *opline)
6783
0
{
6784
0
  ir_op op;
6785
6786
0
  switch (opline->opcode) {
6787
0
    case ZEND_IS_EQUAL:
6788
0
    case ZEND_IS_IDENTICAL:
6789
0
    case ZEND_CASE:
6790
0
    case ZEND_CASE_STRICT:
6791
0
      op = IR_EQ;
6792
0
      break;
6793
0
    case ZEND_IS_NOT_EQUAL:
6794
0
    case ZEND_IS_NOT_IDENTICAL:
6795
0
      op = IR_NE;
6796
0
      break;
6797
0
    case ZEND_IS_SMALLER:
6798
0
      op = IR_LT;
6799
0
      break;
6800
0
    case ZEND_IS_SMALLER_OR_EQUAL:
6801
0
      op = IR_LE;
6802
0
      break;
6803
0
    default:
6804
0
      ZEND_UNREACHABLE();
6805
0
  }
6806
0
  return op;
6807
0
}
6808
6809
static ir_ref zend_jit_cmp_long_long(zend_jit_ctx   *jit,
6810
                                     const zend_op  *opline,
6811
                                     zend_ssa_range *op1_range,
6812
                                     zend_jit_addr   op1_addr,
6813
                                     zend_ssa_range *op2_range,
6814
                                     zend_jit_addr   op2_addr,
6815
                                     zend_jit_addr   res_addr,
6816
                                     uint8_t         smart_branch_opcode,
6817
                                     uint32_t        target_label,
6818
                                     uint32_t        target_label2,
6819
                                     const void     *exit_addr,
6820
                                     bool       skip_comparison)
6821
0
{
6822
0
  ir_ref ref;
6823
0
  bool result;
6824
6825
0
  if (zend_jit_is_constant_cmp_long_long(opline, op1_range, op1_addr, op2_range, op2_addr, &result)) {
6826
0
    if (!smart_branch_opcode ||
6827
0
        smart_branch_opcode == ZEND_JMPZ_EX ||
6828
0
        smart_branch_opcode == ZEND_JMPNZ_EX) {
6829
0
      jit_set_Z_TYPE_INFO(jit, res_addr, result ? IS_TRUE : IS_FALSE);
6830
0
    }
6831
0
    if (smart_branch_opcode && !exit_addr) {
6832
0
      if (smart_branch_opcode == ZEND_JMPZ ||
6833
0
          smart_branch_opcode == ZEND_JMPZ_EX) {
6834
0
        return jit_IF_ex(jit, IR_FALSE, result ? target_label : target_label2);
6835
0
      } else if (smart_branch_opcode == ZEND_JMPNZ ||
6836
0
                 smart_branch_opcode == ZEND_JMPNZ_EX) {
6837
0
        return jit_IF_ex(jit, IR_TRUE, result ? target_label : target_label2);
6838
0
      } else {
6839
0
        ZEND_UNREACHABLE();
6840
0
      }
6841
0
    }
6842
0
    if (opline->opcode != ZEND_IS_IDENTICAL
6843
0
     && opline->opcode != ZEND_IS_NOT_IDENTICAL
6844
0
     && opline->opcode != ZEND_CASE_STRICT) {
6845
0
      return ir_END();
6846
0
    } else {
6847
0
      return IR_NULL; /* success */
6848
0
    }
6849
0
  }
6850
6851
0
  ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_LVAL(jit, op1_addr), jit_Z_LVAL(jit, op2_addr));
6852
6853
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6854
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6855
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6856
0
  }
6857
0
  if (exit_addr) {
6858
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6859
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6860
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6861
0
      } else {
6862
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6863
0
      }
6864
0
    } else {
6865
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6866
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6867
0
      } else {
6868
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6869
0
      }
6870
0
    }
6871
0
  } else if (smart_branch_opcode) {
6872
0
    return jit_IF_ex(jit, ref,
6873
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6874
0
  }
6875
6876
0
  if (opline->opcode != ZEND_IS_IDENTICAL
6877
0
   && opline->opcode != ZEND_IS_NOT_IDENTICAL
6878
0
   && opline->opcode != ZEND_CASE_STRICT) {
6879
0
    return ir_END();
6880
0
  } else {
6881
0
    return IR_NULL; /* success */
6882
0
  }
6883
0
}
6884
6885
static ir_ref zend_jit_cmp_long_double(zend_jit_ctx *jit, const zend_op *opline, zend_jit_addr op1_addr, zend_jit_addr op2_addr, zend_jit_addr res_addr, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
6886
0
{
6887
0
  ir_ref ref = ir_CMP_OP(zend_jit_cmp_op(opline), ir_INT2D(jit_Z_LVAL(jit, op1_addr)), jit_Z_DVAL(jit, op2_addr));
6888
6889
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6890
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6891
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6892
0
  }
6893
0
  if (exit_addr) {
6894
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6895
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6896
0
    } else {
6897
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6898
0
    }
6899
0
  } else if (smart_branch_opcode) {
6900
0
    return jit_IF_ex(jit, ref,
6901
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6902
0
  }
6903
0
  return ir_END();
6904
0
}
6905
6906
static ir_ref zend_jit_cmp_double_long(zend_jit_ctx *jit, const zend_op *opline, zend_jit_addr op1_addr, zend_jit_addr op2_addr, zend_jit_addr res_addr, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
6907
0
{
6908
0
  ir_ref ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_DVAL(jit, op1_addr), ir_INT2D(jit_Z_LVAL(jit, op2_addr)));
6909
6910
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6911
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6912
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6913
0
  }
6914
0
  if (exit_addr) {
6915
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6916
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6917
0
    } else {
6918
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6919
0
    }
6920
0
  } else if (smart_branch_opcode) {
6921
0
    return jit_IF_ex(jit, ref,
6922
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6923
0
  }
6924
0
  return ir_END();
6925
0
}
6926
6927
static ir_ref zend_jit_cmp_double_double(zend_jit_ctx *jit, const zend_op *opline, zend_jit_addr op1_addr, zend_jit_addr op2_addr, zend_jit_addr res_addr, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
6928
0
{
6929
0
  ir_ref ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_DVAL(jit, op1_addr), jit_Z_DVAL(jit, op2_addr));
6930
6931
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6932
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6933
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6934
0
  }
6935
0
  if (exit_addr) {
6936
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6937
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6938
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6939
0
      } else {
6940
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6941
0
      }
6942
0
    } else {
6943
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6944
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6945
0
      } else {
6946
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6947
0
      }
6948
0
    }
6949
0
  } else if (smart_branch_opcode) {
6950
0
    return jit_IF_ex(jit, ref,
6951
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6952
0
  }
6953
0
  if (opline->opcode != ZEND_IS_IDENTICAL
6954
0
   && opline->opcode != ZEND_IS_NOT_IDENTICAL
6955
0
   && opline->opcode != ZEND_CASE_STRICT) {
6956
0
    return ir_END();
6957
0
  } else {
6958
0
    return IR_NULL; /* success */
6959
0
  }
6960
0
}
6961
6962
static ir_ref zend_jit_cmp_slow(zend_jit_ctx *jit, ir_ref ref, const zend_op *opline, zend_jit_addr res_addr, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
6963
0
{
6964
0
  ref = ir_CMP_OP(zend_jit_cmp_op(opline), ref, ir_CONST_I32(0));
6965
6966
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6967
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6968
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6969
0
  }
6970
0
  if (exit_addr) {
6971
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6972
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6973
0
    } else {
6974
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6975
0
    }
6976
0
  } else if (smart_branch_opcode) {
6977
0
    return jit_IF_ex(jit, ref,
6978
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6979
0
  }
6980
6981
0
  return ir_END();
6982
0
}
6983
6984
static int zend_jit_cmp(zend_jit_ctx   *jit,
6985
                        const zend_op  *opline,
6986
                        uint32_t        op1_info,
6987
                        zend_ssa_range *op1_range,
6988
                        zend_jit_addr   op1_addr,
6989
                        uint32_t        op2_info,
6990
                        zend_ssa_range *op2_range,
6991
                        zend_jit_addr   op2_addr,
6992
                        zend_jit_addr   res_addr,
6993
                        int             may_throw,
6994
                        uint8_t         smart_branch_opcode,
6995
                        uint32_t        target_label,
6996
                        uint32_t        target_label2,
6997
                        const void     *exit_addr,
6998
                        bool       skip_comparison)
6999
0
{
7000
0
  ir_ref ref = IR_UNUSED;
7001
0
  ir_ref if_op1_long = IR_UNUSED;
7002
0
  ir_ref if_op1_double = IR_UNUSED;
7003
0
  ir_ref if_op2_double = IR_UNUSED;
7004
0
  ir_ref if_op1_long_op2_long = IR_UNUSED;
7005
0
  ir_ref if_op1_long_op2_double = IR_UNUSED;
7006
0
  ir_ref if_op1_double_op2_double = IR_UNUSED;
7007
0
  ir_ref if_op1_double_op2_long = IR_UNUSED;
7008
0
  ir_ref slow_inputs = IR_UNUSED;
7009
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
7010
0
  bool has_slow =
7011
0
    (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
7012
0
    (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
7013
0
    ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
7014
0
     (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))));
7015
0
  ir_refs *end_inputs;
7016
7017
0
  ir_refs_init(end_inputs, 8);
7018
7019
0
  if (Z_MODE(op1_addr) == IS_REG) {
7020
0
    if (!has_concrete_type(op2_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
7021
      /* Force load */
7022
0
      zend_jit_use_reg(jit, op1_addr);
7023
0
    }
7024
0
  } else if (Z_MODE(op2_addr) == IS_REG) {
7025
0
    if (!has_concrete_type(op1_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
7026
      /* Force load */
7027
0
      zend_jit_use_reg(jit, op2_addr);
7028
0
    }
7029
0
  }
7030
7031
0
  if ((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG)) {
7032
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
7033
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
7034
0
      ir_IF_TRUE(if_op1_long);
7035
0
    }
7036
0
    if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
7037
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7038
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
7039
0
      if (op2_info & MAY_BE_DOUBLE) {
7040
0
        if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7041
0
          if_op1_long_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7042
0
          ir_IF_FALSE_cold(if_op1_long_op2_double);
7043
0
          ir_END_list(slow_inputs);
7044
0
          ir_IF_TRUE(if_op1_long_op2_double);
7045
0
        }
7046
0
        ref = zend_jit_cmp_long_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7047
0
        if (!ref) {
7048
0
          return 0;
7049
0
        }
7050
0
        ir_refs_add(end_inputs, ref);
7051
0
      } else {
7052
0
        ir_END_list(slow_inputs);
7053
0
      }
7054
0
      ir_IF_TRUE(if_op1_long_op2_long);
7055
0
    }
7056
0
    ref = zend_jit_cmp_long_long(jit, opline, op1_range, op1_addr, op2_range, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr, skip_comparison);
7057
0
    if (!ref) {
7058
0
      return 0;
7059
0
    }
7060
0
    ir_refs_add(end_inputs, ref);
7061
7062
0
    if (if_op1_long) {
7063
0
      ir_IF_FALSE_cold(if_op1_long);
7064
0
    }
7065
0
    if (op1_info & MAY_BE_DOUBLE) {
7066
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7067
0
        if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7068
0
        ir_IF_FALSE_cold(if_op1_double);
7069
0
        ir_END_list(slow_inputs);
7070
0
        ir_IF_TRUE(if_op1_double);
7071
0
      }
7072
0
      if (op2_info & MAY_BE_DOUBLE) {
7073
0
        if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7074
0
          if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7075
0
          ir_IF_TRUE(if_op1_double_op2_double);
7076
0
        }
7077
0
        ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7078
0
        if (!ref) {
7079
0
          return 0;
7080
0
        }
7081
0
        ir_refs_add(end_inputs, ref);
7082
0
        if (if_op1_double_op2_double) {
7083
0
          ir_IF_FALSE_cold(if_op1_double_op2_double);
7084
0
        }
7085
0
      }
7086
0
      if (!same_ops) {
7087
0
        if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7088
0
          if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7089
0
          ir_IF_FALSE_cold(if_op1_double_op2_long);
7090
0
          ir_END_list(slow_inputs);
7091
0
          ir_IF_TRUE(if_op1_double_op2_long);
7092
0
        }
7093
0
        ref = zend_jit_cmp_double_long(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7094
0
        if (!ref) {
7095
0
          return 0;
7096
0
        }
7097
0
        ir_refs_add(end_inputs, ref);
7098
0
      } else if (if_op1_double_op2_double) {
7099
0
        ir_END_list(slow_inputs);
7100
0
      }
7101
0
    } else if (if_op1_long) {
7102
0
      ir_END_list(slow_inputs);
7103
0
    }
7104
0
  } else if ((op1_info & MAY_BE_DOUBLE) &&
7105
0
             !(op1_info & MAY_BE_LONG) &&
7106
0
             (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
7107
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE)) {
7108
0
      if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7109
0
      ir_IF_FALSE_cold(if_op1_double);
7110
0
      ir_END_list(slow_inputs);
7111
0
      ir_IF_TRUE(if_op1_double);
7112
0
    }
7113
0
    if (op2_info & MAY_BE_DOUBLE) {
7114
0
      if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7115
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7116
0
        ir_IF_TRUE(if_op1_double_op2_double);
7117
0
      }
7118
0
      ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7119
0
      if (!ref) {
7120
0
        return 0;
7121
0
      }
7122
0
      ir_refs_add(end_inputs, ref);
7123
0
      if (if_op1_double_op2_double) {
7124
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
7125
0
      }
7126
0
    }
7127
0
    if (!same_ops && (op2_info & MAY_BE_LONG)) {
7128
0
      if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
7129
0
        if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7130
0
        ir_IF_FALSE_cold(if_op1_double_op2_long);
7131
0
        ir_END_list(slow_inputs);
7132
0
        ir_IF_TRUE(if_op1_double_op2_long);
7133
0
      }
7134
0
      ref = zend_jit_cmp_double_long(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7135
0
      if (!ref) {
7136
0
        return 0;
7137
0
      }
7138
0
      ir_refs_add(end_inputs, ref);
7139
0
    } else if (if_op1_double_op2_double) {
7140
0
      ir_END_list(slow_inputs);
7141
0
    }
7142
0
  } else if ((op2_info & MAY_BE_DOUBLE) &&
7143
0
             !(op2_info & MAY_BE_LONG) &&
7144
0
             (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
7145
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE)) {
7146
0
      if_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7147
0
      ir_IF_FALSE_cold(if_op2_double);
7148
0
      ir_END_list(slow_inputs);
7149
0
      ir_IF_TRUE(if_op2_double);
7150
0
    }
7151
0
    if (op1_info & MAY_BE_DOUBLE) {
7152
0
      if (!same_ops && (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7153
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7154
0
        ir_IF_TRUE(if_op1_double_op2_double);
7155
0
      }
7156
0
      ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7157
0
      if (!ref) {
7158
0
        return 0;
7159
0
      }
7160
0
      ir_refs_add(end_inputs, ref);
7161
0
      if (if_op1_double_op2_double) {
7162
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
7163
0
      }
7164
0
    }
7165
0
    if (!same_ops && (op1_info & MAY_BE_LONG)) {
7166
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
7167
0
        if_op1_long_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
7168
0
        ir_IF_FALSE_cold(if_op1_long_op2_double);
7169
0
        ir_END_list(slow_inputs);
7170
0
        ir_IF_TRUE(if_op1_long_op2_double);
7171
0
      }
7172
0
      ref = zend_jit_cmp_long_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7173
0
      if (!ref) {
7174
0
        return 0;
7175
0
      }
7176
0
      ir_refs_add(end_inputs, ref);
7177
0
    } else if (if_op1_double_op2_double) {
7178
0
      ir_END_list(slow_inputs);
7179
0
    }
7180
0
  }
7181
7182
0
  if (has_slow ||
7183
0
      (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
7184
0
      (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE)))) {
7185
0
      ir_ref op1, op2, ref;
7186
7187
0
    if (slow_inputs) {
7188
0
      ir_MERGE_list(slow_inputs);
7189
0
    }
7190
0
    jit_SET_EX_OPLINE(jit, opline);
7191
7192
0
    if (Z_MODE(op1_addr) == IS_REG) {
7193
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7194
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7195
0
        return 0;
7196
0
      }
7197
0
      op1_addr = real_addr;
7198
0
    }
7199
0
    if (Z_MODE(op2_addr) == IS_REG) {
7200
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7201
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7202
0
        return 0;
7203
0
      }
7204
0
      op2_addr = real_addr;
7205
0
    }
7206
7207
0
    op1 = jit_ZVAL_ADDR(jit, op1_addr);
7208
0
    if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7209
0
      op1 = zend_jit_zval_check_undef(jit, op1, opline->op1.var, NULL, false);
7210
0
    }
7211
0
    op2 = jit_ZVAL_ADDR(jit, op2_addr);
7212
0
    if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
7213
0
      op2 = zend_jit_zval_check_undef(jit, op2, opline->op2.var, NULL, false);
7214
0
    }
7215
0
    ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_compare), op1, op2);
7216
0
    if (opline->opcode != ZEND_CASE) {
7217
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7218
0
    }
7219
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
7220
0
    if (may_throw) {
7221
0
      zend_jit_check_exception_undef_result(jit, opline);
7222
0
    }
7223
7224
0
    ref = zend_jit_cmp_slow(jit, ref, opline, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7225
0
    if (!ref) {
7226
0
      return 0;
7227
0
    }
7228
0
    ir_refs_add(end_inputs, ref);
7229
0
  }
7230
7231
0
  if (end_inputs->count) {
7232
0
    uint32_t n = end_inputs->count;
7233
7234
0
    if (smart_branch_opcode && !exit_addr) {
7235
0
      zend_basic_block *bb;
7236
0
      ir_ref ref;
7237
0
      uint32_t label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7238
0
        target_label2 : target_label;
7239
0
      uint32_t label2 = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7240
0
        target_label : target_label2;
7241
7242
0
      ZEND_ASSERT(jit->b >= 0);
7243
0
      bb = &jit->ssa->cfg.blocks[jit->b];
7244
0
      ZEND_ASSERT(bb->successors_count == 2);
7245
7246
0
      if (UNEXPECTED(bb->successors[0] == bb->successors[1])) {
7247
0
        ir_ref merge_inputs = IR_UNUSED;
7248
7249
0
        while (n) {
7250
0
          n--;
7251
0
          ir_IF_TRUE(end_inputs->refs[n]);
7252
0
          ir_END_list(merge_inputs);
7253
0
          ir_IF_FALSE(end_inputs->refs[n]);
7254
0
          ir_END_list(merge_inputs);
7255
0
        }
7256
0
        ir_MERGE_list(merge_inputs);
7257
0
        _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7258
0
      } else if (n == 1) {
7259
0
        ref = end_inputs->refs[0];
7260
0
        _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
7261
0
        _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
7262
0
      } else {
7263
0
        ir_ref true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7264
7265
0
        while (n) {
7266
0
          n--;
7267
0
          jit_IF_TRUE_FALSE_ex(jit, end_inputs->refs[n], label);
7268
0
          ir_END_list(true_inputs);
7269
0
          jit_IF_TRUE_FALSE_ex(jit, end_inputs->refs[n], label2);
7270
0
          ir_END_list(false_inputs);
7271
0
        }
7272
0
        ir_MERGE_list(true_inputs);
7273
0
        _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7274
0
        ir_MERGE_list(false_inputs);
7275
0
        _zend_jit_add_predecessor_ref(jit, label2, jit->b, ir_END());
7276
0
      }
7277
0
      jit->b = -1;
7278
0
    } else {
7279
0
      ir_MERGE_N(n, end_inputs->refs);
7280
0
    }
7281
0
  } else if (smart_branch_opcode && !exit_addr) {
7282
    /* dead code */
7283
0
    _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
7284
0
    jit->b = -1;
7285
0
  }
7286
7287
0
  return 1;
7288
0
}
7289
7290
static int zend_jit_identical(zend_jit_ctx   *jit,
7291
                              const zend_op  *opline,
7292
                              uint32_t        op1_info,
7293
                              zend_ssa_range *op1_range,
7294
                              zend_jit_addr   op1_addr,
7295
                              uint32_t        op2_info,
7296
                              zend_ssa_range *op2_range,
7297
                              zend_jit_addr   op2_addr,
7298
                              zend_jit_addr   res_addr,
7299
                              int             may_throw,
7300
                              uint8_t         smart_branch_opcode,
7301
                              uint32_t        target_label,
7302
                              uint32_t        target_label2,
7303
                              const void     *exit_addr,
7304
                              bool       skip_comparison)
7305
0
{
7306
0
  bool always_false = false, always_true = false;
7307
0
  ir_ref ref = IR_UNUSED;
7308
7309
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7310
0
    ir_ref op1 = jit_ZVAL_ADDR(jit, op1_addr);
7311
0
    op1 = zend_jit_zval_check_undef(jit, op1, opline->op1.var, opline, false);
7312
0
    op1_info |= MAY_BE_NULL;
7313
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1);
7314
0
  }
7315
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
7316
0
    ir_ref op2 = jit_ZVAL_ADDR(jit, op2_addr);
7317
0
    op2 = zend_jit_zval_check_undef(jit, op2, opline->op2.var, opline, false);
7318
0
    op2_info |= MAY_BE_NULL;
7319
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2);
7320
0
  }
7321
7322
0
  if ((op1_info & op2_info & MAY_BE_ANY) == 0) {
7323
0
    always_false = true;
7324
0
  } else if (has_concrete_type(op1_info)
7325
0
   && has_concrete_type(op2_info)
7326
0
   && concrete_type(op1_info) == concrete_type(op2_info)
7327
0
   && concrete_type(op1_info) <= IS_TRUE) {
7328
0
    always_true = true;
7329
0
  } else if (Z_MODE(op1_addr) == IS_CONST_ZVAL && Z_MODE(op2_addr) == IS_CONST_ZVAL) {
7330
0
    if (zend_is_identical(Z_ZV(op1_addr), Z_ZV(op2_addr))) {
7331
0
      always_true = true;
7332
0
    } else {
7333
0
      always_false = true;
7334
0
    }
7335
0
  }
7336
7337
0
  if (always_true) {
7338
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7339
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7340
0
    }
7341
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
7342
0
    if (!smart_branch_opcode
7343
0
     || smart_branch_opcode == ZEND_JMPZ_EX
7344
0
     || smart_branch_opcode == ZEND_JMPNZ_EX) {
7345
0
      jit_set_Z_TYPE_INFO(jit, res_addr, opline->opcode != ZEND_IS_NOT_IDENTICAL ? IS_TRUE : IS_FALSE);
7346
0
    }
7347
0
    if (may_throw) {
7348
0
      zend_jit_check_exception(jit);
7349
0
    }
7350
0
    if (exit_addr) {
7351
0
      if (smart_branch_opcode == ZEND_JMPNZ || smart_branch_opcode == ZEND_JMPNZ_EX) {
7352
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7353
0
      }
7354
0
    } else if (smart_branch_opcode) {
7355
0
      uint32_t label;
7356
7357
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7358
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7359
0
          target_label : target_label2;
7360
0
      } else {
7361
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7362
0
          target_label2 : target_label;
7363
0
      }
7364
0
      _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7365
0
      jit->b = -1;
7366
0
    }
7367
0
    return 1;
7368
0
  } else if (always_false) {
7369
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7370
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7371
0
    }
7372
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
7373
0
    if (!smart_branch_opcode
7374
0
     || smart_branch_opcode == ZEND_JMPZ_EX
7375
0
     || smart_branch_opcode == ZEND_JMPNZ_EX) {
7376
0
      jit_set_Z_TYPE_INFO(jit, res_addr, opline->opcode != ZEND_IS_NOT_IDENTICAL ? IS_FALSE : IS_TRUE);
7377
0
    }
7378
0
    if (may_throw) {
7379
0
      zend_jit_check_exception(jit);
7380
0
    }
7381
0
    if (exit_addr) {
7382
0
      if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
7383
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7384
0
      }
7385
0
    } else if (smart_branch_opcode) {
7386
0
      uint32_t label;
7387
7388
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7389
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7390
0
          target_label2 : target_label;
7391
0
      } else {
7392
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7393
0
          target_label : target_label2;
7394
0
      }
7395
0
      _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7396
0
      jit->b = -1;
7397
0
    }
7398
0
    return 1;
7399
0
  }
7400
7401
0
  if ((opline->op1_type & (IS_CV|IS_VAR)) && (op1_info & MAY_BE_REF)) {
7402
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
7403
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7404
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
7405
0
  }
7406
0
  if ((opline->op2_type & (IS_CV|IS_VAR)) && (op2_info & MAY_BE_REF)) {
7407
0
    ref = jit_ZVAL_ADDR(jit, op2_addr);
7408
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7409
0
    op2_addr = ZEND_ADDR_REF_ZVAL(ref);
7410
0
  }
7411
7412
0
  if ((op1_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG &&
7413
0
      (op2_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
7414
0
    ref = zend_jit_cmp_long_long(jit, opline, op1_range, op1_addr, op2_range, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr, skip_comparison);
7415
0
    if (!ref) {
7416
0
      return 0;
7417
0
    }
7418
0
  } else if ((op1_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE &&
7419
0
             (op2_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE) {
7420
0
    ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7421
0
    if (!ref) {
7422
0
      return 0;
7423
0
    }
7424
0
  } else {
7425
0
    if (opline->op1_type != IS_CONST) {
7426
0
      if (Z_MODE(op1_addr) == IS_REG) {
7427
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7428
0
        if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7429
0
          return 0;
7430
0
        }
7431
0
        op1_addr = real_addr;
7432
0
      }
7433
0
    }
7434
0
    if (opline->op2_type != IS_CONST) {
7435
0
      if (Z_MODE(op2_addr) == IS_REG) {
7436
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7437
0
        if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7438
0
          return 0;
7439
0
        }
7440
0
      }
7441
0
    }
7442
7443
0
    if (Z_MODE(op1_addr) == IS_CONST_ZVAL && Z_TYPE_P(Z_ZV(op1_addr)) <= IS_TRUE) {
7444
0
      zval *val = Z_ZV(op1_addr);
7445
7446
0
      ref = ir_EQ(jit_Z_TYPE(jit, op2_addr), ir_CONST_U8(Z_TYPE_P(val)));
7447
0
    } else if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_TYPE_P(Z_ZV(op2_addr)) <= IS_TRUE) {
7448
0
      zval *val = Z_ZV(op2_addr);
7449
7450
0
      ref = ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(Z_TYPE_P(val)));
7451
0
    } else {
7452
0
      if (Z_MODE(op1_addr) == IS_REG) {
7453
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7454
0
        if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7455
0
          return 0;
7456
0
        }
7457
0
        op1_addr = real_addr;
7458
0
      }
7459
0
      if (Z_MODE(op2_addr) == IS_REG) {
7460
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7461
0
        if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7462
0
          return 0;
7463
0
        }
7464
0
        op2_addr = real_addr;
7465
0
      }
7466
0
      if (may_throw) {
7467
0
        jit_SET_EX_OPLINE(jit, opline);
7468
0
      }
7469
7470
0
      ref = ir_CALL_2(IR_BOOL, ir_CONST_FC_FUNC(zend_is_identical),
7471
0
        jit_ZVAL_ADDR(jit, op1_addr),
7472
0
        jit_ZVAL_ADDR(jit, op2_addr));
7473
0
    }
7474
7475
0
    if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
7476
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7477
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7478
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7479
0
      } else {
7480
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7481
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7482
0
      }
7483
0
    }
7484
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7485
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7486
0
    }
7487
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
7488
0
    if (may_throw) {
7489
0
      zend_jit_check_exception_undef_result(jit, opline);
7490
0
    }
7491
0
    if (exit_addr) {
7492
0
      if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
7493
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7494
0
      } else {
7495
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7496
0
      }
7497
0
    } else if (smart_branch_opcode) {
7498
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7499
        /* swap labels */
7500
0
        uint32_t tmp = target_label;
7501
0
        target_label = target_label2;
7502
0
        target_label2 = tmp;
7503
0
      }
7504
0
      ref = jit_IF_ex(jit, ref,
7505
0
        (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
7506
0
    }
7507
0
  }
7508
7509
0
  if (smart_branch_opcode && !exit_addr) {
7510
0
    zend_basic_block *bb;
7511
7512
0
    ZEND_ASSERT(jit->b >= 0);
7513
0
    bb = &jit->ssa->cfg.blocks[jit->b];
7514
0
    ZEND_ASSERT(bb->successors_count == 2);
7515
7516
0
    if (bb->successors_count == 2 && bb->successors[0] == bb->successors[1]) {
7517
0
      ir_IF_TRUE(ref);
7518
0
      ir_MERGE_WITH_EMPTY_FALSE(ref);
7519
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
7520
0
    } else {
7521
0
      ZEND_ASSERT(bb->successors_count == 2);
7522
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
7523
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
7524
0
    }
7525
0
    jit->b = -1;
7526
0
  }
7527
7528
0
  return 1;
7529
0
}
7530
7531
static int zend_jit_bool_jmpznz(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, zend_jit_addr res_addr, uint32_t target_label, uint32_t target_label2, int may_throw, uint8_t branch_opcode, const void *exit_addr)
7532
0
{
7533
0
  uint32_t true_label = -1;
7534
0
  uint32_t false_label = -1;
7535
0
  bool set_bool = false;
7536
0
  bool set_bool_not = false;
7537
0
  bool always_true = false, always_false = false;
7538
0
  ir_ref ref, end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7539
0
  ir_type type = IR_UNUSED;
7540
7541
0
  if (branch_opcode == ZEND_BOOL) {
7542
0
    set_bool = true;
7543
0
  } else if (branch_opcode == ZEND_BOOL_NOT) {
7544
0
    set_bool = true;
7545
0
    set_bool_not = true;
7546
0
  } else if (branch_opcode == ZEND_JMPZ) {
7547
0
    true_label = target_label2;
7548
0
    false_label = target_label;
7549
0
  } else if (branch_opcode == ZEND_JMPNZ) {
7550
0
    true_label = target_label;
7551
0
    false_label = target_label2;
7552
0
  } else if (branch_opcode == ZEND_JMPZ_EX) {
7553
0
    set_bool = true;
7554
0
    true_label = target_label2;
7555
0
    false_label = target_label;
7556
0
  } else if (branch_opcode == ZEND_JMPNZ_EX) {
7557
0
    set_bool = true;
7558
0
    true_label = target_label;
7559
0
    false_label = target_label2;
7560
0
  } else {
7561
0
    ZEND_UNREACHABLE();
7562
0
  }
7563
7564
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_REF)) {
7565
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
7566
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7567
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
7568
0
  }
7569
7570
0
  if (Z_MODE(op1_addr) == IS_CONST_ZVAL
7571
    /* NAN Value must cause a warning to be emitted */
7572
0
    && (Z_TYPE_P(Z_ZV(op1_addr)) != IS_DOUBLE || !zend_isnan(Z_DVAL_P(Z_ZV(op1_addr))))) {
7573
0
    if (zend_is_true(Z_ZV(op1_addr))) {
7574
0
      always_true = true;
7575
0
    } else {
7576
0
      always_false = true;
7577
0
    }
7578
0
  } else if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE)) {
7579
0
    if (!(op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)-MAY_BE_TRUE))) {
7580
0
      always_true = true;
7581
0
    } else if (!(op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE)))) {
7582
0
      if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7583
0
        ref = jit_ZVAL_ADDR(jit, op1_addr);
7584
0
        zend_jit_zval_check_undef(jit, ref, opline->op1.var, opline, false);
7585
0
      }
7586
0
      always_false = true;
7587
0
    }
7588
0
  }
7589
7590
0
  if (always_true) {
7591
0
    if (set_bool) {
7592
0
      jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7593
0
    }
7594
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7595
0
    if (may_throw) {
7596
0
      zend_jit_check_exception(jit);
7597
0
    }
7598
0
    if (true_label != (uint32_t)-1) {
7599
0
      ZEND_ASSERT(exit_addr == NULL);
7600
0
      _zend_jit_add_predecessor_ref(jit, true_label, jit->b, ir_END());
7601
0
      jit->b = -1;
7602
0
    }
7603
0
    return 1;
7604
0
  } else if (always_false) {
7605
0
    if (set_bool) {
7606
0
      jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7607
0
    }
7608
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7609
0
    if (may_throw) {
7610
0
      zend_jit_check_exception(jit);
7611
0
    }
7612
0
    if (false_label != (uint32_t)-1) {
7613
0
      ZEND_ASSERT(exit_addr == NULL);
7614
0
      _zend_jit_add_predecessor_ref(jit, false_label, jit->b, ir_END());
7615
0
      jit->b = -1;
7616
0
    }
7617
0
    return 1;
7618
0
  }
7619
7620
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE)) {
7621
0
    type = jit_Z_TYPE(jit, op1_addr);
7622
0
    if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
7623
0
      ir_ref if_type = ir_IF(ir_LT(type, ir_CONST_U8(IS_TRUE)));
7624
7625
0
      ir_IF_TRUE_cold(if_type);
7626
7627
0
      if (op1_info & MAY_BE_UNDEF) {
7628
0
        zend_jit_type_check_undef(jit,
7629
0
          type,
7630
0
          opline->op1.var,
7631
0
          opline, true, false, true);
7632
0
      }
7633
0
      if (set_bool) {
7634
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7635
0
      }
7636
0
      if (exit_addr) {
7637
0
        if (branch_opcode == ZEND_JMPNZ || branch_opcode == ZEND_JMPNZ_EX) {
7638
0
          ir_END_list(end_inputs);
7639
0
        } else {
7640
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7641
0
        }
7642
0
      } else if (false_label != (uint32_t)-1) {
7643
0
        ir_END_list(false_inputs);
7644
0
      } else {
7645
0
        ir_END_list(end_inputs);
7646
0
      }
7647
0
      ir_IF_FALSE(if_type);
7648
0
    }
7649
7650
0
    if (op1_info & MAY_BE_TRUE) {
7651
0
      ir_ref if_type = IR_UNUSED;
7652
7653
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
7654
0
        if_type = ir_IF(ir_EQ(type, ir_CONST_U8(IS_TRUE)));
7655
7656
0
        ir_IF_TRUE(if_type);
7657
0
      }
7658
0
      if (set_bool) {
7659
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7660
0
      }
7661
0
      if (exit_addr) {
7662
0
        if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7663
0
          ir_END_list(end_inputs);
7664
0
        } else {
7665
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7666
0
        }
7667
0
      } else if (true_label != (uint32_t)-1) {
7668
0
        ir_END_list(true_inputs);
7669
0
      } else {
7670
0
        ir_END_list(end_inputs);
7671
0
      }
7672
0
      if (if_type) {
7673
0
        ir_IF_FALSE(if_type);
7674
0
      }
7675
0
    }
7676
0
  }
7677
7678
0
  if (op1_info & MAY_BE_LONG) {
7679
0
    ir_ref if_long = IR_UNUSED;
7680
0
    ir_ref ref;
7681
7682
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG))) {
7683
0
      if (!type) {
7684
0
        type = jit_Z_TYPE(jit, op1_addr);
7685
0
      }
7686
0
      if_long = ir_IF(ir_EQ(type, ir_CONST_U8(IS_LONG)));
7687
0
      ir_IF_TRUE(if_long);
7688
0
    }
7689
0
    ref = jit_Z_LVAL(jit, op1_addr);
7690
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7691
0
      ref = ir_NE(ref, ir_CONST_LONG(0));
7692
0
      if (set_bool_not) {
7693
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7694
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7695
0
      } else {
7696
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7697
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7698
0
      }
7699
0
      ir_END_list(end_inputs);
7700
0
    } else if (exit_addr) {
7701
0
      if (set_bool) {
7702
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7703
0
          ir_ADD_U32(ir_ZEXT_U32(ir_NE(ref, ir_CONST_LONG(0))), ir_CONST_U32(IS_FALSE)));
7704
0
      }
7705
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7706
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7707
0
      } else {
7708
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7709
0
      }
7710
0
      ir_END_list(end_inputs);
7711
0
    } else {
7712
0
      ir_ref if_val = ir_IF(ref);
7713
0
      ir_IF_TRUE(if_val);
7714
0
      if (set_bool) {
7715
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7716
0
      }
7717
0
      ir_END_list(true_inputs);
7718
0
      ir_IF_FALSE(if_val);
7719
0
      if (set_bool) {
7720
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7721
0
      }
7722
0
      ir_END_list(false_inputs);
7723
0
    }
7724
0
    if (if_long) {
7725
0
      ir_IF_FALSE(if_long);
7726
0
    }
7727
0
  }
7728
7729
0
  if (op1_info & MAY_BE_DOUBLE) {
7730
0
    ir_ref if_double = IR_UNUSED;
7731
0
    ir_ref ref;
7732
7733
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))) {
7734
0
      if (!type) {
7735
0
        type = jit_Z_TYPE(jit, op1_addr);
7736
0
      }
7737
0
      if_double = ir_IF(ir_EQ(type, ir_CONST_U8(IS_DOUBLE)));
7738
0
      ir_IF_TRUE(if_double);
7739
0
    }
7740
7741
0
    ir_ref dval = jit_Z_DVAL(jit, op1_addr);
7742
0
    ir_ref is_nan = ir_NE(dval, dval);
7743
0
    ir_ref if_val = ir_IF(is_nan);
7744
0
    ir_IF_TRUE_cold(if_val);
7745
0
    jit_SET_EX_OPLINE(jit, opline);
7746
0
    ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_nan_coerced_to_type_warning));
7747
0
    ir_MERGE_WITH_EMPTY_FALSE(if_val);
7748
7749
0
    ref = ir_NE(dval, ir_CONST_DOUBLE(0.0));
7750
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7751
0
      if (set_bool_not) {
7752
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7753
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7754
0
      } else {
7755
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7756
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7757
0
      }
7758
0
      ir_END_list(end_inputs);
7759
0
    } else if (exit_addr) {
7760
0
        if (set_bool) {
7761
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7762
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7763
0
        }
7764
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7765
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7766
0
      } else {
7767
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7768
0
      }
7769
0
      ir_END_list(end_inputs);
7770
0
    } else {
7771
0
      ir_ref if_val = ir_IF(ref);
7772
0
      ir_IF_TRUE(if_val);
7773
0
      if (set_bool) {
7774
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7775
0
      }
7776
0
      ir_END_list(true_inputs);
7777
0
      ir_IF_FALSE(if_val);
7778
0
      if (set_bool) {
7779
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7780
0
      }
7781
0
      ir_END_list(false_inputs);
7782
0
    }
7783
0
    if (if_double) {
7784
0
      ir_IF_FALSE(if_double);
7785
0
    }
7786
0
  }
7787
7788
0
  if (op1_info & (MAY_BE_ANY - (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))) {
7789
0
    jit_SET_EX_OPLINE(jit, opline);
7790
0
    ref = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_is_true), jit_ZVAL_ADDR(jit, op1_addr));
7791
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7792
0
    if (may_throw) {
7793
0
      zend_jit_check_exception_undef_result(jit, opline);
7794
0
    }
7795
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7796
0
      if (set_bool_not) {
7797
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7798
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7799
0
      } else {
7800
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7801
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7802
0
      }
7803
0
      if (end_inputs) {
7804
0
        ir_END_list(end_inputs);
7805
0
      }
7806
0
    } else if (exit_addr) {
7807
0
      if (set_bool) {
7808
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7809
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7810
0
      }
7811
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7812
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7813
0
      } else {
7814
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7815
0
      }
7816
0
      if (end_inputs) {
7817
0
        ir_END_list(end_inputs);
7818
0
      }
7819
0
    } else {
7820
0
      ir_ref if_val = ir_IF(ref);
7821
0
      ir_IF_TRUE(if_val);
7822
0
      if (set_bool) {
7823
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7824
0
      }
7825
0
      ir_END_list(true_inputs);
7826
0
      ir_IF_FALSE(if_val);
7827
0
      if (set_bool) {
7828
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7829
0
      }
7830
0
      ir_END_list(false_inputs);
7831
0
    }
7832
0
  }
7833
7834
0
  if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT || exit_addr) {
7835
0
    if (end_inputs) {
7836
0
      ir_MERGE_list(end_inputs);
7837
0
    }
7838
0
  } else {
7839
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
7840
0
  }
7841
7842
0
  return 1;
7843
0
}
7844
7845
static int zend_jit_defined(zend_jit_ctx *jit, const zend_op *opline, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
7846
0
{
7847
0
  uint32_t defined_label = (uint32_t)-1;
7848
0
  uint32_t undefined_label = (uint32_t)-1;
7849
0
  zval *zv = RT_CONSTANT(opline, opline->op1);
7850
0
  zend_jit_addr res_addr = 0;
7851
0
  ir_ref ref, ref2, if_set, if_zero, if_set2;
7852
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7853
7854
0
  if (smart_branch_opcode && !exit_addr) {
7855
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7856
0
      defined_label = target_label2;
7857
0
      undefined_label = target_label;
7858
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
7859
0
      defined_label = target_label;
7860
0
      undefined_label = target_label2;
7861
0
    } else {
7862
0
      ZEND_UNREACHABLE();
7863
0
    }
7864
0
  } else {
7865
0
    res_addr = RES_ADDR();
7866
0
  }
7867
7868
  // if (CACHED_PTR(opline->extended_value)) {
7869
0
  ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value));
7870
7871
0
  if_set = ir_IF(ref);
7872
7873
0
  ir_IF_FALSE_cold(if_set);
7874
0
  if_zero = ir_END();
7875
7876
0
  ir_IF_TRUE(if_set);
7877
0
  if_set2 = ir_IF(ir_AND_A(ref, ir_CONST_ADDR(CACHE_SPECIAL)));
7878
0
  ir_IF_FALSE(if_set2);
7879
7880
0
  if (exit_addr) {
7881
0
    if (smart_branch_opcode == ZEND_JMPNZ) {
7882
0
      jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7883
0
    } else {
7884
0
      ir_END_list(end_inputs);
7885
0
    }
7886
0
  } else if (smart_branch_opcode) {
7887
0
    ir_END_list(true_inputs);
7888
0
  } else {
7889
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
7890
0
    ir_END_list(end_inputs);
7891
0
  }
7892
7893
0
  ir_IF_TRUE_cold(if_set2);
7894
7895
0
  ref2 = jit_EG(zend_constants);
7896
0
  ref = ir_SHR_A(ref, ir_CONST_ADDR(1));
7897
0
  if (sizeof(void*) == 8) {
7898
0
    ref = ir_TRUNC_U32(ref);
7899
0
  }
7900
0
  ref2 = ir_EQ(ref, ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(ref2), offsetof(HashTable, nNumOfElements))));
7901
0
  ref2 = ir_IF(ref2);
7902
0
  ir_IF_TRUE(ref2);
7903
7904
0
  if (exit_addr) {
7905
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7906
0
      jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7907
0
    } else {
7908
0
      ir_END_list(end_inputs);
7909
0
    }
7910
0
  } else if (smart_branch_opcode) {
7911
0
    ir_END_list(false_inputs);
7912
0
  } else {
7913
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
7914
0
    ir_END_list(end_inputs);
7915
0
  }
7916
7917
0
  ir_IF_FALSE(ref2);
7918
0
  ir_MERGE_2(if_zero, ir_END());
7919
7920
0
  jit_SET_EX_OPLINE(jit, opline);
7921
0
  ref2 = ir_NE(ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_check_constant), ir_CONST_ADDR(zv)), IR_NULL);
7922
0
  if (exit_addr) {
7923
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7924
0
      ir_GUARD(ref2, ir_CONST_ADDR(exit_addr));
7925
0
    } else {
7926
0
      ir_GUARD_NOT(ref2, ir_CONST_ADDR(exit_addr));
7927
0
    }
7928
0
    ir_END_list(end_inputs);
7929
0
  } else if (smart_branch_opcode) {
7930
0
    ref2 = ir_IF(ref2);
7931
0
    ir_IF_TRUE(ref2);
7932
0
    ir_END_list(true_inputs);
7933
0
    ir_IF_FALSE(ref2);
7934
0
    ir_END_list(false_inputs);
7935
0
  } else {
7936
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7937
0
      ir_ADD_U32(ir_ZEXT_U32(ref2), ir_CONST_U32(IS_FALSE)));
7938
0
    ir_END_list(end_inputs);
7939
0
  }
7940
7941
0
  if (!smart_branch_opcode || exit_addr) {
7942
0
    if (end_inputs) {
7943
0
      ir_MERGE_list(end_inputs);
7944
0
    }
7945
0
  } else {
7946
0
    _zend_jit_merge_smart_branch_inputs(jit, defined_label, undefined_label, true_inputs, false_inputs);
7947
0
  }
7948
7949
0
  return 1;
7950
0
}
7951
7952
static int zend_jit_escape_if_undef(zend_jit_ctx *jit, int var, uint32_t flags, const zend_op *opline, int8_t reg)
7953
0
{
7954
0
  zend_jit_addr reg_addr = ZEND_ADDR_REF_ZVAL(zend_jit_deopt_rload(jit, IR_ADDR, reg));
7955
0
  ir_ref if_def = ir_IF(jit_Z_TYPE(jit, reg_addr));
7956
7957
0
  ir_IF_FALSE_cold(if_def);
7958
7959
0
  if (flags & ZEND_JIT_EXIT_RESTORE_CALL) {
7960
0
    if (!zend_jit_save_call_chain(jit, -1)) {
7961
0
      return 0;
7962
0
    }
7963
0
  }
7964
7965
0
  if ((opline-1)->opcode != ZEND_FETCH_CONSTANT
7966
0
   && (opline-1)->opcode != ZEND_FETCH_LIST_R
7967
0
   && ((opline-1)->op1_type & (IS_VAR|IS_TMP_VAR))
7968
0
   && !(flags & ZEND_JIT_EXIT_FREE_OP1)) {
7969
0
    zend_jit_addr val_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline-1)->op1.var);
7970
7971
0
    zend_jit_zval_try_addref(jit, val_addr);
7972
0
  }
7973
7974
0
  jit_LOAD_IP_ADDR(jit, opline - 1);
7975
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_escape));
7976
7977
0
  ir_IF_TRUE(if_def);
7978
7979
0
  return 1;
7980
0
}
7981
7982
static int zend_jit_restore_zval(zend_jit_ctx *jit, int var, int8_t reg)
7983
0
{
7984
0
  zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
7985
0
  zend_jit_addr reg_addr = ZEND_ADDR_REF_ZVAL(zend_jit_deopt_rload(jit, IR_ADDR, reg));
7986
7987
  // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
7988
0
  jit_ZVAL_COPY(jit, var_addr, MAY_BE_ANY, reg_addr, MAY_BE_ANY, true);
7989
0
  return 1;
7990
0
}
7991
7992
static zend_jit_addr zend_jit_guard_fetch_result_type(zend_jit_ctx         *jit,
7993
                                                      const zend_op        *opline,
7994
                                                      zend_jit_addr         val_addr,
7995
                                                      uint8_t               type,
7996
                                                      bool                  deref,
7997
                                                      uint32_t              flags,
7998
                                                      bool                  op1_avoid_refcounting)
7999
0
{
8000
0
  zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
8001
0
  int32_t exit_point;
8002
0
  const void *res_exit_addr = NULL;
8003
0
  ir_ref end1 = IR_UNUSED, ref1 = IR_UNUSED;
8004
0
  ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
8005
0
  uint32_t old_op1_info = 0;
8006
0
  uint32_t old_info;
8007
0
  ir_ref old_ref;
8008
8009
8010
0
  if (opline->op1_type & (IS_VAR|IS_TMP_VAR|IS_CV)) {
8011
0
    old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
8012
0
    if (op1_avoid_refcounting
8013
0
     || ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
8014
0
      && STACK_FLAGS(stack, EX_VAR_TO_NUM(opline->op1.var)) & (ZREG_ZVAL_ADDREF|ZREG_THIS))) {
8015
0
      SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
8016
0
    }
8017
0
  }
8018
0
  old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
8019
0
  old_ref = STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
8020
0
  CLEAR_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
8021
0
  SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
8022
8023
0
  if (deref) {
8024
0
    ir_ref if_type;
8025
8026
0
    if (type == IS_NULL && (opline->opcode == ZEND_FETCH_DIM_IS || opline->opcode == ZEND_FETCH_OBJ_IS)) {
8027
0
      if_type = ir_IF(ir_ULE(jit_Z_TYPE(jit, val_addr), ir_CONST_U8(type)));
8028
0
    } else {
8029
0
      if_type = jit_if_Z_TYPE(jit, val_addr, type);
8030
0
    }
8031
0
    ir_IF_TRUE(if_type);
8032
0
    end1 = ir_END();
8033
0
    ref1 = ref;
8034
0
    ir_IF_FALSE_cold(if_type);
8035
8036
0
    SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ref, ZREG_ZVAL_COPY);
8037
0
    exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
8038
0
    res_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8039
0
    if (!res_exit_addr) {
8040
0
      return 0;
8041
0
    }
8042
8043
0
    jit_guard_Z_TYPE(jit, val_addr, IS_REFERENCE, res_exit_addr);
8044
0
    ref = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
8045
0
    val_addr = ZEND_ADDR_REF_ZVAL(ref);
8046
0
  }
8047
8048
0
  SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ref, ZREG_ZVAL_COPY);
8049
0
  exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
8050
0
  res_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8051
0
  if (!res_exit_addr) {
8052
0
    return 0;
8053
0
  }
8054
8055
0
  if (!deref && type == IS_NULL && (opline->opcode == ZEND_FETCH_DIM_IS || opline->opcode == ZEND_FETCH_OBJ_IS)) {
8056
0
    ir_GUARD(ir_ULE(jit_Z_TYPE(jit, val_addr), ir_CONST_U8(type)), ir_CONST_ADDR(res_exit_addr));
8057
0
  } else {
8058
0
    jit_guard_Z_TYPE(jit, val_addr, type, res_exit_addr);
8059
0
  }
8060
8061
0
  if (deref) {
8062
0
    ir_MERGE_WITH(end1);
8063
0
    ref = ir_PHI_2(IR_ADDR, ref, ref1);
8064
0
  }
8065
8066
0
  val_addr = ZEND_ADDR_REF_ZVAL(ref);
8067
8068
0
  SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), old_ref);
8069
0
  SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
8070
0
  if (opline->op1_type & (IS_VAR|IS_TMP_VAR|IS_CV)) {
8071
0
    SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
8072
0
  }
8073
8074
0
  return val_addr;
8075
0
}
8076
8077
static int zend_jit_fetch_constant(zend_jit_ctx         *jit,
8078
                                   const zend_op        *opline,
8079
                                   const zend_op_array  *op_array,
8080
                                   zend_ssa             *ssa,
8081
                                   const zend_ssa_op    *ssa_op,
8082
                                   zend_jit_addr         res_addr)
8083
0
{
8084
0
  zval *zv = RT_CONSTANT(opline, opline->op2) + 1;
8085
0
  uint32_t res_info = RES_INFO();
8086
0
  ir_ref ref, ref2, if_set, if_special, not_set_path, special_path, fast_path;
8087
8088
  // JIT: c = CACHED_PTR(opline->extended_value);
8089
0
  ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value));
8090
8091
  // JIT: if (c != NULL)
8092
0
  if_set = ir_IF(ref);
8093
8094
0
  if (!zend_jit_is_persistent_constant(zv, opline->op1.num)) {
8095
    // JIT: if (!IS_SPECIAL_CACHE_VAL(c))
8096
0
    ir_IF_FALSE_cold(if_set);
8097
0
    not_set_path = ir_END();
8098
0
    ir_IF_TRUE(if_set);
8099
0
    if_special = ir_IF(ir_AND_A(ref, ir_CONST_ADDR(CACHE_SPECIAL)));
8100
0
    ir_IF_TRUE_cold(if_special);
8101
0
    special_path = ir_END();
8102
0
    ir_IF_FALSE(if_special);
8103
0
    fast_path = ir_END();
8104
0
    ir_MERGE_2(not_set_path, special_path);
8105
0
  } else {
8106
0
    ir_IF_TRUE(if_set);
8107
0
    fast_path = ir_END();
8108
0
    ir_IF_FALSE_cold(if_set);
8109
0
  }
8110
8111
  // JIT: zend_jit_get_constant(RT_CONSTANT(opline, opline->op2) + 1, opline->op1.num);
8112
0
  jit_SET_EX_OPLINE(jit, opline);
8113
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_get_constant),
8114
0
    ir_CONST_ADDR(zv),
8115
0
    ir_CONST_U32(opline->op1.num));
8116
0
  ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
8117
8118
0
  ir_MERGE_WITH(fast_path);
8119
0
  ref = ir_PHI_2(IR_ADDR, ref2, ref);
8120
8121
0
  if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
8122
0
    uint8_t type = concrete_type(res_info);
8123
0
    zend_jit_addr const_addr = ZEND_ADDR_REF_ZVAL(ref);
8124
8125
0
    const_addr = zend_jit_guard_fetch_result_type(jit, opline, const_addr, type, false, 0, false);
8126
0
    if (!const_addr) {
8127
0
      return 0;
8128
0
    }
8129
8130
0
    res_info &= ~MAY_BE_GUARD;
8131
0
    ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
8132
8133
    // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8134
0
    jit_ZVAL_COPY(jit, res_addr, MAY_BE_ANY, const_addr, res_info, true);
8135
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
8136
0
      return 0;
8137
0
    }
8138
0
  } else {
8139
0
    ir_ref const_addr = ZEND_ADDR_REF_ZVAL(ref);
8140
8141
    // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8142
0
    jit_ZVAL_COPY(jit, res_addr, MAY_BE_ANY, const_addr, MAY_BE_ANY, true);
8143
0
  }
8144
8145
8146
0
  return 1;
8147
0
}
8148
8149
static int zend_jit_type_check(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
8150
0
{
8151
0
  uint32_t  mask;
8152
0
  zend_jit_addr op1_addr = OP1_ADDR();
8153
0
  zend_jit_addr res_addr = 0;
8154
0
  uint32_t true_label = -1, false_label = -1;
8155
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
8156
8157
  // TODO: support for is_resource() ???
8158
0
  ZEND_ASSERT(opline->extended_value != MAY_BE_RESOURCE);
8159
8160
0
  if (smart_branch_opcode && !exit_addr) {
8161
0
    if (smart_branch_opcode == ZEND_JMPZ) {
8162
0
      true_label = target_label2;
8163
0
      false_label = target_label;
8164
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
8165
0
      true_label = target_label;
8166
0
      false_label = target_label2;
8167
0
    } else {
8168
0
      ZEND_UNREACHABLE();
8169
0
    }
8170
0
  } else {
8171
0
    res_addr = RES_ADDR();
8172
0
  }
8173
8174
0
  if (op1_info & MAY_BE_UNDEF) {
8175
0
    ir_ref if_def = IR_UNUSED;
8176
8177
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
8178
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
8179
0
      ir_IF_FALSE_cold(if_def);
8180
0
    }
8181
8182
0
    jit_SET_EX_OPLINE(jit, opline);
8183
0
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
8184
0
    zend_jit_check_exception_undef_result(jit, opline);
8185
0
    if (opline->extended_value & MAY_BE_NULL) {
8186
0
      if (exit_addr) {
8187
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
8188
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8189
0
        } else {
8190
0
          ir_END_list(end_inputs);
8191
0
        }
8192
0
      } else if (smart_branch_opcode) {
8193
0
        ir_END_list(true_inputs);
8194
0
      } else {
8195
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8196
0
        ir_END_list(end_inputs);
8197
0
      }
8198
0
    } else {
8199
0
      if (exit_addr) {
8200
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8201
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8202
0
        } else {
8203
0
          ir_END_list(end_inputs);
8204
0
        }
8205
0
      } else if (smart_branch_opcode) {
8206
0
        ir_END_list(false_inputs);
8207
0
      } else {
8208
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8209
0
        if (if_def) {
8210
0
          ir_END_list(end_inputs);
8211
0
        }
8212
0
      }
8213
0
    }
8214
8215
0
    if (if_def) {
8216
0
      ir_IF_TRUE(if_def);
8217
0
      op1_info |= MAY_BE_NULL;
8218
0
    }
8219
0
  }
8220
8221
0
  if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
8222
0
    mask = opline->extended_value;
8223
0
    if (!(op1_info & MAY_BE_GUARD) && !(op1_info & (MAY_BE_ANY - mask))) {
8224
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8225
0
      if (exit_addr) {
8226
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
8227
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8228
0
        } else if (end_inputs) {
8229
0
          ir_END_list(end_inputs);
8230
0
        }
8231
0
      } else if (smart_branch_opcode) {
8232
0
        ir_END_list(true_inputs);
8233
0
      } else {
8234
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8235
0
        ir_END_list(end_inputs);
8236
0
      }
8237
0
      } else if (!(op1_info & MAY_BE_GUARD) && !(op1_info & mask)) {
8238
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8239
0
      if (exit_addr) {
8240
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8241
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8242
0
        } else if (end_inputs) {
8243
0
          ir_END_list(end_inputs);
8244
0
        }
8245
0
      } else if (smart_branch_opcode) {
8246
0
        ir_END_list(false_inputs);
8247
0
      } else {
8248
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8249
0
        ir_END_list(end_inputs);
8250
0
      }
8251
0
    } else {
8252
0
      ir_ref ref;
8253
0
      bool invert = false;
8254
0
      uint8_t type;
8255
8256
0
      switch (mask) {
8257
0
        case MAY_BE_NULL:   type = IS_NULL;   break;
8258
0
        case MAY_BE_FALSE:  type = IS_FALSE;  break;
8259
0
        case MAY_BE_TRUE:   type = IS_TRUE;   break;
8260
0
        case MAY_BE_LONG:   type = IS_LONG;   break;
8261
0
        case MAY_BE_DOUBLE: type = IS_DOUBLE; break;
8262
0
        case MAY_BE_STRING: type = IS_STRING; break;
8263
0
        case MAY_BE_ARRAY:  type = IS_ARRAY;  break;
8264
0
        case MAY_BE_OBJECT: type = IS_OBJECT; break;
8265
0
        case MAY_BE_ANY - MAY_BE_NULL:     type = IS_NULL;   invert = true; break;
8266
0
        case MAY_BE_ANY - MAY_BE_FALSE:    type = IS_FALSE;  invert = true; break;
8267
0
        case MAY_BE_ANY - MAY_BE_TRUE:     type = IS_TRUE;   invert = true; break;
8268
0
        case MAY_BE_ANY - MAY_BE_LONG:     type = IS_LONG;   invert = true; break;
8269
0
        case MAY_BE_ANY - MAY_BE_DOUBLE:   type = IS_DOUBLE; invert = true; break;
8270
0
        case MAY_BE_ANY - MAY_BE_STRING:   type = IS_STRING; invert = true; break;
8271
0
        case MAY_BE_ANY - MAY_BE_ARRAY:    type = IS_ARRAY;  invert = true; break;
8272
0
        case MAY_BE_ANY - MAY_BE_OBJECT:   type = IS_OBJECT; invert = true; break;
8273
0
        case MAY_BE_ANY - MAY_BE_RESOURCE: type = IS_OBJECT; invert = true; break;
8274
0
        default:
8275
0
          type = 0;
8276
0
      }
8277
8278
0
      if (op1_info & MAY_BE_REF) {
8279
0
        ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8280
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
8281
0
        op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8282
0
      }
8283
0
      if (type == 0) {
8284
0
        ref = ir_AND_U32(ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE(jit, op1_addr)), ir_CONST_U32(mask));
8285
0
        if (!smart_branch_opcode) {
8286
0
          ref = ir_NE(ref, ir_CONST_U32(0));
8287
0
        }
8288
0
      } else if (invert) {
8289
0
        ref = ir_NE(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(type));
8290
0
      } else {
8291
0
        ref = ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(type));
8292
0
      }
8293
8294
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8295
8296
0
      if (exit_addr) {
8297
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8298
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8299
0
        } else {
8300
0
          ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
8301
0
        }
8302
0
        if (end_inputs) {
8303
0
          ir_END_list(end_inputs);
8304
0
        }
8305
0
      } else if (smart_branch_opcode) {
8306
0
        ir_ref if_val = ir_IF(ref);
8307
0
        ir_IF_TRUE(if_val);
8308
0
        ir_END_list(true_inputs);
8309
0
        ir_IF_FALSE(if_val);
8310
0
        ir_END_list(false_inputs);
8311
0
      } else {
8312
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
8313
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
8314
0
        ir_END_list(end_inputs);
8315
0
      }
8316
0
      }
8317
0
  }
8318
8319
0
  if (!smart_branch_opcode || exit_addr) {
8320
0
    if (end_inputs) {
8321
0
      ir_MERGE_list(end_inputs);
8322
0
    } else if (exit_addr && !jit->ctx.control) {
8323
0
      ir_BEGIN(IR_UNUSED); /* unreachable block */
8324
0
    }
8325
0
  } else {
8326
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
8327
0
  }
8328
8329
0
  return 1;
8330
0
}
8331
8332
static int zend_jit_isset_isempty_cv(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
8333
0
{
8334
0
  zend_jit_addr res_addr = RES_ADDR();
8335
0
  uint32_t true_label = -1, false_label = -1;
8336
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
8337
8338
  // TODO: support for empty() ???
8339
0
  ZEND_ASSERT(opline->extended_value != MAY_BE_RESOURCE);
8340
8341
0
  if (smart_branch_opcode && !exit_addr) {
8342
0
    if (smart_branch_opcode == ZEND_JMPZ) {
8343
0
      true_label = target_label2;
8344
0
      false_label = target_label;
8345
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
8346
0
      true_label = target_label;
8347
0
      false_label = target_label2;
8348
0
    } else {
8349
0
      ZEND_UNREACHABLE();
8350
0
    }
8351
0
  } else {
8352
0
    res_addr = RES_ADDR();
8353
0
  }
8354
8355
0
  if (op1_info & MAY_BE_REF) {
8356
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8357
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
8358
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8359
0
  }
8360
8361
0
  if (!(op1_info & (MAY_BE_UNDEF|MAY_BE_NULL))) {
8362
0
    if (exit_addr) {
8363
0
      ZEND_ASSERT(smart_branch_opcode == ZEND_JMPZ);
8364
0
    } else if (smart_branch_opcode) {
8365
0
      ir_END_list(true_inputs);
8366
0
    } else {
8367
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8368
0
      ir_END_list(end_inputs);
8369
0
    }
8370
0
  } else if (!(op1_info & (MAY_BE_ANY - MAY_BE_NULL))) {
8371
0
    if (exit_addr) {
8372
0
      ZEND_ASSERT(smart_branch_opcode == ZEND_JMPNZ);
8373
0
    } else if (smart_branch_opcode) {
8374
0
      ir_END_list(false_inputs);
8375
0
    } else {
8376
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8377
0
      ir_END_list(end_inputs);
8378
0
    }
8379
0
  } else {
8380
0
    ir_ref ref = ir_GT(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_NULL));
8381
0
    if (exit_addr) {
8382
0
      if (smart_branch_opcode == ZEND_JMPNZ) {
8383
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
8384
0
      } else {
8385
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8386
0
      }
8387
0
    } else if (smart_branch_opcode) {
8388
0
      ir_ref if_val = ir_IF(ref);
8389
0
      ir_IF_TRUE(if_val);
8390
0
      ir_END_list(true_inputs);
8391
0
      ir_IF_FALSE(if_val);
8392
0
      ir_END_list(false_inputs);
8393
0
    } else {
8394
0
      jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
8395
0
        ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
8396
0
      ir_END_list(end_inputs);
8397
0
    }
8398
0
  }
8399
8400
0
  if (!smart_branch_opcode || exit_addr) {
8401
0
    if (end_inputs) {
8402
0
      ir_MERGE_list(end_inputs);
8403
0
    }
8404
0
  } else {
8405
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
8406
0
  }
8407
8408
0
  return 1;
8409
0
}
8410
8411
/* copy of hidden zend_closure */
8412
typedef struct _zend_closure {
8413
  zend_object       std;
8414
  zend_function     func;
8415
  zval              this_ptr;
8416
  zend_class_entry *called_scope;
8417
  zif_handler       orig_internal_handler;
8418
} zend_closure;
8419
8420
static int zend_jit_stack_check(zend_jit_ctx *jit, const zend_op *opline, uint32_t used_stack)
8421
0
{
8422
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
8423
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8424
8425
0
  if (!exit_addr) {
8426
0
    return 0;
8427
0
  }
8428
8429
  // JIT: if (EG(vm_stack_end) - EG(vm_stack_top) < used_stack)
8430
0
  ir_GUARD(
8431
0
    ir_UGE(
8432
0
      ir_SUB_A(ir_LOAD_A(jit_EG(vm_stack_end)), ir_LOAD_A(jit_EG(vm_stack_top))),
8433
0
      ir_CONST_ADDR(used_stack)),
8434
0
    ir_CONST_ADDR(exit_addr));
8435
8436
0
  return 1;
8437
0
}
8438
8439
static int zend_jit_free_trampoline(zend_jit_ctx *jit, ir_ref func)
8440
0
{
8441
  // JIT: if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
8442
0
  ir_ref if_trampoline = ir_IF(ir_AND_U32(
8443
0
    ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, common.fn_flags))),
8444
0
    ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE)));
8445
8446
0
  ir_IF_TRUE(if_trampoline);
8447
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_free_trampoline_helper), func);
8448
0
  ir_MERGE_WITH_EMPTY_FALSE(if_trampoline);
8449
8450
0
  return 1;
8451
0
}
8452
8453
static int zend_jit_push_call_frame(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, zend_function *func, bool is_closure, bool delayed_fetch_this, int checked_stack, ir_ref func_ref, ir_ref this_ref)
8454
0
{
8455
0
  uint32_t used_stack;
8456
0
  ir_ref used_stack_ref = IR_UNUSED;
8457
0
  bool stack_check = true;
8458
0
  ir_ref rx, ref, top, if_enough_stack, cold_path = IR_UNUSED;
8459
8460
0
  ZEND_ASSERT(func_ref != IR_NULL);
8461
0
  if (func) {
8462
0
    used_stack = zend_vm_calc_used_stack(opline->extended_value, func);
8463
0
    if ((int)used_stack <= checked_stack) {
8464
0
      stack_check = false;
8465
0
    }
8466
0
    used_stack_ref = ir_CONST_ADDR(used_stack);
8467
0
  } else {
8468
0
    ir_ref num_args_ref;
8469
0
    ir_ref if_internal_func = IR_UNUSED;
8470
0
    const size_t func_type_offset = is_closure ? offsetof(zend_closure, func.type) : offsetof(zend_function, type);
8471
8472
0
    used_stack = (ZEND_CALL_FRAME_SLOT + opline->extended_value + ZEND_OBSERVER_ENABLED) * sizeof(zval);
8473
0
    used_stack_ref = ir_CONST_ADDR(used_stack);
8474
0
    used_stack_ref = ir_HARD_COPY_A(used_stack_ref); /* load constant once */
8475
8476
    // JIT: if (EXPECTED(ZEND_USER_CODE(func->type))) {
8477
0
    ir_ref tmp = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, func_type_offset));
8478
0
    if_internal_func = ir_IF(ir_AND_U8(tmp, ir_CONST_U8(1)));
8479
0
    ir_IF_FALSE(if_internal_func);
8480
8481
    // JIT: used_stack += (func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args)) * sizeof(zval);
8482
0
    num_args_ref = ir_CONST_U32(opline->extended_value);
8483
0
    if (!is_closure) {
8484
0
      ref = ir_SUB_U32(
8485
0
        ir_SUB_U32(
8486
0
          ir_MIN_U32(
8487
0
            num_args_ref,
8488
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.num_args)))),
8489
0
          ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.last_var)))),
8490
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.T))));
8491
0
    } else {
8492
0
      ref = ir_SUB_U32(
8493
0
        ir_SUB_U32(
8494
0
          ir_MIN_U32(
8495
0
            num_args_ref,
8496
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.num_args)))),
8497
0
          ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.last_var)))),
8498
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.T))));
8499
0
    }
8500
0
    ref = ir_MUL_U32(ref, ir_CONST_U32(sizeof(zval)));
8501
0
    if (sizeof(void*) == 8) {
8502
0
      ref = ir_SEXT_A(ref);
8503
0
    }
8504
0
    ref = ir_SUB_A(used_stack_ref, ref);
8505
8506
0
    ir_MERGE_WITH_EMPTY_TRUE(if_internal_func);
8507
0
    used_stack_ref = ir_PHI_2(IR_ADDR, ref, used_stack_ref);
8508
0
  }
8509
8510
0
  zend_jit_start_reuse_ip(jit);
8511
8512
  // JIT: if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
8513
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EG(vm_stack_top)));
8514
8515
0
  if (stack_check) {
8516
    // JIT: Check Stack Overflow
8517
0
    ref = ir_UGE(
8518
0
      ir_SUB_A(
8519
0
        ir_LOAD_A(jit_EG(vm_stack_end)),
8520
0
        jit_IP(jit)),
8521
0
      used_stack_ref);
8522
8523
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8524
0
      bool may_be_trampoline = !func && (opline->opcode == ZEND_INIT_METHOD_CALL);
8525
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline,
8526
0
        may_be_trampoline ?
8527
0
          (ZEND_JIT_EXIT_TO_VM | ZEND_JIT_EXIT_METHOD_CALL) : ZEND_JIT_EXIT_TO_VM);
8528
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8529
8530
0
      if (!exit_addr) {
8531
0
        return 0;
8532
0
      }
8533
8534
0
      if (may_be_trampoline) {
8535
0
        jit->trace->exit_info[exit_point].poly_func.ref = func_ref;
8536
0
        jit->trace->exit_info[exit_point].poly_this.ref = this_ref;
8537
0
      }
8538
8539
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8540
0
    } else {
8541
0
      if_enough_stack = ir_IF(ref);
8542
0
      ir_IF_FALSE_cold(if_enough_stack);
8543
8544
#ifdef _WIN32
8545
      if (0) {
8546
#else
8547
0
      if (opline->opcode == ZEND_INIT_FCALL && func && func->type == ZEND_INTERNAL_FUNCTION) {
8548
0
#endif
8549
0
        jit_SET_EX_OPLINE(jit, opline);
8550
0
        ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_int_extend_stack_helper), used_stack_ref);
8551
0
      } else {
8552
0
        if (!is_closure) {
8553
0
          ref = func_ref;
8554
0
        } else {
8555
0
          ref = ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func));
8556
0
        }
8557
0
        jit_SET_EX_OPLINE(jit, opline);
8558
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_extend_stack_helper),
8559
0
          used_stack_ref, ref);
8560
0
      }
8561
0
      jit_STORE_IP(jit, ref);
8562
8563
0
      cold_path = ir_END();
8564
0
      ir_IF_TRUE(if_enough_stack);
8565
0
    }
8566
0
  }
8567
8568
0
  ref = jit_EG(vm_stack_top);
8569
0
  rx = jit_IP(jit);
8570
0
#if !OPTIMIZE_FOR_SIZE
8571
  /* JIT: EG(vm_stack_top) = (zval*)((char*)call + used_stack);
8572
   * This vesions is longer but faster
8573
   *    mov EG(vm_stack_top), %CALL
8574
   *    lea size(%call), %tmp
8575
   *    mov %tmp, EG(vm_stack_top)
8576
   */
8577
0
  top = rx;
8578
#else
8579
  /* JIT: EG(vm_stack_top) += used_stack;
8580
   * Use ir_emit() because ir_LOAD() makes load forwarding and doesn't allow load/store fusion
8581
   *    mov EG(vm_stack_top), %CALL
8582
   *    add $size, EG(vm_stack_top)
8583
   */
8584
  top = jit->ctx.control = ir_emit2(&jit->ctx, IR_OPT(IR_LOAD, IR_ADDR), jit->ctx.control, ref);
8585
#endif
8586
0
  ir_STORE(ref, ir_ADD_A(top, used_stack_ref));
8587
8588
  // JIT: zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
8589
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE || opline->opcode != ZEND_INIT_METHOD_CALL) {
8590
    // JIT: ZEND_SET_CALL_INFO(call, 0, call_info);
8591
0
    ir_STORE(jit_CALL(rx, This.u1.type_info), ir_CONST_U32(IS_UNDEF | ZEND_CALL_NESTED_FUNCTION));
8592
0
  }
8593
#ifdef _WIN32
8594
  if (0) {
8595
#else
8596
0
  if (opline->opcode == ZEND_INIT_FCALL && func && func->type == ZEND_INTERNAL_FUNCTION) {
8597
0
#endif
8598
0
    if (cold_path) {
8599
0
      ir_MERGE_WITH(cold_path);
8600
0
      rx = jit_IP(jit);
8601
0
    }
8602
8603
    // JIT: call->func = func;
8604
0
    ir_STORE(jit_CALL(rx, func), func_ref);
8605
0
  } else {
8606
0
    if (!is_closure) {
8607
      // JIT: call->func = func;
8608
0
      ir_STORE(jit_CALL(rx, func), func_ref);
8609
0
    } else {
8610
      // JIT: call->func = &closure->func;
8611
0
      ir_STORE(jit_CALL(rx, func), ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func)));
8612
0
    }
8613
0
    if (cold_path) {
8614
0
      ir_MERGE_WITH(cold_path);
8615
0
      rx = jit_IP(jit);
8616
0
    }
8617
0
  }
8618
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL) {
8619
    // JIT: Z_PTR(call->This) = obj;
8620
0
    ZEND_ASSERT(this_ref != IR_NULL);
8621
0
    ir_STORE(jit_CALL(rx, This.value.ptr), this_ref);
8622
0
      if (opline->op1_type == IS_UNUSED || delayed_fetch_this) {
8623
      // JIT: call->call_info |= ZEND_CALL_HAS_THIS;
8624
0
      ref = jit_CALL(rx, This.u1.type_info);
8625
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8626
0
        ir_STORE(ref, ir_CONST_U32( ZEND_CALL_HAS_THIS));
8627
0
      } else {
8628
0
        ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_HAS_THIS)));
8629
0
      }
8630
0
      } else {
8631
0
      if (opline->op1_type == IS_CV) {
8632
        // JIT: GC_ADDREF(obj);
8633
0
        jit_GC_ADDREF(jit, this_ref);
8634
0
      }
8635
8636
      // JIT: call->call_info |= ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS;
8637
0
      ref = jit_CALL(rx, This.u1.type_info);
8638
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8639
0
        ir_STORE(ref, ir_CONST_U32( ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS));
8640
0
      } else {
8641
0
        ir_STORE(ref,
8642
0
          ir_OR_U32(ir_LOAD_U32(ref),
8643
0
            ir_CONST_U32(ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS)));
8644
0
      }
8645
0
      }
8646
0
  } else if (opline->opcode == ZEND_INIT_STATIC_METHOD_CALL) {
8647
    // JIT: Z_CE(call->This) = called_scope;
8648
0
    ir_STORE(jit_CALL(rx, This), this_ref);
8649
0
  } else if (!is_closure) {
8650
    // JIT: Z_CE(call->This) = called_scope;
8651
0
    ir_STORE(jit_CALL(rx, This), IR_NULL);
8652
0
  } else {
8653
0
    ir_ref object_or_called_scope, call_info, call_info2, object, if_cond;
8654
0
    ir_ref if_cond_user = IR_UNUSED;
8655
8656
0
    if (opline->op2_type == IS_CV) {
8657
      // JIT: GC_ADDREF(closure);
8658
0
      jit_GC_ADDREF(jit, func_ref);
8659
0
    }
8660
8661
    // JIT: RX(object_or_called_scope) = closure->called_scope;
8662
0
    object_or_called_scope = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, called_scope)));
8663
8664
    // JIT: call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE |
8665
    //      (closure->func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE);
8666
0
    call_info = ir_OR_U32(
8667
0
      ir_AND_U32(
8668
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.common.fn_flags))),
8669
0
        ir_CONST_U32(ZEND_ACC_FAKE_CLOSURE)),
8670
0
      ir_CONST_U32(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE));
8671
    // JIT: if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
8672
0
    if_cond = ir_IF(ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, this_ptr.u1.v.type))));
8673
0
    ir_IF_TRUE(if_cond);
8674
8675
    // JIT: call_info |= ZEND_CALL_HAS_THIS;
8676
0
    call_info2 = ir_OR_U32(call_info, ir_CONST_U32(ZEND_CALL_HAS_THIS));
8677
8678
    // JIT: object_or_called_scope = Z_OBJ(closure->this_ptr);
8679
0
    object = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, this_ptr.value.ptr)));
8680
8681
0
    ir_MERGE_WITH_EMPTY_FALSE(if_cond);
8682
0
    call_info = ir_PHI_2(IR_U32, call_info2, call_info);
8683
0
    object_or_called_scope = ir_PHI_2(IR_ADDR, object, object_or_called_scope);
8684
8685
    // JIT: ZEND_SET_CALL_INFO(call, 0, call_info);
8686
0
    ref = jit_CALL(rx, This.u1.type_info);
8687
0
    ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), call_info));
8688
8689
    // JIT: Z_PTR(call->This) = object_or_called_scope;
8690
0
    ir_STORE(jit_CALL(rx, This.value.ptr), object_or_called_scope);
8691
8692
0
    if (!func) {
8693
      // JIT: if (closure->func.common.type & ZEND_USER_FUNCTION)
8694
0
      ir_ref type = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.type)));
8695
0
      if_cond_user = ir_IF(ir_AND_U8(type, ir_CONST_U8(ZEND_USER_FUNCTION)));
8696
0
      ir_IF_TRUE(if_cond_user);
8697
0
    }
8698
8699
0
    if (!func || func->common.type == ZEND_USER_FUNCTION) {
8700
      // JIT: zend_jit_init_func_run_time_cache_helper(closure->func);
8701
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_init_func_run_time_cache_helper),
8702
0
        ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func)));
8703
0
    }
8704
8705
0
    if (!func) {
8706
0
      ir_MERGE_WITH_EMPTY_FALSE(if_cond_user);
8707
0
    }
8708
0
  }
8709
8710
  // JIT: ZEND_CALL_NUM_ARGS(call) = num_args;
8711
0
  ir_STORE(jit_CALL(rx, This.u2.num_args), ir_CONST_U32(opline->extended_value));
8712
8713
0
  return 1;
8714
0
}
8715
8716
static int zend_jit_func_guard(zend_jit_ctx *jit, ir_ref func_ref, const zend_function *func, const void *exit_addr)
8717
0
{
8718
0
  if (func->type == ZEND_USER_FUNCTION &&
8719
0
      (!(func->common.fn_flags & ZEND_ACC_IMMUTABLE) ||
8720
0
       (func->common.fn_flags & ZEND_ACC_CLOSURE) ||
8721
0
       !func->common.function_name)) {
8722
0
    const zend_op *opcodes = func->op_array.opcodes;
8723
8724
    // JIT: if (call->func.op_array.opcodes != opcodes) goto exit_addr;
8725
0
    ir_GUARD(
8726
0
      ir_EQ(
8727
0
        ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes))),
8728
0
        ir_CONST_ADDR(opcodes)),
8729
0
      ir_CONST_ADDR(exit_addr));
8730
#ifdef ZEND_WIN32
8731
  } else if (func->type == ZEND_INTERNAL_FUNCTION) {
8732
    // ASLR may cause different addresses in different workers. Check for the internal function handler.
8733
    // JIT: if (call->func.internal_function.handler != handler) goto exit_addr;
8734
    ir_GUARD(
8735
      ir_EQ(
8736
        ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_internal_function, handler))),
8737
        ir_CONST_FC_FUNC(func->internal_function.handler)),
8738
      ir_CONST_ADDR(exit_addr));
8739
#endif
8740
0
  } else {
8741
    // JIT: if (call->func != func) goto exit_addr;
8742
0
    ir_GUARD(ir_EQ(func_ref, ir_CONST_ADDR(func)), ir_CONST_ADDR(exit_addr));
8743
0
  }
8744
8745
0
  return 1;
8746
0
}
8747
8748
static int zend_jit_init_fcall_guard(zend_jit_ctx *jit, uint32_t level, const zend_function *func, const zend_op *to_opline)
8749
0
{
8750
0
  int32_t exit_point;
8751
0
  const void *exit_addr;
8752
0
  ir_ref call;
8753
8754
0
  if (func->type == ZEND_USER_FUNCTION
8755
0
   && !zend_accel_in_shm(func->op_array.opcodes)) {
8756
    /* op_array and op_array->opcodes are not persistent. We can't link. */
8757
0
    return 0;
8758
0
  }
8759
8760
0
  exit_point = zend_jit_trace_get_exit_point(to_opline, ZEND_JIT_EXIT_POLYMORPHISM);
8761
0
  exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8762
0
  if (!exit_addr) {
8763
0
    return 0;
8764
0
  }
8765
8766
  // call = EX(call);
8767
0
  call = ir_LOAD_A(jit_EX(call));
8768
0
  while (level > 0) {
8769
    // call = call->prev_execute_data
8770
0
    call = ir_LOAD_A(jit_CALL(call, prev_execute_data));
8771
0
    level--;
8772
0
  }
8773
8774
0
  return zend_jit_func_guard(jit, ir_LOAD_A(jit_CALL(call, func)), func, exit_addr);
8775
0
}
8776
8777
static int zend_jit_init_fcall(zend_jit_ctx *jit, const zend_op *opline, uint32_t b, const zend_op_array *op_array, zend_ssa *ssa, const zend_ssa_op *ssa_op, int call_level, zend_jit_trace_rec *trace, int checked_stack)
8778
0
{
8779
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
8780
0
  zend_call_info *call_info = NULL;
8781
0
  zend_function *func = NULL;
8782
0
  ir_ref func_ref = IR_UNUSED;
8783
8784
0
  if (jit->delayed_call_level) {
8785
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
8786
0
      return 0;
8787
0
    }
8788
0
  }
8789
8790
0
  if (info) {
8791
0
    call_info = info->callee_info;
8792
0
    while (call_info && call_info->caller_init_opline != opline) {
8793
0
      call_info = call_info->next_callee;
8794
0
    }
8795
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
8796
0
      func = call_info->callee_func;
8797
0
    }
8798
0
  }
8799
8800
0
  if (!func
8801
0
   && trace
8802
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL) {
8803
#ifdef _WIN32
8804
    /* ASLR */
8805
    if (trace->func->type != ZEND_INTERNAL_FUNCTION) {
8806
      func = (zend_function*)trace->func;
8807
    }
8808
#else
8809
0
    func = (zend_function*)trace->func;
8810
0
#endif
8811
0
  }
8812
8813
#ifdef _WIN32
8814
  if (0) {
8815
#else
8816
0
  if (opline->opcode == ZEND_INIT_FCALL
8817
0
   && func
8818
0
   && func->type == ZEND_INTERNAL_FUNCTION) {
8819
0
#endif
8820
    /* load constant address later */
8821
0
    func_ref = ir_CONST_ADDR(func);
8822
0
  } else if (func && op_array == &func->op_array) {
8823
    /* recursive call */
8824
0
    if (!(func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)
8825
0
     || zend_jit_prefer_const_addr_load(jit, (uintptr_t)func)) {
8826
0
      func_ref = ir_LOAD_A(jit_EX(func));
8827
0
    } else {
8828
0
      func_ref = ir_CONST_ADDR(func);
8829
0
    }
8830
0
  } else {
8831
0
    ir_ref if_func, cache_slot_ref, ref;
8832
8833
    // JIT: if (CACHED_PTR(opline->result.num))
8834
0
    cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num);
8835
0
    func_ref = ir_LOAD_A(cache_slot_ref);
8836
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
8837
0
     && func
8838
0
     && (func->common.fn_flags & ZEND_ACC_IMMUTABLE)
8839
0
     && opline->opcode != ZEND_INIT_FCALL) {
8840
      /* Called func may be changed because of recompilation. See ext/opcache/tests/jit/init_fcall_003.phpt */
8841
0
      if_func = ir_IF(ir_EQ(func_ref, ir_CONST_ADDR(func)));
8842
0
    } else {
8843
0
      if_func = ir_IF(func_ref);
8844
0
    }
8845
0
    ir_IF_FALSE_cold(if_func);
8846
0
    if (opline->opcode == ZEND_INIT_FCALL
8847
0
     && func
8848
0
     && func->type == ZEND_USER_FUNCTION
8849
0
     && (func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)) {
8850
0
      ref = ir_HARD_COPY_A(ir_CONST_ADDR(func)); /* load constant once */
8851
0
        ir_STORE(cache_slot_ref, ref);
8852
0
      ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_init_func_run_time_cache_helper), ref);
8853
0
    } else {
8854
0
      zval *zv = RT_CONSTANT(opline, opline->op2);
8855
8856
0
      if (opline->opcode == ZEND_INIT_FCALL) {
8857
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_func_helper),
8858
0
          ir_CONST_ADDR(Z_STR_P(zv)),
8859
0
          cache_slot_ref);
8860
0
      } else if (opline->opcode == ZEND_INIT_FCALL_BY_NAME) {
8861
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_func_helper),
8862
0
          ir_CONST_ADDR(Z_STR_P(zv + 1)),
8863
0
          cache_slot_ref);
8864
0
      } else if (opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
8865
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_ns_func_helper),
8866
0
          ir_CONST_ADDR(zv),
8867
0
          cache_slot_ref);
8868
0
      } else {
8869
0
        ZEND_UNREACHABLE();
8870
0
      }
8871
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8872
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline,
8873
0
          func && (func->common.fn_flags & ZEND_ACC_IMMUTABLE) ? ZEND_JIT_EXIT_INVALIDATE : 0);
8874
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8875
8876
0
        if (!exit_addr) {
8877
0
          return 0;
8878
0
        }
8879
0
        if (!func || opline->opcode == ZEND_INIT_FCALL) {
8880
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8881
0
        } else if (!zend_jit_func_guard(jit, ref, func, exit_addr)) {
8882
0
          return 0;
8883
0
        }
8884
0
      } else {
8885
0
jit_SET_EX_OPLINE(jit, opline);
8886
0
        ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_undefined_function));
8887
0
      }
8888
0
    }
8889
0
    ir_MERGE_WITH_EMPTY_TRUE(if_func);
8890
0
    func_ref = ir_PHI_2(IR_ADDR, ref, func_ref);
8891
0
  }
8892
8893
0
  if (!zend_jit_push_call_frame(jit, opline, op_array, func, false, false, checked_stack, func_ref, IR_UNUSED)) {
8894
0
    return 0;
8895
0
  }
8896
8897
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
8898
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
8899
0
      return 0;
8900
0
    }
8901
0
  } else {
8902
0
    ZEND_ASSERT(call_level > 0);
8903
0
    jit->delayed_call_level = call_level;
8904
0
    delayed_call_chain = true;
8905
0
  }
8906
8907
0
  if (trace
8908
0
   && trace->op == ZEND_JIT_TRACE_END
8909
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
8910
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
8911
0
      return 0;
8912
0
    }
8913
0
  }
8914
8915
0
  return 1;
8916
0
}
8917
8918
static int zend_jit_init_method_call(zend_jit_ctx         *jit,
8919
                                     const zend_op        *opline,
8920
                                     uint32_t              b,
8921
                                     const zend_op_array  *op_array,
8922
                                     zend_ssa             *ssa,
8923
                                     const zend_ssa_op    *ssa_op,
8924
                                     int                   call_level,
8925
                                     uint32_t              op1_info,
8926
                                     zend_jit_addr         op1_addr,
8927
                                     zend_class_entry     *ce,
8928
                                     bool                  ce_is_instanceof,
8929
                                     bool                  on_this,
8930
                                     bool                  delayed_fetch_this,
8931
                                     zend_class_entry     *trace_ce,
8932
                                     zend_jit_trace_rec   *trace,
8933
                                     int                   checked_stack,
8934
                                     ir_ref                func_ref,
8935
                                     ir_ref                this_ref,
8936
                                     bool                  polymorphic_side_trace)
8937
0
{
8938
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
8939
0
  zend_call_info *call_info = NULL;
8940
0
  zend_function *func = NULL;
8941
0
  zval *function_name;
8942
0
  ir_ref if_static = IR_UNUSED, cold_path;
8943
8944
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
8945
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
8946
8947
0
  function_name = RT_CONSTANT(opline, opline->op2);
8948
8949
0
  if (info) {
8950
0
    call_info = info->callee_info;
8951
0
    while (call_info && call_info->caller_init_opline != opline) {
8952
0
      call_info = call_info->next_callee;
8953
0
    }
8954
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
8955
0
      func = call_info->callee_func;
8956
0
    }
8957
0
  }
8958
8959
0
  if (polymorphic_side_trace) {
8960
    /* function is passed from parent snapshot */
8961
0
    ZEND_ASSERT(func_ref != IR_UNUSED && this_ref != IR_UNUSED);
8962
0
  } else {
8963
0
    ir_ref ref, ref2, if_found, fast_path, run_time_cache, this_ref2;
8964
8965
0
    if (on_this) {
8966
0
      zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
8967
0
      this_ref = jit_Z_PTR(jit, this_addr);
8968
0
    } else {
8969
0
        if (op1_info & MAY_BE_REF) {
8970
0
        if (opline->op1_type == IS_CV) {
8971
          // JIT: ZVAL_DEREF(op1)
8972
0
          ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8973
0
          ref = jit_ZVAL_DEREF_ref(jit, ref);
8974
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8975
0
        } else {
8976
0
          ir_ref if_ref;
8977
8978
          /* Hack: Convert reference to regular value to simplify JIT code */
8979
0
          ZEND_ASSERT(Z_REG(op1_addr) == ZREG_FP);
8980
8981
0
          if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
8982
0
          ir_IF_TRUE(if_ref);
8983
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_unref_helper), jit_ZVAL_ADDR(jit, op1_addr));
8984
8985
0
          ir_MERGE_WITH_EMPTY_FALSE(if_ref);
8986
0
        }
8987
0
      }
8988
0
      if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
8989
0
        if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8990
0
          int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
8991
0
          const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8992
8993
0
          if (!exit_addr) {
8994
0
            return 0;
8995
0
          }
8996
0
          ir_GUARD(ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_OBJECT)),
8997
0
            ir_CONST_ADDR(exit_addr));
8998
0
        } else {
8999
0
          ir_ref if_object = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
9000
9001
0
          ir_IF_FALSE_cold(if_object);
9002
9003
0
          jit_SET_EX_OPLINE(jit, opline);
9004
0
          if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9005
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_method_call_tmp),
9006
0
              jit_ZVAL_ADDR(jit, op1_addr));
9007
0
          } else {
9008
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_method_call),
9009
0
              jit_ZVAL_ADDR(jit, op1_addr));
9010
0
          }
9011
0
          ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
9012
0
          ir_IF_TRUE(if_object);
9013
0
        }
9014
0
      }
9015
9016
0
      this_ref = jit_Z_PTR(jit, op1_addr);
9017
0
    }
9018
9019
0
    if (jit->delayed_call_level) {
9020
0
      if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9021
0
        return 0;
9022
0
      }
9023
0
    }
9024
9025
0
    if (func) {
9026
      // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9027
0
      ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num + sizeof(void*)));
9028
9029
0
      if_found = ir_IF(ref);
9030
0
      ir_IF_TRUE(if_found);
9031
0
      fast_path = ir_END();
9032
0
    } else {
9033
      // JIT: if (CACHED_PTR(opline->result.num) == obj->ce)) {
9034
0
      run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
9035
0
      ref = ir_EQ(
9036
0
        ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->result.num)),
9037
0
        ir_LOAD_A(ir_ADD_OFFSET(this_ref, offsetof(zend_object, ce))));
9038
0
      if_found = ir_IF(ref);
9039
0
      ir_IF_TRUE(if_found);
9040
9041
      // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9042
0
      ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->result.num + sizeof(void*)));
9043
0
      fast_path = ir_END();
9044
9045
0
    }
9046
9047
0
    ir_IF_FALSE_cold(if_found);
9048
0
    jit_SET_EX_OPLINE(jit, opline);
9049
9050
0
    if (!jit->ctx.fixed_call_stack_size) {
9051
      // JIT: alloca(sizeof(void*));
9052
0
      this_ref2 = ir_ALLOCA(ir_CONST_ADDR(0x10));
9053
0
    } else {
9054
#ifdef _WIN64
9055
      this_ref2 = ir_HARD_COPY_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
9056
#else
9057
0
      this_ref2 = ir_HARD_COPY_A(ir_RLOAD_A(IR_REG_SP));
9058
0
#endif
9059
0
    }
9060
0
    ir_STORE(this_ref2, this_ref);
9061
9062
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9063
0
      ref2 = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_method_tmp_helper),
9064
0
          this_ref,
9065
0
          ir_CONST_ADDR(function_name),
9066
0
          this_ref2);
9067
0
    } else {
9068
0
      ref2 = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_method_helper),
9069
0
          this_ref,
9070
0
          ir_CONST_ADDR(function_name),
9071
0
          this_ref2);
9072
0
    }
9073
9074
9075
0
    if (!jit->ctx.fixed_call_stack_size) {
9076
0
      this_ref2 = ir_LOAD_A(ir_RLOAD_A(IR_REG_SP));
9077
      // JIT: revert alloca
9078
0
      ir_AFREE(ir_CONST_ADDR(0x10));
9079
0
    } else {
9080
#ifdef _WIN64
9081
      this_ref2 = ir_LOAD_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
9082
#else
9083
0
      this_ref2 = ir_LOAD_A(ir_RLOAD_A(IR_REG_SP));
9084
0
#endif
9085
0
    }
9086
9087
0
    ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9088
9089
0
    ir_MERGE_WITH(fast_path);
9090
0
    func_ref = ir_PHI_2(IR_ADDR, ref2, ref);
9091
0
    this_ref = ir_PHI_2(IR_ADDR, this_ref2, this_ref);
9092
0
  }
9093
9094
0
  if ((!func || zend_jit_may_be_modified(func, op_array))
9095
0
   && trace
9096
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9097
0
   && trace->func) {
9098
0
    int32_t exit_point;
9099
0
    const void *exit_addr;
9100
9101
0
    exit_point = zend_jit_trace_get_exit_point(opline, func ? ZEND_JIT_EXIT_INVALIDATE : ZEND_JIT_EXIT_METHOD_CALL);
9102
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9103
0
    if (!exit_addr) {
9104
0
      return 0;
9105
0
    }
9106
9107
0
    jit->trace->exit_info[exit_point].poly_func.ref = func_ref;
9108
0
    jit->trace->exit_info[exit_point].poly_this.ref = this_ref;
9109
9110
0
    func = (zend_function*)trace->func;
9111
9112
0
    if (!zend_jit_func_guard(jit, func_ref, func, exit_addr)) {
9113
0
      return 0;
9114
0
    }
9115
0
  }
9116
9117
0
  if (!func) {
9118
    // JIT: if (fbc->common.fn_flags & ZEND_ACC_STATIC) {
9119
0
    if_static = ir_IF(ir_AND_U32(
9120
0
      ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
9121
0
      ir_CONST_U32(ZEND_ACC_STATIC)));
9122
0
    ir_IF_TRUE_cold(if_static);
9123
0
  }
9124
9125
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC) != 0) {
9126
0
    ir_ref ret;
9127
9128
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9129
0
      ret = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_static_method_call_frame_tmp),
9130
0
          this_ref,
9131
0
          func_ref,
9132
0
          ir_CONST_U32(opline->extended_value));
9133
0
    } else {
9134
0
      ret = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_static_method_call_frame),
9135
0
          this_ref,
9136
0
          func_ref,
9137
0
          ir_CONST_U32(opline->extended_value));
9138
0
    }
9139
9140
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR) && !delayed_fetch_this)) {
9141
0
      ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9142
0
    }
9143
0
    jit_STORE_IP(jit, ret);
9144
0
  }
9145
9146
0
  if (!func) {
9147
0
    cold_path = ir_END();
9148
0
    ir_IF_FALSE(if_static);
9149
0
  }
9150
9151
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC) == 0) {
9152
0
    if (!zend_jit_push_call_frame(jit, opline, NULL, func, false, delayed_fetch_this, checked_stack, func_ref, this_ref)) {
9153
0
      return 0;
9154
0
    }
9155
0
  }
9156
9157
0
  if (!func) {
9158
0
    ir_MERGE_WITH(cold_path);
9159
0
  }
9160
0
  zend_jit_start_reuse_ip(jit);
9161
9162
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9163
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9164
0
      return 0;
9165
0
    }
9166
0
  } else {
9167
0
    ZEND_ASSERT(call_level > 0);
9168
0
    delayed_call_chain = true;
9169
0
    jit->delayed_call_level = call_level;
9170
0
  }
9171
9172
0
  if (trace
9173
0
   && trace->op == ZEND_JIT_TRACE_END
9174
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9175
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9176
0
      return 0;
9177
0
    }
9178
0
  }
9179
9180
0
  return 1;
9181
0
}
9182
9183
static int zend_jit_init_static_method_call(zend_jit_ctx         *jit,
9184
                                            const zend_op        *opline,
9185
                                            uint32_t              b,
9186
                                            const zend_op_array  *op_array,
9187
                                            zend_ssa             *ssa,
9188
                                            const zend_ssa_op    *ssa_op,
9189
                                            int                   call_level,
9190
                                            zend_jit_trace_rec   *trace,
9191
                                            int                   checked_stack)
9192
0
{
9193
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
9194
0
  zend_call_info *call_info = NULL;
9195
0
  zend_class_entry *ce;
9196
0
  zend_function *func = NULL;
9197
0
  ir_ref func_ref, func_ref2, scope_ref, scope_ref2, if_cached, cold_path, ref;
9198
0
  ir_ref if_static = IR_UNUSED;
9199
9200
0
  if (info) {
9201
0
    call_info = info->callee_info;
9202
0
    while (call_info && call_info->caller_init_opline != opline) {
9203
0
      call_info = call_info->next_callee;
9204
0
    }
9205
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
9206
0
      func = call_info->callee_func;
9207
0
    }
9208
0
  }
9209
9210
0
  ce = zend_get_known_class(op_array, opline, opline->op1_type, opline->op1);
9211
0
  if (!func && ce && (opline->op1_type == IS_CONST || !(ce->ce_flags & ZEND_ACC_TRAIT))) {
9212
0
    zval *zv = RT_CONSTANT(opline, opline->op2);
9213
0
    zend_string *method_name;
9214
9215
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
9216
0
    method_name = Z_STR_P(zv);
9217
0
    zv = zend_hash_find(&ce->function_table, method_name);
9218
0
    if (zv) {
9219
0
      zend_function *fn = Z_PTR_P(zv);
9220
9221
0
      if (fn->common.scope == op_array->scope
9222
0
       || (fn->common.fn_flags & ZEND_ACC_PUBLIC)
9223
0
       || ((fn->common.fn_flags & ZEND_ACC_PROTECTED)
9224
0
        && op_array->scope
9225
0
        && instanceof_function_slow(op_array->scope, fn->common.scope))) {
9226
0
        func = fn;
9227
0
      }
9228
0
    }
9229
0
  }
9230
9231
0
  if (jit->delayed_call_level) {
9232
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9233
0
      return 0;
9234
0
    }
9235
0
  }
9236
9237
  // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9238
0
  func_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num + sizeof(void*)));
9239
9240
  // JIT: if (fbc)
9241
0
  if_cached = ir_IF(func_ref);
9242
0
  ir_IF_FALSE_cold(if_cached);
9243
9244
0
  jit_SET_EX_OPLINE(jit, opline);
9245
0
  scope_ref2 = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_class_helper), jit_FP(jit));
9246
0
  ir_GUARD(scope_ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9247
9248
0
  func_ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_static_method_helper), jit_FP(jit), scope_ref2);
9249
0
  ir_GUARD(func_ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9250
9251
0
  cold_path = ir_END();
9252
9253
0
  ir_IF_TRUE(if_cached);
9254
0
  if (ce && (ce->ce_flags & ZEND_ACC_IMMUTABLE) && (ce->ce_flags & ZEND_ACC_LINKED)) {
9255
0
    scope_ref = ir_CONST_ADDR(ce);
9256
0
  } else {
9257
0
    scope_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num));
9258
0
  }
9259
9260
0
  ir_MERGE_2(cold_path, ir_END());
9261
0
  func_ref = ir_PHI_2(IR_ADDR, func_ref2, func_ref);
9262
0
  scope_ref = ir_PHI_2(IR_ADDR, scope_ref2, scope_ref);
9263
9264
0
  if ((!func || zend_jit_may_be_modified(func, op_array))
9265
0
   && trace
9266
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9267
0
   && trace->func) {
9268
0
    int32_t exit_point;
9269
0
    const void *exit_addr;
9270
9271
0
    exit_point = zend_jit_trace_get_exit_point(opline, func ? ZEND_JIT_EXIT_INVALIDATE : 0);
9272
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9273
0
    if (!exit_addr) {
9274
0
      return 0;
9275
0
    }
9276
9277
//    jit->trace->exit_info[exit_point].poly_func_ref = func_ref;
9278
//    jit->trace->exit_info[exit_point].poly_this_ref = scope_ref;
9279
9280
0
    func = (zend_function*)trace->func;
9281
9282
0
    if (!zend_jit_func_guard(jit, func_ref, func, exit_addr)) {
9283
0
      return 0;
9284
0
    }
9285
0
  }
9286
9287
0
  if (!func || !(func->common.fn_flags & ZEND_ACC_STATIC)) {
9288
0
    if (!func) {
9289
      // JIT: if (fbc->common.fn_flags & ZEND_ACC_STATIC) {
9290
0
      if_static = ir_IF(ir_AND_U32(
9291
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
9292
0
        ir_CONST_U32(ZEND_ACC_STATIC)));
9293
0
      ir_IF_FALSE_cold(if_static);
9294
0
    }
9295
9296
0
    jit_SET_EX_OPLINE(jit, opline);
9297
0
    ref = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_this_method_call_frame),
9298
0
        scope_ref,
9299
0
        func_ref,
9300
0
        ir_CONST_U32(opline->extended_value));
9301
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9302
0
    jit_STORE_IP(jit, ref);
9303
9304
0
    if (!func) {
9305
0
      cold_path = ir_END();
9306
0
      ir_IF_TRUE(if_static);
9307
0
    }
9308
0
  }
9309
9310
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC)) {
9311
0
    if (opline->op1_type == IS_UNUSED
9312
0
     && ((opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT ||
9313
0
         (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF)) {
9314
0
      if (op_array->fn_flags & ZEND_ACC_STATIC) {
9315
0
        scope_ref = ir_LOAD_A(jit_EX(This.value.ref));
9316
0
      } else if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
9317
0
        ir_ref if_object, values = IR_UNUSED;
9318
9319
0
        if_object = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, jit_EX(This)), ir_CONST_U8(IS_OBJECT)));
9320
0
        ir_IF_TRUE(if_object);
9321
0
        ir_END_PHI_list(values,
9322
0
          ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(This.value.ref)), offsetof(zend_object, ce))));
9323
0
        ir_IF_FALSE(if_object);
9324
0
        ir_END_PHI_list(values, ir_LOAD_A(jit_EX(This.value.ref)));
9325
0
        ir_PHI_list(values);
9326
0
      } else {
9327
0
        scope_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(This.value.ref)), offsetof(zend_object, ce)));
9328
0
      }
9329
0
    }
9330
0
    if (!zend_jit_push_call_frame(jit, opline, op_array, func, false, false, checked_stack, func_ref, scope_ref)) {
9331
0
      return 0;
9332
0
    }
9333
9334
0
    if (!func) {
9335
0
      ir_MERGE_2(cold_path, ir_END());
9336
0
    }
9337
0
  }
9338
9339
0
  zend_jit_start_reuse_ip(jit);
9340
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9341
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9342
0
      return 0;
9343
0
    }
9344
0
  } else {
9345
0
    ZEND_ASSERT(call_level > 0);
9346
0
    jit->delayed_call_level = call_level;
9347
0
    delayed_call_chain = true;
9348
0
  }
9349
9350
0
  if (trace
9351
0
   && trace->op == ZEND_JIT_TRACE_END
9352
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9353
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9354
0
      return 0;
9355
0
    }
9356
0
  }
9357
9358
0
  return 1;
9359
0
}
9360
9361
static int zend_jit_init_closure_call(zend_jit_ctx         *jit,
9362
                                      const zend_op        *opline,
9363
                                      uint32_t              b,
9364
                                      const zend_op_array  *op_array,
9365
                                      zend_ssa             *ssa,
9366
                                      const zend_ssa_op    *ssa_op,
9367
                                      int                   call_level,
9368
                                      zend_jit_trace_rec   *trace,
9369
                                      int                   checked_stack)
9370
0
{
9371
0
  zend_function *func = NULL;
9372
0
  zend_jit_addr op2_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
9373
0
  ir_ref ref;
9374
9375
0
  ref = jit_Z_PTR(jit, op2_addr);
9376
9377
0
  if (ssa->var_info[ssa_op->op2_use].ce != zend_ce_closure
9378
0
   && !(ssa->var_info[ssa_op->op2_use].type & MAY_BE_CLASS_GUARD)) {
9379
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9380
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9381
9382
0
    if (!exit_addr) {
9383
0
      return 0;
9384
0
    }
9385
9386
0
    ir_GUARD(
9387
0
      ir_EQ(
9388
0
        ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_object, ce))),
9389
0
        ir_CONST_ADDR(zend_ce_closure)),
9390
0
      ir_CONST_ADDR(exit_addr));
9391
9392
0
    if (ssa->var_info && ssa_op->op2_use >= 0) {
9393
0
      ssa->var_info[ssa_op->op2_use].type |= MAY_BE_CLASS_GUARD;
9394
0
      ssa->var_info[ssa_op->op2_use].ce = zend_ce_closure;
9395
0
      ssa->var_info[ssa_op->op2_use].is_instanceof = 0;
9396
0
    }
9397
0
  }
9398
9399
0
  if (trace
9400
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9401
0
   && trace->func
9402
0
   && trace->func->type == ZEND_USER_FUNCTION) {
9403
0
    const zend_op *opcodes;
9404
0
    int32_t exit_point;
9405
0
    const void *exit_addr;
9406
9407
0
    func = (zend_function*)trace->func;
9408
0
    opcodes = func->op_array.opcodes;
9409
0
    exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_CLOSURE_CALL);
9410
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9411
0
    if (!exit_addr) {
9412
0
      return 0;
9413
0
    }
9414
9415
0
    ir_GUARD(
9416
0
      ir_EQ(
9417
0
        ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_closure, func.op_array.opcodes))),
9418
0
        ir_CONST_ADDR(opcodes)),
9419
0
      ir_CONST_ADDR(exit_addr));
9420
0
  }
9421
9422
0
  if (jit->delayed_call_level) {
9423
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9424
0
      return 0;
9425
0
    }
9426
0
  }
9427
9428
0
  if (!zend_jit_push_call_frame(jit, opline, NULL, func, true, false, checked_stack, ref, IR_UNUSED)) {
9429
0
    return 0;
9430
0
  }
9431
9432
0
  if (zend_jit_needs_call_chain(NULL, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9433
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9434
0
      return 0;
9435
0
    }
9436
0
  } else {
9437
0
    ZEND_ASSERT(call_level > 0);
9438
0
    delayed_call_chain = true;
9439
0
    jit->delayed_call_level = call_level;
9440
0
  }
9441
9442
0
  if (trace
9443
0
   && trace->op == ZEND_JIT_TRACE_END
9444
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9445
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9446
0
      return 0;
9447
0
    }
9448
0
  }
9449
9450
0
  return 1;
9451
0
}
9452
9453
static int zend_jit_send_val(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr)
9454
0
{
9455
0
  uint32_t arg_num = opline->op2.num;
9456
0
  zend_jit_addr arg_addr;
9457
9458
0
  ZEND_ASSERT(opline->opcode == ZEND_SEND_VAL || arg_num <= MAX_ARG_FLAG_NUM);
9459
9460
0
  if (!zend_jit_reuse_ip(jit)) {
9461
0
    return 0;
9462
0
  }
9463
9464
0
  if (opline->opcode == ZEND_SEND_VAL_EX) {
9465
0
    uint32_t mask = ZEND_SEND_BY_REF << ((arg_num + 3) * 2);
9466
9467
0
    ZEND_ASSERT(arg_num <= MAX_ARG_FLAG_NUM);
9468
9469
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9470
0
     && JIT_G(current_frame)
9471
0
     && JIT_G(current_frame)->call
9472
0
     && JIT_G(current_frame)->call->func) {
9473
0
      if (ARG_MUST_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9474
        /* Don't generate code that always throws exception */
9475
0
        return 0;
9476
0
      }
9477
0
    } else {
9478
0
      ir_ref cond = ir_AND_U32(
9479
0
        ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_RX(func)), offsetof(zend_function, quick_arg_flags))),
9480
0
        ir_CONST_U32(mask));
9481
9482
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9483
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9484
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9485
0
        if (!exit_addr) {
9486
0
          return 0;
9487
0
        }
9488
0
        ir_GUARD_NOT(cond, ir_CONST_ADDR(exit_addr));
9489
0
      } else {
9490
0
        ir_ref if_pass_by_ref;
9491
9492
0
        if_pass_by_ref = ir_IF(cond);
9493
9494
0
        ir_IF_TRUE_cold(if_pass_by_ref);
9495
0
        if (Z_MODE(op1_addr) == IS_REG) {
9496
          /* set type to avoid zval_ptr_dtor() on uninitialized value */
9497
0
          zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
9498
0
          jit_set_Z_TYPE_INFO(jit, addr, IS_UNDEF);
9499
0
        }
9500
0
        jit_SET_EX_OPLINE(jit, opline);
9501
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_throw_cannot_pass_by_ref));
9502
9503
0
        ir_IF_FALSE(if_pass_by_ref);
9504
0
      }
9505
0
    }
9506
0
  }
9507
9508
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9509
9510
0
  if (opline->op1_type == IS_CONST) {
9511
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
9512
9513
0
    jit_ZVAL_COPY_CONST(jit,
9514
0
      arg_addr,
9515
0
      MAY_BE_ANY, MAY_BE_ANY,
9516
0
      zv, true);
9517
0
  } else {
9518
0
    jit_ZVAL_COPY(jit,
9519
0
      arg_addr,
9520
0
      MAY_BE_ANY,
9521
0
      op1_addr, op1_info, false);
9522
0
  }
9523
9524
0
  return 1;
9525
0
}
9526
9527
static int zend_jit_send_ref(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, uint32_t op1_info, int cold)
9528
0
{
9529
0
  zend_jit_addr op1_addr, arg_addr, ref_addr;
9530
0
  ir_ref ref_path = IR_UNUSED;
9531
9532
0
  op1_addr = OP1_ADDR();
9533
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9534
9535
0
  if (!zend_jit_reuse_ip(jit)) {
9536
0
    return 0;
9537
0
  }
9538
9539
0
  if (opline->op1_type == IS_VAR) {
9540
0
    if (op1_info & MAY_BE_INDIRECT) {
9541
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
9542
0
    }
9543
0
  } else if (opline->op1_type == IS_CV) {
9544
0
    if (op1_info & MAY_BE_UNDEF) {
9545
0
      if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9546
        // JIT: if (Z_TYPE_P(op1) == IS_UNDEF)
9547
0
        ir_ref if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
9548
0
        ir_IF_FALSE(if_def);
9549
        // JIT: ZVAL_NULL(op1)
9550
0
        jit_set_Z_TYPE_INFO(jit,op1_addr, IS_NULL);
9551
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def);
9552
0
      }
9553
0
      op1_info &= ~MAY_BE_UNDEF;
9554
0
      op1_info |= MAY_BE_NULL;
9555
0
    }
9556
0
  } else {
9557
0
    ZEND_UNREACHABLE();
9558
0
  }
9559
9560
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) {
9561
0
    ir_ref ref, ref2;
9562
9563
0
    if (op1_info & MAY_BE_REF) {
9564
0
      ir_ref if_ref;
9565
9566
      // JIT: if (Z_TYPE_P(op1) == IS_UNDEF)
9567
0
      if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9568
0
      ir_IF_TRUE(if_ref);
9569
      // JIT: ref = Z_PTR_P(op1)
9570
0
      ref = jit_Z_PTR(jit, op1_addr);
9571
      // JIT: GC_ADDREF(ref)
9572
0
      jit_GC_ADDREF(jit, ref);
9573
      // JIT: ZVAL_REFERENCE(arg, ref)
9574
0
      jit_set_Z_PTR(jit, arg_addr, ref);
9575
0
      jit_set_Z_TYPE_INFO(jit, arg_addr, IS_REFERENCE_EX);
9576
0
      ref_path = ir_END();
9577
0
      ir_IF_FALSE(if_ref);
9578
0
    }
9579
9580
    // JIT: ZVAL_NEW_REF(arg, varptr);
9581
    // JIT: ref = emalloc(sizeof(zend_reference));
9582
0
    ref = jit_EMALLOC(jit, sizeof(zend_reference), op_array, opline);
9583
    // JIT: GC_REFCOUNT(ref) = 2
9584
0
    jit_set_GC_REFCOUNT(jit, ref, 2);
9585
    // JIT: GC_TYPE(ref) = GC_REFERENCE
9586
0
    ir_STORE(ir_ADD_OFFSET(ref, offsetof(zend_reference, gc.u.type_info)), ir_CONST_U32(GC_REFERENCE));
9587
0
    ir_STORE(ir_ADD_OFFSET(ref, offsetof(zend_reference, sources.ptr)), IR_NULL);
9588
0
    ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
9589
0
    ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
9590
9591
        // JIT: ZVAL_COPY_VALUE(&ref->val, op1)
9592
0
    jit_ZVAL_COPY(jit,
9593
0
      ref_addr,
9594
0
      MAY_BE_ANY,
9595
0
      op1_addr, op1_info, false);
9596
9597
    // JIT: ZVAL_REFERENCE(arg, ref)
9598
0
    jit_set_Z_PTR(jit, op1_addr, ref);
9599
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
9600
9601
    // JIT: ZVAL_REFERENCE(arg, ref)
9602
0
    jit_set_Z_PTR(jit, arg_addr, ref);
9603
0
    jit_set_Z_TYPE_INFO(jit, arg_addr, IS_REFERENCE_EX);
9604
0
  }
9605
9606
0
  if (ref_path) {
9607
0
    ir_MERGE_WITH(ref_path);
9608
0
  }
9609
9610
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
9611
9612
0
  return 1;
9613
0
}
9614
9615
static int zend_jit_send_var(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, uint32_t op1_info, zend_jit_addr op1_addr, zend_jit_addr op1_def_addr)
9616
0
{
9617
0
  uint32_t arg_num = opline->op2.num;
9618
0
  zend_jit_addr arg_addr;
9619
0
  ir_ref end_inputs = IR_UNUSED;
9620
9621
0
  ZEND_ASSERT((opline->opcode != ZEND_SEND_VAR_EX &&
9622
0
       opline->opcode != ZEND_SEND_VAR_NO_REF_EX) ||
9623
0
      arg_num <= MAX_ARG_FLAG_NUM);
9624
9625
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9626
9627
0
  if (!zend_jit_reuse_ip(jit)) {
9628
0
    return 0;
9629
0
  }
9630
9631
0
  if (opline->opcode == ZEND_SEND_VAR_EX) {
9632
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9633
0
     && JIT_G(current_frame)
9634
0
     && JIT_G(current_frame)->call
9635
0
     && JIT_G(current_frame)->call->func) {
9636
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9637
0
        if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 0)) {
9638
0
          return 0;
9639
0
        }
9640
0
        return 1;
9641
0
      }
9642
0
    } else {
9643
0
      uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9644
9645
      // JIT: if (RX->func->quick_arg_flags & mask)
9646
0
      ir_ref if_send_by_ref = ir_IF(ir_AND_U32(
9647
0
        ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_RX(func)), offsetof(zend_function, quick_arg_flags))),
9648
0
        ir_CONST_U32(mask)));
9649
0
      ir_IF_TRUE_cold(if_send_by_ref);
9650
9651
0
      if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 1)) {
9652
0
        return 0;
9653
0
      }
9654
9655
0
      ir_END_list(end_inputs);
9656
0
      ir_IF_FALSE(if_send_by_ref);
9657
0
    }
9658
0
  } else if (opline->opcode == ZEND_SEND_VAR_NO_REF_EX) {
9659
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9660
0
     && JIT_G(current_frame)
9661
0
     && JIT_G(current_frame)->call
9662
0
     && JIT_G(current_frame)->call->func) {
9663
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9664
9665
            // JIT: ZVAL_COPY_VALUE(arg, op1)
9666
0
        jit_ZVAL_COPY(jit,
9667
0
          arg_addr,
9668
0
          MAY_BE_ANY,
9669
0
          op1_addr, op1_info, false);
9670
9671
0
        if (!ARG_MAY_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9672
0
          if (!(op1_info & MAY_BE_REF)) {
9673
            /* Don't generate code that always throws exception */
9674
0
            return 0;
9675
0
          } else {
9676
0
            int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9677
0
            const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9678
0
            if (!exit_addr) {
9679
0
              return 0;
9680
0
            }
9681
9682
            // JIT: if (Z_TYPE_P(op1) != IS_REFERENCE)
9683
0
            ir_GUARD(ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U32(IS_REFERENCE)),
9684
0
              ir_CONST_ADDR(exit_addr));
9685
0
          }
9686
0
        }
9687
0
        return 1;
9688
0
      }
9689
0
    } else {
9690
0
      uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9691
0
      ir_ref func, if_send_by_ref, if_prefer_ref;
9692
9693
      // JIT: if (RX->func->quick_arg_flags & mask)
9694
0
      func = ir_LOAD_A(jit_RX(func));
9695
0
      if_send_by_ref = ir_IF(ir_AND_U32(
9696
0
        ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, quick_arg_flags))),
9697
0
        ir_CONST_U32(mask)));
9698
0
      ir_IF_TRUE_cold(if_send_by_ref);
9699
9700
0
      mask = ZEND_SEND_PREFER_REF << ((arg_num + 3) * 2);
9701
9702
          // JIT: ZVAL_COPY_VALUE(arg, op1)
9703
0
      jit_ZVAL_COPY(jit,
9704
0
        arg_addr,
9705
0
        MAY_BE_ANY,
9706
0
        op1_addr, op1_info, false);
9707
9708
0
      if (op1_info & MAY_BE_REF) {
9709
0
        ir_ref if_ref = jit_if_Z_TYPE(jit, arg_addr, IS_REFERENCE);
9710
0
        ir_IF_TRUE(if_ref);
9711
0
        ir_END_list(end_inputs);
9712
0
        ir_IF_FALSE(if_ref);
9713
0
      }
9714
9715
      // JIT: if (RX->func->quick_arg_flags & mask)
9716
0
      if_prefer_ref = ir_IF(ir_AND_U32(
9717
0
        ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, quick_arg_flags))),
9718
0
        ir_CONST_U32(mask)));
9719
0
      ir_IF_TRUE(if_prefer_ref);
9720
0
      ir_END_list(end_inputs);
9721
0
      ir_IF_FALSE(if_prefer_ref);
9722
9723
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9724
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9725
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9726
0
        if (!exit_addr) {
9727
0
          return 0;
9728
0
        }
9729
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
9730
0
      } else {
9731
0
        jit_SET_EX_OPLINE(jit, opline);
9732
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_only_vars_by_reference),
9733
0
          jit_ZVAL_ADDR(jit, arg_addr));
9734
0
        zend_jit_check_exception(jit);
9735
0
        ir_END_list(end_inputs);
9736
0
      }
9737
9738
0
      ir_IF_FALSE(if_send_by_ref);
9739
0
    }
9740
0
  } else if (opline->opcode == ZEND_SEND_FUNC_ARG) {
9741
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9742
0
     && JIT_G(current_frame)
9743
0
     && JIT_G(current_frame)->call
9744
0
     && JIT_G(current_frame)->call->func) {
9745
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9746
0
        if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 0)) {
9747
0
          return 0;
9748
0
        }
9749
0
        return 1;
9750
0
      }
9751
0
    } else {
9752
      // JIT: if (RX->This.u1.type_info & ZEND_CALL_SEND_ARG_BY_REF)
9753
0
      ir_ref if_send_by_ref = ir_IF(ir_AND_U32(
9754
0
        ir_LOAD_U32(jit_RX(This.u1.type_info)),
9755
0
        ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9756
0
      ir_IF_TRUE_cold(if_send_by_ref);
9757
9758
0
      if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 1)) {
9759
0
        return 0;
9760
0
      }
9761
9762
0
      ir_END_list(end_inputs);
9763
0
      ir_IF_FALSE(if_send_by_ref);
9764
0
    }
9765
0
  }
9766
9767
0
  if (op1_info & MAY_BE_UNDEF) {
9768
0
    ir_ref ref, if_def = IR_UNUSED;
9769
9770
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9771
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
9772
0
      ir_IF_FALSE_cold(if_def);
9773
0
    }
9774
9775
    // JIT: zend_jit_undefined_op_helper(opline->op1.var)
9776
0
    jit_SET_EX_OPLINE(jit, opline);
9777
0
    ref = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
9778
0
      ir_CONST_U32(opline->op1.var));
9779
9780
    // JIT: ZVAL_NULL(arg)
9781
0
    jit_set_Z_TYPE_INFO(jit, arg_addr, IS_NULL);
9782
9783
    // JIT: check_exception
9784
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9785
9786
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9787
0
      ir_END_list(end_inputs);
9788
0
      ir_IF_TRUE(if_def);
9789
0
    } else {
9790
0
      if (end_inputs) {
9791
0
        ir_END_list(end_inputs);
9792
0
        ir_MERGE_list(end_inputs);
9793
0
      }
9794
0
      return 1;
9795
0
    }
9796
0
  }
9797
9798
0
  if (opline->opcode == ZEND_SEND_VAR_NO_REF) {
9799
        // JIT: ZVAL_COPY_VALUE(arg, op1)
9800
0
    jit_ZVAL_COPY(jit,
9801
0
      arg_addr,
9802
0
      MAY_BE_ANY,
9803
0
      op1_addr, op1_info, false);
9804
0
    if (op1_info & MAY_BE_REF) {
9805
        // JIT: if (Z_TYPE_P(arg) == IS_REFERENCE)
9806
0
        ir_ref if_ref = jit_if_Z_TYPE(jit, arg_addr, IS_REFERENCE);
9807
0
        ir_IF_TRUE(if_ref);
9808
0
        ir_END_list(end_inputs);
9809
0
        ir_IF_FALSE(if_ref);
9810
0
    }
9811
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9812
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9813
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9814
0
      if (!exit_addr) {
9815
0
        return 0;
9816
0
      }
9817
0
      ir_GUARD(IR_FALSE, ir_CONST_ADDR(exit_addr));
9818
0
    } else {
9819
0
      jit_SET_EX_OPLINE(jit, opline);
9820
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_only_vars_by_reference),
9821
0
        jit_ZVAL_ADDR(jit, arg_addr));
9822
0
      zend_jit_check_exception(jit);
9823
0
    }
9824
0
  } else {
9825
0
    if (op1_info & MAY_BE_REF) {
9826
0
      if (opline->op1_type == IS_CV) {
9827
0
        ir_ref ref;
9828
9829
        // JIT: ZVAL_DEREF(op1)
9830
0
        ref = jit_ZVAL_ADDR(jit, op1_addr);
9831
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
9832
0
        op1_addr = ZEND_ADDR_REF_ZVAL(ref);
9833
9834
            // JIT: ZVAL_COPY(arg, op1)
9835
0
        jit_ZVAL_COPY(jit,
9836
0
          arg_addr,
9837
0
          MAY_BE_ANY,
9838
0
          op1_addr, op1_info, true);
9839
0
      } else {
9840
0
        ir_ref if_ref, ref, ref2, refcount, if_not_zero, if_refcounted;
9841
0
        zend_jit_addr ref_addr;
9842
9843
        // JIT: if (Z_TYPE_P(op1) == IS_REFERENCE)
9844
0
        if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9845
0
        ir_IF_TRUE_cold(if_ref);
9846
9847
        // JIT: ref = Z_COUNTED_P(op1);
9848
0
        ref = jit_Z_PTR(jit, op1_addr);
9849
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
9850
0
        ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
9851
9852
        // JIT: ZVAL_COPY_VALUE(arg, op1);
9853
0
        jit_ZVAL_COPY(jit,
9854
0
          arg_addr,
9855
0
          MAY_BE_ANY,
9856
0
          ref_addr, op1_info, false);
9857
9858
        // JIT: if (GC_DELREF(ref) != 0)
9859
0
        refcount = jit_GC_DELREF(jit, ref);
9860
0
        if_not_zero = ir_IF(refcount);
9861
0
        ir_IF_TRUE(if_not_zero);
9862
9863
                // JIT: if (Z_REFCOUNTED_P(arg)
9864
0
        if_refcounted = jit_if_REFCOUNTED(jit, arg_addr);
9865
0
        ir_IF_TRUE(if_refcounted);
9866
        // JIT: Z_ADDREF_P(arg)
9867
0
        jit_GC_ADDREF(jit, jit_Z_PTR(jit, arg_addr));
9868
0
        ir_END_list(end_inputs);
9869
0
        ir_IF_FALSE(if_refcounted);
9870
0
        ir_END_list(end_inputs);
9871
9872
0
        ir_IF_FALSE(if_not_zero);
9873
9874
        // JIT: efree(ref)
9875
0
        jit_EFREE(jit, ref, sizeof(zend_reference), op_array, opline);
9876
0
        ir_END_list(end_inputs);
9877
9878
0
        ir_IF_FALSE(if_ref);
9879
9880
        // JIT: ZVAL_COPY_VALUE(arg, op1);
9881
0
        jit_ZVAL_COPY(jit,
9882
0
          arg_addr,
9883
0
          MAY_BE_ANY,
9884
0
          op1_addr, op1_info, false);
9885
0
      }
9886
0
    } else {
9887
0
      if (op1_addr != op1_def_addr) {
9888
0
        if (!zend_jit_update_regs(jit, opline->op1.var, op1_addr, op1_def_addr, op1_info)) {
9889
0
          return 0;
9890
0
        }
9891
0
        if (Z_MODE(op1_def_addr) == IS_REG && Z_MODE(op1_addr) != IS_REG) {
9892
0
          op1_addr = op1_def_addr;
9893
0
        }
9894
0
      }
9895
9896
          // JIT: ZVAL_COPY_VALUE(arg, op1)
9897
0
      jit_ZVAL_COPY(jit,
9898
0
        arg_addr,
9899
0
        MAY_BE_ANY,
9900
0
        op1_addr, op1_info, opline->op1_type == IS_CV);
9901
0
    }
9902
0
  }
9903
9904
0
  if (end_inputs) {
9905
0
    ir_END_list(end_inputs);
9906
0
    ir_MERGE_list(end_inputs);
9907
0
  }
9908
9909
0
  return 1;
9910
0
}
9911
9912
static int zend_jit_check_func_arg(zend_jit_ctx *jit, const zend_op *opline)
9913
0
{
9914
0
  uint32_t arg_num = opline->op2.num;
9915
0
  ir_ref ref;
9916
9917
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9918
0
   && JIT_G(current_frame)
9919
0
   && JIT_G(current_frame)->call
9920
0
   && JIT_G(current_frame)->call->func) {
9921
0
    if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9922
0
      if (!TRACE_FRAME_IS_LAST_SEND_BY_REF(JIT_G(current_frame)->call)) {
9923
0
        TRACE_FRAME_SET_LAST_SEND_BY_REF(JIT_G(current_frame)->call);
9924
        // JIT: ZEND_ADD_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9925
0
        if (jit->reuse_ip) {
9926
0
          ref = jit_IP(jit);
9927
0
        } else {
9928
0
          ref = ir_LOAD_A(jit_EX(call));
9929
0
        }
9930
0
        ref = jit_CALL(ref, This.u1.type_info);
9931
0
        ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9932
0
      }
9933
0
    } else {
9934
0
      if (!TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
9935
0
        TRACE_FRAME_SET_LAST_SEND_BY_VAL(JIT_G(current_frame)->call);
9936
        // JIT: ZEND_DEL_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9937
0
        if (jit->reuse_ip) {
9938
0
          ref = jit_IP(jit);
9939
0
        } else {
9940
0
          ref = ir_LOAD_A(jit_EX(call));
9941
0
        }
9942
0
        ref = jit_CALL(ref, This.u1.type_info);
9943
0
        ir_STORE(ref, ir_AND_U32(ir_LOAD_U32(ref), ir_CONST_U32(~ZEND_CALL_SEND_ARG_BY_REF)));
9944
0
      }
9945
0
    }
9946
0
  } else {
9947
    // JIT: if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(EX(call)->func, arg_num)) {
9948
0
    uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9949
0
    ir_ref rx, if_ref, cold_path;
9950
9951
0
    if (!zend_jit_reuse_ip(jit)) {
9952
0
      return 0;
9953
0
    }
9954
9955
0
    rx = jit_IP(jit);
9956
9957
0
    ref = ir_AND_U32(
9958
0
      ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_CALL(rx, func)), offsetof(zend_function, quick_arg_flags))),
9959
0
      ir_CONST_U32(mask));
9960
0
    if_ref = ir_IF(ref);
9961
0
    ir_IF_TRUE_cold(if_ref);
9962
9963
    // JIT: ZEND_ADD_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9964
0
    ref = jit_CALL(rx, This.u1.type_info);
9965
0
    ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9966
9967
0
    cold_path = ir_END();
9968
0
    ir_IF_FALSE(if_ref);
9969
9970
    // JIT: ZEND_DEL_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9971
0
    ref = jit_CALL(rx, This.u1.type_info);
9972
0
    ir_STORE(ref, ir_AND_U32(ir_LOAD_U32(ref), ir_CONST_U32(~ZEND_CALL_SEND_ARG_BY_REF)));
9973
9974
0
    ir_MERGE_WITH(cold_path);
9975
0
  }
9976
9977
0
  return 1;
9978
0
}
9979
9980
static int zend_jit_check_undef_args(zend_jit_ctx *jit, const zend_op *opline)
9981
0
{
9982
0
  ir_ref call, if_may_have_undef, ret;
9983
9984
0
  if (jit->reuse_ip) {
9985
0
    call = jit_IP(jit);
9986
0
  } else {
9987
0
    call = ir_LOAD_A(jit_EX(call));
9988
0
  }
9989
9990
0
  if_may_have_undef = ir_IF(ir_AND_U8(
9991
0
    ir_LOAD_U8(ir_ADD_OFFSET(call, offsetof(zend_execute_data, This.u1.type_info) + 3)),
9992
0
    ir_CONST_U8(ZEND_CALL_MAY_HAVE_UNDEF >> 24)));
9993
9994
0
  ir_IF_TRUE_cold(if_may_have_undef);
9995
0
  jit_SET_EX_OPLINE(jit, opline);
9996
0
  ret = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_handle_undef_args), call);
9997
0
  ir_GUARD_NOT(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9998
0
  ir_MERGE_WITH_EMPTY_FALSE(if_may_have_undef);
9999
10000
0
  return 1;
10001
0
}
10002
10003
static int zend_jit_do_fcall(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, zend_ssa *ssa, int call_level, unsigned int next_block, zend_jit_trace_rec *trace)
10004
0
{
10005
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
10006
0
  zend_call_info *call_info = NULL;
10007
0
  const zend_function *func = NULL;
10008
0
  uint32_t i;
10009
0
  uint32_t call_num_args = 0;
10010
0
  bool unknown_num_args = false;
10011
0
  const void *exit_addr = NULL;
10012
0
  const zend_op *prev_opline;
10013
0
  ir_ref rx, func_ref = IR_UNUSED, if_user = IR_UNUSED, user_path = IR_UNUSED;
10014
10015
0
  prev_opline = opline - 1;
10016
0
  while (prev_opline->opcode == ZEND_EXT_FCALL_BEGIN || prev_opline->opcode == ZEND_TICKS) {
10017
0
    prev_opline--;
10018
0
  }
10019
0
  if (prev_opline->opcode == ZEND_SEND_UNPACK || prev_opline->opcode == ZEND_SEND_ARRAY ||
10020
0
      prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS) {
10021
0
    unknown_num_args = true;
10022
0
  }
10023
10024
0
  if (info) {
10025
0
    call_info = info->callee_info;
10026
0
    while (call_info && call_info->caller_call_opline != opline) {
10027
0
      call_info = call_info->next_callee;
10028
0
    }
10029
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
10030
0
      func = call_info->callee_func;
10031
0
    }
10032
0
    if ((op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)
10033
0
     && (!JIT_G(current_frame)
10034
0
      || !JIT_G(current_frame)->call
10035
0
      || !JIT_G(current_frame)->call->func)) {
10036
0
      call_info = NULL; func = NULL; /* megamorphic call from trait */
10037
0
    }
10038
0
  }
10039
0
  if (!func) {
10040
    /* resolve function at run time */
10041
0
  } else if (func->type == ZEND_USER_FUNCTION) {
10042
0
    ZEND_ASSERT(opline->opcode != ZEND_DO_ICALL);
10043
0
    call_num_args = call_info->num_args;
10044
0
  } else if (func->type == ZEND_INTERNAL_FUNCTION) {
10045
0
    ZEND_ASSERT(opline->opcode != ZEND_DO_UCALL);
10046
0
    call_num_args = call_info->num_args;
10047
0
  } else {
10048
0
    ZEND_UNREACHABLE();
10049
0
  }
10050
10051
0
  if (trace && !func) {
10052
0
    if (trace->op == ZEND_JIT_TRACE_DO_ICALL) {
10053
0
      ZEND_ASSERT(!trace->func || trace->func->type == ZEND_INTERNAL_FUNCTION);
10054
0
#ifndef ZEND_WIN32
10055
      // TODO: ASLR may cause different addresses in different workers ???
10056
0
      func = trace->func;
10057
0
      if (JIT_G(current_frame) &&
10058
0
          JIT_G(current_frame)->call &&
10059
0
          TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call) >= 0) {
10060
0
        call_num_args = TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call);
10061
0
      } else {
10062
0
        unknown_num_args = true;
10063
0
      }
10064
0
#endif
10065
0
    } else if (trace->op == ZEND_JIT_TRACE_ENTER) {
10066
0
      ZEND_ASSERT(trace->func->type == ZEND_USER_FUNCTION);
10067
0
      if (zend_accel_in_shm(trace->func->op_array.opcodes)) {
10068
0
        func = trace->func;
10069
0
        if (JIT_G(current_frame) &&
10070
0
            JIT_G(current_frame)->call &&
10071
0
            TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call) >= 0) {
10072
0
          call_num_args = TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call);
10073
0
        } else {
10074
0
          unknown_num_args = true;
10075
0
        }
10076
0
      }
10077
0
    }
10078
0
  }
10079
10080
0
  bool may_have_extra_named_params =
10081
0
    opline->extended_value == ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS &&
10082
0
    (!func || func->common.fn_flags & ZEND_ACC_VARIADIC);
10083
10084
0
  if (!jit->reuse_ip) {
10085
0
    zend_jit_start_reuse_ip(jit);
10086
    // JIT: call = EX(call);
10087
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(call)));
10088
0
  }
10089
0
  rx = jit_IP(jit);
10090
0
  zend_jit_stop_reuse_ip(jit);
10091
10092
0
  jit_SET_EX_OPLINE(jit, opline);
10093
10094
0
  if (opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) {
10095
0
    if (!func) {
10096
0
      if (trace) {
10097
0
        uint32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10098
10099
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10100
0
        if (!exit_addr) {
10101
0
          return 0;
10102
0
        }
10103
10104
0
        func_ref = ir_LOAD_A(jit_CALL(rx, func));
10105
0
        ir_GUARD_NOT(
10106
0
          ir_AND_U32(
10107
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10108
0
            ir_CONST_U32(ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD)),
10109
0
          ir_CONST_ADDR(exit_addr));
10110
0
      }
10111
0
    }
10112
0
  }
10113
10114
0
  if (!jit->delayed_call_level) {
10115
    // JIT: EX(call) = call->prev_execute_data;
10116
0
    ir_STORE(jit_EX(call),
10117
0
      (call_level == 1) ? IR_NULL : ir_LOAD_A(jit_CALL(rx, prev_execute_data)));
10118
0
  }
10119
0
  delayed_call_chain = false;
10120
0
  jit->delayed_call_level = 0;
10121
10122
  // JIT: call->prev_execute_data = execute_data;
10123
0
  ir_STORE(jit_CALL(rx, prev_execute_data), jit_FP(jit));
10124
10125
0
  if (!func) {
10126
0
    if (!func_ref) {
10127
0
      func_ref = ir_LOAD_A(jit_CALL(rx, func));
10128
0
    }
10129
0
  }
10130
10131
0
  if (opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) {
10132
0
    if (!func) {
10133
0
      if (!trace) {
10134
0
        ir_ref if_deprecated_nodiscard, ret;
10135
10136
0
        uint32_t no_discard = RETURN_VALUE_USED(opline) ? 0 : ZEND_ACC_NODISCARD;
10137
10138
0
        if_deprecated_nodiscard = ir_IF(ir_AND_U32(
10139
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10140
0
            ir_CONST_U32(ZEND_ACC_DEPRECATED|no_discard)));
10141
0
        ir_IF_TRUE_cold(if_deprecated_nodiscard);
10142
10143
0
        ir_ref helper = ir_CONST_FC_FUNC(no_discard ? zend_jit_deprecated_nodiscard_helper : zend_jit_deprecated_helper);
10144
0
        if (GCC_GLOBAL_REGS) {
10145
0
          ret = ir_CALL(IR_BOOL, helper);
10146
0
        } else {
10147
0
          ret = ir_CALL_1(IR_BOOL, helper, rx);
10148
0
        }
10149
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10150
0
        ir_MERGE_WITH_EMPTY_FALSE(if_deprecated_nodiscard);
10151
0
      }
10152
0
    } else {
10153
0
      if (func->common.fn_flags & ZEND_ACC_DEPRECATED) {
10154
0
        ir_ref ret;
10155
10156
0
        if (GCC_GLOBAL_REGS) {
10157
0
          ret = ir_CALL(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_deprecated_helper));
10158
0
        } else {
10159
0
          ret = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_deprecated_helper), rx);
10160
0
        }
10161
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10162
0
      }
10163
10164
0
      if ((func->common.fn_flags & ZEND_ACC_NODISCARD) && !RETURN_VALUE_USED(opline)) {
10165
0
        ir_ref ret;
10166
10167
0
        if (GCC_GLOBAL_REGS) {
10168
0
          ret = ir_CALL(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_nodiscard_helper));
10169
0
        } else {
10170
0
          ret = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_nodiscard_helper), rx);
10171
0
        }
10172
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10173
0
      }
10174
0
    }
10175
0
  }
10176
10177
0
  if (!func
10178
0
   && opline->opcode != ZEND_DO_UCALL
10179
0
   && opline->opcode != ZEND_DO_ICALL) {
10180
0
    ir_ref type_ref = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_function, type)));
10181
0
    if_user = ir_IF(ir_EQ(type_ref, ir_CONST_U8(ZEND_USER_FUNCTION)));
10182
0
    ir_IF_TRUE(if_user);
10183
0
  }
10184
10185
0
  if ((!func || func->type == ZEND_USER_FUNCTION)
10186
0
   && opline->opcode != ZEND_DO_ICALL) {
10187
0
    bool recursive_call_through_jmp = false;
10188
0
    uint32_t num_args = 0;
10189
10190
    // JIT: EX(call) = NULL;
10191
0
    ir_STORE(jit_CALL(rx, call), IR_NULL);
10192
10193
    // JIT: EX(return_value) = RETURN_VALUE_USED(opline) ? EX_VAR(opline->result.var) : 0;
10194
0
    ir_STORE(jit_CALL(rx, return_value),
10195
0
      RETURN_VALUE_USED(opline) ?
10196
0
        jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var)) :
10197
0
        IR_NULL);
10198
10199
    // JIT: EX_LOAD_RUN_TIME_CACHE(op_array);
10200
0
    if (!func || func->op_array.cache_size) {
10201
0
      ir_ref run_time_cache;
10202
10203
0
      if (func && op_array == &func->op_array) {
10204
        /* recursive call */
10205
0
        run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
10206
0
      } else if (func
10207
0
       && !(func->op_array.fn_flags & ZEND_ACC_CLOSURE)
10208
0
       && ZEND_MAP_PTR_IS_OFFSET(func->op_array.run_time_cache)) {
10209
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_CG(map_ptr_base)),
10210
0
          (uintptr_t)ZEND_MAP_PTR(func->op_array.run_time_cache)));
10211
0
      } else if ((func && (func->op_array.fn_flags & ZEND_ACC_CLOSURE)) ||
10212
0
          (JIT_G(current_frame) &&
10213
0
           JIT_G(current_frame)->call &&
10214
0
           TRACE_FRAME_IS_CLOSURE_CALL(JIT_G(current_frame)->call))) {
10215
        /* Closures always use direct pointers */
10216
0
        ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
10217
10218
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(local_func_ref, offsetof(zend_op_array, run_time_cache__ptr)));
10219
0
      } else {
10220
0
        ir_ref if_odd, run_time_cache2;
10221
0
        ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
10222
10223
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(local_func_ref, offsetof(zend_op_array, run_time_cache__ptr)));
10224
0
        if_odd = ir_IF(ir_AND_A(run_time_cache, ir_CONST_ADDR(1)));
10225
0
        ir_IF_TRUE(if_odd);
10226
10227
0
        run_time_cache2 = ir_LOAD_A(ir_ADD_A(run_time_cache, ir_LOAD_A(jit_CG(map_ptr_base))));
10228
10229
0
        ir_MERGE_WITH_EMPTY_FALSE(if_odd);
10230
0
        run_time_cache = ir_PHI_2(IR_ADDR, run_time_cache2, run_time_cache);
10231
0
      }
10232
10233
0
      ir_STORE(jit_CALL(rx, run_time_cache), run_time_cache);
10234
0
    }
10235
10236
    // JIT: EG(current_execute_data) = execute_data = call;
10237
0
    ir_STORE(jit_EG(current_execute_data), rx);
10238
0
    jit_STORE_FP(jit, rx);
10239
10240
    // JIT: opline = op_array->opcodes;
10241
0
    if (func && !unknown_num_args) {
10242
10243
0
      for (i = call_num_args; i < func->op_array.last_var; i++) {
10244
0
        uint32_t n = EX_NUM_TO_VAR(i);
10245
0
        zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, n);
10246
10247
0
        jit_set_Z_TYPE_INFO_ex(jit, var_addr, ir_CONST_U32(IS_UNDEF));
10248
0
      }
10249
10250
0
      if (call_num_args <= func->op_array.num_args) {
10251
0
        if (!trace || (trace->op == ZEND_JIT_TRACE_END
10252
0
         && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10253
0
          if ((func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0) {
10254
0
            if (trace) {
10255
0
              num_args = 0;
10256
0
            } else if (call_info) {
10257
0
              num_args = skip_valid_arguments(op_array, ssa, call_info);
10258
0
            } else {
10259
0
              num_args = call_num_args;
10260
0
            }
10261
0
          } else {
10262
0
            num_args = call_num_args;
10263
0
          }
10264
0
          if (zend_accel_in_shm(func->op_array.opcodes)) {
10265
0
            jit_LOAD_IP_ADDR(jit, func->op_array.opcodes + num_args);
10266
0
          } else {
10267
0
            if (!func_ref) {
10268
0
              func_ref = ir_LOAD_A(jit_CALL(rx, func));
10269
0
            }
10270
0
            ir_ref ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10271
0
            if (num_args) {
10272
0
              ip = ir_ADD_OFFSET(ip, num_args * sizeof(zend_op));
10273
0
            }
10274
0
            jit_STORE_IP(jit, ip);
10275
0
          }
10276
10277
0
          if (!trace && op_array == &func->op_array && call_num_args >= op_array->required_num_args) {
10278
            /* recursive call */
10279
0
            recursive_call_through_jmp = true;
10280
0
          }
10281
0
        }
10282
0
      } else {
10283
0
        ir_ref helper;
10284
0
        if (!trace || (trace->op == ZEND_JIT_TRACE_END
10285
0
         && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10286
0
          ir_ref ip;
10287
10288
0
          if (zend_accel_in_shm(func->op_array.opcodes)) {
10289
0
            ip = ir_CONST_ADDR(func->op_array.opcodes);
10290
0
          } else {
10291
0
            if (!func_ref) {
10292
0
              func_ref = ir_LOAD_A(jit_CALL(rx, func));
10293
0
            }
10294
0
            ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10295
0
          }
10296
0
          jit_STORE_IP(jit, ip);
10297
0
          helper = ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper);
10298
0
        } else {
10299
0
          helper = ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper_no_skip_recv);
10300
0
        }
10301
0
        if (GCC_GLOBAL_REGS) {
10302
0
          ir_CALL(IR_VOID, helper);
10303
0
        } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
10304
0
          ir_CALL_2(IR_ADDR, helper, jit_FP(jit), jit_IP(jit));
10305
0
        } else {
10306
0
          ir_ref ref = ir_CALL_2(IR_ADDR, helper, jit_FP(jit), jit_IP(jit));
10307
0
          jit_STORE_IP(jit, ref);
10308
0
        }
10309
0
      }
10310
0
    } else {
10311
0
      ir_ref ip;
10312
0
      ir_ref merge_inputs = IR_UNUSED;
10313
10314
      // JIT: opline = op_array->opcodes
10315
0
      if (func && zend_accel_in_shm(func->op_array.opcodes)) {
10316
0
        ip = ir_CONST_ADDR(func->op_array.opcodes);
10317
0
      } else {
10318
0
        if (!func_ref) {
10319
0
          func_ref = ir_LOAD_A(jit_CALL(rx, func));
10320
0
        }
10321
0
        ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10322
0
      }
10323
0
      jit_STORE_IP(jit, ip);
10324
10325
      // JIT: num_args = EX_NUM_ARGS();
10326
0
      ir_ref num_args, first_extra_arg;
10327
10328
0
      num_args = ir_LOAD_U32(jit_EX(This.u2.num_args));
10329
0
      if (func) {
10330
0
        first_extra_arg = ir_CONST_U32(func->op_array.num_args);
10331
0
      } else {
10332
        // JIT: first_extra_arg = op_array->num_args;
10333
0
        ZEND_ASSERT(func_ref);
10334
0
        first_extra_arg = ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, num_args)));
10335
0
      }
10336
10337
      // JIT: if (UNEXPECTED(num_args > first_extra_arg))
10338
0
      ir_ref if_extra_args = ir_IF(ir_GT(num_args, first_extra_arg));
10339
0
      ir_IF_TRUE_cold(if_extra_args);
10340
0
      if (GCC_GLOBAL_REGS) {
10341
0
        ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper));
10342
0
      } else {
10343
0
        ir_ref ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper), jit_FP(jit), jit_IP(jit));
10344
0
        jit_STORE_IP(jit, ref);
10345
0
      }
10346
0
      ir_END_list(merge_inputs);
10347
0
      ir_IF_FALSE(if_extra_args);
10348
0
      if (!func || (func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0) {
10349
0
        if (!func) {
10350
          // JIT: if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0))
10351
0
          ir_ref if_has_type_hints = ir_IF(ir_AND_U32(
10352
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10353
0
            ir_CONST_U32(ZEND_ACC_HAS_TYPE_HINTS)));
10354
0
          ir_IF_TRUE(if_has_type_hints);
10355
0
          ir_END_list(merge_inputs);
10356
0
          ir_IF_FALSE(if_has_type_hints);
10357
0
        }
10358
        // JIT: opline += num_args;
10359
10360
0
        ir_ref ref = ir_MUL_U32(num_args, ir_CONST_U32(sizeof(zend_op)));
10361
10362
0
        if (sizeof(void*) == 8) {
10363
0
          ref = ir_ZEXT_A(ref);
10364
0
        }
10365
10366
0
        jit_STORE_IP(jit, ir_ADD_A(jit_IP(jit), ref));
10367
0
      }
10368
10369
0
      ir_END_list(merge_inputs);
10370
0
      ir_MERGE_list(merge_inputs);
10371
10372
      // JIT: if (EXPECTED((int)num_args < op_array->last_var)) {
10373
0
      ir_ref last_var;
10374
10375
0
      if (func) {
10376
0
        last_var = ir_CONST_U32(func->op_array.last_var);
10377
0
      } else {
10378
0
        ZEND_ASSERT(func_ref);
10379
0
        last_var = ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, last_var)));
10380
0
      }
10381
10382
0
      ir_ref idx = ir_SUB_U32(last_var, num_args);
10383
0
      ir_ref if_need = ir_IF(ir_GT(idx, ir_CONST_U32(0)));
10384
0
      ir_IF_TRUE(if_need);
10385
10386
      // JIT: zval *var = EX_VAR_NUM(num_args);
10387
0
      if (sizeof(void*) == 8) {
10388
0
        num_args = ir_ZEXT_A(num_args);
10389
0
      }
10390
0
      ir_ref var_ref = ir_ADD_OFFSET(
10391
0
        ir_ADD_A(jit_FP(jit), ir_MUL_A(num_args, ir_CONST_ADDR(sizeof(zval)))),
10392
0
        (ZEND_CALL_FRAME_SLOT * sizeof(zval)) + offsetof(zval, u1.type_info));
10393
10394
0
      ir_ref loop = ir_LOOP_BEGIN(ir_END());
10395
0
      var_ref = ir_PHI_2(IR_ADDR, var_ref, IR_UNUSED);
10396
0
      idx = ir_PHI_2(IR_U32, idx, IR_UNUSED);
10397
0
      ir_STORE(var_ref, ir_CONST_I32(IS_UNDEF));
10398
0
      ir_PHI_SET_OP(var_ref, 2, ir_ADD_OFFSET(var_ref, sizeof(zval)));
10399
0
      ir_ref idx2 = ir_SUB_U32(idx, ir_CONST_U32(1));
10400
0
      ir_PHI_SET_OP(idx, 2, idx2);
10401
0
      ir_ref if_not_zero = ir_IF(idx2);
10402
0
      ir_IF_TRUE(if_not_zero);
10403
0
      ir_MERGE_SET_OP(loop, 2, ir_LOOP_END());
10404
0
      ir_IF_FALSE(if_not_zero);
10405
0
      ir_MERGE_WITH_EMPTY_FALSE(if_need);
10406
0
    }
10407
10408
0
    if (ZEND_OBSERVER_ENABLED && (!func || (func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0)) {
10409
0
      ir_ref observer_handler;
10410
0
      ir_ref rx = jit_FP(jit);
10411
0
      struct jit_observer_fcall_is_unobserved_data unobserved_data = jit_observer_fcall_is_unobserved_start(jit, func, &observer_handler, rx, func_ref);
10412
0
      if (trace && (trace->op != ZEND_JIT_TRACE_END || trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10413
0
        ZEND_ASSERT(trace[1].op == ZEND_JIT_TRACE_VM || trace[1].op == ZEND_JIT_TRACE_END);
10414
0
        jit_SET_EX_OPLINE(jit, trace[1].opline);
10415
0
      } else {
10416
        // EX(opline) = opline
10417
0
        ir_STORE(jit_EX(opline), jit_IP(jit));
10418
0
      }
10419
0
      jit_observer_fcall_begin(jit, rx, observer_handler);
10420
10421
0
      if (trace) {
10422
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10423
10424
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10425
0
        if (!exit_addr) {
10426
0
          return 0;
10427
0
        }
10428
0
      } else {
10429
0
        exit_addr = NULL;
10430
0
      }
10431
10432
0
      zend_jit_check_timeout(jit, NULL /* we're inside the called function */, exit_addr);
10433
10434
0
      jit_observer_fcall_is_unobserved_end(jit, &unobserved_data);
10435
0
    }
10436
10437
0
    if (trace) {
10438
0
      if (!func && (opline->opcode != ZEND_DO_UCALL)) {
10439
0
        user_path = ir_END();
10440
0
      }
10441
0
    } else {
10442
0
      zend_basic_block *bb;
10443
10444
0
      do {
10445
0
        if (recursive_call_through_jmp) {
10446
0
          ir_ref begin, end;
10447
0
          ir_insn *insn;
10448
10449
          /* attempt to convert direct recursive call into loop */
10450
0
          begin = jit->bb_start_ref[num_args];
10451
0
          ZEND_ASSERT(begin != IR_UNUSED);
10452
0
          insn = &jit->ctx.ir_base[begin];
10453
0
          if (insn->op == IR_BEGIN) {
10454
0
            end = ir_LOOP_END();
10455
0
            insn = &jit->ctx.ir_base[begin];
10456
0
            insn->op = IR_LOOP_BEGIN;
10457
0
            insn->inputs_count = 2;
10458
0
            insn->op2 = end;
10459
0
            break;
10460
0
          } else if ((insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN)
10461
0
              && insn->inputs_count == 2) {
10462
0
            end = ir_LOOP_END();
10463
0
            insn = &jit->ctx.ir_base[begin];
10464
0
            insn->op = IR_LOOP_BEGIN;
10465
0
            insn->inputs_count = 3;
10466
0
            insn->op3 = end;
10467
0
            break;
10468
0
          } else if (insn->op == IR_LOOP_BEGIN && insn->inputs_count == 3) {
10469
0
            ZEND_ASSERT(jit->ctx.ir_base[insn->op3].op == IR_LOOP_END);
10470
0
            jit->ctx.ir_base[insn->op3].op = IR_END;
10471
0
            ir_MERGE_2(insn->op3, ir_END());
10472
0
            end = ir_LOOP_END();
10473
0
            insn = &jit->ctx.ir_base[begin];
10474
0
            insn->op3 = end;
10475
0
            break;
10476
0
          }
10477
0
        }
10478
        /* fallback to indirect JMP or RETURN */
10479
0
        if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
10480
0
          zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
10481
0
        } else {
10482
0
          zend_jit_vm_enter(jit, jit_IP(jit));
10483
0
        }
10484
0
      } while (0);
10485
10486
0
      bb = &jit->ssa->cfg.blocks[jit->b];
10487
0
      if (bb->successors_count > 0) {
10488
0
        int succ;
10489
0
        ir_ref ref;
10490
10491
0
        ZEND_ASSERT(bb->successors_count == 1);
10492
0
        succ = bb->successors[0];
10493
        /* Add a fake control edge from UNREACHABLE/RETURN to the following ENTRY */
10494
0
        ref = jit->ctx.insns_count - 1;
10495
0
        ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_UNREACHABLE
10496
0
          || jit->ctx.ir_base[ref].op == IR_RETURN
10497
0
          || jit->ctx.ir_base[ref].op == IR_LOOP_END);
10498
0
        ZEND_ASSERT(jit->ssa->cfg.blocks[succ].flags & ZEND_BB_ENTRY);
10499
0
        ref = zend_jit_continue_entry(jit, ref, jit->ssa->cfg.blocks[succ].start);
10500
0
        if (func || (opline->opcode == ZEND_DO_UCALL)) {
10501
0
          _zend_jit_add_predecessor_ref(jit, succ, jit->b, ref);
10502
0
          jit->b = -1;
10503
0
        } else {
10504
0
          user_path = ref;
10505
0
        }
10506
0
      }
10507
0
    }
10508
0
  }
10509
10510
0
  if ((!func || func->type == ZEND_INTERNAL_FUNCTION)
10511
0
   && (opline->opcode != ZEND_DO_UCALL)) {
10512
0
    if (!func && (opline->opcode != ZEND_DO_ICALL)) {
10513
0
      ir_IF_FALSE(if_user);
10514
0
    }
10515
10516
    // JIT: EG(current_execute_data) = execute_data;
10517
0
    ir_STORE(jit_EG(current_execute_data), rx);
10518
10519
0
    bool may_have_observer = ZEND_OBSERVER_ENABLED && (!func || (func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0);
10520
0
    if (may_have_observer) {
10521
0
      ir_ref observer_handler;
10522
0
      struct jit_observer_fcall_is_unobserved_data unobserved_data = jit_observer_fcall_is_unobserved_start(jit, func, &observer_handler, rx, func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func)));
10523
0
      jit_observer_fcall_begin(jit, rx, observer_handler);
10524
0
      jit_observer_fcall_is_unobserved_end(jit, &unobserved_data);
10525
0
    }
10526
10527
    // JIT: ZVAL_NULL(EX_VAR(opline->result.var));
10528
0
    ir_ref res_addr = IR_UNUSED, func_ptr;
10529
10530
0
    if (RETURN_VALUE_USED(opline)) {
10531
0
      res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10532
0
    } else {
10533
      /* CPU stack allocated temporary zval */
10534
0
      ir_ref ptr;
10535
10536
0
      if (!jit->ctx.fixed_call_stack_size) {
10537
        // JIT: alloca(sizeof(void*));
10538
0
        ptr = ir_ALLOCA(ir_CONST_ADDR(sizeof(zval)));
10539
0
      } else {
10540
#ifdef _WIN64
10541
        ptr = ir_HARD_COPY_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
10542
#else
10543
0
        ptr = ir_HARD_COPY_A(ir_RLOAD_A(IR_REG_SP));
10544
0
#endif
10545
0
      }
10546
0
      res_addr = ZEND_ADDR_REF_ZVAL(ptr);
10547
0
    }
10548
10549
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
10550
10551
0
    zend_jit_reset_last_valid_opline(jit);
10552
10553
    // JIT: (zend_execute_internal ? zend_execute_internal : fbc->internal_function.handler)(call, ret);
10554
0
    ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
10555
0
    if (zend_execute_internal) {
10556
0
      ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_execute_internal), rx, res_ref);
10557
0
    } else {
10558
0
      if (func) {
10559
0
        func_ptr = ir_CONST_FC_FUNC(func->internal_function.handler);
10560
0
      } else {
10561
0
        func_ptr = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_internal_function, handler)));
10562
#if defined(IR_TARGET_X86)
10563
        func_ptr = ir_CAST_FC_FUNC(func_ptr);
10564
#endif
10565
0
      }
10566
0
      ir_CALL_2(IR_VOID, func_ptr, rx, res_ref);
10567
0
    }
10568
10569
0
    if (may_have_observer) {
10570
0
      jit_observer_fcall_end(jit, rx, res_ref);
10571
0
    }
10572
10573
    /* When zend_interrupt_function is set, it gets called while
10574
     * the frame is still on top. This is less efficient than
10575
     * doing it later once it's popped off. There is code further
10576
     * down that handles when there isn't an interrupt function.
10577
     */
10578
0
    if (zend_interrupt_function) {
10579
      // JIT: if (EG(vm_interrupt)) zend_fcall_interrupt(execute_data);
10580
0
      ir_ref if_interrupt = ir_IF(ir_LOAD_U8(jit_EG(vm_interrupt)));
10581
0
      ir_IF_TRUE_cold(if_interrupt);
10582
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_fcall_interrupt), rx);
10583
0
      ir_MERGE_WITH_EMPTY_FALSE(if_interrupt);
10584
0
    }
10585
10586
    // JIT: EG(current_execute_data) = execute_data;
10587
0
    ir_STORE(jit_EG(current_execute_data), jit_FP(jit));
10588
10589
    // JIT: zend_vm_stack_free_args(call);
10590
0
    if (func && !unknown_num_args) {
10591
0
      for (i = 0; i < call_num_args; i++ ) {
10592
0
        if (zend_jit_needs_arg_dtor(func, i, call_info)) {
10593
0
          uint32_t offset = EX_NUM_TO_VAR(i);
10594
0
          zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, offset);
10595
10596
0
          jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, false,
10597
0
                opline);
10598
0
        }
10599
0
      }
10600
0
    } else {
10601
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_vm_stack_free_args_helper), rx);
10602
0
    }
10603
10604
0
    if (may_have_extra_named_params) {
10605
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS))
10606
0
      ir_ref if_has_named = ir_IF(ir_AND_U8(
10607
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 3)),
10608
0
        ir_CONST_U8(ZEND_CALL_HAS_EXTRA_NAMED_PARAMS >> 24)));
10609
0
      ir_IF_TRUE_cold(if_has_named);
10610
10611
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_free_extra_named_params),
10612
0
        ir_LOAD_A(jit_CALL(rx, extra_named_params)));
10613
10614
0
      ir_MERGE_WITH_EMPTY_FALSE(if_has_named);
10615
0
    }
10616
10617
0
    if (opline->opcode == ZEND_DO_FCALL) {
10618
      // TODO: optimize ???
10619
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS))
10620
0
      ir_ref if_release_this = ir_IF(ir_AND_U8(
10621
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 2)),
10622
0
        ir_CONST_U8(ZEND_CALL_RELEASE_THIS >> 16)));
10623
0
      ir_IF_TRUE_cold(if_release_this);
10624
10625
      // JIT: OBJ_RELEASE(Z_OBJ(RX->This));
10626
0
      jit_OBJ_RELEASE(jit, ir_LOAD_A(jit_CALL(rx, This.value.obj)));
10627
10628
0
      ir_MERGE_WITH_EMPTY_FALSE(if_release_this);
10629
0
    }
10630
10631
10632
0
    ir_ref allocated_path = IR_UNUSED;
10633
10634
0
    if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
10635
0
        !JIT_G(current_frame) ||
10636
0
        !JIT_G(current_frame)->call ||
10637
0
        !TRACE_FRAME_IS_NESTED(JIT_G(current_frame)->call) ||
10638
0
        prev_opline->opcode == ZEND_SEND_UNPACK ||
10639
0
        prev_opline->opcode == ZEND_SEND_ARRAY ||
10640
0
      prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS) {
10641
10642
      // JIT: zend_vm_stack_free_call_frame(call);
10643
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_ALLOCATED))
10644
0
      ir_ref if_allocated = ir_IF(ir_AND_U8(
10645
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 2)),
10646
0
        ir_CONST_U8(ZEND_CALL_ALLOCATED >> 16)));
10647
0
      ir_IF_TRUE_cold(if_allocated);
10648
10649
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_free_call_frame), rx);
10650
10651
0
      allocated_path = ir_END();
10652
0
      ir_IF_FALSE(if_allocated);
10653
0
    }
10654
10655
0
    ir_STORE(jit_EG(vm_stack_top), rx);
10656
10657
0
    if (allocated_path) {
10658
0
      ir_MERGE_WITH(allocated_path);
10659
0
    }
10660
10661
0
    if (!RETURN_VALUE_USED(opline)) {
10662
0
      zend_class_entry *ce;
10663
0
      bool ce_is_instanceof;
10664
0
      uint32_t func_info = call_info ?
10665
0
        zend_get_func_info(call_info, ssa, &ce, &ce_is_instanceof) :
10666
0
        (MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN);
10667
10668
      /* If an exception is thrown, the return_value may stay at the
10669
       * original value of null. */
10670
0
      func_info |= MAY_BE_NULL;
10671
10672
0
      if (func_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
10673
0
        ir_ref sp;
10674
0
        if (!jit->ctx.fixed_call_stack_size) {
10675
0
          sp = ir_RLOAD_A(IR_REG_SP);
10676
0
        } else {
10677
#ifdef _WIN64
10678
          sp = jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS);
10679
#else
10680
0
          sp = ir_RLOAD_A(IR_REG_SP);
10681
0
#endif
10682
0
        }
10683
0
        res_addr = ZEND_ADDR_REF_ZVAL(sp);
10684
0
        jit_ZVAL_PTR_DTOR(jit, res_addr, func_info, true, opline);
10685
0
      }
10686
0
      if (!jit->ctx.fixed_call_stack_size) {
10687
        // JIT: revert alloca
10688
0
        ir_AFREE(ir_CONST_ADDR(sizeof(zval)));
10689
0
      }
10690
0
    }
10691
10692
    // JIT: if (UNEXPECTED(EG(exception) != NULL)) {
10693
0
    ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
10694
0
      jit_STUB_ADDR(jit, jit_stub_icall_throw));
10695
10696
    /* If there isn't a zend_interrupt_function, the timeout is
10697
     * handled here because it's more efficient.
10698
     */
10699
0
    if (!zend_interrupt_function) {
10700
      // TODO: Can we avoid checking for interrupts after each call ???
10701
0
      if (trace && jit->last_valid_opline != opline) {
10702
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline + 1, ZEND_JIT_EXIT_TO_VM);
10703
10704
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10705
0
        if (!exit_addr) {
10706
0
          return 0;
10707
0
        }
10708
0
      } else {
10709
0
        exit_addr = NULL;
10710
0
      }
10711
10712
0
      zend_jit_check_timeout(jit, opline + 1, exit_addr);
10713
0
    }
10714
10715
0
    if ((!trace || !func) && opline->opcode != ZEND_DO_ICALL) {
10716
0
      jit_LOAD_IP_ADDR(jit, opline + 1);
10717
0
    } else if (trace
10718
0
     && trace->op == ZEND_JIT_TRACE_END
10719
0
     && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
10720
0
      jit_LOAD_IP_ADDR(jit, opline + 1);
10721
0
    }
10722
0
  }
10723
10724
0
  if (user_path) {
10725
0
    ir_MERGE_WITH(user_path);
10726
0
  }
10727
10728
0
  return 1;
10729
0
}
10730
10731
static int zend_jit_constructor(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, zend_ssa *ssa, int call_level, int next_block)
10732
0
{
10733
0
  ir_ref if_skip_constructor = jit_IF_ex(jit, jit_CMP_IP(jit, IR_NE, opline), next_block);
10734
10735
0
  ir_IF_FALSE(if_skip_constructor);
10736
10737
0
  if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
10738
0
    if (!zend_jit_tail_handler(jit, opline)) {
10739
0
      return 0;
10740
0
    }
10741
0
  } else {
10742
0
    if (!zend_jit_do_fcall(jit, opline, op_array, ssa, call_level, next_block, NULL)) {
10743
0
      return 0;
10744
0
    }
10745
0
  }
10746
10747
  /* override predecessors of the next block */
10748
0
  ZEND_ASSERT(jit->ssa->cfg.blocks[next_block].predecessors_count == 1);
10749
0
  if (!jit->ctx.control) {
10750
0
    ZEND_ASSERT(jit->bb_edges[jit->bb_predecessors[next_block]]);
10751
0
    ir_IF_TRUE(if_skip_constructor);
10752
0
    ir_MERGE_2(jit->bb_edges[jit->bb_predecessors[next_block]], ir_END());
10753
0
    jit->bb_edges[jit->bb_predecessors[next_block]] = ir_END();
10754
0
  } else {
10755
0
    ZEND_ASSERT(!jit->bb_edges[jit->bb_predecessors[next_block]]);
10756
    /* merge current control path with the true branch of constructor skip condition */
10757
0
    ir_MERGE_WITH_EMPTY_TRUE(if_skip_constructor);
10758
0
    jit->bb_edges[jit->bb_predecessors[next_block]] = ir_END();
10759
10760
0
    jit->b = -1;
10761
0
  }
10762
10763
0
  return 1;
10764
0
}
10765
10766
static int zend_jit_verify_arg_type(zend_jit_ctx *jit, const zend_op *opline, zend_arg_info *arg_info, bool check_exception)
10767
0
{
10768
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10769
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type) & MAY_BE_ANY;
10770
0
  ir_ref ref, fast_path = IR_UNUSED;
10771
10772
0
  ref = jit_ZVAL_ADDR(jit, res_addr);
10773
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
10774
0
   && JIT_G(current_frame)
10775
0
   && JIT_G(current_frame)->prev) {
10776
0
    zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
10777
0
    uint8_t type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var));
10778
10779
0
    if (type != IS_UNKNOWN && (type_mask & (1u << type))) {
10780
0
      return 1;
10781
0
    }
10782
0
  }
10783
10784
0
  if (ZEND_ARG_SEND_MODE(arg_info)) {
10785
0
    if (opline->opcode == ZEND_RECV_INIT) {
10786
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
10787
0
    } else {
10788
0
      ref = jit_Z_PTR_ref(jit, ref);
10789
0
      ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
10790
0
    }
10791
0
  }
10792
10793
0
  if (type_mask != 0) {
10794
0
    if (is_power_of_two(type_mask)) {
10795
0
      uint32_t type_code = concrete_type(type_mask);
10796
0
      ir_ref if_ok = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(type_code));
10797
0
      ir_IF_TRUE(if_ok);
10798
0
      fast_path = ir_END();
10799
0
      ir_IF_FALSE_cold(if_ok);
10800
0
    } else {
10801
0
      ir_ref if_ok = ir_IF(ir_AND_U32(
10802
0
        ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE_ref(jit, ref)),
10803
0
        ir_CONST_U32(type_mask)));
10804
0
      ir_IF_TRUE(if_ok);
10805
0
      fast_path = ir_END();
10806
0
      ir_IF_FALSE_cold(if_ok);
10807
0
    }
10808
0
  }
10809
10810
0
  jit_SET_EX_OPLINE(jit, opline);
10811
0
  ref = ir_CALL_2(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_verify_arg_slow),
10812
0
    ref, ir_CONST_ADDR(arg_info));
10813
10814
0
  if (check_exception) {
10815
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10816
0
  }
10817
10818
0
  if (fast_path) {
10819
0
    ir_MERGE_WITH(fast_path);
10820
0
  }
10821
10822
0
  return 1;
10823
0
}
10824
10825
static int zend_jit_recv(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array)
10826
0
{
10827
0
  uint32_t arg_num = opline->op1.num;
10828
0
  zend_arg_info *arg_info = NULL;
10829
10830
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
10831
0
    if (EXPECTED(arg_num <= op_array->num_args)) {
10832
0
      arg_info = &op_array->arg_info[arg_num-1];
10833
0
    } else if (UNEXPECTED(op_array->fn_flags & ZEND_ACC_VARIADIC)) {
10834
0
      arg_info = &op_array->arg_info[op_array->num_args];
10835
0
    }
10836
0
    if (arg_info && !ZEND_TYPE_IS_SET(arg_info->type)) {
10837
0
      arg_info = NULL;
10838
0
    }
10839
0
  }
10840
10841
0
  if (arg_info || (opline+1)->opcode != ZEND_RECV) {
10842
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
10843
0
      if (!JIT_G(current_frame) ||
10844
0
          TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) < 0 ||
10845
0
          arg_num > TRACE_FRAME_NUM_ARGS(JIT_G(current_frame))) {
10846
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10847
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10848
10849
0
        if (!exit_addr) {
10850
0
          return 0;
10851
0
        }
10852
0
        ir_GUARD(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)),
10853
0
          ir_CONST_ADDR(exit_addr));
10854
0
      }
10855
0
    } else {
10856
0
      ir_ref if_ok =ir_IF(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)));
10857
0
      ir_IF_FALSE_cold(if_ok);
10858
10859
0
      jit_SET_EX_OPLINE(jit, opline);
10860
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_missing_arg_error), jit_FP(jit));
10861
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
10862
0
      ir_IF_TRUE(if_ok);
10863
0
    }
10864
0
  }
10865
10866
0
  if (arg_info) {
10867
0
    if (!zend_jit_verify_arg_type(jit, opline, arg_info, true)) {
10868
0
      return 0;
10869
0
    }
10870
0
  }
10871
10872
0
  return 1;
10873
0
}
10874
10875
static int zend_jit_recv_init(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, bool is_last, int may_throw)
10876
0
{
10877
0
  uint32_t arg_num = opline->op1.num;
10878
0
  zval *zv = RT_CONSTANT(opline, opline->op2);
10879
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10880
0
  ir_ref ref, if_fail, skip_path = IR_UNUSED;
10881
10882
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
10883
0
   && JIT_G(current_frame)
10884
0
   && TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) >= 0) {
10885
0
    if (arg_num > TRACE_FRAME_NUM_ARGS(JIT_G(current_frame))) {
10886
0
      jit_ZVAL_COPY_CONST(jit,
10887
0
        res_addr,
10888
0
        -1, -1,
10889
0
        zv, true);
10890
0
    }
10891
0
  } else {
10892
0
    if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
10893
0
        (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
10894
0
      ir_ref if_skip = ir_IF(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)));
10895
0
      ir_IF_TRUE(if_skip);
10896
0
      skip_path = ir_END();
10897
0
      ir_IF_FALSE(if_skip);
10898
0
    }
10899
0
    jit_ZVAL_COPY_CONST(jit,
10900
0
      res_addr,
10901
0
      -1, -1,
10902
0
      zv, true);
10903
0
  }
10904
10905
0
  if (Z_CONSTANT_P(zv)) {
10906
0
    jit_SET_EX_OPLINE(jit, opline);
10907
0
    ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zval_update_constant_ex),
10908
0
      jit_ZVAL_ADDR(jit, res_addr),
10909
0
      ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(func)), offsetof(zend_op_array, scope))));
10910
10911
0
    if_fail = ir_IF(ref);
10912
0
    ir_IF_TRUE_cold(if_fail);
10913
0
    jit_ZVAL_PTR_DTOR(jit, res_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, true, opline);
10914
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
10915
0
    ir_IF_FALSE(if_fail);
10916
0
  }
10917
10918
0
  if (skip_path) {
10919
0
    ir_MERGE_WITH(skip_path);
10920
0
  }
10921
10922
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
10923
0
    do {
10924
0
      zend_arg_info *arg_info;
10925
10926
0
      if (arg_num <= op_array->num_args) {
10927
0
        arg_info = &op_array->arg_info[arg_num-1];
10928
0
      } else if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
10929
0
        arg_info = &op_array->arg_info[op_array->num_args];
10930
0
      } else {
10931
0
        break;
10932
0
      }
10933
0
      if (!ZEND_TYPE_IS_SET(arg_info->type)) {
10934
0
        break;
10935
0
      }
10936
0
      if (!zend_jit_verify_arg_type(jit, opline, arg_info, may_throw)) {
10937
0
        return 0;
10938
0
      }
10939
0
    } while (0);
10940
0
  }
10941
10942
0
  return 1;
10943
0
}
10944
10945
static bool zend_jit_verify_return_type(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, uint32_t op1_info)
10946
0
{
10947
0
  zend_arg_info *arg_info = &op_array->arg_info[-1];
10948
0
  ZEND_ASSERT(ZEND_TYPE_IS_SET(arg_info->type));
10949
0
  zend_jit_addr op1_addr = OP1_ADDR();
10950
0
  bool needs_slow_check = true;
10951
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type) & MAY_BE_ANY;
10952
0
  ir_ref fast_path = IR_UNUSED;
10953
10954
0
  if (type_mask != 0) {
10955
0
    if (((op1_info & MAY_BE_ANY) & type_mask) == 0) {
10956
      /* pass */
10957
0
    } else if (((op1_info & MAY_BE_ANY) | type_mask) == type_mask) {
10958
0
      needs_slow_check = false;
10959
0
    } else if (is_power_of_two(type_mask)) {
10960
0
      uint32_t type_code = concrete_type(type_mask);
10961
0
      ir_ref if_ok = jit_if_Z_TYPE(jit, op1_addr, type_code);
10962
10963
0
      ir_IF_TRUE(if_ok);
10964
0
      fast_path = ir_END();
10965
0
      ir_IF_FALSE_cold(if_ok);
10966
0
    } else {
10967
0
      ir_ref if_ok = ir_IF(ir_AND_U32(
10968
0
        ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE(jit, op1_addr)),
10969
0
        ir_CONST_U32(type_mask)));
10970
10971
0
      ir_IF_TRUE(if_ok);
10972
0
      fast_path = ir_END();
10973
0
      ir_IF_FALSE_cold(if_ok);
10974
0
    }
10975
0
  }
10976
0
  if (needs_slow_check) {
10977
0
    ir_ref ref;
10978
10979
0
    jit_SET_EX_OPLINE(jit, opline);
10980
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
10981
0
    if (op1_info & MAY_BE_UNDEF) {
10982
0
      ref = zend_jit_zval_check_undef(jit, ref, opline->op1.var, NULL, true);
10983
0
    }
10984
10985
0
    ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_verify_return_slow),
10986
0
      ref,
10987
0
      ir_LOAD_A(jit_EX(func)),
10988
0
      ir_CONST_ADDR(arg_info));
10989
10990
0
    zend_jit_check_exception(jit);
10991
10992
0
    if (fast_path) {
10993
0
      ir_MERGE_WITH(fast_path);
10994
0
    }
10995
0
  }
10996
10997
0
  return true;
10998
0
}
10999
11000
static int zend_jit_leave_frame(zend_jit_ctx *jit)
11001
0
{
11002
  // JIT: EG(current_execute_data) = EX(prev_execute_data);
11003
0
  ir_STORE(jit_EG(current_execute_data), ir_LOAD_A(jit_EX(prev_execute_data)));
11004
0
  return 1;
11005
0
}
11006
11007
static int zend_jit_free_cvs(zend_jit_ctx *jit)
11008
0
{
11009
  // JIT: EG(current_execute_data) = EX(prev_execute_data);
11010
0
  ir_STORE(jit_EG(current_execute_data), ir_LOAD_A(jit_EX(prev_execute_data)));
11011
11012
  // JIT: zend_free_compiled_variables(execute_data);
11013
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_free_compiled_variables), jit_FP(jit));
11014
0
  return 1;
11015
0
}
11016
11017
static int zend_jit_free_cv(zend_jit_ctx *jit, uint32_t info, uint32_t var)
11018
0
{
11019
0
  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11020
0
    zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
11021
11022
0
    jit_ZVAL_PTR_DTOR(jit, var_addr, info, true, NULL);
11023
0
  }
11024
0
  return 1;
11025
0
}
11026
11027
static int zend_jit_free_op(zend_jit_ctx *jit, const zend_op *opline, uint32_t info, uint32_t var_offset)
11028
0
{
11029
0
  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11030
0
    jit_ZVAL_PTR_DTOR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, var_offset), info, false, opline);
11031
0
  }
11032
0
  return 1;
11033
0
}
11034
11035
static int zend_jit_leave_func(zend_jit_ctx         *jit,
11036
                               const zend_op_array  *op_array,
11037
                               const zend_op        *opline,
11038
                               uint32_t              op1_info,
11039
                               bool             left_frame,
11040
                               zend_jit_trace_rec   *trace,
11041
                               zend_jit_trace_info  *trace_info,
11042
                               int                   indirect_var_access,
11043
                               int                   may_throw)
11044
0
{
11045
0
  bool may_be_top_frame =
11046
0
    JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11047
0
    !JIT_G(current_frame) ||
11048
0
    !TRACE_FRAME_IS_NESTED(JIT_G(current_frame));
11049
0
  bool may_need_call_helper =
11050
0
    indirect_var_access || /* may have symbol table */
11051
0
    !op_array->function_name || /* may have symbol table */
11052
0
    may_be_top_frame ||
11053
0
    (op_array->fn_flags & ZEND_ACC_VARIADIC) || /* may have extra named args */
11054
0
    JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11055
0
    !JIT_G(current_frame) ||
11056
0
    TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) == -1 || /* unknown number of args */
11057
0
    (uint32_t)TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) > op_array->num_args; /* extra args */
11058
0
  bool may_need_release_this =
11059
0
    !(op_array->fn_flags & ZEND_ACC_CLOSURE) &&
11060
0
    op_array->scope &&
11061
0
    !(op_array->fn_flags & ZEND_ACC_STATIC) &&
11062
0
    (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11063
0
     !JIT_G(current_frame) ||
11064
0
     !TRACE_FRAME_NO_NEED_RELEASE_THIS(JIT_G(current_frame)));
11065
0
  ir_ref call_info = IR_UNUSED, ref, cold_path = IR_UNUSED;
11066
11067
0
  if (may_need_call_helper) {
11068
0
    if (!left_frame) {
11069
0
      left_frame = true;
11070
0
        if (!zend_jit_leave_frame(jit)) {
11071
0
        return 0;
11072
0
        }
11073
0
    }
11074
    /* ZEND_CALL_FAKE_CLOSURE handled on slow path to eliminate check for ZEND_CALL_CLOSURE on fast path */
11075
0
    call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
11076
0
    ref = ir_AND_U32(call_info,
11077
0
      ir_CONST_U32(ZEND_CALL_TOP|ZEND_CALL_HAS_SYMBOL_TABLE|ZEND_CALL_FREE_EXTRA_ARGS|ZEND_CALL_ALLOCATED|ZEND_CALL_HAS_EXTRA_NAMED_PARAMS|ZEND_CALL_FAKE_CLOSURE));
11078
0
    if (trace && trace->op != ZEND_JIT_TRACE_END) {
11079
0
      ir_ref if_slow = ir_IF(ref);
11080
11081
0
      ir_IF_TRUE_cold(if_slow);
11082
0
      if (!GCC_GLOBAL_REGS) {
11083
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_func_helper), jit_FP(jit), jit_IP(jit));
11084
0
      } else {
11085
0
        ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_func_helper));
11086
0
      }
11087
11088
0
      if (may_be_top_frame) {
11089
        // TODO: try to avoid this check ???
11090
0
        if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
11091
#if 0
11092
          /* this check should be handled by the following OPLINE guard */
11093
          | cmp IP, zend_jit_halt_op
11094
          | je ->trace_halt
11095
#endif
11096
0
        } else if (GCC_GLOBAL_REGS) {
11097
0
          ir_GUARD(jit_IP(jit), jit_STUB_ADDR(jit, jit_stub_trace_halt));
11098
0
        } else {
11099
0
          ir_GUARD(ir_NE(ref, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)), jit_STUB_ADDR(jit, jit_stub_trace_halt));
11100
0
          jit_STORE_IP(jit, ref);
11101
0
        }
11102
0
      }
11103
11104
0
      if (!GCC_GLOBAL_REGS) {
11105
        // execute_data = EG(current_execute_data)
11106
0
        jit_STORE_FP(jit, ir_LOAD_A(jit_EG(current_execute_data)));
11107
0
      }
11108
0
      cold_path = ir_END();
11109
0
      ir_IF_FALSE(if_slow);
11110
0
    } else {
11111
0
      ir_GUARD_NOT(ref, jit_STUB_ADDR(jit, jit_stub_leave_function_handler));
11112
0
    }
11113
0
  }
11114
11115
0
  if ((op_array->fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) == ZEND_ACC_CLOSURE) {
11116
0
    if (!left_frame) {
11117
0
      left_frame = true;
11118
0
        if (!zend_jit_leave_frame(jit)) {
11119
0
        return 0;
11120
0
        }
11121
0
    }
11122
    // JIT: OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
11123
0
    jit_OBJ_RELEASE(jit, ir_ADD_OFFSET(ir_LOAD_A(jit_EX(func)), -sizeof(zend_object)));
11124
0
  } else if (may_need_release_this) {
11125
0
    ir_ref if_release, fast_path = IR_UNUSED;
11126
11127
0
    if (!left_frame) {
11128
0
      left_frame = true;
11129
0
        if (!zend_jit_leave_frame(jit)) {
11130
0
        return 0;
11131
0
        }
11132
0
    }
11133
0
    if (!JIT_G(current_frame) || !TRACE_FRAME_ALWAYS_RELEASE_THIS(JIT_G(current_frame))) {
11134
      // JIT: if (call_info & ZEND_CALL_RELEASE_THIS)
11135
0
      if (!call_info) {
11136
0
        call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
11137
0
      }
11138
0
      if_release = ir_IF(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_RELEASE_THIS)));
11139
0
      ir_IF_FALSE(if_release);
11140
0
      fast_path = ir_END();
11141
0
      ir_IF_TRUE(if_release);
11142
0
    }
11143
    // JIT: OBJ_RELEASE(execute_data->This))
11144
0
    jit_OBJ_RELEASE(jit, ir_LOAD_A(jit_EX(This.value.obj)));
11145
0
    if (fast_path) {
11146
0
      ir_MERGE_WITH(fast_path);
11147
0
    }
11148
    // TODO: avoid EG(excption) check for $this->foo() calls
11149
0
    may_throw = 1;
11150
0
  }
11151
11152
  // JIT: EG(vm_stack_top) = (zval*)execute_data
11153
0
  ir_STORE(jit_EG(vm_stack_top), jit_FP(jit));
11154
11155
  // JITL execute_data = EX(prev_execute_data)
11156
0
  jit_STORE_FP(jit, ir_LOAD_A(jit_EX(prev_execute_data)));
11157
11158
0
  if (!left_frame) {
11159
    // JIT: EG(current_execute_data) = execute_data
11160
0
    ir_STORE(jit_EG(current_execute_data), jit_FP(jit));
11161
0
  }
11162
11163
0
  if (trace) {
11164
0
    if (trace->op != ZEND_JIT_TRACE_END
11165
0
     && (JIT_G(current_frame) && !TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame)))) {
11166
0
      zend_jit_reset_last_valid_opline(jit);
11167
0
    } else {
11168
      /* We add extra RLOAD and RSTORE to make fusion for persistent register
11169
       *     mov (%FP), %IP
11170
       *     add $0x1c, %IP
11171
       * The naive (commented) code leads to extra register allocation and move.
11172
       *     mov (%FP), %tmp
11173
       *     add $0x1c, %tmp
11174
       *     mov %tmp, %FP
11175
       */
11176
#if 0
11177
      jit_STORE_IP(jit, ir_ADD_OFFSET(ir_LOAD_A(jit_EX(opline)), sizeof(zend_op)));
11178
#else
11179
0
      jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
11180
0
      jit_STORE_IP(jit, ir_ADD_OFFSET(jit_IP(jit), sizeof(zend_op)));
11181
0
#endif
11182
0
    }
11183
11184
0
    if (cold_path) {
11185
0
      ir_MERGE_WITH(cold_path);
11186
0
    }
11187
11188
0
    if (trace->op == ZEND_JIT_TRACE_BACK
11189
0
     && (!JIT_G(current_frame) || TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame)))) {
11190
0
      const zend_op *next_opline = trace->opline;
11191
11192
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
11193
0
       && (op1_info & MAY_BE_RC1)
11194
0
       && (op1_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
11195
        /* exception might be thrown during destruction of unused return value */
11196
        // JIT: if (EG(exception))
11197
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11198
0
      }
11199
0
      do {
11200
0
        trace++;
11201
0
      } while (trace->op == ZEND_JIT_TRACE_INIT_CALL);
11202
0
      ZEND_ASSERT(trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END);
11203
0
      next_opline = trace->opline;
11204
0
      ZEND_ASSERT(next_opline != NULL);
11205
11206
0
      if (trace->op == ZEND_JIT_TRACE_END
11207
0
       && trace->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
11208
0
        trace_info->flags |= ZEND_JIT_TRACE_LOOP;
11209
11210
0
        ir_ref if_eq = ir_IF(jit_CMP_IP(jit, IR_EQ, next_opline));
11211
11212
0
        ir_IF_TRUE(if_eq);
11213
0
        ZEND_ASSERT(jit->trace_loop_ref);
11214
0
        ZEND_ASSERT(jit->ctx.ir_base[jit->trace_loop_ref].op2 == IR_UNUSED);
11215
0
        ir_MERGE_SET_OP(jit->trace_loop_ref, 2, ir_END());
11216
0
        ir_IF_FALSE(if_eq);
11217
11218
#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE
11219
        ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
11220
#else
11221
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_escape));
11222
0
#endif
11223
0
      } else {
11224
0
        ir_GUARD(jit_CMP_IP(jit, IR_EQ, next_opline), jit_STUB_ADDR(jit, jit_stub_trace_escape));
11225
0
      }
11226
11227
0
      zend_jit_set_last_valid_opline(jit, trace->opline);
11228
11229
0
      return 1;
11230
0
    } else if (may_throw ||
11231
0
        (((opline->op1_type & (IS_VAR|IS_TMP_VAR))
11232
0
          && (op1_info & MAY_BE_RC1)
11233
0
          && (op1_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)))
11234
0
         && (!JIT_G(current_frame) || TRACE_FRAME_IS_RETURN_VALUE_UNUSED(JIT_G(current_frame))))) {
11235
      // JIT: if (EG(exception))
11236
0
      ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11237
0
    }
11238
11239
0
    return 1;
11240
0
  } else {
11241
    // JIT: if (EG(exception))
11242
0
    ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11243
    // JIT: opline = EX(opline) + 1
11244
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
11245
0
    jit_STORE_IP(jit, ir_ADD_OFFSET(jit_IP(jit), sizeof(zend_op)));
11246
0
  }
11247
11248
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
11249
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
11250
0
  } else {
11251
0
    zend_jit_vm_leave(jit, jit_IP(jit));
11252
0
  }
11253
11254
0
  jit->b = -1;
11255
11256
0
  return 1;
11257
0
}
11258
11259
static void zend_jit_common_return(zend_jit_ctx *jit)
11260
0
{
11261
0
  ZEND_ASSERT(jit->return_inputs);
11262
0
  ir_MERGE_list(jit->return_inputs);
11263
0
}
11264
11265
static int zend_jit_return(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, uint32_t op1_info, zend_jit_addr op1_addr)
11266
0
{
11267
0
  zend_jit_addr ret_addr;
11268
0
  int8_t return_value_used = -1;
11269
0
  ir_ref return_value = IR_UNUSED, ref, refcount, if_return_value_used = IR_UNUSED;
11270
11271
0
  ZEND_ASSERT(op_array->type != ZEND_EVAL_CODE && op_array->function_name);
11272
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF));
11273
11274
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
11275
0
    jit->return_inputs = IR_UNUSED;
11276
0
    if (JIT_G(current_frame)) {
11277
0
      if (TRACE_FRAME_IS_RETURN_VALUE_USED(JIT_G(current_frame))) {
11278
0
        return_value_used = 1;
11279
0
      } else if (TRACE_FRAME_IS_RETURN_VALUE_UNUSED(JIT_G(current_frame))) {
11280
0
        return_value_used = 0;
11281
0
      } else {
11282
0
        return_value_used = -1;
11283
0
      }
11284
0
    }
11285
0
  }
11286
11287
0
  if (ZEND_OBSERVER_ENABLED) {
11288
0
    if (Z_MODE(op1_addr) == IS_REG) {
11289
0
      zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
11290
11291
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, dst, op1_info)) {
11292
0
        return 0;
11293
0
      }
11294
0
      op1_addr = dst;
11295
0
    }
11296
0
    jit_observer_fcall_end(jit, jit_FP(jit), jit_ZVAL_ADDR(jit, op1_addr));
11297
0
  }
11298
11299
  // JIT: if (!EX(return_value))
11300
0
  return_value = ir_LOAD_A(jit_EX(return_value));
11301
0
  ret_addr = ZEND_ADDR_REF_ZVAL(return_value);
11302
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) &&
11303
0
      (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11304
0
    if (return_value_used == -1) {
11305
0
      if_return_value_used = ir_IF(return_value);
11306
0
      ir_IF_FALSE_cold(if_return_value_used);
11307
0
    }
11308
0
    if (return_value_used != 1) {
11309
0
      if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11310
0
        ir_ref if_refcounted = jit_if_REFCOUNTED(jit, op1_addr);
11311
0
        ir_IF_FALSE(if_refcounted);
11312
0
        ir_END_list(jit->return_inputs);
11313
0
        ir_IF_TRUE(if_refcounted);
11314
0
      }
11315
0
      ref = jit_Z_PTR(jit, op1_addr);
11316
0
      refcount = jit_GC_DELREF(jit, ref);
11317
11318
0
      if (RC_MAY_BE_1(op1_info)) {
11319
0
        if (RC_MAY_BE_N(op1_info)) {
11320
0
          ir_ref if_non_zero = ir_IF(refcount);
11321
0
          ir_IF_TRUE(if_non_zero);
11322
0
          ir_END_list(jit->return_inputs);
11323
0
          ir_IF_FALSE(if_non_zero);
11324
0
        }
11325
0
        jit_ZVAL_DTOR(jit, ref, op1_info, opline);
11326
0
      }
11327
0
      if (return_value_used == -1) {
11328
0
        ir_END_list(jit->return_inputs);
11329
0
      }
11330
0
    }
11331
0
  } else if (return_value_used == -1) {
11332
0
    if_return_value_used = ir_IF(return_value);
11333
0
    ir_IF_FALSE_cold(if_return_value_used);
11334
0
    ir_END_list(jit->return_inputs);
11335
0
  }
11336
11337
0
  if (if_return_value_used) {
11338
0
    ir_IF_TRUE(if_return_value_used);
11339
0
  }
11340
11341
0
  if (return_value_used == 0) {
11342
0
    if (jit->return_inputs) {
11343
0
      ZEND_ASSERT(JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE);
11344
0
      ir_END_list(jit->return_inputs);
11345
0
      ir_MERGE_list(jit->return_inputs);
11346
0
      jit->return_inputs = IR_UNUSED;
11347
0
    }
11348
0
    return 1;
11349
0
  }
11350
11351
0
  if (opline->op1_type == IS_CONST) {
11352
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
11353
11354
0
    jit_ZVAL_COPY_CONST(jit, ret_addr, MAY_BE_ANY, MAY_BE_ANY, zv, true);
11355
0
  } else if (opline->op1_type == IS_TMP_VAR) {
11356
0
    jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11357
0
  } else if (opline->op1_type == IS_CV) {
11358
0
    if (op1_info & MAY_BE_REF) {
11359
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
11360
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
11361
0
      op1_addr = ZEND_ADDR_REF_ZVAL(ref);
11362
0
    }
11363
11364
0
    if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
11365
0
      if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11366
0
          (op1_info & (MAY_BE_REF|MAY_BE_OBJECT)) ||
11367
0
          !op_array->function_name) {
11368
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, true);
11369
0
      } else if (return_value_used != 1) {
11370
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11371
        // JIT: if (EXPECTED(!(EX_CALL_INFO() & ZEND_CALL_CODE))) ZVAL_NULL(retval_ptr);
11372
0
        jit_set_Z_TYPE_INFO(jit, op1_addr, IS_NULL);
11373
0
      } else {
11374
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11375
0
      }
11376
0
    } else {
11377
0
      jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11378
0
    }
11379
0
  } else {
11380
0
    if (op1_info & MAY_BE_REF) {
11381
0
      ir_ref if_ref, ref2, if_non_zero;
11382
0
      zend_jit_addr ref_addr;
11383
11384
0
      if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
11385
0
      ir_IF_TRUE_cold(if_ref);
11386
11387
      // JIT: zend_refcounted *ref = Z_COUNTED_P(retval_ptr)
11388
0
      ref = jit_Z_PTR(jit, op1_addr);
11389
11390
      // JIT: ZVAL_COPY_VALUE(return_value, &ref->value)
11391
0
      ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
11392
0
      ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
11393
0
      jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, ref_addr, op1_info, false);
11394
0
      ref2 = jit_GC_DELREF(jit, ref);
11395
0
      if_non_zero = ir_IF(ref2);
11396
0
      ir_IF_TRUE(if_non_zero);
11397
11398
      // JIT: if (IS_REFCOUNTED())
11399
0
      ir_ref if_refcounted = jit_if_REFCOUNTED(jit, ret_addr);
11400
0
      ir_IF_FALSE(if_refcounted);
11401
0
      ir_END_list(jit->return_inputs);
11402
0
      ir_IF_TRUE(if_refcounted);
11403
11404
      // JIT: ADDREF
11405
0
      ref2 = jit_Z_PTR(jit, ret_addr);
11406
0
      jit_GC_ADDREF(jit, ref2);
11407
0
      ir_END_list(jit->return_inputs);
11408
11409
0
      ir_IF_FALSE(if_non_zero);
11410
11411
0
      jit_EFREE(jit, ref, sizeof(zend_reference), op_array, opline);
11412
0
      ir_END_list(jit->return_inputs);
11413
11414
0
      ir_IF_FALSE(if_ref);
11415
0
    }
11416
0
    jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11417
0
  }
11418
11419
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
11420
0
    if (jit->return_inputs) {
11421
0
      ir_END_list(jit->return_inputs);
11422
0
      ir_MERGE_list(jit->return_inputs);
11423
0
      jit->return_inputs = IR_UNUSED;
11424
0
    }
11425
0
  } else {
11426
0
    ir_END_list(jit->return_inputs);
11427
0
    jit->b = -1;
11428
0
  }
11429
11430
0
  return 1;
11431
0
}
11432
11433
static int zend_jit_bind_global(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
11434
0
{
11435
0
  zend_jit_addr op1_addr = OP1_ADDR();
11436
0
  zend_string *varname = Z_STR_P(RT_CONSTANT(opline, opline->op2));
11437
0
  ir_ref cache_slot_ref, idx_ref, num_used_ref, bucket_ref, ref, ref2;
11438
0
  ir_ref if_fit, if_reference, if_same_key, fast_path;
11439
0
  ir_ref slow_inputs = IR_UNUSED, end_inputs = IR_UNUSED;
11440
11441
  // JIT: idx = (uintptr_t)CACHED_PTR(opline->extended_value) - 1;
11442
0
  cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value);
11443
0
  idx_ref = ir_SUB_A(ir_LOAD_A(cache_slot_ref), ir_CONST_ADDR(1));
11444
11445
  // JIT: if (EXPECTED(idx < EG(symbol_table).nNumUsed * sizeof(Bucket)))
11446
0
  num_used_ref = ir_MUL_U32(ir_LOAD_U32(jit_EG(symbol_table.nNumUsed)),
11447
0
    ir_CONST_U32(sizeof(Bucket)));
11448
0
  if (sizeof(void*) == 8) {
11449
0
    num_used_ref = ir_ZEXT_A(num_used_ref);
11450
0
  }
11451
0
  if_fit = ir_IF(ir_ULT(idx_ref, num_used_ref));
11452
0
  ir_IF_FALSE_cold(if_fit);
11453
0
  ir_END_list(slow_inputs);
11454
0
  ir_IF_TRUE(if_fit);
11455
11456
  // JIT: Bucket *p = (Bucket*)((char*)EG(symbol_table).arData + idx);
11457
0
  bucket_ref = ir_ADD_A(ir_LOAD_A(jit_EG(symbol_table.arData)), idx_ref);
11458
0
  if_reference = jit_if_Z_TYPE_ref(jit, bucket_ref, ir_CONST_U8(IS_REFERENCE));
11459
0
  ir_IF_FALSE_cold(if_reference);
11460
0
  ir_END_list(slow_inputs);
11461
0
  ir_IF_TRUE(if_reference);
11462
11463
  // JIT: (EXPECTED(p->key == varname))
11464
0
  if_same_key = ir_IF(ir_EQ(ir_LOAD_A(ir_ADD_OFFSET(bucket_ref, offsetof(Bucket, key))), ir_CONST_ADDR(varname)));
11465
0
  ir_IF_FALSE_cold(if_same_key);
11466
0
  ir_END_list(slow_inputs);
11467
0
  ir_IF_TRUE(if_same_key);
11468
11469
  // JIT: GC_ADDREF(Z_PTR(p->val))
11470
0
  ref = jit_Z_PTR_ref(jit, bucket_ref);
11471
0
  jit_GC_ADDREF(jit, ref);
11472
11473
0
  fast_path = ir_END();
11474
0
  ir_MERGE_list(slow_inputs);
11475
11476
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_global_helper),
11477
0
    ir_CONST_ADDR(varname),
11478
0
    cache_slot_ref);
11479
11480
0
  ir_MERGE_WITH(fast_path);
11481
0
  ref = ir_PHI_2(IR_ADDR, ref2, ref);
11482
11483
0
  if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11484
0
    ir_ref if_refcounted = IR_UNUSED, refcount, if_non_zero, if_may_not_leak;
11485
11486
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11487
      // JIT: if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr)))
11488
0
      if_refcounted = jit_if_REFCOUNTED(jit, op1_addr);
11489
0
      ir_IF_TRUE_cold(if_refcounted);
11490
0
    }
11491
11492
    // JIT:zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
11493
0
    ref2 = jit_Z_PTR(jit, op1_addr);
11494
11495
    // JIT: ZVAL_REF(variable_ptr, ref)
11496
0
    jit_set_Z_PTR(jit, op1_addr, ref);
11497
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
11498
11499
    // JIT: if (GC_DELREF(garbage) == 0)
11500
0
    refcount = jit_GC_DELREF(jit, ref2);
11501
0
    if_non_zero = ir_IF(refcount);
11502
0
    if (!(op1_info & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT))) {
11503
0
      ir_IF_TRUE(if_non_zero);
11504
0
      ir_END_list(end_inputs);
11505
0
    }
11506
0
    ir_IF_FALSE(if_non_zero);
11507
11508
0
    jit_ZVAL_DTOR(jit, ref2, op1_info, opline);
11509
0
    if (op1_info & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT)) {
11510
0
      ir_END_list(end_inputs);
11511
0
      ir_IF_TRUE(if_non_zero);
11512
11513
      // JIT: GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr)
11514
0
      if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref2);
11515
0
      ir_IF_TRUE(if_may_not_leak);
11516
0
      ir_END_list(end_inputs);
11517
0
      ir_IF_FALSE(if_may_not_leak);
11518
0
      if (opline) {
11519
0
        jit_SET_EX_OPLINE(jit, opline);
11520
0
      }
11521
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref2);
11522
0
    }
11523
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11524
0
      ir_END_list(end_inputs);
11525
0
      ir_IF_FALSE(if_refcounted);
11526
0
    }
11527
0
  }
11528
11529
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11530
    // JIT: ZVAL_REF(variable_ptr, ref)
11531
0
    jit_set_Z_PTR(jit, op1_addr, ref);
11532
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
11533
0
  }
11534
11535
0
  if (end_inputs) {
11536
0
    ir_END_list(end_inputs);
11537
0
    ir_MERGE_list(end_inputs);
11538
0
  }
11539
11540
0
  return 1;
11541
0
}
11542
11543
static int zend_jit_free(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, int may_throw)
11544
0
{
11545
0
  zend_jit_addr op1_addr = OP1_ADDR();
11546
11547
0
  if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11548
0
    if (may_throw) {
11549
0
      jit_SET_EX_OPLINE(jit, opline);
11550
0
    }
11551
0
    if (opline->opcode == ZEND_FE_FREE && (op1_info & (MAY_BE_OBJECT|MAY_BE_REF))) {
11552
0
      ir_ref ref, if_array, if_exists, end_inputs = IR_UNUSED;
11553
11554
0
      if (op1_info & MAY_BE_ARRAY) {
11555
0
        if_array = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
11556
0
        ir_IF_TRUE(if_array);
11557
0
        ir_END_list(end_inputs);
11558
0
        ir_IF_FALSE(if_array);
11559
0
      }
11560
0
      ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_iter_idx)));
11561
0
      if_exists = ir_IF(ir_EQ(ref, ir_CONST_U32(-1)));
11562
0
      ir_IF_TRUE(if_exists);
11563
0
      ir_END_list(end_inputs);
11564
0
      ir_IF_FALSE(if_exists);
11565
11566
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_hash_iterator_del), ref);
11567
11568
0
      ir_END_list(end_inputs);
11569
0
      ir_MERGE_list(end_inputs);
11570
0
    }
11571
11572
0
    jit_ZVAL_PTR_DTOR(jit, op1_addr, op1_info, false, opline);
11573
11574
0
    if (may_throw) {
11575
0
      zend_jit_check_exception(jit);
11576
0
    }
11577
0
  }
11578
11579
0
  return 1;
11580
0
}
11581
11582
static int zend_jit_echo(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
11583
0
{
11584
0
  if (opline->op1_type == IS_CONST) {
11585
0
    zval *zv;
11586
0
    size_t len;
11587
11588
0
    zv = RT_CONSTANT(opline, opline->op1);
11589
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11590
0
    len = Z_STRLEN_P(zv);
11591
11592
0
    if (len > 0) {
11593
0
      const char *str = Z_STRVAL_P(zv);
11594
11595
0
      jit_SET_EX_OPLINE(jit, opline);
11596
0
      ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_write),
11597
0
        ir_CONST_ADDR(str), ir_CONST_ADDR(len));
11598
11599
0
      zend_jit_check_exception(jit);
11600
0
    }
11601
0
  } else {
11602
0
    zend_jit_addr op1_addr = OP1_ADDR();
11603
0
    ir_ref ref;
11604
11605
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11606
11607
0
    jit_SET_EX_OPLINE(jit, opline);
11608
11609
0
    ref = jit_Z_PTR(jit, op1_addr);
11610
0
    ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_write),
11611
0
      ir_ADD_OFFSET(ref, offsetof(zend_string, val)),
11612
0
      ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_string, len))));
11613
11614
0
    if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
11615
0
      jit_ZVAL_PTR_DTOR(jit, op1_addr, op1_info, false, opline);
11616
0
    }
11617
11618
0
    zend_jit_check_exception(jit);
11619
0
  }
11620
0
  return 1;
11621
0
}
11622
11623
static int zend_jit_strlen(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, zend_jit_addr res_addr)
11624
0
{
11625
0
  if (opline->op1_type == IS_CONST) {
11626
0
    zval *zv;
11627
0
    size_t len;
11628
11629
0
    zv = RT_CONSTANT(opline, opline->op1);
11630
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11631
0
    len = Z_STRLEN_P(zv);
11632
11633
0
    jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(len));
11634
0
    if (Z_MODE(res_addr) != IS_REG) {
11635
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11636
0
    } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11637
0
      return 0;
11638
0
    }
11639
0
  } else {
11640
0
    ir_ref ref;
11641
11642
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11643
11644
0
    ref = jit_Z_PTR(jit, op1_addr);
11645
0
    ref = ir_LOAD_L(ir_ADD_OFFSET(ref, offsetof(zend_string, len)));
11646
0
    jit_set_Z_LVAL(jit, res_addr, ref);
11647
11648
0
    if (Z_MODE(res_addr) == IS_REG) {
11649
0
      if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11650
0
        return 0;
11651
0
      }
11652
0
    } else {
11653
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11654
0
    }
11655
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
11656
0
  }
11657
0
  return 1;
11658
0
}
11659
11660
static int zend_jit_count(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, zend_jit_addr res_addr, int may_throw)
11661
0
{
11662
0
  if (opline->op1_type == IS_CONST) {
11663
0
    zval *zv;
11664
0
    zend_long count;
11665
11666
0
    zv = RT_CONSTANT(opline, opline->op1);
11667
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
11668
0
    count = zend_hash_num_elements(Z_ARRVAL_P(zv));
11669
11670
0
    jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(count));
11671
0
    if (Z_MODE(res_addr) != IS_REG) {
11672
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11673
0
    } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11674
0
      return 0;
11675
0
    }
11676
0
  } else {
11677
0
    ir_ref ref;
11678
11679
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_ARRAY);
11680
    // Note: See the implementation of ZEND_COUNT in Zend/zend_vm_def.h - arrays do not contain IS_UNDEF starting in php 8.1+.
11681
11682
0
    ref = jit_Z_PTR(jit, op1_addr);
11683
0
    if (sizeof(void*) == 8) {
11684
0
      ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(HashTable, nNumOfElements)));
11685
0
      ref = ir_ZEXT_L(ref);
11686
0
    } else {
11687
0
      ref = ir_LOAD_L(ir_ADD_OFFSET(ref, offsetof(HashTable, nNumOfElements)));
11688
0
    }
11689
0
    jit_set_Z_LVAL(jit, res_addr, ref);
11690
11691
0
    if (Z_MODE(res_addr) == IS_REG) {
11692
0
      if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11693
0
        return 0;
11694
0
      }
11695
0
    } else {
11696
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11697
0
    }
11698
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
11699
0
  }
11700
11701
0
  if (may_throw) {
11702
0
    zend_jit_check_exception(jit);
11703
0
  }
11704
0
  return 1;
11705
0
}
11706
11707
static int zend_jit_in_array(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, uint8_t smart_branch_opcode, uint32_t target_label, uint32_t target_label2, const void *exit_addr)
11708
0
{
11709
0
  HashTable *ht = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
11710
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
11711
0
  ir_ref ref;
11712
11713
0
  ZEND_ASSERT(opline->op1_type != IS_VAR && opline->op1_type != IS_TMP_VAR);
11714
0
  ZEND_ASSERT((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == MAY_BE_STRING);
11715
11716
  // JIT: result = zend_hash_find_ex(ht, Z_STR_P(op1), OP1_TYPE == IS_CONST);
11717
0
  if (opline->op1_type != IS_CONST) {
11718
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find),
11719
0
      ir_CONST_ADDR(ht),
11720
0
      jit_Z_PTR(jit, op1_addr));
11721
0
  } else {
11722
0
    zend_string *str = Z_STR_P(RT_CONSTANT(opline, opline->op1));
11723
11724
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash),
11725
0
      ir_CONST_ADDR(ht), ir_CONST_ADDR(str));
11726
0
  }
11727
11728
0
  if (exit_addr) {
11729
0
    if (smart_branch_opcode == ZEND_JMPZ) {
11730
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
11731
0
    } else {
11732
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
11733
0
    }
11734
0
  } else if (smart_branch_opcode) {
11735
0
    zend_basic_block *bb;
11736
11737
0
    ZEND_ASSERT(jit->b >= 0);
11738
0
    bb = &jit->ssa->cfg.blocks[jit->b];
11739
0
    ZEND_ASSERT(bb->successors_count == 2);
11740
0
    ref = jit_IF_ex(jit, ref,
11741
0
      (smart_branch_opcode == ZEND_JMPZ) ? target_label2 : target_label);
11742
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
11743
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
11744
0
    jit->b = -1;
11745
0
  } else {
11746
0
    jit_set_Z_TYPE_INFO_ex(jit, res_addr,
11747
0
      ir_ADD_U32(ir_ZEXT_U32(ir_NE(ref, IR_NULL)), ir_CONST_U32(IS_FALSE)));
11748
0
  }
11749
11750
0
  return 1;
11751
0
}
11752
11753
static int zend_jit_rope(zend_jit_ctx *jit, const zend_op *opline, uint32_t op2_info)
11754
0
{
11755
0
  uint32_t offset;
11756
11757
0
  offset = (opline->opcode == ZEND_ROPE_INIT) ?
11758
0
    opline->result.var :
11759
0
    opline->op1.var + opline->extended_value * sizeof(zend_string*);
11760
11761
0
  if (opline->op2_type == IS_CONST) {
11762
0
    zval *zv = RT_CONSTANT(opline, opline->op2);
11763
0
    zend_string *str;
11764
11765
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11766
0
    str = Z_STR_P(zv);
11767
11768
0
    ir_STORE(ir_ADD_OFFSET(jit_FP(jit), offset), ir_CONST_ADDR(str));
11769
0
  } else {
11770
0
    zend_jit_addr op2_addr = OP2_ADDR();
11771
0
    ir_ref ref;
11772
11773
0
    ZEND_ASSERT((op2_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11774
11775
0
    ref = jit_Z_PTR(jit, op2_addr);
11776
0
    ir_STORE(ir_ADD_OFFSET(jit_FP(jit), offset), ref);
11777
0
    if (opline->op2_type == IS_CV) {
11778
0
      ir_ref if_refcounted, long_path;
11779
11780
0
      if_refcounted = jit_if_REFCOUNTED(jit, op2_addr);
11781
0
      ir_IF_TRUE(if_refcounted);
11782
0
      jit_GC_ADDREF(jit, ref);
11783
0
      long_path = ir_END();
11784
11785
0
      ir_IF_FALSE(if_refcounted);
11786
0
      ir_MERGE_WITH(long_path);
11787
0
    }
11788
0
  }
11789
11790
0
  if (opline->opcode == ZEND_ROPE_END) {
11791
0
    zend_jit_addr res_addr = RES_ADDR();
11792
0
    ir_ref ref;
11793
11794
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_rope_end),
11795
0
      ir_ADD_OFFSET(jit_FP(jit), opline->op1.var),
11796
0
      ir_CONST_U32(opline->extended_value));
11797
11798
0
    jit_set_Z_PTR(jit, res_addr, ref);
11799
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING_EX);
11800
0
  }
11801
11802
0
  return 1;
11803
0
}
11804
11805
static int zend_jit_zval_copy_deref_reg(zend_jit_ctx *jit, zend_jit_addr res_addr, uint32_t res_info, zend_jit_addr val_addr, ir_ref type, ir_ref *values)
11806
0
{
11807
0
  ir_ref if_type, val;
11808
11809
0
  if (res_info == MAY_BE_LONG) {
11810
0
    if_type = ir_IF(ir_EQ(type, ir_CONST_U32(IS_LONG)));
11811
0
    ir_IF_TRUE(if_type);
11812
0
    val = jit_ZVAL_ADDR(jit, val_addr);
11813
0
    ir_END_PHI_list(*values, val);
11814
0
    ir_IF_FALSE(if_type);
11815
0
    val = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
11816
0
    ir_END_PHI_list(*values, val);
11817
0
  } else if (res_info == MAY_BE_DOUBLE) {
11818
0
    if_type = ir_IF(ir_EQ(type, ir_CONST_U32(IS_DOUBLE)));
11819
0
    ir_IF_TRUE(if_type);
11820
0
    val = jit_ZVAL_ADDR(jit, val_addr);
11821
0
    ir_END_PHI_list(*values, val);
11822
0
    ir_IF_FALSE(if_type);
11823
0
    val = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
11824
0
    ir_END_PHI_list(*values, val);
11825
0
  } else {
11826
0
    ZEND_UNREACHABLE();
11827
0
  }
11828
0
  return 1;
11829
0
}
11830
11831
static int zend_jit_zval_copy_deref(zend_jit_ctx *jit, zend_jit_addr res_addr, zend_jit_addr val_addr, ir_ref type)
11832
0
{
11833
0
  ir_ref if_refcounted, if_reference, if_refcounted2, ptr, val2, ptr2, type2;
11834
0
  ir_refs *merge_inputs, *types, *ptrs;
11835
#if SIZEOF_ZEND_LONG == 4
11836
  ir_ref val = jit_ZVAL_ADDR(jit, val_addr);
11837
  ir_refs *values; /* we need this only for zval.w2 copy */
11838
#endif
11839
11840
0
  ir_refs_init(merge_inputs, 4);
11841
0
  ir_refs_init(types, 4);
11842
0
  ir_refs_init(ptrs, 4);
11843
#if SIZEOF_ZEND_LONG == 4
11844
  ir_refs_init(values, 4);
11845
#endif
11846
11847
  // JIT: ptr = Z_PTR_P(val);
11848
0
  ptr = jit_Z_PTR(jit, val_addr);
11849
11850
  // JIT: if (Z_OPT_REFCOUNTED_P(val)) {
11851
0
  if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(Z_TYPE_FLAGS_MASK)));
11852
0
  ir_IF_FALSE_cold(if_refcounted);
11853
0
  ir_refs_add(merge_inputs, ir_END());
11854
0
  ir_refs_add(types, type);
11855
0
  ir_refs_add(ptrs, ptr);
11856
#if SIZEOF_ZEND_LONG == 4
11857
  ir_refs_add(values, val);
11858
#endif
11859
11860
0
  ir_IF_TRUE(if_refcounted);
11861
11862
  // JIT: if (UNEXPECTED(Z_OPT_ISREF_P(val))) {
11863
0
  if_reference = ir_IF(ir_EQ(type, ir_CONST_U32(IS_REFERENCE_EX)));
11864
//  if_reference = ir_IF(ir_EQ(ir_TRUNC_U8(type), ir_CONST_U8(IS_REFERENCE))); // TODO: fix IR to avoid need for extra register ???
11865
0
  ir_IF_TRUE(if_reference);
11866
11867
  // JIT: val = Z_REFVAL_P(val);
11868
0
  val2 = ir_ADD_OFFSET(ptr, offsetof(zend_reference, val));
11869
0
  type2 = jit_Z_TYPE_INFO_ref(jit, val2);
11870
0
  ptr2 = jit_Z_PTR_ref(jit, val2);
11871
11872
  // JIT: if (Z_OPT_REFCOUNTED_P(val)) {
11873
0
  if_refcounted2 = ir_IF(ir_AND_U32(type2, ir_CONST_U32(Z_TYPE_FLAGS_MASK)));
11874
0
  ir_IF_FALSE_cold(if_refcounted2);
11875
0
  ir_refs_add(merge_inputs, ir_END());
11876
0
  ir_refs_add(types, type2);
11877
0
  ir_refs_add(ptrs, ptr2);
11878
#if SIZEOF_ZEND_LONG == 4
11879
  ir_refs_add(values, val2);
11880
#endif
11881
11882
0
  ir_IF_TRUE(if_refcounted2);
11883
0
  ir_MERGE_WITH_EMPTY_FALSE(if_reference);
11884
0
  type = ir_PHI_2(IR_U32, type2, type);
11885
0
  ptr = ir_PHI_2(IR_ADDR, ptr2, ptr);
11886
#if SIZEOF_ZEND_LONG == 4
11887
  val = ir_PHI_2(IR_ADDR, val2, val);
11888
#endif
11889
11890
  // JIT: Z_ADDREF_P(val);
11891
0
  jit_GC_ADDREF(jit, ptr);
11892
0
  ir_refs_add(merge_inputs, ir_END());
11893
0
  ir_refs_add(types, type);
11894
0
  ir_refs_add(ptrs, ptr);
11895
#if SIZEOF_ZEND_LONG == 4
11896
  ir_refs_add(values, val);
11897
#endif
11898
11899
0
  ir_MERGE_N(merge_inputs->count, merge_inputs->refs);
11900
0
  type = ir_PHI_N(IR_U32, types->count, types->refs);
11901
0
  ptr = ir_PHI_N(IR_ADDR, ptrs->count, ptrs->refs);
11902
#if SIZEOF_ZEND_LONG == 4
11903
  val = ir_PHI_N(IR_ADDR, values->count, values->refs);
11904
  val_addr = ZEND_ADDR_REF_ZVAL(val);
11905
#endif
11906
11907
  // JIT: Z_PTR_P(res) = ptr;
11908
0
  jit_set_Z_PTR(jit, res_addr, ptr);
11909
#if SIZEOF_ZEND_LONG == 4
11910
  jit_set_Z_W2(jit, res_addr, jit_Z_W2(jit, val_addr));
11911
#endif
11912
0
  jit_set_Z_TYPE_INFO_ex(jit, res_addr, type);
11913
11914
0
  return 1;
11915
0
}
11916
11917
static int zend_jit_fetch_dimension_address_inner(zend_jit_ctx  *jit,
11918
                                                  const zend_op *opline,
11919
                                                  uint32_t       type,
11920
                                                  uint32_t       op1_info,
11921
                                                  uint32_t       op2_info,
11922
                                                  zend_jit_addr  op2_addr,
11923
                                                  zend_ssa_range *op2_range,
11924
                                                  uint8_t        dim_type,
11925
                                                  const void    *found_exit_addr,
11926
                                                  const void    *not_found_exit_addr,
11927
                                                  const void    *exit_addr,
11928
                                                  bool           result_type_guard,
11929
                                                  ir_ref         ht_ref,
11930
                                                  ir_refs       *found_inputs,
11931
                                                  ir_refs       *found_vals,
11932
                                                  ir_ref        *end_inputs,
11933
                                                  ir_ref        *not_found_inputs)
11934
0
{
11935
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
11936
0
  ir_ref ref = IR_UNUSED, cond, if_found;
11937
0
  ir_ref if_type = IS_UNUSED;
11938
0
  ir_refs *test_zval_inputs, *test_zval_values;
11939
11940
0
  ir_refs_init(test_zval_inputs, 4);
11941
0
  ir_refs_init(test_zval_values, 4);
11942
11943
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
11944
0
   && type == BP_VAR_R
11945
0
   && !exit_addr) {
11946
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
11947
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
11948
0
    if (!exit_addr) {
11949
0
      return 0;
11950
0
    }
11951
0
  }
11952
11953
0
  if (op2_info & MAY_BE_LONG) {
11954
0
    bool op2_loaded = false;
11955
0
    bool packed_loaded = false;
11956
0
    bool bad_packed_key = false;
11957
0
    ir_ref if_packed = IS_UNDEF;
11958
0
    ir_ref h = IR_UNUSED;
11959
0
    ir_ref idx_not_found_inputs = IR_UNUSED;
11960
11961
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_LONG)) {
11962
      // JIT: if (EXPECTED(Z_TYPE_P(dim) == IS_LONG))
11963
0
      if_type = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
11964
0
      ir_IF_TRUE(if_type);
11965
0
    }
11966
0
    if (op1_info & MAY_BE_PACKED_GUARD) {
11967
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_PACKED_GUARD);
11968
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
11969
11970
0
      if (!exit_addr) {
11971
0
        return 0;
11972
0
      }
11973
0
      cond = ir_AND_U32(
11974
0
        ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
11975
0
        ir_CONST_U32(HASH_FLAG_PACKED));
11976
0
      if (op1_info & MAY_BE_ARRAY_PACKED) {
11977
0
        ir_GUARD(cond, ir_CONST_ADDR(exit_addr));
11978
0
      } else {
11979
0
        ir_GUARD_NOT(cond, ir_CONST_ADDR(exit_addr));
11980
0
      }
11981
0
    }
11982
0
    if (type == BP_VAR_W) {
11983
      // JIT: hval = Z_LVAL_P(dim);
11984
0
      h = jit_Z_LVAL(jit, op2_addr);
11985
0
      op2_loaded = true;
11986
0
    }
11987
0
    if (op1_info & MAY_BE_ARRAY_PACKED) {
11988
0
      zend_long val = -1;
11989
11990
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
11991
0
        val = Z_LVAL_P(Z_ZV(op2_addr));
11992
0
        if (val >= 0 && val < HT_MAX_SIZE) {
11993
0
          packed_loaded = true;
11994
0
        } else {
11995
0
          bad_packed_key = true;
11996
0
        }
11997
0
        h = ir_CONST_LONG(val);
11998
0
      } else {
11999
0
        if (!op2_loaded) {
12000
          // JIT: hval = Z_LVAL_P(dim);
12001
0
          h = jit_Z_LVAL(jit, op2_addr);
12002
0
          op2_loaded = true;
12003
0
        }
12004
0
        packed_loaded = true;
12005
0
      }
12006
12007
0
      if (dim_type == IS_UNDEF && type == BP_VAR_W && packed_loaded) {
12008
        /* don't generate "fast" code for packed array */
12009
0
        packed_loaded = false;
12010
0
      }
12011
12012
0
      if (packed_loaded) {
12013
        // JIT: ZEND_HASH_INDEX_FIND(ht, hval, retval, num_undef);
12014
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12015
0
          if_packed = ir_IF(
12016
0
            ir_AND_U32(
12017
0
              ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
12018
0
              ir_CONST_U32(HASH_FLAG_PACKED)));
12019
0
          ir_IF_TRUE(if_packed);
12020
0
        }
12021
        // JIT: if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed))
12022
0
        ref = ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed)));
12023
0
#if SIZEOF_ZEND_LONG == 8
12024
0
        if ((Z_MODE(op2_addr) == IS_CONST_ZVAL && val >= 0 && val <= UINT32_MAX)
12025
0
         || (op2_range && op2_range->min >= 0 && op2_range->max <= UINT32_MAX)) {
12026
          /* comapre only the lower 32-bits to allow load fusion on x86_64 */
12027
0
          cond = ir_ULT(ir_TRUNC_U32(h), ref);
12028
0
        } else {
12029
0
          cond = ir_ULT(h, ir_ZEXT_L(ref));
12030
0
        }
12031
#else
12032
        cond = ir_ULT(h, ref);
12033
#endif
12034
0
        if (type == BP_JIT_IS) {
12035
0
          if (not_found_exit_addr) {
12036
0
            ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12037
0
          } else {
12038
0
            ir_ref if_fit = ir_IF(cond);
12039
0
            ir_IF_FALSE(if_fit);
12040
0
            ir_END_list(*end_inputs);
12041
0
            ir_IF_TRUE(if_fit);
12042
0
          }
12043
0
        } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12044
0
          ir_GUARD(cond, ir_CONST_ADDR(exit_addr));
12045
0
        } else if (type == BP_VAR_IS && not_found_exit_addr) {
12046
0
          ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12047
0
        } else if (type == BP_VAR_RW && not_found_exit_addr) {
12048
0
          ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12049
0
        } else if (type == BP_VAR_IS && result_type_guard) {
12050
0
          ir_ref if_fit = ir_IF(cond);
12051
0
          ir_IF_FALSE(if_fit);
12052
0
          ir_END_list(*not_found_inputs);
12053
0
          ir_IF_TRUE(if_fit);
12054
0
        } else {
12055
0
          ir_ref if_fit = ir_IF(cond);
12056
0
          ir_IF_FALSE(if_fit);
12057
0
          ir_END_list(idx_not_found_inputs);
12058
0
          ir_IF_TRUE(if_fit);
12059
0
        }
12060
        // JIT: _ret = &_ht->arPacked[h];
12061
0
        ref = ir_MUL_L(h, ir_CONST_LONG(sizeof(zval)));
12062
0
        ref = ir_BITCAST_A(ref);
12063
0
        ref = ir_ADD_A(ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arPacked))), ref);
12064
0
        if (type == BP_JIT_IS) {
12065
0
          ir_refs_add(test_zval_values, ref);
12066
0
          ir_refs_add(test_zval_inputs, ir_END());
12067
0
        }
12068
0
      }
12069
0
    }
12070
0
    switch (type) {
12071
0
      case BP_JIT_IS:
12072
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12073
0
          if (if_packed) {
12074
0
            ir_IF_FALSE(if_packed);
12075
0
            if_packed = IR_UNUSED;
12076
0
          }
12077
0
          if (!op2_loaded) {
12078
            // JIT: hval = Z_LVAL_P(dim);
12079
0
            h = jit_Z_LVAL(jit, op2_addr);
12080
0
            op2_loaded = true;
12081
0
          }
12082
0
          if (packed_loaded) {
12083
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(_zend_hash_index_find), ht_ref, h);
12084
0
          } else {
12085
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_find), ht_ref, h);
12086
0
          }
12087
0
          if (not_found_exit_addr) {
12088
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12089
0
          } else {
12090
0
            if_found = ir_IF(ref);
12091
0
            ir_IF_FALSE(if_found);
12092
0
            ir_END_list(*end_inputs);
12093
0
            ir_IF_TRUE(if_found);
12094
0
          }
12095
0
          ir_refs_add(test_zval_values, ref);
12096
0
          ir_refs_add(test_zval_inputs, ir_END());
12097
0
        } else if (!not_found_exit_addr && !packed_loaded) {
12098
0
          ir_END_list(*end_inputs);
12099
0
        }
12100
0
        break;
12101
0
      case BP_VAR_R:
12102
0
      case BP_VAR_IS:
12103
0
      case BP_VAR_UNSET:
12104
0
        if (packed_loaded) {
12105
0
          ir_ref type_ref = jit_Z_TYPE_ref(jit, ref);
12106
12107
0
          if (result_type_guard) {
12108
            /* perform IS_UNDEF check only after result type guard (during deoptimization) */
12109
0
          } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12110
0
            ir_GUARD(type_ref, ir_CONST_ADDR(exit_addr));
12111
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12112
0
            ir_GUARD(type_ref, ir_CONST_ADDR(not_found_exit_addr));
12113
0
          } else {
12114
0
            ir_ref if_def = ir_IF(type_ref);
12115
0
            ir_IF_FALSE(if_def);
12116
0
            ir_END_list(idx_not_found_inputs);
12117
0
            ir_IF_TRUE(if_def);
12118
0
          }
12119
0
          ir_refs_add(found_inputs, ir_END());
12120
0
          ir_refs_add(found_vals, ref);
12121
0
        }
12122
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12123
0
          if (if_packed) {
12124
0
            ir_IF_FALSE(if_packed);
12125
0
            if_packed = IR_UNUSED;
12126
0
          }
12127
0
          if (!op2_loaded) {
12128
            // JIT: hval = Z_LVAL_P(dim);
12129
0
            h = jit_Z_LVAL(jit, op2_addr);
12130
0
            op2_loaded = true;
12131
0
          }
12132
0
          if (packed_loaded) {
12133
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(_zend_hash_index_find), ht_ref, h);
12134
0
          } else {
12135
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_find), ht_ref, h);
12136
0
          }
12137
0
          if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12138
0
            ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
12139
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12140
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12141
0
          } else if (type == BP_VAR_IS && result_type_guard) {
12142
0
            if_found = ir_IF(ref);
12143
0
            ir_IF_FALSE(if_found);
12144
0
            ir_END_list(*not_found_inputs);
12145
0
            ir_IF_TRUE(if_found);
12146
0
          } else {
12147
0
            if_found = ir_IF(ref);
12148
0
            ir_IF_FALSE(if_found);
12149
0
            ir_END_list(idx_not_found_inputs);
12150
0
            ir_IF_TRUE(if_found);
12151
0
          }
12152
0
          ir_refs_add(found_inputs, ir_END());
12153
0
          ir_refs_add(found_vals, ref);
12154
0
        } else if (!packed_loaded) {
12155
0
          if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12156
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
12157
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12158
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(not_found_exit_addr));
12159
0
          } else if (type == BP_VAR_IS && result_type_guard) {
12160
0
            ir_END_list(*not_found_inputs);
12161
0
          } else {
12162
0
            ir_END_list(idx_not_found_inputs);
12163
0
          }
12164
0
        }
12165
12166
0
        if (idx_not_found_inputs) {
12167
0
          ir_MERGE_list(idx_not_found_inputs);
12168
0
          switch (type) {
12169
0
            case BP_VAR_R:
12170
0
              ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
12171
              // JIT: zend_error(E_WARNING,"Undefined array key " ZEND_LONG_FMT, hval);
12172
              // JIT: retval = &EG(uninitialized_zval);
12173
0
              jit_SET_EX_OPLINE(jit, opline);
12174
0
              if (Z_MODE(op2_addr) == IS_REG) {
12175
0
                if (!op2_loaded) {
12176
                  // JIT: hval = Z_LVAL_P(dim);
12177
0
                  h = jit_Z_LVAL(jit, op2_addr);
12178
0
                }
12179
0
                if (GCC_GLOBAL_REGS) {
12180
0
                  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key_ex), h);
12181
0
                } else {
12182
0
                  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key_ex), h, jit_FP(jit));
12183
0
                }
12184
0
              } else {
12185
0
                ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_undefined_offset, IR_FASTCALL_FUNC));
12186
0
              }
12187
0
              ir_END_list(*end_inputs);
12188
0
              break;
12189
0
            case BP_VAR_IS:
12190
0
            case BP_VAR_UNSET:
12191
0
              if (!not_found_exit_addr) {
12192
                // JIT: retval = &EG(uninitialized_zval);
12193
0
                jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12194
0
                ir_END_list(*end_inputs);
12195
0
              }
12196
0
              break;
12197
0
            default:
12198
0
              ZEND_UNREACHABLE();
12199
0
          }
12200
0
                }
12201
0
        break;
12202
0
      case BP_VAR_RW:
12203
0
        if (packed_loaded) {
12204
0
          if (not_found_exit_addr) {
12205
0
            ir_refs_add(found_inputs, ir_END());
12206
0
            ir_refs_add(found_vals, ref);
12207
0
          } else {
12208
0
            ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
12209
0
            ir_IF_TRUE(if_def);
12210
0
            ir_refs_add(found_inputs, ir_END());
12211
0
            ir_refs_add(found_vals, ref);
12212
0
            ir_IF_FALSE_cold(if_def);
12213
0
            ir_END_list(idx_not_found_inputs);
12214
0
          }
12215
0
        }
12216
0
        if (!packed_loaded ||
12217
0
            !not_found_exit_addr ||
12218
0
            (op1_info & MAY_BE_ARRAY_NUMERIC_HASH)) {
12219
0
          if (if_packed) {
12220
0
            ir_IF_FALSE(if_packed);
12221
0
            if_packed = IR_UNUSED;
12222
0
            ir_END_list(idx_not_found_inputs);
12223
0
          } else if (!packed_loaded) {
12224
0
            ir_END_list(idx_not_found_inputs);
12225
0
          }
12226
12227
0
          ir_MERGE_list(idx_not_found_inputs);
12228
0
          if (!op2_loaded) {
12229
            // JIT: hval = Z_LVAL_P(dim);
12230
0
            h = jit_Z_LVAL(jit, op2_addr);
12231
0
          }
12232
0
          if (packed_loaded) {
12233
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_index_lookup_rw_no_packed),
12234
0
              ht_ref, h);
12235
0
          } else {
12236
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_index_lookup_rw), ht_ref, h);
12237
0
          }
12238
0
          if (not_found_exit_addr) {
12239
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12240
0
          } else {
12241
0
            if_found = ir_IF(ref);
12242
0
            ir_IF_FALSE(if_found);
12243
0
            ir_END_list(*end_inputs);
12244
0
            ir_IF_TRUE(if_found);
12245
0
          }
12246
0
          ir_refs_add(found_inputs, ir_END());
12247
0
          ir_refs_add(found_vals, ref);
12248
0
        }
12249
0
        break;
12250
0
      case BP_VAR_W:
12251
0
        if (packed_loaded) {
12252
0
          ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
12253
0
          ir_IF_TRUE_cold(if_def);
12254
0
          ir_refs_add(found_inputs, ir_END());
12255
0
          ir_refs_add(found_vals, ref);
12256
0
          ir_IF_FALSE(if_def);
12257
0
          ir_END_list(idx_not_found_inputs);
12258
0
        }
12259
0
        if (!(op1_info & MAY_BE_ARRAY_KEY_LONG) || (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) || packed_loaded || bad_packed_key || dim_type == IS_UNDEF) {
12260
0
          if (if_packed) {
12261
0
            ir_IF_FALSE(if_packed);
12262
0
            if_packed = IR_UNUSED;
12263
0
            ir_END_list(idx_not_found_inputs);
12264
0
          } else if (!packed_loaded) {
12265
0
            ir_END_list(idx_not_found_inputs);
12266
0
          }
12267
0
          ir_MERGE_list(idx_not_found_inputs);
12268
0
          if (!op2_loaded) {
12269
            // JIT: hval = Z_LVAL_P(dim);
12270
0
            h = jit_Z_LVAL(jit, op2_addr);
12271
0
          }
12272
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_lookup), ht_ref, h);
12273
0
          ir_refs_add(found_inputs, ir_END());
12274
0
          ir_refs_add(found_vals, ref);
12275
0
        }
12276
0
        break;
12277
0
      default:
12278
0
        ZEND_UNREACHABLE();
12279
0
    }
12280
0
  }
12281
12282
0
  if (op2_info & MAY_BE_STRING) {
12283
0
    ir_ref key;
12284
12285
0
    if (if_type) {
12286
0
      ir_IF_FALSE(if_type);
12287
0
      if_type = IS_UNUSED;
12288
0
    }
12289
12290
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
12291
      // JIT: if (EXPECTED(Z_TYPE_P(dim) == IS_STRING))
12292
0
      if_type = jit_if_Z_TYPE(jit, op2_addr, IS_STRING);
12293
0
      ir_IF_TRUE(if_type);
12294
0
    }
12295
12296
    // JIT: offset_key = Z_STR_P(dim);
12297
0
    key = jit_Z_PTR(jit, op2_addr);
12298
12299
    // JIT: retval = zend_hash_find(ht, offset_key);
12300
0
    switch (type) {
12301
0
      case BP_JIT_IS:
12302
0
        if (opline->op2_type != IS_CONST) {
12303
0
          ir_ref if_num, end1, ref2;
12304
12305
0
          if_num = ir_IF(
12306
0
            ir_ULE(
12307
0
              ir_LOAD_C(ir_ADD_OFFSET(key, offsetof(zend_string, val))),
12308
0
              ir_CONST_CHAR('9')));
12309
0
          ir_IF_TRUE_cold(if_num);
12310
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_find), ht_ref, key);
12311
0
          end1 = ir_END();
12312
0
          ir_IF_FALSE(if_num);
12313
0
          ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find), ht_ref, key);
12314
0
          ir_MERGE_WITH(end1);
12315
0
          ref = ir_PHI_2(IR_ADDR, ref2, ref);
12316
0
        } else {
12317
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash), ht_ref, key);
12318
0
        }
12319
0
        if (not_found_exit_addr) {
12320
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12321
0
        } else {
12322
0
          if_found = ir_IF(ref);
12323
0
          ir_IF_FALSE(if_found);
12324
0
          ir_END_list(*end_inputs);
12325
0
          ir_IF_TRUE(if_found);
12326
0
        }
12327
0
        ir_refs_add(test_zval_values, ref);
12328
0
        ir_refs_add(test_zval_inputs, ir_END());
12329
0
        break;
12330
0
      case BP_VAR_R:
12331
0
      case BP_VAR_IS:
12332
0
      case BP_VAR_UNSET:
12333
0
        if (opline->op2_type != IS_CONST) {
12334
0
          ir_ref if_num, end1, ref2;
12335
12336
0
          if_num = ir_IF(
12337
0
            ir_ULE(
12338
0
              ir_LOAD_C(ir_ADD_OFFSET(key, offsetof(zend_string, val))),
12339
0
              ir_CONST_CHAR('9')));
12340
0
          ir_IF_TRUE_cold(if_num);
12341
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_find), ht_ref, key);
12342
0
          end1 = ir_END();
12343
0
          ir_IF_FALSE(if_num);
12344
0
          ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find), ht_ref, key);
12345
0
          ir_MERGE_WITH(end1);
12346
0
          ref = ir_PHI_2(IR_ADDR, ref2, ref);
12347
0
        } else {
12348
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash), ht_ref, key);
12349
0
        }
12350
0
        if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12351
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
12352
0
        } else if (type == BP_VAR_IS && not_found_exit_addr) {
12353
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12354
0
        } else if (type == BP_VAR_IS && result_type_guard) {
12355
0
          if_found = ir_IF(ref);
12356
0
          ir_IF_FALSE(if_found);
12357
0
          ir_END_list(*not_found_inputs);
12358
0
          ir_IF_TRUE(if_found);
12359
0
        } else {
12360
0
          if_found = ir_IF(ref);
12361
0
          switch (type) {
12362
0
            case BP_VAR_R:
12363
0
              ir_IF_FALSE_cold(if_found);
12364
              // JIT: zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(offset_key));
12365
0
              jit_SET_EX_OPLINE(jit, opline);
12366
0
              ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_undefined_key, IR_FASTCALL_FUNC));
12367
0
              ir_END_list(*end_inputs);
12368
0
              break;
12369
0
            case BP_VAR_IS:
12370
0
            case BP_VAR_UNSET:
12371
0
              ir_IF_FALSE(if_found);
12372
              // JIT: retval = &EG(uninitialized_zval);
12373
0
              jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12374
0
              ir_END_list(*end_inputs);
12375
0
              break;
12376
0
            default:
12377
0
              ZEND_UNREACHABLE();
12378
0
          }
12379
0
          ir_IF_TRUE(if_found);
12380
0
        }
12381
0
        ir_refs_add(found_inputs, ir_END());
12382
0
        ir_refs_add(found_vals, ref);
12383
0
        break;
12384
0
      case BP_VAR_RW:
12385
0
        if (opline->op2_type != IS_CONST) {
12386
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_lookup_rw), ht_ref, key);
12387
0
        } else {
12388
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_lookup_rw), ht_ref, key);
12389
0
        }
12390
0
        if (not_found_exit_addr) {
12391
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12392
0
        } else {
12393
0
          if_found = ir_IF(ref);
12394
0
          ir_IF_FALSE(if_found);
12395
0
          ir_END_list(*end_inputs);
12396
0
          ir_IF_TRUE(if_found);
12397
0
        }
12398
0
        ir_refs_add(found_inputs, ir_END());
12399
0
        ir_refs_add(found_vals, ref);
12400
0
        break;
12401
0
      case BP_VAR_W:
12402
0
        if (opline->op2_type != IS_CONST) {
12403
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_lookup_w), ht_ref, key);
12404
0
        } else {
12405
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_lookup), ht_ref, key);
12406
0
        }
12407
0
        ir_refs_add(found_inputs, ir_END());
12408
0
        ir_refs_add(found_vals, ref);
12409
0
        break;
12410
0
      default:
12411
0
        ZEND_UNREACHABLE();
12412
0
    }
12413
0
  }
12414
12415
0
  if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
12416
0
      if (if_type) {
12417
0
      ir_IF_FALSE_cold(if_type);
12418
0
      if_type = IS_UNDEF;
12419
0
    }
12420
0
    if (type != BP_VAR_RW) {
12421
0
      jit_SET_EX_OPLINE(jit, opline);
12422
0
    }
12423
0
    ref = jit_ZVAL_ADDR(jit, op2_addr);
12424
0
    switch (type) {
12425
0
      case BP_VAR_R:
12426
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_r_helper),
12427
0
          ht_ref,
12428
0
          ref,
12429
0
          jit_ZVAL_ADDR(jit, res_addr));
12430
0
        ir_END_list(*end_inputs);
12431
0
        break;
12432
0
      case BP_JIT_IS:
12433
0
        ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_fetch_dim_isset_helper), ht_ref, ref);
12434
0
        if (not_found_exit_addr) {
12435
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12436
0
          ir_refs_add(found_inputs, ir_END());
12437
0
        } else if (found_exit_addr) {
12438
0
          ir_GUARD_NOT(ref, ir_CONST_ADDR(found_exit_addr));
12439
0
          ir_END_list(*end_inputs);
12440
0
        } else {
12441
0
          if_found = ir_IF(ref);
12442
0
          ir_IF_TRUE(if_found);
12443
0
          ir_refs_add(found_inputs, ir_END());
12444
0
          ir_IF_FALSE(if_found);
12445
0
          ir_END_list(*end_inputs);
12446
0
        }
12447
0
        break;
12448
0
      case BP_VAR_IS:
12449
0
      case BP_VAR_UNSET:
12450
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_is_helper),
12451
0
          ht_ref,
12452
0
          ref,
12453
0
          jit_ZVAL_ADDR(jit, res_addr));
12454
0
        ir_END_list(*end_inputs);
12455
0
        break;
12456
0
      case BP_VAR_RW:
12457
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_rw_helper), ht_ref, ref);
12458
0
        if_found = ir_IF(ref);
12459
0
        ir_IF_TRUE(if_found);
12460
0
        ir_refs_add(found_inputs, ir_END());
12461
0
        ir_refs_add(found_vals, ref);
12462
0
        ir_IF_FALSE(if_found);
12463
0
        ir_END_list(*end_inputs);
12464
0
        break;
12465
0
      case BP_VAR_W:
12466
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref);
12467
0
        if_found = ir_IF(ref);
12468
0
        ir_IF_TRUE(if_found);
12469
0
        ir_refs_add(found_inputs, ir_END());
12470
0
        ir_refs_add(found_vals, ref);
12471
0
        ir_IF_FALSE(if_found);
12472
0
        ir_END_list(*end_inputs);
12473
0
        break;
12474
0
      default:
12475
0
        ZEND_UNREACHABLE();
12476
0
    }
12477
0
  }
12478
12479
0
  if (type == BP_JIT_IS
12480
0
   && !(op2_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
12481
    /* dead code */
12482
0
    ir_END_list(*end_inputs);
12483
0
  } else if (type == BP_JIT_IS
12484
0
   && (op1_info & MAY_BE_ARRAY)
12485
0
   && (op2_info & (MAY_BE_LONG|MAY_BE_STRING))
12486
0
   && test_zval_inputs->count) {
12487
12488
0
    ir_MERGE_N(test_zval_inputs->count, test_zval_inputs->refs);
12489
0
    ref = ir_PHI_N(IR_ADDR, test_zval_values->count, test_zval_values->refs);
12490
12491
0
    if (op1_info & MAY_BE_ARRAY_OF_REF) {
12492
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
12493
0
    }
12494
0
    cond = ir_GT(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_NULL));
12495
0
    if (not_found_exit_addr) {
12496
0
      ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12497
0
      ir_refs_add(found_inputs, ir_END());
12498
0
    } else if (found_exit_addr) {
12499
0
      ir_GUARD_NOT(cond, ir_CONST_ADDR(found_exit_addr));
12500
0
      ir_END_list(*end_inputs);
12501
0
    } else {
12502
0
      ir_ref if_set = ir_IF(cond);
12503
0
      ir_IF_FALSE(if_set);
12504
0
      ir_END_list(*end_inputs);
12505
0
      ir_IF_TRUE(if_set);
12506
0
      ir_refs_add(found_inputs, ir_END());
12507
0
    }
12508
0
  }
12509
12510
0
  return 1;
12511
0
}
12512
12513
static int zend_jit_fetch_dim_read(zend_jit_ctx       *jit,
12514
                                   const zend_op      *opline,
12515
                                   zend_ssa           *ssa,
12516
                                   const zend_ssa_op  *ssa_op,
12517
                                   uint32_t            op1_info,
12518
                                   zend_jit_addr       op1_addr,
12519
                                   bool           op1_avoid_refcounting,
12520
                                   uint32_t            op2_info,
12521
                                   zend_jit_addr       op2_addr,
12522
                                   zend_ssa_range     *op2_range,
12523
                                   uint32_t            res_info,
12524
                                   zend_jit_addr       res_addr,
12525
                                   uint8_t             dim_type)
12526
0
{
12527
0
  zend_jit_addr orig_op1_addr;
12528
0
  const void *exit_addr = NULL;
12529
0
  const void *not_found_exit_addr = NULL;
12530
0
  bool result_type_guard = false;
12531
0
  bool result_avoid_refcounting = false;
12532
0
  uint32_t may_be_string = (opline->opcode != ZEND_FETCH_LIST_R) ? MAY_BE_STRING : 0;
12533
0
  int may_throw = 0;
12534
0
  ir_ref if_type = IR_UNUSED;
12535
0
  ir_ref end_inputs = IR_UNUSED;
12536
0
  ir_ref not_found_inputs = IR_UNUSED;
12537
12538
0
  orig_op1_addr = OP1_ADDR();
12539
12540
0
  if (opline->opcode != ZEND_FETCH_DIM_IS
12541
0
   && JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
12542
0
   && !has_concrete_type(op1_info)) {
12543
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
12544
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
12545
0
    if (!exit_addr) {
12546
0
      return 0;
12547
0
    }
12548
0
  }
12549
12550
0
  if ((res_info & MAY_BE_GUARD)
12551
0
   && JIT_G(current_frame)
12552
0
   && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
12553
12554
0
    if (!(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF) - (MAY_BE_STRING|MAY_BE_LONG)))) {
12555
0
      result_type_guard = true;
12556
0
      res_info &= ~MAY_BE_GUARD;
12557
0
      ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
12558
0
    }
12559
12560
0
    if ((opline->result_type & (IS_VAR|IS_TMP_VAR))
12561
0
     && (opline->opcode == ZEND_FETCH_LIST_R
12562
0
      || !(opline->op1_type & (IS_VAR|IS_TMP_VAR))
12563
0
      || op1_avoid_refcounting)
12564
0
     && (res_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
12565
0
     && (ssa_op+1)->op1_use == ssa_op->result_def
12566
0
     && !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF) - (MAY_BE_STRING|MAY_BE_LONG)))
12567
0
     && zend_jit_may_avoid_refcounting(opline+1, res_info)) {
12568
0
      result_avoid_refcounting = true;
12569
0
      ssa->var_info[ssa_op->result_def].avoid_refcounting = 1;
12570
0
    }
12571
12572
0
    if (opline->opcode == ZEND_FETCH_DIM_IS
12573
0
     && !(res_info & MAY_BE_NULL)) {
12574
0
      uint32_t flags = 0;
12575
0
      uint32_t old_op1_info = 0;
12576
0
      uint32_t old_info;
12577
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
12578
0
      int32_t exit_point;
12579
12580
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
12581
0
       && !op1_avoid_refcounting) {
12582
0
        flags |= ZEND_JIT_EXIT_FREE_OP1;
12583
0
      }
12584
0
      if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
12585
0
       && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
12586
0
        flags |= ZEND_JIT_EXIT_FREE_OP2;
12587
0
      }
12588
12589
0
      if (op1_avoid_refcounting) {
12590
0
        old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
12591
0
        SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
12592
0
      }
12593
12594
0
      old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
12595
0
      SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_NULL, 0);
12596
0
      SET_STACK_REG_EX(stack, EX_VAR_TO_NUM(opline->result.var), ZREG_NONE, ZREG_TYPE_ONLY);
12597
0
      exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
12598
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
12599
0
      not_found_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
12600
0
      if (!not_found_exit_addr) {
12601
0
        return 0;
12602
0
      }
12603
12604
0
      if (op1_avoid_refcounting) {
12605
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
12606
0
      }
12607
0
    }
12608
0
  }
12609
12610
0
  if (op1_info & MAY_BE_REF) {
12611
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
12612
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
12613
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
12614
0
  }
12615
12616
0
  if (op1_info & MAY_BE_ARRAY) {
12617
0
    ir_ref ht_ref, ref;
12618
0
    zend_jit_addr val_addr;
12619
0
    ir_refs *found_inputs, *found_vals;
12620
12621
0
    ir_refs_init(found_inputs, 10);
12622
0
    ir_refs_init(found_vals, 10);
12623
12624
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
12625
0
      if (exit_addr && !(op1_info & (MAY_BE_OBJECT|may_be_string))) {
12626
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_ARRAY, exit_addr);
12627
0
      } else {
12628
0
        if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
12629
0
        ir_IF_TRUE(if_type);
12630
0
      }
12631
0
    }
12632
12633
0
    ht_ref = jit_Z_PTR(jit, op1_addr);
12634
12635
0
    if ((op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) ||
12636
0
        (opline->opcode != ZEND_FETCH_DIM_IS && JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE)) {
12637
0
      may_throw = 1;
12638
0
    }
12639
12640
0
    if (!zend_jit_fetch_dimension_address_inner(jit, opline,
12641
0
        (opline->opcode != ZEND_FETCH_DIM_IS) ? BP_VAR_R : BP_VAR_IS,
12642
0
        op1_info, op2_info, op2_addr, op2_range, dim_type, NULL, not_found_exit_addr, exit_addr,
12643
0
        result_type_guard, ht_ref, found_inputs, found_vals,
12644
0
        &end_inputs, &not_found_inputs)) {
12645
0
      return 0;
12646
0
    }
12647
12648
0
    if (found_inputs->count) {
12649
0
      ir_MERGE_N(found_inputs->count, found_inputs->refs);
12650
0
      ref = ir_PHI_N(IR_ADDR, found_vals->count, found_vals->refs);
12651
0
      val_addr = ZEND_ADDR_REF_ZVAL(ref);
12652
12653
0
      if (result_type_guard) {
12654
0
        uint8_t type = concrete_type(res_info);
12655
0
        uint32_t flags = 0;
12656
12657
0
        if (opline->opcode != ZEND_FETCH_LIST_R
12658
0
         && (opline->op1_type & (IS_VAR|IS_TMP_VAR))
12659
0
         && !op1_avoid_refcounting) {
12660
0
          flags |= ZEND_JIT_EXIT_FREE_OP1;
12661
0
        }
12662
0
        if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
12663
0
         && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
12664
0
          flags |= ZEND_JIT_EXIT_FREE_OP2;
12665
0
        }
12666
12667
0
        val_addr = zend_jit_guard_fetch_result_type(jit, opline, val_addr, type,
12668
0
          (op1_info & MAY_BE_ARRAY_OF_REF) != 0, flags, op1_avoid_refcounting);
12669
0
        if (!val_addr) {
12670
0
          return 0;
12671
0
        }
12672
12673
0
        if (not_found_inputs) {
12674
0
          ir_END_list(not_found_inputs);
12675
0
          ir_MERGE_list(not_found_inputs);
12676
0
        }
12677
12678
        // ZVAL_COPY
12679
0
        jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, !result_avoid_refcounting);
12680
0
        if (Z_MODE(res_addr) != IS_REG) {
12681
0
        } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
12682
0
          return 0;
12683
0
        }
12684
0
      } else if (op1_info & MAY_BE_ARRAY_OF_REF) {
12685
        // ZVAL_COPY_DEREF
12686
0
        ir_ref type_info = jit_Z_TYPE_INFO(jit, val_addr);
12687
0
        if (!zend_jit_zval_copy_deref(jit, res_addr, val_addr, type_info)) {
12688
0
          return 0;
12689
0
        }
12690
0
      } else  {
12691
        // ZVAL_COPY
12692
0
        jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, true);
12693
0
      }
12694
12695
0
      ir_END_list(end_inputs);
12696
0
    } else if (not_found_inputs) {
12697
0
      ir_MERGE_list(not_found_inputs);
12698
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12699
0
      ir_END_list(end_inputs);
12700
0
    } else if (!end_inputs && jit->ctx.control) {
12701
0
      ir_END_list(end_inputs); /* dead code */
12702
0
    }
12703
0
  }
12704
12705
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY)) {
12706
0
    if (if_type) {
12707
0
      ir_IF_FALSE_cold(if_type);
12708
0
      if_type = IS_UNDEF;
12709
0
    }
12710
12711
0
    if (opline->opcode != ZEND_FETCH_LIST_R && (op1_info & MAY_BE_STRING)) {
12712
0
      ir_ref str_ref;
12713
12714
0
      may_throw = 1;
12715
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_STRING))) {
12716
0
        if (exit_addr && !(op1_info & MAY_BE_OBJECT)) {
12717
0
          jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, exit_addr);
12718
0
        } else {
12719
0
          if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
12720
0
          ir_IF_TRUE(if_type);
12721
0
        }
12722
0
      }
12723
0
      jit_SET_EX_OPLINE(jit, opline);
12724
0
      str_ref = jit_Z_PTR(jit, op1_addr);
12725
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12726
0
        ir_ref ref;
12727
12728
0
        if ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG) {
12729
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_offset_r_helper),
12730
0
            str_ref, jit_Z_LVAL(jit, op2_addr));
12731
0
        } else {
12732
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_r_helper),
12733
0
            str_ref, jit_ZVAL_ADDR(jit, op2_addr));
12734
0
        }
12735
0
        jit_set_Z_PTR(jit, res_addr, ref);
12736
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING);
12737
0
      } else {
12738
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_is_helper),
12739
0
          str_ref,
12740
0
          jit_ZVAL_ADDR(jit, op2_addr),
12741
0
          jit_ZVAL_ADDR(jit, res_addr));
12742
0
      }
12743
0
      ir_END_list(end_inputs);
12744
0
    }
12745
12746
0
    if (op1_info & MAY_BE_OBJECT) {
12747
0
      ir_ref arg2;
12748
12749
0
      if (if_type) {
12750
0
        ir_IF_FALSE_cold(if_type);
12751
0
        if_type = IS_UNDEF;
12752
0
      }
12753
12754
0
      may_throw = 1;
12755
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string))) {
12756
0
        if (exit_addr) {
12757
0
          jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
12758
0
        } else {
12759
0
          if_type = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
12760
0
          ir_IF_TRUE(if_type);
12761
0
        }
12762
0
      }
12763
12764
0
      jit_SET_EX_OPLINE(jit, opline);
12765
0
      if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
12766
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
12767
0
        arg2 = ir_CONST_ADDR(Z_ZV(op2_addr)+1);
12768
0
      } else {
12769
0
        arg2 = jit_ZVAL_ADDR(jit, op2_addr);
12770
0
      }
12771
12772
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12773
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_r_helper),
12774
0
          jit_ZVAL_ADDR(jit, op1_addr),
12775
0
          arg2,
12776
0
          jit_ZVAL_ADDR(jit, res_addr));
12777
0
      } else {
12778
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_is_helper),
12779
0
          jit_ZVAL_ADDR(jit, op1_addr),
12780
0
          arg2,
12781
0
          jit_ZVAL_ADDR(jit, res_addr));
12782
0
      }
12783
12784
0
      ir_END_list(end_inputs);
12785
0
    }
12786
12787
0
    if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string)))
12788
0
     && (!exit_addr || !(op1_info & (MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string)))) {
12789
12790
0
      if (if_type) {
12791
0
        ir_IF_FALSE_cold(if_type);
12792
0
        if_type = IS_UNDEF;
12793
0
      }
12794
12795
0
      if ((opline->opcode != ZEND_FETCH_DIM_IS && (op1_info & MAY_BE_UNDEF)) || (op2_info & MAY_BE_UNDEF)) {
12796
0
        jit_SET_EX_OPLINE(jit, opline);
12797
0
        if (opline->opcode != ZEND_FETCH_DIM_IS && (op1_info & MAY_BE_UNDEF)) {
12798
0
          may_throw = 1;
12799
0
          zend_jit_type_check_undef(jit, jit_Z_TYPE(jit, op1_addr), opline->op1.var, NULL,
12800
0
                  false, true, false);
12801
0
        }
12802
12803
0
        if (op2_info & MAY_BE_UNDEF) {
12804
0
          may_throw = 1;
12805
0
          zend_jit_type_check_undef(jit, jit_Z_TYPE(jit, op2_addr), opline->op2.var, NULL,
12806
0
                  false, true, false);
12807
0
        }
12808
0
      }
12809
12810
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12811
0
        ir_ref ref;
12812
12813
0
        may_throw = 1;
12814
0
        if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
12815
0
          ref = jit_ZVAL_ADDR(jit, orig_op1_addr);
12816
0
        } else {
12817
0
          jit_SET_EX_OPLINE(jit, opline);
12818
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
12819
0
        }
12820
0
        if (opline->opcode == ZEND_FETCH_LIST_R) {
12821
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_array_use), ref);
12822
0
        } else {
12823
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_array_access), ref);
12824
0
        }
12825
0
      }
12826
12827
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12828
0
      ir_END_list(end_inputs);
12829
0
    }
12830
0
  }
12831
12832
0
  if (end_inputs) {
12833
0
    ir_MERGE_list(end_inputs);
12834
12835
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
12836
0
    if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & MAY_BE_OBJECT)) {
12837
      /* Magic offsetGet() may increase refcount of the key */
12838
0
      op2_info |= MAY_BE_RCN;
12839
0
    }
12840
0
#endif
12841
12842
0
    if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
12843
0
      if ((op2_info & MAY_HAVE_DTOR) && (op2_info & MAY_BE_RC1)) {
12844
0
        may_throw = 1;
12845
0
      }
12846
0
      jit_FREE_OP(jit,  opline->op2_type, opline->op2, op2_info, opline);
12847
0
    }
12848
0
    if (opline->opcode != ZEND_FETCH_LIST_R && !op1_avoid_refcounting) {
12849
0
      if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
12850
0
        if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
12851
0
          may_throw = 1;
12852
0
        }
12853
0
        jit_FREE_OP(jit,  opline->op1_type, opline->op1, op1_info, opline);
12854
0
      }
12855
0
    }
12856
12857
0
    if (may_throw) {
12858
0
      zend_jit_check_exception(jit);
12859
0
    }
12860
0
  } else if (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) {
12861
0
    ir_BEGIN(IR_UNUSED); /* unreachable tail */
12862
0
  }
12863
12864
0
  return 1;
12865
0
}
12866
12867
static zend_jit_addr zend_jit_prepare_array_update(zend_jit_ctx   *jit,
12868
                                                   const zend_op  *opline,
12869
                                                   uint32_t       *op1_info_ptr,
12870
                                                   zend_jit_addr   op1_addr,
12871
                                                   ir_ref         *if_type,
12872
                                                   ir_ref         *ht_ref,
12873
                                                   int            *may_throw)
12874
0
{
12875
0
  ir_ref ref = IR_UNUSED;
12876
0
  ir_ref array_reference_end = IR_UNUSED, array_reference_ref = IR_UNUSED;
12877
0
  ir_refs *array_inputs, *array_values;
12878
0
  uint32_t op1_info = *op1_info_ptr;
12879
12880
0
  ir_refs_init(array_inputs, 4);
12881
0
  ir_refs_init(array_values, 4);
12882
12883
0
  ref = jit_ZVAL_ADDR(jit, op1_addr);
12884
0
  if (op1_info & MAY_BE_REF) {
12885
0
    ir_ref if_reference, if_array, end1, ref2;
12886
12887
0
    *may_throw = 1;
12888
0
    if_reference = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
12889
0
    ir_IF_FALSE(if_reference);
12890
0
    end1 = ir_END();
12891
0
    ir_IF_TRUE_cold(if_reference);
12892
0
    array_reference_ref = ir_ADD_OFFSET(jit_Z_PTR_ref(jit, ref), offsetof(zend_reference, val));
12893
0
    if_array = jit_if_Z_TYPE_ref(jit, array_reference_ref, ir_CONST_U8(IS_ARRAY));
12894
0
    ir_IF_TRUE(if_array);
12895
0
    array_reference_end = ir_END();
12896
0
    ir_IF_FALSE_cold(if_array);
12897
0
    if (opline->opcode != ZEND_FETCH_DIM_RW && opline->opcode != ZEND_ASSIGN_DIM_OP) {
12898
0
      jit_SET_EX_OPLINE(jit, opline);
12899
0
    }
12900
0
    ref2 = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_prepare_assign_dim_ref), ref);
12901
0
    ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
12902
12903
0
    ir_MERGE_WITH(end1);
12904
0
    ref = ir_PHI_2(IR_ADDR, ref2, ref);
12905
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
12906
0
  }
12907
12908
0
  if (op1_info & MAY_BE_ARRAY) {
12909
0
    ir_ref op1_ref = ref;
12910
12911
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
12912
0
      *if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
12913
0
      ir_IF_TRUE(*if_type);
12914
0
    }
12915
0
    if (array_reference_end) {
12916
0
      ir_MERGE_WITH(array_reference_end);
12917
0
      op1_ref = ir_PHI_2(IR_ADDR, ref, array_reference_ref);
12918
0
    }
12919
    // JIT: SEPARATE_ARRAY()
12920
0
    ref = jit_Z_PTR_ref(jit, op1_ref);
12921
0
    if (RC_MAY_BE_N(op1_info)) {
12922
0
      if (RC_MAY_BE_1(op1_info)) {
12923
0
        ir_ref if_refcount_1 = ir_IF(ir_EQ(jit_GC_REFCOUNT(jit, ref), ir_CONST_U32(1)));
12924
0
        ir_IF_TRUE(if_refcount_1);
12925
0
        ir_refs_add(array_inputs, ir_END());
12926
0
        ir_refs_add(array_values, ref);
12927
0
        ir_IF_FALSE(if_refcount_1);
12928
0
      }
12929
0
      ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_zval_array_dup), op1_ref);
12930
0
    }
12931
0
    if (array_inputs->count || (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL))) {
12932
0
      ir_refs_add(array_inputs, ir_END());
12933
0
      ir_refs_add(array_values, ref);
12934
0
    }
12935
0
  }
12936
12937
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL)) {
12938
0
    if (*if_type) {
12939
0
      ir_IF_FALSE_cold(*if_type);
12940
0
      *if_type = IR_UNUSED;
12941
0
    }
12942
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_ARRAY))) {
12943
0
      *if_type = ir_IF(ir_LE(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_NULL)));
12944
0
      ir_IF_TRUE(*if_type);
12945
0
    }
12946
0
    if ((op1_info & MAY_BE_UNDEF)
12947
0
     && (opline->opcode == ZEND_FETCH_DIM_RW || opline->opcode == ZEND_ASSIGN_DIM_OP)) {
12948
0
      ir_ref end1 = IR_UNUSED;
12949
12950
0
      *may_throw = 1;
12951
0
      if (op1_info & MAY_BE_NULL) {
12952
0
        ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
12953
0
        ir_IF_TRUE(if_def);
12954
0
        end1 = ir_END();
12955
0
        ir_IF_FALSE(if_def);
12956
0
      }
12957
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
12958
0
      if (end1) {
12959
0
        ir_MERGE_WITH(end1);
12960
0
      }
12961
0
    }
12962
    // JIT: ZVAL_ARR(container, zend_new_array(8));
12963
0
    ref = ir_CALL_1(IR_ADDR,
12964
0
      jit_STUB_FUNC_ADDR(jit, jit_stub_new_array, IR_FASTCALL_FUNC),
12965
0
      jit_ZVAL_ADDR(jit, op1_addr));
12966
0
    if (array_inputs->count) {
12967
0
      ir_refs_add(array_inputs, ir_END());
12968
0
      ir_refs_add(array_values, ref);
12969
0
    }
12970
0
    op1_info &= ~(MAY_BE_UNDEF | MAY_BE_NULL);
12971
0
    op1_info |= MAY_BE_ARRAY | MAY_BE_RC1;
12972
0
    *op1_info_ptr = op1_info;
12973
0
  }
12974
12975
0
  if (array_inputs->count) {
12976
0
    ir_MERGE_N(array_inputs->count, array_inputs->refs);
12977
0
    ref = ir_PHI_N(IR_ADDR, array_values->count, array_values->refs);
12978
0
  }
12979
12980
0
  *ht_ref = ref;
12981
0
  return op1_addr;
12982
0
}
12983
12984
static int zend_jit_fetch_dim(zend_jit_ctx   *jit,
12985
                              const zend_op  *opline,
12986
                              uint32_t        op1_info,
12987
                              zend_jit_addr   op1_addr,
12988
                              uint32_t        op2_info,
12989
                              zend_jit_addr   op2_addr,
12990
                              zend_ssa_range *op2_range,
12991
                              zend_jit_addr   res_addr,
12992
                              uint8_t         dim_type)
12993
0
{
12994
0
  int may_throw = 0;
12995
0
  ir_ref end_inputs = IR_UNUSED;
12996
0
  ir_ref ref, if_type = IR_UNUSED, ht_ref;
12997
12998
0
  if (opline->opcode == ZEND_FETCH_DIM_RW) {
12999
0
    jit_SET_EX_OPLINE(jit, opline);
13000
0
  }
13001
13002
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13003
13004
0
  if (op1_info & MAY_BE_ARRAY) {
13005
0
    ir_refs *found_inputs, *found_vals;
13006
13007
0
    ir_refs_init(found_inputs, 8);
13008
0
    ir_refs_init(found_vals, 8);
13009
13010
0
    if (opline->op2_type == IS_UNUSED) {
13011
0
      ir_ref if_ok;
13012
13013
0
      may_throw = 1;
13014
      // JIT:var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13015
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13016
0
        ht_ref, jit_EG(uninitialized_zval));
13017
13018
      // JIT: if (UNEXPECTED(!var_ptr)) {
13019
0
      if_ok = ir_IF(ref);
13020
0
      ir_IF_FALSE_cold(if_ok);
13021
0
      if (opline->opcode != ZEND_FETCH_DIM_RW) {
13022
0
        jit_SET_EX_OPLINE(jit, opline);
13023
0
      }
13024
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13025
0
      ir_END_list(end_inputs);
13026
13027
0
      ir_IF_TRUE(if_ok);
13028
0
      jit_set_Z_PTR(jit, res_addr, ref);
13029
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
13030
13031
0
      ir_END_list(end_inputs);
13032
0
    } else {
13033
0
      uint32_t type;
13034
13035
0
      switch (opline->opcode) {
13036
0
        case ZEND_FETCH_DIM_W:
13037
0
        case ZEND_FETCH_LIST_W:
13038
0
          type = BP_VAR_W;
13039
0
          break;
13040
0
        case ZEND_FETCH_DIM_RW:
13041
0
          may_throw = 1;
13042
0
          type = BP_VAR_RW;
13043
0
          break;
13044
0
        case ZEND_FETCH_DIM_UNSET:
13045
0
          type = BP_VAR_UNSET;
13046
0
          break;
13047
0
        default:
13048
0
          ZEND_UNREACHABLE();
13049
0
      }
13050
13051
0
      if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
13052
0
        may_throw = 1;
13053
0
      }
13054
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, type, op1_info,
13055
0
          op2_info, op2_addr, op2_range, dim_type, NULL, NULL, NULL,
13056
0
          false, ht_ref, found_inputs, found_vals, &end_inputs, NULL)) {
13057
0
        return 0;
13058
0
      }
13059
13060
0
      if (type == BP_VAR_RW || (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING)))) {
13061
0
        if (end_inputs) {
13062
0
          ir_MERGE_list(end_inputs);
13063
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
13064
0
          end_inputs = ir_END();
13065
0
        }
13066
0
      } else if (!(op2_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
13067
        /* impossible dead path */
13068
0
        end_inputs = ir_END();
13069
0
      } else {
13070
0
        ZEND_ASSERT(end_inputs == IR_UNUSED);
13071
0
      }
13072
13073
0
      if (found_inputs->count) {
13074
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13075
0
        ref = ir_PHI_N(IR_ADDR, found_vals->count, found_vals->refs);
13076
0
        jit_set_Z_PTR(jit, res_addr, ref);
13077
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
13078
0
        ir_END_list(end_inputs);
13079
0
      }
13080
13081
0
    }
13082
0
  }
13083
13084
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13085
0
    ir_ref arg2;
13086
13087
0
    may_throw = 1;
13088
13089
0
    if (if_type) {
13090
0
      ir_IF_FALSE(if_type);
13091
0
      if_type = IR_UNUSED;
13092
0
    }
13093
13094
0
    if (opline->opcode != ZEND_FETCH_DIM_RW) {
13095
0
      jit_SET_EX_OPLINE(jit, opline);
13096
0
    }
13097
13098
0
      if (opline->op2_type == IS_UNUSED) {
13099
0
      arg2 = IR_NULL;
13100
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13101
0
      ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13102
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13103
0
    } else {
13104
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13105
0
    }
13106
13107
0
    switch (opline->opcode) {
13108
0
      case ZEND_FETCH_DIM_W:
13109
0
      case ZEND_FETCH_LIST_W:
13110
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_w_helper),
13111
0
          jit_ZVAL_ADDR(jit, op1_addr),
13112
0
          arg2,
13113
0
          jit_ZVAL_ADDR(jit, res_addr));
13114
0
        break;
13115
0
      case ZEND_FETCH_DIM_RW:
13116
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_rw_helper),
13117
0
          jit_ZVAL_ADDR(jit, op1_addr),
13118
0
          arg2,
13119
0
          jit_ZVAL_ADDR(jit, res_addr));
13120
0
        break;
13121
//      case ZEND_FETCH_DIM_UNSET:
13122
//        | EXT_CALL zend_jit_fetch_dim_obj_unset_helper, r0
13123
//        break;
13124
0
      default:
13125
0
        ZEND_UNREACHABLE();
13126
0
      }
13127
13128
0
    if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_ARRAY)) {
13129
0
      ir_END_list(end_inputs);
13130
0
    }
13131
0
  }
13132
13133
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13134
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_ARRAY|MAY_BE_OBJECT))) {
13135
    /* ASSIGN_DIM may increase refcount of the key */
13136
0
    op2_info |= MAY_BE_RCN;
13137
0
  }
13138
0
#endif
13139
13140
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR))
13141
0
   && (op2_info & MAY_HAVE_DTOR)
13142
0
   && (op2_info & MAY_BE_RC1)) {
13143
0
    may_throw = 1;
13144
0
  }
13145
13146
0
  if (end_inputs) {
13147
0
    ir_MERGE_list(end_inputs);
13148
0
  }
13149
13150
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13151
13152
0
  if (may_throw) {
13153
0
    zend_jit_check_exception(jit);
13154
0
  }
13155
13156
0
  return 1;
13157
0
}
13158
13159
static int zend_jit_isset_isempty_dim(zend_jit_ctx   *jit,
13160
                                      const zend_op  *opline,
13161
                                      uint32_t        op1_info,
13162
                                      zend_jit_addr   op1_addr,
13163
                                      bool       op1_avoid_refcounting,
13164
                                      uint32_t        op2_info,
13165
                                      zend_jit_addr   op2_addr,
13166
                                      zend_ssa_range *op2_range,
13167
                                      uint8_t         dim_type,
13168
                                      int             may_throw,
13169
                                      uint8_t         smart_branch_opcode,
13170
                                      uint32_t        target_label,
13171
                                      uint32_t        target_label2,
13172
                                      const void     *exit_addr)
13173
0
{
13174
0
  zend_jit_addr res_addr;
13175
0
  ir_ref if_type = IR_UNUSED;
13176
0
  ir_ref false_inputs = IR_UNUSED, end_inputs = IR_UNUSED;
13177
0
  ir_refs *true_inputs;
13178
13179
0
  ir_refs_init(true_inputs, 8);
13180
13181
  // TODO: support for empty() ???
13182
0
  ZEND_ASSERT(!(opline->extended_value & ZEND_ISEMPTY));
13183
13184
0
  res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
13185
13186
0
  if (op1_info & MAY_BE_REF) {
13187
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
13188
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
13189
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
13190
0
  }
13191
13192
0
  if (op1_info & MAY_BE_ARRAY) {
13193
0
    const void *found_exit_addr = NULL;
13194
0
    const void *not_found_exit_addr = NULL;
13195
0
    ir_ref ht_ref;
13196
13197
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
13198
0
      if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
13199
0
      ir_IF_TRUE(if_type);
13200
0
    }
13201
13202
0
    ht_ref = jit_Z_PTR(jit, op1_addr);
13203
13204
0
    if (exit_addr
13205
0
     && !(op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY))
13206
0
     && !may_throw
13207
0
     && (!(opline->op1_type & (IS_TMP_VAR|IS_VAR)) || op1_avoid_refcounting)
13208
0
     && (!(opline->op2_type & (IS_TMP_VAR|IS_VAR)) || !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)))) {
13209
0
      if (smart_branch_opcode == ZEND_JMPNZ) {
13210
0
        found_exit_addr = exit_addr;
13211
0
      } else {
13212
0
        not_found_exit_addr = exit_addr;
13213
0
      }
13214
0
    }
13215
0
    if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_JIT_IS, op1_info,
13216
0
        op2_info, op2_addr, op2_range, dim_type, found_exit_addr, not_found_exit_addr, NULL,
13217
0
        false, ht_ref, true_inputs, NULL, &false_inputs, NULL)) {
13218
0
      return 0;
13219
0
    }
13220
13221
0
    if (found_exit_addr) {
13222
0
      ir_MERGE_list(false_inputs);
13223
0
      return 1;
13224
0
    } else if (not_found_exit_addr) {
13225
0
      ir_MERGE_N(true_inputs->count, true_inputs->refs);
13226
0
      return 1;
13227
0
    }
13228
0
  }
13229
13230
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY)) {
13231
0
    if (if_type) {
13232
0
      ir_IF_FALSE(if_type);
13233
0
      if_type = IR_UNUSED;
13234
0
    }
13235
13236
0
    if (op1_info & (MAY_BE_STRING|MAY_BE_OBJECT)) {
13237
0
      ir_ref ref, arg1, arg2, if_true;
13238
13239
0
      jit_SET_EX_OPLINE(jit, opline);
13240
0
      arg1 = jit_ZVAL_ADDR(jit, op1_addr);
13241
0
      if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13242
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13243
0
        arg2 = ir_CONST_ADDR(Z_ZV(op2_addr)+1);
13244
0
      } else {
13245
0
        arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13246
0
      }
13247
0
      ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_isset_dim_helper), arg1, arg2);
13248
0
      if_true = ir_IF(ref);
13249
0
      ir_IF_TRUE(if_true);
13250
0
      ir_refs_add(true_inputs, ir_END());
13251
0
      ir_IF_FALSE(if_true);
13252
0
      ir_END_list(false_inputs);
13253
0
    } else {
13254
0
      if (op2_info & MAY_BE_UNDEF) {
13255
0
        ir_ref end1 = IR_UNUSED;
13256
13257
0
        if (op2_info & MAY_BE_ANY) {
13258
0
          ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op2_addr));
13259
0
          ir_IF_TRUE(if_def);
13260
0
          end1 = ir_END();
13261
0
          ir_IF_FALSE(if_def);
13262
0
        }
13263
0
        jit_SET_EX_OPLINE(jit, opline);
13264
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op2.var));
13265
0
        if (end1) {
13266
0
          ir_MERGE_WITH(end1);
13267
0
        }
13268
0
      }
13269
0
      ir_END_list(false_inputs);
13270
0
    }
13271
0
  }
13272
13273
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13274
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & MAY_BE_OBJECT)) {
13275
    /* Magic offsetExists() may increase refcount of the key */
13276
0
    op2_info |= MAY_BE_RCN;
13277
0
  }
13278
0
#endif
13279
13280
0
  if (true_inputs->count) {
13281
0
    ir_MERGE_N(true_inputs->count, true_inputs->refs);
13282
13283
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13284
0
    if (!op1_avoid_refcounting) {
13285
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13286
0
    }
13287
0
    if (may_throw) {
13288
0
      zend_jit_check_exception_undef_result(jit, opline);
13289
0
    }
13290
0
    if (!(opline->extended_value & ZEND_ISEMPTY)) {
13291
0
      if (exit_addr) {
13292
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
13293
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13294
0
        } else {
13295
0
          ir_END_list(end_inputs);
13296
0
        }
13297
0
      } else if (smart_branch_opcode) {
13298
0
        if (smart_branch_opcode == ZEND_JMPZ) {
13299
0
          _zend_jit_add_predecessor_ref(jit, target_label2, jit->b, ir_END());
13300
0
        } else if (smart_branch_opcode == ZEND_JMPNZ) {
13301
0
          _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
13302
0
        } else {
13303
0
          ZEND_UNREACHABLE();
13304
0
        }
13305
0
      } else {
13306
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
13307
0
        ir_END_list(end_inputs);
13308
0
      }
13309
0
    } else {
13310
0
      ZEND_UNREACHABLE(); // TODO: support for empty()
13311
0
    }
13312
0
  }
13313
13314
0
  ir_MERGE_list(false_inputs);
13315
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13316
0
  if (!op1_avoid_refcounting) {
13317
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13318
0
  }
13319
0
  if (may_throw) {
13320
0
    zend_jit_check_exception_undef_result(jit, opline);
13321
0
  }
13322
0
  if (!(opline->extended_value & ZEND_ISEMPTY)) {
13323
0
    if (exit_addr) {
13324
0
      if (smart_branch_opcode == ZEND_JMPZ) {
13325
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13326
0
      } else {
13327
0
        ir_END_list(end_inputs);
13328
0
      }
13329
0
    } else if (smart_branch_opcode) {
13330
0
      if (smart_branch_opcode == ZEND_JMPZ) {
13331
0
        _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
13332
0
      } else if (smart_branch_opcode == ZEND_JMPNZ) {
13333
0
        _zend_jit_add_predecessor_ref(jit, target_label2, jit->b, ir_END());
13334
0
      } else {
13335
0
        ZEND_UNREACHABLE();
13336
0
      }
13337
0
    } else {
13338
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
13339
0
      ir_END_list(end_inputs);
13340
0
    }
13341
0
  } else {
13342
0
    ZEND_UNREACHABLE(); // TODO: support for empty()
13343
0
  }
13344
13345
0
    if (!exit_addr && smart_branch_opcode) {
13346
0
    jit->b = -1;
13347
0
    } else {
13348
0
    ir_MERGE_list(end_inputs);
13349
0
    }
13350
13351
0
  return 1;
13352
0
}
13353
13354
static int zend_jit_assign_dim(zend_jit_ctx  *jit,
13355
                               const zend_op *opline,
13356
                               uint32_t       op1_info,
13357
                               zend_jit_addr  op1_addr,
13358
                               bool           op1_indirect,
13359
                               uint32_t       op2_info,
13360
                               zend_jit_addr  op2_addr,
13361
                               zend_ssa_range *op2_range,
13362
                               uint32_t       val_info,
13363
                               zend_jit_addr  op3_addr,
13364
                               zend_jit_addr  op3_def_addr,
13365
                               zend_jit_addr  res_addr,
13366
                               uint8_t        dim_type,
13367
                               int            may_throw)
13368
0
{
13369
0
  ir_ref if_type = IR_UNUSED;
13370
0
  ir_ref end_inputs = IR_UNUSED, ht_ref;
13371
13372
0
  if (op3_addr != op3_def_addr && op3_def_addr) {
13373
0
    if (!zend_jit_update_regs(jit, (opline+1)->op1.var, op3_addr, op3_def_addr, val_info)) {
13374
0
      return 0;
13375
0
    }
13376
0
    if (Z_MODE(op3_def_addr) == IS_REG && Z_MODE(op3_addr) != IS_REG) {
13377
0
      op3_addr = op3_def_addr;
13378
0
    }
13379
0
  }
13380
13381
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && (val_info & MAY_BE_UNDEF)) {
13382
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
13383
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13384
13385
0
    if (!exit_addr) {
13386
0
      return 0;
13387
0
    }
13388
13389
0
    jit_guard_not_Z_TYPE(jit, op3_addr, IS_UNDEF, exit_addr);
13390
13391
0
    val_info &= ~MAY_BE_UNDEF;
13392
0
  }
13393
13394
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13395
13396
0
  if (op1_info & MAY_BE_ARRAY) {
13397
0
    if (opline->op2_type == IS_UNUSED) {
13398
0
      uint32_t var_info = MAY_BE_NULL;
13399
0
      ir_ref if_ok, ref;
13400
0
      zend_jit_addr var_addr;
13401
13402
      // JIT: var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13403
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13404
0
        ht_ref, jit_EG(uninitialized_zval));
13405
13406
      // JIT: if (UNEXPECTED(!var_ptr)) {
13407
0
      if_ok = ir_IF(ref);
13408
0
      ir_IF_FALSE_cold(if_ok);
13409
13410
      // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
13411
0
      jit_SET_EX_OPLINE(jit, opline);
13412
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13413
13414
0
      ir_END_list(end_inputs);
13415
13416
0
      ir_IF_TRUE(if_ok);
13417
0
      var_addr = ZEND_ADDR_REF_ZVAL(ref);
13418
0
      if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, -1, (opline+1)->op1_type, op3_addr, val_info, res_addr, false)) {
13419
0
        return 0;
13420
0
      }
13421
0
    } else {
13422
0
      uint32_t var_info = zend_array_element_type(op1_info, opline->op1_type, 0, 0);
13423
0
      zend_jit_addr var_addr;
13424
0
      ir_ref ref;
13425
0
      ir_refs *found_inputs, *found_values;
13426
13427
0
      ir_refs_init(found_inputs, 8);
13428
0
      ir_refs_init(found_values, 8);
13429
13430
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_VAR_W, op1_info,
13431
0
          op2_info, op2_addr, op2_range, dim_type, NULL, NULL, NULL,
13432
0
          false, ht_ref, found_inputs, found_values, &end_inputs, NULL)) {
13433
0
        return 0;
13434
0
      }
13435
13436
0
      if (op1_info & (MAY_BE_ARRAY_OF_REF|MAY_BE_OBJECT)) {
13437
0
        var_info |= MAY_BE_REF;
13438
0
      }
13439
0
      if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
13440
0
        var_info |= MAY_BE_RC1;
13441
0
      }
13442
13443
0
      if (found_inputs->count) {
13444
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13445
0
        ref = ir_PHI_N(IR_ADDR, found_values->count, found_values->refs);
13446
0
        var_addr = ZEND_ADDR_REF_ZVAL(ref);
13447
13448
        // JIT: value = zend_assign_to_variable(variable_ptr, value, OP_DATA_TYPE);
13449
0
        if (opline->op1_type == IS_VAR
13450
0
         && Z_MODE(op3_addr) != IS_REG
13451
0
         && opline->result_type == IS_UNUSED
13452
0
         && (res_addr == 0 || Z_MODE(res_addr) != IS_REG)) {
13453
0
          if (!zend_jit_assign_to_variable_call(jit, opline, var_addr, var_addr, var_info, -1, (opline+1)->op1_type, op3_addr, val_info, res_addr, false)) {
13454
0
            return 0;
13455
0
          }
13456
0
        } else {
13457
0
          if (!zend_jit_assign_to_variable(jit, opline, var_addr, var_addr, var_info, -1, (opline+1)->op1_type, op3_addr, val_info, res_addr, 0, false)) {
13458
0
            return 0;
13459
0
          }
13460
0
        }
13461
0
      }
13462
0
    }
13463
13464
0
    ir_END_list(end_inputs);
13465
0
  }
13466
13467
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13468
0
    ir_ref arg2, arg4;
13469
13470
0
    if (if_type) {
13471
0
      ir_IF_FALSE_cold(if_type);
13472
0
      if_type = IR_UNUSED;
13473
0
    }
13474
13475
0
    jit_SET_EX_OPLINE(jit, opline);
13476
13477
0
      if (opline->op2_type == IS_UNUSED) {
13478
0
      arg2 = IR_NULL;
13479
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13480
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13481
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13482
0
    } else {
13483
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13484
0
    }
13485
13486
0
    if (opline->result_type == IS_UNUSED) {
13487
0
      arg4 = IR_NULL;
13488
0
    } else {
13489
0
      arg4 = jit_ZVAL_ADDR(jit, res_addr);
13490
0
    }
13491
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_helper),
13492
0
      jit_ZVAL_ADDR(jit, op1_addr),
13493
0
      arg2,
13494
0
      jit_ZVAL_ADDR(jit, op3_addr),
13495
0
      arg4);
13496
13497
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13498
0
    if (((opline+1)->op1_type & (IS_TMP_VAR|IS_VAR)) && (val_info & MAY_BE_RC1)) {
13499
      /* ASSIGN_DIM may increase refcount of the value */
13500
0
      val_info |= MAY_BE_RCN;
13501
0
    }
13502
0
#endif
13503
13504
0
    jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, NULL);
13505
13506
0
    ir_END_list(end_inputs);
13507
0
  }
13508
13509
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13510
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_ARRAY|MAY_BE_OBJECT))) {
13511
    /* ASSIGN_DIM may increase refcount of the key */
13512
0
    op2_info |= MAY_BE_RCN;
13513
0
  }
13514
0
#endif
13515
13516
0
  ir_MERGE_list(end_inputs);
13517
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13518
13519
0
  if (!op1_indirect) {
13520
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13521
0
  }
13522
13523
0
  if (may_throw) {
13524
0
    zend_jit_check_exception(jit);
13525
0
  }
13526
13527
0
  return 1;
13528
0
}
13529
13530
static int zend_jit_assign_dim_op(zend_jit_ctx   *jit,
13531
                                  const zend_op  *opline,
13532
                                  uint32_t        op1_info,
13533
                                  uint32_t        op1_def_info,
13534
                                  zend_jit_addr   op1_addr,
13535
                                  bool            op1_indirect,
13536
                                  uint32_t        op2_info,
13537
                                  zend_jit_addr   op2_addr,
13538
                                  zend_ssa_range *op2_range,
13539
                                  uint32_t        op1_data_info,
13540
                                  zend_jit_addr   op3_addr,
13541
                                  zend_ssa_range *op1_data_range,
13542
                                  uint8_t         dim_type,
13543
                                  int             may_throw)
13544
0
{
13545
0
  zend_jit_addr var_addr = IS_UNUSED;
13546
0
  const void *not_found_exit_addr = NULL;
13547
0
  uint32_t var_info = MAY_BE_NULL;
13548
0
  ir_ref if_type = IS_UNUSED;
13549
0
  ir_ref end_inputs = IR_UNUSED, ht_ref;
13550
0
  bool emit_fast_path = true;
13551
13552
0
  ZEND_ASSERT(opline->result_type == IS_UNUSED);
13553
13554
0
  if (may_throw) {
13555
0
    jit_SET_EX_OPLINE(jit, opline);
13556
0
  }
13557
13558
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13559
13560
0
  if (Z_MODE(op3_addr) == IS_REG
13561
0
   && Z_LOAD(op3_addr)
13562
0
   && jit->ra[Z_SSA_VAR(op3_addr)].ref == IR_NULL) {
13563
    /* Force load */
13564
0
    zend_jit_use_reg(jit, op3_addr);
13565
0
  }
13566
13567
0
  if (op1_info & MAY_BE_ARRAY) {
13568
0
    uint32_t var_def_info = zend_array_element_type(op1_def_info, opline->op1_type, 1, 0);
13569
13570
0
    if (opline->op2_type == IS_UNUSED) {
13571
0
      var_info = MAY_BE_NULL;
13572
0
      ir_ref if_ok, ref;
13573
13574
      // JIT: var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13575
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13576
0
        ht_ref, jit_EG(uninitialized_zval));
13577
13578
      // JIT: if (UNEXPECTED(!var_ptr)) {
13579
0
      if_ok = ir_IF(ref);
13580
0
      ir_IF_FALSE_cold(if_ok);
13581
13582
      // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
13583
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13584
13585
0
      ir_END_list(end_inputs);
13586
13587
0
      ir_IF_TRUE(if_ok);
13588
0
      var_addr = ZEND_ADDR_REF_ZVAL(ref);
13589
0
    } else {
13590
0
      ir_ref ref;
13591
0
      ir_refs *found_inputs, *found_values;
13592
13593
0
      ir_refs_init(found_inputs, 8);
13594
0
      ir_refs_init(found_values, 8);
13595
13596
0
      var_info = zend_array_element_type(op1_info, opline->op1_type, 0, 0);
13597
0
      if (op1_info & (MAY_BE_ARRAY_OF_REF|MAY_BE_OBJECT)) {
13598
0
        var_info |= MAY_BE_REF;
13599
0
      }
13600
0
      if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
13601
0
        var_info |= MAY_BE_RC1;
13602
0
      }
13603
13604
0
      if (dim_type != IS_UNKNOWN
13605
0
       && dim_type != IS_UNDEF
13606
0
       && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY
13607
0
       && (op2_info & (MAY_BE_LONG|MAY_BE_STRING))
13608
0
       && !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING)))) {
13609
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
13610
0
        not_found_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13611
0
        if (!not_found_exit_addr) {
13612
0
          return 0;
13613
0
        }
13614
0
      }
13615
13616
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_VAR_RW, op1_info,
13617
0
          op2_info, op2_addr, op2_range, dim_type, NULL, not_found_exit_addr, NULL,
13618
0
          false, ht_ref, found_inputs, found_values, &end_inputs, NULL)) {
13619
0
        return 0;
13620
0
      }
13621
13622
0
      if (found_inputs->count) {
13623
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13624
0
        ref = ir_PHI_N(IR_ADDR, found_values->count, found_values->refs);
13625
0
        var_addr = ZEND_ADDR_REF_ZVAL(ref);
13626
13627
0
        if (not_found_exit_addr && dim_type != IS_REFERENCE) {
13628
0
          jit_guard_Z_TYPE(jit, var_addr, dim_type, not_found_exit_addr);
13629
0
          var_info = (1 << dim_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
13630
0
        }
13631
0
        if (var_info & MAY_BE_REF) {
13632
0
          binary_op_type binary_op = get_binary_op(opline->extended_value);
13633
0
          ir_ref if_ref, if_typed, noref_path, ref_path, ref, reference, ref2, arg2;
13634
13635
0
          ref = jit_ZVAL_ADDR(jit, var_addr);
13636
0
          if_ref = jit_if_Z_TYPE(jit, var_addr, IS_REFERENCE);
13637
0
          ir_IF_FALSE(if_ref);
13638
0
          noref_path = ir_END();
13639
0
          ir_IF_TRUE(if_ref);
13640
13641
0
          reference = jit_Z_PTR_ref(jit, ref);
13642
0
          ref2 = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
13643
0
          if_typed = jit_if_TYPED_REF(jit, reference);
13644
0
          ir_IF_FALSE(if_typed);
13645
0
          ref_path = ir_END();
13646
0
          ir_IF_TRUE_cold(if_typed);
13647
13648
0
          if (Z_MODE(op3_addr) == IS_REG) {
13649
0
            zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
13650
0
            if (!zend_jit_spill_store_inv(jit, op3_addr, real_addr, op1_data_info)) {
13651
0
              return 0;
13652
0
            }
13653
0
            op3_addr = real_addr;
13654
0
          }
13655
0
          arg2 = jit_ZVAL_ADDR(jit, op3_addr);
13656
0
          ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
13657
0
            reference, arg2, ir_CONST_FC_FUNC(binary_op));
13658
13659
0
          ir_END_list(end_inputs);
13660
13661
0
          ir_MERGE_2(noref_path, ref_path);
13662
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
13663
0
          var_addr = ZEND_ADDR_REF_ZVAL(ref);
13664
0
        }
13665
0
      } else {
13666
0
        emit_fast_path = false;
13667
0
      }
13668
0
    }
13669
13670
0
    if (emit_fast_path) {
13671
0
      uint8_t val_op_type = (opline+1)->op1_type;
13672
13673
0
      if (val_op_type & (IS_TMP_VAR|IS_VAR)) {
13674
        /* prevent FREE_OP in the helpers */
13675
0
        val_op_type = IS_CV;
13676
0
      }
13677
13678
0
      switch (opline->extended_value) {
13679
0
        case ZEND_ADD:
13680
0
        case ZEND_SUB:
13681
0
        case ZEND_MUL:
13682
0
        case ZEND_DIV:
13683
0
          if (!zend_jit_math_helper(jit, opline, opline->extended_value, IS_CV, opline->op1, var_addr, var_info, val_op_type, (opline+1)->op1, op3_addr, op1_data_info, 0, var_addr, var_def_info, var_info,
13684
0
              1 /* may overflow */, may_throw)) {
13685
0
            return 0;
13686
0
          }
13687
0
          break;
13688
0
        case ZEND_BW_OR:
13689
0
        case ZEND_BW_AND:
13690
0
        case ZEND_BW_XOR:
13691
0
        case ZEND_SL:
13692
0
        case ZEND_SR:
13693
0
        case ZEND_MOD:
13694
0
          if (!zend_jit_long_math_helper(jit, opline, opline->extended_value,
13695
0
              IS_CV, opline->op1, var_addr, var_info, NULL,
13696
0
              val_op_type, (opline+1)->op1, op3_addr, op1_data_info,
13697
0
              op1_data_range,
13698
0
              0, var_addr, var_def_info, var_info, may_throw)) {
13699
0
            return 0;
13700
0
          }
13701
0
          break;
13702
0
        case ZEND_CONCAT:
13703
0
          if (!zend_jit_concat_helper(jit, opline, IS_CV, opline->op1, var_addr, var_info, val_op_type, (opline+1)->op1, op3_addr, op1_data_info, var_addr,
13704
0
              may_throw)) {
13705
0
            return 0;
13706
0
          }
13707
0
          break;
13708
0
        default:
13709
0
          ZEND_UNREACHABLE();
13710
0
      }
13711
13712
0
      ir_END_list(end_inputs);
13713
0
    }
13714
0
  }
13715
13716
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13717
0
    binary_op_type binary_op;
13718
0
    ir_ref arg2;
13719
13720
0
    if (if_type) {
13721
0
      ir_IF_FALSE_cold(if_type);
13722
0
      if_type = IS_UNUSED;
13723
0
    }
13724
13725
0
      if (opline->op2_type == IS_UNUSED) {
13726
0
      arg2 = IR_NULL;
13727
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13728
0
      ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13729
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13730
0
    } else {
13731
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13732
0
    }
13733
0
    binary_op = get_binary_op(opline->extended_value);
13734
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_op_helper),
13735
0
      jit_ZVAL_ADDR(jit, op1_addr),
13736
0
      arg2,
13737
0
      jit_ZVAL_ADDR(jit, op3_addr),
13738
0
      ir_CONST_FC_FUNC(binary_op));
13739
0
    ir_END_list(end_inputs);
13740
0
  }
13741
13742
0
  if (end_inputs) {
13743
0
    ir_MERGE_list(end_inputs);
13744
0
  }
13745
13746
0
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, op1_data_info, NULL);
13747
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
13748
0
  if (!op1_indirect) {
13749
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
13750
0
  }
13751
0
  if (may_throw) {
13752
0
    zend_jit_check_exception(jit);
13753
0
  }
13754
13755
0
  return 1;
13756
0
}
13757
13758
static int zend_jit_fe_reset(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
13759
0
{
13760
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
13761
13762
  // JIT: ZVAL_COPY(res, value);
13763
0
  if (opline->op1_type == IS_CONST) {
13764
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
13765
13766
0
    jit_ZVAL_COPY_CONST(jit, res_addr, MAY_BE_ANY, MAY_BE_ANY, zv, true);
13767
0
  } else {
13768
0
    zend_jit_addr op1_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
13769
13770
0
    jit_ZVAL_COPY(jit, res_addr, -1, op1_addr, op1_info, opline->op1_type == IS_CV);
13771
0
  }
13772
13773
  // JIT: Z_FE_POS_P(res) = 0;
13774
0
  ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->result.var + offsetof(zval, u2.fe_pos)), ir_CONST_U32(0));
13775
13776
0
  return 1;
13777
0
}
13778
13779
static int zend_jit_packed_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var, uint32_t op_info)
13780
0
{
13781
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_PACKED_GUARD);
13782
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13783
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
13784
0
  ir_ref ref;
13785
13786
0
  if (!exit_addr) {
13787
0
    return 0;
13788
0
  }
13789
13790
0
  ref = ir_AND_U32(
13791
0
    ir_LOAD_U32(ir_ADD_OFFSET(jit_Z_PTR(jit, addr), offsetof(zend_array, u.flags))),
13792
0
    ir_CONST_U32(HASH_FLAG_PACKED));
13793
0
  if (op_info & MAY_BE_ARRAY_PACKED) {
13794
0
    ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13795
0
  } else {
13796
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
13797
0
  }
13798
13799
0
  return 1;
13800
0
}
13801
13802
static int zend_jit_fe_fetch(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint32_t op2_info, unsigned int target_label, uint8_t exit_opcode, const void *exit_addr)
13803
0
{
13804
0
  zend_jit_addr op1_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
13805
0
  ir_ref ref, ht_ref, hash_pos_ref, packed_pos_ref, hash_p_ref = IR_UNUSED, packed_p_ref = IR_UNUSED, if_packed = IR_UNUSED;
13806
0
  ir_ref if_def_hash = IR_UNUSED, if_def_packed = IR_UNUSED;
13807
0
  ir_ref exit_inputs = IR_UNUSED;
13808
13809
0
  if (!MAY_BE_HASH(op1_info) && !MAY_BE_PACKED(op1_info)) {
13810
    /* empty array */
13811
0
    if (exit_addr) {
13812
0
      if (exit_opcode == ZEND_JMP) {
13813
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13814
0
      }
13815
0
    } else {
13816
0
      zend_basic_block *bb;
13817
13818
0
      ZEND_ASSERT(jit->b >= 0);
13819
0
      bb = &jit->ssa->cfg.blocks[jit->b];
13820
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
13821
0
      jit->b = -1;
13822
0
    }
13823
0
    return 1;
13824
0
  }
13825
13826
  // JIT: array = EX_VAR(opline->op1.var);
13827
  // JIT: fe_ht = Z_ARRVAL_P(array);
13828
0
  ht_ref = jit_Z_PTR(jit, op1_addr);
13829
13830
0
  if (op1_info & MAY_BE_PACKED_GUARD) {
13831
0
    if (!zend_jit_packed_guard(jit, opline, opline->op1.var, op1_info)) {
13832
0
      return 0;
13833
0
    }
13834
0
  }
13835
13836
  // JIT: pos = Z_FE_POS_P(array);
13837
0
  hash_pos_ref = packed_pos_ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)));
13838
13839
0
  if (MAY_BE_HASH(op1_info)) {
13840
0
    ir_ref loop_ref, pos2_ref, p2_ref;
13841
13842
0
    if (MAY_BE_PACKED(op1_info)) {
13843
0
      ref = ir_AND_U32(
13844
0
        ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
13845
0
        ir_CONST_U32(HASH_FLAG_PACKED));
13846
0
      if_packed = ir_IF(ref);
13847
0
      ir_IF_FALSE(if_packed);
13848
0
    }
13849
13850
    // JIT: p = fe_ht->arData + pos;
13851
0
    if (sizeof(void*) == 8) {
13852
0
      ref = ir_ZEXT_A(hash_pos_ref);
13853
0
    } else {
13854
0
      ref = ir_BITCAST_A(hash_pos_ref);
13855
0
    }
13856
0
    hash_p_ref = ir_ADD_A(
13857
0
      ir_MUL_A(ref, ir_CONST_ADDR(sizeof(Bucket))),
13858
0
      ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arData))));
13859
13860
0
    loop_ref = ir_LOOP_BEGIN(ir_END());
13861
0
    hash_pos_ref = ir_PHI_2(IR_U32, hash_pos_ref, IR_UNUSED);
13862
0
    hash_p_ref = ir_PHI_2(IR_ADDR, hash_p_ref, IR_UNUSED);
13863
13864
    // JIT: if (UNEXPECTED(pos >= fe_ht->nNumUsed)) {
13865
0
    ref = ir_ULT(hash_pos_ref,
13866
0
      ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed))));
13867
13868
    // JIT: ZEND_VM_SET_RELATIVE_OPCODE(opline, opline->extended_value);
13869
    // JIT: ZEND_VM_CONTINUE();
13870
13871
0
    if (exit_addr) {
13872
0
      if (exit_opcode == ZEND_JMP) {
13873
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13874
0
      } else {
13875
0
        ir_ref if_fit = ir_IF(ref);
13876
0
        ir_IF_FALSE(if_fit);
13877
0
        ir_END_list(exit_inputs);
13878
0
        ir_IF_TRUE(if_fit);
13879
0
      }
13880
0
    } else {
13881
0
      ir_ref if_fit = ir_IF(ref);
13882
0
      ir_IF_FALSE(if_fit);
13883
0
      ir_END_list(exit_inputs);
13884
0
      ir_IF_TRUE(if_fit);
13885
0
    }
13886
13887
    // JIT: pos++;
13888
0
    pos2_ref = ir_ADD_U32(hash_pos_ref, ir_CONST_U32(1));
13889
13890
    // JIT: value_type = Z_TYPE_INFO_P(value);
13891
    // JIT: if (EXPECTED(value_type != IS_UNDEF)) {
13892
0
    if (!exit_addr || exit_opcode == ZEND_JMP) {
13893
0
      if_def_hash = ir_IF(jit_Z_TYPE_ref(jit, hash_p_ref));
13894
0
      ir_IF_FALSE(if_def_hash);
13895
0
    } else {
13896
0
      ir_GUARD_NOT(jit_Z_TYPE_ref(jit, hash_p_ref), ir_CONST_ADDR(exit_addr));
13897
0
    }
13898
13899
    // JIT: p++;
13900
0
    p2_ref = ir_ADD_OFFSET(hash_p_ref, sizeof(Bucket));
13901
13902
0
    ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
13903
0
    ir_PHI_SET_OP(hash_pos_ref, 2, pos2_ref);
13904
0
    ir_PHI_SET_OP(hash_p_ref, 2, p2_ref);
13905
13906
0
    if (MAY_BE_PACKED(op1_info)) {
13907
0
      ir_IF_TRUE(if_packed);
13908
0
    }
13909
0
  }
13910
0
  if (MAY_BE_PACKED(op1_info)) {
13911
0
    ir_ref loop_ref, pos2_ref, p2_ref;
13912
13913
    // JIT: p = fe_ht->arPacked + pos;
13914
0
    if (sizeof(void*) == 8) {
13915
0
      ref = ir_ZEXT_A(packed_pos_ref);
13916
0
    } else {
13917
0
      ref = ir_BITCAST_A(packed_pos_ref);
13918
0
    }
13919
0
    packed_p_ref = ir_ADD_A(
13920
0
      ir_MUL_A(ref, ir_CONST_ADDR(sizeof(zval))),
13921
0
      ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arPacked))));
13922
13923
0
    loop_ref = ir_LOOP_BEGIN(ir_END());
13924
0
    packed_pos_ref = ir_PHI_2(IR_U32, packed_pos_ref, IR_UNUSED);
13925
0
    packed_p_ref = ir_PHI_2(IR_ADDR, packed_p_ref, IR_UNUSED);
13926
13927
    // JIT: if (UNEXPECTED(pos >= fe_ht->nNumUsed)) {
13928
0
    ref = ir_ULT(packed_pos_ref,
13929
0
      ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed))));
13930
13931
    // JIT: ZEND_VM_SET_RELATIVE_OPCODE(opline, opline->extended_value);
13932
    // JIT: ZEND_VM_CONTINUE();
13933
0
    if (exit_addr) {
13934
0
      if (exit_opcode == ZEND_JMP) {
13935
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13936
0
      } else {
13937
0
        ir_ref if_fit = ir_IF(ref);
13938
0
        ir_IF_FALSE(if_fit);
13939
0
        ir_END_list(exit_inputs);
13940
0
        ir_IF_TRUE(if_fit);
13941
0
      }
13942
0
    } else {
13943
0
      ir_ref if_fit = ir_IF(ref);
13944
0
      ir_IF_FALSE(if_fit);
13945
0
      ir_END_list(exit_inputs);
13946
0
      ir_IF_TRUE(if_fit);
13947
0
    }
13948
13949
    // JIT: pos++;
13950
0
    pos2_ref = ir_ADD_U32(packed_pos_ref, ir_CONST_U32(1));
13951
13952
    // JIT: value_type = Z_TYPE_INFO_P(value);
13953
    // JIT: if (EXPECTED(value_type != IS_UNDEF)) {
13954
0
    if (!exit_addr || exit_opcode == ZEND_JMP) {
13955
0
      if_def_packed = ir_IF(jit_Z_TYPE_ref(jit, packed_p_ref));
13956
0
      ir_IF_FALSE(if_def_packed);
13957
0
    } else {
13958
0
      ir_GUARD_NOT(jit_Z_TYPE_ref(jit, packed_p_ref), ir_CONST_ADDR(exit_addr));
13959
0
    }
13960
13961
    // JIT: p++;
13962
0
    p2_ref = ir_ADD_OFFSET(packed_p_ref, sizeof(zval));
13963
13964
0
    ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
13965
0
    ir_PHI_SET_OP(packed_pos_ref, 2, pos2_ref);
13966
0
    ir_PHI_SET_OP(packed_p_ref, 2, p2_ref);
13967
0
  }
13968
13969
0
  if (!exit_addr || exit_opcode == ZEND_JMP) {
13970
0
    zend_jit_addr val_addr;
13971
0
    zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
13972
0
    uint32_t val_info;
13973
0
    ir_ref p_ref = IR_UNUSED, hash_path = IR_UNUSED;
13974
13975
0
    if (RETURN_VALUE_USED(opline)) {
13976
0
      zend_jit_addr res_addr = RES_ADDR();
13977
13978
0
      if (MAY_BE_HASH(op1_info)) {
13979
0
        ir_ref key_ref = IR_UNUSED, if_key = IR_UNUSED, key_path = IR_UNUSED;
13980
13981
0
        ZEND_ASSERT(if_def_hash);
13982
0
        ir_IF_TRUE(if_def_hash);
13983
13984
        // JIT: Z_FE_POS_P(array) = pos + 1;
13985
0
        ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
13986
0
          ir_ADD_U32(hash_pos_ref, ir_CONST_U32(1)));
13987
13988
0
        if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
13989
0
          key_ref = ir_LOAD_A(ir_ADD_OFFSET(hash_p_ref, offsetof(Bucket, key)));
13990
0
        }
13991
0
        if ((op1_info & MAY_BE_ARRAY_KEY_LONG)
13992
0
         && (op1_info & MAY_BE_ARRAY_KEY_STRING)) {
13993
          // JIT: if (!p->key) {
13994
0
          if_key = ir_IF(key_ref);
13995
0
          ir_IF_TRUE(if_key);
13996
0
        }
13997
0
        if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
13998
0
          ir_ref if_interned, interned_path;
13999
14000
          // JIT: ZVAL_STR_COPY(EX_VAR(opline->result.var), p->key);
14001
0
          jit_set_Z_PTR(jit, res_addr, key_ref);
14002
0
          ref = ir_AND_U32(
14003
0
            ir_LOAD_U32(ir_ADD_OFFSET(key_ref, offsetof(zend_refcounted, gc.u.type_info))),
14004
0
            ir_CONST_U32(IS_STR_INTERNED));
14005
0
          if_interned = ir_IF(ref);
14006
0
          ir_IF_TRUE(if_interned);
14007
14008
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING);
14009
14010
0
          interned_path = ir_END();
14011
0
          ir_IF_FALSE(if_interned);
14012
14013
0
          jit_GC_ADDREF(jit, key_ref);
14014
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING_EX);
14015
14016
0
          ir_MERGE_WITH(interned_path);
14017
14018
0
          if (op1_info & MAY_BE_ARRAY_KEY_LONG) {
14019
0
            key_path = ir_END();
14020
0
          }
14021
0
        }
14022
0
        if (op1_info & MAY_BE_ARRAY_KEY_LONG) {
14023
0
          if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14024
0
            ir_IF_FALSE(if_key);
14025
0
          }
14026
          // JIT: ZVAL_LONG(EX_VAR(opline->result.var), p->h);
14027
0
          ref = ir_LOAD_L(ir_ADD_OFFSET(hash_p_ref, offsetof(Bucket, h)));
14028
0
          jit_set_Z_LVAL(jit, res_addr, ref);
14029
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
14030
14031
0
          if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14032
0
            ir_MERGE_WITH(key_path);
14033
0
          }
14034
0
        }
14035
0
        if (MAY_BE_PACKED(op1_info)) {
14036
0
          hash_path = ir_END();
14037
0
        } else {
14038
0
          p_ref = hash_p_ref;
14039
0
        }
14040
0
      }
14041
0
      if (MAY_BE_PACKED(op1_info)) {
14042
0
        ZEND_ASSERT(if_def_packed);
14043
0
        ir_IF_TRUE(if_def_packed);
14044
14045
        // JIT: Z_FE_POS_P(array) = pos + 1;
14046
0
        ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14047
0
          ir_ADD_U32(packed_pos_ref, ir_CONST_U32(1)));
14048
14049
        // JIT: ZVAL_LONG(EX_VAR(opline->result.var), pos);
14050
0
        if (sizeof(zend_long) == 8) {
14051
0
          packed_pos_ref = ir_ZEXT_L(packed_pos_ref);
14052
0
        } else {
14053
0
          packed_pos_ref = ir_BITCAST_L(packed_pos_ref);
14054
0
        }
14055
0
        jit_set_Z_LVAL(jit, res_addr, packed_pos_ref);
14056
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
14057
14058
0
        if (MAY_BE_HASH(op1_info)) {
14059
0
          ir_MERGE_WITH(hash_path);
14060
0
          p_ref = ir_PHI_2(IR_ADDR, packed_p_ref, hash_p_ref);
14061
0
        } else {
14062
0
          p_ref = packed_p_ref;
14063
0
        }
14064
0
      }
14065
0
    } else {
14066
0
      ir_ref pos_ref = IR_UNUSED;
14067
14068
0
      if (if_def_hash && if_def_packed) {
14069
0
        ir_IF_TRUE(if_def_hash);
14070
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def_packed);
14071
0
        pos_ref = ir_PHI_2(IR_U32, hash_pos_ref, packed_pos_ref);
14072
0
        p_ref = ir_PHI_2(IR_ADDR, hash_p_ref, packed_p_ref);
14073
0
      } else if (if_def_hash) {
14074
0
        ir_IF_TRUE(if_def_hash);
14075
0
        pos_ref = hash_pos_ref;
14076
0
        p_ref = hash_p_ref;
14077
0
      } else if (if_def_packed) {
14078
0
        ir_IF_TRUE(if_def_packed);
14079
0
        pos_ref = packed_pos_ref;
14080
0
        p_ref = packed_p_ref;
14081
0
      } else {
14082
0
        ZEND_UNREACHABLE();
14083
0
      }
14084
14085
      // JIT: Z_FE_POS_P(array) = pos + 1;
14086
0
      ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14087
0
        ir_ADD_U32(pos_ref, ir_CONST_U32(1)));
14088
0
    }
14089
14090
0
    val_info = ((op1_info & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
14091
0
    if (val_info & MAY_BE_ARRAY) {
14092
0
      val_info |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
14093
0
    }
14094
0
    if (op1_info & MAY_BE_ARRAY_OF_REF) {
14095
0
      val_info |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY |
14096
0
        MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
14097
0
    } else if (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
14098
0
      val_info |= MAY_BE_RC1 | MAY_BE_RCN;
14099
0
    }
14100
14101
0
    val_addr = ZEND_ADDR_REF_ZVAL(p_ref);
14102
0
    if (opline->op2_type == IS_CV) {
14103
      // JIT: zend_assign_to_variable(variable_ptr, value, IS_CV, EX_USES_STRICT_TYPES());
14104
0
      if (!zend_jit_assign_to_variable(jit, opline, var_addr, var_addr, op2_info, -1, IS_CV, val_addr, val_info, 0, 0, true)) {
14105
0
        return 0;
14106
0
      }
14107
0
    } else {
14108
      // JIT: ZVAL_COPY(res, value);
14109
0
      jit_ZVAL_COPY(jit, var_addr, -1, val_addr, val_info, true);
14110
0
    }
14111
14112
0
    if (!exit_addr) {
14113
0
      zend_basic_block *bb;
14114
14115
0
      ZEND_ASSERT(jit->b >= 0);
14116
0
      bb = &jit->ssa->cfg.blocks[jit->b];
14117
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ir_END());
14118
0
      ZEND_ASSERT(exit_inputs);
14119
0
      if (!jit->ctx.ir_base[exit_inputs].op2) {
14120
0
        ref = exit_inputs;
14121
0
      } else {
14122
0
        ir_MERGE_list(exit_inputs);
14123
0
        ref = ir_END();
14124
0
      }
14125
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
14126
0
      jit->b = -1;
14127
0
    }
14128
0
  } else {
14129
0
    ZEND_ASSERT(exit_inputs);
14130
0
    ir_MERGE_list(exit_inputs);
14131
0
  }
14132
14133
0
  return 1;
14134
0
}
14135
14136
static int zend_jit_load_this(zend_jit_ctx *jit, uint32_t var)
14137
0
{
14138
0
  zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14139
0
  zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
14140
0
  ir_ref ref = jit_Z_PTR(jit, this_addr);
14141
14142
0
  jit_set_Z_PTR(jit, var_addr, ref);
14143
0
  jit_set_Z_TYPE_INFO(jit, var_addr, IS_OBJECT_EX);
14144
0
  jit_GC_ADDREF(jit, ref);
14145
14146
0
  return 1;
14147
0
}
14148
14149
static int zend_jit_fetch_this(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, bool check_only)
14150
0
{
14151
0
  if (!op_array->scope ||
14152
0
      (op_array->fn_flags & ZEND_ACC_STATIC) ||
14153
0
      ((op_array->fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_IMMUTABLE)) == ZEND_ACC_CLOSURE)) {
14154
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14155
0
      if (!JIT_G(current_frame) ||
14156
0
          !TRACE_FRAME_IS_THIS_CHECKED(JIT_G(current_frame))) {
14157
14158
0
        zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14159
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14160
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14161
14162
0
        if (!exit_addr) {
14163
0
          return 0;
14164
0
        }
14165
14166
0
        jit_guard_Z_TYPE(jit, this_addr, IS_OBJECT, exit_addr);
14167
14168
0
        if (JIT_G(current_frame)) {
14169
0
          TRACE_FRAME_SET_THIS_CHECKED(JIT_G(current_frame));
14170
0
        }
14171
0
      }
14172
0
    } else {
14173
0
      zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14174
0
      ir_ref if_object = jit_if_Z_TYPE(jit, this_addr, IS_OBJECT);
14175
14176
0
      ir_IF_FALSE_cold(if_object);
14177
0
      jit_SET_EX_OPLINE(jit, opline);
14178
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_invalid_this));
14179
14180
0
      ir_IF_TRUE(if_object);
14181
0
    }
14182
0
  }
14183
14184
0
  if (!check_only) {
14185
0
    if (!zend_jit_load_this(jit, opline->result.var)) {
14186
0
      return 0;
14187
0
    }
14188
0
  }
14189
14190
0
  return 1;
14191
0
}
14192
14193
static int zend_jit_class_guard(zend_jit_ctx *jit, const zend_op *opline, ir_ref obj_ref, zend_class_entry *ce)
14194
0
{
14195
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
14196
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14197
14198
0
  if (!exit_addr) {
14199
0
    return 0;
14200
0
  }
14201
14202
0
  ir_GUARD(ir_EQ(ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce))), ir_CONST_ADDR(ce)),
14203
0
    ir_CONST_ADDR(exit_addr));
14204
14205
0
  return 1;
14206
0
}
14207
14208
static int zend_jit_fetch_obj(zend_jit_ctx         *jit,
14209
                              const zend_op        *opline,
14210
                              const zend_op_array  *op_array,
14211
                              zend_ssa             *ssa,
14212
                              const zend_ssa_op    *ssa_op,
14213
                              uint32_t              op1_info,
14214
                              zend_jit_addr         op1_addr,
14215
                              bool                  op1_indirect,
14216
                              zend_class_entry     *ce,
14217
                              bool                  ce_is_instanceof,
14218
                              bool                  on_this,
14219
                              bool                  delayed_fetch_this,
14220
                              bool                  op1_avoid_refcounting,
14221
                              zend_class_entry     *trace_ce,
14222
                              zend_jit_addr         res_addr,
14223
                              uint8_t               prop_type,
14224
                              int                   may_throw)
14225
0
{
14226
0
  zval *member;
14227
0
  zend_property_info *prop_info;
14228
0
  bool may_be_dynamic = true;
14229
0
  zend_jit_addr prop_addr;
14230
0
  uint32_t res_info = RES_INFO();
14231
0
  ir_ref prop_type_ref = IR_UNUSED;
14232
0
  ir_ref obj_ref = IR_UNUSED;
14233
0
  ir_ref prop_ref = IR_UNUSED;
14234
0
  ir_ref end_inputs = IR_UNUSED;
14235
0
  ir_ref slow_inputs = IR_UNUSED;
14236
0
  ir_ref end_values = IR_UNUSED;
14237
14238
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
14239
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
14240
14241
0
  member = RT_CONSTANT(opline, opline->op2);
14242
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
14243
0
  prop_info = zend_get_known_property_info(op_array, ce, Z_STR_P(member), on_this, op_array->filename);
14244
14245
0
  if (on_this) {
14246
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14247
0
    obj_ref = jit_Z_PTR(jit, this_addr);
14248
0
  } else {
14249
0
    if (opline->op1_type == IS_VAR
14250
0
     && opline->opcode == ZEND_FETCH_OBJ_W
14251
0
     && (op1_info & MAY_BE_INDIRECT)
14252
0
     && Z_REG(op1_addr) == ZREG_FP) {
14253
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
14254
0
    }
14255
0
    if (op1_info & MAY_BE_REF) {
14256
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
14257
0
    }
14258
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
14259
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14260
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14261
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14262
14263
0
        if (!exit_addr) {
14264
0
          return 0;
14265
0
        }
14266
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
14267
0
      } else {
14268
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
14269
14270
0
        ir_IF_FALSE_cold(if_obj);
14271
0
        if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14272
0
          ir_ref op1_ref = IR_UNUSED;
14273
14274
0
          jit_SET_EX_OPLINE(jit, opline);
14275
0
          if (opline->opcode != ZEND_FETCH_OBJ_W && (op1_info & MAY_BE_UNDEF)) {
14276
0
            zend_jit_addr orig_op1_addr = OP1_ADDR();
14277
0
            ir_ref fast_path = IR_UNUSED;
14278
14279
0
            if (op1_info & MAY_BE_ANY) {
14280
0
              ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
14281
0
              ir_IF_TRUE(if_def);
14282
0
              fast_path = ir_END();
14283
0
              ir_IF_FALSE_cold(if_def);
14284
0
            }
14285
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
14286
0
              ir_CONST_U32(opline->op1.var));
14287
0
            if (fast_path) {
14288
0
              ir_MERGE_WITH(fast_path);
14289
0
            }
14290
0
            op1_ref = jit_ZVAL_ADDR(jit, orig_op1_addr);
14291
0
          } else {
14292
0
            op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
14293
0
          }
14294
0
          if (opline->opcode == ZEND_FETCH_OBJ_W) {
14295
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_write),
14296
0
              op1_ref, ir_CONST_ADDR(Z_STRVAL_P(member)));
14297
0
            jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14298
0
          } else {
14299
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_read),
14300
0
              op1_ref, ir_CONST_ADDR(Z_STRVAL_P(member)));
14301
0
            jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14302
0
          }
14303
0
        } else {
14304
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14305
0
        }
14306
0
        ir_END_list(end_inputs);
14307
14308
0
        ir_IF_TRUE(if_obj);
14309
0
      }
14310
0
    }
14311
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
14312
0
  }
14313
14314
0
  ZEND_ASSERT(obj_ref);
14315
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
14316
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, Z_STR_P(member), on_this, op_array->filename);
14317
0
    if (prop_info) {
14318
0
      ce = trace_ce;
14319
0
      ce_is_instanceof = false;
14320
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
14321
0
        if (on_this && JIT_G(current_frame)
14322
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
14323
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
14324
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
14325
0
          if (on_this && JIT_G(current_frame)) {
14326
0
            JIT_G(current_frame)->ce = ce;
14327
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
14328
0
          }
14329
0
        } else {
14330
0
          return 0;
14331
0
        }
14332
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
14333
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
14334
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
14335
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
14336
0
        }
14337
0
      }
14338
0
    }
14339
0
  }
14340
14341
0
  if (!prop_info) {
14342
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
14343
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
14344
0
    ir_ref if_same = ir_IF(ir_EQ(ref,
14345
0
      ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
14346
14347
0
    ir_IF_FALSE_cold(if_same);
14348
0
    ir_END_list(slow_inputs);
14349
14350
0
    ir_IF_TRUE(if_same);
14351
0
    ir_ref offset_ref = ir_LOAD_A(
14352
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
14353
14354
0
    may_be_dynamic = zend_may_be_dynamic_property(ce, Z_STR_P(member), opline->op1_type == IS_UNUSED, op_array);
14355
0
    if (may_be_dynamic) {
14356
0
      ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
14357
0
      if (opline->opcode == ZEND_FETCH_OBJ_W) {
14358
0
        ir_IF_TRUE_cold(if_dynamic);
14359
0
        ir_END_list(slow_inputs);
14360
0
      } else {
14361
0
        ir_IF_TRUE_cold(if_dynamic);
14362
0
        jit_SET_EX_OPLINE(jit, opline);
14363
14364
0
        if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14365
0
          if (Z_MODE(res_addr) == IS_REG) {
14366
0
            ir_ref val_addr = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_dynamic_ex),
14367
0
              obj_ref, offset_ref);
14368
0
            ir_END_PHI_list(end_values, val_addr);
14369
0
          } else {
14370
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_dynamic),
14371
0
              obj_ref, offset_ref);
14372
0
            ir_END_list(end_inputs);
14373
0
          }
14374
0
        } else {
14375
0
          if (Z_MODE(res_addr) == IS_REG) {
14376
0
            ir_ref val_addr = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_dynamic_ex),
14377
0
              obj_ref, offset_ref);
14378
0
            ir_END_PHI_list(end_values, val_addr);
14379
0
          } else {
14380
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_dynamic),
14381
0
              obj_ref, offset_ref);
14382
0
            ir_END_list(end_inputs);
14383
0
          }
14384
0
        }
14385
0
      }
14386
0
      ir_IF_FALSE(if_dynamic);
14387
0
    }
14388
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
14389
0
    prop_type_ref = jit_Z_TYPE_ref(jit, prop_ref);
14390
0
    ir_ref if_def = ir_IF(prop_type_ref);
14391
0
    ir_IF_FALSE_cold(if_def);
14392
0
    ir_END_list(slow_inputs);
14393
0
    ir_IF_TRUE(if_def);
14394
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14395
0
    if (opline->opcode == ZEND_FETCH_OBJ_W
14396
0
     && (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT)))) {
14397
0
      uint32_t flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
14398
14399
0
      ir_ref allowed_inputs = IR_UNUSED;
14400
0
      ir_ref forbidden_inputs = IR_UNUSED;
14401
14402
0
      ir_ref prop_info_ref = ir_LOAD_A(
14403
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
14404
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
14405
14406
0
      ir_IF_TRUE_cold(if_has_prop_info);
14407
14408
0
      ir_ref prop_flags = ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, flags)));
14409
0
      ir_ref if_readonly_or_avis = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)));
14410
14411
0
      ir_IF_FALSE(if_readonly_or_avis);
14412
0
      ir_END_list(allowed_inputs);
14413
14414
0
      ir_IF_TRUE_cold(if_readonly_or_avis);
14415
14416
0
      ir_ref if_readonly = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY)));
14417
0
      ir_IF_TRUE(if_readonly);
14418
0
      ir_END_list(forbidden_inputs);
14419
14420
0
      ir_IF_FALSE(if_readonly);
14421
0
      ir_ref has_avis_access = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_asymmetric_property_has_set_access), prop_info_ref);
14422
0
      ir_ref if_avis_access = ir_IF(has_avis_access);
14423
0
      ir_IF_TRUE(if_avis_access);
14424
0
      ir_END_list(allowed_inputs);
14425
14426
0
      ir_IF_FALSE(if_avis_access);
14427
0
      ir_END_list(forbidden_inputs);
14428
14429
0
      ir_MERGE_list(forbidden_inputs);
14430
14431
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14432
0
      ir_IF_TRUE(if_prop_obj);
14433
0
      ref = jit_Z_PTR(jit, prop_addr);
14434
0
      jit_GC_ADDREF(jit, ref);
14435
0
      jit_set_Z_PTR(jit, res_addr, ref);
14436
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14437
0
      ir_END_list(end_inputs);
14438
14439
0
      ir_IF_FALSE_cold(if_prop_obj);
14440
14441
0
      jit_SET_EX_OPLINE(jit, opline);
14442
0
      if_readonly = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY)));
14443
0
      ir_IF_TRUE(if_readonly);
14444
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_readonly_property_indirect_modification_error), prop_info_ref);
14445
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14446
0
      ir_END_list(end_inputs);
14447
14448
0
      ir_IF_FALSE(if_readonly);
14449
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_asymmetric_visibility_property_modification_error),
14450
0
        prop_info_ref, ir_CONST_ADDR("indirectly modify"));
14451
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14452
0
      ir_END_list(end_inputs);
14453
14454
0
      ir_MERGE_list(allowed_inputs);
14455
14456
0
      if (flags == ZEND_FETCH_DIM_WRITE) {
14457
0
        jit_SET_EX_OPLINE(jit, opline);
14458
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_array_promotion),
14459
0
          prop_ref, prop_info_ref);
14460
0
        ir_END_list(end_inputs);
14461
0
        ir_IF_FALSE(if_has_prop_info);
14462
0
      } else if (flags == ZEND_FETCH_REF) {
14463
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_create_typed_ref),
14464
0
          prop_ref,
14465
0
          prop_info_ref,
14466
0
          jit_ZVAL_ADDR(jit, res_addr));
14467
0
        ir_END_list(end_inputs);
14468
0
        ir_IF_FALSE(if_has_prop_info);
14469
0
      } else {
14470
0
        ZEND_ASSERT(flags == 0);
14471
0
        ir_MERGE_WITH_EMPTY_FALSE(if_has_prop_info);
14472
0
      }
14473
0
    }
14474
0
  } else {
14475
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
14476
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14477
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14478
0
      if (opline->opcode == ZEND_FETCH_OBJ_W || !(res_info & MAY_BE_GUARD) || !JIT_G(current_frame)) {
14479
        /* perform IS_UNDEF check only after result type guard (during deoptimization) */
14480
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14481
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14482
14483
0
        if (!exit_addr) {
14484
0
          return 0;
14485
0
        }
14486
0
        prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14487
0
        ir_GUARD(prop_type_ref, ir_CONST_ADDR(exit_addr));
14488
0
      }
14489
0
    } else {
14490
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14491
0
      ir_ref if_def = ir_IF(prop_type_ref);
14492
0
      ir_IF_FALSE_cold(if_def);
14493
0
      ir_END_list(slow_inputs);
14494
0
      ir_IF_TRUE(if_def);
14495
0
    }
14496
0
    if (opline->opcode == ZEND_FETCH_OBJ_W && (prop_info->flags & ZEND_ACC_READONLY)) {
14497
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14498
0
      ir_IF_TRUE(if_prop_obj);
14499
0
      ir_ref ref = jit_Z_PTR(jit, prop_addr);
14500
0
      jit_GC_ADDREF(jit, ref);
14501
0
      jit_set_Z_PTR(jit, res_addr, ref);
14502
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14503
0
      ir_END_list(end_inputs);
14504
14505
0
      ir_IF_FALSE_cold(if_prop_obj);
14506
0
      jit_SET_EX_OPLINE(jit, opline);
14507
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_readonly_property_indirect_modification_error), ir_CONST_ADDR(prop_info));
14508
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14509
0
      ir_END_list(end_inputs);
14510
14511
0
      goto result_fetched;
14512
0
    } else if (opline->opcode == ZEND_FETCH_OBJ_W && (prop_info->flags & ZEND_ACC_PPP_SET_MASK)) {
14513
      /* Readonly properties which are also asymmetric are never mutable indirectly, which is
14514
       * handled by the previous branch. */
14515
0
      ir_ref has_access = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_asymmetric_property_has_set_access), ir_CONST_ADDR(prop_info));
14516
14517
0
      ir_ref if_access = ir_IF(has_access);
14518
0
      ir_IF_FALSE_cold(if_access);
14519
14520
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14521
0
      ir_IF_TRUE(if_prop_obj);
14522
0
      ir_ref ref = jit_Z_PTR(jit, prop_addr);
14523
0
      jit_GC_ADDREF(jit, ref);
14524
0
      jit_set_Z_PTR(jit, res_addr, ref);
14525
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14526
0
      ir_END_list(end_inputs);
14527
14528
0
      ir_IF_FALSE_cold(if_prop_obj);
14529
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_asymmetric_visibility_property_modification_error),
14530
0
        ir_CONST_ADDR(prop_info), ir_CONST_ADDR("indirectly modify"));
14531
0
      ir_END_list(end_inputs);
14532
14533
0
      ir_IF_TRUE(if_access);
14534
0
    }
14535
14536
0
    if (opline->opcode == ZEND_FETCH_OBJ_W
14537
0
     && (opline->extended_value & ZEND_FETCH_OBJ_FLAGS)
14538
0
     && ZEND_TYPE_IS_SET(prop_info->type)) {
14539
0
      uint32_t flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
14540
14541
0
      if (flags == ZEND_FETCH_DIM_WRITE) {
14542
0
        if ((ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_ARRAY) == 0) {
14543
0
          if (!prop_type_ref) {
14544
0
            prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14545
0
          }
14546
0
          ir_ref if_null_or_flase = ir_IF(ir_LE(prop_type_ref, ir_CONST_U32(IR_FALSE)));
14547
0
          ir_IF_TRUE_cold(if_null_or_flase);
14548
0
          jit_SET_EX_OPLINE(jit, opline);
14549
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_array_promotion),
14550
0
            prop_ref, ir_CONST_ADDR(prop_info));
14551
0
          ir_END_list(end_inputs);
14552
0
          ir_IF_FALSE(if_null_or_flase);
14553
0
        }
14554
0
      } else if (flags == ZEND_FETCH_REF) {
14555
0
        ir_ref ref;
14556
14557
0
        if (!prop_type_ref) {
14558
0
          prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14559
0
        }
14560
14561
0
        ir_ref if_reference = ir_IF(ir_EQ(prop_type_ref, ir_CONST_U32(IS_REFERENCE_EX)));
14562
0
        ir_IF_FALSE(if_reference);
14563
0
        if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
14564
0
          ref = ir_CONST_ADDR(prop_info);
14565
0
        } else {
14566
0
          int prop_info_offset =
14567
0
            (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
14568
14569
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
14570
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
14571
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
14572
0
        }
14573
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_create_typed_ref),
14574
0
          prop_ref,
14575
0
          ref,
14576
0
          jit_ZVAL_ADDR(jit, res_addr));
14577
0
        ir_END_list(end_inputs);
14578
0
        ir_IF_TRUE(if_reference);
14579
0
      } else {
14580
0
        ZEND_UNREACHABLE();
14581
0
      }
14582
0
    }
14583
0
  }
14584
14585
0
  if (opline->opcode == ZEND_FETCH_OBJ_W) {
14586
0
    ZEND_ASSERT(prop_ref);
14587
0
    jit_set_Z_PTR(jit, res_addr, prop_ref);
14588
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
14589
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && prop_info) {
14590
0
      ssa->var_info[ssa_op->result_def].indirect_reference = 1;
14591
0
    }
14592
0
    ir_END_list(end_inputs);
14593
0
  } else {
14594
0
    if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame) && prop_info) {
14595
0
      ir_END_PHI_list(end_values, jit_ZVAL_ADDR(jit, prop_addr));
14596
0
    } else if ((res_info & MAY_BE_GUARD) && Z_MODE(res_addr) == IS_REG) {
14597
0
      ir_END_PHI_list(end_values, jit_ZVAL_ADDR(jit, prop_addr));
14598
0
    } else if (Z_MODE(res_addr) == IS_REG) {
14599
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14600
14601
0
      if (!zend_jit_zval_copy_deref_reg(jit, res_addr, res_info & ~MAY_BE_GUARD, prop_addr, prop_type_ref, &end_values)) {
14602
0
        return 0;
14603
0
      }
14604
0
    } else {
14605
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14606
14607
0
      if (!zend_jit_zval_copy_deref(jit, res_addr, prop_addr, prop_type_ref)) {
14608
0
        return 0;
14609
0
      }
14610
0
      ir_END_list(end_inputs);
14611
0
    }
14612
0
  }
14613
14614
0
result_fetched:
14615
0
  if (op1_avoid_refcounting) {
14616
0
    SET_STACK_REG(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
14617
0
  }
14618
14619
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE || !prop_info) {
14620
0
    ir_MERGE_list(slow_inputs);
14621
0
    jit_SET_EX_OPLINE(jit, opline);
14622
14623
0
    op1_info |= MAY_BE_RC1 | MAY_BE_RCN; /* object may be captured/released in magic handler */
14624
0
    if (opline->opcode == ZEND_FETCH_OBJ_W) {
14625
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_w_slow), obj_ref);
14626
0
      ir_END_list(end_inputs);
14627
0
    } else if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14628
0
      if (Z_MODE(res_addr) == IS_REG) {
14629
0
        ir_ref val_ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_slow_ex), obj_ref);
14630
0
        ir_END_PHI_list(end_values, val_ref);
14631
0
      } else {
14632
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_slow), obj_ref);
14633
0
        ir_END_list(end_inputs);
14634
0
      }
14635
0
    } else {
14636
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_slow), obj_ref);
14637
0
      ir_END_list(end_inputs);
14638
0
    }
14639
0
  }
14640
14641
0
  if (end_values) {
14642
0
    ir_ref val_ref = ir_PHI_list(end_values);
14643
0
    zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val_ref);
14644
0
    bool result_avoid_refcounting = false;
14645
14646
0
    ZEND_ASSERT(opline->opcode == ZEND_FETCH_OBJ_R
14647
0
      || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
14648
0
      || opline->opcode == ZEND_FETCH_OBJ_IS);
14649
0
    ZEND_ASSERT(end_inputs == IR_UNUSED);
14650
0
    if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
14651
0
      uint8_t type = concrete_type(res_info);
14652
0
      uint32_t flags = ZEND_JIT_EXIT_CHECK_EXCEPTION;
14653
14654
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
14655
0
       && !delayed_fetch_this
14656
0
       && !op1_avoid_refcounting) {
14657
0
        flags |= ZEND_JIT_EXIT_FREE_OP1;
14658
0
      }
14659
14660
0
      if ((opline->result_type & (IS_VAR|IS_TMP_VAR))
14661
0
       && !(flags & ZEND_JIT_EXIT_FREE_OP1)
14662
0
       && (res_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
14663
0
       && (ssa_op+1)->op1_use == ssa_op->result_def
14664
0
       && zend_jit_may_avoid_refcounting(opline+1, res_info)) {
14665
0
        result_avoid_refcounting = true;
14666
0
        ssa->var_info[ssa_op->result_def].avoid_refcounting = 1;
14667
0
      }
14668
14669
0
      val_addr = zend_jit_guard_fetch_result_type(jit, opline, val_addr, type,
14670
0
        true, flags, op1_avoid_refcounting);
14671
0
      if (!val_addr) {
14672
0
        return 0;
14673
0
      }
14674
14675
0
      res_info &= ~MAY_BE_GUARD;
14676
0
      ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
14677
0
    }
14678
14679
    // ZVAL_COPY
14680
0
    jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, !result_avoid_refcounting);
14681
14682
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
14683
0
      return 0;
14684
0
    }
14685
0
  } else {
14686
0
    ir_MERGE_list(end_inputs);
14687
0
  }
14688
14689
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
14690
0
    if (opline->op1_type == IS_VAR
14691
0
     && opline->opcode == ZEND_FETCH_OBJ_W
14692
0
     && (op1_info & MAY_BE_RC1)) {
14693
0
      zend_jit_addr orig_op1_addr = OP1_ADDR();
14694
0
      ir_ref if_refcounted, ptr, refcount, if_non_zero;
14695
0
      ir_ref merge_inputs = IR_UNUSED;
14696
14697
0
      if_refcounted = jit_if_REFCOUNTED(jit, orig_op1_addr);
14698
0
      ir_IF_FALSE( if_refcounted);
14699
0
      ir_END_list(merge_inputs);
14700
0
      ir_IF_TRUE( if_refcounted);
14701
0
      ptr = jit_Z_PTR(jit, orig_op1_addr);
14702
0
      refcount = jit_GC_DELREF(jit, ptr);
14703
0
      if_non_zero = ir_IF(refcount);
14704
0
      ir_IF_TRUE( if_non_zero);
14705
0
      ir_END_list(merge_inputs);
14706
0
      ir_IF_FALSE( if_non_zero);
14707
0
      jit_SET_EX_OPLINE(jit, opline);
14708
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_extract_helper), ptr);
14709
0
      ir_END_list(merge_inputs);
14710
0
      ir_MERGE_list(merge_inputs);
14711
0
    } else if (!op1_avoid_refcounting) {
14712
0
      if (on_this) {
14713
0
        op1_info &= ~MAY_BE_RC1;
14714
0
      }
14715
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
14716
0
    }
14717
0
  }
14718
14719
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
14720
0
   && prop_info
14721
0
   && (opline->opcode != ZEND_FETCH_OBJ_W ||
14722
0
       !(opline->extended_value & ZEND_FETCH_OBJ_FLAGS) ||
14723
0
       !ZEND_TYPE_IS_SET(prop_info->type))
14724
0
   && (!(opline->op1_type & (IS_VAR|IS_TMP_VAR)) || on_this || op1_indirect)) {
14725
0
    may_throw = 0;
14726
0
  }
14727
14728
0
  if (may_throw) {
14729
0
    if (Z_MODE(res_addr) == IS_REG) {
14730
0
      zend_jit_check_exception_undef_result(jit, opline);
14731
0
    } else {
14732
0
      zend_jit_check_exception(jit);
14733
0
    }
14734
0
  }
14735
14736
0
  return 1;
14737
0
}
14738
14739
static int zend_jit_assign_obj(zend_jit_ctx         *jit,
14740
                               const zend_op        *opline,
14741
                               const zend_op_array  *op_array,
14742
                               zend_ssa             *ssa,
14743
                               const zend_ssa_op    *ssa_op,
14744
                               uint32_t              op1_info,
14745
                               zend_jit_addr         op1_addr,
14746
                               uint32_t              val_info,
14747
                               zend_jit_addr         val_addr,
14748
                               zend_jit_addr         val_def_addr,
14749
                               zend_jit_addr         res_addr,
14750
                               bool                  op1_indirect,
14751
                               zend_class_entry     *ce,
14752
                               bool                  ce_is_instanceof,
14753
                               bool                  on_this,
14754
                               bool                  delayed_fetch_this,
14755
                               zend_class_entry     *trace_ce,
14756
                               uint8_t               prop_type,
14757
                               int                   may_throw)
14758
0
{
14759
0
  zval *member;
14760
0
  zend_string *name;
14761
0
  zend_property_info *prop_info;
14762
0
  zend_jit_addr prop_addr;
14763
0
  ir_ref obj_ref = IR_UNUSED;
14764
0
  ir_ref prop_ref = IR_UNUSED;
14765
0
  ir_ref delayed_end_input = IR_UNUSED;
14766
0
  ir_ref end_inputs = IR_UNUSED;
14767
0
  ir_ref slow_inputs = IR_UNUSED;
14768
0
  uint32_t res_info = RES_INFO();
14769
14770
0
  if (Z_MODE(val_addr) == IS_REG
14771
0
   && Z_LOAD(val_addr)
14772
0
   && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
14773
    /* Force load */
14774
0
    zend_jit_use_reg(jit, val_addr);
14775
0
  }
14776
14777
0
  if (val_addr != val_def_addr && val_def_addr) {
14778
0
    if (!zend_jit_update_regs(jit, (opline+1)->op1.var, val_addr, val_def_addr, val_info)) {
14779
0
      return 0;
14780
0
    }
14781
0
    if (Z_MODE(val_def_addr) == IS_REG && Z_MODE(val_addr) != IS_REG) {
14782
0
      val_addr = val_def_addr;
14783
0
    }
14784
0
  }
14785
14786
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
14787
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
14788
14789
0
  member = RT_CONSTANT(opline, opline->op2);
14790
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
14791
0
  name = Z_STR_P(member);
14792
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
14793
14794
0
  if (on_this) {
14795
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14796
0
    obj_ref = jit_Z_PTR(jit, this_addr);
14797
0
  } else {
14798
0
    if (opline->op1_type == IS_VAR
14799
0
     && (op1_info & MAY_BE_INDIRECT)
14800
0
     && Z_REG(op1_addr) == ZREG_FP) {
14801
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
14802
0
    }
14803
0
    if (op1_info & MAY_BE_REF) {
14804
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
14805
0
    }
14806
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
14807
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14808
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14809
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14810
14811
0
        if (!exit_addr) {
14812
0
          return 0;
14813
0
        }
14814
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
14815
0
      } else {
14816
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
14817
0
        ir_IF_FALSE_cold(if_obj);
14818
14819
0
        jit_SET_EX_OPLINE(jit, opline);
14820
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_assign),
14821
0
          jit_ZVAL_ADDR(jit, op1_addr),
14822
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
14823
14824
0
        if (RETURN_VALUE_USED(opline) && Z_MODE(res_addr) != IS_REG) {
14825
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14826
0
        }
14827
14828
0
        ir_END_list(end_inputs);
14829
14830
0
        ir_IF_TRUE(if_obj);
14831
0
      }
14832
0
    }
14833
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
14834
0
  }
14835
14836
0
  ZEND_ASSERT(obj_ref);
14837
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
14838
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
14839
0
    if (prop_info) {
14840
0
      ce = trace_ce;
14841
0
      ce_is_instanceof = false;
14842
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
14843
0
        if (on_this && JIT_G(current_frame)
14844
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
14845
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
14846
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
14847
0
          if (on_this && JIT_G(current_frame)) {
14848
0
            JIT_G(current_frame)->ce = ce;
14849
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
14850
0
          }
14851
0
        } else {
14852
0
          return 0;
14853
0
        }
14854
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
14855
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
14856
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
14857
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
14858
0
        }
14859
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
14860
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
14861
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
14862
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
14863
0
        }
14864
0
      }
14865
0
    }
14866
0
  }
14867
14868
0
  if (!prop_info) {
14869
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
14870
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
14871
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
14872
14873
0
    ir_IF_FALSE_cold(if_same);
14874
0
    ir_END_list(slow_inputs);
14875
14876
0
    ir_IF_TRUE(if_same);
14877
0
    ir_ref offset_ref = ir_LOAD_A(
14878
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
14879
14880
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
14881
0
    ir_IF_TRUE_cold(if_dynamic);
14882
0
    ir_END_list(slow_inputs);
14883
14884
0
    ir_IF_FALSE(if_dynamic);
14885
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
14886
0
    ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
14887
0
    ir_IF_FALSE_cold(if_def);
14888
0
    ir_END_list(slow_inputs);
14889
14890
0
    ir_IF_TRUE(if_def);
14891
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14892
14893
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
14894
0
      ir_ref arg3, arg4;
14895
0
      ir_ref prop_info_ref = ir_LOAD_A(
14896
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
14897
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
14898
0
      ir_IF_TRUE_cold(if_has_prop_info);
14899
14900
0
      if (Z_MODE(val_addr) == IS_REG) {
14901
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
14902
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
14903
0
          return 0;
14904
0
        }
14905
0
        arg3 = jit_ZVAL_ADDR(jit, real_addr);
14906
0
      } else {
14907
0
        arg3 = jit_ZVAL_ADDR(jit, val_addr);
14908
0
      }
14909
14910
0
      if (!RETURN_VALUE_USED(opline)) {
14911
0
        arg4 = IR_NULL;
14912
0
      } else if (Z_MODE(res_addr) == IS_REG) {
14913
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
14914
0
        arg4 = jit_ZVAL_ADDR(jit, real_addr);
14915
0
      } else {
14916
0
        arg4 = jit_ZVAL_ADDR(jit, res_addr);
14917
0
      }
14918
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
14919
0
      jit_SET_EX_OPLINE(jit, opline);
14920
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_to_typed_prop),
14921
0
        prop_ref,
14922
0
        prop_info_ref,
14923
0
        arg3,
14924
0
        arg4);
14925
14926
0
      if ((opline+1)->op1_type == IS_CONST) {
14927
        // TODO: ???
14928
        // if (Z_TYPE_P(value) == orig_type) {
14929
        // CACHE_PTR_EX(cache_slot + 2, NULL);
14930
0
      }
14931
14932
0
      ir_END_list(end_inputs);
14933
0
      ir_IF_FALSE(if_has_prop_info);
14934
0
    }
14935
0
  } else {
14936
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
14937
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14938
    /* With the exception of __clone(), readonly assignment always happens on IS_UNDEF, doding
14939
     * the fast path. Thus, the fast path is not useful. */
14940
0
    if (prop_info->flags & ZEND_ACC_READONLY) {
14941
0
      ZEND_ASSERT(slow_inputs == IR_UNUSED);
14942
0
      goto slow_path;
14943
0
    }
14944
14945
    // Undefined property with potential magic __get()/__set() or lazy object
14946
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && prop_type != IS_UNDEF) {
14947
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14948
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14949
14950
0
      if (!exit_addr) {
14951
0
        return 0;
14952
0
      }
14953
0
      ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
14954
0
    } else {
14955
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
14956
0
      ir_IF_FALSE_cold(if_def);
14957
0
      ir_END_list(slow_inputs);
14958
0
      ir_IF_TRUE(if_def);
14959
0
    }
14960
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
14961
0
      ir_ref ref, arg3, arg4;
14962
14963
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
14964
0
      jit_SET_EX_OPLINE(jit, opline);
14965
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
14966
0
        ref = ir_CONST_ADDR(prop_info);
14967
0
      } else {
14968
0
        int prop_info_offset =
14969
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
14970
14971
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
14972
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
14973
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
14974
0
      }
14975
0
      if (Z_MODE(val_addr) == IS_REG) {
14976
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
14977
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
14978
0
          return 0;
14979
0
        }
14980
0
        arg3 = jit_ZVAL_ADDR(jit, real_addr);
14981
0
      } else {
14982
0
        arg3 = jit_ZVAL_ADDR(jit, val_addr);
14983
0
      }
14984
0
      if (!RETURN_VALUE_USED(opline)) {
14985
0
        arg4 = IR_NULL;
14986
0
      } else if (Z_MODE(res_addr) == IS_REG) {
14987
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
14988
0
        arg4 = jit_ZVAL_ADDR(jit, real_addr);
14989
0
      } else {
14990
0
        arg4 = jit_ZVAL_ADDR(jit, res_addr);
14991
0
      }
14992
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_to_typed_prop),
14993
0
        prop_ref,
14994
0
        ref,
14995
0
        arg3,
14996
0
        arg4);
14997
14998
0
      ir_END_list(end_inputs);
14999
0
    }
15000
0
  }
15001
15002
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15003
0
    if (Z_MODE(val_addr) != IS_REG
15004
0
     && (res_addr == 0 || Z_MODE(res_addr) != IS_REG)
15005
0
     && opline->result_type == IS_UNUSED) {
15006
0
      if (!zend_jit_assign_to_variable_call(jit, opline, prop_addr, prop_addr, -1, -1, (opline+1)->op1_type, val_addr, val_info, res_addr, false)) {
15007
0
        return 0;
15008
0
      }
15009
0
    } else {
15010
0
      zend_jit_addr real_res_addr;
15011
15012
0
      if (res_addr && Z_MODE(res_addr) == IS_REG) {
15013
0
        real_res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15014
0
      } else {
15015
0
        real_res_addr = res_addr;
15016
0
      }
15017
0
      if (!zend_jit_assign_to_variable(jit, opline, prop_addr, prop_addr, -1, -1, (opline+1)->op1_type, val_addr, val_info, real_res_addr, 0, false)) {
15018
0
        return 0;
15019
0
      }
15020
0
    }
15021
0
    if (end_inputs || slow_inputs) {
15022
0
      if (((opline+1)->op1_type & (IS_VAR|IS_TMP_VAR))
15023
0
       && (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15024
        /* skip FREE_OP_DATA() */
15025
0
        delayed_end_input = ir_END();
15026
0
      } else {
15027
0
        ir_END_list(end_inputs);
15028
0
      }
15029
0
    }
15030
0
  }
15031
15032
0
  if (slow_inputs) {
15033
0
    ir_ref arg3, arg5;
15034
15035
0
    ir_MERGE_list(slow_inputs);
15036
15037
0
slow_path:
15038
0
    jit_SET_EX_OPLINE(jit, opline);
15039
15040
0
    if (Z_MODE(val_addr) == IS_REG) {
15041
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15042
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15043
0
        return 0;
15044
0
      }
15045
0
      arg3 = jit_ZVAL_ADDR(jit, real_addr);
15046
0
    } else {
15047
0
      arg3 = jit_ZVAL_ADDR(jit, val_addr);
15048
0
    }
15049
0
    if (!RETURN_VALUE_USED(opline)) {
15050
0
      arg5 = IR_NULL;
15051
0
    } else if (Z_MODE(res_addr) == IS_REG) {
15052
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15053
0
      arg5 = jit_ZVAL_ADDR(jit, real_addr);
15054
0
    } else {
15055
0
      arg5 = jit_ZVAL_ADDR(jit, res_addr);
15056
0
    }
15057
15058
    // JIT: value = zobj->handlers->write_property(zobj, name, value, CACHE_ADDR(opline->extended_value));
15059
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15060
0
    ir_CALL_5(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_obj_helper),
15061
0
      obj_ref,
15062
0
      ir_CONST_ADDR(name),
15063
0
      arg3,
15064
0
      ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15065
0
      arg5);
15066
15067
0
    ir_END_list(end_inputs);
15068
0
  }
15069
15070
0
  if (end_inputs) {
15071
0
    ir_MERGE_list(end_inputs);
15072
15073
0
    if (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15074
0
      val_info |= MAY_BE_RC1|MAY_BE_RCN;
15075
0
    }
15076
0
    jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, opline);
15077
15078
0
    if (delayed_end_input) {
15079
0
      ir_MERGE_WITH(delayed_end_input);
15080
0
    }
15081
0
  }
15082
15083
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
15084
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
15085
0
  }
15086
15087
0
  if (RETURN_VALUE_USED(opline) && Z_MODE(res_addr) == IS_REG) {
15088
0
    zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15089
0
    if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
15090
0
      return 0;
15091
0
    }
15092
0
  }
15093
15094
0
  if (may_throw) {
15095
0
    zend_jit_check_exception(jit);
15096
0
  }
15097
15098
0
  return 1;
15099
0
}
15100
15101
static int zend_jit_assign_obj_op(zend_jit_ctx         *jit,
15102
                                  const zend_op        *opline,
15103
                                  const zend_op_array  *op_array,
15104
                                  zend_ssa             *ssa,
15105
                                  const zend_ssa_op    *ssa_op,
15106
                                  uint32_t              op1_info,
15107
                                  zend_jit_addr         op1_addr,
15108
                                  uint32_t              val_info,
15109
                                  zend_jit_addr         val_addr,
15110
                                  zend_ssa_range       *val_range,
15111
                                  bool                  op1_indirect,
15112
                                  zend_class_entry     *ce,
15113
                                  bool                  ce_is_instanceof,
15114
                                  bool                  on_this,
15115
                                  bool                  delayed_fetch_this,
15116
                                  zend_class_entry     *trace_ce,
15117
                                  uint8_t               prop_type)
15118
0
{
15119
0
  zval *member;
15120
0
  zend_string *name;
15121
0
  zend_property_info *prop_info;
15122
0
  zend_jit_addr prop_addr;
15123
0
  bool use_prop_guard = false;
15124
0
  bool may_throw = false;
15125
0
  binary_op_type binary_op = get_binary_op(opline->extended_value);
15126
0
  ir_ref obj_ref = IR_UNUSED;
15127
0
  ir_ref prop_ref = IR_UNUSED;
15128
0
  ir_ref end_inputs = IR_UNUSED;
15129
0
  ir_ref slow_inputs = IR_UNUSED;
15130
15131
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
15132
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
15133
0
  ZEND_ASSERT(opline->result_type == IS_UNUSED);
15134
15135
0
  member = RT_CONSTANT(opline, opline->op2);
15136
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
15137
0
  name = Z_STR_P(member);
15138
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
15139
15140
0
  if (on_this) {
15141
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
15142
0
    obj_ref = jit_Z_PTR(jit, this_addr);
15143
0
  } else {
15144
0
    if (opline->op1_type == IS_VAR
15145
0
     && (op1_info & MAY_BE_INDIRECT)
15146
0
     && Z_REG(op1_addr) == ZREG_FP) {
15147
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
15148
0
    }
15149
0
    if (op1_info & MAY_BE_REF) {
15150
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
15151
0
    }
15152
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
15153
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15154
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15155
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15156
15157
0
        if (!exit_addr) {
15158
0
          return 0;
15159
0
        }
15160
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
15161
0
      } else {
15162
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
15163
0
        ir_IF_FALSE_cold(if_obj);
15164
15165
0
        jit_SET_EX_OPLINE(jit, opline);
15166
0
        ir_CALL_2(IR_VOID,
15167
0
          (op1_info & MAY_BE_UNDEF) ?
15168
0
            ir_CONST_FC_FUNC(zend_jit_invalid_property_assign_op) :
15169
0
            ir_CONST_FC_FUNC(zend_jit_invalid_property_assign),
15170
0
          jit_ZVAL_ADDR(jit, op1_addr),
15171
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
15172
15173
0
        may_throw = true;
15174
15175
0
        ir_END_list(end_inputs);
15176
0
        ir_IF_TRUE(if_obj);
15177
0
      }
15178
0
    }
15179
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
15180
0
  }
15181
15182
0
  ZEND_ASSERT(obj_ref);
15183
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
15184
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
15185
0
    if (prop_info) {
15186
0
      ce = trace_ce;
15187
0
      ce_is_instanceof = false;
15188
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
15189
0
        if (on_this && JIT_G(current_frame)
15190
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
15191
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
15192
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
15193
0
          if (on_this && JIT_G(current_frame)) {
15194
0
            JIT_G(current_frame)->ce = ce;
15195
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
15196
0
          }
15197
0
        } else {
15198
0
          return 0;
15199
0
        }
15200
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
15201
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
15202
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
15203
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
15204
0
        }
15205
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
15206
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
15207
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
15208
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
15209
0
        }
15210
0
      }
15211
0
    }
15212
0
  }
15213
15214
0
  use_prop_guard = (prop_type != IS_UNKNOWN
15215
0
    && prop_type != IS_UNDEF
15216
0
    && prop_type != IS_REFERENCE
15217
0
    && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_OBJECT);
15218
15219
0
  if (Z_MODE(val_addr) == IS_REG
15220
0
   && Z_LOAD(val_addr)
15221
0
   && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
15222
    /* Force load */
15223
0
    zend_jit_use_reg(jit, val_addr);
15224
0
  }
15225
15226
0
  if (!prop_info) {
15227
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15228
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, (opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
15229
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
15230
15231
0
    ir_IF_FALSE_cold(if_same);
15232
0
    ir_END_list(slow_inputs);
15233
15234
0
    ir_IF_TRUE(if_same);
15235
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
15236
0
      ir_ref prop_info_ref = ir_LOAD_A(
15237
0
        ir_ADD_OFFSET(run_time_cache, ((opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
15238
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
15239
0
      ir_IF_TRUE_cold(if_has_prop_info);
15240
0
      ir_END_list(slow_inputs);
15241
15242
0
      ir_IF_FALSE(if_has_prop_info);
15243
0
    }
15244
0
    ir_ref offset_ref = ir_LOAD_A(
15245
0
      ir_ADD_OFFSET(run_time_cache, ((opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
15246
15247
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
15248
0
    ir_IF_TRUE_cold(if_dynamic);
15249
0
    ir_END_list(slow_inputs);
15250
15251
0
    ir_IF_FALSE(if_dynamic);
15252
15253
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
15254
0
    if (!use_prop_guard) {
15255
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
15256
0
      ir_IF_FALSE_cold(if_def);
15257
0
      ir_END_list(slow_inputs);
15258
15259
0
      ir_IF_TRUE(if_def);
15260
0
    }
15261
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15262
0
  } else {
15263
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
15264
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15265
15266
0
    if (ZEND_TYPE_IS_SET(prop_info->type) || !use_prop_guard) {
15267
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15268
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15269
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15270
15271
0
        if (!exit_addr) {
15272
0
          return 0;
15273
0
        }
15274
0
        ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15275
0
      } else {
15276
0
        ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15277
0
        ir_IF_FALSE_cold(if_def);
15278
0
        ir_END_list(slow_inputs);
15279
0
        ir_IF_TRUE(if_def);
15280
0
      }
15281
0
    }
15282
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15283
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref, arg2;
15284
15285
0
      may_throw = true;
15286
15287
0
      jit_SET_EX_OPLINE(jit, opline);
15288
15289
0
      if (Z_MODE(val_addr) == IS_REG) {
15290
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15291
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15292
0
          return 0;
15293
0
        }
15294
0
        arg2 = jit_ZVAL_ADDR(jit, real_addr);
15295
0
      } else {
15296
0
        arg2 = jit_ZVAL_ADDR(jit, val_addr);
15297
0
      }
15298
15299
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15300
0
      ir_IF_FALSE(if_ref);
15301
0
      noref_path = ir_END();
15302
0
      ir_IF_TRUE(if_ref);
15303
15304
0
      reference = jit_Z_PTR(jit, prop_addr);
15305
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15306
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15307
0
      ir_IF_FALSE(if_typed);
15308
0
      ref_path = ir_END();
15309
0
      ir_IF_TRUE_cold(if_typed);
15310
15311
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
15312
0
        reference,
15313
0
        arg2,
15314
0
        ir_CONST_FC_FUNC(binary_op));
15315
15316
0
      ir_END_list(end_inputs);
15317
15318
0
      ir_MERGE_2(noref_path, ref_path);
15319
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15320
0
      prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15321
15322
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
15323
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15324
0
        ref = ir_CONST_ADDR(prop_info);
15325
0
      } else {
15326
0
        int prop_info_offset =
15327
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15328
15329
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15330
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15331
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15332
0
      }
15333
15334
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_prop),
15335
0
        prop_ref,
15336
0
        ref,
15337
0
        arg2,
15338
0
        ir_CONST_FC_FUNC(binary_op));
15339
15340
0
      ir_END_list(end_inputs);
15341
0
    }
15342
0
  }
15343
15344
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15345
0
    zend_jit_addr var_addr = prop_addr;
15346
0
    uint32_t var_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15347
0
    uint32_t var_def_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15348
15349
0
    if (use_prop_guard) {
15350
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
15351
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15352
0
      if (!exit_addr) {
15353
0
        return 0;
15354
0
      }
15355
15356
0
      jit_guard_Z_TYPE(jit, prop_addr, prop_type, exit_addr);
15357
0
      var_info = (1 << prop_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
15358
0
    }
15359
15360
0
    if (var_info & MAY_BE_REF) {
15361
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref, arg2;
15362
15363
0
      may_throw = true;
15364
15365
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15366
0
      ir_IF_FALSE(if_ref);
15367
0
      noref_path = ir_END();
15368
0
      ir_IF_TRUE(if_ref);
15369
15370
0
      reference = jit_Z_PTR(jit, var_addr);
15371
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15372
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15373
0
      ir_IF_FALSE(if_typed);
15374
0
      ref_path = ir_END();
15375
0
      ir_IF_TRUE_cold(if_typed);
15376
15377
0
      jit_SET_EX_OPLINE(jit, opline);
15378
15379
0
      if (Z_MODE(val_addr) == IS_REG) {
15380
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15381
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15382
0
          return 0;
15383
0
        }
15384
0
        arg2 = jit_ZVAL_ADDR(jit, real_addr);
15385
0
      } else {
15386
0
        arg2 = jit_ZVAL_ADDR(jit, val_addr);
15387
0
      }
15388
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
15389
0
        reference,
15390
0
        arg2,
15391
0
        ir_CONST_FC_FUNC(binary_op));
15392
15393
0
      ir_END_list(end_inputs);
15394
15395
0
      ir_MERGE_2(noref_path, ref_path);
15396
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15397
0
      var_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15398
15399
0
      var_info &= ~MAY_BE_REF;
15400
0
    }
15401
15402
0
    uint8_t val_op_type = (opline+1)->op1_type;
15403
0
    if (val_op_type & (IS_TMP_VAR|IS_VAR)) {
15404
      /* prevent FREE_OP in the helpers */
15405
0
      val_op_type = IS_CV;
15406
0
    }
15407
15408
0
    switch (opline->extended_value) {
15409
0
      case ZEND_ADD:
15410
0
      case ZEND_SUB:
15411
0
      case ZEND_MUL:
15412
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15413
0
            (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15414
0
          if (opline->extended_value != ZEND_ADD ||
15415
0
              (var_info & MAY_BE_ANY) != MAY_BE_ARRAY ||
15416
0
              (val_info & MAY_BE_ANY) == MAY_BE_ARRAY) {
15417
0
            may_throw = true;
15418
0
          }
15419
0
        }
15420
0
        if (!zend_jit_math_helper(jit, opline, opline->extended_value, IS_CV, opline->op1, var_addr, var_info, val_op_type, (opline+1)->op1, val_addr, val_info, 0, var_addr, var_def_info, var_info,
15421
0
            1 /* may overflow */, 0)) {
15422
0
          return 0;
15423
0
        }
15424
0
        break;
15425
0
      case ZEND_BW_OR:
15426
0
      case ZEND_BW_AND:
15427
0
      case ZEND_BW_XOR:
15428
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15429
0
            (val_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15430
0
          if ((var_info & MAY_BE_ANY) != MAY_BE_STRING ||
15431
0
              (val_info & MAY_BE_ANY) != MAY_BE_STRING) {
15432
0
            may_throw = true;
15433
0
          }
15434
0
        }
15435
0
        goto long_math;
15436
0
      case ZEND_SL:
15437
0
      case ZEND_SR:
15438
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15439
0
            (val_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15440
0
          may_throw = true;
15441
0
        }
15442
0
        if (val_op_type != IS_CONST ||
15443
0
            Z_TYPE_P(RT_CONSTANT((opline+1), (opline+1)->op1)) != IS_LONG ||
15444
0
            Z_LVAL_P(RT_CONSTANT((opline+1), (opline+1)->op1)) < 0) {
15445
0
          may_throw = true;
15446
0
        }
15447
0
        goto long_math;
15448
0
      case ZEND_MOD:
15449
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15450
0
            (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15451
0
          may_throw = true;
15452
0
        }
15453
0
        if (val_op_type != IS_CONST ||
15454
0
            Z_TYPE_P(RT_CONSTANT((opline+1), (opline+1)->op1)) != IS_LONG ||
15455
0
            Z_LVAL_P(RT_CONSTANT((opline+1), (opline+1)->op1)) == 0) {
15456
0
          may_throw = true;
15457
0
        }
15458
0
long_math:
15459
0
        if (!zend_jit_long_math_helper(jit, opline, opline->extended_value,
15460
0
            IS_CV, opline->op1, var_addr, var_info, NULL,
15461
0
            val_op_type, (opline+1)->op1, val_addr, val_info,
15462
0
            val_range,
15463
0
            0, var_addr, var_def_info, var_info, /* may throw */ 1)) {
15464
0
          return 0;
15465
0
        }
15466
0
        break;
15467
0
      case ZEND_CONCAT:
15468
0
        may_throw = true;
15469
0
        if (!zend_jit_concat_helper(jit, opline, IS_CV, opline->op1, var_addr, var_info, val_op_type, (opline+1)->op1, val_addr, val_info, var_addr,
15470
0
            0)) {
15471
0
          return 0;
15472
0
        }
15473
0
        break;
15474
0
      default:
15475
0
        ZEND_UNREACHABLE();
15476
0
    }
15477
0
    if (end_inputs || slow_inputs) {
15478
0
      ir_END_list(end_inputs);
15479
0
    }
15480
0
  }
15481
15482
0
  if (slow_inputs) {
15483
0
    ir_ref arg3;
15484
15485
0
    ir_MERGE_list(slow_inputs);
15486
15487
0
    may_throw = true;
15488
15489
0
    if (Z_MODE(val_addr) == IS_REG) {
15490
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15491
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15492
0
        return 0;
15493
0
      }
15494
0
      arg3 = jit_ZVAL_ADDR(jit, real_addr);
15495
0
    } else {
15496
0
      arg3 = jit_ZVAL_ADDR(jit, val_addr);
15497
0
    }
15498
0
    jit_SET_EX_OPLINE(jit, opline);
15499
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15500
0
    ir_CALL_5(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_obj_op_helper),
15501
0
      obj_ref,
15502
0
      ir_CONST_ADDR(name),
15503
0
      arg3,
15504
0
      ir_ADD_OFFSET(run_time_cache, (opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15505
0
      ir_CONST_FC_FUNC(binary_op));
15506
15507
0
    ir_END_list(end_inputs);
15508
0
  }
15509
15510
0
  if (end_inputs) {
15511
0
    ir_MERGE_list(end_inputs);
15512
0
  }
15513
15514
0
  if (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15515
0
    val_info |= MAY_BE_RC1|MAY_BE_RCN;
15516
0
  }
15517
15518
  // JIT: FREE_OP_DATA();
15519
0
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, opline);
15520
15521
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
15522
0
    if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
15523
0
      may_throw = true;
15524
0
    }
15525
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
15526
0
  }
15527
15528
0
  if (may_throw) {
15529
0
    zend_jit_check_exception(jit);
15530
0
  }
15531
15532
0
  return 1;
15533
0
}
15534
15535
static int zend_jit_incdec_obj(zend_jit_ctx         *jit,
15536
                               const zend_op        *opline,
15537
                               const zend_op_array  *op_array,
15538
                               zend_ssa             *ssa,
15539
                               const zend_ssa_op    *ssa_op,
15540
                               uint32_t              op1_info,
15541
                               zend_jit_addr         op1_addr,
15542
                               bool                  op1_indirect,
15543
                               zend_class_entry     *ce,
15544
                               bool                  ce_is_instanceof,
15545
                               bool                  on_this,
15546
                               bool                  delayed_fetch_this,
15547
                               zend_class_entry     *trace_ce,
15548
                               uint8_t               prop_type)
15549
0
{
15550
0
  zval *member;
15551
0
  zend_string *name;
15552
0
  zend_property_info *prop_info;
15553
0
  zend_jit_addr res_addr = 0;
15554
0
  zend_jit_addr prop_addr;
15555
0
  bool use_prop_guard = false;
15556
0
  bool may_throw = false;
15557
0
  uint32_t res_info = (opline->result_type != IS_UNDEF) ? RES_INFO() : 0;
15558
0
  ir_ref obj_ref = IR_UNUSED;
15559
0
  ir_ref prop_ref = IR_UNUSED;
15560
0
  ir_ref end_inputs = IR_UNUSED;
15561
0
  ir_ref slow_inputs = IR_UNUSED;
15562
15563
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
15564
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
15565
15566
0
  if (opline->result_type != IS_UNUSED) {
15567
0
    res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15568
0
  }
15569
15570
0
  member = RT_CONSTANT(opline, opline->op2);
15571
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
15572
0
  name = Z_STR_P(member);
15573
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
15574
15575
0
  if (on_this) {
15576
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
15577
0
    obj_ref = jit_Z_PTR(jit, this_addr);
15578
0
  } else {
15579
0
    if (opline->op1_type == IS_VAR
15580
0
     && (op1_info & MAY_BE_INDIRECT)
15581
0
     && Z_REG(op1_addr) == ZREG_FP) {
15582
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
15583
0
    }
15584
0
    if (op1_info & MAY_BE_REF) {
15585
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
15586
0
    }
15587
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
15588
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15589
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15590
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15591
15592
0
        if (!exit_addr) {
15593
0
          return 0;
15594
0
        }
15595
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
15596
0
      } else {
15597
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
15598
0
        ir_IF_FALSE_cold(if_obj);
15599
15600
0
        jit_SET_EX_OPLINE(jit, opline);
15601
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_incdec),
15602
0
          jit_ZVAL_ADDR(jit, op1_addr),
15603
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
15604
15605
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
15606
0
        ir_IF_TRUE(if_obj);
15607
0
      }
15608
0
    }
15609
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
15610
0
  }
15611
15612
0
  ZEND_ASSERT(obj_ref);
15613
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
15614
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
15615
0
    if (prop_info) {
15616
0
      ce = trace_ce;
15617
0
      ce_is_instanceof = false;
15618
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
15619
0
        if (on_this && JIT_G(current_frame)
15620
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
15621
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
15622
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
15623
0
          if (on_this && JIT_G(current_frame)) {
15624
0
            JIT_G(current_frame)->ce = ce;
15625
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
15626
0
          }
15627
0
        } else {
15628
0
          return 0;
15629
0
        }
15630
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
15631
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
15632
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
15633
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
15634
0
        }
15635
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
15636
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
15637
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
15638
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
15639
0
        }
15640
0
      }
15641
0
    }
15642
0
  }
15643
15644
0
  use_prop_guard = (prop_type != IS_UNKNOWN
15645
0
    && prop_type != IS_UNDEF
15646
0
    && prop_type != IS_REFERENCE
15647
0
    && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_OBJECT);
15648
15649
0
  if (!prop_info) {
15650
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15651
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
15652
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
15653
15654
0
    ir_IF_FALSE_cold(if_same);
15655
0
    ir_END_list(slow_inputs);
15656
15657
0
    ir_IF_TRUE(if_same);
15658
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
15659
0
      ir_ref prop_info_ref = ir_LOAD_A(
15660
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
15661
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
15662
0
      ir_IF_TRUE_cold(if_has_prop_info);
15663
0
      ir_END_list(slow_inputs);
15664
15665
0
      ir_IF_FALSE(if_has_prop_info);
15666
0
    }
15667
0
    ir_ref offset_ref = ir_LOAD_A(
15668
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
15669
15670
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
15671
0
    ir_IF_TRUE_cold(if_dynamic);
15672
0
    ir_END_list(slow_inputs);
15673
15674
0
    ir_IF_FALSE(if_dynamic);
15675
15676
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
15677
0
    if (!use_prop_guard) {
15678
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
15679
0
      ir_IF_FALSE_cold(if_def);
15680
0
      ir_END_list(slow_inputs);
15681
15682
0
      ir_IF_TRUE(if_def);
15683
0
    }
15684
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15685
0
  } else {
15686
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
15687
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15688
15689
0
    if (ZEND_TYPE_IS_SET(prop_info->type) || !use_prop_guard) {
15690
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15691
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15692
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15693
15694
0
        if (!exit_addr) {
15695
0
          return 0;
15696
0
        }
15697
0
        ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15698
0
      } else {
15699
0
        ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15700
0
        ir_IF_FALSE_cold(if_def);
15701
0
        ir_END_list(slow_inputs);
15702
0
        ir_IF_TRUE(if_def);
15703
0
      }
15704
0
    }
15705
15706
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15707
0
      const void *func;
15708
0
      ir_ref ref;
15709
15710
0
      may_throw = true;
15711
0
      jit_SET_EX_OPLINE(jit, opline);
15712
15713
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15714
0
        ref = ir_CONST_ADDR(prop_info);
15715
0
      } else {
15716
0
        int prop_info_offset =
15717
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15718
15719
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15720
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15721
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15722
0
      }
15723
15724
0
      if (opline->result_type == IS_UNUSED) {
15725
0
        switch (opline->opcode) {
15726
0
          case ZEND_PRE_INC_OBJ:
15727
0
          case ZEND_POST_INC_OBJ:
15728
0
            func = zend_jit_inc_typed_prop;
15729
0
            break;
15730
0
          case ZEND_PRE_DEC_OBJ:
15731
0
          case ZEND_POST_DEC_OBJ:
15732
0
            func = zend_jit_dec_typed_prop;
15733
0
            break;
15734
0
          default:
15735
0
            ZEND_UNREACHABLE();
15736
0
        }
15737
15738
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(func), prop_ref, ref);
15739
0
      } else {
15740
0
        switch (opline->opcode) {
15741
0
          case ZEND_PRE_INC_OBJ:
15742
0
            func = zend_jit_pre_inc_typed_prop;
15743
0
            break;
15744
0
          case ZEND_PRE_DEC_OBJ:
15745
0
            func = zend_jit_pre_dec_typed_prop;
15746
0
            break;
15747
0
          case ZEND_POST_INC_OBJ:
15748
0
            func = zend_jit_post_inc_typed_prop;
15749
0
            break;
15750
0
          case ZEND_POST_DEC_OBJ:
15751
0
            func = zend_jit_post_dec_typed_prop;
15752
0
            break;
15753
0
          default:
15754
0
            ZEND_UNREACHABLE();
15755
0
        }
15756
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(func),
15757
0
          prop_ref,
15758
0
          ref,
15759
0
          jit_ZVAL_ADDR(jit, res_addr));
15760
0
      }
15761
0
      ir_END_list(end_inputs);
15762
0
    }
15763
0
  }
15764
15765
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15766
0
    uint32_t var_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15767
0
    zend_jit_addr var_addr = prop_addr;
15768
0
    ir_ref if_long = IR_UNUSED;
15769
0
    ir_ref if_overflow = IR_UNUSED;
15770
15771
0
    if (use_prop_guard) {
15772
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
15773
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15774
0
      if (!exit_addr) {
15775
0
        return 0;
15776
0
      }
15777
15778
0
      jit_guard_Z_TYPE(jit, prop_addr, prop_type, exit_addr);
15779
0
      var_info = (1 << prop_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
15780
0
    }
15781
15782
0
    if (var_info & MAY_BE_REF) {
15783
0
      const void *func;
15784
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref;
15785
15786
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15787
0
      ir_IF_FALSE(if_ref);
15788
0
      noref_path = ir_END();
15789
0
      ir_IF_TRUE(if_ref);
15790
15791
0
      reference = jit_Z_PTR(jit, var_addr);
15792
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15793
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15794
0
      ir_IF_FALSE(if_typed);
15795
0
      ref_path = ir_END();
15796
0
      ir_IF_TRUE_cold(if_typed);
15797
15798
0
      switch (opline->opcode) {
15799
0
        case ZEND_PRE_INC_OBJ:
15800
0
          func = zend_jit_pre_inc_typed_ref;
15801
0
          break;
15802
0
        case ZEND_PRE_DEC_OBJ:
15803
0
          func = zend_jit_pre_dec_typed_ref;
15804
0
          break;
15805
0
        case ZEND_POST_INC_OBJ:
15806
0
          func = zend_jit_post_inc_typed_ref;
15807
0
          break;
15808
0
        case ZEND_POST_DEC_OBJ:
15809
0
          func = zend_jit_post_dec_typed_ref;
15810
0
          break;
15811
0
        default:
15812
0
          ZEND_UNREACHABLE();
15813
0
      }
15814
15815
0
      may_throw = true;
15816
0
      jit_SET_EX_OPLINE(jit, opline);
15817
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(func),
15818
0
        reference,
15819
0
        (opline->result_type == IS_UNUSED) ? IR_NULL : jit_ZVAL_ADDR(jit, res_addr));
15820
15821
0
      ir_END_list(end_inputs);
15822
15823
0
      ir_MERGE_2(noref_path, ref_path);
15824
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15825
0
      var_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15826
15827
0
      var_info &= ~MAY_BE_REF;
15828
0
    }
15829
15830
0
    if (var_info & MAY_BE_LONG) {
15831
0
      ir_ref addr, ref;
15832
15833
0
      if (var_info & (MAY_BE_ANY - MAY_BE_LONG)) {
15834
0
        if_long = jit_if_Z_TYPE(jit, var_addr, IS_LONG);
15835
0
        ir_IF_TRUE(if_long);
15836
0
      }
15837
15838
0
      addr = jit_ZVAL_ADDR(jit, var_addr);
15839
0
      ref = ir_LOAD_L(addr);
15840
0
      if (opline->opcode == ZEND_POST_INC_OBJ || opline->opcode == ZEND_POST_DEC_OBJ) {
15841
0
        if (opline->result_type != IS_UNUSED) {
15842
0
          jit_set_Z_LVAL(jit, res_addr, ref);
15843
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
15844
0
        }
15845
0
      }
15846
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15847
0
        ref = ir_ADD_OV_L(ref, ir_CONST_LONG(1));
15848
0
      } else {
15849
0
        ref = ir_SUB_OV_L(ref, ir_CONST_LONG(1));
15850
0
      }
15851
15852
0
      ir_STORE(addr, ref);
15853
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
15854
0
      ir_IF_FALSE(if_overflow);
15855
15856
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ) {
15857
0
        if (opline->result_type != IS_UNUSED) {
15858
0
          jit_set_Z_LVAL(jit, res_addr, ref);
15859
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
15860
0
        }
15861
0
      }
15862
0
      ir_END_list(end_inputs);
15863
0
    }
15864
15865
0
    if (var_info & (MAY_BE_ANY - MAY_BE_LONG)) {
15866
0
      if (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15867
0
        may_throw = true;
15868
0
      }
15869
0
      if (if_long) {
15870
0
        ir_IF_FALSE_cold(if_long);
15871
0
      }
15872
0
      if (opline->opcode == ZEND_POST_INC_OBJ || opline->opcode == ZEND_POST_DEC_OBJ) {
15873
0
        jit_ZVAL_COPY(jit, res_addr, -1, var_addr, var_info, true);
15874
0
      }
15875
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15876
0
        if (opline->opcode == ZEND_PRE_INC_OBJ && opline->result_type != IS_UNUSED) {
15877
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_inc),
15878
0
            jit_ZVAL_ADDR(jit, var_addr),
15879
0
            jit_ZVAL_ADDR(jit, res_addr));
15880
0
        } else {
15881
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(increment_function),
15882
0
            jit_ZVAL_ADDR(jit, var_addr));
15883
0
        }
15884
0
      } else {
15885
0
        if (opline->opcode == ZEND_PRE_DEC_OBJ && opline->result_type != IS_UNUSED) {
15886
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_dec),
15887
0
            jit_ZVAL_ADDR(jit, var_addr),
15888
0
            jit_ZVAL_ADDR(jit, res_addr));
15889
0
        } else {
15890
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(decrement_function),
15891
0
            jit_ZVAL_ADDR(jit, var_addr));
15892
0
        }
15893
0
      }
15894
15895
0
      ir_END_list(end_inputs);
15896
0
    }
15897
0
    if (var_info & MAY_BE_LONG) {
15898
0
      ir_IF_TRUE_cold(if_overflow);
15899
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15900
#if SIZEOF_ZEND_LONG == 4
15901
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0));
15902
        jit_set_Z_W2(jit, var_addr, ir_CONST_U32(0x41e00000));
15903
#else
15904
0
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0x43e0000000000000));
15905
0
#endif
15906
0
        jit_set_Z_TYPE_INFO(jit, var_addr, IS_DOUBLE);
15907
0
        if (opline->opcode == ZEND_PRE_INC_OBJ && opline->result_type != IS_UNUSED) {
15908
#if SIZEOF_ZEND_LONG == 4
15909
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
15910
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
15911
#else
15912
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
15913
0
#endif
15914
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
15915
0
        }
15916
0
      } else {
15917
#if SIZEOF_ZEND_LONG == 4
15918
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0x00200000));
15919
        jit_set_Z_W2(jit, var_addr, ir_CONST_U32(0xc1e00000));
15920
#else
15921
0
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0xc3e0000000000000));
15922
0
#endif
15923
0
        jit_set_Z_TYPE_INFO(jit, var_addr, IS_DOUBLE);
15924
0
        if (opline->opcode == ZEND_PRE_DEC_OBJ && opline->result_type != IS_UNUSED) {
15925
#if SIZEOF_ZEND_LONG == 4
15926
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
15927
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
15928
#else
15929
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
15930
0
#endif
15931
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
15932
0
        }
15933
0
      }
15934
0
      if (opline->result_type != IS_UNUSED
15935
0
       && (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ)
15936
0
       && prop_info
15937
0
       && !ZEND_TYPE_IS_SET(prop_info->type)
15938
0
       && (res_info & MAY_BE_GUARD)
15939
0
       && (res_info & MAY_BE_LONG)) {
15940
0
        zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
15941
0
        uint32_t old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
15942
0
        int32_t exit_point;
15943
0
        const void *exit_addr;
15944
15945
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
15946
0
        exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
15947
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15948
0
        if (!exit_addr) {
15949
0
          return 0;
15950
0
        }
15951
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
15952
0
        ssa->var_info[ssa_op->result_def].type = res_info & ~MAY_BE_GUARD;
15953
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
15954
0
      } else {
15955
0
        ir_END_list(end_inputs);
15956
0
      }
15957
0
    }
15958
0
  }
15959
15960
0
  if (slow_inputs) {
15961
0
    const void *func;
15962
15963
0
    ir_MERGE_list(slow_inputs);
15964
15965
    // JIT: zend_jit_pre_inc_obj_helper(zobj, name, CACHE_ADDR(opline->extended_value), result);
15966
0
    switch (opline->opcode) {
15967
0
      case ZEND_PRE_INC_OBJ:
15968
0
        func = zend_jit_pre_inc_obj_helper;
15969
0
        break;
15970
0
      case ZEND_PRE_DEC_OBJ:
15971
0
        func = zend_jit_pre_dec_obj_helper;
15972
0
        break;
15973
0
      case ZEND_POST_INC_OBJ:
15974
0
        func = zend_jit_post_inc_obj_helper;
15975
0
        break;
15976
0
      case ZEND_POST_DEC_OBJ:
15977
0
        func = zend_jit_post_dec_obj_helper;
15978
0
        break;
15979
0
      default:
15980
0
        ZEND_UNREACHABLE();
15981
0
    }
15982
15983
0
    may_throw = true;
15984
0
    jit_SET_EX_OPLINE(jit, opline);
15985
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15986
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(func),
15987
0
      obj_ref,
15988
0
      ir_CONST_ADDR(name),
15989
0
      ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15990
0
      (opline->result_type == IS_UNUSED) ? IR_NULL : jit_ZVAL_ADDR(jit, res_addr));
15991
15992
0
    ir_END_list(end_inputs);
15993
0
  }
15994
15995
0
  if (end_inputs) {
15996
0
    ir_MERGE_list(end_inputs);
15997
0
  }
15998
15999
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this && !op1_indirect) {
16000
0
    if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
16001
0
      may_throw = true;
16002
0
    }
16003
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
16004
0
  }
16005
16006
0
  if (may_throw) {
16007
0
    zend_jit_check_exception(jit);
16008
0
  }
16009
16010
0
  return 1;
16011
0
}
16012
16013
static int zend_jit_fetch_static_prop(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array)
16014
0
{
16015
0
  zend_jit_addr res_addr = RES_ADDR();
16016
0
  uint32_t cache_slot = opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS;
16017
0
  uint32_t flags;
16018
0
  ir_ref ref, ref2, if_cached, fast_path, cold_path, prop_info_ref, if_typed, if_def;
16019
0
  int fetch_type;
16020
0
  zend_property_info *known_prop_info = NULL;
16021
0
  zend_class_entry *ce;
16022
16023
0
  ce = zend_get_known_class(op_array, opline, opline->op2_type, opline->op2);
16024
0
  if (ce && (opline->op2_type == IS_CONST || !(ce->ce_flags & ZEND_ACC_TRAIT))) {
16025
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
16026
0
    zend_string *prop_name;
16027
16028
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
16029
0
    prop_name = Z_STR_P(zv);
16030
0
    zv = zend_hash_find(&ce->properties_info, prop_name);
16031
0
    if (zv) {
16032
0
      zend_property_info *prop_info = Z_PTR_P(zv);
16033
16034
0
      if (prop_info->flags & ZEND_ACC_STATIC) {
16035
0
        if (prop_info->ce == op_array->scope
16036
0
         || (prop_info->flags & ZEND_ACC_PUBLIC)
16037
0
         || ((prop_info->flags & ZEND_ACC_PROTECTED)
16038
0
          && op_array->scope
16039
0
          && instanceof_function_slow(op_array->scope, prop_info->ce))) {
16040
0
          known_prop_info = prop_info;
16041
0
        }
16042
0
      }
16043
0
    }
16044
0
  }
16045
16046
0
  switch (opline->opcode) {
16047
0
    case ZEND_FETCH_STATIC_PROP_R:
16048
0
    case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
16049
0
      fetch_type = BP_VAR_R;
16050
0
      break;
16051
0
    case ZEND_FETCH_STATIC_PROP_IS:
16052
0
      fetch_type = BP_VAR_IS;
16053
0
      break;
16054
0
    case ZEND_FETCH_STATIC_PROP_W:
16055
0
      fetch_type = BP_VAR_W;
16056
0
      break;
16057
0
    case ZEND_FETCH_STATIC_PROP_RW:
16058
0
      fetch_type = BP_VAR_RW;
16059
0
      break;
16060
0
    case ZEND_FETCH_STATIC_PROP_UNSET:
16061
0
      fetch_type = BP_VAR_UNSET;
16062
0
      break;
16063
0
    EMPTY_SWITCH_DEFAULT_CASE();
16064
0
  }
16065
16066
  // JIT: result = CACHED_PTR(cache_slot + sizeof(void *));
16067
0
  ref = ir_LOAD_A(
16068
0
    ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*)));
16069
16070
  // JIT: if (result)
16071
0
  if_cached = ir_IF(ref);
16072
0
  ir_IF_TRUE(if_cached);
16073
16074
0
  if (fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW) {
16075
0
    if (!known_prop_info || ZEND_TYPE_IS_SET(known_prop_info->type)) {
16076
0
      ir_ref merge = IR_UNUSED;
16077
16078
      // JIT: if (UNEXPECTED(Z_TYPE_P(result) == IS_UNDEF)
16079
0
      if_typed = IR_UNUSED;
16080
0
      if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
16081
0
      ir_IF_FALSE_cold(if_def);
16082
0
      if (!known_prop_info) {
16083
        // JIT: if (ZEND_TYPE_IS_SET(property_info->type))
16084
0
        prop_info_ref = ir_LOAD_L(
16085
0
          ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*) * 2));
16086
0
        if_typed = ir_IF(ir_AND_U32(
16087
0
          ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, type.type_mask))),
16088
0
          ir_CONST_U32(_ZEND_TYPE_MASK)));
16089
0
        ir_IF_FALSE(if_typed);
16090
0
        ir_END_list(merge);
16091
0
        ir_IF_TRUE(if_typed);
16092
0
      }
16093
      // JIT: zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
16094
      //      ZSTR_VAL(property_info->ce->name),
16095
      //      zend_get_unmangled_property_name(property_info->name));
16096
0
      jit_SET_EX_OPLINE(jit, opline);
16097
0
      ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_uninit_static_prop));
16098
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
16099
16100
0
      ir_IF_TRUE(if_def);
16101
0
      if (!known_prop_info) {
16102
0
        ir_END_list(merge);
16103
0
        ir_MERGE_list(merge);
16104
0
      }
16105
0
    }
16106
0
  } else if (fetch_type == BP_VAR_W) {
16107
0
    flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
16108
0
    if (flags && (!known_prop_info || ZEND_TYPE_IS_SET(known_prop_info->type))) {
16109
0
        ir_ref merge = IR_UNUSED;
16110
16111
0
      if (!known_prop_info) {
16112
        // JIT: if (ZEND_TYPE_IS_SET(property_info->type))
16113
0
        prop_info_ref = ir_LOAD_L(
16114
0
          ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*) * 2));
16115
0
        if_typed = ir_IF(ir_AND_U32(
16116
0
          ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, type.type_mask))),
16117
0
          ir_CONST_U32(_ZEND_TYPE_MASK)));
16118
0
        ir_IF_FALSE(if_typed);
16119
0
        ir_END_list(merge);
16120
0
        ir_IF_TRUE(if_typed);
16121
0
      } else {
16122
0
        prop_info_ref = ir_CONST_ADDR(known_prop_info);
16123
0
      }
16124
16125
      // JIT: zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
16126
0
      ir_ref if_ok = ir_IF(ir_CALL_5(IR_BOOL, ir_CONST_FUNC(zend_handle_fetch_obj_flags),
16127
0
        IR_NULL, ref, IR_NULL, prop_info_ref, ir_CONST_U32(flags)));
16128
0
      ir_IF_FALSE_cold(if_ok);
16129
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
16130
0
      ir_IF_TRUE(if_ok);
16131
0
      if (!known_prop_info) {
16132
0
        ir_END_list(merge);
16133
0
        ir_MERGE_list(merge);
16134
0
      }
16135
0
    }
16136
0
  }
16137
16138
0
  fast_path = ir_END();
16139
16140
0
  ir_IF_FALSE_cold(if_cached);
16141
0
  jit_SET_EX_OPLINE(jit, opline);
16142
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_fetch_static_property), jit_FP(jit), ir_CONST_I32(fetch_type));
16143
0
  zend_jit_check_exception_undef_result(jit, opline);
16144
0
  cold_path = ir_END();
16145
16146
0
  ir_MERGE_2(fast_path, cold_path);
16147
0
  ref = ir_PHI_2(IR_ADDR, ref, ref2);
16148
16149
0
  if (fetch_type == BP_VAR_R || fetch_type == BP_VAR_IS) {
16150
    // JIT: ZVAL_COPY_DEREF(EX_VAR(opline->result.var), result);
16151
0
    if (!zend_jit_zval_copy_deref(jit, res_addr, ZEND_ADDR_REF_ZVAL(ref),
16152
0
        jit_Z_TYPE_INFO_ref(jit, ref))) {
16153
0
      return 0;
16154
0
    }
16155
0
  } else {
16156
    // JIT: ZVAL_INDIRECT(EX_VAR(opline->result.var), result);
16157
0
    jit_set_Z_PTR(jit, res_addr, ref);
16158
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
16159
0
  }
16160
16161
0
  return 1;
16162
0
}
16163
16164
static int zend_jit_switch(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, zend_ssa *ssa, zend_jit_trace_rec *trace, zend_jit_trace_info *trace_info)
16165
0
{
16166
0
  HashTable *jumptable = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
16167
0
  const zend_op *next_opline = NULL;
16168
0
  ir_refs *slow_inputs;
16169
16170
0
  ir_refs_init(slow_inputs, 8);
16171
16172
0
  if (trace) {
16173
0
    ZEND_ASSERT(trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END);
16174
0
    ZEND_ASSERT(trace->opline != NULL);
16175
0
    next_opline = trace->opline;
16176
0
  }
16177
16178
0
  if (opline->op1_type == IS_CONST) {
16179
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
16180
0
    zval *jump_zv = NULL;
16181
0
    int b;
16182
16183
0
    if (opline->opcode == ZEND_SWITCH_LONG) {
16184
0
      if (Z_TYPE_P(zv) == IS_LONG) {
16185
0
        jump_zv = zend_hash_index_find(jumptable, Z_LVAL_P(zv));
16186
0
      }
16187
0
    } else if (opline->opcode == ZEND_SWITCH_STRING) {
16188
0
      if (Z_TYPE_P(zv) == IS_STRING) {
16189
0
        jump_zv = zend_hash_find_known_hash(jumptable, Z_STR_P(zv));
16190
0
      }
16191
0
    } else if (opline->opcode == ZEND_MATCH) {
16192
0
      if (Z_TYPE_P(zv) == IS_LONG) {
16193
0
        jump_zv = zend_hash_index_find(jumptable, Z_LVAL_P(zv));
16194
0
      } else if (Z_TYPE_P(zv) == IS_STRING) {
16195
0
        jump_zv = zend_hash_find_known_hash(jumptable, Z_STR_P(zv));
16196
0
      }
16197
0
    } else {
16198
0
      ZEND_UNREACHABLE();
16199
0
    }
16200
0
    if (next_opline) {
16201
0
      const zend_op *target;
16202
16203
0
      if (jump_zv != NULL) {
16204
0
        target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(jump_zv));
16205
0
      } else {
16206
0
        target = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
16207
0
      }
16208
0
      ZEND_ASSERT(target == next_opline);
16209
0
    } else {
16210
0
      if (jump_zv != NULL) {
16211
0
        b = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(jump_zv)) - op_array->opcodes];
16212
0
      } else {
16213
0
        b = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) - op_array->opcodes];
16214
0
      }
16215
0
      _zend_jit_add_predecessor_ref(jit, b, jit->b, ir_END());
16216
0
      jit->b = -1;
16217
0
    }
16218
0
  } else {
16219
0
    zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[opline - op_array->opcodes] : NULL;
16220
0
    uint32_t op1_info = OP1_INFO();
16221
0
    zend_jit_addr op1_addr = OP1_ADDR();
16222
0
    const zend_op *default_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
16223
0
    const zend_op *target;
16224
0
    int default_b = next_opline ? -1 : ssa->cfg.map[default_opline - op_array->opcodes];
16225
0
    int b;
16226
0
    int32_t exit_point;
16227
0
    const void *exit_addr;
16228
0
    const void *default_label = NULL;
16229
0
    zval *zv;
16230
16231
0
    if (next_opline) {
16232
0
      if (next_opline != default_opline) {
16233
0
        exit_point = zend_jit_trace_get_exit_point(default_opline, 0);
16234
0
        default_label = zend_jit_trace_get_exit_addr(exit_point);
16235
0
        if (!default_label) {
16236
0
          return 0;
16237
0
        }
16238
0
      }
16239
0
    }
16240
16241
0
    if (opline->opcode == ZEND_SWITCH_LONG) {
16242
0
      if (op1_info & MAY_BE_LONG) {
16243
0
        const void *fallback_label = NULL;
16244
16245
0
        if (next_opline) {
16246
0
          if (next_opline != opline + 1) {
16247
0
            exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
16248
0
            fallback_label = zend_jit_trace_get_exit_addr(exit_point);
16249
0
            if (!fallback_label) {
16250
0
              return 0;
16251
0
            }
16252
0
          }
16253
0
        }
16254
0
        if (op1_info & MAY_BE_REF) {
16255
0
          ir_ref ref, if_long, fast_path, ref2;
16256
16257
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
16258
0
          if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16259
0
          ir_IF_TRUE(if_long);
16260
0
          fast_path = ir_END();
16261
0
          ir_IF_FALSE_cold(if_long);
16262
16263
          // JIT: ZVAL_DEREF(op)
16264
0
          if (fallback_label) {
16265
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_REFERENCE, fallback_label);
16266
0
          } else {
16267
0
            ir_ref if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
16268
0
            ir_IF_FALSE_cold(if_ref);
16269
0
            ir_refs_add(slow_inputs, ir_END());
16270
0
            ir_IF_TRUE(if_ref);
16271
0
          }
16272
16273
0
          ref2 = ir_ADD_OFFSET(jit_Z_PTR(jit, op1_addr), offsetof(zend_reference, val));
16274
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref2);
16275
16276
0
          if (fallback_label) {
16277
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16278
0
          } else {
16279
0
            if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16280
0
            ir_IF_FALSE_cold(if_long);
16281
0
            ir_refs_add(slow_inputs, ir_END());
16282
0
            ir_IF_TRUE(if_long);
16283
0
          }
16284
16285
0
          ir_MERGE_2(fast_path, ir_END());
16286
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
16287
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
16288
0
        } else if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
16289
0
          if (fallback_label) {
16290
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16291
0
          } else {
16292
0
            ir_ref if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16293
0
            ir_IF_FALSE_cold(if_long);
16294
0
            ir_refs_add(slow_inputs, ir_END());
16295
0
            ir_IF_TRUE(if_long);
16296
0
          }
16297
0
        }
16298
0
        ir_ref ref = jit_Z_LVAL(jit, op1_addr);
16299
16300
0
        if (!HT_IS_PACKED(jumptable)) {
16301
0
          ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_index_find),
16302
0
            ir_CONST_ADDR(jumptable), ref);
16303
0
          ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16304
          /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16305
0
          if (sizeof(Bucket) == 32) {
16306
0
            ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16307
0
          } else {
16308
0
            ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16309
0
          }
16310
0
        }
16311
0
        ref = ir_SWITCH(ref);
16312
16313
0
        if (next_opline) {
16314
0
          ir_ref continue_list = IR_UNUSED;
16315
16316
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16317
0
            ir_ref idx;
16318
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16319
16320
0
            if (HT_IS_PACKED(jumptable)) {
16321
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16322
0
            } else {
16323
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16324
0
            }
16325
0
            ir_CASE_VAL(ref, idx);
16326
0
            if (target == next_opline) {
16327
0
              ir_END_list(continue_list);
16328
0
            } else {
16329
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16330
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16331
0
              if (!exit_addr) {
16332
0
                return 0;
16333
0
              }
16334
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16335
0
            }
16336
0
          } ZEND_HASH_FOREACH_END();
16337
16338
0
          ir_CASE_DEFAULT(ref);
16339
0
          if (next_opline == default_opline) {
16340
0
            ir_END_list(continue_list);
16341
0
          } else {
16342
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16343
0
          }
16344
0
          if (continue_list) {
16345
0
            ir_MERGE_list(continue_list);
16346
0
          } else {
16347
0
            ZEND_ASSERT(slow_inputs->count);
16348
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16349
0
          }
16350
0
        } else {
16351
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16352
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16353
0
            b = ssa->cfg.map[target - op_array->opcodes];
16354
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16355
0
          } ZEND_HASH_FOREACH_END();
16356
16357
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16358
0
          if (slow_inputs->count) {
16359
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16360
0
            _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16361
0
          }
16362
0
          jit->b = -1;
16363
0
        }
16364
0
      } else if (!next_opline) {
16365
0
        _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16366
0
        jit->b = -1;
16367
0
      }
16368
0
    } else if (opline->opcode == ZEND_SWITCH_STRING) {
16369
0
      if (op1_info & MAY_BE_STRING) {
16370
0
        const void *fallback_label = NULL;
16371
16372
0
        if (next_opline) {
16373
0
          if (next_opline != opline + 1) {
16374
0
            exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
16375
0
            fallback_label = zend_jit_trace_get_exit_addr(exit_point);
16376
0
            if (!fallback_label) {
16377
0
              return 0;
16378
0
            }
16379
0
          }
16380
0
        }
16381
0
        if (op1_info & MAY_BE_REF) {
16382
0
          ir_ref ref, if_string, fast_path, ref2;
16383
16384
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
16385
0
          if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16386
0
          ir_IF_TRUE(if_string);
16387
0
          fast_path = ir_END();
16388
0
          ir_IF_FALSE_cold(if_string);
16389
16390
          // JIT: ZVAL_DEREF(op)
16391
0
          if (fallback_label) {
16392
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_REFERENCE, fallback_label);
16393
0
          } else {
16394
0
            ir_ref if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16395
0
            ir_IF_FALSE_cold(if_ref);
16396
0
            ir_refs_add(slow_inputs, ir_END());
16397
0
            ir_IF_TRUE(if_ref);
16398
0
          }
16399
16400
0
          ref2 = ir_ADD_OFFSET(jit_Z_PTR(jit, op1_addr), offsetof(zend_reference, val));
16401
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref2);
16402
16403
0
          if (fallback_label) {
16404
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16405
0
          } else {
16406
0
            if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16407
0
            ir_IF_FALSE_cold(if_string);
16408
0
            ir_refs_add(slow_inputs, ir_END());
16409
0
            ir_IF_TRUE(if_string);
16410
0
          }
16411
16412
0
          ir_MERGE_2(fast_path, ir_END());
16413
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
16414
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
16415
0
        } else if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_STRING)) {
16416
0
          if (fallback_label) {
16417
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, fallback_label);
16418
0
          } else {
16419
0
            ir_ref if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16420
0
            ir_IF_FALSE_cold(if_string);
16421
0
            ir_refs_add(slow_inputs, ir_END());
16422
0
            ir_IF_TRUE(if_string);
16423
0
          }
16424
0
        }
16425
16426
0
        ir_ref ref = jit_Z_PTR(jit, op1_addr);
16427
0
        ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_find),
16428
0
          ir_CONST_ADDR(jumptable), ref);
16429
0
        ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16430
        /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16431
0
        if (sizeof(Bucket) == 32) {
16432
0
          ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16433
0
        } else {
16434
0
          ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16435
0
        }
16436
0
        ref = ir_SWITCH(ref);
16437
16438
0
        if (next_opline) {
16439
0
          ir_ref continue_list = IR_UNUSED;
16440
16441
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16442
0
            ir_ref idx;
16443
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16444
16445
0
            if (HT_IS_PACKED(jumptable)) {
16446
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16447
0
            } else {
16448
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16449
0
            }
16450
0
            ir_CASE_VAL(ref, idx);
16451
0
            if (target == next_opline) {
16452
0
              ir_END_list(continue_list);
16453
0
            } else {
16454
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16455
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16456
0
              if (!exit_addr) {
16457
0
                return 0;
16458
0
              }
16459
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16460
0
            }
16461
0
          } ZEND_HASH_FOREACH_END();
16462
16463
0
          ir_CASE_DEFAULT(ref);
16464
0
          if (next_opline == default_opline) {
16465
0
            ir_END_list(continue_list);
16466
0
          } else {
16467
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16468
0
          }
16469
0
          if (continue_list) {
16470
0
            ir_MERGE_list(continue_list);
16471
0
          } else {
16472
0
            ZEND_ASSERT(slow_inputs->count);
16473
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16474
0
          }
16475
0
        } else {
16476
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16477
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16478
0
            b = ssa->cfg.map[target - op_array->opcodes];
16479
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16480
0
          } ZEND_HASH_FOREACH_END();
16481
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16482
0
          if (slow_inputs->count) {
16483
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16484
0
            _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16485
0
          }
16486
0
          jit->b = -1;
16487
0
        }
16488
0
      } else if (!next_opline) {
16489
0
        _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16490
0
        jit->b = -1;
16491
0
      }
16492
0
    } else if (opline->opcode == ZEND_MATCH) {
16493
0
      ir_ref if_type = IR_UNUSED, default_input_list = IR_UNUSED, ref = IR_UNUSED;
16494
0
      ir_ref continue_list = IR_UNUSED;
16495
16496
0
      if (op1_info & (MAY_BE_LONG|MAY_BE_STRING)) {
16497
0
        ir_ref long_path = IR_UNUSED;
16498
16499
0
        if (op1_info & MAY_BE_REF) {
16500
0
          op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
16501
0
        }
16502
0
        if (op1_info & MAY_BE_LONG) {
16503
0
          if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
16504
0
            if (op1_info & (MAY_BE_STRING|MAY_BE_UNDEF)) {
16505
0
              if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16506
0
              ir_IF_TRUE(if_type);
16507
0
            } else if (default_label) {
16508
0
              jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, default_label);
16509
0
            } else if (next_opline) {
16510
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16511
0
              ir_IF_FALSE(if_type);
16512
0
              ir_END_list(continue_list);
16513
0
              ir_IF_TRUE(if_type);
16514
0
            } else {
16515
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16516
0
              ir_IF_FALSE(if_type);
16517
0
              ir_END_list(default_input_list);
16518
0
              ir_IF_TRUE(if_type);
16519
0
            }
16520
0
          }
16521
0
          ref = jit_Z_LVAL(jit, op1_addr);
16522
0
          ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_index_find),
16523
0
            ir_CONST_ADDR(jumptable), ref);
16524
0
          if (op1_info & MAY_BE_STRING) {
16525
0
            long_path = ir_END();
16526
0
          }
16527
0
        }
16528
0
        if (op1_info & MAY_BE_STRING) {
16529
0
          if (if_type) {
16530
0
            ir_IF_FALSE(if_type);
16531
0
            if_type = IS_UNUSED;
16532
0
          }
16533
0
          if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_STRING))) {
16534
0
            if (op1_info & MAY_BE_UNDEF) {
16535
0
              if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16536
0
              ir_IF_TRUE(if_type);
16537
0
            } else if (default_label) {
16538
0
              jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, default_label);
16539
0
            } else if (next_opline) {
16540
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16541
0
              ir_IF_FALSE(if_type);
16542
0
              ir_END_list(continue_list);
16543
0
              ir_IF_TRUE(if_type);
16544
0
            } else {
16545
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16546
0
              ir_IF_FALSE(if_type);
16547
0
              ir_END_list(default_input_list);
16548
0
              ir_IF_TRUE(if_type);
16549
0
            }
16550
0
          }
16551
0
          ir_ref ref2 = jit_Z_PTR(jit, op1_addr);
16552
0
          ref2 = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_find),
16553
0
            ir_CONST_ADDR(jumptable), ref2);
16554
0
          if (op1_info & MAY_BE_LONG) {
16555
0
            ir_MERGE_WITH(long_path);
16556
0
            ref = ir_PHI_2(IR_LONG, ref2, ref);
16557
0
          } else {
16558
0
            ref = ref2;
16559
0
          }
16560
0
        }
16561
16562
0
        ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16563
        /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16564
0
        if (HT_IS_PACKED(jumptable)) {
16565
0
          ZEND_ASSERT(sizeof(zval) == 16);
16566
0
          ref = ir_SHR_L(ref, ir_CONST_LONG(4));
16567
0
        } else {
16568
0
          if (sizeof(Bucket) == 32) {
16569
0
            ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16570
0
          } else {
16571
0
            ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16572
0
          }
16573
0
        }
16574
0
        ref = ir_SWITCH(ref);
16575
16576
0
        if (next_opline) {
16577
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16578
0
            ir_ref idx;
16579
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16580
16581
0
            if (HT_IS_PACKED(jumptable)) {
16582
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16583
0
            } else {
16584
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16585
0
            }
16586
0
            ir_CASE_VAL(ref, idx);
16587
0
            if (target == next_opline) {
16588
0
              ir_END_list(continue_list);
16589
0
            } else {
16590
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16591
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16592
0
              if (!exit_addr) {
16593
0
                return 0;
16594
0
              }
16595
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16596
0
            }
16597
0
          } ZEND_HASH_FOREACH_END();
16598
16599
0
          ir_CASE_DEFAULT(ref);
16600
0
          if (next_opline == default_opline) {
16601
0
            ir_END_list(continue_list);
16602
0
          } else {
16603
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16604
0
          }
16605
0
        } else {
16606
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16607
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16608
0
            b = ssa->cfg.map[target - op_array->opcodes];
16609
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16610
0
          } ZEND_HASH_FOREACH_END();
16611
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16612
0
        }
16613
0
      } else if (!(op1_info & MAY_BE_UNDEF)) {
16614
0
        if (next_opline) {
16615
0
          if (next_opline == default_opline) {
16616
0
            ir_END_list(continue_list);
16617
0
          } else {
16618
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16619
0
          }
16620
0
        } else {
16621
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ir_END());
16622
0
        }
16623
0
      }
16624
16625
0
      if (op1_info & MAY_BE_UNDEF) {
16626
0
        if (if_type) {
16627
0
          ir_IF_FALSE(if_type);
16628
0
          if_type = IS_UNUSED;
16629
0
        }
16630
0
        if (op1_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_STRING))) {
16631
0
          if (default_label) {
16632
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_UNDEF, default_label);
16633
0
          } else if (next_opline) {
16634
0
            ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
16635
0
            ir_IF_TRUE(if_def);
16636
0
            ir_END_list(continue_list);
16637
0
            ir_IF_FALSE_cold(if_def);
16638
0
          } else {
16639
0
            ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
16640
0
            ir_IF_TRUE(if_def);
16641
0
            ir_END_list(default_input_list);
16642
0
            ir_IF_FALSE_cold(if_def);
16643
0
          }
16644
0
        }
16645
16646
0
        jit_SET_EX_OPLINE(jit, opline);
16647
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
16648
0
          ir_CONST_U32(opline->op1.var));
16649
0
        zend_jit_check_exception_undef_result(jit, opline);
16650
0
        if (default_label) {
16651
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16652
0
        } else if (next_opline) {
16653
0
          ir_END_list(continue_list);
16654
0
        } else {
16655
0
          ir_END_list(default_input_list);
16656
0
        }
16657
0
      }
16658
0
      if (next_opline) {
16659
0
        ZEND_ASSERT(continue_list);
16660
0
        ir_MERGE_list(continue_list);
16661
0
      } else {
16662
0
        if (default_input_list) {
16663
0
          if (jit->ctx.ir_base[ref].op == IR_SWITCH) {
16664
0
            ZEND_ASSERT(jit->ctx.ir_base[ref].op3 == IR_UNUSED);
16665
0
            jit->ctx.ir_base[ref].op3 = default_input_list;
16666
0
          } else {
16667
0
            ir_MERGE_list(default_input_list);
16668
0
            _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ir_END());
16669
0
          }
16670
0
        }
16671
0
        jit->b = -1;
16672
0
      }
16673
0
    } else {
16674
0
      ZEND_UNREACHABLE();
16675
0
    }
16676
0
  }
16677
0
  return 1;
16678
0
}
16679
16680
static int zend_jit_start(zend_jit_ctx *jit, const zend_op_array *op_array, zend_ssa *ssa)
16681
0
{
16682
0
  int i, count;
16683
0
  zend_basic_block *bb;
16684
16685
0
  zend_jit_init_ctx(jit, (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) ? 0 : (IR_START_BR_TARGET|IR_ENTRY_BR_TARGET));
16686
16687
0
  jit->ctx.spill_base = ZREG_FP;
16688
16689
0
  jit->op_array = jit->current_op_array = op_array;
16690
0
  jit->ssa = ssa;
16691
0
  jit->bb_start_ref = zend_arena_calloc(&CG(arena), ssa->cfg.blocks_count * 2, sizeof(ir_ref));
16692
0
  jit->bb_predecessors = jit->bb_start_ref + ssa->cfg.blocks_count;
16693
16694
0
  count = 0;
16695
0
  for (i = 0, bb = ssa->cfg.blocks; i < ssa->cfg.blocks_count; i++, bb++) {
16696
0
    jit->bb_predecessors[i] = count;
16697
0
    count += bb->predecessors_count;
16698
0
  }
16699
0
  jit->bb_edges = zend_arena_calloc(&CG(arena), count, sizeof(ir_ref));
16700
16701
0
  if (!GCC_GLOBAL_REGS) {
16702
0
    if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
16703
0
      ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
16704
0
      ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
16705
0
      jit_STORE_FP(jit, execute_data_ref);
16706
0
      jit_STORE_IP(jit, opline_ref);
16707
0
    }
16708
0
    jit->ctx.flags |= IR_FASTCALL_FUNC;
16709
0
  }
16710
16711
0
  return 1;
16712
0
}
16713
16714
static zend_vm_opcode_handler_t zend_jit_finish(zend_jit_ctx *jit)
16715
0
{
16716
0
  void *entry;
16717
0
  size_t size;
16718
0
  zend_string *str = NULL;
16719
16720
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP|
16721
0
      ZEND_JIT_DEBUG_IR_SRC|ZEND_JIT_DEBUG_IR_AFTER_SCCP|ZEND_JIT_DEBUG_IR_AFTER_SCCP|
16722
0
      ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE|ZEND_JIT_DEBUG_IR_AFTER_REGS|ZEND_JIT_DEBUG_IR_FINAL|ZEND_JIT_DEBUG_IR_CODEGEN)) {
16723
0
    if (jit->name) {
16724
0
      str = zend_string_copy(jit->name);
16725
0
    } else {
16726
0
      str = zend_jit_func_name(jit->op_array);
16727
0
    }
16728
0
  }
16729
16730
0
  if (jit->op_array) {
16731
    /* Only for function JIT */
16732
0
    _zend_jit_fix_merges(jit);
16733
#if defined(IR_TARGET_AARCH64)
16734
  } else if (jit->trace) {
16735
    jit->ctx.deoptimization_exits = jit->trace->exit_count;
16736
    jit->ctx.get_exit_addr = zend_jit_trace_get_exit_addr;
16737
#endif
16738
0
  } else {
16739
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
16740
0
    jit->ctx.flags |= IR_GEN_CACHE_DEMOTE;
16741
0
#endif
16742
0
  }
16743
16744
0
  entry = zend_jit_ir_compile(&jit->ctx, &size, str ? ZSTR_VAL(str) : NULL);
16745
0
  if (entry) {
16746
0
    if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
16747
#ifdef HAVE_CAPSTONE
16748
      if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
16749
        if (str) {
16750
          ir_disasm_add_symbol(ZSTR_VAL(str), (uintptr_t)entry, size);
16751
        }
16752
        ir_disasm(str ? ZSTR_VAL(str) : "unknown",
16753
          entry, size,
16754
          (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_ADDR) != 0,
16755
          &jit->ctx, stderr);
16756
      }
16757
#endif
16758
0
#ifndef _WIN32
16759
0
      if (str) {
16760
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
16761
0
          uintptr_t sp_offset = 0;
16762
16763
//          ir_mem_unprotect(entry, size);
16764
0
          if (!(jit->ctx.flags & IR_FUNCTION)
16765
0
           && ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
16766
0
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
16767
0
            sp_offset = zend_jit_hybrid_vm_sp_adj;
16768
#else
16769
            sp_offset = sizeof(void*);
16770
#endif
16771
0
          } else {
16772
0
            sp_offset = sizeof(void*);
16773
0
          }
16774
0
          ir_gdb_register(ZSTR_VAL(str), entry, size, sp_offset, 0);
16775
//          ir_mem_protect(entry, size);
16776
0
        }
16777
16778
0
        if (JIT_G(debug) & (ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
16779
0
          ir_perf_map_register(ZSTR_VAL(str), entry, size);
16780
0
          if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
16781
0
            ir_perf_jitdump_register(ZSTR_VAL(str), entry, size);
16782
0
          }
16783
0
        }
16784
0
      }
16785
0
#endif
16786
0
    }
16787
16788
0
    if (jit->op_array) {
16789
      /* Only for function JIT */
16790
0
      const zend_op_array *op_array = jit->op_array;
16791
0
      zend_op *opline = (zend_op*)op_array->opcodes;
16792
16793
0
      if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
16794
0
        while (opline->opcode == ZEND_RECV) {
16795
0
          opline++;
16796
0
        }
16797
0
      }
16798
0
      opline->handler = (zend_vm_opcode_handler_t)entry;
16799
16800
0
      if (jit->ctx.entries_count) {
16801
        /* For all entries */
16802
0
        int i = jit->ctx.entries_count;
16803
0
        do {
16804
0
          ir_insn *insn = &jit->ctx.ir_base[jit->ctx.entries[--i]];
16805
0
          op_array->opcodes[insn->op2].handler = (zend_vm_opcode_handler_t)((char*)entry + insn->op3);
16806
0
        } while (i != 0);
16807
0
      }
16808
0
    } else {
16809
      /* Only for tracing JIT */
16810
0
      zend_jit_trace_info *t = jit->trace;
16811
0
      zend_jit_trace_stack *stack;
16812
0
      uint32_t i;
16813
16814
0
      if (t) {
16815
0
        for (i = 0; i < t->stack_map_size; i++) {
16816
0
          stack = t->stack_map + i;
16817
0
          if (stack->flags & ZREG_SPILL_SLOT) {
16818
0
            stack->reg = (jit->ctx.flags & IR_USE_FRAME_POINTER) ? IR_REG_FP : IR_REG_SP;
16819
0
            stack->ref = ir_get_spill_slot_offset(&jit->ctx, stack->ref);
16820
0
          }
16821
0
        }
16822
0
      }
16823
16824
0
      zend_jit_trace_add_code(entry, size);
16825
0
    }
16826
0
  }
16827
16828
0
  if (str) {
16829
0
    zend_string_release(str);
16830
0
  }
16831
16832
0
  return (zend_vm_opcode_handler_t)entry;
16833
0
}
16834
16835
static const void *zend_jit_trace_allocate_exit_group(uint32_t n)
16836
0
{
16837
0
  const void *entry;
16838
0
  size_t size;
16839
0
  ir_code_buffer code_buffer;
16840
16841
0
  code_buffer.start = dasm_buf;
16842
0
  code_buffer.end = dasm_end;
16843
0
  code_buffer.pos = *dasm_ptr;
16844
16845
0
  entry = ir_emit_exitgroup(n, ZEND_JIT_EXIT_POINTS_PER_GROUP, zend_jit_stub_handlers[jit_stub_trace_exit],
16846
0
    &code_buffer, &size);
16847
16848
0
  *dasm_ptr = code_buffer.pos;
16849
16850
0
  if (entry) {
16851
#ifdef HAVE_CAPSTONE
16852
    if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
16853
      uint32_t i;
16854
      char name[32];
16855
16856
      for (i = 0; i < ZEND_JIT_EXIT_POINTS_PER_GROUP; i++) {
16857
        snprintf(name, sizeof(name), "jit$$trace_exit_%d", n + i);
16858
        ir_disasm_add_symbol(name, (uintptr_t)entry + (i * ZEND_JIT_EXIT_POINTS_SPACING), ZEND_JIT_EXIT_POINTS_SPACING);
16859
      }
16860
    }
16861
#endif
16862
0
  }
16863
16864
0
  return entry;
16865
0
}
16866
16867
static int zend_jit_type_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var, uint8_t type)
16868
0
{
16869
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16870
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16871
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
16872
16873
0
  if (!exit_addr) {
16874
0
    return 0;
16875
0
  }
16876
0
  ir_GUARD(ir_EQ(jit_Z_TYPE(jit, addr), ir_CONST_U8(type)), ir_CONST_ADDR(exit_addr));
16877
16878
0
  return 1;
16879
0
}
16880
16881
static int zend_jit_scalar_type_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var)
16882
0
{
16883
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16884
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16885
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
16886
16887
0
  if (!exit_addr) {
16888
0
    return 0;
16889
0
  }
16890
0
  ir_GUARD(ir_LT(jit_Z_TYPE(jit, addr), ir_CONST_U8(IS_STRING)), ir_CONST_ADDR(exit_addr));
16891
16892
0
  return 1;
16893
0
}
16894
16895
static bool zend_jit_noref_guard(zend_jit_ctx *jit, const zend_op *opline, zend_jit_addr var_addr)
16896
0
{
16897
0
  uint32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16898
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16899
16900
0
  if (!exit_addr) {
16901
0
    return false;
16902
0
  }
16903
0
  ir_GUARD(ir_NE(jit_Z_TYPE(jit, var_addr), ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16904
16905
0
  return true;
16906
0
}
16907
16908
static int zend_jit_trace_opline_guard(zend_jit_ctx *jit, const zend_op *opline)
16909
0
{
16910
0
  uint32_t exit_point = zend_jit_trace_get_exit_point(NULL, 0);
16911
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16912
16913
0
  if (!exit_addr) {
16914
0
    return 0;
16915
0
  }
16916
16917
0
  ir_GUARD(jit_CMP_IP(jit, IR_EQ, opline), ir_CONST_ADDR(exit_addr));
16918
0
  zend_jit_set_last_valid_opline(jit, opline);
16919
16920
0
  return 1;
16921
0
}
16922
16923
static bool zend_jit_guard_reference(zend_jit_ctx  *jit,
16924
                                     const zend_op *opline,
16925
                                     zend_jit_addr *var_addr_ptr,
16926
                                     zend_jit_addr *ref_addr_ptr,
16927
                                     bool           add_ref_guard)
16928
0
{
16929
0
  zend_jit_addr var_addr = *var_addr_ptr;
16930
0
  const void *exit_addr = NULL;
16931
0
  ir_ref ref;
16932
16933
0
  if (add_ref_guard) {
16934
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16935
16936
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16937
0
    if (!exit_addr) {
16938
0
      return false;
16939
0
    }
16940
16941
0
    ref = jit_Z_TYPE(jit, var_addr);
16942
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16943
0
  }
16944
16945
0
  ref = jit_Z_PTR(jit, var_addr);
16946
0
  *ref_addr_ptr = ZEND_ADDR_REF_ZVAL(ref);
16947
0
  ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
16948
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
16949
0
  *var_addr_ptr = var_addr;
16950
16951
0
  return true;
16952
0
}
16953
16954
static bool zend_jit_fetch_reference(zend_jit_ctx  *jit,
16955
                                     const zend_op *opline,
16956
                                     uint8_t        var_type,
16957
                                     uint32_t      *var_info_ptr,
16958
                                     zend_jit_addr *var_addr_ptr,
16959
                                     bool           add_ref_guard,
16960
                                     bool           add_type_guard)
16961
0
{
16962
0
  zend_jit_addr var_addr = *var_addr_ptr;
16963
0
  uint32_t var_info = *var_info_ptr;
16964
0
  const void *exit_addr = NULL;
16965
0
  ir_ref ref;
16966
16967
0
  if (add_ref_guard || add_type_guard) {
16968
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16969
16970
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16971
0
    if (!exit_addr) {
16972
0
      return false;
16973
0
    }
16974
0
  }
16975
16976
0
  if (add_ref_guard) {
16977
0
    ref = jit_Z_TYPE(jit, var_addr);
16978
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16979
0
  }
16980
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL && opline->op1_type == IS_VAR) {
16981
    /* Hack: Convert reference to regular value to simplify JIT code for INIT_METHOD_CALL */
16982
0
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_unref_helper),
16983
0
      jit_ZVAL_ADDR(jit, var_addr));
16984
0
    *var_addr_ptr = var_addr;
16985
0
  } else {
16986
0
    ref = jit_Z_PTR(jit, var_addr);
16987
0
    ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
16988
0
    var_addr = ZEND_ADDR_REF_ZVAL(ref);
16989
0
    *var_addr_ptr = var_addr;
16990
0
  }
16991
16992
0
  if (var_type != IS_UNKNOWN) {
16993
0
    var_type &= ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED);
16994
0
  }
16995
0
  if (add_type_guard
16996
0
   && var_type != IS_UNKNOWN
16997
0
   && (var_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << var_type)) {
16998
0
    ref = jit_Z_TYPE(jit, var_addr);
16999
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(var_type)), ir_CONST_ADDR(exit_addr));
17000
17001
0
    ZEND_ASSERT(var_info & (1 << var_type));
17002
0
    if (var_type < IS_STRING) {
17003
0
      var_info = (1 << var_type);
17004
0
    } else if (var_type != IS_ARRAY) {
17005
0
      var_info = (1 << var_type) | (var_info & (MAY_BE_RC1|MAY_BE_RCN));
17006
0
    } else {
17007
0
      var_info = MAY_BE_ARRAY | (var_info & (MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_ARRAY_KEY_ANY|MAY_BE_RC1|MAY_BE_RCN));
17008
0
    }
17009
17010
0
    *var_info_ptr = var_info;
17011
0
  } else {
17012
0
    var_info &= ~MAY_BE_REF;
17013
0
    *var_info_ptr = var_info;
17014
0
  }
17015
0
  *var_info_ptr |= MAY_BE_GUARD; /* prevent generation of specialized zval dtor */
17016
17017
0
  return true;
17018
0
}
17019
17020
static bool zend_jit_fetch_indirect_var(zend_jit_ctx *jit, const zend_op *opline, uint8_t var_type, uint32_t *var_info_ptr, zend_jit_addr *var_addr_ptr, bool add_indirect_guard)
17021
0
{
17022
0
  zend_jit_addr var_addr = *var_addr_ptr;
17023
0
  uint32_t var_info = *var_info_ptr;
17024
0
  int32_t exit_point;
17025
0
  const void *exit_addr;
17026
0
  ir_ref ref = IR_UNUSED;
17027
17028
0
  if (add_indirect_guard) {
17029
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
17030
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17031
17032
0
    if (!exit_addr) {
17033
0
      return false;
17034
0
    }
17035
0
    jit_guard_Z_TYPE(jit, var_addr, IS_INDIRECT, exit_addr);
17036
0
    ref = jit_Z_PTR(jit, var_addr);
17037
0
  } else {
17038
    /* This LOAD of INDIRECT VAR, stored by the previous FETCH_(DIM/OBJ)_W,
17039
     * is eliminated by store forwarding (S2L) */
17040
0
    ref = jit_Z_PTR(jit, var_addr);
17041
0
  }
17042
0
  *var_info_ptr &= ~MAY_BE_INDIRECT;
17043
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
17044
0
  *var_addr_ptr = var_addr;
17045
17046
0
  if (var_type != IS_UNKNOWN) {
17047
0
    var_type &= ~(IS_TRACE_INDIRECT|IS_TRACE_PACKED);
17048
0
  }
17049
0
  if (!(var_type & IS_TRACE_REFERENCE)
17050
0
   && var_type != IS_UNKNOWN
17051
0
   && (var_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << var_type)) {
17052
0
    exit_point = zend_jit_trace_get_exit_point(opline, 0);
17053
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17054
17055
0
    if (!exit_addr) {
17056
0
      return false;
17057
0
    }
17058
17059
0
    jit_guard_Z_TYPE(jit, var_addr, var_type, exit_addr);
17060
17061
    //var_info = zend_jit_trace_type_to_info_ex(var_type, var_info);
17062
0
    ZEND_ASSERT(var_info & (1 << var_type));
17063
0
    if (var_type < IS_STRING) {
17064
0
      var_info = (1 << var_type);
17065
0
    } else if (var_type != IS_ARRAY) {
17066
0
      var_info = (1 << var_type) | (var_info & (MAY_BE_RC1|MAY_BE_RCN));
17067
0
    } else {
17068
0
      var_info = MAY_BE_ARRAY | (var_info & (MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_ARRAY_KEY_ANY|MAY_BE_RC1|MAY_BE_RCN));
17069
0
    }
17070
17071
0
    *var_info_ptr = var_info;
17072
0
  }
17073
17074
0
  return true;
17075
0
}
17076
17077
static int zend_jit_trace_handler(zend_jit_ctx *jit, const zend_op_array *op_array, const zend_op *opline, int may_throw, zend_jit_trace_rec *trace)
17078
0
{
17079
0
  zend_jit_op_array_trace_extension *jit_extension =
17080
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
17081
0
  size_t offset = jit_extension->offset;
17082
0
  zend_vm_opcode_handler_func_t handler =
17083
0
    ZEND_OP_TRACE_INFO(opline, offset)->call_handler;
17084
0
  ir_ref ref;
17085
17086
0
  zend_jit_set_ip(jit, opline);
17087
0
  if (GCC_GLOBAL_REGS) {
17088
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
17089
0
  } else {
17090
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
17091
0
    if (opline->opcode == ZEND_RETURN ||
17092
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17093
0
        opline->opcode == ZEND_DO_UCALL ||
17094
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
17095
0
        opline->opcode == ZEND_DO_FCALL ||
17096
0
        opline->opcode == ZEND_GENERATOR_CREATE ||
17097
0
        opline->opcode == ZEND_INCLUDE_OR_EVAL) {
17098
17099
0
      jit_STORE_IP(jit, ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT)));
17100
0
    } else {
17101
0
      jit_STORE_IP(jit, ref);
17102
0
    }
17103
0
  }
17104
0
  if (may_throw
17105
0
   && opline->opcode != ZEND_RETURN
17106
0
   && opline->opcode != ZEND_RETURN_BY_REF) {
17107
0
    zend_jit_check_exception(jit);
17108
0
  }
17109
17110
0
  while (trace->op != ZEND_JIT_TRACE_VM && trace->op != ZEND_JIT_TRACE_END) {
17111
0
    trace++;
17112
0
  }
17113
17114
0
  if ((!GCC_GLOBAL_REGS
17115
0
   && (trace->op != ZEND_JIT_TRACE_END || trace->stop != ZEND_JIT_TRACE_STOP_RETURN))
17116
0
   || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
17117
0
    if (opline->opcode == ZEND_RETURN ||
17118
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17119
0
        opline->opcode == ZEND_DO_UCALL ||
17120
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
17121
0
        opline->opcode == ZEND_DO_FCALL ||
17122
0
        opline->opcode == ZEND_GENERATOR_CREATE ||
17123
0
        opline->opcode == ZEND_INCLUDE_OR_EVAL) {
17124
17125
0
      ir_ref addr = jit_EG(current_execute_data);
17126
17127
0
      jit_STORE_FP(jit, ir_LOAD_A(addr));
17128
0
    }
17129
0
  }
17130
17131
0
  if (zend_jit_trace_may_exit(op_array, opline)) {
17132
0
    if (opline->opcode == ZEND_RETURN ||
17133
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17134
0
        opline->opcode == ZEND_GENERATOR_CREATE) {
17135
17136
0
      if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
17137
0
        if (trace->op != ZEND_JIT_TRACE_END ||
17138
0
            (trace->stop != ZEND_JIT_TRACE_STOP_RETURN &&
17139
0
             trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
17140
          /* this check may be handled by the following OPLINE guard or jmp [IP] */
17141
0
          ir_GUARD(ir_NE(jit_IP(jit), ir_CONST_ADDR(zend_jit_halt_op)),
17142
0
            jit_STUB_ADDR(jit, jit_stub_trace_halt));
17143
0
        }
17144
0
      } else {
17145
        /* IP has been cleared of ZEND_VM_ENTER_BIT already */
17146
0
        ir_GUARD(jit_IP(jit), jit_STUB_ADDR(jit, jit_stub_trace_halt));
17147
0
      }
17148
0
    } else if (opline->opcode == ZEND_GENERATOR_RETURN ||
17149
0
               opline->opcode == ZEND_YIELD ||
17150
0
               opline->opcode == ZEND_YIELD_FROM) {
17151
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_halt));
17152
0
      ir_BEGIN(IR_UNUSED); /* unreachable block */
17153
0
    }
17154
0
    if (trace->op != ZEND_JIT_TRACE_END ||
17155
0
        (trace->stop != ZEND_JIT_TRACE_STOP_RETURN &&
17156
0
         trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
17157
17158
0
      const zend_op *next_opline = trace->opline;
17159
0
      const zend_op *exit_opline = NULL;
17160
0
      uint32_t exit_point;
17161
0
      const void *exit_addr;
17162
0
      uint32_t old_info = 0;
17163
0
      uint32_t old_res_info = 0;
17164
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
17165
17166
0
      if (zend_is_smart_branch(opline)) {
17167
0
        bool exit_if_true = false;
17168
0
        exit_opline = zend_jit_trace_get_exit_opline(trace, opline + 1, &exit_if_true);
17169
0
      } else {
17170
0
        switch (opline->opcode) {
17171
0
          case ZEND_JMPZ:
17172
0
          case ZEND_JMPNZ:
17173
0
          case ZEND_JMPZ_EX:
17174
0
          case ZEND_JMPNZ_EX:
17175
0
          case ZEND_JMP_SET:
17176
0
          case ZEND_COALESCE:
17177
0
          case ZEND_JMP_NULL:
17178
0
          case ZEND_FE_RESET_R:
17179
0
          case ZEND_FE_RESET_RW:
17180
0
            exit_opline = (trace->opline == opline + 1) ?
17181
0
              OP_JMP_ADDR(opline, opline->op2) :
17182
0
              opline + 1;
17183
0
            break;
17184
0
          case ZEND_FE_FETCH_R:
17185
0
          case ZEND_FE_FETCH_RW:
17186
0
            exit_opline = (trace->opline == opline + 1) ?
17187
0
              ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) :
17188
0
              opline + 1;
17189
0
            break;
17190
17191
0
        }
17192
0
      }
17193
17194
0
      switch (opline->opcode) {
17195
0
        case ZEND_FE_FETCH_R:
17196
0
        case ZEND_FE_FETCH_RW:
17197
0
          if (opline->op2_type != IS_UNUSED) {
17198
0
            old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op2.var));
17199
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var), IS_UNKNOWN, 1);
17200
0
          }
17201
0
          break;
17202
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
17203
0
          if (opline->op1_type == IS_CV) {
17204
0
            old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
17205
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17206
0
          }
17207
0
          break;
17208
0
      }
17209
0
      if (opline->result_type == IS_VAR || opline->result_type == IS_TMP_VAR) {
17210
0
        old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
17211
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
17212
0
      }
17213
0
      exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
17214
0
      exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17215
17216
0
      if (opline->result_type == IS_VAR || opline->result_type == IS_TMP_VAR) {
17217
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
17218
0
      }
17219
0
      switch (opline->opcode) {
17220
0
        case ZEND_FE_FETCH_R:
17221
0
        case ZEND_FE_FETCH_RW:
17222
0
          if (opline->op2_type != IS_UNUSED) {
17223
0
            SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op2.var), old_info);
17224
0
          }
17225
0
          break;
17226
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
17227
0
          if (opline->op1_type == IS_CV) {
17228
0
            SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_info);
17229
0
          }
17230
0
          break;
17231
0
      }
17232
17233
0
      if (!exit_addr) {
17234
0
        return 0;
17235
0
      }
17236
0
      ir_GUARD(jit_CMP_IP(jit, IR_EQ, next_opline), ir_CONST_ADDR(exit_addr));
17237
0
    }
17238
0
  }
17239
17240
0
  zend_jit_set_last_valid_opline(jit, trace->opline);
17241
17242
0
  return 1;
17243
0
}
17244
17245
static int zend_jit_deoptimizer_start(zend_jit_ctx        *jit,
17246
                                      zend_string         *name,
17247
                                      uint32_t             trace_num,
17248
                                      uint32_t             exit_num)
17249
0
{
17250
0
  zend_jit_init_ctx(jit, (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) ? 0 : IR_START_BR_TARGET);
17251
17252
0
  jit->ctx.spill_base = ZREG_FP;
17253
17254
0
  jit->op_array = NULL;
17255
0
  jit->ssa = NULL;
17256
0
  jit->name = zend_string_copy(name);
17257
17258
0
  jit->ctx.flags |= IR_SKIP_PROLOGUE;
17259
17260
0
  return 1;
17261
0
}
17262
17263
static int zend_jit_trace_start(zend_jit_ctx        *jit,
17264
                                const zend_op_array *op_array,
17265
                                zend_ssa            *ssa,
17266
                                zend_string         *name,
17267
                                uint32_t             trace_num,
17268
                                zend_jit_trace_info *parent,
17269
                                uint32_t             exit_num)
17270
0
{
17271
0
  zend_jit_init_ctx(jit, (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) ? 0 : IR_START_BR_TARGET);
17272
17273
0
  jit->ctx.spill_base = ZREG_FP;
17274
17275
0
  jit->op_array = NULL;
17276
0
  jit->current_op_array = op_array;
17277
0
  jit->ssa = ssa;
17278
0
  jit->name = zend_string_copy(name);
17279
17280
0
  if (!GCC_GLOBAL_REGS) {
17281
0
    if (!parent) {
17282
0
      if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
17283
0
        ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
17284
0
        ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
17285
0
        jit_STORE_FP(jit, execute_data_ref);
17286
0
        jit_STORE_IP(jit, opline_ref);
17287
0
      }
17288
0
      jit->ctx.flags |= IR_FASTCALL_FUNC;
17289
0
    }
17290
0
  }
17291
17292
0
  if (parent) {
17293
0
    jit->ctx.flags |= IR_SKIP_PROLOGUE;
17294
0
  }
17295
17296
0
  if (parent) {
17297
0
    int i;
17298
0
    int parent_vars_count = parent->exit_info[exit_num].stack_size;
17299
0
    zend_jit_trace_stack *parent_stack = parent_vars_count == 0 ? NULL :
17300
0
      parent->stack_map +
17301
0
      parent->exit_info[exit_num].stack_offset;
17302
17303
    /* prevent clobbering of registers used for deoptimization */
17304
0
    for (i = 0; i < parent_vars_count; i++) {
17305
0
      if (STACK_FLAGS(parent_stack, i) != ZREG_CONST
17306
0
       && STACK_REG(parent_stack, i) != ZREG_NONE) {
17307
0
        int32_t reg = STACK_REG(parent_stack, i);
17308
0
        ir_type type;
17309
17310
0
        if (STACK_FLAGS(parent_stack, i) == ZREG_ZVAL_COPY) {
17311
0
          type = IR_ADDR;
17312
0
        } else if (STACK_TYPE(parent_stack, i) == IS_LONG) {
17313
0
          type = IR_LONG;
17314
0
        } else if (STACK_TYPE(parent_stack, i) == IS_DOUBLE) {
17315
0
          type = IR_DOUBLE;
17316
0
        } else {
17317
0
          ZEND_UNREACHABLE();
17318
0
        }
17319
0
        if (ssa && ssa->vars[i].no_val) {
17320
          /* pass */
17321
0
        } else {
17322
0
          ir_ref ref = ir_RLOAD(type, reg);
17323
17324
0
          if (STACK_FLAGS(parent_stack, i) & (ZREG_LOAD|ZREG_STORE)) {
17325
            /* op3 is used as a flag that the value is already stored in memory.
17326
             * In case the IR framework decides to spill the result of IR_LOAD,
17327
             * it doesn't have to store the value once again.
17328
             *
17329
             * See: insn->op3 check in ir_emit_rload()
17330
             */
17331
0
            ir_set_op(&jit->ctx, ref, 3, EX_NUM_TO_VAR(i));
17332
0
          }
17333
0
        }
17334
0
      }
17335
0
    }
17336
0
  }
17337
17338
0
  if (parent && parent->exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL) {
17339
0
    ZEND_ASSERT(parent->exit_info[exit_num].poly_func.reg >= 0 && parent->exit_info[exit_num].poly_this.reg >= 0);
17340
0
    if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_func.reg)) {
17341
0
      ir_RLOAD_A(parent->exit_info[exit_num].poly_func.reg);
17342
0
    }
17343
0
    if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_this.reg)) {
17344
0
      ir_RLOAD_A(parent->exit_info[exit_num].poly_this.reg);
17345
0
    }
17346
0
  }
17347
17348
0
  ir_STORE(jit_EG(jit_trace_num), ir_CONST_U32(trace_num));
17349
17350
0
  return 1;
17351
0
}
17352
17353
static int zend_jit_trace_begin_loop(zend_jit_ctx *jit)
17354
0
{
17355
0
  return ir_LOOP_BEGIN(ir_END());
17356
0
}
17357
17358
static void zend_jit_trace_gen_phi(zend_jit_ctx *jit, zend_ssa_phi *phi)
17359
0
{
17360
0
  int dst_var = phi->ssa_var;
17361
0
  int src_var = phi->sources[0];
17362
0
  ir_ref ref;
17363
17364
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
17365
0
  ZEND_ASSERT(jit->ra[src_var].ref != IR_UNUSED && jit->ra[src_var].ref != IR_NULL);
17366
17367
0
  ref = ir_PHI_2(
17368
0
    (jit->ssa->var_info[src_var].type & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE,
17369
0
    zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var)), IR_UNUSED);
17370
17371
0
  src_var = phi->sources[1];
17372
0
  ZEND_ASSERT(jit->ra[src_var].ref == IR_NULL);
17373
0
  jit->ra[src_var].flags |= ZREG_FORWARD;
17374
17375
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var), ref);
17376
0
}
17377
17378
static int zend_jit_trace_end_loop(zend_jit_ctx *jit, int loop_ref, const void *timeout_exit_addr)
17379
0
{
17380
0
  if (timeout_exit_addr) {
17381
0
    zend_jit_check_timeout(jit, NULL, timeout_exit_addr);
17382
0
  }
17383
0
  ZEND_ASSERT(jit->ctx.ir_base[loop_ref].op2 == IR_UNUSED);
17384
0
  ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
17385
0
  return 1;
17386
0
}
17387
17388
static int zend_jit_trace_return(zend_jit_ctx *jit, bool original_handler, const zend_op *opline)
17389
0
{
17390
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
17391
0
    if (!original_handler) {
17392
0
      zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
17393
0
    } else {
17394
0
      zend_jit_tailcall_handler(jit, zend_jit_orig_opline_handler(jit));
17395
0
    }
17396
0
  } else {
17397
0
    if (original_handler) {
17398
0
      ir_ref ref;
17399
0
      ir_ref addr = zend_jit_orig_opline_handler(jit);
17400
17401
#if defined(IR_TARGET_X86)
17402
      addr = ir_CAST_FC_FUNC(addr);
17403
#endif
17404
0
      ref = ir_CALL_2(IR_ADDR, addr, jit_FP(jit), jit_IP(jit));
17405
0
      zend_jit_vm_enter(jit, ref);
17406
0
      return 1;
17407
0
    }
17408
0
    zend_jit_vm_enter(jit, jit_IP(jit));
17409
0
  }
17410
0
  return 1;
17411
0
}
17412
17413
static int zend_jit_link_side_trace(const void *code, size_t size, uint32_t jmp_table_size, uint32_t exit_num, const void *addr)
17414
0
{
17415
0
  return ir_patch(code, size, jmp_table_size, zend_jit_trace_get_exit_addr(exit_num), addr);
17416
0
}
17417
17418
static int zend_jit_trace_link_to_root(zend_jit_ctx *jit, zend_jit_trace_info *t, const void *timeout_exit_addr)
17419
0
{
17420
0
  const void *link_addr;
17421
17422
  /* Skip prologue. */
17423
0
  ZEND_ASSERT(zend_jit_trace_prologue_size != (size_t)-1);
17424
0
  link_addr = (const void*)((const char*)t->code_start + zend_jit_trace_prologue_size);
17425
17426
0
  if (timeout_exit_addr) {
17427
0
    zend_jit_check_timeout(jit, NULL, timeout_exit_addr);
17428
0
  }
17429
0
  ir_IJMP(ir_CONST_ADDR(link_addr));
17430
17431
0
  return 1;
17432
0
}
17433
17434
static bool zend_jit_opline_supports_reg(const zend_op_array *op_array, zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op, zend_jit_trace_rec *trace)
17435
0
{
17436
0
  uint32_t op1_info, op2_info;
17437
17438
0
  switch (opline->opcode) {
17439
0
    case ZEND_SEND_VAR:
17440
0
    case ZEND_SEND_VAL:
17441
0
    case ZEND_SEND_VAL_EX:
17442
0
      return (opline->op2_type != IS_CONST) && (opline->opcode != ZEND_SEND_VAL_EX || opline->op2.num <= MAX_ARG_FLAG_NUM);
17443
0
    case ZEND_QM_ASSIGN:
17444
0
    case ZEND_IS_SMALLER:
17445
0
    case ZEND_IS_SMALLER_OR_EQUAL:
17446
0
    case ZEND_IS_EQUAL:
17447
0
    case ZEND_IS_NOT_EQUAL:
17448
0
    case ZEND_IS_IDENTICAL:
17449
0
    case ZEND_IS_NOT_IDENTICAL:
17450
0
    case ZEND_CASE:
17451
0
      return true;
17452
0
    case ZEND_RETURN:
17453
0
      return (op_array->type != ZEND_EVAL_CODE && op_array->function_name);
17454
0
    case ZEND_ASSIGN:
17455
0
      return (opline->op1_type == IS_CV);
17456
0
    case ZEND_ASSIGN_OP:
17457
0
      if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
17458
0
        return false;
17459
0
      }
17460
0
      op1_info = OP1_INFO();
17461
0
      op2_info = OP2_INFO();
17462
0
      return zend_jit_supported_binary_op(opline->extended_value, op1_info, op2_info);
17463
0
    case ZEND_ADD:
17464
0
    case ZEND_SUB:
17465
0
    case ZEND_MUL:
17466
0
      op1_info = OP1_INFO();
17467
0
      op2_info = OP2_INFO();
17468
0
      if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
17469
0
        return false;
17470
0
      }
17471
0
      if (trace && trace->op1_type != IS_UNKNOWN) {
17472
0
        op1_info &= 1U << (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17473
0
      }
17474
0
      if (trace && trace->op2_type != IS_UNKNOWN) {
17475
0
        op2_info &= 1U << (trace->op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17476
0
      }
17477
0
      return !(op1_info & MAY_BE_UNDEF)
17478
0
        && !(op2_info & MAY_BE_UNDEF)
17479
0
        && (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))
17480
0
        && (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE));
17481
0
    case ZEND_BW_OR:
17482
0
    case ZEND_BW_AND:
17483
0
    case ZEND_BW_XOR:
17484
0
    case ZEND_SL:
17485
0
    case ZEND_SR:
17486
0
    case ZEND_MOD:
17487
0
      op1_info = OP1_INFO();
17488
0
      op2_info = OP2_INFO();
17489
0
      if (trace && trace->op1_type != IS_UNKNOWN) {
17490
0
        op1_info &= 1U << (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17491
0
      }
17492
0
      if (trace && trace->op2_type != IS_UNKNOWN) {
17493
0
        op2_info &= 1U << (trace->op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17494
0
      }
17495
0
      return (op1_info & MAY_BE_LONG)
17496
0
        && (op2_info & MAY_BE_LONG);
17497
0
    case ZEND_PRE_INC:
17498
0
    case ZEND_PRE_DEC:
17499
0
    case ZEND_POST_INC:
17500
0
    case ZEND_POST_DEC:
17501
0
      op1_info = OP1_INFO();
17502
0
      return opline->op1_type == IS_CV
17503
0
        && (op1_info & MAY_BE_LONG)
17504
0
        && !(op1_info & MAY_BE_REF);
17505
0
    case ZEND_STRLEN:
17506
0
      op1_info = OP1_INFO();
17507
0
      return (opline->op1_type & (IS_CV|IS_CONST))
17508
0
        && (op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_STRING;
17509
0
    case ZEND_COUNT:
17510
0
      op1_info = OP1_INFO();
17511
0
      return (opline->op1_type & (IS_CV|IS_CONST))
17512
0
        && (op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_ARRAY;
17513
0
    case ZEND_JMPZ:
17514
0
    case ZEND_JMPNZ:
17515
0
      if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE) {
17516
0
        if (!ssa->cfg.map) {
17517
0
          return false;
17518
0
        }
17519
0
        if (opline > op_array->opcodes + ssa->cfg.blocks[ssa->cfg.map[opline-op_array->opcodes]].start &&
17520
0
            ((opline-1)->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
17521
0
          return false;
17522
0
        }
17523
0
      }
17524
0
      ZEND_FALLTHROUGH;
17525
0
    case ZEND_BOOL:
17526
0
    case ZEND_BOOL_NOT:
17527
0
    case ZEND_JMPZ_EX:
17528
0
    case ZEND_JMPNZ_EX:
17529
0
      return true;
17530
0
    case ZEND_FETCH_CONSTANT:
17531
0
      return true;
17532
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
17533
0
      if ((opline->extended_value & ZEND_ISEMPTY)) {
17534
0
        return false;
17535
0
      }
17536
0
      ZEND_FALLTHROUGH;
17537
0
    case ZEND_FETCH_DIM_R:
17538
0
    case ZEND_FETCH_DIM_IS:
17539
0
    case ZEND_FETCH_LIST_R:
17540
0
      op1_info = OP1_INFO();
17541
0
      op2_info = OP2_INFO();
17542
0
      if (trace
17543
0
       && trace->op1_type != IS_UNKNOWN
17544
0
       && (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED)) == IS_ARRAY) {
17545
0
        op1_info &= ~((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY);
17546
0
      }
17547
0
      return ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) &&
17548
0
          (((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) ||
17549
0
           ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_STRING));
17550
0
    case ZEND_ASSIGN_DIM_OP:
17551
0
      if (opline->result_type != IS_UNUSED) {
17552
0
        return false;
17553
0
      }
17554
0
      if (!zend_jit_supported_binary_op(opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
17555
0
        return false;
17556
0
      }
17557
0
      ZEND_FALLTHROUGH;
17558
0
    case ZEND_ASSIGN_DIM:
17559
0
    case ZEND_FETCH_DIM_W:
17560
0
    case ZEND_FETCH_DIM_RW:
17561
0
    case ZEND_FETCH_LIST_W:
17562
0
      op1_info = OP1_INFO();
17563
0
      op2_info = OP2_INFO();
17564
0
      if (trace) {
17565
0
        if (opline->op1_type == IS_CV) {
17566
0
          if ((opline->opcode == ZEND_ASSIGN_DIM
17567
0
            || opline->opcode == ZEND_ASSIGN_DIM_OP)
17568
0
           && (opline+1)->op1_type == IS_CV
17569
0
           && (opline+1)->op1.var == opline->op1.var) {
17570
            /* skip $a[x] = $a; */
17571
0
            return false;
17572
0
          }
17573
0
        } else if (opline->op1_type == IS_VAR) {
17574
0
          if (trace->op1_type == IS_UNKNOWN
17575
0
           || !(trace->op1_type & IS_TRACE_INDIRECT)
17576
0
           || opline->result_type != IS_UNUSED) {
17577
0
            return false;
17578
0
          }
17579
0
        }
17580
0
        if (trace->op1_type != IS_UNKNOWN
17581
0
         && (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED)) == IS_ARRAY) {
17582
0
          op1_info &= ~((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY);
17583
0
        }
17584
0
      } else {
17585
0
        if (opline->op1_type != IS_CV) {
17586
0
          return false;
17587
0
        }
17588
0
      }
17589
0
      return ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) &&
17590
0
          (((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) ||
17591
0
           ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_STRING));
17592
0
    case ZEND_ASSIGN_OBJ_OP:
17593
0
      if (opline->result_type != IS_UNUSED) {
17594
0
        return false;
17595
0
      }
17596
0
      if (!zend_jit_supported_binary_op(opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
17597
0
        return false;
17598
0
      }
17599
0
      ZEND_FALLTHROUGH;
17600
0
    case ZEND_FETCH_OBJ_R:
17601
0
    case ZEND_ASSIGN_OBJ:
17602
0
      if (opline->op2_type != IS_CONST
17603
0
       || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
17604
0
       || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
17605
0
        return false;
17606
0
      }
17607
0
      op1_info = OP1_INFO();
17608
0
      return opline->op1_type == IS_UNUSED || (op1_info & MAY_BE_OBJECT);
17609
0
  }
17610
0
  return false;
17611
0
}
17612
17613
static bool zend_jit_var_supports_reg(zend_ssa *ssa, int var)
17614
0
{
17615
0
  if (ssa->vars[var].no_val) {
17616
    /* we don't need the value */
17617
0
    return false;
17618
0
  }
17619
17620
0
  if (!(JIT_G(opt_flags) & ZEND_JIT_REG_ALLOC_GLOBAL)) {
17621
    /* Disable global register allocation,
17622
     * register allocation for SSA variables connected through Phi functions
17623
     */
17624
0
    if (ssa->vars[var].definition_phi) {
17625
0
      return false;
17626
0
    }
17627
0
    if (ssa->vars[var].phi_use_chain) {
17628
0
      zend_ssa_phi *phi = ssa->vars[var].phi_use_chain;
17629
0
      do {
17630
0
        if (!ssa->vars[phi->ssa_var].no_val) {
17631
0
          return false;
17632
0
        }
17633
0
        phi = zend_ssa_next_use_phi(ssa, var, phi);
17634
0
      } while (phi);
17635
0
    }
17636
0
  }
17637
17638
0
  if (((ssa->var_info[var].type & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_DOUBLE) &&
17639
0
      ((ssa->var_info[var].type & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_LONG)) {
17640
      /* bad type */
17641
0
    return false;
17642
0
  }
17643
17644
0
  return true;
17645
0
}
17646
17647
static bool zend_jit_may_be_in_reg(const zend_op_array *op_array, zend_ssa *ssa, int var)
17648
0
{
17649
0
  if (!zend_jit_var_supports_reg(ssa, var)) {
17650
0
    return false;
17651
0
  }
17652
17653
0
  if (ssa->vars[var].definition >= 0) {
17654
0
    uint32_t def = ssa->vars[var].definition;
17655
0
    if (!zend_jit_opline_supports_reg(op_array, ssa, op_array->opcodes + def, ssa->ops + def, NULL)) {
17656
0
      return false;
17657
0
    }
17658
0
  }
17659
17660
0
  if (ssa->vars[var].use_chain >= 0) {
17661
0
    int use = ssa->vars[var].use_chain;
17662
17663
0
    do {
17664
0
      if (!zend_ssa_is_no_val_use(op_array->opcodes + use, ssa->ops + use, var) &&
17665
0
          !zend_jit_opline_supports_reg(op_array, ssa, op_array->opcodes + use, ssa->ops + use, NULL)) {
17666
0
        return false;
17667
0
      }
17668
0
      use = zend_ssa_next_use(ssa->ops, var, use);
17669
0
    } while (use >= 0);
17670
0
  }
17671
17672
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE) {
17673
0
    int def_block, use_block, b, use, j;
17674
0
    zend_basic_block *bb;
17675
0
    zend_ssa_phi *p;
17676
0
    bool ret = true;
17677
0
    zend_worklist worklist;
17678
0
    ALLOCA_FLAG(use_heap)
17679
17680
    /* Check if live range is split by ENTRY block */
17681
0
    if (ssa->vars[var].definition >= 0) {
17682
0
      def_block =ssa->cfg.map[ssa->vars[var].definition];
17683
0
    } else {
17684
0
      ZEND_ASSERT(ssa->vars[var].definition_phi);
17685
0
      def_block = ssa->vars[var].definition_phi->block;
17686
0
    }
17687
17688
0
    ZEND_WORKLIST_ALLOCA(&worklist, ssa->cfg.blocks_count, use_heap);
17689
17690
0
    if (ssa->vars[var].use_chain >= 0) {
17691
0
      use = ssa->vars[var].use_chain;
17692
0
      do {
17693
0
        use_block = ssa->cfg.map[use];
17694
0
        if (use_block != def_block) {
17695
0
          zend_worklist_push(&worklist, use_block);
17696
0
        }
17697
0
        use = zend_ssa_next_use(ssa->ops, var, use);
17698
0
      } while (use >= 0);
17699
0
    }
17700
17701
0
    p = ssa->vars[var].phi_use_chain;
17702
0
    while (p) {
17703
0
      use_block = p->block;
17704
0
      if (use_block != def_block) {
17705
0
        bb = &ssa->cfg.blocks[use_block];
17706
0
        for (j = 0; j < bb->predecessors_count; j++) {
17707
0
          if (p->sources[j] == var) {
17708
0
            use_block = ssa->cfg.predecessors[bb->predecessor_offset + j];
17709
0
            if (use_block != def_block) {
17710
0
              zend_worklist_push(&worklist, use_block);
17711
0
            }
17712
0
          }
17713
0
        }
17714
0
      }
17715
0
      p = zend_ssa_next_use_phi(ssa, var, p);
17716
0
    }
17717
17718
0
    while (zend_worklist_len(&worklist) != 0) {
17719
0
      b = zend_worklist_pop(&worklist);
17720
0
      bb = &ssa->cfg.blocks[b];
17721
0
      if (bb->flags & (ZEND_BB_ENTRY|ZEND_BB_RECV_ENTRY)) {
17722
0
        ret = false;
17723
0
        break;
17724
0
      }
17725
0
      for (j = 0; j < bb->predecessors_count; j++) {
17726
0
        b = ssa->cfg.predecessors[bb->predecessor_offset + j];
17727
0
        if (b != def_block) {
17728
0
          zend_worklist_push(&worklist, b);
17729
0
        }
17730
0
      }
17731
0
    }
17732
17733
0
    ZEND_WORKLIST_FREE_ALLOCA(&worklist, use_heap);
17734
17735
0
    return ret;
17736
0
  }
17737
17738
0
  return true;
17739
0
}
17740
17741
0
static ir_ref jit_frameless_observer(zend_jit_ctx *jit, const zend_op *opline) {
17742
  // JIT: zend_observer_handler_is_unobserved(ZEND_OBSERVER_DATA(fbc))
17743
0
  ir_ref observer_handler;
17744
0
  zend_function *fbc = ZEND_FLF_FUNC(opline);
17745
  // Not need for runtime cache or generator checks here, we just need if_unobserved
17746
0
  ir_ref if_unobserved = jit_observer_fcall_is_unobserved_start(jit, fbc, &observer_handler, IR_UNUSED, IR_UNUSED).if_unobserved;
17747
17748
  // Call zend_frameless_observed_call for the main logic.
17749
0
  ir_CALL_1(IR_VOID, ir_CONST_ADDR((size_t)zend_frameless_observed_call), jit_FP(jit));
17750
17751
0
  ir_ref skip = ir_END();
17752
0
  ir_IF_TRUE(if_unobserved);
17753
0
  return skip;
17754
0
}
17755
17756
static void jit_frameless_icall0(zend_jit_ctx *jit, const zend_op *opline)
17757
0
{
17758
0
  jit_SET_EX_OPLINE(jit, opline);
17759
17760
0
  void *function = ZEND_FLF_HANDLER(opline);
17761
0
  zend_jit_addr res_addr = RES_ADDR();
17762
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17763
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17764
17765
0
  ir_ref skip_observer = IR_UNUSED;
17766
0
  if (ZEND_OBSERVER_ENABLED) {
17767
0
    skip_observer = jit_frameless_observer(jit, opline);
17768
0
  }
17769
17770
0
  ir_CALL_1(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref);
17771
17772
0
  if (skip_observer != IR_UNUSED) {
17773
0
    ir_MERGE_WITH(skip_observer);
17774
0
  }
17775
17776
0
  zend_jit_check_exception(jit);
17777
0
}
17778
17779
static void jit_frameless_icall1(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
17780
0
{
17781
0
  jit_SET_EX_OPLINE(jit, opline);
17782
17783
  /* Avoid dropping RC check in case op escapes. */
17784
0
  if (op1_info & MAY_BE_RC1) {
17785
0
    op1_info |= MAY_BE_RCN;
17786
0
  }
17787
17788
0
  void *function = ZEND_FLF_HANDLER(opline);
17789
0
  zend_jit_addr res_addr = RES_ADDR();
17790
0
  zend_jit_addr op1_addr = OP1_ADDR();
17791
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17792
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17793
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17794
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17795
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17796
0
    op1_info &= ~MAY_BE_UNDEF;
17797
0
    op1_info |= MAY_BE_NULL;
17798
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17799
0
  }
17800
0
  if (op1_info & MAY_BE_REF) {
17801
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17802
0
  }
17803
17804
0
  ir_ref skip_observer = IR_UNUSED;
17805
0
  if (ZEND_OBSERVER_ENABLED) {
17806
0
    skip_observer = jit_frameless_observer(jit, opline);
17807
0
  }
17808
17809
0
  ir_CALL_2(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref);
17810
17811
0
  if (skip_observer != IR_UNUSED) {
17812
0
    ir_MERGE_WITH(skip_observer);
17813
0
  }
17814
17815
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17816
0
  zend_jit_check_exception(jit);
17817
0
}
17818
17819
static void jit_frameless_icall2(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint32_t op2_info)
17820
0
{
17821
0
  jit_SET_EX_OPLINE(jit, opline);
17822
17823
  /* Avoid dropping RC check in case op escapes. */
17824
0
  if (op1_info & MAY_BE_RC1) {
17825
0
    op1_info |= MAY_BE_RCN;
17826
0
  }
17827
0
  if (op2_info & MAY_BE_RC1) {
17828
0
    op2_info |= MAY_BE_RCN;
17829
0
  }
17830
17831
0
  void *function = ZEND_FLF_HANDLER(opline);
17832
0
  zend_jit_addr res_addr = RES_ADDR();
17833
0
  zend_jit_addr op1_addr = OP1_ADDR();
17834
0
  zend_jit_addr op2_addr = OP2_ADDR();
17835
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17836
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17837
0
  ir_ref op2_ref = jit_ZVAL_ADDR(jit, op2_addr);
17838
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17839
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17840
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17841
0
    op1_info &= ~MAY_BE_UNDEF;
17842
0
    op1_info |= MAY_BE_NULL;
17843
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17844
0
  }
17845
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
17846
0
    op2_ref = zend_jit_zval_check_undef(jit, op2_ref, opline->op2.var, opline, true);
17847
0
    op2_info &= ~MAY_BE_UNDEF;
17848
0
    op2_info |= MAY_BE_NULL;
17849
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2_ref);
17850
0
  }
17851
0
  if (op1_info & MAY_BE_REF) {
17852
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17853
0
  }
17854
0
  if (op2_info & MAY_BE_REF) {
17855
0
    op2_ref = jit_ZVAL_DEREF_ref(jit, op2_ref);
17856
0
  }
17857
17858
0
  ir_ref skip_observer = IR_UNUSED;
17859
0
  if (ZEND_OBSERVER_ENABLED) {
17860
0
    skip_observer = jit_frameless_observer(jit, opline);
17861
0
  }
17862
17863
0
  ir_CALL_3(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref, op2_ref);
17864
17865
0
  if (skip_observer != IR_UNUSED) {
17866
0
    ir_MERGE_WITH(skip_observer);
17867
0
  }
17868
17869
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17870
  /* Set OP1 to UNDEF in case FREE_OP2() throws. */
17871
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) != 0
17872
0
   && (opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
17873
0
   && (op2_info & MAY_BE_RC1)
17874
0
   && (op2_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
17875
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_UNDEF);
17876
0
    if (JIT_G(current_frame)) {
17877
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
17878
0
        EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17879
0
    }
17880
0
  }
17881
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
17882
0
  zend_jit_check_exception(jit);
17883
0
}
17884
17885
static void jit_frameless_icall3(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint32_t op2_info, uint32_t op1_data_info)
17886
0
{
17887
0
  jit_SET_EX_OPLINE(jit, opline);
17888
17889
  /* Avoid dropping RC check in case op escapes. */
17890
0
  if (op1_info & MAY_BE_RC1) {
17891
0
    op1_info |= MAY_BE_RCN;
17892
0
  }
17893
0
  if (op2_info & MAY_BE_RC1) {
17894
0
    op2_info |= MAY_BE_RCN;
17895
0
  }
17896
0
  if (op1_data_info & MAY_BE_RC1) {
17897
0
    op1_data_info |= MAY_BE_RCN;
17898
0
  }
17899
17900
0
  void *function = ZEND_FLF_HANDLER(opline);
17901
0
  uint8_t op_data_type = (opline + 1)->op1_type;
17902
0
  zend_jit_addr res_addr = RES_ADDR();
17903
0
  zend_jit_addr op1_addr = OP1_ADDR();
17904
0
  zend_jit_addr op2_addr = OP2_ADDR();
17905
0
  zend_jit_addr op3_addr = OP1_DATA_ADDR();
17906
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17907
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17908
0
  ir_ref op2_ref = jit_ZVAL_ADDR(jit, op2_addr);
17909
0
  ir_ref op3_ref = jit_ZVAL_ADDR(jit, op3_addr);
17910
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17911
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17912
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17913
0
    op1_info &= ~MAY_BE_UNDEF;
17914
0
    op1_info |= MAY_BE_NULL;
17915
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17916
0
  }
17917
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
17918
0
    op2_ref = zend_jit_zval_check_undef(jit, op2_ref, opline->op2.var, opline, true);
17919
0
    op2_info &= ~MAY_BE_UNDEF;
17920
0
    op2_info |= MAY_BE_NULL;
17921
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2_ref);
17922
0
  }
17923
0
  if ((opline+1)->op1_type == IS_CV && (op1_data_info & MAY_BE_UNDEF)) {
17924
0
    op3_ref = zend_jit_zval_check_undef(jit, op3_ref, (opline+1)->op1.var, opline, true);
17925
0
    op1_data_info &= ~MAY_BE_UNDEF;
17926
0
    op1_data_info |= MAY_BE_NULL;
17927
0
    op3_addr = ZEND_ADDR_REF_ZVAL(op3_ref);
17928
0
  }
17929
0
  if (op1_info & MAY_BE_REF) {
17930
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17931
0
  }
17932
0
  if (op2_info & MAY_BE_REF) {
17933
0
    op2_ref = jit_ZVAL_DEREF_ref(jit, op2_ref);
17934
0
  }
17935
0
  if (op1_data_info & MAY_BE_REF) {
17936
0
    op3_ref = jit_ZVAL_DEREF_ref(jit, op3_ref);
17937
0
  }
17938
17939
0
  ir_ref skip_observer = IR_UNUSED;
17940
0
  if (ZEND_OBSERVER_ENABLED) {
17941
0
    skip_observer = jit_frameless_observer(jit, opline);
17942
0
  }
17943
17944
0
  ir_CALL_4(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref, op2_ref, op3_ref);
17945
17946
0
  if (skip_observer != IR_UNUSED) {
17947
0
    ir_MERGE_WITH(skip_observer);
17948
0
  }
17949
17950
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17951
  /* Set OP1 to UNDEF in case FREE_OP2() throws. */
17952
0
  bool op1_undef = false;
17953
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
17954
0
   && (((opline->op2_type & (IS_VAR|IS_TMP_VAR))
17955
0
     && (op2_info & MAY_BE_RC1)
17956
0
     && (op2_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)))
17957
0
    || ((op_data_type & (IS_VAR|IS_TMP_VAR))
17958
0
     && (op1_data_info & MAY_BE_RC1)
17959
0
     && (op1_data_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))))) {
17960
0
      op1_undef = true;
17961
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_UNDEF);
17962
0
    if (JIT_G(current_frame)) {
17963
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
17964
0
        EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17965
0
    }
17966
0
  }
17967
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
17968
  /* If OP1 is set to UNDEF, we don't need to set OP2 to UNDEF on free because
17969
   * zend_fetch_debug_backtrace aborts when it encounters the first UNDEF TMP|VAR. */
17970
0
  if (!op1_undef
17971
0
   && (opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
17972
0
   && (op_data_type & (IS_VAR|IS_TMP_VAR)) != 0
17973
0
   && (op1_data_info & MAY_BE_RC1)
17974
0
   && (op1_data_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
17975
0
    jit_set_Z_TYPE_INFO(jit, op2_addr, IS_UNDEF);
17976
0
    if (JIT_G(current_frame)) {
17977
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
17978
0
        EX_VAR_TO_NUM(opline->op2.var), IS_UNKNOWN, 1);
17979
0
    }
17980
0
  }
17981
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, op1_data_info, NULL);
17982
0
  zend_jit_check_exception(jit);
17983
0
}
17984
17985
/*
17986
 * Local variables:
17987
 * tab-width: 4
17988
 * c-basic-offset: 4
17989
 * indent-tabs-mode: t
17990
 * End:
17991
 */