Coverage Report

Created: 2026-07-25 06:39

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 © The PHP Group and Contributors.                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to the Modified BSD License that is      |
8
   | bundled with this package in the file LICENSE, and is available      |
9
   | through the World Wide Web at <https://www.php.net/license/>.        |
10
   |                                                                      |
11
   | SPDX-License-Identifier: BSD-3-Clause                                |
12
   +----------------------------------------------------------------------+
13
   | Authors: Dmitry Stogov <dmitry@php.net>                              |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include "Zend/zend_cpuinfo.h"
18
#include "Zend/zend_types.h"
19
#include "Zend/zend_type_info.h"
20
#include "jit/ir/ir.h"
21
#include "jit/ir/ir_builder.h"
22
#include "jit/tls/zend_jit_tls.h"
23
24
#if defined(IR_TARGET_X86)
25
# define IR_REG_SP            4 /* IR_REG_RSP */
26
# define IR_REG_FP            5 /* IR_REG_RBP */
27
# define ZREG_FP              6 /* IR_REG_RSI */
28
# define ZREG_IP              7 /* IR_REG_RDI */
29
# define ZREG_FIRST_FPR       8
30
# define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<6) | (1<<7)) /* all preserved registers */
31
# if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
32
#  error
33
# endif
34
#elif defined(IR_TARGET_X64)
35
0
# define IR_REG_SP            4 /* IR_REG_RSP */
36
0
# define IR_REG_FP            5 /* IR_REG_RBP */
37
# if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
38
/* Use the first two arg registers of the preserve_none calling convention for FP/IP
39
 * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/X86/X86CallingConv.td#L1029 */
40
0
#  define ZREG_FP             12 /* IR_REG_R12 */
41
0
#  define ZREG_IP             13 /* IR_REG_R13 */
42
# else
43
#  define ZREG_FP             14 /* IR_REG_R14 */
44
#  define ZREG_IP             15 /* IR_REG_R15 */
45
# endif
46
0
# define ZREG_FIRST_FPR      16
47
# if defined(_WIN64)
48
#  define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<6) | (1<<7) | (1<<12) | (1<<13) | (1<<14) | (1<<15))
49
/*
50
#  define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<6) | (1<<7) | (1<<12) | (1<<13) | (1<<14) | (1<<15) | \
51
                               (1<<(16+6)) | (1<<(16+7)) | (1<<(16+8)) | (1<<(16+9)) | (1<<(16+10)) | \
52
                               (1<<(16+11)) | (1<<(16+12)) | (1<<(16+13)) | (1<<(16+14)) | (1<<(16+15)))
53
*/
54
#  define IR_SHADOW_ARGS     32
55
# else
56
0
#  define IR_REGSET_PRESERVED ((1<<3) | (1<<5) | (1<<12) | (1<<13) | (1<<14) | (1<<15)) /* all preserved registers */
57
# endif
58
#elif defined(IR_TARGET_AARCH64)
59
# define IR_REG_SP           31 /* IR_REG_RSP */
60
# define IR_REG_LR           30 /* IR_REG_X30 */
61
# define IR_REG_FP           29 /* IR_REG_X29 */
62
# if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
63
/* Use the first two arg registers of the preserve_none calling convention for FP/IP
64
 * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/AArch64/AArch64CallingConvention.td#L541 */
65
#  define ZREG_FP             20 /* IR_REG_X20 */
66
#  define ZREG_IP             21 /* IR_REG_X21 */
67
# else
68
#  define ZREG_FP             27 /* IR_REG_X27 */
69
#  define ZREG_IP             28 /* IR_REG_X28 */
70
# endif
71
# define ZREG_FIRST_FPR      32
72
# define IR_REGSET_PRESERVED ((1<<19) | (1<<20) | (1<<21) | (1<<22) | (1<<23) | \
73
                              (1<<24) | (1<<25) | (1<<26) | (1<<27) | (1<<28)) /* all preserved registers */
74
#else
75
# error "Unknown IR target"
76
#endif
77
78
0
#define ZREG_RX ZREG_IP
79
80
#define OPTIMIZE_FOR_SIZE 0
81
82
/* IR builder defines */
83
#undef  _ir_CTX
84
0
#define _ir_CTX                 (&jit->ctx)
85
86
#if GCC_GLOBAL_REGS
87
# define IR_OPCODE_HANDLER_RET IR_VOID
88
#else
89
# define IR_OPCODE_HANDLER_RET IR_ADDR
90
#endif
91
92
#undef  ir_CONST_ADDR
93
0
#define ir_CONST_ADDR(_addr)    jit_CONST_ADDR(jit, (uintptr_t)(_addr))
94
#define ir_CONST_FUNC(_addr)    jit_CONST_FUNC(jit, (uintptr_t)(_addr), 0)
95
0
#define ir_CONST_FC_FUNC(_addr) jit_CONST_FUNC(jit, (uintptr_t)(_addr), IR_FASTCALL_FUNC)
96
#define ir_CAST_FC_FUNC(_addr)  ir_fold2(_ir_CTX, IR_OPT(IR_PROTO, IR_ADDR), (_addr), \
97
  ir_proto_0(_ir_CTX, IR_FASTCALL_FUNC, IR_I32))
98
# define ir_CONST_OPCODE_HANDLER_FUNC(_addr) \
99
0
  jit_CONST_OPCODE_HANDLER_FUNC(jit, _addr)
100
# define ir_CAST_OPCODE_HANDLER_FUNC(_addr)  ir_fold2(_ir_CTX, IR_OPT(IR_PROTO, IR_ADDR), (_addr), \
101
  ir_proto_0(_ir_CTX, IR_FASTCALL_FUNC, IR_OPCODE_HANDLER_RET))
102
103
#define ir_CONST_FUNC_PROTO(_addr, _proto) \
104
  jit_CONST_FUNC_PROTO(jit, (uintptr_t)(_addr), (_proto))
105
106
#undef  ir_ADD_OFFSET
107
#define ir_ADD_OFFSET(_addr, _offset) \
108
0
  jit_ADD_OFFSET(jit, _addr, _offset)
109
110
#ifdef ZEND_ENABLE_ZVAL_LONG64
111
0
# define IR_LONG           IR_I64
112
0
# define ir_CONST_LONG     ir_CONST_I64
113
# define ir_UNARY_OP_L     ir_UNARY_OP_I64
114
0
# define ir_BINARY_OP_L    ir_BINARY_OP_I64
115
# define ir_ADD_L          ir_ADD_I64
116
0
# define ir_SUB_L          ir_SUB_I64
117
0
# define ir_MUL_L          ir_MUL_I64
118
0
# define ir_DIV_L          ir_DIV_I64
119
0
# define ir_MOD_L          ir_MOD_I64
120
# define ir_NEG_L          ir_NEG_I64
121
# define ir_ABS_L          ir_ABS_I64
122
# define ir_SEXT_L         ir_SEXT_I64
123
0
# define ir_ZEXT_L         ir_ZEXT_I64
124
# define ir_TRUNC_L        ir_TRUNC_I64
125
0
# define ir_BITCAST_L      ir_BITCAST_I64
126
# define ir_FP2L           ir_FP2I64
127
0
# define ir_ADD_OV_L       ir_ADD_OV_I64
128
0
# define ir_SUB_OV_L       ir_SUB_OV_I64
129
# define ir_MUL_OV_L       ir_MUL_OV_I64
130
# define ir_NOT_L          ir_NOT_I64
131
# define ir_OR_L           ir_OR_I64
132
0
# define ir_AND_L          ir_AND_I64
133
# define ir_XOR_L          ir_XOR_I64
134
0
# define ir_SHL_L          ir_SHL_I64
135
0
# define ir_SHR_L          ir_SHR_I64
136
0
# define ir_SAR_L          ir_SAR_I64
137
# define ir_ROL_L          ir_ROL_I64
138
# define ir_ROR_L          ir_ROR_I64
139
# define ir_MIN_L          ir_MIN_I64
140
# define ir_MAX_L          ir_MAX_I64
141
0
# define ir_LOAD_L         ir_LOAD_I64
142
#else
143
# define IR_LONG           IR_I32
144
# define ir_CONST_LONG     ir_CONST_I32
145
# define ir_UNARY_OP_L     ir_UNARY_OP_I32
146
# define ir_BINARY_OP_L    ir_BINARY_OP_I32
147
# define ir_ADD_L          ir_ADD_I32
148
# define ir_SUB_L          ir_SUB_I32
149
# define ir_MUL_L          ir_MUL_I32
150
# define ir_DIV_L          ir_DIV_I32
151
# define ir_MOD_L          ir_MOD_I32
152
# define ir_NEG_L          ir_NEG_I32
153
# define ir_ABS_L          ir_ABS_I32
154
# define ir_SEXT_L         ir_SEXT_I32
155
# define ir_ZEXT_L         ir_ZEXT_I32
156
# define ir_TRUNC_L        ir_TRUNC_I32
157
# define ir_BITCAST_L      ir_BITCAST_I32
158
# define ir_FP2L           ir_FP2I32
159
# define ir_ADD_OV_L       ir_ADD_OV_I32
160
# define ir_SUB_OV_L       ir_SUB_OV_I32
161
# define ir_MUL_OV_L       ir_MUL_OV_I32
162
# define ir_NOT_L          ir_NOT_I32
163
# define ir_OR_L           ir_OR_I32
164
# define ir_AND_L          ir_AND_I32
165
# define ir_XOR_L          ir_XOR_I32
166
# define ir_SHL_L          ir_SHL_I32
167
# define ir_SHR_L          ir_SHR_I32
168
# define ir_SAR_L          ir_SAR_I32
169
# define ir_ROL_L          ir_ROL_I32
170
# define ir_ROR_L          ir_ROR_I32
171
# define ir_MIN_L          ir_MIN_I32
172
# define ir_MAX_L          ir_MAX_I32
173
# define ir_LOAD_L         ir_LOAD_I32
174
#endif
175
176
/* A helper structure to collect IT rers for the following use in (MERGE/PHI)_N */
177
typedef struct _ir_refs {
178
  uint32_t count;
179
  uint32_t limit;
180
  ir_ref   refs[] ZEND_ELEMENT_COUNT(count);
181
} ir_refs;
182
183
#define ir_refs_size(_n)          (offsetof(ir_refs, refs) + sizeof(ir_ref) * (_n))
184
0
#define ir_refs_init(_name, _n)   _name = alloca(ir_refs_size(_n)); \
185
0
                                  do {_name->count = 0; _name->limit = (_n);} while (0)
186
187
static void ir_refs_add(ir_refs *refs, ir_ref ref)
188
0
{
189
0
  ir_ref *ptr;
190
191
0
  ZEND_ASSERT(refs->count < refs->limit);
192
0
  ptr = refs->refs;
193
0
  ptr[refs->count++] = ref;
194
0
}
195
196
static size_t zend_jit_trace_prologue_size = (size_t)-1;
197
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
198
static uint32_t allowed_opt_flags = 0;
199
static uint32_t default_mflags = 0;
200
#endif
201
static bool delayed_call_chain = false; // TODO: remove this var (use jit->delayed_call_level) ???
202
203
#ifdef ZTS
204
static size_t tsrm_ls_cache_tcb_offset = 0;
205
static size_t tsrm_tls_index = -1;
206
static size_t tsrm_tls_offset = -1;
207
208
# define EG_TLS_OFFSET(field) \
209
  (executor_globals_offset + offsetof(zend_executor_globals, field))
210
211
# define CG_TLS_OFFSET(field) \
212
  (compiler_globals_offset + offsetof(zend_compiler_globals, field))
213
214
# define jit_EG(_field) \
215
  ir_ADD_OFFSET(jit_TLS(jit), EG_TLS_OFFSET(_field))
216
217
# define jit_CG(_field) \
218
  ir_ADD_OFFSET(jit_TLS(jit), CG_TLS_OFFSET(_field))
219
220
#else
221
222
# define jit_EG(_field) \
223
0
  ir_CONST_ADDR(&EG(_field))
224
225
# define jit_CG(_field) \
226
  ir_CONST_ADDR(&CG(_field))
227
228
#endif
229
230
#define jit_CALL(_call, _field) \
231
0
  ir_ADD_OFFSET(_call, offsetof(zend_execute_data, _field))
232
233
#define jit_EX(_field) \
234
  jit_CALL(jit_FP(jit), _field)
235
236
#define jit_RX(_field) \
237
  jit_CALL(jit_IP(jit), _field)
238
239
#define JIT_STUBS(_) \
240
  _(exception_handler,              IR_SKIP_PROLOGUE) \
241
  _(exception_handler_undef,        IR_SKIP_PROLOGUE) \
242
  _(exception_handler_free_op2,     IR_SKIP_PROLOGUE) \
243
  _(exception_handler_free_op1_op2, IR_SKIP_PROLOGUE) \
244
  _(interrupt_handler,              IR_SKIP_PROLOGUE) \
245
  _(leave_function_handler,         IR_SKIP_PROLOGUE) \
246
  _(negative_shift,                 IR_SKIP_PROLOGUE) \
247
  _(mod_by_zero,                    IR_SKIP_PROLOGUE) \
248
  _(invalid_this,                   IR_SKIP_PROLOGUE) \
249
  _(undefined_function,             IR_SKIP_PROLOGUE) \
250
  _(throw_cannot_pass_by_ref,       IR_SKIP_PROLOGUE) \
251
  _(icall_throw,                    IR_SKIP_PROLOGUE) \
252
  _(leave_throw,                    IR_SKIP_PROLOGUE) \
253
  _(hybrid_runtime_jit,             IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
254
  _(hybrid_profile_jit,             IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
255
  _(hybrid_func_hot_counter,        IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
256
  _(hybrid_loop_hot_counter,        IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
257
  _(hybrid_func_trace_counter,      IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
258
  _(hybrid_ret_trace_counter,       IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
259
  _(hybrid_loop_trace_counter,      IR_SKIP_PROLOGUE | IR_START_BR_TARGET) \
260
  _(trace_halt,                     IR_SKIP_PROLOGUE) \
261
  _(trace_escape,                   IR_SKIP_PROLOGUE) \
262
  _(trace_exit,                     IR_SKIP_PROLOGUE) \
263
  _(undefined_offset,               IR_FUNCTION | IR_FASTCALL_FUNC) \
264
  _(undefined_key,                  IR_FUNCTION | IR_FASTCALL_FUNC) \
265
  _(cannot_add_element,             IR_FUNCTION | IR_FASTCALL_FUNC) \
266
  _(assign_const,                   IR_FUNCTION | IR_FASTCALL_FUNC) \
267
  _(assign_tmp,                     IR_FUNCTION | IR_FASTCALL_FUNC) \
268
  _(assign_var,                     IR_FUNCTION | IR_FASTCALL_FUNC) \
269
  _(assign_cv_noref,                IR_FUNCTION | IR_FASTCALL_FUNC) \
270
  _(assign_cv,                      IR_FUNCTION | IR_FASTCALL_FUNC) \
271
  _(new_array,                      IR_FUNCTION | IR_FASTCALL_FUNC) \
272
273
#define JIT_STUB_ID(name, flags) \
274
  jit_stub_ ## name,
275
276
#define JIT_STUB_FORWARD(name, flags) \
277
  static int zend_jit_ ## name ## _stub(zend_jit_ctx *jit);
278
279
#define JIT_STUB(name, flags) \
280
  {JIT_STUB_PREFIX #name, zend_jit_ ## name ## _stub, flags},
281
282
typedef enum _jit_stub_id {
283
  JIT_STUBS(JIT_STUB_ID)
284
  jit_last_stub
285
} jit_stub_id;
286
287
typedef struct _zend_jit_reg_var {
288
  ir_ref   ref;
289
  uint32_t flags;
290
} zend_jit_reg_var;
291
292
typedef struct _zend_jit_ctx {
293
  ir_ctx               ctx;
294
  const zend_op       *last_valid_opline;
295
  bool                 use_last_valid_opline;
296
  bool                 track_last_valid_opline;
297
  bool                 reuse_ip;
298
  uint32_t             delayed_call_level;
299
  int                  b;           /* current basic block number or -1 */
300
#ifdef ZTS
301
  ir_ref               tls;
302
#endif
303
  ir_ref               fp;
304
  ir_ref               poly_func_ref; /* restored from parent trace snapshot */
305
  ir_ref               poly_this_ref; /* restored from parent trace snapshot */
306
  ir_ref               trace_loop_ref;
307
  ir_ref               return_inputs;
308
  const zend_op_array *op_array;
309
  const zend_op_array *current_op_array;
310
  zend_ssa            *ssa;
311
  zend_string         *name;
312
  ir_ref              *bb_start_ref;      /* PHP BB -> IR ref mapping */
313
  ir_ref              *bb_predecessors;   /* PHP BB -> index in bb_edges -> IR refs of predessors */
314
  ir_ref              *bb_edges;
315
  zend_jit_trace_info *trace;
316
  zend_jit_reg_var    *ra;
317
  int                  delay_var;
318
  ir_refs             *delay_refs;
319
  ir_ref               eg_exception_addr;
320
  HashTable            addr_hash;
321
  ir_ref               stub_addr[jit_last_stub];
322
} zend_jit_ctx;
323
324
typedef int8_t zend_reg;
325
326
typedef struct _zend_jit_registers_buf {
327
#if defined(IR_TARGET_X64)
328
  uint64_t gpr[16]; /* general purpose integer register */
329
  double   fpr[16]; /* floating point registers */
330
#elif defined(IR_TARGET_X86)
331
  uint32_t gpr[8]; /* general purpose integer register */
332
  double   fpr[8]; /* floating point registers */
333
#elif defined (IR_TARGET_AARCH64)
334
  uint64_t gpr[32]; /* general purpose integer register */
335
  double   fpr[32]; /* floating point registers */
336
#else
337
# error "Unknown IR target"
338
#endif
339
} zend_jit_registers_buf;
340
341
/* Keep 32 exit points in a single code block */
342
0
#define ZEND_JIT_EXIT_POINTS_SPACING   4  // push byte + short jmp = bytes
343
0
#define ZEND_JIT_EXIT_POINTS_PER_GROUP 32 // number of continuous exit points
344
345
static uint32_t zend_jit_exit_point_by_addr(const void *addr);
346
int ZEND_FASTCALL zend_jit_trace_exit(uint32_t exit_num, zend_jit_registers_buf *regs);
347
348
static int zend_jit_assign_to_variable(zend_jit_ctx   *jit,
349
                                       const zend_op  *opline,
350
                                       zend_jit_addr   var_use_addr,
351
                                       zend_jit_addr   var_addr,
352
                                       uint32_t        var_info,
353
                                       uint32_t        var_def_info,
354
                                       uint8_t         val_type,
355
                                       zend_jit_addr   val_addr,
356
                                       uint32_t        val_info,
357
                                       zend_jit_addr   res_addr,
358
                                       zend_jit_addr   ref_addr,
359
                                       bool       check_exception);
360
361
static ir_ref jit_CONST_FUNC(zend_jit_ctx *jit, uintptr_t addr, uint16_t flags);
362
363
typedef struct _zend_jit_stub {
364
  const char *name;
365
  int (*stub)(zend_jit_ctx *jit);
366
  uint32_t flags;
367
} zend_jit_stub;
368
369
JIT_STUBS(JIT_STUB_FORWARD)
370
371
static const zend_jit_stub zend_jit_stubs[] = {
372
  JIT_STUBS(JIT_STUB)
373
};
374
375
#if defined(_WIN32) || defined(IR_TARGET_AARCH64)
376
/* We keep addresses in SHM to share them between sepaeate processes (on Windows) or to support veneers (on AArch64) */
377
static void** zend_jit_stub_handlers = NULL;
378
#else
379
static void* zend_jit_stub_handlers[sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0])];
380
#endif
381
382
#if defined(IR_TARGET_AARCH64)
383
384
# ifdef __FreeBSD__
385
/* https://github.com/freebsd/freebsd-src/blob/c52ca7dd09066648b1cc40f758289404d68ab886/libexec/rtld-elf/aarch64/reloc.c#L180-L184 */
386
typedef struct TLSDescriptor {
387
  void*   thunk;
388
  int     index;
389
  size_t  offset;
390
} TLSDescriptor;
391
# endif
392
393
#define IR_HAS_VENEERS (1U<<31) /* IR_RESERVED_FLAG_1 */
394
395
static const void *zend_jit_get_veneer(ir_ctx *ctx, const void *addr)
396
{
397
  int i, count = sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
398
399
  for (i = 0; i < count; i++) {
400
    if (zend_jit_stub_handlers[i] == addr) {
401
      return zend_jit_stub_handlers[count + i];
402
    }
403
  }
404
405
  if (((zend_jit_ctx*)ctx)->trace
406
   && (void*)addr >= dasm_buf && (void*)addr < dasm_end) {
407
    uint32_t exit_point = zend_jit_exit_point_by_addr(addr);
408
409
    if (exit_point != (uint32_t)-1) {
410
      zend_jit_trace_info *t = ((zend_jit_ctx*)ctx)->trace;
411
412
      ZEND_ASSERT(exit_point < t->exit_count);
413
      return (const void*)((char*)ctx->deoptimization_exits_base + (exit_point * 4));
414
    }
415
  }
416
417
  return NULL;
418
}
419
420
static bool zend_jit_set_veneer(ir_ctx *ctx, const void *addr, const void *veneer)
421
{
422
  int i, count = sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
423
  uint32_t exit_point = zend_jit_exit_point_by_addr(addr);
424
425
  if (exit_point != (uint32_t)-1) {
426
    return true;
427
  }
428
  for (i = 0; i < count; i++) {
429
    if (zend_jit_stub_handlers[i] == addr) {
430
      const void **ptr = (const void**)&zend_jit_stub_handlers[count + i];
431
      *ptr = veneer;
432
      ctx->flags2 |= IR_HAS_VENEERS;
433
#ifdef HAVE_CAPSTONE
434
      int64_t offset;
435
        if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
436
        const char *name = ir_disasm_find_symbol((uint64_t)(uintptr_t)addr, &offset);
437
438
        if (name && !offset) {
439
          if (strstr(name, "@veneer") == NULL) {
440
            char *new_name;
441
442
            zend_spprintf(&new_name, 0, "%s@veneer", name);
443
            ir_disasm_add_symbol(new_name, (uint64_t)(uintptr_t)veneer, 4);
444
            efree(new_name);
445
          } else {
446
            ir_disasm_add_symbol(name, (uint64_t)(uintptr_t)veneer, 4);
447
          }
448
        }
449
      }
450
#endif
451
      return true;
452
    }
453
  }
454
455
  return false;
456
}
457
458
static void zend_jit_commit_veneers(void)
459
{
460
  int i, count = sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
461
462
  for (i = 0; i < count; i++) {
463
    if (zend_jit_stub_handlers[count + i]) {
464
      zend_jit_stub_handlers[i] = zend_jit_stub_handlers[count + i];
465
      zend_jit_stub_handlers[count + i] = NULL;
466
    }
467
  }
468
}
469
#endif
470
471
static bool zend_jit_prefer_const_addr_load(zend_jit_ctx *jit, uintptr_t addr)
472
0
{
473
#if defined(IR_TARGET_X86)
474
  return false; /* always use immediate value */
475
#elif defined(IR_TARGET_X64)
476
  return addr > 0xffffffff; /* prefer loading long constant from memery */
477
#elif defined(IR_TARGET_AARCH64)
478
  return addr > 0xffff;
479
#else
480
# error "Unknown IR target"
481
#endif
482
0
}
483
484
static const char* zend_reg_name(int8_t reg)
485
0
{
486
0
  return ir_reg_name(reg, ir_reg_is_int(reg) ? IR_LONG : IR_DOUBLE);
487
0
}
488
489
/* IR helpers */
490
491
#ifdef ZTS
492
static void * ZEND_FASTCALL zend_jit_get_tsrm_ls_cache(void)
493
{
494
  return _tsrm_ls_cache;
495
}
496
497
static ir_ref jit_TLS(zend_jit_ctx *jit)
498
{
499
  ZEND_ASSERT(jit->ctx.control);
500
  if (jit->tls) {
501
    /* Emit "TLS" once for basic block */
502
    ir_insn *insn;
503
    ir_ref ref = jit->ctx.control;
504
505
    while (1) {
506
      if (ref == jit->tls) {
507
        return jit->tls;
508
      }
509
      insn = &jit->ctx.ir_base[ref];
510
      if (insn->op >= IR_START || insn->op == IR_CALL) {
511
        break;
512
      }
513
      ref = insn->op1;
514
    }
515
  }
516
517
  if (tsrm_ls_cache_tcb_offset == 0 && tsrm_tls_index == -1) {
518
    jit->tls = ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_get_tsrm_ls_cache));
519
  } else {
520
    jit->tls = ir_TLS(
521
        tsrm_ls_cache_tcb_offset ? tsrm_ls_cache_tcb_offset : tsrm_tls_index,
522
        tsrm_ls_cache_tcb_offset ? IR_NULL : tsrm_tls_offset);
523
  }
524
525
  return jit->tls;
526
}
527
#endif
528
529
static ir_ref jit_CONST_ADDR(zend_jit_ctx *jit, uintptr_t addr)
530
0
{
531
0
  ir_ref ref;
532
0
  zval *zv;
533
534
0
  if (addr == 0) {
535
0
    return IR_NULL;
536
0
  }
537
0
  zv = zend_hash_index_lookup(&jit->addr_hash, addr);
538
0
  if (Z_TYPE_P(zv) == IS_LONG) {
539
0
    ref = Z_LVAL_P(zv);
540
0
    ZEND_ASSERT(jit->ctx.ir_base[ref].opt == IR_OPT(IR_ADDR, IR_ADDR));
541
0
  } else {
542
0
    ref = ir_unique_const_addr(&jit->ctx, addr);
543
0
    ZVAL_LONG(zv, ref);
544
0
  }
545
0
  return ref;
546
0
}
547
548
static ir_ref jit_CONST_FUNC_PROTO(zend_jit_ctx *jit, uintptr_t addr, ir_ref proto)
549
0
{
550
0
  ir_ref ref;
551
0
  ir_insn *insn;
552
0
  zval *zv;
553
554
0
  ZEND_ASSERT(addr != 0);
555
0
  zv = zend_hash_index_lookup(&jit->addr_hash, addr);
556
0
  if (Z_TYPE_P(zv) == IS_LONG) {
557
0
    ref = Z_LVAL_P(zv);
558
0
    ZEND_ASSERT(jit->ctx.ir_base[ref].opt == IR_OPT(IR_FUNC_ADDR, IR_ADDR) && jit->ctx.ir_base[ref].proto == proto);
559
0
  } else {
560
0
    ref = ir_unique_const_addr(&jit->ctx, addr);
561
0
    insn = &jit->ctx.ir_base[ref];
562
0
    insn->optx = IR_OPT(IR_FUNC_ADDR, IR_ADDR);
563
0
    insn->proto = proto;
564
0
    ZVAL_LONG(zv, ref);
565
0
  }
566
0
  return ref;
567
0
}
568
569
static ir_ref jit_CONST_FUNC(zend_jit_ctx *jit, uintptr_t addr, uint16_t flags)
570
0
{
571
#if defined(IR_TARGET_X86)
572
  /* TODO: dummy prototype (only flags matter) ??? */
573
  ir_ref proto = flags ? ir_proto_0(&jit->ctx, flags, IR_I32) : 0;
574
#else
575
0
  ir_ref proto = 0;
576
0
#endif
577
578
0
  return jit_CONST_FUNC_PROTO(jit, addr, proto);
579
0
}
580
581
static ir_ref jit_CONST_OPCODE_HANDLER_FUNC(zend_jit_ctx *jit, zend_vm_opcode_handler_t handler)
582
0
{
583
0
  return jit_CONST_FUNC(jit, (uintptr_t)handler, IR_FASTCALL_FUNC);
584
0
}
585
586
static ir_ref jit_ADD_OFFSET(zend_jit_ctx *jit, ir_ref addr, uintptr_t offset)
587
0
{
588
0
  if (offset) {
589
0
    addr = ir_ADD_A(addr, ir_CONST_ADDR(offset));
590
0
  }
591
0
  return addr;
592
0
}
593
594
static ir_ref jit_EG_exception(zend_jit_ctx *jit)
595
0
{
596
#ifdef ZTS
597
  return jit_EG(exception);
598
#else
599
0
  ir_ref ref = jit->eg_exception_addr;
600
601
0
  if (UNEXPECTED(!ref)) {
602
0
    ref = ir_unique_const_addr(&jit->ctx, (uintptr_t)&EG(exception));
603
0
    jit->eg_exception_addr = ref;
604
0
  }
605
0
  return ref;
606
0
#endif
607
0
}
608
609
static ir_ref jit_STUB_ADDR(zend_jit_ctx *jit, jit_stub_id id)
610
0
{
611
0
  ir_ref ref = jit->stub_addr[id];
612
613
0
  if (UNEXPECTED(!ref)) {
614
0
    ref = ir_unique_const_addr(&jit->ctx, (uintptr_t)zend_jit_stub_handlers[id]);
615
0
    jit->stub_addr[id] = ref;
616
0
  }
617
0
  return ref;
618
0
}
619
620
static ir_ref jit_STUB_FUNC_ADDR(zend_jit_ctx *jit, jit_stub_id id, uint16_t flags)
621
0
{
622
0
  ir_ref ref = jit->stub_addr[id];
623
0
  ir_insn *insn;
624
625
0
  if (UNEXPECTED(!ref)) {
626
0
    ref = ir_unique_const_addr(&jit->ctx, (uintptr_t)zend_jit_stub_handlers[id]);
627
0
    insn = &jit->ctx.ir_base[ref];
628
0
    insn->optx = IR_OPT(IR_FUNC_ADDR, IR_ADDR);
629
#if defined(IR_TARGET_X86)
630
    /* TODO: dummy prototype (only flags matter) ??? */
631
    insn->proto = flags ? ir_proto_0(&jit->ctx, flags, IR_I32) : 0;
632
#else
633
0
    insn->proto = 0;
634
0
#endif
635
0
    jit->stub_addr[id] = ref;
636
0
  }
637
0
  return ref;
638
0
}
639
640
static void jit_SNAPSHOT(zend_jit_ctx *jit, ir_ref addr)
641
0
{
642
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && JIT_G(current_frame)) {
643
0
    const void *ptr = (const void*)jit->ctx.ir_base[addr].val.addr;
644
0
    const zend_op_array *op_array = &JIT_G(current_frame)->func->op_array;
645
0
    uint32_t stack_size = op_array->last_var + op_array->T;
646
647
0
    if (ptr == zend_jit_stub_handlers[jit_stub_exception_handler]
648
0
       || ptr == zend_jit_stub_handlers[jit_stub_exception_handler_undef]
649
0
       || ptr == zend_jit_stub_handlers[jit_stub_exception_handler_free_op1_op2]
650
0
       || ptr == zend_jit_stub_handlers[jit_stub_exception_handler_free_op2]
651
0
       || ptr == zend_jit_stub_handlers[jit_stub_interrupt_handler]
652
0
       || ptr == zend_jit_stub_handlers[jit_stub_leave_function_handler]
653
0
       || ptr == zend_jit_stub_handlers[jit_stub_negative_shift]
654
0
       || ptr == zend_jit_stub_handlers[jit_stub_mod_by_zero]
655
0
       || ptr == zend_jit_stub_handlers[jit_stub_invalid_this]
656
0
       || ptr == zend_jit_stub_handlers[jit_stub_undefined_function]
657
0
       || ptr == zend_jit_stub_handlers[jit_stub_throw_cannot_pass_by_ref]
658
0
       || ptr == zend_jit_stub_handlers[jit_stub_icall_throw]
659
0
       || ptr == zend_jit_stub_handlers[jit_stub_leave_throw]
660
0
       || ptr == zend_jit_stub_handlers[jit_stub_trace_halt]
661
0
       || ptr == zend_jit_stub_handlers[jit_stub_trace_escape]) {
662
      /* This is a GUARD that trigger exit through a stub code (without deoptimization) */
663
0
      return;
664
0
    }
665
666
    /* Check if we need snapshot entries for polymorphic method call */
667
0
    zend_jit_trace_info *t = jit->trace;
668
0
    uint32_t exit_point = 0, n = 0;
669
670
0
    if (addr < 0) {
671
      /* addr is not always the address of the *last* exit point,
672
       * so we can not optimize this to 'exit_point = t->exit_count-1' */
673
0
      exit_point = zend_jit_exit_point_by_addr(ptr);
674
0
      ZEND_ASSERT(exit_point != -1);
675
0
      if (t->exit_info[exit_point].flags & ZEND_JIT_EXIT_METHOD_CALL) {
676
0
        n = 2;
677
0
      }
678
0
    }
679
680
0
    if (stack_size || n) {
681
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
682
0
      uint32_t snapshot_size, i;
683
684
0
      snapshot_size = stack_size;
685
0
      while (snapshot_size > 0) {
686
0
        ir_ref ref = STACK_REF(stack, snapshot_size - 1);
687
688
0
        if (!ref || ref == IR_NULL || (STACK_FLAGS(stack, snapshot_size - 1) & (/*ZREG_LOAD|*/ZREG_STORE))) {
689
0
          snapshot_size--;
690
0
        } else {
691
0
          break;
692
0
        }
693
0
      }
694
0
      if (snapshot_size || n) {
695
0
        ir_ref snapshot;
696
697
0
        snapshot = ir_SNAPSHOT(snapshot_size + n);
698
0
        for (i = 0; i < snapshot_size; i++) {
699
0
          ir_ref ref = STACK_REF(stack, i);
700
701
0
          if (!ref || ref == IR_NULL || (STACK_FLAGS(stack, i) & (/*ZREG_LOAD|*/ZREG_STORE))) {
702
0
            ref = IR_UNUSED;
703
0
          }
704
0
          ir_SNAPSHOT_SET_OP(snapshot, i + 1, ref);
705
0
        }
706
0
        if (n) {
707
0
          ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 1, t->exit_info[exit_point].poly_func.ref);
708
0
          ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 2, t->exit_info[exit_point].poly_this.ref);
709
0
        }
710
0
      }
711
0
    }
712
0
  }
713
0
}
714
715
static int32_t _add_trace_const(zend_jit_trace_info *t, int64_t val)
716
0
{
717
0
  int32_t i;
718
719
0
  for (i = 0; i < t->consts_count; i++) {
720
0
    if (t->constants[i].i == val) {
721
0
      return i;
722
0
    }
723
0
  }
724
0
  ZEND_ASSERT(i < 0x7fffffff);
725
0
  t->consts_count = i + 1;
726
0
  t->constants = erealloc(t->constants, (i + 1) * sizeof(zend_jit_exit_const));
727
0
  t->constants[i].i = val;
728
0
  return i;
729
0
}
730
731
uint32_t zend_jit_duplicate_exit_point(ir_ctx *ctx, zend_jit_trace_info *t, uint32_t exit_point, ir_ref snapshot_ref)
732
0
{
733
0
  uint32_t stack_size, stack_offset;
734
0
  uint32_t new_exit_point = t->exit_count;
735
736
0
  if (new_exit_point >= ZEND_JIT_TRACE_MAX_EXITS) {
737
0
    ctx->status = -ZEND_JIT_TRACE_STOP_TOO_MANY_EXITS;
738
0
    return exit_point;
739
0
  }
740
741
0
  t->exit_count++;
742
0
  memcpy(&t->exit_info[new_exit_point], &t->exit_info[exit_point], sizeof(zend_jit_trace_exit_info));
743
0
  stack_size = t->exit_info[new_exit_point].stack_size;
744
0
  if (stack_size != 0) {
745
0
    stack_offset = t->stack_map_size;
746
0
    t->stack_map_size += stack_size;
747
    // TODO: reduce number of reallocations ???
748
0
    t->stack_map = erealloc(t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
749
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));
750
0
    t->exit_info[new_exit_point].stack_offset = stack_offset;
751
0
  }
752
0
  t->exit_info[new_exit_point].flags &= ~ZEND_JIT_EXIT_FIXED;
753
754
0
  return new_exit_point;
755
0
}
756
757
static void zend_jit_resolve_ref_snapshot(zend_jit_ref_snapshot *dest, ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snapshot, int op)
758
0
{
759
0
  int8_t *reg_ops = ctx->regs[snapshot_ref];
760
0
  ZEND_ASSERT(reg_ops[op] != ZREG_NONE);
761
762
0
  int8_t reg = reg_ops[op];
763
0
  int32_t offset;
764
765
0
  if (IR_REG_SPILLED(reg)) {
766
0
    reg = ((ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FP : IR_REG_SP) | IR_REG_SPILL_LOAD;
767
0
    offset = ir_get_spill_slot_offset(ctx, ir_insn_op(snapshot, op));
768
0
  } else {
769
0
    offset = 0;
770
0
  }
771
772
0
  dest->reg = reg;
773
0
  dest->offset = offset;
774
0
}
775
776
static bool zend_jit_ref_snapshot_equals(const zend_jit_ref_snapshot *a, const zend_jit_ref_snapshot *b)
777
0
{
778
0
  return a->reg == b->reg
779
0
    && (!IR_REG_SPILLED(a->reg) || (a->offset == b->offset));
780
0
}
781
782
void *zend_jit_snapshot_handler(ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snapshot, void *addr)
783
0
{
784
0
  zend_jit_trace_info *t = ((zend_jit_ctx*)ctx)->trace;
785
0
  uint32_t exit_point, exit_flags;
786
0
  ir_ref n = snapshot->inputs_count;
787
0
  ir_ref i;
788
789
0
  exit_point = zend_jit_exit_point_by_addr(addr);
790
0
  ZEND_ASSERT(exit_point < t->exit_count);
791
0
  exit_flags = t->exit_info[exit_point].flags;
792
793
0
  if (exit_flags & ZEND_JIT_EXIT_METHOD_CALL) {
794
0
    zend_jit_ref_snapshot func, this;
795
0
    zend_jit_resolve_ref_snapshot(&func, ctx, snapshot_ref, snapshot, n - 1);
796
0
    zend_jit_resolve_ref_snapshot(&this, ctx, snapshot_ref, snapshot, n);
797
798
0
    if ((exit_flags & ZEND_JIT_EXIT_FIXED)
799
0
     && (!zend_jit_ref_snapshot_equals(&t->exit_info[exit_point].poly_func, &func)
800
0
       || !zend_jit_ref_snapshot_equals(&t->exit_info[exit_point].poly_this, &this))) {
801
0
      exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
802
0
      addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
803
0
      exit_flags &= ~ZEND_JIT_EXIT_FIXED;
804
0
    }
805
0
    t->exit_info[exit_point].poly_func = func;
806
0
    t->exit_info[exit_point].poly_this = this;
807
0
    n -= 2;
808
0
  }
809
810
0
  for (i = 2; i <= n; i++) {
811
0
    ir_ref ref = ir_insn_op(snapshot, i);
812
813
0
    if (ref) {
814
0
      int8_t *reg_ops = ctx->regs[snapshot_ref];
815
0
      int8_t reg = reg_ops[i];
816
0
      ir_ref var = i - 2;
817
818
0
      ZEND_ASSERT(var < t->exit_info[exit_point].stack_size);
819
0
      if (t->stack_map[t->exit_info[exit_point].stack_offset + var].flags == ZREG_ZVAL_COPY) {
820
0
        ZEND_ASSERT(reg != ZREG_NONE);
821
0
        if ((exit_flags & ZEND_JIT_EXIT_FIXED)
822
0
         && t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != IR_REG_NUM(reg)) {
823
0
          exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
824
0
          addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
825
0
          exit_flags &= ~ZEND_JIT_EXIT_FIXED;
826
0
        }
827
0
        t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = IR_REG_NUM(reg);
828
0
      } else if (t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_CONST) {
829
0
        ZEND_ASSERT(t->stack_map[t->exit_info[exit_point].stack_offset + var].type == IS_LONG ||
830
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].type == IS_DOUBLE);
831
832
0
        if (ref > 0) {
833
0
          if (reg != ZREG_NONE) {
834
0
            if (reg & IR_REG_SPILL_LOAD) {
835
0
              ZEND_ASSERT(!(reg & IR_REG_SPILL_SPECIAL));
836
              /* spill slot on a CPU stack */
837
0
              if ((exit_flags & ZEND_JIT_EXIT_FIXED)
838
0
               && (t->stack_map[t->exit_info[exit_point].stack_offset + var].ref != ref
839
0
                || t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
840
0
                || !(t->stack_map[t->exit_info[exit_point].stack_offset + var].flags & ZREG_SPILL_SLOT))) {
841
0
                exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
842
0
                addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
843
0
                exit_flags &= ~ZEND_JIT_EXIT_FIXED;
844
0
              }
845
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].ref = ref;
846
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = ZREG_NONE;
847
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].flags |= ZREG_SPILL_SLOT;
848
0
            } else if (reg & IR_REG_SPILL_SPECIAL) {
849
              /* spill slot on a VM stack */
850
0
              if ((exit_flags & ZEND_JIT_EXIT_FIXED)
851
0
               && (t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
852
0
               || t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_TYPE_ONLY)) {
853
0
                exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
854
0
                addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
855
0
                exit_flags &= ~ZEND_JIT_EXIT_FIXED;
856
0
              }
857
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = ZREG_NONE;
858
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_TYPE_ONLY;
859
0
            } else {
860
0
              if ((exit_flags & ZEND_JIT_EXIT_FIXED)
861
0
               && (t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != IR_REG_NUM(reg)
862
0
               || (t->stack_map[t->exit_info[exit_point].stack_offset + var].flags & ~(ZREG_LOAD|ZREG_STORE|ZREG_LAST_USE)))) {
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
              t->stack_map[t->exit_info[exit_point].stack_offset + var].flags &= (ZREG_LOAD|ZREG_STORE|ZREG_LAST_USE);
869
0
            }
870
0
          } else {
871
0
            if ((exit_flags & ZEND_JIT_EXIT_FIXED)
872
0
             && (t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
873
0
             || t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_TYPE_ONLY)) {
874
0
              exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
875
0
              addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
876
0
              exit_flags &= ~ZEND_JIT_EXIT_FIXED;
877
0
            }
878
0
            t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = ZREG_NONE;
879
0
            t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_TYPE_ONLY;
880
0
          }
881
0
        } else if (!(exit_flags & ZEND_JIT_EXIT_FIXED)) {
882
0
          int32_t idx = _add_trace_const(t, ctx->ir_base[ref].val.i64);
883
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_CONST;
884
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].ref = idx;
885
0
        }
886
0
      }
887
0
    }
888
0
  }
889
0
  t->exit_info[exit_point].flags |= ZEND_JIT_EXIT_FIXED;
890
0
  return addr;
891
0
}
892
893
static void jit_SIDE_EXIT(zend_jit_ctx *jit, ir_ref addr)
894
0
{
895
0
  jit_SNAPSHOT(jit, addr);
896
0
  ir_IJMP(addr);
897
0
}
898
899
/* PHP JIT helpers */
900
901
static ir_ref jit_EMALLOC(zend_jit_ctx *jit, size_t size, const zend_op_array *op_array, const zend_op *opline)
902
0
{
903
0
#if ZEND_DEBUG
904
0
  return ir_CALL_5(IR_ADDR, ir_CONST_FC_FUNC(_emalloc),
905
0
    ir_CONST_ADDR(size),
906
0
    op_array->filename ? ir_CONST_ADDR(op_array->filename->val) : IR_NULL,
907
0
    ir_CONST_U32(opline ? opline->lineno : 0),
908
0
    IR_NULL,
909
0
    ir_CONST_U32(0));
910
#elif defined(HAVE_BUILTIN_CONSTANT_P)
911
  if (size > 24 && size <= 32) {
912
    return ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(_emalloc_32));
913
  } else {
914
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_emalloc), ir_CONST_ADDR(size));
915
  }
916
#else
917
  return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_emalloc), ir_CONST_ADDR(size));
918
#endif
919
0
}
920
921
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)
922
0
{
923
0
#if ZEND_DEBUG
924
0
  return ir_CALL_5(IR_ADDR, ir_CONST_FC_FUNC(_efree),
925
0
    ptr,
926
0
    op_array && op_array->filename ? ir_CONST_ADDR(op_array->filename->val) : IR_NULL,
927
0
    ir_CONST_U32(opline ? opline->lineno : 0),
928
0
    IR_NULL,
929
0
    ir_CONST_U32(0));
930
#elif defined(HAVE_BUILTIN_CONSTANT_P)
931
  if (size > 24 && size <= 32) {
932
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree_32), ptr);
933
  } else {
934
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree), ptr);
935
  }
936
#else
937
  return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree), ptr);
938
#endif
939
0
}
940
941
static ir_ref jit_FP(zend_jit_ctx *jit)
942
0
{
943
0
  ZEND_ASSERT(jit->ctx.control);
944
0
  if (jit->fp == IR_UNUSED) {
945
    /* Emit "RLOAD FP" once for basic block */
946
0
    jit->fp = ir_RLOAD_A(ZREG_FP);
947
0
  } else {
948
0
    ir_insn *insn;
949
0
    ir_ref ref = jit->ctx.control;
950
951
0
    while (1) {
952
0
      if (ref == jit->fp) {
953
0
        break;
954
0
      }
955
0
      insn = &jit->ctx.ir_base[ref];
956
0
      if (insn->op >= IR_START || insn->op == IR_CALL) {
957
0
        jit->fp = ir_RLOAD_A(ZREG_FP);
958
0
        break;
959
0
      }
960
0
      ref = insn->op1;
961
0
    }
962
0
  }
963
0
  return jit->fp;
964
0
}
965
966
static void jit_STORE_FP(zend_jit_ctx *jit, ir_ref ref)
967
0
{
968
0
  ir_RSTORE(ZREG_FP, ref);
969
0
  jit->fp = IR_UNUSED;
970
0
}
971
972
static ir_ref jit_IP(zend_jit_ctx *jit)
973
0
{
974
0
  return ir_RLOAD_A(ZREG_IP);
975
0
}
976
977
static void jit_STORE_IP(zend_jit_ctx *jit, ir_ref ref)
978
0
{
979
0
  ir_RSTORE(ZREG_IP, ref);
980
0
}
981
982
static ir_ref jit_IP32(zend_jit_ctx *jit)
983
0
{
984
0
  return ir_RLOAD_U32(ZREG_IP);
985
0
}
986
987
static void jit_LOAD_IP_ADDR(zend_jit_ctx *jit, const zend_op *target)
988
0
{
989
0
  jit_STORE_IP(jit, ir_CONST_ADDR(target));
990
0
}
991
992
static void zend_jit_track_last_valid_opline(zend_jit_ctx *jit)
993
0
{
994
0
  jit->use_last_valid_opline = false;
995
0
  jit->track_last_valid_opline = true;
996
0
}
997
998
static void zend_jit_use_last_valid_opline(zend_jit_ctx *jit)
999
0
{
1000
0
  if (jit->track_last_valid_opline) {
1001
0
    jit->use_last_valid_opline = true;
1002
0
    jit->track_last_valid_opline = false;
1003
0
  }
1004
0
}
1005
1006
static bool zend_jit_trace_uses_initial_ip(zend_jit_ctx *jit)
1007
0
{
1008
0
  return jit->use_last_valid_opline;
1009
0
}
1010
1011
static void zend_jit_set_last_valid_opline(zend_jit_ctx *jit, const zend_op *opline)
1012
0
{
1013
0
  if (!jit->reuse_ip) {
1014
0
    jit->track_last_valid_opline = true;
1015
0
    jit->last_valid_opline = opline;
1016
0
  }
1017
0
}
1018
1019
static void zend_jit_reset_last_valid_opline(zend_jit_ctx *jit)
1020
0
{
1021
0
  jit->track_last_valid_opline = false;
1022
0
  jit->last_valid_opline = NULL;
1023
0
}
1024
1025
static void zend_jit_start_reuse_ip(zend_jit_ctx *jit)
1026
0
{
1027
0
  zend_jit_reset_last_valid_opline(jit);
1028
0
  jit->reuse_ip = true;
1029
0
}
1030
1031
static int zend_jit_reuse_ip(zend_jit_ctx *jit)
1032
0
{
1033
0
  if (!jit->reuse_ip) {
1034
0
    zend_jit_start_reuse_ip(jit);
1035
    // RX = EX(call);
1036
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(call)));
1037
0
  }
1038
0
  return 1;
1039
0
}
1040
1041
static void zend_jit_stop_reuse_ip(zend_jit_ctx *jit)
1042
0
{
1043
0
  jit->reuse_ip = false;
1044
0
}
1045
1046
static int zend_jit_save_call_chain(zend_jit_ctx *jit, uint32_t call_level)
1047
0
{
1048
0
  ir_ref rx, call;
1049
1050
0
  if (call_level == 1) {
1051
    // JIT: call = NULL;
1052
0
    call = IR_NULL;
1053
0
  } else {
1054
    // JIT: call = EX(call);
1055
0
    call = ir_LOAD_A(jit_EX(call));
1056
0
  }
1057
1058
0
  rx = jit_IP(jit);
1059
1060
  // JIT: call->prev_execute_data = call;
1061
0
  ir_STORE(jit_CALL(rx, prev_execute_data), call);
1062
1063
  // JIT: EX(call) = call;
1064
0
  ir_STORE(jit_EX(call), rx);
1065
1066
0
  jit->delayed_call_level = 0;
1067
0
  delayed_call_chain = false;
1068
1069
0
  return 1;
1070
0
}
1071
1072
static int zend_jit_set_ip(zend_jit_ctx *jit, const zend_op *target)
1073
0
{
1074
0
  ir_ref ref;
1075
1076
0
  if (jit->delayed_call_level) {
1077
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
1078
0
      return 0;
1079
0
    }
1080
0
  }
1081
1082
0
  if (jit->last_valid_opline) {
1083
0
    zend_jit_use_last_valid_opline(jit);
1084
0
    if (jit->last_valid_opline != target) {
1085
0
      ref = jit_IP(jit);
1086
0
      if (target > jit->last_valid_opline) {
1087
0
        ref = ir_ADD_OFFSET(ref, (uintptr_t)target - (uintptr_t)jit->last_valid_opline);
1088
0
      } else {
1089
0
        ref = ir_SUB_A(ref, ir_CONST_ADDR((uintptr_t)jit->last_valid_opline - (uintptr_t)target));
1090
0
      }
1091
0
      jit_STORE_IP(jit, ref);
1092
0
    }
1093
0
  } else {
1094
0
    jit_STORE_IP(jit, ir_CONST_ADDR(target));
1095
0
  }
1096
0
  jit->reuse_ip = false;
1097
0
  zend_jit_set_last_valid_opline(jit, target);
1098
0
  return 1;
1099
0
}
1100
1101
static void jit_SET_EX_OPLINE(zend_jit_ctx *jit, const zend_op *target)
1102
0
{
1103
0
  if (jit->last_valid_opline == target) {
1104
0
    zend_jit_use_last_valid_opline(jit);
1105
    // EX(opline) = opline
1106
0
    ir_STORE(jit_EX(opline), jit_IP(jit));
1107
0
  } else {
1108
0
    ir_STORE(jit_EX(opline), ir_CONST_ADDR(target));
1109
0
  }
1110
0
}
1111
1112
static ir_ref jit_ZVAL_ADDR(zend_jit_ctx *jit, zend_jit_addr addr)
1113
0
{
1114
0
  if (Z_MODE(addr) == IS_MEM_ZVAL) {
1115
0
    ir_ref reg;
1116
1117
0
    if (Z_REG(addr) == ZREG_FP) {
1118
0
      reg = jit_FP(jit);
1119
0
    } else if (Z_REG(addr) == ZREG_RX) {
1120
0
      reg = jit_IP(jit);
1121
0
    } else {
1122
0
      ZEND_UNREACHABLE();
1123
0
    }
1124
0
    return ir_ADD_OFFSET(reg, Z_OFFSET(addr));
1125
0
  } else if (Z_MODE(addr) == IS_REF_ZVAL) {
1126
0
    return Z_IR_REF(addr);
1127
0
  } else {
1128
0
    ZEND_ASSERT(Z_MODE(addr) == IS_CONST_ZVAL);
1129
0
    return ir_CONST_ADDR(Z_ZV(addr));
1130
0
  }
1131
0
}
1132
1133
static ir_ref jit_Z_TYPE_ref(zend_jit_ctx *jit, ir_ref ref)
1134
0
{
1135
0
  return ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zval, u1.v.type)));
1136
0
}
1137
1138
static ir_ref jit_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr)
1139
0
{
1140
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1141
0
    return ir_CONST_U8(Z_TYPE_P(Z_ZV(addr)));
1142
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1143
0
    ir_ref reg;
1144
1145
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1146
0
    if (Z_REG(addr) == ZREG_FP) {
1147
0
      reg = jit_FP(jit);
1148
0
    } else if (Z_REG(addr) == ZREG_RX) {
1149
0
      reg = jit_IP(jit);
1150
0
    } else {
1151
0
      ZEND_UNREACHABLE();
1152
0
    }
1153
0
    return ir_LOAD_U8(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.v.type)));
1154
0
  } else {
1155
0
    return jit_Z_TYPE_ref(jit, jit_ZVAL_ADDR(jit, addr));
1156
0
  }
1157
0
}
1158
1159
static ir_ref jit_Z_TYPE_FLAGS_ref(zend_jit_ctx *jit, ir_ref ref)
1160
0
{
1161
0
  return ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zval, u1.v.type_flags)));
1162
0
}
1163
1164
static ir_ref jit_Z_TYPE_FLAGS(zend_jit_ctx *jit, zend_jit_addr addr)
1165
0
{
1166
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1167
0
    return ir_CONST_U8(Z_TYPE_FLAGS_P(Z_ZV(addr)));
1168
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1169
0
    ir_ref reg;
1170
1171
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1172
0
    if (Z_REG(addr) == ZREG_FP) {
1173
0
      reg = jit_FP(jit);
1174
0
    } else if (Z_REG(addr) == ZREG_RX) {
1175
0
      reg = jit_IP(jit);
1176
0
    } else {
1177
0
      ZEND_UNREACHABLE();
1178
0
    }
1179
0
    return ir_LOAD_U8(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.v.type_flags)));
1180
0
  } else {
1181
0
    return jit_Z_TYPE_FLAGS_ref(jit, jit_ZVAL_ADDR(jit, addr));
1182
0
  }
1183
0
}
1184
1185
static ir_ref jit_Z_TYPE_INFO_ref(zend_jit_ctx *jit, ir_ref ref)
1186
0
{
1187
0
  return ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zval, u1.type_info)));
1188
0
}
1189
1190
static ir_ref jit_Z_TYPE_INFO(zend_jit_ctx *jit, zend_jit_addr addr)
1191
0
{
1192
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1193
0
    return ir_CONST_U32(Z_TYPE_INFO_P(Z_ZV(addr)));
1194
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1195
0
    ir_ref reg;
1196
1197
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1198
0
    if (Z_REG(addr) == ZREG_FP) {
1199
0
      reg = jit_FP(jit);
1200
0
    } else if (Z_REG(addr) == ZREG_RX) {
1201
0
      reg = jit_IP(jit);
1202
0
    } else {
1203
0
      ZEND_UNREACHABLE();
1204
0
    }
1205
0
    return ir_LOAD_U32(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.type_info)));
1206
0
  } else {
1207
0
    return jit_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, addr));
1208
0
  }
1209
0
}
1210
1211
static void jit_set_Z_TYPE_INFO_ref(zend_jit_ctx *jit, ir_ref ref, ir_ref type_info)
1212
0
{
1213
0
  ir_STORE(ir_ADD_OFFSET(ref, offsetof(zval, u1.type_info)), type_info);
1214
0
}
1215
1216
static void jit_set_Z_TYPE_INFO_ex(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref type_info)
1217
0
{
1218
0
  if (Z_MODE(addr) == IS_MEM_ZVAL) {
1219
0
    ir_ref reg;
1220
1221
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1222
0
    if (Z_REG(addr) == ZREG_FP) {
1223
0
      reg = jit_FP(jit);
1224
0
    } else if (Z_REG(addr) == ZREG_RX) {
1225
0
      reg = jit_IP(jit);
1226
0
    } else {
1227
0
      ZEND_UNREACHABLE();
1228
0
    }
1229
0
    ir_STORE(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.type_info)), type_info);
1230
0
  } else {
1231
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, addr), type_info);
1232
0
  }
1233
0
}
1234
1235
static void jit_set_Z_TYPE_INFO(zend_jit_ctx *jit, zend_jit_addr addr, uint32_t type_info)
1236
0
{
1237
0
  if (type_info < IS_STRING
1238
0
   && Z_MODE(addr) == IS_MEM_ZVAL
1239
0
   && Z_REG(addr) == ZREG_FP
1240
0
   && JIT_G(current_frame)
1241
0
   && STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(addr))) == type_info) {
1242
    /* type is already set */
1243
0
    return;
1244
0
  }
1245
0
  jit_set_Z_TYPE_INFO_ex(jit, addr, ir_CONST_U32(type_info));
1246
0
}
1247
1248
static ir_ref jit_if_Z_TYPE_ref(zend_jit_ctx *jit, ir_ref ref, ir_ref type)
1249
0
{
1250
0
  return ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), type));
1251
0
}
1252
1253
static ir_ref jit_if_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type)
1254
0
{
1255
0
  ZEND_ASSERT(type != IS_UNDEF);
1256
0
  return ir_IF(ir_EQ(jit_Z_TYPE(jit, addr), ir_CONST_U8(type)));
1257
0
}
1258
1259
static ir_ref jit_if_not_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type)
1260
0
{
1261
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1262
1263
0
  if (type != IS_UNDEF) {
1264
0
    ref = ir_NE(ref, ir_CONST_U8(type));
1265
0
  }
1266
0
  return ir_IF(ref);
1267
0
}
1268
1269
static void jit_guard_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type, const void *exit_addr)
1270
0
{
1271
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1272
1273
0
  if (type != IS_UNDEF) {
1274
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(type)), ir_CONST_ADDR(exit_addr));
1275
0
  } else {
1276
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
1277
0
  }
1278
0
}
1279
1280
static void jit_guard_not_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type, const void *exit_addr)
1281
0
{
1282
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1283
1284
0
  if (type != IS_UNDEF) {
1285
0
    ref = ir_NE(ref, ir_CONST_U8(type));
1286
0
  }
1287
0
  ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
1288
0
}
1289
1290
static ir_ref jit_if_REFCOUNTED(zend_jit_ctx *jit, zend_jit_addr addr)
1291
0
{
1292
0
  return ir_IF(jit_Z_TYPE_FLAGS(jit, addr));
1293
0
}
1294
1295
static ir_ref jit_if_COLLECTABLE_ref(zend_jit_ctx *jit, ir_ref addr_ref)
1296
0
{
1297
0
  return ir_IF(ir_AND_U8(jit_Z_TYPE_FLAGS_ref(jit, addr_ref), ir_CONST_U8(IS_TYPE_COLLECTABLE)));
1298
0
}
1299
1300
static ir_ref jit_Z_LVAL_ref(zend_jit_ctx *jit, ir_ref ref)
1301
0
{
1302
0
  return ir_LOAD_L(ref);
1303
0
}
1304
1305
static ir_ref jit_Z_DVAL_ref(zend_jit_ctx *jit, ir_ref ref)
1306
0
{
1307
0
  return ir_LOAD_D(ref);
1308
0
}
1309
1310
static bool zend_jit_spilling_may_cause_conflict(zend_jit_ctx *jit, int var, ir_ref val)
1311
0
{
1312
0
  if (jit->ctx.ir_base[val].op == IR_RLOAD) {
1313
    /* Deoptimization */
1314
0
    return false;
1315
0
  }
1316
//  if (jit->ctx.ir_base[val].op == IR_LOAD
1317
//   && jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op == IR_ADD
1318
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op == IR_RLOAD
1319
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op2 == ZREG_FP
1320
//   && IR_IS_CONST_REF(jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2)
1321
//   && 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)) {
1322
//    /* LOAD from the same location (the LOAD is pinned) */
1323
//    // TODO: should be anti-dependent with the following stores ???
1324
//    return 0;
1325
//  }
1326
0
  if (jit->ssa->vars[var].var < jit->current_op_array->last_var) {
1327
    /* IS_CV */
1328
0
    if (jit->ctx.ir_base[val].op == IR_LOAD
1329
0
     && jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op == IR_ADD
1330
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op == IR_RLOAD
1331
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op2 == ZREG_FP
1332
0
     && IR_IS_CONST_REF(jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2)
1333
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)
1334
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) {
1335
      /* binding between different CVs may cause spill conflict */
1336
0
      return true;
1337
0
    } else if (jit->ssa->vars[var].definition >= 0
1338
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_def == var
1339
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_use >= 0
1340
0
     && jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].no_val
1341
0
     && jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].definition_phi
1342
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)) {
1343
      /* Avoid moving spill store out of loop */
1344
0
      return true;
1345
0
    } else if (jit->ssa->vars[var].definition >= 0
1346
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_def == var
1347
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_use >= 0
1348
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op2_use >= 0
1349
0
     && jit->ra[jit->ssa->ops[jit->ssa->vars[var].definition].op2_use].ref == val) {
1350
      /* Avoid spill conflict between of ASSIGN.op1_def and ASSIGN.op1_use */
1351
0
      return true;
1352
0
    }
1353
0
    return false;
1354
0
  }
1355
0
  return true;
1356
0
}
1357
1358
static void zend_jit_def_reg(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref val)
1359
0
{
1360
0
  int var;
1361
1362
0
  ZEND_ASSERT(Z_MODE(addr) == IS_REG);
1363
0
  var = Z_SSA_VAR(addr);
1364
0
  if (var == jit->delay_var) {
1365
0
    ir_refs_add(jit->delay_refs, val);
1366
0
    return;
1367
0
  }
1368
0
  ZEND_ASSERT(jit->ra && jit->ra[var].ref == IR_NULL);
1369
1370
  /* Negative "var" has special meaning for IR */
1371
0
  if (val > 0) {
1372
0
    if (jit->ctx.binding) {
1373
0
      ir_ref old = ir_binding_find(&jit->ctx, val);
1374
0
      if (old && old != -EX_NUM_TO_VAR(jit->ssa->vars[var].var)) {
1375
0
        val = ir_emit2(&jit->ctx, IR_OPT(IR_COPY, jit->ctx.ir_base[val].type), val, 1);
1376
0
      }
1377
0
    }
1378
0
    if (!zend_jit_spilling_may_cause_conflict(jit, var, val)) {
1379
0
      val = ir_bind(&jit->ctx, -EX_NUM_TO_VAR(jit->ssa->vars[var].var), val);
1380
0
    }
1381
0
  }
1382
0
  jit->ra[var].ref = val;
1383
1384
0
  if (jit->ra[var].flags & ZREG_FORWARD) {
1385
0
    zend_ssa_phi *phi = jit->ssa->vars[var].phi_use_chain;
1386
0
    zend_basic_block *bb;
1387
0
    int n, j, *p;
1388
0
    ir_ref *q;
1389
1390
0
    jit->ra[var].flags &= ~ZREG_FORWARD;
1391
0
    while (phi != NULL) {
1392
0
      zend_ssa_phi *dst_phi = phi;
1393
0
      int src_var = var;
1394
1395
0
      if (dst_phi->pi >= 0) {
1396
0
        jit->ra[src_var].ref = val;
1397
0
        src_var = dst_phi->ssa_var;
1398
0
        if (!(jit->ra[src_var].flags & ZREG_FORWARD)) {
1399
0
          phi = zend_ssa_next_use_phi(jit->ssa, var, phi);
1400
0
          continue;
1401
0
        }
1402
0
        dst_phi = jit->ssa->vars[src_var].phi_use_chain;
1403
0
        ZEND_ASSERT(dst_phi != NULL && "reg forwarding");
1404
0
        ZEND_ASSERT(!zend_ssa_next_use_phi(jit->ssa, src_var, dst_phi) && "reg forwarding");
1405
0
        jit->ra[src_var].flags &= ~ZREG_FORWARD;
1406
0
      }
1407
1408
0
      if (jit->ra[dst_phi->ssa_var].ref > 0) {
1409
0
        ir_insn *phi_insn = &jit->ctx.ir_base[jit->ra[dst_phi->ssa_var].ref];
1410
0
        if (phi_insn->op == IR_PHI) {
1411
//          ZEND_ASSERT(ir_operands_count(ctx, phi_insn) == n + 1);
1412
0
          bb = &jit->ssa->cfg.blocks[dst_phi->block];
1413
0
          n = bb->predecessors_count;
1414
0
          for (j = 0, p = &dst_phi->sources[0], q = phi_insn->ops + 2; j < n; j++, p++, q++) {
1415
0
            if (*p == src_var) {
1416
0
              *q = val;
1417
0
            }
1418
0
          }
1419
0
        }
1420
0
      }
1421
1422
0
      phi = zend_ssa_next_use_phi(jit->ssa, var, phi);
1423
0
    }
1424
0
  }
1425
0
}
1426
1427
static ir_ref zend_jit_use_reg(zend_jit_ctx *jit, zend_jit_addr addr)
1428
0
{
1429
0
  int var = Z_SSA_VAR(addr);
1430
1431
0
  ZEND_ASSERT(Z_MODE(addr) == IS_REG);
1432
0
  ZEND_ASSERT(jit->ra && jit->ra[var].ref);
1433
0
  if (jit->ra[var].ref == IR_NULL) {
1434
0
    zend_jit_addr mem_addr;
1435
0
    ir_ref ref;
1436
1437
0
    ZEND_ASSERT(jit->ra[var].flags & ZREG_LOAD);
1438
0
    mem_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(jit->ssa->vars[var].var));
1439
0
    if ((jit->ssa->var_info[var].type & MAY_BE_ANY) == MAY_BE_LONG) {
1440
0
      ref = jit_Z_LVAL_ref(jit, jit_ZVAL_ADDR(jit, mem_addr));
1441
0
    } else if ((jit->ssa->var_info[var].type & MAY_BE_ANY) == MAY_BE_DOUBLE) {
1442
0
      ref = jit_Z_DVAL_ref(jit, jit_ZVAL_ADDR(jit, mem_addr));
1443
0
    } else {
1444
0
      ZEND_UNREACHABLE();
1445
0
    }
1446
0
    zend_jit_def_reg(jit, addr, ref);
1447
0
    return ref;
1448
0
  }
1449
0
  return jit->ra[Z_SSA_VAR(addr)].ref;
1450
0
}
1451
1452
static void zend_jit_gen_pi(zend_jit_ctx *jit, zend_ssa_phi *phi)
1453
0
{
1454
0
  int src_var = phi->sources[0];
1455
0
  int dst_var = phi->ssa_var;
1456
1457
0
  ZEND_ASSERT(phi->pi >= 0);
1458
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
1459
0
  ZEND_ASSERT(jit->ra[src_var].ref);
1460
1461
0
  if (jit->ra[src_var].ref == IR_NULL) {
1462
    /* Not defined yet */
1463
0
    if (jit->ssa->vars[dst_var].use_chain < 0
1464
0
     && jit->ssa->vars[dst_var].phi_use_chain) {
1465
0
      zend_ssa_phi *phi = jit->ssa->vars[dst_var].phi_use_chain;
1466
0
      if (!zend_ssa_next_use_phi(jit->ssa, dst_var, phi)) {
1467
        /* This is a Pi forwarded to Phi */
1468
0
        jit->ra[src_var].flags |= ZREG_FORWARD;
1469
0
        return;
1470
0
      }
1471
0
    }
1472
0
    ZEND_ASSERT(0 && "Not defined Pi source");
1473
0
  }
1474
  /* Reuse register */
1475
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var),
1476
0
    zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var)));
1477
0
}
1478
1479
static void zend_jit_gen_phi(zend_jit_ctx *jit, zend_ssa_phi *phi)
1480
0
{
1481
0
  int dst_var = phi->ssa_var;
1482
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[phi->block];
1483
0
  uint32_t n = bb->predecessors_count;
1484
0
  ir_type type = (jit->ssa->var_info[phi->ssa_var].type & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE;
1485
0
  ir_ref merge = jit->bb_start_ref[phi->block];
1486
0
  ir_ref ref;
1487
0
  ir_ref old_insns_count = jit->ctx.insns_count;
1488
0
  ir_ref same_src_ref = IR_UNUSED;
1489
0
  bool phi_inputs_are_the_same = true;
1490
1491
0
  ZEND_ASSERT(phi->pi < 0);
1492
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
1493
0
  ZEND_ASSERT(merge);
1494
0
  ZEND_ASSERT(jit->ctx.ir_base[merge].op == IR_MERGE || jit->ctx.ir_base[merge].op == IR_LOOP_BEGIN);
1495
0
  ZEND_ASSERT(n == jit->ctx.ir_base[merge].inputs_count);
1496
1497
0
  ref = ir_emit_N(&jit->ctx, IR_OPT(IR_PHI, type), n + 1);
1498
0
  ir_set_op(&jit->ctx, ref, 1, merge);
1499
1500
0
  for (uint32_t i = 0; i < n; i++) {
1501
0
    int src_var = phi->sources[i];
1502
1503
0
    ZEND_ASSERT(jit->ra[src_var].ref);
1504
0
    if (jit->ra[src_var].ref == IR_NULL) {
1505
0
      jit->ra[src_var].flags |= ZREG_FORWARD;
1506
0
      phi_inputs_are_the_same = false;
1507
0
    } else {
1508
0
      ir_ref src_ref = zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var));
1509
0
      if (i == 0) {
1510
0
        same_src_ref = src_ref;
1511
0
      } else if (same_src_ref != src_ref) {
1512
0
        phi_inputs_are_the_same = false;
1513
0
      }
1514
0
      ir_set_op(&jit->ctx, ref, i + 2, src_ref);
1515
0
    }
1516
0
  }
1517
0
  if (phi_inputs_are_the_same) {
1518
0
    ref = same_src_ref;
1519
0
    jit->ctx.insns_count = old_insns_count;
1520
0
  }
1521
1522
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var), ref);
1523
0
}
1524
1525
static ir_ref jit_Z_LVAL(zend_jit_ctx *jit, zend_jit_addr addr)
1526
0
{
1527
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1528
0
    return ir_CONST_LONG(Z_LVAL_P(Z_ZV(addr)));
1529
0
  } else if (Z_MODE(addr) == IS_REG) {
1530
0
    return zend_jit_use_reg(jit, addr);
1531
0
  } else {
1532
0
    return jit_Z_LVAL_ref(jit, jit_ZVAL_ADDR(jit, addr));
1533
0
  }
1534
0
}
1535
1536
static void jit_set_Z_LVAL(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref lval)
1537
0
{
1538
0
  if (Z_MODE(addr) == IS_REG) {
1539
0
    zend_jit_def_reg(jit, addr, lval);
1540
0
  } else {
1541
0
    ir_STORE(jit_ZVAL_ADDR(jit, addr), lval);
1542
0
  }
1543
0
}
1544
1545
#if SIZEOF_ZEND_LONG == 4
1546
static ir_ref jit_Z_W2(zend_jit_ctx *jit, zend_jit_addr addr)
1547
{
1548
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1549
    return ir_CONST_U32((Z_ZV(addr))->value.ww.w2);
1550
  } else {
1551
    return ir_LOAD_L(ir_ADD_OFFSET(jit_ZVAL_ADDR(jit, addr), offsetof(zval, value.ww.w2)));
1552
  }
1553
}
1554
1555
static void jit_set_Z_W2(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref lval)
1556
{
1557
  ir_STORE(ir_ADD_OFFSET(jit_ZVAL_ADDR(jit, addr), offsetof(zval, value.ww.w2)), lval);
1558
}
1559
#endif
1560
1561
static ir_ref jit_Z_DVAL(zend_jit_ctx *jit, zend_jit_addr addr)
1562
0
{
1563
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1564
0
    return ir_CONST_DOUBLE(Z_DVAL_P(Z_ZV(addr)));
1565
0
  } else if (Z_MODE(addr) == IS_REG) {
1566
0
    return zend_jit_use_reg(jit, addr);
1567
0
  } else {
1568
0
    return jit_Z_DVAL_ref(jit, jit_ZVAL_ADDR(jit, addr));
1569
0
  }
1570
0
}
1571
1572
static void jit_set_Z_DVAL(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref dval)
1573
0
{
1574
0
  if (Z_MODE(addr) == IS_REG) {
1575
0
    zend_jit_def_reg(jit, addr, dval);
1576
0
  } else {
1577
0
    ir_STORE(jit_ZVAL_ADDR(jit, addr), dval);
1578
0
  }
1579
0
}
1580
1581
static ir_ref jit_Z_PTR_ref(zend_jit_ctx *jit, ir_ref ref)
1582
0
{
1583
0
  return ir_LOAD_A(ref);
1584
0
}
1585
1586
static ir_ref jit_Z_PTR(zend_jit_ctx *jit, zend_jit_addr addr)
1587
0
{
1588
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1589
0
    return ir_CONST_ADDR(Z_PTR_P(Z_ZV(addr)));
1590
0
  } else {
1591
0
    return jit_Z_PTR_ref(jit, jit_ZVAL_ADDR(jit, addr));
1592
0
  }
1593
0
}
1594
1595
static void jit_set_Z_PTR(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref ptr)
1596
0
{
1597
0
  ir_STORE(jit_ZVAL_ADDR(jit, addr), ptr);
1598
0
}
1599
1600
static ir_ref jit_GC_REFCOUNT(zend_jit_ctx *jit, ir_ref ref)
1601
0
{
1602
0
  return ir_LOAD_U32(ref);
1603
0
}
1604
1605
static void jit_set_GC_REFCOUNT(zend_jit_ctx *jit, ir_ref ref, uint32_t refcount)
1606
0
{
1607
0
  ir_STORE(ref, ir_CONST_U32(refcount));
1608
0
}
1609
1610
static void jit_GC_ADDREF(zend_jit_ctx *jit, ir_ref ref)
1611
0
{
1612
0
  ir_STORE(ref, ir_ADD_U32(ir_LOAD_U32(ref), ir_CONST_U32(1)));
1613
0
}
1614
1615
static void jit_GC_ADDREF2(zend_jit_ctx *jit, ir_ref ref)
1616
0
{
1617
0
  ir_ref counter = ir_LOAD_U32(ref);
1618
0
  ir_STORE(ref, ir_ADD_U32(counter, ir_CONST_U32(2)));
1619
0
}
1620
1621
static ir_ref jit_GC_DELREF(zend_jit_ctx *jit, ir_ref ref)
1622
0
{
1623
0
  ir_ref counter = ir_LOAD_U32(ref);
1624
0
  counter = ir_SUB_U32(counter, ir_CONST_U32(1));
1625
0
  ir_STORE(ref, counter);
1626
0
  return counter;
1627
0
}
1628
1629
static ir_ref jit_if_GC_MAY_NOT_LEAK(zend_jit_ctx *jit, ir_ref ref)
1630
0
{
1631
0
  return ir_IF(
1632
0
    ir_AND_U32(
1633
0
      ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_refcounted, gc.u.type_info))),
1634
0
      ir_CONST_U32(GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))));
1635
0
}
1636
1637
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)
1638
0
{
1639
0
  ir_ref ref = IR_UNUSED;
1640
1641
0
  if (Z_TYPE_P(zv) > IS_TRUE) {
1642
0
    if (Z_TYPE_P(zv) == IS_DOUBLE) {
1643
0
      jit_set_Z_DVAL(jit, dst, ir_CONST_DOUBLE(Z_DVAL_P(zv)));
1644
0
    } else if (Z_TYPE_P(zv) == IS_LONG && dst_def_info == MAY_BE_DOUBLE) {
1645
0
      jit_set_Z_DVAL(jit, dst, ir_CONST_DOUBLE((double)Z_LVAL_P(zv)));
1646
0
    } else if (Z_TYPE_P(zv) == IS_LONG) {
1647
0
      jit_set_Z_LVAL(jit, dst, ir_CONST_LONG(Z_LVAL_P(zv)));
1648
0
    } else {
1649
0
      ref = ir_CONST_ADDR(Z_PTR_P(zv));
1650
0
      jit_set_Z_PTR(jit, dst, ref);
1651
0
      if (addref && Z_REFCOUNTED_P(zv)) {
1652
0
        jit_GC_ADDREF(jit, ref);
1653
0
      }
1654
0
    }
1655
0
  }
1656
0
  if (Z_MODE(dst) != IS_REG) {
1657
0
    if (dst_def_info == MAY_BE_DOUBLE) {
1658
0
      if ((dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
1659
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
1660
0
      }
1661
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) {
1662
0
      jit_set_Z_TYPE_INFO(jit, dst, Z_TYPE_INFO_P(zv));
1663
0
    }
1664
0
  }
1665
0
}
1666
1667
static ir_ref jit_if_TYPED_REF(zend_jit_ctx *jit, ir_ref ref)
1668
0
{
1669
0
  return ir_IF(ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_reference, sources.ptr))));
1670
0
}
1671
1672
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)
1673
0
{
1674
0
  ir_ref ref = IR_UNUSED;
1675
1676
0
  if (src_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
1677
0
    if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_LONG) {
1678
0
      jit_set_Z_LVAL(jit, dst, jit_Z_LVAL(jit, src));
1679
0
    } else if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_DOUBLE) {
1680
0
      jit_set_Z_DVAL(jit, dst, jit_Z_DVAL(jit, src));
1681
0
    } else {
1682
#if SIZEOF_ZEND_LONG == 4
1683
      if (src_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) {
1684
        jit_set_Z_W2(jit, dst, jit_Z_W2(jit, src));
1685
      }
1686
#endif
1687
0
      ref = jit_Z_PTR(jit, src);
1688
0
      jit_set_Z_PTR(jit, dst, ref);
1689
0
    }
1690
0
  }
1691
0
  if (has_concrete_type(src_info & MAY_BE_ANY)
1692
0
   && (src_info & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))
1693
0
     && !(src_info & MAY_BE_GUARD)) {
1694
0
    if (Z_MODE(dst) != IS_REG
1695
0
     && (dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (src_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD))) {
1696
0
      uint8_t type = concrete_type(src_info);
1697
0
      jit_set_Z_TYPE_INFO(jit, dst, type);
1698
0
    }
1699
0
  } else {
1700
0
    ir_ref type = jit_Z_TYPE_INFO(jit, src);
1701
0
    jit_set_Z_TYPE_INFO_ex(jit, dst, type);
1702
0
    if (addref) {
1703
0
      if (src_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
1704
0
        ir_ref if_refcounted = IR_UNUSED;
1705
1706
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1707
0
          if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(0xff00)));
1708
0
          ir_IF_TRUE(if_refcounted);
1709
0
        }
1710
1711
0
        jit_GC_ADDREF(jit, ref);
1712
1713
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1714
0
          ir_MERGE_WITH_EMPTY_FALSE(if_refcounted);
1715
0
        }
1716
0
      }
1717
0
    }
1718
0
  }
1719
0
}
1720
1721
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)
1722
0
{
1723
0
  ir_ref ref = IR_UNUSED;
1724
1725
0
  if (src_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
1726
0
    if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_LONG) {
1727
0
      ref = jit_Z_LVAL(jit, src);
1728
0
      jit_set_Z_LVAL(jit, dst, ref);
1729
0
      jit_set_Z_LVAL(jit, dst2, ref);
1730
0
    } else if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_DOUBLE) {
1731
0
      ref = jit_Z_DVAL(jit, src);
1732
0
      jit_set_Z_DVAL(jit, dst, ref);
1733
0
      jit_set_Z_DVAL(jit, dst2, ref);
1734
0
    } else {
1735
#if SIZEOF_ZEND_LONG == 4
1736
      if (src_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) {
1737
        ref = jit_Z_W2(jit, src);
1738
        jit_set_Z_W2(jit, dst, ref);
1739
        jit_set_Z_W2(jit, dst2, ref);
1740
      }
1741
#endif
1742
0
      ref = jit_Z_PTR(jit, src);
1743
0
      jit_set_Z_PTR(jit, dst, ref);
1744
0
      jit_set_Z_PTR(jit, dst2, ref);
1745
0
    }
1746
0
  }
1747
0
  if (has_concrete_type(src_info & MAY_BE_ANY)
1748
0
   && (src_info & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))
1749
0
     && !(src_info & MAY_BE_GUARD)) {
1750
0
    uint8_t type = concrete_type(src_info);
1751
0
    ir_ref type_ref = ir_CONST_U32(type);
1752
1753
0
    if (Z_MODE(dst) != IS_REG
1754
0
     && (dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (src_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD))) {
1755
0
      jit_set_Z_TYPE_INFO_ex(jit, dst, type_ref);
1756
0
    }
1757
0
    if (Z_MODE(dst2) != IS_REG) {
1758
0
      jit_set_Z_TYPE_INFO_ex(jit, dst2, type_ref);
1759
0
    }
1760
0
  } else {
1761
0
    ir_ref type = jit_Z_TYPE_INFO(jit, src);
1762
0
    jit_set_Z_TYPE_INFO_ex(jit, dst, type);
1763
0
    jit_set_Z_TYPE_INFO_ex(jit, dst2, type);
1764
0
    if (addref) {
1765
0
      if (src_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
1766
0
        ir_ref if_refcounted = IR_UNUSED;
1767
1768
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1769
0
          if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(0xff00)));
1770
0
          ir_IF_TRUE(if_refcounted);
1771
0
        }
1772
1773
0
        if (addref == 2) {
1774
0
          jit_GC_ADDREF2(jit, ref);
1775
0
        } else {
1776
0
          jit_GC_ADDREF(jit, ref);
1777
0
        }
1778
1779
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1780
0
          ir_MERGE_WITH_EMPTY_FALSE(if_refcounted);
1781
0
        }
1782
0
      }
1783
0
    }
1784
0
  }
1785
0
}
1786
1787
static void jit_ZVAL_DTOR(zend_jit_ctx *jit, ir_ref ref, uint32_t op_info, const zend_op *opline)
1788
0
{
1789
0
  if (!((op_info) & MAY_BE_GUARD)
1790
0
   && has_concrete_type((op_info) & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1791
0
    uint8_t type = concrete_type((op_info) & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
1792
0
    if (type == IS_STRING && !ZEND_DEBUG) {
1793
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(_efree), ref);
1794
0
        return;
1795
0
    } else if (type == IS_ARRAY) {
1796
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)) {
1797
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))) {
1798
0
          jit_SET_EX_OPLINE(jit, opline);
1799
0
        }
1800
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_array_destroy), ref);
1801
0
      } else {
1802
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_array_free), ref);
1803
0
      }
1804
0
      return;
1805
0
    } else if (type == IS_OBJECT) {
1806
0
      if (opline) {
1807
0
        jit_SET_EX_OPLINE(jit, opline);
1808
0
      }
1809
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_objects_store_del), ref);
1810
0
      return;
1811
0
    }
1812
0
  }
1813
0
  if (opline) {
1814
0
    jit_SET_EX_OPLINE(jit, opline);
1815
0
  }
1816
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(rc_dtor_func), ref);
1817
0
}
1818
1819
static void jit_ZVAL_PTR_DTOR(zend_jit_ctx  *jit,
1820
                              zend_jit_addr  addr,
1821
                              uint32_t       op_info,
1822
                              bool           gc,
1823
                              const zend_op *opline)
1824
0
{
1825
0
    ir_ref ref, ref2;
1826
0
  ir_ref if_refcounted = IR_UNUSED;
1827
0
  ir_ref if_not_zero = IR_UNUSED;
1828
0
  ir_ref end_inputs = IR_UNUSED;
1829
1830
0
  if (op_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF|MAY_BE_GUARD)) {
1831
0
    if ((op_info) & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_INDIRECT|MAY_BE_GUARD)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1832
0
      if_refcounted = jit_if_REFCOUNTED(jit, addr);
1833
0
      ir_IF_FALSE(if_refcounted);
1834
0
      ir_END_list(end_inputs);
1835
0
      ir_IF_TRUE(if_refcounted);
1836
0
    }
1837
0
    ref = jit_Z_PTR(jit, addr);
1838
0
    ref2 = jit_GC_DELREF(jit, ref);
1839
1840
0
    if (((op_info) & MAY_BE_GUARD) || RC_MAY_BE_1(op_info)) {
1841
0
      if (((op_info) & MAY_BE_GUARD) || RC_MAY_BE_N(op_info)) {
1842
0
        if_not_zero = ir_IF(ref2);
1843
0
        ir_IF_FALSE(if_not_zero);
1844
0
      }
1845
      // zval_dtor_func(r);
1846
0
      jit_ZVAL_DTOR(jit, ref, op_info, opline);
1847
0
      if (if_not_zero) {
1848
0
        ir_END_list(end_inputs);
1849
0
        ir_IF_TRUE(if_not_zero);
1850
0
      }
1851
0
    }
1852
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))) {
1853
0
      ir_ref if_may_not_leak;
1854
1855
0
      if ((op_info) & (MAY_BE_REF|MAY_BE_GUARD)) {
1856
0
        ir_ref if_ref, if_collectable;
1857
1858
0
        if_ref = jit_if_Z_TYPE(jit, addr, IS_REFERENCE);
1859
0
        ir_IF_TRUE(if_ref);
1860
1861
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
1862
1863
0
        if_collectable = jit_if_COLLECTABLE_ref(jit, ref2);
1864
0
        ir_IF_FALSE(if_collectable);
1865
0
        ir_END_list(end_inputs);
1866
0
        ir_IF_TRUE(if_collectable);
1867
1868
0
        ref2 = jit_Z_PTR_ref(jit, ref2);
1869
1870
0
        ir_MERGE_WITH_EMPTY_FALSE(if_ref);
1871
0
        ref = ir_PHI_2(IR_ADDR, ref2, ref);
1872
0
      }
1873
1874
0
      if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
1875
0
      ir_IF_TRUE(if_may_not_leak);
1876
0
      ir_END_list(end_inputs);
1877
0
      ir_IF_FALSE(if_may_not_leak);
1878
1879
0
      if (opline) {
1880
0
        jit_SET_EX_OPLINE(jit, opline);
1881
0
      }
1882
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
1883
0
    }
1884
1885
0
    if (end_inputs) {
1886
0
      ir_END_list(end_inputs);
1887
0
      ir_MERGE_list(end_inputs);
1888
0
    }
1889
0
  }
1890
0
}
1891
1892
static void jit_FREE_OP(zend_jit_ctx  *jit,
1893
                        uint8_t        op_type,
1894
                        znode_op       op,
1895
                        uint32_t       op_info,
1896
                        const zend_op *opline)
1897
0
{
1898
0
  if (op_type & (IS_VAR|IS_TMP_VAR)) {
1899
0
    jit_ZVAL_PTR_DTOR(jit,
1900
0
      ZEND_ADDR_MEM_ZVAL(ZREG_FP, op.var),
1901
0
      op_info, false, opline);
1902
0
  }
1903
0
}
1904
1905
static void jit_OBJ_RELEASE(zend_jit_ctx  *jit, ir_ref ref)
1906
0
{
1907
0
  ir_ref end_inputs = IR_UNUSED;
1908
0
    ir_ref if_not_zero, if_may_not_leak;
1909
1910
  // JIT: if (GC_DELREF(obj) == 0) {
1911
0
  if_not_zero = ir_IF(jit_GC_DELREF(jit, ref));
1912
0
  ir_IF_FALSE(if_not_zero);
1913
1914
  // JIT: zend_objects_store_del(obj)
1915
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_objects_store_del), ref);
1916
0
  ir_END_list(end_inputs);
1917
1918
0
  ir_IF_TRUE(if_not_zero);
1919
0
  if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
1920
1921
0
  ir_IF_TRUE(if_may_not_leak);
1922
0
  ir_END_list(end_inputs);
1923
1924
0
  ir_IF_FALSE(if_may_not_leak);
1925
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
1926
0
  ir_END_list(end_inputs);
1927
1928
0
  ir_MERGE_list(end_inputs);
1929
0
}
1930
1931
static void zend_jit_check_timeout(zend_jit_ctx *jit, const zend_op *opline, const void *exit_addr)
1932
0
{
1933
0
  ir_ref ref = ir_LOAD_U8(jit_EG(vm_interrupt));
1934
1935
0
  if (exit_addr) {
1936
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
1937
0
  } else if (!opline || jit->last_valid_opline == opline) {
1938
0
    ir_GUARD_NOT(ref, jit_STUB_ADDR(jit, jit_stub_interrupt_handler));
1939
0
  } else {
1940
0
    ir_ref if_timeout = ir_IF(ref);
1941
1942
0
    ir_IF_TRUE_cold(if_timeout);
1943
0
    jit_LOAD_IP_ADDR(jit, opline);
1944
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_interrupt_handler));
1945
0
    ir_IF_FALSE(if_timeout);
1946
0
  }
1947
0
}
1948
1949
static void zend_jit_vm_enter(zend_jit_ctx *jit, ir_ref to_opline)
1950
0
{
1951
  // ZEND_VM_ENTER()
1952
0
  ir_RETURN(ir_OR_A(to_opline, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)));
1953
0
}
1954
1955
static void zend_jit_vm_leave(zend_jit_ctx *jit, ir_ref to_opline)
1956
0
{
1957
0
  // ZEND_VM_LEAVE()
1958
0
  ir_RETURN(ir_OR_A(to_opline, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)));
1959
0
}
1960
1961
static void zend_jit_tailcall_handler(zend_jit_ctx *jit, ir_ref handler)
1962
0
{
1963
#if defined(IR_TARGET_X86)
1964
  if (!IR_IS_CONST_REF(handler)) {
1965
    handler = ir_CAST_OPCODE_HANDLER_FUNC(handler);
1966
  }
1967
#endif
1968
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
1969
0
    ir_TAILCALL(IR_OPCODE_HANDLER_RET, handler);
1970
0
  } else {
1971
0
    ir_TAILCALL_2(IR_ADDR, handler, jit_FP(jit), jit_IP(jit));
1972
0
  }
1973
0
}
1974
1975
/* stubs */
1976
1977
static int zend_jit_exception_handler_stub(zend_jit_ctx *jit)
1978
0
{
1979
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
1980
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(EG(exception_op));
1981
1982
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
1983
0
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
1984
0
  } else {
1985
0
    zend_vm_opcode_handler_t handler = EG(exception_op)->handler;
1986
1987
0
    if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
1988
0
      zend_jit_tailcall_handler(jit, ir_CONST_OPCODE_HANDLER_FUNC(handler));
1989
0
    } else {
1990
0
      ir_ref ref = ir_CALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
1991
0
      zend_jit_vm_enter(jit, ref);
1992
0
    }
1993
0
  }
1994
0
  return 1;
1995
0
}
1996
1997
static int zend_jit_exception_handler_undef_stub(zend_jit_ctx *jit)
1998
0
{
1999
0
  ir_ref ref, result_type, if_result_used;
2000
2001
0
  ref = jit_EG(opline_before_exception);
2002
0
  result_type = ir_LOAD_U8(ir_ADD_OFFSET(ir_LOAD_A(ref), offsetof(zend_op, result_type)));
2003
2004
0
  if_result_used = ir_IF(ir_AND_U8(result_type, ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2005
0
  ir_IF_TRUE(if_result_used);
2006
2007
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(ref), offsetof(zend_op, result.var)));
2008
0
  if (sizeof(void*) == 8) {
2009
0
    ref = ir_ZEXT_A(ref);
2010
0
  }
2011
0
  ir_STORE(ir_ADD_OFFSET(ir_ADD_A(jit_FP(jit), ref), offsetof(zval, u1.type_info)), ir_CONST_U32(IS_UNDEF));
2012
0
  ir_MERGE_WITH_EMPTY_FALSE(if_result_used);
2013
2014
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2015
2016
0
  return 1;
2017
0
}
2018
2019
static int zend_jit_exception_handler_free_op1_op2_stub(zend_jit_ctx *jit)
2020
0
{
2021
0
  ir_ref ref, if_dtor;
2022
0
  zend_jit_addr var_addr;
2023
2024
0
  ref = ir_LOAD_A(jit_EG(opline_before_exception));
2025
0
  if_dtor = ir_IF(ir_AND_U8(ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zend_op, op1_type))),
2026
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2027
0
  ir_IF_TRUE(if_dtor);
2028
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op1.var)));
2029
0
  if (sizeof(void*) == 8) {
2030
0
    ref = ir_ZEXT_A(ref);
2031
0
  }
2032
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2033
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
2034
0
  jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2035
0
  ir_MERGE_WITH_EMPTY_FALSE(if_dtor);
2036
2037
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
2038
2039
0
  return 1;
2040
0
}
2041
2042
static int zend_jit_exception_handler_free_op2_stub(zend_jit_ctx *jit)
2043
0
{
2044
0
  ir_ref ref, if_dtor;
2045
0
  zend_jit_addr var_addr;
2046
2047
0
  ref = ir_LOAD_A(jit_EG(opline_before_exception));
2048
0
  if_dtor = ir_IF(ir_AND_U8(ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zend_op, op2_type))),
2049
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2050
0
  ir_IF_TRUE(if_dtor);
2051
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.var)));
2052
0
  if (sizeof(void*) == 8) {
2053
0
    ref = ir_ZEXT_A(ref);
2054
0
  }
2055
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2056
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
2057
0
  jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2058
0
  ir_MERGE_WITH_EMPTY_FALSE(if_dtor);
2059
2060
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
2061
2062
0
  return 1;
2063
0
}
2064
2065
static int zend_jit_interrupt_handler_stub(zend_jit_ctx *jit)
2066
0
{
2067
0
  ir_ref if_timeout, if_exception;
2068
2069
  // EX(opline) = opline
2070
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2071
2072
0
  ir_STORE(jit_EG(vm_interrupt), ir_CONST_U8(0));
2073
0
  if_timeout = ir_IF(ir_EQ(ir_LOAD_U8(jit_EG(timed_out)), ir_CONST_U8(0)));
2074
0
  ir_IF_FALSE(if_timeout);
2075
0
  ir_CALL(IR_VOID, ir_CONST_FUNC(zend_timeout));
2076
0
  ir_MERGE_WITH_EMPTY_TRUE(if_timeout);
2077
2078
0
  if (zend_interrupt_function) {
2079
0
    ir_CALL_1(IR_VOID, ir_CONST_FUNC(zend_interrupt_function), jit_FP(jit));
2080
0
    if_exception = ir_IF(ir_LOAD_A(jit_EG(exception)));
2081
0
    ir_IF_TRUE(if_exception);
2082
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(zend_jit_exception_in_interrupt_handler_helper));
2083
0
    ir_MERGE_WITH_EMPTY_FALSE(if_exception);
2084
2085
0
    jit_STORE_FP(jit, ir_LOAD_A(jit_EG(current_execute_data)));
2086
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2087
0
  }
2088
2089
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2090
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2091
0
  } else {
2092
0
    zend_jit_vm_enter(jit, jit_IP(jit));
2093
0
  }
2094
0
  return 1;
2095
0
}
2096
2097
static int zend_jit_leave_function_handler_stub(zend_jit_ctx *jit)
2098
0
{
2099
0
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
2100
0
  ir_TAILCALL(IR_OPCODE_HANDLER_RET, ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_leave_func_helper_tailcall));
2101
0
  return 1;
2102
#else
2103
  ir_ref call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
2104
  ir_ref if_top = ir_IF(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_TOP)));
2105
2106
  ir_IF_FALSE(if_top);
2107
2108
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2109
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), call_info);
2110
    jit_STORE_IP(jit,
2111
      ir_LOAD_A(jit_EX(opline)));
2112
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2113
  } else if (GCC_GLOBAL_REGS) {
2114
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), call_info);
2115
  } else {
2116
    ir_TAILCALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), jit_FP(jit), jit_IP(jit), call_info);
2117
  }
2118
2119
  ir_IF_TRUE(if_top);
2120
2121
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2122
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), call_info);
2123
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2124
  } else if (GCC_GLOBAL_REGS) {
2125
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), call_info);
2126
  } else {
2127
    ir_TAILCALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), jit_FP(jit), jit_IP(jit), call_info);
2128
  }
2129
2130
  return 1;
2131
#endif /* ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL */
2132
0
}
2133
2134
static int zend_jit_negative_shift_stub(zend_jit_ctx *jit)
2135
0
{
2136
0
  ir_CALL_2(IR_VOID,
2137
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2138
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2139
0
    ir_CONST_ADDR(zend_ce_arithmetic_error),
2140
0
    ir_CONST_ADDR("Bit shift by negative number"));
2141
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op1_op2));
2142
0
  return 1;
2143
0
}
2144
2145
static int zend_jit_mod_by_zero_stub(zend_jit_ctx *jit)
2146
0
{
2147
0
  ir_CALL_2(IR_VOID,
2148
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2149
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2150
0
    ir_CONST_ADDR(zend_ce_division_by_zero_error),
2151
0
    ir_CONST_ADDR("Modulo by zero"));
2152
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op1_op2));
2153
0
  return 1;
2154
0
}
2155
2156
static int zend_jit_invalid_this_stub(zend_jit_ctx *jit)
2157
0
{
2158
0
  ir_CALL_2(IR_VOID,
2159
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2160
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2161
0
    IR_NULL,
2162
0
    ir_CONST_ADDR("Using $this when not in object context"));
2163
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
2164
0
  return 1;
2165
0
}
2166
2167
static int zend_jit_undefined_function_stub(zend_jit_ctx *jit)
2168
0
{
2169
  // JIT: load EX(opline)
2170
0
  ir_ref ref = ir_LOAD_A(jit_FP(jit));
2171
0
  ir_ref arg3 = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.constant)));
2172
2173
0
  if (sizeof(void*) == 8) {
2174
0
    arg3 = ir_LOAD_A(ir_ADD_A(ref, ir_SEXT_A(arg3)));
2175
0
  } else {
2176
0
    arg3 = ir_LOAD_A(arg3);
2177
0
  }
2178
0
  arg3 = ir_ADD_OFFSET(arg3, offsetof(zend_string, val));
2179
2180
0
  ir_CALL_3(IR_VOID,
2181
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2182
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2183
0
    IR_NULL,
2184
0
    ir_CONST_ADDR("Call to undefined function %s()"),
2185
0
    arg3);
2186
2187
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2188
2189
0
  return 1;
2190
0
}
2191
2192
static int zend_jit_throw_cannot_pass_by_ref_stub(zend_jit_ctx *jit)
2193
0
{
2194
0
  ir_ref opline, ref, rx, if_eq, if_tmp;
2195
2196
  // JIT: opline = EX(opline)
2197
0
  opline = ir_LOAD_A(jit_FP(jit));
2198
2199
  // JIT: ZVAL_UNDEF(ZEND_CALL_VAR(RX, opline->result.var))
2200
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, result.var)));
2201
0
  if (sizeof(void*) == 8) {
2202
0
    ref = ir_ZEXT_A(ref);
2203
0
  }
2204
0
  rx = jit_IP(jit);
2205
0
  jit_set_Z_TYPE_INFO_ref(jit, ir_ADD_A(rx, ref), ir_CONST_U32(IS_UNDEF));
2206
2207
  // last EX(call) frame may be delayed
2208
  // JIT: if (EX(call) == RX)
2209
0
  ref = ir_LOAD_A(jit_EX(call));
2210
0
  if_eq = ir_IF(ir_EQ(rx, ref));
2211
0
  ir_IF_FALSE(if_eq);
2212
2213
  // JIT: RX->prev_execute_data == EX(call)
2214
0
  ir_STORE(jit_CALL(rx, prev_execute_data), ref);
2215
2216
  // JIT: EX(call) = RX
2217
0
  ir_STORE(jit_EX(call), rx);
2218
0
  ir_MERGE_WITH_EMPTY_TRUE(if_eq);
2219
2220
  // JIT: IP = opline
2221
0
  jit_STORE_IP(jit, opline);
2222
2223
  // JIT: zend_cannot_pass_by_reference(opline->op2.num)
2224
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_cannot_pass_by_reference),
2225
0
    ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, op2.num))));
2226
2227
  // JIT: if (IP->op1_type == IS_TMP_VAR)
2228
0
  ref = ir_LOAD_U8(ir_ADD_OFFSET(jit_IP(jit), offsetof(zend_op, op1_type)));
2229
0
  if_tmp = ir_IF(ir_EQ(ref, ir_CONST_U8(IS_TMP_VAR)));
2230
0
  ir_IF_TRUE(if_tmp);
2231
2232
  // JIT: zval_ptr_dtor(EX_VAR(IP->op1.var))
2233
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_IP(jit), offsetof(zend_op, op1.var)));
2234
0
  if (sizeof(void*) == 8) {
2235
0
    ref = ir_ZEXT_A(ref);
2236
0
  }
2237
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2238
0
  jit_ZVAL_PTR_DTOR(jit,
2239
0
    ZEND_ADDR_REF_ZVAL(ref),
2240
0
    MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2241
0
  ir_MERGE_WITH_EMPTY_FALSE(if_tmp);
2242
2243
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2244
2245
0
  return 1;
2246
0
}
2247
2248
static int zend_jit_icall_throw_stub(zend_jit_ctx *jit)
2249
0
{
2250
0
  ir_ref ip, if_set;
2251
2252
  // JIT: zend_rethrow_exception(zend_execute_data *execute_data)
2253
  // JIT: if (EX(opline)->opcode != ZEND_HANDLE_EXCEPTION) {
2254
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2255
0
  ip = jit_IP(jit);
2256
0
  if_set = ir_IF(ir_EQ(ir_LOAD_U8(ir_ADD_OFFSET(ip, offsetof(zend_op, opcode))),
2257
0
    ir_CONST_U8(ZEND_HANDLE_EXCEPTION)));
2258
0
  ir_IF_FALSE(if_set);
2259
2260
  // JIT: EG(opline_before_exception) = opline;
2261
0
  ir_STORE(jit_EG(opline_before_exception), ip);
2262
0
  ir_MERGE_WITH_EMPTY_TRUE(if_set);
2263
2264
  // JIT: opline = EG(exception_op);
2265
0
  jit_STORE_IP(jit, jit_EG(exception_op));
2266
2267
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2268
2269
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2270
2271
0
  return 1;
2272
0
}
2273
2274
static int zend_jit_leave_throw_stub(zend_jit_ctx *jit)
2275
0
{
2276
0
  ir_ref ip, if_set;
2277
2278
  // JIT: if (opline->opcode != ZEND_HANDLE_EXCEPTION) {
2279
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2280
0
  ip = jit_IP(jit);
2281
0
  if_set = ir_IF(ir_EQ(ir_LOAD_U8(ir_ADD_OFFSET(ip, offsetof(zend_op, opcode))),
2282
0
    ir_CONST_U8(ZEND_HANDLE_EXCEPTION)));
2283
0
  ir_IF_FALSE(if_set);
2284
2285
  // JIT: EG(opline_before_exception) = opline;
2286
0
  ir_STORE(jit_EG(opline_before_exception), ip);
2287
0
  ir_MERGE_WITH_EMPTY_TRUE(if_set);
2288
2289
  // JIT: opline = EG(exception_op);
2290
0
  jit_STORE_IP(jit, jit_EG(exception_op));
2291
2292
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2293
0
    ir_STORE(jit_EX(opline), jit_IP(jit));
2294
2295
    // JIT: HANDLE_EXCEPTION()
2296
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2297
0
  } else {
2298
0
    zend_jit_vm_leave(jit, jit_IP(jit));
2299
0
  }
2300
2301
0
  return 1;
2302
0
}
2303
2304
static int zend_jit_hybrid_runtime_jit_stub(zend_jit_ctx *jit)
2305
0
{
2306
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) {
2307
0
    return 0;
2308
0
  }
2309
2310
0
  ir_CALL(IR_VOID, ir_CONST_OPCODE_HANDLER_FUNC(zend_runtime_jit));
2311
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2312
0
  return 1;
2313
0
}
2314
2315
static int zend_jit_hybrid_profile_jit_stub(zend_jit_ctx *jit)
2316
0
{
2317
0
  ir_ref addr, func, run_time_cache, jit_extension;
2318
2319
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) {
2320
0
    return 0;
2321
0
  }
2322
2323
0
  addr = ir_CONST_ADDR(&zend_jit_profile_counter),
2324
0
  ir_STORE(addr, ir_ADD_L(ir_LOAD_L(addr), ir_CONST_LONG(1)));
2325
2326
0
  func = ir_LOAD_A(jit_EX(func));
2327
0
  run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
2328
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2329
2330
0
  if (zend_jit_profile_counter_rid) {
2331
0
    addr = ir_ADD_OFFSET(run_time_cache, zend_jit_profile_counter_rid * sizeof(void*));
2332
0
  } else {
2333
0
    addr = run_time_cache;
2334
0
  }
2335
0
  ir_STORE(addr, ir_ADD_L(ir_LOAD_L(addr), ir_CONST_LONG(1)));
2336
2337
0
  addr = ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_extension, orig_handler));
2338
0
  ir_IJMP(ir_LOAD_A(addr));
2339
2340
0
  return 1;
2341
0
}
2342
2343
static int _zend_jit_hybrid_hot_counter_stub(zend_jit_ctx *jit, uint32_t cost)
2344
0
{
2345
0
  ir_ref func, jit_extension, addr, ref, if_overflow;
2346
0
2347
0
  func = ir_LOAD_A(jit_EX(func));
2348
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2349
0
  addr = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_hot_extension, counter)));
2350
0
  ref = ir_SUB_I16(ir_LOAD_I16(addr), ir_CONST_I16(cost));
2351
0
  ir_STORE(addr, ref);
2352
0
  if_overflow = ir_IF(ir_LE(ref, ir_CONST_I16(0)));
2353
0
2354
0
  ir_IF_TRUE_cold(if_overflow);
2355
0
  ir_STORE(addr, ir_CONST_I16(ZEND_JIT_COUNTER_INIT));
2356
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_hot_func),
2357
0
    jit_FP(jit),
2358
0
    jit_IP(jit));
2359
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2360
0
2361
0
  ir_IF_FALSE(if_overflow);
2362
0
  ref = ir_SUB_A(jit_IP(jit),
2363
0
    ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, opcodes))));
2364
0
  ref = ir_DIV_A(ref, ir_CONST_ADDR(sizeof(zend_op) / sizeof(void*)));
2365
0
2366
0
  addr = ir_ADD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_hot_extension, orig_handlers)),
2367
0
    ref);
2368
0
  ir_IJMP(ir_LOAD_A(addr));
2369
0
2370
0
  return 1;
2371
0
}
2372
2373
static int zend_jit_hybrid_func_hot_counter_stub(zend_jit_ctx *jit)
2374
0
{
2375
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_func)) {
2376
0
    return 0;
2377
0
  }
2378
2379
0
  return _zend_jit_hybrid_hot_counter_stub(jit,
2380
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
2381
0
}
2382
2383
static int zend_jit_hybrid_loop_hot_counter_stub(zend_jit_ctx *jit)
2384
0
{
2385
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_loop)) {
2386
0
    return 0;
2387
0
  }
2388
2389
0
  return _zend_jit_hybrid_hot_counter_stub(jit,
2390
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
2391
0
}
2392
2393
static ir_ref _zend_jit_orig_opline_handler(zend_jit_ctx *jit, ir_ref offset)
2394
0
{
2395
0
  ir_ref addr = ir_ADD_A(offset, jit_IP(jit));
2396
2397
0
  return ir_LOAD_A(addr);
2398
0
}
2399
2400
static ir_ref zend_jit_orig_opline_handler(zend_jit_ctx *jit)
2401
0
{
2402
0
  ir_ref func, jit_extension, offset;
2403
2404
0
  func = ir_LOAD_A(jit_EX(func));
2405
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2406
0
  offset = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_trace_extension, offset)));
2407
0
  return _zend_jit_orig_opline_handler(jit, offset);
2408
0
}
2409
2410
static int _zend_jit_hybrid_trace_counter_stub(zend_jit_ctx *jit, uint32_t cost)
2411
0
{
2412
0
  ir_ref func, jit_extension, offset, addr, ref, if_overflow, ret, if_halt;
2413
0
2414
0
  func = ir_LOAD_A(jit_EX(func));
2415
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2416
0
  offset = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_trace_extension, offset)));
2417
0
  addr = ir_LOAD_A(ir_ADD_OFFSET(ir_ADD_A(offset, jit_IP(jit)), offsetof(zend_op_trace_info, counter)));
2418
0
  ref = ir_SUB_I16(ir_LOAD_I16(addr), ir_CONST_I16(cost));
2419
0
  ir_STORE(addr, ref);
2420
0
  if_overflow = ir_IF(ir_LE(ref, ir_CONST_I16(0)));
2421
0
2422
0
  ir_IF_TRUE_cold(if_overflow);
2423
0
  ir_STORE(addr, ir_CONST_I16(ZEND_JIT_COUNTER_INIT));
2424
0
  ret = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_trace_hot_root),
2425
0
    jit_FP(jit),
2426
0
    jit_IP(jit));
2427
0
  if_halt = ir_IF(ir_LT(ret, ir_CONST_I32(0)));
2428
0
  ir_IF_FALSE(if_halt);
2429
0
2430
0
  ref = jit_EG(current_execute_data);
2431
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2432
0
  ref = ir_LOAD_A(jit_EX(opline));
2433
0
  jit_STORE_IP(jit, ref);
2434
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2435
0
2436
0
  ir_IF_FALSE(if_overflow);
2437
0
  ir_IJMP(_zend_jit_orig_opline_handler(jit, offset));
2438
0
2439
0
  ir_IF_TRUE(if_halt);
2440
0
  ir_IJMP(ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_halt_op->handler));
2441
0
2442
0
  return 1;
2443
0
}
2444
2445
static int zend_jit_hybrid_func_trace_counter_stub(zend_jit_ctx *jit)
2446
0
{
2447
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_func)) {
2448
0
    return 0;
2449
0
  }
2450
2451
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2452
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
2453
0
}
2454
2455
static int zend_jit_hybrid_ret_trace_counter_stub(zend_jit_ctx *jit)
2456
0
{
2457
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_return)) {
2458
0
    return 0;
2459
0
  }
2460
2461
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2462
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_return) - 1) / JIT_G(hot_return)));
2463
0
}
2464
2465
static int zend_jit_hybrid_loop_trace_counter_stub(zend_jit_ctx *jit)
2466
0
{
2467
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_loop)) {
2468
0
    return 0;
2469
0
  }
2470
2471
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2472
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
2473
0
}
2474
2475
static int zend_jit_trace_halt_stub(zend_jit_ctx *jit)
2476
0
{
2477
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2478
0
    ir_TAILCALL(IR_VOID, ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_halt_op->handler));
2479
0
  } else if (GCC_GLOBAL_REGS) {
2480
0
    jit_STORE_IP(jit, IR_NULL);
2481
0
    ir_RETURN(IR_VOID);
2482
0
  } else {
2483
0
    ir_RETURN(ir_CONST_ADDR(ZEND_VM_ENTER_BIT)); // ZEND_VM_RETURN
2484
0
  }
2485
0
  return 1;
2486
0
}
2487
2488
static int zend_jit_trace_escape_stub(zend_jit_ctx *jit)
2489
0
{
2490
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2491
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2492
0
  } else {
2493
0
    zend_jit_vm_enter(jit, jit_IP(jit));
2494
0
  }
2495
2496
0
  return 1;
2497
0
}
2498
2499
static int zend_jit_trace_exit_stub(zend_jit_ctx *jit)
2500
0
{
2501
0
  ir_ref ref, ret, if_zero, addr;
2502
2503
  // EX(opline) = opline
2504
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2505
2506
0
  ret = ir_EXITCALL(ir_CONST_FC_FUNC(zend_jit_trace_exit));
2507
2508
0
  if_zero = ir_IF(ir_EQ(ret, ir_CONST_I32(0)));
2509
2510
0
  ir_IF_TRUE(if_zero);
2511
2512
0
  ref = jit_EG(current_execute_data);
2513
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2514
0
  ref = ir_LOAD_A(jit_EX(opline));
2515
0
  jit_STORE_IP(jit, ref);
2516
2517
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2518
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2519
0
  } else {
2520
0
    zend_jit_vm_enter(jit, ref);
2521
0
  }
2522
2523
0
  ir_IF_FALSE(if_zero);
2524
2525
0
  ir_GUARD(ir_GE(ret, ir_CONST_I32(0)), jit_STUB_ADDR(jit, jit_stub_trace_halt));
2526
2527
0
  ref = jit_EG(current_execute_data);
2528
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2529
2530
0
  ref = ir_LOAD_A(jit_EX(opline));
2531
0
  jit_STORE_IP(jit, ref);
2532
2533
  // check for interrupt (try to avoid this ???)
2534
0
  zend_jit_check_timeout(jit, NULL, NULL);
2535
2536
0
  addr = zend_jit_orig_opline_handler(jit);
2537
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2538
0
    zend_jit_tailcall_handler(jit, addr);
2539
0
  } else {
2540
#if defined(IR_TARGET_X86)
2541
    addr = ir_CAST_OPCODE_HANDLER_FUNC(addr);
2542
#endif
2543
0
    ref = ir_CALL_2(IR_ADDR, addr, jit_FP(jit), jit_IP(jit));
2544
0
    zend_jit_vm_enter(jit, ref);
2545
0
  }
2546
2547
0
  return 1;
2548
0
}
2549
2550
static int zend_jit_undefined_offset_stub(zend_jit_ctx *jit)
2551
0
{
2552
0
  if (GCC_GLOBAL_REGS) {
2553
0
    ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key));
2554
0
  } else {
2555
0
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key), jit_FP(jit));
2556
0
  }
2557
2558
0
  return 1;
2559
0
}
2560
2561
static int zend_jit_undefined_key_stub(zend_jit_ctx *jit)
2562
0
{
2563
0
  if (GCC_GLOBAL_REGS) {
2564
0
    ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_string_key));
2565
0
  } else {
2566
0
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_string_key), jit_FP(jit));
2567
0
  }
2568
2569
0
  return 1;
2570
0
}
2571
2572
static int zend_jit_cannot_add_element_stub(zend_jit_ctx *jit)
2573
0
{
2574
0
  ir_ref opline = ir_LOAD_A(jit_EX(opline));
2575
0
  ir_ref ref, if_result_used;
2576
2577
0
  if_result_used = ir_IF(ir_AND_U8(
2578
0
    ir_LOAD_U8(ir_ADD_OFFSET(opline, offsetof(zend_op, result_type))),
2579
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2580
0
  ir_IF_TRUE(if_result_used);
2581
2582
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, result.var)));
2583
0
  if (sizeof(void*) == 8) {
2584
0
    ref = ir_ZEXT_A(ref);
2585
0
  }
2586
0
  jit_set_Z_TYPE_INFO_ref(jit, ir_ADD_A(jit_FP(jit), ref), ir_CONST_U32(IS_UNDEF));
2587
0
  ir_MERGE_WITH_EMPTY_FALSE(if_result_used);
2588
2589
0
  ir_CALL_2(IR_VOID,
2590
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2591
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2592
0
    IR_NULL,
2593
0
    ir_CONST_ADDR("Cannot add element to the array as the next element is already occupied"));
2594
0
  ir_RETURN(IR_VOID);
2595
2596
0
  return 1;
2597
0
}
2598
2599
static int zend_jit_assign_const_stub(zend_jit_ctx *jit)
2600
0
{
2601
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2602
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2603
2604
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2605
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2606
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN;
2607
2608
0
  if (!zend_jit_assign_to_variable(
2609
0
      jit, NULL,
2610
0
      var_addr, var_addr, -1, -1,
2611
0
      IS_CONST, val_addr, val_info,
2612
0
      0, 0, false)) {
2613
0
    return 0;
2614
0
  }
2615
0
  ir_RETURN(IR_VOID);
2616
0
  return 1;
2617
0
}
2618
2619
static int zend_jit_assign_tmp_stub(zend_jit_ctx *jit)
2620
0
{
2621
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2622
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2623
2624
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2625
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2626
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN;
2627
2628
0
  if (!zend_jit_assign_to_variable(
2629
0
      jit, NULL,
2630
0
      var_addr, var_addr, -1, -1,
2631
0
      IS_TMP_VAR, val_addr, val_info,
2632
0
      0, 0, false)) {
2633
0
    return 0;
2634
0
  }
2635
0
  ir_RETURN(IR_VOID);
2636
0
  return 1;
2637
0
}
2638
2639
static int zend_jit_assign_var_stub(zend_jit_ctx *jit)
2640
0
{
2641
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2642
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2643
2644
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2645
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2646
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF;
2647
2648
0
  if (!zend_jit_assign_to_variable(
2649
0
      jit, NULL,
2650
0
      var_addr, var_addr, -1, -1,
2651
0
      IS_VAR, val_addr, val_info,
2652
0
      0, 0, false)) {
2653
0
    return 0;
2654
0
  }
2655
0
  ir_RETURN(IR_VOID);
2656
0
  return 1;
2657
0
}
2658
2659
static int zend_jit_assign_cv_noref_stub(zend_jit_ctx *jit)
2660
0
{
2661
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2662
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2663
2664
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2665
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2666
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN/*|MAY_BE_UNDEF*/;
2667
2668
0
  if (!zend_jit_assign_to_variable(
2669
0
      jit, NULL,
2670
0
      var_addr, var_addr, -1, -1,
2671
0
      IS_CV, val_addr, val_info,
2672
0
      0, 0, false)) {
2673
0
    return 0;
2674
0
  }
2675
0
  ir_RETURN(IR_VOID);
2676
0
  return 1;
2677
0
}
2678
2679
static int zend_jit_new_array_stub(zend_jit_ctx *jit)
2680
0
{
2681
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2682
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2683
0
  ir_ref ref = ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(_zend_new_array_0));
2684
2685
0
  jit_set_Z_PTR(jit, var_addr, ref);
2686
0
  jit_set_Z_TYPE_INFO(jit, var_addr, IS_ARRAY_EX);
2687
0
  ir_RETURN(ref);
2688
0
  return 1;
2689
0
}
2690
2691
static int zend_jit_assign_cv_stub(zend_jit_ctx *jit)
2692
0
{
2693
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2694
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2695
2696
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2697
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2698
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF/*|MAY_BE_UNDEF*/;
2699
2700
0
  if (!zend_jit_assign_to_variable(
2701
0
      jit, NULL,
2702
0
      var_addr, var_addr, -1, -1,
2703
0
      IS_CV, val_addr, val_info,
2704
0
      0, 0, false)) {
2705
0
    return 0;
2706
0
  }
2707
0
  ir_RETURN(IR_VOID);
2708
0
  return 1;
2709
0
}
2710
2711
static void zend_jit_init_ctx(zend_jit_ctx *jit, uint32_t flags)
2712
0
{
2713
#if defined (__CET__) && (__CET__ & 1) != 0
2714
  flags |= IR_GEN_ENDBR;
2715
#endif
2716
0
  flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN;
2717
2718
0
  ir_init(&jit->ctx, flags, 256, 1024);
2719
0
  jit->ctx.ret_type = -1;
2720
2721
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
2722
0
  jit->ctx.mflags |= default_mflags;
2723
0
  if (JIT_G(opt_flags) & allowed_opt_flags & ZEND_JIT_CPU_AVX) {
2724
0
    jit->ctx.mflags |= IR_X86_AVX;
2725
0
  }
2726
#elif defined(IR_TARGET_AARCH64)
2727
  jit->ctx.get_veneer = zend_jit_get_veneer;
2728
  jit->ctx.set_veneer = zend_jit_set_veneer;
2729
#endif
2730
2731
0
  jit->ctx.fixed_regset = (1<<ZREG_FP) | (1<<ZREG_IP);
2732
0
  if (!(flags & IR_FUNCTION)) {
2733
0
    jit->ctx.flags |= IR_NO_STACK_COMBINE;
2734
0
    if (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2735
0
      jit->ctx.flags |= IR_FUNCTION;
2736
      /* Stack must be 16 byte aligned */
2737
      /* TODO: select stack size ??? */
2738
0
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
2739
# if defined(IR_TARGET_AARCH64)
2740
      /* Must save LR */
2741
      jit->ctx.flags |= IR_USE_FRAME_POINTER;
2742
      /* Same as HYBRID VM */
2743
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 4; /* 4 spill slots */
2744
# else
2745
      /* Same as HYBRID VM, plus 1 slot for re-alignment (caller pushes return address, frame is not aligned on entry) */
2746
0
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 5; /* 5 spill slots (8 bytes) or 10 spill slots (4 bytes) */
2747
0
# endif
2748
#elif defined(IR_TARGET_AARCH64)
2749
      jit->ctx.flags |= IR_USE_FRAME_POINTER;
2750
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 16; /* 10 saved registers and 6 spill slots (8 bytes) */
2751
#elif defined(_WIN64)
2752
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 11; /* 8 saved registers and 3 spill slots (8 bytes) */
2753
#elif defined(IR_TARGET_X86_64)
2754
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 9;  /* 6 saved registers and 3 spill slots (8 bytes) */
2755
#else /* IR_TARGET_x86 */
2756
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 11; /* 4 saved registers and 7 spill slots (4 bytes) */
2757
#endif
2758
      /* JIT-ed code is called only from execute_ex, which takes care
2759
       * of saving ZREG_FP, ZREG_IP when GCC_GLOBAL_REGS is 1, so we don't
2760
       * have to save them.
2761
       */
2762
0
      if (GCC_GLOBAL_REGS) {
2763
0
        jit->ctx.fixed_save_regset = IR_REGSET_PRESERVED & ~((1<<ZREG_FP) | (1<<ZREG_IP));
2764
0
      } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2765
        /* The only preserved register on x86 is RBP:
2766
         * https://github.com/llvm/llvm-project/blob/a414877a7a5f000d01370acb1162eb1dea87f48c/llvm/lib/Target/X86/X86RegisterInfo.cpp#L319
2767
         * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/X86/X86CallingConv.td#L1183
2768
         * On AArch64 it's LR, FP:
2769
         * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/AArch64/AArch64CallingConvention.td#L681
2770
         *
2771
         * Add them to the fixed_regset to prevent usage or these regs.
2772
         * It's cheaper to not use them than to save them.
2773
         */
2774
0
#if defined(IR_TARGET_X64)
2775
0
        jit->ctx.fixed_regset |= (1<<IR_REG_FP);
2776
#elif defined(IR_TARGET_AARCH64)
2777
        jit->ctx.fixed_regset |= (1<<IR_REG_FP) | (1<<IR_REG_LR);
2778
#else
2779
        ZEND_UNREACHABLE();
2780
#endif
2781
0
      } else {
2782
0
        jit->ctx.fixed_save_regset = IR_REGSET_PRESERVED;
2783
//#ifdef _WIN64
2784
//        jit->ctx.fixed_save_regset &= 0xffff; // TODO: don't save FP registers ???
2785
//#endif
2786
0
      }
2787
#ifdef _WIN64
2788
      jit->ctx.fixed_call_stack_size = 16 + IR_SHADOW_ARGS;
2789
#else
2790
0
      jit->ctx.fixed_call_stack_size = 16;
2791
0
#endif
2792
0
    } else {
2793
#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE
2794
      jit->ctx.fixed_stack_red_zone = ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE;
2795
      if (jit->ctx.fixed_stack_red_zone > 16) {
2796
        jit->ctx.fixed_stack_frame_size = jit->ctx.fixed_stack_red_zone - 16;
2797
        jit->ctx.fixed_call_stack_size = 16;
2798
      }
2799
      jit->ctx.flags |= IR_MERGE_EMPTY_ENTRIES;
2800
#else
2801
0
      jit->ctx.fixed_stack_red_zone = 0;
2802
0
      jit->ctx.fixed_stack_frame_size = 32; /* 4 spill slots (8 bytes) or 8 spill slots (4 bytes) */
2803
0
      jit->ctx.fixed_call_stack_size = 16;
2804
0
#endif
2805
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
2806
0
      jit->ctx.fixed_regset |= (1<<IR_REG_FP); /* prevent %rbp (%r5) usage */
2807
0
#endif
2808
0
    }
2809
0
  }
2810
2811
0
  jit->ctx.snapshot_create = (ir_snapshot_create_t)jit_SNAPSHOT;
2812
2813
0
  jit->op_array = NULL;
2814
0
  jit->current_op_array = NULL;
2815
0
  jit->ssa = NULL;
2816
0
  jit->name = NULL;
2817
0
  jit->last_valid_opline = NULL;
2818
0
  jit->use_last_valid_opline = false;
2819
0
  jit->track_last_valid_opline = false;
2820
0
  jit->reuse_ip = false;
2821
0
  jit->delayed_call_level = 0;
2822
0
  delayed_call_chain = false;
2823
0
  jit->b = -1;
2824
#ifdef ZTS
2825
  jit->tls = IR_UNUSED;
2826
#endif
2827
0
  jit->fp = IR_UNUSED;
2828
0
  jit->poly_func_ref = IR_UNUSED;
2829
0
  jit->poly_this_ref = IR_UNUSED;
2830
0
  jit->trace_loop_ref = IR_UNUSED;
2831
0
  jit->return_inputs = IR_UNUSED;
2832
0
  jit->bb_start_ref = NULL;
2833
0
  jit->bb_predecessors = NULL;
2834
0
  jit->bb_edges = NULL;
2835
0
  jit->trace = NULL;
2836
0
  jit->ra = NULL;
2837
0
  jit->delay_var = -1;
2838
0
  jit->delay_refs = NULL;
2839
0
  jit->eg_exception_addr = 0;
2840
0
  zend_hash_init(&jit->addr_hash, 64, NULL, NULL, 0);
2841
0
  memset(jit->stub_addr, 0, sizeof(jit->stub_addr));
2842
2843
0
  ir_START();
2844
0
}
2845
2846
static int zend_jit_free_ctx(zend_jit_ctx *jit)
2847
0
{
2848
0
  if (jit->name) {
2849
0
    zend_string_release(jit->name);
2850
0
  }
2851
0
  zend_hash_destroy(&jit->addr_hash);
2852
0
  ir_free(&jit->ctx);
2853
0
  return 1;
2854
0
}
2855
2856
static void *zend_jit_ir_compile(ir_ctx *ctx, size_t *size, const char *name)
2857
0
{
2858
0
  void *entry;
2859
0
  ir_code_buffer code_buffer;
2860
2861
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_SRC) {
2862
0
    if (name) fprintf(stderr, "%s: ; after folding\n", name);
2863
0
    ir_save(ctx, 0, stderr);
2864
0
  }
2865
2866
0
#if ZEND_DEBUG
2867
0
  ir_check(ctx);
2868
0
#endif
2869
2870
0
  ir_build_def_use_lists(ctx);
2871
2872
0
#if ZEND_DEBUG
2873
0
  ir_check(ctx);
2874
0
#endif
2875
2876
0
#if 1
2877
0
  ir_sccp(ctx);
2878
0
#endif
2879
2880
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_SCCP) {
2881
0
    if (name) fprintf(stderr, "%s: ; after SCCP\n", name);
2882
0
    ir_save(ctx, 0, stderr);
2883
0
  }
2884
2885
0
  ir_build_cfg(ctx);
2886
0
  ir_build_dominators_tree(ctx);
2887
0
  ir_find_loops(ctx);
2888
2889
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_CFG) {
2890
0
    if (name) fprintf(stderr, "%s: ; after CFG\n", name);
2891
0
    ir_save(ctx, IR_SAVE_CFG, stderr);
2892
0
  }
2893
2894
0
  ir_gcm(ctx);
2895
2896
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_GCM) {
2897
0
    if (name) fprintf(stderr, "%s: ; after GCM\n", name);
2898
0
    ir_save(ctx, IR_SAVE_CFG|IR_SAVE_CFG_MAP, stderr);
2899
0
  }
2900
2901
0
  ir_schedule(ctx);
2902
2903
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE) {
2904
0
    if (name) fprintf(stderr, "%s: ; after schedule\n", name);
2905
0
    ir_save(ctx, IR_SAVE_CFG, stderr);
2906
0
  }
2907
2908
0
  ir_match(ctx);
2909
0
#if !defined(IR_TARGET_AARCH64)
2910
0
  ctx->flags &= ~IR_USE_FRAME_POINTER; /* don't use FRAME_POINTER even with ALLOCA, TODO: cleanup this ??? */
2911
0
#endif
2912
0
  ir_assign_virtual_registers(ctx);
2913
0
  ir_compute_live_ranges(ctx);
2914
0
  ir_coalesce(ctx);
2915
0
  ir_reg_alloc(ctx);
2916
2917
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_REGS) {
2918
0
    if (name) fprintf(stderr, "%s: ; after register allocation\n", name);
2919
0
    ir_save(ctx, IR_SAVE_CFG|IR_SAVE_RULES|IR_SAVE_REGS, stderr);
2920
0
    ir_dump_live_ranges(ctx, stderr);
2921
0
  }
2922
2923
0
  ir_schedule_blocks(ctx);
2924
2925
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_IR_FINAL|ZEND_JIT_DEBUG_IR_CODEGEN)) {
2926
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_CODEGEN) {
2927
0
      if (name) fprintf(stderr, "%s: ; codegen\n", name);
2928
0
      ir_dump_codegen(ctx, stderr);
2929
0
    } else {
2930
0
      if (name) fprintf(stderr, "%s: ; final\n", name);
2931
0
      ir_save(ctx, IR_SAVE_CFG|IR_SAVE_RULES|IR_SAVE_REGS, stderr);
2932
0
    }
2933
0
  }
2934
2935
0
#if ZEND_DEBUG
2936
0
  ir_check(ctx);
2937
0
#endif
2938
2939
0
  code_buffer.start = dasm_buf;
2940
0
  code_buffer.end = dasm_end;
2941
0
  code_buffer.pos = *dasm_ptr;
2942
0
  ctx->code_buffer = &code_buffer;
2943
2944
0
  entry = ir_emit_code(ctx, size);
2945
2946
0
  *dasm_ptr = code_buffer.pos;
2947
2948
#if defined(IR_TARGET_AARCH64)
2949
  if (ctx->flags2 & IR_HAS_VENEERS) {
2950
    zend_jit_commit_veneers();
2951
  }
2952
#endif
2953
2954
0
  return entry;
2955
0
}
2956
2957
static void zend_jit_setup_stubs(void)
2958
0
{
2959
0
  zend_jit_ctx jit;
2960
0
  void *entry;
2961
0
  size_t size;
2962
0
  uint32_t i;
2963
2964
0
  for (i = 0; i < sizeof(zend_jit_stubs)/sizeof(zend_jit_stubs[0]); i++) {
2965
0
    zend_jit_init_ctx(&jit, zend_jit_stubs[i].flags);
2966
2967
0
    if (!zend_jit_stubs[i].stub(&jit)) {
2968
0
      zend_jit_free_ctx(&jit);
2969
0
      zend_jit_stub_handlers[i] = NULL;
2970
0
      continue;
2971
0
    }
2972
2973
0
    entry = zend_jit_ir_compile(&jit.ctx, &size, zend_jit_stubs[i].name);
2974
0
    if (!entry) {
2975
0
      zend_jit_free_ctx(&jit);
2976
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: could not compile stub");
2977
0
    }
2978
2979
0
    zend_jit_stub_handlers[i] = entry;
2980
2981
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)) {
2982
#ifdef HAVE_CAPSTONE
2983
      if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
2984
        ir_disasm_add_symbol(zend_jit_stubs[i].name, (uintptr_t)entry, size);
2985
      }
2986
      if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_STUBS) {
2987
        ir_disasm(zend_jit_stubs[i].name,
2988
          entry, size, (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_ADDR) != 0, &jit.ctx, stderr);
2989
      }
2990
#endif
2991
0
#ifndef _WIN32
2992
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
2993
//        ir_mem_unprotect(entry, size);
2994
0
        ir_gdb_register(zend_jit_stubs[i].name, entry, size, 0, 0);
2995
//        ir_mem_protect(entry, size);
2996
0
      }
2997
2998
0
      if (JIT_G(debug) & (ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
2999
0
        ir_perf_map_register(zend_jit_stubs[i].name, entry, size);
3000
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3001
0
          ir_perf_jitdump_register(zend_jit_stubs[i].name, entry, size);
3002
0
        }
3003
0
      }
3004
0
#endif
3005
0
    }
3006
0
    zend_jit_free_ctx(&jit);
3007
0
  }
3008
0
}
3009
3010
#define REGISTER_HELPER(n)  \
3011
  ir_disasm_add_symbol(#n, (uint64_t)(uintptr_t)n, sizeof(void*));
3012
#define REGISTER_DATA(n)  \
3013
  ir_disasm_add_symbol(#n, (uint64_t)(uintptr_t)&n, sizeof(n));
3014
3015
static void zend_jit_setup_disasm(void)
3016
0
{
3017
#ifdef HAVE_CAPSTONE
3018
  ir_disasm_init();
3019
3020
  if (zend_vm_kind() == ZEND_VM_KIND_HYBRID) {
3021
    zend_op opline;
3022
3023
    memset(&opline, 0, sizeof(opline));
3024
3025
    opline.opcode = ZEND_DO_UCALL;
3026
    opline.result_type = IS_UNUSED;
3027
    zend_vm_set_opcode_handler(&opline);
3028
    ir_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3029
3030
    opline.opcode = ZEND_DO_UCALL;
3031
    opline.result_type = IS_VAR;
3032
    zend_vm_set_opcode_handler(&opline);
3033
    ir_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3034
3035
    opline.opcode = ZEND_DO_FCALL_BY_NAME;
3036
    opline.result_type = IS_UNUSED;
3037
    zend_vm_set_opcode_handler(&opline);
3038
    ir_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3039
3040
    opline.opcode = ZEND_DO_FCALL_BY_NAME;
3041
    opline.result_type = IS_VAR;
3042
    zend_vm_set_opcode_handler(&opline);
3043
    ir_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3044
3045
    opline.opcode = ZEND_DO_FCALL;
3046
    opline.result_type = IS_UNUSED;
3047
    zend_vm_set_opcode_handler(&opline);
3048
    ir_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3049
3050
    opline.opcode = ZEND_DO_FCALL;
3051
    opline.result_type = IS_VAR;
3052
    zend_vm_set_opcode_handler(&opline);
3053
    ir_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3054
3055
    opline.opcode = ZEND_RETURN;
3056
    opline.op1_type = IS_CONST;
3057
    zend_vm_set_opcode_handler(&opline);
3058
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_CONST_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3059
3060
    opline.opcode = ZEND_RETURN;
3061
    opline.op1_type = IS_TMP_VAR;
3062
    zend_vm_set_opcode_handler(&opline);
3063
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_TMP_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3064
3065
    opline.opcode = ZEND_RETURN;
3066
    opline.op1_type = IS_VAR;
3067
    zend_vm_set_opcode_handler(&opline);
3068
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_VAR_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3069
3070
    opline.opcode = ZEND_RETURN;
3071
    opline.op1_type = IS_CV;
3072
    zend_vm_set_opcode_handler(&opline);
3073
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_CV_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3074
3075
    ir_disasm_add_symbol("ZEND_HYBRID_HALT_LABEL", (uint64_t)(uintptr_t)zend_jit_halt_op->handler, sizeof(void*));
3076
  }
3077
3078
  REGISTER_DATA(zend_jit_profile_counter);
3079
3080
  REGISTER_HELPER(zend_runtime_jit);
3081
  REGISTER_HELPER(zend_jit_hot_func);
3082
  REGISTER_HELPER(zend_jit_trace_hot_root);
3083
  REGISTER_HELPER(zend_jit_trace_exit);
3084
3085
  REGISTER_HELPER(zend_jit_array_free);
3086
  REGISTER_HELPER(zend_jit_undefined_op_helper);
3087
  REGISTER_HELPER(zend_jit_pre_inc_typed_ref);
3088
  REGISTER_HELPER(zend_jit_pre_dec_typed_ref);
3089
  REGISTER_HELPER(zend_jit_post_inc_typed_ref);
3090
  REGISTER_HELPER(zend_jit_post_dec_typed_ref);
3091
  REGISTER_HELPER(zend_jit_pre_inc);
3092
  REGISTER_HELPER(zend_jit_pre_dec);
3093
  REGISTER_HELPER(zend_jit_add_arrays_helper);
3094
  REGISTER_HELPER(zend_jit_fast_assign_concat_helper);
3095
  REGISTER_HELPER(zend_jit_fast_concat_helper);
3096
  REGISTER_HELPER(zend_jit_fast_concat_tmp_helper);
3097
  REGISTER_HELPER(zend_jit_assign_op_to_typed_ref_tmp);
3098
  REGISTER_HELPER(zend_jit_assign_op_to_typed_ref);
3099
  REGISTER_HELPER(zend_jit_assign_const_to_typed_ref);
3100
  REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref);
3101
  REGISTER_HELPER(zend_jit_assign_var_to_typed_ref);
3102
  REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref);
3103
  REGISTER_HELPER(zend_jit_assign_const_to_typed_ref2);
3104
  REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref2);
3105
  REGISTER_HELPER(zend_jit_assign_var_to_typed_ref2);
3106
  REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref2);
3107
  REGISTER_HELPER(zend_jit_check_constant);
3108
  REGISTER_HELPER(zend_jit_get_constant);
3109
  REGISTER_HELPER(zend_jit_int_extend_stack_helper);
3110
  REGISTER_HELPER(zend_jit_extend_stack_helper);
3111
  REGISTER_HELPER(zend_jit_init_func_run_time_cache_helper);
3112
  REGISTER_HELPER(zend_jit_find_func_helper);
3113
  REGISTER_HELPER(zend_jit_find_ns_func_helper);
3114
  REGISTER_HELPER(zend_jit_jmp_frameless_helper);
3115
  REGISTER_HELPER(zend_jit_unref_helper);
3116
  REGISTER_HELPER(zend_jit_invalid_method_call);
3117
  REGISTER_HELPER(zend_jit_invalid_method_call_tmp);
3118
  REGISTER_HELPER(zend_jit_find_method_helper);
3119
  REGISTER_HELPER(zend_jit_find_method_tmp_helper);
3120
  REGISTER_HELPER(zend_jit_push_static_method_call_frame);
3121
  REGISTER_HELPER(zend_jit_push_static_method_call_frame_tmp);
3122
  REGISTER_HELPER(zend_jit_find_class_helper);
3123
  REGISTER_HELPER(zend_jit_find_static_method_helper);
3124
  REGISTER_HELPER(zend_jit_push_this_method_call_frame);
3125
  REGISTER_HELPER(zend_jit_free_trampoline_helper);
3126
  REGISTER_HELPER(zend_jit_verify_return_slow);
3127
  REGISTER_HELPER(zend_jit_deprecated_helper);
3128
  REGISTER_HELPER(zend_jit_undefined_long_key);
3129
  REGISTER_HELPER(zend_jit_undefined_long_key_ex);
3130
  REGISTER_HELPER(zend_jit_undefined_string_key);
3131
  REGISTER_HELPER(zend_jit_copy_extra_args_helper);
3132
  REGISTER_HELPER(zend_jit_copy_extra_args_helper_no_skip_recv);
3133
  REGISTER_HELPER(zend_jit_vm_stack_free_args_helper);
3134
  REGISTER_HELPER(zend_free_extra_named_params);
3135
  REGISTER_HELPER(zend_jit_free_call_frame);
3136
  REGISTER_HELPER(zend_jit_exception_in_interrupt_handler_helper);
3137
  REGISTER_HELPER(zend_jit_verify_arg_slow);
3138
  REGISTER_HELPER(zend_missing_arg_error);
3139
  REGISTER_HELPER(zend_jit_only_vars_by_reference);
3140
  REGISTER_HELPER(zend_jit_leave_func_helper);
3141
  REGISTER_HELPER(zend_jit_leave_nested_func_helper);
3142
  REGISTER_HELPER(zend_jit_leave_top_func_helper);
3143
  REGISTER_HELPER(zend_jit_fetch_global_helper);
3144
  REGISTER_HELPER(zend_jit_hash_index_lookup_rw_no_packed);
3145
  REGISTER_HELPER(zend_jit_hash_index_lookup_rw);
3146
  REGISTER_HELPER(zend_jit_hash_lookup_rw);
3147
  REGISTER_HELPER(zend_jit_symtable_find);
3148
  REGISTER_HELPER(zend_jit_symtable_lookup_w);
3149
  REGISTER_HELPER(zend_jit_symtable_lookup_rw);
3150
  REGISTER_HELPER(zend_jit_fetch_dim_r_helper);
3151
  REGISTER_HELPER(zend_jit_fetch_dim_is_helper);
3152
  REGISTER_HELPER(zend_jit_fetch_dim_isset_helper);
3153
  REGISTER_HELPER(zend_jit_fetch_dim_rw_helper);
3154
  REGISTER_HELPER(zend_jit_fetch_dim_w_helper);
3155
  REGISTER_HELPER(zend_jit_fetch_dim_str_offset_r_helper);
3156
  REGISTER_HELPER(zend_jit_fetch_dim_str_r_helper);
3157
  REGISTER_HELPER(zend_jit_fetch_dim_str_is_helper);
3158
  REGISTER_HELPER(zend_jit_fetch_dim_obj_r_helper);
3159
  REGISTER_HELPER(zend_jit_fetch_dim_obj_is_helper);
3160
  REGISTER_HELPER(zend_jit_invalid_array_access);
3161
  REGISTER_HELPER(zend_jit_zval_array_dup);
3162
  REGISTER_HELPER(zend_jit_prepare_assign_dim_ref);
3163
  REGISTER_HELPER(zend_jit_fetch_dim_obj_w_helper);
3164
  REGISTER_HELPER(zend_jit_fetch_dim_obj_rw_helper);
3165
  REGISTER_HELPER(zend_jit_isset_dim_helper);
3166
  REGISTER_HELPER(zend_jit_assign_dim_helper);
3167
  REGISTER_HELPER(zend_jit_assign_dim_op_helper);
3168
  REGISTER_HELPER(zend_jit_fetch_obj_w_slow);
3169
  REGISTER_HELPER(zend_jit_fetch_obj_r_slow);
3170
  REGISTER_HELPER(zend_jit_fetch_obj_r_slow_ex);
3171
  REGISTER_HELPER(zend_jit_fetch_obj_is_slow);
3172
  REGISTER_HELPER(zend_jit_fetch_obj_is_slow_ex);
3173
  REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic);
3174
  REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic_ex);
3175
  REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic);
3176
  REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic_ex);
3177
  REGISTER_HELPER(zend_jit_check_array_promotion);
3178
  REGISTER_HELPER(zend_jit_create_typed_ref);
3179
  REGISTER_HELPER(zend_jit_invalid_property_write);
3180
  REGISTER_HELPER(zend_jit_invalid_property_read);
3181
  REGISTER_HELPER(zend_jit_extract_helper);
3182
  REGISTER_HELPER(zend_jit_invalid_property_assign);
3183
  REGISTER_HELPER(zend_jit_assign_to_typed_prop);
3184
  REGISTER_HELPER(zend_jit_assign_obj_helper);
3185
  REGISTER_HELPER(zend_jit_invalid_property_assign_op);
3186
  REGISTER_HELPER(zend_jit_assign_op_to_typed_prop);
3187
  REGISTER_HELPER(zend_jit_assign_obj_op_helper);
3188
  REGISTER_HELPER(zend_jit_invalid_property_incdec);
3189
  REGISTER_HELPER(zend_jit_inc_typed_prop);
3190
  REGISTER_HELPER(zend_jit_dec_typed_prop);
3191
  REGISTER_HELPER(zend_jit_pre_inc_typed_prop);
3192
  REGISTER_HELPER(zend_jit_post_inc_typed_prop);
3193
  REGISTER_HELPER(zend_jit_pre_dec_typed_prop);
3194
  REGISTER_HELPER(zend_jit_post_dec_typed_prop);
3195
  REGISTER_HELPER(zend_jit_pre_inc_obj_helper);
3196
  REGISTER_HELPER(zend_jit_post_inc_obj_helper);
3197
  REGISTER_HELPER(zend_jit_pre_dec_obj_helper);
3198
  REGISTER_HELPER(zend_jit_post_dec_obj_helper);
3199
  REGISTER_HELPER(zend_jit_uninit_static_prop);
3200
  REGISTER_HELPER(zend_jit_rope_end);
3201
  REGISTER_HELPER(zend_fcall_interrupt);
3202
3203
#ifndef ZTS
3204
  REGISTER_DATA(EG(current_execute_data));
3205
  REGISTER_DATA(EG(exception));
3206
  REGISTER_DATA(EG(opline_before_exception));
3207
  REGISTER_DATA(EG(vm_interrupt));
3208
  REGISTER_DATA(EG(timed_out));
3209
  REGISTER_DATA(EG(uninitialized_zval));
3210
  REGISTER_DATA(EG(zend_constants));
3211
  REGISTER_DATA(EG(jit_trace_num));
3212
  REGISTER_DATA(EG(vm_stack_top));
3213
  REGISTER_DATA(EG(vm_stack_end));
3214
  REGISTER_DATA(EG(exception_op));
3215
  REGISTER_DATA(EG(symbol_table));
3216
3217
  REGISTER_DATA(CG(map_ptr_base));
3218
#else /* ZTS */
3219
  REGISTER_HELPER(zend_jit_get_tsrm_ls_cache);
3220
#endif
3221
#endif
3222
0
}
3223
3224
static void zend_jit_calc_trace_prologue_size(void)
3225
0
{
3226
0
  zend_jit_ctx jit_ctx;
3227
0
  zend_jit_ctx *jit = &jit_ctx;
3228
0
  void *entry;
3229
0
  size_t size;
3230
3231
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);
3232
3233
0
  if (!GCC_GLOBAL_REGS) {
3234
0
    if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
3235
0
      ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
3236
0
      ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
3237
0
      jit_STORE_FP(jit, execute_data_ref);
3238
0
      jit_STORE_IP(jit, opline_ref);
3239
0
    }
3240
0
    jit->ctx.flags |= IR_FASTCALL_FUNC;
3241
0
  }
3242
3243
0
  ir_UNREACHABLE();
3244
3245
0
  entry = zend_jit_ir_compile(&jit->ctx, &size, "JIT$trace_prologue");
3246
0
  zend_jit_free_ctx(jit);
3247
3248
0
  if (!entry) {
3249
0
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: could not compile prologue");
3250
0
  }
3251
3252
0
  zend_jit_trace_prologue_size = size;
3253
0
}
3254
3255
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
3256
static uintptr_t zend_jit_hybrid_vm_sp_adj = 0;
3257
3258
typedef struct _Unwind_Context _Unwind_Context;
3259
typedef int (*_Unwind_Trace_Fn)(_Unwind_Context *, void *);
3260
extern int _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
3261
extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
3262
3263
typedef struct _zend_jit_unwind_arg {
3264
  int cnt;
3265
  uintptr_t cfa[3];
3266
} zend_jit_unwind_arg;
3267
3268
static int zend_jit_unwind_cb(_Unwind_Context *ctx, void *a)
3269
0
{
3270
0
  zend_jit_unwind_arg *arg = (zend_jit_unwind_arg*)a;
3271
0
  arg->cfa[arg->cnt] = _Unwind_GetCFA(ctx);
3272
0
  arg->cnt++;
3273
0
  if (arg->cnt == 3) {
3274
0
    return 5; // _URC_END_OF_STACK
3275
0
  }
3276
0
  return 0; // _URC_NO_REASON;
3277
0
}
3278
3279
static void ZEND_FASTCALL zend_jit_touch_vm_stack_data(void *vm_stack_data)
3280
0
{
3281
0
  zend_jit_unwind_arg arg;
3282
0
3283
0
  memset(&arg, 0, sizeof(arg));
3284
0
  _Unwind_Backtrace(zend_jit_unwind_cb, &arg);
3285
0
  if (arg.cnt == 3) {
3286
0
    zend_jit_hybrid_vm_sp_adj = arg.cfa[2] - arg.cfa[1];
3287
0
  }
3288
0
}
3289
3290
extern void (ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data);
3291
3292
static zend_never_inline void zend_jit_set_sp_adj_vm(void)
3293
0
{
3294
0
  void (ZEND_FASTCALL *orig_zend_touch_vm_stack_data)(void *);
3295
0
3296
0
  orig_zend_touch_vm_stack_data = zend_touch_vm_stack_data;
3297
0
  zend_touch_vm_stack_data = zend_jit_touch_vm_stack_data;
3298
0
  execute_ex(NULL);                                        // set sp_adj[SP_ADJ_VM]
3299
0
  zend_touch_vm_stack_data = orig_zend_touch_vm_stack_data;
3300
0
}
3301
#endif
3302
3303
#ifdef _WIN64
3304
/*
3305
 * We use a single unwind entry for the whole JIT buffer.
3306
 * This works, because all the JIT-ed PHP functions have the same "fixed stack frame".
3307
 */
3308
static PRUNTIME_FUNCTION zend_jit_uw_func = NULL;
3309
3310
#ifdef ZEND_JIT_RT_UNWINDER
3311
static PRUNTIME_FUNCTION zend_jit_unwind_callback(DWORD64 pc, PVOID context)
3312
{
3313
  return zend_jit_uw_func;
3314
}
3315
#endif
3316
3317
static void zend_jit_setup_unwinder(void)
3318
{
3319
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
3320
  /* TAILCALL VM: fixed_save_regset=0, no registers pushed in prologue.
3321
   * fixed_stack_frame_size=40, fixed_call_stack_size=48 (16+IR_SHADOW_ARGS).
3322
   * Prologue is: sub rsp, 0x58 (88 bytes = 40+48). */
3323
  static const unsigned char uw_data[] = {
3324
    0x01, // Version=1, Flags=0
3325
    0x04, // Size of prolog (sub rsp,imm8 = 4 bytes: 48 83 ec 58)
3326
    0x01, // Count of unwind codes
3327
    0x00, // Frame Register=none
3328
    0x04, 0xa2, // offset 4: UWOP_ALLOC_SMALL info=10, alloc=(10+1)*8=88
3329
    0x00, 0x00, // padding
3330
  };
3331
  /* Exit call variant: base 88 + 304 (shadow+GP+FP+padding) = 392 (0x188) */
3332
  static const unsigned char uw_data_exitcall[] = {
3333
    0x01, // Version=1, Flags=0
3334
    0x07, // Size of prolog (sub rsp,imm32 = 7 bytes: 48 81 ec 88 01 00 00)
3335
    0x02, // Count of unwind codes
3336
    0x00, // Frame Register=none
3337
    0x07, 0x01, 0x31, 0x00, // offset 7: UWOP_ALLOC_LARGE info=0, size/8=49, alloc=392
3338
  };
3339
#else
3340
  /* Hardcoded SEH unwind data for JIT-ed PHP functions with "fixed stack frame" */
3341
  static const unsigned char uw_data[] = {
3342
    0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
3343
    0x10, // UBYTE Size of prolog
3344
    0x09, // UBYTE Count of unwind codes
3345
    0x00, // UBYTE: 4 Frame Register, UBYTE: 4 Frame Register offset (scaled)
3346
    // USHORT * n Unwind codes array
3347
    0x10, 0x82, // c: subq $0x48, %rsp
3348
    0x0c, 0xf0, // a: pushq %r15
3349
    0x0a, 0xe0, // 8: pushq %r14
3350
    0x08, 0xd0, // 6: pushq %r13
3351
    0x06, 0xc0, // 4: pushq %r12
3352
    0x04, 0x70, // 3: pushq %rdi
3353
    0x03, 0x60, // 2: pushq %rsi
3354
    0x02, 0x50, // 1: pushq %rbp
3355
    0x01, 0x30, // 0: pushq %rbx
3356
    0x00, 0x00,
3357
  };
3358
  static const unsigned char uw_data_exitcall[] = {
3359
    0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
3360
    0x10, // UBYTE Size of prolog
3361
    0x0a, // UBYTE Count of unwind codes
3362
    0x00, // UBYTE: 4 Frame Register, UBYTE: 4 Frame Register offset (scaled)
3363
    // USHORT * n Unwind codes array
3364
    0x10, 0x01, 0x2f, 0x00, // c: subq 376, %rsp ; 0x48 + 32+16*8+16*8+8+8
3365
    0x0c, 0xf0, // a: pushq %r15
3366
    0x0a, 0xe0, // 8: pushq %r14
3367
    0x08, 0xd0, // 6: pushq %r13
3368
    0x06, 0xc0, // 4: pushq %r12
3369
    0x04, 0x70, // 3: pushq %rdi
3370
    0x03, 0x60, // 2: pushq %rsi
3371
    0x02, 0x50, // 1: pushq %rbp
3372
    0x01, 0x30, // 0: pushq %rbx
3373
  };
3374
#endif
3375
3376
  zend_jit_uw_func = (PRUNTIME_FUNCTION)*dasm_ptr;
3377
  *dasm_ptr = (char*)*dasm_ptr + ZEND_MM_ALIGNED_SIZE_EX(sizeof(RUNTIME_FUNCTION) * 4 +
3378
    sizeof(uw_data) + sizeof(uw_data_exitcall) + sizeof(uw_data), 16);
3379
3380
  zend_jit_uw_func[0].BeginAddress = 0;
3381
  zend_jit_uw_func[1].BeginAddress = (uintptr_t)zend_jit_stub_handlers[jit_stub_trace_exit] - (uintptr_t)dasm_buf;
3382
  zend_jit_uw_func[2].BeginAddress = (uintptr_t)zend_jit_stub_handlers[jit_stub_undefined_offset] - (uintptr_t)dasm_buf;
3383
3384
  zend_jit_uw_func[0].EndAddress = zend_jit_uw_func[1].BeginAddress;
3385
  zend_jit_uw_func[1].EndAddress = zend_jit_uw_func[2].BeginAddress;
3386
  zend_jit_uw_func[2].EndAddress = (uintptr_t)dasm_end - (uintptr_t)dasm_buf;
3387
3388
  zend_jit_uw_func[0].UnwindData = (uintptr_t)zend_jit_uw_func  - (uintptr_t)dasm_buf + sizeof(RUNTIME_FUNCTION) * 4;
3389
  zend_jit_uw_func[1].UnwindData = zend_jit_uw_func[0].UnwindData + sizeof(uw_data);
3390
  zend_jit_uw_func[2].UnwindData = zend_jit_uw_func[1].UnwindData + sizeof(uw_data_exitcall);
3391
3392
  memcpy((char*)dasm_buf + zend_jit_uw_func[0].UnwindData, uw_data, sizeof(uw_data));
3393
  memcpy((char*)dasm_buf + zend_jit_uw_func[1].UnwindData, uw_data_exitcall, sizeof(uw_data_exitcall));
3394
  memcpy((char*)dasm_buf + zend_jit_uw_func[2].UnwindData, uw_data, sizeof(uw_data));
3395
3396
#ifdef ZEND_JIT_RT_UNWINDER
3397
  RtlInstallFunctionTableCallback(
3398
    (uintptr_t)dasm_buf | 3,
3399
    (uintptr_t) dasm_buf,
3400
    (uintptr_t)dasm_end - (uintptr_t)dasm_buf,
3401
    zend_jit_unwind_callback,
3402
    NULL,
3403
    NULL);
3404
#else
3405
  RtlAddFunctionTable(zend_jit_uw_func, 3, (uintptr_t)dasm_buf);
3406
#endif
3407
}
3408
#endif
3409
3410
static void zend_jit_setup(bool reattached)
3411
0
{
3412
#if defined(IR_TARGET_X86)
3413
  if (!zend_cpu_supports_sse2()) {
3414
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: CPU doesn't support SSE2");
3415
  }
3416
#endif
3417
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
3418
0
  allowed_opt_flags = 0;
3419
0
  if (zend_cpu_supports_avx()) {
3420
0
    allowed_opt_flags |= ZEND_JIT_CPU_AVX;
3421
0
  }
3422
0
# ifdef HAVE_ZEND_CPU_SUPPORTS_CLDEMOTE
3423
0
  if (zend_cpu_supports_cldemote()) {
3424
0
    default_mflags |= IR_X86_CLDEMOTE;
3425
0
  }
3426
0
# endif
3427
0
#endif
3428
3429
#ifdef ZTS
3430
  zend_result result = zend_jit_resolve_tsrm_ls_cache_offsets(
3431
    &tsrm_ls_cache_tcb_offset,
3432
    &tsrm_tls_index,
3433
    &tsrm_tls_offset
3434
  );
3435
  if (result == FAILURE) {
3436
    zend_accel_error(ACCEL_LOG_INFO,
3437
        "Could not get _tsrm_ls_cache offsets, will fallback to runtime resolution");
3438
  }
3439
#endif
3440
3441
0
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
3442
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
3443
0
    zend_jit_set_sp_adj_vm(); // set zend_jit_hybrid_vm_sp_adj
3444
0
  }
3445
0
#endif
3446
3447
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
3448
0
    zend_jit_setup_disasm();
3449
0
  }
3450
3451
0
#ifndef _WIN32
3452
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3453
0
    ir_perf_jitdump_open();
3454
0
  }
3455
3456
0
#endif
3457
0
  zend_long debug = JIT_G(debug);
3458
0
  if (!(debug & ZEND_JIT_DEBUG_ASM_STUBS)) {
3459
0
    JIT_G(debug) &= ~(ZEND_JIT_DEBUG_IR_SRC|ZEND_JIT_DEBUG_IR_FINAL|
3460
0
      ZEND_JIT_DEBUG_IR_CODEGEN|
3461
0
      ZEND_JIT_DEBUG_IR_AFTER_SCCP|ZEND_JIT_DEBUG_IR_AFTER_CFG|ZEND_JIT_DEBUG_IR_AFTER_GCM|
3462
0
      ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE|ZEND_JIT_DEBUG_IR_AFTER_REGS);
3463
0
  }
3464
3465
0
  zend_jit_calc_trace_prologue_size();
3466
0
  if (!reattached) {
3467
0
    zend_jit_setup_stubs();
3468
0
  }
3469
0
  JIT_G(debug) = debug;
3470
3471
#ifdef _WIN64
3472
  zend_jit_setup_unwinder();
3473
#endif
3474
0
}
3475
3476
static void zend_jit_shutdown_ir(void)
3477
0
{
3478
0
#ifndef _WIN32
3479
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3480
0
    ir_perf_jitdump_close();
3481
0
  }
3482
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
3483
0
    ir_gdb_unregister_all();
3484
0
  }
3485
0
#endif
3486
#ifdef HAVE_CAPSTONE
3487
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
3488
    ir_disasm_free();
3489
  }
3490
#endif
3491
0
}
3492
3493
/* PHP control flow reconstruction helpers */
3494
static ir_ref jit_IF_ex(zend_jit_ctx *jit, ir_ref condition, ir_ref true_block)
3495
0
{
3496
0
  ir_ref ref = ir_IF(condition);
3497
  /* op3 is used as a temporary storage for PHP BB number to reconstruct PHP control flow.
3498
   *
3499
   * It's used in jit_IF_TRUE_FALSE_ex() to select IF_TRUE or IF_FALSE instructions
3500
   * to start target block
3501
   */
3502
0
  ir_set_op(&jit->ctx, ref, 3, true_block);
3503
0
  return ref;
3504
0
}
3505
3506
static void jit_IF_TRUE_FALSE_ex(zend_jit_ctx *jit, ir_ref if_ref, ir_ref true_block)
3507
0
{
3508
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3509
0
  ZEND_ASSERT(if_ref);
3510
0
  ZEND_ASSERT(jit->ctx.ir_base[if_ref].op == IR_IF);
3511
0
  ZEND_ASSERT(jit->ctx.ir_base[if_ref].op3);
3512
0
  if (jit->ctx.ir_base[if_ref].op3 == true_block) {
3513
0
    ir_IF_TRUE(if_ref);
3514
0
  } else {
3515
0
    ir_IF_FALSE(if_ref);
3516
0
  }
3517
0
}
3518
3519
static void zend_jit_case_start(zend_jit_ctx *jit, int switch_b, int case_b, ir_ref switch_ref);
3520
3521
static void _zend_jit_add_predecessor_ref(zend_jit_ctx *jit, int b, int pred, ir_ref ref)
3522
0
{
3523
0
  int *p;
3524
0
  zend_basic_block *bb;
3525
0
  ir_ref *r, header;
3526
3527
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3528
0
  bb = &jit->ssa->cfg.blocks[b];
3529
0
  p = &jit->ssa->cfg.predecessors[bb->predecessor_offset];
3530
0
  r = &jit->bb_edges[jit->bb_predecessors[b]];
3531
0
  for (uint32_t i = 0; i < bb->predecessors_count; i++, p++, r++) {
3532
0
    if (*p == pred) {
3533
0
      ZEND_ASSERT(*r == IR_UNUSED || *r == ref);
3534
0
      header = jit->bb_start_ref[b];
3535
0
      if (header) {
3536
        /* this is back edge */
3537
0
        ZEND_ASSERT(jit->ctx.ir_base[header].op == IR_LOOP_BEGIN);
3538
0
        if (jit->ctx.ir_base[ref].op == IR_END) {
3539
0
          jit->ctx.ir_base[ref].op = IR_LOOP_END;
3540
0
        } else if (jit->ctx.ir_base[ref].op == IR_IF) {
3541
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3542
0
          ref = ir_LOOP_END();
3543
0
        } else if (jit->ctx.ir_base[ref].op == IR_SWITCH) {
3544
0
          zend_jit_case_start(jit, pred, b, ref);
3545
0
          ref = ir_LOOP_END();
3546
0
        } else if (jit->ctx.ir_base[ref].op == IR_UNREACHABLE) {
3547
0
          ir_BEGIN(ref);
3548
0
          ref = ir_LOOP_END();
3549
0
        } else {
3550
0
          ZEND_UNREACHABLE();
3551
0
        }
3552
0
        ir_MERGE_SET_OP(header, i + 1, ref);
3553
0
      }
3554
0
      *r = ref;
3555
0
      return;
3556
0
    }
3557
0
  }
3558
0
  ZEND_UNREACHABLE();
3559
0
}
3560
3561
static void _zend_jit_merge_smart_branch_inputs(zend_jit_ctx *jit,
3562
                                                uint32_t      true_label,
3563
                                                uint32_t      false_label,
3564
                                                ir_ref        true_inputs,
3565
                                                ir_ref        false_inputs)
3566
0
{
3567
0
  ir_ref true_path = IR_UNUSED, false_path = IR_UNUSED;
3568
3569
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3570
0
  if (true_inputs) {
3571
0
    ZEND_ASSERT(jit->ctx.ir_base[true_inputs].op == IR_END);
3572
0
    if (!jit->ctx.ir_base[true_inputs].op2) {
3573
0
      true_path = true_inputs;
3574
0
    } else {
3575
0
      ir_MERGE_list(true_inputs);
3576
0
      true_path = ir_END();
3577
0
    }
3578
0
  }
3579
0
  if (false_inputs) {
3580
0
    ZEND_ASSERT(jit->ctx.ir_base[false_inputs].op == IR_END);
3581
0
    if (!jit->ctx.ir_base[false_inputs].op2) {
3582
0
      false_path = false_inputs;
3583
0
    } else {
3584
0
      ir_MERGE_list(false_inputs);
3585
0
      false_path = ir_END();
3586
0
    }
3587
0
  }
3588
3589
0
  if (true_label == false_label && true_path && false_path) {
3590
0
    ir_MERGE_2(true_path, false_path);
3591
0
    _zend_jit_add_predecessor_ref(jit, true_label, jit->b, ir_END());
3592
0
  } else if (!true_path && !false_path) {
3593
    /* dead code */
3594
0
    true_path = ir_END();
3595
0
    _zend_jit_add_predecessor_ref(jit, true_label, jit->b, true_path);
3596
0
  } else {
3597
0
    if (true_path) {
3598
0
      _zend_jit_add_predecessor_ref(jit, true_label, jit->b, true_path);
3599
0
    }
3600
0
    if (false_path) {
3601
0
      _zend_jit_add_predecessor_ref(jit, false_label, jit->b, false_path);
3602
0
    }
3603
0
  }
3604
3605
0
  jit->b = -1;
3606
0
}
3607
3608
static void _zend_jit_fix_merges(zend_jit_ctx *jit)
3609
0
{
3610
0
  int i, count;
3611
0
  ir_ref j, k, n, *p, *q, *r;
3612
0
  ir_ref ref;
3613
0
  ir_insn *insn, *phi;
3614
3615
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3616
0
  count = jit->ssa->cfg.blocks_count;
3617
0
  for (i = 0, p = jit->bb_start_ref; i < count; i++, p++) {
3618
0
    ref = *p;
3619
0
    if (ref) {
3620
0
      insn = &jit->ctx.ir_base[ref];
3621
0
      if (insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN) {
3622
0
        n = insn->inputs_count;
3623
        /* Remove IS_UNUSED inputs */
3624
0
        for (j = k = 0, q = r = insn->ops + 1; j < n; j++, q++) {
3625
0
          if (*q) {
3626
0
            if (q != r) {
3627
0
              *r = *q;
3628
0
              phi = insn + 1 + (n >> 2);
3629
0
              while (phi->op == IR_PI) {
3630
0
                phi++;
3631
0
              }
3632
0
              while (phi->op == IR_PHI) {
3633
0
                ir_insn_set_op(phi, k + 2, ir_insn_op(phi, j + 2));
3634
0
                phi += 1 + ((n + 1) >> 2);
3635
0
              }
3636
0
            }
3637
0
            k++;
3638
0
            r++;
3639
0
          }
3640
0
        }
3641
0
        if (k != n) {
3642
0
          ir_ref n2, k2;
3643
3644
0
          if (k <= 1) {
3645
0
            insn->op = IR_BEGIN;
3646
0
            insn->inputs_count = 0;
3647
0
          } else {
3648
0
            insn->inputs_count = k;
3649
0
          }
3650
0
          n2 = 1 + (n >> 2);
3651
0
          k2 = 1 + (k >> 2);
3652
0
          while (k2 != n2) {
3653
0
            (insn+k2)->optx = IR_NOP;
3654
0
            k2++;
3655
0
          }
3656
0
          phi = insn + 1 + (n >> 2);
3657
0
          while (phi->op == IR_PI) {
3658
0
            phi++;
3659
0
          }
3660
0
          while (phi->op == IR_PHI) {
3661
0
            if (k <= 1) {
3662
0
              phi->op = IR_COPY;
3663
0
              phi->op1 = phi->op2;
3664
0
              phi->op2 = 1;
3665
0
              phi->inputs_count = 0;
3666
0
            } else {
3667
0
              phi->inputs_count = k + 1;
3668
0
            }
3669
0
            n2 = 1 + ((n + 1) >> 2);
3670
0
            k2 = 1 + ((k + 1) >> 2);
3671
0
            while (k2 != n2) {
3672
0
              (phi+k2)->optx = IR_NOP;
3673
0
              k2++;
3674
0
            }
3675
0
            phi += 1 + ((n + 1) >> 2);
3676
0
          }
3677
0
        }
3678
0
      }
3679
0
    }
3680
0
  }
3681
0
}
3682
3683
static void zend_jit_case_start(zend_jit_ctx *jit, int switch_b, int case_b, ir_ref switch_ref)
3684
0
{
3685
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[switch_b];
3686
0
  const zend_op *opline = &jit->op_array->opcodes[bb->start + bb->len - 1];
3687
3688
0
  if (opline->opcode == ZEND_SWITCH_LONG
3689
0
   || opline->opcode == ZEND_SWITCH_STRING
3690
0
   || opline->opcode == ZEND_MATCH) {
3691
0
    HashTable *jumptable = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
3692
0
    const zend_op *default_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
3693
0
    int default_b = jit->ssa->cfg.map[default_opline - jit->op_array->opcodes];
3694
0
    zval *zv;
3695
0
    ir_ref list = IR_UNUSED, idx;
3696
0
    bool first = true;
3697
3698
0
    ZEND_HASH_FOREACH_VAL(jumptable, zv) {
3699
0
      const zend_op *target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
3700
0
      int b = jit->ssa->cfg.map[target - jit->op_array->opcodes];
3701
3702
0
      if (b == case_b) {
3703
0
        if (!first) {
3704
0
          ir_END_list(list);
3705
0
        }
3706
0
        if (HT_IS_PACKED(jumptable)) {
3707
0
          idx = ir_CONST_LONG(zv - jumptable->arPacked);
3708
0
        } else {
3709
0
          idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
3710
0
        }
3711
0
        ir_CASE_VAL(switch_ref, idx);
3712
0
        first = false;
3713
0
      }
3714
0
    } ZEND_HASH_FOREACH_END();
3715
0
    if (default_b == case_b) {
3716
0
      if (!first) {
3717
0
        ir_END_list(list);
3718
0
      }
3719
0
      if (jit->ctx.ir_base[switch_ref].op3) {
3720
        /* op3 may contain a list of additional "default" path inputs for MATCH */
3721
0
        ir_ref ref = jit->ctx.ir_base[switch_ref].op3;
3722
0
        jit->ctx.ir_base[switch_ref].op3 = IS_UNDEF;
3723
0
        ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_END);
3724
0
        ir_ref end = ref;
3725
0
        while (jit->ctx.ir_base[end].op2) {
3726
0
          ZEND_ASSERT(jit->ctx.ir_base[end].op == IR_END);
3727
0
          end = jit->ctx.ir_base[end].op2;
3728
0
        }
3729
0
        jit->ctx.ir_base[end].op2 = list;
3730
0
        list = ref;
3731
0
      }
3732
0
      ir_CASE_DEFAULT(switch_ref);
3733
0
    }
3734
0
    if (list) {
3735
0
      ir_END_list(list);
3736
0
      ir_MERGE_list(list);
3737
0
    }
3738
0
  } else {
3739
0
    ZEND_UNREACHABLE();
3740
0
  }
3741
0
}
3742
3743
static int zend_jit_bb_start(zend_jit_ctx *jit, int b)
3744
0
{
3745
0
  zend_basic_block *bb;
3746
0
  int *p, pred;
3747
0
  ir_ref ref, bb_start;
3748
3749
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3750
0
  ZEND_ASSERT(b < jit->ssa->cfg.blocks_count);
3751
0
  bb = &jit->ssa->cfg.blocks[b];
3752
0
  ZEND_ASSERT((bb->flags & ZEND_BB_REACHABLE) != 0);
3753
0
  uint32_t n = bb->predecessors_count;
3754
3755
0
  if (n == 0) {
3756
    /* pass */
3757
0
    ZEND_ASSERT(jit->ctx.control);
3758
0
#if ZEND_DEBUG
3759
0
    ref = jit->ctx.control;
3760
0
    ir_insn *insn = &jit->ctx.ir_base[ref];
3761
0
    while (insn->op >= IR_CALL && insn->op <= IR_TRAP) {
3762
0
      ref = insn->op1;
3763
0
      insn = &jit->ctx.ir_base[ref];
3764
0
    }
3765
0
    ZEND_ASSERT(insn->op == IR_START);
3766
0
    ZEND_ASSERT(ref == 1);
3767
0
#endif
3768
0
    bb_start = 1;
3769
0
    if (jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY) {
3770
      /* prvent END/BEGIN merging */
3771
0
      jit->ctx.control = ir_emit1(&jit->ctx, IR_BEGIN, ir_END());
3772
0
      bb_start = jit->ctx.control;
3773
0
    }
3774
0
  } else if (n == 1) {
3775
0
    ZEND_ASSERT(!jit->ctx.control);
3776
0
    pred = jit->ssa->cfg.predecessors[bb->predecessor_offset];
3777
0
    ref = jit->bb_edges[jit->bb_predecessors[b]];
3778
0
    if (ref == IR_UNUSED) {
3779
0
      if (!jit->ctx.control) {
3780
0
        ir_BEGIN(IR_UNUSED); /* unreachable block */
3781
0
      }
3782
0
    } else {
3783
0
      ir_op op = jit->ctx.ir_base[ref].op;
3784
3785
0
      if (op == IR_IF) {
3786
0
        if (!jit->ctx.control) {
3787
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3788
0
        } else {
3789
0
          ir_ref entry_path = ir_END();
3790
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3791
0
          ir_MERGE_WITH(entry_path);
3792
0
        }
3793
0
      } else if (op == IR_SWITCH) {
3794
0
        zend_jit_case_start(jit, pred, b, ref);
3795
0
      } else {
3796
0
        if (!jit->ctx.control) {
3797
0
          ZEND_ASSERT(op == IR_END || op == IR_UNREACHABLE || op == IR_RETURN);
3798
0
          if ((jit->ssa->cfg.blocks[b].flags & ZEND_BB_RECV_ENTRY)
3799
0
           && (jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY)) {
3800
            /* prvent END/BEGIN merging */
3801
0
            jit->ctx.control = ir_emit1(&jit->ctx, IR_BEGIN, ref);
3802
0
          } else {
3803
0
            ir_BEGIN(ref);
3804
0
          }
3805
0
        } else {
3806
0
          ir_MERGE_WITH(ref);
3807
0
        }
3808
0
      }
3809
0
    }
3810
0
    bb_start = jit->ctx.control;
3811
0
  } else {
3812
0
    int forward_edges_count = 0;
3813
0
    int back_edges_count = 0;
3814
0
    ir_ref *pred_refs;
3815
0
    ir_ref entry_path = IR_UNUSED;
3816
0
    ALLOCA_FLAG(use_heap);
3817
3818
0
    ZEND_ASSERT(!jit->ctx.control);
3819
0
    if (jit->ctx.control) {
3820
0
      entry_path = ir_END();
3821
0
    }
3822
0
    pred_refs = (ir_ref *)do_alloca(sizeof(ir_ref) * n, use_heap);
3823
0
    uint32_t i;
3824
0
    for (i = 0, p = jit->ssa->cfg.predecessors + bb->predecessor_offset; i < n; p++, i++) {
3825
0
      pred = *p;
3826
0
      if (jit->bb_start_ref[pred]) {
3827
        /* forward edge */
3828
0
        forward_edges_count++;
3829
0
        ref = jit->bb_edges[jit->bb_predecessors[b] + i];
3830
0
        if (ref == IR_UNUSED) {
3831
          /* dead edge */
3832
0
          pred_refs[i] = IR_UNUSED;
3833
0
        } else {
3834
0
          ir_op op = jit->ctx.ir_base[ref].op;
3835
3836
0
          if (op == IR_IF) {
3837
0
            jit_IF_TRUE_FALSE_ex(jit, ref, b);
3838
0
            pred_refs[i] = ir_END();
3839
0
          } else if (op == IR_SWITCH) {
3840
0
            zend_jit_case_start(jit, pred, b, ref);
3841
0
            pred_refs[i] = ir_END();
3842
0
          } else {
3843
0
            ZEND_ASSERT(op == IR_END || op == IR_UNREACHABLE || op == IR_RETURN);
3844
0
            pred_refs[i] = ref;
3845
0
          }
3846
0
        }
3847
0
      } else {
3848
        /* backward edge */
3849
0
        back_edges_count++;
3850
0
        pred_refs[i] = IR_UNUSED;
3851
0
      }
3852
0
    }
3853
3854
0
    if (bb->flags & ZEND_BB_LOOP_HEADER) {
3855
0
      ZEND_ASSERT(back_edges_count != 0);
3856
0
      ZEND_ASSERT(forward_edges_count != 0);
3857
0
      ir_MERGE_N(n, pred_refs);
3858
0
      jit->ctx.ir_base[jit->ctx.control].op = IR_LOOP_BEGIN;
3859
0
      bb_start = jit->ctx.control;
3860
0
      if (entry_path) {
3861
0
        ir_MERGE_WITH(entry_path);
3862
0
      }
3863
0
    } else {
3864
//      ZEND_ASSERT(back_edges_count != 0);
3865
      /* edges from exceptional blocks may be counted as back edges */
3866
0
      ir_MERGE_N(n, pred_refs);
3867
0
      bb_start = jit->ctx.control;
3868
0
      if (entry_path) {
3869
0
        ir_MERGE_WITH(entry_path);
3870
0
      }
3871
0
    }
3872
0
    free_alloca(pred_refs, use_heap);
3873
0
  }
3874
0
  jit->b = b;
3875
0
  jit->bb_start_ref[b] = bb_start;
3876
3877
0
  if ((bb->flags & ZEND_BB_ENTRY) || (bb->idom >= 0 && jit->bb_start_ref[bb->idom] < jit->ctx.fold_cse_limit)) {
3878
0
    jit->ctx.fold_cse_limit = bb_start;
3879
0
  }
3880
3881
0
  return 1;
3882
0
}
3883
3884
static int zend_jit_bb_end(zend_jit_ctx *jit, int b)
3885
0
{
3886
0
  int succ;
3887
0
  zend_basic_block *bb;
3888
3889
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3890
0
  if (jit->b != b) {
3891
0
    return 1;
3892
0
  }
3893
3894
0
  bb = &jit->ssa->cfg.blocks[b];
3895
0
  ZEND_ASSERT(bb->successors_count != 0);
3896
0
  if (bb->successors_count == 1) {
3897
0
    succ = bb->successors[0];
3898
0
  } else {
3899
0
    const zend_op *opline = &jit->op_array->opcodes[bb->start + bb->len - 1];
3900
3901
    /* Use only the following successor of SWITCH and FE_RESET_R */
3902
0
    ZEND_ASSERT(opline->opcode == ZEND_SWITCH_LONG
3903
0
     || opline->opcode == ZEND_SWITCH_STRING
3904
0
     || opline->opcode == ZEND_MATCH
3905
0
     || opline->opcode == ZEND_FE_RESET_R);
3906
0
    succ = b + 1;
3907
0
  }
3908
0
  _zend_jit_add_predecessor_ref(jit, succ, b, ir_END());
3909
0
  jit->b = -1;
3910
0
  return 1;
3911
0
}
3912
3913
static int jit_CMP_IP(zend_jit_ctx *jit, ir_op op, const zend_op *next_opline)
3914
0
{
3915
0
  ir_ref ref;
3916
3917
0
#if 1
3918
0
  ref = jit_IP32(jit);
3919
0
  ref = ir_CMP_OP(op, ref, ir_CONST_U32((uint32_t)(uintptr_t)next_opline));
3920
#else
3921
  ref = jit_IP(jit);
3922
  ref = ir_CMP_OP(op, ref, ir_CONST_ADDR(next_opline));
3923
#endif
3924
0
  return ref;
3925
0
}
3926
3927
static int zend_jit_jmp_frameless(
3928
  zend_jit_ctx *jit,
3929
  const zend_op *opline,
3930
  const void *exit_addr,
3931
  zend_jmp_fl_result guard
3932
0
) {
3933
0
  ir_ref ref, if_ref, cache_result, function_result, phi_result, cache_slot_ref;
3934
0
  zend_basic_block *bb;
3935
3936
  // JIT: CACHED_PTR(opline->extended_value)
3937
0
  cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value);
3938
0
  cache_result = ir_LOAD_L(cache_slot_ref);
3939
3940
  // JIT: if (UNEXPECTED(!result))
3941
0
  if_ref = ir_IF(cache_result);
3942
0
  ir_IF_FALSE_cold(if_ref);
3943
0
  zval *func_name_zv = RT_CONSTANT(opline, opline->op1);
3944
0
  function_result = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_jit_jmp_frameless_helper),
3945
0
    ir_CONST_ADDR(func_name_zv),
3946
0
    cache_slot_ref);
3947
0
  ir_MERGE_WITH_EMPTY_TRUE(if_ref);
3948
3949
0
  phi_result = ir_PHI_2(IR_LONG, function_result, cache_result);
3950
3951
0
  if (exit_addr) {
3952
0
    ir_GUARD(ir_EQ(phi_result, ir_CONST_LONG(guard)), ir_CONST_ADDR(exit_addr));
3953
0
  } else {
3954
0
    ZEND_ASSERT(jit->b >= 0);
3955
0
    bb = &jit->ssa->cfg.blocks[jit->b];
3956
    // JIT: if (result == ZEND_JMP_FL_HIT)
3957
0
    ref = jit_IF_ex(jit, ir_EQ(phi_result, ir_CONST_LONG(ZEND_JMP_FL_HIT)), bb->successors[0]);
3958
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
3959
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
3960
0
    jit->b = -1;
3961
0
  }
3962
3963
0
  return 1;
3964
0
}
3965
3966
static int zend_jit_cond_jmp(zend_jit_ctx *jit, const zend_op *next_opline, int target_block)
3967
0
{
3968
0
  ir_ref ref;
3969
0
  zend_basic_block *bb;
3970
3971
0
  ZEND_ASSERT(jit->b >= 0);
3972
0
  bb = &jit->ssa->cfg.blocks[jit->b];
3973
3974
0
  ZEND_ASSERT(bb->successors_count == 2);
3975
0
  if (bb->successors[0] == bb->successors[1]) {
3976
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
3977
0
    jit->b = -1;
3978
0
    zend_jit_set_last_valid_opline(jit, next_opline);
3979
0
    return 1;
3980
0
  }
3981
3982
0
  ref = jit_IF_ex(jit, jit_CMP_IP(jit, IR_NE, next_opline), target_block);
3983
3984
0
  _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
3985
0
  _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
3986
3987
0
  jit->b = -1;
3988
0
  zend_jit_set_last_valid_opline(jit, next_opline);
3989
3990
0
  return 1;
3991
0
}
3992
3993
static int zend_jit_set_cond(zend_jit_ctx *jit, const zend_op *opline, const zend_op *next_opline, uint32_t var)
3994
0
{
3995
0
  ir_ref ref;
3996
3997
0
  ir_op op = (opline->result_type & IS_SMART_BRANCH_JMPZ) ? IR_EQ : IR_NE;
3998
0
  ref = ir_ADD_U32(ir_ZEXT_U32(jit_CMP_IP(jit, op, next_opline)), ir_CONST_U32(IS_FALSE));
3999
4000
  // EX_VAR(var) = ...
4001
0
  ir_STORE(ir_ADD_OFFSET(jit_FP(jit), var + offsetof(zval, u1.type_info)), ref);
4002
4003
0
  zend_jit_reset_last_valid_opline(jit);
4004
0
  return zend_jit_set_ip(jit, next_opline - 1);
4005
0
}
4006
4007
/* PHP JIT handlers */
4008
static void zend_jit_check_exception(zend_jit_ctx *jit)
4009
0
{
4010
0
  ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
4011
0
    jit_STUB_ADDR(jit, jit_stub_exception_handler));
4012
0
}
4013
4014
static void zend_jit_check_exception_undef_result(zend_jit_ctx *jit, const zend_op *opline)
4015
0
{
4016
0
  ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
4017
0
    jit_STUB_ADDR(jit,
4018
0
      (opline->result_type & (IS_TMP_VAR|IS_VAR)) ? jit_stub_exception_handler_undef : jit_stub_exception_handler));
4019
0
}
4020
4021
static void zend_jit_type_check_undef(zend_jit_ctx  *jit,
4022
                                      ir_ref         type,
4023
                                      uint32_t       var,
4024
                                      const zend_op *opline,
4025
                                      bool           check_exception,
4026
                                      bool           in_cold_path,
4027
                                      bool           undef_result)
4028
0
{
4029
0
  ir_ref if_def = ir_IF(type);
4030
4031
0
  if (!in_cold_path) {
4032
0
    ir_IF_FALSE_cold(if_def);
4033
0
  } else {
4034
0
    ir_IF_FALSE(if_def);
4035
0
  }
4036
0
  if (opline) {
4037
0
    jit_SET_EX_OPLINE(jit, opline);
4038
0
  }
4039
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(var));
4040
0
  if (check_exception) {
4041
0
    if (undef_result) {
4042
0
      zend_jit_check_exception_undef_result(jit, opline);
4043
0
    } else {
4044
0
      zend_jit_check_exception(jit);
4045
0
    }
4046
0
  }
4047
0
  ir_MERGE_WITH_EMPTY_TRUE(if_def);
4048
0
}
4049
4050
static ir_ref zend_jit_zval_check_undef(zend_jit_ctx  *jit,
4051
                                        ir_ref         ref,
4052
                                        uint32_t       var,
4053
                                        const zend_op *opline,
4054
                                        bool           check_exception)
4055
0
{
4056
0
  ir_ref if_def, ref2;
4057
4058
0
  if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
4059
0
  ir_IF_FALSE_cold(if_def);
4060
4061
0
  if (opline) {
4062
0
    jit_SET_EX_OPLINE(jit, opline);
4063
0
  }
4064
4065
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(var));
4066
4067
0
  if (check_exception) {
4068
0
    zend_jit_check_exception(jit);
4069
0
  }
4070
4071
0
  ref2 = jit_EG(uninitialized_zval);
4072
4073
0
  ir_MERGE_WITH_EMPTY_TRUE(if_def);
4074
4075
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
4076
0
}
4077
4078
static void zend_jit_recv_entry(zend_jit_ctx *jit, int b)
4079
0
{
4080
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[b];
4081
0
  int pred;
4082
0
  ir_ref ref;
4083
4084
0
  ZEND_ASSERT(bb->predecessors_count > 0);
4085
4086
0
  pred = jit->bb_predecessors[b];
4087
0
  ref = jit->bb_edges[pred];
4088
4089
0
  ZEND_ASSERT(ref);
4090
0
  ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_END);
4091
4092
  /* Insert a MERGE block with additional ENTRY input between predecessor and this one */
4093
0
  ir_ENTRY(ref, bb->start);
4094
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4095
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4096
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4097
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4098
0
    jit_STORE_FP(jit, 2);
4099
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4100
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4101
0
    jit_STORE_IP(jit, 3);
4102
0
  }
4103
4104
0
  ir_MERGE_WITH(ref);
4105
0
  jit->bb_edges[pred] = ir_END();
4106
0
}
4107
4108
static void zend_jit_osr_entry(zend_jit_ctx *jit, int b)
4109
0
{
4110
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[b];
4111
0
  ir_ref ref = ir_END();
4112
4113
  /* Insert a MERGE block with additional ENTRY input between predecessor and this one */
4114
0
  ir_ENTRY(ref, bb->start);
4115
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4116
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4117
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4118
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4119
0
    jit_STORE_FP(jit, 2);
4120
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4121
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4122
0
    jit_STORE_IP(jit, 3);
4123
0
  }
4124
4125
0
  ir_MERGE_WITH(ref);
4126
0
}
4127
4128
static ir_ref zend_jit_continue_entry(zend_jit_ctx *jit, ir_ref src, unsigned int label)
4129
0
{
4130
0
  ir_ENTRY(src, label);
4131
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4132
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4133
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4134
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4135
0
    jit_STORE_FP(jit, 2);
4136
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4137
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4138
0
    jit_STORE_IP(jit, 3);
4139
0
  }
4140
0
  return ir_END();
4141
0
}
4142
4143
static int zend_jit_handler(zend_jit_ctx *jit, const zend_op *opline, int may_throw)
4144
0
{
4145
0
  zend_jit_set_ip(jit, opline);
4146
0
  if (GCC_GLOBAL_REGS) {
4147
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4148
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
4149
0
  } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
4150
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4151
0
    ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
4152
0
    jit_STORE_IP(jit, ip);
4153
0
  } else {
4154
0
    zend_vm_opcode_handler_t handler = opline->handler;
4155
0
    ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
4156
0
    jit_STORE_IP(jit, ip);
4157
0
  }
4158
0
  if (may_throw) {
4159
0
    zend_jit_check_exception(jit);
4160
0
  }
4161
  /* Skip the following OP_DATA */
4162
0
  switch (opline->opcode) {
4163
0
    case ZEND_ASSIGN_DIM:
4164
0
    case ZEND_ASSIGN_OBJ:
4165
0
    case ZEND_ASSIGN_STATIC_PROP:
4166
0
    case ZEND_ASSIGN_DIM_OP:
4167
0
    case ZEND_ASSIGN_OBJ_OP:
4168
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
4169
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
4170
0
    case ZEND_ASSIGN_OBJ_REF:
4171
0
    case ZEND_FRAMELESS_ICALL_3:
4172
0
    case ZEND_DECLARE_ATTRIBUTED_CONST:
4173
0
      zend_jit_set_last_valid_opline(jit, opline + 2);
4174
0
      break;
4175
0
    default:
4176
0
      zend_jit_set_last_valid_opline(jit, opline + 1);
4177
0
      break;
4178
0
  }
4179
0
  return 1;
4180
0
}
4181
4182
static int zend_jit_tail_handler(zend_jit_ctx *jit, const zend_op *opline)
4183
0
{
4184
0
  ir_ref ref;
4185
0
  zend_basic_block *bb;
4186
4187
0
  zend_jit_set_ip(jit, opline);
4188
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
4189
0
    if (opline->opcode == ZEND_DO_UCALL ||
4190
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
4191
0
        opline->opcode == ZEND_DO_FCALL ||
4192
0
        opline->opcode == ZEND_RETURN) {
4193
4194
      /* Use inlined HYBRID VM handler */
4195
0
      zend_vm_opcode_handler_t handler = opline->handler;
4196
0
      ir_TAILCALL(IR_VOID, ir_CONST_FUNC(handler));
4197
0
    } else {
4198
0
      zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4199
0
      ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
4200
0
      ref = ir_LOAD_A(jit_IP(jit));
4201
0
      ir_TAILCALL(IR_VOID, ref);
4202
0
    }
4203
0
  } else {
4204
0
    zend_vm_opcode_handler_t handler = opline->handler;
4205
0
    if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
4206
0
      zend_jit_tailcall_handler(jit, ir_CONST_OPCODE_HANDLER_FUNC(handler));
4207
0
    } else if ((jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY)
4208
0
     && (opline->opcode == ZEND_CATCH
4209
0
      || opline->opcode == ZEND_FAST_CALL
4210
0
      || opline->opcode == ZEND_FAST_RET
4211
0
      || opline->opcode == ZEND_MATCH_ERROR
4212
0
      || opline->opcode == ZEND_THROW
4213
0
      || opline->opcode == ZEND_VERIFY_NEVER_TYPE)) {
4214
0
      ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
4215
0
      zend_jit_vm_enter(jit, ip);
4216
0
    } else {
4217
0
      ir_TAILCALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
4218
0
    }
4219
0
  }
4220
0
  if (jit->b >= 0) {
4221
0
    bb = &jit->ssa->cfg.blocks[jit->b];
4222
0
    if (bb->successors_count > 0
4223
0
     && (opline->opcode == ZEND_DO_FCALL
4224
0
      || opline->opcode == ZEND_DO_UCALL
4225
0
      || opline->opcode == ZEND_DO_FCALL_BY_NAME
4226
0
      || opline->opcode == ZEND_INCLUDE_OR_EVAL
4227
0
      || opline->opcode == ZEND_GENERATOR_CREATE
4228
0
      || opline->opcode == ZEND_YIELD
4229
0
      || opline->opcode == ZEND_YIELD_FROM
4230
0
      || opline->opcode == ZEND_FAST_CALL)) {
4231
      /* Add a fake control edge from UNREACHABLE to the following ENTRY */
4232
0
      int succ;
4233
4234
0
      if (bb->successors_count == 1) {
4235
0
        succ = bb->successors[0];
4236
0
        ZEND_ASSERT(jit->ssa->cfg.blocks[succ].flags & ZEND_BB_ENTRY);
4237
0
      } else {
4238
        /* Use only the following successor of FAST_CALL */
4239
0
        ZEND_ASSERT(opline->opcode == ZEND_FAST_CALL);
4240
0
        succ = jit->b + 1;
4241
        /* we need an entry */
4242
0
        jit->ssa->cfg.blocks[succ].flags |= ZEND_BB_ENTRY;
4243
0
      }
4244
0
      ref = jit->ctx.insns_count - 1;
4245
0
      ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_UNREACHABLE || jit->ctx.ir_base[ref].op == IR_RETURN);
4246
0
      ref = zend_jit_continue_entry(jit, ref, jit->ssa->cfg.blocks[succ].start);
4247
0
      _zend_jit_add_predecessor_ref(jit, succ, jit->b, ref);
4248
0
    }
4249
0
    jit->b = -1;
4250
0
    zend_jit_reset_last_valid_opline(jit);
4251
0
  }
4252
0
  return 1;
4253
0
}
4254
4255
static int zend_jit_call(zend_jit_ctx *jit, const zend_op *opline, unsigned int next_block)
4256
0
{
4257
0
  return zend_jit_tail_handler(jit, opline);
4258
0
}
4259
4260
static int zend_jit_spill_store(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info, bool set_type)
4261
0
{
4262
0
  ZEND_ASSERT(Z_MODE(src) == IS_REG);
4263
0
  ZEND_ASSERT(Z_MODE(dst) == IS_MEM_ZVAL);
4264
4265
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4266
0
    jit_set_Z_LVAL(jit, dst, zend_jit_use_reg(jit, src));
4267
0
    if (set_type &&
4268
0
        (Z_REG(dst) != ZREG_FP ||
4269
0
         !JIT_G(current_frame) ||
4270
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4271
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4272
0
    }
4273
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4274
0
    jit_set_Z_DVAL(jit, dst, zend_jit_use_reg(jit, src));
4275
0
    if (set_type &&
4276
0
        (Z_REG(dst) != ZREG_FP ||
4277
0
         !JIT_G(current_frame) ||
4278
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4279
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4280
0
    }
4281
0
  } else {
4282
0
    ZEND_UNREACHABLE();
4283
0
  }
4284
0
  return 1;
4285
0
}
4286
4287
static int zend_jit_spill_store_inv(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4288
0
{
4289
0
  ZEND_ASSERT(Z_MODE(src) == IS_REG);
4290
0
  ZEND_ASSERT(Z_MODE(dst) == IS_MEM_ZVAL);
4291
4292
0
  if (Z_LOAD(src) || Z_STORE(src)) {
4293
    /* it's not necessary to store register if it was previously loaded or already stored */
4294
0
    return 1;
4295
0
  }
4296
4297
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4298
0
    jit_set_Z_LVAL(jit, dst, zend_jit_use_reg(jit, src));
4299
0
    if (Z_REG(dst) != ZREG_FP || !JIT_G(current_frame)) {
4300
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4301
0
    } else if (STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG) {
4302
      /* invalidate memory type */
4303
0
      STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) = IS_UNKNOWN;
4304
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4305
0
    }
4306
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4307
0
    jit_set_Z_DVAL(jit, dst, zend_jit_use_reg(jit, src));
4308
0
    if (Z_REG(dst) != ZREG_FP || !JIT_G(current_frame)) {
4309
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4310
0
    } else if (STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE) {
4311
      /* invalidate memory type */
4312
0
      STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) = IS_UNKNOWN;
4313
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4314
0
    }
4315
0
  } else {
4316
0
    ZEND_UNREACHABLE();
4317
0
  }
4318
0
  return 1;
4319
0
}
4320
4321
static int zend_jit_load_reg(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4322
0
{
4323
0
  ZEND_ASSERT(Z_MODE(src) == IS_MEM_ZVAL);
4324
0
  ZEND_ASSERT(Z_MODE(dst) == IS_REG);
4325
4326
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4327
0
    zend_jit_def_reg(jit, dst, jit_Z_LVAL(jit, src));
4328
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4329
0
    zend_jit_def_reg(jit, dst, jit_Z_DVAL(jit, src));
4330
0
  } else {
4331
0
    ZEND_UNREACHABLE();
4332
0
  }
4333
0
  return 1;
4334
0
}
4335
4336
static int zend_jit_store_var(zend_jit_ctx *jit, uint32_t info, int var, int ssa_var, bool set_type)
4337
0
{
4338
0
  zend_jit_addr src = ZEND_ADDR_REG(ssa_var);
4339
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4340
4341
0
  return zend_jit_spill_store(jit, src, dst, info, set_type);
4342
0
}
4343
4344
static int zend_jit_store_ref(zend_jit_ctx *jit, uint32_t info, int var, int32_t src, bool set_type)
4345
0
{
4346
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4347
4348
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4349
0
    jit_set_Z_LVAL(jit, dst, src);
4350
0
    if (set_type &&
4351
0
        (Z_REG(dst) != ZREG_FP ||
4352
0
         !JIT_G(current_frame) ||
4353
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4354
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4355
0
    }
4356
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4357
0
    jit_set_Z_DVAL(jit, dst, src);
4358
0
    if (set_type &&
4359
0
        (Z_REG(dst) != ZREG_FP ||
4360
0
         !JIT_G(current_frame) ||
4361
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4362
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4363
0
    }
4364
0
  } else {
4365
0
    ZEND_UNREACHABLE();
4366
0
  }
4367
0
  return 1;
4368
0
}
4369
4370
static ir_ref zend_jit_deopt_rload(zend_jit_ctx *jit, ir_type type, int32_t reg)
4371
0
{
4372
0
  ir_ref ref = jit->ctx.control;
4373
0
  ir_insn *insn;
4374
4375
0
  while (1) {
4376
0
    insn = &jit->ctx.ir_base[ref];
4377
0
    if (insn->op == IR_RLOAD && insn->op2 == reg) {
4378
0
      ZEND_ASSERT(insn->type == type);
4379
0
      return ref;
4380
0
    } else if (insn->op == IR_START) {
4381
0
      break;
4382
0
    }
4383
0
    ref = insn->op1;
4384
0
  }
4385
0
  return ir_RLOAD(type, reg);
4386
0
}
4387
4388
/* Same as zend_jit_deopt_rload(), but 'reg' may be spilled on C stack */
4389
static ir_ref zend_jit_deopt_rload_spilled(zend_jit_ctx *jit, ir_type type, int8_t reg, int32_t offset)
4390
0
{
4391
0
  ZEND_ASSERT(reg >= 0);
4392
4393
0
  if (IR_REG_SPILLED(reg)) {
4394
0
    return ir_LOAD(type, ir_ADD_OFFSET(zend_jit_deopt_rload(jit, type, IR_REG_NUM(reg)), offset));
4395
0
  } else {
4396
0
    return zend_jit_deopt_rload(jit, type, reg);
4397
0
  }
4398
0
}
4399
4400
static int zend_jit_store_const_long(zend_jit_ctx *jit, int var, zend_long val)
4401
0
{
4402
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4403
0
  ir_ref src = ir_CONST_LONG(val);
4404
4405
0
  if (jit->ra && jit->ra[var].ref == IR_NULL) {
4406
0
    zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4407
0
  }
4408
0
  jit_set_Z_LVAL(jit, dst, src);
4409
0
  jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4410
0
  return 1;
4411
0
}
4412
4413
static int zend_jit_store_const_double(zend_jit_ctx *jit, int var, double val)
4414
0
{
4415
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4416
0
  ir_ref src = ir_CONST_DOUBLE(val);
4417
4418
0
  if (jit->ra && jit->ra[var].ref == IR_NULL) {
4419
0
    zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4420
0
  }
4421
0
  jit_set_Z_DVAL(jit, dst, src);
4422
0
  jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4423
0
  return 1;
4424
0
}
4425
4426
static int zend_jit_store_type(zend_jit_ctx *jit, int var, uint8_t type)
4427
0
{
4428
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4429
4430
0
  ZEND_ASSERT(type <= IS_DOUBLE);
4431
0
  jit_set_Z_TYPE_INFO(jit, dst, type);
4432
0
  return 1;
4433
0
}
4434
4435
static int zend_jit_store_reg(zend_jit_ctx *jit, uint32_t info, int var, int8_t reg, bool in_mem, bool set_type)
4436
0
{
4437
0
  zend_jit_addr src;
4438
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4439
0
  ir_type type;
4440
4441
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4442
0
    type = IR_LONG;
4443
0
    src = zend_jit_deopt_rload(jit, type, reg);
4444
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4445
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4446
0
    } else if (!in_mem) {
4447
0
      jit_set_Z_LVAL(jit, dst, src);
4448
0
      if (set_type &&
4449
0
          (Z_REG(dst) != ZREG_FP ||
4450
0
           !JIT_G(current_frame) ||
4451
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4452
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4453
0
      }
4454
0
    }
4455
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4456
0
    type = IR_DOUBLE;
4457
0
    src = zend_jit_deopt_rload(jit, type, reg);
4458
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4459
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4460
0
    } else if (!in_mem) {
4461
0
      jit_set_Z_DVAL(jit, dst, src);
4462
0
      if (set_type &&
4463
0
          (Z_REG(dst) != ZREG_FP ||
4464
0
           !JIT_G(current_frame) ||
4465
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4466
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4467
0
      }
4468
0
    }
4469
0
  } else {
4470
0
    ZEND_UNREACHABLE();
4471
0
  }
4472
0
  return 1;
4473
0
}
4474
4475
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)
4476
0
{
4477
0
  zend_jit_addr src;
4478
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4479
4480
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4481
0
    src = ir_LOAD_L(ir_ADD_OFFSET(ir_RLOAD_A(reg), offset));
4482
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4483
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4484
0
    } else {
4485
0
      jit_set_Z_LVAL(jit, dst, src);
4486
0
      if (set_type &&
4487
0
          (Z_REG(dst) != ZREG_FP ||
4488
0
           !JIT_G(current_frame) ||
4489
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4490
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4491
0
      }
4492
0
    }
4493
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4494
0
    src = ir_LOAD_D(ir_ADD_OFFSET(ir_RLOAD_A(reg), offset));
4495
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4496
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4497
0
    } else {
4498
0
      jit_set_Z_DVAL(jit, dst, src);
4499
0
      if (set_type &&
4500
0
          (Z_REG(dst) != ZREG_FP ||
4501
0
           !JIT_G(current_frame) ||
4502
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4503
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4504
0
      }
4505
0
    }
4506
0
  } else {
4507
0
    ZEND_UNREACHABLE();
4508
0
  }
4509
0
  return 1;
4510
0
}
4511
4512
static int zend_jit_store_var_type(zend_jit_ctx *jit, int var, uint32_t type)
4513
0
{
4514
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4515
4516
0
  jit_set_Z_TYPE_INFO(jit, dst, type);
4517
0
  return 1;
4518
0
}
4519
4520
static int zend_jit_zval_try_addref(zend_jit_ctx *jit, zend_jit_addr var_addr)
4521
0
{
4522
0
  ir_ref if_refcounted, end1;
4523
4524
0
  if_refcounted = jit_if_REFCOUNTED(jit, var_addr);
4525
0
  ir_IF_FALSE(if_refcounted);
4526
0
  end1 = ir_END();
4527
0
  ir_IF_TRUE(if_refcounted);
4528
0
  jit_GC_ADDREF(jit, jit_Z_PTR(jit, var_addr));
4529
0
  ir_MERGE_WITH(end1);
4530
0
  return 1;
4531
0
}
4532
4533
static int zend_jit_store_var_if_necessary(zend_jit_ctx *jit, int var, zend_jit_addr src, uint32_t info)
4534
0
{
4535
0
  if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
4536
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4537
0
    return zend_jit_spill_store(jit, src, dst, info, true);
4538
0
  }
4539
0
  return 1;
4540
0
}
4541
4542
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)
4543
0
{
4544
0
  if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
4545
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4546
0
    bool set_type = true;
4547
4548
0
    if ((info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) ==
4549
0
        (old_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF))) {
4550
0
      if (Z_MODE(old) != IS_REG || Z_LOAD(old) || Z_STORE(old)) {
4551
0
        if (JIT_G(current_frame)) {
4552
0
          uint32_t mem_type = STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var));
4553
4554
0
          if (mem_type != IS_UNKNOWN
4555
0
           && (info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == (1 << mem_type)) {
4556
0
            set_type = false;
4557
0
          }
4558
0
        } else {
4559
0
          set_type = false;
4560
0
        }
4561
0
      }
4562
0
    }
4563
0
    return zend_jit_spill_store(jit, src, dst, info, set_type);
4564
0
  }
4565
0
  return 1;
4566
0
}
4567
4568
static int zend_jit_load_var(zend_jit_ctx *jit, uint32_t info, int var, int ssa_var)
4569
0
{
4570
0
  zend_jit_addr src = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4571
0
  zend_jit_addr dst = ZEND_ADDR_REG(ssa_var);
4572
4573
0
  return zend_jit_load_reg(jit, src, dst, info);
4574
0
}
4575
4576
static int zend_jit_invalidate_var_if_necessary(zend_jit_ctx *jit, uint8_t op_type, zend_jit_addr addr, znode_op op)
4577
0
{
4578
0
  if ((op_type & (IS_TMP_VAR|IS_VAR)) && Z_MODE(addr) == IS_REG && !Z_LOAD(addr) && !Z_STORE(addr)) {
4579
    /* Invalidate operand type to prevent incorrect destuction by exception_handler_free_op1_op2() */
4580
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op.var);
4581
0
    jit_set_Z_TYPE_INFO(jit, dst, IS_UNDEF);
4582
0
  }
4583
0
  return 1;
4584
0
}
4585
4586
static int zend_jit_update_regs(zend_jit_ctx *jit, uint32_t var, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4587
0
{
4588
0
  if (!zend_jit_same_addr(src, dst)) {
4589
0
    if (Z_MODE(src) == IS_REG) {
4590
0
      if (Z_MODE(dst) == IS_REG) {
4591
0
        zend_jit_def_reg(jit, dst, zend_jit_use_reg(jit, src));
4592
0
        if (!Z_LOAD(src) && !Z_STORE(src) && Z_STORE(dst)) {
4593
0
          zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4594
4595
0
          if (!zend_jit_spill_store(jit, dst, var_addr, info,
4596
0
              JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4597
0
              JIT_G(current_frame) == NULL ||
4598
0
              STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4599
0
              (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4600
0
          )) {
4601
0
            return 0;
4602
0
          }
4603
0
        }
4604
0
      } else if (Z_MODE(dst) == IS_MEM_ZVAL) {
4605
0
        if (!Z_LOAD(src) && !Z_STORE(src)) {
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
      } else {
4616
0
        ZEND_UNREACHABLE();
4617
0
      }
4618
0
    } else if (Z_MODE(src) == IS_MEM_ZVAL) {
4619
0
      if (Z_MODE(dst) == IS_REG) {
4620
0
        if (!zend_jit_load_reg(jit, src, dst, info)) {
4621
0
          return 0;
4622
0
        }
4623
0
      } else {
4624
0
        ZEND_UNREACHABLE();
4625
0
      }
4626
0
    } else {
4627
0
      ZEND_UNREACHABLE();
4628
0
    }
4629
0
  } else if (Z_MODE(dst) == IS_REG && Z_STORE(dst)) {
4630
0
    dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4631
0
    if (!zend_jit_spill_store(jit, src, dst, info,
4632
0
        JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4633
0
        JIT_G(current_frame) == NULL ||
4634
0
        STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4635
0
        (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4636
0
    )) {
4637
0
      return 0;
4638
0
    }
4639
0
  }
4640
0
  return 1;
4641
0
}
4642
4643
struct jit_observer_fcall_is_unobserved_data {
4644
  ir_ref if_unobserved;
4645
  ir_ref ir_end_inputs;
4646
};
4647
4648
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) {
4649
0
  ir_ref run_time_cache;
4650
0
  struct jit_observer_fcall_is_unobserved_data data = { .ir_end_inputs = IR_UNUSED };
4651
0
  if (func) {
4652
0
    ZEND_ASSERT((func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0);
4653
0
  } else {
4654
    // JIT: if (function->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) {
4655
0
    ZEND_ASSERT(rx != IR_UNUSED);
4656
0
    ir_ref if_trampoline_or_generator = ir_IF(ir_AND_U32(
4657
0
      ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
4658
0
      ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)));
4659
0
    ir_IF_TRUE(if_trampoline_or_generator);
4660
0
    ir_END_list(data.ir_end_inputs);
4661
0
    ir_IF_FALSE(if_trampoline_or_generator);
4662
0
  }
4663
0
  if (func && (func->common.fn_flags & ZEND_ACC_CLOSURE) == 0 && ZEND_MAP_PTR_IS_OFFSET(func->common.run_time_cache)) {
4664
    // JIT: ZEND_MAP_PTR_GET_IMM(func->common.runtime_cache)
4665
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)));
4666
0
#ifndef ZTS
4667
0
  } else if (func && rx == IS_UNUSED) { // happens for internal functions only
4668
0
    ZEND_ASSERT(!ZEND_USER_CODE(func->type));
4669
0
    run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_CONST_ADDR(func), offsetof(zend_op_array, run_time_cache__ptr)));
4670
0
#endif
4671
0
  } else {
4672
    // Closures may be duplicated and have a different runtime cache. Use the regular run_time_cache access pattern for these
4673
0
    if (func && ZEND_USER_CODE(func->type)) { // not a closure and definitely not an internal function
4674
0
      run_time_cache = ir_LOAD_A(jit_CALL(rx, run_time_cache));
4675
0
    } else {
4676
      // JIT: ZEND_MAP_PTR_GET(func->common.runtime_cache)
4677
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)));
4678
0
      ir_ref if_odd = ir_IF(ir_AND_A(run_time_cache, ir_CONST_ADDR(1)));
4679
0
      ir_IF_TRUE(if_odd);
4680
4681
0
      ir_ref run_time_cache2 = ir_LOAD_A(ir_ADD_A(run_time_cache, ir_LOAD_A(jit_CG(map_ptr_base))));
4682
4683
0
      ir_ref if_odd_end = ir_END();
4684
0
      ir_IF_FALSE(if_odd);
4685
4686
      // JIT: if (func->common.runtime_cache != NULL) {
4687
0
      ir_ref if_rt_cache = ir_IF(ir_EQ(run_time_cache, IR_NULL));
4688
0
      ir_IF_TRUE(if_rt_cache);
4689
0
      ir_END_list(data.ir_end_inputs);
4690
0
      ir_IF_FALSE(if_rt_cache);
4691
4692
0
      ir_MERGE_WITH(if_odd_end);
4693
0
      run_time_cache = ir_PHI_2(IR_ADDR, run_time_cache, run_time_cache2);
4694
0
    }
4695
0
  }
4696
  // JIT: observer_handler = runtime_cache + ZEND_OBSERVER_HANDLE(function)
4697
0
  if (func) {
4698
0
    *observer_handler = ir_ADD_OFFSET(run_time_cache, ZEND_OBSERVER_HANDLE(func) * sizeof(void *));
4699
0
  } else {
4700
    // JIT: (func->type == ZEND_INTERNAL_FUNCTION ? zend_observer_fcall_internal_function_extension : zend_observer_fcall_op_array_extension) * sizeof(void *)
4701
0
    ir_ref tmp = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_function, type)));
4702
0
    ir_ref if_internal_func = ir_IF(ir_AND_U8(tmp, ir_CONST_U8(ZEND_INTERNAL_FUNCTION)));
4703
0
    ir_IF_TRUE(if_internal_func);
4704
4705
0
    ir_ref observer_handler_internal = ir_ADD_OFFSET(run_time_cache, zend_observer_fcall_internal_function_extension * sizeof(void *));
4706
4707
0
    ir_ref if_internal_func_end = ir_END();
4708
0
    ir_IF_FALSE(if_internal_func);
4709
4710
0
    ir_ref observer_handler_user = ir_ADD_OFFSET(run_time_cache, zend_observer_fcall_op_array_extension * sizeof(void *));
4711
4712
0
    ir_MERGE_WITH(if_internal_func_end);
4713
0
    *observer_handler = ir_PHI_2(IR_ADDR, observer_handler_user, observer_handler_internal);
4714
0
  }
4715
4716
  // JIT: if (*observer_handler == ZEND_OBSERVER_NONE_OBSERVED) {
4717
0
  data.if_unobserved = ir_IF(ir_EQ(ir_LOAD_A(*observer_handler), ir_CONST_ADDR(ZEND_OBSERVER_NONE_OBSERVED)));
4718
0
  ir_IF_FALSE(data.if_unobserved);
4719
0
  return data;
4720
0
}
4721
4722
/* For frameless the true branch of if_unobserved is used and this function not called. */
4723
0
static void jit_observer_fcall_is_unobserved_end(zend_jit_ctx *jit, struct jit_observer_fcall_is_unobserved_data *data) {
4724
0
  ir_END_list(data->ir_end_inputs);
4725
0
  ir_IF_TRUE(data->if_unobserved);
4726
0
  ir_END_list(data->ir_end_inputs);
4727
0
  ir_MERGE_list(data->ir_end_inputs);
4728
0
}
4729
4730
0
static void jit_observer_fcall_begin(zend_jit_ctx *jit, ir_ref rx, ir_ref observer_handler) {
4731
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_observer_fcall_begin_prechecked), rx, observer_handler);
4732
0
}
4733
4734
0
static void jit_observer_fcall_end(zend_jit_ctx *jit, ir_ref rx, ir_ref res_ref) {
4735
  // JIT: if (execute_data == EG(current_observed_frame)) {
4736
0
  ir_ref has_end_observer = ir_IF(ir_EQ(rx, ir_LOAD_A(jit_EG(current_observed_frame))));
4737
0
  ir_IF_TRUE(has_end_observer);
4738
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_observer_fcall_end_prechecked),
4739
0
    rx, res_ref);
4740
0
  ir_MERGE_WITH_EMPTY_FALSE(has_end_observer);
4741
0
}
4742
4743
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)
4744
0
{
4745
0
  ir_ref if_long = IR_UNUSED;
4746
0
  ir_ref op1_lval_ref = IR_UNUSED;
4747
0
  ir_ref ref;
4748
0
  ir_op op;
4749
4750
0
  if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)-MAY_BE_LONG)) {
4751
0
    if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
4752
0
    ir_IF_TRUE(if_long);
4753
0
  }
4754
0
  if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
4755
0
    op1_lval_ref = jit_Z_LVAL(jit, op1_addr);
4756
0
    jit_set_Z_LVAL(jit, res_addr, op1_lval_ref);
4757
0
    if (Z_MODE(res_addr) != IS_REG) {
4758
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4759
0
    }
4760
0
  }
4761
0
  if (Z_MODE(op1_def_addr) == IS_MEM_ZVAL
4762
0
   && Z_MODE(op1_addr) == IS_REG
4763
0
   && !Z_LOAD(op1_addr)
4764
0
   && !Z_STORE(op1_addr)) {
4765
0
    jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_LONG);
4766
0
  }
4767
0
  if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4768
0
    op = may_overflow ? IR_ADD_OV : IR_ADD;
4769
0
  } else {
4770
0
    op = may_overflow ? IR_SUB_OV : IR_SUB;
4771
0
  }
4772
0
  if (!op1_lval_ref) {
4773
0
    op1_lval_ref = jit_Z_LVAL(jit, op1_addr);
4774
0
  }
4775
0
  ref = ir_BINARY_OP_L(op, op1_lval_ref, ir_CONST_LONG(1));
4776
0
  if (op1_def_info & MAY_BE_LONG) {
4777
0
    jit_set_Z_LVAL(jit, op1_def_addr, ref);
4778
0
  }
4779
0
  if (may_overflow &&
4780
0
      (((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)) ||
4781
0
       ((opline->result_type != IS_UNUSED && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD))))) {
4782
0
    int32_t exit_point;
4783
0
    const void *exit_addr;
4784
0
    zend_jit_trace_stack *stack;
4785
0
    uint32_t old_op1_info, old_res_info = 0;
4786
4787
0
    stack = JIT_G(current_frame)->stack;
4788
0
    old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
4789
0
    SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_DOUBLE, 0);
4790
0
    if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4791
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4792
0
    } else {
4793
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4794
0
    }
4795
0
    if (opline->result_type != IS_UNUSED) {
4796
0
      old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
4797
0
      if (opline->opcode == ZEND_PRE_INC) {
4798
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
4799
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4800
0
      } else if (opline->opcode == ZEND_PRE_DEC) {
4801
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
4802
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4803
0
      } else if (opline->opcode == ZEND_POST_INC) {
4804
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4805
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_LONG(ZEND_LONG_MAX));
4806
0
      } else if (opline->opcode == ZEND_POST_DEC) {
4807
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4808
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_LONG(ZEND_LONG_MIN));
4809
0
      }
4810
0
    }
4811
4812
0
    exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
4813
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4814
0
    ir_GUARD_NOT(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
4815
4816
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4817
0
        opline->result_type != IS_UNUSED) {
4818
0
      jit_set_Z_LVAL(jit, res_addr, ref);
4819
0
      if (Z_MODE(res_addr) != IS_REG) {
4820
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4821
0
      }
4822
0
    }
4823
4824
0
    SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
4825
0
    if (opline->result_type != IS_UNUSED) {
4826
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
4827
0
    }
4828
0
  } else if (may_overflow) {
4829
0
    ir_ref if_overflow;
4830
0
    ir_ref merge_inputs = IR_UNUSED;
4831
4832
0
    if (((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_DOUBLE|MAY_BE_GUARD))
4833
0
     || (opline->result_type != IS_UNUSED && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) ==  (MAY_BE_DOUBLE|MAY_BE_GUARD))) {
4834
0
      int32_t exit_point;
4835
0
      const void *exit_addr;
4836
0
      zend_jit_trace_stack *stack;
4837
0
      uint32_t old_res_info = 0, old_op1_info = 0;
4838
4839
0
      stack = JIT_G(current_frame)->stack;
4840
0
      if (opline->result_type != IS_UNUSED) {
4841
0
        old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
4842
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4843
0
        if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) {
4844
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ref);
4845
0
        } else {
4846
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), op1_lval_ref);
4847
0
        }
4848
0
      }
4849
0
      old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
4850
0
      SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_LONG, 0);
4851
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ref);
4852
4853
0
      exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
4854
0
      exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4855
0
      ir_GUARD(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
4856
4857
0
      if (opline->result_type != IS_UNUSED) {
4858
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
4859
0
      }
4860
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
4861
0
    } else {
4862
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
4863
0
      ir_IF_FALSE(if_overflow);
4864
0
      if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4865
0
          opline->result_type != IS_UNUSED) {
4866
0
        jit_set_Z_LVAL(jit, res_addr, ref);
4867
0
        if (Z_MODE(res_addr) != IS_REG) {
4868
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4869
0
        }
4870
0
      }
4871
0
      ir_END_list(merge_inputs);
4872
4873
      /* overflow => cold path */
4874
0
      ir_IF_TRUE_cold(if_overflow);
4875
0
    }
4876
4877
0
    if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4878
0
      if (Z_MODE(op1_def_addr) == IS_REG) {
4879
0
        jit_set_Z_DVAL(jit, op1_def_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4880
0
      } else {
4881
#if SIZEOF_ZEND_LONG == 4
4882
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0));
4883
        jit_set_Z_W2(jit, op1_def_addr, ir_CONST_U32(0x41e00000));
4884
#else
4885
0
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0x43e0000000000000));
4886
0
#endif
4887
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_DOUBLE);
4888
0
      }
4889
0
    } else {
4890
0
      if (Z_MODE(op1_def_addr) == IS_REG) {
4891
0
        jit_set_Z_DVAL(jit, op1_def_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4892
0
      } else {
4893
#if SIZEOF_ZEND_LONG == 4
4894
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0x00200000));
4895
        jit_set_Z_W2(jit, op1_def_addr, ir_CONST_U32(0xc1e00000));
4896
#else
4897
0
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0xc3e0000000000000));
4898
0
#endif
4899
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_DOUBLE);
4900
0
      }
4901
0
    }
4902
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4903
0
        opline->result_type != IS_UNUSED) {
4904
0
      if (opline->opcode == ZEND_PRE_INC) {
4905
0
        if (Z_MODE(res_addr) == IS_REG) {
4906
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4907
0
        } else {
4908
#if SIZEOF_ZEND_LONG == 4
4909
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
4910
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
4911
#else
4912
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
4913
0
#endif
4914
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
4915
0
        }
4916
0
      } else {
4917
0
        if (Z_MODE(res_addr) == IS_REG) {
4918
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4919
0
        } else {
4920
#if SIZEOF_ZEND_LONG == 4
4921
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
4922
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
4923
#else
4924
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
4925
0
#endif
4926
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
4927
0
        }
4928
0
      }
4929
0
    }
4930
4931
0
    if (merge_inputs) {
4932
0
      ir_END_list(merge_inputs);
4933
0
      ir_MERGE_list(merge_inputs);
4934
0
    }
4935
0
  } else {
4936
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4937
0
        opline->result_type != IS_UNUSED) {
4938
0
      jit_set_Z_LVAL(jit, res_addr, ref);
4939
0
      if (Z_MODE(res_addr) != IS_REG) {
4940
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4941
0
      }
4942
0
    }
4943
0
  }
4944
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
4945
0
    ir_ref merge_inputs = ir_END();
4946
4947
    /* !is_long => cold path */
4948
0
    ir_IF_FALSE_cold(if_long);
4949
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
4950
0
      jit_SET_EX_OPLINE(jit, opline);
4951
0
      if (op1_info & MAY_BE_UNDEF) {
4952
0
        ir_ref if_def;
4953
4954
0
        if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
4955
0
        ir_IF_FALSE_cold(if_def);
4956
4957
        // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
4958
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
4959
4960
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_NULL);
4961
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def);
4962
4963
0
        op1_info |= MAY_BE_NULL;
4964
0
      }
4965
4966
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
4967
4968
0
      if (op1_info & MAY_BE_REF) {
4969
0
        ir_ref if_ref, if_typed, func, ref2, arg2;
4970
4971
0
        if_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
4972
0
        ir_IF_TRUE(if_ref);
4973
0
        ref2 = jit_Z_PTR_ref(jit, ref);
4974
4975
0
        if_typed = jit_if_TYPED_REF(jit, ref2);
4976
0
        ir_IF_TRUE(if_typed);
4977
4978
0
        if (RETURN_VALUE_USED(opline)) {
4979
0
          ZEND_ASSERT(Z_MODE(res_addr) != IS_REG);
4980
0
          arg2 = jit_ZVAL_ADDR(jit, res_addr);
4981
0
        } else {
4982
0
          arg2 = IR_NULL;
4983
0
        }
4984
0
        if (opline->opcode == ZEND_PRE_INC) {
4985
0
          func = ir_CONST_FC_FUNC(zend_jit_pre_inc_typed_ref);
4986
0
        } else if (opline->opcode == ZEND_PRE_DEC) {
4987
0
          func = ir_CONST_FC_FUNC(zend_jit_pre_dec_typed_ref);
4988
0
        } else if (opline->opcode == ZEND_POST_INC) {
4989
0
          func = ir_CONST_FC_FUNC(zend_jit_post_inc_typed_ref);
4990
0
        } else if (opline->opcode == ZEND_POST_DEC) {
4991
0
          func = ir_CONST_FC_FUNC(zend_jit_post_dec_typed_ref);
4992
0
        } else {
4993
0
          ZEND_UNREACHABLE();
4994
0
        }
4995
4996
0
        ir_CALL_2(IR_VOID, func, ref2, arg2);
4997
0
        zend_jit_check_exception(jit);
4998
0
        ir_END_list(merge_inputs);
4999
5000
0
        ir_IF_FALSE(if_typed);
5001
0
        ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
5002
0
        ir_MERGE_WITH_EMPTY_FALSE(if_ref);
5003
0
        ref = ir_PHI_2(IR_ADDR, ref2, ref);
5004
0
      }
5005
5006
0
      if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
5007
0
        jit_ZVAL_COPY(jit,
5008
0
          res_addr,
5009
0
          res_use_info,
5010
0
          ZEND_ADDR_REF_ZVAL(ref), op1_info, true);
5011
0
      }
5012
0
      if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
5013
0
        if (opline->opcode == ZEND_PRE_INC && opline->result_type != IS_UNUSED) {
5014
0
          ir_ref arg2 = jit_ZVAL_ADDR(jit, res_addr);
5015
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_inc), ref, arg2);
5016
0
        } else {
5017
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(increment_function), ref);
5018
0
        }
5019
0
      } else {
5020
0
        if (opline->opcode == ZEND_PRE_DEC && opline->result_type != IS_UNUSED) {
5021
0
          ir_ref arg2 = jit_ZVAL_ADDR(jit, res_addr);
5022
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_dec), ref, arg2);
5023
0
        } else {
5024
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(decrement_function), ref);
5025
0
        }
5026
0
      }
5027
0
      if (may_throw) {
5028
0
        zend_jit_check_exception(jit);
5029
0
      }
5030
0
    } else {
5031
0
      ref = jit_Z_DVAL(jit, op1_addr);
5032
0
      if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
5033
0
        jit_set_Z_DVAL(jit, res_addr, ref);
5034
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5035
0
      }
5036
0
      if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
5037
0
        op = IR_ADD;
5038
0
      } else {
5039
0
        op = IR_SUB;
5040
0
      }
5041
0
      ref = ir_BINARY_OP_D(op, ref, ir_CONST_DOUBLE(1.0));
5042
0
      jit_set_Z_DVAL(jit, op1_def_addr, ref);
5043
0
      if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
5044
0
          opline->result_type != IS_UNUSED) {
5045
0
        jit_set_Z_DVAL(jit, res_addr, ref);
5046
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5047
0
      }
5048
0
    }
5049
0
    ir_END_list(merge_inputs);
5050
0
    ir_MERGE_list(merge_inputs);
5051
0
  }
5052
0
  if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_def_addr, op1_def_info, op1_addr, op1_info)) {
5053
0
    return 0;
5054
0
  }
5055
0
  if (opline->result_type != IS_UNUSED) {
5056
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
5057
0
      return 0;
5058
0
    }
5059
0
  }
5060
0
  return 1;
5061
0
}
5062
5063
static int zend_jit_math_long_long(zend_jit_ctx   *jit,
5064
                                   const zend_op  *opline,
5065
                                   uint8_t         opcode,
5066
                                   zend_jit_addr   op1_addr,
5067
                                   zend_jit_addr   op2_addr,
5068
                                   zend_jit_addr   res_addr,
5069
                                   uint32_t        res_info,
5070
                                   uint32_t        res_use_info,
5071
                                   int             may_overflow)
5072
0
{
5073
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5074
0
  ir_op op;
5075
0
  ir_ref op1, op2, ref, if_overflow = IR_UNUSED;
5076
5077
0
  if (opcode == ZEND_ADD) {
5078
0
    op = may_overflow ? IR_ADD_OV : IR_ADD;
5079
0
  } else if (opcode == ZEND_SUB) {
5080
0
    op = may_overflow ? IR_SUB_OV : IR_SUB;
5081
0
  } else if (opcode == ZEND_MUL) {
5082
0
    op = may_overflow ? IR_MUL_OV : IR_MUL;
5083
0
  } else {
5084
0
    ZEND_UNREACHABLE();
5085
0
  }
5086
0
  op1 = jit_Z_LVAL(jit, op1_addr);
5087
0
  op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5088
0
  ref = ir_BINARY_OP_L(op, op1, op2);
5089
5090
0
  if (may_overflow) {
5091
0
    if (res_info & MAY_BE_GUARD) {
5092
0
      if ((res_info & MAY_BE_ANY) == MAY_BE_LONG) {
5093
0
        zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
5094
0
        uint32_t old_res_info;
5095
0
        int32_t exit_point;
5096
0
        const void *exit_addr;
5097
5098
0
        if (opline->opcode == ZEND_ADD
5099
0
         && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5100
0
          old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5101
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
5102
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
5103
0
          exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
5104
0
          SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
5105
0
        } else if (opline->opcode == ZEND_SUB
5106
0
         && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5107
0
          old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5108
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
5109
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
5110
0
          exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
5111
0
          SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
5112
0
        } else {
5113
0
          exit_point = zend_jit_trace_get_exit_point(opline, 0);
5114
0
        }
5115
5116
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5117
0
        if (!exit_addr) {
5118
0
          return 0;
5119
0
        }
5120
0
        ir_GUARD_NOT(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
5121
0
        may_overflow = 0;
5122
0
      } else if ((res_info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
5123
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
5124
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5125
5126
0
        if (!exit_addr) {
5127
0
          return 0;
5128
0
        }
5129
0
        ir_GUARD(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
5130
0
      } else {
5131
0
        ZEND_UNREACHABLE();
5132
0
      }
5133
0
    } else {
5134
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
5135
0
      ir_IF_FALSE(if_overflow);
5136
0
    }
5137
0
  }
5138
5139
0
  if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5140
0
    jit_set_Z_LVAL(jit, res_addr, ref);
5141
5142
0
    if (Z_MODE(res_addr) != IS_REG) {
5143
0
      if (!zend_jit_same_addr(op1_addr, res_addr)) {
5144
0
        if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_LONG) {
5145
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
5146
0
        }
5147
0
      }
5148
0
    }
5149
0
  }
5150
5151
0
  if (may_overflow) {
5152
0
    ir_ref fast_path = IR_UNUSED;
5153
5154
0
    if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5155
0
      fast_path = ir_END();
5156
0
      ir_IF_TRUE_cold(if_overflow);
5157
0
    }
5158
0
    if (opcode == ZEND_ADD) {
5159
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5160
0
        if (Z_MODE(res_addr) == IS_REG) {
5161
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
5162
0
        } else {
5163
#if SIZEOF_ZEND_LONG == 4
5164
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
5165
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
5166
#else
5167
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
5168
0
#endif
5169
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5170
0
        }
5171
0
        if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5172
0
          ir_MERGE_WITH(fast_path);
5173
0
        }
5174
0
        return 1;
5175
0
      }
5176
0
      op = IR_ADD;
5177
0
    } else if (opcode == ZEND_SUB) {
5178
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5179
0
        if (Z_MODE(res_addr) == IS_REG) {
5180
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
5181
0
        } else {
5182
#if SIZEOF_ZEND_LONG == 4
5183
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
5184
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
5185
#else
5186
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
5187
0
#endif
5188
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5189
0
        }
5190
0
        if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5191
0
          ir_MERGE_WITH(fast_path);
5192
0
        }
5193
0
        return 1;
5194
0
      }
5195
0
      op = IR_SUB;
5196
0
    } else if (opcode == ZEND_MUL) {
5197
0
      op = IR_MUL;
5198
0
    } else {
5199
0
      ZEND_UNREACHABLE();
5200
0
    }
5201
0
#if 1
5202
    /* reload */
5203
0
    op1 = jit_Z_LVAL(jit, op1_addr);
5204
0
    op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5205
0
#endif
5206
0
#if 1
5207
    /* disable CSE */
5208
0
    ir_ref old_cse_limit = jit->ctx.fold_cse_limit;
5209
0
    jit->ctx.fold_cse_limit = 0x7fffffff;
5210
0
#endif
5211
0
    op1 = ir_INT2D(op1);
5212
0
    op2 = ir_INT2D(op2);
5213
0
#if 1
5214
0
    jit->ctx.fold_cse_limit = old_cse_limit;
5215
0
#endif
5216
0
    ref = ir_BINARY_OP_D(op, op1, op2);
5217
0
    jit_set_Z_DVAL(jit, res_addr, ref);
5218
0
    if (Z_MODE(res_addr) != IS_REG) {
5219
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5220
0
    }
5221
0
    if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5222
0
      ir_MERGE_WITH(fast_path);
5223
0
    }
5224
0
  }
5225
5226
0
  return 1;
5227
0
}
5228
5229
static int zend_jit_math_long_double(zend_jit_ctx   *jit,
5230
                                     uint8_t         opcode,
5231
                                     zend_jit_addr   op1_addr,
5232
                                     zend_jit_addr   op2_addr,
5233
                                     zend_jit_addr   res_addr,
5234
                                     uint32_t        res_use_info)
5235
0
{
5236
0
  ir_op op;
5237
0
  ir_ref op1, op2, ref;
5238
5239
0
  if (opcode == ZEND_ADD) {
5240
0
    op = IR_ADD;
5241
0
  } else if (opcode == ZEND_SUB) {
5242
0
    op = IR_SUB;
5243
0
  } else if (opcode == ZEND_MUL) {
5244
0
    op = IR_MUL;
5245
0
  } else if (opcode == ZEND_DIV) {
5246
0
    op = IR_DIV;
5247
0
  } else {
5248
0
    ZEND_UNREACHABLE();
5249
0
  }
5250
0
  op1 = jit_Z_LVAL(jit, op1_addr);
5251
0
  op2 = jit_Z_DVAL(jit, op2_addr);
5252
0
  ref = ir_BINARY_OP_D(op, ir_INT2D(op1), op2);
5253
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5254
5255
0
  if (Z_MODE(res_addr) != IS_REG) {
5256
0
    if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5257
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5258
0
    }
5259
0
  }
5260
0
  return 1;
5261
0
}
5262
5263
static int zend_jit_math_double_long(zend_jit_ctx   *jit,
5264
                                     uint8_t         opcode,
5265
                                     zend_jit_addr   op1_addr,
5266
                                     zend_jit_addr   op2_addr,
5267
                                     zend_jit_addr   res_addr,
5268
                                     uint32_t        res_use_info)
5269
0
{
5270
0
  ir_op op;
5271
0
  ir_ref op1, op2, ref;
5272
5273
0
  if (opcode == ZEND_ADD) {
5274
0
    op = IR_ADD;
5275
0
  } else if (opcode == ZEND_SUB) {
5276
0
    op = IR_SUB;
5277
0
  } else if (opcode == ZEND_MUL) {
5278
0
    op = IR_MUL;
5279
0
  } else if (opcode == ZEND_DIV) {
5280
0
    op = IR_DIV;
5281
0
  } else {
5282
0
    ZEND_UNREACHABLE();
5283
0
  }
5284
0
  op1 = jit_Z_DVAL(jit, op1_addr);
5285
0
  op2 = jit_Z_LVAL(jit, op2_addr);
5286
0
  ref = ir_BINARY_OP_D(op, op1, ir_INT2D(op2));
5287
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5288
5289
0
  if (Z_MODE(res_addr) != IS_REG) {
5290
0
    if (!zend_jit_same_addr(op1_addr, res_addr)) {
5291
0
      if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5292
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5293
0
      }
5294
0
    }
5295
0
  }
5296
0
  return 1;
5297
0
}
5298
5299
static int zend_jit_math_double_double(zend_jit_ctx   *jit,
5300
                                       uint8_t         opcode,
5301
                                       zend_jit_addr   op1_addr,
5302
                                       zend_jit_addr   op2_addr,
5303
                                       zend_jit_addr   res_addr,
5304
                                       uint32_t        res_use_info)
5305
0
{
5306
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5307
0
  ir_op op;
5308
0
  ir_ref op1, op2, ref;
5309
5310
0
  if (opcode == ZEND_ADD) {
5311
0
    op = IR_ADD;
5312
0
  } else if (opcode == ZEND_SUB) {
5313
0
    op = IR_SUB;
5314
0
  } else if (opcode == ZEND_MUL) {
5315
0
    op = IR_MUL;
5316
0
  } else if (opcode == ZEND_DIV) {
5317
0
    op = IR_DIV;
5318
0
  } else {
5319
0
    ZEND_UNREACHABLE();
5320
0
  }
5321
0
  op1 = jit_Z_DVAL(jit, op1_addr);
5322
0
  op2 = (same_ops) ? op1 : jit_Z_DVAL(jit, op2_addr);
5323
0
  ref = ir_BINARY_OP_D(op, op1, op2);
5324
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5325
5326
0
  if (Z_MODE(res_addr) != IS_REG) {
5327
0
    if (!zend_jit_same_addr(op1_addr, res_addr)) {
5328
0
      if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5329
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5330
0
      }
5331
0
    }
5332
0
  }
5333
0
  return 1;
5334
0
}
5335
5336
static int zend_jit_math_helper(zend_jit_ctx   *jit,
5337
                                const zend_op  *opline,
5338
                                uint8_t         opcode,
5339
                                uint8_t         op1_type,
5340
                                znode_op        op1,
5341
                                zend_jit_addr   op1_addr,
5342
                                uint32_t        op1_info,
5343
                                uint8_t         op2_type,
5344
                                znode_op        op2,
5345
                                zend_jit_addr   op2_addr,
5346
                                uint32_t        op2_info,
5347
                                uint32_t        res_var,
5348
                                zend_jit_addr   res_addr,
5349
                                uint32_t        res_info,
5350
                                uint32_t        res_use_info,
5351
                                int             may_overflow,
5352
                                int             may_throw)
5353
0
{
5354
0
  ir_ref if_op1_long = IR_UNUSED;
5355
0
  ir_ref if_op1_double = IR_UNUSED;
5356
0
  ir_ref if_op2_double = IR_UNUSED;
5357
0
  ir_ref if_op1_long_op2_long = IR_UNUSED;
5358
0
  ir_ref if_op1_long_op2_double = IR_UNUSED;
5359
0
  ir_ref if_op1_double_op2_double = IR_UNUSED;
5360
0
  ir_ref if_op1_double_op2_long = IR_UNUSED;
5361
0
  ir_ref slow_inputs = IR_UNUSED;
5362
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5363
0
  ir_refs *end_inputs;
5364
0
  ir_refs *res_inputs;
5365
5366
0
  ir_refs_init(end_inputs, 6);
5367
0
  ir_refs_init(res_inputs, 6);
5368
5369
0
  if (Z_MODE(op1_addr) == IS_REG) {
5370
0
    if (!has_concrete_type(op2_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
5371
      /* Force load */
5372
0
      zend_jit_use_reg(jit, op1_addr);
5373
0
    }
5374
0
  } else if (Z_MODE(op2_addr) == IS_REG) {
5375
0
    if (!has_concrete_type(op1_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
5376
      /* Force load */
5377
0
      zend_jit_use_reg(jit, op2_addr);
5378
0
    }
5379
0
  }
5380
5381
0
  if (Z_MODE(res_addr) == IS_REG) {
5382
0
    jit->delay_var = Z_SSA_VAR(res_addr);
5383
0
    jit->delay_refs = res_inputs;
5384
0
  }
5385
5386
0
  if ((res_info & MAY_BE_GUARD) && (res_info & MAY_BE_LONG) && (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG)) {
5387
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_LONG)) {
5388
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5389
0
      ir_IF_TRUE(if_op1_long);
5390
0
    }
5391
0
    if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_LONG))) {
5392
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5393
0
      ir_IF_TRUE(if_op1_long_op2_long);
5394
0
    }
5395
0
    if (!zend_jit_math_long_long(jit, opline, opcode, op1_addr, op2_addr, res_addr, res_info, res_use_info, may_overflow)) {
5396
0
      return 0;
5397
0
    }
5398
0
    ir_refs_add(end_inputs, ir_END());
5399
0
    if (if_op1_long) {
5400
0
      ir_IF_FALSE_cold(if_op1_long);
5401
0
      ir_END_list(slow_inputs);
5402
0
    }
5403
0
    if (if_op1_long_op2_long) {
5404
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
5405
0
      ir_END_list(slow_inputs);
5406
0
    }
5407
0
  } else if ((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (res_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
5408
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_LONG)) {
5409
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5410
0
      ir_IF_TRUE(if_op1_long);
5411
0
    }
5412
0
    if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_LONG))) {
5413
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5414
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
5415
0
      if (op2_info & MAY_BE_DOUBLE) {
5416
0
        if (op2_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5417
0
          if_op1_long_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5418
0
          ir_IF_FALSE_cold(if_op1_long_op2_double);
5419
0
          ir_END_list(slow_inputs);
5420
0
          ir_IF_TRUE(if_op1_long_op2_double);
5421
0
        }
5422
0
        if (!zend_jit_math_long_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5423
0
          return 0;
5424
0
        }
5425
0
        ir_refs_add(end_inputs, ir_END());
5426
0
      } else {
5427
0
        ir_END_list(slow_inputs);
5428
0
      }
5429
0
      ir_IF_TRUE(if_op1_long_op2_long);
5430
0
    }
5431
0
    if (!zend_jit_math_long_long(jit, opline, opcode, op1_addr, op2_addr, res_addr, res_info, res_use_info, may_overflow)) {
5432
0
      return 0;
5433
0
    }
5434
0
    ir_refs_add(end_inputs, ir_END());
5435
5436
0
    if (if_op1_long) {
5437
0
      ir_IF_FALSE_cold(if_op1_long);
5438
0
    }
5439
5440
0
    if (op1_info & MAY_BE_DOUBLE) {
5441
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5442
0
        if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5443
0
        ir_IF_FALSE_cold(if_op1_double);
5444
0
        ir_END_list(slow_inputs);
5445
0
        ir_IF_TRUE(if_op1_double);
5446
0
      }
5447
0
      if (op2_info & MAY_BE_DOUBLE) {
5448
0
        if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5449
0
          if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5450
0
          ir_IF_TRUE(if_op1_double_op2_double);
5451
0
        }
5452
0
        if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5453
0
          return 0;
5454
0
        }
5455
0
        ir_refs_add(end_inputs, ir_END());
5456
0
        if (if_op1_double_op2_double) {
5457
0
          ir_IF_FALSE_cold(if_op1_double_op2_double);
5458
0
        }
5459
0
      }
5460
0
      if (!same_ops) {
5461
0
        if (op2_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5462
0
          if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5463
0
          ir_IF_FALSE_cold(if_op1_double_op2_long);
5464
0
          ir_END_list(slow_inputs);
5465
0
          ir_IF_TRUE(if_op1_double_op2_long);
5466
0
        }
5467
0
        if (!zend_jit_math_double_long(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
      } else if (if_op1_double_op2_double) {
5472
0
        ir_END_list(slow_inputs);
5473
0
      }
5474
0
    } else if (if_op1_long) {
5475
0
      ir_END_list(slow_inputs);
5476
0
    }
5477
0
  } else if ((op1_info & MAY_BE_DOUBLE) &&
5478
0
             !(op1_info & MAY_BE_LONG) &&
5479
0
             (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
5480
0
             (res_info & MAY_BE_DOUBLE)) {
5481
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_DOUBLE)) {
5482
0
      if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5483
0
      ir_IF_FALSE_cold(if_op1_double);
5484
0
      ir_END_list(slow_inputs);
5485
0
      ir_IF_TRUE(if_op1_double);
5486
0
    }
5487
0
    if (op2_info & MAY_BE_DOUBLE) {
5488
0
      if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5489
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5490
0
        ir_IF_TRUE(if_op1_double_op2_double);
5491
0
      }
5492
0
      if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5493
0
        return 0;
5494
0
      }
5495
0
      ir_refs_add(end_inputs, ir_END());
5496
0
      if (if_op1_double_op2_double) {
5497
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
5498
0
      }
5499
0
    }
5500
0
    if (!same_ops && (op2_info & MAY_BE_LONG)) {
5501
0
      if (op2_info & (MAY_BE_ANY-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
5502
0
        if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5503
0
        ir_IF_FALSE_cold(if_op1_double_op2_long);
5504
0
        ir_END_list(slow_inputs);
5505
0
        ir_IF_TRUE(if_op1_double_op2_long);
5506
0
      }
5507
0
      if (!zend_jit_math_double_long(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5508
0
        return 0;
5509
0
      }
5510
0
      ir_refs_add(end_inputs, ir_END());
5511
0
    } else if (if_op1_double_op2_double) {
5512
0
      ir_END_list(slow_inputs);
5513
0
    }
5514
0
  } else if ((op2_info & MAY_BE_DOUBLE) &&
5515
0
             !(op2_info & MAY_BE_LONG) &&
5516
0
             (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
5517
0
             (res_info & MAY_BE_DOUBLE)) {
5518
0
    if (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE)) {
5519
0
      if_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5520
0
      ir_IF_FALSE_cold(if_op2_double);
5521
0
      ir_END_list(slow_inputs);
5522
0
      ir_IF_TRUE(if_op2_double);
5523
0
    }
5524
0
    if (op1_info & MAY_BE_DOUBLE) {
5525
0
      if (!same_ops && (op1_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5526
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5527
0
        ir_IF_TRUE(if_op1_double_op2_double);
5528
0
      }
5529
0
      if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5530
0
        return 0;
5531
0
      }
5532
0
      ir_refs_add(end_inputs, ir_END());
5533
0
      if (if_op1_double_op2_double) {
5534
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
5535
0
      }
5536
0
    }
5537
0
    if (!same_ops && (op1_info & MAY_BE_LONG)) {
5538
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
5539
0
        if_op1_long_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5540
0
        ir_IF_FALSE_cold(if_op1_long_op2_double);
5541
0
        ir_END_list(slow_inputs);
5542
0
        ir_IF_TRUE(if_op1_long_op2_double);
5543
0
      }
5544
0
      if (!zend_jit_math_long_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5545
0
        return 0;
5546
0
      }
5547
0
      ir_refs_add(end_inputs, ir_END());
5548
0
    } else if (if_op1_double_op2_double) {
5549
0
      ir_END_list(slow_inputs);
5550
0
    }
5551
0
  }
5552
5553
0
  if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
5554
0
    (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE)))) {
5555
0
    ir_ref func, arg1, arg2, arg3;
5556
5557
0
    if (slow_inputs) {
5558
0
      ir_MERGE_list(slow_inputs);
5559
0
    }
5560
5561
0
    if (Z_MODE(op1_addr) == IS_REG) {
5562
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op1.var);
5563
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
5564
0
        return 0;
5565
0
      }
5566
0
      op1_addr = real_addr;
5567
0
    }
5568
0
    if (Z_MODE(op2_addr) == IS_REG) {
5569
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op2.var);
5570
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
5571
0
        return 0;
5572
0
      }
5573
0
      op2_addr = real_addr;
5574
0
    }
5575
0
    if (Z_MODE(res_addr) == IS_REG) {
5576
0
      arg1 = jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var));
5577
0
    } else {
5578
0
      arg1 = jit_ZVAL_ADDR(jit, res_addr);
5579
0
    }
5580
0
    arg2 = jit_ZVAL_ADDR(jit, op1_addr);
5581
0
    arg3 = jit_ZVAL_ADDR(jit, op2_addr);
5582
0
    jit_SET_EX_OPLINE(jit, opline);
5583
0
    if (opcode == ZEND_ADD) {
5584
0
      func = ir_CONST_FC_FUNC(add_function);
5585
0
    } else if (opcode == ZEND_SUB) {
5586
0
      func = ir_CONST_FC_FUNC(sub_function);
5587
0
    } else if (opcode == ZEND_MUL) {
5588
0
      func = ir_CONST_FC_FUNC(mul_function);
5589
0
    } else if (opcode == ZEND_DIV) {
5590
0
      func = ir_CONST_FC_FUNC(div_function);
5591
0
    } else {
5592
0
      ZEND_UNREACHABLE();
5593
0
    }
5594
0
    ir_CALL_3(IR_VOID, func, arg1, arg2, arg3);
5595
5596
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
5597
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
5598
5599
0
    if (may_throw) {
5600
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
5601
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
5602
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
5603
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
5604
0
        zend_jit_check_exception_undef_result(jit, opline);
5605
0
      } else {
5606
0
        zend_jit_check_exception(jit);
5607
0
      }
5608
0
    }
5609
0
    if (Z_MODE(res_addr) == IS_REG) {
5610
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var);
5611
0
      if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
5612
0
        return 0;
5613
0
      }
5614
0
    }
5615
0
    ir_refs_add(end_inputs, ir_END());
5616
0
  }
5617
5618
0
  if (end_inputs->count) {
5619
0
    ir_MERGE_N(end_inputs->count, end_inputs->refs);
5620
0
  }
5621
5622
0
  if (Z_MODE(res_addr) == IS_REG) {
5623
0
    ZEND_ASSERT(jit->delay_refs == res_inputs);
5624
0
    ZEND_ASSERT(end_inputs->count == res_inputs->count);
5625
0
    jit->delay_var = -1;
5626
0
    jit->delay_refs = NULL;
5627
0
    if (res_inputs->count == 1) {
5628
0
      zend_jit_def_reg(jit, res_addr, res_inputs->refs[0]);
5629
0
    } else {
5630
0
      ir_ref phi = ir_PHI_N((res_info & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE, res_inputs->count, res_inputs->refs);
5631
0
      zend_jit_def_reg(jit, res_addr, phi);
5632
0
    }
5633
0
  }
5634
5635
0
  return 1;
5636
0
}
5637
5638
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)
5639
0
{
5640
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
5641
5642
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)) {
5643
0
    return 0;
5644
0
  }
5645
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
5646
0
    return 0;
5647
0
  }
5648
0
  return 1;
5649
0
}
5650
5651
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)
5652
0
{
5653
0
  ir_ref ref;
5654
0
  ir_ref arg1 = jit_Z_PTR(jit, op1_addr);
5655
0
  ir_ref arg2 = jit_Z_PTR(jit, op2_addr);
5656
5657
0
  ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_add_arrays_helper), arg1, arg2);
5658
0
  jit_set_Z_PTR(jit, res_addr, ref);
5659
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_ARRAY_EX);
5660
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
5661
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
5662
0
  return 1;
5663
0
}
5664
5665
static int zend_jit_long_math_helper(zend_jit_ctx   *jit,
5666
                                     const zend_op  *opline,
5667
                                     uint8_t         opcode,
5668
                                     uint8_t         op1_type,
5669
                                     znode_op        op1,
5670
                                     zend_jit_addr   op1_addr,
5671
                                     uint32_t        op1_info,
5672
                                     zend_ssa_range *op1_range,
5673
                                     uint8_t         op2_type,
5674
                                     znode_op        op2,
5675
                                     zend_jit_addr   op2_addr,
5676
                                     uint32_t        op2_info,
5677
                                     zend_ssa_range *op2_range,
5678
                                     uint32_t        res_var,
5679
                                     zend_jit_addr   res_addr,
5680
                                     uint32_t        res_info,
5681
                                     uint32_t        res_use_info,
5682
                                     int             may_throw)
5683
0
{
5684
0
  ir_ref ref = IR_UNUSED;
5685
0
  ir_ref if_long1 = IR_UNUSED;
5686
0
  ir_ref if_long2 = IR_UNUSED;
5687
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5688
0
  ir_refs *res_inputs;
5689
5690
0
  ir_refs_init(res_inputs, 2);
5691
5692
0
  if (Z_MODE(op1_addr) == IS_REG
5693
0
   && Z_LOAD(op1_addr)
5694
0
   && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
5695
    /* Force load */
5696
0
    zend_jit_use_reg(jit, op1_addr);
5697
0
  }
5698
0
  if (Z_MODE(op2_addr) == IS_REG
5699
0
   && Z_LOAD(op2_addr)
5700
0
   && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
5701
    /* Force load */
5702
0
    zend_jit_use_reg(jit, op2_addr);
5703
0
  }
5704
5705
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
5706
0
    if_long1 = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5707
0
    ir_IF_TRUE(if_long1);
5708
0
  }
5709
0
  if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
5710
0
    if_long2 = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5711
0
    ir_IF_TRUE(if_long2);
5712
0
  }
5713
5714
0
  if (opcode == ZEND_SL) {
5715
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5716
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5717
5718
0
      if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
5719
0
        if (EXPECTED(op2_lval > 0)) {
5720
0
          ref = ir_CONST_LONG(0);
5721
0
        } else {
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
          jit_SET_EX_OPLINE(jit, opline);
5725
0
          ir_GUARD(IR_FALSE, jit_STUB_ADDR(jit, jit_stub_negative_shift));
5726
0
          if (Z_MODE(res_addr) == IS_REG) {
5727
0
            ref = ir_CONST_LONG(0); // dead code
5728
0
          }
5729
0
        }
5730
0
      } else {
5731
0
        ref = ir_SHL_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5732
0
      }
5733
0
    } else {
5734
0
      ref = jit_Z_LVAL(jit, op2_addr);
5735
0
      if (!op2_range ||
5736
0
           op2_range->min < 0 ||
5737
0
           op2_range->max >= SIZEOF_ZEND_LONG * 8) {
5738
5739
0
        ir_ref if_wrong, cold_path, ref2, if_ok;
5740
0
        ir_ref op1_ref = jit_Z_LVAL(jit, op1_addr);
5741
5742
0
        if_wrong = ir_IF(ir_UGT(ref, ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1)));
5743
0
        ir_IF_TRUE_cold(if_wrong);
5744
0
        if_ok = ir_IF(ir_GE(ref, ir_CONST_LONG(0)));
5745
0
        ir_IF_FALSE(if_ok);
5746
0
        jit_SET_EX_OPLINE(jit, opline);
5747
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5748
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5749
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_negative_shift));
5750
0
        ir_IF_TRUE(if_ok);
5751
0
        ref2 = ir_CONST_LONG(0);
5752
0
        cold_path = ir_END();
5753
0
        ir_IF_FALSE(if_wrong);
5754
0
        ref = ir_SHL_L(op1_ref, ref);
5755
0
        ir_MERGE_WITH(cold_path);
5756
0
        ref = ir_PHI_2(IR_LONG, ref, ref2);
5757
0
      } else {
5758
0
        ref = ir_SHL_L(jit_Z_LVAL(jit, op1_addr), ref);
5759
0
      }
5760
0
    }
5761
0
  } else if (opcode == ZEND_SR) {
5762
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5763
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5764
5765
0
      if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
5766
0
        if (EXPECTED(op2_lval > 0)) {
5767
0
          ref = ir_SAR_L(
5768
0
            jit_Z_LVAL(jit, op1_addr),
5769
0
            ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1));
5770
0
        } else {
5771
0
          zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5772
0
          zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5773
0
          jit_SET_EX_OPLINE(jit, opline);
5774
0
          ir_GUARD(IR_FALSE, jit_STUB_ADDR(jit, jit_stub_negative_shift));
5775
0
          if (Z_MODE(res_addr) == IS_REG) {
5776
0
            ref = ir_CONST_LONG(0); // dead code
5777
0
          }
5778
0
        }
5779
0
      } else {
5780
0
        ref = ir_SAR_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5781
0
      }
5782
0
    } else {
5783
0
      ref = jit_Z_LVAL(jit, op2_addr);
5784
0
      if (!op2_range ||
5785
0
           op2_range->min < 0 ||
5786
0
           op2_range->max >= SIZEOF_ZEND_LONG * 8) {
5787
5788
0
        ir_ref if_wrong, cold_path, ref2, if_ok;
5789
5790
0
        if_wrong = ir_IF(ir_UGT(ref, ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1)));
5791
0
        ir_IF_TRUE_cold(if_wrong);
5792
0
        if_ok = ir_IF(ir_GE(ref, ir_CONST_LONG(0)));
5793
0
        ir_IF_FALSE(if_ok);
5794
0
        jit_SET_EX_OPLINE(jit, opline);
5795
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5796
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5797
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_negative_shift));
5798
0
        ir_IF_TRUE(if_ok);
5799
0
        ref2 = ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1);
5800
0
        cold_path = ir_END();
5801
0
        ir_IF_FALSE(if_wrong);
5802
0
        ir_MERGE_WITH(cold_path);
5803
0
        ref = ir_PHI_2(IR_LONG, ref, ref2);
5804
0
      }
5805
0
      ref = ir_SAR_L(jit_Z_LVAL(jit, op1_addr), ref);
5806
0
    }
5807
0
  } else if (opcode == ZEND_MOD) {
5808
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5809
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5810
5811
0
      if (op2_lval == 0) {
5812
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5813
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5814
0
        jit_SET_EX_OPLINE(jit, opline);
5815
0
        ir_GUARD(IR_FALSE,  jit_STUB_ADDR(jit, jit_stub_mod_by_zero));
5816
0
        if (Z_MODE(res_addr) == IS_REG) {
5817
0
          ref = ir_CONST_LONG(0); // dead code
5818
0
        }
5819
0
      } else if (zend_long_is_power_of_two(op2_lval) && op1_range && op1_range->min >= 0) {
5820
0
        ref = ir_AND_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval - 1));
5821
0
      } else {
5822
0
        ref = ir_MOD_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5823
0
      }
5824
0
    } else {
5825
0
      ir_ref zero_path = 0;
5826
0
      ir_ref op1_ref = jit_Z_LVAL(jit, op1_addr);
5827
5828
0
      ref = jit_Z_LVAL(jit, op2_addr);
5829
0
      if ((op2_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) || !op2_range || (op2_range->min <= 0 && op2_range->max >= 0)) {
5830
0
        ir_ref if_ok = ir_IF(ref);
5831
0
        ir_IF_FALSE(if_ok);
5832
0
        jit_SET_EX_OPLINE(jit, opline);
5833
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5834
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5835
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_mod_by_zero));
5836
0
        ir_IF_TRUE(if_ok);
5837
0
      }
5838
5839
      /* Prevent overflow error/crash if op1 == LONG_MIN and op2 == -1 */
5840
0
      if (!op2_range || (op2_range->min <= -1 && op2_range->max >= -1)) {
5841
0
        ir_ref if_minus_one = ir_IF(ir_EQ(ref, ir_CONST_LONG(-1)));
5842
0
        ir_IF_TRUE_cold(if_minus_one);
5843
0
        zero_path = ir_END();
5844
0
        ir_IF_FALSE(if_minus_one);
5845
0
      }
5846
0
      ref = ir_MOD_L(op1_ref, ref);
5847
5848
0
      if (zero_path) {
5849
0
        ir_MERGE_WITH(zero_path);
5850
0
        ref = ir_PHI_2(IR_LONG, ref, ir_CONST_LONG(0));
5851
0
      }
5852
0
    }
5853
0
  } else {
5854
0
    ir_op op;
5855
0
    ir_ref op1, op2;
5856
5857
0
    if (opcode == ZEND_BW_OR) {
5858
0
      op = IR_OR;
5859
0
    } else if (opcode == ZEND_BW_AND) {
5860
0
      op = IR_AND;
5861
0
    } else if (opcode == ZEND_BW_XOR) {
5862
0
      op = IR_XOR;
5863
0
    } else {
5864
0
      ZEND_UNREACHABLE();
5865
0
    }
5866
0
    op1 = jit_Z_LVAL(jit, op1_addr);
5867
0
    op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5868
0
    ref = ir_BINARY_OP_L(op, op1, op2);
5869
0
  }
5870
5871
0
  if (ref) {
5872
0
    if (Z_MODE(res_addr) == IS_REG
5873
0
     && ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))
5874
0
      || (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)))) {
5875
0
      jit->delay_var = Z_SSA_VAR(res_addr);
5876
0
      jit->delay_refs = res_inputs;
5877
0
    }
5878
0
    jit_set_Z_LVAL(jit, res_addr, ref);
5879
0
    if (Z_MODE(res_addr) != IS_REG) {
5880
0
      if (!zend_jit_same_addr(op1_addr, res_addr)) {
5881
0
        if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_LONG) {
5882
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
5883
0
        }
5884
0
      }
5885
0
    }
5886
0
  }
5887
5888
0
  if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) ||
5889
0
    (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
5890
0
    ir_ref fast_path = ir_END();
5891
0
    ir_ref func, arg1, arg2, arg3;
5892
5893
0
    if (if_long2 && if_long1) {
5894
0
      ir_ref ref;
5895
0
      ir_IF_FALSE_cold(if_long2);
5896
0
      ref = ir_END();
5897
0
      ir_IF_FALSE_cold(if_long1);
5898
0
      ir_MERGE_2(ref, ir_END());
5899
0
    } else if (if_long1) {
5900
0
      ir_IF_FALSE_cold(if_long1);
5901
0
    } else if (if_long2) {
5902
0
      ir_IF_FALSE_cold(if_long2);
5903
0
    }
5904
5905
0
    if (op1_info & MAY_BE_UNDEF) {
5906
0
      ir_ref if_def, ref, ref2;
5907
5908
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
5909
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
5910
0
      ir_IF_FALSE_cold(if_def);
5911
5912
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
5913
0
      jit_SET_EX_OPLINE(jit, opline);
5914
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
5915
5916
0
      ref2 = jit_EG(uninitialized_zval);
5917
0
      ir_MERGE_WITH_EMPTY_TRUE(if_def);
5918
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
5919
0
      op1_addr = ZEND_ADDR_REF_ZVAL(ref);
5920
0
    }
5921
5922
0
    if (op2_info & MAY_BE_UNDEF) {
5923
0
      ir_ref if_def, ref, ref2;
5924
5925
0
      ref = jit_ZVAL_ADDR(jit, op2_addr);
5926
0
      if_def = jit_if_not_Z_TYPE(jit, op2_addr, IS_UNDEF);
5927
0
      ir_IF_FALSE_cold(if_def);
5928
5929
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op2.var)));
5930
0
      jit_SET_EX_OPLINE(jit, opline);
5931
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op2.var));
5932
5933
0
      ref2 = jit_EG(uninitialized_zval);
5934
0
      ir_MERGE_WITH_EMPTY_TRUE(if_def);
5935
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
5936
0
      op2_addr = ZEND_ADDR_REF_ZVAL(ref);
5937
0
    }
5938
5939
0
    if (Z_MODE(op1_addr) == IS_REG) {
5940
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op1.var);
5941
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
5942
0
        return 0;
5943
0
      }
5944
0
      op1_addr = real_addr;
5945
0
    }
5946
0
    if (Z_MODE(op2_addr) == IS_REG) {
5947
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op2.var);
5948
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
5949
0
        return 0;
5950
0
      }
5951
0
      op2_addr = real_addr;
5952
0
    }
5953
0
    if (Z_MODE(res_addr) == IS_REG) {
5954
0
      arg1 = jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var));
5955
0
    } else {
5956
0
      arg1 = jit_ZVAL_ADDR(jit, res_addr);
5957
0
    }
5958
0
    arg2 = jit_ZVAL_ADDR(jit, op1_addr);
5959
0
    arg3 = jit_ZVAL_ADDR(jit, op2_addr);
5960
0
    jit_SET_EX_OPLINE(jit, opline);
5961
0
    if (opcode == ZEND_BW_OR) {
5962
0
      func = ir_CONST_FC_FUNC(bitwise_or_function);
5963
0
    } else if (opcode == ZEND_BW_AND) {
5964
0
      func = ir_CONST_FC_FUNC(bitwise_and_function);
5965
0
    } else if (opcode == ZEND_BW_XOR) {
5966
0
      func = ir_CONST_FC_FUNC(bitwise_xor_function);
5967
0
    } else if (opcode == ZEND_SL) {
5968
0
      func = ir_CONST_FC_FUNC(shift_left_function);
5969
0
    } else if (opcode == ZEND_SR) {
5970
0
      func = ir_CONST_FC_FUNC(shift_right_function);
5971
0
    } else if (opcode == ZEND_MOD) {
5972
0
      func = ir_CONST_FC_FUNC(mod_function);
5973
0
    } else {
5974
0
      ZEND_UNREACHABLE();
5975
0
    }
5976
0
    ir_CALL_3(IR_VOID, func, arg1, arg2, arg3);
5977
5978
0
    if (op1_addr == res_addr && (op2_info & MAY_BE_RCN)) {
5979
      /* compound assignment may decrement "op2" refcount */
5980
0
      op2_info |= MAY_BE_RC1;
5981
0
    }
5982
5983
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
5984
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
5985
5986
0
    if (may_throw) {
5987
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
5988
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
5989
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
5990
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
5991
0
        zend_jit_check_exception_undef_result(jit, opline);
5992
0
      } else {
5993
0
        zend_jit_check_exception(jit);
5994
0
      }
5995
0
    }
5996
5997
0
    if (Z_MODE(res_addr) == IS_REG) {
5998
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var);
5999
0
      if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
6000
0
        return 0;
6001
0
      }
6002
0
    }
6003
6004
0
    ir_MERGE_2(fast_path, ir_END());
6005
6006
0
    if (Z_MODE(res_addr) == IS_REG) {
6007
0
      ZEND_ASSERT(jit->delay_refs == res_inputs);
6008
0
      ZEND_ASSERT(res_inputs->count == 2);
6009
0
      jit->delay_var = -1;
6010
0
      jit->delay_refs = NULL;
6011
0
      if (res_inputs->count == 1) {
6012
0
        zend_jit_def_reg(jit, res_addr, res_inputs->refs[0]);
6013
0
      } else {
6014
0
        ir_ref phi = ir_PHI_N(IR_LONG, res_inputs->count, res_inputs->refs);
6015
0
        zend_jit_def_reg(jit, res_addr, phi);
6016
0
      }
6017
0
    }
6018
0
  }
6019
6020
0
  return 1;
6021
0
}
6022
6023
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)
6024
0
{
6025
0
  ZEND_ASSERT((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG));
6026
6027
0
  if (!zend_jit_long_math_helper(jit, opline, opline->opcode,
6028
0
      opline->op1_type, opline->op1, op1_addr, op1_info, op1_range,
6029
0
      opline->op2_type, opline->op2, op2_addr, op2_info, op2_range,
6030
0
      opline->result.var, res_addr, res_info, res_use_info, may_throw)) {
6031
0
    return 0;
6032
0
  }
6033
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6034
0
    return 0;
6035
0
  }
6036
0
  return 1;
6037
0
}
6038
6039
static int zend_jit_concat_helper(zend_jit_ctx   *jit,
6040
                                  const zend_op  *opline,
6041
                                  uint8_t         op1_type,
6042
                                  znode_op        op1,
6043
                                  zend_jit_addr   op1_addr,
6044
                                  uint32_t        op1_info,
6045
                                  uint8_t         op2_type,
6046
                                  znode_op        op2,
6047
                                  zend_jit_addr   op2_addr,
6048
                                  uint32_t        op2_info,
6049
                                  zend_jit_addr   res_addr,
6050
                                  int             may_throw)
6051
0
{
6052
0
  ir_ref if_op1_string = IR_UNUSED;
6053
0
  ir_ref if_op2_string = IR_UNUSED;
6054
0
  ir_ref fast_path = IR_UNUSED;
6055
6056
0
  if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6057
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) {
6058
0
      if_op1_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
6059
0
      ir_IF_TRUE(if_op1_string);
6060
0
    }
6061
0
    if (op2_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) {
6062
0
      if_op2_string = jit_if_Z_TYPE(jit, op2_addr, IS_STRING);
6063
0
      ir_IF_TRUE(if_op2_string);
6064
0
    }
6065
0
    if (zend_jit_same_addr(op1_addr, res_addr)) {
6066
0
      ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6067
0
      ir_ref arg2 = jit_ZVAL_ADDR(jit, op2_addr);
6068
6069
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_assign_concat_helper), arg1, arg2);
6070
      /* concatenation with itself may reduce refcount */
6071
0
      op2_info |= MAY_BE_RC1;
6072
0
    } else {
6073
0
      ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6074
0
      ir_ref arg2 = jit_ZVAL_ADDR(jit, op1_addr);
6075
0
      ir_ref arg3 = jit_ZVAL_ADDR(jit, op2_addr);
6076
6077
0
      if (op1_type == IS_CV || op1_type == IS_CONST) {
6078
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_concat_helper), arg1, arg2, arg3);
6079
0
      } else {
6080
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_concat_tmp_helper), arg1, arg2, arg3);
6081
0
      }
6082
0
    }
6083
    /* concatenation with empty string may increase refcount */
6084
0
    op2_info |= MAY_BE_RCN;
6085
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, opline);
6086
0
    if (if_op1_string || if_op2_string) {
6087
0
      fast_path = ir_END();
6088
0
    }
6089
0
  }
6090
0
  if ((op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) ||
6091
0
      (op2_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING))) {
6092
0
    if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6093
0
      if (if_op1_string && if_op2_string) {
6094
0
        ir_IF_FALSE(if_op1_string);
6095
0
        ir_MERGE_WITH_EMPTY_FALSE(if_op2_string);
6096
0
      } else if (if_op1_string) {
6097
0
        ir_IF_FALSE_cold(if_op1_string);
6098
0
      } else if (if_op2_string) {
6099
0
        ir_IF_FALSE_cold(if_op2_string);
6100
0
      }
6101
0
    }
6102
0
    ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6103
0
    ir_ref arg2 = jit_ZVAL_ADDR(jit, op1_addr);
6104
0
    ir_ref arg3 = jit_ZVAL_ADDR(jit, op2_addr);
6105
6106
0
    jit_SET_EX_OPLINE(jit, opline);
6107
0
    ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(concat_function), arg1, arg2, arg3);
6108
    /* concatenation with empty string may increase refcount */
6109
0
    op1_info |= MAY_BE_RCN;
6110
0
    op2_info |= MAY_BE_RCN;
6111
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
6112
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
6113
0
    if (may_throw) {
6114
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
6115
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
6116
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
6117
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
6118
0
        zend_jit_check_exception_undef_result(jit, opline);
6119
0
      } else {
6120
0
        zend_jit_check_exception(jit);
6121
0
      }
6122
0
    }
6123
0
    if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6124
0
      ir_MERGE_WITH(fast_path);
6125
0
    }
6126
0
  }
6127
0
  return 1;
6128
0
}
6129
6130
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)
6131
0
{
6132
0
  zend_jit_addr op1_addr, op2_addr;
6133
6134
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
6135
0
  ZEND_ASSERT((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING));
6136
6137
0
  op1_addr = OP1_ADDR();
6138
0
  op2_addr = OP2_ADDR();
6139
6140
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);
6141
0
}
6142
6143
static int zend_jit_assign_op(zend_jit_ctx   *jit,
6144
                              const zend_op  *opline,
6145
                              uint32_t        op1_info,
6146
                              zend_jit_addr   op1_addr,
6147
                              zend_ssa_range *op1_range,
6148
                              uint32_t        op1_def_info,
6149
                              zend_jit_addr   op1_def_addr,
6150
                              uint32_t        op1_mem_info,
6151
                              uint32_t        op2_info,
6152
                              zend_jit_addr   op2_addr,
6153
                              zend_ssa_range *op2_range,
6154
                              int             may_overflow,
6155
                              int             may_throw)
6156
0
{
6157
0
  int result = 1;
6158
0
  ir_ref slow_path = IR_UNUSED;
6159
6160
0
  ZEND_ASSERT(opline->op1_type == IS_CV && opline->result_type == IS_UNUSED);
6161
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
6162
6163
0
  if (op1_info & MAY_BE_REF) {
6164
0
    ir_ref ref, ref2, arg2, op1_noref_path;
6165
0
    ir_ref if_op1_ref = IR_UNUSED;
6166
0
    ir_ref if_op1_typed = IR_UNUSED;
6167
0
    binary_op_type binary_op = get_binary_op(opline->extended_value);
6168
6169
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
6170
0
    if_op1_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
6171
0
    ir_IF_FALSE(if_op1_ref);
6172
0
    op1_noref_path = ir_END();
6173
0
    ir_IF_TRUE(if_op1_ref);
6174
0
    ref2 = jit_Z_PTR_ref(jit, ref);
6175
6176
0
    if_op1_typed = jit_if_TYPED_REF(jit, ref2);
6177
0
    ir_IF_TRUE_cold(if_op1_typed);
6178
6179
0
    if (Z_MODE(op2_addr) == IS_REG) {
6180
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
6181
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
6182
0
        return 0;
6183
0
      }
6184
0
      arg2 = jit_ZVAL_ADDR(jit, real_addr);
6185
0
    } else {
6186
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
6187
0
    }
6188
0
    jit_SET_EX_OPLINE(jit, opline);
6189
0
    if ((opline->op2_type & (IS_TMP_VAR|IS_VAR))
6190
0
     && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
6191
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref_tmp),
6192
0
        ref2, arg2, ir_CONST_FC_FUNC(binary_op));
6193
0
    } else {
6194
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
6195
0
        ref2, arg2, ir_CONST_FC_FUNC(binary_op));
6196
0
    }
6197
0
    zend_jit_check_exception(jit);
6198
0
    slow_path = ir_END();
6199
6200
0
    ir_IF_FALSE(if_op1_typed);
6201
0
    ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
6202
6203
0
    ir_MERGE_WITH(op1_noref_path);
6204
0
    ref = ir_PHI_2(IR_ADDR, ref2, ref);
6205
0
    ZEND_ASSERT(op1_addr == op1_def_addr);
6206
0
    op1_def_addr = op1_addr = ZEND_ADDR_REF_ZVAL(ref);
6207
0
  }
6208
6209
0
  switch (opline->extended_value) {
6210
0
    case ZEND_ADD:
6211
0
    case ZEND_SUB:
6212
0
    case ZEND_MUL:
6213
0
    case ZEND_DIV:
6214
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);
6215
0
      break;
6216
0
    case ZEND_BW_OR:
6217
0
    case ZEND_BW_AND:
6218
0
    case ZEND_BW_XOR:
6219
0
    case ZEND_SL:
6220
0
    case ZEND_SR:
6221
0
    case ZEND_MOD:
6222
0
      result = zend_jit_long_math_helper(jit, opline, opline->extended_value,
6223
0
        opline->op1_type, opline->op1, op1_addr, op1_info, op1_range,
6224
0
        opline->op2_type, opline->op2, op2_addr, op2_info, op2_range,
6225
0
        opline->op1.var, op1_def_addr, op1_def_info, op1_mem_info, may_throw);
6226
0
      break;
6227
0
    case ZEND_CONCAT:
6228
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);
6229
0
      break;
6230
0
    default:
6231
0
      ZEND_UNREACHABLE();
6232
0
  }
6233
6234
0
  if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_def_addr, op1_def_info, op1_addr, op1_info)) {
6235
0
    return 0;
6236
0
  }
6237
6238
0
  if (op1_info & MAY_BE_REF) {
6239
0
    ir_MERGE_WITH(slow_path);
6240
0
  }
6241
6242
0
  return result;
6243
0
}
6244
6245
static ir_ref jit_ZVAL_DEREF_ref(zend_jit_ctx *jit, ir_ref ref)
6246
0
{
6247
0
  ir_ref if_ref, ref2;
6248
6249
0
  if_ref = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_REFERENCE)));
6250
0
  ir_IF_TRUE(if_ref);
6251
0
  ref2 = ir_ADD_OFFSET(jit_Z_PTR_ref(jit, ref), offsetof(zend_reference, val));
6252
0
  ir_MERGE_WITH_EMPTY_FALSE(if_ref);
6253
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
6254
0
}
6255
6256
static zend_jit_addr jit_ZVAL_DEREF(zend_jit_ctx *jit, zend_jit_addr addr)
6257
0
{
6258
0
  ir_ref ref = jit_ZVAL_ADDR(jit, addr);
6259
0
  ref = jit_ZVAL_DEREF_ref(jit, ref);
6260
0
  return ZEND_ADDR_REF_ZVAL(ref);
6261
0
}
6262
6263
static ir_ref jit_ZVAL_INDIRECT_DEREF_ref(zend_jit_ctx *jit, ir_ref ref)
6264
0
{
6265
0
  ir_ref if_ref, ref2;
6266
6267
0
  if_ref = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_INDIRECT)));
6268
0
  ir_IF_TRUE(if_ref);
6269
0
  ref2 = jit_Z_PTR_ref(jit, ref);
6270
0
  ir_MERGE_WITH_EMPTY_FALSE(if_ref);
6271
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
6272
0
}
6273
6274
static zend_jit_addr jit_ZVAL_INDIRECT_DEREF(zend_jit_ctx *jit, zend_jit_addr addr)
6275
0
{
6276
0
  ir_ref ref = jit_ZVAL_ADDR(jit, addr);
6277
0
  ref = jit_ZVAL_INDIRECT_DEREF_ref(jit, ref);
6278
0
  return ZEND_ADDR_REF_ZVAL(ref);
6279
0
}
6280
6281
static int zend_jit_simple_assign(zend_jit_ctx   *jit,
6282
                                  const zend_op  *opline,
6283
                                  zend_jit_addr   var_addr,
6284
                                  uint32_t        var_info,
6285
                                  uint32_t        var_def_info,
6286
                                  uint8_t         val_type,
6287
                                  zend_jit_addr   val_addr,
6288
                                  uint32_t        val_info,
6289
                                  zend_jit_addr   res_addr,
6290
                                  bool            check_exception)
6291
0
{
6292
0
  ir_ref end_inputs = IR_UNUSED;
6293
6294
0
  if (Z_MODE(val_addr) == IS_CONST_ZVAL) {
6295
0
    zval *zv = Z_ZV(val_addr);
6296
6297
0
    if (!res_addr) {
6298
0
      jit_ZVAL_COPY_CONST(jit,
6299
0
        var_addr,
6300
0
        var_info, var_def_info,
6301
0
        zv, true);
6302
0
    } else {
6303
0
      jit_ZVAL_COPY_CONST(jit,
6304
0
        var_addr,
6305
0
        var_info, var_def_info,
6306
0
        zv, true);
6307
0
      jit_ZVAL_COPY_CONST(jit,
6308
0
        res_addr,
6309
0
        -1, var_def_info,
6310
0
        zv, true);
6311
0
    }
6312
0
  } else {
6313
0
    if (val_info & MAY_BE_UNDEF) {
6314
0
      ir_ref if_def, ret;
6315
6316
0
      if_def = jit_if_not_Z_TYPE(jit, val_addr, IS_UNDEF);
6317
0
      ir_IF_FALSE_cold(if_def);
6318
6319
0
      jit_set_Z_TYPE_INFO(jit, var_addr, IS_NULL);
6320
0
      if (res_addr) {
6321
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
6322
0
      }
6323
0
      jit_SET_EX_OPLINE(jit, opline);
6324
6325
0
      ZEND_ASSERT(Z_MODE(val_addr) == IS_MEM_ZVAL);
6326
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
6327
0
      ret = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(Z_OFFSET(val_addr)));
6328
6329
0
      if (check_exception) {
6330
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
6331
0
      }
6332
6333
0
      ir_END_list(end_inputs);
6334
0
      ir_IF_TRUE(if_def);
6335
0
    }
6336
0
    if (val_info & MAY_BE_REF) {
6337
0
      if (val_type == IS_CV) {
6338
0
        ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
6339
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
6340
0
        val_addr = ZEND_ADDR_REF_ZVAL(ref);
6341
0
      } else {
6342
0
        ir_ref ref, type, if_ref, ref2, refcount, if_not_zero;
6343
6344
0
        ref = jit_ZVAL_ADDR(jit, val_addr);
6345
0
        type = jit_Z_TYPE_ref(jit, ref);
6346
0
        if_ref = ir_IF(ir_EQ(type, ir_CONST_U8(IS_REFERENCE)));
6347
6348
0
        ir_IF_TRUE_cold(if_ref);
6349
0
        ref = jit_Z_PTR_ref(jit, ref);
6350
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
6351
0
        if (!res_addr) {
6352
0
          jit_ZVAL_COPY(jit,
6353
0
            var_addr,
6354
0
            var_info,
6355
0
            ZEND_ADDR_REF_ZVAL(ref2), val_info, true);
6356
0
        } else {
6357
0
          jit_ZVAL_COPY_2(jit,
6358
0
            res_addr,
6359
0
            var_addr,
6360
0
            var_info,
6361
0
            ZEND_ADDR_REF_ZVAL(ref2), val_info, 2);
6362
0
        }
6363
6364
0
        refcount = jit_GC_DELREF(jit, ref);
6365
0
        if_not_zero = ir_IF(refcount);
6366
0
        ir_IF_FALSE(if_not_zero);
6367
        // TODO: instead of dtor() call and ADDREF above, we may call efree() and move addref at "true" path ???
6368
        // This is related to GH-10168 (keep this before GH-10168 is completely closed)
6369
        // jit_EFREE(jit, ref, sizeof(zend_reference), NULL, NULL);
6370
0
        jit_ZVAL_DTOR(jit, ref, val_info, opline);
6371
0
        ir_END_list(end_inputs);
6372
0
        ir_IF_TRUE(if_not_zero);
6373
0
        ir_END_list(end_inputs);
6374
6375
0
        ir_IF_FALSE(if_ref);
6376
0
      }
6377
0
    }
6378
6379
0
    if (!res_addr) {
6380
0
      jit_ZVAL_COPY(jit,
6381
0
        var_addr,
6382
0
        var_info,
6383
0
        val_addr, val_info, val_type == IS_CV);
6384
0
    } else {
6385
0
      jit_ZVAL_COPY_2(jit,
6386
0
        res_addr,
6387
0
        var_addr,
6388
0
        var_info,
6389
0
        val_addr, val_info, val_type == IS_CV ? 2 : 1);
6390
0
    }
6391
0
  }
6392
6393
0
  if (end_inputs) {
6394
0
    ir_END_list(end_inputs);
6395
0
    ir_MERGE_list(end_inputs);
6396
0
  }
6397
6398
0
  return 1;
6399
0
}
6400
6401
static int zend_jit_assign_to_variable_call(zend_jit_ctx   *jit,
6402
                                            const zend_op  *opline,
6403
                                            zend_jit_addr   __var_use_addr,
6404
                                            zend_jit_addr   var_addr,
6405
                                            uint32_t        __var_info,
6406
                                            uint32_t        __var_def_info,
6407
                                            uint8_t         val_type,
6408
                                            zend_jit_addr   val_addr,
6409
                                            uint32_t        val_info,
6410
                                            zend_jit_addr   __res_addr,
6411
                                            bool       __check_exception)
6412
0
{
6413
0
  jit_stub_id func;
6414
0
  ir_ref undef_path = IR_UNUSED;
6415
6416
0
  if (val_info & MAY_BE_UNDEF) {
6417
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
6418
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
6419
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
6420
6421
0
      if (!exit_addr) {
6422
0
        return 0;
6423
0
      }
6424
6425
0
      jit_guard_not_Z_TYPE(jit, val_addr, IS_UNDEF, exit_addr);
6426
0
    } else {
6427
0
      ir_ref if_def;
6428
6429
0
      ZEND_ASSERT(Z_MODE(val_addr) == IS_MEM_ZVAL && Z_REG(val_addr) == ZREG_FP);
6430
0
      if_def = ir_IF(jit_Z_TYPE(jit, val_addr));
6431
0
      ir_IF_FALSE_cold(if_def);
6432
0
      jit_SET_EX_OPLINE(jit, opline);
6433
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(Z_OFFSET(val_addr)));
6434
6435
0
      ir_CALL_2(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_assign_const, IR_FASTCALL_FUNC),
6436
0
        jit_ZVAL_ADDR(jit, var_addr),
6437
0
        jit_EG(uninitialized_zval));
6438
6439
0
      undef_path = ir_END();
6440
0
      ir_IF_TRUE(if_def);
6441
0
    }
6442
0
  }
6443
6444
0
  if (!(val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {
6445
0
    func = jit_stub_assign_tmp;
6446
0
  } else if (val_type == IS_CONST) {
6447
0
    func = jit_stub_assign_const;
6448
0
  } else if (val_type == IS_TMP_VAR) {
6449
0
    func = jit_stub_assign_tmp;
6450
0
  } else if (val_type == IS_VAR) {
6451
0
    if (!(val_info & MAY_BE_REF)) {
6452
0
      func = jit_stub_assign_tmp;
6453
0
    } else {
6454
0
      func = jit_stub_assign_var;
6455
0
    }
6456
0
  } else if (val_type == IS_CV) {
6457
0
    if (!(val_info & MAY_BE_REF)) {
6458
0
      func = jit_stub_assign_cv_noref;
6459
0
    } else {
6460
0
      func = jit_stub_assign_cv;
6461
0
    }
6462
0
  } else {
6463
0
    ZEND_UNREACHABLE();
6464
0
  }
6465
6466
0
  if (opline) {
6467
0
    jit_SET_EX_OPLINE(jit, opline);
6468
0
  }
6469
6470
0
  ir_CALL_2(IR_VOID, jit_STUB_FUNC_ADDR(jit, func, IR_FASTCALL_FUNC),
6471
0
    jit_ZVAL_ADDR(jit, var_addr),
6472
0
    jit_ZVAL_ADDR(jit, val_addr));
6473
6474
0
  if (undef_path) {
6475
0
    ir_MERGE_WITH(undef_path);
6476
0
  }
6477
6478
0
  return 1;
6479
0
}
6480
6481
static int zend_jit_assign_to_variable(zend_jit_ctx   *jit,
6482
                                       const zend_op  *opline,
6483
                                       zend_jit_addr   var_use_addr,
6484
                                       zend_jit_addr   var_addr,
6485
                                       uint32_t        var_info,
6486
                                       uint32_t        var_def_info,
6487
                                       uint8_t         val_type,
6488
                                       zend_jit_addr   val_addr,
6489
                                       uint32_t        val_info,
6490
                                       zend_jit_addr   res_addr,
6491
                                       zend_jit_addr   ref_addr,
6492
                                       bool       check_exception)
6493
0
{
6494
0
  ir_ref if_refcounted = IR_UNUSED;
6495
0
  ir_ref simple_inputs = IR_UNUSED;
6496
0
  bool done = false;
6497
0
  zend_jit_addr real_res_addr = 0;
6498
0
  ir_refs *end_inputs;
6499
0
  ir_refs *res_inputs;
6500
6501
0
  ir_refs_init(end_inputs, 6);
6502
0
  ir_refs_init(res_inputs, 6);
6503
6504
0
  if (Z_MODE(val_addr) == IS_REG && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
6505
    /* Force load */
6506
0
    zend_jit_use_reg(jit, val_addr);
6507
0
  }
6508
6509
0
  if (Z_MODE(var_addr) == IS_REG) {
6510
0
    jit->delay_var = Z_SSA_VAR(var_addr);
6511
0
    jit->delay_refs = res_inputs;
6512
0
    if (Z_MODE(res_addr) == IS_REG) {
6513
0
      real_res_addr = res_addr;
6514
0
      res_addr = 0;
6515
0
    }
6516
0
  } else if (Z_MODE(res_addr) == IS_REG) {
6517
0
    jit->delay_var = Z_SSA_VAR(res_addr);
6518
0
    jit->delay_refs = res_inputs;
6519
0
  }
6520
6521
0
  if ((var_info & MAY_BE_REF) || ref_addr) {
6522
0
    ir_ref ref = 0, if_ref = 0, ref2, arg2, if_typed, non_ref_path;
6523
0
    uintptr_t func;
6524
6525
0
    if (!ref_addr) {
6526
0
      ref = jit_ZVAL_ADDR(jit, var_use_addr);
6527
0
      if_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
6528
0
      ir_IF_TRUE(if_ref);
6529
0
      ref2 = jit_Z_PTR_ref(jit, ref);
6530
0
    } else {
6531
0
      ref2 = jit_ZVAL_ADDR(jit, ref_addr);
6532
0
    }
6533
0
    if_typed = jit_if_TYPED_REF(jit, ref2);
6534
0
    ir_IF_TRUE_cold(if_typed);
6535
0
    jit_SET_EX_OPLINE(jit, opline);
6536
0
    if (Z_MODE(val_addr) == IS_REG) {
6537
0
      zend_jit_addr real_addr;
6538
6539
0
      if (opline->opcode == ZEND_ASSIGN_DIM || opline->opcode == ZEND_ASSIGN_OBJ) {
6540
0
        real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
6541
0
      } else {
6542
0
        ZEND_ASSERT(opline->opcode == ZEND_ASSIGN);
6543
0
        real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
6544
0
      }
6545
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
6546
0
        return 0;
6547
0
      }
6548
0
      arg2 = jit_ZVAL_ADDR(jit, real_addr);
6549
0
    } else {
6550
0
      arg2 = jit_ZVAL_ADDR(jit, val_addr);
6551
0
    }
6552
0
    if (!res_addr) {
6553
0
      if (val_type == IS_CONST) {
6554
0
        func = (uintptr_t)zend_jit_assign_const_to_typed_ref;
6555
0
      } else if (val_type == IS_TMP_VAR) {
6556
0
        func = (uintptr_t)zend_jit_assign_tmp_to_typed_ref;
6557
0
      } else if (val_type == IS_VAR) {
6558
0
        func = (uintptr_t)zend_jit_assign_var_to_typed_ref;
6559
0
      } else if (val_type == IS_CV) {
6560
0
        func = (uintptr_t)zend_jit_assign_cv_to_typed_ref;
6561
0
      } else {
6562
0
        ZEND_UNREACHABLE();
6563
0
      }
6564
0
      ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(func), ref2, arg2);
6565
0
    } else {
6566
0
      if (val_type == IS_CONST) {
6567
0
        func = (uintptr_t)zend_jit_assign_const_to_typed_ref2;
6568
0
      } else if (val_type == IS_TMP_VAR) {
6569
0
        func = (uintptr_t)zend_jit_assign_tmp_to_typed_ref2;
6570
0
      } else if (val_type == IS_VAR) {
6571
0
        func = (uintptr_t)zend_jit_assign_var_to_typed_ref2;
6572
0
      } else if (val_type == IS_CV) {
6573
0
        func = (uintptr_t)zend_jit_assign_cv_to_typed_ref2;
6574
0
      } else {
6575
0
        ZEND_UNREACHABLE();
6576
0
      }
6577
0
      ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(func), ref2, arg2, jit_ZVAL_ADDR(jit, res_addr));
6578
0
    }
6579
0
    if (check_exception) {
6580
0
      zend_jit_check_exception(jit);
6581
0
    }
6582
0
    ir_refs_add(end_inputs, ir_END());
6583
6584
0
    if (!ref_addr) {
6585
0
      ir_IF_FALSE(if_ref);
6586
0
      non_ref_path = ir_END();
6587
0
      ir_IF_FALSE(if_typed);
6588
0
      ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
6589
0
      ir_MERGE_WITH(non_ref_path);
6590
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
6591
0
      var_addr = var_use_addr = ZEND_ADDR_REF_ZVAL(ref);
6592
0
    } else {
6593
0
      ir_IF_FALSE(if_typed);
6594
0
    }
6595
0
  }
6596
6597
0
  if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
6598
0
    ir_ref ref, counter, if_not_zero;
6599
6600
0
    if (var_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
6601
0
      if_refcounted = jit_if_REFCOUNTED(jit, var_use_addr);
6602
0
      ir_IF_FALSE(if_refcounted);
6603
0
      ir_END_list(simple_inputs);
6604
0
      ir_IF_TRUE_cold(if_refcounted);
6605
0
    } else if (RC_MAY_BE_1(var_info)) {
6606
0
      done = true;
6607
0
    }
6608
0
    ref = jit_Z_PTR(jit, var_use_addr);
6609
0
    if (RC_MAY_BE_1(var_info)) {
6610
0
      if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, var_def_info, val_type, val_addr, val_info, res_addr, false)) {
6611
0
        return 0;
6612
0
      }
6613
0
      counter = jit_GC_DELREF(jit, ref);
6614
6615
0
      if_not_zero = ir_IF(counter);
6616
0
      ir_IF_FALSE(if_not_zero);
6617
0
      jit_ZVAL_DTOR(jit, ref, var_info, opline);
6618
0
      if (check_exception) {
6619
0
        zend_jit_check_exception(jit);
6620
0
      }
6621
0
      ir_refs_add(end_inputs, ir_END());
6622
0
      ir_IF_TRUE(if_not_zero);
6623
0
      if (RC_MAY_BE_N(var_info) && (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT)) != 0) {
6624
0
        ir_ref if_may_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
6625
0
        ir_IF_FALSE(if_may_leak);
6626
0
        if (opline) {
6627
0
          jit_SET_EX_OPLINE(jit, opline);
6628
0
        }
6629
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
6630
6631
0
        if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6632
0
          ZEND_ASSERT(jit->delay_refs == res_inputs);
6633
0
          ZEND_ASSERT(res_inputs->count > 0);
6634
0
          ir_refs_add(res_inputs, res_inputs->refs[res_inputs->count - 1]);
6635
0
        }
6636
0
        if (check_exception && (val_info & MAY_BE_UNDEF)) {
6637
0
          zend_jit_check_exception(jit);
6638
0
        }
6639
0
        ir_refs_add(end_inputs, ir_END());
6640
0
        ir_IF_TRUE(if_may_leak);
6641
0
      }
6642
0
      if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6643
0
        ZEND_ASSERT(jit->delay_refs == res_inputs);
6644
0
        ZEND_ASSERT(res_inputs->count > 0);
6645
0
        ir_refs_add(res_inputs, res_inputs->refs[res_inputs->count - 1]);
6646
0
      }
6647
0
      if (check_exception && (val_info & MAY_BE_UNDEF)) {
6648
0
        zend_jit_check_exception(jit);
6649
0
      }
6650
0
      ir_refs_add(end_inputs, ir_END());
6651
0
    } else /* if (RC_MAY_BE_N(var_info)) */ {
6652
0
      jit_GC_DELREF(jit, ref);
6653
0
      if (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT)) {
6654
0
        ir_ref if_may_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
6655
0
        ir_IF_FALSE(if_may_leak);
6656
0
        if (opline) {
6657
0
          jit_SET_EX_OPLINE(jit, opline);
6658
0
        }
6659
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
6660
0
        ir_END_list(simple_inputs);
6661
0
        ir_IF_TRUE(if_may_leak);
6662
0
      }
6663
0
      ir_END_list(simple_inputs);
6664
0
    }
6665
0
  }
6666
6667
0
  if (simple_inputs) {
6668
0
    ir_MERGE_list(simple_inputs);
6669
0
  }
6670
6671
0
  if (!done) {
6672
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)) {
6673
0
      return 0;
6674
0
    }
6675
0
    if (end_inputs->count) {
6676
0
      ir_refs_add(end_inputs, ir_END());
6677
0
    }
6678
0
  }
6679
6680
0
  if (end_inputs->count) {
6681
0
    ir_MERGE_N(end_inputs->count, end_inputs->refs);
6682
0
  }
6683
6684
0
  if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6685
0
    ir_ref phi;
6686
6687
0
    ZEND_ASSERT(jit->delay_refs == res_inputs);
6688
0
    ZEND_ASSERT(end_inputs->count == res_inputs->count || (end_inputs->count == 0 && res_inputs->count == 1));
6689
0
    jit->delay_var = -1;
6690
0
    jit->delay_refs = NULL;
6691
0
    if (res_inputs->count == 1) {
6692
0
      phi = res_inputs->refs[0];
6693
0
    } else {
6694
0
      phi = ir_PHI_N((var_def_info & MAY_BE_LONG & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE,
6695
0
        res_inputs->count, res_inputs->refs);
6696
0
    }
6697
0
    if (Z_MODE(var_addr) == IS_REG) {
6698
0
      if ((var_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) || ref_addr) {
6699
0
        phi = ir_emit2(&jit->ctx, IR_OPT(IR_COPY, jit->ctx.ir_base[phi].type), phi, 1);
6700
0
      }
6701
0
      zend_jit_def_reg(jit, var_addr, phi);
6702
0
      if (real_res_addr) {
6703
0
        if (var_def_info & MAY_BE_LONG) {
6704
0
          jit_set_Z_LVAL(jit, real_res_addr, jit_Z_LVAL(jit, var_addr));
6705
0
        } else {
6706
0
          jit_set_Z_DVAL(jit, real_res_addr, jit_Z_DVAL(jit, var_addr));
6707
0
        }
6708
0
      }
6709
0
    } else {
6710
0
      zend_jit_def_reg(jit, res_addr, phi);
6711
0
    }
6712
0
  }
6713
6714
0
  return 1;
6715
0
}
6716
6717
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)
6718
0
{
6719
0
  if (op1_addr != op1_def_addr) {
6720
0
    if (!zend_jit_update_regs(jit, opline->op1.var, op1_addr, op1_def_addr, op1_info)) {
6721
0
      return 0;
6722
0
    }
6723
0
    if (Z_MODE(op1_def_addr) == IS_REG && Z_MODE(op1_addr) != IS_REG) {
6724
0
      op1_addr = op1_def_addr;
6725
0
    }
6726
0
  }
6727
6728
0
  if (!zend_jit_simple_assign(jit, opline, res_addr, res_use_info, res_info, opline->op1_type, op1_addr, op1_info, 0, true)) {
6729
0
    return 0;
6730
0
  }
6731
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6732
0
    return 0;
6733
0
  }
6734
0
  return 1;
6735
0
}
6736
6737
static int zend_jit_assign(zend_jit_ctx  *jit,
6738
                           const zend_op *opline,
6739
                           uint32_t       op1_info,
6740
                           zend_jit_addr  op1_use_addr,
6741
                           uint32_t       op1_def_info,
6742
                           zend_jit_addr  op1_addr,
6743
                           uint32_t       op2_info,
6744
                           zend_jit_addr  op2_addr,
6745
                           zend_jit_addr  op2_def_addr,
6746
                           uint32_t       res_info,
6747
                           zend_jit_addr  res_addr,
6748
                           zend_jit_addr  ref_addr,
6749
                           int            may_throw)
6750
0
{
6751
0
  ZEND_ASSERT(opline->op1_type == IS_CV);
6752
6753
0
  if (op2_addr != op2_def_addr) {
6754
0
    if (!zend_jit_update_regs(jit, opline->op2.var, op2_addr, op2_def_addr, op2_info)) {
6755
0
      return 0;
6756
0
    }
6757
0
    if (Z_MODE(op2_def_addr) == IS_REG && Z_MODE(op2_addr) != IS_REG) {
6758
0
      op2_addr = op2_def_addr;
6759
0
    }
6760
0
  }
6761
6762
0
  if (Z_MODE(op1_addr) != IS_REG
6763
0
   && Z_MODE(op1_use_addr) == IS_REG
6764
0
   && !Z_LOAD(op1_use_addr)
6765
0
   && !Z_STORE(op1_use_addr)) {
6766
    /* Force type update */
6767
0
    op1_info |= MAY_BE_UNDEF;
6768
0
  }
6769
0
  if (!zend_jit_assign_to_variable(jit, opline, op1_use_addr, op1_addr, op1_info, op1_def_info,
6770
0
      opline->op2_type, op2_addr, op2_info, res_addr, ref_addr, may_throw)) {
6771
0
    return 0;
6772
0
  }
6773
0
  if (Z_MODE(op1_addr) == IS_REG) {
6774
0
    if (Z_STORE(op1_addr)) {
6775
0
      if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_addr, op1_def_info, op1_use_addr, op1_info)) {
6776
0
        return 0;
6777
0
      }
6778
0
    } else if ((op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
6779
0
      && Z_MODE(op1_use_addr) == IS_MEM_ZVAL
6780
0
      && Z_REG(op1_use_addr) == ZREG_FP
6781
0
      && EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)) < jit->current_op_array->last_var) {
6782
      /* We have to update type of CV because it may be captured by exception backtrace or released on RETURN */
6783
0
      if ((op1_def_info & MAY_BE_ANY) == MAY_BE_LONG) {
6784
0
        jit_set_Z_TYPE_INFO(jit, op1_use_addr, IS_LONG);
6785
0
        if (JIT_G(current_frame)) {
6786
0
          SET_STACK_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)), IS_LONG, 1);
6787
0
        }
6788
0
      } else if ((op1_def_info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
6789
0
        jit_set_Z_TYPE_INFO(jit, op1_use_addr, IS_DOUBLE);
6790
0
        if (JIT_G(current_frame)) {
6791
0
          SET_STACK_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)), IS_DOUBLE, 1);
6792
0
        }
6793
0
      } else {
6794
0
        ZEND_UNREACHABLE();
6795
0
      }
6796
0
    }
6797
0
  }
6798
0
  if (opline->result_type != IS_UNUSED) {
6799
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6800
0
      return 0;
6801
0
    }
6802
0
  }
6803
6804
0
  return 1;
6805
0
}
6806
6807
static ir_op zend_jit_cmp_op(const zend_op *opline)
6808
0
{
6809
0
  ir_op op;
6810
6811
0
  switch (opline->opcode) {
6812
0
    case ZEND_IS_EQUAL:
6813
0
    case ZEND_IS_IDENTICAL:
6814
0
    case ZEND_CASE:
6815
0
    case ZEND_CASE_STRICT:
6816
0
      op = IR_EQ;
6817
0
      break;
6818
0
    case ZEND_IS_NOT_EQUAL:
6819
0
    case ZEND_IS_NOT_IDENTICAL:
6820
0
      op = IR_NE;
6821
0
      break;
6822
0
    case ZEND_IS_SMALLER:
6823
0
      op = IR_LT;
6824
0
      break;
6825
0
    case ZEND_IS_SMALLER_OR_EQUAL:
6826
0
      op = IR_LE;
6827
0
      break;
6828
0
    default:
6829
0
      ZEND_UNREACHABLE();
6830
0
  }
6831
0
  return op;
6832
0
}
6833
6834
static ir_ref zend_jit_cmp_long_long(zend_jit_ctx   *jit,
6835
                                     const zend_op  *opline,
6836
                                     zend_ssa_range *op1_range,
6837
                                     zend_jit_addr   op1_addr,
6838
                                     zend_ssa_range *op2_range,
6839
                                     zend_jit_addr   op2_addr,
6840
                                     zend_jit_addr   res_addr,
6841
                                     uint8_t         smart_branch_opcode,
6842
                                     uint32_t        target_label,
6843
                                     uint32_t        target_label2,
6844
                                     const void     *exit_addr,
6845
                                     bool       skip_comparison)
6846
0
{
6847
0
  ir_ref ref;
6848
0
  bool result;
6849
6850
0
  if (zend_jit_is_constant_cmp_long_long(opline, op1_range, op1_addr, op2_range, op2_addr, &result)) {
6851
0
    if (!smart_branch_opcode ||
6852
0
        smart_branch_opcode == ZEND_JMPZ_EX ||
6853
0
        smart_branch_opcode == ZEND_JMPNZ_EX) {
6854
0
      jit_set_Z_TYPE_INFO(jit, res_addr, result ? IS_TRUE : IS_FALSE);
6855
0
    }
6856
0
    if (smart_branch_opcode && !exit_addr) {
6857
0
      if (smart_branch_opcode == ZEND_JMPZ ||
6858
0
          smart_branch_opcode == ZEND_JMPZ_EX) {
6859
0
        return jit_IF_ex(jit, IR_FALSE, result ? target_label : target_label2);
6860
0
      } else if (smart_branch_opcode == ZEND_JMPNZ ||
6861
0
                 smart_branch_opcode == ZEND_JMPNZ_EX) {
6862
0
        return jit_IF_ex(jit, IR_TRUE, result ? target_label : target_label2);
6863
0
      } else {
6864
0
        ZEND_UNREACHABLE();
6865
0
      }
6866
0
    }
6867
0
    if (opline->opcode != ZEND_IS_IDENTICAL
6868
0
     && opline->opcode != ZEND_IS_NOT_IDENTICAL
6869
0
     && opline->opcode != ZEND_CASE_STRICT) {
6870
0
      return ir_END();
6871
0
    } else {
6872
0
      return IR_NULL; /* success */
6873
0
    }
6874
0
  }
6875
6876
0
  ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_LVAL(jit, op1_addr), jit_Z_LVAL(jit, op2_addr));
6877
6878
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6879
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6880
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6881
0
  }
6882
0
  if (exit_addr) {
6883
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6884
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6885
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6886
0
      } else {
6887
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6888
0
      }
6889
0
    } else {
6890
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6891
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6892
0
      } else {
6893
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6894
0
      }
6895
0
    }
6896
0
  } else if (smart_branch_opcode) {
6897
0
    return jit_IF_ex(jit, ref,
6898
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6899
0
  }
6900
6901
0
  if (opline->opcode != ZEND_IS_IDENTICAL
6902
0
   && opline->opcode != ZEND_IS_NOT_IDENTICAL
6903
0
   && opline->opcode != ZEND_CASE_STRICT) {
6904
0
    return ir_END();
6905
0
  } else {
6906
0
    return IR_NULL; /* success */
6907
0
  }
6908
0
}
6909
6910
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)
6911
0
{
6912
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));
6913
6914
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6915
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6916
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6917
0
  }
6918
0
  if (exit_addr) {
6919
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6920
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6921
0
    } else {
6922
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6923
0
    }
6924
0
  } else if (smart_branch_opcode) {
6925
0
    return jit_IF_ex(jit, ref,
6926
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6927
0
  }
6928
0
  return ir_END();
6929
0
}
6930
6931
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)
6932
0
{
6933
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)));
6934
6935
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6936
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6937
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6938
0
  }
6939
0
  if (exit_addr) {
6940
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6941
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6942
0
    } else {
6943
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6944
0
    }
6945
0
  } else if (smart_branch_opcode) {
6946
0
    return jit_IF_ex(jit, ref,
6947
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6948
0
  }
6949
0
  return ir_END();
6950
0
}
6951
6952
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)
6953
0
{
6954
0
  ir_ref ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_DVAL(jit, op1_addr), jit_Z_DVAL(jit, op2_addr));
6955
6956
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6957
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6958
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6959
0
  }
6960
0
  if (exit_addr) {
6961
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6962
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6963
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6964
0
      } else {
6965
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6966
0
      }
6967
0
    } else {
6968
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6969
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6970
0
      } else {
6971
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6972
0
      }
6973
0
    }
6974
0
  } else if (smart_branch_opcode) {
6975
0
    return jit_IF_ex(jit, ref,
6976
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6977
0
  }
6978
0
  if (opline->opcode != ZEND_IS_IDENTICAL
6979
0
   && opline->opcode != ZEND_IS_NOT_IDENTICAL
6980
0
   && opline->opcode != ZEND_CASE_STRICT) {
6981
0
    return ir_END();
6982
0
  } else {
6983
0
    return IR_NULL; /* success */
6984
0
  }
6985
0
}
6986
6987
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)
6988
0
{
6989
0
  ref = ir_CMP_OP(zend_jit_cmp_op(opline), ref, ir_CONST_I32(0));
6990
6991
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6992
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6993
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6994
0
  }
6995
0
  if (exit_addr) {
6996
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6997
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6998
0
    } else {
6999
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7000
0
    }
7001
0
  } else if (smart_branch_opcode) {
7002
0
    return jit_IF_ex(jit, ref,
7003
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
7004
0
  }
7005
7006
0
  return ir_END();
7007
0
}
7008
7009
static int zend_jit_cmp(zend_jit_ctx   *jit,
7010
                        const zend_op  *opline,
7011
                        uint32_t        op1_info,
7012
                        zend_ssa_range *op1_range,
7013
                        zend_jit_addr   op1_addr,
7014
                        uint32_t        op2_info,
7015
                        zend_ssa_range *op2_range,
7016
                        zend_jit_addr   op2_addr,
7017
                        zend_jit_addr   res_addr,
7018
                        int             may_throw,
7019
                        uint8_t         smart_branch_opcode,
7020
                        uint32_t        target_label,
7021
                        uint32_t        target_label2,
7022
                        const void     *exit_addr,
7023
                        bool       skip_comparison)
7024
0
{
7025
0
  ir_ref ref = IR_UNUSED;
7026
0
  ir_ref if_op1_long = IR_UNUSED;
7027
0
  ir_ref if_op1_double = IR_UNUSED;
7028
0
  ir_ref if_op2_double = IR_UNUSED;
7029
0
  ir_ref if_op1_long_op2_long = IR_UNUSED;
7030
0
  ir_ref if_op1_long_op2_double = IR_UNUSED;
7031
0
  ir_ref if_op1_double_op2_double = IR_UNUSED;
7032
0
  ir_ref if_op1_double_op2_long = IR_UNUSED;
7033
0
  ir_ref slow_inputs = IR_UNUSED;
7034
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
7035
0
  bool has_slow =
7036
0
    (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
7037
0
    (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
7038
0
    ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
7039
0
     (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))));
7040
0
  ir_refs *end_inputs;
7041
7042
0
  ir_refs_init(end_inputs, 8);
7043
7044
0
  if (Z_MODE(op1_addr) == IS_REG) {
7045
0
    if (!has_concrete_type(op2_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
7046
      /* Force load */
7047
0
      zend_jit_use_reg(jit, op1_addr);
7048
0
    }
7049
0
  } else if (Z_MODE(op2_addr) == IS_REG) {
7050
0
    if (!has_concrete_type(op1_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
7051
      /* Force load */
7052
0
      zend_jit_use_reg(jit, op2_addr);
7053
0
    }
7054
0
  }
7055
7056
0
  if ((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG)) {
7057
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
7058
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
7059
0
      ir_IF_TRUE(if_op1_long);
7060
0
    }
7061
0
    if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
7062
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7063
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
7064
0
      if (op2_info & MAY_BE_DOUBLE) {
7065
0
        if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7066
0
          if_op1_long_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7067
0
          ir_IF_FALSE_cold(if_op1_long_op2_double);
7068
0
          ir_END_list(slow_inputs);
7069
0
          ir_IF_TRUE(if_op1_long_op2_double);
7070
0
        }
7071
0
        ref = zend_jit_cmp_long_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7072
0
        if (!ref) {
7073
0
          return 0;
7074
0
        }
7075
0
        ir_refs_add(end_inputs, ref);
7076
0
      } else {
7077
0
        ir_END_list(slow_inputs);
7078
0
      }
7079
0
      ir_IF_TRUE(if_op1_long_op2_long);
7080
0
    }
7081
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);
7082
0
    if (!ref) {
7083
0
      return 0;
7084
0
    }
7085
0
    ir_refs_add(end_inputs, ref);
7086
7087
0
    if (if_op1_long) {
7088
0
      ir_IF_FALSE_cold(if_op1_long);
7089
0
    }
7090
0
    if (op1_info & MAY_BE_DOUBLE) {
7091
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7092
0
        if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7093
0
        ir_IF_FALSE_cold(if_op1_double);
7094
0
        ir_END_list(slow_inputs);
7095
0
        ir_IF_TRUE(if_op1_double);
7096
0
      }
7097
0
      if (op2_info & MAY_BE_DOUBLE) {
7098
0
        if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7099
0
          if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7100
0
          ir_IF_TRUE(if_op1_double_op2_double);
7101
0
        }
7102
0
        ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7103
0
        if (!ref) {
7104
0
          return 0;
7105
0
        }
7106
0
        ir_refs_add(end_inputs, ref);
7107
0
        if (if_op1_double_op2_double) {
7108
0
          ir_IF_FALSE_cold(if_op1_double_op2_double);
7109
0
        }
7110
0
      }
7111
0
      if (!same_ops) {
7112
0
        if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7113
0
          if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7114
0
          ir_IF_FALSE_cold(if_op1_double_op2_long);
7115
0
          ir_END_list(slow_inputs);
7116
0
          ir_IF_TRUE(if_op1_double_op2_long);
7117
0
        }
7118
0
        ref = zend_jit_cmp_double_long(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
      } else if (if_op1_double_op2_double) {
7124
0
        ir_END_list(slow_inputs);
7125
0
      }
7126
0
    } else if (if_op1_long) {
7127
0
      ir_END_list(slow_inputs);
7128
0
    }
7129
0
  } else if ((op1_info & MAY_BE_DOUBLE) &&
7130
0
             !(op1_info & MAY_BE_LONG) &&
7131
0
             (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
7132
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE)) {
7133
0
      if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7134
0
      ir_IF_FALSE_cold(if_op1_double);
7135
0
      ir_END_list(slow_inputs);
7136
0
      ir_IF_TRUE(if_op1_double);
7137
0
    }
7138
0
    if (op2_info & MAY_BE_DOUBLE) {
7139
0
      if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7140
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7141
0
        ir_IF_TRUE(if_op1_double_op2_double);
7142
0
      }
7143
0
      ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7144
0
      if (!ref) {
7145
0
        return 0;
7146
0
      }
7147
0
      ir_refs_add(end_inputs, ref);
7148
0
      if (if_op1_double_op2_double) {
7149
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
7150
0
      }
7151
0
    }
7152
0
    if (!same_ops && (op2_info & MAY_BE_LONG)) {
7153
0
      if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
7154
0
        if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7155
0
        ir_IF_FALSE_cold(if_op1_double_op2_long);
7156
0
        ir_END_list(slow_inputs);
7157
0
        ir_IF_TRUE(if_op1_double_op2_long);
7158
0
      }
7159
0
      ref = zend_jit_cmp_double_long(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7160
0
      if (!ref) {
7161
0
        return 0;
7162
0
      }
7163
0
      ir_refs_add(end_inputs, ref);
7164
0
    } else if (if_op1_double_op2_double) {
7165
0
      ir_END_list(slow_inputs);
7166
0
    }
7167
0
  } else if ((op2_info & MAY_BE_DOUBLE) &&
7168
0
             !(op2_info & MAY_BE_LONG) &&
7169
0
             (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
7170
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE)) {
7171
0
      if_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7172
0
      ir_IF_FALSE_cold(if_op2_double);
7173
0
      ir_END_list(slow_inputs);
7174
0
      ir_IF_TRUE(if_op2_double);
7175
0
    }
7176
0
    if (op1_info & MAY_BE_DOUBLE) {
7177
0
      if (!same_ops && (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7178
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7179
0
        ir_IF_TRUE(if_op1_double_op2_double);
7180
0
      }
7181
0
      ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7182
0
      if (!ref) {
7183
0
        return 0;
7184
0
      }
7185
0
      ir_refs_add(end_inputs, ref);
7186
0
      if (if_op1_double_op2_double) {
7187
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
7188
0
      }
7189
0
    }
7190
0
    if (!same_ops && (op1_info & MAY_BE_LONG)) {
7191
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
7192
0
        if_op1_long_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
7193
0
        ir_IF_FALSE_cold(if_op1_long_op2_double);
7194
0
        ir_END_list(slow_inputs);
7195
0
        ir_IF_TRUE(if_op1_long_op2_double);
7196
0
      }
7197
0
      ref = zend_jit_cmp_long_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7198
0
      if (!ref) {
7199
0
        return 0;
7200
0
      }
7201
0
      ir_refs_add(end_inputs, ref);
7202
0
    } else if (if_op1_double_op2_double) {
7203
0
      ir_END_list(slow_inputs);
7204
0
    }
7205
0
  }
7206
7207
0
  if (has_slow ||
7208
0
      (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
7209
0
      (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE)))) {
7210
0
      ir_ref op1, op2, ref;
7211
7212
0
    if (slow_inputs) {
7213
0
      ir_MERGE_list(slow_inputs);
7214
0
    }
7215
0
    jit_SET_EX_OPLINE(jit, opline);
7216
7217
0
    if (Z_MODE(op1_addr) == IS_REG) {
7218
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7219
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7220
0
        return 0;
7221
0
      }
7222
0
      op1_addr = real_addr;
7223
0
    }
7224
0
    if (Z_MODE(op2_addr) == IS_REG) {
7225
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7226
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7227
0
        return 0;
7228
0
      }
7229
0
      op2_addr = real_addr;
7230
0
    }
7231
7232
0
    op1 = jit_ZVAL_ADDR(jit, op1_addr);
7233
0
    if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7234
0
      op1 = zend_jit_zval_check_undef(jit, op1, opline->op1.var, NULL, false);
7235
0
    }
7236
0
    op2 = jit_ZVAL_ADDR(jit, op2_addr);
7237
0
    if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
7238
0
      op2 = zend_jit_zval_check_undef(jit, op2, opline->op2.var, NULL, false);
7239
0
    }
7240
0
    ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_compare), op1, op2);
7241
0
    if (opline->opcode != ZEND_CASE) {
7242
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7243
0
    }
7244
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
7245
0
    if (may_throw) {
7246
0
      zend_jit_check_exception_undef_result(jit, opline);
7247
0
    }
7248
7249
0
    ref = zend_jit_cmp_slow(jit, ref, opline, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7250
0
    if (!ref) {
7251
0
      return 0;
7252
0
    }
7253
0
    ir_refs_add(end_inputs, ref);
7254
0
  }
7255
7256
0
  if (end_inputs->count) {
7257
0
    uint32_t n = end_inputs->count;
7258
7259
0
    if (smart_branch_opcode && !exit_addr) {
7260
0
      zend_basic_block *bb;
7261
0
      ir_ref ref;
7262
0
      uint32_t label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7263
0
        target_label2 : target_label;
7264
0
      uint32_t label2 = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7265
0
        target_label : target_label2;
7266
7267
0
      ZEND_ASSERT(jit->b >= 0);
7268
0
      bb = &jit->ssa->cfg.blocks[jit->b];
7269
0
      ZEND_ASSERT(bb->successors_count == 2);
7270
7271
0
      if (UNEXPECTED(bb->successors[0] == bb->successors[1])) {
7272
0
        ir_ref merge_inputs = IR_UNUSED;
7273
7274
0
        while (n) {
7275
0
          n--;
7276
0
          ir_IF_TRUE(end_inputs->refs[n]);
7277
0
          ir_END_list(merge_inputs);
7278
0
          ir_IF_FALSE(end_inputs->refs[n]);
7279
0
          ir_END_list(merge_inputs);
7280
0
        }
7281
0
        ir_MERGE_list(merge_inputs);
7282
0
        _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7283
0
      } else if (n == 1) {
7284
0
        ref = end_inputs->refs[0];
7285
0
        _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
7286
0
        _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
7287
0
      } else {
7288
0
        ir_ref true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7289
7290
0
        while (n) {
7291
0
          n--;
7292
0
          jit_IF_TRUE_FALSE_ex(jit, end_inputs->refs[n], label);
7293
0
          ir_END_list(true_inputs);
7294
0
          jit_IF_TRUE_FALSE_ex(jit, end_inputs->refs[n], label2);
7295
0
          ir_END_list(false_inputs);
7296
0
        }
7297
0
        ir_MERGE_list(true_inputs);
7298
0
        _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7299
0
        ir_MERGE_list(false_inputs);
7300
0
        _zend_jit_add_predecessor_ref(jit, label2, jit->b, ir_END());
7301
0
      }
7302
0
      jit->b = -1;
7303
0
    } else {
7304
0
      ir_MERGE_N(n, end_inputs->refs);
7305
0
    }
7306
0
  } else if (smart_branch_opcode && !exit_addr) {
7307
    /* dead code */
7308
0
    _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
7309
0
    jit->b = -1;
7310
0
  }
7311
7312
0
  return 1;
7313
0
}
7314
7315
static int zend_jit_identical(zend_jit_ctx   *jit,
7316
                              const zend_op  *opline,
7317
                              uint32_t        op1_info,
7318
                              zend_ssa_range *op1_range,
7319
                              zend_jit_addr   op1_addr,
7320
                              uint32_t        op2_info,
7321
                              zend_ssa_range *op2_range,
7322
                              zend_jit_addr   op2_addr,
7323
                              zend_jit_addr   res_addr,
7324
                              int             may_throw,
7325
                              uint8_t         smart_branch_opcode,
7326
                              uint32_t        target_label,
7327
                              uint32_t        target_label2,
7328
                              const void     *exit_addr,
7329
                              bool       skip_comparison)
7330
0
{
7331
0
  bool always_false = false, always_true = false;
7332
0
  ir_ref ref = IR_UNUSED;
7333
7334
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7335
0
    ir_ref op1 = jit_ZVAL_ADDR(jit, op1_addr);
7336
0
    op1 = zend_jit_zval_check_undef(jit, op1, opline->op1.var, opline, false);
7337
0
    op1_info |= MAY_BE_NULL;
7338
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1);
7339
0
  }
7340
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
7341
0
    ir_ref op2 = jit_ZVAL_ADDR(jit, op2_addr);
7342
0
    op2 = zend_jit_zval_check_undef(jit, op2, opline->op2.var, opline, false);
7343
0
    op2_info |= MAY_BE_NULL;
7344
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2);
7345
0
  }
7346
7347
0
  if ((op1_info & op2_info & MAY_BE_ANY) == 0) {
7348
0
    always_false = true;
7349
0
  } else if (has_concrete_type(op1_info)
7350
0
   && has_concrete_type(op2_info)
7351
0
   && concrete_type(op1_info) == concrete_type(op2_info)
7352
0
   && concrete_type(op1_info) <= IS_TRUE) {
7353
0
    always_true = true;
7354
0
  } else if (Z_MODE(op1_addr) == IS_CONST_ZVAL && Z_MODE(op2_addr) == IS_CONST_ZVAL) {
7355
0
    if (zend_is_identical(Z_ZV(op1_addr), Z_ZV(op2_addr))) {
7356
0
      always_true = true;
7357
0
    } else {
7358
0
      always_false = true;
7359
0
    }
7360
0
  }
7361
7362
0
  if (always_true) {
7363
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7364
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7365
0
    }
7366
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
7367
0
    if (!smart_branch_opcode
7368
0
     || smart_branch_opcode == ZEND_JMPZ_EX
7369
0
     || smart_branch_opcode == ZEND_JMPNZ_EX) {
7370
0
      jit_set_Z_TYPE_INFO(jit, res_addr, opline->opcode != ZEND_IS_NOT_IDENTICAL ? IS_TRUE : IS_FALSE);
7371
0
    }
7372
0
    if (may_throw) {
7373
0
      zend_jit_check_exception(jit);
7374
0
    }
7375
0
    if (exit_addr) {
7376
0
      if (smart_branch_opcode == ZEND_JMPNZ || smart_branch_opcode == ZEND_JMPNZ_EX) {
7377
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7378
0
      }
7379
0
    } else if (smart_branch_opcode) {
7380
0
      uint32_t label;
7381
7382
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7383
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7384
0
          target_label : target_label2;
7385
0
      } else {
7386
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7387
0
          target_label2 : target_label;
7388
0
      }
7389
0
      _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7390
0
      jit->b = -1;
7391
0
    }
7392
0
    return 1;
7393
0
  } else if (always_false) {
7394
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7395
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7396
0
    }
7397
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
7398
0
    if (!smart_branch_opcode
7399
0
     || smart_branch_opcode == ZEND_JMPZ_EX
7400
0
     || smart_branch_opcode == ZEND_JMPNZ_EX) {
7401
0
      jit_set_Z_TYPE_INFO(jit, res_addr, opline->opcode != ZEND_IS_NOT_IDENTICAL ? IS_FALSE : IS_TRUE);
7402
0
    }
7403
0
    if (may_throw) {
7404
0
      zend_jit_check_exception(jit);
7405
0
    }
7406
0
    if (exit_addr) {
7407
0
      if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
7408
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7409
0
      }
7410
0
    } else if (smart_branch_opcode) {
7411
0
      uint32_t label;
7412
7413
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7414
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7415
0
          target_label2 : target_label;
7416
0
      } else {
7417
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7418
0
          target_label : target_label2;
7419
0
      }
7420
0
      _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7421
0
      jit->b = -1;
7422
0
    }
7423
0
    return 1;
7424
0
  }
7425
7426
0
  if ((opline->op1_type & (IS_CV|IS_VAR)) && (op1_info & MAY_BE_REF)) {
7427
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
7428
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7429
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
7430
0
  }
7431
0
  if ((opline->op2_type & (IS_CV|IS_VAR)) && (op2_info & MAY_BE_REF)) {
7432
0
    ref = jit_ZVAL_ADDR(jit, op2_addr);
7433
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7434
0
    op2_addr = ZEND_ADDR_REF_ZVAL(ref);
7435
0
  }
7436
7437
0
  if ((op1_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG &&
7438
0
      (op2_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
7439
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);
7440
0
    if (!ref) {
7441
0
      return 0;
7442
0
    }
7443
0
  } else if ((op1_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE &&
7444
0
             (op2_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE) {
7445
0
    ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7446
0
    if (!ref) {
7447
0
      return 0;
7448
0
    }
7449
0
  } else {
7450
0
    if (opline->op1_type != IS_CONST) {
7451
0
      if (Z_MODE(op1_addr) == IS_REG) {
7452
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7453
0
        if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7454
0
          return 0;
7455
0
        }
7456
0
        op1_addr = real_addr;
7457
0
      }
7458
0
    }
7459
0
    if (opline->op2_type != IS_CONST) {
7460
0
      if (Z_MODE(op2_addr) == IS_REG) {
7461
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7462
0
        if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7463
0
          return 0;
7464
0
        }
7465
0
      }
7466
0
    }
7467
7468
0
    if (Z_MODE(op1_addr) == IS_CONST_ZVAL && Z_TYPE_P(Z_ZV(op1_addr)) <= IS_TRUE) {
7469
0
      zval *val = Z_ZV(op1_addr);
7470
7471
0
      ref = ir_EQ(jit_Z_TYPE(jit, op2_addr), ir_CONST_U8(Z_TYPE_P(val)));
7472
0
    } else if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_TYPE_P(Z_ZV(op2_addr)) <= IS_TRUE) {
7473
0
      zval *val = Z_ZV(op2_addr);
7474
7475
0
      ref = ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(Z_TYPE_P(val)));
7476
0
    } else {
7477
0
      if (Z_MODE(op1_addr) == IS_REG) {
7478
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7479
0
        if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7480
0
          return 0;
7481
0
        }
7482
0
        op1_addr = real_addr;
7483
0
      }
7484
0
      if (Z_MODE(op2_addr) == IS_REG) {
7485
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7486
0
        if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7487
0
          return 0;
7488
0
        }
7489
0
        op2_addr = real_addr;
7490
0
      }
7491
0
      if (may_throw) {
7492
0
        jit_SET_EX_OPLINE(jit, opline);
7493
0
      }
7494
7495
0
      ref = ir_CALL_2(IR_BOOL, ir_CONST_FC_FUNC(zend_is_identical),
7496
0
        jit_ZVAL_ADDR(jit, op1_addr),
7497
0
        jit_ZVAL_ADDR(jit, op2_addr));
7498
0
    }
7499
7500
0
    if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
7501
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7502
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7503
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7504
0
      } else {
7505
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7506
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7507
0
      }
7508
0
    }
7509
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7510
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7511
0
    }
7512
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
7513
0
    if (may_throw) {
7514
0
      zend_jit_check_exception_undef_result(jit, opline);
7515
0
    }
7516
0
    if (exit_addr) {
7517
0
      if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
7518
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7519
0
      } else {
7520
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7521
0
      }
7522
0
    } else if (smart_branch_opcode) {
7523
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7524
        /* swap labels */
7525
0
        uint32_t tmp = target_label;
7526
0
        target_label = target_label2;
7527
0
        target_label2 = tmp;
7528
0
      }
7529
0
      ref = jit_IF_ex(jit, ref,
7530
0
        (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
7531
0
    }
7532
0
  }
7533
7534
0
  if (smart_branch_opcode && !exit_addr) {
7535
0
    zend_basic_block *bb;
7536
7537
0
    ZEND_ASSERT(jit->b >= 0);
7538
0
    bb = &jit->ssa->cfg.blocks[jit->b];
7539
0
    ZEND_ASSERT(bb->successors_count == 2);
7540
7541
0
    if (bb->successors_count == 2 && bb->successors[0] == bb->successors[1]) {
7542
0
      ir_IF_TRUE(ref);
7543
0
      ir_MERGE_WITH_EMPTY_FALSE(ref);
7544
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
7545
0
    } else {
7546
0
      ZEND_ASSERT(bb->successors_count == 2);
7547
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
7548
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
7549
0
    }
7550
0
    jit->b = -1;
7551
0
  }
7552
7553
0
  return 1;
7554
0
}
7555
7556
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)
7557
0
{
7558
0
  uint32_t true_label = -1;
7559
0
  uint32_t false_label = -1;
7560
0
  bool set_bool = false;
7561
0
  bool set_bool_not = false;
7562
0
  bool always_true = false, always_false = false;
7563
0
  ir_ref ref, end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7564
0
  ir_type type = IR_UNUSED;
7565
7566
0
  if (branch_opcode == ZEND_BOOL) {
7567
0
    set_bool = true;
7568
0
  } else if (branch_opcode == ZEND_BOOL_NOT) {
7569
0
    set_bool = true;
7570
0
    set_bool_not = true;
7571
0
  } else if (branch_opcode == ZEND_JMPZ) {
7572
0
    true_label = target_label2;
7573
0
    false_label = target_label;
7574
0
  } else if (branch_opcode == ZEND_JMPNZ) {
7575
0
    true_label = target_label;
7576
0
    false_label = target_label2;
7577
0
  } else if (branch_opcode == ZEND_JMPZ_EX) {
7578
0
    set_bool = true;
7579
0
    true_label = target_label2;
7580
0
    false_label = target_label;
7581
0
  } else if (branch_opcode == ZEND_JMPNZ_EX) {
7582
0
    set_bool = true;
7583
0
    true_label = target_label;
7584
0
    false_label = target_label2;
7585
0
  } else {
7586
0
    ZEND_UNREACHABLE();
7587
0
  }
7588
7589
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_REF)) {
7590
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
7591
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7592
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
7593
0
  }
7594
7595
0
  if (Z_MODE(op1_addr) == IS_CONST_ZVAL
7596
    /* NAN Value must cause a warning to be emitted */
7597
0
    && (Z_TYPE_P(Z_ZV(op1_addr)) != IS_DOUBLE || !zend_isnan(Z_DVAL_P(Z_ZV(op1_addr))))) {
7598
0
    if (zend_is_true(Z_ZV(op1_addr))) {
7599
0
      always_true = true;
7600
0
    } else {
7601
0
      always_false = true;
7602
0
    }
7603
0
  } else if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE)) {
7604
0
    if (!(op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)-MAY_BE_TRUE))) {
7605
0
      always_true = true;
7606
0
    } else if (!(op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE)))) {
7607
0
      if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7608
0
        ref = jit_ZVAL_ADDR(jit, op1_addr);
7609
0
        zend_jit_zval_check_undef(jit, ref, opline->op1.var, opline, false);
7610
0
      }
7611
0
      always_false = true;
7612
0
    }
7613
0
  }
7614
7615
0
  if (always_true) {
7616
0
    if (set_bool) {
7617
0
      jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7618
0
    }
7619
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7620
0
    if (may_throw) {
7621
0
      zend_jit_check_exception(jit);
7622
0
    }
7623
0
    if (true_label != (uint32_t)-1) {
7624
0
      ZEND_ASSERT(exit_addr == NULL);
7625
0
      _zend_jit_add_predecessor_ref(jit, true_label, jit->b, ir_END());
7626
0
      jit->b = -1;
7627
0
    }
7628
0
    return 1;
7629
0
  } else if (always_false) {
7630
0
    if (set_bool) {
7631
0
      jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7632
0
    }
7633
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7634
0
    if (may_throw) {
7635
0
      zend_jit_check_exception(jit);
7636
0
    }
7637
0
    if (false_label != (uint32_t)-1) {
7638
0
      ZEND_ASSERT(exit_addr == NULL);
7639
0
      _zend_jit_add_predecessor_ref(jit, false_label, jit->b, ir_END());
7640
0
      jit->b = -1;
7641
0
    }
7642
0
    return 1;
7643
0
  }
7644
7645
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE)) {
7646
0
    type = jit_Z_TYPE(jit, op1_addr);
7647
0
    if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
7648
0
      ir_ref if_type = ir_IF(ir_LT(type, ir_CONST_U8(IS_TRUE)));
7649
7650
0
      ir_IF_TRUE_cold(if_type);
7651
7652
0
      if (op1_info & MAY_BE_UNDEF) {
7653
0
        zend_jit_type_check_undef(jit,
7654
0
          type,
7655
0
          opline->op1.var,
7656
0
          opline, true, false, true);
7657
0
      }
7658
0
      if (set_bool) {
7659
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7660
0
      }
7661
0
      if (exit_addr) {
7662
0
        if (branch_opcode == ZEND_JMPNZ || branch_opcode == ZEND_JMPNZ_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 (false_label != (uint32_t)-1) {
7668
0
        ir_END_list(false_inputs);
7669
0
      } else {
7670
0
        ir_END_list(end_inputs);
7671
0
      }
7672
0
      ir_IF_FALSE(if_type);
7673
0
    }
7674
7675
0
    if (op1_info & MAY_BE_TRUE) {
7676
0
      ir_ref if_type = IR_UNUSED;
7677
7678
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
7679
0
        if_type = ir_IF(ir_EQ(type, ir_CONST_U8(IS_TRUE)));
7680
7681
0
        ir_IF_TRUE(if_type);
7682
0
      }
7683
0
      if (set_bool) {
7684
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7685
0
      }
7686
0
      if (exit_addr) {
7687
0
        if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7688
0
          ir_END_list(end_inputs);
7689
0
        } else {
7690
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7691
0
        }
7692
0
      } else if (true_label != (uint32_t)-1) {
7693
0
        ir_END_list(true_inputs);
7694
0
      } else {
7695
0
        ir_END_list(end_inputs);
7696
0
      }
7697
0
      if (if_type) {
7698
0
        ir_IF_FALSE(if_type);
7699
0
      }
7700
0
    }
7701
0
  }
7702
7703
0
  if (op1_info & MAY_BE_LONG) {
7704
0
    ir_ref if_long = IR_UNUSED;
7705
0
    ir_ref ref;
7706
7707
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG))) {
7708
0
      if (!type) {
7709
0
        type = jit_Z_TYPE(jit, op1_addr);
7710
0
      }
7711
0
      if_long = ir_IF(ir_EQ(type, ir_CONST_U8(IS_LONG)));
7712
0
      ir_IF_TRUE(if_long);
7713
0
    }
7714
0
    ref = jit_Z_LVAL(jit, op1_addr);
7715
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7716
0
      ref = ir_NE(ref, ir_CONST_LONG(0));
7717
0
      if (set_bool_not) {
7718
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7719
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7720
0
      } else {
7721
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7722
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7723
0
      }
7724
0
      ir_END_list(end_inputs);
7725
0
    } else if (exit_addr) {
7726
0
      if (set_bool) {
7727
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7728
0
          ir_ADD_U32(ir_ZEXT_U32(ir_NE(ref, ir_CONST_LONG(0))), ir_CONST_U32(IS_FALSE)));
7729
0
      }
7730
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7731
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7732
0
      } else {
7733
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7734
0
      }
7735
0
      ir_END_list(end_inputs);
7736
0
    } else {
7737
0
      ir_ref if_val = ir_IF(ref);
7738
0
      ir_IF_TRUE(if_val);
7739
0
      if (set_bool) {
7740
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7741
0
      }
7742
0
      ir_END_list(true_inputs);
7743
0
      ir_IF_FALSE(if_val);
7744
0
      if (set_bool) {
7745
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7746
0
      }
7747
0
      ir_END_list(false_inputs);
7748
0
    }
7749
0
    if (if_long) {
7750
0
      ir_IF_FALSE(if_long);
7751
0
    }
7752
0
  }
7753
7754
0
  if (op1_info & MAY_BE_DOUBLE) {
7755
0
    ir_ref if_double = IR_UNUSED;
7756
0
    ir_ref ref;
7757
7758
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))) {
7759
0
      if (!type) {
7760
0
        type = jit_Z_TYPE(jit, op1_addr);
7761
0
      }
7762
0
      if_double = ir_IF(ir_EQ(type, ir_CONST_U8(IS_DOUBLE)));
7763
0
      ir_IF_TRUE(if_double);
7764
0
    }
7765
7766
0
    ir_ref dval = jit_Z_DVAL(jit, op1_addr);
7767
0
    ir_ref is_nan = ir_NE(dval, dval);
7768
0
    ir_ref if_val = ir_IF(is_nan);
7769
0
    ir_IF_TRUE_cold(if_val);
7770
0
    jit_SET_EX_OPLINE(jit, opline);
7771
0
    ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_nan_coerced_to_type_warning));
7772
0
    ir_MERGE_WITH_EMPTY_FALSE(if_val);
7773
7774
0
    ref = ir_NE(dval, ir_CONST_DOUBLE(0.0));
7775
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7776
0
      if (set_bool_not) {
7777
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7778
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7779
0
      } else {
7780
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7781
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7782
0
      }
7783
0
      ir_END_list(end_inputs);
7784
0
    } else if (exit_addr) {
7785
0
        if (set_bool) {
7786
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7787
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7788
0
        }
7789
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7790
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7791
0
      } else {
7792
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7793
0
      }
7794
0
      ir_END_list(end_inputs);
7795
0
    } else {
7796
0
      ir_ref if_val = ir_IF(ref);
7797
0
      ir_IF_TRUE(if_val);
7798
0
      if (set_bool) {
7799
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7800
0
      }
7801
0
      ir_END_list(true_inputs);
7802
0
      ir_IF_FALSE(if_val);
7803
0
      if (set_bool) {
7804
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7805
0
      }
7806
0
      ir_END_list(false_inputs);
7807
0
    }
7808
0
    if (if_double) {
7809
0
      ir_IF_FALSE(if_double);
7810
0
    }
7811
0
  }
7812
7813
0
  if (op1_info & (MAY_BE_ANY - (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))) {
7814
0
    jit_SET_EX_OPLINE(jit, opline);
7815
0
    ref = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_is_true), jit_ZVAL_ADDR(jit, op1_addr));
7816
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7817
0
    if (may_throw) {
7818
0
      zend_jit_check_exception_undef_result(jit, opline);
7819
0
    }
7820
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7821
0
      if (set_bool_not) {
7822
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7823
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7824
0
      } else {
7825
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7826
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7827
0
      }
7828
0
      if (end_inputs) {
7829
0
        ir_END_list(end_inputs);
7830
0
      }
7831
0
    } else if (exit_addr) {
7832
0
      if (set_bool) {
7833
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7834
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7835
0
      }
7836
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7837
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7838
0
      } else {
7839
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7840
0
      }
7841
0
      if (end_inputs) {
7842
0
        ir_END_list(end_inputs);
7843
0
      }
7844
0
    } else {
7845
0
      ir_ref if_val = ir_IF(ref);
7846
0
      ir_IF_TRUE(if_val);
7847
0
      if (set_bool) {
7848
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7849
0
      }
7850
0
      ir_END_list(true_inputs);
7851
0
      ir_IF_FALSE(if_val);
7852
0
      if (set_bool) {
7853
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7854
0
      }
7855
0
      ir_END_list(false_inputs);
7856
0
    }
7857
0
  }
7858
7859
0
  if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT || exit_addr) {
7860
0
    if (end_inputs) {
7861
0
      ir_MERGE_list(end_inputs);
7862
0
    }
7863
0
  } else {
7864
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
7865
0
  }
7866
7867
0
  return 1;
7868
0
}
7869
7870
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)
7871
0
{
7872
0
  uint32_t defined_label = (uint32_t)-1;
7873
0
  uint32_t undefined_label = (uint32_t)-1;
7874
0
  zval *zv = RT_CONSTANT(opline, opline->op1);
7875
0
  zend_jit_addr res_addr = 0;
7876
0
  ir_ref ref, ref2, if_set, if_zero, if_set2;
7877
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7878
7879
0
  if (smart_branch_opcode && !exit_addr) {
7880
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7881
0
      defined_label = target_label2;
7882
0
      undefined_label = target_label;
7883
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
7884
0
      defined_label = target_label;
7885
0
      undefined_label = target_label2;
7886
0
    } else {
7887
0
      ZEND_UNREACHABLE();
7888
0
    }
7889
0
  } else {
7890
0
    res_addr = RES_ADDR();
7891
0
  }
7892
7893
  // if (CACHED_PTR(opline->extended_value)) {
7894
0
  ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value));
7895
7896
0
  if_set = ir_IF(ref);
7897
7898
0
  ir_IF_FALSE_cold(if_set);
7899
0
  if_zero = ir_END();
7900
7901
0
  ir_IF_TRUE(if_set);
7902
0
  if_set2 = ir_IF(ir_AND_A(ref, ir_CONST_ADDR(CACHE_SPECIAL)));
7903
0
  ir_IF_FALSE(if_set2);
7904
7905
0
  if (exit_addr) {
7906
0
    if (smart_branch_opcode == ZEND_JMPNZ) {
7907
0
      jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7908
0
    } else {
7909
0
      ir_END_list(end_inputs);
7910
0
    }
7911
0
  } else if (smart_branch_opcode) {
7912
0
    ir_END_list(true_inputs);
7913
0
  } else {
7914
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
7915
0
    ir_END_list(end_inputs);
7916
0
  }
7917
7918
0
  ir_IF_TRUE_cold(if_set2);
7919
7920
0
  ref2 = jit_EG(zend_constants);
7921
0
  ref = ir_SHR_A(ref, ir_CONST_ADDR(1));
7922
0
  if (sizeof(void*) == 8) {
7923
0
    ref = ir_TRUNC_U32(ref);
7924
0
  }
7925
0
  ref2 = ir_EQ(ref, ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(ref2), offsetof(HashTable, nNumOfElements))));
7926
0
  ref2 = ir_IF(ref2);
7927
0
  ir_IF_TRUE(ref2);
7928
7929
0
  if (exit_addr) {
7930
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7931
0
      jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7932
0
    } else {
7933
0
      ir_END_list(end_inputs);
7934
0
    }
7935
0
  } else if (smart_branch_opcode) {
7936
0
    ir_END_list(false_inputs);
7937
0
  } else {
7938
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
7939
0
    ir_END_list(end_inputs);
7940
0
  }
7941
7942
0
  ir_IF_FALSE(ref2);
7943
0
  ir_MERGE_2(if_zero, ir_END());
7944
7945
0
  jit_SET_EX_OPLINE(jit, opline);
7946
0
  ref2 = ir_NE(ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_check_constant), ir_CONST_ADDR(zv)), IR_NULL);
7947
0
  if (exit_addr) {
7948
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7949
0
      ir_GUARD(ref2, ir_CONST_ADDR(exit_addr));
7950
0
    } else {
7951
0
      ir_GUARD_NOT(ref2, ir_CONST_ADDR(exit_addr));
7952
0
    }
7953
0
    ir_END_list(end_inputs);
7954
0
  } else if (smart_branch_opcode) {
7955
0
    ref2 = ir_IF(ref2);
7956
0
    ir_IF_TRUE(ref2);
7957
0
    ir_END_list(true_inputs);
7958
0
    ir_IF_FALSE(ref2);
7959
0
    ir_END_list(false_inputs);
7960
0
  } else {
7961
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7962
0
      ir_ADD_U32(ir_ZEXT_U32(ref2), ir_CONST_U32(IS_FALSE)));
7963
0
    ir_END_list(end_inputs);
7964
0
  }
7965
7966
0
  if (!smart_branch_opcode || exit_addr) {
7967
0
    if (end_inputs) {
7968
0
      ir_MERGE_list(end_inputs);
7969
0
    }
7970
0
  } else {
7971
0
    _zend_jit_merge_smart_branch_inputs(jit, defined_label, undefined_label, true_inputs, false_inputs);
7972
0
  }
7973
7974
0
  return 1;
7975
0
}
7976
7977
static int zend_jit_escape_if_undef(zend_jit_ctx *jit, int var, uint32_t flags, const zend_op *opline, const zend_op_array *op_array, int8_t reg)
7978
0
{
7979
0
  zend_jit_addr reg_addr = ZEND_ADDR_REF_ZVAL(zend_jit_deopt_rload(jit, IR_ADDR, reg));
7980
0
  ir_ref if_def = ir_IF(jit_Z_TYPE(jit, reg_addr));
7981
7982
0
  ir_IF_FALSE_cold(if_def);
7983
7984
0
  if (flags & ZEND_JIT_EXIT_RESTORE_CALL) {
7985
0
    if (!zend_jit_save_call_chain(jit, -1)) {
7986
0
      return 0;
7987
0
    }
7988
0
  }
7989
7990
0
  if ((opline-1)->opcode != ZEND_FETCH_CONSTANT
7991
0
   && (opline-1)->opcode != ZEND_FETCH_LIST_R
7992
0
   && ((opline-1)->op1_type & (IS_VAR|IS_TMP_VAR))
7993
0
   && !(flags & ZEND_JIT_EXIT_FREE_OP1)) {
7994
0
    zend_jit_addr val_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline-1)->op1.var);
7995
7996
0
    zend_jit_zval_try_addref(jit, val_addr);
7997
0
  }
7998
7999
0
  jit_LOAD_IP_ADDR(jit, opline - 1);
8000
8001
  /* We can't use trace_escape() because opcode handler may be overridden by JIT */
8002
0
  zend_jit_op_array_trace_extension *jit_extension =
8003
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8004
0
  size_t offset = jit_extension->offset;
8005
0
  ir_ref ref = ir_CONST_FC_FUNC(ZEND_OP_TRACE_INFO((opline - 1), offset)->orig_handler);
8006
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
8007
0
    ir_TAILCALL(IR_OPCODE_HANDLER_RET, ref);
8008
0
  } else {
8009
0
    ir_ref opline_ref = ir_CALL_2(IR_OPCODE_HANDLER_RET, ref, jit_FP(jit), jit_IP(jit));
8010
0
    zend_jit_vm_enter(jit, opline_ref);
8011
0
  }
8012
8013
0
  ir_IF_TRUE(if_def);
8014
8015
0
  return 1;
8016
0
}
8017
8018
static int zend_jit_restore_zval(zend_jit_ctx *jit, int var, int8_t reg)
8019
0
{
8020
0
  zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
8021
0
  zend_jit_addr reg_addr = ZEND_ADDR_REF_ZVAL(zend_jit_deopt_rload(jit, IR_ADDR, reg));
8022
8023
  // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8024
0
  jit_ZVAL_COPY(jit, var_addr, MAY_BE_ANY, reg_addr, MAY_BE_ANY, true);
8025
0
  return 1;
8026
0
}
8027
8028
static zend_jit_addr zend_jit_guard_fetch_result_type(zend_jit_ctx         *jit,
8029
                                                      const zend_op        *opline,
8030
                                                      zend_jit_addr         val_addr,
8031
                                                      uint8_t               type,
8032
                                                      bool                  deref,
8033
                                                      uint32_t              flags,
8034
                                                      bool                  op1_avoid_refcounting)
8035
0
{
8036
0
  zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
8037
0
  int32_t exit_point;
8038
0
  const void *res_exit_addr = NULL;
8039
0
  ir_ref end1 = IR_UNUSED, ref1 = IR_UNUSED;
8040
0
  ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
8041
0
  uint32_t old_op1_info = 0;
8042
0
  uint32_t old_info;
8043
0
  ir_ref old_ref;
8044
8045
8046
0
  if (opline->op1_type & (IS_VAR|IS_TMP_VAR|IS_CV)) {
8047
0
    old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
8048
0
    if (op1_avoid_refcounting
8049
0
     || ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
8050
0
      && STACK_FLAGS(stack, EX_VAR_TO_NUM(opline->op1.var)) & (ZREG_ZVAL_ADDREF|ZREG_THIS))) {
8051
0
      SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
8052
0
    }
8053
0
  }
8054
0
  old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
8055
0
  old_ref = STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
8056
0
  CLEAR_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
8057
0
  SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
8058
8059
0
  if (deref) {
8060
0
    ir_ref if_type;
8061
8062
0
    if (type == IS_NULL && (opline->opcode == ZEND_FETCH_DIM_IS || opline->opcode == ZEND_FETCH_OBJ_IS)) {
8063
0
      if_type = ir_IF(ir_ULE(jit_Z_TYPE(jit, val_addr), ir_CONST_U8(type)));
8064
0
    } else {
8065
0
      if_type = jit_if_Z_TYPE(jit, val_addr, type);
8066
0
    }
8067
0
    ir_IF_TRUE(if_type);
8068
0
    end1 = ir_END();
8069
0
    ref1 = ref;
8070
0
    ir_IF_FALSE_cold(if_type);
8071
8072
0
    SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ref, ZREG_ZVAL_COPY);
8073
0
    exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
8074
0
    res_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8075
0
    if (!res_exit_addr) {
8076
0
      return 0;
8077
0
    }
8078
8079
0
    jit_guard_Z_TYPE(jit, val_addr, IS_REFERENCE, res_exit_addr);
8080
0
    ref = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
8081
0
    val_addr = ZEND_ADDR_REF_ZVAL(ref);
8082
0
  }
8083
8084
0
  SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ref, ZREG_ZVAL_COPY);
8085
0
  exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
8086
0
  res_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8087
0
  if (!res_exit_addr) {
8088
0
    return 0;
8089
0
  }
8090
8091
0
  if (!deref && type == IS_NULL && (opline->opcode == ZEND_FETCH_DIM_IS || opline->opcode == ZEND_FETCH_OBJ_IS)) {
8092
0
    ir_GUARD(ir_ULE(jit_Z_TYPE(jit, val_addr), ir_CONST_U8(type)), ir_CONST_ADDR(res_exit_addr));
8093
0
  } else {
8094
0
    jit_guard_Z_TYPE(jit, val_addr, type, res_exit_addr);
8095
0
  }
8096
8097
0
  if (deref) {
8098
0
    ir_MERGE_WITH(end1);
8099
0
    ref = ir_PHI_2(IR_ADDR, ref, ref1);
8100
0
  }
8101
8102
0
  val_addr = ZEND_ADDR_REF_ZVAL(ref);
8103
8104
0
  SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), old_ref);
8105
0
  SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
8106
0
  if (opline->op1_type & (IS_VAR|IS_TMP_VAR|IS_CV)) {
8107
0
    SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
8108
0
  }
8109
8110
0
  return val_addr;
8111
0
}
8112
8113
static int zend_jit_fetch_constant(zend_jit_ctx         *jit,
8114
                                   const zend_op        *opline,
8115
                                   const zend_op_array  *op_array,
8116
                                   zend_ssa             *ssa,
8117
                                   const zend_ssa_op    *ssa_op,
8118
                                   zend_jit_addr         res_addr)
8119
0
{
8120
0
  zval *zv = RT_CONSTANT(opline, opline->op2) + 1;
8121
0
  uint32_t res_info = RES_INFO();
8122
0
  ir_ref ref, ref2, if_set, if_special, not_set_path, special_path, fast_path;
8123
8124
  // JIT: c = CACHED_PTR(opline->extended_value);
8125
0
  ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value));
8126
8127
  // JIT: if (c != NULL)
8128
0
  if_set = ir_IF(ref);
8129
8130
0
  if (!zend_jit_is_persistent_constant(zv, opline->op1.num)) {
8131
    // JIT: if (!IS_SPECIAL_CACHE_VAL(c))
8132
0
    ir_IF_FALSE_cold(if_set);
8133
0
    not_set_path = ir_END();
8134
0
    ir_IF_TRUE(if_set);
8135
0
    if_special = ir_IF(ir_AND_A(ref, ir_CONST_ADDR(CACHE_SPECIAL)));
8136
0
    ir_IF_TRUE_cold(if_special);
8137
0
    special_path = ir_END();
8138
0
    ir_IF_FALSE(if_special);
8139
0
    fast_path = ir_END();
8140
0
    ir_MERGE_2(not_set_path, special_path);
8141
0
  } else {
8142
0
    ir_IF_TRUE(if_set);
8143
0
    fast_path = ir_END();
8144
0
    ir_IF_FALSE_cold(if_set);
8145
0
  }
8146
8147
  // JIT: zend_jit_get_constant(RT_CONSTANT(opline, opline->op2) + 1, opline->op1.num);
8148
0
  jit_SET_EX_OPLINE(jit, opline);
8149
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_get_constant),
8150
0
    ir_CONST_ADDR(zv),
8151
0
    ir_CONST_U32(opline->op1.num));
8152
0
  ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
8153
8154
0
  ir_MERGE_WITH(fast_path);
8155
0
  ref = ir_PHI_2(IR_ADDR, ref2, ref);
8156
8157
0
  if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
8158
0
    uint8_t type = concrete_type(res_info);
8159
0
    zend_jit_addr const_addr = ZEND_ADDR_REF_ZVAL(ref);
8160
8161
0
    const_addr = zend_jit_guard_fetch_result_type(jit, opline, const_addr, type, false, 0, false);
8162
0
    if (!const_addr) {
8163
0
      return 0;
8164
0
    }
8165
8166
0
    res_info &= ~MAY_BE_GUARD;
8167
0
    ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
8168
8169
    // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8170
0
    jit_ZVAL_COPY(jit, res_addr, MAY_BE_ANY, const_addr, res_info, true);
8171
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
8172
0
      return 0;
8173
0
    }
8174
0
  } else {
8175
0
    ir_ref const_addr = ZEND_ADDR_REF_ZVAL(ref);
8176
8177
    // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8178
0
    jit_ZVAL_COPY(jit, res_addr, MAY_BE_ANY, const_addr, MAY_BE_ANY, true);
8179
0
  }
8180
8181
8182
0
  return 1;
8183
0
}
8184
8185
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)
8186
0
{
8187
0
  uint32_t  mask;
8188
0
  zend_jit_addr op1_addr = OP1_ADDR();
8189
0
  zend_jit_addr res_addr = 0;
8190
0
  uint32_t true_label = -1, false_label = -1;
8191
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
8192
8193
  // TODO: support for is_resource() ???
8194
0
  ZEND_ASSERT(opline->extended_value != MAY_BE_RESOURCE);
8195
8196
0
  if (smart_branch_opcode && !exit_addr) {
8197
0
    if (smart_branch_opcode == ZEND_JMPZ) {
8198
0
      true_label = target_label2;
8199
0
      false_label = target_label;
8200
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
8201
0
      true_label = target_label;
8202
0
      false_label = target_label2;
8203
0
    } else {
8204
0
      ZEND_UNREACHABLE();
8205
0
    }
8206
0
  } else {
8207
0
    res_addr = RES_ADDR();
8208
0
  }
8209
8210
0
  if (op1_info & MAY_BE_UNDEF) {
8211
0
    ir_ref if_def = IR_UNUSED;
8212
8213
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
8214
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
8215
0
      ir_IF_FALSE_cold(if_def);
8216
0
    }
8217
8218
0
    jit_SET_EX_OPLINE(jit, opline);
8219
0
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
8220
0
    zend_jit_check_exception_undef_result(jit, opline);
8221
0
    if (opline->extended_value & MAY_BE_NULL) {
8222
0
      if (exit_addr) {
8223
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
8224
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8225
0
        } else {
8226
0
          ir_END_list(end_inputs);
8227
0
        }
8228
0
      } else if (smart_branch_opcode) {
8229
0
        ir_END_list(true_inputs);
8230
0
      } else {
8231
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8232
0
        ir_END_list(end_inputs);
8233
0
      }
8234
0
    } else {
8235
0
      if (exit_addr) {
8236
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8237
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8238
0
        } else {
8239
0
          ir_END_list(end_inputs);
8240
0
        }
8241
0
      } else if (smart_branch_opcode) {
8242
0
        ir_END_list(false_inputs);
8243
0
      } else {
8244
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8245
0
        if (if_def) {
8246
0
          ir_END_list(end_inputs);
8247
0
        }
8248
0
      }
8249
0
    }
8250
8251
0
    if (if_def) {
8252
0
      ir_IF_TRUE(if_def);
8253
0
      op1_info |= MAY_BE_NULL;
8254
0
    }
8255
0
  }
8256
8257
0
  if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
8258
0
    mask = opline->extended_value;
8259
0
    if (!(op1_info & MAY_BE_GUARD) && !(op1_info & (MAY_BE_ANY - mask))) {
8260
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8261
0
      if (exit_addr) {
8262
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
8263
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8264
0
        } else if (end_inputs) {
8265
0
          ir_END_list(end_inputs);
8266
0
        }
8267
0
      } else if (smart_branch_opcode) {
8268
0
        ir_END_list(true_inputs);
8269
0
      } else {
8270
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8271
0
        ir_END_list(end_inputs);
8272
0
      }
8273
0
      } else if (!(op1_info & MAY_BE_GUARD) && !(op1_info & mask)) {
8274
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8275
0
      if (exit_addr) {
8276
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8277
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8278
0
        } else if (end_inputs) {
8279
0
          ir_END_list(end_inputs);
8280
0
        }
8281
0
      } else if (smart_branch_opcode) {
8282
0
        ir_END_list(false_inputs);
8283
0
      } else {
8284
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8285
0
        ir_END_list(end_inputs);
8286
0
      }
8287
0
    } else {
8288
0
      ir_ref ref;
8289
0
      bool invert = false;
8290
0
      uint8_t type;
8291
8292
0
      switch (mask) {
8293
0
        case MAY_BE_NULL:   type = IS_NULL;   break;
8294
0
        case MAY_BE_FALSE:  type = IS_FALSE;  break;
8295
0
        case MAY_BE_TRUE:   type = IS_TRUE;   break;
8296
0
        case MAY_BE_LONG:   type = IS_LONG;   break;
8297
0
        case MAY_BE_DOUBLE: type = IS_DOUBLE; break;
8298
0
        case MAY_BE_STRING: type = IS_STRING; break;
8299
0
        case MAY_BE_ARRAY:  type = IS_ARRAY;  break;
8300
0
        case MAY_BE_OBJECT: type = IS_OBJECT; break;
8301
0
        case MAY_BE_ANY - MAY_BE_NULL:     type = IS_NULL;   invert = true; break;
8302
0
        case MAY_BE_ANY - MAY_BE_FALSE:    type = IS_FALSE;  invert = true; break;
8303
0
        case MAY_BE_ANY - MAY_BE_TRUE:     type = IS_TRUE;   invert = true; break;
8304
0
        case MAY_BE_ANY - MAY_BE_LONG:     type = IS_LONG;   invert = true; break;
8305
0
        case MAY_BE_ANY - MAY_BE_DOUBLE:   type = IS_DOUBLE; invert = true; break;
8306
0
        case MAY_BE_ANY - MAY_BE_STRING:   type = IS_STRING; invert = true; break;
8307
0
        case MAY_BE_ANY - MAY_BE_ARRAY:    type = IS_ARRAY;  invert = true; break;
8308
0
        case MAY_BE_ANY - MAY_BE_OBJECT:   type = IS_OBJECT; invert = true; break;
8309
0
        case MAY_BE_ANY - MAY_BE_RESOURCE: type = IS_OBJECT; invert = true; break;
8310
0
        default:
8311
0
          type = 0;
8312
0
      }
8313
8314
0
      if (op1_info & MAY_BE_REF) {
8315
0
        ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8316
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
8317
0
        op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8318
0
      }
8319
0
      if (type == 0) {
8320
0
        ref = ir_AND_U32(ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE(jit, op1_addr)), ir_CONST_U32(mask));
8321
0
        if (!smart_branch_opcode) {
8322
0
          ref = ir_NE(ref, ir_CONST_U32(0));
8323
0
        }
8324
0
      } else if (invert) {
8325
0
        ref = ir_NE(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(type));
8326
0
      } else {
8327
0
        ref = ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(type));
8328
0
      }
8329
8330
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8331
8332
0
      if (exit_addr) {
8333
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8334
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8335
0
        } else {
8336
0
          ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
8337
0
        }
8338
0
        if (end_inputs) {
8339
0
          ir_END_list(end_inputs);
8340
0
        }
8341
0
      } else if (smart_branch_opcode) {
8342
0
        ir_ref if_val = ir_IF(ref);
8343
0
        ir_IF_TRUE(if_val);
8344
0
        ir_END_list(true_inputs);
8345
0
        ir_IF_FALSE(if_val);
8346
0
        ir_END_list(false_inputs);
8347
0
      } else {
8348
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
8349
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
8350
0
        ir_END_list(end_inputs);
8351
0
      }
8352
0
      }
8353
0
  }
8354
8355
0
  if (!smart_branch_opcode || exit_addr) {
8356
0
    if (end_inputs) {
8357
0
      ir_MERGE_list(end_inputs);
8358
0
    } else if (exit_addr && !jit->ctx.control) {
8359
0
      ir_BEGIN(IR_UNUSED); /* unreachable block */
8360
0
    }
8361
0
  } else {
8362
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
8363
0
  }
8364
8365
0
  return 1;
8366
0
}
8367
8368
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)
8369
0
{
8370
0
  zend_jit_addr res_addr = RES_ADDR();
8371
0
  uint32_t true_label = -1, false_label = -1;
8372
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
8373
8374
  // TODO: support for empty() ???
8375
0
  ZEND_ASSERT(opline->extended_value != MAY_BE_RESOURCE);
8376
8377
0
  if (smart_branch_opcode && !exit_addr) {
8378
0
    if (smart_branch_opcode == ZEND_JMPZ) {
8379
0
      true_label = target_label2;
8380
0
      false_label = target_label;
8381
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
8382
0
      true_label = target_label;
8383
0
      false_label = target_label2;
8384
0
    } else {
8385
0
      ZEND_UNREACHABLE();
8386
0
    }
8387
0
  } else {
8388
0
    res_addr = RES_ADDR();
8389
0
  }
8390
8391
0
  if (op1_info & MAY_BE_REF) {
8392
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8393
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
8394
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8395
0
  }
8396
8397
0
  if (!(op1_info & (MAY_BE_UNDEF|MAY_BE_NULL))) {
8398
0
    if (exit_addr) {
8399
0
      ZEND_ASSERT(smart_branch_opcode == ZEND_JMPZ);
8400
0
    } else if (smart_branch_opcode) {
8401
0
      ir_END_list(true_inputs);
8402
0
    } else {
8403
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8404
0
      ir_END_list(end_inputs);
8405
0
    }
8406
0
  } else if (!(op1_info & (MAY_BE_ANY - MAY_BE_NULL))) {
8407
0
    if (exit_addr) {
8408
0
      ZEND_ASSERT(smart_branch_opcode == ZEND_JMPNZ);
8409
0
    } else if (smart_branch_opcode) {
8410
0
      ir_END_list(false_inputs);
8411
0
    } else {
8412
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8413
0
      ir_END_list(end_inputs);
8414
0
    }
8415
0
  } else {
8416
0
    ir_ref ref = ir_GT(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_NULL));
8417
0
    if (exit_addr) {
8418
0
      if (smart_branch_opcode == ZEND_JMPNZ) {
8419
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
8420
0
      } else {
8421
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8422
0
      }
8423
0
    } else if (smart_branch_opcode) {
8424
0
      ir_ref if_val = ir_IF(ref);
8425
0
      ir_IF_TRUE(if_val);
8426
0
      ir_END_list(true_inputs);
8427
0
      ir_IF_FALSE(if_val);
8428
0
      ir_END_list(false_inputs);
8429
0
    } else {
8430
0
      jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
8431
0
        ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
8432
0
      ir_END_list(end_inputs);
8433
0
    }
8434
0
  }
8435
8436
0
  if (!smart_branch_opcode || exit_addr) {
8437
0
    if (end_inputs) {
8438
0
      ir_MERGE_list(end_inputs);
8439
0
    }
8440
0
  } else {
8441
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
8442
0
  }
8443
8444
0
  return 1;
8445
0
}
8446
8447
/* copy of hidden zend_closure */
8448
typedef struct _zend_closure {
8449
  zend_object       std;
8450
  zend_function     func;
8451
  zval              this_ptr;
8452
  zend_class_entry *called_scope;
8453
  zif_handler       orig_internal_handler;
8454
} zend_closure;
8455
8456
static int zend_jit_stack_check(zend_jit_ctx *jit, const zend_op *opline, uint32_t used_stack)
8457
0
{
8458
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
8459
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8460
8461
0
  if (!exit_addr) {
8462
0
    return 0;
8463
0
  }
8464
8465
  // JIT: if (EG(vm_stack_end) - EG(vm_stack_top) < used_stack)
8466
0
  ir_GUARD(
8467
0
    ir_UGE(
8468
0
      ir_SUB_A(ir_LOAD_A(jit_EG(vm_stack_end)), ir_LOAD_A(jit_EG(vm_stack_top))),
8469
0
      ir_CONST_ADDR(used_stack)),
8470
0
    ir_CONST_ADDR(exit_addr));
8471
8472
0
  return 1;
8473
0
}
8474
8475
static int zend_jit_free_trampoline(zend_jit_ctx *jit, ir_ref func)
8476
0
{
8477
  // JIT: if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
8478
0
  ir_ref if_trampoline = ir_IF(ir_AND_U32(
8479
0
    ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, common.fn_flags))),
8480
0
    ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE)));
8481
8482
0
  ir_IF_TRUE(if_trampoline);
8483
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_free_trampoline_helper), func);
8484
0
  ir_MERGE_WITH_EMPTY_FALSE(if_trampoline);
8485
8486
0
  return 1;
8487
0
}
8488
8489
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)
8490
0
{
8491
0
  uint32_t used_stack;
8492
0
  ir_ref used_stack_ref = IR_UNUSED;
8493
0
  bool stack_check = true;
8494
0
  ir_ref rx, ref, top, if_enough_stack, cold_path = IR_UNUSED;
8495
8496
0
  ZEND_ASSERT(func_ref != IR_NULL);
8497
0
  if (func) {
8498
0
    used_stack = zend_vm_calc_used_stack(opline->extended_value, func);
8499
0
    if ((int)used_stack <= checked_stack) {
8500
0
      stack_check = false;
8501
0
    }
8502
0
    used_stack_ref = ir_CONST_ADDR(used_stack);
8503
0
  } else {
8504
0
    ir_ref num_args_ref;
8505
0
    ir_ref if_internal_func = IR_UNUSED;
8506
0
    const size_t func_type_offset = is_closure ? offsetof(zend_closure, func.type) : offsetof(zend_function, type);
8507
8508
0
    used_stack = (ZEND_CALL_FRAME_SLOT + opline->extended_value + ZEND_OBSERVER_ENABLED) * sizeof(zval);
8509
0
    used_stack_ref = ir_CONST_ADDR(used_stack);
8510
0
    used_stack_ref = ir_HARD_COPY_A(used_stack_ref); /* load constant once */
8511
8512
    // JIT: if (EXPECTED(ZEND_USER_CODE(func->type))) {
8513
0
    ir_ref tmp = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, func_type_offset));
8514
0
    if_internal_func = ir_IF(ir_AND_U8(tmp, ir_CONST_U8(1)));
8515
0
    ir_IF_FALSE(if_internal_func);
8516
8517
    // JIT: used_stack += (func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args)) * sizeof(zval);
8518
0
    num_args_ref = ir_CONST_U32(opline->extended_value);
8519
0
    if (!is_closure) {
8520
0
      ref = ir_SUB_U32(
8521
0
        ir_SUB_U32(
8522
0
          ir_MIN_U32(
8523
0
            num_args_ref,
8524
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.num_args)))),
8525
0
          ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.last_var)))),
8526
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.T))));
8527
0
    } else {
8528
0
      ref = ir_SUB_U32(
8529
0
        ir_SUB_U32(
8530
0
          ir_MIN_U32(
8531
0
            num_args_ref,
8532
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.num_args)))),
8533
0
          ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.last_var)))),
8534
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.T))));
8535
0
    }
8536
0
    ref = ir_MUL_U32(ref, ir_CONST_U32(sizeof(zval)));
8537
0
    if (sizeof(void*) == 8) {
8538
0
      ref = ir_SEXT_A(ref);
8539
0
    }
8540
0
    ref = ir_SUB_A(used_stack_ref, ref);
8541
8542
0
    ir_MERGE_WITH_EMPTY_TRUE(if_internal_func);
8543
0
    used_stack_ref = ir_PHI_2(IR_ADDR, ref, used_stack_ref);
8544
0
  }
8545
8546
0
  zend_jit_start_reuse_ip(jit);
8547
8548
  // JIT: if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
8549
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EG(vm_stack_top)));
8550
8551
0
  if (stack_check) {
8552
    // JIT: Check Stack Overflow
8553
0
    ref = ir_UGE(
8554
0
      ir_SUB_A(
8555
0
        ir_LOAD_A(jit_EG(vm_stack_end)),
8556
0
        jit_IP(jit)),
8557
0
      used_stack_ref);
8558
8559
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8560
0
      bool may_be_trampoline = !func && (opline->opcode == ZEND_INIT_METHOD_CALL);
8561
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline,
8562
0
        may_be_trampoline ?
8563
0
          (ZEND_JIT_EXIT_TO_VM | ZEND_JIT_EXIT_METHOD_CALL) : ZEND_JIT_EXIT_TO_VM);
8564
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8565
8566
0
      if (!exit_addr) {
8567
0
        return 0;
8568
0
      }
8569
8570
0
      if (may_be_trampoline) {
8571
0
        jit->trace->exit_info[exit_point].poly_func.ref = func_ref;
8572
0
        jit->trace->exit_info[exit_point].poly_this.ref = this_ref;
8573
0
      }
8574
8575
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8576
0
    } else {
8577
0
      if_enough_stack = ir_IF(ref);
8578
0
      ir_IF_FALSE_cold(if_enough_stack);
8579
8580
#ifdef _WIN32
8581
      if (0) {
8582
#else
8583
0
      if (opline->opcode == ZEND_INIT_FCALL && func && func->type == ZEND_INTERNAL_FUNCTION) {
8584
0
#endif
8585
0
        jit_SET_EX_OPLINE(jit, opline);
8586
0
        ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_int_extend_stack_helper), used_stack_ref);
8587
0
      } else {
8588
0
        if (!is_closure) {
8589
0
          ref = func_ref;
8590
0
        } else {
8591
0
          ref = ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func));
8592
0
        }
8593
0
        jit_SET_EX_OPLINE(jit, opline);
8594
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_extend_stack_helper),
8595
0
          used_stack_ref, ref);
8596
0
      }
8597
0
      jit_STORE_IP(jit, ref);
8598
8599
0
      cold_path = ir_END();
8600
0
      ir_IF_TRUE(if_enough_stack);
8601
0
    }
8602
0
  }
8603
8604
0
  ref = jit_EG(vm_stack_top);
8605
0
  rx = jit_IP(jit);
8606
0
#if !OPTIMIZE_FOR_SIZE
8607
  /* JIT: EG(vm_stack_top) = (zval*)((char*)call + used_stack);
8608
   * This vesions is longer but faster
8609
   *    mov EG(vm_stack_top), %CALL
8610
   *    lea size(%call), %tmp
8611
   *    mov %tmp, EG(vm_stack_top)
8612
   */
8613
0
  top = rx;
8614
#else
8615
  /* JIT: EG(vm_stack_top) += used_stack;
8616
   * Use ir_emit() because ir_LOAD() makes load forwarding and doesn't allow load/store fusion
8617
   *    mov EG(vm_stack_top), %CALL
8618
   *    add $size, EG(vm_stack_top)
8619
   */
8620
  top = jit->ctx.control = ir_emit2(&jit->ctx, IR_OPT(IR_LOAD, IR_ADDR), jit->ctx.control, ref);
8621
#endif
8622
0
  ir_STORE(ref, ir_ADD_A(top, used_stack_ref));
8623
8624
  // JIT: zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
8625
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE || opline->opcode != ZEND_INIT_METHOD_CALL) {
8626
    // JIT: ZEND_SET_CALL_INFO(call, 0, call_info);
8627
0
    ir_STORE(jit_CALL(rx, This.u1.type_info), ir_CONST_U32(IS_UNDEF | ZEND_CALL_NESTED_FUNCTION));
8628
0
  }
8629
#ifdef _WIN32
8630
  if (0) {
8631
#else
8632
0
  if (opline->opcode == ZEND_INIT_FCALL && func && func->type == ZEND_INTERNAL_FUNCTION) {
8633
0
#endif
8634
0
    if (cold_path) {
8635
0
      ir_MERGE_WITH(cold_path);
8636
0
      rx = jit_IP(jit);
8637
0
    }
8638
8639
    // JIT: call->func = func;
8640
0
    ir_STORE(jit_CALL(rx, func), func_ref);
8641
0
  } else {
8642
0
    if (!is_closure) {
8643
      // JIT: call->func = func;
8644
0
      ir_STORE(jit_CALL(rx, func), func_ref);
8645
0
    } else {
8646
      // JIT: call->func = &closure->func;
8647
0
      ir_STORE(jit_CALL(rx, func), ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func)));
8648
0
    }
8649
0
    if (cold_path) {
8650
0
      ir_MERGE_WITH(cold_path);
8651
0
      rx = jit_IP(jit);
8652
0
    }
8653
0
  }
8654
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL) {
8655
    // JIT: Z_PTR(call->This) = obj;
8656
0
    ZEND_ASSERT(this_ref != IR_NULL);
8657
0
    ir_STORE(jit_CALL(rx, This.value.ptr), this_ref);
8658
0
      if (opline->op1_type == IS_UNUSED || delayed_fetch_this) {
8659
      // JIT: call->call_info |= ZEND_CALL_HAS_THIS;
8660
0
      ref = jit_CALL(rx, This.u1.type_info);
8661
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8662
0
        ir_STORE(ref, ir_CONST_U32( ZEND_CALL_HAS_THIS));
8663
0
      } else {
8664
0
        ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_HAS_THIS)));
8665
0
      }
8666
0
      } else {
8667
0
      if (opline->op1_type == IS_CV) {
8668
        // JIT: GC_ADDREF(obj);
8669
0
        jit_GC_ADDREF(jit, this_ref);
8670
0
      }
8671
8672
      // JIT: call->call_info |= ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS;
8673
0
      ref = jit_CALL(rx, This.u1.type_info);
8674
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8675
0
        ir_STORE(ref, ir_CONST_U32( ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS));
8676
0
      } else {
8677
0
        ir_STORE(ref,
8678
0
          ir_OR_U32(ir_LOAD_U32(ref),
8679
0
            ir_CONST_U32(ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS)));
8680
0
      }
8681
0
      }
8682
0
  } else if (opline->opcode == ZEND_INIT_STATIC_METHOD_CALL) {
8683
    // JIT: Z_CE(call->This) = called_scope;
8684
0
    ir_STORE(jit_CALL(rx, This), this_ref);
8685
0
  } else if (!is_closure) {
8686
    // JIT: Z_CE(call->This) = called_scope;
8687
0
    ir_STORE(jit_CALL(rx, This), IR_NULL);
8688
0
  } else {
8689
0
    ir_ref object_or_called_scope, call_info, call_info2, object, if_cond;
8690
0
    ir_ref if_cond_user = IR_UNUSED;
8691
8692
0
    if (opline->op2_type == IS_CV) {
8693
      // JIT: GC_ADDREF(closure);
8694
0
      jit_GC_ADDREF(jit, func_ref);
8695
0
    }
8696
8697
    // JIT: RX(object_or_called_scope) = closure->called_scope;
8698
0
    object_or_called_scope = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, called_scope)));
8699
8700
    // JIT: call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE |
8701
    //      (closure->func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE);
8702
0
    call_info = ir_OR_U32(
8703
0
      ir_AND_U32(
8704
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.common.fn_flags))),
8705
0
        ir_CONST_U32(ZEND_ACC_FAKE_CLOSURE)),
8706
0
      ir_CONST_U32(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE));
8707
    // JIT: if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
8708
0
    if_cond = ir_IF(ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, this_ptr.u1.v.type))));
8709
0
    ir_IF_TRUE(if_cond);
8710
8711
    // JIT: call_info |= ZEND_CALL_HAS_THIS;
8712
0
    call_info2 = ir_OR_U32(call_info, ir_CONST_U32(ZEND_CALL_HAS_THIS));
8713
8714
    // JIT: object_or_called_scope = Z_OBJ(closure->this_ptr);
8715
0
    object = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, this_ptr.value.ptr)));
8716
8717
0
    ir_MERGE_WITH_EMPTY_FALSE(if_cond);
8718
0
    call_info = ir_PHI_2(IR_U32, call_info2, call_info);
8719
0
    object_or_called_scope = ir_PHI_2(IR_ADDR, object, object_or_called_scope);
8720
8721
    // JIT: ZEND_SET_CALL_INFO(call, 0, call_info);
8722
0
    ref = jit_CALL(rx, This.u1.type_info);
8723
0
    ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), call_info));
8724
8725
    // JIT: Z_PTR(call->This) = object_or_called_scope;
8726
0
    ir_STORE(jit_CALL(rx, This.value.ptr), object_or_called_scope);
8727
8728
0
    if (!func) {
8729
      // JIT: if (closure->func.common.type & ZEND_USER_FUNCTION)
8730
0
      ir_ref type = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.type)));
8731
0
      if_cond_user = ir_IF(ir_AND_U8(type, ir_CONST_U8(ZEND_USER_FUNCTION)));
8732
0
      ir_IF_TRUE(if_cond_user);
8733
0
    }
8734
8735
0
    if (!func || func->common.type == ZEND_USER_FUNCTION) {
8736
      // JIT: zend_jit_init_func_run_time_cache_helper(closure->func);
8737
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_init_func_run_time_cache_helper),
8738
0
        ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func)));
8739
0
    }
8740
8741
0
    if (!func) {
8742
0
      ir_MERGE_WITH_EMPTY_FALSE(if_cond_user);
8743
0
    }
8744
0
  }
8745
8746
  // JIT: ZEND_CALL_NUM_ARGS(call) = num_args;
8747
0
  ir_STORE(jit_CALL(rx, This.u2.num_args), ir_CONST_U32(opline->extended_value));
8748
8749
0
  return 1;
8750
0
}
8751
8752
static int zend_jit_func_guard(zend_jit_ctx *jit, ir_ref func_ref, const zend_function *func, const void *exit_addr)
8753
0
{
8754
0
  if (func->type == ZEND_USER_FUNCTION &&
8755
0
      (!(func->common.fn_flags & ZEND_ACC_IMMUTABLE) ||
8756
0
       (func->common.fn_flags & ZEND_ACC_CLOSURE) ||
8757
0
       !func->common.function_name)) {
8758
0
    const zend_op *opcodes = func->op_array.opcodes;
8759
8760
    // JIT: if (call->func.op_array.opcodes != opcodes) goto exit_addr;
8761
0
    ir_GUARD(
8762
0
      ir_EQ(
8763
0
        ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes))),
8764
0
        ir_CONST_ADDR(opcodes)),
8765
0
      ir_CONST_ADDR(exit_addr));
8766
#ifdef ZEND_WIN32
8767
  } else if (func->type == ZEND_INTERNAL_FUNCTION) {
8768
    // ASLR may cause different addresses in different workers. Check for the internal function handler.
8769
    // JIT: if (call->func.internal_function.handler != handler) goto exit_addr;
8770
    ir_GUARD(
8771
      ir_EQ(
8772
        ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_internal_function, handler))),
8773
        ir_CONST_FC_FUNC(func->internal_function.handler)),
8774
      ir_CONST_ADDR(exit_addr));
8775
#endif
8776
0
  } else {
8777
    // JIT: if (call->func != func) goto exit_addr;
8778
0
    ir_GUARD(ir_EQ(func_ref, ir_CONST_ADDR(func)), ir_CONST_ADDR(exit_addr));
8779
0
  }
8780
8781
0
  return 1;
8782
0
}
8783
8784
static int zend_jit_init_fcall_guard(zend_jit_ctx *jit, uint32_t level, const zend_function *func, const zend_op *to_opline)
8785
0
{
8786
0
  int32_t exit_point;
8787
0
  const void *exit_addr;
8788
0
  ir_ref call;
8789
8790
0
  if (func->type == ZEND_USER_FUNCTION
8791
0
   && !zend_accel_in_shm(func->op_array.opcodes)) {
8792
    /* op_array and op_array->opcodes are not persistent. We can't link. */
8793
0
    return 0;
8794
0
  }
8795
8796
0
  exit_point = zend_jit_trace_get_exit_point(to_opline, ZEND_JIT_EXIT_POLYMORPHISM);
8797
0
  exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8798
0
  if (!exit_addr) {
8799
0
    return 0;
8800
0
  }
8801
8802
  // call = EX(call);
8803
0
  call = ir_LOAD_A(jit_EX(call));
8804
0
  while (level > 0) {
8805
    // call = call->prev_execute_data
8806
0
    call = ir_LOAD_A(jit_CALL(call, prev_execute_data));
8807
0
    level--;
8808
0
  }
8809
8810
0
  return zend_jit_func_guard(jit, ir_LOAD_A(jit_CALL(call, func)), func, exit_addr);
8811
0
}
8812
8813
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)
8814
0
{
8815
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
8816
0
  zend_call_info *call_info = NULL;
8817
0
  zend_function *func = NULL;
8818
0
  ir_ref func_ref = IR_UNUSED;
8819
8820
0
  if (jit->delayed_call_level) {
8821
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
8822
0
      return 0;
8823
0
    }
8824
0
  }
8825
8826
0
  if (info) {
8827
0
    call_info = info->callee_info;
8828
0
    while (call_info && call_info->caller_init_opline != opline) {
8829
0
      call_info = call_info->next_callee;
8830
0
    }
8831
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
8832
0
      func = call_info->callee_func;
8833
0
    }
8834
0
  }
8835
8836
0
  if (!func
8837
0
   && trace
8838
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL) {
8839
#ifdef _WIN32
8840
    /* ASLR */
8841
    if (trace->func->type != ZEND_INTERNAL_FUNCTION) {
8842
      func = (zend_function*)trace->func;
8843
    }
8844
#else
8845
0
    func = (zend_function*)trace->func;
8846
0
#endif
8847
0
  }
8848
8849
#ifdef _WIN32
8850
  if (0) {
8851
#else
8852
0
  if (opline->opcode == ZEND_INIT_FCALL
8853
0
   && func
8854
0
   && func->type == ZEND_INTERNAL_FUNCTION) {
8855
0
#endif
8856
    /* load constant address later */
8857
0
    func_ref = ir_CONST_ADDR(func);
8858
0
  } else if (func && op_array == &func->op_array) {
8859
    /* recursive call */
8860
0
    if (!(func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)
8861
0
     || zend_jit_prefer_const_addr_load(jit, (uintptr_t)func)) {
8862
0
      func_ref = ir_LOAD_A(jit_EX(func));
8863
0
    } else {
8864
0
      func_ref = ir_CONST_ADDR(func);
8865
0
    }
8866
0
  } else {
8867
0
    ir_ref if_func, cache_slot_ref, ref;
8868
8869
    // JIT: if (CACHED_PTR(opline->result.num))
8870
0
    cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num);
8871
0
    func_ref = ir_LOAD_A(cache_slot_ref);
8872
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
8873
0
     && func
8874
0
     && (func->common.fn_flags & ZEND_ACC_IMMUTABLE)
8875
0
     && opline->opcode != ZEND_INIT_FCALL) {
8876
      /* Called func may be changed because of recompilation. See ext/opcache/tests/jit/init_fcall_003.phpt */
8877
0
      if_func = ir_IF(ir_EQ(func_ref, ir_CONST_ADDR(func)));
8878
0
    } else {
8879
0
      if_func = ir_IF(func_ref);
8880
0
    }
8881
0
    ir_IF_FALSE_cold(if_func);
8882
0
    if (opline->opcode == ZEND_INIT_FCALL
8883
0
     && func
8884
0
     && func->type == ZEND_USER_FUNCTION
8885
0
     && (func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)) {
8886
0
      ref = ir_HARD_COPY_A(ir_CONST_ADDR(func)); /* load constant once */
8887
0
        ir_STORE(cache_slot_ref, ref);
8888
0
      ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_init_func_run_time_cache_helper), ref);
8889
0
    } else {
8890
0
      zval *zv = RT_CONSTANT(opline, opline->op2);
8891
8892
0
      if (opline->opcode == ZEND_INIT_FCALL) {
8893
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_func_helper),
8894
0
          ir_CONST_ADDR(Z_STR_P(zv)),
8895
0
          cache_slot_ref);
8896
0
      } else if (opline->opcode == ZEND_INIT_FCALL_BY_NAME) {
8897
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_func_helper),
8898
0
          ir_CONST_ADDR(Z_STR_P(zv + 1)),
8899
0
          cache_slot_ref);
8900
0
      } else if (opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
8901
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_ns_func_helper),
8902
0
          ir_CONST_ADDR(zv),
8903
0
          cache_slot_ref);
8904
0
      } else {
8905
0
        ZEND_UNREACHABLE();
8906
0
      }
8907
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8908
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline,
8909
0
          func && (func->common.fn_flags & ZEND_ACC_IMMUTABLE) ? ZEND_JIT_EXIT_INVALIDATE : 0);
8910
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8911
8912
0
        if (!exit_addr) {
8913
0
          return 0;
8914
0
        }
8915
0
        if (!func || opline->opcode == ZEND_INIT_FCALL) {
8916
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8917
0
        } else if (!zend_jit_func_guard(jit, ref, func, exit_addr)) {
8918
0
          return 0;
8919
0
        }
8920
0
      } else {
8921
0
jit_SET_EX_OPLINE(jit, opline);
8922
0
        ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_undefined_function));
8923
0
      }
8924
0
    }
8925
0
    ir_MERGE_WITH_EMPTY_TRUE(if_func);
8926
0
    func_ref = ir_PHI_2(IR_ADDR, ref, func_ref);
8927
0
  }
8928
8929
0
  if (!zend_jit_push_call_frame(jit, opline, op_array, func, false, false, checked_stack, func_ref, IR_UNUSED)) {
8930
0
    return 0;
8931
0
  }
8932
8933
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
8934
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
8935
0
      return 0;
8936
0
    }
8937
0
  } else {
8938
0
    ZEND_ASSERT(call_level > 0);
8939
0
    jit->delayed_call_level = call_level;
8940
0
    delayed_call_chain = true;
8941
0
  }
8942
8943
0
  if (trace
8944
0
   && trace->op == ZEND_JIT_TRACE_END
8945
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
8946
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
8947
0
      return 0;
8948
0
    }
8949
0
  }
8950
8951
0
  return 1;
8952
0
}
8953
8954
static int zend_jit_init_method_call(zend_jit_ctx         *jit,
8955
                                     const zend_op        *opline,
8956
                                     uint32_t              b,
8957
                                     const zend_op_array  *op_array,
8958
                                     zend_ssa             *ssa,
8959
                                     const zend_ssa_op    *ssa_op,
8960
                                     int                   call_level,
8961
                                     uint32_t              op1_info,
8962
                                     zend_jit_addr         op1_addr,
8963
                                     zend_class_entry     *ce,
8964
                                     bool                  ce_is_instanceof,
8965
                                     bool                  on_this,
8966
                                     bool                  delayed_fetch_this,
8967
                                     zend_class_entry     *trace_ce,
8968
                                     zend_jit_trace_rec   *trace,
8969
                                     int                   checked_stack,
8970
                                     ir_ref                func_ref,
8971
                                     ir_ref                this_ref,
8972
                                     bool                  polymorphic_side_trace)
8973
0
{
8974
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
8975
0
  zend_call_info *call_info = NULL;
8976
0
  zend_function *func = NULL;
8977
0
  zval *function_name;
8978
0
  ir_ref if_static = IR_UNUSED, cold_path;
8979
8980
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
8981
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
8982
8983
0
  function_name = RT_CONSTANT(opline, opline->op2);
8984
8985
0
  if (info) {
8986
0
    call_info = info->callee_info;
8987
0
    while (call_info && call_info->caller_init_opline != opline) {
8988
0
      call_info = call_info->next_callee;
8989
0
    }
8990
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
8991
0
      func = call_info->callee_func;
8992
0
    }
8993
0
  }
8994
8995
0
  if (polymorphic_side_trace) {
8996
    /* function is passed from parent snapshot */
8997
0
    ZEND_ASSERT(func_ref != IR_UNUSED && this_ref != IR_UNUSED);
8998
0
  } else {
8999
0
    ir_ref ref, ref2, if_found, fast_path, run_time_cache, this_ref2;
9000
9001
0
    if (on_this) {
9002
0
      zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
9003
0
      this_ref = jit_Z_PTR(jit, this_addr);
9004
0
    } else {
9005
0
        if (op1_info & MAY_BE_REF) {
9006
0
        if (opline->op1_type == IS_CV) {
9007
          // JIT: ZVAL_DEREF(op1)
9008
0
          ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
9009
0
          ref = jit_ZVAL_DEREF_ref(jit, ref);
9010
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
9011
0
        } else {
9012
0
          ir_ref if_ref;
9013
9014
          /* Hack: Convert reference to regular value to simplify JIT code */
9015
0
          ZEND_ASSERT(Z_REG(op1_addr) == ZREG_FP);
9016
9017
0
          if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9018
0
          ir_IF_TRUE(if_ref);
9019
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_unref_helper), jit_ZVAL_ADDR(jit, op1_addr));
9020
9021
0
          ir_MERGE_WITH_EMPTY_FALSE(if_ref);
9022
0
        }
9023
0
      }
9024
0
      if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
9025
0
        if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9026
0
          int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9027
0
          const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9028
9029
0
          if (!exit_addr) {
9030
0
            return 0;
9031
0
          }
9032
0
          ir_GUARD(ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_OBJECT)),
9033
0
            ir_CONST_ADDR(exit_addr));
9034
0
        } else {
9035
0
          ir_ref if_object = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
9036
9037
0
          ir_IF_FALSE_cold(if_object);
9038
9039
0
          jit_SET_EX_OPLINE(jit, opline);
9040
0
          if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9041
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_method_call_tmp),
9042
0
              jit_ZVAL_ADDR(jit, op1_addr));
9043
0
          } else {
9044
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_method_call),
9045
0
              jit_ZVAL_ADDR(jit, op1_addr));
9046
0
          }
9047
0
          ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
9048
0
          ir_IF_TRUE(if_object);
9049
0
        }
9050
0
      }
9051
9052
0
      this_ref = jit_Z_PTR(jit, op1_addr);
9053
0
    }
9054
9055
0
    if (jit->delayed_call_level) {
9056
0
      if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9057
0
        return 0;
9058
0
      }
9059
0
    }
9060
9061
0
    if (func) {
9062
      // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9063
0
      ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num + sizeof(void*)));
9064
9065
0
      if_found = ir_IF(ref);
9066
0
      ir_IF_TRUE(if_found);
9067
0
      fast_path = ir_END();
9068
0
    } else {
9069
      // JIT: if (CACHED_PTR(opline->result.num) == obj->ce)) {
9070
0
      run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
9071
0
      ref = ir_EQ(
9072
0
        ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->result.num)),
9073
0
        ir_LOAD_A(ir_ADD_OFFSET(this_ref, offsetof(zend_object, ce))));
9074
0
      if_found = ir_IF(ref);
9075
0
      ir_IF_TRUE(if_found);
9076
9077
      // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9078
0
      ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->result.num + sizeof(void*)));
9079
0
      fast_path = ir_END();
9080
9081
0
    }
9082
9083
0
    ir_IF_FALSE_cold(if_found);
9084
0
    jit_SET_EX_OPLINE(jit, opline);
9085
9086
0
    if (!jit->ctx.fixed_call_stack_size) {
9087
      // JIT: alloca(sizeof(void*));
9088
0
      this_ref2 = ir_ALLOCA(ir_CONST_ADDR(0x10));
9089
0
    } else {
9090
#ifdef _WIN64
9091
      this_ref2 = ir_HARD_COPY_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
9092
#else
9093
0
      this_ref2 = ir_HARD_COPY_A(ir_RLOAD_A(IR_REG_SP));
9094
0
#endif
9095
0
    }
9096
0
    ir_STORE(this_ref2, this_ref);
9097
9098
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9099
0
      ref2 = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_method_tmp_helper),
9100
0
          this_ref,
9101
0
          ir_CONST_ADDR(function_name),
9102
0
          this_ref2);
9103
0
    } else {
9104
0
      ref2 = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_method_helper),
9105
0
          this_ref,
9106
0
          ir_CONST_ADDR(function_name),
9107
0
          this_ref2);
9108
0
    }
9109
9110
9111
0
    if (!jit->ctx.fixed_call_stack_size) {
9112
0
      this_ref2 = ir_LOAD_A(ir_RLOAD_A(IR_REG_SP));
9113
      // JIT: revert alloca
9114
0
      ir_AFREE(ir_CONST_ADDR(0x10));
9115
0
    } else {
9116
#ifdef _WIN64
9117
      this_ref2 = ir_LOAD_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
9118
#else
9119
0
      this_ref2 = ir_LOAD_A(ir_RLOAD_A(IR_REG_SP));
9120
0
#endif
9121
0
    }
9122
9123
0
    ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9124
9125
0
    ir_MERGE_WITH(fast_path);
9126
0
    func_ref = ir_PHI_2(IR_ADDR, ref2, ref);
9127
0
    this_ref = ir_PHI_2(IR_ADDR, this_ref2, this_ref);
9128
0
  }
9129
9130
0
  if ((!func || zend_jit_may_be_modified(func, op_array))
9131
0
   && trace
9132
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9133
0
   && trace->func) {
9134
0
    int32_t exit_point;
9135
0
    const void *exit_addr;
9136
9137
0
    exit_point = zend_jit_trace_get_exit_point(opline, func ? ZEND_JIT_EXIT_INVALIDATE : ZEND_JIT_EXIT_METHOD_CALL);
9138
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9139
0
    if (!exit_addr) {
9140
0
      return 0;
9141
0
    }
9142
9143
0
    jit->trace->exit_info[exit_point].poly_func.ref = func_ref;
9144
0
    jit->trace->exit_info[exit_point].poly_this.ref = this_ref;
9145
9146
0
    func = (zend_function*)trace->func;
9147
9148
0
    if (!zend_jit_func_guard(jit, func_ref, func, exit_addr)) {
9149
0
      return 0;
9150
0
    }
9151
0
  }
9152
9153
0
  if (!func) {
9154
    // JIT: if (fbc->common.fn_flags & ZEND_ACC_STATIC) {
9155
0
    if_static = ir_IF(ir_AND_U32(
9156
0
      ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
9157
0
      ir_CONST_U32(ZEND_ACC_STATIC)));
9158
0
    ir_IF_TRUE_cold(if_static);
9159
0
  }
9160
9161
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC) != 0) {
9162
0
    ir_ref ret;
9163
9164
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9165
0
      ret = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_static_method_call_frame_tmp),
9166
0
          this_ref,
9167
0
          func_ref,
9168
0
          ir_CONST_U32(opline->extended_value));
9169
0
    } else {
9170
0
      ret = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_static_method_call_frame),
9171
0
          this_ref,
9172
0
          func_ref,
9173
0
          ir_CONST_U32(opline->extended_value));
9174
0
    }
9175
9176
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR) && !delayed_fetch_this)) {
9177
0
      ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9178
0
    }
9179
0
    jit_STORE_IP(jit, ret);
9180
0
  }
9181
9182
0
  if (!func) {
9183
0
    cold_path = ir_END();
9184
0
    ir_IF_FALSE(if_static);
9185
0
  }
9186
9187
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC) == 0) {
9188
0
    if (!zend_jit_push_call_frame(jit, opline, NULL, func, false, delayed_fetch_this, checked_stack, func_ref, this_ref)) {
9189
0
      return 0;
9190
0
    }
9191
0
  }
9192
9193
0
  if (!func) {
9194
0
    ir_MERGE_WITH(cold_path);
9195
0
  }
9196
0
  zend_jit_start_reuse_ip(jit);
9197
9198
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9199
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9200
0
      return 0;
9201
0
    }
9202
0
  } else {
9203
0
    ZEND_ASSERT(call_level > 0);
9204
0
    delayed_call_chain = true;
9205
0
    jit->delayed_call_level = call_level;
9206
0
  }
9207
9208
0
  if (trace
9209
0
   && trace->op == ZEND_JIT_TRACE_END
9210
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9211
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9212
0
      return 0;
9213
0
    }
9214
0
  }
9215
9216
0
  return 1;
9217
0
}
9218
9219
static int zend_jit_init_static_method_call(zend_jit_ctx         *jit,
9220
                                            const zend_op        *opline,
9221
                                            uint32_t              b,
9222
                                            const zend_op_array  *op_array,
9223
                                            zend_ssa             *ssa,
9224
                                            const zend_ssa_op    *ssa_op,
9225
                                            int                   call_level,
9226
                                            zend_jit_trace_rec   *trace,
9227
                                            int                   checked_stack)
9228
0
{
9229
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
9230
0
  zend_call_info *call_info = NULL;
9231
0
  zend_class_entry *ce;
9232
0
  zend_function *func = NULL;
9233
0
  ir_ref func_ref, func_ref2, scope_ref, scope_ref2, if_cached, cold_path, ref;
9234
0
  ir_ref if_static = IR_UNUSED;
9235
9236
0
  if (info) {
9237
0
    call_info = info->callee_info;
9238
0
    while (call_info && call_info->caller_init_opline != opline) {
9239
0
      call_info = call_info->next_callee;
9240
0
    }
9241
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
9242
0
      func = call_info->callee_func;
9243
0
    }
9244
0
  }
9245
9246
0
  ce = zend_get_known_class(op_array, opline, opline->op1_type, opline->op1);
9247
0
  if (!func && ce && (opline->op1_type == IS_CONST || !(ce->ce_flags & ZEND_ACC_TRAIT))) {
9248
0
    zval *zv = RT_CONSTANT(opline, opline->op2);
9249
0
    zend_string *method_name;
9250
9251
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
9252
0
    method_name = Z_STR_P(zv);
9253
0
    zv = zend_hash_find(&ce->function_table, method_name);
9254
0
    if (zv) {
9255
0
      zend_function *fn = Z_PTR_P(zv);
9256
9257
0
      if (fn->common.scope == op_array->scope
9258
0
       || (fn->common.fn_flags & ZEND_ACC_PUBLIC)
9259
0
       || ((fn->common.fn_flags & ZEND_ACC_PROTECTED)
9260
0
        && op_array->scope
9261
0
        && instanceof_function_slow(op_array->scope, fn->common.scope))) {
9262
0
        func = fn;
9263
0
      }
9264
0
    }
9265
0
  }
9266
9267
0
  if (jit->delayed_call_level) {
9268
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9269
0
      return 0;
9270
0
    }
9271
0
  }
9272
9273
  // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9274
0
  func_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num + sizeof(void*)));
9275
9276
  // JIT: if (fbc)
9277
0
  if_cached = ir_IF(func_ref);
9278
0
  ir_IF_FALSE_cold(if_cached);
9279
9280
0
  jit_SET_EX_OPLINE(jit, opline);
9281
0
  scope_ref2 = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_class_helper), jit_FP(jit));
9282
0
  ir_GUARD(scope_ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9283
9284
0
  func_ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_static_method_helper), jit_FP(jit), scope_ref2);
9285
0
  ir_GUARD(func_ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9286
9287
0
  cold_path = ir_END();
9288
9289
0
  ir_IF_TRUE(if_cached);
9290
0
  if (ce && (ce->ce_flags & ZEND_ACC_IMMUTABLE) && (ce->ce_flags & ZEND_ACC_LINKED)) {
9291
0
    scope_ref = ir_CONST_ADDR(ce);
9292
0
  } else {
9293
0
    scope_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num));
9294
0
  }
9295
9296
0
  ir_MERGE_2(cold_path, ir_END());
9297
0
  func_ref = ir_PHI_2(IR_ADDR, func_ref2, func_ref);
9298
0
  scope_ref = ir_PHI_2(IR_ADDR, scope_ref2, scope_ref);
9299
9300
0
  if ((!func || zend_jit_may_be_modified(func, op_array))
9301
0
   && trace
9302
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9303
0
   && trace->func) {
9304
0
    int32_t exit_point;
9305
0
    const void *exit_addr;
9306
9307
0
    exit_point = zend_jit_trace_get_exit_point(opline, func ? ZEND_JIT_EXIT_INVALIDATE : 0);
9308
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9309
0
    if (!exit_addr) {
9310
0
      return 0;
9311
0
    }
9312
9313
//    jit->trace->exit_info[exit_point].poly_func_ref = func_ref;
9314
//    jit->trace->exit_info[exit_point].poly_this_ref = scope_ref;
9315
9316
0
    func = (zend_function*)trace->func;
9317
9318
0
    if (!zend_jit_func_guard(jit, func_ref, func, exit_addr)) {
9319
0
      return 0;
9320
0
    }
9321
0
  }
9322
9323
0
  if (!func || !(func->common.fn_flags & ZEND_ACC_STATIC)) {
9324
0
    if (!func) {
9325
      // JIT: if (fbc->common.fn_flags & ZEND_ACC_STATIC) {
9326
0
      if_static = ir_IF(ir_AND_U32(
9327
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
9328
0
        ir_CONST_U32(ZEND_ACC_STATIC)));
9329
0
      ir_IF_FALSE_cold(if_static);
9330
0
    }
9331
9332
0
    jit_SET_EX_OPLINE(jit, opline);
9333
0
    ref = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_this_method_call_frame),
9334
0
        scope_ref,
9335
0
        func_ref,
9336
0
        ir_CONST_U32(opline->extended_value));
9337
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9338
0
    jit_STORE_IP(jit, ref);
9339
9340
0
    if (!func) {
9341
0
      cold_path = ir_END();
9342
0
      ir_IF_TRUE(if_static);
9343
0
    }
9344
0
  }
9345
9346
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC)) {
9347
0
    if (opline->op1_type == IS_UNUSED
9348
0
     && ((opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT ||
9349
0
         (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF)) {
9350
0
      if (op_array->fn_flags & ZEND_ACC_STATIC) {
9351
0
        scope_ref = ir_LOAD_A(jit_EX(This.value.ref));
9352
0
      } else if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
9353
0
        ir_ref if_object, values = IR_UNUSED;
9354
9355
0
        if_object = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, jit_EX(This)), ir_CONST_U8(IS_OBJECT)));
9356
0
        ir_IF_TRUE(if_object);
9357
0
        ir_END_PHI_list(values,
9358
0
          ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(This.value.ref)), offsetof(zend_object, ce))));
9359
0
        ir_IF_FALSE(if_object);
9360
0
        ir_END_PHI_list(values, ir_LOAD_A(jit_EX(This.value.ref)));
9361
0
        ir_PHI_list(values);
9362
0
      } else {
9363
0
        scope_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(This.value.ref)), offsetof(zend_object, ce)));
9364
0
      }
9365
0
    }
9366
0
    if (!zend_jit_push_call_frame(jit, opline, op_array, func, false, false, checked_stack, func_ref, scope_ref)) {
9367
0
      return 0;
9368
0
    }
9369
9370
0
    if (!func) {
9371
0
      ir_MERGE_2(cold_path, ir_END());
9372
0
    }
9373
0
  }
9374
9375
0
  zend_jit_start_reuse_ip(jit);
9376
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9377
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9378
0
      return 0;
9379
0
    }
9380
0
  } else {
9381
0
    ZEND_ASSERT(call_level > 0);
9382
0
    jit->delayed_call_level = call_level;
9383
0
    delayed_call_chain = true;
9384
0
  }
9385
9386
0
  if (trace
9387
0
   && trace->op == ZEND_JIT_TRACE_END
9388
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9389
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9390
0
      return 0;
9391
0
    }
9392
0
  }
9393
9394
0
  return 1;
9395
0
}
9396
9397
static int zend_jit_init_closure_call(zend_jit_ctx         *jit,
9398
                                      const zend_op        *opline,
9399
                                      uint32_t              b,
9400
                                      const zend_op_array  *op_array,
9401
                                      zend_ssa             *ssa,
9402
                                      const zend_ssa_op    *ssa_op,
9403
                                      int                   call_level,
9404
                                      zend_jit_trace_rec   *trace,
9405
                                      int                   checked_stack)
9406
0
{
9407
0
  zend_function *func = NULL;
9408
0
  zend_jit_addr op2_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
9409
0
  ir_ref ref;
9410
9411
0
  ref = jit_Z_PTR(jit, op2_addr);
9412
9413
0
  if (ssa->var_info[ssa_op->op2_use].ce != zend_ce_closure
9414
0
   && !(ssa->var_info[ssa_op->op2_use].type & MAY_BE_CLASS_GUARD)) {
9415
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9416
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9417
9418
0
    if (!exit_addr) {
9419
0
      return 0;
9420
0
    }
9421
9422
0
    ir_GUARD(
9423
0
      ir_EQ(
9424
0
        ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_object, ce))),
9425
0
        ir_CONST_ADDR(zend_ce_closure)),
9426
0
      ir_CONST_ADDR(exit_addr));
9427
9428
0
    if (ssa->var_info && ssa_op->op2_use >= 0) {
9429
0
      ssa->var_info[ssa_op->op2_use].type |= MAY_BE_CLASS_GUARD;
9430
0
      ssa->var_info[ssa_op->op2_use].ce = zend_ce_closure;
9431
0
      ssa->var_info[ssa_op->op2_use].is_instanceof = 0;
9432
0
    }
9433
0
  }
9434
9435
0
  if (trace
9436
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9437
0
   && trace->func
9438
0
   && trace->func->type == ZEND_USER_FUNCTION) {
9439
0
    const zend_op *opcodes;
9440
0
    int32_t exit_point;
9441
0
    const void *exit_addr;
9442
9443
0
    func = (zend_function*)trace->func;
9444
0
    opcodes = func->op_array.opcodes;
9445
0
    exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_CLOSURE_CALL);
9446
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9447
0
    if (!exit_addr) {
9448
0
      return 0;
9449
0
    }
9450
9451
0
    ir_GUARD(
9452
0
      ir_EQ(
9453
0
        ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_closure, func.op_array.opcodes))),
9454
0
        ir_CONST_ADDR(opcodes)),
9455
0
      ir_CONST_ADDR(exit_addr));
9456
0
  }
9457
9458
0
  if (jit->delayed_call_level) {
9459
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9460
0
      return 0;
9461
0
    }
9462
0
  }
9463
9464
0
  if (!zend_jit_push_call_frame(jit, opline, NULL, func, true, false, checked_stack, ref, IR_UNUSED)) {
9465
0
    return 0;
9466
0
  }
9467
9468
0
  if (zend_jit_needs_call_chain(NULL, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9469
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9470
0
      return 0;
9471
0
    }
9472
0
  } else {
9473
0
    ZEND_ASSERT(call_level > 0);
9474
0
    delayed_call_chain = true;
9475
0
    jit->delayed_call_level = call_level;
9476
0
  }
9477
9478
0
  if (trace
9479
0
   && trace->op == ZEND_JIT_TRACE_END
9480
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9481
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9482
0
      return 0;
9483
0
    }
9484
0
  }
9485
9486
0
  return 1;
9487
0
}
9488
9489
static int zend_jit_send_val(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr)
9490
0
{
9491
0
  uint32_t arg_num = opline->op2.num;
9492
0
  zend_jit_addr arg_addr;
9493
9494
0
  ZEND_ASSERT(opline->opcode == ZEND_SEND_VAL || arg_num <= MAX_ARG_FLAG_NUM);
9495
9496
0
  if (!zend_jit_reuse_ip(jit)) {
9497
0
    return 0;
9498
0
  }
9499
9500
0
  if (opline->opcode == ZEND_SEND_VAL_EX) {
9501
0
    uint32_t mask = ZEND_SEND_BY_REF << ((arg_num + 3) * 2);
9502
9503
0
    ZEND_ASSERT(arg_num <= MAX_ARG_FLAG_NUM);
9504
9505
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9506
0
     && JIT_G(current_frame)
9507
0
     && JIT_G(current_frame)->call
9508
0
     && JIT_G(current_frame)->call->func) {
9509
0
      if (ARG_MUST_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9510
        /* Don't generate code that always throws exception */
9511
0
        return 0;
9512
0
      }
9513
0
    } else {
9514
0
      ir_ref cond = ir_AND_U32(
9515
0
        ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_RX(func)), offsetof(zend_function, quick_arg_flags))),
9516
0
        ir_CONST_U32(mask));
9517
9518
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9519
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9520
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9521
0
        if (!exit_addr) {
9522
0
          return 0;
9523
0
        }
9524
0
        ir_GUARD_NOT(cond, ir_CONST_ADDR(exit_addr));
9525
0
      } else {
9526
0
        ir_ref if_pass_by_ref;
9527
9528
0
        if_pass_by_ref = ir_IF(cond);
9529
9530
0
        ir_IF_TRUE_cold(if_pass_by_ref);
9531
0
        if (Z_MODE(op1_addr) == IS_REG) {
9532
          /* set type to avoid zval_ptr_dtor() on uninitialized value */
9533
0
          zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
9534
0
          jit_set_Z_TYPE_INFO(jit, addr, IS_UNDEF);
9535
0
        }
9536
0
        jit_SET_EX_OPLINE(jit, opline);
9537
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_throw_cannot_pass_by_ref));
9538
9539
0
        ir_IF_FALSE(if_pass_by_ref);
9540
0
      }
9541
0
    }
9542
0
  }
9543
9544
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9545
9546
0
  if (opline->op1_type == IS_CONST) {
9547
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
9548
9549
0
    jit_ZVAL_COPY_CONST(jit,
9550
0
      arg_addr,
9551
0
      MAY_BE_ANY, MAY_BE_ANY,
9552
0
      zv, true);
9553
0
  } else {
9554
0
    jit_ZVAL_COPY(jit,
9555
0
      arg_addr,
9556
0
      MAY_BE_ANY,
9557
0
      op1_addr, op1_info, false);
9558
0
  }
9559
9560
0
  return 1;
9561
0
}
9562
9563
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)
9564
0
{
9565
0
  zend_jit_addr op1_addr, arg_addr, ref_addr;
9566
0
  ir_ref ref_path = IR_UNUSED;
9567
9568
0
  op1_addr = OP1_ADDR();
9569
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9570
9571
0
  if (!zend_jit_reuse_ip(jit)) {
9572
0
    return 0;
9573
0
  }
9574
9575
0
  if (opline->op1_type == IS_VAR) {
9576
0
    if (op1_info & MAY_BE_INDIRECT) {
9577
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
9578
0
    }
9579
0
  } else if (opline->op1_type == IS_CV) {
9580
0
    if (op1_info & MAY_BE_UNDEF) {
9581
0
      if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9582
        // JIT: if (Z_TYPE_P(op1) == IS_UNDEF)
9583
0
        ir_ref if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
9584
0
        ir_IF_FALSE(if_def);
9585
        // JIT: ZVAL_NULL(op1)
9586
0
        jit_set_Z_TYPE_INFO(jit,op1_addr, IS_NULL);
9587
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def);
9588
0
      }
9589
0
      op1_info &= ~MAY_BE_UNDEF;
9590
0
      op1_info |= MAY_BE_NULL;
9591
0
    }
9592
0
  } else {
9593
0
    ZEND_UNREACHABLE();
9594
0
  }
9595
9596
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) {
9597
0
    ir_ref ref, ref2;
9598
9599
0
    if (op1_info & MAY_BE_REF) {
9600
0
      ir_ref if_ref;
9601
9602
      // JIT: if (Z_TYPE_P(op1) == IS_UNDEF)
9603
0
      if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9604
0
      ir_IF_TRUE(if_ref);
9605
      // JIT: ref = Z_PTR_P(op1)
9606
0
      ref = jit_Z_PTR(jit, op1_addr);
9607
      // JIT: GC_ADDREF(ref)
9608
0
      jit_GC_ADDREF(jit, ref);
9609
      // JIT: ZVAL_REFERENCE(arg, ref)
9610
0
      jit_set_Z_PTR(jit, arg_addr, ref);
9611
0
      jit_set_Z_TYPE_INFO(jit, arg_addr, IS_REFERENCE_EX);
9612
0
      ref_path = ir_END();
9613
0
      ir_IF_FALSE(if_ref);
9614
0
    }
9615
9616
    // JIT: ZVAL_NEW_REF(arg, varptr);
9617
    // JIT: ref = emalloc(sizeof(zend_reference));
9618
0
    ref = jit_EMALLOC(jit, sizeof(zend_reference), op_array, opline);
9619
    // JIT: GC_REFCOUNT(ref) = 2
9620
0
    jit_set_GC_REFCOUNT(jit, ref, 2);
9621
    // JIT: GC_TYPE(ref) = GC_REFERENCE
9622
0
    ir_STORE(ir_ADD_OFFSET(ref, offsetof(zend_reference, gc.u.type_info)), ir_CONST_U32(GC_REFERENCE));
9623
0
    ir_STORE(ir_ADD_OFFSET(ref, offsetof(zend_reference, sources.ptr)), IR_NULL);
9624
0
    ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
9625
0
    ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
9626
9627
        // JIT: ZVAL_COPY_VALUE(&ref->val, op1)
9628
0
    jit_ZVAL_COPY(jit,
9629
0
      ref_addr,
9630
0
      MAY_BE_ANY,
9631
0
      op1_addr, op1_info, false);
9632
9633
    // JIT: ZVAL_REFERENCE(arg, ref)
9634
0
    jit_set_Z_PTR(jit, op1_addr, ref);
9635
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
9636
9637
    // JIT: ZVAL_REFERENCE(arg, ref)
9638
0
    jit_set_Z_PTR(jit, arg_addr, ref);
9639
0
    jit_set_Z_TYPE_INFO(jit, arg_addr, IS_REFERENCE_EX);
9640
0
  }
9641
9642
0
  if (ref_path) {
9643
0
    ir_MERGE_WITH(ref_path);
9644
0
  }
9645
9646
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
9647
9648
0
  return 1;
9649
0
}
9650
9651
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)
9652
0
{
9653
0
  uint32_t arg_num = opline->op2.num;
9654
0
  zend_jit_addr arg_addr;
9655
0
  ir_ref end_inputs = IR_UNUSED;
9656
9657
0
  ZEND_ASSERT((opline->opcode != ZEND_SEND_VAR_EX &&
9658
0
       opline->opcode != ZEND_SEND_VAR_NO_REF_EX) ||
9659
0
      arg_num <= MAX_ARG_FLAG_NUM);
9660
9661
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9662
9663
0
  if (!zend_jit_reuse_ip(jit)) {
9664
0
    return 0;
9665
0
  }
9666
9667
0
  if (opline->opcode == ZEND_SEND_VAR_EX) {
9668
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9669
0
     && JIT_G(current_frame)
9670
0
     && JIT_G(current_frame)->call
9671
0
     && JIT_G(current_frame)->call->func) {
9672
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9673
0
        if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 0)) {
9674
0
          return 0;
9675
0
        }
9676
0
        return 1;
9677
0
      }
9678
0
    } else {
9679
0
      uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9680
9681
      // JIT: if (RX->func->quick_arg_flags & mask)
9682
0
      ir_ref if_send_by_ref = ir_IF(ir_AND_U32(
9683
0
        ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_RX(func)), offsetof(zend_function, quick_arg_flags))),
9684
0
        ir_CONST_U32(mask)));
9685
0
      ir_IF_TRUE_cold(if_send_by_ref);
9686
9687
0
      if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 1)) {
9688
0
        return 0;
9689
0
      }
9690
9691
0
      ir_END_list(end_inputs);
9692
0
      ir_IF_FALSE(if_send_by_ref);
9693
0
    }
9694
0
  } else if (opline->opcode == ZEND_SEND_VAR_NO_REF_EX) {
9695
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9696
0
     && JIT_G(current_frame)
9697
0
     && JIT_G(current_frame)->call
9698
0
     && JIT_G(current_frame)->call->func) {
9699
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9700
9701
            // JIT: ZVAL_COPY_VALUE(arg, op1)
9702
0
        jit_ZVAL_COPY(jit,
9703
0
          arg_addr,
9704
0
          MAY_BE_ANY,
9705
0
          op1_addr, op1_info, false);
9706
9707
0
        if (!ARG_MAY_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9708
0
          if (!(op1_info & MAY_BE_REF)) {
9709
            /* Don't generate code that always throws exception */
9710
0
            return 0;
9711
0
          } else {
9712
0
            int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9713
0
            const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9714
0
            if (!exit_addr) {
9715
0
              return 0;
9716
0
            }
9717
9718
            // JIT: if (Z_TYPE_P(op1) != IS_REFERENCE)
9719
0
            ir_GUARD(ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U32(IS_REFERENCE)),
9720
0
              ir_CONST_ADDR(exit_addr));
9721
0
          }
9722
0
        }
9723
0
        return 1;
9724
0
      }
9725
0
    } else {
9726
0
      uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9727
0
      ir_ref func, if_send_by_ref, if_prefer_ref;
9728
9729
      // JIT: if (RX->func->quick_arg_flags & mask)
9730
0
      func = ir_LOAD_A(jit_RX(func));
9731
0
      if_send_by_ref = ir_IF(ir_AND_U32(
9732
0
        ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, quick_arg_flags))),
9733
0
        ir_CONST_U32(mask)));
9734
0
      ir_IF_TRUE_cold(if_send_by_ref);
9735
9736
0
      mask = ZEND_SEND_PREFER_REF << ((arg_num + 3) * 2);
9737
9738
          // JIT: ZVAL_COPY_VALUE(arg, op1)
9739
0
      jit_ZVAL_COPY(jit,
9740
0
        arg_addr,
9741
0
        MAY_BE_ANY,
9742
0
        op1_addr, op1_info, false);
9743
9744
0
      if (op1_info & MAY_BE_REF) {
9745
0
        ir_ref if_ref = jit_if_Z_TYPE(jit, arg_addr, IS_REFERENCE);
9746
0
        ir_IF_TRUE(if_ref);
9747
0
        ir_END_list(end_inputs);
9748
0
        ir_IF_FALSE(if_ref);
9749
0
      }
9750
9751
      // JIT: if (RX->func->quick_arg_flags & mask)
9752
0
      if_prefer_ref = ir_IF(ir_AND_U32(
9753
0
        ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, quick_arg_flags))),
9754
0
        ir_CONST_U32(mask)));
9755
0
      ir_IF_TRUE(if_prefer_ref);
9756
0
      ir_END_list(end_inputs);
9757
0
      ir_IF_FALSE(if_prefer_ref);
9758
9759
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9760
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9761
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9762
0
        if (!exit_addr) {
9763
0
          return 0;
9764
0
        }
9765
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
9766
0
      } else {
9767
0
        jit_SET_EX_OPLINE(jit, opline);
9768
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_only_vars_by_reference),
9769
0
          jit_ZVAL_ADDR(jit, arg_addr));
9770
0
        zend_jit_check_exception(jit);
9771
0
        ir_END_list(end_inputs);
9772
0
      }
9773
9774
0
      ir_IF_FALSE(if_send_by_ref);
9775
0
    }
9776
0
  } else if (opline->opcode == ZEND_SEND_FUNC_ARG) {
9777
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9778
0
     && JIT_G(current_frame)
9779
0
     && JIT_G(current_frame)->call
9780
0
     && JIT_G(current_frame)->call->func) {
9781
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9782
0
        if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 0)) {
9783
0
          return 0;
9784
0
        }
9785
0
        return 1;
9786
0
      }
9787
0
    } else {
9788
      // JIT: if (RX->This.u1.type_info & ZEND_CALL_SEND_ARG_BY_REF)
9789
0
      ir_ref if_send_by_ref = ir_IF(ir_AND_U32(
9790
0
        ir_LOAD_U32(jit_RX(This.u1.type_info)),
9791
0
        ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9792
0
      ir_IF_TRUE_cold(if_send_by_ref);
9793
9794
0
      if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 1)) {
9795
0
        return 0;
9796
0
      }
9797
9798
0
      ir_END_list(end_inputs);
9799
0
      ir_IF_FALSE(if_send_by_ref);
9800
0
    }
9801
0
  }
9802
9803
0
  if (op1_info & MAY_BE_UNDEF) {
9804
0
    ir_ref ref, if_def = IR_UNUSED;
9805
9806
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9807
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
9808
0
      ir_IF_FALSE_cold(if_def);
9809
0
    }
9810
9811
    // JIT: zend_jit_undefined_op_helper(opline->op1.var)
9812
0
    jit_SET_EX_OPLINE(jit, opline);
9813
0
    ref = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
9814
0
      ir_CONST_U32(opline->op1.var));
9815
9816
    // JIT: ZVAL_NULL(arg)
9817
0
    jit_set_Z_TYPE_INFO(jit, arg_addr, IS_NULL);
9818
9819
    // JIT: check_exception
9820
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9821
9822
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9823
0
      ir_END_list(end_inputs);
9824
0
      ir_IF_TRUE(if_def);
9825
0
    } else {
9826
0
      if (end_inputs) {
9827
0
        ir_END_list(end_inputs);
9828
0
        ir_MERGE_list(end_inputs);
9829
0
      }
9830
0
      return 1;
9831
0
    }
9832
0
  }
9833
9834
0
  if (opline->opcode == ZEND_SEND_VAR_NO_REF) {
9835
        // JIT: ZVAL_COPY_VALUE(arg, op1)
9836
0
    jit_ZVAL_COPY(jit,
9837
0
      arg_addr,
9838
0
      MAY_BE_ANY,
9839
0
      op1_addr, op1_info, false);
9840
0
    if (op1_info & MAY_BE_REF) {
9841
        // JIT: if (Z_TYPE_P(arg) == IS_REFERENCE)
9842
0
        ir_ref if_ref = jit_if_Z_TYPE(jit, arg_addr, IS_REFERENCE);
9843
0
        ir_IF_TRUE(if_ref);
9844
0
        ir_END_list(end_inputs);
9845
0
        ir_IF_FALSE(if_ref);
9846
0
    }
9847
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9848
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9849
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9850
0
      if (!exit_addr) {
9851
0
        return 0;
9852
0
      }
9853
0
      ir_GUARD(IR_FALSE, ir_CONST_ADDR(exit_addr));
9854
0
    } else {
9855
0
      jit_SET_EX_OPLINE(jit, opline);
9856
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_only_vars_by_reference),
9857
0
        jit_ZVAL_ADDR(jit, arg_addr));
9858
0
      zend_jit_check_exception(jit);
9859
0
    }
9860
0
  } else {
9861
0
    if (op1_info & MAY_BE_REF) {
9862
0
      if (opline->op1_type == IS_CV) {
9863
0
        ir_ref ref;
9864
9865
        // JIT: ZVAL_DEREF(op1)
9866
0
        ref = jit_ZVAL_ADDR(jit, op1_addr);
9867
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
9868
0
        op1_addr = ZEND_ADDR_REF_ZVAL(ref);
9869
9870
            // JIT: ZVAL_COPY(arg, op1)
9871
0
        jit_ZVAL_COPY(jit,
9872
0
          arg_addr,
9873
0
          MAY_BE_ANY,
9874
0
          op1_addr, op1_info, true);
9875
0
      } else {
9876
0
        ir_ref if_ref, ref, ref2, refcount, if_not_zero, if_refcounted;
9877
0
        zend_jit_addr ref_addr;
9878
9879
        // JIT: if (Z_TYPE_P(op1) == IS_REFERENCE)
9880
0
        if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9881
0
        ir_IF_TRUE_cold(if_ref);
9882
9883
        // JIT: ref = Z_COUNTED_P(op1);
9884
0
        ref = jit_Z_PTR(jit, op1_addr);
9885
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
9886
0
        ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
9887
9888
        // JIT: ZVAL_COPY_VALUE(arg, op1);
9889
0
        jit_ZVAL_COPY(jit,
9890
0
          arg_addr,
9891
0
          MAY_BE_ANY,
9892
0
          ref_addr, op1_info, false);
9893
9894
        // JIT: if (GC_DELREF(ref) != 0)
9895
0
        refcount = jit_GC_DELREF(jit, ref);
9896
0
        if_not_zero = ir_IF(refcount);
9897
0
        ir_IF_TRUE(if_not_zero);
9898
9899
                // JIT: if (Z_REFCOUNTED_P(arg)
9900
0
        if_refcounted = jit_if_REFCOUNTED(jit, arg_addr);
9901
0
        ir_IF_TRUE(if_refcounted);
9902
        // JIT: Z_ADDREF_P(arg)
9903
0
        jit_GC_ADDREF(jit, jit_Z_PTR(jit, arg_addr));
9904
0
        ir_END_list(end_inputs);
9905
0
        ir_IF_FALSE(if_refcounted);
9906
0
        ir_END_list(end_inputs);
9907
9908
0
        ir_IF_FALSE(if_not_zero);
9909
9910
        // JIT: efree(ref)
9911
0
        jit_EFREE(jit, ref, sizeof(zend_reference), op_array, opline);
9912
0
        ir_END_list(end_inputs);
9913
9914
0
        ir_IF_FALSE(if_ref);
9915
9916
        // JIT: ZVAL_COPY_VALUE(arg, op1);
9917
0
        jit_ZVAL_COPY(jit,
9918
0
          arg_addr,
9919
0
          MAY_BE_ANY,
9920
0
          op1_addr, op1_info, false);
9921
0
      }
9922
0
    } else {
9923
0
      if (op1_addr != op1_def_addr) {
9924
0
        if (!zend_jit_update_regs(jit, opline->op1.var, op1_addr, op1_def_addr, op1_info)) {
9925
0
          return 0;
9926
0
        }
9927
0
        if (Z_MODE(op1_def_addr) == IS_REG && Z_MODE(op1_addr) != IS_REG) {
9928
0
          op1_addr = op1_def_addr;
9929
0
        }
9930
0
      }
9931
9932
          // JIT: ZVAL_COPY_VALUE(arg, op1)
9933
0
      jit_ZVAL_COPY(jit,
9934
0
        arg_addr,
9935
0
        MAY_BE_ANY,
9936
0
        op1_addr, op1_info, opline->op1_type == IS_CV);
9937
0
    }
9938
0
  }
9939
9940
0
  if (end_inputs) {
9941
0
    ir_END_list(end_inputs);
9942
0
    ir_MERGE_list(end_inputs);
9943
0
  }
9944
9945
0
  return 1;
9946
0
}
9947
9948
static int zend_jit_check_func_arg(zend_jit_ctx *jit, const zend_op *opline)
9949
0
{
9950
0
  uint32_t arg_num = opline->op2.num;
9951
0
  ir_ref ref;
9952
9953
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9954
0
   && JIT_G(current_frame)
9955
0
   && JIT_G(current_frame)->call
9956
0
   && JIT_G(current_frame)->call->func) {
9957
0
    if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9958
0
      if (!TRACE_FRAME_IS_LAST_SEND_BY_REF(JIT_G(current_frame)->call)) {
9959
0
        TRACE_FRAME_SET_LAST_SEND_BY_REF(JIT_G(current_frame)->call);
9960
        // JIT: ZEND_ADD_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9961
0
        if (jit->reuse_ip) {
9962
0
          ref = jit_IP(jit);
9963
0
        } else {
9964
0
          ref = ir_LOAD_A(jit_EX(call));
9965
0
        }
9966
0
        ref = jit_CALL(ref, This.u1.type_info);
9967
0
        ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9968
0
      }
9969
0
    } else {
9970
0
      if (!TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
9971
0
        TRACE_FRAME_SET_LAST_SEND_BY_VAL(JIT_G(current_frame)->call);
9972
        // JIT: ZEND_DEL_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9973
0
        if (jit->reuse_ip) {
9974
0
          ref = jit_IP(jit);
9975
0
        } else {
9976
0
          ref = ir_LOAD_A(jit_EX(call));
9977
0
        }
9978
0
        ref = jit_CALL(ref, This.u1.type_info);
9979
0
        ir_STORE(ref, ir_AND_U32(ir_LOAD_U32(ref), ir_CONST_U32(~ZEND_CALL_SEND_ARG_BY_REF)));
9980
0
      }
9981
0
    }
9982
0
  } else {
9983
    // JIT: if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(EX(call)->func, arg_num)) {
9984
0
    uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9985
0
    ir_ref rx, if_ref, cold_path;
9986
9987
0
    if (!zend_jit_reuse_ip(jit)) {
9988
0
      return 0;
9989
0
    }
9990
9991
0
    rx = jit_IP(jit);
9992
9993
0
    ref = ir_AND_U32(
9994
0
      ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_CALL(rx, func)), offsetof(zend_function, quick_arg_flags))),
9995
0
      ir_CONST_U32(mask));
9996
0
    if_ref = ir_IF(ref);
9997
0
    ir_IF_TRUE_cold(if_ref);
9998
9999
    // JIT: ZEND_ADD_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
10000
0
    ref = jit_CALL(rx, This.u1.type_info);
10001
0
    ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
10002
10003
0
    cold_path = ir_END();
10004
0
    ir_IF_FALSE(if_ref);
10005
10006
    // JIT: ZEND_DEL_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
10007
0
    ref = jit_CALL(rx, This.u1.type_info);
10008
0
    ir_STORE(ref, ir_AND_U32(ir_LOAD_U32(ref), ir_CONST_U32(~ZEND_CALL_SEND_ARG_BY_REF)));
10009
10010
0
    ir_MERGE_WITH(cold_path);
10011
0
  }
10012
10013
0
  return 1;
10014
0
}
10015
10016
static int zend_jit_check_undef_args(zend_jit_ctx *jit, const zend_op *opline)
10017
0
{
10018
0
  ir_ref call, if_may_have_undef, ret;
10019
10020
0
  if (jit->reuse_ip) {
10021
0
    call = jit_IP(jit);
10022
0
  } else {
10023
0
    call = ir_LOAD_A(jit_EX(call));
10024
0
  }
10025
10026
0
  if_may_have_undef = ir_IF(ir_AND_U8(
10027
0
    ir_LOAD_U8(ir_ADD_OFFSET(call, offsetof(zend_execute_data, This.u1.type_info) + 3)),
10028
0
    ir_CONST_U8(ZEND_CALL_MAY_HAVE_UNDEF >> 24)));
10029
10030
0
  ir_IF_TRUE_cold(if_may_have_undef);
10031
0
  jit_SET_EX_OPLINE(jit, opline);
10032
0
  ret = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_handle_undef_args), call);
10033
0
  ir_GUARD_NOT(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10034
0
  ir_MERGE_WITH_EMPTY_FALSE(if_may_have_undef);
10035
10036
0
  return 1;
10037
0
}
10038
10039
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)
10040
0
{
10041
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
10042
0
  zend_call_info *call_info = NULL;
10043
0
  const zend_function *func = NULL;
10044
0
  uint32_t i;
10045
0
  uint32_t call_num_args = 0;
10046
0
  bool unknown_num_args = false;
10047
0
  const void *exit_addr = NULL;
10048
0
  const zend_op *prev_opline;
10049
0
  ir_ref rx, func_ref = IR_UNUSED, if_user = IR_UNUSED, user_path = IR_UNUSED;
10050
10051
0
  prev_opline = opline - 1;
10052
0
  while (prev_opline->opcode == ZEND_EXT_FCALL_BEGIN || prev_opline->opcode == ZEND_TICKS) {
10053
0
    prev_opline--;
10054
0
  }
10055
0
  if (prev_opline->opcode == ZEND_SEND_UNPACK || prev_opline->opcode == ZEND_SEND_ARRAY ||
10056
0
      prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS) {
10057
0
    unknown_num_args = true;
10058
0
  }
10059
10060
0
  if (info) {
10061
0
    call_info = info->callee_info;
10062
0
    while (call_info && call_info->caller_call_opline != opline) {
10063
0
      call_info = call_info->next_callee;
10064
0
    }
10065
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
10066
0
      func = call_info->callee_func;
10067
0
    }
10068
0
    if ((op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)
10069
0
     && (!JIT_G(current_frame)
10070
0
      || !JIT_G(current_frame)->call
10071
0
      || !JIT_G(current_frame)->call->func)) {
10072
0
      call_info = NULL; func = NULL; /* megamorphic call from trait */
10073
0
    }
10074
0
  }
10075
0
  if (!func) {
10076
    /* resolve function at run time */
10077
0
  } else if (func->type == ZEND_USER_FUNCTION) {
10078
0
    ZEND_ASSERT(opline->opcode != ZEND_DO_ICALL);
10079
0
    call_num_args = call_info->num_args;
10080
0
  } else if (func->type == ZEND_INTERNAL_FUNCTION) {
10081
0
    ZEND_ASSERT(opline->opcode != ZEND_DO_UCALL);
10082
0
    call_num_args = call_info->num_args;
10083
0
  } else {
10084
0
    ZEND_UNREACHABLE();
10085
0
  }
10086
10087
0
  if (trace && !func) {
10088
0
    if (trace->op == ZEND_JIT_TRACE_DO_ICALL) {
10089
0
      ZEND_ASSERT(!trace->func || trace->func->type == ZEND_INTERNAL_FUNCTION);
10090
0
#ifndef ZEND_WIN32
10091
      // TODO: ASLR may cause different addresses in different workers ???
10092
0
      func = trace->func;
10093
0
      if (JIT_G(current_frame) &&
10094
0
          JIT_G(current_frame)->call &&
10095
0
          TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call) >= 0) {
10096
0
        call_num_args = TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call);
10097
0
      } else {
10098
0
        unknown_num_args = true;
10099
0
      }
10100
0
#endif
10101
0
    } else if (trace->op == ZEND_JIT_TRACE_ENTER) {
10102
0
      ZEND_ASSERT(trace->func->type == ZEND_USER_FUNCTION);
10103
0
      if (zend_accel_in_shm(trace->func->op_array.opcodes)) {
10104
0
        func = trace->func;
10105
0
        if (JIT_G(current_frame) &&
10106
0
            JIT_G(current_frame)->call &&
10107
0
            TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call) >= 0) {
10108
0
          call_num_args = TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call);
10109
0
        } else {
10110
0
          unknown_num_args = true;
10111
0
        }
10112
0
      }
10113
0
    }
10114
0
  }
10115
10116
0
  bool may_have_extra_named_params =
10117
0
    (opline->extended_value & ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS) &&
10118
0
    (!func || func->common.fn_flags & ZEND_ACC_VARIADIC);
10119
10120
0
  if (!jit->reuse_ip) {
10121
0
    zend_jit_start_reuse_ip(jit);
10122
    // JIT: call = EX(call);
10123
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(call)));
10124
0
  }
10125
0
  rx = jit_IP(jit);
10126
0
  zend_jit_stop_reuse_ip(jit);
10127
10128
0
  jit_SET_EX_OPLINE(jit, opline);
10129
10130
0
  if (opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) {
10131
0
    if (!func) {
10132
0
      if (trace) {
10133
0
        uint32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10134
10135
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10136
0
        if (!exit_addr) {
10137
0
          return 0;
10138
0
        }
10139
10140
0
        func_ref = ir_LOAD_A(jit_CALL(rx, func));
10141
0
        ir_GUARD_NOT(
10142
0
          ir_AND_U32(
10143
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10144
0
            ir_CONST_U32(ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD)),
10145
0
          ir_CONST_ADDR(exit_addr));
10146
0
      }
10147
0
    }
10148
0
  }
10149
10150
0
  if (!jit->delayed_call_level) {
10151
    // JIT: EX(call) = call->prev_execute_data;
10152
0
    ir_STORE(jit_EX(call),
10153
0
      (call_level == 1) ? IR_NULL : ir_LOAD_A(jit_CALL(rx, prev_execute_data)));
10154
0
  }
10155
0
  delayed_call_chain = false;
10156
0
  jit->delayed_call_level = 0;
10157
10158
  // JIT: call->prev_execute_data = execute_data;
10159
0
  ir_STORE(jit_CALL(rx, prev_execute_data), jit_FP(jit));
10160
10161
0
  if (!func) {
10162
0
    if (!func_ref) {
10163
0
      func_ref = ir_LOAD_A(jit_CALL(rx, func));
10164
0
    }
10165
0
  }
10166
10167
0
  if (opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) {
10168
0
    if (!func) {
10169
0
      if (!trace) {
10170
0
        ir_ref if_deprecated_nodiscard, ret;
10171
10172
0
        uint32_t no_discard = RETURN_VALUE_USED(opline) ? 0 : ZEND_ACC_NODISCARD;
10173
10174
0
        if_deprecated_nodiscard = ir_IF(ir_AND_U32(
10175
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10176
0
            ir_CONST_U32(ZEND_ACC_DEPRECATED|no_discard)));
10177
0
        ir_IF_TRUE_cold(if_deprecated_nodiscard);
10178
10179
0
        ir_ref helper = ir_CONST_FC_FUNC(no_discard ? zend_jit_deprecated_nodiscard_helper : zend_jit_deprecated_helper);
10180
0
        if (GCC_GLOBAL_REGS) {
10181
0
          ret = ir_CALL(IR_BOOL, helper);
10182
0
        } else {
10183
0
          ret = ir_CALL_1(IR_BOOL, helper, rx);
10184
0
        }
10185
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10186
0
        ir_MERGE_WITH_EMPTY_FALSE(if_deprecated_nodiscard);
10187
0
      }
10188
0
    } else {
10189
0
      if (func->common.fn_flags & ZEND_ACC_DEPRECATED) {
10190
0
        ir_ref ret;
10191
10192
0
        if (GCC_GLOBAL_REGS) {
10193
0
          ret = ir_CALL(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_deprecated_helper));
10194
0
        } else {
10195
0
          ret = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_deprecated_helper), rx);
10196
0
        }
10197
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10198
0
      }
10199
10200
0
      if ((func->common.fn_flags & ZEND_ACC_NODISCARD) && !RETURN_VALUE_USED(opline)) {
10201
0
        ir_ref ret;
10202
10203
0
        if (GCC_GLOBAL_REGS) {
10204
0
          ret = ir_CALL(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_nodiscard_helper));
10205
0
        } else {
10206
0
          ret = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_nodiscard_helper), rx);
10207
0
        }
10208
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10209
0
      }
10210
0
    }
10211
0
  }
10212
10213
0
  if (!func
10214
0
   && opline->opcode != ZEND_DO_UCALL
10215
0
   && opline->opcode != ZEND_DO_ICALL) {
10216
0
    ir_ref type_ref = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_function, type)));
10217
0
    if_user = ir_IF(ir_EQ(type_ref, ir_CONST_U8(ZEND_USER_FUNCTION)));
10218
0
    ir_IF_TRUE(if_user);
10219
0
  }
10220
10221
0
  if ((!func || func->type == ZEND_USER_FUNCTION)
10222
0
   && opline->opcode != ZEND_DO_ICALL) {
10223
0
    bool recursive_call_through_jmp = false;
10224
0
    uint32_t num_args = 0;
10225
10226
    // JIT: EX(call) = NULL;
10227
0
    ir_STORE(jit_CALL(rx, call), IR_NULL);
10228
10229
    // JIT: EX(return_value) = RETURN_VALUE_USED(opline) ? EX_VAR(opline->result.var) : 0;
10230
0
    ir_STORE(jit_CALL(rx, return_value),
10231
0
      RETURN_VALUE_USED(opline) ?
10232
0
        jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var)) :
10233
0
        IR_NULL);
10234
10235
    // JIT: EX_LOAD_RUN_TIME_CACHE(op_array);
10236
0
    if (!func || func->op_array.cache_size) {
10237
0
      ir_ref run_time_cache;
10238
10239
0
      if (func && op_array == &func->op_array) {
10240
        /* recursive call */
10241
0
        run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
10242
0
      } else if (func
10243
0
       && !(func->op_array.fn_flags & ZEND_ACC_CLOSURE)
10244
0
       && ZEND_MAP_PTR_IS_OFFSET(func->op_array.run_time_cache)) {
10245
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_CG(map_ptr_base)),
10246
0
          (uintptr_t)ZEND_MAP_PTR(func->op_array.run_time_cache)));
10247
0
      } else if (func && (func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
10248
        /* Closures always use direct pointers */
10249
0
        ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
10250
10251
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(local_func_ref, offsetof(zend_op_array, run_time_cache__ptr)));
10252
0
      } else {
10253
0
        ir_ref if_odd, run_time_cache2;
10254
0
        ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
10255
10256
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(local_func_ref, offsetof(zend_op_array, run_time_cache__ptr)));
10257
0
        if_odd = ir_IF(ir_AND_A(run_time_cache, ir_CONST_ADDR(1)));
10258
0
        ir_IF_TRUE(if_odd);
10259
10260
0
        run_time_cache2 = ir_LOAD_A(ir_ADD_A(run_time_cache, ir_LOAD_A(jit_CG(map_ptr_base))));
10261
10262
0
        ir_MERGE_WITH_EMPTY_FALSE(if_odd);
10263
0
        run_time_cache = ir_PHI_2(IR_ADDR, run_time_cache2, run_time_cache);
10264
0
      }
10265
10266
0
      ir_STORE(jit_CALL(rx, run_time_cache), run_time_cache);
10267
0
    }
10268
10269
    // JIT: EG(current_execute_data) = execute_data = call;
10270
0
    ir_STORE(jit_EG(current_execute_data), rx);
10271
0
    jit_STORE_FP(jit, rx);
10272
10273
    // JIT: opline = op_array->opcodes;
10274
0
    if (func && !unknown_num_args) {
10275
10276
0
      for (i = call_num_args; i < func->op_array.last_var; i++) {
10277
0
        uint32_t n = EX_NUM_TO_VAR(i);
10278
0
        zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, n);
10279
10280
0
        jit_set_Z_TYPE_INFO_ex(jit, var_addr, ir_CONST_U32(IS_UNDEF));
10281
0
      }
10282
10283
0
      if (call_num_args <= func->op_array.num_args) {
10284
0
        if (!trace || (trace->op == ZEND_JIT_TRACE_END
10285
0
         && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10286
0
          if ((func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0) {
10287
0
            if (trace) {
10288
0
              num_args = 0;
10289
0
            } else if (call_info) {
10290
0
              num_args = skip_valid_arguments(op_array, ssa, call_info);
10291
0
            } else {
10292
0
              num_args = call_num_args;
10293
0
            }
10294
0
          } else {
10295
0
            num_args = call_num_args;
10296
0
          }
10297
0
          if (zend_accel_in_shm(func->op_array.opcodes)) {
10298
0
            jit_LOAD_IP_ADDR(jit, func->op_array.opcodes + num_args);
10299
0
          } else {
10300
0
            if (!func_ref) {
10301
0
              func_ref = ir_LOAD_A(jit_CALL(rx, func));
10302
0
            }
10303
0
            ir_ref ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10304
0
            if (num_args) {
10305
0
              ip = ir_ADD_OFFSET(ip, num_args * sizeof(zend_op));
10306
0
            }
10307
0
            jit_STORE_IP(jit, ip);
10308
0
          }
10309
10310
0
          if (!trace && op_array == &func->op_array && call_num_args >= op_array->required_num_args) {
10311
            /* recursive call */
10312
0
            recursive_call_through_jmp = true;
10313
0
          }
10314
0
        }
10315
0
      } else {
10316
0
        ir_ref helper;
10317
0
        if (!trace || (trace->op == ZEND_JIT_TRACE_END
10318
0
         && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10319
0
          ir_ref ip;
10320
10321
0
          if (zend_accel_in_shm(func->op_array.opcodes)) {
10322
0
            ip = ir_CONST_ADDR(func->op_array.opcodes);
10323
0
          } else {
10324
0
            if (!func_ref) {
10325
0
              func_ref = ir_LOAD_A(jit_CALL(rx, func));
10326
0
            }
10327
0
            ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10328
0
          }
10329
0
          jit_STORE_IP(jit, ip);
10330
0
          helper = ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper);
10331
0
        } else {
10332
0
          helper = ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper_no_skip_recv);
10333
0
        }
10334
0
        if (GCC_GLOBAL_REGS) {
10335
0
          ir_CALL(IR_VOID, helper);
10336
0
        } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
10337
0
          ir_CALL_2(IR_ADDR, helper, jit_FP(jit), jit_IP(jit));
10338
0
        } else {
10339
0
          ir_ref ref = ir_CALL_2(IR_ADDR, helper, jit_FP(jit), jit_IP(jit));
10340
0
          jit_STORE_IP(jit, ref);
10341
0
        }
10342
0
      }
10343
0
    } else {
10344
0
      ir_ref ip;
10345
0
      ir_ref merge_inputs = IR_UNUSED;
10346
10347
      // JIT: opline = op_array->opcodes
10348
0
      if (func && zend_accel_in_shm(func->op_array.opcodes)) {
10349
0
        ip = ir_CONST_ADDR(func->op_array.opcodes);
10350
0
      } else {
10351
0
        if (!func_ref) {
10352
0
          func_ref = ir_LOAD_A(jit_CALL(rx, func));
10353
0
        }
10354
0
        ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10355
0
      }
10356
0
      jit_STORE_IP(jit, ip);
10357
10358
      // JIT: num_args = EX_NUM_ARGS();
10359
0
      ir_ref num_args, first_extra_arg;
10360
10361
0
      num_args = ir_LOAD_U32(jit_EX(This.u2.num_args));
10362
0
      if (func) {
10363
0
        first_extra_arg = ir_CONST_U32(func->op_array.num_args);
10364
0
      } else {
10365
        // JIT: first_extra_arg = op_array->num_args;
10366
0
        ZEND_ASSERT(func_ref);
10367
0
        first_extra_arg = ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, num_args)));
10368
0
      }
10369
10370
      // JIT: if (UNEXPECTED(num_args > first_extra_arg))
10371
0
      ir_ref if_extra_args = ir_IF(ir_GT(num_args, first_extra_arg));
10372
0
      ir_IF_TRUE_cold(if_extra_args);
10373
0
      if (GCC_GLOBAL_REGS) {
10374
0
        ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper));
10375
0
      } else {
10376
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));
10377
0
        jit_STORE_IP(jit, ref);
10378
0
      }
10379
0
      ir_END_list(merge_inputs);
10380
0
      ir_IF_FALSE(if_extra_args);
10381
0
      if (!func || (func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0) {
10382
0
        if (!func) {
10383
          // JIT: if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0))
10384
0
          ir_ref if_has_type_hints = ir_IF(ir_AND_U32(
10385
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10386
0
            ir_CONST_U32(ZEND_ACC_HAS_TYPE_HINTS)));
10387
0
          ir_IF_TRUE(if_has_type_hints);
10388
0
          ir_END_list(merge_inputs);
10389
0
          ir_IF_FALSE(if_has_type_hints);
10390
0
        }
10391
        // JIT: opline += num_args;
10392
10393
0
        ir_ref ref = ir_MUL_U32(num_args, ir_CONST_U32(sizeof(zend_op)));
10394
10395
0
        if (sizeof(void*) == 8) {
10396
0
          ref = ir_ZEXT_A(ref);
10397
0
        }
10398
10399
0
        jit_STORE_IP(jit, ir_ADD_A(jit_IP(jit), ref));
10400
0
      }
10401
10402
0
      ir_END_list(merge_inputs);
10403
0
      ir_MERGE_list(merge_inputs);
10404
10405
      // JIT: if (EXPECTED((int)num_args < op_array->last_var)) {
10406
0
      ir_ref last_var;
10407
10408
0
      if (func) {
10409
0
        last_var = ir_CONST_U32(func->op_array.last_var);
10410
0
      } else {
10411
0
        ZEND_ASSERT(func_ref);
10412
0
        last_var = ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, last_var)));
10413
0
      }
10414
10415
0
      ir_ref idx = ir_SUB_U32(last_var, num_args);
10416
0
      ir_ref if_need = ir_IF(ir_GT(idx, ir_CONST_U32(0)));
10417
0
      ir_IF_TRUE(if_need);
10418
10419
      // JIT: zval *var = EX_VAR_NUM(num_args);
10420
0
      if (sizeof(void*) == 8) {
10421
0
        num_args = ir_ZEXT_A(num_args);
10422
0
      }
10423
0
      ir_ref var_ref = ir_ADD_OFFSET(
10424
0
        ir_ADD_A(jit_FP(jit), ir_MUL_A(num_args, ir_CONST_ADDR(sizeof(zval)))),
10425
0
        (ZEND_CALL_FRAME_SLOT * sizeof(zval)) + offsetof(zval, u1.type_info));
10426
10427
0
      ir_ref loop = ir_LOOP_BEGIN(ir_END());
10428
0
      var_ref = ir_PHI_2(IR_ADDR, var_ref, IR_UNUSED);
10429
0
      idx = ir_PHI_2(IR_U32, idx, IR_UNUSED);
10430
0
      ir_STORE(var_ref, ir_CONST_I32(IS_UNDEF));
10431
0
      ir_PHI_SET_OP(var_ref, 2, ir_ADD_OFFSET(var_ref, sizeof(zval)));
10432
0
      ir_ref idx2 = ir_SUB_U32(idx, ir_CONST_U32(1));
10433
0
      ir_PHI_SET_OP(idx, 2, idx2);
10434
0
      ir_ref if_not_zero = ir_IF(idx2);
10435
0
      ir_IF_TRUE(if_not_zero);
10436
0
      ir_MERGE_SET_OP(loop, 2, ir_LOOP_END());
10437
0
      ir_IF_FALSE(if_not_zero);
10438
0
      ir_MERGE_WITH_EMPTY_FALSE(if_need);
10439
0
    }
10440
10441
0
    if (ZEND_OBSERVER_ENABLED && (!func || (func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0)) {
10442
0
      ir_ref observer_handler;
10443
0
      ir_ref rx = jit_FP(jit);
10444
0
      const zend_op *observer_opline = NULL;
10445
0
      struct jit_observer_fcall_is_unobserved_data unobserved_data = jit_observer_fcall_is_unobserved_start(jit, func, &observer_handler, rx, func_ref);
10446
0
      if (trace && (trace->op != ZEND_JIT_TRACE_END || trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10447
0
        ZEND_ASSERT(trace[1].op == ZEND_JIT_TRACE_VM || trace[1].op == ZEND_JIT_TRACE_END);
10448
0
        observer_opline = trace[1].opline;
10449
0
        jit_SET_EX_OPLINE(jit, observer_opline);
10450
0
      } else {
10451
        // EX(opline) = opline
10452
0
        ir_STORE(jit_EX(opline), jit_IP(jit));
10453
0
      }
10454
0
      jit_observer_fcall_begin(jit, rx, observer_handler);
10455
10456
0
      zend_jit_check_timeout(jit, observer_opline, NULL);
10457
10458
0
      jit_observer_fcall_is_unobserved_end(jit, &unobserved_data);
10459
0
    }
10460
10461
0
    if (trace) {
10462
0
      if (!func && (opline->opcode != ZEND_DO_UCALL)) {
10463
0
        user_path = ir_END();
10464
0
      }
10465
0
    } else {
10466
0
      zend_basic_block *bb;
10467
10468
0
      do {
10469
0
        if (recursive_call_through_jmp) {
10470
0
          ir_ref begin, end;
10471
0
          ir_insn *insn;
10472
10473
          /* attempt to convert direct recursive call into loop */
10474
0
          begin = jit->bb_start_ref[num_args];
10475
0
          ZEND_ASSERT(begin != IR_UNUSED);
10476
0
          insn = &jit->ctx.ir_base[begin];
10477
0
          if (insn->op == IR_BEGIN) {
10478
0
            end = ir_LOOP_END();
10479
0
            insn = &jit->ctx.ir_base[begin];
10480
0
            insn->op = IR_LOOP_BEGIN;
10481
0
            insn->inputs_count = 2;
10482
0
            insn->op2 = end;
10483
0
            break;
10484
0
          } else if ((insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN)
10485
0
              && insn->inputs_count == 2) {
10486
0
            end = ir_LOOP_END();
10487
0
            insn = &jit->ctx.ir_base[begin];
10488
0
            insn->op = IR_LOOP_BEGIN;
10489
0
            insn->inputs_count = 3;
10490
0
            insn->op3 = end;
10491
0
            break;
10492
0
          } else if (insn->op == IR_LOOP_BEGIN && insn->inputs_count == 3) {
10493
0
            ZEND_ASSERT(jit->ctx.ir_base[insn->op3].op == IR_LOOP_END);
10494
0
            jit->ctx.ir_base[insn->op3].op = IR_END;
10495
0
            ir_MERGE_2(insn->op3, ir_END());
10496
0
            end = ir_LOOP_END();
10497
0
            insn = &jit->ctx.ir_base[begin];
10498
0
            insn->op3 = end;
10499
0
            break;
10500
0
          }
10501
0
        }
10502
        /* fallback to indirect JMP or RETURN */
10503
0
        if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
10504
0
          zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
10505
0
        } else {
10506
0
          zend_jit_vm_enter(jit, jit_IP(jit));
10507
0
        }
10508
0
      } while (0);
10509
10510
0
      bb = &jit->ssa->cfg.blocks[jit->b];
10511
0
      if (bb->successors_count > 0) {
10512
0
        int succ;
10513
0
        ir_ref ref;
10514
10515
0
        ZEND_ASSERT(bb->successors_count == 1);
10516
0
        succ = bb->successors[0];
10517
        /* Add a fake control edge from UNREACHABLE/RETURN to the following ENTRY */
10518
0
        ref = jit->ctx.insns_count - 1;
10519
0
        ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_UNREACHABLE
10520
0
          || jit->ctx.ir_base[ref].op == IR_RETURN
10521
0
          || jit->ctx.ir_base[ref].op == IR_LOOP_END);
10522
0
        ZEND_ASSERT(jit->ssa->cfg.blocks[succ].flags & ZEND_BB_ENTRY);
10523
0
        ref = zend_jit_continue_entry(jit, ref, jit->ssa->cfg.blocks[succ].start);
10524
0
        if (func || (opline->opcode == ZEND_DO_UCALL)) {
10525
0
          _zend_jit_add_predecessor_ref(jit, succ, jit->b, ref);
10526
0
          jit->b = -1;
10527
0
        } else {
10528
0
          user_path = ref;
10529
0
        }
10530
0
      }
10531
0
    }
10532
0
  }
10533
10534
0
  if ((!func || func->type == ZEND_INTERNAL_FUNCTION)
10535
0
   && (opline->opcode != ZEND_DO_UCALL)) {
10536
0
    if (!func && (opline->opcode != ZEND_DO_ICALL)) {
10537
0
      ir_IF_FALSE(if_user);
10538
0
    }
10539
10540
    // JIT: EG(current_execute_data) = execute_data;
10541
0
    ir_STORE(jit_EG(current_execute_data), rx);
10542
10543
0
    bool may_have_observer = ZEND_OBSERVER_ENABLED && (!func || (func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0);
10544
0
    if (may_have_observer) {
10545
0
      ir_ref observer_handler;
10546
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)));
10547
0
      jit_observer_fcall_begin(jit, rx, observer_handler);
10548
0
      jit_observer_fcall_is_unobserved_end(jit, &unobserved_data);
10549
0
    }
10550
10551
    // JIT: ZVAL_NULL(EX_VAR(opline->result.var));
10552
0
    ir_ref res_addr = IR_UNUSED, func_ptr;
10553
10554
0
    if (RETURN_VALUE_USED(opline)) {
10555
0
      res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10556
0
    } else {
10557
      /* CPU stack allocated temporary zval */
10558
0
      ir_ref ptr;
10559
10560
0
      if (!jit->ctx.fixed_call_stack_size) {
10561
        // JIT: alloca(sizeof(void*));
10562
0
        ptr = ir_ALLOCA(ir_CONST_ADDR(sizeof(zval)));
10563
0
      } else {
10564
#ifdef _WIN64
10565
        ptr = ir_HARD_COPY_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
10566
#else
10567
0
        ptr = ir_HARD_COPY_A(ir_RLOAD_A(IR_REG_SP));
10568
0
#endif
10569
0
      }
10570
0
      res_addr = ZEND_ADDR_REF_ZVAL(ptr);
10571
0
    }
10572
10573
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
10574
10575
0
    zend_jit_reset_last_valid_opline(jit);
10576
10577
    // JIT: (zend_execute_internal ? zend_execute_internal : fbc->internal_function.handler)(call, ret);
10578
0
    ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
10579
0
    if (zend_execute_internal) {
10580
0
      ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_execute_internal), rx, res_ref);
10581
0
    } else {
10582
0
      if (func) {
10583
0
        func_ptr = ir_CONST_FC_FUNC(func->internal_function.handler);
10584
0
      } else {
10585
0
        func_ptr = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_internal_function, handler)));
10586
#if defined(IR_TARGET_X86)
10587
        func_ptr = ir_CAST_FC_FUNC(func_ptr);
10588
#endif
10589
0
      }
10590
0
      ir_CALL_2(IR_VOID, func_ptr, rx, res_ref);
10591
0
    }
10592
10593
0
    if (may_have_observer) {
10594
0
      jit_observer_fcall_end(jit, rx, res_ref);
10595
0
    }
10596
10597
    /* When zend_interrupt_function is set, it gets called while
10598
     * the frame is still on top. This is less efficient than
10599
     * doing it later once it's popped off. There is code further
10600
     * down that handles when there isn't an interrupt function.
10601
     */
10602
0
    if (zend_interrupt_function) {
10603
      // JIT: if (EG(vm_interrupt)) zend_fcall_interrupt(execute_data);
10604
0
      ir_ref if_interrupt = ir_IF(ir_LOAD_U8(jit_EG(vm_interrupt)));
10605
0
      ir_IF_TRUE_cold(if_interrupt);
10606
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_fcall_interrupt), rx);
10607
0
      ir_MERGE_WITH_EMPTY_FALSE(if_interrupt);
10608
0
    }
10609
10610
    // JIT: EG(current_execute_data) = execute_data;
10611
0
    ir_STORE(jit_EG(current_execute_data), jit_FP(jit));
10612
10613
    // JIT: zend_vm_stack_free_args(call);
10614
0
    if (func && !unknown_num_args) {
10615
0
      for (i = 0; i < call_num_args; i++ ) {
10616
0
        if (zend_jit_needs_arg_dtor(func, i, call_info)) {
10617
0
          uint32_t offset = EX_NUM_TO_VAR(i);
10618
0
          zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, offset);
10619
10620
0
          jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, false,
10621
0
                opline);
10622
0
        }
10623
0
      }
10624
0
    } else {
10625
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_vm_stack_free_args_helper), rx);
10626
0
    }
10627
10628
0
    if (may_have_extra_named_params) {
10629
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS))
10630
0
      ir_ref if_has_named = ir_IF(ir_AND_U8(
10631
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 3)),
10632
0
        ir_CONST_U8(ZEND_CALL_HAS_EXTRA_NAMED_PARAMS >> 24)));
10633
0
      ir_IF_TRUE_cold(if_has_named);
10634
10635
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_free_extra_named_params),
10636
0
        ir_LOAD_A(jit_CALL(rx, extra_named_params)));
10637
10638
0
      ir_MERGE_WITH_EMPTY_FALSE(if_has_named);
10639
0
    }
10640
10641
0
    if (opline->opcode == ZEND_DO_FCALL) {
10642
      // TODO: optimize ???
10643
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS))
10644
0
      ir_ref if_release_this = 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_RELEASE_THIS >> 16)));
10647
0
      ir_IF_TRUE_cold(if_release_this);
10648
10649
      // JIT: OBJ_RELEASE(Z_OBJ(RX->This));
10650
0
      jit_OBJ_RELEASE(jit, ir_LOAD_A(jit_CALL(rx, This.value.obj)));
10651
10652
0
      ir_MERGE_WITH_EMPTY_FALSE(if_release_this);
10653
0
    }
10654
10655
10656
0
    ir_ref allocated_path = IR_UNUSED;
10657
10658
0
    if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
10659
0
        !JIT_G(current_frame) ||
10660
0
        !JIT_G(current_frame)->call ||
10661
0
        !TRACE_FRAME_IS_NESTED(JIT_G(current_frame)->call) ||
10662
0
        prev_opline->opcode == ZEND_SEND_UNPACK ||
10663
0
        prev_opline->opcode == ZEND_SEND_ARRAY ||
10664
0
      prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS) {
10665
10666
      // JIT: zend_vm_stack_free_call_frame(call);
10667
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_ALLOCATED))
10668
0
      ir_ref if_allocated = ir_IF(ir_AND_U8(
10669
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 2)),
10670
0
        ir_CONST_U8(ZEND_CALL_ALLOCATED >> 16)));
10671
0
      ir_IF_TRUE_cold(if_allocated);
10672
10673
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_free_call_frame), rx);
10674
10675
0
      allocated_path = ir_END();
10676
0
      ir_IF_FALSE(if_allocated);
10677
0
    }
10678
10679
0
    ir_STORE(jit_EG(vm_stack_top), rx);
10680
10681
0
    if (allocated_path) {
10682
0
      ir_MERGE_WITH(allocated_path);
10683
0
    }
10684
10685
0
    if (!RETURN_VALUE_USED(opline)) {
10686
0
      zend_class_entry *ce;
10687
0
      bool ce_is_instanceof;
10688
0
      uint32_t func_info = call_info ?
10689
0
        zend_get_func_info(call_info, ssa, &ce, &ce_is_instanceof) :
10690
0
        (MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN);
10691
10692
      /* If an exception is thrown, the return_value may stay at the
10693
       * original value of null. */
10694
0
      func_info |= MAY_BE_NULL;
10695
10696
0
      if (func_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
10697
0
        ir_ref sp;
10698
0
        if (!jit->ctx.fixed_call_stack_size) {
10699
0
          sp = ir_RLOAD_A(IR_REG_SP);
10700
0
        } else {
10701
#ifdef _WIN64
10702
          sp = jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS);
10703
#else
10704
0
          sp = ir_RLOAD_A(IR_REG_SP);
10705
0
#endif
10706
0
        }
10707
0
        res_addr = ZEND_ADDR_REF_ZVAL(sp);
10708
0
        jit_ZVAL_PTR_DTOR(jit, res_addr, func_info, true, opline);
10709
0
      }
10710
0
      if (!jit->ctx.fixed_call_stack_size) {
10711
        // JIT: revert alloca
10712
0
        ir_AFREE(ir_CONST_ADDR(sizeof(zval)));
10713
0
      }
10714
0
    }
10715
10716
    // JIT: if (UNEXPECTED(EG(exception) != NULL)) {
10717
0
    ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
10718
0
      jit_STUB_ADDR(jit, jit_stub_icall_throw));
10719
10720
    /* If there isn't a zend_interrupt_function, the timeout is
10721
     * handled here because it's more efficient.
10722
     */
10723
0
    if (!zend_interrupt_function) {
10724
      // TODO: Can we avoid checking for interrupts after each call ???
10725
0
      if (trace && jit->last_valid_opline != opline) {
10726
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline + 1, ZEND_JIT_EXIT_TO_VM);
10727
10728
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10729
0
        if (!exit_addr) {
10730
0
          return 0;
10731
0
        }
10732
0
      } else {
10733
0
        exit_addr = NULL;
10734
0
      }
10735
10736
0
      zend_jit_check_timeout(jit, opline + 1, exit_addr);
10737
0
    }
10738
10739
0
    if ((!trace || !func) && opline->opcode != ZEND_DO_ICALL) {
10740
0
      jit_LOAD_IP_ADDR(jit, opline + 1);
10741
0
    } else if (trace
10742
0
     && trace->op == ZEND_JIT_TRACE_END
10743
0
     && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
10744
0
      jit_LOAD_IP_ADDR(jit, opline + 1);
10745
0
    }
10746
0
  }
10747
10748
0
  if (user_path) {
10749
0
    ir_MERGE_WITH(user_path);
10750
0
  }
10751
10752
0
  return 1;
10753
0
}
10754
10755
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)
10756
0
{
10757
0
  ir_ref if_skip_constructor = jit_IF_ex(jit, jit_CMP_IP(jit, IR_NE, opline), next_block);
10758
10759
0
  ir_IF_FALSE(if_skip_constructor);
10760
10761
0
  if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
10762
0
    if (!zend_jit_tail_handler(jit, opline)) {
10763
0
      return 0;
10764
0
    }
10765
0
  } else {
10766
0
    if (!zend_jit_do_fcall(jit, opline, op_array, ssa, call_level, next_block, NULL)) {
10767
0
      return 0;
10768
0
    }
10769
0
  }
10770
10771
  /* override predecessors of the next block */
10772
0
  ZEND_ASSERT(jit->ssa->cfg.blocks[next_block].predecessors_count == 1);
10773
0
  if (!jit->ctx.control) {
10774
0
    ZEND_ASSERT(jit->bb_edges[jit->bb_predecessors[next_block]]);
10775
0
    ir_IF_TRUE(if_skip_constructor);
10776
0
    ir_MERGE_2(jit->bb_edges[jit->bb_predecessors[next_block]], ir_END());
10777
0
    jit->bb_edges[jit->bb_predecessors[next_block]] = ir_END();
10778
0
  } else {
10779
0
    ZEND_ASSERT(!jit->bb_edges[jit->bb_predecessors[next_block]]);
10780
    /* merge current control path with the true branch of constructor skip condition */
10781
0
    ir_MERGE_WITH_EMPTY_TRUE(if_skip_constructor);
10782
0
    jit->bb_edges[jit->bb_predecessors[next_block]] = ir_END();
10783
10784
0
    jit->b = -1;
10785
0
  }
10786
10787
0
  return 1;
10788
0
}
10789
10790
static int zend_jit_verify_arg_type(zend_jit_ctx *jit, const zend_op *opline, zend_arg_info *arg_info, bool check_exception)
10791
0
{
10792
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10793
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type) & MAY_BE_ANY;
10794
0
  ir_ref ref, fast_path = IR_UNUSED;
10795
10796
0
  ref = jit_ZVAL_ADDR(jit, res_addr);
10797
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
10798
0
   && JIT_G(current_frame)
10799
0
   && JIT_G(current_frame)->prev) {
10800
0
    zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
10801
0
    uint8_t type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var));
10802
10803
0
    if (type != IS_UNKNOWN && (type_mask & (1u << type))) {
10804
0
      return 1;
10805
0
    }
10806
0
  }
10807
10808
0
  if (ZEND_ARG_SEND_MODE(arg_info)) {
10809
0
    if (opline->opcode == ZEND_RECV_INIT) {
10810
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
10811
0
    } else {
10812
0
      ref = jit_Z_PTR_ref(jit, ref);
10813
0
      ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
10814
0
    }
10815
0
  }
10816
10817
0
  if (type_mask != 0) {
10818
0
    if (is_power_of_two(type_mask)) {
10819
0
      uint32_t type_code = concrete_type(type_mask);
10820
0
      ir_ref if_ok = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(type_code));
10821
0
      ir_IF_TRUE(if_ok);
10822
0
      fast_path = ir_END();
10823
0
      ir_IF_FALSE_cold(if_ok);
10824
0
    } else {
10825
0
      ir_ref if_ok = ir_IF(ir_AND_U32(
10826
0
        ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE_ref(jit, ref)),
10827
0
        ir_CONST_U32(type_mask)));
10828
0
      ir_IF_TRUE(if_ok);
10829
0
      fast_path = ir_END();
10830
0
      ir_IF_FALSE_cold(if_ok);
10831
0
    }
10832
0
  }
10833
10834
0
  jit_SET_EX_OPLINE(jit, opline);
10835
0
  ref = ir_CALL_2(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_verify_arg_slow),
10836
0
    ref, ir_CONST_ADDR(arg_info));
10837
10838
0
  if (check_exception) {
10839
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10840
0
  }
10841
10842
0
  if (fast_path) {
10843
0
    ir_MERGE_WITH(fast_path);
10844
0
  }
10845
10846
0
  return 1;
10847
0
}
10848
10849
static int zend_jit_recv(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array)
10850
0
{
10851
0
  uint32_t arg_num = opline->op1.num;
10852
0
  zend_arg_info *arg_info = NULL;
10853
10854
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
10855
0
    if (EXPECTED(arg_num <= op_array->num_args)) {
10856
0
      arg_info = &op_array->arg_info[arg_num-1];
10857
0
    } else if (UNEXPECTED(op_array->fn_flags & ZEND_ACC_VARIADIC)) {
10858
0
      arg_info = &op_array->arg_info[op_array->num_args];
10859
0
    }
10860
0
    if (arg_info && !ZEND_TYPE_IS_SET(arg_info->type)) {
10861
0
      arg_info = NULL;
10862
0
    }
10863
0
  }
10864
10865
0
  if (arg_info || (opline+1)->opcode != ZEND_RECV) {
10866
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
10867
0
      if (!JIT_G(current_frame) ||
10868
0
          TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) < 0 ||
10869
0
          arg_num > TRACE_FRAME_NUM_ARGS(JIT_G(current_frame))) {
10870
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10871
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10872
10873
0
        if (!exit_addr) {
10874
0
          return 0;
10875
0
        }
10876
0
        ir_GUARD(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)),
10877
0
          ir_CONST_ADDR(exit_addr));
10878
0
      }
10879
0
    } else {
10880
0
      ir_ref if_ok =ir_IF(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)));
10881
0
      ir_IF_FALSE_cold(if_ok);
10882
10883
0
      jit_SET_EX_OPLINE(jit, opline);
10884
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_missing_arg_error), jit_FP(jit));
10885
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
10886
0
      ir_IF_TRUE(if_ok);
10887
0
    }
10888
0
  }
10889
10890
0
  if (arg_info) {
10891
0
    if (!zend_jit_verify_arg_type(jit, opline, arg_info, true)) {
10892
0
      return 0;
10893
0
    }
10894
0
  }
10895
10896
0
  return 1;
10897
0
}
10898
10899
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)
10900
0
{
10901
0
  uint32_t arg_num = opline->op1.num;
10902
0
  zval *zv = RT_CONSTANT(opline, opline->op2);
10903
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10904
0
  ir_ref ref, if_fail, skip_path = IR_UNUSED;
10905
10906
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
10907
0
   && JIT_G(current_frame)
10908
0
   && TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) >= 0) {
10909
0
    if (arg_num > TRACE_FRAME_NUM_ARGS(JIT_G(current_frame))) {
10910
0
      jit_ZVAL_COPY_CONST(jit,
10911
0
        res_addr,
10912
0
        -1, -1,
10913
0
        zv, true);
10914
0
    }
10915
0
  } else {
10916
0
    if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
10917
0
        (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
10918
0
      ir_ref if_skip = ir_IF(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)));
10919
0
      ir_IF_TRUE(if_skip);
10920
0
      skip_path = ir_END();
10921
0
      ir_IF_FALSE(if_skip);
10922
0
    }
10923
0
    jit_ZVAL_COPY_CONST(jit,
10924
0
      res_addr,
10925
0
      -1, -1,
10926
0
      zv, true);
10927
0
  }
10928
10929
0
  if (Z_TYPE_P(zv) == IS_CONSTANT_AST) {
10930
0
    jit_SET_EX_OPLINE(jit, opline);
10931
0
    ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zval_update_constant_ex),
10932
0
      jit_ZVAL_ADDR(jit, res_addr),
10933
0
      ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(func)), offsetof(zend_op_array, scope))));
10934
10935
0
    if_fail = ir_IF(ref);
10936
0
    ir_IF_TRUE_cold(if_fail);
10937
0
    jit_ZVAL_PTR_DTOR(jit, res_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, true, opline);
10938
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
10939
0
    ir_IF_FALSE(if_fail);
10940
0
  }
10941
10942
0
  if (skip_path) {
10943
0
    ir_MERGE_WITH(skip_path);
10944
0
  }
10945
10946
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
10947
0
    do {
10948
0
      zend_arg_info *arg_info;
10949
10950
0
      if (arg_num <= op_array->num_args) {
10951
0
        arg_info = &op_array->arg_info[arg_num-1];
10952
0
      } else if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
10953
0
        arg_info = &op_array->arg_info[op_array->num_args];
10954
0
      } else {
10955
0
        break;
10956
0
      }
10957
0
      if (!ZEND_TYPE_IS_SET(arg_info->type)) {
10958
0
        break;
10959
0
      }
10960
0
      if (!zend_jit_verify_arg_type(jit, opline, arg_info, may_throw)) {
10961
0
        return 0;
10962
0
      }
10963
0
    } while (0);
10964
0
  }
10965
10966
0
  return 1;
10967
0
}
10968
10969
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)
10970
0
{
10971
0
  zend_arg_info *arg_info = &op_array->arg_info[-1];
10972
0
  ZEND_ASSERT(ZEND_TYPE_IS_SET(arg_info->type));
10973
0
  zend_jit_addr op1_addr = OP1_ADDR();
10974
0
  bool needs_slow_check = true;
10975
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type) & MAY_BE_ANY;
10976
0
  ir_ref fast_path = IR_UNUSED;
10977
10978
0
  if (type_mask != 0) {
10979
0
    if (((op1_info & MAY_BE_ANY) & type_mask) == 0) {
10980
      /* pass */
10981
0
    } else if (((op1_info & MAY_BE_ANY) | type_mask) == type_mask) {
10982
0
      needs_slow_check = false;
10983
0
    } else if (is_power_of_two(type_mask)) {
10984
0
      uint32_t type_code = concrete_type(type_mask);
10985
0
      ir_ref if_ok = jit_if_Z_TYPE(jit, op1_addr, type_code);
10986
10987
0
      ir_IF_TRUE(if_ok);
10988
0
      fast_path = ir_END();
10989
0
      ir_IF_FALSE_cold(if_ok);
10990
0
    } else {
10991
0
      ir_ref if_ok = ir_IF(ir_AND_U32(
10992
0
        ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE(jit, op1_addr)),
10993
0
        ir_CONST_U32(type_mask)));
10994
10995
0
      ir_IF_TRUE(if_ok);
10996
0
      fast_path = ir_END();
10997
0
      ir_IF_FALSE_cold(if_ok);
10998
0
    }
10999
0
  }
11000
0
  if (needs_slow_check) {
11001
0
    ir_ref ref;
11002
11003
0
    jit_SET_EX_OPLINE(jit, opline);
11004
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
11005
0
    if (op1_info & MAY_BE_UNDEF) {
11006
0
      ref = zend_jit_zval_check_undef(jit, ref, opline->op1.var, NULL, true);
11007
0
    }
11008
11009
0
    ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_verify_return_slow),
11010
0
      ref,
11011
0
      ir_LOAD_A(jit_EX(func)),
11012
0
      ir_CONST_ADDR(arg_info));
11013
11014
0
    zend_jit_check_exception(jit);
11015
11016
0
    if (fast_path) {
11017
0
      ir_MERGE_WITH(fast_path);
11018
0
    }
11019
0
  }
11020
11021
0
  return true;
11022
0
}
11023
11024
static int zend_jit_leave_frame(zend_jit_ctx *jit)
11025
0
{
11026
  // JIT: EG(current_execute_data) = EX(prev_execute_data);
11027
0
  ir_STORE(jit_EG(current_execute_data), ir_LOAD_A(jit_EX(prev_execute_data)));
11028
0
  return 1;
11029
0
}
11030
11031
static int zend_jit_free_cvs(zend_jit_ctx *jit)
11032
0
{
11033
  // JIT: EG(current_execute_data) = EX(prev_execute_data);
11034
0
  ir_STORE(jit_EG(current_execute_data), ir_LOAD_A(jit_EX(prev_execute_data)));
11035
11036
  // JIT: zend_free_compiled_variables(execute_data);
11037
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_free_compiled_variables), jit_FP(jit));
11038
0
  return 1;
11039
0
}
11040
11041
static int zend_jit_free_cv(zend_jit_ctx *jit, uint32_t info, uint32_t var)
11042
0
{
11043
0
  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11044
0
    zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
11045
11046
0
    jit_ZVAL_PTR_DTOR(jit, var_addr, info, true, NULL);
11047
0
  }
11048
0
  return 1;
11049
0
}
11050
11051
static int zend_jit_free_op(zend_jit_ctx *jit, const zend_op *opline, uint32_t info, uint32_t var_offset)
11052
0
{
11053
0
  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11054
0
    jit_ZVAL_PTR_DTOR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, var_offset), info, false, opline);
11055
0
  }
11056
0
  return 1;
11057
0
}
11058
11059
static int zend_jit_leave_func(zend_jit_ctx         *jit,
11060
                               const zend_op_array  *op_array,
11061
                               const zend_op        *opline,
11062
                               uint32_t              op1_info,
11063
                               bool             left_frame,
11064
                               zend_jit_trace_rec   *trace,
11065
                               zend_jit_trace_info  *trace_info,
11066
                               int                   indirect_var_access,
11067
                               int                   may_throw)
11068
0
{
11069
0
  bool may_be_top_frame =
11070
0
    JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11071
0
    !JIT_G(current_frame) ||
11072
0
    !TRACE_FRAME_IS_NESTED(JIT_G(current_frame));
11073
0
  bool may_need_call_helper =
11074
0
    indirect_var_access || /* may have symbol table */
11075
0
    !op_array->function_name || /* may have symbol table */
11076
0
    may_be_top_frame ||
11077
0
    (op_array->fn_flags & ZEND_ACC_VARIADIC) || /* may have extra named args */
11078
0
    JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11079
0
    !JIT_G(current_frame) ||
11080
0
    TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) == -1 || /* unknown number of args */
11081
0
    (uint32_t)TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) > op_array->num_args; /* extra args */
11082
0
  bool may_need_release_this =
11083
0
    !(op_array->fn_flags & ZEND_ACC_CLOSURE) &&
11084
0
    op_array->scope &&
11085
0
    !(op_array->fn_flags & ZEND_ACC_STATIC) &&
11086
0
    (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11087
0
     !JIT_G(current_frame) ||
11088
0
     !TRACE_FRAME_NO_NEED_RELEASE_THIS(JIT_G(current_frame)));
11089
0
  ir_ref call_info = IR_UNUSED, ref, cold_path = IR_UNUSED;
11090
11091
0
  if (may_need_call_helper) {
11092
0
    if (!left_frame) {
11093
0
      left_frame = true;
11094
0
        if (!zend_jit_leave_frame(jit)) {
11095
0
        return 0;
11096
0
        }
11097
0
    }
11098
    /* ZEND_CALL_FAKE_CLOSURE handled on slow path to eliminate check for ZEND_CALL_CLOSURE on fast path */
11099
0
    call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
11100
0
    ref = ir_AND_U32(call_info,
11101
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));
11102
0
    if (trace && trace->op != ZEND_JIT_TRACE_END) {
11103
0
      ir_ref if_slow = ir_IF(ref);
11104
11105
0
      ir_IF_TRUE_cold(if_slow);
11106
0
      if (!GCC_GLOBAL_REGS) {
11107
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_func_helper), jit_FP(jit), jit_IP(jit));
11108
0
      } else {
11109
0
        ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_func_helper));
11110
0
      }
11111
11112
0
      if (may_be_top_frame) {
11113
        // TODO: try to avoid this check ???
11114
0
        if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
11115
#if 0
11116
          /* this check should be handled by the following OPLINE guard */
11117
          | cmp IP, zend_jit_halt_op
11118
          | je ->trace_halt
11119
#endif
11120
0
        } else if (GCC_GLOBAL_REGS) {
11121
0
          ir_GUARD(jit_IP(jit), jit_STUB_ADDR(jit, jit_stub_trace_halt));
11122
0
        } else {
11123
0
          ir_GUARD(ir_NE(ref, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)), jit_STUB_ADDR(jit, jit_stub_trace_halt));
11124
0
          jit_STORE_IP(jit, ref);
11125
0
        }
11126
0
      }
11127
11128
0
      if (!GCC_GLOBAL_REGS) {
11129
        // execute_data = EG(current_execute_data)
11130
0
        jit_STORE_FP(jit, ir_LOAD_A(jit_EG(current_execute_data)));
11131
0
      }
11132
0
      cold_path = ir_END();
11133
0
      ir_IF_FALSE(if_slow);
11134
0
    } else {
11135
0
      ir_GUARD_NOT(ref, jit_STUB_ADDR(jit, jit_stub_leave_function_handler));
11136
0
    }
11137
0
  }
11138
11139
0
  if ((op_array->fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) == ZEND_ACC_CLOSURE) {
11140
0
    if (!left_frame) {
11141
0
      left_frame = true;
11142
0
        if (!zend_jit_leave_frame(jit)) {
11143
0
        return 0;
11144
0
        }
11145
0
    }
11146
    // JIT: OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
11147
0
    jit_OBJ_RELEASE(jit, ir_ADD_OFFSET(ir_LOAD_A(jit_EX(func)), -sizeof(zend_object)));
11148
0
  } else if (may_need_release_this) {
11149
0
    ir_ref if_release, fast_path = IR_UNUSED;
11150
11151
0
    if (!left_frame) {
11152
0
      left_frame = true;
11153
0
        if (!zend_jit_leave_frame(jit)) {
11154
0
        return 0;
11155
0
        }
11156
0
    }
11157
0
    if (!JIT_G(current_frame) || !TRACE_FRAME_ALWAYS_RELEASE_THIS(JIT_G(current_frame))) {
11158
      // JIT: if (call_info & ZEND_CALL_RELEASE_THIS)
11159
0
      if (!call_info) {
11160
0
        call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
11161
0
      }
11162
0
      if_release = ir_IF(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_RELEASE_THIS)));
11163
0
      ir_IF_FALSE(if_release);
11164
0
      fast_path = ir_END();
11165
0
      ir_IF_TRUE(if_release);
11166
0
    }
11167
    // JIT: OBJ_RELEASE(execute_data->This))
11168
0
    jit_OBJ_RELEASE(jit, ir_LOAD_A(jit_EX(This.value.obj)));
11169
0
    if (fast_path) {
11170
0
      ir_MERGE_WITH(fast_path);
11171
0
    }
11172
    // TODO: avoid EG(excption) check for $this->foo() calls
11173
0
    may_throw = 1;
11174
0
  }
11175
11176
  // JIT: EG(vm_stack_top) = (zval*)execute_data
11177
0
  ir_STORE(jit_EG(vm_stack_top), jit_FP(jit));
11178
11179
  // JITL execute_data = EX(prev_execute_data)
11180
0
  jit_STORE_FP(jit, ir_LOAD_A(jit_EX(prev_execute_data)));
11181
11182
0
  if (!left_frame) {
11183
    // JIT: EG(current_execute_data) = execute_data
11184
0
    ir_STORE(jit_EG(current_execute_data), jit_FP(jit));
11185
0
  }
11186
11187
0
  if (trace) {
11188
0
    if (trace->op != ZEND_JIT_TRACE_END
11189
0
     && (JIT_G(current_frame) && !TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame)))) {
11190
0
      zend_jit_reset_last_valid_opline(jit);
11191
0
    } else {
11192
      /* We add extra RLOAD and RSTORE to make fusion for persistent register
11193
       *     mov (%FP), %IP
11194
       *     add $0x1c, %IP
11195
       * The naive (commented) code leads to extra register allocation and move.
11196
       *     mov (%FP), %tmp
11197
       *     add $0x1c, %tmp
11198
       *     mov %tmp, %FP
11199
       */
11200
#if 0
11201
      jit_STORE_IP(jit, ir_ADD_OFFSET(ir_LOAD_A(jit_EX(opline)), sizeof(zend_op)));
11202
#else
11203
0
      jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
11204
0
      jit_STORE_IP(jit, ir_ADD_OFFSET(jit_IP(jit), sizeof(zend_op)));
11205
0
#endif
11206
0
    }
11207
11208
0
    if (cold_path) {
11209
0
      ir_MERGE_WITH(cold_path);
11210
0
    }
11211
11212
0
    if (trace->op == ZEND_JIT_TRACE_BACK
11213
0
     && (!JIT_G(current_frame) || TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame)))) {
11214
0
      const zend_op *next_opline = trace->opline;
11215
11216
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
11217
0
       && (op1_info & MAY_BE_RC1)
11218
0
       && (op1_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
11219
        /* exception might be thrown during destruction of unused return value */
11220
        // JIT: if (EG(exception))
11221
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11222
0
      }
11223
0
      do {
11224
0
        trace++;
11225
0
      } while (trace->op == ZEND_JIT_TRACE_INIT_CALL);
11226
0
      ZEND_ASSERT(trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END);
11227
0
      next_opline = trace->opline;
11228
0
      ZEND_ASSERT(next_opline != NULL);
11229
11230
0
      if (trace->op == ZEND_JIT_TRACE_END
11231
0
       && trace->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
11232
0
        trace_info->flags |= ZEND_JIT_TRACE_LOOP;
11233
11234
0
        ir_ref if_eq = ir_IF(jit_CMP_IP(jit, IR_EQ, next_opline));
11235
11236
0
        ir_IF_TRUE(if_eq);
11237
0
        ZEND_ASSERT(jit->trace_loop_ref);
11238
0
        ZEND_ASSERT(jit->ctx.ir_base[jit->trace_loop_ref].op2 == IR_UNUSED);
11239
0
        ir_MERGE_SET_OP(jit->trace_loop_ref, 2, ir_END());
11240
0
        ir_IF_FALSE(if_eq);
11241
11242
#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE
11243
        ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
11244
#else
11245
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_escape));
11246
0
#endif
11247
0
      } else {
11248
0
        ir_GUARD(jit_CMP_IP(jit, IR_EQ, next_opline), jit_STUB_ADDR(jit, jit_stub_trace_escape));
11249
0
      }
11250
11251
0
      zend_jit_set_last_valid_opline(jit, trace->opline);
11252
11253
0
      return 1;
11254
0
    } else if (may_throw ||
11255
0
        (((opline->op1_type & (IS_VAR|IS_TMP_VAR))
11256
0
          && (op1_info & MAY_BE_RC1)
11257
0
          && (op1_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)))
11258
0
         && (!JIT_G(current_frame) || TRACE_FRAME_IS_RETURN_VALUE_UNUSED(JIT_G(current_frame))))) {
11259
      // JIT: if (EG(exception))
11260
0
      ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11261
0
    }
11262
11263
0
    return 1;
11264
0
  } else {
11265
    // JIT: if (EG(exception))
11266
0
    ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11267
    // JIT: opline = EX(opline) + 1
11268
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
11269
0
    jit_STORE_IP(jit, ir_ADD_OFFSET(jit_IP(jit), sizeof(zend_op)));
11270
0
  }
11271
11272
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
11273
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
11274
0
  } else {
11275
0
    zend_jit_vm_leave(jit, jit_IP(jit));
11276
0
  }
11277
11278
0
  jit->b = -1;
11279
11280
0
  return 1;
11281
0
}
11282
11283
static void zend_jit_common_return(zend_jit_ctx *jit)
11284
0
{
11285
0
  ZEND_ASSERT(jit->return_inputs);
11286
0
  ir_MERGE_list(jit->return_inputs);
11287
0
}
11288
11289
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)
11290
0
{
11291
0
  zend_jit_addr ret_addr;
11292
0
  int8_t return_value_used = -1;
11293
0
  ir_ref return_value = IR_UNUSED, ref, refcount, if_return_value_used = IR_UNUSED;
11294
11295
0
  ZEND_ASSERT(op_array->type != ZEND_EVAL_CODE && op_array->function_name);
11296
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF));
11297
11298
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
11299
0
    jit->return_inputs = IR_UNUSED;
11300
0
    if (JIT_G(current_frame)) {
11301
0
      if (TRACE_FRAME_IS_RETURN_VALUE_USED(JIT_G(current_frame))) {
11302
0
        return_value_used = 1;
11303
0
      } else if (TRACE_FRAME_IS_RETURN_VALUE_UNUSED(JIT_G(current_frame))) {
11304
0
        return_value_used = 0;
11305
0
      } else {
11306
0
        return_value_used = -1;
11307
0
      }
11308
0
    }
11309
0
  }
11310
11311
0
  if (ZEND_OBSERVER_ENABLED) {
11312
0
    if (Z_MODE(op1_addr) == IS_REG) {
11313
0
      zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
11314
11315
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, dst, op1_info)) {
11316
0
        return 0;
11317
0
      }
11318
0
      op1_addr = dst;
11319
0
    }
11320
0
    jit_observer_fcall_end(jit, jit_FP(jit), jit_ZVAL_ADDR(jit, op1_addr));
11321
0
  }
11322
11323
  // JIT: if (!EX(return_value))
11324
0
  return_value = ir_LOAD_A(jit_EX(return_value));
11325
0
  ret_addr = ZEND_ADDR_REF_ZVAL(return_value);
11326
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) &&
11327
0
      (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11328
0
    if (return_value_used == -1) {
11329
0
      if_return_value_used = ir_IF(return_value);
11330
0
      ir_IF_FALSE_cold(if_return_value_used);
11331
0
    }
11332
0
    if (return_value_used != 1) {
11333
0
      if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11334
0
        ir_ref if_refcounted = jit_if_REFCOUNTED(jit, op1_addr);
11335
0
        ir_IF_FALSE(if_refcounted);
11336
0
        ir_END_list(jit->return_inputs);
11337
0
        ir_IF_TRUE(if_refcounted);
11338
0
      }
11339
0
      ref = jit_Z_PTR(jit, op1_addr);
11340
0
      refcount = jit_GC_DELREF(jit, ref);
11341
11342
0
      if (RC_MAY_BE_1(op1_info)) {
11343
0
        if (RC_MAY_BE_N(op1_info)) {
11344
0
          ir_ref if_non_zero = ir_IF(refcount);
11345
0
          ir_IF_TRUE(if_non_zero);
11346
0
          ir_END_list(jit->return_inputs);
11347
0
          ir_IF_FALSE(if_non_zero);
11348
0
        }
11349
0
        jit_ZVAL_DTOR(jit, ref, op1_info, opline);
11350
0
      }
11351
0
      if (return_value_used == -1) {
11352
0
        ir_END_list(jit->return_inputs);
11353
0
      }
11354
0
    }
11355
0
  } else if (return_value_used == -1) {
11356
0
    if_return_value_used = ir_IF(return_value);
11357
0
    ir_IF_FALSE_cold(if_return_value_used);
11358
0
    ir_END_list(jit->return_inputs);
11359
0
  }
11360
11361
0
  if (if_return_value_used) {
11362
0
    ir_IF_TRUE(if_return_value_used);
11363
0
  }
11364
11365
0
  if (return_value_used == 0) {
11366
0
    if (jit->return_inputs) {
11367
0
      ZEND_ASSERT(JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE);
11368
0
      ir_END_list(jit->return_inputs);
11369
0
      ir_MERGE_list(jit->return_inputs);
11370
0
      jit->return_inputs = IR_UNUSED;
11371
0
    }
11372
0
    return 1;
11373
0
  }
11374
11375
0
  if (opline->op1_type == IS_CONST) {
11376
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
11377
11378
0
    jit_ZVAL_COPY_CONST(jit, ret_addr, MAY_BE_ANY, MAY_BE_ANY, zv, true);
11379
0
  } else if (opline->op1_type == IS_TMP_VAR) {
11380
0
    jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11381
0
  } else if (opline->op1_type == IS_CV) {
11382
0
    if (op1_info & MAY_BE_REF) {
11383
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
11384
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
11385
0
      op1_addr = ZEND_ADDR_REF_ZVAL(ref);
11386
0
    }
11387
11388
0
    if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
11389
0
      if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11390
0
          (op1_info & (MAY_BE_REF|MAY_BE_OBJECT)) ||
11391
0
          !op_array->function_name) {
11392
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, true);
11393
0
      } else if (return_value_used != 1) {
11394
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11395
        // JIT: if (EXPECTED(!(EX_CALL_INFO() & ZEND_CALL_CODE))) ZVAL_NULL(retval_ptr);
11396
0
        jit_set_Z_TYPE_INFO(jit, op1_addr, IS_NULL);
11397
0
      } else {
11398
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11399
0
      }
11400
0
    } else {
11401
0
      jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11402
0
    }
11403
0
  } else {
11404
0
    if (op1_info & MAY_BE_REF) {
11405
0
      ir_ref if_ref, ref2, if_non_zero;
11406
0
      zend_jit_addr ref_addr;
11407
11408
0
      if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
11409
0
      ir_IF_TRUE_cold(if_ref);
11410
11411
      // JIT: zend_refcounted *ref = Z_COUNTED_P(retval_ptr)
11412
0
      ref = jit_Z_PTR(jit, op1_addr);
11413
11414
      // JIT: ZVAL_COPY_VALUE(return_value, &ref->value)
11415
0
      ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
11416
0
      ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
11417
0
      jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, ref_addr, op1_info, false);
11418
0
      ref2 = jit_GC_DELREF(jit, ref);
11419
0
      if_non_zero = ir_IF(ref2);
11420
0
      ir_IF_TRUE(if_non_zero);
11421
11422
      // JIT: if (IS_REFCOUNTED())
11423
0
      ir_ref if_refcounted = jit_if_REFCOUNTED(jit, ret_addr);
11424
0
      ir_IF_FALSE(if_refcounted);
11425
0
      ir_END_list(jit->return_inputs);
11426
0
      ir_IF_TRUE(if_refcounted);
11427
11428
      // JIT: ADDREF
11429
0
      ref2 = jit_Z_PTR(jit, ret_addr);
11430
0
      jit_GC_ADDREF(jit, ref2);
11431
0
      ir_END_list(jit->return_inputs);
11432
11433
0
      ir_IF_FALSE(if_non_zero);
11434
11435
0
      jit_EFREE(jit, ref, sizeof(zend_reference), op_array, opline);
11436
0
      ir_END_list(jit->return_inputs);
11437
11438
0
      ir_IF_FALSE(if_ref);
11439
0
    }
11440
0
    jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11441
0
  }
11442
11443
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
11444
0
    if (jit->return_inputs) {
11445
0
      ir_END_list(jit->return_inputs);
11446
0
      ir_MERGE_list(jit->return_inputs);
11447
0
      jit->return_inputs = IR_UNUSED;
11448
0
    }
11449
0
  } else {
11450
0
    ir_END_list(jit->return_inputs);
11451
0
    jit->b = -1;
11452
0
  }
11453
11454
0
  return 1;
11455
0
}
11456
11457
static int zend_jit_bind_global(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
11458
0
{
11459
0
  zend_jit_addr op1_addr = OP1_ADDR();
11460
0
  zend_string *varname = Z_STR_P(RT_CONSTANT(opline, opline->op2));
11461
0
  ir_ref cache_slot_ref, idx_ref, num_used_ref, bucket_ref, ref, ref2;
11462
0
  ir_ref if_fit, if_reference, if_same_key, fast_path;
11463
0
  ir_ref slow_inputs = IR_UNUSED, end_inputs = IR_UNUSED;
11464
11465
  // JIT: idx = (uintptr_t)CACHED_PTR(opline->extended_value) - 1;
11466
0
  cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value);
11467
0
  idx_ref = ir_SUB_A(ir_LOAD_A(cache_slot_ref), ir_CONST_ADDR(1));
11468
11469
  // JIT: if (EXPECTED(idx < EG(symbol_table).nNumUsed * sizeof(Bucket)))
11470
0
  num_used_ref = ir_MUL_U32(ir_LOAD_U32(jit_EG(symbol_table.nNumUsed)),
11471
0
    ir_CONST_U32(sizeof(Bucket)));
11472
0
  if (sizeof(void*) == 8) {
11473
0
    num_used_ref = ir_ZEXT_A(num_used_ref);
11474
0
  }
11475
0
  if_fit = ir_IF(ir_ULT(idx_ref, num_used_ref));
11476
0
  ir_IF_FALSE_cold(if_fit);
11477
0
  ir_END_list(slow_inputs);
11478
0
  ir_IF_TRUE(if_fit);
11479
11480
  // JIT: Bucket *p = (Bucket*)((char*)EG(symbol_table).arData + idx);
11481
0
  bucket_ref = ir_ADD_A(ir_LOAD_A(jit_EG(symbol_table.arData)), idx_ref);
11482
0
  if_reference = jit_if_Z_TYPE_ref(jit, bucket_ref, ir_CONST_U8(IS_REFERENCE));
11483
0
  ir_IF_FALSE_cold(if_reference);
11484
0
  ir_END_list(slow_inputs);
11485
0
  ir_IF_TRUE(if_reference);
11486
11487
  // JIT: (EXPECTED(p->key == varname))
11488
0
  if_same_key = ir_IF(ir_EQ(ir_LOAD_A(ir_ADD_OFFSET(bucket_ref, offsetof(Bucket, key))), ir_CONST_ADDR(varname)));
11489
0
  ir_IF_FALSE_cold(if_same_key);
11490
0
  ir_END_list(slow_inputs);
11491
0
  ir_IF_TRUE(if_same_key);
11492
11493
  // JIT: GC_ADDREF(Z_PTR(p->val))
11494
0
  ref = jit_Z_PTR_ref(jit, bucket_ref);
11495
0
  jit_GC_ADDREF(jit, ref);
11496
11497
0
  fast_path = ir_END();
11498
0
  ir_MERGE_list(slow_inputs);
11499
11500
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_global_helper),
11501
0
    ir_CONST_ADDR(varname),
11502
0
    cache_slot_ref);
11503
11504
0
  ir_MERGE_WITH(fast_path);
11505
0
  ref = ir_PHI_2(IR_ADDR, ref2, ref);
11506
11507
0
  if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11508
0
    ir_ref if_refcounted = IR_UNUSED, refcount, if_non_zero, if_may_not_leak;
11509
11510
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11511
      // JIT: if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr)))
11512
0
      if_refcounted = jit_if_REFCOUNTED(jit, op1_addr);
11513
0
      ir_IF_TRUE_cold(if_refcounted);
11514
0
    }
11515
11516
    // JIT:zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
11517
0
    ref2 = jit_Z_PTR(jit, op1_addr);
11518
11519
    // JIT: ZVAL_REF(variable_ptr, ref)
11520
0
    jit_set_Z_PTR(jit, op1_addr, ref);
11521
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
11522
11523
    // JIT: if (GC_DELREF(garbage) == 0)
11524
0
    refcount = jit_GC_DELREF(jit, ref2);
11525
0
    if_non_zero = ir_IF(refcount);
11526
0
    if (!(op1_info & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT))) {
11527
0
      ir_IF_TRUE(if_non_zero);
11528
0
      ir_END_list(end_inputs);
11529
0
    }
11530
0
    ir_IF_FALSE(if_non_zero);
11531
11532
0
    jit_ZVAL_DTOR(jit, ref2, op1_info, opline);
11533
0
    if (op1_info & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT)) {
11534
0
      ir_END_list(end_inputs);
11535
0
      ir_IF_TRUE(if_non_zero);
11536
11537
      // JIT: GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr)
11538
0
      if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref2);
11539
0
      ir_IF_TRUE(if_may_not_leak);
11540
0
      ir_END_list(end_inputs);
11541
0
      ir_IF_FALSE(if_may_not_leak);
11542
0
      if (opline) {
11543
0
        jit_SET_EX_OPLINE(jit, opline);
11544
0
      }
11545
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref2);
11546
0
    }
11547
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11548
0
      ir_END_list(end_inputs);
11549
0
      ir_IF_FALSE(if_refcounted);
11550
0
    }
11551
0
  }
11552
11553
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11554
    // JIT: ZVAL_REF(variable_ptr, ref)
11555
0
    jit_set_Z_PTR(jit, op1_addr, ref);
11556
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
11557
0
  }
11558
11559
0
  if (end_inputs) {
11560
0
    ir_END_list(end_inputs);
11561
0
    ir_MERGE_list(end_inputs);
11562
0
  }
11563
11564
0
  return 1;
11565
0
}
11566
11567
static int zend_jit_free(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, int may_throw)
11568
0
{
11569
0
  zend_jit_addr op1_addr = OP1_ADDR();
11570
11571
0
  if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11572
0
    if (may_throw) {
11573
0
      jit_SET_EX_OPLINE(jit, opline);
11574
0
    }
11575
0
    if (opline->opcode == ZEND_FE_FREE && (op1_info & (MAY_BE_OBJECT|MAY_BE_REF))) {
11576
0
      ir_ref ref, if_array, if_exists, end_inputs = IR_UNUSED;
11577
11578
0
      if (op1_info & MAY_BE_ARRAY) {
11579
0
        if_array = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
11580
0
        ir_IF_TRUE(if_array);
11581
0
        ir_END_list(end_inputs);
11582
0
        ir_IF_FALSE(if_array);
11583
0
      }
11584
0
      ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_iter_idx)));
11585
0
      if_exists = ir_IF(ir_EQ(ref, ir_CONST_U32(-1)));
11586
0
      ir_IF_TRUE(if_exists);
11587
0
      ir_END_list(end_inputs);
11588
0
      ir_IF_FALSE(if_exists);
11589
11590
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_hash_iterator_del), ref);
11591
11592
0
      ir_END_list(end_inputs);
11593
0
      ir_MERGE_list(end_inputs);
11594
0
    }
11595
11596
0
    jit_ZVAL_PTR_DTOR(jit, op1_addr, op1_info, false, opline);
11597
11598
0
    if (may_throw) {
11599
0
      zend_jit_check_exception(jit);
11600
0
    }
11601
0
  }
11602
11603
0
  return 1;
11604
0
}
11605
11606
static int zend_jit_echo(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
11607
0
{
11608
0
  if (opline->op1_type == IS_CONST) {
11609
0
    zval *zv;
11610
0
    size_t len;
11611
11612
0
    zv = RT_CONSTANT(opline, opline->op1);
11613
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11614
0
    len = Z_STRLEN_P(zv);
11615
11616
0
    if (len > 0) {
11617
0
      const char *str = Z_STRVAL_P(zv);
11618
11619
0
      jit_SET_EX_OPLINE(jit, opline);
11620
0
      ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_write),
11621
0
        ir_CONST_ADDR(str), ir_CONST_ADDR(len));
11622
11623
0
      zend_jit_check_exception(jit);
11624
0
    }
11625
0
  } else {
11626
0
    zend_jit_addr op1_addr = OP1_ADDR();
11627
0
    ir_ref ref;
11628
11629
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11630
11631
0
    jit_SET_EX_OPLINE(jit, opline);
11632
11633
0
    ref = jit_Z_PTR(jit, op1_addr);
11634
0
    ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_write),
11635
0
      ir_ADD_OFFSET(ref, offsetof(zend_string, val)),
11636
0
      ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_string, len))));
11637
11638
0
    if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
11639
0
      jit_ZVAL_PTR_DTOR(jit, op1_addr, op1_info, false, opline);
11640
0
    }
11641
11642
0
    zend_jit_check_exception(jit);
11643
0
  }
11644
0
  return 1;
11645
0
}
11646
11647
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)
11648
0
{
11649
0
  if (opline->op1_type == IS_CONST) {
11650
0
    zval *zv;
11651
0
    size_t len;
11652
11653
0
    zv = RT_CONSTANT(opline, opline->op1);
11654
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11655
0
    len = Z_STRLEN_P(zv);
11656
11657
0
    jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(len));
11658
0
    if (Z_MODE(res_addr) != IS_REG) {
11659
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11660
0
    } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11661
0
      return 0;
11662
0
    }
11663
0
  } else {
11664
0
    ir_ref ref;
11665
11666
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11667
11668
0
    ref = jit_Z_PTR(jit, op1_addr);
11669
0
    ref = ir_LOAD_L(ir_ADD_OFFSET(ref, offsetof(zend_string, len)));
11670
0
    jit_set_Z_LVAL(jit, res_addr, ref);
11671
11672
0
    if (Z_MODE(res_addr) == IS_REG) {
11673
0
      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
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11678
0
    }
11679
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
11680
0
  }
11681
0
  return 1;
11682
0
}
11683
11684
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)
11685
0
{
11686
0
  if (opline->op1_type == IS_CONST) {
11687
0
    zval *zv;
11688
0
    zend_long count;
11689
11690
0
    zv = RT_CONSTANT(opline, opline->op1);
11691
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
11692
0
    count = zend_hash_num_elements(Z_ARRVAL_P(zv));
11693
11694
0
    jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(count));
11695
0
    if (Z_MODE(res_addr) != IS_REG) {
11696
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11697
0
    } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11698
0
      return 0;
11699
0
    }
11700
0
  } else {
11701
0
    ir_ref ref;
11702
11703
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_ARRAY);
11704
    // Note: See the implementation of ZEND_COUNT in Zend/zend_vm_def.h - arrays do not contain IS_UNDEF starting in php 8.1+.
11705
11706
0
    ref = jit_Z_PTR(jit, op1_addr);
11707
0
    if (sizeof(void*) == 8) {
11708
0
      ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(HashTable, nNumOfElements)));
11709
0
      ref = ir_ZEXT_L(ref);
11710
0
    } else {
11711
0
      ref = ir_LOAD_L(ir_ADD_OFFSET(ref, offsetof(HashTable, nNumOfElements)));
11712
0
    }
11713
0
    jit_set_Z_LVAL(jit, res_addr, ref);
11714
11715
0
    if (Z_MODE(res_addr) == IS_REG) {
11716
0
      if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11717
0
        return 0;
11718
0
      }
11719
0
    } else {
11720
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11721
0
    }
11722
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
11723
0
  }
11724
11725
0
  if (may_throw) {
11726
0
    zend_jit_check_exception(jit);
11727
0
  }
11728
0
  return 1;
11729
0
}
11730
11731
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)
11732
0
{
11733
0
  HashTable *ht = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
11734
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
11735
0
  ir_ref ref;
11736
11737
0
  ZEND_ASSERT(opline->op1_type != IS_VAR && opline->op1_type != IS_TMP_VAR);
11738
0
  ZEND_ASSERT((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == MAY_BE_STRING);
11739
11740
  // JIT: result = zend_hash_find_ex(ht, Z_STR_P(op1), OP1_TYPE == IS_CONST);
11741
0
  if (opline->op1_type != IS_CONST) {
11742
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find),
11743
0
      ir_CONST_ADDR(ht),
11744
0
      jit_Z_PTR(jit, op1_addr));
11745
0
  } else {
11746
0
    zend_string *str = Z_STR_P(RT_CONSTANT(opline, opline->op1));
11747
11748
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash),
11749
0
      ir_CONST_ADDR(ht), ir_CONST_ADDR(str));
11750
0
  }
11751
11752
0
  if (exit_addr) {
11753
0
    if (smart_branch_opcode == ZEND_JMPZ) {
11754
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
11755
0
    } else {
11756
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
11757
0
    }
11758
0
  } else if (smart_branch_opcode) {
11759
0
    zend_basic_block *bb;
11760
11761
0
    ZEND_ASSERT(jit->b >= 0);
11762
0
    bb = &jit->ssa->cfg.blocks[jit->b];
11763
0
    ZEND_ASSERT(bb->successors_count == 2);
11764
0
    ref = jit_IF_ex(jit, ref,
11765
0
      (smart_branch_opcode == ZEND_JMPZ) ? target_label2 : target_label);
11766
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
11767
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
11768
0
    jit->b = -1;
11769
0
  } else {
11770
0
    jit_set_Z_TYPE_INFO_ex(jit, res_addr,
11771
0
      ir_ADD_U32(ir_ZEXT_U32(ir_NE(ref, IR_NULL)), ir_CONST_U32(IS_FALSE)));
11772
0
  }
11773
11774
0
  return 1;
11775
0
}
11776
11777
static int zend_jit_rope(zend_jit_ctx *jit, const zend_op *opline, uint32_t op2_info)
11778
0
{
11779
0
  uint32_t offset;
11780
11781
0
  offset = (opline->opcode == ZEND_ROPE_INIT) ?
11782
0
    opline->result.var :
11783
0
    opline->op1.var + opline->extended_value * sizeof(zend_string*);
11784
11785
0
  if (opline->op2_type == IS_CONST) {
11786
0
    zval *zv = RT_CONSTANT(opline, opline->op2);
11787
0
    zend_string *str;
11788
11789
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11790
0
    str = Z_STR_P(zv);
11791
11792
0
    ir_STORE(ir_ADD_OFFSET(jit_FP(jit), offset), ir_CONST_ADDR(str));
11793
0
  } else {
11794
0
    zend_jit_addr op2_addr = OP2_ADDR();
11795
0
    ir_ref ref;
11796
11797
0
    ZEND_ASSERT((op2_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11798
11799
0
    ref = jit_Z_PTR(jit, op2_addr);
11800
0
    ir_STORE(ir_ADD_OFFSET(jit_FP(jit), offset), ref);
11801
0
    if (opline->op2_type == IS_CV) {
11802
0
      ir_ref if_refcounted, long_path;
11803
11804
0
      if_refcounted = jit_if_REFCOUNTED(jit, op2_addr);
11805
0
      ir_IF_TRUE(if_refcounted);
11806
0
      jit_GC_ADDREF(jit, ref);
11807
0
      long_path = ir_END();
11808
11809
0
      ir_IF_FALSE(if_refcounted);
11810
0
      ir_MERGE_WITH(long_path);
11811
0
    }
11812
0
  }
11813
11814
0
  if (opline->opcode == ZEND_ROPE_END) {
11815
0
    zend_jit_addr res_addr = RES_ADDR();
11816
0
    ir_ref ref;
11817
11818
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_rope_end),
11819
0
      ir_ADD_OFFSET(jit_FP(jit), opline->op1.var),
11820
0
      ir_CONST_U32(opline->extended_value));
11821
11822
0
    jit_set_Z_PTR(jit, res_addr, ref);
11823
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING_EX);
11824
0
  }
11825
11826
0
  return 1;
11827
0
}
11828
11829
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)
11830
0
{
11831
0
  ir_ref if_type, val;
11832
11833
0
  if (res_info == MAY_BE_LONG) {
11834
0
    if_type = ir_IF(ir_EQ(type, ir_CONST_U32(IS_LONG)));
11835
0
    ir_IF_TRUE(if_type);
11836
0
    val = jit_ZVAL_ADDR(jit, val_addr);
11837
0
    ir_END_PHI_list(*values, val);
11838
0
    ir_IF_FALSE(if_type);
11839
0
    val = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
11840
0
    ir_END_PHI_list(*values, val);
11841
0
  } else if (res_info == MAY_BE_DOUBLE) {
11842
0
    if_type = ir_IF(ir_EQ(type, ir_CONST_U32(IS_DOUBLE)));
11843
0
    ir_IF_TRUE(if_type);
11844
0
    val = jit_ZVAL_ADDR(jit, val_addr);
11845
0
    ir_END_PHI_list(*values, val);
11846
0
    ir_IF_FALSE(if_type);
11847
0
    val = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
11848
0
    ir_END_PHI_list(*values, val);
11849
0
  } else {
11850
0
    ZEND_UNREACHABLE();
11851
0
  }
11852
0
  return 1;
11853
0
}
11854
11855
static int zend_jit_zval_copy_deref(zend_jit_ctx *jit, zend_jit_addr res_addr, zend_jit_addr val_addr, ir_ref type)
11856
0
{
11857
0
  ir_ref if_refcounted, if_reference, if_refcounted2, ptr, val2, ptr2, type2;
11858
0
  ir_refs *merge_inputs, *types, *ptrs;
11859
#if SIZEOF_ZEND_LONG == 4
11860
  ir_ref val = jit_ZVAL_ADDR(jit, val_addr);
11861
  ir_refs *values; /* we need this only for zval.w2 copy */
11862
#endif
11863
11864
0
  ir_refs_init(merge_inputs, 4);
11865
0
  ir_refs_init(types, 4);
11866
0
  ir_refs_init(ptrs, 4);
11867
#if SIZEOF_ZEND_LONG == 4
11868
  ir_refs_init(values, 4);
11869
#endif
11870
11871
  // JIT: ptr = Z_PTR_P(val);
11872
0
  ptr = jit_Z_PTR(jit, val_addr);
11873
11874
  // JIT: if (Z_OPT_REFCOUNTED_P(val)) {
11875
0
  if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(Z_TYPE_FLAGS_MASK)));
11876
0
  ir_IF_FALSE_cold(if_refcounted);
11877
0
  ir_refs_add(merge_inputs, ir_END());
11878
0
  ir_refs_add(types, type);
11879
0
  ir_refs_add(ptrs, ptr);
11880
#if SIZEOF_ZEND_LONG == 4
11881
  ir_refs_add(values, val);
11882
#endif
11883
11884
0
  ir_IF_TRUE(if_refcounted);
11885
11886
  // JIT: if (UNEXPECTED(Z_OPT_ISREF_P(val))) {
11887
0
  if_reference = ir_IF(ir_EQ(type, ir_CONST_U32(IS_REFERENCE_EX)));
11888
//  if_reference = ir_IF(ir_EQ(ir_TRUNC_U8(type), ir_CONST_U8(IS_REFERENCE))); // TODO: fix IR to avoid need for extra register ???
11889
0
  ir_IF_TRUE(if_reference);
11890
11891
  // JIT: val = Z_REFVAL_P(val);
11892
0
  val2 = ir_ADD_OFFSET(ptr, offsetof(zend_reference, val));
11893
0
  type2 = jit_Z_TYPE_INFO_ref(jit, val2);
11894
0
  ptr2 = jit_Z_PTR_ref(jit, val2);
11895
11896
  // JIT: if (Z_OPT_REFCOUNTED_P(val)) {
11897
0
  if_refcounted2 = ir_IF(ir_AND_U32(type2, ir_CONST_U32(Z_TYPE_FLAGS_MASK)));
11898
0
  ir_IF_FALSE_cold(if_refcounted2);
11899
0
  ir_refs_add(merge_inputs, ir_END());
11900
0
  ir_refs_add(types, type2);
11901
0
  ir_refs_add(ptrs, ptr2);
11902
#if SIZEOF_ZEND_LONG == 4
11903
  ir_refs_add(values, val2);
11904
#endif
11905
11906
0
  ir_IF_TRUE(if_refcounted2);
11907
0
  ir_MERGE_WITH_EMPTY_FALSE(if_reference);
11908
0
  type = ir_PHI_2(IR_U32, type2, type);
11909
0
  ptr = ir_PHI_2(IR_ADDR, ptr2, ptr);
11910
#if SIZEOF_ZEND_LONG == 4
11911
  val = ir_PHI_2(IR_ADDR, val2, val);
11912
#endif
11913
11914
  // JIT: Z_ADDREF_P(val);
11915
0
  jit_GC_ADDREF(jit, ptr);
11916
0
  ir_refs_add(merge_inputs, ir_END());
11917
0
  ir_refs_add(types, type);
11918
0
  ir_refs_add(ptrs, ptr);
11919
#if SIZEOF_ZEND_LONG == 4
11920
  ir_refs_add(values, val);
11921
#endif
11922
11923
0
  ir_MERGE_N(merge_inputs->count, merge_inputs->refs);
11924
0
  type = ir_PHI_N(IR_U32, types->count, types->refs);
11925
0
  ptr = ir_PHI_N(IR_ADDR, ptrs->count, ptrs->refs);
11926
#if SIZEOF_ZEND_LONG == 4
11927
  val = ir_PHI_N(IR_ADDR, values->count, values->refs);
11928
  val_addr = ZEND_ADDR_REF_ZVAL(val);
11929
#endif
11930
11931
  // JIT: Z_PTR_P(res) = ptr;
11932
0
  jit_set_Z_PTR(jit, res_addr, ptr);
11933
#if SIZEOF_ZEND_LONG == 4
11934
  jit_set_Z_W2(jit, res_addr, jit_Z_W2(jit, val_addr));
11935
#endif
11936
0
  jit_set_Z_TYPE_INFO_ex(jit, res_addr, type);
11937
11938
0
  return 1;
11939
0
}
11940
11941
static int zend_jit_fetch_dimension_address_inner(zend_jit_ctx  *jit,
11942
                                                  const zend_op *opline,
11943
                                                  uint32_t       type,
11944
                                                  uint32_t       op1_info,
11945
                                                  uint32_t       op2_info,
11946
                                                  zend_jit_addr  op2_addr,
11947
                                                  zend_ssa_range *op2_range,
11948
                                                  uint8_t        dim_type,
11949
                                                  const void    *found_exit_addr,
11950
                                                  const void    *not_found_exit_addr,
11951
                                                  const void    *exit_addr,
11952
                                                  bool           result_type_guard,
11953
                                                  ir_ref         ht_ref,
11954
                                                  ir_refs       *found_inputs,
11955
                                                  ir_refs       *found_vals,
11956
                                                  ir_ref        *end_inputs,
11957
                                                  ir_ref        *not_found_inputs)
11958
0
{
11959
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
11960
0
  ir_ref ref = IR_UNUSED, cond, if_found;
11961
0
  ir_ref if_type = IS_UNUSED;
11962
0
  ir_refs *test_zval_inputs, *test_zval_values;
11963
11964
0
  ir_refs_init(test_zval_inputs, 4);
11965
0
  ir_refs_init(test_zval_values, 4);
11966
11967
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
11968
0
   && type == BP_VAR_R
11969
0
   && !exit_addr) {
11970
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
11971
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
11972
0
    if (!exit_addr) {
11973
0
      return 0;
11974
0
    }
11975
0
  }
11976
11977
0
  if (op2_info & MAY_BE_LONG) {
11978
0
    bool op2_loaded = false;
11979
0
    bool packed_loaded = false;
11980
0
    bool bad_packed_key = false;
11981
0
    ir_ref if_packed = IS_UNDEF;
11982
0
    ir_ref h = IR_UNUSED;
11983
0
    ir_ref idx_not_found_inputs = IR_UNUSED;
11984
11985
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_LONG)) {
11986
      // JIT: if (EXPECTED(Z_TYPE_P(dim) == IS_LONG))
11987
0
      if_type = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
11988
0
      ir_IF_TRUE(if_type);
11989
0
    }
11990
0
    if (op1_info & MAY_BE_PACKED_GUARD) {
11991
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_PACKED_GUARD);
11992
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
11993
11994
0
      if (!exit_addr) {
11995
0
        return 0;
11996
0
      }
11997
0
      cond = ir_AND_U32(
11998
0
        ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
11999
0
        ir_CONST_U32(HASH_FLAG_PACKED));
12000
0
      if (op1_info & MAY_BE_ARRAY_PACKED) {
12001
0
        ir_GUARD(cond, ir_CONST_ADDR(exit_addr));
12002
0
      } else {
12003
0
        ir_GUARD_NOT(cond, ir_CONST_ADDR(exit_addr));
12004
0
      }
12005
0
    }
12006
0
    if (type == BP_VAR_W) {
12007
      // JIT: hval = Z_LVAL_P(dim);
12008
0
      h = jit_Z_LVAL(jit, op2_addr);
12009
0
      op2_loaded = true;
12010
0
    }
12011
0
    if (op1_info & MAY_BE_ARRAY_PACKED) {
12012
0
      zend_long val = -1;
12013
12014
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
12015
0
        val = Z_LVAL_P(Z_ZV(op2_addr));
12016
0
        if (val >= 0 && val < HT_MAX_SIZE) {
12017
0
          packed_loaded = true;
12018
0
        } else {
12019
0
          bad_packed_key = true;
12020
0
        }
12021
0
        h = ir_CONST_LONG(val);
12022
0
      } else {
12023
0
        if (!op2_loaded) {
12024
          // JIT: hval = Z_LVAL_P(dim);
12025
0
          h = jit_Z_LVAL(jit, op2_addr);
12026
0
          op2_loaded = true;
12027
0
        }
12028
0
        packed_loaded = true;
12029
0
      }
12030
12031
0
      if (dim_type == IS_UNDEF && type == BP_VAR_W && packed_loaded) {
12032
        /* don't generate "fast" code for packed array */
12033
0
        packed_loaded = false;
12034
0
      }
12035
12036
0
      if (packed_loaded) {
12037
        // JIT: ZEND_HASH_INDEX_FIND(ht, hval, retval, num_undef);
12038
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12039
0
          if_packed = ir_IF(
12040
0
            ir_AND_U32(
12041
0
              ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
12042
0
              ir_CONST_U32(HASH_FLAG_PACKED)));
12043
0
          ir_IF_TRUE(if_packed);
12044
0
        }
12045
        // JIT: if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed))
12046
0
        ref = ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed)));
12047
0
#if SIZEOF_ZEND_LONG == 8
12048
0
        if ((Z_MODE(op2_addr) == IS_CONST_ZVAL && val >= 0 && val <= UINT32_MAX)
12049
0
         || (op2_range && op2_range->min >= 0 && op2_range->max <= UINT32_MAX)) {
12050
          /* comapre only the lower 32-bits to allow load fusion on x86_64 */
12051
0
          cond = ir_ULT(ir_TRUNC_U32(h), ref);
12052
0
        } else {
12053
0
          cond = ir_ULT(h, ir_ZEXT_L(ref));
12054
0
        }
12055
#else
12056
        cond = ir_ULT(h, ref);
12057
#endif
12058
0
        if (type == BP_JIT_IS) {
12059
0
          if (not_found_exit_addr) {
12060
0
            ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12061
0
          } else {
12062
0
            ir_ref if_fit = ir_IF(cond);
12063
0
            ir_IF_FALSE(if_fit);
12064
0
            ir_END_list(*end_inputs);
12065
0
            ir_IF_TRUE(if_fit);
12066
0
          }
12067
0
        } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12068
0
          ir_GUARD(cond, ir_CONST_ADDR(exit_addr));
12069
0
        } else if (type == BP_VAR_IS && not_found_exit_addr) {
12070
0
          ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12071
0
        } else if (type == BP_VAR_RW && not_found_exit_addr) {
12072
0
          ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12073
0
        } else if (type == BP_VAR_IS && result_type_guard) {
12074
0
          ir_ref if_fit = ir_IF(cond);
12075
0
          ir_IF_FALSE(if_fit);
12076
0
          ir_END_list(*not_found_inputs);
12077
0
          ir_IF_TRUE(if_fit);
12078
0
        } else {
12079
0
          ir_ref if_fit = ir_IF(cond);
12080
0
          ir_IF_FALSE(if_fit);
12081
0
          ir_END_list(idx_not_found_inputs);
12082
0
          ir_IF_TRUE(if_fit);
12083
0
        }
12084
        // JIT: _ret = &_ht->arPacked[h];
12085
0
        ref = ir_MUL_L(h, ir_CONST_LONG(sizeof(zval)));
12086
0
        ref = ir_BITCAST_A(ref);
12087
0
        ref = ir_ADD_A(ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arPacked))), ref);
12088
0
        if (type == BP_JIT_IS) {
12089
0
          ir_refs_add(test_zval_values, ref);
12090
0
          ir_refs_add(test_zval_inputs, ir_END());
12091
0
        }
12092
0
      }
12093
0
    }
12094
0
    switch (type) {
12095
0
      case BP_JIT_IS:
12096
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12097
0
          if (if_packed) {
12098
0
            ir_IF_FALSE(if_packed);
12099
0
            if_packed = IR_UNUSED;
12100
0
          }
12101
0
          if (!op2_loaded) {
12102
            // JIT: hval = Z_LVAL_P(dim);
12103
0
            h = jit_Z_LVAL(jit, op2_addr);
12104
0
            op2_loaded = true;
12105
0
          }
12106
0
          if (packed_loaded) {
12107
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(_zend_hash_index_find), ht_ref, h);
12108
0
          } else {
12109
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_find), ht_ref, h);
12110
0
          }
12111
0
          if (not_found_exit_addr) {
12112
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12113
0
          } else {
12114
0
            if_found = ir_IF(ref);
12115
0
            ir_IF_FALSE(if_found);
12116
0
            ir_END_list(*end_inputs);
12117
0
            ir_IF_TRUE(if_found);
12118
0
          }
12119
0
          ir_refs_add(test_zval_values, ref);
12120
0
          ir_refs_add(test_zval_inputs, ir_END());
12121
0
        } else if (!not_found_exit_addr && !packed_loaded) {
12122
0
          ir_END_list(*end_inputs);
12123
0
        }
12124
0
        break;
12125
0
      case BP_VAR_R:
12126
0
      case BP_VAR_IS:
12127
0
      case BP_VAR_UNSET:
12128
0
        if (packed_loaded) {
12129
0
          ir_ref type_ref = jit_Z_TYPE_ref(jit, ref);
12130
12131
0
          if (result_type_guard) {
12132
            /* perform IS_UNDEF check only after result type guard (during deoptimization) */
12133
0
          } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12134
0
            ir_GUARD(type_ref, ir_CONST_ADDR(exit_addr));
12135
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12136
0
            ir_GUARD(type_ref, ir_CONST_ADDR(not_found_exit_addr));
12137
0
          } else {
12138
0
            ir_ref if_def = ir_IF(type_ref);
12139
0
            ir_IF_FALSE(if_def);
12140
0
            ir_END_list(idx_not_found_inputs);
12141
0
            ir_IF_TRUE(if_def);
12142
0
          }
12143
0
          ir_refs_add(found_inputs, ir_END());
12144
0
          ir_refs_add(found_vals, ref);
12145
0
        }
12146
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12147
0
          if (if_packed) {
12148
0
            ir_IF_FALSE(if_packed);
12149
0
            if_packed = IR_UNUSED;
12150
0
          }
12151
0
          if (!op2_loaded) {
12152
            // JIT: hval = Z_LVAL_P(dim);
12153
0
            h = jit_Z_LVAL(jit, op2_addr);
12154
0
            op2_loaded = true;
12155
0
          }
12156
0
          if (packed_loaded) {
12157
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(_zend_hash_index_find), ht_ref, h);
12158
0
          } else {
12159
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_find), ht_ref, h);
12160
0
          }
12161
0
          if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12162
0
            ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
12163
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12164
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12165
0
          } else if (type == BP_VAR_IS && result_type_guard) {
12166
0
            if_found = ir_IF(ref);
12167
0
            ir_IF_FALSE(if_found);
12168
0
            ir_END_list(*not_found_inputs);
12169
0
            ir_IF_TRUE(if_found);
12170
0
          } else {
12171
0
            if_found = ir_IF(ref);
12172
0
            ir_IF_FALSE(if_found);
12173
0
            ir_END_list(idx_not_found_inputs);
12174
0
            ir_IF_TRUE(if_found);
12175
0
          }
12176
0
          ir_refs_add(found_inputs, ir_END());
12177
0
          ir_refs_add(found_vals, ref);
12178
0
        } else if (!packed_loaded) {
12179
0
          if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12180
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
12181
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12182
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(not_found_exit_addr));
12183
0
          } else if (type == BP_VAR_IS && result_type_guard) {
12184
0
            ir_END_list(*not_found_inputs);
12185
0
          } else {
12186
0
            ir_END_list(idx_not_found_inputs);
12187
0
          }
12188
0
        }
12189
12190
0
        if (idx_not_found_inputs) {
12191
0
          ir_MERGE_list(idx_not_found_inputs);
12192
0
          switch (type) {
12193
0
            case BP_VAR_R:
12194
0
              ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
12195
              // JIT: zend_error(E_WARNING,"Undefined array key " ZEND_LONG_FMT, hval);
12196
              // JIT: retval = &EG(uninitialized_zval);
12197
0
              jit_SET_EX_OPLINE(jit, opline);
12198
0
              if (Z_MODE(op2_addr) == IS_REG) {
12199
0
                if (!op2_loaded) {
12200
                  // JIT: hval = Z_LVAL_P(dim);
12201
0
                  h = jit_Z_LVAL(jit, op2_addr);
12202
0
                }
12203
0
                if (GCC_GLOBAL_REGS) {
12204
0
                  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key_ex), h);
12205
0
                } else {
12206
0
                  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key_ex), h, jit_FP(jit));
12207
0
                }
12208
0
              } else {
12209
0
                ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_undefined_offset, IR_FASTCALL_FUNC));
12210
0
              }
12211
0
              ir_END_list(*end_inputs);
12212
0
              break;
12213
0
            case BP_VAR_IS:
12214
0
            case BP_VAR_UNSET:
12215
0
              if (!not_found_exit_addr) {
12216
                // JIT: retval = &EG(uninitialized_zval);
12217
0
                jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12218
0
                ir_END_list(*end_inputs);
12219
0
              }
12220
0
              break;
12221
0
            default:
12222
0
              ZEND_UNREACHABLE();
12223
0
          }
12224
0
                }
12225
0
        break;
12226
0
      case BP_VAR_RW:
12227
0
        if (packed_loaded) {
12228
0
          if (not_found_exit_addr) {
12229
0
            ir_refs_add(found_inputs, ir_END());
12230
0
            ir_refs_add(found_vals, ref);
12231
0
          } else {
12232
0
            ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
12233
0
            ir_IF_TRUE(if_def);
12234
0
            ir_refs_add(found_inputs, ir_END());
12235
0
            ir_refs_add(found_vals, ref);
12236
0
            ir_IF_FALSE_cold(if_def);
12237
0
            ir_END_list(idx_not_found_inputs);
12238
0
          }
12239
0
        }
12240
0
        if (!packed_loaded ||
12241
0
            !not_found_exit_addr ||
12242
0
            (op1_info & MAY_BE_ARRAY_NUMERIC_HASH)) {
12243
0
          if (if_packed) {
12244
0
            ir_IF_FALSE(if_packed);
12245
0
            if_packed = IR_UNUSED;
12246
0
            ir_END_list(idx_not_found_inputs);
12247
0
          } else if (!packed_loaded) {
12248
0
            ir_END_list(idx_not_found_inputs);
12249
0
          }
12250
12251
0
          ir_MERGE_list(idx_not_found_inputs);
12252
0
          if (!op2_loaded) {
12253
            // JIT: hval = Z_LVAL_P(dim);
12254
0
            h = jit_Z_LVAL(jit, op2_addr);
12255
0
          }
12256
0
          if (packed_loaded) {
12257
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_index_lookup_rw_no_packed),
12258
0
              ht_ref, h);
12259
0
          } else {
12260
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_index_lookup_rw), ht_ref, h);
12261
0
          }
12262
0
          if (not_found_exit_addr) {
12263
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12264
0
          } else {
12265
0
            if_found = ir_IF(ref);
12266
0
            ir_IF_FALSE(if_found);
12267
0
            ir_END_list(*end_inputs);
12268
0
            ir_IF_TRUE(if_found);
12269
0
          }
12270
0
          ir_refs_add(found_inputs, ir_END());
12271
0
          ir_refs_add(found_vals, ref);
12272
0
        }
12273
0
        break;
12274
0
      case BP_VAR_W:
12275
0
        if (packed_loaded) {
12276
0
          ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
12277
0
          ir_IF_TRUE_cold(if_def);
12278
0
          ir_refs_add(found_inputs, ir_END());
12279
0
          ir_refs_add(found_vals, ref);
12280
0
          ir_IF_FALSE(if_def);
12281
0
          ir_END_list(idx_not_found_inputs);
12282
0
        }
12283
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) {
12284
0
          if (if_packed) {
12285
0
            ir_IF_FALSE(if_packed);
12286
0
            if_packed = IR_UNUSED;
12287
0
            ir_END_list(idx_not_found_inputs);
12288
0
          } else if (!packed_loaded) {
12289
0
            ir_END_list(idx_not_found_inputs);
12290
0
          }
12291
0
          ir_MERGE_list(idx_not_found_inputs);
12292
0
          if (!op2_loaded) {
12293
            // JIT: hval = Z_LVAL_P(dim);
12294
0
            h = jit_Z_LVAL(jit, op2_addr);
12295
0
          }
12296
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_lookup), ht_ref, h);
12297
0
          ir_refs_add(found_inputs, ir_END());
12298
0
          ir_refs_add(found_vals, ref);
12299
0
        }
12300
0
        break;
12301
0
      default:
12302
0
        ZEND_UNREACHABLE();
12303
0
    }
12304
0
  }
12305
12306
0
  if (op2_info & MAY_BE_STRING) {
12307
0
    ir_ref key;
12308
12309
0
    if (if_type) {
12310
0
      ir_IF_FALSE(if_type);
12311
0
      if_type = IS_UNUSED;
12312
0
    }
12313
12314
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
12315
      // JIT: if (EXPECTED(Z_TYPE_P(dim) == IS_STRING))
12316
0
      if_type = jit_if_Z_TYPE(jit, op2_addr, IS_STRING);
12317
0
      ir_IF_TRUE(if_type);
12318
0
    }
12319
12320
    // JIT: offset_key = Z_STR_P(dim);
12321
0
    key = jit_Z_PTR(jit, op2_addr);
12322
12323
    // JIT: retval = zend_hash_find(ht, offset_key);
12324
0
    switch (type) {
12325
0
      case BP_JIT_IS:
12326
0
        if (opline->op2_type != IS_CONST) {
12327
0
          ir_ref if_num, end1, ref2;
12328
12329
0
          if_num = ir_IF(
12330
0
            ir_ULE(
12331
0
              ir_LOAD_C(ir_ADD_OFFSET(key, offsetof(zend_string, val))),
12332
0
              ir_CONST_CHAR('9')));
12333
0
          ir_IF_TRUE_cold(if_num);
12334
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_find), ht_ref, key);
12335
0
          end1 = ir_END();
12336
0
          ir_IF_FALSE(if_num);
12337
0
          ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find), ht_ref, key);
12338
0
          ir_MERGE_WITH(end1);
12339
0
          ref = ir_PHI_2(IR_ADDR, ref2, ref);
12340
0
        } else {
12341
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash), ht_ref, key);
12342
0
        }
12343
0
        if (not_found_exit_addr) {
12344
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12345
0
        } else {
12346
0
          if_found = ir_IF(ref);
12347
0
          ir_IF_FALSE(if_found);
12348
0
          ir_END_list(*end_inputs);
12349
0
          ir_IF_TRUE(if_found);
12350
0
        }
12351
0
        ir_refs_add(test_zval_values, ref);
12352
0
        ir_refs_add(test_zval_inputs, ir_END());
12353
0
        break;
12354
0
      case BP_VAR_R:
12355
0
      case BP_VAR_IS:
12356
0
      case BP_VAR_UNSET:
12357
0
        if (opline->op2_type != IS_CONST) {
12358
0
          ir_ref if_num, end1, ref2;
12359
12360
0
          if_num = ir_IF(
12361
0
            ir_ULE(
12362
0
              ir_LOAD_C(ir_ADD_OFFSET(key, offsetof(zend_string, val))),
12363
0
              ir_CONST_CHAR('9')));
12364
0
          ir_IF_TRUE_cold(if_num);
12365
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_find), ht_ref, key);
12366
0
          end1 = ir_END();
12367
0
          ir_IF_FALSE(if_num);
12368
0
          ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find), ht_ref, key);
12369
0
          ir_MERGE_WITH(end1);
12370
0
          ref = ir_PHI_2(IR_ADDR, ref2, ref);
12371
0
        } else {
12372
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash), ht_ref, key);
12373
0
        }
12374
0
        if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12375
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
12376
0
        } else if (type == BP_VAR_IS && not_found_exit_addr) {
12377
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12378
0
        } else if (type == BP_VAR_IS && result_type_guard) {
12379
0
          if_found = ir_IF(ref);
12380
0
          ir_IF_FALSE(if_found);
12381
0
          ir_END_list(*not_found_inputs);
12382
0
          ir_IF_TRUE(if_found);
12383
0
        } else {
12384
0
          if_found = ir_IF(ref);
12385
0
          switch (type) {
12386
0
            case BP_VAR_R:
12387
0
              ir_IF_FALSE_cold(if_found);
12388
              // JIT: zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(offset_key));
12389
0
              jit_SET_EX_OPLINE(jit, opline);
12390
0
              ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_undefined_key, IR_FASTCALL_FUNC));
12391
0
              ir_END_list(*end_inputs);
12392
0
              break;
12393
0
            case BP_VAR_IS:
12394
0
            case BP_VAR_UNSET:
12395
0
              ir_IF_FALSE(if_found);
12396
              // JIT: retval = &EG(uninitialized_zval);
12397
0
              jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12398
0
              ir_END_list(*end_inputs);
12399
0
              break;
12400
0
            default:
12401
0
              ZEND_UNREACHABLE();
12402
0
          }
12403
0
          ir_IF_TRUE(if_found);
12404
0
        }
12405
0
        ir_refs_add(found_inputs, ir_END());
12406
0
        ir_refs_add(found_vals, ref);
12407
0
        break;
12408
0
      case BP_VAR_RW:
12409
0
        if (opline->op2_type != IS_CONST) {
12410
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_lookup_rw), ht_ref, key);
12411
0
        } else {
12412
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_lookup_rw), ht_ref, key);
12413
0
        }
12414
0
        if (not_found_exit_addr) {
12415
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12416
0
        } else {
12417
0
          if_found = ir_IF(ref);
12418
0
          ir_IF_FALSE(if_found);
12419
0
          ir_END_list(*end_inputs);
12420
0
          ir_IF_TRUE(if_found);
12421
0
        }
12422
0
        ir_refs_add(found_inputs, ir_END());
12423
0
        ir_refs_add(found_vals, ref);
12424
0
        break;
12425
0
      case BP_VAR_W:
12426
0
        if (opline->op2_type != IS_CONST) {
12427
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_lookup_w), ht_ref, key);
12428
0
        } else {
12429
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_lookup), ht_ref, key);
12430
0
        }
12431
0
        ir_refs_add(found_inputs, ir_END());
12432
0
        ir_refs_add(found_vals, ref);
12433
0
        break;
12434
0
      default:
12435
0
        ZEND_UNREACHABLE();
12436
0
    }
12437
0
  }
12438
12439
0
  if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
12440
0
      if (if_type) {
12441
0
      ir_IF_FALSE_cold(if_type);
12442
0
      if_type = IS_UNDEF;
12443
0
    }
12444
0
    if (type != BP_VAR_RW) {
12445
0
      jit_SET_EX_OPLINE(jit, opline);
12446
0
    }
12447
0
    ref = jit_ZVAL_ADDR(jit, op2_addr);
12448
0
    switch (type) {
12449
0
      case BP_VAR_R:
12450
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_r_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_JIT_IS:
12457
0
        ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_fetch_dim_isset_helper), ht_ref, ref);
12458
0
        if (not_found_exit_addr) {
12459
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12460
0
          ir_refs_add(found_inputs, ir_END());
12461
0
        } else if (found_exit_addr) {
12462
0
          ir_GUARD_NOT(ref, ir_CONST_ADDR(found_exit_addr));
12463
0
          ir_END_list(*end_inputs);
12464
0
        } else {
12465
0
          if_found = ir_IF(ref);
12466
0
          ir_IF_TRUE(if_found);
12467
0
          ir_refs_add(found_inputs, ir_END());
12468
0
          ir_IF_FALSE(if_found);
12469
0
          ir_END_list(*end_inputs);
12470
0
        }
12471
0
        break;
12472
0
      case BP_VAR_IS:
12473
0
      case BP_VAR_UNSET:
12474
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_is_helper),
12475
0
          ht_ref,
12476
0
          ref,
12477
0
          jit_ZVAL_ADDR(jit, res_addr));
12478
0
        ir_END_list(*end_inputs);
12479
0
        break;
12480
0
      case BP_VAR_RW:
12481
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_rw_helper), ht_ref, ref);
12482
0
        if_found = ir_IF(ref);
12483
0
        ir_IF_TRUE(if_found);
12484
0
        ir_refs_add(found_inputs, ir_END());
12485
0
        ir_refs_add(found_vals, ref);
12486
0
        ir_IF_FALSE(if_found);
12487
0
        ir_END_list(*end_inputs);
12488
0
        break;
12489
0
      case BP_VAR_W:
12490
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref);
12491
0
        if_found = ir_IF(ref);
12492
0
        ir_IF_TRUE(if_found);
12493
0
        ir_refs_add(found_inputs, ir_END());
12494
0
        ir_refs_add(found_vals, ref);
12495
0
        ir_IF_FALSE(if_found);
12496
0
        ir_END_list(*end_inputs);
12497
0
        break;
12498
0
      default:
12499
0
        ZEND_UNREACHABLE();
12500
0
    }
12501
0
  }
12502
12503
0
  if (type == BP_JIT_IS
12504
0
   && !(op2_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
12505
    /* dead code */
12506
0
    ir_END_list(*end_inputs);
12507
0
  } else if (type == BP_JIT_IS
12508
0
   && (op1_info & MAY_BE_ARRAY)
12509
0
   && (op2_info & (MAY_BE_LONG|MAY_BE_STRING))
12510
0
   && test_zval_inputs->count) {
12511
12512
0
    ir_MERGE_N(test_zval_inputs->count, test_zval_inputs->refs);
12513
0
    ref = ir_PHI_N(IR_ADDR, test_zval_values->count, test_zval_values->refs);
12514
12515
0
    if (op1_info & MAY_BE_ARRAY_OF_REF) {
12516
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
12517
0
    }
12518
0
    cond = ir_GT(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_NULL));
12519
0
    if (not_found_exit_addr) {
12520
0
      ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12521
0
      ir_refs_add(found_inputs, ir_END());
12522
0
    } else if (found_exit_addr) {
12523
0
      ir_GUARD_NOT(cond, ir_CONST_ADDR(found_exit_addr));
12524
0
      ir_END_list(*end_inputs);
12525
0
    } else {
12526
0
      ir_ref if_set = ir_IF(cond);
12527
0
      ir_IF_FALSE(if_set);
12528
0
      ir_END_list(*end_inputs);
12529
0
      ir_IF_TRUE(if_set);
12530
0
      ir_refs_add(found_inputs, ir_END());
12531
0
    }
12532
0
  }
12533
12534
0
  return 1;
12535
0
}
12536
12537
static int zend_jit_fetch_dim_read(zend_jit_ctx       *jit,
12538
                                   const zend_op      *opline,
12539
                                   zend_ssa           *ssa,
12540
                                   const zend_ssa_op  *ssa_op,
12541
                                   uint32_t            op1_info,
12542
                                   zend_jit_addr       op1_addr,
12543
                                   bool           op1_avoid_refcounting,
12544
                                   uint32_t            op2_info,
12545
                                   zend_jit_addr       op2_addr,
12546
                                   zend_ssa_range     *op2_range,
12547
                                   uint32_t            res_info,
12548
                                   zend_jit_addr       res_addr,
12549
                                   uint8_t             dim_type)
12550
0
{
12551
0
  zend_jit_addr orig_op1_addr;
12552
0
  const void *exit_addr = NULL;
12553
0
  const void *not_found_exit_addr = NULL;
12554
0
  bool result_type_guard = false;
12555
0
  bool result_avoid_refcounting = false;
12556
0
  uint32_t may_be_string = (opline->opcode != ZEND_FETCH_LIST_R) ? MAY_BE_STRING : 0;
12557
0
  int may_throw = 0;
12558
0
  ir_ref if_type = IR_UNUSED;
12559
0
  ir_ref end_inputs = IR_UNUSED;
12560
0
  ir_ref not_found_inputs = IR_UNUSED;
12561
12562
0
  orig_op1_addr = OP1_ADDR();
12563
12564
0
  if (opline->opcode != ZEND_FETCH_DIM_IS
12565
0
   && JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
12566
0
   && !has_concrete_type(op1_info)) {
12567
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
12568
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
12569
0
    if (!exit_addr) {
12570
0
      return 0;
12571
0
    }
12572
0
  }
12573
12574
0
  if ((res_info & MAY_BE_GUARD)
12575
0
   && JIT_G(current_frame)
12576
0
   && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
12577
12578
0
    if (!(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF) - (MAY_BE_STRING|MAY_BE_LONG)))) {
12579
0
      result_type_guard = true;
12580
0
      res_info &= ~MAY_BE_GUARD;
12581
0
      ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
12582
0
    }
12583
12584
0
    if ((opline->result_type & (IS_VAR|IS_TMP_VAR))
12585
0
     && (opline->opcode == ZEND_FETCH_LIST_R
12586
0
      || !(opline->op1_type & (IS_VAR|IS_TMP_VAR))
12587
0
      || op1_avoid_refcounting)
12588
0
     && (res_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
12589
0
     && (ssa_op+1)->op1_use == ssa_op->result_def
12590
0
     && !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF) - (MAY_BE_STRING|MAY_BE_LONG)))
12591
0
     && zend_jit_may_avoid_refcounting(opline+1, res_info)) {
12592
0
      result_avoid_refcounting = true;
12593
0
      ssa->var_info[ssa_op->result_def].avoid_refcounting = 1;
12594
0
    }
12595
12596
0
    if (opline->opcode == ZEND_FETCH_DIM_IS
12597
0
     && !(res_info & MAY_BE_NULL)) {
12598
0
      uint32_t flags = 0;
12599
0
      uint32_t old_op1_info = 0;
12600
0
      uint32_t old_info;
12601
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
12602
0
      int32_t exit_point;
12603
12604
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
12605
0
       && !op1_avoid_refcounting) {
12606
0
        flags |= ZEND_JIT_EXIT_FREE_OP1;
12607
0
      }
12608
0
      if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
12609
0
       && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
12610
0
        flags |= ZEND_JIT_EXIT_FREE_OP2;
12611
0
      }
12612
12613
0
      if (op1_avoid_refcounting) {
12614
0
        old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
12615
0
        SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
12616
0
      }
12617
12618
0
      old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
12619
0
      SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_NULL, 0);
12620
0
      SET_STACK_REG_EX(stack, EX_VAR_TO_NUM(opline->result.var), ZREG_NONE, ZREG_TYPE_ONLY);
12621
0
      exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
12622
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
12623
0
      not_found_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
12624
0
      if (!not_found_exit_addr) {
12625
0
        return 0;
12626
0
      }
12627
12628
0
      if (op1_avoid_refcounting) {
12629
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
12630
0
      }
12631
0
    }
12632
0
  }
12633
12634
0
  if (op1_info & MAY_BE_REF) {
12635
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
12636
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
12637
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
12638
0
  }
12639
12640
0
  if (op1_info & MAY_BE_ARRAY) {
12641
0
    ir_ref ht_ref, ref;
12642
0
    zend_jit_addr val_addr;
12643
0
    ir_refs *found_inputs, *found_vals;
12644
12645
0
    ir_refs_init(found_inputs, 10);
12646
0
    ir_refs_init(found_vals, 10);
12647
12648
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
12649
0
      if (exit_addr && !(op1_info & (MAY_BE_OBJECT|may_be_string))) {
12650
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_ARRAY, exit_addr);
12651
0
      } else {
12652
0
        if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
12653
0
        ir_IF_TRUE(if_type);
12654
0
      }
12655
0
    }
12656
12657
0
    ht_ref = jit_Z_PTR(jit, op1_addr);
12658
12659
0
    if ((op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) ||
12660
0
        (opline->opcode != ZEND_FETCH_DIM_IS && JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE)) {
12661
0
      may_throw = 1;
12662
0
    }
12663
12664
0
    if (!zend_jit_fetch_dimension_address_inner(jit, opline,
12665
0
        (opline->opcode != ZEND_FETCH_DIM_IS) ? BP_VAR_R : BP_VAR_IS,
12666
0
        op1_info, op2_info, op2_addr, op2_range, dim_type, NULL, not_found_exit_addr, exit_addr,
12667
0
        result_type_guard, ht_ref, found_inputs, found_vals,
12668
0
        &end_inputs, &not_found_inputs)) {
12669
0
      return 0;
12670
0
    }
12671
12672
0
    if (found_inputs->count) {
12673
0
      ir_MERGE_N(found_inputs->count, found_inputs->refs);
12674
0
      ref = ir_PHI_N(IR_ADDR, found_vals->count, found_vals->refs);
12675
0
      val_addr = ZEND_ADDR_REF_ZVAL(ref);
12676
12677
0
      if (result_type_guard) {
12678
0
        uint8_t type = concrete_type(res_info);
12679
0
        uint32_t flags = 0;
12680
12681
0
        if (opline->opcode != ZEND_FETCH_LIST_R
12682
0
         && (opline->op1_type & (IS_VAR|IS_TMP_VAR))
12683
0
         && !op1_avoid_refcounting) {
12684
0
          flags |= ZEND_JIT_EXIT_FREE_OP1;
12685
0
        }
12686
0
        if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
12687
0
         && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
12688
0
          flags |= ZEND_JIT_EXIT_FREE_OP2;
12689
0
        }
12690
12691
0
        val_addr = zend_jit_guard_fetch_result_type(jit, opline, val_addr, type,
12692
0
          (op1_info & MAY_BE_ARRAY_OF_REF) != 0, flags, op1_avoid_refcounting);
12693
0
        if (!val_addr) {
12694
0
          return 0;
12695
0
        }
12696
12697
0
        if (not_found_inputs) {
12698
0
          ir_END_list(not_found_inputs);
12699
0
          ir_MERGE_list(not_found_inputs);
12700
0
        }
12701
12702
        // ZVAL_COPY
12703
0
        jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, !result_avoid_refcounting);
12704
0
        if (Z_MODE(res_addr) != IS_REG) {
12705
0
        } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
12706
0
          return 0;
12707
0
        }
12708
0
      } else if (op1_info & MAY_BE_ARRAY_OF_REF) {
12709
        // ZVAL_COPY_DEREF
12710
0
        ir_ref type_info = jit_Z_TYPE_INFO(jit, val_addr);
12711
0
        if (!zend_jit_zval_copy_deref(jit, res_addr, val_addr, type_info)) {
12712
0
          return 0;
12713
0
        }
12714
0
      } else  {
12715
        // ZVAL_COPY
12716
0
        jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, true);
12717
0
      }
12718
12719
0
      ir_END_list(end_inputs);
12720
0
    } else if (not_found_inputs) {
12721
0
      ir_MERGE_list(not_found_inputs);
12722
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12723
0
      ir_END_list(end_inputs);
12724
0
    } else if (!end_inputs && jit->ctx.control) {
12725
0
      ir_END_list(end_inputs); /* dead code */
12726
0
    }
12727
0
  }
12728
12729
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY)) {
12730
0
    if (if_type) {
12731
0
      ir_IF_FALSE_cold(if_type);
12732
0
      if_type = IS_UNDEF;
12733
0
    }
12734
12735
0
    if (opline->opcode != ZEND_FETCH_LIST_R && (op1_info & MAY_BE_STRING)) {
12736
0
      ir_ref str_ref;
12737
12738
0
      may_throw = 1;
12739
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_STRING))) {
12740
0
        if (exit_addr && !(op1_info & MAY_BE_OBJECT)) {
12741
0
          jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, exit_addr);
12742
0
        } else {
12743
0
          if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
12744
0
          ir_IF_TRUE(if_type);
12745
0
        }
12746
0
      }
12747
0
      jit_SET_EX_OPLINE(jit, opline);
12748
0
      str_ref = jit_Z_PTR(jit, op1_addr);
12749
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12750
0
        ir_ref ref;
12751
12752
0
        if ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG) {
12753
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_offset_r_helper),
12754
0
            str_ref, jit_Z_LVAL(jit, op2_addr));
12755
0
        } else {
12756
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_r_helper),
12757
0
            str_ref, jit_ZVAL_ADDR(jit, op2_addr));
12758
0
        }
12759
0
        jit_set_Z_PTR(jit, res_addr, ref);
12760
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING);
12761
0
      } else {
12762
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_is_helper),
12763
0
          str_ref,
12764
0
          jit_ZVAL_ADDR(jit, op2_addr),
12765
0
          jit_ZVAL_ADDR(jit, res_addr));
12766
0
      }
12767
0
      ir_END_list(end_inputs);
12768
0
    }
12769
12770
0
    if (op1_info & MAY_BE_OBJECT) {
12771
0
      ir_ref arg2;
12772
12773
0
      if (if_type) {
12774
0
        ir_IF_FALSE_cold(if_type);
12775
0
        if_type = IS_UNDEF;
12776
0
      }
12777
12778
0
      may_throw = 1;
12779
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string))) {
12780
0
        if (exit_addr) {
12781
0
          jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
12782
0
        } else {
12783
0
          if_type = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
12784
0
          ir_IF_TRUE(if_type);
12785
0
        }
12786
0
      }
12787
12788
0
      jit_SET_EX_OPLINE(jit, opline);
12789
0
      if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
12790
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
12791
0
        arg2 = ir_CONST_ADDR(Z_ZV(op2_addr)+1);
12792
0
      } else {
12793
0
        arg2 = jit_ZVAL_ADDR(jit, op2_addr);
12794
0
      }
12795
12796
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12797
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_r_helper),
12798
0
          jit_ZVAL_ADDR(jit, op1_addr),
12799
0
          arg2,
12800
0
          jit_ZVAL_ADDR(jit, res_addr));
12801
0
      } else {
12802
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_is_helper),
12803
0
          jit_ZVAL_ADDR(jit, op1_addr),
12804
0
          arg2,
12805
0
          jit_ZVAL_ADDR(jit, res_addr));
12806
0
      }
12807
12808
0
      ir_END_list(end_inputs);
12809
0
    }
12810
12811
0
    if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string)))
12812
0
     && (!exit_addr || !(op1_info & (MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string)))) {
12813
12814
0
      if (if_type) {
12815
0
        ir_IF_FALSE_cold(if_type);
12816
0
        if_type = IS_UNDEF;
12817
0
      }
12818
12819
0
      if ((opline->opcode != ZEND_FETCH_DIM_IS && (op1_info & MAY_BE_UNDEF)) || (op2_info & MAY_BE_UNDEF)) {
12820
0
        jit_SET_EX_OPLINE(jit, opline);
12821
0
        if (opline->opcode != ZEND_FETCH_DIM_IS && (op1_info & MAY_BE_UNDEF)) {
12822
0
          may_throw = 1;
12823
0
          zend_jit_type_check_undef(jit, jit_Z_TYPE(jit, op1_addr), opline->op1.var, NULL,
12824
0
                  false, true, false);
12825
0
        }
12826
12827
0
        if (op2_info & MAY_BE_UNDEF) {
12828
0
          may_throw = 1;
12829
0
          zend_jit_type_check_undef(jit, jit_Z_TYPE(jit, op2_addr), opline->op2.var, NULL,
12830
0
                  false, true, false);
12831
0
        }
12832
0
      }
12833
12834
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12835
0
        ir_ref ref;
12836
12837
0
        may_throw = 1;
12838
0
        if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
12839
0
          ref = jit_ZVAL_ADDR(jit, orig_op1_addr);
12840
0
        } else {
12841
0
          jit_SET_EX_OPLINE(jit, opline);
12842
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
12843
0
        }
12844
0
        if (opline->opcode == ZEND_FETCH_LIST_R) {
12845
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_array_use), ref);
12846
0
        } else {
12847
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_array_access), ref);
12848
0
        }
12849
0
      }
12850
12851
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12852
0
      ir_END_list(end_inputs);
12853
0
    }
12854
0
  }
12855
12856
0
  if (end_inputs) {
12857
0
    ir_MERGE_list(end_inputs);
12858
12859
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
12860
0
    if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & MAY_BE_OBJECT)) {
12861
      /* Magic offsetGet() may increase refcount of the key */
12862
0
      op2_info |= MAY_BE_RCN;
12863
0
    }
12864
0
#endif
12865
12866
0
    if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
12867
0
      if ((op2_info & MAY_HAVE_DTOR) && (op2_info & MAY_BE_RC1)) {
12868
0
        may_throw = 1;
12869
0
      }
12870
0
      jit_FREE_OP(jit,  opline->op2_type, opline->op2, op2_info, opline);
12871
0
    }
12872
0
    if (opline->opcode != ZEND_FETCH_LIST_R && !op1_avoid_refcounting) {
12873
0
      if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
12874
0
        if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
12875
0
          may_throw = 1;
12876
0
        }
12877
0
        jit_FREE_OP(jit,  opline->op1_type, opline->op1, op1_info, opline);
12878
0
      }
12879
0
    }
12880
12881
0
    if (may_throw) {
12882
0
      zend_jit_check_exception(jit);
12883
0
    }
12884
0
  } else if (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) {
12885
0
    ir_BEGIN(IR_UNUSED); /* unreachable tail */
12886
0
  }
12887
12888
0
  return 1;
12889
0
}
12890
12891
static zend_jit_addr zend_jit_prepare_array_update(zend_jit_ctx   *jit,
12892
                                                   const zend_op  *opline,
12893
                                                   uint32_t       *op1_info_ptr,
12894
                                                   zend_jit_addr   op1_addr,
12895
                                                   ir_ref         *if_type,
12896
                                                   ir_ref         *ht_ref,
12897
                                                   int            *may_throw)
12898
0
{
12899
0
  ir_ref ref = IR_UNUSED;
12900
0
  ir_ref array_reference_end = IR_UNUSED, array_reference_ref = IR_UNUSED;
12901
0
  ir_refs *array_inputs, *array_values;
12902
0
  uint32_t op1_info = *op1_info_ptr;
12903
12904
0
  ir_refs_init(array_inputs, 4);
12905
0
  ir_refs_init(array_values, 4);
12906
12907
0
  ref = jit_ZVAL_ADDR(jit, op1_addr);
12908
0
  if (op1_info & MAY_BE_REF) {
12909
0
    ir_ref if_reference, if_array, end1, ref2;
12910
12911
0
    *may_throw = 1;
12912
0
    if_reference = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
12913
0
    ir_IF_FALSE(if_reference);
12914
0
    end1 = ir_END();
12915
0
    ir_IF_TRUE_cold(if_reference);
12916
0
    array_reference_ref = ir_ADD_OFFSET(jit_Z_PTR_ref(jit, ref), offsetof(zend_reference, val));
12917
0
    if_array = jit_if_Z_TYPE_ref(jit, array_reference_ref, ir_CONST_U8(IS_ARRAY));
12918
0
    ir_IF_TRUE(if_array);
12919
0
    array_reference_end = ir_END();
12920
0
    ir_IF_FALSE_cold(if_array);
12921
0
    if (opline->opcode != ZEND_FETCH_DIM_RW && opline->opcode != ZEND_ASSIGN_DIM_OP) {
12922
0
      jit_SET_EX_OPLINE(jit, opline);
12923
0
    }
12924
0
    ref2 = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_prepare_assign_dim_ref), ref);
12925
0
    ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
12926
12927
0
    ir_MERGE_WITH(end1);
12928
0
    ref = ir_PHI_2(IR_ADDR, ref2, ref);
12929
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
12930
0
  }
12931
12932
0
  if (op1_info & MAY_BE_ARRAY) {
12933
0
    ir_ref op1_ref = ref;
12934
12935
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
12936
0
      *if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
12937
0
      ir_IF_TRUE(*if_type);
12938
0
    }
12939
0
    if (array_reference_end) {
12940
0
      ir_MERGE_WITH(array_reference_end);
12941
0
      op1_ref = ir_PHI_2(IR_ADDR, ref, array_reference_ref);
12942
0
    }
12943
    // JIT: SEPARATE_ARRAY()
12944
0
    ref = jit_Z_PTR_ref(jit, op1_ref);
12945
0
    if (RC_MAY_BE_N(op1_info)) {
12946
0
      if (RC_MAY_BE_1(op1_info)) {
12947
0
        ir_ref if_refcount_1 = ir_IF(ir_EQ(jit_GC_REFCOUNT(jit, ref), ir_CONST_U32(1)));
12948
0
        ir_IF_TRUE(if_refcount_1);
12949
0
        ir_refs_add(array_inputs, ir_END());
12950
0
        ir_refs_add(array_values, ref);
12951
0
        ir_IF_FALSE(if_refcount_1);
12952
0
      }
12953
0
      ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_zval_array_dup), op1_ref);
12954
0
    }
12955
0
    if (array_inputs->count || (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL))) {
12956
0
      ir_refs_add(array_inputs, ir_END());
12957
0
      ir_refs_add(array_values, ref);
12958
0
    }
12959
0
  }
12960
12961
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL)) {
12962
0
    if (*if_type) {
12963
0
      ir_IF_FALSE_cold(*if_type);
12964
0
      *if_type = IR_UNUSED;
12965
0
    }
12966
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_ARRAY))) {
12967
0
      *if_type = ir_IF(ir_LE(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_NULL)));
12968
0
      ir_IF_TRUE(*if_type);
12969
0
    }
12970
0
    if ((op1_info & MAY_BE_UNDEF)
12971
0
     && (opline->opcode == ZEND_FETCH_DIM_RW || opline->opcode == ZEND_ASSIGN_DIM_OP)) {
12972
0
      ir_ref end1 = IR_UNUSED;
12973
12974
0
      *may_throw = 1;
12975
0
      if (op1_info & MAY_BE_NULL) {
12976
0
        ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
12977
0
        ir_IF_TRUE(if_def);
12978
0
        end1 = ir_END();
12979
0
        ir_IF_FALSE(if_def);
12980
0
      }
12981
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
12982
0
      if (end1) {
12983
0
        ir_MERGE_WITH(end1);
12984
0
      }
12985
0
    }
12986
    // JIT: ZVAL_ARR(container, zend_new_array(8));
12987
0
    ref = ir_CALL_1(IR_ADDR,
12988
0
      jit_STUB_FUNC_ADDR(jit, jit_stub_new_array, IR_FASTCALL_FUNC),
12989
0
      jit_ZVAL_ADDR(jit, op1_addr));
12990
0
    if (array_inputs->count) {
12991
0
      ir_refs_add(array_inputs, ir_END());
12992
0
      ir_refs_add(array_values, ref);
12993
0
    }
12994
0
    op1_info &= ~(MAY_BE_UNDEF | MAY_BE_NULL);
12995
0
    op1_info |= MAY_BE_ARRAY | MAY_BE_RC1;
12996
0
    *op1_info_ptr = op1_info;
12997
0
  }
12998
12999
0
  if (array_inputs->count) {
13000
0
    ir_MERGE_N(array_inputs->count, array_inputs->refs);
13001
0
    ref = ir_PHI_N(IR_ADDR, array_values->count, array_values->refs);
13002
0
  }
13003
13004
0
  *ht_ref = ref;
13005
0
  return op1_addr;
13006
0
}
13007
13008
static int zend_jit_fetch_dim(zend_jit_ctx   *jit,
13009
                              const zend_op  *opline,
13010
                              uint32_t        op1_info,
13011
                              zend_jit_addr   op1_addr,
13012
                              uint32_t        op2_info,
13013
                              zend_jit_addr   op2_addr,
13014
                              zend_ssa_range *op2_range,
13015
                              zend_jit_addr   res_addr,
13016
                              uint8_t         dim_type)
13017
0
{
13018
0
  int may_throw = 0;
13019
0
  ir_ref end_inputs = IR_UNUSED;
13020
0
  ir_ref ref, if_type = IR_UNUSED, ht_ref;
13021
13022
0
  if (opline->opcode == ZEND_FETCH_DIM_RW) {
13023
0
    jit_SET_EX_OPLINE(jit, opline);
13024
0
  }
13025
13026
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13027
13028
0
  if (op1_info & MAY_BE_ARRAY) {
13029
0
    ir_refs *found_inputs, *found_vals;
13030
13031
0
    ir_refs_init(found_inputs, 8);
13032
0
    ir_refs_init(found_vals, 8);
13033
13034
0
    if (opline->op2_type == IS_UNUSED) {
13035
0
      ir_ref if_ok;
13036
13037
0
      may_throw = 1;
13038
      // JIT:var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13039
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13040
0
        ht_ref, jit_EG(uninitialized_zval));
13041
13042
      // JIT: if (UNEXPECTED(!var_ptr)) {
13043
0
      if_ok = ir_IF(ref);
13044
0
      ir_IF_FALSE_cold(if_ok);
13045
0
      if (opline->opcode != ZEND_FETCH_DIM_RW) {
13046
0
        jit_SET_EX_OPLINE(jit, opline);
13047
0
      }
13048
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13049
0
      ir_END_list(end_inputs);
13050
13051
0
      ir_IF_TRUE(if_ok);
13052
0
      jit_set_Z_PTR(jit, res_addr, ref);
13053
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
13054
13055
0
      ir_END_list(end_inputs);
13056
0
    } else {
13057
0
      uint32_t type;
13058
13059
0
      switch (opline->opcode) {
13060
0
        case ZEND_FETCH_DIM_W:
13061
0
        case ZEND_FETCH_LIST_W:
13062
0
          type = BP_VAR_W;
13063
0
          break;
13064
0
        case ZEND_FETCH_DIM_RW:
13065
0
          may_throw = 1;
13066
0
          type = BP_VAR_RW;
13067
0
          break;
13068
0
        case ZEND_FETCH_DIM_UNSET:
13069
0
          type = BP_VAR_UNSET;
13070
0
          break;
13071
0
        default:
13072
0
          ZEND_UNREACHABLE();
13073
0
      }
13074
13075
0
      if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
13076
0
        may_throw = 1;
13077
0
      }
13078
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, type, op1_info,
13079
0
          op2_info, op2_addr, op2_range, dim_type, NULL, NULL, NULL,
13080
0
          false, ht_ref, found_inputs, found_vals, &end_inputs, NULL)) {
13081
0
        return 0;
13082
0
      }
13083
13084
0
      if (type == BP_VAR_RW || (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING)))) {
13085
0
        if (end_inputs) {
13086
0
          ir_MERGE_list(end_inputs);
13087
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
13088
0
          end_inputs = ir_END();
13089
0
        }
13090
0
      } else if (!(op2_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
13091
        /* impossible dead path */
13092
0
        end_inputs = ir_END();
13093
0
      } else {
13094
0
        ZEND_ASSERT(end_inputs == IR_UNUSED);
13095
0
      }
13096
13097
0
      if (found_inputs->count) {
13098
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13099
0
        ref = ir_PHI_N(IR_ADDR, found_vals->count, found_vals->refs);
13100
0
        jit_set_Z_PTR(jit, res_addr, ref);
13101
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
13102
0
        ir_END_list(end_inputs);
13103
0
      }
13104
13105
0
    }
13106
0
  }
13107
13108
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13109
0
    ir_ref arg2;
13110
13111
0
    may_throw = 1;
13112
13113
0
    if (if_type) {
13114
0
      ir_IF_FALSE(if_type);
13115
0
      if_type = IR_UNUSED;
13116
0
    }
13117
13118
0
    if (opline->opcode != ZEND_FETCH_DIM_RW) {
13119
0
      jit_SET_EX_OPLINE(jit, opline);
13120
0
    }
13121
13122
0
      if (opline->op2_type == IS_UNUSED) {
13123
0
      arg2 = IR_NULL;
13124
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13125
0
      ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13126
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13127
0
    } else {
13128
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13129
0
    }
13130
13131
0
    switch (opline->opcode) {
13132
0
      case ZEND_FETCH_DIM_W:
13133
0
      case ZEND_FETCH_LIST_W:
13134
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_w_helper),
13135
0
          jit_ZVAL_ADDR(jit, op1_addr),
13136
0
          arg2,
13137
0
          jit_ZVAL_ADDR(jit, res_addr));
13138
0
        break;
13139
0
      case ZEND_FETCH_DIM_RW:
13140
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_rw_helper),
13141
0
          jit_ZVAL_ADDR(jit, op1_addr),
13142
0
          arg2,
13143
0
          jit_ZVAL_ADDR(jit, res_addr));
13144
0
        break;
13145
//      case ZEND_FETCH_DIM_UNSET:
13146
//        | EXT_CALL zend_jit_fetch_dim_obj_unset_helper, r0
13147
//        break;
13148
0
      default:
13149
0
        ZEND_UNREACHABLE();
13150
0
      }
13151
13152
0
    if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_ARRAY)) {
13153
0
      ir_END_list(end_inputs);
13154
0
    }
13155
0
  }
13156
13157
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13158
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))) {
13159
    /* ASSIGN_DIM may increase refcount of the key */
13160
0
    op2_info |= MAY_BE_RCN;
13161
0
  }
13162
0
#endif
13163
13164
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR))
13165
0
   && (op2_info & MAY_HAVE_DTOR)
13166
0
   && (op2_info & MAY_BE_RC1)) {
13167
0
    may_throw = 1;
13168
0
  }
13169
13170
0
  if (end_inputs) {
13171
0
    ir_MERGE_list(end_inputs);
13172
0
  }
13173
13174
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13175
13176
0
  if (may_throw) {
13177
0
    zend_jit_check_exception(jit);
13178
0
  }
13179
13180
0
  return 1;
13181
0
}
13182
13183
static int zend_jit_isset_isempty_dim(zend_jit_ctx   *jit,
13184
                                      const zend_op  *opline,
13185
                                      uint32_t        op1_info,
13186
                                      zend_jit_addr   op1_addr,
13187
                                      bool       op1_avoid_refcounting,
13188
                                      uint32_t        op2_info,
13189
                                      zend_jit_addr   op2_addr,
13190
                                      zend_ssa_range *op2_range,
13191
                                      uint8_t         dim_type,
13192
                                      int             may_throw,
13193
                                      uint8_t         smart_branch_opcode,
13194
                                      uint32_t        target_label,
13195
                                      uint32_t        target_label2,
13196
                                      const void     *exit_addr)
13197
0
{
13198
0
  zend_jit_addr res_addr;
13199
0
  ir_ref if_type = IR_UNUSED;
13200
0
  ir_ref false_inputs = IR_UNUSED, end_inputs = IR_UNUSED;
13201
0
  ir_refs *true_inputs;
13202
13203
0
  ir_refs_init(true_inputs, 8);
13204
13205
  // TODO: support for empty() ???
13206
0
  ZEND_ASSERT(!(opline->extended_value & ZEND_ISEMPTY));
13207
13208
0
  res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
13209
13210
0
  if (op1_info & MAY_BE_REF) {
13211
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
13212
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
13213
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
13214
0
  }
13215
13216
0
  if (op1_info & MAY_BE_ARRAY) {
13217
0
    const void *found_exit_addr = NULL;
13218
0
    const void *not_found_exit_addr = NULL;
13219
0
    ir_ref ht_ref;
13220
13221
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
13222
0
      if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
13223
0
      ir_IF_TRUE(if_type);
13224
0
    }
13225
13226
0
    ht_ref = jit_Z_PTR(jit, op1_addr);
13227
13228
0
    if (exit_addr
13229
0
     && !(op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY))
13230
0
     && !may_throw
13231
0
     && (!(opline->op1_type & (IS_TMP_VAR|IS_VAR)) || op1_avoid_refcounting)
13232
0
     && (!(opline->op2_type & (IS_TMP_VAR|IS_VAR)) || !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)))) {
13233
0
      if (smart_branch_opcode == ZEND_JMPNZ) {
13234
0
        found_exit_addr = exit_addr;
13235
0
      } else {
13236
0
        not_found_exit_addr = exit_addr;
13237
0
      }
13238
0
    }
13239
0
    if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_JIT_IS, op1_info,
13240
0
        op2_info, op2_addr, op2_range, dim_type, found_exit_addr, not_found_exit_addr, NULL,
13241
0
        false, ht_ref, true_inputs, NULL, &false_inputs, NULL)) {
13242
0
      return 0;
13243
0
    }
13244
13245
0
    if (found_exit_addr) {
13246
0
      ir_MERGE_list(false_inputs);
13247
0
      return 1;
13248
0
    } else if (not_found_exit_addr) {
13249
0
      ir_MERGE_N(true_inputs->count, true_inputs->refs);
13250
0
      return 1;
13251
0
    }
13252
0
  }
13253
13254
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY)) {
13255
0
    if (if_type) {
13256
0
      ir_IF_FALSE(if_type);
13257
0
      if_type = IR_UNUSED;
13258
0
    }
13259
13260
0
    if (op1_info & (MAY_BE_STRING|MAY_BE_OBJECT)) {
13261
0
      ir_ref ref, arg1, arg2, if_true;
13262
13263
0
      jit_SET_EX_OPLINE(jit, opline);
13264
0
      arg1 = jit_ZVAL_ADDR(jit, op1_addr);
13265
0
      if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13266
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13267
0
        arg2 = ir_CONST_ADDR(Z_ZV(op2_addr)+1);
13268
0
      } else {
13269
0
        arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13270
0
      }
13271
0
      ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_isset_dim_helper), arg1, arg2);
13272
0
      if_true = ir_IF(ref);
13273
0
      ir_IF_TRUE(if_true);
13274
0
      ir_refs_add(true_inputs, ir_END());
13275
0
      ir_IF_FALSE(if_true);
13276
0
      ir_END_list(false_inputs);
13277
0
    } else {
13278
0
      if (op2_info & MAY_BE_UNDEF) {
13279
0
        ir_ref end1 = IR_UNUSED;
13280
13281
0
        if (op2_info & MAY_BE_ANY) {
13282
0
          ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op2_addr));
13283
0
          ir_IF_TRUE(if_def);
13284
0
          end1 = ir_END();
13285
0
          ir_IF_FALSE(if_def);
13286
0
        }
13287
0
        jit_SET_EX_OPLINE(jit, opline);
13288
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op2.var));
13289
0
        if (end1) {
13290
0
          ir_MERGE_WITH(end1);
13291
0
        }
13292
0
      }
13293
0
      ir_END_list(false_inputs);
13294
0
    }
13295
0
  }
13296
13297
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13298
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & MAY_BE_OBJECT)) {
13299
    /* Magic offsetExists() may increase refcount of the key */
13300
0
    op2_info |= MAY_BE_RCN;
13301
0
  }
13302
0
#endif
13303
13304
0
  if (true_inputs->count) {
13305
0
    ir_MERGE_N(true_inputs->count, true_inputs->refs);
13306
13307
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13308
0
    if (!op1_avoid_refcounting) {
13309
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13310
0
    }
13311
0
    if (may_throw) {
13312
0
      zend_jit_check_exception_undef_result(jit, opline);
13313
0
    }
13314
0
    if (!(opline->extended_value & ZEND_ISEMPTY)) {
13315
0
      if (exit_addr) {
13316
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
13317
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13318
0
        } else {
13319
0
          ir_END_list(end_inputs);
13320
0
        }
13321
0
      } else if (smart_branch_opcode) {
13322
0
        if (smart_branch_opcode == ZEND_JMPZ) {
13323
0
          _zend_jit_add_predecessor_ref(jit, target_label2, jit->b, ir_END());
13324
0
        } else if (smart_branch_opcode == ZEND_JMPNZ) {
13325
0
          _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
13326
0
        } else {
13327
0
          ZEND_UNREACHABLE();
13328
0
        }
13329
0
      } else {
13330
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
13331
0
        ir_END_list(end_inputs);
13332
0
      }
13333
0
    } else {
13334
0
      ZEND_UNREACHABLE(); // TODO: support for empty()
13335
0
    }
13336
0
  }
13337
13338
0
  ir_MERGE_list(false_inputs);
13339
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13340
0
  if (!op1_avoid_refcounting) {
13341
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13342
0
  }
13343
0
  if (may_throw) {
13344
0
    zend_jit_check_exception_undef_result(jit, opline);
13345
0
  }
13346
0
  if (!(opline->extended_value & ZEND_ISEMPTY)) {
13347
0
    if (exit_addr) {
13348
0
      if (smart_branch_opcode == ZEND_JMPZ) {
13349
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13350
0
      } else {
13351
0
        ir_END_list(end_inputs);
13352
0
      }
13353
0
    } else if (smart_branch_opcode) {
13354
0
      if (smart_branch_opcode == ZEND_JMPZ) {
13355
0
        _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
13356
0
      } else if (smart_branch_opcode == ZEND_JMPNZ) {
13357
0
        _zend_jit_add_predecessor_ref(jit, target_label2, jit->b, ir_END());
13358
0
      } else {
13359
0
        ZEND_UNREACHABLE();
13360
0
      }
13361
0
    } else {
13362
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
13363
0
      ir_END_list(end_inputs);
13364
0
    }
13365
0
  } else {
13366
0
    ZEND_UNREACHABLE(); // TODO: support for empty()
13367
0
  }
13368
13369
0
    if (!exit_addr && smart_branch_opcode) {
13370
0
    jit->b = -1;
13371
0
    } else {
13372
0
    ir_MERGE_list(end_inputs);
13373
0
    }
13374
13375
0
  return 1;
13376
0
}
13377
13378
static int zend_jit_assign_dim(zend_jit_ctx  *jit,
13379
                               const zend_op *opline,
13380
                               uint32_t       op1_info,
13381
                               zend_jit_addr  op1_addr,
13382
                               bool           op1_indirect,
13383
                               uint32_t       op2_info,
13384
                               zend_jit_addr  op2_addr,
13385
                               zend_ssa_range *op2_range,
13386
                               uint32_t       val_info,
13387
                               zend_jit_addr  op3_addr,
13388
                               zend_jit_addr  op3_def_addr,
13389
                               zend_jit_addr  res_addr,
13390
                               uint8_t        dim_type,
13391
                               int            may_throw)
13392
0
{
13393
0
  ir_ref if_type = IR_UNUSED;
13394
0
  ir_ref end_inputs = IR_UNUSED, ht_ref;
13395
13396
0
  if (op3_addr != op3_def_addr && op3_def_addr) {
13397
0
    if (!zend_jit_update_regs(jit, (opline+1)->op1.var, op3_addr, op3_def_addr, val_info)) {
13398
0
      return 0;
13399
0
    }
13400
0
    if (Z_MODE(op3_def_addr) == IS_REG && Z_MODE(op3_addr) != IS_REG) {
13401
0
      op3_addr = op3_def_addr;
13402
0
    }
13403
0
  }
13404
13405
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && (val_info & MAY_BE_UNDEF)) {
13406
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
13407
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13408
13409
0
    if (!exit_addr) {
13410
0
      return 0;
13411
0
    }
13412
13413
0
    jit_guard_not_Z_TYPE(jit, op3_addr, IS_UNDEF, exit_addr);
13414
13415
0
    val_info &= ~MAY_BE_UNDEF;
13416
0
  }
13417
13418
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13419
13420
0
  if (op1_info & MAY_BE_ARRAY) {
13421
0
    if (opline->op2_type == IS_UNUSED) {
13422
0
      uint32_t var_info = MAY_BE_NULL;
13423
0
      ir_ref if_ok, ref;
13424
0
      zend_jit_addr var_addr;
13425
13426
      // JIT: var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13427
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13428
0
        ht_ref, jit_EG(uninitialized_zval));
13429
13430
      // JIT: if (UNEXPECTED(!var_ptr)) {
13431
0
      if_ok = ir_IF(ref);
13432
0
      ir_IF_FALSE_cold(if_ok);
13433
13434
      // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
13435
0
      jit_SET_EX_OPLINE(jit, opline);
13436
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13437
13438
0
      ir_END_list(end_inputs);
13439
13440
0
      ir_IF_TRUE(if_ok);
13441
0
      var_addr = ZEND_ADDR_REF_ZVAL(ref);
13442
0
      if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, -1, (opline+1)->op1_type, op3_addr, val_info, res_addr, false)) {
13443
0
        return 0;
13444
0
      }
13445
0
    } else {
13446
0
      uint32_t var_info = zend_array_element_type(op1_info, opline->op1_type, 0, 0);
13447
0
      zend_jit_addr var_addr;
13448
0
      ir_ref ref;
13449
0
      ir_refs *found_inputs, *found_values;
13450
13451
0
      ir_refs_init(found_inputs, 8);
13452
0
      ir_refs_init(found_values, 8);
13453
13454
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_VAR_W, op1_info,
13455
0
          op2_info, op2_addr, op2_range, dim_type, NULL, NULL, NULL,
13456
0
          false, ht_ref, found_inputs, found_values, &end_inputs, NULL)) {
13457
0
        return 0;
13458
0
      }
13459
13460
0
      if (op1_info & (MAY_BE_ARRAY_OF_REF|MAY_BE_OBJECT)) {
13461
0
        var_info |= MAY_BE_REF;
13462
0
      }
13463
0
      if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
13464
0
        var_info |= MAY_BE_RC1;
13465
0
      }
13466
13467
0
      if (found_inputs->count) {
13468
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13469
0
        ref = ir_PHI_N(IR_ADDR, found_values->count, found_values->refs);
13470
0
        var_addr = ZEND_ADDR_REF_ZVAL(ref);
13471
13472
        // JIT: value = zend_assign_to_variable(variable_ptr, value, OP_DATA_TYPE);
13473
0
        if (opline->op1_type == IS_VAR
13474
0
         && Z_MODE(op3_addr) != IS_REG
13475
0
         && opline->result_type == IS_UNUSED
13476
0
         && (res_addr == 0 || Z_MODE(res_addr) != IS_REG)) {
13477
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)) {
13478
0
            return 0;
13479
0
          }
13480
0
        } else {
13481
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)) {
13482
0
            return 0;
13483
0
          }
13484
0
        }
13485
0
      }
13486
0
    }
13487
13488
0
    ir_END_list(end_inputs);
13489
0
  }
13490
13491
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13492
0
    ir_ref arg2, arg4;
13493
13494
0
    if (if_type) {
13495
0
      ir_IF_FALSE_cold(if_type);
13496
0
      if_type = IR_UNUSED;
13497
0
    }
13498
13499
0
    jit_SET_EX_OPLINE(jit, opline);
13500
13501
0
      if (opline->op2_type == IS_UNUSED) {
13502
0
      arg2 = IR_NULL;
13503
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13504
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13505
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13506
0
    } else {
13507
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13508
0
    }
13509
13510
0
    if (opline->result_type == IS_UNUSED) {
13511
0
      arg4 = IR_NULL;
13512
0
    } else {
13513
0
      arg4 = jit_ZVAL_ADDR(jit, res_addr);
13514
0
    }
13515
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_helper),
13516
0
      jit_ZVAL_ADDR(jit, op1_addr),
13517
0
      arg2,
13518
0
      jit_ZVAL_ADDR(jit, op3_addr),
13519
0
      arg4);
13520
13521
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13522
0
    if (((opline+1)->op1_type & (IS_TMP_VAR|IS_VAR)) && (val_info & MAY_BE_RC1)) {
13523
      /* ASSIGN_DIM may increase refcount of the value */
13524
0
      val_info |= MAY_BE_RCN;
13525
0
    }
13526
0
#endif
13527
13528
0
    jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, NULL);
13529
13530
0
    ir_END_list(end_inputs);
13531
0
  }
13532
13533
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13534
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))) {
13535
    /* ASSIGN_DIM may increase refcount of the key */
13536
0
    op2_info |= MAY_BE_RCN;
13537
0
  }
13538
0
#endif
13539
13540
0
  ir_MERGE_list(end_inputs);
13541
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13542
13543
0
  if (!op1_indirect) {
13544
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13545
0
  }
13546
13547
0
  if (may_throw) {
13548
0
    zend_jit_check_exception(jit);
13549
0
  }
13550
13551
0
  return 1;
13552
0
}
13553
13554
static int zend_jit_assign_dim_op(zend_jit_ctx   *jit,
13555
                                  const zend_op  *opline,
13556
                                  uint32_t        op1_info,
13557
                                  uint32_t        op1_def_info,
13558
                                  zend_jit_addr   op1_addr,
13559
                                  bool            op1_indirect,
13560
                                  uint32_t        op2_info,
13561
                                  zend_jit_addr   op2_addr,
13562
                                  zend_ssa_range *op2_range,
13563
                                  uint32_t        op1_data_info,
13564
                                  zend_jit_addr   op3_addr,
13565
                                  zend_ssa_range *op1_data_range,
13566
                                  uint8_t         dim_type,
13567
                                  int             may_throw)
13568
0
{
13569
0
  zend_jit_addr var_addr = IS_UNUSED;
13570
0
  const void *not_found_exit_addr = NULL;
13571
0
  uint32_t var_info = MAY_BE_NULL;
13572
0
  ir_ref if_type = IS_UNUSED;
13573
0
  ir_ref end_inputs = IR_UNUSED, ht_ref;
13574
0
  bool emit_fast_path = true;
13575
13576
0
  ZEND_ASSERT(opline->result_type == IS_UNUSED);
13577
13578
0
  if (may_throw) {
13579
0
    jit_SET_EX_OPLINE(jit, opline);
13580
0
  }
13581
13582
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13583
13584
0
  if (Z_MODE(op3_addr) == IS_REG
13585
0
   && Z_LOAD(op3_addr)
13586
0
   && jit->ra[Z_SSA_VAR(op3_addr)].ref == IR_NULL) {
13587
    /* Force load */
13588
0
    zend_jit_use_reg(jit, op3_addr);
13589
0
  }
13590
13591
0
  if (op1_info & MAY_BE_ARRAY) {
13592
0
    uint32_t var_def_info = zend_array_element_type(op1_def_info, opline->op1_type, 1, 0);
13593
13594
0
    if (opline->op2_type == IS_UNUSED) {
13595
0
      var_info = MAY_BE_NULL;
13596
0
      ir_ref if_ok, ref;
13597
13598
      // JIT: var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13599
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13600
0
        ht_ref, jit_EG(uninitialized_zval));
13601
13602
      // JIT: if (UNEXPECTED(!var_ptr)) {
13603
0
      if_ok = ir_IF(ref);
13604
0
      ir_IF_FALSE_cold(if_ok);
13605
13606
      // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
13607
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13608
13609
0
      ir_END_list(end_inputs);
13610
13611
0
      ir_IF_TRUE(if_ok);
13612
0
      var_addr = ZEND_ADDR_REF_ZVAL(ref);
13613
0
    } else {
13614
0
      ir_ref ref;
13615
0
      ir_refs *found_inputs, *found_values;
13616
13617
0
      ir_refs_init(found_inputs, 8);
13618
0
      ir_refs_init(found_values, 8);
13619
13620
0
      var_info = zend_array_element_type(op1_info, opline->op1_type, 0, 0);
13621
0
      if (op1_info & (MAY_BE_ARRAY_OF_REF|MAY_BE_OBJECT)) {
13622
0
        var_info |= MAY_BE_REF;
13623
0
      }
13624
0
      if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
13625
0
        var_info |= MAY_BE_RC1;
13626
0
      }
13627
13628
0
      if (dim_type != IS_UNKNOWN
13629
0
       && dim_type != IS_UNDEF
13630
0
       && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY
13631
0
       && (op2_info & (MAY_BE_LONG|MAY_BE_STRING))
13632
0
       && !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING)))) {
13633
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
13634
0
        not_found_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13635
0
        if (!not_found_exit_addr) {
13636
0
          return 0;
13637
0
        }
13638
0
      }
13639
13640
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_VAR_RW, op1_info,
13641
0
          op2_info, op2_addr, op2_range, dim_type, NULL, not_found_exit_addr, NULL,
13642
0
          false, ht_ref, found_inputs, found_values, &end_inputs, NULL)) {
13643
0
        return 0;
13644
0
      }
13645
13646
0
      if (found_inputs->count) {
13647
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13648
0
        ref = ir_PHI_N(IR_ADDR, found_values->count, found_values->refs);
13649
0
        var_addr = ZEND_ADDR_REF_ZVAL(ref);
13650
13651
0
        if (not_found_exit_addr && dim_type != IS_REFERENCE) {
13652
0
          jit_guard_Z_TYPE(jit, var_addr, dim_type, not_found_exit_addr);
13653
0
          var_info = (1 << dim_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
13654
0
        }
13655
0
        if (var_info & MAY_BE_REF) {
13656
0
          binary_op_type binary_op = get_binary_op(opline->extended_value);
13657
0
          ir_ref if_ref, if_typed, noref_path, ref_path, ref, reference, ref2, arg2;
13658
13659
0
          ref = jit_ZVAL_ADDR(jit, var_addr);
13660
0
          if_ref = jit_if_Z_TYPE(jit, var_addr, IS_REFERENCE);
13661
0
          ir_IF_FALSE(if_ref);
13662
0
          noref_path = ir_END();
13663
0
          ir_IF_TRUE(if_ref);
13664
13665
0
          reference = jit_Z_PTR_ref(jit, ref);
13666
0
          ref2 = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
13667
0
          if_typed = jit_if_TYPED_REF(jit, reference);
13668
0
          ir_IF_FALSE(if_typed);
13669
0
          ref_path = ir_END();
13670
0
          ir_IF_TRUE_cold(if_typed);
13671
13672
0
          if (Z_MODE(op3_addr) == IS_REG) {
13673
0
            zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
13674
0
            if (!zend_jit_spill_store_inv(jit, op3_addr, real_addr, op1_data_info)) {
13675
0
              return 0;
13676
0
            }
13677
0
            op3_addr = real_addr;
13678
0
          }
13679
0
          arg2 = jit_ZVAL_ADDR(jit, op3_addr);
13680
0
          ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
13681
0
            reference, arg2, ir_CONST_FC_FUNC(binary_op));
13682
13683
0
          ir_END_list(end_inputs);
13684
13685
0
          ir_MERGE_2(noref_path, ref_path);
13686
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
13687
0
          var_addr = ZEND_ADDR_REF_ZVAL(ref);
13688
0
        }
13689
0
      } else {
13690
0
        emit_fast_path = false;
13691
0
      }
13692
0
    }
13693
13694
0
    if (emit_fast_path) {
13695
0
      uint8_t val_op_type = (opline+1)->op1_type;
13696
13697
0
      if (val_op_type & (IS_TMP_VAR|IS_VAR)) {
13698
        /* prevent FREE_OP in the helpers */
13699
0
        val_op_type = IS_CV;
13700
0
      }
13701
13702
0
      switch (opline->extended_value) {
13703
0
        case ZEND_ADD:
13704
0
        case ZEND_SUB:
13705
0
        case ZEND_MUL:
13706
0
        case ZEND_DIV:
13707
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,
13708
0
              1 /* may overflow */, may_throw)) {
13709
0
            return 0;
13710
0
          }
13711
0
          break;
13712
0
        case ZEND_BW_OR:
13713
0
        case ZEND_BW_AND:
13714
0
        case ZEND_BW_XOR:
13715
0
        case ZEND_SL:
13716
0
        case ZEND_SR:
13717
0
        case ZEND_MOD:
13718
0
          if (!zend_jit_long_math_helper(jit, opline, opline->extended_value,
13719
0
              IS_CV, opline->op1, var_addr, var_info, NULL,
13720
0
              val_op_type, (opline+1)->op1, op3_addr, op1_data_info,
13721
0
              op1_data_range,
13722
0
              0, var_addr, var_def_info, var_info, may_throw)) {
13723
0
            return 0;
13724
0
          }
13725
0
          break;
13726
0
        case ZEND_CONCAT:
13727
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,
13728
0
              may_throw)) {
13729
0
            return 0;
13730
0
          }
13731
0
          break;
13732
0
        default:
13733
0
          ZEND_UNREACHABLE();
13734
0
      }
13735
13736
0
      ir_END_list(end_inputs);
13737
0
    }
13738
0
  }
13739
13740
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13741
0
    binary_op_type binary_op;
13742
0
    ir_ref arg2;
13743
13744
0
    if (if_type) {
13745
0
      ir_IF_FALSE_cold(if_type);
13746
0
      if_type = IS_UNUSED;
13747
0
    }
13748
13749
0
      if (opline->op2_type == IS_UNUSED) {
13750
0
      arg2 = IR_NULL;
13751
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13752
0
      ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13753
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13754
0
    } else {
13755
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13756
0
    }
13757
0
    binary_op = get_binary_op(opline->extended_value);
13758
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_op_helper),
13759
0
      jit_ZVAL_ADDR(jit, op1_addr),
13760
0
      arg2,
13761
0
      jit_ZVAL_ADDR(jit, op3_addr),
13762
0
      ir_CONST_FC_FUNC(binary_op));
13763
0
    ir_END_list(end_inputs);
13764
0
  }
13765
13766
0
  if (end_inputs) {
13767
0
    ir_MERGE_list(end_inputs);
13768
0
  }
13769
13770
0
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, op1_data_info, NULL);
13771
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
13772
0
  if (!op1_indirect) {
13773
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
13774
0
  }
13775
0
  if (may_throw) {
13776
0
    zend_jit_check_exception(jit);
13777
0
  }
13778
13779
0
  return 1;
13780
0
}
13781
13782
static int zend_jit_fe_reset(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
13783
0
{
13784
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
13785
13786
  // JIT: ZVAL_COPY(res, value);
13787
0
  if (opline->op1_type == IS_CONST) {
13788
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
13789
13790
0
    jit_ZVAL_COPY_CONST(jit, res_addr, MAY_BE_ANY, MAY_BE_ANY, zv, true);
13791
0
  } else {
13792
0
    zend_jit_addr op1_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
13793
13794
0
    jit_ZVAL_COPY(jit, res_addr, -1, op1_addr, op1_info, opline->op1_type == IS_CV);
13795
0
  }
13796
13797
  // JIT: Z_FE_POS_P(res) = 0;
13798
0
  ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->result.var + offsetof(zval, u2.fe_pos)), ir_CONST_U32(0));
13799
13800
0
  return 1;
13801
0
}
13802
13803
static int zend_jit_packed_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var, uint32_t op_info)
13804
0
{
13805
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_PACKED_GUARD);
13806
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13807
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
13808
0
  ir_ref ref;
13809
13810
0
  if (!exit_addr) {
13811
0
    return 0;
13812
0
  }
13813
13814
0
  ref = ir_AND_U32(
13815
0
    ir_LOAD_U32(ir_ADD_OFFSET(jit_Z_PTR(jit, addr), offsetof(zend_array, u.flags))),
13816
0
    ir_CONST_U32(HASH_FLAG_PACKED));
13817
0
  if (op_info & MAY_BE_ARRAY_PACKED) {
13818
0
    ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13819
0
  } else {
13820
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
13821
0
  }
13822
13823
0
  return 1;
13824
0
}
13825
13826
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)
13827
0
{
13828
0
  zend_jit_addr op1_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
13829
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;
13830
0
  ir_ref if_def_hash = IR_UNUSED, if_def_packed = IR_UNUSED;
13831
0
  ir_ref exit_inputs = IR_UNUSED;
13832
13833
0
  if (!MAY_BE_HASH(op1_info) && !MAY_BE_PACKED(op1_info)) {
13834
    /* empty array */
13835
0
    if (exit_addr) {
13836
0
      if (exit_opcode == ZEND_JMP) {
13837
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13838
0
      }
13839
0
    } else {
13840
0
      zend_basic_block *bb;
13841
13842
0
      ZEND_ASSERT(jit->b >= 0);
13843
0
      bb = &jit->ssa->cfg.blocks[jit->b];
13844
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
13845
0
      jit->b = -1;
13846
0
    }
13847
0
    return 1;
13848
0
  }
13849
13850
  // JIT: array = EX_VAR(opline->op1.var);
13851
  // JIT: fe_ht = Z_ARRVAL_P(array);
13852
0
  ht_ref = jit_Z_PTR(jit, op1_addr);
13853
13854
0
  if (op1_info & MAY_BE_PACKED_GUARD) {
13855
0
    if (!zend_jit_packed_guard(jit, opline, opline->op1.var, op1_info)) {
13856
0
      return 0;
13857
0
    }
13858
0
  }
13859
13860
  // JIT: pos = Z_FE_POS_P(array);
13861
0
  hash_pos_ref = packed_pos_ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)));
13862
13863
0
  if (MAY_BE_HASH(op1_info)) {
13864
0
    ir_ref loop_ref, pos2_ref, p2_ref;
13865
13866
0
    if (MAY_BE_PACKED(op1_info)) {
13867
0
      ref = ir_AND_U32(
13868
0
        ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
13869
0
        ir_CONST_U32(HASH_FLAG_PACKED));
13870
0
      if_packed = ir_IF(ref);
13871
0
      ir_IF_FALSE(if_packed);
13872
0
    }
13873
13874
    // JIT: p = fe_ht->arData + pos;
13875
0
    if (sizeof(void*) == 8) {
13876
0
      ref = ir_ZEXT_A(hash_pos_ref);
13877
0
    } else {
13878
0
      ref = ir_BITCAST_A(hash_pos_ref);
13879
0
    }
13880
0
    hash_p_ref = ir_ADD_A(
13881
0
      ir_MUL_A(ref, ir_CONST_ADDR(sizeof(Bucket))),
13882
0
      ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arData))));
13883
13884
0
    loop_ref = ir_LOOP_BEGIN(ir_END());
13885
0
    hash_pos_ref = ir_PHI_2(IR_U32, hash_pos_ref, IR_UNUSED);
13886
0
    hash_p_ref = ir_PHI_2(IR_ADDR, hash_p_ref, IR_UNUSED);
13887
13888
    // JIT: if (UNEXPECTED(pos >= fe_ht->nNumUsed)) {
13889
0
    ref = ir_ULT(hash_pos_ref,
13890
0
      ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed))));
13891
13892
    // JIT: ZEND_VM_SET_RELATIVE_OPCODE(opline, opline->extended_value);
13893
    // JIT: ZEND_VM_CONTINUE();
13894
13895
0
    if (exit_addr) {
13896
0
      if (exit_opcode == ZEND_JMP) {
13897
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13898
0
      } else {
13899
0
        ir_ref if_fit = ir_IF(ref);
13900
0
        ir_IF_FALSE(if_fit);
13901
0
        ir_END_list(exit_inputs);
13902
0
        ir_IF_TRUE(if_fit);
13903
0
      }
13904
0
    } else {
13905
0
      ir_ref if_fit = ir_IF(ref);
13906
0
      ir_IF_FALSE(if_fit);
13907
0
      ir_END_list(exit_inputs);
13908
0
      ir_IF_TRUE(if_fit);
13909
0
    }
13910
13911
    // JIT: pos++;
13912
0
    pos2_ref = ir_ADD_U32(hash_pos_ref, ir_CONST_U32(1));
13913
13914
    // JIT: value_type = Z_TYPE_INFO_P(value);
13915
    // JIT: if (EXPECTED(value_type != IS_UNDEF)) {
13916
0
    if (!exit_addr || exit_opcode == ZEND_JMP) {
13917
0
      if_def_hash = ir_IF(jit_Z_TYPE_ref(jit, hash_p_ref));
13918
0
      ir_IF_FALSE(if_def_hash);
13919
0
    } else {
13920
0
      ir_GUARD_NOT(jit_Z_TYPE_ref(jit, hash_p_ref), ir_CONST_ADDR(exit_addr));
13921
0
    }
13922
13923
    // JIT: p++;
13924
0
    p2_ref = ir_ADD_OFFSET(hash_p_ref, sizeof(Bucket));
13925
13926
0
    ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
13927
0
    ir_PHI_SET_OP(hash_pos_ref, 2, pos2_ref);
13928
0
    ir_PHI_SET_OP(hash_p_ref, 2, p2_ref);
13929
13930
0
    if (MAY_BE_PACKED(op1_info)) {
13931
0
      ir_IF_TRUE(if_packed);
13932
0
    }
13933
0
  }
13934
0
  if (MAY_BE_PACKED(op1_info)) {
13935
0
    ir_ref loop_ref, pos2_ref, p2_ref;
13936
13937
    // JIT: p = fe_ht->arPacked + pos;
13938
0
    if (sizeof(void*) == 8) {
13939
0
      ref = ir_ZEXT_A(packed_pos_ref);
13940
0
    } else {
13941
0
      ref = ir_BITCAST_A(packed_pos_ref);
13942
0
    }
13943
0
    packed_p_ref = ir_ADD_A(
13944
0
      ir_MUL_A(ref, ir_CONST_ADDR(sizeof(zval))),
13945
0
      ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arPacked))));
13946
13947
0
    loop_ref = ir_LOOP_BEGIN(ir_END());
13948
0
    packed_pos_ref = ir_PHI_2(IR_U32, packed_pos_ref, IR_UNUSED);
13949
0
    packed_p_ref = ir_PHI_2(IR_ADDR, packed_p_ref, IR_UNUSED);
13950
13951
    // JIT: if (UNEXPECTED(pos >= fe_ht->nNumUsed)) {
13952
0
    ref = ir_ULT(packed_pos_ref,
13953
0
      ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed))));
13954
13955
    // JIT: ZEND_VM_SET_RELATIVE_OPCODE(opline, opline->extended_value);
13956
    // JIT: ZEND_VM_CONTINUE();
13957
0
    if (exit_addr) {
13958
0
      if (exit_opcode == ZEND_JMP) {
13959
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13960
0
      } else {
13961
0
        ir_ref if_fit = ir_IF(ref);
13962
0
        ir_IF_FALSE(if_fit);
13963
0
        ir_END_list(exit_inputs);
13964
0
        ir_IF_TRUE(if_fit);
13965
0
      }
13966
0
    } else {
13967
0
      ir_ref if_fit = ir_IF(ref);
13968
0
      ir_IF_FALSE(if_fit);
13969
0
      ir_END_list(exit_inputs);
13970
0
      ir_IF_TRUE(if_fit);
13971
0
    }
13972
13973
    // JIT: pos++;
13974
0
    pos2_ref = ir_ADD_U32(packed_pos_ref, ir_CONST_U32(1));
13975
13976
    // JIT: value_type = Z_TYPE_INFO_P(value);
13977
    // JIT: if (EXPECTED(value_type != IS_UNDEF)) {
13978
0
    if (!exit_addr || exit_opcode == ZEND_JMP) {
13979
0
      if_def_packed = ir_IF(jit_Z_TYPE_ref(jit, packed_p_ref));
13980
0
      ir_IF_FALSE(if_def_packed);
13981
0
    } else {
13982
0
      ir_GUARD_NOT(jit_Z_TYPE_ref(jit, packed_p_ref), ir_CONST_ADDR(exit_addr));
13983
0
    }
13984
13985
    // JIT: p++;
13986
0
    p2_ref = ir_ADD_OFFSET(packed_p_ref, sizeof(zval));
13987
13988
0
    ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
13989
0
    ir_PHI_SET_OP(packed_pos_ref, 2, pos2_ref);
13990
0
    ir_PHI_SET_OP(packed_p_ref, 2, p2_ref);
13991
0
  }
13992
13993
0
  if (!exit_addr || exit_opcode == ZEND_JMP) {
13994
0
    zend_jit_addr val_addr;
13995
0
    zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
13996
0
    uint32_t val_info;
13997
0
    ir_ref p_ref = IR_UNUSED, hash_path = IR_UNUSED;
13998
13999
0
    if (RETURN_VALUE_USED(opline)) {
14000
0
      zend_jit_addr res_addr = RES_ADDR();
14001
14002
0
      if (MAY_BE_HASH(op1_info)) {
14003
0
        ir_ref key_ref = IR_UNUSED, if_key = IR_UNUSED, key_path = IR_UNUSED;
14004
14005
0
        ZEND_ASSERT(if_def_hash);
14006
0
        ir_IF_TRUE(if_def_hash);
14007
14008
        // JIT: Z_FE_POS_P(array) = pos + 1;
14009
0
        ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14010
0
          ir_ADD_U32(hash_pos_ref, ir_CONST_U32(1)));
14011
14012
0
        if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14013
0
          key_ref = ir_LOAD_A(ir_ADD_OFFSET(hash_p_ref, offsetof(Bucket, key)));
14014
0
        }
14015
0
        if ((op1_info & MAY_BE_ARRAY_KEY_LONG)
14016
0
         && (op1_info & MAY_BE_ARRAY_KEY_STRING)) {
14017
          // JIT: if (!p->key) {
14018
0
          if_key = ir_IF(key_ref);
14019
0
          ir_IF_TRUE(if_key);
14020
0
        }
14021
0
        if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14022
0
          ir_ref if_interned, interned_path;
14023
14024
          // JIT: ZVAL_STR_COPY(EX_VAR(opline->result.var), p->key);
14025
0
          jit_set_Z_PTR(jit, res_addr, key_ref);
14026
0
          ref = ir_AND_U32(
14027
0
            ir_LOAD_U32(ir_ADD_OFFSET(key_ref, offsetof(zend_refcounted, gc.u.type_info))),
14028
0
            ir_CONST_U32(IS_STR_INTERNED));
14029
0
          if_interned = ir_IF(ref);
14030
0
          ir_IF_TRUE(if_interned);
14031
14032
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING);
14033
14034
0
          interned_path = ir_END();
14035
0
          ir_IF_FALSE(if_interned);
14036
14037
0
          jit_GC_ADDREF(jit, key_ref);
14038
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING_EX);
14039
14040
0
          ir_MERGE_WITH(interned_path);
14041
14042
0
          if (op1_info & MAY_BE_ARRAY_KEY_LONG) {
14043
0
            key_path = ir_END();
14044
0
          }
14045
0
        }
14046
0
        if (op1_info & MAY_BE_ARRAY_KEY_LONG) {
14047
0
          if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14048
0
            ir_IF_FALSE(if_key);
14049
0
          }
14050
          // JIT: ZVAL_LONG(EX_VAR(opline->result.var), p->h);
14051
0
          ref = ir_LOAD_L(ir_ADD_OFFSET(hash_p_ref, offsetof(Bucket, h)));
14052
0
          jit_set_Z_LVAL(jit, res_addr, ref);
14053
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
14054
14055
0
          if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14056
0
            ir_MERGE_WITH(key_path);
14057
0
          }
14058
0
        }
14059
0
        if (MAY_BE_PACKED(op1_info)) {
14060
0
          hash_path = ir_END();
14061
0
        } else {
14062
0
          p_ref = hash_p_ref;
14063
0
        }
14064
0
      }
14065
0
      if (MAY_BE_PACKED(op1_info)) {
14066
0
        ZEND_ASSERT(if_def_packed);
14067
0
        ir_IF_TRUE(if_def_packed);
14068
14069
        // JIT: Z_FE_POS_P(array) = pos + 1;
14070
0
        ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14071
0
          ir_ADD_U32(packed_pos_ref, ir_CONST_U32(1)));
14072
14073
        // JIT: ZVAL_LONG(EX_VAR(opline->result.var), pos);
14074
0
        if (sizeof(zend_long) == 8) {
14075
0
          packed_pos_ref = ir_ZEXT_L(packed_pos_ref);
14076
0
        } else {
14077
0
          packed_pos_ref = ir_BITCAST_L(packed_pos_ref);
14078
0
        }
14079
0
        jit_set_Z_LVAL(jit, res_addr, packed_pos_ref);
14080
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
14081
14082
0
        if (MAY_BE_HASH(op1_info)) {
14083
0
          ir_MERGE_WITH(hash_path);
14084
0
          p_ref = ir_PHI_2(IR_ADDR, packed_p_ref, hash_p_ref);
14085
0
        } else {
14086
0
          p_ref = packed_p_ref;
14087
0
        }
14088
0
      }
14089
0
    } else {
14090
0
      ir_ref pos_ref = IR_UNUSED;
14091
14092
0
      if (if_def_hash && if_def_packed) {
14093
0
        ir_IF_TRUE(if_def_hash);
14094
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def_packed);
14095
0
        pos_ref = ir_PHI_2(IR_U32, hash_pos_ref, packed_pos_ref);
14096
0
        p_ref = ir_PHI_2(IR_ADDR, hash_p_ref, packed_p_ref);
14097
0
      } else if (if_def_hash) {
14098
0
        ir_IF_TRUE(if_def_hash);
14099
0
        pos_ref = hash_pos_ref;
14100
0
        p_ref = hash_p_ref;
14101
0
      } else if (if_def_packed) {
14102
0
        ir_IF_TRUE(if_def_packed);
14103
0
        pos_ref = packed_pos_ref;
14104
0
        p_ref = packed_p_ref;
14105
0
      } else {
14106
0
        ZEND_UNREACHABLE();
14107
0
      }
14108
14109
      // JIT: Z_FE_POS_P(array) = pos + 1;
14110
0
      ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14111
0
        ir_ADD_U32(pos_ref, ir_CONST_U32(1)));
14112
0
    }
14113
14114
0
    val_info = ((op1_info & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
14115
0
    if (val_info & MAY_BE_ARRAY) {
14116
0
      val_info |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
14117
0
    }
14118
0
    if (op1_info & MAY_BE_ARRAY_OF_REF) {
14119
0
      val_info |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY |
14120
0
        MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
14121
0
    } else if (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
14122
0
      val_info |= MAY_BE_RC1 | MAY_BE_RCN;
14123
0
    }
14124
14125
0
    val_addr = ZEND_ADDR_REF_ZVAL(p_ref);
14126
0
    if (opline->op2_type == IS_CV) {
14127
      // JIT: zend_assign_to_variable(variable_ptr, value, IS_CV, EX_USES_STRICT_TYPES());
14128
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)) {
14129
0
        return 0;
14130
0
      }
14131
0
    } else {
14132
      // JIT: ZVAL_DEREF(value);
14133
0
      if (val_info & MAY_BE_REF) {
14134
0
        ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
14135
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
14136
0
        val_addr = ZEND_ADDR_REF_ZVAL(ref);
14137
0
        val_info &= ~MAY_BE_REF;
14138
0
      }
14139
      // JIT: ZVAL_COPY(res, value);
14140
0
      jit_ZVAL_COPY(jit, var_addr, -1, val_addr, val_info, true);
14141
0
    }
14142
14143
0
    if (!exit_addr) {
14144
0
      zend_basic_block *bb;
14145
14146
0
      ZEND_ASSERT(jit->b >= 0);
14147
0
      bb = &jit->ssa->cfg.blocks[jit->b];
14148
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ir_END());
14149
0
      ZEND_ASSERT(exit_inputs);
14150
0
      if (!jit->ctx.ir_base[exit_inputs].op2) {
14151
0
        ref = exit_inputs;
14152
0
      } else {
14153
0
        ir_MERGE_list(exit_inputs);
14154
0
        ref = ir_END();
14155
0
      }
14156
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
14157
0
      jit->b = -1;
14158
0
    }
14159
0
  } else {
14160
0
    ZEND_ASSERT(exit_inputs);
14161
0
    ir_MERGE_list(exit_inputs);
14162
0
  }
14163
14164
0
  return 1;
14165
0
}
14166
14167
static int zend_jit_load_this(zend_jit_ctx *jit, uint32_t var)
14168
0
{
14169
0
  zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14170
0
  zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
14171
0
  ir_ref ref = jit_Z_PTR(jit, this_addr);
14172
14173
0
  jit_set_Z_PTR(jit, var_addr, ref);
14174
0
  jit_set_Z_TYPE_INFO(jit, var_addr, IS_OBJECT_EX);
14175
0
  jit_GC_ADDREF(jit, ref);
14176
14177
0
  return 1;
14178
0
}
14179
14180
static int zend_jit_fetch_this(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, bool check_only)
14181
0
{
14182
0
  if (!op_array->scope ||
14183
0
      (op_array->fn_flags & ZEND_ACC_STATIC) ||
14184
0
      ((op_array->fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_IMMUTABLE)) == ZEND_ACC_CLOSURE)) {
14185
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14186
0
      if (!JIT_G(current_frame) ||
14187
0
          !TRACE_FRAME_IS_THIS_CHECKED(JIT_G(current_frame))) {
14188
14189
0
        zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14190
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14191
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14192
14193
0
        if (!exit_addr) {
14194
0
          return 0;
14195
0
        }
14196
14197
0
        jit_guard_Z_TYPE(jit, this_addr, IS_OBJECT, exit_addr);
14198
14199
0
        if (JIT_G(current_frame)) {
14200
0
          TRACE_FRAME_SET_THIS_CHECKED(JIT_G(current_frame));
14201
0
        }
14202
0
      }
14203
0
    } else {
14204
0
      zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14205
0
      ir_ref if_object = jit_if_Z_TYPE(jit, this_addr, IS_OBJECT);
14206
14207
0
      ir_IF_FALSE_cold(if_object);
14208
0
      jit_SET_EX_OPLINE(jit, opline);
14209
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_invalid_this));
14210
14211
0
      ir_IF_TRUE(if_object);
14212
0
    }
14213
0
  }
14214
14215
0
  if (!check_only) {
14216
0
    if (!zend_jit_load_this(jit, opline->result.var)) {
14217
0
      return 0;
14218
0
    }
14219
0
  }
14220
14221
0
  return 1;
14222
0
}
14223
14224
static int zend_jit_class_guard(zend_jit_ctx *jit, const zend_op *opline, ir_ref obj_ref, zend_class_entry *ce)
14225
0
{
14226
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
14227
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14228
14229
0
  if (!exit_addr) {
14230
0
    return 0;
14231
0
  }
14232
14233
0
  ir_GUARD(ir_EQ(ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce))), ir_CONST_ADDR(ce)),
14234
0
    ir_CONST_ADDR(exit_addr));
14235
14236
0
  return 1;
14237
0
}
14238
14239
static int zend_jit_func_arg_by_ref_guard(zend_jit_ctx *jit, const zend_op *opline)
14240
0
{
14241
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14242
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14243
0
  ir_ref rx, call_info;
14244
14245
0
  if (!exit_addr) {
14246
0
    return 0;
14247
0
  }
14248
0
  if (jit->reuse_ip) {
14249
0
    rx = jit_IP(jit);
14250
0
  } else {
14251
0
    rx = ir_LOAD_A(jit_EX(call));
14252
0
  }
14253
0
  call_info = ir_LOAD_U32(jit_CALL(rx, This.u1.type_info));
14254
0
  ir_GUARD_NOT(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)),
14255
0
    ir_CONST_ADDR(exit_addr));
14256
0
  return 1;
14257
0
}
14258
14259
static int zend_jit_fetch_obj(zend_jit_ctx         *jit,
14260
                              const zend_op        *opline,
14261
                              const zend_op_array  *op_array,
14262
                              zend_ssa             *ssa,
14263
                              const zend_ssa_op    *ssa_op,
14264
                              uint32_t              op1_info,
14265
                              zend_jit_addr         op1_addr,
14266
                              bool                  op1_indirect,
14267
                              zend_class_entry     *ce,
14268
                              bool                  ce_is_instanceof,
14269
                              bool                  on_this,
14270
                              bool                  delayed_fetch_this,
14271
                              bool                  op1_avoid_refcounting,
14272
                              zend_class_entry     *trace_ce,
14273
                              zend_jit_addr         res_addr,
14274
                              uint8_t               prop_type,
14275
                              int                   may_throw)
14276
0
{
14277
0
  zval *member;
14278
0
  zend_property_info *prop_info;
14279
0
  bool may_be_dynamic = true;
14280
0
  zend_jit_addr prop_addr;
14281
0
  uint32_t res_info = RES_INFO();
14282
0
  ir_ref prop_type_ref = IR_UNUSED;
14283
0
  ir_ref obj_ref = IR_UNUSED;
14284
0
  ir_ref prop_ref = IR_UNUSED;
14285
0
  ir_ref end_inputs = IR_UNUSED;
14286
0
  ir_ref slow_inputs = IR_UNUSED;
14287
0
  ir_ref end_values = IR_UNUSED;
14288
14289
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
14290
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
14291
14292
0
  member = RT_CONSTANT(opline, opline->op2);
14293
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
14294
0
  prop_info = zend_get_known_property_info(op_array, ce, Z_STR_P(member), on_this, op_array->filename);
14295
14296
0
  if (on_this) {
14297
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14298
0
    obj_ref = jit_Z_PTR(jit, this_addr);
14299
0
  } else {
14300
0
    if (opline->op1_type == IS_VAR
14301
0
     && opline->opcode == ZEND_FETCH_OBJ_W
14302
0
     && (op1_info & MAY_BE_INDIRECT)
14303
0
     && Z_REG(op1_addr) == ZREG_FP) {
14304
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
14305
0
    }
14306
0
    if (op1_info & MAY_BE_REF) {
14307
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
14308
0
    }
14309
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
14310
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14311
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14312
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14313
14314
0
        if (!exit_addr) {
14315
0
          return 0;
14316
0
        }
14317
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
14318
0
      } else {
14319
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
14320
14321
0
        ir_IF_FALSE_cold(if_obj);
14322
0
        if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14323
0
          ir_ref op1_ref = IR_UNUSED;
14324
14325
0
          jit_SET_EX_OPLINE(jit, opline);
14326
0
          if (opline->opcode != ZEND_FETCH_OBJ_W && (op1_info & MAY_BE_UNDEF)) {
14327
0
            zend_jit_addr orig_op1_addr = OP1_ADDR();
14328
0
            ir_ref fast_path = IR_UNUSED;
14329
14330
0
            if (op1_info & MAY_BE_ANY) {
14331
0
              ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
14332
0
              ir_IF_TRUE(if_def);
14333
0
              fast_path = ir_END();
14334
0
              ir_IF_FALSE_cold(if_def);
14335
0
            }
14336
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
14337
0
              ir_CONST_U32(opline->op1.var));
14338
0
            if (fast_path) {
14339
0
              ir_MERGE_WITH(fast_path);
14340
0
            }
14341
0
            op1_ref = jit_ZVAL_ADDR(jit, orig_op1_addr);
14342
0
          } else {
14343
0
            op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
14344
0
          }
14345
0
          if (opline->opcode == ZEND_FETCH_OBJ_W) {
14346
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_write),
14347
0
              op1_ref, ir_CONST_ADDR(Z_STRVAL_P(member)));
14348
0
            jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14349
0
          } else {
14350
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_read),
14351
0
              op1_ref, ir_CONST_ADDR(Z_STRVAL_P(member)));
14352
0
            jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14353
0
          }
14354
0
        } else {
14355
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14356
0
        }
14357
0
        ir_END_list(end_inputs);
14358
14359
0
        ir_IF_TRUE(if_obj);
14360
0
      }
14361
0
    }
14362
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
14363
0
  }
14364
14365
0
  ZEND_ASSERT(obj_ref);
14366
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
14367
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, Z_STR_P(member), on_this, op_array->filename);
14368
0
    if (prop_info) {
14369
0
      ce = trace_ce;
14370
0
      ce_is_instanceof = false;
14371
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
14372
0
        if (on_this && JIT_G(current_frame)
14373
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
14374
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
14375
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
14376
0
          if (on_this && JIT_G(current_frame)) {
14377
0
            JIT_G(current_frame)->ce = ce;
14378
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
14379
0
          }
14380
0
        } else {
14381
0
          return 0;
14382
0
        }
14383
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
14384
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
14385
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
14386
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
14387
0
        }
14388
0
      }
14389
0
    }
14390
0
  }
14391
14392
0
  if (!prop_info) {
14393
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
14394
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
14395
0
    ir_ref if_same = ir_IF(ir_EQ(ref,
14396
0
      ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
14397
14398
0
    ir_IF_FALSE_cold(if_same);
14399
0
    ir_END_list(slow_inputs);
14400
14401
0
    ir_IF_TRUE(if_same);
14402
0
    ir_ref offset_ref = ir_LOAD_A(
14403
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
14404
14405
0
    may_be_dynamic = zend_may_be_dynamic_property(ce, Z_STR_P(member), opline->op1_type == IS_UNUSED, op_array);
14406
0
    if (may_be_dynamic) {
14407
0
      ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
14408
0
      if (opline->opcode == ZEND_FETCH_OBJ_W) {
14409
0
        ir_IF_TRUE_cold(if_dynamic);
14410
0
        ir_END_list(slow_inputs);
14411
0
      } else {
14412
0
        ir_IF_TRUE_cold(if_dynamic);
14413
0
        jit_SET_EX_OPLINE(jit, opline);
14414
14415
0
        if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14416
0
          if (Z_MODE(res_addr) == IS_REG) {
14417
0
            ir_ref val_addr = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_dynamic_ex),
14418
0
              obj_ref, offset_ref);
14419
0
            ir_END_PHI_list(end_values, val_addr);
14420
0
          } else {
14421
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_dynamic),
14422
0
              obj_ref, offset_ref);
14423
0
            ir_END_list(end_inputs);
14424
0
          }
14425
0
        } else {
14426
0
          if (Z_MODE(res_addr) == IS_REG) {
14427
0
            ir_ref val_addr = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_dynamic_ex),
14428
0
              obj_ref, offset_ref);
14429
0
            ir_END_PHI_list(end_values, val_addr);
14430
0
          } else {
14431
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_dynamic),
14432
0
              obj_ref, offset_ref);
14433
0
            ir_END_list(end_inputs);
14434
0
          }
14435
0
        }
14436
0
      }
14437
0
      ir_IF_FALSE(if_dynamic);
14438
0
    }
14439
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
14440
0
    prop_type_ref = jit_Z_TYPE_ref(jit, prop_ref);
14441
0
    ir_ref if_def = ir_IF(prop_type_ref);
14442
0
    ir_IF_FALSE_cold(if_def);
14443
0
    ir_END_list(slow_inputs);
14444
0
    ir_IF_TRUE(if_def);
14445
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14446
0
    if (opline->opcode == ZEND_FETCH_OBJ_W
14447
0
     && (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT)))) {
14448
0
      uint32_t flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
14449
14450
0
      ir_ref allowed_inputs = IR_UNUSED;
14451
0
      ir_ref forbidden_inputs = IR_UNUSED;
14452
14453
0
      ir_ref prop_info_ref = ir_LOAD_A(
14454
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
14455
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
14456
14457
0
      ir_IF_TRUE_cold(if_has_prop_info);
14458
14459
0
      ir_ref prop_flags = ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, flags)));
14460
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)));
14461
14462
0
      ir_IF_FALSE(if_readonly_or_avis);
14463
0
      ir_END_list(allowed_inputs);
14464
14465
0
      ir_IF_TRUE_cold(if_readonly_or_avis);
14466
14467
0
      ir_ref if_readonly = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY)));
14468
0
      ir_IF_TRUE(if_readonly);
14469
0
      ir_END_list(forbidden_inputs);
14470
14471
0
      ir_IF_FALSE(if_readonly);
14472
0
      ir_ref has_avis_access = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_asymmetric_property_has_set_access), prop_info_ref);
14473
0
      ir_ref if_avis_access = ir_IF(has_avis_access);
14474
0
      ir_IF_TRUE(if_avis_access);
14475
0
      ir_END_list(allowed_inputs);
14476
14477
0
      ir_IF_FALSE(if_avis_access);
14478
0
      ir_END_list(forbidden_inputs);
14479
14480
0
      ir_MERGE_list(forbidden_inputs);
14481
14482
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14483
0
      ir_IF_TRUE(if_prop_obj);
14484
0
      ref = jit_Z_PTR(jit, prop_addr);
14485
0
      jit_GC_ADDREF(jit, ref);
14486
0
      jit_set_Z_PTR(jit, res_addr, ref);
14487
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14488
0
      ir_END_list(end_inputs);
14489
14490
0
      ir_IF_FALSE_cold(if_prop_obj);
14491
14492
0
      jit_SET_EX_OPLINE(jit, opline);
14493
0
      if_readonly = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY)));
14494
0
      ir_IF_TRUE(if_readonly);
14495
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_readonly_property_indirect_modification_error), prop_info_ref);
14496
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14497
0
      ir_END_list(end_inputs);
14498
14499
0
      ir_IF_FALSE(if_readonly);
14500
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_asymmetric_visibility_property_modification_error),
14501
0
        prop_info_ref, ir_CONST_ADDR("indirectly modify"));
14502
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14503
0
      ir_END_list(end_inputs);
14504
14505
0
      ir_MERGE_list(allowed_inputs);
14506
14507
0
      if (flags == ZEND_FETCH_DIM_WRITE) {
14508
0
        jit_SET_EX_OPLINE(jit, opline);
14509
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_array_promotion),
14510
0
          prop_ref, prop_info_ref);
14511
0
        ir_END_list(end_inputs);
14512
0
        ir_IF_FALSE(if_has_prop_info);
14513
0
      } else if (flags == ZEND_FETCH_REF) {
14514
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_create_typed_ref),
14515
0
          prop_ref,
14516
0
          prop_info_ref,
14517
0
          jit_ZVAL_ADDR(jit, res_addr));
14518
0
        ir_END_list(end_inputs);
14519
0
        ir_IF_FALSE(if_has_prop_info);
14520
0
      } else {
14521
0
        ZEND_ASSERT(flags == 0);
14522
0
        ir_MERGE_WITH_EMPTY_FALSE(if_has_prop_info);
14523
0
      }
14524
0
    }
14525
0
  } else {
14526
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
14527
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14528
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14529
0
      if (opline->opcode == ZEND_FETCH_OBJ_W || !(res_info & MAY_BE_GUARD) || !JIT_G(current_frame)) {
14530
        /* perform IS_UNDEF check only after result type guard (during deoptimization) */
14531
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14532
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14533
14534
0
        if (!exit_addr) {
14535
0
          return 0;
14536
0
        }
14537
0
        prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14538
0
        ir_GUARD(prop_type_ref, ir_CONST_ADDR(exit_addr));
14539
0
      }
14540
0
    } else {
14541
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14542
0
      ir_ref if_def = ir_IF(prop_type_ref);
14543
0
      ir_IF_FALSE_cold(if_def);
14544
0
      ir_END_list(slow_inputs);
14545
0
      ir_IF_TRUE(if_def);
14546
0
    }
14547
0
    if (opline->opcode == ZEND_FETCH_OBJ_W && (prop_info->flags & ZEND_ACC_READONLY)) {
14548
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14549
0
      ir_IF_TRUE(if_prop_obj);
14550
0
      ir_ref ref = jit_Z_PTR(jit, prop_addr);
14551
0
      jit_GC_ADDREF(jit, ref);
14552
0
      jit_set_Z_PTR(jit, res_addr, ref);
14553
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14554
0
      ir_END_list(end_inputs);
14555
14556
0
      ir_IF_FALSE_cold(if_prop_obj);
14557
0
      jit_SET_EX_OPLINE(jit, opline);
14558
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_readonly_property_indirect_modification_error), ir_CONST_ADDR(prop_info));
14559
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14560
0
      ir_END_list(end_inputs);
14561
14562
0
      goto result_fetched;
14563
0
    } else if (opline->opcode == ZEND_FETCH_OBJ_W && (prop_info->flags & ZEND_ACC_PPP_SET_MASK)) {
14564
      /* Readonly properties which are also asymmetric are never mutable indirectly, which is
14565
       * handled by the previous branch. */
14566
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));
14567
14568
0
      ir_ref if_access = ir_IF(has_access);
14569
0
      ir_IF_FALSE_cold(if_access);
14570
14571
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14572
0
      ir_IF_TRUE(if_prop_obj);
14573
0
      ir_ref ref = jit_Z_PTR(jit, prop_addr);
14574
0
      jit_GC_ADDREF(jit, ref);
14575
0
      jit_set_Z_PTR(jit, res_addr, ref);
14576
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14577
0
      ir_END_list(end_inputs);
14578
14579
0
      ir_IF_FALSE_cold(if_prop_obj);
14580
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_asymmetric_visibility_property_modification_error),
14581
0
        ir_CONST_ADDR(prop_info), ir_CONST_ADDR("indirectly modify"));
14582
0
      ir_END_list(end_inputs);
14583
14584
0
      ir_IF_TRUE(if_access);
14585
0
    }
14586
14587
0
    if (opline->opcode == ZEND_FETCH_OBJ_W
14588
0
     && (opline->extended_value & ZEND_FETCH_OBJ_FLAGS)
14589
0
     && ZEND_TYPE_IS_SET(prop_info->type)) {
14590
0
      uint32_t flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
14591
14592
0
      if (flags == ZEND_FETCH_DIM_WRITE) {
14593
0
        if ((ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_ARRAY) == 0) {
14594
0
          if (!prop_type_ref) {
14595
0
            prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14596
0
          }
14597
0
          ir_ref if_null_or_flase = ir_IF(ir_LE(prop_type_ref, ir_CONST_U32(IR_FALSE)));
14598
0
          ir_IF_TRUE_cold(if_null_or_flase);
14599
0
          jit_SET_EX_OPLINE(jit, opline);
14600
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_array_promotion),
14601
0
            prop_ref, ir_CONST_ADDR(prop_info));
14602
0
          ir_END_list(end_inputs);
14603
0
          ir_IF_FALSE(if_null_or_flase);
14604
0
        }
14605
0
      } else if (flags == ZEND_FETCH_REF) {
14606
0
        ir_ref ref;
14607
14608
0
        if (!prop_type_ref) {
14609
0
          prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14610
0
        }
14611
14612
0
        ir_ref if_reference = ir_IF(ir_EQ(prop_type_ref, ir_CONST_U32(IS_REFERENCE_EX)));
14613
0
        ir_IF_FALSE(if_reference);
14614
0
        if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
14615
0
          ref = ir_CONST_ADDR(prop_info);
14616
0
        } else {
14617
0
          int prop_info_offset =
14618
0
            (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
14619
14620
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
14621
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
14622
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
14623
0
        }
14624
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_create_typed_ref),
14625
0
          prop_ref,
14626
0
          ref,
14627
0
          jit_ZVAL_ADDR(jit, res_addr));
14628
0
        ir_END_list(end_inputs);
14629
0
        ir_IF_TRUE(if_reference);
14630
0
      } else {
14631
0
        ZEND_UNREACHABLE();
14632
0
      }
14633
0
    }
14634
0
  }
14635
14636
0
  if (opline->opcode == ZEND_FETCH_OBJ_W) {
14637
0
    ZEND_ASSERT(prop_ref);
14638
0
    jit_set_Z_PTR(jit, res_addr, prop_ref);
14639
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
14640
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && prop_info) {
14641
0
      ssa->var_info[ssa_op->result_def].indirect_reference = 1;
14642
0
    }
14643
0
    ir_END_list(end_inputs);
14644
0
  } else {
14645
0
    if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame) && prop_info) {
14646
0
      ir_END_PHI_list(end_values, jit_ZVAL_ADDR(jit, prop_addr));
14647
0
    } else if ((res_info & MAY_BE_GUARD) && Z_MODE(res_addr) == IS_REG) {
14648
0
      ir_END_PHI_list(end_values, jit_ZVAL_ADDR(jit, prop_addr));
14649
0
    } else if (Z_MODE(res_addr) == IS_REG) {
14650
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14651
14652
0
      if (!zend_jit_zval_copy_deref_reg(jit, res_addr, res_info & ~MAY_BE_GUARD, prop_addr, prop_type_ref, &end_values)) {
14653
0
        return 0;
14654
0
      }
14655
0
    } else {
14656
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14657
14658
0
      if (!zend_jit_zval_copy_deref(jit, res_addr, prop_addr, prop_type_ref)) {
14659
0
        return 0;
14660
0
      }
14661
0
      ir_END_list(end_inputs);
14662
0
    }
14663
0
  }
14664
14665
0
result_fetched:
14666
0
  if (op1_avoid_refcounting) {
14667
0
    SET_STACK_REG(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
14668
0
  }
14669
14670
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE || !prop_info) {
14671
0
    ir_MERGE_list(slow_inputs);
14672
0
    jit_SET_EX_OPLINE(jit, opline);
14673
14674
0
    op1_info |= MAY_BE_RC1 | MAY_BE_RCN; /* object may be captured/released in magic handler */
14675
0
    if (opline->opcode == ZEND_FETCH_OBJ_W) {
14676
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_w_slow), obj_ref);
14677
0
      ir_END_list(end_inputs);
14678
0
    } else if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14679
0
      if (Z_MODE(res_addr) == IS_REG) {
14680
0
        ir_ref val_ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_slow_ex), obj_ref);
14681
0
        ir_END_PHI_list(end_values, val_ref);
14682
0
      } else {
14683
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_slow), obj_ref);
14684
0
        ir_END_list(end_inputs);
14685
0
      }
14686
0
    } else {
14687
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_slow), obj_ref);
14688
0
      ir_END_list(end_inputs);
14689
0
    }
14690
0
  }
14691
14692
0
  if (end_values) {
14693
0
    ir_ref val_ref = ir_PHI_list(end_values);
14694
0
    zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val_ref);
14695
0
    bool result_avoid_refcounting = false;
14696
14697
0
    ZEND_ASSERT(opline->opcode == ZEND_FETCH_OBJ_R
14698
0
      || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
14699
0
      || opline->opcode == ZEND_FETCH_OBJ_IS);
14700
0
    ZEND_ASSERT(end_inputs == IR_UNUSED);
14701
0
    if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
14702
0
      uint8_t type = concrete_type(res_info);
14703
0
      uint32_t flags = ZEND_JIT_EXIT_CHECK_EXCEPTION;
14704
14705
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
14706
0
       && !delayed_fetch_this
14707
0
       && !op1_avoid_refcounting) {
14708
0
        flags |= ZEND_JIT_EXIT_FREE_OP1;
14709
0
      }
14710
14711
0
      if ((opline->result_type & (IS_VAR|IS_TMP_VAR))
14712
0
       && !(flags & ZEND_JIT_EXIT_FREE_OP1)
14713
0
       && (res_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
14714
0
       && (ssa_op+1)->op1_use == ssa_op->result_def
14715
0
       && zend_jit_may_avoid_refcounting(opline+1, res_info)) {
14716
0
        result_avoid_refcounting = true;
14717
0
        ssa->var_info[ssa_op->result_def].avoid_refcounting = 1;
14718
0
      }
14719
14720
0
      val_addr = zend_jit_guard_fetch_result_type(jit, opline, val_addr, type,
14721
0
        true, flags, op1_avoid_refcounting);
14722
0
      if (!val_addr) {
14723
0
        return 0;
14724
0
      }
14725
14726
0
      res_info &= ~MAY_BE_GUARD;
14727
0
      ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
14728
0
    }
14729
14730
    // ZVAL_COPY
14731
0
    jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, !result_avoid_refcounting);
14732
14733
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
14734
0
      return 0;
14735
0
    }
14736
0
  } else {
14737
0
    ir_MERGE_list(end_inputs);
14738
0
  }
14739
14740
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
14741
0
    if (opline->op1_type == IS_VAR
14742
0
     && opline->opcode == ZEND_FETCH_OBJ_W
14743
0
     && (op1_info & MAY_BE_RC1)) {
14744
0
      zend_jit_addr orig_op1_addr = OP1_ADDR();
14745
0
      ir_ref if_refcounted, ptr, refcount, if_non_zero;
14746
0
      ir_ref merge_inputs = IR_UNUSED;
14747
14748
0
      if_refcounted = jit_if_REFCOUNTED(jit, orig_op1_addr);
14749
0
      ir_IF_FALSE( if_refcounted);
14750
0
      ir_END_list(merge_inputs);
14751
0
      ir_IF_TRUE( if_refcounted);
14752
0
      ptr = jit_Z_PTR(jit, orig_op1_addr);
14753
0
      refcount = jit_GC_DELREF(jit, ptr);
14754
0
      if_non_zero = ir_IF(refcount);
14755
0
      ir_IF_TRUE( if_non_zero);
14756
0
      ir_END_list(merge_inputs);
14757
0
      ir_IF_FALSE( if_non_zero);
14758
0
      jit_SET_EX_OPLINE(jit, opline);
14759
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_extract_helper), ptr);
14760
0
      ir_END_list(merge_inputs);
14761
0
      ir_MERGE_list(merge_inputs);
14762
0
    } else if (!op1_avoid_refcounting) {
14763
0
      if (on_this) {
14764
0
        op1_info &= ~MAY_BE_RC1;
14765
0
      }
14766
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
14767
0
    }
14768
0
  }
14769
14770
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
14771
0
   && prop_info
14772
0
   && (opline->opcode != ZEND_FETCH_OBJ_W ||
14773
0
       !(opline->extended_value & ZEND_FETCH_OBJ_FLAGS) ||
14774
0
       !ZEND_TYPE_IS_SET(prop_info->type))
14775
0
   && (!(opline->op1_type & (IS_VAR|IS_TMP_VAR)) || on_this || op1_indirect)) {
14776
0
    may_throw = 0;
14777
0
  }
14778
14779
0
  if (may_throw) {
14780
0
    if (Z_MODE(res_addr) == IS_REG) {
14781
0
      zend_jit_check_exception_undef_result(jit, opline);
14782
0
    } else {
14783
0
      zend_jit_check_exception(jit);
14784
0
    }
14785
0
  }
14786
14787
0
  return 1;
14788
0
}
14789
14790
static int zend_jit_assign_obj(zend_jit_ctx         *jit,
14791
                               const zend_op        *opline,
14792
                               const zend_op_array  *op_array,
14793
                               zend_ssa             *ssa,
14794
                               const zend_ssa_op    *ssa_op,
14795
                               uint32_t              op1_info,
14796
                               zend_jit_addr         op1_addr,
14797
                               uint32_t              val_info,
14798
                               zend_jit_addr         val_addr,
14799
                               zend_jit_addr         val_def_addr,
14800
                               zend_jit_addr         res_addr,
14801
                               bool                  op1_indirect,
14802
                               zend_class_entry     *ce,
14803
                               bool                  ce_is_instanceof,
14804
                               bool                  on_this,
14805
                               bool                  delayed_fetch_this,
14806
                               zend_class_entry     *trace_ce,
14807
                               uint8_t               prop_type,
14808
                               int                   may_throw)
14809
0
{
14810
0
  zval *member;
14811
0
  zend_string *name;
14812
0
  zend_property_info *prop_info;
14813
0
  zend_jit_addr prop_addr;
14814
0
  ir_ref obj_ref = IR_UNUSED;
14815
0
  ir_ref prop_ref = IR_UNUSED;
14816
0
  ir_ref delayed_end_input = IR_UNUSED;
14817
0
  ir_ref end_inputs = IR_UNUSED;
14818
0
  ir_ref slow_inputs = IR_UNUSED;
14819
0
  uint32_t res_info = RES_INFO();
14820
14821
0
  if (Z_MODE(val_addr) == IS_REG
14822
0
   && Z_LOAD(val_addr)
14823
0
   && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
14824
    /* Force load */
14825
0
    zend_jit_use_reg(jit, val_addr);
14826
0
  }
14827
14828
0
  if (val_addr != val_def_addr && val_def_addr) {
14829
0
    if (!zend_jit_update_regs(jit, (opline+1)->op1.var, val_addr, val_def_addr, val_info)) {
14830
0
      return 0;
14831
0
    }
14832
0
    if (Z_MODE(val_def_addr) == IS_REG && Z_MODE(val_addr) != IS_REG) {
14833
0
      val_addr = val_def_addr;
14834
0
    }
14835
0
  }
14836
14837
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
14838
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
14839
14840
0
  member = RT_CONSTANT(opline, opline->op2);
14841
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
14842
0
  name = Z_STR_P(member);
14843
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
14844
14845
0
  if (on_this) {
14846
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14847
0
    obj_ref = jit_Z_PTR(jit, this_addr);
14848
0
  } else {
14849
0
    if (opline->op1_type == IS_VAR
14850
0
     && (op1_info & MAY_BE_INDIRECT)
14851
0
     && Z_REG(op1_addr) == ZREG_FP) {
14852
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
14853
0
    }
14854
0
    if (op1_info & MAY_BE_REF) {
14855
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
14856
0
    }
14857
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
14858
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14859
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14860
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14861
14862
0
        if (!exit_addr) {
14863
0
          return 0;
14864
0
        }
14865
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
14866
0
      } else {
14867
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
14868
0
        ir_IF_FALSE_cold(if_obj);
14869
14870
0
        jit_SET_EX_OPLINE(jit, opline);
14871
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_assign),
14872
0
          jit_ZVAL_ADDR(jit, op1_addr),
14873
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
14874
14875
0
        if (RETURN_VALUE_USED(opline) && Z_MODE(res_addr) != IS_REG) {
14876
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14877
0
        }
14878
14879
0
        ir_END_list(end_inputs);
14880
14881
0
        ir_IF_TRUE(if_obj);
14882
0
      }
14883
0
    }
14884
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
14885
0
  }
14886
14887
0
  ZEND_ASSERT(obj_ref);
14888
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
14889
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
14890
0
    if (prop_info) {
14891
0
      ce = trace_ce;
14892
0
      ce_is_instanceof = false;
14893
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
14894
0
        if (on_this && JIT_G(current_frame)
14895
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
14896
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
14897
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
14898
0
          if (on_this && JIT_G(current_frame)) {
14899
0
            JIT_G(current_frame)->ce = ce;
14900
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
14901
0
          }
14902
0
        } else {
14903
0
          return 0;
14904
0
        }
14905
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
14906
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
14907
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
14908
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
14909
0
        }
14910
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
14911
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
14912
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
14913
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
14914
0
        }
14915
0
      }
14916
0
    }
14917
0
  }
14918
14919
0
  if (!prop_info) {
14920
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
14921
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
14922
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
14923
14924
0
    ir_IF_FALSE_cold(if_same);
14925
0
    ir_END_list(slow_inputs);
14926
14927
0
    ir_IF_TRUE(if_same);
14928
0
    ir_ref offset_ref = ir_LOAD_A(
14929
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
14930
14931
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
14932
0
    ir_IF_TRUE_cold(if_dynamic);
14933
0
    ir_END_list(slow_inputs);
14934
14935
0
    ir_IF_FALSE(if_dynamic);
14936
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
14937
0
    ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
14938
0
    ir_IF_FALSE_cold(if_def);
14939
0
    ir_END_list(slow_inputs);
14940
14941
0
    ir_IF_TRUE(if_def);
14942
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14943
14944
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
14945
0
      ir_ref arg3, arg4;
14946
0
      ir_ref prop_info_ref = ir_LOAD_A(
14947
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
14948
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
14949
0
      ir_IF_TRUE_cold(if_has_prop_info);
14950
14951
0
      if (Z_MODE(val_addr) == IS_REG) {
14952
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
14953
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
14954
0
          return 0;
14955
0
        }
14956
0
        arg3 = jit_ZVAL_ADDR(jit, real_addr);
14957
0
      } else {
14958
0
        arg3 = jit_ZVAL_ADDR(jit, val_addr);
14959
0
      }
14960
14961
0
      if (!RETURN_VALUE_USED(opline)) {
14962
0
        arg4 = IR_NULL;
14963
0
      } else if (Z_MODE(res_addr) == IS_REG) {
14964
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
14965
0
        arg4 = jit_ZVAL_ADDR(jit, real_addr);
14966
0
      } else {
14967
0
        arg4 = jit_ZVAL_ADDR(jit, res_addr);
14968
0
      }
14969
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
14970
0
      jit_SET_EX_OPLINE(jit, opline);
14971
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_to_typed_prop),
14972
0
        prop_ref,
14973
0
        prop_info_ref,
14974
0
        arg3,
14975
0
        arg4);
14976
14977
0
      if ((opline+1)->op1_type == IS_CONST) {
14978
        // TODO: ???
14979
        // if (Z_TYPE_P(value) == orig_type) {
14980
        // CACHE_PTR_EX(cache_slot + 2, NULL);
14981
0
      }
14982
14983
0
      ir_END_list(end_inputs);
14984
0
      ir_IF_FALSE(if_has_prop_info);
14985
0
    }
14986
0
  } else {
14987
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
14988
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14989
    /* With the exception of __clone(), readonly assignment always happens on IS_UNDEF, doding
14990
     * the fast path. Thus, the fast path is not useful. */
14991
0
    if (prop_info->flags & ZEND_ACC_READONLY) {
14992
0
      ZEND_ASSERT(slow_inputs == IR_UNUSED);
14993
0
      goto slow_path;
14994
0
    }
14995
14996
    // Undefined property with potential magic __get()/__set() or lazy object
14997
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && prop_type != IS_UNDEF) {
14998
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14999
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15000
15001
0
      if (!exit_addr) {
15002
0
        return 0;
15003
0
      }
15004
0
      ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15005
0
    } else {
15006
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15007
0
      ir_IF_FALSE_cold(if_def);
15008
0
      ir_END_list(slow_inputs);
15009
0
      ir_IF_TRUE(if_def);
15010
0
    }
15011
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15012
0
      ir_ref ref, arg3, arg4;
15013
15014
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
15015
0
      jit_SET_EX_OPLINE(jit, opline);
15016
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15017
0
        ref = ir_CONST_ADDR(prop_info);
15018
0
      } else {
15019
0
        int prop_info_offset =
15020
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15021
15022
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15023
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15024
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15025
0
      }
15026
0
      if (Z_MODE(val_addr) == IS_REG) {
15027
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15028
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15029
0
          return 0;
15030
0
        }
15031
0
        arg3 = jit_ZVAL_ADDR(jit, real_addr);
15032
0
      } else {
15033
0
        arg3 = jit_ZVAL_ADDR(jit, val_addr);
15034
0
      }
15035
0
      if (!RETURN_VALUE_USED(opline)) {
15036
0
        arg4 = IR_NULL;
15037
0
      } else if (Z_MODE(res_addr) == IS_REG) {
15038
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15039
0
        arg4 = jit_ZVAL_ADDR(jit, real_addr);
15040
0
      } else {
15041
0
        arg4 = jit_ZVAL_ADDR(jit, res_addr);
15042
0
      }
15043
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_to_typed_prop),
15044
0
        prop_ref,
15045
0
        ref,
15046
0
        arg3,
15047
0
        arg4);
15048
15049
0
      ir_END_list(end_inputs);
15050
0
    }
15051
0
  }
15052
15053
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15054
0
    if (Z_MODE(val_addr) != IS_REG
15055
0
     && (res_addr == 0 || Z_MODE(res_addr) != IS_REG)
15056
0
     && opline->result_type == IS_UNUSED) {
15057
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)) {
15058
0
        return 0;
15059
0
      }
15060
0
    } else {
15061
0
      zend_jit_addr real_res_addr;
15062
15063
0
      if (res_addr && Z_MODE(res_addr) == IS_REG) {
15064
0
        real_res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15065
0
      } else {
15066
0
        real_res_addr = res_addr;
15067
0
      }
15068
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)) {
15069
0
        return 0;
15070
0
      }
15071
0
    }
15072
0
    if (end_inputs || slow_inputs) {
15073
0
      if (((opline+1)->op1_type & (IS_VAR|IS_TMP_VAR))
15074
0
       && (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15075
        /* skip FREE_OP_DATA() */
15076
0
        delayed_end_input = ir_END();
15077
0
      } else {
15078
0
        ir_END_list(end_inputs);
15079
0
      }
15080
0
    }
15081
0
  }
15082
15083
0
  if (slow_inputs) {
15084
0
    ir_ref arg3, arg5;
15085
15086
0
    ir_MERGE_list(slow_inputs);
15087
15088
0
slow_path:
15089
0
    jit_SET_EX_OPLINE(jit, opline);
15090
15091
0
    if (Z_MODE(val_addr) == IS_REG) {
15092
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15093
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15094
0
        return 0;
15095
0
      }
15096
0
      arg3 = jit_ZVAL_ADDR(jit, real_addr);
15097
0
    } else {
15098
0
      arg3 = jit_ZVAL_ADDR(jit, val_addr);
15099
0
    }
15100
0
    if (!RETURN_VALUE_USED(opline)) {
15101
0
      arg5 = IR_NULL;
15102
0
    } else if (Z_MODE(res_addr) == IS_REG) {
15103
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15104
0
      arg5 = jit_ZVAL_ADDR(jit, real_addr);
15105
0
    } else {
15106
0
      arg5 = jit_ZVAL_ADDR(jit, res_addr);
15107
0
    }
15108
15109
    // JIT: value = zobj->handlers->write_property(zobj, name, value, CACHE_ADDR(opline->extended_value));
15110
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15111
0
    ir_CALL_5(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_obj_helper),
15112
0
      obj_ref,
15113
0
      ir_CONST_ADDR(name),
15114
0
      arg3,
15115
0
      ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15116
0
      arg5);
15117
15118
0
    ir_END_list(end_inputs);
15119
0
  }
15120
15121
0
  if (end_inputs) {
15122
0
    ir_MERGE_list(end_inputs);
15123
15124
0
    if (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15125
0
      val_info |= MAY_BE_RC1|MAY_BE_RCN;
15126
0
    }
15127
0
    jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, opline);
15128
15129
0
    if (delayed_end_input) {
15130
0
      ir_MERGE_WITH(delayed_end_input);
15131
0
    }
15132
0
  }
15133
15134
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
15135
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
15136
0
  }
15137
15138
0
  if (RETURN_VALUE_USED(opline) && Z_MODE(res_addr) == IS_REG) {
15139
0
    zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15140
0
    if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
15141
0
      return 0;
15142
0
    }
15143
0
  }
15144
15145
0
  if (may_throw) {
15146
0
    zend_jit_check_exception(jit);
15147
0
  }
15148
15149
0
  return 1;
15150
0
}
15151
15152
static int zend_jit_assign_obj_op(zend_jit_ctx         *jit,
15153
                                  const zend_op        *opline,
15154
                                  const zend_op_array  *op_array,
15155
                                  zend_ssa             *ssa,
15156
                                  const zend_ssa_op    *ssa_op,
15157
                                  uint32_t              op1_info,
15158
                                  zend_jit_addr         op1_addr,
15159
                                  uint32_t              val_info,
15160
                                  zend_jit_addr         val_addr,
15161
                                  zend_ssa_range       *val_range,
15162
                                  bool                  op1_indirect,
15163
                                  zend_class_entry     *ce,
15164
                                  bool                  ce_is_instanceof,
15165
                                  bool                  on_this,
15166
                                  bool                  delayed_fetch_this,
15167
                                  zend_class_entry     *trace_ce,
15168
                                  uint8_t               prop_type)
15169
0
{
15170
0
  zval *member;
15171
0
  zend_string *name;
15172
0
  zend_property_info *prop_info;
15173
0
  zend_jit_addr prop_addr;
15174
0
  bool use_prop_guard = false;
15175
0
  bool may_throw = false;
15176
0
  binary_op_type binary_op = get_binary_op(opline->extended_value);
15177
0
  ir_ref obj_ref = IR_UNUSED;
15178
0
  ir_ref prop_ref = IR_UNUSED;
15179
0
  ir_ref end_inputs = IR_UNUSED;
15180
0
  ir_ref slow_inputs = IR_UNUSED;
15181
15182
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
15183
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
15184
0
  ZEND_ASSERT(opline->result_type == IS_UNUSED);
15185
15186
0
  member = RT_CONSTANT(opline, opline->op2);
15187
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
15188
0
  name = Z_STR_P(member);
15189
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
15190
15191
0
  if (on_this) {
15192
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
15193
0
    obj_ref = jit_Z_PTR(jit, this_addr);
15194
0
  } else {
15195
0
    if (opline->op1_type == IS_VAR
15196
0
     && (op1_info & MAY_BE_INDIRECT)
15197
0
     && Z_REG(op1_addr) == ZREG_FP) {
15198
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
15199
0
    }
15200
0
    if (op1_info & MAY_BE_REF) {
15201
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
15202
0
    }
15203
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
15204
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15205
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15206
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15207
15208
0
        if (!exit_addr) {
15209
0
          return 0;
15210
0
        }
15211
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
15212
0
      } else {
15213
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
15214
0
        ir_IF_FALSE_cold(if_obj);
15215
15216
0
        jit_SET_EX_OPLINE(jit, opline);
15217
0
        ir_CALL_2(IR_VOID,
15218
0
          (op1_info & MAY_BE_UNDEF) ?
15219
0
            ir_CONST_FC_FUNC(zend_jit_invalid_property_assign_op) :
15220
0
            ir_CONST_FC_FUNC(zend_jit_invalid_property_assign),
15221
0
          jit_ZVAL_ADDR(jit, op1_addr),
15222
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
15223
15224
0
        may_throw = true;
15225
15226
0
        ir_END_list(end_inputs);
15227
0
        ir_IF_TRUE(if_obj);
15228
0
      }
15229
0
    }
15230
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
15231
0
  }
15232
15233
0
  ZEND_ASSERT(obj_ref);
15234
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
15235
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
15236
0
    if (prop_info) {
15237
0
      ce = trace_ce;
15238
0
      ce_is_instanceof = false;
15239
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
15240
0
        if (on_this && JIT_G(current_frame)
15241
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
15242
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
15243
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
15244
0
          if (on_this && JIT_G(current_frame)) {
15245
0
            JIT_G(current_frame)->ce = ce;
15246
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
15247
0
          }
15248
0
        } else {
15249
0
          return 0;
15250
0
        }
15251
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
15252
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
15253
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
15254
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
15255
0
        }
15256
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
15257
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
15258
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
15259
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
15260
0
        }
15261
0
      }
15262
0
    }
15263
0
  }
15264
15265
0
  use_prop_guard = (prop_type != IS_UNKNOWN
15266
0
    && prop_type != IS_UNDEF
15267
0
    && prop_type != IS_REFERENCE
15268
0
    && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_OBJECT);
15269
15270
0
  if (Z_MODE(val_addr) == IS_REG
15271
0
   && Z_LOAD(val_addr)
15272
0
   && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
15273
    /* Force load */
15274
0
    zend_jit_use_reg(jit, val_addr);
15275
0
  }
15276
15277
0
  if (!prop_info) {
15278
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15279
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, (opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
15280
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
15281
15282
0
    ir_IF_FALSE_cold(if_same);
15283
0
    ir_END_list(slow_inputs);
15284
15285
0
    ir_IF_TRUE(if_same);
15286
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
15287
0
      ir_ref prop_info_ref = ir_LOAD_A(
15288
0
        ir_ADD_OFFSET(run_time_cache, ((opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
15289
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
15290
0
      ir_IF_TRUE_cold(if_has_prop_info);
15291
0
      ir_END_list(slow_inputs);
15292
15293
0
      ir_IF_FALSE(if_has_prop_info);
15294
0
    }
15295
0
    ir_ref offset_ref = ir_LOAD_A(
15296
0
      ir_ADD_OFFSET(run_time_cache, ((opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
15297
15298
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
15299
0
    ir_IF_TRUE_cold(if_dynamic);
15300
0
    ir_END_list(slow_inputs);
15301
15302
0
    ir_IF_FALSE(if_dynamic);
15303
15304
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
15305
0
    if (!use_prop_guard) {
15306
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
15307
0
      ir_IF_FALSE_cold(if_def);
15308
0
      ir_END_list(slow_inputs);
15309
15310
0
      ir_IF_TRUE(if_def);
15311
0
    }
15312
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15313
0
  } else {
15314
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
15315
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15316
15317
0
    if (ZEND_TYPE_IS_SET(prop_info->type) || !use_prop_guard) {
15318
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15319
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15320
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15321
15322
0
        if (!exit_addr) {
15323
0
          return 0;
15324
0
        }
15325
0
        ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15326
0
      } else {
15327
0
        ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15328
0
        ir_IF_FALSE_cold(if_def);
15329
0
        ir_END_list(slow_inputs);
15330
0
        ir_IF_TRUE(if_def);
15331
0
      }
15332
0
    }
15333
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15334
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref, arg2;
15335
15336
0
      may_throw = true;
15337
15338
0
      jit_SET_EX_OPLINE(jit, opline);
15339
15340
0
      if (Z_MODE(val_addr) == IS_REG) {
15341
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15342
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15343
0
          return 0;
15344
0
        }
15345
0
        arg2 = jit_ZVAL_ADDR(jit, real_addr);
15346
0
      } else {
15347
0
        arg2 = jit_ZVAL_ADDR(jit, val_addr);
15348
0
      }
15349
15350
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15351
0
      ir_IF_FALSE(if_ref);
15352
0
      noref_path = ir_END();
15353
0
      ir_IF_TRUE(if_ref);
15354
15355
0
      reference = jit_Z_PTR(jit, prop_addr);
15356
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15357
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15358
0
      ir_IF_FALSE(if_typed);
15359
0
      ref_path = ir_END();
15360
0
      ir_IF_TRUE_cold(if_typed);
15361
15362
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
15363
0
        reference,
15364
0
        arg2,
15365
0
        ir_CONST_FC_FUNC(binary_op));
15366
15367
0
      ir_END_list(end_inputs);
15368
15369
0
      ir_MERGE_2(noref_path, ref_path);
15370
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15371
0
      prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15372
15373
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
15374
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15375
0
        ref = ir_CONST_ADDR(prop_info);
15376
0
      } else {
15377
0
        int prop_info_offset =
15378
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15379
15380
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15381
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15382
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15383
0
      }
15384
15385
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_prop),
15386
0
        prop_ref,
15387
0
        ref,
15388
0
        arg2,
15389
0
        ir_CONST_FC_FUNC(binary_op));
15390
15391
0
      ir_END_list(end_inputs);
15392
0
    }
15393
0
  }
15394
15395
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15396
0
    zend_jit_addr var_addr = prop_addr;
15397
0
    uint32_t var_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15398
0
    uint32_t var_def_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15399
15400
0
    if (use_prop_guard) {
15401
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
15402
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15403
0
      if (!exit_addr) {
15404
0
        return 0;
15405
0
      }
15406
15407
0
      jit_guard_Z_TYPE(jit, prop_addr, prop_type, exit_addr);
15408
0
      var_info = (1 << prop_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
15409
0
    }
15410
15411
0
    if (var_info & MAY_BE_REF) {
15412
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref, arg2;
15413
15414
0
      may_throw = true;
15415
15416
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15417
0
      ir_IF_FALSE(if_ref);
15418
0
      noref_path = ir_END();
15419
0
      ir_IF_TRUE(if_ref);
15420
15421
0
      reference = jit_Z_PTR(jit, var_addr);
15422
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15423
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15424
0
      ir_IF_FALSE(if_typed);
15425
0
      ref_path = ir_END();
15426
0
      ir_IF_TRUE_cold(if_typed);
15427
15428
0
      jit_SET_EX_OPLINE(jit, opline);
15429
15430
0
      if (Z_MODE(val_addr) == IS_REG) {
15431
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15432
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15433
0
          return 0;
15434
0
        }
15435
0
        arg2 = jit_ZVAL_ADDR(jit, real_addr);
15436
0
      } else {
15437
0
        arg2 = jit_ZVAL_ADDR(jit, val_addr);
15438
0
      }
15439
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
15440
0
        reference,
15441
0
        arg2,
15442
0
        ir_CONST_FC_FUNC(binary_op));
15443
15444
0
      ir_END_list(end_inputs);
15445
15446
0
      ir_MERGE_2(noref_path, ref_path);
15447
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15448
0
      var_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15449
15450
0
      var_info &= ~MAY_BE_REF;
15451
0
    }
15452
15453
0
    uint8_t val_op_type = (opline+1)->op1_type;
15454
0
    if (val_op_type & (IS_TMP_VAR|IS_VAR)) {
15455
      /* prevent FREE_OP in the helpers */
15456
0
      val_op_type = IS_CV;
15457
0
    }
15458
15459
0
    switch (opline->extended_value) {
15460
0
      case ZEND_ADD:
15461
0
      case ZEND_SUB:
15462
0
      case ZEND_MUL:
15463
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15464
0
            (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15465
0
          if (opline->extended_value != ZEND_ADD ||
15466
0
              (var_info & MAY_BE_ANY) != MAY_BE_ARRAY ||
15467
0
              (val_info & MAY_BE_ANY) == MAY_BE_ARRAY) {
15468
0
            may_throw = true;
15469
0
          }
15470
0
        }
15471
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,
15472
0
            1 /* may overflow */, 0)) {
15473
0
          return 0;
15474
0
        }
15475
0
        break;
15476
0
      case ZEND_BW_OR:
15477
0
      case ZEND_BW_AND:
15478
0
      case ZEND_BW_XOR:
15479
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15480
0
            (val_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15481
0
          if ((var_info & MAY_BE_ANY) != MAY_BE_STRING ||
15482
0
              (val_info & MAY_BE_ANY) != MAY_BE_STRING) {
15483
0
            may_throw = true;
15484
0
          }
15485
0
        }
15486
0
        goto long_math;
15487
0
      case ZEND_SL:
15488
0
      case ZEND_SR:
15489
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15490
0
            (val_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15491
0
          may_throw = true;
15492
0
        }
15493
0
        if (val_op_type != IS_CONST ||
15494
0
            Z_TYPE_P(RT_CONSTANT((opline+1), (opline+1)->op1)) != IS_LONG ||
15495
0
            Z_LVAL_P(RT_CONSTANT((opline+1), (opline+1)->op1)) < 0) {
15496
0
          may_throw = true;
15497
0
        }
15498
0
        goto long_math;
15499
0
      case ZEND_MOD:
15500
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15501
0
            (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15502
0
          may_throw = true;
15503
0
        }
15504
0
        if (val_op_type != IS_CONST ||
15505
0
            Z_TYPE_P(RT_CONSTANT((opline+1), (opline+1)->op1)) != IS_LONG ||
15506
0
            Z_LVAL_P(RT_CONSTANT((opline+1), (opline+1)->op1)) == 0) {
15507
0
          may_throw = true;
15508
0
        }
15509
0
long_math:
15510
0
        if (!zend_jit_long_math_helper(jit, opline, opline->extended_value,
15511
0
            IS_CV, opline->op1, var_addr, var_info, NULL,
15512
0
            val_op_type, (opline+1)->op1, val_addr, val_info,
15513
0
            val_range,
15514
0
            0, var_addr, var_def_info, var_info, /* may throw */ 1)) {
15515
0
          return 0;
15516
0
        }
15517
0
        break;
15518
0
      case ZEND_CONCAT:
15519
0
        may_throw = true;
15520
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,
15521
0
            0)) {
15522
0
          return 0;
15523
0
        }
15524
0
        break;
15525
0
      default:
15526
0
        ZEND_UNREACHABLE();
15527
0
    }
15528
0
    if (end_inputs || slow_inputs) {
15529
0
      ir_END_list(end_inputs);
15530
0
    }
15531
0
  }
15532
15533
0
  if (slow_inputs) {
15534
0
    ir_ref arg3;
15535
15536
0
    ir_MERGE_list(slow_inputs);
15537
15538
0
    may_throw = true;
15539
15540
0
    if (Z_MODE(val_addr) == IS_REG) {
15541
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15542
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15543
0
        return 0;
15544
0
      }
15545
0
      arg3 = jit_ZVAL_ADDR(jit, real_addr);
15546
0
    } else {
15547
0
      arg3 = jit_ZVAL_ADDR(jit, val_addr);
15548
0
    }
15549
0
    jit_SET_EX_OPLINE(jit, opline);
15550
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15551
0
    ir_CALL_5(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_obj_op_helper),
15552
0
      obj_ref,
15553
0
      ir_CONST_ADDR(name),
15554
0
      arg3,
15555
0
      ir_ADD_OFFSET(run_time_cache, (opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15556
0
      ir_CONST_FC_FUNC(binary_op));
15557
15558
0
    ir_END_list(end_inputs);
15559
0
  }
15560
15561
0
  if (end_inputs) {
15562
0
    ir_MERGE_list(end_inputs);
15563
0
  }
15564
15565
0
  if (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15566
0
    val_info |= MAY_BE_RC1|MAY_BE_RCN;
15567
0
  }
15568
15569
  // JIT: FREE_OP_DATA();
15570
0
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, opline);
15571
15572
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
15573
0
    if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
15574
0
      may_throw = true;
15575
0
    }
15576
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
15577
0
  }
15578
15579
0
  if (may_throw) {
15580
0
    zend_jit_check_exception(jit);
15581
0
  }
15582
15583
0
  return 1;
15584
0
}
15585
15586
static int zend_jit_incdec_obj(zend_jit_ctx         *jit,
15587
                               const zend_op        *opline,
15588
                               const zend_op_array  *op_array,
15589
                               zend_ssa             *ssa,
15590
                               const zend_ssa_op    *ssa_op,
15591
                               uint32_t              op1_info,
15592
                               zend_jit_addr         op1_addr,
15593
                               bool                  op1_indirect,
15594
                               zend_class_entry     *ce,
15595
                               bool                  ce_is_instanceof,
15596
                               bool                  on_this,
15597
                               bool                  delayed_fetch_this,
15598
                               zend_class_entry     *trace_ce,
15599
                               uint8_t               prop_type)
15600
0
{
15601
0
  zval *member;
15602
0
  zend_string *name;
15603
0
  zend_property_info *prop_info;
15604
0
  zend_jit_addr res_addr = 0;
15605
0
  zend_jit_addr prop_addr;
15606
0
  bool use_prop_guard = false;
15607
0
  bool may_throw = false;
15608
0
  uint32_t res_info = (opline->result_type != IS_UNDEF) ? RES_INFO() : 0;
15609
0
  ir_ref obj_ref = IR_UNUSED;
15610
0
  ir_ref prop_ref = IR_UNUSED;
15611
0
  ir_ref end_inputs = IR_UNUSED;
15612
0
  ir_ref slow_inputs = IR_UNUSED;
15613
15614
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
15615
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
15616
15617
0
  if (opline->result_type != IS_UNUSED) {
15618
0
    res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15619
0
  }
15620
15621
0
  member = RT_CONSTANT(opline, opline->op2);
15622
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
15623
0
  name = Z_STR_P(member);
15624
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
15625
15626
0
  if (on_this) {
15627
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
15628
0
    obj_ref = jit_Z_PTR(jit, this_addr);
15629
0
  } else {
15630
0
    if (opline->op1_type == IS_VAR
15631
0
     && (op1_info & MAY_BE_INDIRECT)
15632
0
     && Z_REG(op1_addr) == ZREG_FP) {
15633
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
15634
0
    }
15635
0
    if (op1_info & MAY_BE_REF) {
15636
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
15637
0
    }
15638
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
15639
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15640
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15641
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15642
15643
0
        if (!exit_addr) {
15644
0
          return 0;
15645
0
        }
15646
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
15647
0
      } else {
15648
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
15649
0
        ir_IF_FALSE_cold(if_obj);
15650
15651
0
        jit_SET_EX_OPLINE(jit, opline);
15652
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_incdec),
15653
0
          jit_ZVAL_ADDR(jit, op1_addr),
15654
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
15655
15656
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
15657
0
        ir_IF_TRUE(if_obj);
15658
0
      }
15659
0
    }
15660
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
15661
0
  }
15662
15663
0
  ZEND_ASSERT(obj_ref);
15664
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
15665
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
15666
0
    if (prop_info) {
15667
0
      ce = trace_ce;
15668
0
      ce_is_instanceof = false;
15669
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
15670
0
        if (on_this && JIT_G(current_frame)
15671
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
15672
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
15673
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
15674
0
          if (on_this && JIT_G(current_frame)) {
15675
0
            JIT_G(current_frame)->ce = ce;
15676
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
15677
0
          }
15678
0
        } else {
15679
0
          return 0;
15680
0
        }
15681
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
15682
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
15683
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
15684
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
15685
0
        }
15686
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
15687
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
15688
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
15689
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
15690
0
        }
15691
0
      }
15692
0
    }
15693
0
  }
15694
15695
0
  use_prop_guard = (prop_type != IS_UNKNOWN
15696
0
    && prop_type != IS_UNDEF
15697
0
    && prop_type != IS_REFERENCE
15698
0
    && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_OBJECT);
15699
15700
0
  if (!prop_info) {
15701
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15702
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
15703
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
15704
15705
0
    ir_IF_FALSE_cold(if_same);
15706
0
    ir_END_list(slow_inputs);
15707
15708
0
    ir_IF_TRUE(if_same);
15709
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
15710
0
      ir_ref prop_info_ref = ir_LOAD_A(
15711
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
15712
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
15713
0
      ir_IF_TRUE_cold(if_has_prop_info);
15714
0
      ir_END_list(slow_inputs);
15715
15716
0
      ir_IF_FALSE(if_has_prop_info);
15717
0
    }
15718
0
    ir_ref offset_ref = ir_LOAD_A(
15719
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
15720
15721
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
15722
0
    ir_IF_TRUE_cold(if_dynamic);
15723
0
    ir_END_list(slow_inputs);
15724
15725
0
    ir_IF_FALSE(if_dynamic);
15726
15727
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
15728
0
    if (!use_prop_guard) {
15729
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
15730
0
      ir_IF_FALSE_cold(if_def);
15731
0
      ir_END_list(slow_inputs);
15732
15733
0
      ir_IF_TRUE(if_def);
15734
0
    }
15735
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15736
0
  } else {
15737
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
15738
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15739
15740
0
    if (ZEND_TYPE_IS_SET(prop_info->type) || !use_prop_guard) {
15741
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15742
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15743
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15744
15745
0
        if (!exit_addr) {
15746
0
          return 0;
15747
0
        }
15748
0
        ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15749
0
      } else {
15750
0
        ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15751
0
        ir_IF_FALSE_cold(if_def);
15752
0
        ir_END_list(slow_inputs);
15753
0
        ir_IF_TRUE(if_def);
15754
0
      }
15755
0
    }
15756
15757
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15758
0
      const void *func;
15759
0
      ir_ref ref;
15760
15761
0
      may_throw = true;
15762
0
      jit_SET_EX_OPLINE(jit, opline);
15763
15764
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15765
0
        ref = ir_CONST_ADDR(prop_info);
15766
0
      } else {
15767
0
        int prop_info_offset =
15768
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15769
15770
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15771
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15772
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15773
0
      }
15774
15775
0
      if (opline->result_type == IS_UNUSED) {
15776
0
        switch (opline->opcode) {
15777
0
          case ZEND_PRE_INC_OBJ:
15778
0
          case ZEND_POST_INC_OBJ:
15779
0
            func = zend_jit_inc_typed_prop;
15780
0
            break;
15781
0
          case ZEND_PRE_DEC_OBJ:
15782
0
          case ZEND_POST_DEC_OBJ:
15783
0
            func = zend_jit_dec_typed_prop;
15784
0
            break;
15785
0
          default:
15786
0
            ZEND_UNREACHABLE();
15787
0
        }
15788
15789
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(func), prop_ref, ref);
15790
0
      } else {
15791
0
        switch (opline->opcode) {
15792
0
          case ZEND_PRE_INC_OBJ:
15793
0
            func = zend_jit_pre_inc_typed_prop;
15794
0
            break;
15795
0
          case ZEND_PRE_DEC_OBJ:
15796
0
            func = zend_jit_pre_dec_typed_prop;
15797
0
            break;
15798
0
          case ZEND_POST_INC_OBJ:
15799
0
            func = zend_jit_post_inc_typed_prop;
15800
0
            break;
15801
0
          case ZEND_POST_DEC_OBJ:
15802
0
            func = zend_jit_post_dec_typed_prop;
15803
0
            break;
15804
0
          default:
15805
0
            ZEND_UNREACHABLE();
15806
0
        }
15807
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(func),
15808
0
          prop_ref,
15809
0
          ref,
15810
0
          jit_ZVAL_ADDR(jit, res_addr));
15811
0
      }
15812
0
      ir_END_list(end_inputs);
15813
0
    }
15814
0
  }
15815
15816
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15817
0
    uint32_t var_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15818
0
    zend_jit_addr var_addr = prop_addr;
15819
0
    ir_ref if_long = IR_UNUSED;
15820
0
    ir_ref if_overflow = IR_UNUSED;
15821
15822
0
    if (use_prop_guard) {
15823
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
15824
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15825
0
      if (!exit_addr) {
15826
0
        return 0;
15827
0
      }
15828
15829
0
      jit_guard_Z_TYPE(jit, prop_addr, prop_type, exit_addr);
15830
0
      var_info = (1 << prop_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
15831
0
    }
15832
15833
0
    if (var_info & MAY_BE_REF) {
15834
0
      const void *func;
15835
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref;
15836
15837
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15838
0
      ir_IF_FALSE(if_ref);
15839
0
      noref_path = ir_END();
15840
0
      ir_IF_TRUE(if_ref);
15841
15842
0
      reference = jit_Z_PTR(jit, var_addr);
15843
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15844
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15845
0
      ir_IF_FALSE(if_typed);
15846
0
      ref_path = ir_END();
15847
0
      ir_IF_TRUE_cold(if_typed);
15848
15849
0
      switch (opline->opcode) {
15850
0
        case ZEND_PRE_INC_OBJ:
15851
0
          func = zend_jit_pre_inc_typed_ref;
15852
0
          break;
15853
0
        case ZEND_PRE_DEC_OBJ:
15854
0
          func = zend_jit_pre_dec_typed_ref;
15855
0
          break;
15856
0
        case ZEND_POST_INC_OBJ:
15857
0
          func = zend_jit_post_inc_typed_ref;
15858
0
          break;
15859
0
        case ZEND_POST_DEC_OBJ:
15860
0
          func = zend_jit_post_dec_typed_ref;
15861
0
          break;
15862
0
        default:
15863
0
          ZEND_UNREACHABLE();
15864
0
      }
15865
15866
0
      may_throw = true;
15867
0
      jit_SET_EX_OPLINE(jit, opline);
15868
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(func),
15869
0
        reference,
15870
0
        (opline->result_type == IS_UNUSED) ? IR_NULL : jit_ZVAL_ADDR(jit, res_addr));
15871
15872
0
      ir_END_list(end_inputs);
15873
15874
0
      ir_MERGE_2(noref_path, ref_path);
15875
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15876
0
      var_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15877
15878
0
      var_info &= ~MAY_BE_REF;
15879
0
    }
15880
15881
0
    if (var_info & MAY_BE_LONG) {
15882
0
      ir_ref addr, ref;
15883
15884
0
      if (var_info & (MAY_BE_ANY - MAY_BE_LONG)) {
15885
0
        if_long = jit_if_Z_TYPE(jit, var_addr, IS_LONG);
15886
0
        ir_IF_TRUE(if_long);
15887
0
      }
15888
15889
0
      addr = jit_ZVAL_ADDR(jit, var_addr);
15890
0
      ref = ir_LOAD_L(addr);
15891
0
      if (opline->opcode == ZEND_POST_INC_OBJ || opline->opcode == ZEND_POST_DEC_OBJ) {
15892
0
        if (opline->result_type != IS_UNUSED) {
15893
0
          jit_set_Z_LVAL(jit, res_addr, ref);
15894
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
15895
0
        }
15896
0
      }
15897
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15898
0
        ref = ir_ADD_OV_L(ref, ir_CONST_LONG(1));
15899
0
      } else {
15900
0
        ref = ir_SUB_OV_L(ref, ir_CONST_LONG(1));
15901
0
      }
15902
15903
0
      ir_STORE(addr, ref);
15904
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
15905
0
      ir_IF_FALSE(if_overflow);
15906
15907
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ) {
15908
0
        if (opline->result_type != IS_UNUSED) {
15909
0
          jit_set_Z_LVAL(jit, res_addr, ref);
15910
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
15911
0
        }
15912
0
      }
15913
0
      ir_END_list(end_inputs);
15914
0
    }
15915
15916
0
    if (var_info & (MAY_BE_ANY - MAY_BE_LONG)) {
15917
0
      if (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15918
0
        may_throw = true;
15919
0
      }
15920
0
      if (if_long) {
15921
0
        ir_IF_FALSE_cold(if_long);
15922
0
      }
15923
0
      if (opline->opcode == ZEND_POST_INC_OBJ || opline->opcode == ZEND_POST_DEC_OBJ) {
15924
0
        jit_ZVAL_COPY(jit, res_addr, -1, var_addr, var_info, true);
15925
0
      }
15926
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15927
0
        if (opline->opcode == ZEND_PRE_INC_OBJ && opline->result_type != IS_UNUSED) {
15928
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_inc),
15929
0
            jit_ZVAL_ADDR(jit, var_addr),
15930
0
            jit_ZVAL_ADDR(jit, res_addr));
15931
0
        } else {
15932
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(increment_function),
15933
0
            jit_ZVAL_ADDR(jit, var_addr));
15934
0
        }
15935
0
      } else {
15936
0
        if (opline->opcode == ZEND_PRE_DEC_OBJ && opline->result_type != IS_UNUSED) {
15937
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_dec),
15938
0
            jit_ZVAL_ADDR(jit, var_addr),
15939
0
            jit_ZVAL_ADDR(jit, res_addr));
15940
0
        } else {
15941
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(decrement_function),
15942
0
            jit_ZVAL_ADDR(jit, var_addr));
15943
0
        }
15944
0
      }
15945
15946
0
      ir_END_list(end_inputs);
15947
0
    }
15948
0
    if (var_info & MAY_BE_LONG) {
15949
0
      ir_IF_TRUE_cold(if_overflow);
15950
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15951
#if SIZEOF_ZEND_LONG == 4
15952
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0));
15953
        jit_set_Z_W2(jit, var_addr, ir_CONST_U32(0x41e00000));
15954
#else
15955
0
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0x43e0000000000000));
15956
0
#endif
15957
0
        jit_set_Z_TYPE_INFO(jit, var_addr, IS_DOUBLE);
15958
0
        if (opline->opcode == ZEND_PRE_INC_OBJ && opline->result_type != IS_UNUSED) {
15959
#if SIZEOF_ZEND_LONG == 4
15960
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
15961
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
15962
#else
15963
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
15964
0
#endif
15965
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
15966
0
        }
15967
0
      } else {
15968
#if SIZEOF_ZEND_LONG == 4
15969
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0x00200000));
15970
        jit_set_Z_W2(jit, var_addr, ir_CONST_U32(0xc1e00000));
15971
#else
15972
0
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0xc3e0000000000000));
15973
0
#endif
15974
0
        jit_set_Z_TYPE_INFO(jit, var_addr, IS_DOUBLE);
15975
0
        if (opline->opcode == ZEND_PRE_DEC_OBJ && opline->result_type != IS_UNUSED) {
15976
#if SIZEOF_ZEND_LONG == 4
15977
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
15978
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
15979
#else
15980
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
15981
0
#endif
15982
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
15983
0
        }
15984
0
      }
15985
0
      if (opline->result_type != IS_UNUSED
15986
0
       && (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ)
15987
0
       && prop_info
15988
0
       && !ZEND_TYPE_IS_SET(prop_info->type)
15989
0
       && (res_info & MAY_BE_GUARD)
15990
0
       && (res_info & MAY_BE_LONG)) {
15991
0
        zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
15992
0
        uint32_t old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
15993
0
        int32_t exit_point;
15994
0
        const void *exit_addr;
15995
15996
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
15997
0
        exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
15998
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15999
0
        if (!exit_addr) {
16000
0
          return 0;
16001
0
        }
16002
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
16003
0
        ssa->var_info[ssa_op->result_def].type = res_info & ~MAY_BE_GUARD;
16004
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16005
0
      } else {
16006
0
        ir_END_list(end_inputs);
16007
0
      }
16008
0
    }
16009
0
  }
16010
16011
0
  if (slow_inputs) {
16012
0
    const void *func;
16013
16014
0
    ir_MERGE_list(slow_inputs);
16015
16016
    // JIT: zend_jit_pre_inc_obj_helper(zobj, name, CACHE_ADDR(opline->extended_value), result);
16017
0
    switch (opline->opcode) {
16018
0
      case ZEND_PRE_INC_OBJ:
16019
0
        func = zend_jit_pre_inc_obj_helper;
16020
0
        break;
16021
0
      case ZEND_PRE_DEC_OBJ:
16022
0
        func = zend_jit_pre_dec_obj_helper;
16023
0
        break;
16024
0
      case ZEND_POST_INC_OBJ:
16025
0
        func = zend_jit_post_inc_obj_helper;
16026
0
        break;
16027
0
      case ZEND_POST_DEC_OBJ:
16028
0
        func = zend_jit_post_dec_obj_helper;
16029
0
        break;
16030
0
      default:
16031
0
        ZEND_UNREACHABLE();
16032
0
    }
16033
16034
0
    may_throw = true;
16035
0
    jit_SET_EX_OPLINE(jit, opline);
16036
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
16037
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(func),
16038
0
      obj_ref,
16039
0
      ir_CONST_ADDR(name),
16040
0
      ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
16041
0
      (opline->result_type == IS_UNUSED) ? IR_NULL : jit_ZVAL_ADDR(jit, res_addr));
16042
16043
0
    ir_END_list(end_inputs);
16044
0
  }
16045
16046
0
  if (end_inputs) {
16047
0
    ir_MERGE_list(end_inputs);
16048
0
  }
16049
16050
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this && !op1_indirect) {
16051
0
    if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
16052
0
      may_throw = true;
16053
0
    }
16054
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
16055
0
  }
16056
16057
0
  if (may_throw) {
16058
0
    zend_jit_check_exception(jit);
16059
0
  }
16060
16061
0
  return 1;
16062
0
}
16063
16064
static int zend_jit_fetch_static_prop(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array)
16065
0
{
16066
0
  zend_jit_addr res_addr = RES_ADDR();
16067
0
  uint32_t cache_slot = opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS;
16068
0
  uint32_t flags;
16069
0
  ir_ref ref, ref2, if_cached, fast_path, cold_path, prop_info_ref, if_typed, if_def;
16070
0
  int fetch_type;
16071
0
  zend_property_info *known_prop_info = NULL;
16072
0
  zend_class_entry *ce;
16073
16074
0
  ce = zend_get_known_class(op_array, opline, opline->op2_type, opline->op2);
16075
0
  if (ce && (opline->op2_type == IS_CONST || !(ce->ce_flags & ZEND_ACC_TRAIT))) {
16076
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
16077
0
    zend_string *prop_name;
16078
16079
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
16080
0
    prop_name = Z_STR_P(zv);
16081
0
    zv = zend_hash_find(&ce->properties_info, prop_name);
16082
0
    if (zv) {
16083
0
      zend_property_info *prop_info = Z_PTR_P(zv);
16084
16085
0
      if (prop_info->flags & ZEND_ACC_STATIC) {
16086
0
        if (prop_info->ce == op_array->scope
16087
0
         || (prop_info->flags & ZEND_ACC_PUBLIC)
16088
0
         || ((prop_info->flags & ZEND_ACC_PROTECTED)
16089
0
          && op_array->scope
16090
0
          && instanceof_function_slow(op_array->scope, prop_info->ce))) {
16091
0
          known_prop_info = prop_info;
16092
0
        }
16093
0
      }
16094
0
    }
16095
0
  }
16096
16097
0
  switch (opline->opcode) {
16098
0
    case ZEND_FETCH_STATIC_PROP_R:
16099
0
    case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
16100
0
      fetch_type = BP_VAR_R;
16101
0
      break;
16102
0
    case ZEND_FETCH_STATIC_PROP_IS:
16103
0
      fetch_type = BP_VAR_IS;
16104
0
      break;
16105
0
    case ZEND_FETCH_STATIC_PROP_W:
16106
0
      fetch_type = BP_VAR_W;
16107
0
      break;
16108
0
    case ZEND_FETCH_STATIC_PROP_RW:
16109
0
      fetch_type = BP_VAR_RW;
16110
0
      break;
16111
0
    case ZEND_FETCH_STATIC_PROP_UNSET:
16112
0
      fetch_type = BP_VAR_UNSET;
16113
0
      break;
16114
0
    default: ZEND_UNREACHABLE();
16115
0
  }
16116
16117
  // JIT: result = CACHED_PTR(cache_slot + sizeof(void *));
16118
0
  ref = ir_LOAD_A(
16119
0
    ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*)));
16120
16121
  // JIT: if (result)
16122
0
  if_cached = ir_IF(ref);
16123
0
  ir_IF_TRUE(if_cached);
16124
16125
0
  if (fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW) {
16126
0
    if (!known_prop_info || ZEND_TYPE_IS_SET(known_prop_info->type)) {
16127
0
      ir_ref merge = IR_UNUSED;
16128
16129
      // JIT: if (UNEXPECTED(Z_TYPE_P(result) == IS_UNDEF)
16130
0
      if_typed = IR_UNUSED;
16131
0
      if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
16132
0
      ir_IF_FALSE_cold(if_def);
16133
0
      if (!known_prop_info) {
16134
        // JIT: if (ZEND_TYPE_IS_SET(property_info->type))
16135
0
        prop_info_ref = ir_LOAD_L(
16136
0
          ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*) * 2));
16137
0
        if_typed = ir_IF(ir_AND_U32(
16138
0
          ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, type.type_mask))),
16139
0
          ir_CONST_U32(_ZEND_TYPE_MASK)));
16140
0
        ir_IF_FALSE(if_typed);
16141
0
        ir_END_list(merge);
16142
0
        ir_IF_TRUE(if_typed);
16143
0
      }
16144
      // JIT: zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
16145
      //      ZSTR_VAL(property_info->ce->name),
16146
      //      zend_get_unmangled_property_name(property_info->name));
16147
0
      jit_SET_EX_OPLINE(jit, opline);
16148
0
      ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_uninit_static_prop));
16149
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
16150
16151
0
      ir_IF_TRUE(if_def);
16152
0
      if (!known_prop_info) {
16153
0
        ir_END_list(merge);
16154
0
        ir_MERGE_list(merge);
16155
0
      }
16156
0
    }
16157
0
  } else if (fetch_type == BP_VAR_W) {
16158
0
    flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
16159
0
    if (flags && (!known_prop_info || ZEND_TYPE_IS_SET(known_prop_info->type))) {
16160
0
        ir_ref merge = IR_UNUSED;
16161
16162
0
      if (!known_prop_info) {
16163
        // JIT: if (ZEND_TYPE_IS_SET(property_info->type))
16164
0
        prop_info_ref = ir_LOAD_L(
16165
0
          ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*) * 2));
16166
0
        if_typed = ir_IF(ir_AND_U32(
16167
0
          ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, type.type_mask))),
16168
0
          ir_CONST_U32(_ZEND_TYPE_MASK)));
16169
0
        ir_IF_FALSE(if_typed);
16170
0
        ir_END_list(merge);
16171
0
        ir_IF_TRUE(if_typed);
16172
0
      } else {
16173
0
        prop_info_ref = ir_CONST_ADDR(known_prop_info);
16174
0
      }
16175
16176
      // JIT: zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
16177
0
      ir_ref if_ok = ir_IF(ir_CALL_5(IR_BOOL, ir_CONST_FUNC(zend_handle_fetch_obj_flags),
16178
0
        IR_NULL, ref, IR_NULL, prop_info_ref, ir_CONST_U32(flags)));
16179
0
      ir_IF_FALSE_cold(if_ok);
16180
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
16181
0
      ir_IF_TRUE(if_ok);
16182
0
      if (!known_prop_info) {
16183
0
        ir_END_list(merge);
16184
0
        ir_MERGE_list(merge);
16185
0
      }
16186
0
    }
16187
0
  }
16188
16189
0
  fast_path = ir_END();
16190
16191
0
  ir_IF_FALSE_cold(if_cached);
16192
0
  jit_SET_EX_OPLINE(jit, opline);
16193
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_fetch_static_property), jit_FP(jit), ir_CONST_I32(fetch_type));
16194
0
  zend_jit_check_exception_undef_result(jit, opline);
16195
0
  cold_path = ir_END();
16196
16197
0
  ir_MERGE_2(fast_path, cold_path);
16198
0
  ref = ir_PHI_2(IR_ADDR, ref, ref2);
16199
16200
0
  if (fetch_type == BP_VAR_R || fetch_type == BP_VAR_IS) {
16201
    // JIT: ZVAL_COPY_DEREF(EX_VAR(opline->result.var), result);
16202
0
    if (!zend_jit_zval_copy_deref(jit, res_addr, ZEND_ADDR_REF_ZVAL(ref),
16203
0
        jit_Z_TYPE_INFO_ref(jit, ref))) {
16204
0
      return 0;
16205
0
    }
16206
0
  } else {
16207
    // JIT: ZVAL_INDIRECT(EX_VAR(opline->result.var), result);
16208
0
    jit_set_Z_PTR(jit, res_addr, ref);
16209
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
16210
0
  }
16211
16212
0
  return 1;
16213
0
}
16214
16215
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)
16216
0
{
16217
0
  HashTable *jumptable = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
16218
0
  const zend_op *next_opline = NULL;
16219
0
  ir_refs *slow_inputs;
16220
16221
0
  ir_refs_init(slow_inputs, 8);
16222
16223
0
  if (trace) {
16224
0
    ZEND_ASSERT(trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END);
16225
0
    ZEND_ASSERT(trace->opline != NULL);
16226
0
    next_opline = trace->opline;
16227
0
  }
16228
16229
0
  if (opline->op1_type == IS_CONST) {
16230
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
16231
0
    zval *jump_zv = NULL;
16232
0
    int b;
16233
16234
0
    if (opline->opcode == ZEND_SWITCH_LONG) {
16235
0
      if (Z_TYPE_P(zv) == IS_LONG) {
16236
0
        jump_zv = zend_hash_index_find(jumptable, Z_LVAL_P(zv));
16237
0
      }
16238
0
    } else if (opline->opcode == ZEND_SWITCH_STRING) {
16239
0
      if (Z_TYPE_P(zv) == IS_STRING) {
16240
0
        jump_zv = zend_hash_find_known_hash(jumptable, Z_STR_P(zv));
16241
0
      }
16242
0
    } else if (opline->opcode == ZEND_MATCH) {
16243
0
      if (Z_TYPE_P(zv) == IS_LONG) {
16244
0
        jump_zv = zend_hash_index_find(jumptable, Z_LVAL_P(zv));
16245
0
      } else if (Z_TYPE_P(zv) == IS_STRING) {
16246
0
        jump_zv = zend_hash_find_known_hash(jumptable, Z_STR_P(zv));
16247
0
      }
16248
0
    } else {
16249
0
      ZEND_UNREACHABLE();
16250
0
    }
16251
0
    if (next_opline) {
16252
0
      const zend_op *target;
16253
16254
0
      if (jump_zv != NULL) {
16255
0
        target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(jump_zv));
16256
0
      } else {
16257
0
        target = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
16258
0
      }
16259
0
      ZEND_ASSERT(target == next_opline);
16260
0
    } else {
16261
0
      if (jump_zv != NULL) {
16262
0
        b = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(jump_zv)) - op_array->opcodes];
16263
0
      } else {
16264
0
        b = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) - op_array->opcodes];
16265
0
      }
16266
0
      _zend_jit_add_predecessor_ref(jit, b, jit->b, ir_END());
16267
0
      jit->b = -1;
16268
0
    }
16269
0
  } else {
16270
0
    zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[opline - op_array->opcodes] : NULL;
16271
0
    uint32_t op1_info = OP1_INFO();
16272
0
    zend_jit_addr op1_addr = OP1_ADDR();
16273
0
    const zend_op *default_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
16274
0
    const zend_op *target;
16275
0
    int default_b = next_opline ? -1 : ssa->cfg.map[default_opline - op_array->opcodes];
16276
0
    int b;
16277
0
    int32_t exit_point;
16278
0
    const void *exit_addr;
16279
0
    const void *default_label = NULL;
16280
0
    zval *zv;
16281
16282
0
    if (next_opline) {
16283
0
      if (next_opline != default_opline) {
16284
0
        exit_point = zend_jit_trace_get_exit_point(default_opline, 0);
16285
0
        default_label = zend_jit_trace_get_exit_addr(exit_point);
16286
0
        if (!default_label) {
16287
0
          return 0;
16288
0
        }
16289
0
      }
16290
0
    }
16291
16292
0
    if (opline->opcode == ZEND_SWITCH_LONG) {
16293
0
      if (op1_info & MAY_BE_LONG) {
16294
0
        const void *fallback_label = NULL;
16295
16296
0
        if (next_opline) {
16297
0
          if (next_opline != opline + 1) {
16298
0
            exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
16299
0
            fallback_label = zend_jit_trace_get_exit_addr(exit_point);
16300
0
            if (!fallback_label) {
16301
0
              return 0;
16302
0
            }
16303
0
          }
16304
0
        }
16305
0
        if (op1_info & MAY_BE_REF) {
16306
0
          ir_ref ref, if_long, fast_path, ref2;
16307
16308
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
16309
0
          if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16310
0
          ir_IF_TRUE(if_long);
16311
0
          fast_path = ir_END();
16312
0
          ir_IF_FALSE_cold(if_long);
16313
16314
          // JIT: ZVAL_DEREF(op)
16315
0
          if (fallback_label) {
16316
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_REFERENCE, fallback_label);
16317
0
          } else {
16318
0
            ir_ref if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
16319
0
            ir_IF_FALSE_cold(if_ref);
16320
0
            ir_refs_add(slow_inputs, ir_END());
16321
0
            ir_IF_TRUE(if_ref);
16322
0
          }
16323
16324
0
          ref2 = ir_ADD_OFFSET(jit_Z_PTR(jit, op1_addr), offsetof(zend_reference, val));
16325
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref2);
16326
16327
0
          if (fallback_label) {
16328
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16329
0
          } else {
16330
0
            if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16331
0
            ir_IF_FALSE_cold(if_long);
16332
0
            ir_refs_add(slow_inputs, ir_END());
16333
0
            ir_IF_TRUE(if_long);
16334
0
          }
16335
16336
0
          ir_MERGE_2(fast_path, ir_END());
16337
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
16338
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
16339
0
        } else if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
16340
0
          if (fallback_label) {
16341
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16342
0
          } else {
16343
0
            ir_ref if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16344
0
            ir_IF_FALSE_cold(if_long);
16345
0
            ir_refs_add(slow_inputs, ir_END());
16346
0
            ir_IF_TRUE(if_long);
16347
0
          }
16348
0
        }
16349
0
        ir_ref ref = jit_Z_LVAL(jit, op1_addr);
16350
16351
0
        if (!HT_IS_PACKED(jumptable)) {
16352
0
          ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_index_find),
16353
0
            ir_CONST_ADDR(jumptable), ref);
16354
0
          ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16355
          /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16356
0
          if (sizeof(Bucket) == 32) {
16357
0
            ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16358
0
          } else {
16359
0
            ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16360
0
          }
16361
0
        }
16362
0
        ref = ir_SWITCH(ref);
16363
16364
0
        if (next_opline) {
16365
0
          ir_ref continue_list = IR_UNUSED;
16366
16367
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16368
0
            ir_ref idx;
16369
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16370
16371
0
            if (HT_IS_PACKED(jumptable)) {
16372
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16373
0
            } else {
16374
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16375
0
            }
16376
0
            ir_CASE_VAL(ref, idx);
16377
0
            if (target == next_opline) {
16378
0
              ir_END_list(continue_list);
16379
0
            } else {
16380
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16381
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16382
0
              if (!exit_addr) {
16383
0
                return 0;
16384
0
              }
16385
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16386
0
            }
16387
0
          } ZEND_HASH_FOREACH_END();
16388
16389
0
          ir_CASE_DEFAULT(ref);
16390
0
          if (next_opline == default_opline) {
16391
0
            ir_END_list(continue_list);
16392
0
          } else {
16393
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16394
0
          }
16395
0
          if (continue_list) {
16396
0
            ir_MERGE_list(continue_list);
16397
0
          } else {
16398
0
            ZEND_ASSERT(slow_inputs->count);
16399
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16400
0
          }
16401
0
        } else {
16402
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16403
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16404
0
            b = ssa->cfg.map[target - op_array->opcodes];
16405
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16406
0
          } ZEND_HASH_FOREACH_END();
16407
16408
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16409
0
          if (slow_inputs->count) {
16410
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16411
0
            _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16412
0
          }
16413
0
          jit->b = -1;
16414
0
        }
16415
0
      } else if (!next_opline) {
16416
0
        _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16417
0
        jit->b = -1;
16418
0
      }
16419
0
    } else if (opline->opcode == ZEND_SWITCH_STRING) {
16420
0
      if (op1_info & MAY_BE_STRING) {
16421
0
        const void *fallback_label = NULL;
16422
16423
0
        if (next_opline) {
16424
0
          if (next_opline != opline + 1) {
16425
0
            exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
16426
0
            fallback_label = zend_jit_trace_get_exit_addr(exit_point);
16427
0
            if (!fallback_label) {
16428
0
              return 0;
16429
0
            }
16430
0
          }
16431
0
        }
16432
0
        if (op1_info & MAY_BE_REF) {
16433
0
          ir_ref ref, if_string, fast_path, ref2;
16434
16435
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
16436
0
          if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16437
0
          ir_IF_TRUE(if_string);
16438
0
          fast_path = ir_END();
16439
0
          ir_IF_FALSE_cold(if_string);
16440
16441
          // JIT: ZVAL_DEREF(op)
16442
0
          if (fallback_label) {
16443
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_REFERENCE, fallback_label);
16444
0
          } else {
16445
0
            ir_ref if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16446
0
            ir_IF_FALSE_cold(if_ref);
16447
0
            ir_refs_add(slow_inputs, ir_END());
16448
0
            ir_IF_TRUE(if_ref);
16449
0
          }
16450
16451
0
          ref2 = ir_ADD_OFFSET(jit_Z_PTR(jit, op1_addr), offsetof(zend_reference, val));
16452
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref2);
16453
16454
0
          if (fallback_label) {
16455
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16456
0
          } else {
16457
0
            if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16458
0
            ir_IF_FALSE_cold(if_string);
16459
0
            ir_refs_add(slow_inputs, ir_END());
16460
0
            ir_IF_TRUE(if_string);
16461
0
          }
16462
16463
0
          ir_MERGE_2(fast_path, ir_END());
16464
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
16465
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
16466
0
        } else if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_STRING)) {
16467
0
          if (fallback_label) {
16468
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, fallback_label);
16469
0
          } else {
16470
0
            ir_ref if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16471
0
            ir_IF_FALSE_cold(if_string);
16472
0
            ir_refs_add(slow_inputs, ir_END());
16473
0
            ir_IF_TRUE(if_string);
16474
0
          }
16475
0
        }
16476
16477
0
        ir_ref ref = jit_Z_PTR(jit, op1_addr);
16478
0
        ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_find),
16479
0
          ir_CONST_ADDR(jumptable), ref);
16480
0
        ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16481
        /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16482
0
        if (sizeof(Bucket) == 32) {
16483
0
          ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16484
0
        } else {
16485
0
          ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16486
0
        }
16487
0
        ref = ir_SWITCH(ref);
16488
16489
0
        if (next_opline) {
16490
0
          ir_ref continue_list = IR_UNUSED;
16491
16492
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16493
0
            ir_ref idx;
16494
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16495
16496
0
            if (HT_IS_PACKED(jumptable)) {
16497
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16498
0
            } else {
16499
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16500
0
            }
16501
0
            ir_CASE_VAL(ref, idx);
16502
0
            if (target == next_opline) {
16503
0
              ir_END_list(continue_list);
16504
0
            } else {
16505
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16506
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16507
0
              if (!exit_addr) {
16508
0
                return 0;
16509
0
              }
16510
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16511
0
            }
16512
0
          } ZEND_HASH_FOREACH_END();
16513
16514
0
          ir_CASE_DEFAULT(ref);
16515
0
          if (next_opline == default_opline) {
16516
0
            ir_END_list(continue_list);
16517
0
          } else {
16518
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16519
0
          }
16520
0
          if (continue_list) {
16521
0
            ir_MERGE_list(continue_list);
16522
0
          } else {
16523
0
            ZEND_ASSERT(slow_inputs->count);
16524
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16525
0
          }
16526
0
        } else {
16527
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16528
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16529
0
            b = ssa->cfg.map[target - op_array->opcodes];
16530
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16531
0
          } ZEND_HASH_FOREACH_END();
16532
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16533
0
          if (slow_inputs->count) {
16534
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16535
0
            _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16536
0
          }
16537
0
          jit->b = -1;
16538
0
        }
16539
0
      } else if (!next_opline) {
16540
0
        _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16541
0
        jit->b = -1;
16542
0
      }
16543
0
    } else if (opline->opcode == ZEND_MATCH) {
16544
0
      ir_ref if_type = IR_UNUSED, default_input_list = IR_UNUSED, ref = IR_UNUSED;
16545
0
      ir_ref continue_list = IR_UNUSED;
16546
16547
0
      if (op1_info & (MAY_BE_LONG|MAY_BE_STRING)) {
16548
0
        ir_ref long_path = IR_UNUSED;
16549
16550
0
        if (op1_info & MAY_BE_REF) {
16551
0
          op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
16552
0
        }
16553
0
        if (op1_info & MAY_BE_LONG) {
16554
0
          if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
16555
0
            if (op1_info & (MAY_BE_STRING|MAY_BE_UNDEF)) {
16556
0
              if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16557
0
              ir_IF_TRUE(if_type);
16558
0
            } else if (default_label) {
16559
0
              jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, default_label);
16560
0
            } else if (next_opline) {
16561
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16562
0
              ir_IF_FALSE(if_type);
16563
0
              ir_END_list(continue_list);
16564
0
              ir_IF_TRUE(if_type);
16565
0
            } else {
16566
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16567
0
              ir_IF_FALSE(if_type);
16568
0
              ir_END_list(default_input_list);
16569
0
              ir_IF_TRUE(if_type);
16570
0
            }
16571
0
          }
16572
0
          ref = jit_Z_LVAL(jit, op1_addr);
16573
0
          ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_index_find),
16574
0
            ir_CONST_ADDR(jumptable), ref);
16575
0
          if (op1_info & MAY_BE_STRING) {
16576
0
            long_path = ir_END();
16577
0
          }
16578
0
        }
16579
0
        if (op1_info & MAY_BE_STRING) {
16580
0
          if (if_type) {
16581
0
            ir_IF_FALSE(if_type);
16582
0
            if_type = IS_UNUSED;
16583
0
          }
16584
0
          if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_STRING))) {
16585
0
            if (op1_info & MAY_BE_UNDEF) {
16586
0
              if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16587
0
              ir_IF_TRUE(if_type);
16588
0
            } else if (default_label) {
16589
0
              jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, default_label);
16590
0
            } else if (next_opline) {
16591
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16592
0
              ir_IF_FALSE(if_type);
16593
0
              ir_END_list(continue_list);
16594
0
              ir_IF_TRUE(if_type);
16595
0
            } else {
16596
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16597
0
              ir_IF_FALSE(if_type);
16598
0
              ir_END_list(default_input_list);
16599
0
              ir_IF_TRUE(if_type);
16600
0
            }
16601
0
          }
16602
0
          ir_ref ref2 = jit_Z_PTR(jit, op1_addr);
16603
0
          ref2 = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_find),
16604
0
            ir_CONST_ADDR(jumptable), ref2);
16605
0
          if (op1_info & MAY_BE_LONG) {
16606
0
            ir_MERGE_WITH(long_path);
16607
0
            ref = ir_PHI_2(IR_LONG, ref2, ref);
16608
0
          } else {
16609
0
            ref = ref2;
16610
0
          }
16611
0
        }
16612
16613
0
        ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16614
        /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16615
0
        if (HT_IS_PACKED(jumptable)) {
16616
0
          ZEND_ASSERT(sizeof(zval) == 16);
16617
0
          ref = ir_SHR_L(ref, ir_CONST_LONG(4));
16618
0
        } else {
16619
0
          if (sizeof(Bucket) == 32) {
16620
0
            ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16621
0
          } else {
16622
0
            ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16623
0
          }
16624
0
        }
16625
0
        ref = ir_SWITCH(ref);
16626
16627
0
        if (next_opline) {
16628
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16629
0
            ir_ref idx;
16630
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16631
16632
0
            if (HT_IS_PACKED(jumptable)) {
16633
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16634
0
            } else {
16635
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16636
0
            }
16637
0
            ir_CASE_VAL(ref, idx);
16638
0
            if (target == next_opline) {
16639
0
              ir_END_list(continue_list);
16640
0
            } else {
16641
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16642
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16643
0
              if (!exit_addr) {
16644
0
                return 0;
16645
0
              }
16646
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16647
0
            }
16648
0
          } ZEND_HASH_FOREACH_END();
16649
16650
0
          ir_CASE_DEFAULT(ref);
16651
0
          if (next_opline == default_opline) {
16652
0
            ir_END_list(continue_list);
16653
0
          } else {
16654
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16655
0
          }
16656
0
        } else {
16657
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16658
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16659
0
            b = ssa->cfg.map[target - op_array->opcodes];
16660
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16661
0
          } ZEND_HASH_FOREACH_END();
16662
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16663
0
        }
16664
0
      } else if (!(op1_info & MAY_BE_UNDEF)) {
16665
0
        if (next_opline) {
16666
0
          if (next_opline == default_opline) {
16667
0
            ir_END_list(continue_list);
16668
0
          } else {
16669
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16670
0
          }
16671
0
        } else {
16672
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ir_END());
16673
0
        }
16674
0
      }
16675
16676
0
      if (op1_info & MAY_BE_UNDEF) {
16677
0
        if (if_type) {
16678
0
          ir_IF_FALSE(if_type);
16679
0
          if_type = IS_UNUSED;
16680
0
        }
16681
0
        if (op1_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_STRING))) {
16682
0
          if (default_label) {
16683
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_UNDEF, default_label);
16684
0
          } else if (next_opline) {
16685
0
            ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
16686
0
            ir_IF_TRUE(if_def);
16687
0
            ir_END_list(continue_list);
16688
0
            ir_IF_FALSE_cold(if_def);
16689
0
          } else {
16690
0
            ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
16691
0
            ir_IF_TRUE(if_def);
16692
0
            ir_END_list(default_input_list);
16693
0
            ir_IF_FALSE_cold(if_def);
16694
0
          }
16695
0
        }
16696
16697
0
        jit_SET_EX_OPLINE(jit, opline);
16698
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
16699
0
          ir_CONST_U32(opline->op1.var));
16700
0
        zend_jit_check_exception_undef_result(jit, opline);
16701
0
        if (default_label) {
16702
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16703
0
        } else if (next_opline) {
16704
0
          ir_END_list(continue_list);
16705
0
        } else {
16706
0
          ir_END_list(default_input_list);
16707
0
        }
16708
0
      }
16709
0
      if (next_opline) {
16710
0
        ZEND_ASSERT(continue_list);
16711
0
        ir_MERGE_list(continue_list);
16712
0
      } else {
16713
0
        if (default_input_list) {
16714
0
          if (jit->ctx.ir_base[ref].op == IR_SWITCH) {
16715
0
            ZEND_ASSERT(jit->ctx.ir_base[ref].op3 == IR_UNUSED);
16716
0
            jit->ctx.ir_base[ref].op3 = default_input_list;
16717
0
          } else {
16718
0
            ir_MERGE_list(default_input_list);
16719
0
            _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ir_END());
16720
0
          }
16721
0
        }
16722
0
        jit->b = -1;
16723
0
      }
16724
0
    } else {
16725
0
      ZEND_UNREACHABLE();
16726
0
    }
16727
0
  }
16728
0
  return 1;
16729
0
}
16730
16731
static int zend_jit_start(zend_jit_ctx *jit, const zend_op_array *op_array, zend_ssa *ssa)
16732
0
{
16733
0
  uint32_t i, count;
16734
0
  zend_basic_block *bb;
16735
16736
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));
16737
16738
0
  jit->ctx.spill_base = ZREG_FP;
16739
16740
0
  jit->op_array = jit->current_op_array = op_array;
16741
0
  jit->ssa = ssa;
16742
0
  jit->bb_start_ref = zend_arena_calloc(&CG(arena), ssa->cfg.blocks_count * 2, sizeof(ir_ref));
16743
0
  jit->bb_predecessors = jit->bb_start_ref + ssa->cfg.blocks_count;
16744
16745
0
  count = 0;
16746
0
  for (i = 0, bb = ssa->cfg.blocks; i < ssa->cfg.blocks_count; i++, bb++) {
16747
0
    jit->bb_predecessors[i] = count;
16748
0
    count += bb->predecessors_count;
16749
0
  }
16750
0
  jit->bb_edges = zend_arena_calloc(&CG(arena), count, sizeof(ir_ref));
16751
16752
0
  if (!GCC_GLOBAL_REGS) {
16753
0
    if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
16754
0
      ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
16755
0
      ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
16756
0
      jit_STORE_FP(jit, execute_data_ref);
16757
0
      jit_STORE_IP(jit, opline_ref);
16758
0
    }
16759
0
    jit->ctx.flags |= IR_FASTCALL_FUNC;
16760
0
  }
16761
16762
0
  return 1;
16763
0
}
16764
16765
static zend_vm_opcode_handler_t zend_jit_finish(zend_jit_ctx *jit)
16766
0
{
16767
0
  void *entry;
16768
0
  size_t size;
16769
0
  zend_string *str = NULL;
16770
16771
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP|
16772
0
      ZEND_JIT_DEBUG_IR_SRC|ZEND_JIT_DEBUG_IR_AFTER_SCCP|ZEND_JIT_DEBUG_IR_AFTER_SCCP|
16773
0
      ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE|ZEND_JIT_DEBUG_IR_AFTER_REGS|ZEND_JIT_DEBUG_IR_FINAL|ZEND_JIT_DEBUG_IR_CODEGEN)) {
16774
0
    if (jit->name) {
16775
0
      str = zend_string_copy(jit->name);
16776
0
    } else {
16777
0
      str = zend_jit_func_name(jit->op_array);
16778
0
    }
16779
0
  }
16780
16781
0
  if (jit->op_array) {
16782
    /* Only for function JIT */
16783
0
    _zend_jit_fix_merges(jit);
16784
#if defined(IR_TARGET_AARCH64)
16785
  } else if (jit->trace) {
16786
    jit->ctx.deoptimization_exits = jit->trace->exit_count;
16787
    jit->ctx.get_exit_addr = zend_jit_trace_get_exit_addr;
16788
#endif
16789
0
  } else {
16790
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
16791
0
    jit->ctx.flags |= IR_GEN_CACHE_DEMOTE;
16792
0
#endif
16793
0
  }
16794
16795
0
  entry = zend_jit_ir_compile(&jit->ctx, &size, str ? ZSTR_VAL(str) : NULL);
16796
0
  if (entry) {
16797
0
    if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
16798
#ifdef HAVE_CAPSTONE
16799
      if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
16800
        if (str) {
16801
          ir_disasm_add_symbol(ZSTR_VAL(str), (uintptr_t)entry, size);
16802
        }
16803
        ir_disasm(str ? ZSTR_VAL(str) : "unknown",
16804
          entry, size,
16805
          (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_ADDR) != 0,
16806
          &jit->ctx, stderr);
16807
      }
16808
#endif
16809
0
#ifndef _WIN32
16810
0
      if (str) {
16811
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
16812
0
          uintptr_t sp_offset = 0;
16813
16814
//          ir_mem_unprotect(entry, size);
16815
0
          if (!(jit->ctx.flags & IR_FUNCTION)
16816
0
           && ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
16817
0
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
16818
0
            sp_offset = zend_jit_hybrid_vm_sp_adj;
16819
#else
16820
            sp_offset = sizeof(void*);
16821
#endif
16822
0
          } else {
16823
0
            sp_offset = sizeof(void*);
16824
0
          }
16825
0
          ir_gdb_register(ZSTR_VAL(str), entry, size, sp_offset, 0);
16826
//          ir_mem_protect(entry, size);
16827
0
        }
16828
16829
0
        if (JIT_G(debug) & (ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
16830
0
          ir_perf_map_register(ZSTR_VAL(str), entry, size);
16831
0
          if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
16832
0
            ir_perf_jitdump_register(ZSTR_VAL(str), entry, size);
16833
0
          }
16834
0
        }
16835
0
      }
16836
0
#endif
16837
0
    }
16838
16839
0
    if (jit->op_array) {
16840
      /* Only for function JIT */
16841
0
      const zend_op_array *op_array = jit->op_array;
16842
0
      zend_op *opline = (zend_op*)op_array->opcodes;
16843
16844
0
      if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
16845
0
        while (opline->opcode == ZEND_RECV) {
16846
0
          opline++;
16847
0
        }
16848
0
      }
16849
0
      opline->handler = (zend_vm_opcode_handler_t)entry;
16850
16851
0
      if (jit->ctx.entries_count) {
16852
        /* For all entries */
16853
0
        int i = jit->ctx.entries_count;
16854
0
        do {
16855
0
          ir_insn *insn = &jit->ctx.ir_base[jit->ctx.entries[--i]];
16856
0
          op_array->opcodes[insn->op2].handler = (zend_vm_opcode_handler_t)((char*)entry + insn->op3);
16857
0
        } while (i != 0);
16858
0
      }
16859
0
    } else {
16860
      /* Only for tracing JIT */
16861
0
      zend_jit_trace_info *t = jit->trace;
16862
0
      zend_jit_trace_stack *stack;
16863
0
      uint32_t i;
16864
16865
0
      if (t) {
16866
0
        for (i = 0; i < t->stack_map_size; i++) {
16867
0
          stack = t->stack_map + i;
16868
0
          if (stack->flags & ZREG_SPILL_SLOT) {
16869
0
            stack->reg = (jit->ctx.flags & IR_USE_FRAME_POINTER) ? IR_REG_FP : IR_REG_SP;
16870
0
            stack->ref = ir_get_spill_slot_offset(&jit->ctx, stack->ref);
16871
0
          }
16872
0
        }
16873
0
      }
16874
16875
0
      zend_jit_trace_add_code(entry, size);
16876
0
    }
16877
0
  }
16878
16879
0
  if (str) {
16880
0
    zend_string_release(str);
16881
0
  }
16882
16883
0
  return (zend_vm_opcode_handler_t)entry;
16884
0
}
16885
16886
static const void *zend_jit_trace_allocate_exit_group(uint32_t n)
16887
0
{
16888
0
  const void *entry;
16889
0
  size_t size;
16890
0
  ir_code_buffer code_buffer;
16891
16892
0
  code_buffer.start = dasm_buf;
16893
0
  code_buffer.end = dasm_end;
16894
0
  code_buffer.pos = *dasm_ptr;
16895
16896
0
  entry = ir_emit_exitgroup(n, ZEND_JIT_EXIT_POINTS_PER_GROUP, zend_jit_stub_handlers[jit_stub_trace_exit],
16897
0
    &code_buffer, &size);
16898
16899
0
  *dasm_ptr = code_buffer.pos;
16900
16901
0
  if (entry) {
16902
#ifdef HAVE_CAPSTONE
16903
    if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
16904
      uint32_t i;
16905
      char name[32];
16906
16907
      for (i = 0; i < ZEND_JIT_EXIT_POINTS_PER_GROUP; i++) {
16908
        snprintf(name, sizeof(name), "jit$$trace_exit_%d", n + i);
16909
        ir_disasm_add_symbol(name, (uintptr_t)entry + (i * ZEND_JIT_EXIT_POINTS_SPACING), ZEND_JIT_EXIT_POINTS_SPACING);
16910
      }
16911
    }
16912
#endif
16913
0
  }
16914
16915
0
  return entry;
16916
0
}
16917
16918
static int zend_jit_type_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var, uint8_t type)
16919
0
{
16920
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16921
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16922
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
16923
16924
0
  if (!exit_addr) {
16925
0
    return 0;
16926
0
  }
16927
0
  ir_GUARD(ir_EQ(jit_Z_TYPE(jit, addr), ir_CONST_U8(type)), ir_CONST_ADDR(exit_addr));
16928
16929
0
  return 1;
16930
0
}
16931
16932
static int zend_jit_scalar_type_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var)
16933
0
{
16934
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16935
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16936
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
16937
16938
0
  if (!exit_addr) {
16939
0
    return 0;
16940
0
  }
16941
0
  ir_GUARD(ir_LT(jit_Z_TYPE(jit, addr), ir_CONST_U8(IS_STRING)), ir_CONST_ADDR(exit_addr));
16942
16943
0
  return 1;
16944
0
}
16945
16946
static bool zend_jit_noref_guard(zend_jit_ctx *jit, const zend_op *opline, zend_jit_addr var_addr)
16947
0
{
16948
0
  uint32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16949
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16950
16951
0
  if (!exit_addr) {
16952
0
    return false;
16953
0
  }
16954
0
  ir_GUARD(ir_NE(jit_Z_TYPE(jit, var_addr), ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16955
16956
0
  return true;
16957
0
}
16958
16959
static int zend_jit_trace_opline_guard(zend_jit_ctx *jit, const zend_op *opline)
16960
0
{
16961
0
  uint32_t exit_point = zend_jit_trace_get_exit_point(NULL, 0);
16962
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16963
16964
0
  if (!exit_addr) {
16965
0
    return 0;
16966
0
  }
16967
16968
0
  ir_GUARD(jit_CMP_IP(jit, IR_EQ, opline), ir_CONST_ADDR(exit_addr));
16969
0
  zend_jit_set_last_valid_opline(jit, opline);
16970
16971
0
  return 1;
16972
0
}
16973
16974
static bool zend_jit_guard_reference(zend_jit_ctx  *jit,
16975
                                     const zend_op *opline,
16976
                                     zend_jit_addr *var_addr_ptr,
16977
                                     zend_jit_addr *ref_addr_ptr,
16978
                                     bool           add_ref_guard)
16979
0
{
16980
0
  zend_jit_addr var_addr = *var_addr_ptr;
16981
0
  const void *exit_addr = NULL;
16982
0
  ir_ref ref;
16983
16984
0
  if (add_ref_guard) {
16985
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16986
16987
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16988
0
    if (!exit_addr) {
16989
0
      return false;
16990
0
    }
16991
16992
0
    ref = jit_Z_TYPE(jit, var_addr);
16993
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16994
0
  }
16995
16996
0
  ref = jit_Z_PTR(jit, var_addr);
16997
0
  *ref_addr_ptr = ZEND_ADDR_REF_ZVAL(ref);
16998
0
  ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
16999
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
17000
0
  *var_addr_ptr = var_addr;
17001
17002
0
  return true;
17003
0
}
17004
17005
static bool zend_jit_fetch_reference(zend_jit_ctx  *jit,
17006
                                     const zend_op *opline,
17007
                                     uint8_t        var_type,
17008
                                     uint32_t      *var_info_ptr,
17009
                                     zend_jit_addr *var_addr_ptr,
17010
                                     bool           add_ref_guard,
17011
                                     bool           add_type_guard)
17012
0
{
17013
0
  zend_jit_addr var_addr = *var_addr_ptr;
17014
0
  uint32_t var_info = *var_info_ptr;
17015
0
  const void *exit_addr = NULL;
17016
0
  ir_ref ref;
17017
17018
0
  if (add_ref_guard || add_type_guard) {
17019
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
17020
17021
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17022
0
    if (!exit_addr) {
17023
0
      return false;
17024
0
    }
17025
0
  }
17026
17027
0
  if (add_ref_guard) {
17028
0
    ref = jit_Z_TYPE(jit, var_addr);
17029
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
17030
0
  }
17031
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL && opline->op1_type == IS_VAR) {
17032
    /* Hack: Convert reference to regular value to simplify JIT code for INIT_METHOD_CALL */
17033
0
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_unref_helper),
17034
0
      jit_ZVAL_ADDR(jit, var_addr));
17035
0
    *var_addr_ptr = var_addr;
17036
0
  } else {
17037
0
    ref = jit_Z_PTR(jit, var_addr);
17038
0
    ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
17039
0
    var_addr = ZEND_ADDR_REF_ZVAL(ref);
17040
0
    *var_addr_ptr = var_addr;
17041
0
  }
17042
17043
0
  if (var_type != IS_UNKNOWN) {
17044
0
    var_type &= ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED);
17045
0
  }
17046
0
  if (add_type_guard
17047
0
   && var_type != IS_UNKNOWN
17048
0
   && (var_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << var_type)) {
17049
0
    ref = jit_Z_TYPE(jit, var_addr);
17050
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(var_type)), ir_CONST_ADDR(exit_addr));
17051
17052
0
    ZEND_ASSERT(var_info & (1 << var_type));
17053
0
    if (var_type < IS_STRING) {
17054
0
      var_info = (1 << var_type);
17055
0
    } else if (var_type != IS_ARRAY) {
17056
0
      var_info = (1 << var_type) | (var_info & (MAY_BE_RC1|MAY_BE_RCN));
17057
0
    } else {
17058
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));
17059
0
    }
17060
17061
0
    *var_info_ptr = var_info;
17062
0
  } else {
17063
0
    var_info &= ~MAY_BE_REF;
17064
0
    *var_info_ptr = var_info;
17065
0
  }
17066
0
  *var_info_ptr |= MAY_BE_GUARD; /* prevent generation of specialized zval dtor */
17067
17068
0
  return true;
17069
0
}
17070
17071
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)
17072
0
{
17073
0
  zend_jit_addr var_addr = *var_addr_ptr;
17074
0
  uint32_t var_info = *var_info_ptr;
17075
0
  int32_t exit_point;
17076
0
  const void *exit_addr;
17077
0
  ir_ref ref = IR_UNUSED;
17078
17079
0
  if (add_indirect_guard) {
17080
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
17081
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17082
17083
0
    if (!exit_addr) {
17084
0
      return false;
17085
0
    }
17086
0
    jit_guard_Z_TYPE(jit, var_addr, IS_INDIRECT, exit_addr);
17087
0
    ref = jit_Z_PTR(jit, var_addr);
17088
0
  } else {
17089
    /* This LOAD of INDIRECT VAR, stored by the previous FETCH_(DIM/OBJ)_W,
17090
     * is eliminated by store forwarding (S2L) */
17091
0
    ref = jit_Z_PTR(jit, var_addr);
17092
0
  }
17093
0
  *var_info_ptr &= ~MAY_BE_INDIRECT;
17094
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
17095
0
  *var_addr_ptr = var_addr;
17096
17097
0
  if (var_type != IS_UNKNOWN) {
17098
0
    var_type &= ~(IS_TRACE_INDIRECT|IS_TRACE_PACKED);
17099
0
  }
17100
0
  if (!(var_type & IS_TRACE_REFERENCE)
17101
0
   && var_type != IS_UNKNOWN
17102
0
   && (var_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << var_type)) {
17103
0
    exit_point = zend_jit_trace_get_exit_point(opline, 0);
17104
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17105
17106
0
    if (!exit_addr) {
17107
0
      return false;
17108
0
    }
17109
17110
0
    jit_guard_Z_TYPE(jit, var_addr, var_type, exit_addr);
17111
17112
    //var_info = zend_jit_trace_type_to_info_ex(var_type, var_info);
17113
0
    ZEND_ASSERT(var_info & (1 << var_type));
17114
0
    if (var_type < IS_STRING) {
17115
0
      var_info = (1 << var_type);
17116
0
    } else if (var_type != IS_ARRAY) {
17117
0
      var_info = (1 << var_type) | (var_info & (MAY_BE_RC1|MAY_BE_RCN));
17118
0
    } else {
17119
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));
17120
0
    }
17121
17122
0
    *var_info_ptr = var_info;
17123
0
  }
17124
17125
0
  return true;
17126
0
}
17127
17128
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)
17129
0
{
17130
0
  zend_jit_op_array_trace_extension *jit_extension =
17131
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
17132
0
  size_t offset = jit_extension->offset;
17133
0
  zend_vm_opcode_handler_func_t handler =
17134
0
    ZEND_OP_TRACE_INFO(opline, offset)->call_handler;
17135
0
  ir_ref ref;
17136
17137
0
  zend_jit_set_ip(jit, opline);
17138
0
  if (GCC_GLOBAL_REGS) {
17139
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
17140
0
  } else {
17141
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
17142
0
    if (opline->opcode == ZEND_RETURN ||
17143
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17144
0
        opline->opcode == ZEND_DO_UCALL ||
17145
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
17146
0
        opline->opcode == ZEND_DO_FCALL ||
17147
0
        opline->opcode == ZEND_GENERATOR_CREATE ||
17148
0
        opline->opcode == ZEND_INCLUDE_OR_EVAL) {
17149
17150
0
      jit_STORE_IP(jit, ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT)));
17151
0
    } else {
17152
0
      jit_STORE_IP(jit, ref);
17153
0
    }
17154
0
  }
17155
0
  if (may_throw
17156
0
   && opline->opcode != ZEND_RETURN
17157
0
   && opline->opcode != ZEND_RETURN_BY_REF) {
17158
0
    zend_jit_check_exception(jit);
17159
0
  }
17160
17161
0
  while (trace->op != ZEND_JIT_TRACE_VM && trace->op != ZEND_JIT_TRACE_END) {
17162
0
    trace++;
17163
0
  }
17164
17165
0
  if ((!GCC_GLOBAL_REGS
17166
0
   && (trace->op != ZEND_JIT_TRACE_END || trace->stop != ZEND_JIT_TRACE_STOP_RETURN))
17167
0
   || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
17168
0
    if (opline->opcode == ZEND_RETURN ||
17169
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17170
0
        opline->opcode == ZEND_DO_UCALL ||
17171
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
17172
0
        opline->opcode == ZEND_DO_FCALL ||
17173
0
        opline->opcode == ZEND_GENERATOR_CREATE ||
17174
0
        opline->opcode == ZEND_INCLUDE_OR_EVAL) {
17175
17176
0
      ir_ref addr = jit_EG(current_execute_data);
17177
17178
0
      jit_STORE_FP(jit, ir_LOAD_A(addr));
17179
0
    }
17180
0
  }
17181
17182
0
  if (zend_jit_trace_may_exit(op_array, opline)) {
17183
0
    if (opline->opcode == ZEND_RETURN ||
17184
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17185
0
        opline->opcode == ZEND_GENERATOR_CREATE) {
17186
17187
0
      if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
17188
0
        if (trace->op != ZEND_JIT_TRACE_END ||
17189
0
            (trace->stop != ZEND_JIT_TRACE_STOP_RETURN &&
17190
0
             trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
17191
          /* this check may be handled by the following OPLINE guard or jmp [IP] */
17192
0
          ir_GUARD(ir_NE(jit_IP(jit), ir_CONST_ADDR(zend_jit_halt_op)),
17193
0
            jit_STUB_ADDR(jit, jit_stub_trace_halt));
17194
0
        }
17195
0
      } else {
17196
        /* IP has been cleared of ZEND_VM_ENTER_BIT already */
17197
0
        ir_GUARD(jit_IP(jit), jit_STUB_ADDR(jit, jit_stub_trace_halt));
17198
0
      }
17199
0
    } else if (opline->opcode == ZEND_GENERATOR_RETURN ||
17200
0
               opline->opcode == ZEND_YIELD ||
17201
0
               opline->opcode == ZEND_YIELD_FROM) {
17202
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_halt));
17203
0
      ir_BEGIN(IR_UNUSED); /* unreachable block */
17204
0
    }
17205
0
    if (trace->op != ZEND_JIT_TRACE_END ||
17206
0
        (trace->stop != ZEND_JIT_TRACE_STOP_RETURN &&
17207
0
         trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
17208
17209
0
      const zend_op *next_opline = trace->opline;
17210
0
      const zend_op *exit_opline = NULL;
17211
0
      uint32_t exit_point;
17212
0
      const void *exit_addr;
17213
0
      uint32_t old_info = 0;
17214
0
      uint32_t old_res_info = 0;
17215
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
17216
17217
0
      if (zend_is_smart_branch(opline)) {
17218
0
        bool exit_if_true = false;
17219
0
        exit_opline = zend_jit_trace_get_exit_opline(trace, opline + 1, &exit_if_true);
17220
0
      } else {
17221
0
        switch (opline->opcode) {
17222
0
          case ZEND_JMPZ:
17223
0
          case ZEND_JMPNZ:
17224
0
          case ZEND_JMPZ_EX:
17225
0
          case ZEND_JMPNZ_EX:
17226
0
          case ZEND_JMP_SET:
17227
0
          case ZEND_COALESCE:
17228
0
          case ZEND_JMP_NULL:
17229
0
          case ZEND_FE_RESET_R:
17230
0
          case ZEND_FE_RESET_RW:
17231
0
            exit_opline = (trace->opline == opline + 1) ?
17232
0
              OP_JMP_ADDR(opline, opline->op2) :
17233
0
              opline + 1;
17234
0
            break;
17235
0
          case ZEND_FE_FETCH_R:
17236
0
          case ZEND_FE_FETCH_RW:
17237
0
            exit_opline = (trace->opline == opline + 1) ?
17238
0
              ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) :
17239
0
              opline + 1;
17240
0
            break;
17241
17242
0
        }
17243
0
      }
17244
17245
0
      switch (opline->opcode) {
17246
0
        case ZEND_FE_FETCH_R:
17247
0
        case ZEND_FE_FETCH_RW:
17248
0
          if (opline->op2_type != IS_UNUSED) {
17249
0
            old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op2.var));
17250
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var), IS_UNKNOWN, 1);
17251
0
          }
17252
0
          break;
17253
0
        case ZEND_FE_RESET_RW:
17254
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
17255
0
          if (opline->op1_type == IS_CV) {
17256
0
            old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
17257
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17258
0
          }
17259
0
          break;
17260
0
      }
17261
0
      if (opline->result_type == IS_VAR || opline->result_type == IS_TMP_VAR) {
17262
0
        old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
17263
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
17264
0
      }
17265
0
      exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
17266
0
      exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17267
17268
0
      if (opline->result_type == IS_VAR || opline->result_type == IS_TMP_VAR) {
17269
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
17270
0
      }
17271
0
      switch (opline->opcode) {
17272
0
        case ZEND_FE_FETCH_R:
17273
0
        case ZEND_FE_FETCH_RW:
17274
0
          if (opline->op2_type != IS_UNUSED) {
17275
0
            SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op2.var), old_info);
17276
0
          }
17277
0
          break;
17278
0
        case ZEND_FE_RESET_RW:
17279
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
17280
0
          if (opline->op1_type == IS_CV) {
17281
0
            SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_info);
17282
0
          }
17283
0
          break;
17284
0
      }
17285
17286
0
      if (!exit_addr) {
17287
0
        return 0;
17288
0
      }
17289
0
      ir_GUARD(jit_CMP_IP(jit, IR_EQ, next_opline), ir_CONST_ADDR(exit_addr));
17290
0
    }
17291
0
  }
17292
17293
0
  zend_jit_set_last_valid_opline(jit, trace->opline);
17294
17295
0
  return 1;
17296
0
}
17297
17298
static int zend_jit_deoptimizer_start(zend_jit_ctx        *jit,
17299
                                      zend_string         *name,
17300
                                      uint32_t             trace_num,
17301
                                      uint32_t             exit_num)
17302
0
{
17303
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);
17304
17305
0
  jit->ctx.spill_base = ZREG_FP;
17306
17307
0
  jit->op_array = NULL;
17308
0
  jit->ssa = NULL;
17309
0
  jit->name = zend_string_copy(name);
17310
17311
0
  jit->ctx.flags |= IR_SKIP_PROLOGUE;
17312
17313
0
  return 1;
17314
0
}
17315
17316
static int zend_jit_trace_start(zend_jit_ctx        *jit,
17317
                                const zend_op_array *op_array,
17318
                                zend_ssa            *ssa,
17319
                                zend_string         *name,
17320
                                uint32_t             trace_num,
17321
                                zend_jit_trace_info *parent,
17322
                                uint32_t             exit_num)
17323
0
{
17324
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);
17325
17326
0
  jit->ctx.spill_base = ZREG_FP;
17327
17328
0
  jit->op_array = NULL;
17329
0
  jit->current_op_array = op_array;
17330
0
  jit->ssa = ssa;
17331
0
  jit->name = zend_string_copy(name);
17332
17333
0
  if (!GCC_GLOBAL_REGS) {
17334
0
    if (!parent) {
17335
0
      if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
17336
0
        ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
17337
0
        ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
17338
0
        jit_STORE_FP(jit, execute_data_ref);
17339
0
        jit_STORE_IP(jit, opline_ref);
17340
0
      }
17341
0
      jit->ctx.flags |= IR_FASTCALL_FUNC;
17342
0
    }
17343
0
  }
17344
17345
0
  if (parent) {
17346
0
    jit->ctx.flags |= IR_SKIP_PROLOGUE;
17347
0
  }
17348
17349
0
  if (parent) {
17350
0
    int i;
17351
0
    int parent_vars_count = parent->exit_info[exit_num].stack_size;
17352
0
    zend_jit_trace_stack *parent_stack = parent_vars_count == 0 ? NULL :
17353
0
      parent->stack_map +
17354
0
      parent->exit_info[exit_num].stack_offset;
17355
17356
    /* prevent clobbering of registers used for deoptimization */
17357
0
    for (i = 0; i < parent_vars_count; i++) {
17358
0
      if (STACK_FLAGS(parent_stack, i) != ZREG_CONST
17359
0
       && STACK_REG(parent_stack, i) != ZREG_NONE) {
17360
0
        int32_t reg = STACK_REG(parent_stack, i);
17361
0
        ir_type type;
17362
17363
0
        if (STACK_FLAGS(parent_stack, i) == ZREG_ZVAL_COPY) {
17364
0
          type = IR_ADDR;
17365
0
        } else if (STACK_TYPE(parent_stack, i) == IS_LONG) {
17366
0
          type = IR_LONG;
17367
0
        } else if (STACK_TYPE(parent_stack, i) == IS_DOUBLE) {
17368
0
          type = IR_DOUBLE;
17369
0
        } else {
17370
0
          ZEND_UNREACHABLE();
17371
0
        }
17372
0
        if (ssa && ssa->vars[i].no_val) {
17373
          /* pass */
17374
0
        } else {
17375
0
          ir_ref ref = ir_RLOAD(type, reg);
17376
17377
0
          if (STACK_FLAGS(parent_stack, i) & (ZREG_LOAD|ZREG_STORE)) {
17378
            /* op3 is used as a flag that the value is already stored in memory.
17379
             * In case the IR framework decides to spill the result of IR_LOAD,
17380
             * it doesn't have to store the value once again.
17381
             *
17382
             * See: insn->op3 check in ir_emit_rload()
17383
             */
17384
0
            ir_set_op(&jit->ctx, ref, 3, EX_NUM_TO_VAR(i));
17385
0
          }
17386
0
        }
17387
0
      }
17388
0
    }
17389
0
  }
17390
17391
0
  if (parent && parent->exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL) {
17392
0
    ZEND_ASSERT(parent->exit_info[exit_num].poly_func.reg >= 0 && parent->exit_info[exit_num].poly_this.reg >= 0);
17393
0
    if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_func.reg)) {
17394
0
      ir_RLOAD_A(parent->exit_info[exit_num].poly_func.reg);
17395
0
    }
17396
0
    if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_this.reg)) {
17397
0
      ir_RLOAD_A(parent->exit_info[exit_num].poly_this.reg);
17398
0
    }
17399
0
  }
17400
17401
0
  ir_STORE(jit_EG(jit_trace_num), ir_CONST_U32(trace_num));
17402
17403
0
  return 1;
17404
0
}
17405
17406
static int zend_jit_trace_begin_loop(zend_jit_ctx *jit)
17407
0
{
17408
0
  return ir_LOOP_BEGIN(ir_END());
17409
0
}
17410
17411
static void zend_jit_trace_gen_phi(zend_jit_ctx *jit, zend_ssa_phi *phi)
17412
0
{
17413
0
  int dst_var = phi->ssa_var;
17414
0
  int src_var = phi->sources[0];
17415
0
  ir_ref ref;
17416
17417
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
17418
0
  ZEND_ASSERT(jit->ra[src_var].ref != IR_UNUSED && jit->ra[src_var].ref != IR_NULL);
17419
17420
0
  ref = ir_PHI_2(
17421
0
    (jit->ssa->var_info[src_var].type & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE,
17422
0
    zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var)), IR_UNUSED);
17423
17424
0
  src_var = phi->sources[1];
17425
0
  ZEND_ASSERT(jit->ra[src_var].ref == IR_NULL);
17426
0
  jit->ra[src_var].flags |= ZREG_FORWARD;
17427
17428
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var), ref);
17429
0
}
17430
17431
static int zend_jit_trace_end_loop(zend_jit_ctx *jit, int loop_ref, const void *timeout_exit_addr)
17432
0
{
17433
0
  if (timeout_exit_addr) {
17434
0
    zend_jit_check_timeout(jit, NULL, timeout_exit_addr);
17435
0
  }
17436
0
  ZEND_ASSERT(jit->ctx.ir_base[loop_ref].op2 == IR_UNUSED);
17437
0
  ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
17438
0
  return 1;
17439
0
}
17440
17441
static int zend_jit_trace_return(zend_jit_ctx *jit, bool original_handler, const zend_op *opline)
17442
0
{
17443
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
17444
0
    if (!original_handler) {
17445
0
      zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
17446
0
    } else {
17447
0
      zend_jit_tailcall_handler(jit, zend_jit_orig_opline_handler(jit));
17448
0
    }
17449
0
  } else {
17450
0
    if (original_handler) {
17451
0
      ir_ref ref;
17452
0
      ir_ref addr = zend_jit_orig_opline_handler(jit);
17453
17454
#if defined(IR_TARGET_X86)
17455
      addr = ir_CAST_FC_FUNC(addr);
17456
#endif
17457
0
      ref = ir_CALL_2(IR_ADDR, addr, jit_FP(jit), jit_IP(jit));
17458
0
      zend_jit_vm_enter(jit, ref);
17459
0
      return 1;
17460
0
    }
17461
0
    zend_jit_vm_enter(jit, jit_IP(jit));
17462
0
  }
17463
0
  return 1;
17464
0
}
17465
17466
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)
17467
0
{
17468
0
  return ir_patch(code, size, jmp_table_size, zend_jit_trace_get_exit_addr(exit_num), addr);
17469
0
}
17470
17471
static int zend_jit_trace_link_to_root(zend_jit_ctx *jit, zend_jit_trace_info *t, const void *timeout_exit_addr)
17472
0
{
17473
0
  const void *link_addr;
17474
17475
  /* Skip prologue. */
17476
0
  ZEND_ASSERT(zend_jit_trace_prologue_size != (size_t)-1);
17477
0
  link_addr = (const void*)((const char*)t->code_start + zend_jit_trace_prologue_size);
17478
17479
0
  if (timeout_exit_addr) {
17480
0
    zend_jit_check_timeout(jit, NULL, timeout_exit_addr);
17481
0
  }
17482
0
  ir_IJMP(ir_CONST_ADDR(link_addr));
17483
17484
0
  return 1;
17485
0
}
17486
17487
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)
17488
0
{
17489
0
  uint32_t op1_info, op2_info;
17490
17491
0
  switch (opline->opcode) {
17492
0
    case ZEND_SEND_VAR:
17493
0
    case ZEND_SEND_VAL:
17494
0
    case ZEND_SEND_VAL_EX:
17495
0
      return (opline->op2_type != IS_CONST) && (opline->opcode != ZEND_SEND_VAL_EX || opline->op2.num <= MAX_ARG_FLAG_NUM);
17496
0
    case ZEND_QM_ASSIGN:
17497
0
    case ZEND_IS_SMALLER:
17498
0
    case ZEND_IS_SMALLER_OR_EQUAL:
17499
0
    case ZEND_IS_EQUAL:
17500
0
    case ZEND_IS_NOT_EQUAL:
17501
0
    case ZEND_IS_IDENTICAL:
17502
0
    case ZEND_IS_NOT_IDENTICAL:
17503
0
    case ZEND_CASE:
17504
0
      return true;
17505
0
    case ZEND_RETURN:
17506
0
      return (op_array->type != ZEND_EVAL_CODE && op_array->function_name);
17507
0
    case ZEND_ASSIGN:
17508
0
      return (opline->op1_type == IS_CV);
17509
0
    case ZEND_ASSIGN_OP:
17510
0
      if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
17511
0
        return false;
17512
0
      }
17513
0
      op1_info = OP1_INFO();
17514
0
      op2_info = OP2_INFO();
17515
0
      return zend_jit_supported_binary_op(opline->extended_value, op1_info, op2_info);
17516
0
    case ZEND_ADD:
17517
0
    case ZEND_SUB:
17518
0
    case ZEND_MUL:
17519
0
      op1_info = OP1_INFO();
17520
0
      op2_info = OP2_INFO();
17521
0
      if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
17522
0
        return false;
17523
0
      }
17524
0
      if (trace && trace->op1_type != IS_UNKNOWN) {
17525
0
        op1_info &= 1U << (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17526
0
      }
17527
0
      if (trace && trace->op2_type != IS_UNKNOWN) {
17528
0
        op2_info &= 1U << (trace->op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17529
0
      }
17530
0
      return !(op1_info & MAY_BE_UNDEF)
17531
0
        && !(op2_info & MAY_BE_UNDEF)
17532
0
        && (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))
17533
0
        && (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE));
17534
0
    case ZEND_BW_OR:
17535
0
    case ZEND_BW_AND:
17536
0
    case ZEND_BW_XOR:
17537
0
    case ZEND_SL:
17538
0
    case ZEND_SR:
17539
0
    case ZEND_MOD:
17540
0
      op1_info = OP1_INFO();
17541
0
      op2_info = OP2_INFO();
17542
0
      if (trace && trace->op1_type != IS_UNKNOWN) {
17543
0
        op1_info &= 1U << (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17544
0
      }
17545
0
      if (trace && trace->op2_type != IS_UNKNOWN) {
17546
0
        op2_info &= 1U << (trace->op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17547
0
      }
17548
0
      return (op1_info & MAY_BE_LONG)
17549
0
        && (op2_info & MAY_BE_LONG);
17550
0
    case ZEND_PRE_INC:
17551
0
    case ZEND_PRE_DEC:
17552
0
    case ZEND_POST_INC:
17553
0
    case ZEND_POST_DEC:
17554
0
      op1_info = OP1_INFO();
17555
0
      return opline->op1_type == IS_CV
17556
0
        && (op1_info & MAY_BE_LONG)
17557
0
        && !(op1_info & MAY_BE_REF);
17558
0
    case ZEND_STRLEN:
17559
0
      op1_info = OP1_INFO();
17560
0
      return (opline->op1_type & (IS_CV|IS_CONST))
17561
0
        && (op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_STRING;
17562
0
    case ZEND_COUNT:
17563
0
      op1_info = OP1_INFO();
17564
0
      return (opline->op1_type & (IS_CV|IS_CONST))
17565
0
        && (op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_ARRAY;
17566
0
    case ZEND_JMPZ:
17567
0
    case ZEND_JMPNZ:
17568
0
      if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE) {
17569
0
        if (!ssa->cfg.map) {
17570
0
          return false;
17571
0
        }
17572
0
        if (opline > op_array->opcodes + ssa->cfg.blocks[ssa->cfg.map[opline-op_array->opcodes]].start &&
17573
0
            ((opline-1)->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
17574
0
          return false;
17575
0
        }
17576
0
      }
17577
0
      ZEND_FALLTHROUGH;
17578
0
    case ZEND_BOOL:
17579
0
    case ZEND_BOOL_NOT:
17580
0
    case ZEND_JMPZ_EX:
17581
0
    case ZEND_JMPNZ_EX:
17582
0
      return true;
17583
0
    case ZEND_FETCH_CONSTANT:
17584
0
      return true;
17585
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
17586
0
      if ((opline->extended_value & ZEND_ISEMPTY)) {
17587
0
        return false;
17588
0
      }
17589
0
      ZEND_FALLTHROUGH;
17590
0
    case ZEND_FETCH_DIM_R:
17591
0
    case ZEND_FETCH_DIM_IS:
17592
0
    case ZEND_FETCH_LIST_R:
17593
0
      op1_info = OP1_INFO();
17594
0
      op2_info = OP2_INFO();
17595
0
      if (trace
17596
0
       && trace->op1_type != IS_UNKNOWN
17597
0
       && (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED)) == IS_ARRAY) {
17598
0
        op1_info &= ~((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY);
17599
0
      }
17600
0
      return ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) &&
17601
0
          (((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) ||
17602
0
           ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_STRING));
17603
0
    case ZEND_ASSIGN_DIM_OP:
17604
0
      if (opline->result_type != IS_UNUSED) {
17605
0
        return false;
17606
0
      }
17607
0
      if (!zend_jit_supported_binary_op(opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
17608
0
        return false;
17609
0
      }
17610
0
      ZEND_FALLTHROUGH;
17611
0
    case ZEND_ASSIGN_DIM:
17612
0
    case ZEND_FETCH_DIM_W:
17613
0
    case ZEND_FETCH_DIM_RW:
17614
0
    case ZEND_FETCH_LIST_W:
17615
0
      op1_info = OP1_INFO();
17616
0
      op2_info = OP2_INFO();
17617
0
      if (trace) {
17618
0
        if (opline->op1_type == IS_CV) {
17619
0
          if ((opline->opcode == ZEND_ASSIGN_DIM
17620
0
            || opline->opcode == ZEND_ASSIGN_DIM_OP)
17621
0
           && (opline+1)->op1_type == IS_CV
17622
0
           && (opline+1)->op1.var == opline->op1.var) {
17623
            /* skip $a[x] = $a; */
17624
0
            return false;
17625
0
          }
17626
0
        } else if (opline->op1_type == IS_VAR) {
17627
0
          if (trace->op1_type == IS_UNKNOWN
17628
0
           || !(trace->op1_type & IS_TRACE_INDIRECT)
17629
0
           || opline->result_type != IS_UNUSED) {
17630
0
            return false;
17631
0
          }
17632
0
        }
17633
0
        if (trace->op1_type != IS_UNKNOWN
17634
0
         && (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED)) == IS_ARRAY) {
17635
0
          op1_info &= ~((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY);
17636
0
        }
17637
0
      } else {
17638
0
        if (opline->op1_type != IS_CV) {
17639
0
          return false;
17640
0
        }
17641
0
      }
17642
0
      return ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) &&
17643
0
          (((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) ||
17644
0
           ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_STRING));
17645
0
    case ZEND_ASSIGN_OBJ_OP:
17646
0
      if (opline->result_type != IS_UNUSED) {
17647
0
        return false;
17648
0
      }
17649
0
      if (!zend_jit_supported_binary_op(opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
17650
0
        return false;
17651
0
      }
17652
0
      ZEND_FALLTHROUGH;
17653
0
    case ZEND_FETCH_OBJ_R:
17654
0
    case ZEND_ASSIGN_OBJ:
17655
0
      if (opline->op2_type != IS_CONST
17656
0
       || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
17657
0
       || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
17658
0
        return false;
17659
0
      }
17660
0
      op1_info = OP1_INFO();
17661
0
      return opline->op1_type == IS_UNUSED || (op1_info & MAY_BE_OBJECT);
17662
0
  }
17663
0
  return false;
17664
0
}
17665
17666
static bool zend_jit_var_supports_reg(zend_ssa *ssa, int var)
17667
0
{
17668
0
  if (ssa->vars[var].no_val) {
17669
    /* we don't need the value */
17670
0
    return false;
17671
0
  }
17672
17673
0
  if (!(JIT_G(opt_flags) & ZEND_JIT_REG_ALLOC_GLOBAL)) {
17674
    /* Disable global register allocation,
17675
     * register allocation for SSA variables connected through Phi functions
17676
     */
17677
0
    if (ssa->vars[var].definition_phi) {
17678
0
      return false;
17679
0
    }
17680
0
    if (ssa->vars[var].phi_use_chain) {
17681
0
      zend_ssa_phi *phi = ssa->vars[var].phi_use_chain;
17682
0
      do {
17683
0
        if (!ssa->vars[phi->ssa_var].no_val) {
17684
0
          return false;
17685
0
        }
17686
0
        phi = zend_ssa_next_use_phi(ssa, var, phi);
17687
0
      } while (phi);
17688
0
    }
17689
0
  }
17690
17691
0
  if (((ssa->var_info[var].type & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_DOUBLE) &&
17692
0
      ((ssa->var_info[var].type & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_LONG)) {
17693
      /* bad type */
17694
0
    return false;
17695
0
  }
17696
17697
0
  return true;
17698
0
}
17699
17700
static bool zend_jit_may_be_in_reg(const zend_op_array *op_array, zend_ssa *ssa, int var)
17701
0
{
17702
0
  if (!zend_jit_var_supports_reg(ssa, var)) {
17703
0
    return false;
17704
0
  }
17705
17706
0
  if (ssa->vars[var].definition >= 0) {
17707
0
    uint32_t def = ssa->vars[var].definition;
17708
0
    if (!zend_jit_opline_supports_reg(op_array, ssa, op_array->opcodes + def, ssa->ops + def, NULL)) {
17709
0
      return false;
17710
0
    }
17711
0
  }
17712
17713
0
  if (ssa->vars[var].use_chain >= 0) {
17714
0
    int use = ssa->vars[var].use_chain;
17715
17716
0
    do {
17717
0
      if (!zend_ssa_is_no_val_use(op_array->opcodes + use, ssa->ops + use, var) &&
17718
0
          !zend_jit_opline_supports_reg(op_array, ssa, op_array->opcodes + use, ssa->ops + use, NULL)) {
17719
0
        return false;
17720
0
      }
17721
0
      use = zend_ssa_next_use(ssa->ops, var, use);
17722
0
    } while (use >= 0);
17723
0
  }
17724
17725
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE) {
17726
0
    uint32_t def_block, use_block;
17727
0
    int b, use;
17728
0
    uint32_t j;
17729
0
    zend_basic_block *bb;
17730
0
    zend_ssa_phi *p;
17731
0
    bool ret = true;
17732
0
    zend_worklist worklist;
17733
0
    ALLOCA_FLAG(use_heap)
17734
17735
    /* Check if live range is split by ENTRY block */
17736
0
    if (ssa->vars[var].definition >= 0) {
17737
0
      def_block =ssa->cfg.map[ssa->vars[var].definition];
17738
0
    } else {
17739
0
      ZEND_ASSERT(ssa->vars[var].definition_phi);
17740
0
      def_block = ssa->vars[var].definition_phi->block;
17741
0
    }
17742
17743
0
    ZEND_WORKLIST_ALLOCA(&worklist, ssa->cfg.blocks_count, use_heap);
17744
17745
0
    if (ssa->vars[var].use_chain >= 0) {
17746
0
      use = ssa->vars[var].use_chain;
17747
0
      do {
17748
0
        use_block = ssa->cfg.map[use];
17749
0
        if (use_block != def_block) {
17750
0
          zend_worklist_push(&worklist, use_block);
17751
0
        }
17752
0
        use = zend_ssa_next_use(ssa->ops, var, use);
17753
0
      } while (use >= 0);
17754
0
    }
17755
17756
0
    p = ssa->vars[var].phi_use_chain;
17757
0
    while (p) {
17758
0
      use_block = p->block;
17759
0
      if (use_block != def_block) {
17760
0
        bb = &ssa->cfg.blocks[use_block];
17761
0
        for (j = 0; j < bb->predecessors_count; j++) {
17762
0
          if (p->sources[j] == var) {
17763
0
            use_block = ssa->cfg.predecessors[bb->predecessor_offset + j];
17764
0
            if (use_block != def_block) {
17765
0
              zend_worklist_push(&worklist, use_block);
17766
0
            }
17767
0
          }
17768
0
        }
17769
0
      }
17770
0
      p = zend_ssa_next_use_phi(ssa, var, p);
17771
0
    }
17772
17773
0
    while (zend_worklist_len(&worklist) != 0) {
17774
0
      b = zend_worklist_pop(&worklist);
17775
0
      bb = &ssa->cfg.blocks[b];
17776
0
      if (bb->flags & (ZEND_BB_ENTRY|ZEND_BB_RECV_ENTRY)) {
17777
0
        ret = false;
17778
0
        break;
17779
0
      }
17780
0
      for (j = 0; j < bb->predecessors_count; j++) {
17781
0
        b = ssa->cfg.predecessors[bb->predecessor_offset + j];
17782
0
        if (b != def_block) {
17783
0
          zend_worklist_push(&worklist, b);
17784
0
        }
17785
0
      }
17786
0
    }
17787
17788
0
    ZEND_WORKLIST_FREE_ALLOCA(&worklist, use_heap);
17789
17790
0
    return ret;
17791
0
  }
17792
17793
0
  return true;
17794
0
}
17795
17796
0
static ir_ref jit_frameless_observer(zend_jit_ctx *jit, const zend_op *opline) {
17797
  // JIT: zend_observer_handler_is_unobserved(ZEND_OBSERVER_DATA(fbc))
17798
0
  ir_ref observer_handler;
17799
0
  zend_function *fbc = ZEND_FLF_FUNC(opline);
17800
  // Not need for runtime cache or generator checks here, we just need if_unobserved
17801
0
  ir_ref if_unobserved = jit_observer_fcall_is_unobserved_start(jit, fbc, &observer_handler, IR_UNUSED, IR_UNUSED).if_unobserved;
17802
17803
  // Call zend_frameless_observed_call for the main logic.
17804
0
  ir_CALL_1(IR_VOID, ir_CONST_ADDR((size_t)zend_frameless_observed_call), jit_FP(jit));
17805
17806
0
  ir_ref skip = ir_END();
17807
0
  ir_IF_TRUE(if_unobserved);
17808
0
  return skip;
17809
0
}
17810
17811
static void jit_frameless_icall0(zend_jit_ctx *jit, const zend_op *opline)
17812
0
{
17813
0
  jit_SET_EX_OPLINE(jit, opline);
17814
17815
0
  void *function = ZEND_FLF_HANDLER(opline);
17816
0
  zend_jit_addr res_addr = RES_ADDR();
17817
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17818
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17819
17820
0
  ir_ref skip_observer = IR_UNUSED;
17821
0
  if (ZEND_OBSERVER_ENABLED) {
17822
0
    skip_observer = jit_frameless_observer(jit, opline);
17823
0
  }
17824
17825
0
  ir_CALL_1(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref);
17826
17827
0
  if (skip_observer != IR_UNUSED) {
17828
0
    ir_MERGE_WITH(skip_observer);
17829
0
  }
17830
17831
0
  zend_jit_check_exception(jit);
17832
0
}
17833
17834
static void jit_frameless_icall1(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
17835
0
{
17836
0
  jit_SET_EX_OPLINE(jit, opline);
17837
17838
  /* Avoid dropping RC check in case op escapes. */
17839
0
  if (op1_info & MAY_BE_RC1) {
17840
0
    op1_info |= MAY_BE_RCN;
17841
0
  }
17842
17843
0
  void *function = ZEND_FLF_HANDLER(opline);
17844
0
  zend_jit_addr res_addr = RES_ADDR();
17845
0
  zend_jit_addr op1_addr = OP1_ADDR();
17846
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17847
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17848
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17849
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17850
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17851
0
    op1_info &= ~MAY_BE_UNDEF;
17852
0
    op1_info |= MAY_BE_NULL;
17853
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17854
0
  }
17855
0
  if (op1_info & MAY_BE_REF) {
17856
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17857
0
  }
17858
17859
0
  ir_ref skip_observer = IR_UNUSED;
17860
0
  if (ZEND_OBSERVER_ENABLED) {
17861
0
    skip_observer = jit_frameless_observer(jit, opline);
17862
0
  }
17863
17864
0
  ir_CALL_2(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref);
17865
17866
0
  if (skip_observer != IR_UNUSED) {
17867
0
    ir_MERGE_WITH(skip_observer);
17868
0
  }
17869
17870
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17871
0
  zend_jit_check_exception(jit);
17872
0
}
17873
17874
static void jit_frameless_icall2(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint32_t op2_info)
17875
0
{
17876
0
  jit_SET_EX_OPLINE(jit, opline);
17877
17878
  /* Avoid dropping RC check in case op escapes. */
17879
0
  if (op1_info & MAY_BE_RC1) {
17880
0
    op1_info |= MAY_BE_RCN;
17881
0
  }
17882
0
  if (op2_info & MAY_BE_RC1) {
17883
0
    op2_info |= MAY_BE_RCN;
17884
0
  }
17885
17886
0
  void *function = ZEND_FLF_HANDLER(opline);
17887
0
  zend_jit_addr res_addr = RES_ADDR();
17888
0
  zend_jit_addr op1_addr = OP1_ADDR();
17889
0
  zend_jit_addr op2_addr = OP2_ADDR();
17890
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17891
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17892
0
  ir_ref op2_ref = jit_ZVAL_ADDR(jit, op2_addr);
17893
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17894
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17895
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17896
0
    op1_info &= ~MAY_BE_UNDEF;
17897
0
    op1_info |= MAY_BE_NULL;
17898
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17899
0
  }
17900
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
17901
0
    op2_ref = zend_jit_zval_check_undef(jit, op2_ref, opline->op2.var, opline, true);
17902
0
    op2_info &= ~MAY_BE_UNDEF;
17903
0
    op2_info |= MAY_BE_NULL;
17904
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2_ref);
17905
0
  }
17906
0
  if (op1_info & MAY_BE_REF) {
17907
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17908
0
  }
17909
0
  if (op2_info & MAY_BE_REF) {
17910
0
    op2_ref = jit_ZVAL_DEREF_ref(jit, op2_ref);
17911
0
  }
17912
17913
0
  ir_ref skip_observer = IR_UNUSED;
17914
0
  if (ZEND_OBSERVER_ENABLED) {
17915
0
    skip_observer = jit_frameless_observer(jit, opline);
17916
0
  }
17917
17918
0
  ir_CALL_3(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref, op2_ref);
17919
17920
0
  if (skip_observer != IR_UNUSED) {
17921
0
    ir_MERGE_WITH(skip_observer);
17922
0
  }
17923
17924
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17925
  /* Set OP1 to UNDEF in case FREE_OP2() throws. */
17926
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) != 0
17927
0
   && (opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
17928
0
   && (op2_info & MAY_BE_RC1)
17929
0
   && (op2_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
17930
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_UNDEF);
17931
0
    if (JIT_G(current_frame)) {
17932
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
17933
0
        EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17934
0
    }
17935
0
  }
17936
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
17937
0
  zend_jit_check_exception(jit);
17938
0
}
17939
17940
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)
17941
0
{
17942
0
  jit_SET_EX_OPLINE(jit, opline);
17943
17944
  /* Avoid dropping RC check in case op escapes. */
17945
0
  if (op1_info & MAY_BE_RC1) {
17946
0
    op1_info |= MAY_BE_RCN;
17947
0
  }
17948
0
  if (op2_info & MAY_BE_RC1) {
17949
0
    op2_info |= MAY_BE_RCN;
17950
0
  }
17951
0
  if (op1_data_info & MAY_BE_RC1) {
17952
0
    op1_data_info |= MAY_BE_RCN;
17953
0
  }
17954
17955
0
  void *function = ZEND_FLF_HANDLER(opline);
17956
0
  uint8_t op_data_type = (opline + 1)->op1_type;
17957
0
  zend_jit_addr res_addr = RES_ADDR();
17958
0
  zend_jit_addr op1_addr = OP1_ADDR();
17959
0
  zend_jit_addr op2_addr = OP2_ADDR();
17960
0
  zend_jit_addr op3_addr = OP1_DATA_ADDR();
17961
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17962
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17963
0
  ir_ref op2_ref = jit_ZVAL_ADDR(jit, op2_addr);
17964
0
  ir_ref op3_ref = jit_ZVAL_ADDR(jit, op3_addr);
17965
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17966
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17967
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17968
0
    op1_info &= ~MAY_BE_UNDEF;
17969
0
    op1_info |= MAY_BE_NULL;
17970
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17971
0
  }
17972
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
17973
0
    op2_ref = zend_jit_zval_check_undef(jit, op2_ref, opline->op2.var, opline, true);
17974
0
    op2_info &= ~MAY_BE_UNDEF;
17975
0
    op2_info |= MAY_BE_NULL;
17976
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2_ref);
17977
0
  }
17978
0
  if ((opline+1)->op1_type == IS_CV && (op1_data_info & MAY_BE_UNDEF)) {
17979
0
    op3_ref = zend_jit_zval_check_undef(jit, op3_ref, (opline+1)->op1.var, opline, true);
17980
0
    op1_data_info &= ~MAY_BE_UNDEF;
17981
0
    op1_data_info |= MAY_BE_NULL;
17982
0
    op3_addr = ZEND_ADDR_REF_ZVAL(op3_ref);
17983
0
  }
17984
0
  if (op1_info & MAY_BE_REF) {
17985
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17986
0
  }
17987
0
  if (op2_info & MAY_BE_REF) {
17988
0
    op2_ref = jit_ZVAL_DEREF_ref(jit, op2_ref);
17989
0
  }
17990
0
  if (op1_data_info & MAY_BE_REF) {
17991
0
    op3_ref = jit_ZVAL_DEREF_ref(jit, op3_ref);
17992
0
  }
17993
17994
0
  ir_ref skip_observer = IR_UNUSED;
17995
0
  if (ZEND_OBSERVER_ENABLED) {
17996
0
    skip_observer = jit_frameless_observer(jit, opline);
17997
0
  }
17998
17999
0
  ir_CALL_4(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref, op2_ref, op3_ref);
18000
18001
0
  if (skip_observer != IR_UNUSED) {
18002
0
    ir_MERGE_WITH(skip_observer);
18003
0
  }
18004
18005
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
18006
  /* Set OP1 to UNDEF in case FREE_OP2() throws. */
18007
0
  bool op1_undef = false;
18008
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
18009
0
   && (((opline->op2_type & (IS_VAR|IS_TMP_VAR))
18010
0
     && (op2_info & MAY_BE_RC1)
18011
0
     && (op2_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)))
18012
0
    || ((op_data_type & (IS_VAR|IS_TMP_VAR))
18013
0
     && (op1_data_info & MAY_BE_RC1)
18014
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))))) {
18015
0
      op1_undef = true;
18016
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_UNDEF);
18017
0
    if (JIT_G(current_frame)) {
18018
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
18019
0
        EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
18020
0
    }
18021
0
  }
18022
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
18023
  /* If OP1 is set to UNDEF, we don't need to set OP2 to UNDEF on free because
18024
   * zend_fetch_debug_backtrace aborts when it encounters the first UNDEF TMP|VAR. */
18025
0
  if (!op1_undef
18026
0
   && (opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
18027
0
   && (op_data_type & (IS_VAR|IS_TMP_VAR)) != 0
18028
0
   && (op1_data_info & MAY_BE_RC1)
18029
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))) {
18030
0
    jit_set_Z_TYPE_INFO(jit, op2_addr, IS_UNDEF);
18031
0
    if (JIT_G(current_frame)) {
18032
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
18033
0
        EX_VAR_TO_NUM(opline->op2.var), IS_UNKNOWN, 1);
18034
0
    }
18035
0
  }
18036
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, op1_data_info, NULL);
18037
0
  zend_jit_check_exception(jit);
18038
0
}
18039
18040
/*
18041
 * Local variables:
18042
 * tab-width: 4
18043
 * c-basic-offset: 4
18044
 * indent-tabs-mode: t
18045
 * End:
18046
 */