Coverage Report

Created: 2026-06-13 07:01

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
                exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
863
0
                addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
864
0
                exit_flags &= ~ZEND_JIT_EXIT_FIXED;
865
0
              }
866
0
              t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = IR_REG_NUM(reg);
867
0
            }
868
0
          } else {
869
0
            if ((exit_flags & ZEND_JIT_EXIT_FIXED)
870
0
             && (t->stack_map[t->exit_info[exit_point].stack_offset + var].reg != ZREG_NONE
871
0
             || t->stack_map[t->exit_info[exit_point].stack_offset + var].flags != ZREG_TYPE_ONLY)) {
872
0
              exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref);
873
0
              addr = (void*)zend_jit_trace_get_exit_addr(exit_point);
874
0
              exit_flags &= ~ZEND_JIT_EXIT_FIXED;
875
0
            }
876
0
            t->stack_map[t->exit_info[exit_point].stack_offset + var].reg = ZREG_NONE;
877
0
            t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_TYPE_ONLY;
878
0
          }
879
0
        } else if (!(exit_flags & ZEND_JIT_EXIT_FIXED)) {
880
0
          int32_t idx = _add_trace_const(t, ctx->ir_base[ref].val.i64);
881
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].flags = ZREG_CONST;
882
0
          t->stack_map[t->exit_info[exit_point].stack_offset + var].ref = idx;
883
0
        }
884
0
      }
885
0
    }
886
0
  }
887
0
  t->exit_info[exit_point].flags |= ZEND_JIT_EXIT_FIXED;
888
0
  return addr;
889
0
}
890
891
static void jit_SIDE_EXIT(zend_jit_ctx *jit, ir_ref addr)
892
0
{
893
0
  jit_SNAPSHOT(jit, addr);
894
0
  ir_IJMP(addr);
895
0
}
896
897
/* PHP JIT helpers */
898
899
static ir_ref jit_EMALLOC(zend_jit_ctx *jit, size_t size, const zend_op_array *op_array, const zend_op *opline)
900
0
{
901
0
#if ZEND_DEBUG
902
0
  return ir_CALL_5(IR_ADDR, ir_CONST_FC_FUNC(_emalloc),
903
0
    ir_CONST_ADDR(size),
904
0
    op_array->filename ? ir_CONST_ADDR(op_array->filename->val) : IR_NULL,
905
0
    ir_CONST_U32(opline ? opline->lineno : 0),
906
0
    IR_NULL,
907
0
    ir_CONST_U32(0));
908
#elif defined(HAVE_BUILTIN_CONSTANT_P)
909
  if (size > 24 && size <= 32) {
910
    return ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(_emalloc_32));
911
  } else {
912
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_emalloc), ir_CONST_ADDR(size));
913
  }
914
#else
915
  return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_emalloc), ir_CONST_ADDR(size));
916
#endif
917
0
}
918
919
static ir_ref jit_EFREE(zend_jit_ctx *jit, ir_ref ptr, size_t size, const zend_op_array *op_array, const zend_op *opline)
920
0
{
921
0
#if ZEND_DEBUG
922
0
  return ir_CALL_5(IR_ADDR, ir_CONST_FC_FUNC(_efree),
923
0
    ptr,
924
0
    op_array && op_array->filename ? ir_CONST_ADDR(op_array->filename->val) : IR_NULL,
925
0
    ir_CONST_U32(opline ? opline->lineno : 0),
926
0
    IR_NULL,
927
0
    ir_CONST_U32(0));
928
#elif defined(HAVE_BUILTIN_CONSTANT_P)
929
  if (size > 24 && size <= 32) {
930
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree_32), ptr);
931
  } else {
932
    return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree), ptr);
933
  }
934
#else
935
  return ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(_efree), ptr);
936
#endif
937
0
}
938
939
static ir_ref jit_FP(zend_jit_ctx *jit)
940
0
{
941
0
  ZEND_ASSERT(jit->ctx.control);
942
0
  if (jit->fp == IR_UNUSED) {
943
    /* Emit "RLOAD FP" once for basic block */
944
0
    jit->fp = ir_RLOAD_A(ZREG_FP);
945
0
  } else {
946
0
    ir_insn *insn;
947
0
    ir_ref ref = jit->ctx.control;
948
949
0
    while (1) {
950
0
      if (ref == jit->fp) {
951
0
        break;
952
0
      }
953
0
      insn = &jit->ctx.ir_base[ref];
954
0
      if (insn->op >= IR_START || insn->op == IR_CALL) {
955
0
        jit->fp = ir_RLOAD_A(ZREG_FP);
956
0
        break;
957
0
      }
958
0
      ref = insn->op1;
959
0
    }
960
0
  }
961
0
  return jit->fp;
962
0
}
963
964
static void jit_STORE_FP(zend_jit_ctx *jit, ir_ref ref)
965
0
{
966
0
  ir_RSTORE(ZREG_FP, ref);
967
0
  jit->fp = IR_UNUSED;
968
0
}
969
970
static ir_ref jit_IP(zend_jit_ctx *jit)
971
0
{
972
0
  return ir_RLOAD_A(ZREG_IP);
973
0
}
974
975
static void jit_STORE_IP(zend_jit_ctx *jit, ir_ref ref)
976
0
{
977
0
  ir_RSTORE(ZREG_IP, ref);
978
0
}
979
980
static ir_ref jit_IP32(zend_jit_ctx *jit)
981
0
{
982
0
  return ir_RLOAD_U32(ZREG_IP);
983
0
}
984
985
static void jit_LOAD_IP_ADDR(zend_jit_ctx *jit, const zend_op *target)
986
0
{
987
0
  jit_STORE_IP(jit, ir_CONST_ADDR(target));
988
0
}
989
990
static void zend_jit_track_last_valid_opline(zend_jit_ctx *jit)
991
0
{
992
0
  jit->use_last_valid_opline = false;
993
0
  jit->track_last_valid_opline = true;
994
0
}
995
996
static void zend_jit_use_last_valid_opline(zend_jit_ctx *jit)
997
0
{
998
0
  if (jit->track_last_valid_opline) {
999
0
    jit->use_last_valid_opline = true;
1000
0
    jit->track_last_valid_opline = false;
1001
0
  }
1002
0
}
1003
1004
static bool zend_jit_trace_uses_initial_ip(zend_jit_ctx *jit)
1005
0
{
1006
0
  return jit->use_last_valid_opline;
1007
0
}
1008
1009
static void zend_jit_set_last_valid_opline(zend_jit_ctx *jit, const zend_op *opline)
1010
0
{
1011
0
  if (!jit->reuse_ip) {
1012
0
    jit->track_last_valid_opline = true;
1013
0
    jit->last_valid_opline = opline;
1014
0
  }
1015
0
}
1016
1017
static void zend_jit_reset_last_valid_opline(zend_jit_ctx *jit)
1018
0
{
1019
0
  jit->track_last_valid_opline = false;
1020
0
  jit->last_valid_opline = NULL;
1021
0
}
1022
1023
static void zend_jit_start_reuse_ip(zend_jit_ctx *jit)
1024
0
{
1025
0
  zend_jit_reset_last_valid_opline(jit);
1026
0
  jit->reuse_ip = true;
1027
0
}
1028
1029
static int zend_jit_reuse_ip(zend_jit_ctx *jit)
1030
0
{
1031
0
  if (!jit->reuse_ip) {
1032
0
    zend_jit_start_reuse_ip(jit);
1033
    // RX = EX(call);
1034
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(call)));
1035
0
  }
1036
0
  return 1;
1037
0
}
1038
1039
static void zend_jit_stop_reuse_ip(zend_jit_ctx *jit)
1040
0
{
1041
0
  jit->reuse_ip = false;
1042
0
}
1043
1044
static int zend_jit_save_call_chain(zend_jit_ctx *jit, uint32_t call_level)
1045
0
{
1046
0
  ir_ref rx, call;
1047
1048
0
  if (call_level == 1) {
1049
    // JIT: call = NULL;
1050
0
    call = IR_NULL;
1051
0
  } else {
1052
    // JIT: call = EX(call);
1053
0
    call = ir_LOAD_A(jit_EX(call));
1054
0
  }
1055
1056
0
  rx = jit_IP(jit);
1057
1058
  // JIT: call->prev_execute_data = call;
1059
0
  ir_STORE(jit_CALL(rx, prev_execute_data), call);
1060
1061
  // JIT: EX(call) = call;
1062
0
  ir_STORE(jit_EX(call), rx);
1063
1064
0
  jit->delayed_call_level = 0;
1065
0
  delayed_call_chain = false;
1066
1067
0
  return 1;
1068
0
}
1069
1070
static int zend_jit_set_ip(zend_jit_ctx *jit, const zend_op *target)
1071
0
{
1072
0
  ir_ref ref;
1073
1074
0
  if (jit->delayed_call_level) {
1075
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
1076
0
      return 0;
1077
0
    }
1078
0
  }
1079
1080
0
  if (jit->last_valid_opline) {
1081
0
    zend_jit_use_last_valid_opline(jit);
1082
0
    if (jit->last_valid_opline != target) {
1083
0
      ref = jit_IP(jit);
1084
0
      if (target > jit->last_valid_opline) {
1085
0
        ref = ir_ADD_OFFSET(ref, (uintptr_t)target - (uintptr_t)jit->last_valid_opline);
1086
0
      } else {
1087
0
        ref = ir_SUB_A(ref, ir_CONST_ADDR((uintptr_t)jit->last_valid_opline - (uintptr_t)target));
1088
0
      }
1089
0
      jit_STORE_IP(jit, ref);
1090
0
    }
1091
0
  } else {
1092
0
    jit_STORE_IP(jit, ir_CONST_ADDR(target));
1093
0
  }
1094
0
  jit->reuse_ip = false;
1095
0
  zend_jit_set_last_valid_opline(jit, target);
1096
0
  return 1;
1097
0
}
1098
1099
static void jit_SET_EX_OPLINE(zend_jit_ctx *jit, const zend_op *target)
1100
0
{
1101
0
  if (jit->last_valid_opline == target) {
1102
0
    zend_jit_use_last_valid_opline(jit);
1103
    // EX(opline) = opline
1104
0
    ir_STORE(jit_EX(opline), jit_IP(jit));
1105
0
  } else {
1106
0
    ir_STORE(jit_EX(opline), ir_CONST_ADDR(target));
1107
0
  }
1108
0
}
1109
1110
static ir_ref jit_ZVAL_ADDR(zend_jit_ctx *jit, zend_jit_addr addr)
1111
0
{
1112
0
  if (Z_MODE(addr) == IS_MEM_ZVAL) {
1113
0
    ir_ref reg;
1114
1115
0
    if (Z_REG(addr) == ZREG_FP) {
1116
0
      reg = jit_FP(jit);
1117
0
    } else if (Z_REG(addr) == ZREG_RX) {
1118
0
      reg = jit_IP(jit);
1119
0
    } else {
1120
0
      ZEND_UNREACHABLE();
1121
0
    }
1122
0
    return ir_ADD_OFFSET(reg, Z_OFFSET(addr));
1123
0
  } else if (Z_MODE(addr) == IS_REF_ZVAL) {
1124
0
    return Z_IR_REF(addr);
1125
0
  } else {
1126
0
    ZEND_ASSERT(Z_MODE(addr) == IS_CONST_ZVAL);
1127
0
    return ir_CONST_ADDR(Z_ZV(addr));
1128
0
  }
1129
0
}
1130
1131
static ir_ref jit_Z_TYPE_ref(zend_jit_ctx *jit, ir_ref ref)
1132
0
{
1133
0
  return ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zval, u1.v.type)));
1134
0
}
1135
1136
static ir_ref jit_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr)
1137
0
{
1138
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1139
0
    return ir_CONST_U8(Z_TYPE_P(Z_ZV(addr)));
1140
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1141
0
    ir_ref reg;
1142
1143
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1144
0
    if (Z_REG(addr) == ZREG_FP) {
1145
0
      reg = jit_FP(jit);
1146
0
    } else if (Z_REG(addr) == ZREG_RX) {
1147
0
      reg = jit_IP(jit);
1148
0
    } else {
1149
0
      ZEND_UNREACHABLE();
1150
0
    }
1151
0
    return ir_LOAD_U8(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.v.type)));
1152
0
  } else {
1153
0
    return jit_Z_TYPE_ref(jit, jit_ZVAL_ADDR(jit, addr));
1154
0
  }
1155
0
}
1156
1157
static ir_ref jit_Z_TYPE_FLAGS_ref(zend_jit_ctx *jit, ir_ref ref)
1158
0
{
1159
0
  return ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zval, u1.v.type_flags)));
1160
0
}
1161
1162
static ir_ref jit_Z_TYPE_FLAGS(zend_jit_ctx *jit, zend_jit_addr addr)
1163
0
{
1164
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1165
0
    return ir_CONST_U8(Z_TYPE_FLAGS_P(Z_ZV(addr)));
1166
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1167
0
    ir_ref reg;
1168
1169
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1170
0
    if (Z_REG(addr) == ZREG_FP) {
1171
0
      reg = jit_FP(jit);
1172
0
    } else if (Z_REG(addr) == ZREG_RX) {
1173
0
      reg = jit_IP(jit);
1174
0
    } else {
1175
0
      ZEND_UNREACHABLE();
1176
0
    }
1177
0
    return ir_LOAD_U8(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.v.type_flags)));
1178
0
  } else {
1179
0
    return jit_Z_TYPE_FLAGS_ref(jit, jit_ZVAL_ADDR(jit, addr));
1180
0
  }
1181
0
}
1182
1183
static ir_ref jit_Z_TYPE_INFO_ref(zend_jit_ctx *jit, ir_ref ref)
1184
0
{
1185
0
  return ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zval, u1.type_info)));
1186
0
}
1187
1188
static ir_ref jit_Z_TYPE_INFO(zend_jit_ctx *jit, zend_jit_addr addr)
1189
0
{
1190
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1191
0
    return ir_CONST_U32(Z_TYPE_INFO_P(Z_ZV(addr)));
1192
0
  } else if (Z_MODE(addr) == IS_MEM_ZVAL) {
1193
0
    ir_ref reg;
1194
1195
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1196
0
    if (Z_REG(addr) == ZREG_FP) {
1197
0
      reg = jit_FP(jit);
1198
0
    } else if (Z_REG(addr) == ZREG_RX) {
1199
0
      reg = jit_IP(jit);
1200
0
    } else {
1201
0
      ZEND_UNREACHABLE();
1202
0
    }
1203
0
    return ir_LOAD_U32(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.type_info)));
1204
0
  } else {
1205
0
    return jit_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, addr));
1206
0
  }
1207
0
}
1208
1209
static void jit_set_Z_TYPE_INFO_ref(zend_jit_ctx *jit, ir_ref ref, ir_ref type_info)
1210
0
{
1211
0
  ir_STORE(ir_ADD_OFFSET(ref, offsetof(zval, u1.type_info)), type_info);
1212
0
}
1213
1214
static void jit_set_Z_TYPE_INFO_ex(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref type_info)
1215
0
{
1216
0
  if (Z_MODE(addr) == IS_MEM_ZVAL) {
1217
0
    ir_ref reg;
1218
1219
0
    ZEND_ASSERT(Z_MODE(addr) == IS_MEM_ZVAL);
1220
0
    if (Z_REG(addr) == ZREG_FP) {
1221
0
      reg = jit_FP(jit);
1222
0
    } else if (Z_REG(addr) == ZREG_RX) {
1223
0
      reg = jit_IP(jit);
1224
0
    } else {
1225
0
      ZEND_UNREACHABLE();
1226
0
    }
1227
0
    ir_STORE(ir_ADD_OFFSET(reg, Z_OFFSET(addr) + offsetof(zval, u1.type_info)), type_info);
1228
0
  } else {
1229
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, addr), type_info);
1230
0
  }
1231
0
}
1232
1233
static void jit_set_Z_TYPE_INFO(zend_jit_ctx *jit, zend_jit_addr addr, uint32_t type_info)
1234
0
{
1235
0
  if (type_info < IS_STRING
1236
0
   && Z_MODE(addr) == IS_MEM_ZVAL
1237
0
   && Z_REG(addr) == ZREG_FP
1238
0
   && JIT_G(current_frame)
1239
0
   && STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(addr))) == type_info) {
1240
    /* type is already set */
1241
0
    return;
1242
0
  }
1243
0
  jit_set_Z_TYPE_INFO_ex(jit, addr, ir_CONST_U32(type_info));
1244
0
}
1245
1246
static ir_ref jit_if_Z_TYPE_ref(zend_jit_ctx *jit, ir_ref ref, ir_ref type)
1247
0
{
1248
0
  return ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), type));
1249
0
}
1250
1251
static ir_ref jit_if_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type)
1252
0
{
1253
0
  ZEND_ASSERT(type != IS_UNDEF);
1254
0
  return ir_IF(ir_EQ(jit_Z_TYPE(jit, addr), ir_CONST_U8(type)));
1255
0
}
1256
1257
static ir_ref jit_if_not_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type)
1258
0
{
1259
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1260
1261
0
  if (type != IS_UNDEF) {
1262
0
    ref = ir_NE(ref, ir_CONST_U8(type));
1263
0
  }
1264
0
  return ir_IF(ref);
1265
0
}
1266
1267
static void jit_guard_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type, const void *exit_addr)
1268
0
{
1269
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1270
1271
0
  if (type != IS_UNDEF) {
1272
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(type)), ir_CONST_ADDR(exit_addr));
1273
0
  } else {
1274
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
1275
0
  }
1276
0
}
1277
1278
static void jit_guard_not_Z_TYPE(zend_jit_ctx *jit, zend_jit_addr addr, uint8_t type, const void *exit_addr)
1279
0
{
1280
0
  ir_ref ref = jit_Z_TYPE(jit, addr);
1281
1282
0
  if (type != IS_UNDEF) {
1283
0
    ref = ir_NE(ref, ir_CONST_U8(type));
1284
0
  }
1285
0
  ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
1286
0
}
1287
1288
static ir_ref jit_if_REFCOUNTED(zend_jit_ctx *jit, zend_jit_addr addr)
1289
0
{
1290
0
  return ir_IF(jit_Z_TYPE_FLAGS(jit, addr));
1291
0
}
1292
1293
static ir_ref jit_if_COLLECTABLE_ref(zend_jit_ctx *jit, ir_ref addr_ref)
1294
0
{
1295
0
  return ir_IF(ir_AND_U8(jit_Z_TYPE_FLAGS_ref(jit, addr_ref), ir_CONST_U8(IS_TYPE_COLLECTABLE)));
1296
0
}
1297
1298
static ir_ref jit_Z_LVAL_ref(zend_jit_ctx *jit, ir_ref ref)
1299
0
{
1300
0
  return ir_LOAD_L(ref);
1301
0
}
1302
1303
static ir_ref jit_Z_DVAL_ref(zend_jit_ctx *jit, ir_ref ref)
1304
0
{
1305
0
  return ir_LOAD_D(ref);
1306
0
}
1307
1308
static bool zend_jit_spilling_may_cause_conflict(zend_jit_ctx *jit, int var, ir_ref val)
1309
0
{
1310
0
  if (jit->ctx.ir_base[val].op == IR_RLOAD) {
1311
    /* Deoptimization */
1312
0
    return false;
1313
0
  }
1314
//  if (jit->ctx.ir_base[val].op == IR_LOAD
1315
//   && jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op == IR_ADD
1316
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op == IR_RLOAD
1317
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op2 == ZREG_FP
1318
//   && IR_IS_CONST_REF(jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2)
1319
//   && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2].val.addr == (uintptr_t)EX_NUM_TO_VAR(jit->ssa->vars[var].var)) {
1320
//    /* LOAD from the same location (the LOAD is pinned) */
1321
//    // TODO: should be anti-dependent with the following stores ???
1322
//    return 0;
1323
//  }
1324
0
  if (jit->ssa->vars[var].var < jit->current_op_array->last_var) {
1325
    /* IS_CV */
1326
0
    if (jit->ctx.ir_base[val].op == IR_LOAD
1327
0
     && jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op == IR_ADD
1328
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op == IR_RLOAD
1329
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op1].op2 == ZREG_FP
1330
0
     && IR_IS_CONST_REF(jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2)
1331
0
     && jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2].val.addr != (uintptr_t)EX_NUM_TO_VAR(jit->ssa->vars[var].var)
1332
0
     && EX_VAR_TO_NUM(jit->ctx.ir_base[jit->ctx.ir_base[jit->ctx.ir_base[val].op2].op2].val.addr) < jit->current_op_array->last_var) {
1333
      /* binding between different CVs may cause spill conflict */
1334
0
      return true;
1335
0
    } else if (jit->ssa->vars[var].definition >= 0
1336
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_def == var
1337
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_use >= 0
1338
0
     && jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].no_val
1339
0
     && jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].definition_phi
1340
0
     && (jit->ssa->cfg.blocks[jit->ssa->vars[jit->ssa->ops[jit->ssa->vars[var].definition].op1_use].definition_phi->block].flags & ZEND_BB_LOOP_HEADER)) {
1341
      /* Avoid moving spill store out of loop */
1342
0
      return true;
1343
0
    } else if (jit->ssa->vars[var].definition >= 0
1344
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_def == var
1345
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op1_use >= 0
1346
0
     && jit->ssa->ops[jit->ssa->vars[var].definition].op2_use >= 0
1347
0
     && jit->ra[jit->ssa->ops[jit->ssa->vars[var].definition].op2_use].ref == val) {
1348
      /* Avoid spill conflict between of ASSIGN.op1_def and ASSIGN.op1_use */
1349
0
      return true;
1350
0
    }
1351
0
    return false;
1352
0
  }
1353
0
  return true;
1354
0
}
1355
1356
static void zend_jit_def_reg(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref val)
1357
0
{
1358
0
  int var;
1359
1360
0
  ZEND_ASSERT(Z_MODE(addr) == IS_REG);
1361
0
  var = Z_SSA_VAR(addr);
1362
0
  if (var == jit->delay_var) {
1363
0
    ir_refs_add(jit->delay_refs, val);
1364
0
    return;
1365
0
  }
1366
0
  ZEND_ASSERT(jit->ra && jit->ra[var].ref == IR_NULL);
1367
1368
  /* Negative "var" has special meaning for IR */
1369
0
  if (val > 0) {
1370
0
    if (jit->ctx.binding) {
1371
0
      ir_ref old = ir_binding_find(&jit->ctx, val);
1372
0
      if (old && old != -EX_NUM_TO_VAR(jit->ssa->vars[var].var)) {
1373
0
        val = ir_emit2(&jit->ctx, IR_OPT(IR_COPY, jit->ctx.ir_base[val].type), val, 1);
1374
0
      }
1375
0
    }
1376
0
    if (!zend_jit_spilling_may_cause_conflict(jit, var, val)) {
1377
0
      val = ir_bind(&jit->ctx, -EX_NUM_TO_VAR(jit->ssa->vars[var].var), val);
1378
0
    }
1379
0
  }
1380
0
  jit->ra[var].ref = val;
1381
1382
0
  if (jit->ra[var].flags & ZREG_FORWARD) {
1383
0
    zend_ssa_phi *phi = jit->ssa->vars[var].phi_use_chain;
1384
0
    zend_basic_block *bb;
1385
0
    int n, j, *p;
1386
0
    ir_ref *q;
1387
1388
0
    jit->ra[var].flags &= ~ZREG_FORWARD;
1389
0
    while (phi != NULL) {
1390
0
      zend_ssa_phi *dst_phi = phi;
1391
0
      int src_var = var;
1392
1393
0
      if (dst_phi->pi >= 0) {
1394
0
        jit->ra[src_var].ref = val;
1395
0
        src_var = dst_phi->ssa_var;
1396
0
        if (!(jit->ra[src_var].flags & ZREG_FORWARD)) {
1397
0
          phi = zend_ssa_next_use_phi(jit->ssa, var, phi);
1398
0
          continue;
1399
0
        }
1400
0
        dst_phi = jit->ssa->vars[src_var].phi_use_chain;
1401
0
        ZEND_ASSERT(dst_phi != NULL && "reg forwarding");
1402
0
        ZEND_ASSERT(!zend_ssa_next_use_phi(jit->ssa, src_var, dst_phi) && "reg forwarding");
1403
0
        jit->ra[src_var].flags &= ~ZREG_FORWARD;
1404
0
      }
1405
1406
0
      if (jit->ra[dst_phi->ssa_var].ref > 0) {
1407
0
        ir_insn *phi_insn = &jit->ctx.ir_base[jit->ra[dst_phi->ssa_var].ref];
1408
0
        if (phi_insn->op == IR_PHI) {
1409
//          ZEND_ASSERT(ir_operands_count(ctx, phi_insn) == n + 1);
1410
0
          bb = &jit->ssa->cfg.blocks[dst_phi->block];
1411
0
          n = bb->predecessors_count;
1412
0
          for (j = 0, p = &dst_phi->sources[0], q = phi_insn->ops + 2; j < n; j++, p++, q++) {
1413
0
            if (*p == src_var) {
1414
0
              *q = val;
1415
0
            }
1416
0
          }
1417
0
        }
1418
0
      }
1419
1420
0
      phi = zend_ssa_next_use_phi(jit->ssa, var, phi);
1421
0
    }
1422
0
  }
1423
0
}
1424
1425
static ir_ref zend_jit_use_reg(zend_jit_ctx *jit, zend_jit_addr addr)
1426
0
{
1427
0
  int var = Z_SSA_VAR(addr);
1428
1429
0
  ZEND_ASSERT(Z_MODE(addr) == IS_REG);
1430
0
  ZEND_ASSERT(jit->ra && jit->ra[var].ref);
1431
0
  if (jit->ra[var].ref == IR_NULL) {
1432
0
    zend_jit_addr mem_addr;
1433
0
    ir_ref ref;
1434
1435
0
    ZEND_ASSERT(jit->ra[var].flags & ZREG_LOAD);
1436
0
    mem_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(jit->ssa->vars[var].var));
1437
0
    if ((jit->ssa->var_info[var].type & MAY_BE_ANY) == MAY_BE_LONG) {
1438
0
      ref = jit_Z_LVAL_ref(jit, jit_ZVAL_ADDR(jit, mem_addr));
1439
0
    } else if ((jit->ssa->var_info[var].type & MAY_BE_ANY) == MAY_BE_DOUBLE) {
1440
0
      ref = jit_Z_DVAL_ref(jit, jit_ZVAL_ADDR(jit, mem_addr));
1441
0
    } else {
1442
0
      ZEND_UNREACHABLE();
1443
0
    }
1444
0
    zend_jit_def_reg(jit, addr, ref);
1445
0
    return ref;
1446
0
  }
1447
0
  return jit->ra[Z_SSA_VAR(addr)].ref;
1448
0
}
1449
1450
static void zend_jit_gen_pi(zend_jit_ctx *jit, zend_ssa_phi *phi)
1451
0
{
1452
0
  int src_var = phi->sources[0];
1453
0
  int dst_var = phi->ssa_var;
1454
1455
0
  ZEND_ASSERT(phi->pi >= 0);
1456
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
1457
0
  ZEND_ASSERT(jit->ra[src_var].ref);
1458
1459
0
  if (jit->ra[src_var].ref == IR_NULL) {
1460
    /* Not defined yet */
1461
0
    if (jit->ssa->vars[dst_var].use_chain < 0
1462
0
     && jit->ssa->vars[dst_var].phi_use_chain) {
1463
0
      zend_ssa_phi *phi = jit->ssa->vars[dst_var].phi_use_chain;
1464
0
      if (!zend_ssa_next_use_phi(jit->ssa, dst_var, phi)) {
1465
        /* This is a Pi forwarded to Phi */
1466
0
        jit->ra[src_var].flags |= ZREG_FORWARD;
1467
0
        return;
1468
0
      }
1469
0
    }
1470
0
    ZEND_ASSERT(0 && "Not defined Pi source");
1471
0
  }
1472
  /* Reuse register */
1473
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var),
1474
0
    zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var)));
1475
0
}
1476
1477
static void zend_jit_gen_phi(zend_jit_ctx *jit, zend_ssa_phi *phi)
1478
0
{
1479
0
  int dst_var = phi->ssa_var;
1480
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[phi->block];
1481
0
  uint32_t n = bb->predecessors_count;
1482
0
  ir_type type = (jit->ssa->var_info[phi->ssa_var].type & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE;
1483
0
  ir_ref merge = jit->bb_start_ref[phi->block];
1484
0
  ir_ref ref;
1485
0
  ir_ref old_insns_count = jit->ctx.insns_count;
1486
0
  ir_ref same_src_ref = IR_UNUSED;
1487
0
  bool phi_inputs_are_the_same = true;
1488
1489
0
  ZEND_ASSERT(phi->pi < 0);
1490
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
1491
0
  ZEND_ASSERT(merge);
1492
0
  ZEND_ASSERT(jit->ctx.ir_base[merge].op == IR_MERGE || jit->ctx.ir_base[merge].op == IR_LOOP_BEGIN);
1493
0
  ZEND_ASSERT(n == jit->ctx.ir_base[merge].inputs_count);
1494
1495
0
  ref = ir_emit_N(&jit->ctx, IR_OPT(IR_PHI, type), n + 1);
1496
0
  ir_set_op(&jit->ctx, ref, 1, merge);
1497
1498
0
  for (uint32_t i = 0; i < n; i++) {
1499
0
    int src_var = phi->sources[i];
1500
1501
0
    ZEND_ASSERT(jit->ra[src_var].ref);
1502
0
    if (jit->ra[src_var].ref == IR_NULL) {
1503
0
      jit->ra[src_var].flags |= ZREG_FORWARD;
1504
0
      phi_inputs_are_the_same = false;
1505
0
    } else {
1506
0
      ir_ref src_ref = zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var));
1507
0
      if (i == 0) {
1508
0
        same_src_ref = src_ref;
1509
0
      } else if (same_src_ref != src_ref) {
1510
0
        phi_inputs_are_the_same = false;
1511
0
      }
1512
0
      ir_set_op(&jit->ctx, ref, i + 2, src_ref);
1513
0
    }
1514
0
  }
1515
0
  if (phi_inputs_are_the_same) {
1516
0
    ref = same_src_ref;
1517
0
    jit->ctx.insns_count = old_insns_count;
1518
0
  }
1519
1520
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var), ref);
1521
0
}
1522
1523
static ir_ref jit_Z_LVAL(zend_jit_ctx *jit, zend_jit_addr addr)
1524
0
{
1525
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1526
0
    return ir_CONST_LONG(Z_LVAL_P(Z_ZV(addr)));
1527
0
  } else if (Z_MODE(addr) == IS_REG) {
1528
0
    return zend_jit_use_reg(jit, addr);
1529
0
  } else {
1530
0
    return jit_Z_LVAL_ref(jit, jit_ZVAL_ADDR(jit, addr));
1531
0
  }
1532
0
}
1533
1534
static void jit_set_Z_LVAL(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref lval)
1535
0
{
1536
0
  if (Z_MODE(addr) == IS_REG) {
1537
0
    zend_jit_def_reg(jit, addr, lval);
1538
0
  } else {
1539
0
    ir_STORE(jit_ZVAL_ADDR(jit, addr), lval);
1540
0
  }
1541
0
}
1542
1543
#if SIZEOF_ZEND_LONG == 4
1544
static ir_ref jit_Z_W2(zend_jit_ctx *jit, zend_jit_addr addr)
1545
{
1546
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1547
    return ir_CONST_U32((Z_ZV(addr))->value.ww.w2);
1548
  } else {
1549
    return ir_LOAD_L(ir_ADD_OFFSET(jit_ZVAL_ADDR(jit, addr), offsetof(zval, value.ww.w2)));
1550
  }
1551
}
1552
1553
static void jit_set_Z_W2(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref lval)
1554
{
1555
  ir_STORE(ir_ADD_OFFSET(jit_ZVAL_ADDR(jit, addr), offsetof(zval, value.ww.w2)), lval);
1556
}
1557
#endif
1558
1559
static ir_ref jit_Z_DVAL(zend_jit_ctx *jit, zend_jit_addr addr)
1560
0
{
1561
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1562
0
    return ir_CONST_DOUBLE(Z_DVAL_P(Z_ZV(addr)));
1563
0
  } else if (Z_MODE(addr) == IS_REG) {
1564
0
    return zend_jit_use_reg(jit, addr);
1565
0
  } else {
1566
0
    return jit_Z_DVAL_ref(jit, jit_ZVAL_ADDR(jit, addr));
1567
0
  }
1568
0
}
1569
1570
static void jit_set_Z_DVAL(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref dval)
1571
0
{
1572
0
  if (Z_MODE(addr) == IS_REG) {
1573
0
    zend_jit_def_reg(jit, addr, dval);
1574
0
  } else {
1575
0
    ir_STORE(jit_ZVAL_ADDR(jit, addr), dval);
1576
0
  }
1577
0
}
1578
1579
static ir_ref jit_Z_PTR_ref(zend_jit_ctx *jit, ir_ref ref)
1580
0
{
1581
0
  return ir_LOAD_A(ref);
1582
0
}
1583
1584
static ir_ref jit_Z_PTR(zend_jit_ctx *jit, zend_jit_addr addr)
1585
0
{
1586
0
  if (Z_MODE(addr) == IS_CONST_ZVAL) {
1587
0
    return ir_CONST_ADDR(Z_PTR_P(Z_ZV(addr)));
1588
0
  } else {
1589
0
    return jit_Z_PTR_ref(jit, jit_ZVAL_ADDR(jit, addr));
1590
0
  }
1591
0
}
1592
1593
static void jit_set_Z_PTR(zend_jit_ctx *jit, zend_jit_addr addr, ir_ref ptr)
1594
0
{
1595
0
  ir_STORE(jit_ZVAL_ADDR(jit, addr), ptr);
1596
0
}
1597
1598
static ir_ref jit_GC_REFCOUNT(zend_jit_ctx *jit, ir_ref ref)
1599
0
{
1600
0
  return ir_LOAD_U32(ref);
1601
0
}
1602
1603
static void jit_set_GC_REFCOUNT(zend_jit_ctx *jit, ir_ref ref, uint32_t refcount)
1604
0
{
1605
0
  ir_STORE(ref, ir_CONST_U32(refcount));
1606
0
}
1607
1608
static void jit_GC_ADDREF(zend_jit_ctx *jit, ir_ref ref)
1609
0
{
1610
0
  ir_STORE(ref, ir_ADD_U32(ir_LOAD_U32(ref), ir_CONST_U32(1)));
1611
0
}
1612
1613
static void jit_GC_ADDREF2(zend_jit_ctx *jit, ir_ref ref)
1614
0
{
1615
0
  ir_ref counter = ir_LOAD_U32(ref);
1616
0
  ir_STORE(ref, ir_ADD_U32(counter, ir_CONST_U32(2)));
1617
0
}
1618
1619
static ir_ref jit_GC_DELREF(zend_jit_ctx *jit, ir_ref ref)
1620
0
{
1621
0
  ir_ref counter = ir_LOAD_U32(ref);
1622
0
  counter = ir_SUB_U32(counter, ir_CONST_U32(1));
1623
0
  ir_STORE(ref, counter);
1624
0
  return counter;
1625
0
}
1626
1627
static ir_ref jit_if_GC_MAY_NOT_LEAK(zend_jit_ctx *jit, ir_ref ref)
1628
0
{
1629
0
  return ir_IF(
1630
0
    ir_AND_U32(
1631
0
      ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_refcounted, gc.u.type_info))),
1632
0
      ir_CONST_U32(GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))));
1633
0
}
1634
1635
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)
1636
0
{
1637
0
  ir_ref ref = IR_UNUSED;
1638
1639
0
  if (Z_TYPE_P(zv) > IS_TRUE) {
1640
0
    if (Z_TYPE_P(zv) == IS_DOUBLE) {
1641
0
      jit_set_Z_DVAL(jit, dst, ir_CONST_DOUBLE(Z_DVAL_P(zv)));
1642
0
    } else if (Z_TYPE_P(zv) == IS_LONG && dst_def_info == MAY_BE_DOUBLE) {
1643
0
      jit_set_Z_DVAL(jit, dst, ir_CONST_DOUBLE((double)Z_LVAL_P(zv)));
1644
0
    } else if (Z_TYPE_P(zv) == IS_LONG) {
1645
0
      jit_set_Z_LVAL(jit, dst, ir_CONST_LONG(Z_LVAL_P(zv)));
1646
0
    } else {
1647
0
      ref = ir_CONST_ADDR(Z_PTR_P(zv));
1648
0
      jit_set_Z_PTR(jit, dst, ref);
1649
0
      if (addref && Z_REFCOUNTED_P(zv)) {
1650
0
        jit_GC_ADDREF(jit, ref);
1651
0
      }
1652
0
    }
1653
0
  }
1654
0
  if (Z_MODE(dst) != IS_REG) {
1655
0
    if (dst_def_info == MAY_BE_DOUBLE) {
1656
0
      if ((dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
1657
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
1658
0
      }
1659
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) {
1660
0
      jit_set_Z_TYPE_INFO(jit, dst, Z_TYPE_INFO_P(zv));
1661
0
    }
1662
0
  }
1663
0
}
1664
1665
static ir_ref jit_if_TYPED_REF(zend_jit_ctx *jit, ir_ref ref)
1666
0
{
1667
0
  return ir_IF(ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_reference, sources.ptr))));
1668
0
}
1669
1670
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)
1671
0
{
1672
0
  ir_ref ref = IR_UNUSED;
1673
1674
0
  if (src_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
1675
0
    if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_LONG) {
1676
0
      jit_set_Z_LVAL(jit, dst, jit_Z_LVAL(jit, src));
1677
0
    } else if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_DOUBLE) {
1678
0
      jit_set_Z_DVAL(jit, dst, jit_Z_DVAL(jit, src));
1679
0
    } else {
1680
#if SIZEOF_ZEND_LONG == 4
1681
      if (src_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) {
1682
        jit_set_Z_W2(jit, dst, jit_Z_W2(jit, src));
1683
      }
1684
#endif
1685
0
      ref = jit_Z_PTR(jit, src);
1686
0
      jit_set_Z_PTR(jit, dst, ref);
1687
0
    }
1688
0
  }
1689
0
  if (has_concrete_type(src_info & MAY_BE_ANY)
1690
0
   && (src_info & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))
1691
0
     && !(src_info & MAY_BE_GUARD)) {
1692
0
    if (Z_MODE(dst) != IS_REG
1693
0
     && (dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (src_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD))) {
1694
0
      uint8_t type = concrete_type(src_info);
1695
0
      jit_set_Z_TYPE_INFO(jit, dst, type);
1696
0
    }
1697
0
  } else {
1698
0
    ir_ref type = jit_Z_TYPE_INFO(jit, src);
1699
0
    jit_set_Z_TYPE_INFO_ex(jit, dst, type);
1700
0
    if (addref) {
1701
0
      if (src_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
1702
0
        ir_ref if_refcounted = IR_UNUSED;
1703
1704
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1705
0
          if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(0xff00)));
1706
0
          ir_IF_TRUE(if_refcounted);
1707
0
        }
1708
1709
0
        jit_GC_ADDREF(jit, ref);
1710
1711
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1712
0
          ir_MERGE_WITH_EMPTY_FALSE(if_refcounted);
1713
0
        }
1714
0
      }
1715
0
    }
1716
0
  }
1717
0
}
1718
1719
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)
1720
0
{
1721
0
  ir_ref ref = IR_UNUSED;
1722
1723
0
  if (src_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
1724
0
    if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_LONG) {
1725
0
      ref = jit_Z_LVAL(jit, src);
1726
0
      jit_set_Z_LVAL(jit, dst, ref);
1727
0
      jit_set_Z_LVAL(jit, dst2, ref);
1728
0
    } else if ((src_info & (MAY_BE_ANY|MAY_BE_GUARD)) == MAY_BE_DOUBLE) {
1729
0
      ref = jit_Z_DVAL(jit, src);
1730
0
      jit_set_Z_DVAL(jit, dst, ref);
1731
0
      jit_set_Z_DVAL(jit, dst2, ref);
1732
0
    } else {
1733
#if SIZEOF_ZEND_LONG == 4
1734
      if (src_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) {
1735
        ref = jit_Z_W2(jit, src);
1736
        jit_set_Z_W2(jit, dst, ref);
1737
        jit_set_Z_W2(jit, dst2, ref);
1738
      }
1739
#endif
1740
0
      ref = jit_Z_PTR(jit, src);
1741
0
      jit_set_Z_PTR(jit, dst, ref);
1742
0
      jit_set_Z_PTR(jit, dst2, ref);
1743
0
    }
1744
0
  }
1745
0
  if (has_concrete_type(src_info & MAY_BE_ANY)
1746
0
   && (src_info & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))
1747
0
     && !(src_info & MAY_BE_GUARD)) {
1748
0
    uint8_t type = concrete_type(src_info);
1749
0
    ir_ref type_ref = ir_CONST_U32(type);
1750
1751
0
    if (Z_MODE(dst) != IS_REG
1752
0
     && (dst_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) != (src_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD))) {
1753
0
      jit_set_Z_TYPE_INFO_ex(jit, dst, type_ref);
1754
0
    }
1755
0
    if (Z_MODE(dst2) != IS_REG) {
1756
0
      jit_set_Z_TYPE_INFO_ex(jit, dst2, type_ref);
1757
0
    }
1758
0
  } else {
1759
0
    ir_ref type = jit_Z_TYPE_INFO(jit, src);
1760
0
    jit_set_Z_TYPE_INFO_ex(jit, dst, type);
1761
0
    jit_set_Z_TYPE_INFO_ex(jit, dst2, type);
1762
0
    if (addref) {
1763
0
      if (src_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
1764
0
        ir_ref if_refcounted = IR_UNUSED;
1765
1766
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1767
0
          if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(0xff00)));
1768
0
          ir_IF_TRUE(if_refcounted);
1769
0
        }
1770
1771
0
        if (addref == 2) {
1772
0
          jit_GC_ADDREF2(jit, ref);
1773
0
        } else {
1774
0
          jit_GC_ADDREF(jit, ref);
1775
0
        }
1776
1777
0
        if (src_info & (MAY_BE_ANY-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1778
0
          ir_MERGE_WITH_EMPTY_FALSE(if_refcounted);
1779
0
        }
1780
0
      }
1781
0
    }
1782
0
  }
1783
0
}
1784
1785
static void jit_ZVAL_DTOR(zend_jit_ctx *jit, ir_ref ref, uint32_t op_info, const zend_op *opline)
1786
0
{
1787
0
  if (!((op_info) & MAY_BE_GUARD)
1788
0
   && has_concrete_type((op_info) & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1789
0
    uint8_t type = concrete_type((op_info) & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
1790
0
    if (type == IS_STRING && !ZEND_DEBUG) {
1791
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(_efree), ref);
1792
0
        return;
1793
0
    } else if (type == IS_ARRAY) {
1794
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)) {
1795
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))) {
1796
0
          jit_SET_EX_OPLINE(jit, opline);
1797
0
        }
1798
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_array_destroy), ref);
1799
0
      } else {
1800
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_array_free), ref);
1801
0
      }
1802
0
      return;
1803
0
    } else if (type == IS_OBJECT) {
1804
0
      if (opline) {
1805
0
        jit_SET_EX_OPLINE(jit, opline);
1806
0
      }
1807
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_objects_store_del), ref);
1808
0
      return;
1809
0
    }
1810
0
  }
1811
0
  if (opline) {
1812
0
    jit_SET_EX_OPLINE(jit, opline);
1813
0
  }
1814
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(rc_dtor_func), ref);
1815
0
}
1816
1817
static void jit_ZVAL_PTR_DTOR(zend_jit_ctx  *jit,
1818
                              zend_jit_addr  addr,
1819
                              uint32_t       op_info,
1820
                              bool           gc,
1821
                              const zend_op *opline)
1822
0
{
1823
0
    ir_ref ref, ref2;
1824
0
  ir_ref if_refcounted = IR_UNUSED;
1825
0
  ir_ref if_not_zero = IR_UNUSED;
1826
0
  ir_ref end_inputs = IR_UNUSED;
1827
1828
0
  if (op_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF|MAY_BE_GUARD)) {
1829
0
    if ((op_info) & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_INDIRECT|MAY_BE_GUARD)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
1830
0
      if_refcounted = jit_if_REFCOUNTED(jit, addr);
1831
0
      ir_IF_FALSE(if_refcounted);
1832
0
      ir_END_list(end_inputs);
1833
0
      ir_IF_TRUE(if_refcounted);
1834
0
    }
1835
0
    ref = jit_Z_PTR(jit, addr);
1836
0
    ref2 = jit_GC_DELREF(jit, ref);
1837
1838
0
    if (((op_info) & MAY_BE_GUARD) || RC_MAY_BE_1(op_info)) {
1839
0
      if (((op_info) & MAY_BE_GUARD) || RC_MAY_BE_N(op_info)) {
1840
0
        if_not_zero = ir_IF(ref2);
1841
0
        ir_IF_FALSE(if_not_zero);
1842
0
      }
1843
      // zval_dtor_func(r);
1844
0
      jit_ZVAL_DTOR(jit, ref, op_info, opline);
1845
0
      if (if_not_zero) {
1846
0
        ir_END_list(end_inputs);
1847
0
        ir_IF_TRUE(if_not_zero);
1848
0
      }
1849
0
    }
1850
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))) {
1851
0
      ir_ref if_may_not_leak;
1852
1853
0
      if ((op_info) & (MAY_BE_REF|MAY_BE_GUARD)) {
1854
0
        ir_ref if_ref, if_collectable;
1855
1856
0
        if_ref = jit_if_Z_TYPE(jit, addr, IS_REFERENCE);
1857
0
        ir_IF_TRUE(if_ref);
1858
1859
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
1860
1861
0
        if_collectable = jit_if_COLLECTABLE_ref(jit, ref2);
1862
0
        ir_IF_FALSE(if_collectable);
1863
0
        ir_END_list(end_inputs);
1864
0
        ir_IF_TRUE(if_collectable);
1865
1866
0
        ref2 = jit_Z_PTR_ref(jit, ref2);
1867
1868
0
        ir_MERGE_WITH_EMPTY_FALSE(if_ref);
1869
0
        ref = ir_PHI_2(IR_ADDR, ref2, ref);
1870
0
      }
1871
1872
0
      if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
1873
0
      ir_IF_TRUE(if_may_not_leak);
1874
0
      ir_END_list(end_inputs);
1875
0
      ir_IF_FALSE(if_may_not_leak);
1876
1877
0
      if (opline) {
1878
0
        jit_SET_EX_OPLINE(jit, opline);
1879
0
      }
1880
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
1881
0
    }
1882
1883
0
    if (end_inputs) {
1884
0
      ir_END_list(end_inputs);
1885
0
      ir_MERGE_list(end_inputs);
1886
0
    }
1887
0
  }
1888
0
}
1889
1890
static void jit_FREE_OP(zend_jit_ctx  *jit,
1891
                        uint8_t        op_type,
1892
                        znode_op       op,
1893
                        uint32_t       op_info,
1894
                        const zend_op *opline)
1895
0
{
1896
0
  if (op_type & (IS_VAR|IS_TMP_VAR)) {
1897
0
    jit_ZVAL_PTR_DTOR(jit,
1898
0
      ZEND_ADDR_MEM_ZVAL(ZREG_FP, op.var),
1899
0
      op_info, false, opline);
1900
0
  }
1901
0
}
1902
1903
static void jit_OBJ_RELEASE(zend_jit_ctx  *jit, ir_ref ref)
1904
0
{
1905
0
  ir_ref end_inputs = IR_UNUSED;
1906
0
    ir_ref if_not_zero, if_may_not_leak;
1907
1908
  // JIT: if (GC_DELREF(obj) == 0) {
1909
0
  if_not_zero = ir_IF(jit_GC_DELREF(jit, ref));
1910
0
  ir_IF_FALSE(if_not_zero);
1911
1912
  // JIT: zend_objects_store_del(obj)
1913
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_objects_store_del), ref);
1914
0
  ir_END_list(end_inputs);
1915
1916
0
  ir_IF_TRUE(if_not_zero);
1917
0
  if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
1918
1919
0
  ir_IF_TRUE(if_may_not_leak);
1920
0
  ir_END_list(end_inputs);
1921
1922
0
  ir_IF_FALSE(if_may_not_leak);
1923
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
1924
0
  ir_END_list(end_inputs);
1925
1926
0
  ir_MERGE_list(end_inputs);
1927
0
}
1928
1929
static void zend_jit_check_timeout(zend_jit_ctx *jit, const zend_op *opline, const void *exit_addr)
1930
0
{
1931
0
  ir_ref ref = ir_LOAD_U8(jit_EG(vm_interrupt));
1932
1933
0
  if (exit_addr) {
1934
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
1935
0
  } else if (!opline || jit->last_valid_opline == opline) {
1936
0
    ir_GUARD_NOT(ref, jit_STUB_ADDR(jit, jit_stub_interrupt_handler));
1937
0
  } else {
1938
0
    ir_ref if_timeout = ir_IF(ref);
1939
1940
0
    ir_IF_TRUE_cold(if_timeout);
1941
0
    jit_LOAD_IP_ADDR(jit, opline);
1942
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_interrupt_handler));
1943
0
    ir_IF_FALSE(if_timeout);
1944
0
  }
1945
0
}
1946
1947
static void zend_jit_vm_enter(zend_jit_ctx *jit, ir_ref to_opline)
1948
0
{
1949
  // ZEND_VM_ENTER()
1950
0
  ir_RETURN(ir_OR_A(to_opline, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)));
1951
0
}
1952
1953
static void zend_jit_vm_leave(zend_jit_ctx *jit, ir_ref to_opline)
1954
0
{
1955
0
  // ZEND_VM_LEAVE()
1956
0
  ir_RETURN(ir_OR_A(to_opline, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)));
1957
0
}
1958
1959
static void zend_jit_tailcall_handler(zend_jit_ctx *jit, ir_ref handler)
1960
0
{
1961
#if defined(IR_TARGET_X86)
1962
  if (!IR_IS_CONST_REF(handler)) {
1963
    handler = ir_CAST_OPCODE_HANDLER_FUNC(handler);
1964
  }
1965
#endif
1966
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
1967
0
    ir_TAILCALL(IR_OPCODE_HANDLER_RET, handler);
1968
0
  } else {
1969
0
    ir_TAILCALL_2(IR_ADDR, handler, jit_FP(jit), jit_IP(jit));
1970
0
  }
1971
0
}
1972
1973
/* stubs */
1974
1975
static int zend_jit_exception_handler_stub(zend_jit_ctx *jit)
1976
0
{
1977
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
1978
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(EG(exception_op));
1979
1980
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
1981
0
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
1982
0
  } else {
1983
0
    zend_vm_opcode_handler_t handler = EG(exception_op)->handler;
1984
1985
0
    if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
1986
0
      zend_jit_tailcall_handler(jit, ir_CONST_OPCODE_HANDLER_FUNC(handler));
1987
0
    } else {
1988
0
      ir_ref ref = ir_CALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
1989
0
      zend_jit_vm_enter(jit, ref);
1990
0
    }
1991
0
  }
1992
0
  return 1;
1993
0
}
1994
1995
static int zend_jit_exception_handler_undef_stub(zend_jit_ctx *jit)
1996
0
{
1997
0
  ir_ref ref, result_type, if_result_used;
1998
1999
0
  ref = jit_EG(opline_before_exception);
2000
0
  result_type = ir_LOAD_U8(ir_ADD_OFFSET(ir_LOAD_A(ref), offsetof(zend_op, result_type)));
2001
2002
0
  if_result_used = ir_IF(ir_AND_U8(result_type, ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2003
0
  ir_IF_TRUE(if_result_used);
2004
2005
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(ref), offsetof(zend_op, result.var)));
2006
0
  if (sizeof(void*) == 8) {
2007
0
    ref = ir_ZEXT_A(ref);
2008
0
  }
2009
0
  ir_STORE(ir_ADD_OFFSET(ir_ADD_A(jit_FP(jit), ref), offsetof(zval, u1.type_info)), ir_CONST_U32(IS_UNDEF));
2010
0
  ir_MERGE_WITH_EMPTY_FALSE(if_result_used);
2011
2012
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2013
2014
0
  return 1;
2015
0
}
2016
2017
static int zend_jit_exception_handler_free_op1_op2_stub(zend_jit_ctx *jit)
2018
0
{
2019
0
  ir_ref ref, if_dtor;
2020
0
  zend_jit_addr var_addr;
2021
2022
0
  ref = ir_LOAD_A(jit_EG(opline_before_exception));
2023
0
  if_dtor = ir_IF(ir_AND_U8(ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zend_op, op1_type))),
2024
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2025
0
  ir_IF_TRUE(if_dtor);
2026
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op1.var)));
2027
0
  if (sizeof(void*) == 8) {
2028
0
    ref = ir_ZEXT_A(ref);
2029
0
  }
2030
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2031
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
2032
0
  jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2033
0
  ir_MERGE_WITH_EMPTY_FALSE(if_dtor);
2034
2035
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
2036
2037
0
  return 1;
2038
0
}
2039
2040
static int zend_jit_exception_handler_free_op2_stub(zend_jit_ctx *jit)
2041
0
{
2042
0
  ir_ref ref, if_dtor;
2043
0
  zend_jit_addr var_addr;
2044
2045
0
  ref = ir_LOAD_A(jit_EG(opline_before_exception));
2046
0
  if_dtor = ir_IF(ir_AND_U8(ir_LOAD_U8(ir_ADD_OFFSET(ref, offsetof(zend_op, op2_type))),
2047
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2048
0
  ir_IF_TRUE(if_dtor);
2049
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.var)));
2050
0
  if (sizeof(void*) == 8) {
2051
0
    ref = ir_ZEXT_A(ref);
2052
0
  }
2053
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2054
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
2055
0
  jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2056
0
  ir_MERGE_WITH_EMPTY_FALSE(if_dtor);
2057
2058
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
2059
2060
0
  return 1;
2061
0
}
2062
2063
static int zend_jit_interrupt_handler_stub(zend_jit_ctx *jit)
2064
0
{
2065
0
  ir_ref if_timeout, if_exception;
2066
2067
  // EX(opline) = opline
2068
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2069
2070
0
  ir_STORE(jit_EG(vm_interrupt), ir_CONST_U8(0));
2071
0
  if_timeout = ir_IF(ir_EQ(ir_LOAD_U8(jit_EG(timed_out)), ir_CONST_U8(0)));
2072
0
  ir_IF_FALSE(if_timeout);
2073
0
  ir_CALL(IR_VOID, ir_CONST_FUNC(zend_timeout));
2074
0
  ir_MERGE_WITH_EMPTY_TRUE(if_timeout);
2075
2076
0
  if (zend_interrupt_function) {
2077
0
    ir_CALL_1(IR_VOID, ir_CONST_FUNC(zend_interrupt_function), jit_FP(jit));
2078
0
    if_exception = ir_IF(ir_LOAD_A(jit_EG(exception)));
2079
0
    ir_IF_TRUE(if_exception);
2080
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(zend_jit_exception_in_interrupt_handler_helper));
2081
0
    ir_MERGE_WITH_EMPTY_FALSE(if_exception);
2082
2083
0
    jit_STORE_FP(jit, ir_LOAD_A(jit_EG(current_execute_data)));
2084
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2085
0
  }
2086
2087
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2088
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2089
0
  } else {
2090
0
    zend_jit_vm_enter(jit, jit_IP(jit));
2091
0
  }
2092
0
  return 1;
2093
0
}
2094
2095
static int zend_jit_leave_function_handler_stub(zend_jit_ctx *jit)
2096
0
{
2097
0
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
2098
0
  ir_TAILCALL(IR_OPCODE_HANDLER_RET, ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_leave_func_helper_tailcall));
2099
0
  return 1;
2100
#else
2101
  ir_ref call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
2102
  ir_ref if_top = ir_IF(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_TOP)));
2103
2104
  ir_IF_FALSE(if_top);
2105
2106
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2107
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), call_info);
2108
    jit_STORE_IP(jit,
2109
      ir_LOAD_A(jit_EX(opline)));
2110
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2111
  } else if (GCC_GLOBAL_REGS) {
2112
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), call_info);
2113
  } else {
2114
    ir_TAILCALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_nested_func_helper), jit_FP(jit), jit_IP(jit), call_info);
2115
  }
2116
2117
  ir_IF_TRUE(if_top);
2118
2119
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2120
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), call_info);
2121
    ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2122
  } else if (GCC_GLOBAL_REGS) {
2123
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), call_info);
2124
  } else {
2125
    ir_TAILCALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_top_func_helper), jit_FP(jit), jit_IP(jit), call_info);
2126
  }
2127
2128
  return 1;
2129
#endif /* ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL */
2130
0
}
2131
2132
static int zend_jit_negative_shift_stub(zend_jit_ctx *jit)
2133
0
{
2134
0
  ir_CALL_2(IR_VOID,
2135
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2136
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2137
0
    ir_CONST_ADDR(zend_ce_arithmetic_error),
2138
0
    ir_CONST_ADDR("Bit shift by negative number"));
2139
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op1_op2));
2140
0
  return 1;
2141
0
}
2142
2143
static int zend_jit_mod_by_zero_stub(zend_jit_ctx *jit)
2144
0
{
2145
0
  ir_CALL_2(IR_VOID,
2146
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2147
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2148
0
    ir_CONST_ADDR(zend_ce_division_by_zero_error),
2149
0
    ir_CONST_ADDR("Modulo by zero"));
2150
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op1_op2));
2151
0
  return 1;
2152
0
}
2153
2154
static int zend_jit_invalid_this_stub(zend_jit_ctx *jit)
2155
0
{
2156
0
  ir_CALL_2(IR_VOID,
2157
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2158
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2159
0
    IR_NULL,
2160
0
    ir_CONST_ADDR("Using $this when not in object context"));
2161
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
2162
0
  return 1;
2163
0
}
2164
2165
static int zend_jit_undefined_function_stub(zend_jit_ctx *jit)
2166
0
{
2167
  // JIT: load EX(opline)
2168
0
  ir_ref ref = ir_LOAD_A(jit_FP(jit));
2169
0
  ir_ref arg3 = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.constant)));
2170
2171
0
  if (sizeof(void*) == 8) {
2172
0
    arg3 = ir_LOAD_A(ir_ADD_A(ref, ir_SEXT_A(arg3)));
2173
0
  } else {
2174
0
    arg3 = ir_LOAD_A(arg3);
2175
0
  }
2176
0
  arg3 = ir_ADD_OFFSET(arg3, offsetof(zend_string, val));
2177
2178
0
  ir_CALL_3(IR_VOID,
2179
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2180
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2181
0
    IR_NULL,
2182
0
    ir_CONST_ADDR("Call to undefined function %s()"),
2183
0
    arg3);
2184
2185
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2186
2187
0
  return 1;
2188
0
}
2189
2190
static int zend_jit_throw_cannot_pass_by_ref_stub(zend_jit_ctx *jit)
2191
0
{
2192
0
  ir_ref opline, ref, rx, if_eq, if_tmp;
2193
2194
  // JIT: opline = EX(opline)
2195
0
  opline = ir_LOAD_A(jit_FP(jit));
2196
2197
  // JIT: ZVAL_UNDEF(ZEND_CALL_VAR(RX, opline->result.var))
2198
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, result.var)));
2199
0
  if (sizeof(void*) == 8) {
2200
0
    ref = ir_ZEXT_A(ref);
2201
0
  }
2202
0
  rx = jit_IP(jit);
2203
0
  jit_set_Z_TYPE_INFO_ref(jit, ir_ADD_A(rx, ref), ir_CONST_U32(IS_UNDEF));
2204
2205
  // last EX(call) frame may be delayed
2206
  // JIT: if (EX(call) == RX)
2207
0
  ref = ir_LOAD_A(jit_EX(call));
2208
0
  if_eq = ir_IF(ir_EQ(rx, ref));
2209
0
  ir_IF_FALSE(if_eq);
2210
2211
  // JIT: RX->prev_execute_data == EX(call)
2212
0
  ir_STORE(jit_CALL(rx, prev_execute_data), ref);
2213
2214
  // JIT: EX(call) = RX
2215
0
  ir_STORE(jit_EX(call), rx);
2216
0
  ir_MERGE_WITH_EMPTY_TRUE(if_eq);
2217
2218
  // JIT: IP = opline
2219
0
  jit_STORE_IP(jit, opline);
2220
2221
  // JIT: zend_cannot_pass_by_reference(opline->op2.num)
2222
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_cannot_pass_by_reference),
2223
0
    ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, op2.num))));
2224
2225
  // JIT: if (IP->op1_type == IS_TMP_VAR)
2226
0
  ref = ir_LOAD_U8(ir_ADD_OFFSET(jit_IP(jit), offsetof(zend_op, op1_type)));
2227
0
  if_tmp = ir_IF(ir_EQ(ref, ir_CONST_U8(IS_TMP_VAR)));
2228
0
  ir_IF_TRUE(if_tmp);
2229
2230
  // JIT: zval_ptr_dtor(EX_VAR(IP->op1.var))
2231
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_IP(jit), offsetof(zend_op, op1.var)));
2232
0
  if (sizeof(void*) == 8) {
2233
0
    ref = ir_ZEXT_A(ref);
2234
0
  }
2235
0
  ref = ir_ADD_A(jit_FP(jit), ref);
2236
0
  jit_ZVAL_PTR_DTOR(jit,
2237
0
    ZEND_ADDR_REF_ZVAL(ref),
2238
0
    MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF, false, NULL);
2239
0
  ir_MERGE_WITH_EMPTY_FALSE(if_tmp);
2240
2241
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2242
2243
0
  return 1;
2244
0
}
2245
2246
static int zend_jit_icall_throw_stub(zend_jit_ctx *jit)
2247
0
{
2248
0
  ir_ref ip, if_set;
2249
2250
  // JIT: zend_rethrow_exception(zend_execute_data *execute_data)
2251
  // JIT: if (EX(opline)->opcode != ZEND_HANDLE_EXCEPTION) {
2252
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2253
0
  ip = jit_IP(jit);
2254
0
  if_set = ir_IF(ir_EQ(ir_LOAD_U8(ir_ADD_OFFSET(ip, offsetof(zend_op, opcode))),
2255
0
    ir_CONST_U8(ZEND_HANDLE_EXCEPTION)));
2256
0
  ir_IF_FALSE(if_set);
2257
2258
  // JIT: EG(opline_before_exception) = opline;
2259
0
  ir_STORE(jit_EG(opline_before_exception), ip);
2260
0
  ir_MERGE_WITH_EMPTY_TRUE(if_set);
2261
2262
  // JIT: opline = EG(exception_op);
2263
0
  jit_STORE_IP(jit, jit_EG(exception_op));
2264
2265
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2266
2267
0
  ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2268
2269
0
  return 1;
2270
0
}
2271
2272
static int zend_jit_leave_throw_stub(zend_jit_ctx *jit)
2273
0
{
2274
0
  ir_ref ip, if_set;
2275
2276
  // JIT: if (opline->opcode != ZEND_HANDLE_EXCEPTION) {
2277
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
2278
0
  ip = jit_IP(jit);
2279
0
  if_set = ir_IF(ir_EQ(ir_LOAD_U8(ir_ADD_OFFSET(ip, offsetof(zend_op, opcode))),
2280
0
    ir_CONST_U8(ZEND_HANDLE_EXCEPTION)));
2281
0
  ir_IF_FALSE(if_set);
2282
2283
  // JIT: EG(opline_before_exception) = opline;
2284
0
  ir_STORE(jit_EG(opline_before_exception), ip);
2285
0
  ir_MERGE_WITH_EMPTY_TRUE(if_set);
2286
2287
  // JIT: opline = EG(exception_op);
2288
0
  jit_STORE_IP(jit, jit_EG(exception_op));
2289
2290
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2291
0
    ir_STORE(jit_EX(opline), jit_IP(jit));
2292
2293
    // JIT: HANDLE_EXCEPTION()
2294
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
2295
0
  } else {
2296
0
    zend_jit_vm_leave(jit, jit_IP(jit));
2297
0
  }
2298
2299
0
  return 1;
2300
0
}
2301
2302
static int zend_jit_hybrid_runtime_jit_stub(zend_jit_ctx *jit)
2303
0
{
2304
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) {
2305
0
    return 0;
2306
0
  }
2307
2308
0
  ir_CALL(IR_VOID, ir_CONST_OPCODE_HANDLER_FUNC(zend_runtime_jit));
2309
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2310
0
  return 1;
2311
0
}
2312
2313
static int zend_jit_hybrid_profile_jit_stub(zend_jit_ctx *jit)
2314
0
{
2315
0
  ir_ref addr, func, run_time_cache, jit_extension;
2316
2317
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) {
2318
0
    return 0;
2319
0
  }
2320
2321
0
  addr = ir_CONST_ADDR(&zend_jit_profile_counter),
2322
0
  ir_STORE(addr, ir_ADD_L(ir_LOAD_L(addr), ir_CONST_LONG(1)));
2323
2324
0
  func = ir_LOAD_A(jit_EX(func));
2325
0
  run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
2326
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2327
2328
0
  if (zend_jit_profile_counter_rid) {
2329
0
    addr = ir_ADD_OFFSET(run_time_cache, zend_jit_profile_counter_rid * sizeof(void*));
2330
0
  } else {
2331
0
    addr = run_time_cache;
2332
0
  }
2333
0
  ir_STORE(addr, ir_ADD_L(ir_LOAD_L(addr), ir_CONST_LONG(1)));
2334
2335
0
  addr = ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_extension, orig_handler));
2336
0
  ir_IJMP(ir_LOAD_A(addr));
2337
2338
0
  return 1;
2339
0
}
2340
2341
static int _zend_jit_hybrid_hot_counter_stub(zend_jit_ctx *jit, uint32_t cost)
2342
0
{
2343
0
  ir_ref func, jit_extension, addr, ref, if_overflow;
2344
0
2345
0
  func = ir_LOAD_A(jit_EX(func));
2346
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2347
0
  addr = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_hot_extension, counter)));
2348
0
  ref = ir_SUB_I16(ir_LOAD_I16(addr), ir_CONST_I16(cost));
2349
0
  ir_STORE(addr, ref);
2350
0
  if_overflow = ir_IF(ir_LE(ref, ir_CONST_I16(0)));
2351
0
2352
0
  ir_IF_TRUE_cold(if_overflow);
2353
0
  ir_STORE(addr, ir_CONST_I16(ZEND_JIT_COUNTER_INIT));
2354
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_hot_func),
2355
0
    jit_FP(jit),
2356
0
    jit_IP(jit));
2357
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2358
0
2359
0
  ir_IF_FALSE(if_overflow);
2360
0
  ref = ir_SUB_A(jit_IP(jit),
2361
0
    ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, opcodes))));
2362
0
  ref = ir_DIV_A(ref, ir_CONST_ADDR(sizeof(zend_op) / sizeof(void*)));
2363
0
2364
0
  addr = ir_ADD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_hot_extension, orig_handlers)),
2365
0
    ref);
2366
0
  ir_IJMP(ir_LOAD_A(addr));
2367
0
2368
0
  return 1;
2369
0
}
2370
2371
static int zend_jit_hybrid_func_hot_counter_stub(zend_jit_ctx *jit)
2372
0
{
2373
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_func)) {
2374
0
    return 0;
2375
0
  }
2376
2377
0
  return _zend_jit_hybrid_hot_counter_stub(jit,
2378
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
2379
0
}
2380
2381
static int zend_jit_hybrid_loop_hot_counter_stub(zend_jit_ctx *jit)
2382
0
{
2383
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_loop)) {
2384
0
    return 0;
2385
0
  }
2386
2387
0
  return _zend_jit_hybrid_hot_counter_stub(jit,
2388
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
2389
0
}
2390
2391
static ir_ref _zend_jit_orig_opline_handler(zend_jit_ctx *jit, ir_ref offset)
2392
0
{
2393
0
  ir_ref addr = ir_ADD_A(offset, jit_IP(jit));
2394
2395
0
  return ir_LOAD_A(addr);
2396
0
}
2397
2398
static ir_ref zend_jit_orig_opline_handler(zend_jit_ctx *jit)
2399
0
{
2400
0
  ir_ref func, jit_extension, offset;
2401
2402
0
  func = ir_LOAD_A(jit_EX(func));
2403
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2404
0
  offset = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_trace_extension, offset)));
2405
0
  return _zend_jit_orig_opline_handler(jit, offset);
2406
0
}
2407
2408
static int _zend_jit_hybrid_trace_counter_stub(zend_jit_ctx *jit, uint32_t cost)
2409
0
{
2410
0
  ir_ref func, jit_extension, offset, addr, ref, if_overflow, ret, if_halt;
2411
0
2412
0
  func = ir_LOAD_A(jit_EX(func));
2413
0
  jit_extension = ir_LOAD_A(ir_ADD_OFFSET(func, offsetof(zend_op_array, reserved[zend_func_info_rid])));
2414
0
  offset = ir_LOAD_A(ir_ADD_OFFSET(jit_extension, offsetof(zend_jit_op_array_trace_extension, offset)));
2415
0
  addr = ir_LOAD_A(ir_ADD_OFFSET(ir_ADD_A(offset, jit_IP(jit)), offsetof(zend_op_trace_info, counter)));
2416
0
  ref = ir_SUB_I16(ir_LOAD_I16(addr), ir_CONST_I16(cost));
2417
0
  ir_STORE(addr, ref);
2418
0
  if_overflow = ir_IF(ir_LE(ref, ir_CONST_I16(0)));
2419
0
2420
0
  ir_IF_TRUE_cold(if_overflow);
2421
0
  ir_STORE(addr, ir_CONST_I16(ZEND_JIT_COUNTER_INIT));
2422
0
  ret = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_trace_hot_root),
2423
0
    jit_FP(jit),
2424
0
    jit_IP(jit));
2425
0
  if_halt = ir_IF(ir_LT(ret, ir_CONST_I32(0)));
2426
0
  ir_IF_FALSE(if_halt);
2427
0
2428
0
  ref = jit_EG(current_execute_data);
2429
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2430
0
  ref = ir_LOAD_A(jit_EX(opline));
2431
0
  jit_STORE_IP(jit, ref);
2432
0
  ir_IJMP(ir_LOAD_A(jit_IP(jit)));
2433
0
2434
0
  ir_IF_FALSE(if_overflow);
2435
0
  ir_IJMP(_zend_jit_orig_opline_handler(jit, offset));
2436
0
2437
0
  ir_IF_TRUE(if_halt);
2438
0
  ir_IJMP(ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_halt_op->handler));
2439
0
2440
0
  return 1;
2441
0
}
2442
2443
static int zend_jit_hybrid_func_trace_counter_stub(zend_jit_ctx *jit)
2444
0
{
2445
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_func)) {
2446
0
    return 0;
2447
0
  }
2448
2449
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2450
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_func) - 1) / JIT_G(hot_func)));
2451
0
}
2452
2453
static int zend_jit_hybrid_ret_trace_counter_stub(zend_jit_ctx *jit)
2454
0
{
2455
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_return)) {
2456
0
    return 0;
2457
0
  }
2458
2459
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2460
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_return) - 1) / JIT_G(hot_return)));
2461
0
}
2462
2463
static int zend_jit_hybrid_loop_trace_counter_stub(zend_jit_ctx *jit)
2464
0
{
2465
0
  if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID || !JIT_G(hot_loop)) {
2466
0
    return 0;
2467
0
  }
2468
2469
0
  return _zend_jit_hybrid_trace_counter_stub(jit,
2470
0
    ((ZEND_JIT_COUNTER_INIT + JIT_G(hot_loop) - 1) / JIT_G(hot_loop)));
2471
0
}
2472
2473
static int zend_jit_trace_halt_stub(zend_jit_ctx *jit)
2474
0
{
2475
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
2476
0
    ir_TAILCALL(IR_VOID, ir_CONST_OPCODE_HANDLER_FUNC(zend_jit_halt_op->handler));
2477
0
  } else if (GCC_GLOBAL_REGS) {
2478
0
    jit_STORE_IP(jit, IR_NULL);
2479
0
    ir_RETURN(IR_VOID);
2480
0
  } else {
2481
0
    ir_RETURN(ir_CONST_ADDR(ZEND_VM_ENTER_BIT)); // ZEND_VM_RETURN
2482
0
  }
2483
0
  return 1;
2484
0
}
2485
2486
static int zend_jit_trace_escape_stub(zend_jit_ctx *jit)
2487
0
{
2488
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2489
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2490
0
  } else {
2491
0
    zend_jit_vm_enter(jit, jit_IP(jit));
2492
0
  }
2493
2494
0
  return 1;
2495
0
}
2496
2497
static int zend_jit_trace_exit_stub(zend_jit_ctx *jit)
2498
0
{
2499
0
  ir_ref ref, ret, if_zero, addr;
2500
2501
  // EX(opline) = opline
2502
0
  ir_STORE(jit_EX(opline), jit_IP(jit));
2503
2504
0
  ret = ir_EXITCALL(ir_CONST_FC_FUNC(zend_jit_trace_exit));
2505
2506
0
  if_zero = ir_IF(ir_EQ(ret, ir_CONST_I32(0)));
2507
2508
0
  ir_IF_TRUE(if_zero);
2509
2510
0
  ref = jit_EG(current_execute_data);
2511
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2512
0
  ref = ir_LOAD_A(jit_EX(opline));
2513
0
  jit_STORE_IP(jit, ref);
2514
2515
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2516
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
2517
0
  } else {
2518
0
    zend_jit_vm_enter(jit, ref);
2519
0
  }
2520
2521
0
  ir_IF_FALSE(if_zero);
2522
2523
0
  ir_GUARD(ir_GE(ret, ir_CONST_I32(0)), jit_STUB_ADDR(jit, jit_stub_trace_halt));
2524
2525
0
  ref = jit_EG(current_execute_data);
2526
0
  jit_STORE_FP(jit, ir_LOAD_A(ref));
2527
2528
0
  ref = ir_LOAD_A(jit_EX(opline));
2529
0
  jit_STORE_IP(jit, ref);
2530
2531
  // check for interrupt (try to avoid this ???)
2532
0
  zend_jit_check_timeout(jit, NULL, NULL);
2533
2534
0
  addr = zend_jit_orig_opline_handler(jit);
2535
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2536
0
    zend_jit_tailcall_handler(jit, addr);
2537
0
  } else {
2538
#if defined(IR_TARGET_X86)
2539
    addr = ir_CAST_OPCODE_HANDLER_FUNC(addr);
2540
#endif
2541
0
    ref = ir_CALL_2(IR_ADDR, addr, jit_FP(jit), jit_IP(jit));
2542
0
    zend_jit_vm_enter(jit, ref);
2543
0
  }
2544
2545
0
  return 1;
2546
0
}
2547
2548
static int zend_jit_undefined_offset_stub(zend_jit_ctx *jit)
2549
0
{
2550
0
  if (GCC_GLOBAL_REGS) {
2551
0
    ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key));
2552
0
  } else {
2553
0
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key), jit_FP(jit));
2554
0
  }
2555
2556
0
  return 1;
2557
0
}
2558
2559
static int zend_jit_undefined_key_stub(zend_jit_ctx *jit)
2560
0
{
2561
0
  if (GCC_GLOBAL_REGS) {
2562
0
    ir_TAILCALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_string_key));
2563
0
  } else {
2564
0
    ir_TAILCALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_string_key), jit_FP(jit));
2565
0
  }
2566
2567
0
  return 1;
2568
0
}
2569
2570
static int zend_jit_cannot_add_element_stub(zend_jit_ctx *jit)
2571
0
{
2572
0
  ir_ref opline = ir_LOAD_A(jit_EX(opline));
2573
0
  ir_ref ref, if_result_used;
2574
2575
0
  if_result_used = ir_IF(ir_AND_U8(
2576
0
    ir_LOAD_U8(ir_ADD_OFFSET(opline, offsetof(zend_op, result_type))),
2577
0
    ir_CONST_U8(IS_TMP_VAR|IS_VAR)));
2578
0
  ir_IF_TRUE(if_result_used);
2579
2580
0
  ref = ir_LOAD_U32(ir_ADD_OFFSET(opline, offsetof(zend_op, result.var)));
2581
0
  if (sizeof(void*) == 8) {
2582
0
    ref = ir_ZEXT_A(ref);
2583
0
  }
2584
0
  jit_set_Z_TYPE_INFO_ref(jit, ir_ADD_A(jit_FP(jit), ref), ir_CONST_U32(IS_UNDEF));
2585
0
  ir_MERGE_WITH_EMPTY_FALSE(if_result_used);
2586
2587
0
  ir_CALL_2(IR_VOID,
2588
0
    ir_CONST_FUNC_PROTO(zend_throw_error,
2589
0
      ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)),
2590
0
    IR_NULL,
2591
0
    ir_CONST_ADDR("Cannot add element to the array as the next element is already occupied"));
2592
0
  ir_RETURN(IR_VOID);
2593
2594
0
  return 1;
2595
0
}
2596
2597
static int zend_jit_assign_const_stub(zend_jit_ctx *jit)
2598
0
{
2599
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2600
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2601
2602
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2603
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2604
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN;
2605
2606
0
  if (!zend_jit_assign_to_variable(
2607
0
      jit, NULL,
2608
0
      var_addr, var_addr, -1, -1,
2609
0
      IS_CONST, val_addr, val_info,
2610
0
      0, 0, false)) {
2611
0
    return 0;
2612
0
  }
2613
0
  ir_RETURN(IR_VOID);
2614
0
  return 1;
2615
0
}
2616
2617
static int zend_jit_assign_tmp_stub(zend_jit_ctx *jit)
2618
0
{
2619
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2620
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2621
2622
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2623
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2624
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN;
2625
2626
0
  if (!zend_jit_assign_to_variable(
2627
0
      jit, NULL,
2628
0
      var_addr, var_addr, -1, -1,
2629
0
      IS_TMP_VAR, val_addr, val_info,
2630
0
      0, 0, false)) {
2631
0
    return 0;
2632
0
  }
2633
0
  ir_RETURN(IR_VOID);
2634
0
  return 1;
2635
0
}
2636
2637
static int zend_jit_assign_var_stub(zend_jit_ctx *jit)
2638
0
{
2639
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2640
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2641
2642
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2643
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2644
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF;
2645
2646
0
  if (!zend_jit_assign_to_variable(
2647
0
      jit, NULL,
2648
0
      var_addr, var_addr, -1, -1,
2649
0
      IS_VAR, val_addr, val_info,
2650
0
      0, 0, false)) {
2651
0
    return 0;
2652
0
  }
2653
0
  ir_RETURN(IR_VOID);
2654
0
  return 1;
2655
0
}
2656
2657
static int zend_jit_assign_cv_noref_stub(zend_jit_ctx *jit)
2658
0
{
2659
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2660
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2661
2662
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2663
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2664
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN/*|MAY_BE_UNDEF*/;
2665
2666
0
  if (!zend_jit_assign_to_variable(
2667
0
      jit, NULL,
2668
0
      var_addr, var_addr, -1, -1,
2669
0
      IS_CV, val_addr, val_info,
2670
0
      0, 0, false)) {
2671
0
    return 0;
2672
0
  }
2673
0
  ir_RETURN(IR_VOID);
2674
0
  return 1;
2675
0
}
2676
2677
static int zend_jit_new_array_stub(zend_jit_ctx *jit)
2678
0
{
2679
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2680
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2681
0
  ir_ref ref = ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(_zend_new_array_0));
2682
2683
0
  jit_set_Z_PTR(jit, var_addr, ref);
2684
0
  jit_set_Z_TYPE_INFO(jit, var_addr, IS_ARRAY_EX);
2685
0
  ir_RETURN(ref);
2686
0
  return 1;
2687
0
}
2688
2689
static int zend_jit_assign_cv_stub(zend_jit_ctx *jit)
2690
0
{
2691
0
  ir_ref var = ir_PARAM(IR_ADDR, "var", 1);
2692
0
  ir_ref val = ir_PARAM(IR_ADDR, "val", 2);
2693
2694
0
  zend_jit_addr var_addr = ZEND_ADDR_REF_ZVAL(var);
2695
0
  zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val);
2696
0
  uint32_t val_info = MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF/*|MAY_BE_UNDEF*/;
2697
2698
0
  if (!zend_jit_assign_to_variable(
2699
0
      jit, NULL,
2700
0
      var_addr, var_addr, -1, -1,
2701
0
      IS_CV, val_addr, val_info,
2702
0
      0, 0, false)) {
2703
0
    return 0;
2704
0
  }
2705
0
  ir_RETURN(IR_VOID);
2706
0
  return 1;
2707
0
}
2708
2709
static void zend_jit_init_ctx(zend_jit_ctx *jit, uint32_t flags)
2710
0
{
2711
#if defined (__CET__) && (__CET__ & 1) != 0
2712
  flags |= IR_GEN_ENDBR;
2713
#endif
2714
0
  flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN;
2715
2716
0
  ir_init(&jit->ctx, flags, 256, 1024);
2717
0
  jit->ctx.ret_type = -1;
2718
2719
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
2720
0
  jit->ctx.mflags |= default_mflags;
2721
0
  if (JIT_G(opt_flags) & allowed_opt_flags & ZEND_JIT_CPU_AVX) {
2722
0
    jit->ctx.mflags |= IR_X86_AVX;
2723
0
  }
2724
#elif defined(IR_TARGET_AARCH64)
2725
  jit->ctx.get_veneer = zend_jit_get_veneer;
2726
  jit->ctx.set_veneer = zend_jit_set_veneer;
2727
#endif
2728
2729
0
  jit->ctx.fixed_regset = (1<<ZREG_FP) | (1<<ZREG_IP);
2730
0
  if (!(flags & IR_FUNCTION)) {
2731
0
    jit->ctx.flags |= IR_NO_STACK_COMBINE;
2732
0
    if (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2733
0
      jit->ctx.flags |= IR_FUNCTION;
2734
      /* Stack must be 16 byte aligned */
2735
      /* TODO: select stack size ??? */
2736
0
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
2737
# if defined(IR_TARGET_AARCH64)
2738
      /* Must save LR */
2739
      jit->ctx.flags |= IR_USE_FRAME_POINTER;
2740
      /* Same as HYBRID VM */
2741
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 4; /* 4 spill slots */
2742
# else
2743
      /* Same as HYBRID VM, plus 1 slot for re-alignment (caller pushes return address, frame is not aligned on entry) */
2744
0
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 5; /* 5 spill slots (8 bytes) or 10 spill slots (4 bytes) */
2745
0
# endif
2746
#elif defined(IR_TARGET_AARCH64)
2747
      jit->ctx.flags |= IR_USE_FRAME_POINTER;
2748
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 16; /* 10 saved registers and 6 spill slots (8 bytes) */
2749
#elif defined(_WIN64)
2750
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 11; /* 8 saved registers and 3 spill slots (8 bytes) */
2751
#elif defined(IR_TARGET_X86_64)
2752
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 9;  /* 6 saved registers and 3 spill slots (8 bytes) */
2753
#else /* IR_TARGET_x86 */
2754
      jit->ctx.fixed_stack_frame_size = sizeof(void*) * 11; /* 4 saved registers and 7 spill slots (4 bytes) */
2755
#endif
2756
      /* JIT-ed code is called only from execute_ex, which takes care
2757
       * of saving ZREG_FP, ZREG_IP when GCC_GLOBAL_REGS is 1, so we don't
2758
       * have to save them.
2759
       */
2760
0
      if (GCC_GLOBAL_REGS) {
2761
0
        jit->ctx.fixed_save_regset = IR_REGSET_PRESERVED & ~((1<<ZREG_FP) | (1<<ZREG_IP));
2762
0
      } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
2763
        /* The only preserved register on x86 is RBP:
2764
         * https://github.com/llvm/llvm-project/blob/a414877a7a5f000d01370acb1162eb1dea87f48c/llvm/lib/Target/X86/X86RegisterInfo.cpp#L319
2765
         * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/X86/X86CallingConv.td#L1183
2766
         * On AArch64 it's LR, FP:
2767
         * https://github.com/llvm/llvm-project/blob/68bfe91b5a34f80dbcc4f0a7fa5d7aa1cdf959c2/llvm/lib/Target/AArch64/AArch64CallingConvention.td#L681
2768
         *
2769
         * Add them to the fixed_regset to prevent usage or these regs.
2770
         * It's cheaper to not use them than to save them.
2771
         */
2772
0
#if defined(IR_TARGET_X64)
2773
0
        jit->ctx.fixed_regset |= (1<<IR_REG_FP);
2774
#elif defined(IR_TARGET_AARCH64)
2775
        jit->ctx.fixed_regset |= (1<<IR_REG_FP) | (1<<IR_REG_LR);
2776
#else
2777
        ZEND_UNREACHABLE();
2778
#endif
2779
0
      } else {
2780
0
        jit->ctx.fixed_save_regset = IR_REGSET_PRESERVED;
2781
//#ifdef _WIN64
2782
//        jit->ctx.fixed_save_regset &= 0xffff; // TODO: don't save FP registers ???
2783
//#endif
2784
0
      }
2785
#ifdef _WIN64
2786
      jit->ctx.fixed_call_stack_size = 16 + IR_SHADOW_ARGS;
2787
#else
2788
0
      jit->ctx.fixed_call_stack_size = 16;
2789
0
#endif
2790
0
    } else {
2791
#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE
2792
      jit->ctx.fixed_stack_red_zone = ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE;
2793
      if (jit->ctx.fixed_stack_red_zone > 16) {
2794
        jit->ctx.fixed_stack_frame_size = jit->ctx.fixed_stack_red_zone - 16;
2795
        jit->ctx.fixed_call_stack_size = 16;
2796
      }
2797
      jit->ctx.flags |= IR_MERGE_EMPTY_ENTRIES;
2798
#else
2799
0
      jit->ctx.fixed_stack_red_zone = 0;
2800
0
      jit->ctx.fixed_stack_frame_size = 32; /* 4 spill slots (8 bytes) or 8 spill slots (4 bytes) */
2801
0
      jit->ctx.fixed_call_stack_size = 16;
2802
0
#endif
2803
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
2804
0
      jit->ctx.fixed_regset |= (1<<IR_REG_FP); /* prevent %rbp (%r5) usage */
2805
0
#endif
2806
0
    }
2807
0
  }
2808
2809
0
  jit->ctx.snapshot_create = (ir_snapshot_create_t)jit_SNAPSHOT;
2810
2811
0
  jit->op_array = NULL;
2812
0
  jit->current_op_array = NULL;
2813
0
  jit->ssa = NULL;
2814
0
  jit->name = NULL;
2815
0
  jit->last_valid_opline = NULL;
2816
0
  jit->use_last_valid_opline = false;
2817
0
  jit->track_last_valid_opline = false;
2818
0
  jit->reuse_ip = false;
2819
0
  jit->delayed_call_level = 0;
2820
0
  delayed_call_chain = false;
2821
0
  jit->b = -1;
2822
#ifdef ZTS
2823
  jit->tls = IR_UNUSED;
2824
#endif
2825
0
  jit->fp = IR_UNUSED;
2826
0
  jit->poly_func_ref = IR_UNUSED;
2827
0
  jit->poly_this_ref = IR_UNUSED;
2828
0
  jit->trace_loop_ref = IR_UNUSED;
2829
0
  jit->return_inputs = IR_UNUSED;
2830
0
  jit->bb_start_ref = NULL;
2831
0
  jit->bb_predecessors = NULL;
2832
0
  jit->bb_edges = NULL;
2833
0
  jit->trace = NULL;
2834
0
  jit->ra = NULL;
2835
0
  jit->delay_var = -1;
2836
0
  jit->delay_refs = NULL;
2837
0
  jit->eg_exception_addr = 0;
2838
0
  zend_hash_init(&jit->addr_hash, 64, NULL, NULL, 0);
2839
0
  memset(jit->stub_addr, 0, sizeof(jit->stub_addr));
2840
2841
0
  ir_START();
2842
0
}
2843
2844
static int zend_jit_free_ctx(zend_jit_ctx *jit)
2845
0
{
2846
0
  if (jit->name) {
2847
0
    zend_string_release(jit->name);
2848
0
  }
2849
0
  zend_hash_destroy(&jit->addr_hash);
2850
0
  ir_free(&jit->ctx);
2851
0
  return 1;
2852
0
}
2853
2854
static void *zend_jit_ir_compile(ir_ctx *ctx, size_t *size, const char *name)
2855
0
{
2856
0
  void *entry;
2857
0
  ir_code_buffer code_buffer;
2858
2859
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_SRC) {
2860
0
    if (name) fprintf(stderr, "%s: ; after folding\n", name);
2861
0
    ir_save(ctx, 0, stderr);
2862
0
  }
2863
2864
0
#if ZEND_DEBUG
2865
0
  ir_check(ctx);
2866
0
#endif
2867
2868
0
  ir_build_def_use_lists(ctx);
2869
2870
0
#if ZEND_DEBUG
2871
0
  ir_check(ctx);
2872
0
#endif
2873
2874
0
#if 1
2875
0
  ir_sccp(ctx);
2876
0
#endif
2877
2878
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_SCCP) {
2879
0
    if (name) fprintf(stderr, "%s: ; after SCCP\n", name);
2880
0
    ir_save(ctx, 0, stderr);
2881
0
  }
2882
2883
0
  ir_build_cfg(ctx);
2884
0
  ir_build_dominators_tree(ctx);
2885
0
  ir_find_loops(ctx);
2886
2887
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_CFG) {
2888
0
    if (name) fprintf(stderr, "%s: ; after CFG\n", name);
2889
0
    ir_save(ctx, IR_SAVE_CFG, stderr);
2890
0
  }
2891
2892
0
  ir_gcm(ctx);
2893
2894
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_GCM) {
2895
0
    if (name) fprintf(stderr, "%s: ; after GCM\n", name);
2896
0
    ir_save(ctx, IR_SAVE_CFG|IR_SAVE_CFG_MAP, stderr);
2897
0
  }
2898
2899
0
  ir_schedule(ctx);
2900
2901
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE) {
2902
0
    if (name) fprintf(stderr, "%s: ; after schedule\n", name);
2903
0
    ir_save(ctx, IR_SAVE_CFG, stderr);
2904
0
  }
2905
2906
0
  ir_match(ctx);
2907
0
#if !defined(IR_TARGET_AARCH64)
2908
0
  ctx->flags &= ~IR_USE_FRAME_POINTER; /* don't use FRAME_POINTER even with ALLOCA, TODO: cleanup this ??? */
2909
0
#endif
2910
0
  ir_assign_virtual_registers(ctx);
2911
0
  ir_compute_live_ranges(ctx);
2912
0
  ir_coalesce(ctx);
2913
0
  ir_reg_alloc(ctx);
2914
2915
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_AFTER_REGS) {
2916
0
    if (name) fprintf(stderr, "%s: ; after register allocation\n", name);
2917
0
    ir_save(ctx, IR_SAVE_CFG|IR_SAVE_RULES|IR_SAVE_REGS, stderr);
2918
0
    ir_dump_live_ranges(ctx, stderr);
2919
0
  }
2920
2921
0
  ir_schedule_blocks(ctx);
2922
2923
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_IR_FINAL|ZEND_JIT_DEBUG_IR_CODEGEN)) {
2924
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_IR_CODEGEN) {
2925
0
      if (name) fprintf(stderr, "%s: ; codegen\n", name);
2926
0
      ir_dump_codegen(ctx, stderr);
2927
0
    } else {
2928
0
      if (name) fprintf(stderr, "%s: ; final\n", name);
2929
0
      ir_save(ctx, IR_SAVE_CFG|IR_SAVE_RULES|IR_SAVE_REGS, stderr);
2930
0
    }
2931
0
  }
2932
2933
0
#if ZEND_DEBUG
2934
0
  ir_check(ctx);
2935
0
#endif
2936
2937
0
  code_buffer.start = dasm_buf;
2938
0
  code_buffer.end = dasm_end;
2939
0
  code_buffer.pos = *dasm_ptr;
2940
0
  ctx->code_buffer = &code_buffer;
2941
2942
0
  entry = ir_emit_code(ctx, size);
2943
2944
0
  *dasm_ptr = code_buffer.pos;
2945
2946
#if defined(IR_TARGET_AARCH64)
2947
  if (ctx->flags2 & IR_HAS_VENEERS) {
2948
    zend_jit_commit_veneers();
2949
  }
2950
#endif
2951
2952
0
  return entry;
2953
0
}
2954
2955
static void zend_jit_setup_stubs(void)
2956
0
{
2957
0
  zend_jit_ctx jit;
2958
0
  void *entry;
2959
0
  size_t size;
2960
0
  uint32_t i;
2961
2962
0
  for (i = 0; i < sizeof(zend_jit_stubs)/sizeof(zend_jit_stubs[0]); i++) {
2963
0
    zend_jit_init_ctx(&jit, zend_jit_stubs[i].flags);
2964
2965
0
    if (!zend_jit_stubs[i].stub(&jit)) {
2966
0
      zend_jit_free_ctx(&jit);
2967
0
      zend_jit_stub_handlers[i] = NULL;
2968
0
      continue;
2969
0
    }
2970
2971
0
    entry = zend_jit_ir_compile(&jit.ctx, &size, zend_jit_stubs[i].name);
2972
0
    if (!entry) {
2973
0
      zend_jit_free_ctx(&jit);
2974
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: could not compile stub");
2975
0
    }
2976
2977
0
    zend_jit_stub_handlers[i] = entry;
2978
2979
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)) {
2980
#ifdef HAVE_CAPSTONE
2981
      if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
2982
        ir_disasm_add_symbol(zend_jit_stubs[i].name, (uintptr_t)entry, size);
2983
      }
2984
      if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_STUBS) {
2985
        ir_disasm(zend_jit_stubs[i].name,
2986
          entry, size, (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_ADDR) != 0, &jit.ctx, stderr);
2987
      }
2988
#endif
2989
0
#ifndef _WIN32
2990
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
2991
//        ir_mem_unprotect(entry, size);
2992
0
        ir_gdb_register(zend_jit_stubs[i].name, entry, size, 0, 0);
2993
//        ir_mem_protect(entry, size);
2994
0
      }
2995
2996
0
      if (JIT_G(debug) & (ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
2997
0
        ir_perf_map_register(zend_jit_stubs[i].name, entry, size);
2998
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
2999
0
          ir_perf_jitdump_register(zend_jit_stubs[i].name, entry, size);
3000
0
        }
3001
0
      }
3002
0
#endif
3003
0
    }
3004
0
    zend_jit_free_ctx(&jit);
3005
0
  }
3006
0
}
3007
3008
#define REGISTER_HELPER(n)  \
3009
  ir_disasm_add_symbol(#n, (uint64_t)(uintptr_t)n, sizeof(void*));
3010
#define REGISTER_DATA(n)  \
3011
  ir_disasm_add_symbol(#n, (uint64_t)(uintptr_t)&n, sizeof(n));
3012
3013
static void zend_jit_setup_disasm(void)
3014
0
{
3015
#ifdef HAVE_CAPSTONE
3016
  ir_disasm_init();
3017
3018
  if (zend_vm_kind() == ZEND_VM_KIND_HYBRID) {
3019
    zend_op opline;
3020
3021
    memset(&opline, 0, sizeof(opline));
3022
3023
    opline.opcode = ZEND_DO_UCALL;
3024
    opline.result_type = IS_UNUSED;
3025
    zend_vm_set_opcode_handler(&opline);
3026
    ir_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3027
3028
    opline.opcode = ZEND_DO_UCALL;
3029
    opline.result_type = IS_VAR;
3030
    zend_vm_set_opcode_handler(&opline);
3031
    ir_disasm_add_symbol("ZEND_DO_UCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3032
3033
    opline.opcode = ZEND_DO_FCALL_BY_NAME;
3034
    opline.result_type = IS_UNUSED;
3035
    zend_vm_set_opcode_handler(&opline);
3036
    ir_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3037
3038
    opline.opcode = ZEND_DO_FCALL_BY_NAME;
3039
    opline.result_type = IS_VAR;
3040
    zend_vm_set_opcode_handler(&opline);
3041
    ir_disasm_add_symbol("ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3042
3043
    opline.opcode = ZEND_DO_FCALL;
3044
    opline.result_type = IS_UNUSED;
3045
    zend_vm_set_opcode_handler(&opline);
3046
    ir_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3047
3048
    opline.opcode = ZEND_DO_FCALL;
3049
    opline.result_type = IS_VAR;
3050
    zend_vm_set_opcode_handler(&opline);
3051
    ir_disasm_add_symbol("ZEND_DO_FCALL_SPEC_RETVAL_USED_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3052
3053
    opline.opcode = ZEND_RETURN;
3054
    opline.op1_type = IS_CONST;
3055
    zend_vm_set_opcode_handler(&opline);
3056
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_CONST_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3057
3058
    opline.opcode = ZEND_RETURN;
3059
    opline.op1_type = IS_TMP_VAR;
3060
    zend_vm_set_opcode_handler(&opline);
3061
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_TMP_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3062
3063
    opline.opcode = ZEND_RETURN;
3064
    opline.op1_type = IS_VAR;
3065
    zend_vm_set_opcode_handler(&opline);
3066
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_VAR_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3067
3068
    opline.opcode = ZEND_RETURN;
3069
    opline.op1_type = IS_CV;
3070
    zend_vm_set_opcode_handler(&opline);
3071
    ir_disasm_add_symbol("ZEND_RETURN_SPEC_CV_LABEL", (uint64_t)(uintptr_t)opline.handler, sizeof(void*));
3072
3073
    ir_disasm_add_symbol("ZEND_HYBRID_HALT_LABEL", (uint64_t)(uintptr_t)zend_jit_halt_op->handler, sizeof(void*));
3074
  }
3075
3076
  REGISTER_DATA(zend_jit_profile_counter);
3077
3078
  REGISTER_HELPER(zend_runtime_jit);
3079
  REGISTER_HELPER(zend_jit_hot_func);
3080
  REGISTER_HELPER(zend_jit_trace_hot_root);
3081
  REGISTER_HELPER(zend_jit_trace_exit);
3082
3083
  REGISTER_HELPER(zend_jit_array_free);
3084
  REGISTER_HELPER(zend_jit_undefined_op_helper);
3085
  REGISTER_HELPER(zend_jit_pre_inc_typed_ref);
3086
  REGISTER_HELPER(zend_jit_pre_dec_typed_ref);
3087
  REGISTER_HELPER(zend_jit_post_inc_typed_ref);
3088
  REGISTER_HELPER(zend_jit_post_dec_typed_ref);
3089
  REGISTER_HELPER(zend_jit_pre_inc);
3090
  REGISTER_HELPER(zend_jit_pre_dec);
3091
  REGISTER_HELPER(zend_jit_add_arrays_helper);
3092
  REGISTER_HELPER(zend_jit_fast_assign_concat_helper);
3093
  REGISTER_HELPER(zend_jit_fast_concat_helper);
3094
  REGISTER_HELPER(zend_jit_fast_concat_tmp_helper);
3095
  REGISTER_HELPER(zend_jit_assign_op_to_typed_ref_tmp);
3096
  REGISTER_HELPER(zend_jit_assign_op_to_typed_ref);
3097
  REGISTER_HELPER(zend_jit_assign_const_to_typed_ref);
3098
  REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref);
3099
  REGISTER_HELPER(zend_jit_assign_var_to_typed_ref);
3100
  REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref);
3101
  REGISTER_HELPER(zend_jit_assign_const_to_typed_ref2);
3102
  REGISTER_HELPER(zend_jit_assign_tmp_to_typed_ref2);
3103
  REGISTER_HELPER(zend_jit_assign_var_to_typed_ref2);
3104
  REGISTER_HELPER(zend_jit_assign_cv_to_typed_ref2);
3105
  REGISTER_HELPER(zend_jit_check_constant);
3106
  REGISTER_HELPER(zend_jit_get_constant);
3107
  REGISTER_HELPER(zend_jit_int_extend_stack_helper);
3108
  REGISTER_HELPER(zend_jit_extend_stack_helper);
3109
  REGISTER_HELPER(zend_jit_init_func_run_time_cache_helper);
3110
  REGISTER_HELPER(zend_jit_find_func_helper);
3111
  REGISTER_HELPER(zend_jit_find_ns_func_helper);
3112
  REGISTER_HELPER(zend_jit_jmp_frameless_helper);
3113
  REGISTER_HELPER(zend_jit_unref_helper);
3114
  REGISTER_HELPER(zend_jit_invalid_method_call);
3115
  REGISTER_HELPER(zend_jit_invalid_method_call_tmp);
3116
  REGISTER_HELPER(zend_jit_find_method_helper);
3117
  REGISTER_HELPER(zend_jit_find_method_tmp_helper);
3118
  REGISTER_HELPER(zend_jit_push_static_method_call_frame);
3119
  REGISTER_HELPER(zend_jit_push_static_method_call_frame_tmp);
3120
  REGISTER_HELPER(zend_jit_find_class_helper);
3121
  REGISTER_HELPER(zend_jit_find_static_method_helper);
3122
  REGISTER_HELPER(zend_jit_push_this_method_call_frame);
3123
  REGISTER_HELPER(zend_jit_free_trampoline_helper);
3124
  REGISTER_HELPER(zend_jit_verify_return_slow);
3125
  REGISTER_HELPER(zend_jit_deprecated_helper);
3126
  REGISTER_HELPER(zend_jit_undefined_long_key);
3127
  REGISTER_HELPER(zend_jit_undefined_long_key_ex);
3128
  REGISTER_HELPER(zend_jit_undefined_string_key);
3129
  REGISTER_HELPER(zend_jit_copy_extra_args_helper);
3130
  REGISTER_HELPER(zend_jit_copy_extra_args_helper_no_skip_recv);
3131
  REGISTER_HELPER(zend_jit_vm_stack_free_args_helper);
3132
  REGISTER_HELPER(zend_free_extra_named_params);
3133
  REGISTER_HELPER(zend_jit_free_call_frame);
3134
  REGISTER_HELPER(zend_jit_exception_in_interrupt_handler_helper);
3135
  REGISTER_HELPER(zend_jit_verify_arg_slow);
3136
  REGISTER_HELPER(zend_missing_arg_error);
3137
  REGISTER_HELPER(zend_jit_only_vars_by_reference);
3138
  REGISTER_HELPER(zend_jit_leave_func_helper);
3139
  REGISTER_HELPER(zend_jit_leave_nested_func_helper);
3140
  REGISTER_HELPER(zend_jit_leave_top_func_helper);
3141
  REGISTER_HELPER(zend_jit_fetch_global_helper);
3142
  REGISTER_HELPER(zend_jit_hash_index_lookup_rw_no_packed);
3143
  REGISTER_HELPER(zend_jit_hash_index_lookup_rw);
3144
  REGISTER_HELPER(zend_jit_hash_lookup_rw);
3145
  REGISTER_HELPER(zend_jit_symtable_find);
3146
  REGISTER_HELPER(zend_jit_symtable_lookup_w);
3147
  REGISTER_HELPER(zend_jit_symtable_lookup_rw);
3148
  REGISTER_HELPER(zend_jit_fetch_dim_r_helper);
3149
  REGISTER_HELPER(zend_jit_fetch_dim_is_helper);
3150
  REGISTER_HELPER(zend_jit_fetch_dim_isset_helper);
3151
  REGISTER_HELPER(zend_jit_fetch_dim_rw_helper);
3152
  REGISTER_HELPER(zend_jit_fetch_dim_w_helper);
3153
  REGISTER_HELPER(zend_jit_fetch_dim_str_offset_r_helper);
3154
  REGISTER_HELPER(zend_jit_fetch_dim_str_r_helper);
3155
  REGISTER_HELPER(zend_jit_fetch_dim_str_is_helper);
3156
  REGISTER_HELPER(zend_jit_fetch_dim_obj_r_helper);
3157
  REGISTER_HELPER(zend_jit_fetch_dim_obj_is_helper);
3158
  REGISTER_HELPER(zend_jit_invalid_array_access);
3159
  REGISTER_HELPER(zend_jit_zval_array_dup);
3160
  REGISTER_HELPER(zend_jit_prepare_assign_dim_ref);
3161
  REGISTER_HELPER(zend_jit_fetch_dim_obj_w_helper);
3162
  REGISTER_HELPER(zend_jit_fetch_dim_obj_rw_helper);
3163
  REGISTER_HELPER(zend_jit_isset_dim_helper);
3164
  REGISTER_HELPER(zend_jit_assign_dim_helper);
3165
  REGISTER_HELPER(zend_jit_assign_dim_op_helper);
3166
  REGISTER_HELPER(zend_jit_fetch_obj_w_slow);
3167
  REGISTER_HELPER(zend_jit_fetch_obj_r_slow);
3168
  REGISTER_HELPER(zend_jit_fetch_obj_r_slow_ex);
3169
  REGISTER_HELPER(zend_jit_fetch_obj_is_slow);
3170
  REGISTER_HELPER(zend_jit_fetch_obj_is_slow_ex);
3171
  REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic);
3172
  REGISTER_HELPER(zend_jit_fetch_obj_r_dynamic_ex);
3173
  REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic);
3174
  REGISTER_HELPER(zend_jit_fetch_obj_is_dynamic_ex);
3175
  REGISTER_HELPER(zend_jit_check_array_promotion);
3176
  REGISTER_HELPER(zend_jit_create_typed_ref);
3177
  REGISTER_HELPER(zend_jit_invalid_property_write);
3178
  REGISTER_HELPER(zend_jit_invalid_property_read);
3179
  REGISTER_HELPER(zend_jit_extract_helper);
3180
  REGISTER_HELPER(zend_jit_invalid_property_assign);
3181
  REGISTER_HELPER(zend_jit_assign_to_typed_prop);
3182
  REGISTER_HELPER(zend_jit_assign_obj_helper);
3183
  REGISTER_HELPER(zend_jit_invalid_property_assign_op);
3184
  REGISTER_HELPER(zend_jit_assign_op_to_typed_prop);
3185
  REGISTER_HELPER(zend_jit_assign_obj_op_helper);
3186
  REGISTER_HELPER(zend_jit_invalid_property_incdec);
3187
  REGISTER_HELPER(zend_jit_inc_typed_prop);
3188
  REGISTER_HELPER(zend_jit_dec_typed_prop);
3189
  REGISTER_HELPER(zend_jit_pre_inc_typed_prop);
3190
  REGISTER_HELPER(zend_jit_post_inc_typed_prop);
3191
  REGISTER_HELPER(zend_jit_pre_dec_typed_prop);
3192
  REGISTER_HELPER(zend_jit_post_dec_typed_prop);
3193
  REGISTER_HELPER(zend_jit_pre_inc_obj_helper);
3194
  REGISTER_HELPER(zend_jit_post_inc_obj_helper);
3195
  REGISTER_HELPER(zend_jit_pre_dec_obj_helper);
3196
  REGISTER_HELPER(zend_jit_post_dec_obj_helper);
3197
  REGISTER_HELPER(zend_jit_uninit_static_prop);
3198
  REGISTER_HELPER(zend_jit_rope_end);
3199
  REGISTER_HELPER(zend_fcall_interrupt);
3200
3201
#ifndef ZTS
3202
  REGISTER_DATA(EG(current_execute_data));
3203
  REGISTER_DATA(EG(exception));
3204
  REGISTER_DATA(EG(opline_before_exception));
3205
  REGISTER_DATA(EG(vm_interrupt));
3206
  REGISTER_DATA(EG(timed_out));
3207
  REGISTER_DATA(EG(uninitialized_zval));
3208
  REGISTER_DATA(EG(zend_constants));
3209
  REGISTER_DATA(EG(jit_trace_num));
3210
  REGISTER_DATA(EG(vm_stack_top));
3211
  REGISTER_DATA(EG(vm_stack_end));
3212
  REGISTER_DATA(EG(exception_op));
3213
  REGISTER_DATA(EG(symbol_table));
3214
3215
  REGISTER_DATA(CG(map_ptr_base));
3216
#else /* ZTS */
3217
  REGISTER_HELPER(zend_jit_get_tsrm_ls_cache);
3218
#endif
3219
#endif
3220
0
}
3221
3222
static void zend_jit_calc_trace_prologue_size(void)
3223
0
{
3224
0
  zend_jit_ctx jit_ctx;
3225
0
  zend_jit_ctx *jit = &jit_ctx;
3226
0
  void *entry;
3227
0
  size_t size;
3228
3229
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);
3230
3231
0
  if (!GCC_GLOBAL_REGS) {
3232
0
    if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
3233
0
      ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
3234
0
      ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
3235
0
      jit_STORE_FP(jit, execute_data_ref);
3236
0
      jit_STORE_IP(jit, opline_ref);
3237
0
    }
3238
0
    jit->ctx.flags |= IR_FASTCALL_FUNC;
3239
0
  }
3240
3241
0
  ir_UNREACHABLE();
3242
3243
0
  entry = zend_jit_ir_compile(&jit->ctx, &size, "JIT$trace_prologue");
3244
0
  zend_jit_free_ctx(jit);
3245
3246
0
  if (!entry) {
3247
0
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: could not compile prologue");
3248
0
  }
3249
3250
0
  zend_jit_trace_prologue_size = size;
3251
0
}
3252
3253
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
3254
static uintptr_t zend_jit_hybrid_vm_sp_adj = 0;
3255
3256
typedef struct _Unwind_Context _Unwind_Context;
3257
typedef int (*_Unwind_Trace_Fn)(_Unwind_Context *, void *);
3258
extern int _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
3259
extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
3260
3261
typedef struct _zend_jit_unwind_arg {
3262
  int cnt;
3263
  uintptr_t cfa[3];
3264
} zend_jit_unwind_arg;
3265
3266
static int zend_jit_unwind_cb(_Unwind_Context *ctx, void *a)
3267
0
{
3268
0
  zend_jit_unwind_arg *arg = (zend_jit_unwind_arg*)a;
3269
0
  arg->cfa[arg->cnt] = _Unwind_GetCFA(ctx);
3270
0
  arg->cnt++;
3271
0
  if (arg->cnt == 3) {
3272
0
    return 5; // _URC_END_OF_STACK
3273
0
  }
3274
0
  return 0; // _URC_NO_REASON;
3275
0
}
3276
3277
static void ZEND_FASTCALL zend_jit_touch_vm_stack_data(void *vm_stack_data)
3278
0
{
3279
0
  zend_jit_unwind_arg arg;
3280
0
3281
0
  memset(&arg, 0, sizeof(arg));
3282
0
  _Unwind_Backtrace(zend_jit_unwind_cb, &arg);
3283
0
  if (arg.cnt == 3) {
3284
0
    zend_jit_hybrid_vm_sp_adj = arg.cfa[2] - arg.cfa[1];
3285
0
  }
3286
0
}
3287
3288
extern void (ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data);
3289
3290
static zend_never_inline void zend_jit_set_sp_adj_vm(void)
3291
0
{
3292
0
  void (ZEND_FASTCALL *orig_zend_touch_vm_stack_data)(void *);
3293
0
3294
0
  orig_zend_touch_vm_stack_data = zend_touch_vm_stack_data;
3295
0
  zend_touch_vm_stack_data = zend_jit_touch_vm_stack_data;
3296
0
  execute_ex(NULL);                                        // set sp_adj[SP_ADJ_VM]
3297
0
  zend_touch_vm_stack_data = orig_zend_touch_vm_stack_data;
3298
0
}
3299
#endif
3300
3301
#ifdef _WIN64
3302
/*
3303
 * We use a single unwind entry for the whole JIT buffer.
3304
 * This works, because all the JIT-ed PHP functions have the same "fixed stack frame".
3305
 */
3306
static PRUNTIME_FUNCTION zend_jit_uw_func = NULL;
3307
3308
#ifdef ZEND_JIT_RT_UNWINDER
3309
static PRUNTIME_FUNCTION zend_jit_unwind_callback(DWORD64 pc, PVOID context)
3310
{
3311
  return zend_jit_uw_func;
3312
}
3313
#endif
3314
3315
static void zend_jit_setup_unwinder(void)
3316
{
3317
#if ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
3318
  /* TAILCALL VM: fixed_save_regset=0, no registers pushed in prologue.
3319
   * fixed_stack_frame_size=40, fixed_call_stack_size=48 (16+IR_SHADOW_ARGS).
3320
   * Prologue is: sub rsp, 0x58 (88 bytes = 40+48). */
3321
  static const unsigned char uw_data[] = {
3322
    0x01, // Version=1, Flags=0
3323
    0x04, // Size of prolog (sub rsp,imm8 = 4 bytes: 48 83 ec 58)
3324
    0x01, // Count of unwind codes
3325
    0x00, // Frame Register=none
3326
    0x04, 0xa2, // offset 4: UWOP_ALLOC_SMALL info=10, alloc=(10+1)*8=88
3327
    0x00, 0x00, // padding
3328
  };
3329
  /* Exit call variant: base 88 + 304 (shadow+GP+FP+padding) = 392 (0x188) */
3330
  static const unsigned char uw_data_exitcall[] = {
3331
    0x01, // Version=1, Flags=0
3332
    0x07, // Size of prolog (sub rsp,imm32 = 7 bytes: 48 81 ec 88 01 00 00)
3333
    0x02, // Count of unwind codes
3334
    0x00, // Frame Register=none
3335
    0x07, 0x01, 0x31, 0x00, // offset 7: UWOP_ALLOC_LARGE info=0, size/8=49, alloc=392
3336
  };
3337
#else
3338
  /* Hardcoded SEH unwind data for JIT-ed PHP functions with "fixed stack frame" */
3339
  static const unsigned char uw_data[] = {
3340
    0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
3341
    0x10, // UBYTE Size of prolog
3342
    0x09, // UBYTE Count of unwind codes
3343
    0x00, // UBYTE: 4 Frame Register, UBYTE: 4 Frame Register offset (scaled)
3344
    // USHORT * n Unwind codes array
3345
    0x10, 0x82, // c: subq $0x48, %rsp
3346
    0x0c, 0xf0, // a: pushq %r15
3347
    0x0a, 0xe0, // 8: pushq %r14
3348
    0x08, 0xd0, // 6: pushq %r13
3349
    0x06, 0xc0, // 4: pushq %r12
3350
    0x04, 0x70, // 3: pushq %rdi
3351
    0x03, 0x60, // 2: pushq %rsi
3352
    0x02, 0x50, // 1: pushq %rbp
3353
    0x01, 0x30, // 0: pushq %rbx
3354
    0x00, 0x00,
3355
  };
3356
  static const unsigned char uw_data_exitcall[] = {
3357
    0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
3358
    0x10, // UBYTE Size of prolog
3359
    0x0a, // UBYTE Count of unwind codes
3360
    0x00, // UBYTE: 4 Frame Register, UBYTE: 4 Frame Register offset (scaled)
3361
    // USHORT * n Unwind codes array
3362
    0x10, 0x01, 0x2f, 0x00, // c: subq 376, %rsp ; 0x48 + 32+16*8+16*8+8+8
3363
    0x0c, 0xf0, // a: pushq %r15
3364
    0x0a, 0xe0, // 8: pushq %r14
3365
    0x08, 0xd0, // 6: pushq %r13
3366
    0x06, 0xc0, // 4: pushq %r12
3367
    0x04, 0x70, // 3: pushq %rdi
3368
    0x03, 0x60, // 2: pushq %rsi
3369
    0x02, 0x50, // 1: pushq %rbp
3370
    0x01, 0x30, // 0: pushq %rbx
3371
  };
3372
#endif
3373
3374
  zend_jit_uw_func = (PRUNTIME_FUNCTION)*dasm_ptr;
3375
  *dasm_ptr = (char*)*dasm_ptr + ZEND_MM_ALIGNED_SIZE_EX(sizeof(RUNTIME_FUNCTION) * 4 +
3376
    sizeof(uw_data) + sizeof(uw_data_exitcall) + sizeof(uw_data), 16);
3377
3378
  zend_jit_uw_func[0].BeginAddress = 0;
3379
  zend_jit_uw_func[1].BeginAddress = (uintptr_t)zend_jit_stub_handlers[jit_stub_trace_exit] - (uintptr_t)dasm_buf;
3380
  zend_jit_uw_func[2].BeginAddress = (uintptr_t)zend_jit_stub_handlers[jit_stub_undefined_offset] - (uintptr_t)dasm_buf;
3381
3382
  zend_jit_uw_func[0].EndAddress = zend_jit_uw_func[1].BeginAddress;
3383
  zend_jit_uw_func[1].EndAddress = zend_jit_uw_func[2].BeginAddress;
3384
  zend_jit_uw_func[2].EndAddress = (uintptr_t)dasm_end - (uintptr_t)dasm_buf;
3385
3386
  zend_jit_uw_func[0].UnwindData = (uintptr_t)zend_jit_uw_func  - (uintptr_t)dasm_buf + sizeof(RUNTIME_FUNCTION) * 4;
3387
  zend_jit_uw_func[1].UnwindData = zend_jit_uw_func[0].UnwindData + sizeof(uw_data);
3388
  zend_jit_uw_func[2].UnwindData = zend_jit_uw_func[1].UnwindData + sizeof(uw_data_exitcall);
3389
3390
  memcpy((char*)dasm_buf + zend_jit_uw_func[0].UnwindData, uw_data, sizeof(uw_data));
3391
  memcpy((char*)dasm_buf + zend_jit_uw_func[1].UnwindData, uw_data_exitcall, sizeof(uw_data_exitcall));
3392
  memcpy((char*)dasm_buf + zend_jit_uw_func[2].UnwindData, uw_data, sizeof(uw_data));
3393
3394
#ifdef ZEND_JIT_RT_UNWINDER
3395
  RtlInstallFunctionTableCallback(
3396
    (uintptr_t)dasm_buf | 3,
3397
    (uintptr_t) dasm_buf,
3398
    (uintptr_t)dasm_end - (uintptr_t)dasm_buf,
3399
    zend_jit_unwind_callback,
3400
    NULL,
3401
    NULL);
3402
#else
3403
  RtlAddFunctionTable(zend_jit_uw_func, 3, (uintptr_t)dasm_buf);
3404
#endif
3405
}
3406
#endif
3407
3408
static void zend_jit_setup(bool reattached)
3409
0
{
3410
#if defined(IR_TARGET_X86)
3411
  if (!zend_cpu_supports_sse2()) {
3412
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: CPU doesn't support SSE2");
3413
  }
3414
#endif
3415
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
3416
0
  allowed_opt_flags = 0;
3417
0
  if (zend_cpu_supports_avx()) {
3418
0
    allowed_opt_flags |= ZEND_JIT_CPU_AVX;
3419
0
  }
3420
0
# ifdef HAVE_ZEND_CPU_SUPPORTS_CLDEMOTE
3421
0
  if (zend_cpu_supports_cldemote()) {
3422
0
    default_mflags |= IR_X86_CLDEMOTE;
3423
0
  }
3424
0
# endif
3425
0
#endif
3426
3427
#ifdef ZTS
3428
  zend_result result = zend_jit_resolve_tsrm_ls_cache_offsets(
3429
    &tsrm_ls_cache_tcb_offset,
3430
    &tsrm_tls_index,
3431
    &tsrm_tls_offset
3432
  );
3433
  if (result == FAILURE) {
3434
    zend_accel_error(ACCEL_LOG_INFO,
3435
        "Could not get _tsrm_ls_cache offsets, will fallback to runtime resolution");
3436
  }
3437
#endif
3438
3439
0
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
3440
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
3441
0
    zend_jit_set_sp_adj_vm(); // set zend_jit_hybrid_vm_sp_adj
3442
0
  }
3443
0
#endif
3444
3445
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
3446
0
    zend_jit_setup_disasm();
3447
0
  }
3448
3449
0
#ifndef _WIN32
3450
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3451
0
    ir_perf_jitdump_open();
3452
0
  }
3453
3454
0
#endif
3455
0
  zend_long debug = JIT_G(debug);
3456
0
  if (!(debug & ZEND_JIT_DEBUG_ASM_STUBS)) {
3457
0
    JIT_G(debug) &= ~(ZEND_JIT_DEBUG_IR_SRC|ZEND_JIT_DEBUG_IR_FINAL|
3458
0
      ZEND_JIT_DEBUG_IR_CODEGEN|
3459
0
      ZEND_JIT_DEBUG_IR_AFTER_SCCP|ZEND_JIT_DEBUG_IR_AFTER_CFG|ZEND_JIT_DEBUG_IR_AFTER_GCM|
3460
0
      ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE|ZEND_JIT_DEBUG_IR_AFTER_REGS);
3461
0
  }
3462
3463
0
  zend_jit_calc_trace_prologue_size();
3464
0
  if (!reattached) {
3465
0
    zend_jit_setup_stubs();
3466
0
  }
3467
0
  JIT_G(debug) = debug;
3468
3469
#ifdef _WIN64
3470
  zend_jit_setup_unwinder();
3471
#endif
3472
0
}
3473
3474
static void zend_jit_shutdown_ir(void)
3475
0
{
3476
0
#ifndef _WIN32
3477
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
3478
0
    ir_perf_jitdump_close();
3479
0
  }
3480
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
3481
0
    ir_gdb_unregister_all();
3482
0
  }
3483
0
#endif
3484
#ifdef HAVE_CAPSTONE
3485
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_ASM_STUBS)) {
3486
    ir_disasm_free();
3487
  }
3488
#endif
3489
0
}
3490
3491
/* PHP control flow reconstruction helpers */
3492
static ir_ref jit_IF_ex(zend_jit_ctx *jit, ir_ref condition, ir_ref true_block)
3493
0
{
3494
0
  ir_ref ref = ir_IF(condition);
3495
  /* op3 is used as a temporary storage for PHP BB number to reconstruct PHP control flow.
3496
   *
3497
   * It's used in jit_IF_TRUE_FALSE_ex() to select IF_TRUE or IF_FALSE instructions
3498
   * to start target block
3499
   */
3500
0
  ir_set_op(&jit->ctx, ref, 3, true_block);
3501
0
  return ref;
3502
0
}
3503
3504
static void jit_IF_TRUE_FALSE_ex(zend_jit_ctx *jit, ir_ref if_ref, ir_ref true_block)
3505
0
{
3506
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3507
0
  ZEND_ASSERT(if_ref);
3508
0
  ZEND_ASSERT(jit->ctx.ir_base[if_ref].op == IR_IF);
3509
0
  ZEND_ASSERT(jit->ctx.ir_base[if_ref].op3);
3510
0
  if (jit->ctx.ir_base[if_ref].op3 == true_block) {
3511
0
    ir_IF_TRUE(if_ref);
3512
0
  } else {
3513
0
    ir_IF_FALSE(if_ref);
3514
0
  }
3515
0
}
3516
3517
static void zend_jit_case_start(zend_jit_ctx *jit, int switch_b, int case_b, ir_ref switch_ref);
3518
3519
static void _zend_jit_add_predecessor_ref(zend_jit_ctx *jit, int b, int pred, ir_ref ref)
3520
0
{
3521
0
  int *p;
3522
0
  zend_basic_block *bb;
3523
0
  ir_ref *r, header;
3524
3525
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3526
0
  bb = &jit->ssa->cfg.blocks[b];
3527
0
  p = &jit->ssa->cfg.predecessors[bb->predecessor_offset];
3528
0
  r = &jit->bb_edges[jit->bb_predecessors[b]];
3529
0
  for (uint32_t i = 0; i < bb->predecessors_count; i++, p++, r++) {
3530
0
    if (*p == pred) {
3531
0
      ZEND_ASSERT(*r == IR_UNUSED || *r == ref);
3532
0
      header = jit->bb_start_ref[b];
3533
0
      if (header) {
3534
        /* this is back edge */
3535
0
        ZEND_ASSERT(jit->ctx.ir_base[header].op == IR_LOOP_BEGIN);
3536
0
        if (jit->ctx.ir_base[ref].op == IR_END) {
3537
0
          jit->ctx.ir_base[ref].op = IR_LOOP_END;
3538
0
        } else if (jit->ctx.ir_base[ref].op == IR_IF) {
3539
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3540
0
          ref = ir_LOOP_END();
3541
0
        } else if (jit->ctx.ir_base[ref].op == IR_SWITCH) {
3542
0
          zend_jit_case_start(jit, pred, b, ref);
3543
0
          ref = ir_LOOP_END();
3544
0
        } else if (jit->ctx.ir_base[ref].op == IR_UNREACHABLE) {
3545
0
          ir_BEGIN(ref);
3546
0
          ref = ir_LOOP_END();
3547
0
        } else {
3548
0
          ZEND_UNREACHABLE();
3549
0
        }
3550
0
        ir_MERGE_SET_OP(header, i + 1, ref);
3551
0
      }
3552
0
      *r = ref;
3553
0
      return;
3554
0
    }
3555
0
  }
3556
0
  ZEND_UNREACHABLE();
3557
0
}
3558
3559
static void _zend_jit_merge_smart_branch_inputs(zend_jit_ctx *jit,
3560
                                                uint32_t      true_label,
3561
                                                uint32_t      false_label,
3562
                                                ir_ref        true_inputs,
3563
                                                ir_ref        false_inputs)
3564
0
{
3565
0
  ir_ref true_path = IR_UNUSED, false_path = IR_UNUSED;
3566
3567
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3568
0
  if (true_inputs) {
3569
0
    ZEND_ASSERT(jit->ctx.ir_base[true_inputs].op == IR_END);
3570
0
    if (!jit->ctx.ir_base[true_inputs].op2) {
3571
0
      true_path = true_inputs;
3572
0
    } else {
3573
0
      ir_MERGE_list(true_inputs);
3574
0
      true_path = ir_END();
3575
0
    }
3576
0
  }
3577
0
  if (false_inputs) {
3578
0
    ZEND_ASSERT(jit->ctx.ir_base[false_inputs].op == IR_END);
3579
0
    if (!jit->ctx.ir_base[false_inputs].op2) {
3580
0
      false_path = false_inputs;
3581
0
    } else {
3582
0
      ir_MERGE_list(false_inputs);
3583
0
      false_path = ir_END();
3584
0
    }
3585
0
  }
3586
3587
0
  if (true_label == false_label && true_path && false_path) {
3588
0
    ir_MERGE_2(true_path, false_path);
3589
0
    _zend_jit_add_predecessor_ref(jit, true_label, jit->b, ir_END());
3590
0
  } else if (!true_path && !false_path) {
3591
    /* dead code */
3592
0
    true_path = ir_END();
3593
0
    _zend_jit_add_predecessor_ref(jit, true_label, jit->b, true_path);
3594
0
  } else {
3595
0
    if (true_path) {
3596
0
      _zend_jit_add_predecessor_ref(jit, true_label, jit->b, true_path);
3597
0
    }
3598
0
    if (false_path) {
3599
0
      _zend_jit_add_predecessor_ref(jit, false_label, jit->b, false_path);
3600
0
    }
3601
0
  }
3602
3603
0
  jit->b = -1;
3604
0
}
3605
3606
static void _zend_jit_fix_merges(zend_jit_ctx *jit)
3607
0
{
3608
0
  int i, count;
3609
0
  ir_ref j, k, n, *p, *q, *r;
3610
0
  ir_ref ref;
3611
0
  ir_insn *insn, *phi;
3612
3613
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3614
0
  count = jit->ssa->cfg.blocks_count;
3615
0
  for (i = 0, p = jit->bb_start_ref; i < count; i++, p++) {
3616
0
    ref = *p;
3617
0
    if (ref) {
3618
0
      insn = &jit->ctx.ir_base[ref];
3619
0
      if (insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN) {
3620
0
        n = insn->inputs_count;
3621
        /* Remove IS_UNUSED inputs */
3622
0
        for (j = k = 0, q = r = insn->ops + 1; j < n; j++, q++) {
3623
0
          if (*q) {
3624
0
            if (q != r) {
3625
0
              *r = *q;
3626
0
              phi = insn + 1 + (n >> 2);
3627
0
              while (phi->op == IR_PI) {
3628
0
                phi++;
3629
0
              }
3630
0
              while (phi->op == IR_PHI) {
3631
0
                ir_insn_set_op(phi, k + 2, ir_insn_op(phi, j + 2));
3632
0
                phi += 1 + ((n + 1) >> 2);
3633
0
              }
3634
0
            }
3635
0
            k++;
3636
0
            r++;
3637
0
          }
3638
0
        }
3639
0
        if (k != n) {
3640
0
          ir_ref n2, k2;
3641
3642
0
          if (k <= 1) {
3643
0
            insn->op = IR_BEGIN;
3644
0
            insn->inputs_count = 0;
3645
0
          } else {
3646
0
            insn->inputs_count = k;
3647
0
          }
3648
0
          n2 = 1 + (n >> 2);
3649
0
          k2 = 1 + (k >> 2);
3650
0
          while (k2 != n2) {
3651
0
            (insn+k2)->optx = IR_NOP;
3652
0
            k2++;
3653
0
          }
3654
0
          phi = insn + 1 + (n >> 2);
3655
0
          while (phi->op == IR_PI) {
3656
0
            phi++;
3657
0
          }
3658
0
          while (phi->op == IR_PHI) {
3659
0
            if (k <= 1) {
3660
0
              phi->op = IR_COPY;
3661
0
              phi->op1 = phi->op2;
3662
0
              phi->op2 = 1;
3663
0
              phi->inputs_count = 0;
3664
0
            } else {
3665
0
              phi->inputs_count = k + 1;
3666
0
            }
3667
0
            n2 = 1 + ((n + 1) >> 2);
3668
0
            k2 = 1 + ((k + 1) >> 2);
3669
0
            while (k2 != n2) {
3670
0
              (phi+k2)->optx = IR_NOP;
3671
0
              k2++;
3672
0
            }
3673
0
            phi += 1 + ((n + 1) >> 2);
3674
0
          }
3675
0
        }
3676
0
      }
3677
0
    }
3678
0
  }
3679
0
}
3680
3681
static void zend_jit_case_start(zend_jit_ctx *jit, int switch_b, int case_b, ir_ref switch_ref)
3682
0
{
3683
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[switch_b];
3684
0
  const zend_op *opline = &jit->op_array->opcodes[bb->start + bb->len - 1];
3685
3686
0
  if (opline->opcode == ZEND_SWITCH_LONG
3687
0
   || opline->opcode == ZEND_SWITCH_STRING
3688
0
   || opline->opcode == ZEND_MATCH) {
3689
0
    HashTable *jumptable = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
3690
0
    const zend_op *default_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
3691
0
    int default_b = jit->ssa->cfg.map[default_opline - jit->op_array->opcodes];
3692
0
    zval *zv;
3693
0
    ir_ref list = IR_UNUSED, idx;
3694
0
    bool first = true;
3695
3696
0
    ZEND_HASH_FOREACH_VAL(jumptable, zv) {
3697
0
      const zend_op *target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
3698
0
      int b = jit->ssa->cfg.map[target - jit->op_array->opcodes];
3699
3700
0
      if (b == case_b) {
3701
0
        if (!first) {
3702
0
          ir_END_list(list);
3703
0
        }
3704
0
        if (HT_IS_PACKED(jumptable)) {
3705
0
          idx = ir_CONST_LONG(zv - jumptable->arPacked);
3706
0
        } else {
3707
0
          idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
3708
0
        }
3709
0
        ir_CASE_VAL(switch_ref, idx);
3710
0
        first = false;
3711
0
      }
3712
0
    } ZEND_HASH_FOREACH_END();
3713
0
    if (default_b == case_b) {
3714
0
      if (!first) {
3715
0
        ir_END_list(list);
3716
0
      }
3717
0
      if (jit->ctx.ir_base[switch_ref].op3) {
3718
        /* op3 may contain a list of additional "default" path inputs for MATCH */
3719
0
        ir_ref ref = jit->ctx.ir_base[switch_ref].op3;
3720
0
        jit->ctx.ir_base[switch_ref].op3 = IS_UNDEF;
3721
0
        ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_END);
3722
0
        ir_ref end = ref;
3723
0
        while (jit->ctx.ir_base[end].op2) {
3724
0
          ZEND_ASSERT(jit->ctx.ir_base[end].op == IR_END);
3725
0
          end = jit->ctx.ir_base[end].op2;
3726
0
        }
3727
0
        jit->ctx.ir_base[end].op2 = list;
3728
0
        list = ref;
3729
0
      }
3730
0
      ir_CASE_DEFAULT(switch_ref);
3731
0
    }
3732
0
    if (list) {
3733
0
      ir_END_list(list);
3734
0
      ir_MERGE_list(list);
3735
0
    }
3736
0
  } else {
3737
0
    ZEND_UNREACHABLE();
3738
0
  }
3739
0
}
3740
3741
static int zend_jit_bb_start(zend_jit_ctx *jit, int b)
3742
0
{
3743
0
  zend_basic_block *bb;
3744
0
  int *p, pred;
3745
0
  ir_ref ref, bb_start;
3746
3747
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3748
0
  ZEND_ASSERT(b < jit->ssa->cfg.blocks_count);
3749
0
  bb = &jit->ssa->cfg.blocks[b];
3750
0
  ZEND_ASSERT((bb->flags & ZEND_BB_REACHABLE) != 0);
3751
0
  uint32_t n = bb->predecessors_count;
3752
3753
0
  if (n == 0) {
3754
    /* pass */
3755
0
    ZEND_ASSERT(jit->ctx.control);
3756
0
#if ZEND_DEBUG
3757
0
    ref = jit->ctx.control;
3758
0
    ir_insn *insn = &jit->ctx.ir_base[ref];
3759
0
    while (insn->op >= IR_CALL && insn->op <= IR_TRAP) {
3760
0
      ref = insn->op1;
3761
0
      insn = &jit->ctx.ir_base[ref];
3762
0
    }
3763
0
    ZEND_ASSERT(insn->op == IR_START);
3764
0
    ZEND_ASSERT(ref == 1);
3765
0
#endif
3766
0
    bb_start = 1;
3767
0
    if (jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY) {
3768
      /* prvent END/BEGIN merging */
3769
0
      jit->ctx.control = ir_emit1(&jit->ctx, IR_BEGIN, ir_END());
3770
0
      bb_start = jit->ctx.control;
3771
0
    }
3772
0
  } else if (n == 1) {
3773
0
    ZEND_ASSERT(!jit->ctx.control);
3774
0
    pred = jit->ssa->cfg.predecessors[bb->predecessor_offset];
3775
0
    ref = jit->bb_edges[jit->bb_predecessors[b]];
3776
0
    if (ref == IR_UNUSED) {
3777
0
      if (!jit->ctx.control) {
3778
0
        ir_BEGIN(IR_UNUSED); /* unreachable block */
3779
0
      }
3780
0
    } else {
3781
0
      ir_op op = jit->ctx.ir_base[ref].op;
3782
3783
0
      if (op == IR_IF) {
3784
0
        if (!jit->ctx.control) {
3785
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3786
0
        } else {
3787
0
          ir_ref entry_path = ir_END();
3788
0
          jit_IF_TRUE_FALSE_ex(jit, ref, b);
3789
0
          ir_MERGE_WITH(entry_path);
3790
0
        }
3791
0
      } else if (op == IR_SWITCH) {
3792
0
        zend_jit_case_start(jit, pred, b, ref);
3793
0
      } else {
3794
0
        if (!jit->ctx.control) {
3795
0
          ZEND_ASSERT(op == IR_END || op == IR_UNREACHABLE || op == IR_RETURN);
3796
0
          if ((jit->ssa->cfg.blocks[b].flags & ZEND_BB_RECV_ENTRY)
3797
0
           && (jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY)) {
3798
            /* prvent END/BEGIN merging */
3799
0
            jit->ctx.control = ir_emit1(&jit->ctx, IR_BEGIN, ref);
3800
0
          } else {
3801
0
            ir_BEGIN(ref);
3802
0
          }
3803
0
        } else {
3804
0
          ir_MERGE_WITH(ref);
3805
0
        }
3806
0
      }
3807
0
    }
3808
0
    bb_start = jit->ctx.control;
3809
0
  } else {
3810
0
    int forward_edges_count = 0;
3811
0
    int back_edges_count = 0;
3812
0
    ir_ref *pred_refs;
3813
0
    ir_ref entry_path = IR_UNUSED;
3814
0
    ALLOCA_FLAG(use_heap);
3815
3816
0
    ZEND_ASSERT(!jit->ctx.control);
3817
0
    if (jit->ctx.control) {
3818
0
      entry_path = ir_END();
3819
0
    }
3820
0
    pred_refs = (ir_ref *)do_alloca(sizeof(ir_ref) * n, use_heap);
3821
0
    uint32_t i;
3822
0
    for (i = 0, p = jit->ssa->cfg.predecessors + bb->predecessor_offset; i < n; p++, i++) {
3823
0
      pred = *p;
3824
0
      if (jit->bb_start_ref[pred]) {
3825
        /* forward edge */
3826
0
        forward_edges_count++;
3827
0
        ref = jit->bb_edges[jit->bb_predecessors[b] + i];
3828
0
        if (ref == IR_UNUSED) {
3829
          /* dead edge */
3830
0
          pred_refs[i] = IR_UNUSED;
3831
0
        } else {
3832
0
          ir_op op = jit->ctx.ir_base[ref].op;
3833
3834
0
          if (op == IR_IF) {
3835
0
            jit_IF_TRUE_FALSE_ex(jit, ref, b);
3836
0
            pred_refs[i] = ir_END();
3837
0
          } else if (op == IR_SWITCH) {
3838
0
            zend_jit_case_start(jit, pred, b, ref);
3839
0
            pred_refs[i] = ir_END();
3840
0
          } else {
3841
0
            ZEND_ASSERT(op == IR_END || op == IR_UNREACHABLE || op == IR_RETURN);
3842
0
            pred_refs[i] = ref;
3843
0
          }
3844
0
        }
3845
0
      } else {
3846
        /* backward edge */
3847
0
        back_edges_count++;
3848
0
        pred_refs[i] = IR_UNUSED;
3849
0
      }
3850
0
    }
3851
3852
0
    if (bb->flags & ZEND_BB_LOOP_HEADER) {
3853
0
      ZEND_ASSERT(back_edges_count != 0);
3854
0
      ZEND_ASSERT(forward_edges_count != 0);
3855
0
      ir_MERGE_N(n, pred_refs);
3856
0
      jit->ctx.ir_base[jit->ctx.control].op = IR_LOOP_BEGIN;
3857
0
      bb_start = jit->ctx.control;
3858
0
      if (entry_path) {
3859
0
        ir_MERGE_WITH(entry_path);
3860
0
      }
3861
0
    } else {
3862
//      ZEND_ASSERT(back_edges_count != 0);
3863
      /* edges from exceptional blocks may be counted as back edges */
3864
0
      ir_MERGE_N(n, pred_refs);
3865
0
      bb_start = jit->ctx.control;
3866
0
      if (entry_path) {
3867
0
        ir_MERGE_WITH(entry_path);
3868
0
      }
3869
0
    }
3870
0
    free_alloca(pred_refs, use_heap);
3871
0
  }
3872
0
  jit->b = b;
3873
0
  jit->bb_start_ref[b] = bb_start;
3874
3875
0
  if ((bb->flags & ZEND_BB_ENTRY) || (bb->idom >= 0 && jit->bb_start_ref[bb->idom] < jit->ctx.fold_cse_limit)) {
3876
0
    jit->ctx.fold_cse_limit = bb_start;
3877
0
  }
3878
3879
0
  return 1;
3880
0
}
3881
3882
static int zend_jit_bb_end(zend_jit_ctx *jit, int b)
3883
0
{
3884
0
  int succ;
3885
0
  zend_basic_block *bb;
3886
3887
0
  ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
3888
0
  if (jit->b != b) {
3889
0
    return 1;
3890
0
  }
3891
3892
0
  bb = &jit->ssa->cfg.blocks[b];
3893
0
  ZEND_ASSERT(bb->successors_count != 0);
3894
0
  if (bb->successors_count == 1) {
3895
0
    succ = bb->successors[0];
3896
0
  } else {
3897
0
    const zend_op *opline = &jit->op_array->opcodes[bb->start + bb->len - 1];
3898
3899
    /* Use only the following successor of SWITCH and FE_RESET_R */
3900
0
    ZEND_ASSERT(opline->opcode == ZEND_SWITCH_LONG
3901
0
     || opline->opcode == ZEND_SWITCH_STRING
3902
0
     || opline->opcode == ZEND_MATCH
3903
0
     || opline->opcode == ZEND_FE_RESET_R);
3904
0
    succ = b + 1;
3905
0
  }
3906
0
  _zend_jit_add_predecessor_ref(jit, succ, b, ir_END());
3907
0
  jit->b = -1;
3908
0
  return 1;
3909
0
}
3910
3911
static int jit_CMP_IP(zend_jit_ctx *jit, ir_op op, const zend_op *next_opline)
3912
0
{
3913
0
  ir_ref ref;
3914
3915
0
#if 1
3916
0
  ref = jit_IP32(jit);
3917
0
  ref = ir_CMP_OP(op, ref, ir_CONST_U32((uint32_t)(uintptr_t)next_opline));
3918
#else
3919
  ref = jit_IP(jit);
3920
  ref = ir_CMP_OP(op, ref, ir_CONST_ADDR(next_opline));
3921
#endif
3922
0
  return ref;
3923
0
}
3924
3925
static int zend_jit_jmp_frameless(
3926
  zend_jit_ctx *jit,
3927
  const zend_op *opline,
3928
  const void *exit_addr,
3929
  zend_jmp_fl_result guard
3930
0
) {
3931
0
  ir_ref ref, if_ref, cache_result, function_result, phi_result, cache_slot_ref;
3932
0
  zend_basic_block *bb;
3933
3934
  // JIT: CACHED_PTR(opline->extended_value)
3935
0
  cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value);
3936
0
  cache_result = ir_LOAD_L(cache_slot_ref);
3937
3938
  // JIT: if (UNEXPECTED(!result))
3939
0
  if_ref = ir_IF(cache_result);
3940
0
  ir_IF_FALSE_cold(if_ref);
3941
0
  zval *func_name_zv = RT_CONSTANT(opline, opline->op1);
3942
0
  function_result = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_jit_jmp_frameless_helper),
3943
0
    ir_CONST_ADDR(func_name_zv),
3944
0
    cache_slot_ref);
3945
0
  ir_MERGE_WITH_EMPTY_TRUE(if_ref);
3946
3947
0
  phi_result = ir_PHI_2(IR_LONG, function_result, cache_result);
3948
3949
0
  if (exit_addr) {
3950
0
    ir_GUARD(ir_EQ(phi_result, ir_CONST_LONG(guard)), ir_CONST_ADDR(exit_addr));
3951
0
  } else {
3952
0
    ZEND_ASSERT(jit->b >= 0);
3953
0
    bb = &jit->ssa->cfg.blocks[jit->b];
3954
    // JIT: if (result == ZEND_JMP_FL_HIT)
3955
0
    ref = jit_IF_ex(jit, ir_EQ(phi_result, ir_CONST_LONG(ZEND_JMP_FL_HIT)), bb->successors[0]);
3956
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
3957
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
3958
0
    jit->b = -1;
3959
0
  }
3960
3961
0
  return 1;
3962
0
}
3963
3964
static int zend_jit_cond_jmp(zend_jit_ctx *jit, const zend_op *next_opline, int target_block)
3965
0
{
3966
0
  ir_ref ref;
3967
0
  zend_basic_block *bb;
3968
3969
0
  ZEND_ASSERT(jit->b >= 0);
3970
0
  bb = &jit->ssa->cfg.blocks[jit->b];
3971
3972
0
  ZEND_ASSERT(bb->successors_count == 2);
3973
0
  if (bb->successors[0] == bb->successors[1]) {
3974
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
3975
0
    jit->b = -1;
3976
0
    zend_jit_set_last_valid_opline(jit, next_opline);
3977
0
    return 1;
3978
0
  }
3979
3980
0
  ref = jit_IF_ex(jit, jit_CMP_IP(jit, IR_NE, next_opline), target_block);
3981
3982
0
  _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
3983
0
  _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
3984
3985
0
  jit->b = -1;
3986
0
  zend_jit_set_last_valid_opline(jit, next_opline);
3987
3988
0
  return 1;
3989
0
}
3990
3991
static int zend_jit_set_cond(zend_jit_ctx *jit, const zend_op *opline, const zend_op *next_opline, uint32_t var)
3992
0
{
3993
0
  ir_ref ref;
3994
3995
0
  ir_op op = (opline->result_type & IS_SMART_BRANCH_JMPZ) ? IR_EQ : IR_NE;
3996
0
  ref = ir_ADD_U32(ir_ZEXT_U32(jit_CMP_IP(jit, op, next_opline)), ir_CONST_U32(IS_FALSE));
3997
3998
  // EX_VAR(var) = ...
3999
0
  ir_STORE(ir_ADD_OFFSET(jit_FP(jit), var + offsetof(zval, u1.type_info)), ref);
4000
4001
0
  zend_jit_reset_last_valid_opline(jit);
4002
0
  return zend_jit_set_ip(jit, next_opline - 1);
4003
0
}
4004
4005
/* PHP JIT handlers */
4006
static void zend_jit_check_exception(zend_jit_ctx *jit)
4007
0
{
4008
0
  ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
4009
0
    jit_STUB_ADDR(jit, jit_stub_exception_handler));
4010
0
}
4011
4012
static void zend_jit_check_exception_undef_result(zend_jit_ctx *jit, const zend_op *opline)
4013
0
{
4014
0
  ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
4015
0
    jit_STUB_ADDR(jit,
4016
0
      (opline->result_type & (IS_TMP_VAR|IS_VAR)) ? jit_stub_exception_handler_undef : jit_stub_exception_handler));
4017
0
}
4018
4019
static void zend_jit_type_check_undef(zend_jit_ctx  *jit,
4020
                                      ir_ref         type,
4021
                                      uint32_t       var,
4022
                                      const zend_op *opline,
4023
                                      bool           check_exception,
4024
                                      bool           in_cold_path,
4025
                                      bool           undef_result)
4026
0
{
4027
0
  ir_ref if_def = ir_IF(type);
4028
4029
0
  if (!in_cold_path) {
4030
0
    ir_IF_FALSE_cold(if_def);
4031
0
  } else {
4032
0
    ir_IF_FALSE(if_def);
4033
0
  }
4034
0
  if (opline) {
4035
0
    jit_SET_EX_OPLINE(jit, opline);
4036
0
  }
4037
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(var));
4038
0
  if (check_exception) {
4039
0
    if (undef_result) {
4040
0
      zend_jit_check_exception_undef_result(jit, opline);
4041
0
    } else {
4042
0
      zend_jit_check_exception(jit);
4043
0
    }
4044
0
  }
4045
0
  ir_MERGE_WITH_EMPTY_TRUE(if_def);
4046
0
}
4047
4048
static ir_ref zend_jit_zval_check_undef(zend_jit_ctx  *jit,
4049
                                        ir_ref         ref,
4050
                                        uint32_t       var,
4051
                                        const zend_op *opline,
4052
                                        bool           check_exception)
4053
0
{
4054
0
  ir_ref if_def, ref2;
4055
4056
0
  if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
4057
0
  ir_IF_FALSE_cold(if_def);
4058
4059
0
  if (opline) {
4060
0
    jit_SET_EX_OPLINE(jit, opline);
4061
0
  }
4062
4063
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(var));
4064
4065
0
  if (check_exception) {
4066
0
    zend_jit_check_exception(jit);
4067
0
  }
4068
4069
0
  ref2 = jit_EG(uninitialized_zval);
4070
4071
0
  ir_MERGE_WITH_EMPTY_TRUE(if_def);
4072
4073
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
4074
0
}
4075
4076
static void zend_jit_recv_entry(zend_jit_ctx *jit, int b)
4077
0
{
4078
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[b];
4079
0
  int pred;
4080
0
  ir_ref ref;
4081
4082
0
  ZEND_ASSERT(bb->predecessors_count > 0);
4083
4084
0
  pred = jit->bb_predecessors[b];
4085
0
  ref = jit->bb_edges[pred];
4086
4087
0
  ZEND_ASSERT(ref);
4088
0
  ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_END);
4089
4090
  /* Insert a MERGE block with additional ENTRY input between predecessor and this one */
4091
0
  ir_ENTRY(ref, bb->start);
4092
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4093
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4094
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4095
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4096
0
    jit_STORE_FP(jit, 2);
4097
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4098
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4099
0
    jit_STORE_IP(jit, 3);
4100
0
  }
4101
4102
0
  ir_MERGE_WITH(ref);
4103
0
  jit->bb_edges[pred] = ir_END();
4104
0
}
4105
4106
static void zend_jit_osr_entry(zend_jit_ctx *jit, int b)
4107
0
{
4108
0
  zend_basic_block *bb = &jit->ssa->cfg.blocks[b];
4109
0
  ir_ref ref = ir_END();
4110
4111
  /* Insert a MERGE block with additional ENTRY input between predecessor and this one */
4112
0
  ir_ENTRY(ref, bb->start);
4113
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4114
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4115
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4116
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4117
0
    jit_STORE_FP(jit, 2);
4118
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4119
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4120
0
    jit_STORE_IP(jit, 3);
4121
0
  }
4122
4123
0
  ir_MERGE_WITH(ref);
4124
0
}
4125
4126
static ir_ref zend_jit_continue_entry(zend_jit_ctx *jit, ir_ref src, unsigned int label)
4127
0
{
4128
0
  ir_ENTRY(src, label);
4129
0
  if (!GCC_GLOBAL_REGS && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
4130
    /* 2 and 3 are hardcoded reference to IR_PARAMs */
4131
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op == IR_PARAM);
4132
0
    ZEND_ASSERT(jit->ctx.ir_base[2].op3 == 1);
4133
0
    jit_STORE_FP(jit, 2);
4134
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op == IR_PARAM);
4135
0
    ZEND_ASSERT(jit->ctx.ir_base[3].op3 == 2);
4136
0
    jit_STORE_IP(jit, 3);
4137
0
  }
4138
0
  return ir_END();
4139
0
}
4140
4141
static int zend_jit_handler(zend_jit_ctx *jit, const zend_op *opline, int may_throw)
4142
0
{
4143
0
  zend_jit_set_ip(jit, opline);
4144
0
  if (GCC_GLOBAL_REGS) {
4145
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4146
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
4147
0
  } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
4148
0
    zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4149
0
    ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
4150
0
    jit_STORE_IP(jit, ip);
4151
0
  } else {
4152
0
    zend_vm_opcode_handler_t handler = opline->handler;
4153
0
    ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
4154
0
    jit_STORE_IP(jit, ip);
4155
0
  }
4156
0
  if (may_throw) {
4157
0
    zend_jit_check_exception(jit);
4158
0
  }
4159
  /* Skip the following OP_DATA */
4160
0
  switch (opline->opcode) {
4161
0
    case ZEND_ASSIGN_DIM:
4162
0
    case ZEND_ASSIGN_OBJ:
4163
0
    case ZEND_ASSIGN_STATIC_PROP:
4164
0
    case ZEND_ASSIGN_DIM_OP:
4165
0
    case ZEND_ASSIGN_OBJ_OP:
4166
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
4167
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
4168
0
    case ZEND_ASSIGN_OBJ_REF:
4169
0
    case ZEND_FRAMELESS_ICALL_3:
4170
0
    case ZEND_DECLARE_ATTRIBUTED_CONST:
4171
0
      zend_jit_set_last_valid_opline(jit, opline + 2);
4172
0
      break;
4173
0
    default:
4174
0
      zend_jit_set_last_valid_opline(jit, opline + 1);
4175
0
      break;
4176
0
  }
4177
0
  return 1;
4178
0
}
4179
4180
static int zend_jit_tail_handler(zend_jit_ctx *jit, const zend_op *opline)
4181
0
{
4182
0
  ir_ref ref;
4183
0
  zend_basic_block *bb;
4184
4185
0
  zend_jit_set_ip(jit, opline);
4186
0
  if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
4187
0
    if (opline->opcode == ZEND_DO_UCALL ||
4188
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
4189
0
        opline->opcode == ZEND_DO_FCALL ||
4190
0
        opline->opcode == ZEND_RETURN) {
4191
4192
      /* Use inlined HYBRID VM handler */
4193
0
      zend_vm_opcode_handler_t handler = opline->handler;
4194
0
      ir_TAILCALL(IR_VOID, ir_CONST_FUNC(handler));
4195
0
    } else {
4196
0
      zend_vm_opcode_handler_func_t handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(opline);
4197
0
      ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
4198
0
      ref = ir_LOAD_A(jit_IP(jit));
4199
0
      ir_TAILCALL(IR_VOID, ref);
4200
0
    }
4201
0
  } else {
4202
0
    zend_vm_opcode_handler_t handler = opline->handler;
4203
0
    if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
4204
0
      zend_jit_tailcall_handler(jit, ir_CONST_OPCODE_HANDLER_FUNC(handler));
4205
0
    } else if ((jit->ssa->cfg.flags & ZEND_FUNC_RECURSIVE_DIRECTLY)
4206
0
     && (opline->opcode == ZEND_CATCH
4207
0
      || opline->opcode == ZEND_FAST_CALL
4208
0
      || opline->opcode == ZEND_FAST_RET
4209
0
      || opline->opcode == ZEND_MATCH_ERROR
4210
0
      || opline->opcode == ZEND_THROW
4211
0
      || opline->opcode == ZEND_VERIFY_NEVER_TYPE)) {
4212
0
      ir_ref ip = ir_CALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
4213
0
      zend_jit_vm_enter(jit, ip);
4214
0
    } else {
4215
0
      ir_TAILCALL_2(IR_ADDR, ir_CONST_OPCODE_HANDLER_FUNC(handler), jit_FP(jit), jit_IP(jit));
4216
0
    }
4217
0
  }
4218
0
  if (jit->b >= 0) {
4219
0
    bb = &jit->ssa->cfg.blocks[jit->b];
4220
0
    if (bb->successors_count > 0
4221
0
     && (opline->opcode == ZEND_DO_FCALL
4222
0
      || opline->opcode == ZEND_DO_UCALL
4223
0
      || opline->opcode == ZEND_DO_FCALL_BY_NAME
4224
0
      || opline->opcode == ZEND_INCLUDE_OR_EVAL
4225
0
      || opline->opcode == ZEND_GENERATOR_CREATE
4226
0
      || opline->opcode == ZEND_YIELD
4227
0
      || opline->opcode == ZEND_YIELD_FROM
4228
0
      || opline->opcode == ZEND_FAST_CALL)) {
4229
      /* Add a fake control edge from UNREACHABLE to the following ENTRY */
4230
0
      int succ;
4231
4232
0
      if (bb->successors_count == 1) {
4233
0
        succ = bb->successors[0];
4234
0
        ZEND_ASSERT(jit->ssa->cfg.blocks[succ].flags & ZEND_BB_ENTRY);
4235
0
      } else {
4236
        /* Use only the following successor of FAST_CALL */
4237
0
        ZEND_ASSERT(opline->opcode == ZEND_FAST_CALL);
4238
0
        succ = jit->b + 1;
4239
        /* we need an entry */
4240
0
        jit->ssa->cfg.blocks[succ].flags |= ZEND_BB_ENTRY;
4241
0
      }
4242
0
      ref = jit->ctx.insns_count - 1;
4243
0
      ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_UNREACHABLE || jit->ctx.ir_base[ref].op == IR_RETURN);
4244
0
      ref = zend_jit_continue_entry(jit, ref, jit->ssa->cfg.blocks[succ].start);
4245
0
      _zend_jit_add_predecessor_ref(jit, succ, jit->b, ref);
4246
0
    }
4247
0
    jit->b = -1;
4248
0
    zend_jit_reset_last_valid_opline(jit);
4249
0
  }
4250
0
  return 1;
4251
0
}
4252
4253
static int zend_jit_call(zend_jit_ctx *jit, const zend_op *opline, unsigned int next_block)
4254
0
{
4255
0
  return zend_jit_tail_handler(jit, opline);
4256
0
}
4257
4258
static int zend_jit_spill_store(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info, bool set_type)
4259
0
{
4260
0
  ZEND_ASSERT(Z_MODE(src) == IS_REG);
4261
0
  ZEND_ASSERT(Z_MODE(dst) == IS_MEM_ZVAL);
4262
4263
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4264
0
    jit_set_Z_LVAL(jit, dst, zend_jit_use_reg(jit, src));
4265
0
    if (set_type &&
4266
0
        (Z_REG(dst) != ZREG_FP ||
4267
0
         !JIT_G(current_frame) ||
4268
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4269
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4270
0
    }
4271
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4272
0
    jit_set_Z_DVAL(jit, dst, zend_jit_use_reg(jit, src));
4273
0
    if (set_type &&
4274
0
        (Z_REG(dst) != ZREG_FP ||
4275
0
         !JIT_G(current_frame) ||
4276
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4277
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4278
0
    }
4279
0
  } else {
4280
0
    ZEND_UNREACHABLE();
4281
0
  }
4282
0
  return 1;
4283
0
}
4284
4285
static int zend_jit_spill_store_inv(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4286
0
{
4287
0
  ZEND_ASSERT(Z_MODE(src) == IS_REG);
4288
0
  ZEND_ASSERT(Z_MODE(dst) == IS_MEM_ZVAL);
4289
4290
0
  if (Z_LOAD(src) || Z_STORE(src)) {
4291
    /* it's not necessary to store register if it was previously loaded or already stored */
4292
0
    return 1;
4293
0
  }
4294
4295
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4296
0
    jit_set_Z_LVAL(jit, dst, zend_jit_use_reg(jit, src));
4297
0
    if (Z_REG(dst) != ZREG_FP || !JIT_G(current_frame)) {
4298
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4299
0
    } else if (STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG) {
4300
      /* invalidate memory type */
4301
0
      STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) = IS_UNKNOWN;
4302
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4303
0
    }
4304
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4305
0
    jit_set_Z_DVAL(jit, dst, zend_jit_use_reg(jit, src));
4306
0
    if (Z_REG(dst) != ZREG_FP || !JIT_G(current_frame)) {
4307
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4308
0
    } else if (STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE) {
4309
      /* invalidate memory type */
4310
0
      STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) = IS_UNKNOWN;
4311
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4312
0
    }
4313
0
  } else {
4314
0
    ZEND_UNREACHABLE();
4315
0
  }
4316
0
  return 1;
4317
0
}
4318
4319
static int zend_jit_load_reg(zend_jit_ctx *jit, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4320
0
{
4321
0
  ZEND_ASSERT(Z_MODE(src) == IS_MEM_ZVAL);
4322
0
  ZEND_ASSERT(Z_MODE(dst) == IS_REG);
4323
4324
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4325
0
    zend_jit_def_reg(jit, dst, jit_Z_LVAL(jit, src));
4326
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4327
0
    zend_jit_def_reg(jit, dst, jit_Z_DVAL(jit, src));
4328
0
  } else {
4329
0
    ZEND_UNREACHABLE();
4330
0
  }
4331
0
  return 1;
4332
0
}
4333
4334
static int zend_jit_store_var(zend_jit_ctx *jit, uint32_t info, int var, int ssa_var, bool set_type)
4335
0
{
4336
0
  zend_jit_addr src = ZEND_ADDR_REG(ssa_var);
4337
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4338
4339
0
  return zend_jit_spill_store(jit, src, dst, info, set_type);
4340
0
}
4341
4342
static int zend_jit_store_ref(zend_jit_ctx *jit, uint32_t info, int var, int32_t src, bool set_type)
4343
0
{
4344
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4345
4346
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4347
0
    jit_set_Z_LVAL(jit, dst, src);
4348
0
    if (set_type &&
4349
0
        (Z_REG(dst) != ZREG_FP ||
4350
0
         !JIT_G(current_frame) ||
4351
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4352
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4353
0
    }
4354
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4355
0
    jit_set_Z_DVAL(jit, dst, src);
4356
0
    if (set_type &&
4357
0
        (Z_REG(dst) != ZREG_FP ||
4358
0
         !JIT_G(current_frame) ||
4359
0
         STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4360
0
      jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4361
0
    }
4362
0
  } else {
4363
0
    ZEND_UNREACHABLE();
4364
0
  }
4365
0
  return 1;
4366
0
}
4367
4368
static ir_ref zend_jit_deopt_rload(zend_jit_ctx *jit, ir_type type, int32_t reg)
4369
0
{
4370
0
  ir_ref ref = jit->ctx.control;
4371
0
  ir_insn *insn;
4372
4373
0
  while (1) {
4374
0
    insn = &jit->ctx.ir_base[ref];
4375
0
    if (insn->op == IR_RLOAD && insn->op2 == reg) {
4376
0
      ZEND_ASSERT(insn->type == type);
4377
0
      return ref;
4378
0
    } else if (insn->op == IR_START) {
4379
0
      break;
4380
0
    }
4381
0
    ref = insn->op1;
4382
0
  }
4383
0
  return ir_RLOAD(type, reg);
4384
0
}
4385
4386
/* Same as zend_jit_deopt_rload(), but 'reg' may be spilled on C stack */
4387
static ir_ref zend_jit_deopt_rload_spilled(zend_jit_ctx *jit, ir_type type, int8_t reg, int32_t offset)
4388
0
{
4389
0
  ZEND_ASSERT(reg >= 0);
4390
4391
0
  if (IR_REG_SPILLED(reg)) {
4392
0
    return ir_LOAD(type, ir_ADD_OFFSET(zend_jit_deopt_rload(jit, type, IR_REG_NUM(reg)), offset));
4393
0
  } else {
4394
0
    return zend_jit_deopt_rload(jit, type, reg);
4395
0
  }
4396
0
}
4397
4398
static int zend_jit_store_const_long(zend_jit_ctx *jit, int var, zend_long val)
4399
0
{
4400
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4401
0
  ir_ref src = ir_CONST_LONG(val);
4402
4403
0
  if (jit->ra && jit->ra[var].ref == IR_NULL) {
4404
0
    zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4405
0
  }
4406
0
  jit_set_Z_LVAL(jit, dst, src);
4407
0
  jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4408
0
  return 1;
4409
0
}
4410
4411
static int zend_jit_store_const_double(zend_jit_ctx *jit, int var, double val)
4412
0
{
4413
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4414
0
  ir_ref src = ir_CONST_DOUBLE(val);
4415
4416
0
  if (jit->ra && jit->ra[var].ref == IR_NULL) {
4417
0
    zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4418
0
  }
4419
0
  jit_set_Z_DVAL(jit, dst, src);
4420
0
  jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4421
0
  return 1;
4422
0
}
4423
4424
static int zend_jit_store_type(zend_jit_ctx *jit, int var, uint8_t type)
4425
0
{
4426
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4427
4428
0
  ZEND_ASSERT(type <= IS_DOUBLE);
4429
0
  jit_set_Z_TYPE_INFO(jit, dst, type);
4430
0
  return 1;
4431
0
}
4432
4433
static int zend_jit_store_reg(zend_jit_ctx *jit, uint32_t info, int var, int8_t reg, bool in_mem, bool set_type)
4434
0
{
4435
0
  zend_jit_addr src;
4436
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4437
0
  ir_type type;
4438
4439
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4440
0
    type = IR_LONG;
4441
0
    src = zend_jit_deopt_rload(jit, type, reg);
4442
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4443
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4444
0
    } else if (!in_mem) {
4445
0
      jit_set_Z_LVAL(jit, dst, src);
4446
0
      if (set_type &&
4447
0
          (Z_REG(dst) != ZREG_FP ||
4448
0
           !JIT_G(current_frame) ||
4449
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4450
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4451
0
      }
4452
0
    }
4453
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4454
0
    type = IR_DOUBLE;
4455
0
    src = zend_jit_deopt_rload(jit, type, reg);
4456
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4457
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4458
0
    } else if (!in_mem) {
4459
0
      jit_set_Z_DVAL(jit, dst, src);
4460
0
      if (set_type &&
4461
0
          (Z_REG(dst) != ZREG_FP ||
4462
0
           !JIT_G(current_frame) ||
4463
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4464
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4465
0
      }
4466
0
    }
4467
0
  } else {
4468
0
    ZEND_UNREACHABLE();
4469
0
  }
4470
0
  return 1;
4471
0
}
4472
4473
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)
4474
0
{
4475
0
  zend_jit_addr src;
4476
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4477
4478
0
  if ((info & MAY_BE_ANY) == MAY_BE_LONG) {
4479
0
    src = ir_LOAD_L(ir_ADD_OFFSET(ir_RLOAD_A(reg), offset));
4480
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4481
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4482
0
    } else {
4483
0
      jit_set_Z_LVAL(jit, dst, src);
4484
0
      if (set_type &&
4485
0
          (Z_REG(dst) != ZREG_FP ||
4486
0
           !JIT_G(current_frame) ||
4487
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_LONG)) {
4488
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_LONG);
4489
0
      }
4490
0
    }
4491
0
  } else if ((info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4492
0
    src = ir_LOAD_D(ir_ADD_OFFSET(ir_RLOAD_A(reg), offset));
4493
0
    if (jit->ra && jit->ra[var].ref == IR_NULL) {
4494
0
      zend_jit_def_reg(jit, ZEND_ADDR_REG(var), src);
4495
0
    } else {
4496
0
      jit_set_Z_DVAL(jit, dst, src);
4497
0
      if (set_type &&
4498
0
          (Z_REG(dst) != ZREG_FP ||
4499
0
           !JIT_G(current_frame) ||
4500
0
           STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(dst))) != IS_DOUBLE)) {
4501
0
        jit_set_Z_TYPE_INFO(jit, dst, IS_DOUBLE);
4502
0
      }
4503
0
    }
4504
0
  } else {
4505
0
    ZEND_UNREACHABLE();
4506
0
  }
4507
0
  return 1;
4508
0
}
4509
4510
static int zend_jit_store_var_type(zend_jit_ctx *jit, int var, uint32_t type)
4511
0
{
4512
0
  zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4513
4514
0
  jit_set_Z_TYPE_INFO(jit, dst, type);
4515
0
  return 1;
4516
0
}
4517
4518
static int zend_jit_zval_try_addref(zend_jit_ctx *jit, zend_jit_addr var_addr)
4519
0
{
4520
0
  ir_ref if_refcounted, end1;
4521
4522
0
  if_refcounted = jit_if_REFCOUNTED(jit, var_addr);
4523
0
  ir_IF_FALSE(if_refcounted);
4524
0
  end1 = ir_END();
4525
0
  ir_IF_TRUE(if_refcounted);
4526
0
  jit_GC_ADDREF(jit, jit_Z_PTR(jit, var_addr));
4527
0
  ir_MERGE_WITH(end1);
4528
0
  return 1;
4529
0
}
4530
4531
static int zend_jit_store_var_if_necessary(zend_jit_ctx *jit, int var, zend_jit_addr src, uint32_t info)
4532
0
{
4533
0
  if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
4534
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4535
0
    return zend_jit_spill_store(jit, src, dst, info, true);
4536
0
  }
4537
0
  return 1;
4538
0
}
4539
4540
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)
4541
0
{
4542
0
  if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
4543
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4544
0
    bool set_type = true;
4545
4546
0
    if ((info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) ==
4547
0
        (old_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF))) {
4548
0
      if (Z_MODE(old) != IS_REG || Z_LOAD(old) || Z_STORE(old)) {
4549
0
        if (JIT_G(current_frame)) {
4550
0
          uint32_t mem_type = STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var));
4551
4552
0
          if (mem_type != IS_UNKNOWN
4553
0
           && (info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == (1 << mem_type)) {
4554
0
            set_type = false;
4555
0
          }
4556
0
        } else {
4557
0
          set_type = false;
4558
0
        }
4559
0
      }
4560
0
    }
4561
0
    return zend_jit_spill_store(jit, src, dst, info, set_type);
4562
0
  }
4563
0
  return 1;
4564
0
}
4565
4566
static int zend_jit_load_var(zend_jit_ctx *jit, uint32_t info, int var, int ssa_var)
4567
0
{
4568
0
  zend_jit_addr src = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
4569
0
  zend_jit_addr dst = ZEND_ADDR_REG(ssa_var);
4570
4571
0
  return zend_jit_load_reg(jit, src, dst, info);
4572
0
}
4573
4574
static int zend_jit_invalidate_var_if_necessary(zend_jit_ctx *jit, uint8_t op_type, zend_jit_addr addr, znode_op op)
4575
0
{
4576
0
  if ((op_type & (IS_TMP_VAR|IS_VAR)) && Z_MODE(addr) == IS_REG && !Z_LOAD(addr) && !Z_STORE(addr)) {
4577
    /* Invalidate operand type to prevent incorrect destuction by exception_handler_free_op1_op2() */
4578
0
    zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op.var);
4579
0
    jit_set_Z_TYPE_INFO(jit, dst, IS_UNDEF);
4580
0
  }
4581
0
  return 1;
4582
0
}
4583
4584
static int zend_jit_update_regs(zend_jit_ctx *jit, uint32_t var, zend_jit_addr src, zend_jit_addr dst, uint32_t info)
4585
0
{
4586
0
  if (!zend_jit_same_addr(src, dst)) {
4587
0
    if (Z_MODE(src) == IS_REG) {
4588
0
      if (Z_MODE(dst) == IS_REG) {
4589
0
        zend_jit_def_reg(jit, dst, zend_jit_use_reg(jit, src));
4590
0
        if (!Z_LOAD(src) && !Z_STORE(src) && Z_STORE(dst)) {
4591
0
          zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4592
4593
0
          if (!zend_jit_spill_store(jit, dst, var_addr, info,
4594
0
              JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4595
0
              JIT_G(current_frame) == NULL ||
4596
0
              STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4597
0
              (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4598
0
          )) {
4599
0
            return 0;
4600
0
          }
4601
0
        }
4602
0
      } else if (Z_MODE(dst) == IS_MEM_ZVAL) {
4603
0
        if (!Z_LOAD(src) && !Z_STORE(src)) {
4604
0
          if (!zend_jit_spill_store(jit, src, dst, info,
4605
0
              JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4606
0
              JIT_G(current_frame) == NULL ||
4607
0
              STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4608
0
              (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4609
0
          )) {
4610
0
            return 0;
4611
0
          }
4612
0
        }
4613
0
      } else {
4614
0
        ZEND_UNREACHABLE();
4615
0
      }
4616
0
    } else if (Z_MODE(src) == IS_MEM_ZVAL) {
4617
0
      if (Z_MODE(dst) == IS_REG) {
4618
0
        if (!zend_jit_load_reg(jit, src, dst, info)) {
4619
0
          return 0;
4620
0
        }
4621
0
      } else {
4622
0
        ZEND_UNREACHABLE();
4623
0
      }
4624
0
    } else {
4625
0
      ZEND_UNREACHABLE();
4626
0
    }
4627
0
  } else if (Z_MODE(dst) == IS_REG && Z_STORE(dst)) {
4628
0
    dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
4629
0
    if (!zend_jit_spill_store(jit, src, dst, info,
4630
0
        JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
4631
0
        JIT_G(current_frame) == NULL ||
4632
0
        STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var)) == IS_UNKNOWN ||
4633
0
        (1 << STACK_MEM_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(var))) != (info & MAY_BE_ANY)
4634
0
    )) {
4635
0
      return 0;
4636
0
    }
4637
0
  }
4638
0
  return 1;
4639
0
}
4640
4641
struct jit_observer_fcall_is_unobserved_data {
4642
  ir_ref if_unobserved;
4643
  ir_ref ir_end_inputs;
4644
};
4645
4646
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) {
4647
0
  ir_ref run_time_cache;
4648
0
  struct jit_observer_fcall_is_unobserved_data data = { .ir_end_inputs = IR_UNUSED };
4649
0
  if (func) {
4650
0
    ZEND_ASSERT((func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0);
4651
0
  } else {
4652
    // JIT: if (function->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) {
4653
0
    ZEND_ASSERT(rx != IR_UNUSED);
4654
0
    ir_ref if_trampoline_or_generator = ir_IF(ir_AND_U32(
4655
0
      ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
4656
0
      ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)));
4657
0
    ir_IF_TRUE(if_trampoline_or_generator);
4658
0
    ir_END_list(data.ir_end_inputs);
4659
0
    ir_IF_FALSE(if_trampoline_or_generator);
4660
0
  }
4661
0
  if (func && (func->common.fn_flags & ZEND_ACC_CLOSURE) == 0 && ZEND_MAP_PTR_IS_OFFSET(func->common.run_time_cache)) {
4662
    // JIT: ZEND_MAP_PTR_GET_IMM(func->common.runtime_cache)
4663
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)));
4664
0
#ifndef ZTS
4665
0
  } else if (func && rx == IS_UNUSED) { // happens for internal functions only
4666
0
    ZEND_ASSERT(!ZEND_USER_CODE(func->type));
4667
0
    run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_CONST_ADDR(func), offsetof(zend_op_array, run_time_cache__ptr)));
4668
0
#endif
4669
0
  } else {
4670
    // Closures may be duplicated and have a different runtime cache. Use the regular run_time_cache access pattern for these
4671
0
    if (func && ZEND_USER_CODE(func->type)) { // not a closure and definitely not an internal function
4672
0
      run_time_cache = ir_LOAD_A(jit_CALL(rx, run_time_cache));
4673
0
    } else {
4674
      // JIT: ZEND_MAP_PTR_GET(func->common.runtime_cache)
4675
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)));
4676
0
      ir_ref if_odd = ir_IF(ir_AND_A(run_time_cache, ir_CONST_ADDR(1)));
4677
0
      ir_IF_TRUE(if_odd);
4678
4679
0
      ir_ref run_time_cache2 = ir_LOAD_A(ir_ADD_A(run_time_cache, ir_LOAD_A(jit_CG(map_ptr_base))));
4680
4681
0
      ir_ref if_odd_end = ir_END();
4682
0
      ir_IF_FALSE(if_odd);
4683
4684
      // JIT: if (func->common.runtime_cache != NULL) {
4685
0
      ir_ref if_rt_cache = ir_IF(ir_EQ(run_time_cache, IR_NULL));
4686
0
      ir_IF_TRUE(if_rt_cache);
4687
0
      ir_END_list(data.ir_end_inputs);
4688
0
      ir_IF_FALSE(if_rt_cache);
4689
4690
0
      ir_MERGE_WITH(if_odd_end);
4691
0
      run_time_cache = ir_PHI_2(IR_ADDR, run_time_cache, run_time_cache2);
4692
0
    }
4693
0
  }
4694
  // JIT: observer_handler = runtime_cache + ZEND_OBSERVER_HANDLE(function)
4695
0
  if (func) {
4696
0
    *observer_handler = ir_ADD_OFFSET(run_time_cache, ZEND_OBSERVER_HANDLE(func) * sizeof(void *));
4697
0
  } else {
4698
    // JIT: (func->type == ZEND_INTERNAL_FUNCTION ? zend_observer_fcall_internal_function_extension : zend_observer_fcall_op_array_extension) * sizeof(void *)
4699
0
    ir_ref tmp = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_function, type)));
4700
0
    ir_ref if_internal_func = ir_IF(ir_AND_U8(tmp, ir_CONST_U8(ZEND_INTERNAL_FUNCTION)));
4701
0
    ir_IF_TRUE(if_internal_func);
4702
4703
0
    ir_ref observer_handler_internal = ir_ADD_OFFSET(run_time_cache, zend_observer_fcall_internal_function_extension * sizeof(void *));
4704
4705
0
    ir_ref if_internal_func_end = ir_END();
4706
0
    ir_IF_FALSE(if_internal_func);
4707
4708
0
    ir_ref observer_handler_user = ir_ADD_OFFSET(run_time_cache, zend_observer_fcall_op_array_extension * sizeof(void *));
4709
4710
0
    ir_MERGE_WITH(if_internal_func_end);
4711
0
    *observer_handler = ir_PHI_2(IR_ADDR, observer_handler_internal, observer_handler_user);
4712
0
  }
4713
4714
  // JIT: if (*observer_handler == ZEND_OBSERVER_NONE_OBSERVED) {
4715
0
  data.if_unobserved = ir_IF(ir_EQ(ir_LOAD_A(*observer_handler), ir_CONST_ADDR(ZEND_OBSERVER_NONE_OBSERVED)));
4716
0
  ir_IF_FALSE(data.if_unobserved);
4717
0
  return data;
4718
0
}
4719
4720
/* For frameless the true branch of if_unobserved is used and this function not called. */
4721
0
static void jit_observer_fcall_is_unobserved_end(zend_jit_ctx *jit, struct jit_observer_fcall_is_unobserved_data *data) {
4722
0
  ir_END_list(data->ir_end_inputs);
4723
0
  ir_IF_TRUE(data->if_unobserved);
4724
0
  ir_END_list(data->ir_end_inputs);
4725
0
  ir_MERGE_list(data->ir_end_inputs);
4726
0
}
4727
4728
0
static void jit_observer_fcall_begin(zend_jit_ctx *jit, ir_ref rx, ir_ref observer_handler) {
4729
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_observer_fcall_begin_prechecked), rx, observer_handler);
4730
0
}
4731
4732
0
static void jit_observer_fcall_end(zend_jit_ctx *jit, ir_ref rx, ir_ref res_ref) {
4733
  // JIT: if (execute_data == EG(current_observed_frame)) {
4734
0
  ir_ref has_end_observer = ir_IF(ir_EQ(rx, ir_LOAD_A(jit_EG(current_observed_frame))));
4735
0
  ir_IF_TRUE(has_end_observer);
4736
0
  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_observer_fcall_end_prechecked),
4737
0
    rx, res_ref);
4738
0
  ir_MERGE_WITH_EMPTY_FALSE(has_end_observer);
4739
0
}
4740
4741
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)
4742
0
{
4743
0
  ir_ref if_long = IR_UNUSED;
4744
0
  ir_ref op1_lval_ref = IR_UNUSED;
4745
0
  ir_ref ref;
4746
0
  ir_op op;
4747
4748
0
  if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)-MAY_BE_LONG)) {
4749
0
    if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
4750
0
    ir_IF_TRUE(if_long);
4751
0
  }
4752
0
  if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
4753
0
    op1_lval_ref = jit_Z_LVAL(jit, op1_addr);
4754
0
    jit_set_Z_LVAL(jit, res_addr, op1_lval_ref);
4755
0
    if (Z_MODE(res_addr) != IS_REG) {
4756
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4757
0
    }
4758
0
  }
4759
0
  if (Z_MODE(op1_def_addr) == IS_MEM_ZVAL
4760
0
   && Z_MODE(op1_addr) == IS_REG
4761
0
   && !Z_LOAD(op1_addr)
4762
0
   && !Z_STORE(op1_addr)) {
4763
0
    jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_LONG);
4764
0
  }
4765
0
  if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4766
0
    op = may_overflow ? IR_ADD_OV : IR_ADD;
4767
0
  } else {
4768
0
    op = may_overflow ? IR_SUB_OV : IR_SUB;
4769
0
  }
4770
0
  if (!op1_lval_ref) {
4771
0
    op1_lval_ref = jit_Z_LVAL(jit, op1_addr);
4772
0
  }
4773
0
  ref = ir_BINARY_OP_L(op, op1_lval_ref, ir_CONST_LONG(1));
4774
0
  if (op1_def_info & MAY_BE_LONG) {
4775
0
    jit_set_Z_LVAL(jit, op1_def_addr, ref);
4776
0
  }
4777
0
  if (may_overflow &&
4778
0
      (((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)) ||
4779
0
       ((opline->result_type != IS_UNUSED && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD))))) {
4780
0
    int32_t exit_point;
4781
0
    const void *exit_addr;
4782
0
    zend_jit_trace_stack *stack;
4783
0
    uint32_t old_op1_info, old_res_info = 0;
4784
4785
0
    stack = JIT_G(current_frame)->stack;
4786
0
    old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
4787
0
    SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_DOUBLE, 0);
4788
0
    if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4789
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4790
0
    } else {
4791
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4792
0
    }
4793
0
    if (opline->result_type != IS_UNUSED) {
4794
0
      old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
4795
0
      if (opline->opcode == ZEND_PRE_INC) {
4796
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
4797
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4798
0
      } else if (opline->opcode == ZEND_PRE_DEC) {
4799
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
4800
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4801
0
      } else if (opline->opcode == ZEND_POST_INC) {
4802
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4803
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_LONG(ZEND_LONG_MAX));
4804
0
      } else if (opline->opcode == ZEND_POST_DEC) {
4805
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4806
0
        SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_LONG(ZEND_LONG_MIN));
4807
0
      }
4808
0
    }
4809
4810
0
    exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
4811
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4812
0
    ir_GUARD_NOT(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
4813
4814
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4815
0
        opline->result_type != IS_UNUSED) {
4816
0
      jit_set_Z_LVAL(jit, res_addr, ref);
4817
0
      if (Z_MODE(res_addr) != IS_REG) {
4818
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4819
0
      }
4820
0
    }
4821
4822
0
    SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
4823
0
    if (opline->result_type != IS_UNUSED) {
4824
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
4825
0
    }
4826
0
  } else if (may_overflow) {
4827
0
    ir_ref if_overflow;
4828
0
    ir_ref merge_inputs = IR_UNUSED;
4829
4830
0
    if (((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_DOUBLE|MAY_BE_GUARD))
4831
0
     || (opline->result_type != IS_UNUSED && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) ==  (MAY_BE_DOUBLE|MAY_BE_GUARD))) {
4832
0
      int32_t exit_point;
4833
0
      const void *exit_addr;
4834
0
      zend_jit_trace_stack *stack;
4835
0
      uint32_t old_res_info = 0, old_op1_info = 0;
4836
4837
0
      stack = JIT_G(current_frame)->stack;
4838
0
      if (opline->result_type != IS_UNUSED) {
4839
0
        old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
4840
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_LONG, 0);
4841
0
        if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) {
4842
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ref);
4843
0
        } else {
4844
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), op1_lval_ref);
4845
0
        }
4846
0
      }
4847
0
      old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
4848
0
      SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_LONG, 0);
4849
0
      SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var), ref);
4850
4851
0
      exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
4852
0
      exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4853
0
      ir_GUARD(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
4854
4855
0
      if (opline->result_type != IS_UNUSED) {
4856
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
4857
0
      }
4858
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
4859
0
    } else {
4860
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
4861
0
      ir_IF_FALSE(if_overflow);
4862
0
      if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4863
0
          opline->result_type != IS_UNUSED) {
4864
0
        jit_set_Z_LVAL(jit, res_addr, ref);
4865
0
        if (Z_MODE(res_addr) != IS_REG) {
4866
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4867
0
        }
4868
0
      }
4869
0
      ir_END_list(merge_inputs);
4870
4871
      /* overflow => cold path */
4872
0
      ir_IF_TRUE_cold(if_overflow);
4873
0
    }
4874
4875
0
    if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
4876
0
      if (Z_MODE(op1_def_addr) == IS_REG) {
4877
0
        jit_set_Z_DVAL(jit, op1_def_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4878
0
      } else {
4879
#if SIZEOF_ZEND_LONG == 4
4880
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0));
4881
        jit_set_Z_W2(jit, op1_def_addr, ir_CONST_U32(0x41e00000));
4882
#else
4883
0
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0x43e0000000000000));
4884
0
#endif
4885
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_DOUBLE);
4886
0
      }
4887
0
    } else {
4888
0
      if (Z_MODE(op1_def_addr) == IS_REG) {
4889
0
        jit_set_Z_DVAL(jit, op1_def_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4890
0
      } else {
4891
#if SIZEOF_ZEND_LONG == 4
4892
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0x00200000));
4893
        jit_set_Z_W2(jit, op1_def_addr, ir_CONST_U32(0xc1e00000));
4894
#else
4895
0
        jit_set_Z_LVAL(jit, op1_def_addr, ir_CONST_LONG(0xc3e0000000000000));
4896
0
#endif
4897
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_DOUBLE);
4898
0
      }
4899
0
    }
4900
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4901
0
        opline->result_type != IS_UNUSED) {
4902
0
      if (opline->opcode == ZEND_PRE_INC) {
4903
0
        if (Z_MODE(res_addr) == IS_REG) {
4904
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
4905
0
        } else {
4906
#if SIZEOF_ZEND_LONG == 4
4907
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
4908
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
4909
#else
4910
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
4911
0
#endif
4912
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
4913
0
        }
4914
0
      } else {
4915
0
        if (Z_MODE(res_addr) == IS_REG) {
4916
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
4917
0
        } else {
4918
#if SIZEOF_ZEND_LONG == 4
4919
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
4920
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
4921
#else
4922
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
4923
0
#endif
4924
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
4925
0
        }
4926
0
      }
4927
0
    }
4928
4929
0
    if (merge_inputs) {
4930
0
      ir_END_list(merge_inputs);
4931
0
      ir_MERGE_list(merge_inputs);
4932
0
    }
4933
0
  } else {
4934
0
    if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
4935
0
        opline->result_type != IS_UNUSED) {
4936
0
      jit_set_Z_LVAL(jit, res_addr, ref);
4937
0
      if (Z_MODE(res_addr) != IS_REG) {
4938
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
4939
0
      }
4940
0
    }
4941
0
  }
4942
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
4943
0
    ir_ref merge_inputs = ir_END();
4944
4945
    /* !is_long => cold path */
4946
0
    ir_IF_FALSE_cold(if_long);
4947
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
4948
0
      jit_SET_EX_OPLINE(jit, opline);
4949
0
      if (op1_info & MAY_BE_UNDEF) {
4950
0
        ir_ref if_def;
4951
4952
0
        if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
4953
0
        ir_IF_FALSE_cold(if_def);
4954
4955
        // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
4956
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
4957
4958
0
        jit_set_Z_TYPE_INFO(jit, op1_def_addr, IS_NULL);
4959
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def);
4960
4961
0
        op1_info |= MAY_BE_NULL;
4962
0
      }
4963
4964
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
4965
4966
0
      if (op1_info & MAY_BE_REF) {
4967
0
        ir_ref if_ref, if_typed, func, ref2, arg2;
4968
4969
0
        if_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
4970
0
        ir_IF_TRUE(if_ref);
4971
0
        ref2 = jit_Z_PTR_ref(jit, ref);
4972
4973
0
        if_typed = jit_if_TYPED_REF(jit, ref2);
4974
0
        ir_IF_TRUE(if_typed);
4975
4976
0
        if (RETURN_VALUE_USED(opline)) {
4977
0
          ZEND_ASSERT(Z_MODE(res_addr) != IS_REG);
4978
0
          arg2 = jit_ZVAL_ADDR(jit, res_addr);
4979
0
        } else {
4980
0
          arg2 = IR_NULL;
4981
0
        }
4982
0
        if (opline->opcode == ZEND_PRE_INC) {
4983
0
          func = ir_CONST_FC_FUNC(zend_jit_pre_inc_typed_ref);
4984
0
        } else if (opline->opcode == ZEND_PRE_DEC) {
4985
0
          func = ir_CONST_FC_FUNC(zend_jit_pre_dec_typed_ref);
4986
0
        } else if (opline->opcode == ZEND_POST_INC) {
4987
0
          func = ir_CONST_FC_FUNC(zend_jit_post_inc_typed_ref);
4988
0
        } else if (opline->opcode == ZEND_POST_DEC) {
4989
0
          func = ir_CONST_FC_FUNC(zend_jit_post_dec_typed_ref);
4990
0
        } else {
4991
0
          ZEND_UNREACHABLE();
4992
0
        }
4993
4994
0
        ir_CALL_2(IR_VOID, func, ref2, arg2);
4995
0
        zend_jit_check_exception(jit);
4996
0
        ir_END_list(merge_inputs);
4997
4998
0
        ir_IF_FALSE(if_typed);
4999
0
        ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
5000
0
        ir_MERGE_WITH_EMPTY_FALSE(if_ref);
5001
0
        ref = ir_PHI_2(IR_ADDR, ref2, ref);
5002
0
      }
5003
5004
0
      if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
5005
0
        jit_ZVAL_COPY(jit,
5006
0
          res_addr,
5007
0
          res_use_info,
5008
0
          ZEND_ADDR_REF_ZVAL(ref), op1_info, true);
5009
0
      }
5010
0
      if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
5011
0
        if (opline->opcode == ZEND_PRE_INC && opline->result_type != IS_UNUSED) {
5012
0
          ir_ref arg2 = jit_ZVAL_ADDR(jit, res_addr);
5013
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_inc), ref, arg2);
5014
0
        } else {
5015
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(increment_function), ref);
5016
0
        }
5017
0
      } else {
5018
0
        if (opline->opcode == ZEND_PRE_DEC && opline->result_type != IS_UNUSED) {
5019
0
          ir_ref arg2 = jit_ZVAL_ADDR(jit, res_addr);
5020
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_dec), ref, arg2);
5021
0
        } else {
5022
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(decrement_function), ref);
5023
0
        }
5024
0
      }
5025
0
      if (may_throw) {
5026
0
        zend_jit_check_exception(jit);
5027
0
      }
5028
0
    } else {
5029
0
      ref = jit_Z_DVAL(jit, op1_addr);
5030
0
      if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) {
5031
0
        jit_set_Z_DVAL(jit, res_addr, ref);
5032
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5033
0
      }
5034
0
      if (opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_POST_INC) {
5035
0
        op = IR_ADD;
5036
0
      } else {
5037
0
        op = IR_SUB;
5038
0
      }
5039
0
      ref = ir_BINARY_OP_D(op, ref, ir_CONST_DOUBLE(1.0));
5040
0
      jit_set_Z_DVAL(jit, op1_def_addr, ref);
5041
0
      if ((opline->opcode == ZEND_PRE_INC || opline->opcode == ZEND_PRE_DEC) &&
5042
0
          opline->result_type != IS_UNUSED) {
5043
0
        jit_set_Z_DVAL(jit, res_addr, ref);
5044
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5045
0
      }
5046
0
    }
5047
0
    ir_END_list(merge_inputs);
5048
0
    ir_MERGE_list(merge_inputs);
5049
0
  }
5050
0
  if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_def_addr, op1_def_info, op1_addr, op1_info)) {
5051
0
    return 0;
5052
0
  }
5053
0
  if (opline->result_type != IS_UNUSED) {
5054
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
5055
0
      return 0;
5056
0
    }
5057
0
  }
5058
0
  return 1;
5059
0
}
5060
5061
static int zend_jit_math_long_long(zend_jit_ctx   *jit,
5062
                                   const zend_op  *opline,
5063
                                   uint8_t         opcode,
5064
                                   zend_jit_addr   op1_addr,
5065
                                   zend_jit_addr   op2_addr,
5066
                                   zend_jit_addr   res_addr,
5067
                                   uint32_t        res_info,
5068
                                   uint32_t        res_use_info,
5069
                                   int             may_overflow)
5070
0
{
5071
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5072
0
  ir_op op;
5073
0
  ir_ref op1, op2, ref, if_overflow = IR_UNUSED;
5074
5075
0
  if (opcode == ZEND_ADD) {
5076
0
    op = may_overflow ? IR_ADD_OV : IR_ADD;
5077
0
  } else if (opcode == ZEND_SUB) {
5078
0
    op = may_overflow ? IR_SUB_OV : IR_SUB;
5079
0
  } else if (opcode == ZEND_MUL) {
5080
0
    op = may_overflow ? IR_MUL_OV : IR_MUL;
5081
0
  } else {
5082
0
    ZEND_UNREACHABLE();
5083
0
  }
5084
0
  op1 = jit_Z_LVAL(jit, op1_addr);
5085
0
  op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5086
0
  ref = ir_BINARY_OP_L(op, op1, op2);
5087
5088
0
  if (may_overflow) {
5089
0
    if (res_info & MAY_BE_GUARD) {
5090
0
      if ((res_info & MAY_BE_ANY) == MAY_BE_LONG) {
5091
0
        zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
5092
0
        uint32_t old_res_info;
5093
0
        int32_t exit_point;
5094
0
        const void *exit_addr;
5095
5096
0
        if (opline->opcode == ZEND_ADD
5097
0
         && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5098
0
          old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5099
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
5100
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
5101
0
          exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
5102
0
          SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
5103
0
        } else if (opline->opcode == ZEND_SUB
5104
0
         && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5105
0
          old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5106
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
5107
0
          SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
5108
0
          exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
5109
0
          SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
5110
0
        } else {
5111
0
          exit_point = zend_jit_trace_get_exit_point(opline, 0);
5112
0
        }
5113
5114
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5115
0
        if (!exit_addr) {
5116
0
          return 0;
5117
0
        }
5118
0
        ir_GUARD_NOT(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
5119
0
        may_overflow = 0;
5120
0
      } else if ((res_info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
5121
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
5122
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5123
5124
0
        if (!exit_addr) {
5125
0
          return 0;
5126
0
        }
5127
0
        ir_GUARD(ir_OVERFLOW(ref), ir_CONST_ADDR(exit_addr));
5128
0
      } else {
5129
0
        ZEND_UNREACHABLE();
5130
0
      }
5131
0
    } else {
5132
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
5133
0
      ir_IF_FALSE(if_overflow);
5134
0
    }
5135
0
  }
5136
5137
0
  if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5138
0
    jit_set_Z_LVAL(jit, res_addr, ref);
5139
5140
0
    if (Z_MODE(res_addr) != IS_REG) {
5141
0
      if (!zend_jit_same_addr(op1_addr, res_addr)) {
5142
0
        if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_LONG) {
5143
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
5144
0
        }
5145
0
      }
5146
0
    }
5147
0
  }
5148
5149
0
  if (may_overflow) {
5150
0
    ir_ref fast_path = IR_UNUSED;
5151
5152
0
    if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5153
0
      fast_path = ir_END();
5154
0
      ir_IF_TRUE_cold(if_overflow);
5155
0
    }
5156
0
    if (opcode == ZEND_ADD) {
5157
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5158
0
        if (Z_MODE(res_addr) == IS_REG) {
5159
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MAX + 1.0));
5160
0
        } else {
5161
#if SIZEOF_ZEND_LONG == 4
5162
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
5163
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
5164
#else
5165
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
5166
0
#endif
5167
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5168
0
        }
5169
0
        if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5170
0
          ir_MERGE_WITH(fast_path);
5171
0
        }
5172
0
        return 1;
5173
0
      }
5174
0
      op = IR_ADD;
5175
0
    } else if (opcode == ZEND_SUB) {
5176
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1) {
5177
0
        if (Z_MODE(res_addr) == IS_REG) {
5178
0
          jit_set_Z_DVAL(jit, res_addr, ir_CONST_DOUBLE((double)ZEND_LONG_MIN - 1.0));
5179
0
        } else {
5180
#if SIZEOF_ZEND_LONG == 4
5181
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
5182
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
5183
#else
5184
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
5185
0
#endif
5186
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5187
0
        }
5188
0
        if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5189
0
          ir_MERGE_WITH(fast_path);
5190
0
        }
5191
0
        return 1;
5192
0
      }
5193
0
      op = IR_SUB;
5194
0
    } else if (opcode == ZEND_MUL) {
5195
0
      op = IR_MUL;
5196
0
    } else {
5197
0
      ZEND_UNREACHABLE();
5198
0
    }
5199
0
#if 1
5200
    /* reload */
5201
0
    op1 = jit_Z_LVAL(jit, op1_addr);
5202
0
    op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5203
0
#endif
5204
0
#if 1
5205
    /* disable CSE */
5206
0
    ir_ref old_cse_limit = jit->ctx.fold_cse_limit;
5207
0
    jit->ctx.fold_cse_limit = 0x7fffffff;
5208
0
#endif
5209
0
    op1 = ir_INT2D(op1);
5210
0
    op2 = ir_INT2D(op2);
5211
0
#if 1
5212
0
    jit->ctx.fold_cse_limit = old_cse_limit;
5213
0
#endif
5214
0
    ref = ir_BINARY_OP_D(op, op1, op2);
5215
0
    jit_set_Z_DVAL(jit, res_addr, ref);
5216
0
    if (Z_MODE(res_addr) != IS_REG) {
5217
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5218
0
    }
5219
0
    if ((res_info & MAY_BE_ANY) != MAY_BE_DOUBLE) {
5220
0
      ir_MERGE_WITH(fast_path);
5221
0
    }
5222
0
  }
5223
5224
0
  return 1;
5225
0
}
5226
5227
static int zend_jit_math_long_double(zend_jit_ctx   *jit,
5228
                                     uint8_t         opcode,
5229
                                     zend_jit_addr   op1_addr,
5230
                                     zend_jit_addr   op2_addr,
5231
                                     zend_jit_addr   res_addr,
5232
                                     uint32_t        res_use_info)
5233
0
{
5234
0
  ir_op op;
5235
0
  ir_ref op1, op2, ref;
5236
5237
0
  if (opcode == ZEND_ADD) {
5238
0
    op = IR_ADD;
5239
0
  } else if (opcode == ZEND_SUB) {
5240
0
    op = IR_SUB;
5241
0
  } else if (opcode == ZEND_MUL) {
5242
0
    op = IR_MUL;
5243
0
  } else if (opcode == ZEND_DIV) {
5244
0
    op = IR_DIV;
5245
0
  } else {
5246
0
    ZEND_UNREACHABLE();
5247
0
  }
5248
0
  op1 = jit_Z_LVAL(jit, op1_addr);
5249
0
  op2 = jit_Z_DVAL(jit, op2_addr);
5250
0
  ref = ir_BINARY_OP_D(op, ir_INT2D(op1), op2);
5251
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5252
5253
0
  if (Z_MODE(res_addr) != IS_REG) {
5254
0
    if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5255
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5256
0
    }
5257
0
  }
5258
0
  return 1;
5259
0
}
5260
5261
static int zend_jit_math_double_long(zend_jit_ctx   *jit,
5262
                                     uint8_t         opcode,
5263
                                     zend_jit_addr   op1_addr,
5264
                                     zend_jit_addr   op2_addr,
5265
                                     zend_jit_addr   res_addr,
5266
                                     uint32_t        res_use_info)
5267
0
{
5268
0
  ir_op op;
5269
0
  ir_ref op1, op2, ref;
5270
5271
0
  if (opcode == ZEND_ADD) {
5272
0
    op = IR_ADD;
5273
0
  } else if (opcode == ZEND_SUB) {
5274
0
    op = IR_SUB;
5275
0
  } else if (opcode == ZEND_MUL) {
5276
0
    op = IR_MUL;
5277
0
  } else if (opcode == ZEND_DIV) {
5278
0
    op = IR_DIV;
5279
0
  } else {
5280
0
    ZEND_UNREACHABLE();
5281
0
  }
5282
0
  op1 = jit_Z_DVAL(jit, op1_addr);
5283
0
  op2 = jit_Z_LVAL(jit, op2_addr);
5284
0
  ref = ir_BINARY_OP_D(op, op1, ir_INT2D(op2));
5285
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5286
5287
0
  if (Z_MODE(res_addr) != IS_REG) {
5288
0
    if (!zend_jit_same_addr(op1_addr, res_addr)) {
5289
0
      if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5290
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5291
0
      }
5292
0
    }
5293
0
  }
5294
0
  return 1;
5295
0
}
5296
5297
static int zend_jit_math_double_double(zend_jit_ctx   *jit,
5298
                                       uint8_t         opcode,
5299
                                       zend_jit_addr   op1_addr,
5300
                                       zend_jit_addr   op2_addr,
5301
                                       zend_jit_addr   res_addr,
5302
                                       uint32_t        res_use_info)
5303
0
{
5304
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5305
0
  ir_op op;
5306
0
  ir_ref op1, op2, ref;
5307
5308
0
  if (opcode == ZEND_ADD) {
5309
0
    op = IR_ADD;
5310
0
  } else if (opcode == ZEND_SUB) {
5311
0
    op = IR_SUB;
5312
0
  } else if (opcode == ZEND_MUL) {
5313
0
    op = IR_MUL;
5314
0
  } else if (opcode == ZEND_DIV) {
5315
0
    op = IR_DIV;
5316
0
  } else {
5317
0
    ZEND_UNREACHABLE();
5318
0
  }
5319
0
  op1 = jit_Z_DVAL(jit, op1_addr);
5320
0
  op2 = (same_ops) ? op1 : jit_Z_DVAL(jit, op2_addr);
5321
0
  ref = ir_BINARY_OP_D(op, op1, op2);
5322
0
  jit_set_Z_DVAL(jit, res_addr, ref);
5323
5324
0
  if (Z_MODE(res_addr) != IS_REG) {
5325
0
    if (!zend_jit_same_addr(op1_addr, res_addr)) {
5326
0
      if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_DOUBLE) {
5327
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
5328
0
      }
5329
0
    }
5330
0
  }
5331
0
  return 1;
5332
0
}
5333
5334
static int zend_jit_math_helper(zend_jit_ctx   *jit,
5335
                                const zend_op  *opline,
5336
                                uint8_t         opcode,
5337
                                uint8_t         op1_type,
5338
                                znode_op        op1,
5339
                                zend_jit_addr   op1_addr,
5340
                                uint32_t        op1_info,
5341
                                uint8_t         op2_type,
5342
                                znode_op        op2,
5343
                                zend_jit_addr   op2_addr,
5344
                                uint32_t        op2_info,
5345
                                uint32_t        res_var,
5346
                                zend_jit_addr   res_addr,
5347
                                uint32_t        res_info,
5348
                                uint32_t        res_use_info,
5349
                                int             may_overflow,
5350
                                int             may_throw)
5351
0
{
5352
0
  ir_ref if_op1_long = IR_UNUSED;
5353
0
  ir_ref if_op1_double = IR_UNUSED;
5354
0
  ir_ref if_op2_double = IR_UNUSED;
5355
0
  ir_ref if_op1_long_op2_long = IR_UNUSED;
5356
0
  ir_ref if_op1_long_op2_double = IR_UNUSED;
5357
0
  ir_ref if_op1_double_op2_double = IR_UNUSED;
5358
0
  ir_ref if_op1_double_op2_long = IR_UNUSED;
5359
0
  ir_ref slow_inputs = IR_UNUSED;
5360
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5361
0
  ir_refs *end_inputs;
5362
0
  ir_refs *res_inputs;
5363
5364
0
  ir_refs_init(end_inputs, 6);
5365
0
  ir_refs_init(res_inputs, 6);
5366
5367
0
  if (Z_MODE(op1_addr) == IS_REG) {
5368
0
    if (!has_concrete_type(op2_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
5369
      /* Force load */
5370
0
      zend_jit_use_reg(jit, op1_addr);
5371
0
    }
5372
0
  } else if (Z_MODE(op2_addr) == IS_REG) {
5373
0
    if (!has_concrete_type(op1_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
5374
      /* Force load */
5375
0
      zend_jit_use_reg(jit, op2_addr);
5376
0
    }
5377
0
  }
5378
5379
0
  if (Z_MODE(res_addr) == IS_REG) {
5380
0
    jit->delay_var = Z_SSA_VAR(res_addr);
5381
0
    jit->delay_refs = res_inputs;
5382
0
  }
5383
5384
0
  if ((res_info & MAY_BE_GUARD) && (res_info & MAY_BE_LONG) && (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG)) {
5385
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_LONG)) {
5386
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5387
0
      ir_IF_TRUE(if_op1_long);
5388
0
    }
5389
0
    if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_LONG))) {
5390
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5391
0
      ir_IF_TRUE(if_op1_long_op2_long);
5392
0
    }
5393
0
    if (!zend_jit_math_long_long(jit, opline, opcode, op1_addr, op2_addr, res_addr, res_info, res_use_info, may_overflow)) {
5394
0
      return 0;
5395
0
    }
5396
0
    ir_refs_add(end_inputs, ir_END());
5397
0
    if (if_op1_long) {
5398
0
      ir_IF_FALSE_cold(if_op1_long);
5399
0
      ir_END_list(slow_inputs);
5400
0
    }
5401
0
    if (if_op1_long_op2_long) {
5402
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
5403
0
      ir_END_list(slow_inputs);
5404
0
    }
5405
0
  } else if ((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (res_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
5406
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_LONG)) {
5407
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5408
0
      ir_IF_TRUE(if_op1_long);
5409
0
    }
5410
0
    if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_LONG))) {
5411
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5412
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
5413
0
      if (op2_info & MAY_BE_DOUBLE) {
5414
0
        if (op2_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5415
0
          if_op1_long_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5416
0
          ir_IF_FALSE_cold(if_op1_long_op2_double);
5417
0
          ir_END_list(slow_inputs);
5418
0
          ir_IF_TRUE(if_op1_long_op2_double);
5419
0
        }
5420
0
        if (!zend_jit_math_long_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5421
0
          return 0;
5422
0
        }
5423
0
        ir_refs_add(end_inputs, ir_END());
5424
0
      } else {
5425
0
        ir_END_list(slow_inputs);
5426
0
      }
5427
0
      ir_IF_TRUE(if_op1_long_op2_long);
5428
0
    }
5429
0
    if (!zend_jit_math_long_long(jit, opline, opcode, op1_addr, op2_addr, res_addr, res_info, res_use_info, may_overflow)) {
5430
0
      return 0;
5431
0
    }
5432
0
    ir_refs_add(end_inputs, ir_END());
5433
5434
0
    if (if_op1_long) {
5435
0
      ir_IF_FALSE_cold(if_op1_long);
5436
0
    }
5437
5438
0
    if (op1_info & MAY_BE_DOUBLE) {
5439
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5440
0
        if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5441
0
        ir_IF_FALSE_cold(if_op1_double);
5442
0
        ir_END_list(slow_inputs);
5443
0
        ir_IF_TRUE(if_op1_double);
5444
0
      }
5445
0
      if (op2_info & MAY_BE_DOUBLE) {
5446
0
        if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5447
0
          if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5448
0
          ir_IF_TRUE(if_op1_double_op2_double);
5449
0
        }
5450
0
        if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5451
0
          return 0;
5452
0
        }
5453
0
        ir_refs_add(end_inputs, ir_END());
5454
0
        if (if_op1_double_op2_double) {
5455
0
          ir_IF_FALSE_cold(if_op1_double_op2_double);
5456
0
        }
5457
0
      }
5458
0
      if (!same_ops) {
5459
0
        if (op2_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
5460
0
          if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5461
0
          ir_IF_FALSE_cold(if_op1_double_op2_long);
5462
0
          ir_END_list(slow_inputs);
5463
0
          ir_IF_TRUE(if_op1_double_op2_long);
5464
0
        }
5465
0
        if (!zend_jit_math_double_long(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5466
0
          return 0;
5467
0
        }
5468
0
        ir_refs_add(end_inputs, ir_END());
5469
0
      } else if (if_op1_double_op2_double) {
5470
0
        ir_END_list(slow_inputs);
5471
0
      }
5472
0
    } else if (if_op1_long) {
5473
0
      ir_END_list(slow_inputs);
5474
0
    }
5475
0
  } else if ((op1_info & MAY_BE_DOUBLE) &&
5476
0
             !(op1_info & MAY_BE_LONG) &&
5477
0
             (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
5478
0
             (res_info & MAY_BE_DOUBLE)) {
5479
0
    if (op1_info & (MAY_BE_ANY-MAY_BE_DOUBLE)) {
5480
0
      if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5481
0
      ir_IF_FALSE_cold(if_op1_double);
5482
0
      ir_END_list(slow_inputs);
5483
0
      ir_IF_TRUE(if_op1_double);
5484
0
    }
5485
0
    if (op2_info & MAY_BE_DOUBLE) {
5486
0
      if (!same_ops && (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5487
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5488
0
        ir_IF_TRUE(if_op1_double_op2_double);
5489
0
      }
5490
0
      if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5491
0
        return 0;
5492
0
      }
5493
0
      ir_refs_add(end_inputs, ir_END());
5494
0
      if (if_op1_double_op2_double) {
5495
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
5496
0
      }
5497
0
    }
5498
0
    if (!same_ops && (op2_info & MAY_BE_LONG)) {
5499
0
      if (op2_info & (MAY_BE_ANY-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
5500
0
        if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5501
0
        ir_IF_FALSE_cold(if_op1_double_op2_long);
5502
0
        ir_END_list(slow_inputs);
5503
0
        ir_IF_TRUE(if_op1_double_op2_long);
5504
0
      }
5505
0
      if (!zend_jit_math_double_long(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5506
0
        return 0;
5507
0
      }
5508
0
      ir_refs_add(end_inputs, ir_END());
5509
0
    } else if (if_op1_double_op2_double) {
5510
0
      ir_END_list(slow_inputs);
5511
0
    }
5512
0
  } else if ((op2_info & MAY_BE_DOUBLE) &&
5513
0
             !(op2_info & MAY_BE_LONG) &&
5514
0
             (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
5515
0
             (res_info & MAY_BE_DOUBLE)) {
5516
0
    if (op2_info & (MAY_BE_ANY-MAY_BE_DOUBLE)) {
5517
0
      if_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
5518
0
      ir_IF_FALSE_cold(if_op2_double);
5519
0
      ir_END_list(slow_inputs);
5520
0
      ir_IF_TRUE(if_op2_double);
5521
0
    }
5522
0
    if (op1_info & MAY_BE_DOUBLE) {
5523
0
      if (!same_ops && (op1_info & (MAY_BE_ANY-MAY_BE_DOUBLE))) {
5524
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
5525
0
        ir_IF_TRUE(if_op1_double_op2_double);
5526
0
      }
5527
0
      if (!zend_jit_math_double_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5528
0
        return 0;
5529
0
      }
5530
0
      ir_refs_add(end_inputs, ir_END());
5531
0
      if (if_op1_double_op2_double) {
5532
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
5533
0
      }
5534
0
    }
5535
0
    if (!same_ops && (op1_info & MAY_BE_LONG)) {
5536
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
5537
0
        if_op1_long_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5538
0
        ir_IF_FALSE_cold(if_op1_long_op2_double);
5539
0
        ir_END_list(slow_inputs);
5540
0
        ir_IF_TRUE(if_op1_long_op2_double);
5541
0
      }
5542
0
      if (!zend_jit_math_long_double(jit, opcode, op1_addr, op2_addr, res_addr, res_use_info)) {
5543
0
        return 0;
5544
0
      }
5545
0
      ir_refs_add(end_inputs, ir_END());
5546
0
    } else if (if_op1_double_op2_double) {
5547
0
      ir_END_list(slow_inputs);
5548
0
    }
5549
0
  }
5550
5551
0
  if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
5552
0
    (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE)))) {
5553
0
    ir_ref func, arg1, arg2, arg3;
5554
5555
0
    if (slow_inputs) {
5556
0
      ir_MERGE_list(slow_inputs);
5557
0
    }
5558
5559
0
    if (Z_MODE(op1_addr) == IS_REG) {
5560
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op1.var);
5561
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
5562
0
        return 0;
5563
0
      }
5564
0
      op1_addr = real_addr;
5565
0
    }
5566
0
    if (Z_MODE(op2_addr) == IS_REG) {
5567
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op2.var);
5568
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
5569
0
        return 0;
5570
0
      }
5571
0
      op2_addr = real_addr;
5572
0
    }
5573
0
    if (Z_MODE(res_addr) == IS_REG) {
5574
0
      arg1 = jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var));
5575
0
    } else {
5576
0
      arg1 = jit_ZVAL_ADDR(jit, res_addr);
5577
0
    }
5578
0
    arg2 = jit_ZVAL_ADDR(jit, op1_addr);
5579
0
    arg3 = jit_ZVAL_ADDR(jit, op2_addr);
5580
0
    jit_SET_EX_OPLINE(jit, opline);
5581
0
    if (opcode == ZEND_ADD) {
5582
0
      func = ir_CONST_FC_FUNC(add_function);
5583
0
    } else if (opcode == ZEND_SUB) {
5584
0
      func = ir_CONST_FC_FUNC(sub_function);
5585
0
    } else if (opcode == ZEND_MUL) {
5586
0
      func = ir_CONST_FC_FUNC(mul_function);
5587
0
    } else if (opcode == ZEND_DIV) {
5588
0
      func = ir_CONST_FC_FUNC(div_function);
5589
0
    } else {
5590
0
      ZEND_UNREACHABLE();
5591
0
    }
5592
0
    ir_CALL_3(IR_VOID, func, arg1, arg2, arg3);
5593
5594
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
5595
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
5596
5597
0
    if (may_throw) {
5598
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
5599
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
5600
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
5601
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
5602
0
        zend_jit_check_exception_undef_result(jit, opline);
5603
0
      } else {
5604
0
        zend_jit_check_exception(jit);
5605
0
      }
5606
0
    }
5607
0
    if (Z_MODE(res_addr) == IS_REG) {
5608
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var);
5609
0
      if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
5610
0
        return 0;
5611
0
      }
5612
0
    }
5613
0
    ir_refs_add(end_inputs, ir_END());
5614
0
  }
5615
5616
0
  if (end_inputs->count) {
5617
0
    ir_MERGE_N(end_inputs->count, end_inputs->refs);
5618
0
  }
5619
5620
0
  if (Z_MODE(res_addr) == IS_REG) {
5621
0
    ZEND_ASSERT(jit->delay_refs == res_inputs);
5622
0
    ZEND_ASSERT(end_inputs->count == res_inputs->count);
5623
0
    jit->delay_var = -1;
5624
0
    jit->delay_refs = NULL;
5625
0
    if (res_inputs->count == 1) {
5626
0
      zend_jit_def_reg(jit, res_addr, res_inputs->refs[0]);
5627
0
    } else {
5628
0
      ir_ref phi = ir_PHI_N((res_info & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE, res_inputs->count, res_inputs->refs);
5629
0
      zend_jit_def_reg(jit, res_addr, phi);
5630
0
    }
5631
0
  }
5632
5633
0
  return 1;
5634
0
}
5635
5636
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)
5637
0
{
5638
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
5639
5640
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)) {
5641
0
    return 0;
5642
0
  }
5643
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
5644
0
    return 0;
5645
0
  }
5646
0
  return 1;
5647
0
}
5648
5649
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)
5650
0
{
5651
0
  ir_ref ref;
5652
0
  ir_ref arg1 = jit_Z_PTR(jit, op1_addr);
5653
0
  ir_ref arg2 = jit_Z_PTR(jit, op2_addr);
5654
5655
0
  ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_add_arrays_helper), arg1, arg2);
5656
0
  jit_set_Z_PTR(jit, res_addr, ref);
5657
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_ARRAY_EX);
5658
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
5659
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
5660
0
  return 1;
5661
0
}
5662
5663
static int zend_jit_long_math_helper(zend_jit_ctx   *jit,
5664
                                     const zend_op  *opline,
5665
                                     uint8_t         opcode,
5666
                                     uint8_t         op1_type,
5667
                                     znode_op        op1,
5668
                                     zend_jit_addr   op1_addr,
5669
                                     uint32_t        op1_info,
5670
                                     zend_ssa_range *op1_range,
5671
                                     uint8_t         op2_type,
5672
                                     znode_op        op2,
5673
                                     zend_jit_addr   op2_addr,
5674
                                     uint32_t        op2_info,
5675
                                     zend_ssa_range *op2_range,
5676
                                     uint32_t        res_var,
5677
                                     zend_jit_addr   res_addr,
5678
                                     uint32_t        res_info,
5679
                                     uint32_t        res_use_info,
5680
                                     int             may_throw)
5681
0
{
5682
0
  ir_ref ref = IR_UNUSED;
5683
0
  ir_ref if_long1 = IR_UNUSED;
5684
0
  ir_ref if_long2 = IR_UNUSED;
5685
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
5686
0
  ir_refs *res_inputs;
5687
5688
0
  ir_refs_init(res_inputs, 2);
5689
5690
0
  if (Z_MODE(op1_addr) == IS_REG
5691
0
   && Z_LOAD(op1_addr)
5692
0
   && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
5693
    /* Force load */
5694
0
    zend_jit_use_reg(jit, op1_addr);
5695
0
  }
5696
0
  if (Z_MODE(op2_addr) == IS_REG
5697
0
   && Z_LOAD(op2_addr)
5698
0
   && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
5699
    /* Force load */
5700
0
    zend_jit_use_reg(jit, op2_addr);
5701
0
  }
5702
5703
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
5704
0
    if_long1 = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
5705
0
    ir_IF_TRUE(if_long1);
5706
0
  }
5707
0
  if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
5708
0
    if_long2 = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
5709
0
    ir_IF_TRUE(if_long2);
5710
0
  }
5711
5712
0
  if (opcode == ZEND_SL) {
5713
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5714
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5715
5716
0
      if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
5717
0
        if (EXPECTED(op2_lval > 0)) {
5718
0
          ref = ir_CONST_LONG(0);
5719
0
        } else {
5720
0
          zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5721
0
          zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5722
0
          jit_SET_EX_OPLINE(jit, opline);
5723
0
          ir_GUARD(IR_FALSE, jit_STUB_ADDR(jit, jit_stub_negative_shift));
5724
0
          if (Z_MODE(res_addr) == IS_REG) {
5725
0
            ref = ir_CONST_LONG(0); // dead code
5726
0
          }
5727
0
        }
5728
0
      } else {
5729
0
        ref = ir_SHL_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5730
0
      }
5731
0
    } else {
5732
0
      ref = jit_Z_LVAL(jit, op2_addr);
5733
0
      if (!op2_range ||
5734
0
           op2_range->min < 0 ||
5735
0
           op2_range->max >= SIZEOF_ZEND_LONG * 8) {
5736
5737
0
        ir_ref if_wrong, cold_path, ref2, if_ok;
5738
0
        ir_ref op1_ref = jit_Z_LVAL(jit, op1_addr);
5739
5740
0
        if_wrong = ir_IF(ir_UGT(ref, ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1)));
5741
0
        ir_IF_TRUE_cold(if_wrong);
5742
0
        if_ok = ir_IF(ir_GE(ref, ir_CONST_LONG(0)));
5743
0
        ir_IF_FALSE(if_ok);
5744
0
        jit_SET_EX_OPLINE(jit, opline);
5745
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5746
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5747
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_negative_shift));
5748
0
        ir_IF_TRUE(if_ok);
5749
0
        ref2 = ir_CONST_LONG(0);
5750
0
        cold_path = ir_END();
5751
0
        ir_IF_FALSE(if_wrong);
5752
0
        ref = ir_SHL_L(op1_ref, ref);
5753
0
        ir_MERGE_WITH(cold_path);
5754
0
        ref = ir_PHI_2(IR_LONG, ref, ref2);
5755
0
      } else {
5756
0
        ref = ir_SHL_L(jit_Z_LVAL(jit, op1_addr), ref);
5757
0
      }
5758
0
    }
5759
0
  } else if (opcode == ZEND_SR) {
5760
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5761
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5762
5763
0
      if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
5764
0
        if (EXPECTED(op2_lval > 0)) {
5765
0
          ref = ir_SAR_L(
5766
0
            jit_Z_LVAL(jit, op1_addr),
5767
0
            ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1));
5768
0
        } else {
5769
0
          zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5770
0
          zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5771
0
          jit_SET_EX_OPLINE(jit, opline);
5772
0
          ir_GUARD(IR_FALSE, jit_STUB_ADDR(jit, jit_stub_negative_shift));
5773
0
          if (Z_MODE(res_addr) == IS_REG) {
5774
0
            ref = ir_CONST_LONG(0); // dead code
5775
0
          }
5776
0
        }
5777
0
      } else {
5778
0
        ref = ir_SAR_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5779
0
      }
5780
0
    } else {
5781
0
      ref = jit_Z_LVAL(jit, op2_addr);
5782
0
      if (!op2_range ||
5783
0
           op2_range->min < 0 ||
5784
0
           op2_range->max >= SIZEOF_ZEND_LONG * 8) {
5785
5786
0
        ir_ref if_wrong, cold_path, ref2, if_ok;
5787
5788
0
        if_wrong = ir_IF(ir_UGT(ref, ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1)));
5789
0
        ir_IF_TRUE_cold(if_wrong);
5790
0
        if_ok = ir_IF(ir_GE(ref, ir_CONST_LONG(0)));
5791
0
        ir_IF_FALSE(if_ok);
5792
0
        jit_SET_EX_OPLINE(jit, opline);
5793
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5794
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5795
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_negative_shift));
5796
0
        ir_IF_TRUE(if_ok);
5797
0
        ref2 = ir_CONST_LONG((SIZEOF_ZEND_LONG * 8) - 1);
5798
0
        cold_path = ir_END();
5799
0
        ir_IF_FALSE(if_wrong);
5800
0
        ir_MERGE_WITH(cold_path);
5801
0
        ref = ir_PHI_2(IR_LONG, ref, ref2);
5802
0
      }
5803
0
      ref = ir_SAR_L(jit_Z_LVAL(jit, op1_addr), ref);
5804
0
    }
5805
0
  } else if (opcode == ZEND_MOD) {
5806
0
    if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
5807
0
      zend_long op2_lval = Z_LVAL_P(Z_ZV(op2_addr));
5808
5809
0
      if (op2_lval == 0) {
5810
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5811
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5812
0
        jit_SET_EX_OPLINE(jit, opline);
5813
0
        ir_GUARD(IR_FALSE,  jit_STUB_ADDR(jit, jit_stub_mod_by_zero));
5814
0
        if (Z_MODE(res_addr) == IS_REG) {
5815
0
          ref = ir_CONST_LONG(0); // dead code
5816
0
        }
5817
0
      } else if (zend_long_is_power_of_two(op2_lval) && op1_range && op1_range->min >= 0) {
5818
0
        ref = ir_AND_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval - 1));
5819
0
      } else {
5820
0
        ref = ir_MOD_L(jit_Z_LVAL(jit, op1_addr), ir_CONST_LONG(op2_lval));
5821
0
      }
5822
0
    } else {
5823
0
      ir_ref zero_path = 0;
5824
0
      ir_ref op1_ref = jit_Z_LVAL(jit, op1_addr);
5825
5826
0
      ref = jit_Z_LVAL(jit, op2_addr);
5827
0
      if ((op2_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) || !op2_range || (op2_range->min <= 0 && op2_range->max >= 0)) {
5828
0
        ir_ref if_ok = ir_IF(ref);
5829
0
        ir_IF_FALSE(if_ok);
5830
0
        jit_SET_EX_OPLINE(jit, opline);
5831
0
        zend_jit_invalidate_var_if_necessary(jit, op1_type, op1_addr, op1);
5832
0
        zend_jit_invalidate_var_if_necessary(jit, op2_type, op2_addr, op2);
5833
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_mod_by_zero));
5834
0
        ir_IF_TRUE(if_ok);
5835
0
      }
5836
5837
      /* Prevent overflow error/crash if op1 == LONG_MIN and op2 == -1 */
5838
0
      if (!op2_range || (op2_range->min <= -1 && op2_range->max >= -1)) {
5839
0
        ir_ref if_minus_one = ir_IF(ir_EQ(ref, ir_CONST_LONG(-1)));
5840
0
        ir_IF_TRUE_cold(if_minus_one);
5841
0
        zero_path = ir_END();
5842
0
        ir_IF_FALSE(if_minus_one);
5843
0
      }
5844
0
      ref = ir_MOD_L(op1_ref, ref);
5845
5846
0
      if (zero_path) {
5847
0
        ir_MERGE_WITH(zero_path);
5848
0
        ref = ir_PHI_2(IR_LONG, ref, ir_CONST_LONG(0));
5849
0
      }
5850
0
    }
5851
0
  } else {
5852
0
    ir_op op;
5853
0
    ir_ref op1, op2;
5854
5855
0
    if (opcode == ZEND_BW_OR) {
5856
0
      op = IR_OR;
5857
0
    } else if (opcode == ZEND_BW_AND) {
5858
0
      op = IR_AND;
5859
0
    } else if (opcode == ZEND_BW_XOR) {
5860
0
      op = IR_XOR;
5861
0
    } else {
5862
0
      ZEND_UNREACHABLE();
5863
0
    }
5864
0
    op1 = jit_Z_LVAL(jit, op1_addr);
5865
0
    op2 = (same_ops) ? op1 : jit_Z_LVAL(jit, op2_addr);
5866
0
    ref = ir_BINARY_OP_L(op, op1, op2);
5867
0
  }
5868
5869
0
  if (ref) {
5870
0
    if (Z_MODE(res_addr) == IS_REG
5871
0
     && ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))
5872
0
      || (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)))) {
5873
0
      jit->delay_var = Z_SSA_VAR(res_addr);
5874
0
      jit->delay_refs = res_inputs;
5875
0
    }
5876
0
    jit_set_Z_LVAL(jit, res_addr, ref);
5877
0
    if (Z_MODE(res_addr) != IS_REG) {
5878
0
      if (!zend_jit_same_addr(op1_addr, res_addr)) {
5879
0
        if ((res_use_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_GUARD)) != MAY_BE_LONG) {
5880
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
5881
0
        }
5882
0
      }
5883
0
    }
5884
0
  }
5885
5886
0
  if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) ||
5887
0
    (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
5888
0
    ir_ref fast_path = ir_END();
5889
0
    ir_ref func, arg1, arg2, arg3;
5890
5891
0
    if (if_long2 && if_long1) {
5892
0
      ir_ref ref;
5893
0
      ir_IF_FALSE_cold(if_long2);
5894
0
      ref = ir_END();
5895
0
      ir_IF_FALSE_cold(if_long1);
5896
0
      ir_MERGE_2(ref, ir_END());
5897
0
    } else if (if_long1) {
5898
0
      ir_IF_FALSE_cold(if_long1);
5899
0
    } else if (if_long2) {
5900
0
      ir_IF_FALSE_cold(if_long2);
5901
0
    }
5902
5903
0
    if (op1_info & MAY_BE_UNDEF) {
5904
0
      ir_ref if_def, ref, ref2;
5905
5906
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
5907
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
5908
0
      ir_IF_FALSE_cold(if_def);
5909
5910
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
5911
0
      jit_SET_EX_OPLINE(jit, opline);
5912
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
5913
5914
0
      ref2 = jit_EG(uninitialized_zval);
5915
0
      ir_MERGE_WITH_EMPTY_TRUE(if_def);
5916
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
5917
0
      op1_addr = ZEND_ADDR_REF_ZVAL(ref);
5918
0
    }
5919
5920
0
    if (op2_info & MAY_BE_UNDEF) {
5921
0
      ir_ref if_def, ref, ref2;
5922
5923
0
      ref = jit_ZVAL_ADDR(jit, op2_addr);
5924
0
      if_def = jit_if_not_Z_TYPE(jit, op2_addr, IS_UNDEF);
5925
0
      ir_IF_FALSE_cold(if_def);
5926
5927
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op2.var)));
5928
0
      jit_SET_EX_OPLINE(jit, opline);
5929
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op2.var));
5930
5931
0
      ref2 = jit_EG(uninitialized_zval);
5932
0
      ir_MERGE_WITH_EMPTY_TRUE(if_def);
5933
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
5934
0
      op2_addr = ZEND_ADDR_REF_ZVAL(ref);
5935
0
    }
5936
5937
0
    if (Z_MODE(op1_addr) == IS_REG) {
5938
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op1.var);
5939
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
5940
0
        return 0;
5941
0
      }
5942
0
      op1_addr = real_addr;
5943
0
    }
5944
0
    if (Z_MODE(op2_addr) == IS_REG) {
5945
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, op2.var);
5946
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
5947
0
        return 0;
5948
0
      }
5949
0
      op2_addr = real_addr;
5950
0
    }
5951
0
    if (Z_MODE(res_addr) == IS_REG) {
5952
0
      arg1 = jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var));
5953
0
    } else {
5954
0
      arg1 = jit_ZVAL_ADDR(jit, res_addr);
5955
0
    }
5956
0
    arg2 = jit_ZVAL_ADDR(jit, op1_addr);
5957
0
    arg3 = jit_ZVAL_ADDR(jit, op2_addr);
5958
0
    jit_SET_EX_OPLINE(jit, opline);
5959
0
    if (opcode == ZEND_BW_OR) {
5960
0
      func = ir_CONST_FC_FUNC(bitwise_or_function);
5961
0
    } else if (opcode == ZEND_BW_AND) {
5962
0
      func = ir_CONST_FC_FUNC(bitwise_and_function);
5963
0
    } else if (opcode == ZEND_BW_XOR) {
5964
0
      func = ir_CONST_FC_FUNC(bitwise_xor_function);
5965
0
    } else if (opcode == ZEND_SL) {
5966
0
      func = ir_CONST_FC_FUNC(shift_left_function);
5967
0
    } else if (opcode == ZEND_SR) {
5968
0
      func = ir_CONST_FC_FUNC(shift_right_function);
5969
0
    } else if (opcode == ZEND_MOD) {
5970
0
      func = ir_CONST_FC_FUNC(mod_function);
5971
0
    } else {
5972
0
      ZEND_UNREACHABLE();
5973
0
    }
5974
0
    ir_CALL_3(IR_VOID, func, arg1, arg2, arg3);
5975
5976
0
    if (op1_addr == res_addr && (op2_info & MAY_BE_RCN)) {
5977
      /* compound assignment may decrement "op2" refcount */
5978
0
      op2_info |= MAY_BE_RC1;
5979
0
    }
5980
5981
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
5982
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
5983
5984
0
    if (may_throw) {
5985
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
5986
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
5987
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
5988
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
5989
0
        zend_jit_check_exception_undef_result(jit, opline);
5990
0
      } else {
5991
0
        zend_jit_check_exception(jit);
5992
0
      }
5993
0
    }
5994
5995
0
    if (Z_MODE(res_addr) == IS_REG) {
5996
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, res_var);
5997
0
      if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
5998
0
        return 0;
5999
0
      }
6000
0
    }
6001
6002
0
    ir_MERGE_2(fast_path, ir_END());
6003
6004
0
    if (Z_MODE(res_addr) == IS_REG) {
6005
0
      ZEND_ASSERT(jit->delay_refs == res_inputs);
6006
0
      ZEND_ASSERT(res_inputs->count == 2);
6007
0
      jit->delay_var = -1;
6008
0
      jit->delay_refs = NULL;
6009
0
      if (res_inputs->count == 1) {
6010
0
        zend_jit_def_reg(jit, res_addr, res_inputs->refs[0]);
6011
0
      } else {
6012
0
        ir_ref phi = ir_PHI_N(IR_LONG, res_inputs->count, res_inputs->refs);
6013
0
        zend_jit_def_reg(jit, res_addr, phi);
6014
0
      }
6015
0
    }
6016
0
  }
6017
6018
0
  return 1;
6019
0
}
6020
6021
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)
6022
0
{
6023
0
  ZEND_ASSERT((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG));
6024
6025
0
  if (!zend_jit_long_math_helper(jit, opline, opline->opcode,
6026
0
      opline->op1_type, opline->op1, op1_addr, op1_info, op1_range,
6027
0
      opline->op2_type, opline->op2, op2_addr, op2_info, op2_range,
6028
0
      opline->result.var, res_addr, res_info, res_use_info, may_throw)) {
6029
0
    return 0;
6030
0
  }
6031
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6032
0
    return 0;
6033
0
  }
6034
0
  return 1;
6035
0
}
6036
6037
static int zend_jit_concat_helper(zend_jit_ctx   *jit,
6038
                                  const zend_op  *opline,
6039
                                  uint8_t         op1_type,
6040
                                  znode_op        op1,
6041
                                  zend_jit_addr   op1_addr,
6042
                                  uint32_t        op1_info,
6043
                                  uint8_t         op2_type,
6044
                                  znode_op        op2,
6045
                                  zend_jit_addr   op2_addr,
6046
                                  uint32_t        op2_info,
6047
                                  zend_jit_addr   res_addr,
6048
                                  int             may_throw)
6049
0
{
6050
0
  ir_ref if_op1_string = IR_UNUSED;
6051
0
  ir_ref if_op2_string = IR_UNUSED;
6052
0
  ir_ref fast_path = IR_UNUSED;
6053
6054
0
  if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6055
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) {
6056
0
      if_op1_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
6057
0
      ir_IF_TRUE(if_op1_string);
6058
0
    }
6059
0
    if (op2_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) {
6060
0
      if_op2_string = jit_if_Z_TYPE(jit, op2_addr, IS_STRING);
6061
0
      ir_IF_TRUE(if_op2_string);
6062
0
    }
6063
0
    if (zend_jit_same_addr(op1_addr, res_addr)) {
6064
0
      ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6065
0
      ir_ref arg2 = jit_ZVAL_ADDR(jit, op2_addr);
6066
6067
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_assign_concat_helper), arg1, arg2);
6068
      /* concatenation with itself may reduce refcount */
6069
0
      op2_info |= MAY_BE_RC1;
6070
0
    } else {
6071
0
      ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6072
0
      ir_ref arg2 = jit_ZVAL_ADDR(jit, op1_addr);
6073
0
      ir_ref arg3 = jit_ZVAL_ADDR(jit, op2_addr);
6074
6075
0
      if (op1_type == IS_CV || op1_type == IS_CONST) {
6076
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_concat_helper), arg1, arg2, arg3);
6077
0
      } else {
6078
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fast_concat_tmp_helper), arg1, arg2, arg3);
6079
0
      }
6080
0
    }
6081
    /* concatenation with empty string may increase refcount */
6082
0
    op2_info |= MAY_BE_RCN;
6083
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, opline);
6084
0
    if (if_op1_string || if_op2_string) {
6085
0
      fast_path = ir_END();
6086
0
    }
6087
0
  }
6088
0
  if ((op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING)) ||
6089
0
      (op2_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF) - MAY_BE_STRING))) {
6090
0
    if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6091
0
      if (if_op1_string && if_op2_string) {
6092
0
        ir_IF_FALSE(if_op1_string);
6093
0
        ir_MERGE_WITH_EMPTY_FALSE(if_op2_string);
6094
0
      } else if (if_op1_string) {
6095
0
        ir_IF_FALSE_cold(if_op1_string);
6096
0
      } else if (if_op2_string) {
6097
0
        ir_IF_FALSE_cold(if_op2_string);
6098
0
      }
6099
0
    }
6100
0
    ir_ref arg1 = jit_ZVAL_ADDR(jit, res_addr);
6101
0
    ir_ref arg2 = jit_ZVAL_ADDR(jit, op1_addr);
6102
0
    ir_ref arg3 = jit_ZVAL_ADDR(jit, op2_addr);
6103
6104
0
    jit_SET_EX_OPLINE(jit, opline);
6105
0
    ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(concat_function), arg1, arg2, arg3);
6106
    /* concatenation with empty string may increase refcount */
6107
0
    op1_info |= MAY_BE_RCN;
6108
0
    op2_info |= MAY_BE_RCN;
6109
0
    jit_FREE_OP(jit, op1_type, op1, op1_info, NULL);
6110
0
    jit_FREE_OP(jit, op2_type, op2, op2_info, NULL);
6111
0
    if (may_throw) {
6112
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
6113
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
6114
0
          jit_STUB_ADDR(jit, jit_stub_exception_handler_free_op2));
6115
0
      } else if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RX) {
6116
0
        zend_jit_check_exception_undef_result(jit, opline);
6117
0
      } else {
6118
0
        zend_jit_check_exception(jit);
6119
0
      }
6120
0
    }
6121
0
    if ((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING)) {
6122
0
      ir_MERGE_WITH(fast_path);
6123
0
    }
6124
0
  }
6125
0
  return 1;
6126
0
}
6127
6128
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)
6129
0
{
6130
0
  zend_jit_addr op1_addr, op2_addr;
6131
6132
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
6133
0
  ZEND_ASSERT((op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING));
6134
6135
0
  op1_addr = OP1_ADDR();
6136
0
  op2_addr = OP2_ADDR();
6137
6138
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);
6139
0
}
6140
6141
static int zend_jit_assign_op(zend_jit_ctx   *jit,
6142
                              const zend_op  *opline,
6143
                              uint32_t        op1_info,
6144
                              zend_jit_addr   op1_addr,
6145
                              zend_ssa_range *op1_range,
6146
                              uint32_t        op1_def_info,
6147
                              zend_jit_addr   op1_def_addr,
6148
                              uint32_t        op1_mem_info,
6149
                              uint32_t        op2_info,
6150
                              zend_jit_addr   op2_addr,
6151
                              zend_ssa_range *op2_range,
6152
                              int             may_overflow,
6153
                              int             may_throw)
6154
0
{
6155
0
  int result = 1;
6156
0
  ir_ref slow_path = IR_UNUSED;
6157
6158
0
  ZEND_ASSERT(opline->op1_type == IS_CV && opline->result_type == IS_UNUSED);
6159
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF) && !(op2_info & MAY_BE_UNDEF));
6160
6161
0
  if (op1_info & MAY_BE_REF) {
6162
0
    ir_ref ref, ref2, arg2, op1_noref_path;
6163
0
    ir_ref if_op1_ref = IR_UNUSED;
6164
0
    ir_ref if_op1_typed = IR_UNUSED;
6165
0
    binary_op_type binary_op = get_binary_op(opline->extended_value);
6166
6167
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
6168
0
    if_op1_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
6169
0
    ir_IF_FALSE(if_op1_ref);
6170
0
    op1_noref_path = ir_END();
6171
0
    ir_IF_TRUE(if_op1_ref);
6172
0
    ref2 = jit_Z_PTR_ref(jit, ref);
6173
6174
0
    if_op1_typed = jit_if_TYPED_REF(jit, ref2);
6175
0
    ir_IF_TRUE_cold(if_op1_typed);
6176
6177
0
    if (Z_MODE(op2_addr) == IS_REG) {
6178
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
6179
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
6180
0
        return 0;
6181
0
      }
6182
0
      arg2 = jit_ZVAL_ADDR(jit, real_addr);
6183
0
    } else {
6184
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
6185
0
    }
6186
0
    jit_SET_EX_OPLINE(jit, opline);
6187
0
    if ((opline->op2_type & (IS_TMP_VAR|IS_VAR))
6188
0
     && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
6189
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref_tmp),
6190
0
        ref2, arg2, ir_CONST_FC_FUNC(binary_op));
6191
0
    } else {
6192
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
6193
0
        ref2, arg2, ir_CONST_FC_FUNC(binary_op));
6194
0
    }
6195
0
    zend_jit_check_exception(jit);
6196
0
    slow_path = ir_END();
6197
6198
0
    ir_IF_FALSE(if_op1_typed);
6199
0
    ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
6200
6201
0
    ir_MERGE_WITH(op1_noref_path);
6202
0
    ref = ir_PHI_2(IR_ADDR, ref2, ref);
6203
0
    ZEND_ASSERT(op1_addr == op1_def_addr);
6204
0
    op1_def_addr = op1_addr = ZEND_ADDR_REF_ZVAL(ref);
6205
0
  }
6206
6207
0
  switch (opline->extended_value) {
6208
0
    case ZEND_ADD:
6209
0
    case ZEND_SUB:
6210
0
    case ZEND_MUL:
6211
0
    case ZEND_DIV:
6212
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);
6213
0
      break;
6214
0
    case ZEND_BW_OR:
6215
0
    case ZEND_BW_AND:
6216
0
    case ZEND_BW_XOR:
6217
0
    case ZEND_SL:
6218
0
    case ZEND_SR:
6219
0
    case ZEND_MOD:
6220
0
      result = zend_jit_long_math_helper(jit, opline, opline->extended_value,
6221
0
        opline->op1_type, opline->op1, op1_addr, op1_info, op1_range,
6222
0
        opline->op2_type, opline->op2, op2_addr, op2_info, op2_range,
6223
0
        opline->op1.var, op1_def_addr, op1_def_info, op1_mem_info, may_throw);
6224
0
      break;
6225
0
    case ZEND_CONCAT:
6226
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);
6227
0
      break;
6228
0
    default:
6229
0
      ZEND_UNREACHABLE();
6230
0
  }
6231
6232
0
  if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_def_addr, op1_def_info, op1_addr, op1_info)) {
6233
0
    return 0;
6234
0
  }
6235
6236
0
  if (op1_info & MAY_BE_REF) {
6237
0
    ir_MERGE_WITH(slow_path);
6238
0
  }
6239
6240
0
  return result;
6241
0
}
6242
6243
static ir_ref jit_ZVAL_DEREF_ref(zend_jit_ctx *jit, ir_ref ref)
6244
0
{
6245
0
  ir_ref if_ref, ref2;
6246
6247
0
  if_ref = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_REFERENCE)));
6248
0
  ir_IF_TRUE(if_ref);
6249
0
  ref2 = ir_ADD_OFFSET(jit_Z_PTR_ref(jit, ref), offsetof(zend_reference, val));
6250
0
  ir_MERGE_WITH_EMPTY_FALSE(if_ref);
6251
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
6252
0
}
6253
6254
static zend_jit_addr jit_ZVAL_DEREF(zend_jit_ctx *jit, zend_jit_addr addr)
6255
0
{
6256
0
  ir_ref ref = jit_ZVAL_ADDR(jit, addr);
6257
0
  ref = jit_ZVAL_DEREF_ref(jit, ref);
6258
0
  return ZEND_ADDR_REF_ZVAL(ref);
6259
0
}
6260
6261
static ir_ref jit_ZVAL_INDIRECT_DEREF_ref(zend_jit_ctx *jit, ir_ref ref)
6262
0
{
6263
0
  ir_ref if_ref, ref2;
6264
6265
0
  if_ref = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_INDIRECT)));
6266
0
  ir_IF_TRUE(if_ref);
6267
0
  ref2 = jit_Z_PTR_ref(jit, ref);
6268
0
  ir_MERGE_WITH_EMPTY_FALSE(if_ref);
6269
0
  return ir_PHI_2(IR_ADDR, ref2, ref);
6270
0
}
6271
6272
static zend_jit_addr jit_ZVAL_INDIRECT_DEREF(zend_jit_ctx *jit, zend_jit_addr addr)
6273
0
{
6274
0
  ir_ref ref = jit_ZVAL_ADDR(jit, addr);
6275
0
  ref = jit_ZVAL_INDIRECT_DEREF_ref(jit, ref);
6276
0
  return ZEND_ADDR_REF_ZVAL(ref);
6277
0
}
6278
6279
static int zend_jit_simple_assign(zend_jit_ctx   *jit,
6280
                                  const zend_op  *opline,
6281
                                  zend_jit_addr   var_addr,
6282
                                  uint32_t        var_info,
6283
                                  uint32_t        var_def_info,
6284
                                  uint8_t         val_type,
6285
                                  zend_jit_addr   val_addr,
6286
                                  uint32_t        val_info,
6287
                                  zend_jit_addr   res_addr,
6288
                                  bool            check_exception)
6289
0
{
6290
0
  ir_ref end_inputs = IR_UNUSED;
6291
6292
0
  if (Z_MODE(val_addr) == IS_CONST_ZVAL) {
6293
0
    zval *zv = Z_ZV(val_addr);
6294
6295
0
    if (!res_addr) {
6296
0
      jit_ZVAL_COPY_CONST(jit,
6297
0
        var_addr,
6298
0
        var_info, var_def_info,
6299
0
        zv, true);
6300
0
    } else {
6301
0
      jit_ZVAL_COPY_CONST(jit,
6302
0
        var_addr,
6303
0
        var_info, var_def_info,
6304
0
        zv, true);
6305
0
      jit_ZVAL_COPY_CONST(jit,
6306
0
        res_addr,
6307
0
        -1, var_def_info,
6308
0
        zv, true);
6309
0
    }
6310
0
  } else {
6311
0
    if (val_info & MAY_BE_UNDEF) {
6312
0
      ir_ref if_def, ret;
6313
6314
0
      if_def = jit_if_not_Z_TYPE(jit, val_addr, IS_UNDEF);
6315
0
      ir_IF_FALSE_cold(if_def);
6316
6317
0
      jit_set_Z_TYPE_INFO(jit, var_addr, IS_NULL);
6318
0
      if (res_addr) {
6319
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
6320
0
      }
6321
0
      jit_SET_EX_OPLINE(jit, opline);
6322
6323
0
      ZEND_ASSERT(Z_MODE(val_addr) == IS_MEM_ZVAL);
6324
      // zend_error_unchecked(E_WARNING, "Undefined variable $%S", CV_DEF_OF(EX_VAR_TO_NUM(opline->op1.var)));
6325
0
      ret = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(Z_OFFSET(val_addr)));
6326
6327
0
      if (check_exception) {
6328
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
6329
0
      }
6330
6331
0
      ir_END_list(end_inputs);
6332
0
      ir_IF_TRUE(if_def);
6333
0
    }
6334
0
    if (val_info & MAY_BE_REF) {
6335
0
      if (val_type == IS_CV) {
6336
0
        ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
6337
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
6338
0
        val_addr = ZEND_ADDR_REF_ZVAL(ref);
6339
0
      } else {
6340
0
        ir_ref ref, type, if_ref, ref2, refcount, if_not_zero;
6341
6342
0
        ref = jit_ZVAL_ADDR(jit, val_addr);
6343
0
        type = jit_Z_TYPE_ref(jit, ref);
6344
0
        if_ref = ir_IF(ir_EQ(type, ir_CONST_U8(IS_REFERENCE)));
6345
6346
0
        ir_IF_TRUE_cold(if_ref);
6347
0
        ref = jit_Z_PTR_ref(jit, ref);
6348
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
6349
0
        if (!res_addr) {
6350
0
          jit_ZVAL_COPY(jit,
6351
0
            var_addr,
6352
0
            var_info,
6353
0
            ZEND_ADDR_REF_ZVAL(ref2), val_info, true);
6354
0
        } else {
6355
0
          jit_ZVAL_COPY_2(jit,
6356
0
            res_addr,
6357
0
            var_addr,
6358
0
            var_info,
6359
0
            ZEND_ADDR_REF_ZVAL(ref2), val_info, 2);
6360
0
        }
6361
6362
0
        refcount = jit_GC_DELREF(jit, ref);
6363
0
        if_not_zero = ir_IF(refcount);
6364
0
        ir_IF_FALSE(if_not_zero);
6365
        // TODO: instead of dtor() call and ADDREF above, we may call efree() and move addref at "true" path ???
6366
        // This is related to GH-10168 (keep this before GH-10168 is completely closed)
6367
        // jit_EFREE(jit, ref, sizeof(zend_reference), NULL, NULL);
6368
0
        jit_ZVAL_DTOR(jit, ref, val_info, opline);
6369
0
        ir_END_list(end_inputs);
6370
0
        ir_IF_TRUE(if_not_zero);
6371
0
        ir_END_list(end_inputs);
6372
6373
0
        ir_IF_FALSE(if_ref);
6374
0
      }
6375
0
    }
6376
6377
0
    if (!res_addr) {
6378
0
      jit_ZVAL_COPY(jit,
6379
0
        var_addr,
6380
0
        var_info,
6381
0
        val_addr, val_info, val_type == IS_CV);
6382
0
    } else {
6383
0
      jit_ZVAL_COPY_2(jit,
6384
0
        res_addr,
6385
0
        var_addr,
6386
0
        var_info,
6387
0
        val_addr, val_info, val_type == IS_CV ? 2 : 1);
6388
0
    }
6389
0
  }
6390
6391
0
  if (end_inputs) {
6392
0
    ir_END_list(end_inputs);
6393
0
    ir_MERGE_list(end_inputs);
6394
0
  }
6395
6396
0
  return 1;
6397
0
}
6398
6399
static int zend_jit_assign_to_variable_call(zend_jit_ctx   *jit,
6400
                                            const zend_op  *opline,
6401
                                            zend_jit_addr   __var_use_addr,
6402
                                            zend_jit_addr   var_addr,
6403
                                            uint32_t        __var_info,
6404
                                            uint32_t        __var_def_info,
6405
                                            uint8_t         val_type,
6406
                                            zend_jit_addr   val_addr,
6407
                                            uint32_t        val_info,
6408
                                            zend_jit_addr   __res_addr,
6409
                                            bool       __check_exception)
6410
0
{
6411
0
  jit_stub_id func;
6412
0
  ir_ref undef_path = IR_UNUSED;
6413
6414
0
  if (val_info & MAY_BE_UNDEF) {
6415
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
6416
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
6417
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
6418
6419
0
      if (!exit_addr) {
6420
0
        return 0;
6421
0
      }
6422
6423
0
      jit_guard_not_Z_TYPE(jit, val_addr, IS_UNDEF, exit_addr);
6424
0
    } else {
6425
0
      ir_ref if_def;
6426
6427
0
      ZEND_ASSERT(Z_MODE(val_addr) == IS_MEM_ZVAL && Z_REG(val_addr) == ZREG_FP);
6428
0
      if_def = ir_IF(jit_Z_TYPE(jit, val_addr));
6429
0
      ir_IF_FALSE_cold(if_def);
6430
0
      jit_SET_EX_OPLINE(jit, opline);
6431
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(Z_OFFSET(val_addr)));
6432
6433
0
      ir_CALL_2(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_assign_const, IR_FASTCALL_FUNC),
6434
0
        jit_ZVAL_ADDR(jit, var_addr),
6435
0
        jit_EG(uninitialized_zval));
6436
6437
0
      undef_path = ir_END();
6438
0
      ir_IF_TRUE(if_def);
6439
0
    }
6440
0
  }
6441
6442
0
  if (!(val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {
6443
0
    func = jit_stub_assign_tmp;
6444
0
  } else if (val_type == IS_CONST) {
6445
0
    func = jit_stub_assign_const;
6446
0
  } else if (val_type == IS_TMP_VAR) {
6447
0
    func = jit_stub_assign_tmp;
6448
0
  } else if (val_type == IS_VAR) {
6449
0
    if (!(val_info & MAY_BE_REF)) {
6450
0
      func = jit_stub_assign_tmp;
6451
0
    } else {
6452
0
      func = jit_stub_assign_var;
6453
0
    }
6454
0
  } else if (val_type == IS_CV) {
6455
0
    if (!(val_info & MAY_BE_REF)) {
6456
0
      func = jit_stub_assign_cv_noref;
6457
0
    } else {
6458
0
      func = jit_stub_assign_cv;
6459
0
    }
6460
0
  } else {
6461
0
    ZEND_UNREACHABLE();
6462
0
  }
6463
6464
0
  if (opline) {
6465
0
    jit_SET_EX_OPLINE(jit, opline);
6466
0
  }
6467
6468
0
  ir_CALL_2(IR_VOID, jit_STUB_FUNC_ADDR(jit, func, IR_FASTCALL_FUNC),
6469
0
    jit_ZVAL_ADDR(jit, var_addr),
6470
0
    jit_ZVAL_ADDR(jit, val_addr));
6471
6472
0
  if (undef_path) {
6473
0
    ir_MERGE_WITH(undef_path);
6474
0
  }
6475
6476
0
  return 1;
6477
0
}
6478
6479
static int zend_jit_assign_to_variable(zend_jit_ctx   *jit,
6480
                                       const zend_op  *opline,
6481
                                       zend_jit_addr   var_use_addr,
6482
                                       zend_jit_addr   var_addr,
6483
                                       uint32_t        var_info,
6484
                                       uint32_t        var_def_info,
6485
                                       uint8_t         val_type,
6486
                                       zend_jit_addr   val_addr,
6487
                                       uint32_t        val_info,
6488
                                       zend_jit_addr   res_addr,
6489
                                       zend_jit_addr   ref_addr,
6490
                                       bool       check_exception)
6491
0
{
6492
0
  ir_ref if_refcounted = IR_UNUSED;
6493
0
  ir_ref simple_inputs = IR_UNUSED;
6494
0
  bool done = false;
6495
0
  zend_jit_addr real_res_addr = 0;
6496
0
  ir_refs *end_inputs;
6497
0
  ir_refs *res_inputs;
6498
6499
0
  ir_refs_init(end_inputs, 6);
6500
0
  ir_refs_init(res_inputs, 6);
6501
6502
0
  if (Z_MODE(val_addr) == IS_REG && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
6503
    /* Force load */
6504
0
    zend_jit_use_reg(jit, val_addr);
6505
0
  }
6506
6507
0
  if (Z_MODE(var_addr) == IS_REG) {
6508
0
    jit->delay_var = Z_SSA_VAR(var_addr);
6509
0
    jit->delay_refs = res_inputs;
6510
0
    if (Z_MODE(res_addr) == IS_REG) {
6511
0
      real_res_addr = res_addr;
6512
0
      res_addr = 0;
6513
0
    }
6514
0
  } else if (Z_MODE(res_addr) == IS_REG) {
6515
0
    jit->delay_var = Z_SSA_VAR(res_addr);
6516
0
    jit->delay_refs = res_inputs;
6517
0
  }
6518
6519
0
  if ((var_info & MAY_BE_REF) || ref_addr) {
6520
0
    ir_ref ref = 0, if_ref = 0, ref2, arg2, if_typed, non_ref_path;
6521
0
    uintptr_t func;
6522
6523
0
    if (!ref_addr) {
6524
0
      ref = jit_ZVAL_ADDR(jit, var_use_addr);
6525
0
      if_ref = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(IS_REFERENCE));
6526
0
      ir_IF_TRUE(if_ref);
6527
0
      ref2 = jit_Z_PTR_ref(jit, ref);
6528
0
    } else {
6529
0
      ref2 = jit_ZVAL_ADDR(jit, ref_addr);
6530
0
    }
6531
0
    if_typed = jit_if_TYPED_REF(jit, ref2);
6532
0
    ir_IF_TRUE_cold(if_typed);
6533
0
    jit_SET_EX_OPLINE(jit, opline);
6534
0
    if (Z_MODE(val_addr) == IS_REG) {
6535
0
      zend_jit_addr real_addr;
6536
6537
0
      if (opline->opcode == ZEND_ASSIGN_DIM || opline->opcode == ZEND_ASSIGN_OBJ) {
6538
0
        real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
6539
0
      } else {
6540
0
        ZEND_ASSERT(opline->opcode == ZEND_ASSIGN);
6541
0
        real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
6542
0
      }
6543
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
6544
0
        return 0;
6545
0
      }
6546
0
      arg2 = jit_ZVAL_ADDR(jit, real_addr);
6547
0
    } else {
6548
0
      arg2 = jit_ZVAL_ADDR(jit, val_addr);
6549
0
    }
6550
0
    if (!res_addr) {
6551
0
      if (val_type == IS_CONST) {
6552
0
        func = (uintptr_t)zend_jit_assign_const_to_typed_ref;
6553
0
      } else if (val_type == IS_TMP_VAR) {
6554
0
        func = (uintptr_t)zend_jit_assign_tmp_to_typed_ref;
6555
0
      } else if (val_type == IS_VAR) {
6556
0
        func = (uintptr_t)zend_jit_assign_var_to_typed_ref;
6557
0
      } else if (val_type == IS_CV) {
6558
0
        func = (uintptr_t)zend_jit_assign_cv_to_typed_ref;
6559
0
      } else {
6560
0
        ZEND_UNREACHABLE();
6561
0
      }
6562
0
      ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(func), ref2, arg2);
6563
0
    } else {
6564
0
      if (val_type == IS_CONST) {
6565
0
        func = (uintptr_t)zend_jit_assign_const_to_typed_ref2;
6566
0
      } else if (val_type == IS_TMP_VAR) {
6567
0
        func = (uintptr_t)zend_jit_assign_tmp_to_typed_ref2;
6568
0
      } else if (val_type == IS_VAR) {
6569
0
        func = (uintptr_t)zend_jit_assign_var_to_typed_ref2;
6570
0
      } else if (val_type == IS_CV) {
6571
0
        func = (uintptr_t)zend_jit_assign_cv_to_typed_ref2;
6572
0
      } else {
6573
0
        ZEND_UNREACHABLE();
6574
0
      }
6575
0
      ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(func), ref2, arg2, jit_ZVAL_ADDR(jit, res_addr));
6576
0
    }
6577
0
    if (check_exception) {
6578
0
      zend_jit_check_exception(jit);
6579
0
    }
6580
0
    ir_refs_add(end_inputs, ir_END());
6581
6582
0
    if (!ref_addr) {
6583
0
      ir_IF_FALSE(if_ref);
6584
0
      non_ref_path = ir_END();
6585
0
      ir_IF_FALSE(if_typed);
6586
0
      ref2 = ir_ADD_OFFSET(ref2, offsetof(zend_reference, val));
6587
0
      ir_MERGE_WITH(non_ref_path);
6588
0
      ref = ir_PHI_2(IR_ADDR, ref2, ref);
6589
0
      var_addr = var_use_addr = ZEND_ADDR_REF_ZVAL(ref);
6590
0
    } else {
6591
0
      ir_IF_FALSE(if_typed);
6592
0
    }
6593
0
  }
6594
6595
0
  if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
6596
0
    ir_ref ref, counter, if_not_zero;
6597
6598
0
    if (var_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
6599
0
      if_refcounted = jit_if_REFCOUNTED(jit, var_use_addr);
6600
0
      ir_IF_FALSE(if_refcounted);
6601
0
      ir_END_list(simple_inputs);
6602
0
      ir_IF_TRUE_cold(if_refcounted);
6603
0
    } else if (RC_MAY_BE_1(var_info)) {
6604
0
      done = true;
6605
0
    }
6606
0
    ref = jit_Z_PTR(jit, var_use_addr);
6607
0
    if (RC_MAY_BE_1(var_info)) {
6608
0
      if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, var_def_info, val_type, val_addr, val_info, res_addr, false)) {
6609
0
        return 0;
6610
0
      }
6611
0
      counter = jit_GC_DELREF(jit, ref);
6612
6613
0
      if_not_zero = ir_IF(counter);
6614
0
      ir_IF_FALSE(if_not_zero);
6615
0
      jit_ZVAL_DTOR(jit, ref, var_info, opline);
6616
0
      if (check_exception) {
6617
0
        zend_jit_check_exception(jit);
6618
0
      }
6619
0
      ir_refs_add(end_inputs, ir_END());
6620
0
      ir_IF_TRUE(if_not_zero);
6621
0
      if (RC_MAY_BE_N(var_info) && (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT)) != 0) {
6622
0
        ir_ref if_may_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
6623
0
        ir_IF_FALSE(if_may_leak);
6624
0
        if (opline) {
6625
0
          jit_SET_EX_OPLINE(jit, opline);
6626
0
        }
6627
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
6628
6629
0
        if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6630
0
          ZEND_ASSERT(jit->delay_refs == res_inputs);
6631
0
          ZEND_ASSERT(res_inputs->count > 0);
6632
0
          ir_refs_add(res_inputs, res_inputs->refs[res_inputs->count - 1]);
6633
0
        }
6634
0
        if (check_exception && (val_info & MAY_BE_UNDEF)) {
6635
0
          zend_jit_check_exception(jit);
6636
0
        }
6637
0
        ir_refs_add(end_inputs, ir_END());
6638
0
        ir_IF_TRUE(if_may_leak);
6639
0
      }
6640
0
      if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6641
0
        ZEND_ASSERT(jit->delay_refs == res_inputs);
6642
0
        ZEND_ASSERT(res_inputs->count > 0);
6643
0
        ir_refs_add(res_inputs, res_inputs->refs[res_inputs->count - 1]);
6644
0
      }
6645
0
      if (check_exception && (val_info & MAY_BE_UNDEF)) {
6646
0
        zend_jit_check_exception(jit);
6647
0
      }
6648
0
      ir_refs_add(end_inputs, ir_END());
6649
0
    } else /* if (RC_MAY_BE_N(var_info)) */ {
6650
0
      jit_GC_DELREF(jit, ref);
6651
0
      if (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT)) {
6652
0
        ir_ref if_may_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref);
6653
0
        ir_IF_FALSE(if_may_leak);
6654
0
        if (opline) {
6655
0
          jit_SET_EX_OPLINE(jit, opline);
6656
0
        }
6657
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref);
6658
0
        ir_END_list(simple_inputs);
6659
0
        ir_IF_TRUE(if_may_leak);
6660
0
      }
6661
0
      ir_END_list(simple_inputs);
6662
0
    }
6663
0
  }
6664
6665
0
  if (simple_inputs) {
6666
0
    ir_MERGE_list(simple_inputs);
6667
0
  }
6668
6669
0
  if (!done) {
6670
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)) {
6671
0
      return 0;
6672
0
    }
6673
0
    if (end_inputs->count) {
6674
0
      ir_refs_add(end_inputs, ir_END());
6675
0
    }
6676
0
  }
6677
6678
0
  if (end_inputs->count) {
6679
0
    ir_MERGE_N(end_inputs->count, end_inputs->refs);
6680
0
  }
6681
6682
0
  if (Z_MODE(var_addr) == IS_REG || Z_MODE(res_addr) == IS_REG) {
6683
0
    ir_ref phi;
6684
6685
0
    ZEND_ASSERT(jit->delay_refs == res_inputs);
6686
0
    ZEND_ASSERT(end_inputs->count == res_inputs->count || (end_inputs->count == 0 && res_inputs->count == 1));
6687
0
    jit->delay_var = -1;
6688
0
    jit->delay_refs = NULL;
6689
0
    if (res_inputs->count == 1) {
6690
0
      phi = res_inputs->refs[0];
6691
0
    } else {
6692
0
      phi = ir_PHI_N((var_def_info & MAY_BE_LONG & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE,
6693
0
        res_inputs->count, res_inputs->refs);
6694
0
    }
6695
0
    if (Z_MODE(var_addr) == IS_REG) {
6696
0
      if ((var_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) || ref_addr) {
6697
0
        phi = ir_emit2(&jit->ctx, IR_OPT(IR_COPY, jit->ctx.ir_base[phi].type), phi, 1);
6698
0
      }
6699
0
      zend_jit_def_reg(jit, var_addr, phi);
6700
0
      if (real_res_addr) {
6701
0
        if (var_def_info & MAY_BE_LONG) {
6702
0
          jit_set_Z_LVAL(jit, real_res_addr, jit_Z_LVAL(jit, var_addr));
6703
0
        } else {
6704
0
          jit_set_Z_DVAL(jit, real_res_addr, jit_Z_DVAL(jit, var_addr));
6705
0
        }
6706
0
      }
6707
0
    } else {
6708
0
      zend_jit_def_reg(jit, res_addr, phi);
6709
0
    }
6710
0
  }
6711
6712
0
  return 1;
6713
0
}
6714
6715
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)
6716
0
{
6717
0
  if (op1_addr != op1_def_addr) {
6718
0
    if (!zend_jit_update_regs(jit, opline->op1.var, op1_addr, op1_def_addr, op1_info)) {
6719
0
      return 0;
6720
0
    }
6721
0
    if (Z_MODE(op1_def_addr) == IS_REG && Z_MODE(op1_addr) != IS_REG) {
6722
0
      op1_addr = op1_def_addr;
6723
0
    }
6724
0
  }
6725
6726
0
  if (!zend_jit_simple_assign(jit, opline, res_addr, res_use_info, res_info, opline->op1_type, op1_addr, op1_info, 0, true)) {
6727
0
    return 0;
6728
0
  }
6729
0
  if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6730
0
    return 0;
6731
0
  }
6732
0
  return 1;
6733
0
}
6734
6735
static int zend_jit_assign(zend_jit_ctx  *jit,
6736
                           const zend_op *opline,
6737
                           uint32_t       op1_info,
6738
                           zend_jit_addr  op1_use_addr,
6739
                           uint32_t       op1_def_info,
6740
                           zend_jit_addr  op1_addr,
6741
                           uint32_t       op2_info,
6742
                           zend_jit_addr  op2_addr,
6743
                           zend_jit_addr  op2_def_addr,
6744
                           uint32_t       res_info,
6745
                           zend_jit_addr  res_addr,
6746
                           zend_jit_addr  ref_addr,
6747
                           int            may_throw)
6748
0
{
6749
0
  ZEND_ASSERT(opline->op1_type == IS_CV);
6750
6751
0
  if (op2_addr != op2_def_addr) {
6752
0
    if (!zend_jit_update_regs(jit, opline->op2.var, op2_addr, op2_def_addr, op2_info)) {
6753
0
      return 0;
6754
0
    }
6755
0
    if (Z_MODE(op2_def_addr) == IS_REG && Z_MODE(op2_addr) != IS_REG) {
6756
0
      op2_addr = op2_def_addr;
6757
0
    }
6758
0
  }
6759
6760
0
  if (Z_MODE(op1_addr) != IS_REG
6761
0
   && Z_MODE(op1_use_addr) == IS_REG
6762
0
   && !Z_LOAD(op1_use_addr)
6763
0
   && !Z_STORE(op1_use_addr)) {
6764
    /* Force type update */
6765
0
    op1_info |= MAY_BE_UNDEF;
6766
0
  }
6767
0
  if (!zend_jit_assign_to_variable(jit, opline, op1_use_addr, op1_addr, op1_info, op1_def_info,
6768
0
      opline->op2_type, op2_addr, op2_info, res_addr, ref_addr, may_throw)) {
6769
0
    return 0;
6770
0
  }
6771
0
  if (Z_MODE(op1_addr) == IS_REG) {
6772
0
    if (Z_STORE(op1_addr)) {
6773
0
      if (!zend_jit_store_var_if_necessary_ex(jit, opline->op1.var, op1_addr, op1_def_info, op1_use_addr, op1_info)) {
6774
0
        return 0;
6775
0
      }
6776
0
    } else if ((op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
6777
0
      && Z_MODE(op1_use_addr) == IS_MEM_ZVAL
6778
0
      && Z_REG(op1_use_addr) == ZREG_FP
6779
0
      && EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)) < jit->current_op_array->last_var) {
6780
      /* We have to update type of CV because it may be captured by exception backtrace or released on RETURN */
6781
0
      if ((op1_def_info & MAY_BE_ANY) == MAY_BE_LONG) {
6782
0
        jit_set_Z_TYPE_INFO(jit, op1_use_addr, IS_LONG);
6783
0
        if (JIT_G(current_frame)) {
6784
0
          SET_STACK_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)), IS_LONG, 1);
6785
0
        }
6786
0
      } else if ((op1_def_info & MAY_BE_ANY) == MAY_BE_DOUBLE) {
6787
0
        jit_set_Z_TYPE_INFO(jit, op1_use_addr, IS_DOUBLE);
6788
0
        if (JIT_G(current_frame)) {
6789
0
          SET_STACK_TYPE(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(Z_OFFSET(op1_use_addr)), IS_DOUBLE, 1);
6790
0
        }
6791
0
      } else {
6792
0
        ZEND_UNREACHABLE();
6793
0
      }
6794
0
    }
6795
0
  }
6796
0
  if (opline->result_type != IS_UNUSED) {
6797
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
6798
0
      return 0;
6799
0
    }
6800
0
  }
6801
6802
0
  return 1;
6803
0
}
6804
6805
static ir_op zend_jit_cmp_op(const zend_op *opline)
6806
0
{
6807
0
  ir_op op;
6808
6809
0
  switch (opline->opcode) {
6810
0
    case ZEND_IS_EQUAL:
6811
0
    case ZEND_IS_IDENTICAL:
6812
0
    case ZEND_CASE:
6813
0
    case ZEND_CASE_STRICT:
6814
0
      op = IR_EQ;
6815
0
      break;
6816
0
    case ZEND_IS_NOT_EQUAL:
6817
0
    case ZEND_IS_NOT_IDENTICAL:
6818
0
      op = IR_NE;
6819
0
      break;
6820
0
    case ZEND_IS_SMALLER:
6821
0
      op = IR_LT;
6822
0
      break;
6823
0
    case ZEND_IS_SMALLER_OR_EQUAL:
6824
0
      op = IR_LE;
6825
0
      break;
6826
0
    default:
6827
0
      ZEND_UNREACHABLE();
6828
0
  }
6829
0
  return op;
6830
0
}
6831
6832
static ir_ref zend_jit_cmp_long_long(zend_jit_ctx   *jit,
6833
                                     const zend_op  *opline,
6834
                                     zend_ssa_range *op1_range,
6835
                                     zend_jit_addr   op1_addr,
6836
                                     zend_ssa_range *op2_range,
6837
                                     zend_jit_addr   op2_addr,
6838
                                     zend_jit_addr   res_addr,
6839
                                     uint8_t         smart_branch_opcode,
6840
                                     uint32_t        target_label,
6841
                                     uint32_t        target_label2,
6842
                                     const void     *exit_addr,
6843
                                     bool       skip_comparison)
6844
0
{
6845
0
  ir_ref ref;
6846
0
  bool result;
6847
6848
0
  if (zend_jit_is_constant_cmp_long_long(opline, op1_range, op1_addr, op2_range, op2_addr, &result)) {
6849
0
    if (!smart_branch_opcode ||
6850
0
        smart_branch_opcode == ZEND_JMPZ_EX ||
6851
0
        smart_branch_opcode == ZEND_JMPNZ_EX) {
6852
0
      jit_set_Z_TYPE_INFO(jit, res_addr, result ? IS_TRUE : IS_FALSE);
6853
0
    }
6854
0
    if (smart_branch_opcode && !exit_addr) {
6855
0
      if (smart_branch_opcode == ZEND_JMPZ ||
6856
0
          smart_branch_opcode == ZEND_JMPZ_EX) {
6857
0
        return jit_IF_ex(jit, IR_FALSE, result ? target_label : target_label2);
6858
0
      } else if (smart_branch_opcode == ZEND_JMPNZ ||
6859
0
                 smart_branch_opcode == ZEND_JMPNZ_EX) {
6860
0
        return jit_IF_ex(jit, IR_TRUE, result ? target_label : target_label2);
6861
0
      } else {
6862
0
        ZEND_UNREACHABLE();
6863
0
      }
6864
0
    }
6865
0
    if (opline->opcode != ZEND_IS_IDENTICAL
6866
0
     && opline->opcode != ZEND_IS_NOT_IDENTICAL
6867
0
     && opline->opcode != ZEND_CASE_STRICT) {
6868
0
      return ir_END();
6869
0
    } else {
6870
0
      return IR_NULL; /* success */
6871
0
    }
6872
0
  }
6873
6874
0
  ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_LVAL(jit, op1_addr), jit_Z_LVAL(jit, op2_addr));
6875
6876
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6877
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6878
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6879
0
  }
6880
0
  if (exit_addr) {
6881
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6882
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6883
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6884
0
      } else {
6885
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6886
0
      }
6887
0
    } else {
6888
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6889
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6890
0
      } else {
6891
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6892
0
      }
6893
0
    }
6894
0
  } else if (smart_branch_opcode) {
6895
0
    return jit_IF_ex(jit, ref,
6896
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6897
0
  }
6898
6899
0
  if (opline->opcode != ZEND_IS_IDENTICAL
6900
0
   && opline->opcode != ZEND_IS_NOT_IDENTICAL
6901
0
   && opline->opcode != ZEND_CASE_STRICT) {
6902
0
    return ir_END();
6903
0
  } else {
6904
0
    return IR_NULL; /* success */
6905
0
  }
6906
0
}
6907
6908
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)
6909
0
{
6910
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));
6911
6912
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6913
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6914
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6915
0
  }
6916
0
  if (exit_addr) {
6917
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6918
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6919
0
    } else {
6920
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6921
0
    }
6922
0
  } else if (smart_branch_opcode) {
6923
0
    return jit_IF_ex(jit, ref,
6924
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6925
0
  }
6926
0
  return ir_END();
6927
0
}
6928
6929
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)
6930
0
{
6931
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)));
6932
6933
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6934
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6935
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6936
0
  }
6937
0
  if (exit_addr) {
6938
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6939
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6940
0
    } else {
6941
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6942
0
    }
6943
0
  } else if (smart_branch_opcode) {
6944
0
    return jit_IF_ex(jit, ref,
6945
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6946
0
  }
6947
0
  return ir_END();
6948
0
}
6949
6950
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)
6951
0
{
6952
0
  ir_ref ref = ir_CMP_OP(zend_jit_cmp_op(opline), jit_Z_DVAL(jit, op1_addr), jit_Z_DVAL(jit, op2_addr));
6953
6954
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6955
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6956
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6957
0
  }
6958
0
  if (exit_addr) {
6959
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6960
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6961
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6962
0
      } else {
6963
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6964
0
      }
6965
0
    } else {
6966
0
      if (opline->opcode != ZEND_IS_NOT_IDENTICAL) {
6967
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6968
0
      } else {
6969
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6970
0
      }
6971
0
    }
6972
0
  } else if (smart_branch_opcode) {
6973
0
    return jit_IF_ex(jit, ref,
6974
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
6975
0
  }
6976
0
  if (opline->opcode != ZEND_IS_IDENTICAL
6977
0
   && opline->opcode != ZEND_IS_NOT_IDENTICAL
6978
0
   && opline->opcode != ZEND_CASE_STRICT) {
6979
0
    return ir_END();
6980
0
  } else {
6981
0
    return IR_NULL; /* success */
6982
0
  }
6983
0
}
6984
6985
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)
6986
0
{
6987
0
  ref = ir_CMP_OP(zend_jit_cmp_op(opline), ref, ir_CONST_I32(0));
6988
6989
0
  if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
6990
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
6991
0
      ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
6992
0
  }
6993
0
  if (exit_addr) {
6994
0
    if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
6995
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
6996
0
    } else {
6997
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
6998
0
    }
6999
0
  } else if (smart_branch_opcode) {
7000
0
    return jit_IF_ex(jit, ref,
7001
0
      (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
7002
0
  }
7003
7004
0
  return ir_END();
7005
0
}
7006
7007
static int zend_jit_cmp(zend_jit_ctx   *jit,
7008
                        const zend_op  *opline,
7009
                        uint32_t        op1_info,
7010
                        zend_ssa_range *op1_range,
7011
                        zend_jit_addr   op1_addr,
7012
                        uint32_t        op2_info,
7013
                        zend_ssa_range *op2_range,
7014
                        zend_jit_addr   op2_addr,
7015
                        zend_jit_addr   res_addr,
7016
                        int             may_throw,
7017
                        uint8_t         smart_branch_opcode,
7018
                        uint32_t        target_label,
7019
                        uint32_t        target_label2,
7020
                        const void     *exit_addr,
7021
                        bool       skip_comparison)
7022
0
{
7023
0
  ir_ref ref = IR_UNUSED;
7024
0
  ir_ref if_op1_long = IR_UNUSED;
7025
0
  ir_ref if_op1_double = IR_UNUSED;
7026
0
  ir_ref if_op2_double = IR_UNUSED;
7027
0
  ir_ref if_op1_long_op2_long = IR_UNUSED;
7028
0
  ir_ref if_op1_long_op2_double = IR_UNUSED;
7029
0
  ir_ref if_op1_double_op2_double = IR_UNUSED;
7030
0
  ir_ref if_op1_double_op2_long = IR_UNUSED;
7031
0
  ir_ref slow_inputs = IR_UNUSED;
7032
0
  bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
7033
0
  bool has_slow =
7034
0
    (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
7035
0
    (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) &&
7036
0
    ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
7037
0
     (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))));
7038
0
  ir_refs *end_inputs;
7039
7040
0
  ir_refs_init(end_inputs, 8);
7041
7042
0
  if (Z_MODE(op1_addr) == IS_REG) {
7043
0
    if (!has_concrete_type(op2_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op1_addr)].ref == IR_NULL) {
7044
      /* Force load */
7045
0
      zend_jit_use_reg(jit, op1_addr);
7046
0
    }
7047
0
  } else if (Z_MODE(op2_addr) == IS_REG) {
7048
0
    if (!has_concrete_type(op1_info & MAY_BE_ANY) && jit->ra[Z_SSA_VAR(op2_addr)].ref == IR_NULL) {
7049
      /* Force load */
7050
0
      zend_jit_use_reg(jit, op2_addr);
7051
0
    }
7052
0
  }
7053
7054
0
  if ((op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG)) {
7055
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
7056
0
      if_op1_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
7057
0
      ir_IF_TRUE(if_op1_long);
7058
0
    }
7059
0
    if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))) {
7060
0
      if_op1_long_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7061
0
      ir_IF_FALSE_cold(if_op1_long_op2_long);
7062
0
      if (op2_info & MAY_BE_DOUBLE) {
7063
0
        if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7064
0
          if_op1_long_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7065
0
          ir_IF_FALSE_cold(if_op1_long_op2_double);
7066
0
          ir_END_list(slow_inputs);
7067
0
          ir_IF_TRUE(if_op1_long_op2_double);
7068
0
        }
7069
0
        ref = zend_jit_cmp_long_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7070
0
        if (!ref) {
7071
0
          return 0;
7072
0
        }
7073
0
        ir_refs_add(end_inputs, ref);
7074
0
      } else {
7075
0
        ir_END_list(slow_inputs);
7076
0
      }
7077
0
      ir_IF_TRUE(if_op1_long_op2_long);
7078
0
    }
7079
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);
7080
0
    if (!ref) {
7081
0
      return 0;
7082
0
    }
7083
0
    ir_refs_add(end_inputs, ref);
7084
7085
0
    if (if_op1_long) {
7086
0
      ir_IF_FALSE_cold(if_op1_long);
7087
0
    }
7088
0
    if (op1_info & MAY_BE_DOUBLE) {
7089
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7090
0
        if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7091
0
        ir_IF_FALSE_cold(if_op1_double);
7092
0
        ir_END_list(slow_inputs);
7093
0
        ir_IF_TRUE(if_op1_double);
7094
0
      }
7095
0
      if (op2_info & MAY_BE_DOUBLE) {
7096
0
        if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7097
0
          if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7098
0
          ir_IF_TRUE(if_op1_double_op2_double);
7099
0
        }
7100
0
        ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7101
0
        if (!ref) {
7102
0
          return 0;
7103
0
        }
7104
0
        ir_refs_add(end_inputs, ref);
7105
0
        if (if_op1_double_op2_double) {
7106
0
          ir_IF_FALSE_cold(if_op1_double_op2_double);
7107
0
        }
7108
0
      }
7109
0
      if (!same_ops) {
7110
0
        if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) {
7111
0
          if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7112
0
          ir_IF_FALSE_cold(if_op1_double_op2_long);
7113
0
          ir_END_list(slow_inputs);
7114
0
          ir_IF_TRUE(if_op1_double_op2_long);
7115
0
        }
7116
0
        ref = zend_jit_cmp_double_long(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7117
0
        if (!ref) {
7118
0
          return 0;
7119
0
        }
7120
0
        ir_refs_add(end_inputs, ref);
7121
0
      } else if (if_op1_double_op2_double) {
7122
0
        ir_END_list(slow_inputs);
7123
0
      }
7124
0
    } else if (if_op1_long) {
7125
0
      ir_END_list(slow_inputs);
7126
0
    }
7127
0
  } else if ((op1_info & MAY_BE_DOUBLE) &&
7128
0
             !(op1_info & MAY_BE_LONG) &&
7129
0
             (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
7130
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE)) {
7131
0
      if_op1_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7132
0
      ir_IF_FALSE_cold(if_op1_double);
7133
0
      ir_END_list(slow_inputs);
7134
0
      ir_IF_TRUE(if_op1_double);
7135
0
    }
7136
0
    if (op2_info & MAY_BE_DOUBLE) {
7137
0
      if (!same_ops && (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7138
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7139
0
        ir_IF_TRUE(if_op1_double_op2_double);
7140
0
      }
7141
0
      ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7142
0
      if (!ref) {
7143
0
        return 0;
7144
0
      }
7145
0
      ir_refs_add(end_inputs, ref);
7146
0
      if (if_op1_double_op2_double) {
7147
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
7148
0
      }
7149
0
    }
7150
0
    if (!same_ops && (op2_info & MAY_BE_LONG)) {
7151
0
      if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
7152
0
        if_op1_double_op2_long = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
7153
0
        ir_IF_FALSE_cold(if_op1_double_op2_long);
7154
0
        ir_END_list(slow_inputs);
7155
0
        ir_IF_TRUE(if_op1_double_op2_long);
7156
0
      }
7157
0
      ref = zend_jit_cmp_double_long(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7158
0
      if (!ref) {
7159
0
        return 0;
7160
0
      }
7161
0
      ir_refs_add(end_inputs, ref);
7162
0
    } else if (if_op1_double_op2_double) {
7163
0
      ir_END_list(slow_inputs);
7164
0
    }
7165
0
  } else if ((op2_info & MAY_BE_DOUBLE) &&
7166
0
             !(op2_info & MAY_BE_LONG) &&
7167
0
             (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
7168
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE)) {
7169
0
      if_op2_double = jit_if_Z_TYPE(jit, op2_addr, IS_DOUBLE);
7170
0
      ir_IF_FALSE_cold(if_op2_double);
7171
0
      ir_END_list(slow_inputs);
7172
0
      ir_IF_TRUE(if_op2_double);
7173
0
    }
7174
0
    if (op1_info & MAY_BE_DOUBLE) {
7175
0
      if (!same_ops && (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
7176
0
        if_op1_double_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_DOUBLE);
7177
0
        ir_IF_TRUE(if_op1_double_op2_double);
7178
0
      }
7179
0
      ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7180
0
      if (!ref) {
7181
0
        return 0;
7182
0
      }
7183
0
      ir_refs_add(end_inputs, ref);
7184
0
      if (if_op1_double_op2_double) {
7185
0
        ir_IF_FALSE_cold(if_op1_double_op2_double);
7186
0
      }
7187
0
    }
7188
0
    if (!same_ops && (op1_info & MAY_BE_LONG)) {
7189
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_DOUBLE|MAY_BE_LONG))) {
7190
0
        if_op1_long_op2_double = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
7191
0
        ir_IF_FALSE_cold(if_op1_long_op2_double);
7192
0
        ir_END_list(slow_inputs);
7193
0
        ir_IF_TRUE(if_op1_long_op2_double);
7194
0
      }
7195
0
      ref = zend_jit_cmp_long_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7196
0
      if (!ref) {
7197
0
        return 0;
7198
0
      }
7199
0
      ir_refs_add(end_inputs, ref);
7200
0
    } else if (if_op1_double_op2_double) {
7201
0
      ir_END_list(slow_inputs);
7202
0
    }
7203
0
  }
7204
7205
0
  if (has_slow ||
7206
0
      (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) ||
7207
0
      (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE)))) {
7208
0
      ir_ref op1, op2, ref;
7209
7210
0
    if (slow_inputs) {
7211
0
      ir_MERGE_list(slow_inputs);
7212
0
    }
7213
0
    jit_SET_EX_OPLINE(jit, opline);
7214
7215
0
    if (Z_MODE(op1_addr) == IS_REG) {
7216
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7217
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7218
0
        return 0;
7219
0
      }
7220
0
      op1_addr = real_addr;
7221
0
    }
7222
0
    if (Z_MODE(op2_addr) == IS_REG) {
7223
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7224
0
      if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7225
0
        return 0;
7226
0
      }
7227
0
      op2_addr = real_addr;
7228
0
    }
7229
7230
0
    op1 = jit_ZVAL_ADDR(jit, op1_addr);
7231
0
    if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7232
0
      op1 = zend_jit_zval_check_undef(jit, op1, opline->op1.var, NULL, false);
7233
0
    }
7234
0
    op2 = jit_ZVAL_ADDR(jit, op2_addr);
7235
0
    if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
7236
0
      op2 = zend_jit_zval_check_undef(jit, op2, opline->op2.var, NULL, false);
7237
0
    }
7238
0
    ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_compare), op1, op2);
7239
0
    if (opline->opcode != ZEND_CASE) {
7240
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7241
0
    }
7242
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
7243
0
    if (may_throw) {
7244
0
      zend_jit_check_exception_undef_result(jit, opline);
7245
0
    }
7246
7247
0
    ref = zend_jit_cmp_slow(jit, ref, opline, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7248
0
    if (!ref) {
7249
0
      return 0;
7250
0
    }
7251
0
    ir_refs_add(end_inputs, ref);
7252
0
  }
7253
7254
0
  if (end_inputs->count) {
7255
0
    uint32_t n = end_inputs->count;
7256
7257
0
    if (smart_branch_opcode && !exit_addr) {
7258
0
      zend_basic_block *bb;
7259
0
      ir_ref ref;
7260
0
      uint32_t label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7261
0
        target_label2 : target_label;
7262
0
      uint32_t label2 = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7263
0
        target_label : target_label2;
7264
7265
0
      ZEND_ASSERT(jit->b >= 0);
7266
0
      bb = &jit->ssa->cfg.blocks[jit->b];
7267
0
      ZEND_ASSERT(bb->successors_count == 2);
7268
7269
0
      if (UNEXPECTED(bb->successors[0] == bb->successors[1])) {
7270
0
        ir_ref merge_inputs = IR_UNUSED;
7271
7272
0
        while (n) {
7273
0
          n--;
7274
0
          ir_IF_TRUE(end_inputs->refs[n]);
7275
0
          ir_END_list(merge_inputs);
7276
0
          ir_IF_FALSE(end_inputs->refs[n]);
7277
0
          ir_END_list(merge_inputs);
7278
0
        }
7279
0
        ir_MERGE_list(merge_inputs);
7280
0
        _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7281
0
      } else if (n == 1) {
7282
0
        ref = end_inputs->refs[0];
7283
0
        _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
7284
0
        _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
7285
0
      } else {
7286
0
        ir_ref true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7287
7288
0
        while (n) {
7289
0
          n--;
7290
0
          jit_IF_TRUE_FALSE_ex(jit, end_inputs->refs[n], label);
7291
0
          ir_END_list(true_inputs);
7292
0
          jit_IF_TRUE_FALSE_ex(jit, end_inputs->refs[n], label2);
7293
0
          ir_END_list(false_inputs);
7294
0
        }
7295
0
        ir_MERGE_list(true_inputs);
7296
0
        _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7297
0
        ir_MERGE_list(false_inputs);
7298
0
        _zend_jit_add_predecessor_ref(jit, label2, jit->b, ir_END());
7299
0
      }
7300
0
      jit->b = -1;
7301
0
    } else {
7302
0
      ir_MERGE_N(n, end_inputs->refs);
7303
0
    }
7304
0
  } else if (smart_branch_opcode && !exit_addr) {
7305
    /* dead code */
7306
0
    _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
7307
0
    jit->b = -1;
7308
0
  }
7309
7310
0
  return 1;
7311
0
}
7312
7313
static int zend_jit_identical(zend_jit_ctx   *jit,
7314
                              const zend_op  *opline,
7315
                              uint32_t        op1_info,
7316
                              zend_ssa_range *op1_range,
7317
                              zend_jit_addr   op1_addr,
7318
                              uint32_t        op2_info,
7319
                              zend_ssa_range *op2_range,
7320
                              zend_jit_addr   op2_addr,
7321
                              zend_jit_addr   res_addr,
7322
                              int             may_throw,
7323
                              uint8_t         smart_branch_opcode,
7324
                              uint32_t        target_label,
7325
                              uint32_t        target_label2,
7326
                              const void     *exit_addr,
7327
                              bool       skip_comparison)
7328
0
{
7329
0
  bool always_false = false, always_true = false;
7330
0
  ir_ref ref = IR_UNUSED;
7331
7332
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7333
0
    ir_ref op1 = jit_ZVAL_ADDR(jit, op1_addr);
7334
0
    op1 = zend_jit_zval_check_undef(jit, op1, opline->op1.var, opline, false);
7335
0
    op1_info |= MAY_BE_NULL;
7336
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1);
7337
0
  }
7338
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
7339
0
    ir_ref op2 = jit_ZVAL_ADDR(jit, op2_addr);
7340
0
    op2 = zend_jit_zval_check_undef(jit, op2, opline->op2.var, opline, false);
7341
0
    op2_info |= MAY_BE_NULL;
7342
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2);
7343
0
  }
7344
7345
0
  if ((op1_info & op2_info & MAY_BE_ANY) == 0) {
7346
0
    always_false = true;
7347
0
  } else if (has_concrete_type(op1_info)
7348
0
   && has_concrete_type(op2_info)
7349
0
   && concrete_type(op1_info) == concrete_type(op2_info)
7350
0
   && concrete_type(op1_info) <= IS_TRUE) {
7351
0
    always_true = true;
7352
0
  } else if (Z_MODE(op1_addr) == IS_CONST_ZVAL && Z_MODE(op2_addr) == IS_CONST_ZVAL) {
7353
0
    if (zend_is_identical(Z_ZV(op1_addr), Z_ZV(op2_addr))) {
7354
0
      always_true = true;
7355
0
    } else {
7356
0
      always_false = true;
7357
0
    }
7358
0
  }
7359
7360
0
  if (always_true) {
7361
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7362
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7363
0
    }
7364
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
7365
0
    if (!smart_branch_opcode
7366
0
     || smart_branch_opcode == ZEND_JMPZ_EX
7367
0
     || smart_branch_opcode == ZEND_JMPNZ_EX) {
7368
0
      jit_set_Z_TYPE_INFO(jit, res_addr, opline->opcode != ZEND_IS_NOT_IDENTICAL ? IS_TRUE : IS_FALSE);
7369
0
    }
7370
0
    if (may_throw) {
7371
0
      zend_jit_check_exception(jit);
7372
0
    }
7373
0
    if (exit_addr) {
7374
0
      if (smart_branch_opcode == ZEND_JMPNZ || smart_branch_opcode == ZEND_JMPNZ_EX) {
7375
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7376
0
      }
7377
0
    } else if (smart_branch_opcode) {
7378
0
      uint32_t label;
7379
7380
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7381
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7382
0
          target_label : target_label2;
7383
0
      } else {
7384
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7385
0
          target_label2 : target_label;
7386
0
      }
7387
0
      _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7388
0
      jit->b = -1;
7389
0
    }
7390
0
    return 1;
7391
0
  } else if (always_false) {
7392
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7393
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7394
0
    }
7395
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
7396
0
    if (!smart_branch_opcode
7397
0
     || smart_branch_opcode == ZEND_JMPZ_EX
7398
0
     || smart_branch_opcode == ZEND_JMPNZ_EX) {
7399
0
      jit_set_Z_TYPE_INFO(jit, res_addr, opline->opcode != ZEND_IS_NOT_IDENTICAL ? IS_FALSE : IS_TRUE);
7400
0
    }
7401
0
    if (may_throw) {
7402
0
      zend_jit_check_exception(jit);
7403
0
    }
7404
0
    if (exit_addr) {
7405
0
      if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
7406
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7407
0
      }
7408
0
    } else if (smart_branch_opcode) {
7409
0
      uint32_t label;
7410
7411
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7412
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7413
0
          target_label2 : target_label;
7414
0
      } else {
7415
0
        label = (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ?
7416
0
          target_label : target_label2;
7417
0
      }
7418
0
      _zend_jit_add_predecessor_ref(jit, label, jit->b, ir_END());
7419
0
      jit->b = -1;
7420
0
    }
7421
0
    return 1;
7422
0
  }
7423
7424
0
  if ((opline->op1_type & (IS_CV|IS_VAR)) && (op1_info & MAY_BE_REF)) {
7425
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
7426
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7427
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
7428
0
  }
7429
0
  if ((opline->op2_type & (IS_CV|IS_VAR)) && (op2_info & MAY_BE_REF)) {
7430
0
    ref = jit_ZVAL_ADDR(jit, op2_addr);
7431
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7432
0
    op2_addr = ZEND_ADDR_REF_ZVAL(ref);
7433
0
  }
7434
7435
0
  if ((op1_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG &&
7436
0
      (op2_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
7437
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);
7438
0
    if (!ref) {
7439
0
      return 0;
7440
0
    }
7441
0
  } else if ((op1_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE &&
7442
0
             (op2_info & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE) {
7443
0
    ref = zend_jit_cmp_double_double(jit, opline, op1_addr, op2_addr, res_addr, smart_branch_opcode, target_label, target_label2, exit_addr);
7444
0
    if (!ref) {
7445
0
      return 0;
7446
0
    }
7447
0
  } else {
7448
0
    if (opline->op1_type != IS_CONST) {
7449
0
      if (Z_MODE(op1_addr) == IS_REG) {
7450
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7451
0
        if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7452
0
          return 0;
7453
0
        }
7454
0
        op1_addr = real_addr;
7455
0
      }
7456
0
    }
7457
0
    if (opline->op2_type != IS_CONST) {
7458
0
      if (Z_MODE(op2_addr) == IS_REG) {
7459
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7460
0
        if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7461
0
          return 0;
7462
0
        }
7463
0
      }
7464
0
    }
7465
7466
0
    if (Z_MODE(op1_addr) == IS_CONST_ZVAL && Z_TYPE_P(Z_ZV(op1_addr)) <= IS_TRUE) {
7467
0
      zval *val = Z_ZV(op1_addr);
7468
7469
0
      ref = ir_EQ(jit_Z_TYPE(jit, op2_addr), ir_CONST_U8(Z_TYPE_P(val)));
7470
0
    } else if (Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_TYPE_P(Z_ZV(op2_addr)) <= IS_TRUE) {
7471
0
      zval *val = Z_ZV(op2_addr);
7472
7473
0
      ref = ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(Z_TYPE_P(val)));
7474
0
    } else {
7475
0
      if (Z_MODE(op1_addr) == IS_REG) {
7476
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
7477
0
        if (!zend_jit_spill_store_inv(jit, op1_addr, real_addr, op1_info)) {
7478
0
          return 0;
7479
0
        }
7480
0
        op1_addr = real_addr;
7481
0
      }
7482
0
      if (Z_MODE(op2_addr) == IS_REG) {
7483
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
7484
0
        if (!zend_jit_spill_store_inv(jit, op2_addr, real_addr, op2_info)) {
7485
0
          return 0;
7486
0
        }
7487
0
        op2_addr = real_addr;
7488
0
      }
7489
0
      if (may_throw) {
7490
0
        jit_SET_EX_OPLINE(jit, opline);
7491
0
      }
7492
7493
0
      ref = ir_CALL_2(IR_BOOL, ir_CONST_FC_FUNC(zend_is_identical),
7494
0
        jit_ZVAL_ADDR(jit, op1_addr),
7495
0
        jit_ZVAL_ADDR(jit, op2_addr));
7496
0
    }
7497
7498
0
    if (!smart_branch_opcode || smart_branch_opcode == ZEND_JMPNZ_EX || smart_branch_opcode == ZEND_JMPZ_EX) {
7499
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7500
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7501
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7502
0
      } else {
7503
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7504
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7505
0
      }
7506
0
    }
7507
0
    if (opline->opcode != ZEND_CASE_STRICT) {
7508
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7509
0
    }
7510
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
7511
0
    if (may_throw) {
7512
0
      zend_jit_check_exception_undef_result(jit, opline);
7513
0
    }
7514
0
    if (exit_addr) {
7515
0
      if (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) {
7516
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7517
0
      } else {
7518
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7519
0
      }
7520
0
    } else if (smart_branch_opcode) {
7521
0
      if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
7522
        /* swap labels */
7523
0
        uint32_t tmp = target_label;
7524
0
        target_label = target_label2;
7525
0
        target_label2 = tmp;
7526
0
      }
7527
0
      ref = jit_IF_ex(jit, ref,
7528
0
        (smart_branch_opcode == ZEND_JMPZ || smart_branch_opcode == ZEND_JMPZ_EX) ? target_label2 : target_label);
7529
0
    }
7530
0
  }
7531
7532
0
  if (smart_branch_opcode && !exit_addr) {
7533
0
    zend_basic_block *bb;
7534
7535
0
    ZEND_ASSERT(jit->b >= 0);
7536
0
    bb = &jit->ssa->cfg.blocks[jit->b];
7537
0
    ZEND_ASSERT(bb->successors_count == 2);
7538
7539
0
    if (bb->successors_count == 2 && bb->successors[0] == bb->successors[1]) {
7540
0
      ir_IF_TRUE(ref);
7541
0
      ir_MERGE_WITH_EMPTY_FALSE(ref);
7542
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
7543
0
    } else {
7544
0
      ZEND_ASSERT(bb->successors_count == 2);
7545
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
7546
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
7547
0
    }
7548
0
    jit->b = -1;
7549
0
  }
7550
7551
0
  return 1;
7552
0
}
7553
7554
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)
7555
0
{
7556
0
  uint32_t true_label = -1;
7557
0
  uint32_t false_label = -1;
7558
0
  bool set_bool = false;
7559
0
  bool set_bool_not = false;
7560
0
  bool always_true = false, always_false = false;
7561
0
  ir_ref ref, end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7562
0
  ir_type type = IR_UNUSED;
7563
7564
0
  if (branch_opcode == ZEND_BOOL) {
7565
0
    set_bool = true;
7566
0
  } else if (branch_opcode == ZEND_BOOL_NOT) {
7567
0
    set_bool = true;
7568
0
    set_bool_not = true;
7569
0
  } else if (branch_opcode == ZEND_JMPZ) {
7570
0
    true_label = target_label2;
7571
0
    false_label = target_label;
7572
0
  } else if (branch_opcode == ZEND_JMPNZ) {
7573
0
    true_label = target_label;
7574
0
    false_label = target_label2;
7575
0
  } else if (branch_opcode == ZEND_JMPZ_EX) {
7576
0
    set_bool = true;
7577
0
    true_label = target_label2;
7578
0
    false_label = target_label;
7579
0
  } else if (branch_opcode == ZEND_JMPNZ_EX) {
7580
0
    set_bool = true;
7581
0
    true_label = target_label;
7582
0
    false_label = target_label2;
7583
0
  } else {
7584
0
    ZEND_UNREACHABLE();
7585
0
  }
7586
7587
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_REF)) {
7588
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
7589
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
7590
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
7591
0
  }
7592
7593
0
  if (Z_MODE(op1_addr) == IS_CONST_ZVAL
7594
    /* NAN Value must cause a warning to be emitted */
7595
0
    && (Z_TYPE_P(Z_ZV(op1_addr)) != IS_DOUBLE || !zend_isnan(Z_DVAL_P(Z_ZV(op1_addr))))) {
7596
0
    if (zend_is_true(Z_ZV(op1_addr))) {
7597
0
      always_true = true;
7598
0
    } else {
7599
0
      always_false = true;
7600
0
    }
7601
0
  } else if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE)) {
7602
0
    if (!(op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)-MAY_BE_TRUE))) {
7603
0
      always_true = true;
7604
0
    } else if (!(op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE)))) {
7605
0
      if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
7606
0
        ref = jit_ZVAL_ADDR(jit, op1_addr);
7607
0
        zend_jit_zval_check_undef(jit, ref, opline->op1.var, opline, false);
7608
0
      }
7609
0
      always_false = true;
7610
0
    }
7611
0
  }
7612
7613
0
  if (always_true) {
7614
0
    if (set_bool) {
7615
0
      jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7616
0
    }
7617
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7618
0
    if (may_throw) {
7619
0
      zend_jit_check_exception(jit);
7620
0
    }
7621
0
    if (true_label != (uint32_t)-1) {
7622
0
      ZEND_ASSERT(exit_addr == NULL);
7623
0
      _zend_jit_add_predecessor_ref(jit, true_label, jit->b, ir_END());
7624
0
      jit->b = -1;
7625
0
    }
7626
0
    return 1;
7627
0
  } else if (always_false) {
7628
0
    if (set_bool) {
7629
0
      jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7630
0
    }
7631
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
7632
0
    if (may_throw) {
7633
0
      zend_jit_check_exception(jit);
7634
0
    }
7635
0
    if (false_label != (uint32_t)-1) {
7636
0
      ZEND_ASSERT(exit_addr == NULL);
7637
0
      _zend_jit_add_predecessor_ref(jit, false_label, jit->b, ir_END());
7638
0
      jit->b = -1;
7639
0
    }
7640
0
    return 1;
7641
0
  }
7642
7643
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE)) {
7644
0
    type = jit_Z_TYPE(jit, op1_addr);
7645
0
    if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
7646
0
      ir_ref if_type = ir_IF(ir_LT(type, ir_CONST_U8(IS_TRUE)));
7647
7648
0
      ir_IF_TRUE_cold(if_type);
7649
7650
0
      if (op1_info & MAY_BE_UNDEF) {
7651
0
        zend_jit_type_check_undef(jit,
7652
0
          type,
7653
0
          opline->op1.var,
7654
0
          opline, true, false, true);
7655
0
      }
7656
0
      if (set_bool) {
7657
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7658
0
      }
7659
0
      if (exit_addr) {
7660
0
        if (branch_opcode == ZEND_JMPNZ || branch_opcode == ZEND_JMPNZ_EX) {
7661
0
          ir_END_list(end_inputs);
7662
0
        } else {
7663
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7664
0
        }
7665
0
      } else if (false_label != (uint32_t)-1) {
7666
0
        ir_END_list(false_inputs);
7667
0
      } else {
7668
0
        ir_END_list(end_inputs);
7669
0
      }
7670
0
      ir_IF_FALSE(if_type);
7671
0
    }
7672
7673
0
    if (op1_info & MAY_BE_TRUE) {
7674
0
      ir_ref if_type = IR_UNUSED;
7675
7676
0
      if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE))) {
7677
0
        if_type = ir_IF(ir_EQ(type, ir_CONST_U8(IS_TRUE)));
7678
7679
0
        ir_IF_TRUE(if_type);
7680
0
      }
7681
0
      if (set_bool) {
7682
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7683
0
      }
7684
0
      if (exit_addr) {
7685
0
        if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7686
0
          ir_END_list(end_inputs);
7687
0
        } else {
7688
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7689
0
        }
7690
0
      } else if (true_label != (uint32_t)-1) {
7691
0
        ir_END_list(true_inputs);
7692
0
      } else {
7693
0
        ir_END_list(end_inputs);
7694
0
      }
7695
0
      if (if_type) {
7696
0
        ir_IF_FALSE(if_type);
7697
0
      }
7698
0
    }
7699
0
  }
7700
7701
0
  if (op1_info & MAY_BE_LONG) {
7702
0
    ir_ref if_long = IR_UNUSED;
7703
0
    ir_ref ref;
7704
7705
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG))) {
7706
0
      if (!type) {
7707
0
        type = jit_Z_TYPE(jit, op1_addr);
7708
0
      }
7709
0
      if_long = ir_IF(ir_EQ(type, ir_CONST_U8(IS_LONG)));
7710
0
      ir_IF_TRUE(if_long);
7711
0
    }
7712
0
    ref = jit_Z_LVAL(jit, op1_addr);
7713
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7714
0
      ref = ir_NE(ref, ir_CONST_LONG(0));
7715
0
      if (set_bool_not) {
7716
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7717
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7718
0
      } else {
7719
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7720
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7721
0
      }
7722
0
      ir_END_list(end_inputs);
7723
0
    } else if (exit_addr) {
7724
0
      if (set_bool) {
7725
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7726
0
          ir_ADD_U32(ir_ZEXT_U32(ir_NE(ref, ir_CONST_LONG(0))), ir_CONST_U32(IS_FALSE)));
7727
0
      }
7728
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7729
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7730
0
      } else {
7731
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7732
0
      }
7733
0
      ir_END_list(end_inputs);
7734
0
    } else {
7735
0
      ir_ref if_val = ir_IF(ref);
7736
0
      ir_IF_TRUE(if_val);
7737
0
      if (set_bool) {
7738
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7739
0
      }
7740
0
      ir_END_list(true_inputs);
7741
0
      ir_IF_FALSE(if_val);
7742
0
      if (set_bool) {
7743
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7744
0
      }
7745
0
      ir_END_list(false_inputs);
7746
0
    }
7747
0
    if (if_long) {
7748
0
      ir_IF_FALSE(if_long);
7749
0
    }
7750
0
  }
7751
7752
0
  if (op1_info & MAY_BE_DOUBLE) {
7753
0
    ir_ref if_double = IR_UNUSED;
7754
0
    ir_ref ref;
7755
7756
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))) {
7757
0
      if (!type) {
7758
0
        type = jit_Z_TYPE(jit, op1_addr);
7759
0
      }
7760
0
      if_double = ir_IF(ir_EQ(type, ir_CONST_U8(IS_DOUBLE)));
7761
0
      ir_IF_TRUE(if_double);
7762
0
    }
7763
7764
0
    ir_ref dval = jit_Z_DVAL(jit, op1_addr);
7765
0
    ir_ref is_nan = ir_NE(dval, dval);
7766
0
    ir_ref if_val = ir_IF(is_nan);
7767
0
    ir_IF_TRUE_cold(if_val);
7768
0
    jit_SET_EX_OPLINE(jit, opline);
7769
0
    ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_nan_coerced_to_type_warning));
7770
0
    ir_MERGE_WITH_EMPTY_FALSE(if_val);
7771
7772
0
    ref = ir_NE(dval, ir_CONST_DOUBLE(0.0));
7773
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7774
0
      if (set_bool_not) {
7775
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7776
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7777
0
      } else {
7778
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7779
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7780
0
      }
7781
0
      ir_END_list(end_inputs);
7782
0
    } else if (exit_addr) {
7783
0
        if (set_bool) {
7784
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7785
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7786
0
        }
7787
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7788
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7789
0
      } else {
7790
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7791
0
      }
7792
0
      ir_END_list(end_inputs);
7793
0
    } else {
7794
0
      ir_ref if_val = ir_IF(ref);
7795
0
      ir_IF_TRUE(if_val);
7796
0
      if (set_bool) {
7797
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7798
0
      }
7799
0
      ir_END_list(true_inputs);
7800
0
      ir_IF_FALSE(if_val);
7801
0
      if (set_bool) {
7802
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7803
0
      }
7804
0
      ir_END_list(false_inputs);
7805
0
    }
7806
0
    if (if_double) {
7807
0
      ir_IF_FALSE(if_double);
7808
0
    }
7809
0
  }
7810
7811
0
  if (op1_info & (MAY_BE_ANY - (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE))) {
7812
0
    jit_SET_EX_OPLINE(jit, opline);
7813
0
    ref = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_is_true), jit_ZVAL_ADDR(jit, op1_addr));
7814
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
7815
0
    if (may_throw) {
7816
0
      zend_jit_check_exception_undef_result(jit, opline);
7817
0
    }
7818
0
    if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT) {
7819
0
      if (set_bool_not) {
7820
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7821
0
          ir_SUB_U32(ir_CONST_U32(IS_TRUE), ir_ZEXT_U32(ref)));
7822
0
      } else {
7823
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7824
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7825
0
      }
7826
0
      if (end_inputs) {
7827
0
        ir_END_list(end_inputs);
7828
0
      }
7829
0
    } else if (exit_addr) {
7830
0
      if (set_bool) {
7831
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7832
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
7833
0
      }
7834
0
      if (branch_opcode == ZEND_JMPZ || branch_opcode == ZEND_JMPZ_EX) {
7835
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
7836
0
      } else {
7837
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
7838
0
      }
7839
0
      if (end_inputs) {
7840
0
        ir_END_list(end_inputs);
7841
0
      }
7842
0
    } else {
7843
0
      ir_ref if_val = ir_IF(ref);
7844
0
      ir_IF_TRUE(if_val);
7845
0
      if (set_bool) {
7846
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_FALSE : IS_TRUE);
7847
0
      }
7848
0
      ir_END_list(true_inputs);
7849
0
      ir_IF_FALSE(if_val);
7850
0
      if (set_bool) {
7851
0
        jit_set_Z_TYPE_INFO(jit, res_addr, set_bool_not ? IS_TRUE : IS_FALSE);
7852
0
      }
7853
0
      ir_END_list(false_inputs);
7854
0
    }
7855
0
  }
7856
7857
0
  if (branch_opcode == ZEND_BOOL || branch_opcode == ZEND_BOOL_NOT || exit_addr) {
7858
0
    if (end_inputs) {
7859
0
      ir_MERGE_list(end_inputs);
7860
0
    }
7861
0
  } else {
7862
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
7863
0
  }
7864
7865
0
  return 1;
7866
0
}
7867
7868
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)
7869
0
{
7870
0
  uint32_t defined_label = (uint32_t)-1;
7871
0
  uint32_t undefined_label = (uint32_t)-1;
7872
0
  zval *zv = RT_CONSTANT(opline, opline->op1);
7873
0
  zend_jit_addr res_addr = 0;
7874
0
  ir_ref ref, ref2, if_set, if_zero, if_set2;
7875
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
7876
7877
0
  if (smart_branch_opcode && !exit_addr) {
7878
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7879
0
      defined_label = target_label2;
7880
0
      undefined_label = target_label;
7881
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
7882
0
      defined_label = target_label;
7883
0
      undefined_label = target_label2;
7884
0
    } else {
7885
0
      ZEND_UNREACHABLE();
7886
0
    }
7887
0
  } else {
7888
0
    res_addr = RES_ADDR();
7889
0
  }
7890
7891
  // if (CACHED_PTR(opline->extended_value)) {
7892
0
  ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value));
7893
7894
0
  if_set = ir_IF(ref);
7895
7896
0
  ir_IF_FALSE_cold(if_set);
7897
0
  if_zero = ir_END();
7898
7899
0
  ir_IF_TRUE(if_set);
7900
0
  if_set2 = ir_IF(ir_AND_A(ref, ir_CONST_ADDR(CACHE_SPECIAL)));
7901
0
  ir_IF_FALSE(if_set2);
7902
7903
0
  if (exit_addr) {
7904
0
    if (smart_branch_opcode == ZEND_JMPNZ) {
7905
0
      jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7906
0
    } else {
7907
0
      ir_END_list(end_inputs);
7908
0
    }
7909
0
  } else if (smart_branch_opcode) {
7910
0
    ir_END_list(true_inputs);
7911
0
  } else {
7912
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
7913
0
    ir_END_list(end_inputs);
7914
0
  }
7915
7916
0
  ir_IF_TRUE_cold(if_set2);
7917
7918
0
  ref2 = jit_EG(zend_constants);
7919
0
  ref = ir_SHR_A(ref, ir_CONST_ADDR(1));
7920
0
  if (sizeof(void*) == 8) {
7921
0
    ref = ir_TRUNC_U32(ref);
7922
0
  }
7923
0
  ref2 = ir_EQ(ref, ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(ref2), offsetof(HashTable, nNumOfElements))));
7924
0
  ref2 = ir_IF(ref2);
7925
0
  ir_IF_TRUE(ref2);
7926
7927
0
  if (exit_addr) {
7928
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7929
0
      jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
7930
0
    } else {
7931
0
      ir_END_list(end_inputs);
7932
0
    }
7933
0
  } else if (smart_branch_opcode) {
7934
0
    ir_END_list(false_inputs);
7935
0
  } else {
7936
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
7937
0
    ir_END_list(end_inputs);
7938
0
  }
7939
7940
0
  ir_IF_FALSE(ref2);
7941
0
  ir_MERGE_2(if_zero, ir_END());
7942
7943
0
  jit_SET_EX_OPLINE(jit, opline);
7944
0
  ref2 = ir_NE(ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_check_constant), ir_CONST_ADDR(zv)), IR_NULL);
7945
0
  if (exit_addr) {
7946
0
    if (smart_branch_opcode == ZEND_JMPZ) {
7947
0
      ir_GUARD(ref2, ir_CONST_ADDR(exit_addr));
7948
0
    } else {
7949
0
      ir_GUARD_NOT(ref2, ir_CONST_ADDR(exit_addr));
7950
0
    }
7951
0
    ir_END_list(end_inputs);
7952
0
  } else if (smart_branch_opcode) {
7953
0
    ref2 = ir_IF(ref2);
7954
0
    ir_IF_TRUE(ref2);
7955
0
    ir_END_list(true_inputs);
7956
0
    ir_IF_FALSE(ref2);
7957
0
    ir_END_list(false_inputs);
7958
0
  } else {
7959
0
    jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
7960
0
      ir_ADD_U32(ir_ZEXT_U32(ref2), ir_CONST_U32(IS_FALSE)));
7961
0
    ir_END_list(end_inputs);
7962
0
  }
7963
7964
0
  if (!smart_branch_opcode || exit_addr) {
7965
0
    if (end_inputs) {
7966
0
      ir_MERGE_list(end_inputs);
7967
0
    }
7968
0
  } else {
7969
0
    _zend_jit_merge_smart_branch_inputs(jit, defined_label, undefined_label, true_inputs, false_inputs);
7970
0
  }
7971
7972
0
  return 1;
7973
0
}
7974
7975
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)
7976
0
{
7977
0
  zend_jit_addr reg_addr = ZEND_ADDR_REF_ZVAL(zend_jit_deopt_rload(jit, IR_ADDR, reg));
7978
0
  ir_ref if_def = ir_IF(jit_Z_TYPE(jit, reg_addr));
7979
7980
0
  ir_IF_FALSE_cold(if_def);
7981
7982
0
  if (flags & ZEND_JIT_EXIT_RESTORE_CALL) {
7983
0
    if (!zend_jit_save_call_chain(jit, -1)) {
7984
0
      return 0;
7985
0
    }
7986
0
  }
7987
7988
0
  if ((opline-1)->opcode != ZEND_FETCH_CONSTANT
7989
0
   && (opline-1)->opcode != ZEND_FETCH_LIST_R
7990
0
   && ((opline-1)->op1_type & (IS_VAR|IS_TMP_VAR))
7991
0
   && !(flags & ZEND_JIT_EXIT_FREE_OP1)) {
7992
0
    zend_jit_addr val_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline-1)->op1.var);
7993
7994
0
    zend_jit_zval_try_addref(jit, val_addr);
7995
0
  }
7996
7997
0
  jit_LOAD_IP_ADDR(jit, opline - 1);
7998
7999
  /* We can't use trace_escape() because opcode handler may be overridden by JIT */
8000
0
  zend_jit_op_array_trace_extension *jit_extension =
8001
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8002
0
  size_t offset = jit_extension->offset;
8003
0
  ir_ref ref = ir_CONST_FC_FUNC(ZEND_OP_TRACE_INFO((opline - 1), offset)->orig_handler);
8004
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
8005
0
    ir_TAILCALL(IR_OPCODE_HANDLER_RET, ref);
8006
0
  } else {
8007
0
    ir_ref opline_ref = ir_CALL_2(IR_OPCODE_HANDLER_RET, ref, jit_FP(jit), jit_IP(jit));
8008
0
    zend_jit_vm_enter(jit, opline_ref);
8009
0
  }
8010
8011
0
  ir_IF_TRUE(if_def);
8012
8013
0
  return 1;
8014
0
}
8015
8016
static int zend_jit_restore_zval(zend_jit_ctx *jit, int var, int8_t reg)
8017
0
{
8018
0
  zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
8019
0
  zend_jit_addr reg_addr = ZEND_ADDR_REF_ZVAL(zend_jit_deopt_rload(jit, IR_ADDR, reg));
8020
8021
  // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8022
0
  jit_ZVAL_COPY(jit, var_addr, MAY_BE_ANY, reg_addr, MAY_BE_ANY, true);
8023
0
  return 1;
8024
0
}
8025
8026
static zend_jit_addr zend_jit_guard_fetch_result_type(zend_jit_ctx         *jit,
8027
                                                      const zend_op        *opline,
8028
                                                      zend_jit_addr         val_addr,
8029
                                                      uint8_t               type,
8030
                                                      bool                  deref,
8031
                                                      uint32_t              flags,
8032
                                                      bool                  op1_avoid_refcounting)
8033
0
{
8034
0
  zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
8035
0
  int32_t exit_point;
8036
0
  const void *res_exit_addr = NULL;
8037
0
  ir_ref end1 = IR_UNUSED, ref1 = IR_UNUSED;
8038
0
  ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
8039
0
  uint32_t old_op1_info = 0;
8040
0
  uint32_t old_info;
8041
0
  ir_ref old_ref;
8042
8043
8044
0
  if (opline->op1_type & (IS_VAR|IS_TMP_VAR|IS_CV)) {
8045
0
    old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
8046
0
    if (op1_avoid_refcounting
8047
0
     || ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
8048
0
      && STACK_FLAGS(stack, EX_VAR_TO_NUM(opline->op1.var)) & (ZREG_ZVAL_ADDREF|ZREG_THIS))) {
8049
0
      SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
8050
0
    }
8051
0
  }
8052
0
  old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
8053
0
  old_ref = STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
8054
0
  CLEAR_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
8055
0
  SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
8056
8057
0
  if (deref) {
8058
0
    ir_ref if_type;
8059
8060
0
    if (type == IS_NULL && (opline->opcode == ZEND_FETCH_DIM_IS || opline->opcode == ZEND_FETCH_OBJ_IS)) {
8061
0
      if_type = ir_IF(ir_ULE(jit_Z_TYPE(jit, val_addr), ir_CONST_U8(type)));
8062
0
    } else {
8063
0
      if_type = jit_if_Z_TYPE(jit, val_addr, type);
8064
0
    }
8065
0
    ir_IF_TRUE(if_type);
8066
0
    end1 = ir_END();
8067
0
    ref1 = ref;
8068
0
    ir_IF_FALSE_cold(if_type);
8069
8070
0
    SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ref, ZREG_ZVAL_COPY);
8071
0
    exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
8072
0
    res_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8073
0
    if (!res_exit_addr) {
8074
0
      return 0;
8075
0
    }
8076
8077
0
    jit_guard_Z_TYPE(jit, val_addr, IS_REFERENCE, res_exit_addr);
8078
0
    ref = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
8079
0
    val_addr = ZEND_ADDR_REF_ZVAL(ref);
8080
0
  }
8081
8082
0
  SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ref, ZREG_ZVAL_COPY);
8083
0
  exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
8084
0
  res_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8085
0
  if (!res_exit_addr) {
8086
0
    return 0;
8087
0
  }
8088
8089
0
  if (!deref && type == IS_NULL && (opline->opcode == ZEND_FETCH_DIM_IS || opline->opcode == ZEND_FETCH_OBJ_IS)) {
8090
0
    ir_GUARD(ir_ULE(jit_Z_TYPE(jit, val_addr), ir_CONST_U8(type)), ir_CONST_ADDR(res_exit_addr));
8091
0
  } else {
8092
0
    jit_guard_Z_TYPE(jit, val_addr, type, res_exit_addr);
8093
0
  }
8094
8095
0
  if (deref) {
8096
0
    ir_MERGE_WITH(end1);
8097
0
    ref = ir_PHI_2(IR_ADDR, ref, ref1);
8098
0
  }
8099
8100
0
  val_addr = ZEND_ADDR_REF_ZVAL(ref);
8101
8102
0
  SET_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var), old_ref);
8103
0
  SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
8104
0
  if (opline->op1_type & (IS_VAR|IS_TMP_VAR|IS_CV)) {
8105
0
    SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
8106
0
  }
8107
8108
0
  return val_addr;
8109
0
}
8110
8111
static int zend_jit_fetch_constant(zend_jit_ctx         *jit,
8112
                                   const zend_op        *opline,
8113
                                   const zend_op_array  *op_array,
8114
                                   zend_ssa             *ssa,
8115
                                   const zend_ssa_op    *ssa_op,
8116
                                   zend_jit_addr         res_addr)
8117
0
{
8118
0
  zval *zv = RT_CONSTANT(opline, opline->op2) + 1;
8119
0
  uint32_t res_info = RES_INFO();
8120
0
  ir_ref ref, ref2, if_set, if_special, not_set_path, special_path, fast_path;
8121
8122
  // JIT: c = CACHED_PTR(opline->extended_value);
8123
0
  ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value));
8124
8125
  // JIT: if (c != NULL)
8126
0
  if_set = ir_IF(ref);
8127
8128
0
  if (!zend_jit_is_persistent_constant(zv, opline->op1.num)) {
8129
    // JIT: if (!IS_SPECIAL_CACHE_VAL(c))
8130
0
    ir_IF_FALSE_cold(if_set);
8131
0
    not_set_path = ir_END();
8132
0
    ir_IF_TRUE(if_set);
8133
0
    if_special = ir_IF(ir_AND_A(ref, ir_CONST_ADDR(CACHE_SPECIAL)));
8134
0
    ir_IF_TRUE_cold(if_special);
8135
0
    special_path = ir_END();
8136
0
    ir_IF_FALSE(if_special);
8137
0
    fast_path = ir_END();
8138
0
    ir_MERGE_2(not_set_path, special_path);
8139
0
  } else {
8140
0
    ir_IF_TRUE(if_set);
8141
0
    fast_path = ir_END();
8142
0
    ir_IF_FALSE_cold(if_set);
8143
0
  }
8144
8145
  // JIT: zend_jit_get_constant(RT_CONSTANT(opline, opline->op2) + 1, opline->op1.num);
8146
0
  jit_SET_EX_OPLINE(jit, opline);
8147
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_get_constant),
8148
0
    ir_CONST_ADDR(zv),
8149
0
    ir_CONST_U32(opline->op1.num));
8150
0
  ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
8151
8152
0
  ir_MERGE_WITH(fast_path);
8153
0
  ref = ir_PHI_2(IR_ADDR, ref2, ref);
8154
8155
0
  if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
8156
0
    uint8_t type = concrete_type(res_info);
8157
0
    zend_jit_addr const_addr = ZEND_ADDR_REF_ZVAL(ref);
8158
8159
0
    const_addr = zend_jit_guard_fetch_result_type(jit, opline, const_addr, type, false, 0, false);
8160
0
    if (!const_addr) {
8161
0
      return 0;
8162
0
    }
8163
8164
0
    res_info &= ~MAY_BE_GUARD;
8165
0
    ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
8166
8167
    // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8168
0
    jit_ZVAL_COPY(jit, res_addr, MAY_BE_ANY, const_addr, res_info, true);
8169
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
8170
0
      return 0;
8171
0
    }
8172
0
  } else {
8173
0
    ir_ref const_addr = ZEND_ADDR_REF_ZVAL(ref);
8174
8175
    // JIT: ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); (no dup)
8176
0
    jit_ZVAL_COPY(jit, res_addr, MAY_BE_ANY, const_addr, MAY_BE_ANY, true);
8177
0
  }
8178
8179
8180
0
  return 1;
8181
0
}
8182
8183
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)
8184
0
{
8185
0
  uint32_t  mask;
8186
0
  zend_jit_addr op1_addr = OP1_ADDR();
8187
0
  zend_jit_addr res_addr = 0;
8188
0
  uint32_t true_label = -1, false_label = -1;
8189
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
8190
8191
  // TODO: support for is_resource() ???
8192
0
  ZEND_ASSERT(opline->extended_value != MAY_BE_RESOURCE);
8193
8194
0
  if (smart_branch_opcode && !exit_addr) {
8195
0
    if (smart_branch_opcode == ZEND_JMPZ) {
8196
0
      true_label = target_label2;
8197
0
      false_label = target_label;
8198
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
8199
0
      true_label = target_label;
8200
0
      false_label = target_label2;
8201
0
    } else {
8202
0
      ZEND_UNREACHABLE();
8203
0
    }
8204
0
  } else {
8205
0
    res_addr = RES_ADDR();
8206
0
  }
8207
8208
0
  if (op1_info & MAY_BE_UNDEF) {
8209
0
    ir_ref if_def = IR_UNUSED;
8210
8211
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
8212
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
8213
0
      ir_IF_FALSE_cold(if_def);
8214
0
    }
8215
8216
0
    jit_SET_EX_OPLINE(jit, opline);
8217
0
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
8218
0
    zend_jit_check_exception_undef_result(jit, opline);
8219
0
    if (opline->extended_value & MAY_BE_NULL) {
8220
0
      if (exit_addr) {
8221
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
8222
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8223
0
        } else {
8224
0
          ir_END_list(end_inputs);
8225
0
        }
8226
0
      } else if (smart_branch_opcode) {
8227
0
        ir_END_list(true_inputs);
8228
0
      } else {
8229
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8230
0
        ir_END_list(end_inputs);
8231
0
      }
8232
0
    } else {
8233
0
      if (exit_addr) {
8234
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8235
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8236
0
        } else {
8237
0
          ir_END_list(end_inputs);
8238
0
        }
8239
0
      } else if (smart_branch_opcode) {
8240
0
        ir_END_list(false_inputs);
8241
0
      } else {
8242
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8243
0
        if (if_def) {
8244
0
          ir_END_list(end_inputs);
8245
0
        }
8246
0
      }
8247
0
    }
8248
8249
0
    if (if_def) {
8250
0
      ir_IF_TRUE(if_def);
8251
0
      op1_info |= MAY_BE_NULL;
8252
0
    }
8253
0
  }
8254
8255
0
  if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
8256
0
    mask = opline->extended_value;
8257
0
    if (!(op1_info & MAY_BE_GUARD) && !(op1_info & (MAY_BE_ANY - mask))) {
8258
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8259
0
      if (exit_addr) {
8260
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
8261
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8262
0
        } else if (end_inputs) {
8263
0
          ir_END_list(end_inputs);
8264
0
        }
8265
0
      } else if (smart_branch_opcode) {
8266
0
        ir_END_list(true_inputs);
8267
0
      } else {
8268
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8269
0
        ir_END_list(end_inputs);
8270
0
      }
8271
0
      } else if (!(op1_info & MAY_BE_GUARD) && !(op1_info & mask)) {
8272
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8273
0
      if (exit_addr) {
8274
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8275
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
8276
0
        } else if (end_inputs) {
8277
0
          ir_END_list(end_inputs);
8278
0
        }
8279
0
      } else if (smart_branch_opcode) {
8280
0
        ir_END_list(false_inputs);
8281
0
      } else {
8282
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8283
0
        ir_END_list(end_inputs);
8284
0
      }
8285
0
    } else {
8286
0
      ir_ref ref;
8287
0
      bool invert = false;
8288
0
      uint8_t type;
8289
8290
0
      switch (mask) {
8291
0
        case MAY_BE_NULL:   type = IS_NULL;   break;
8292
0
        case MAY_BE_FALSE:  type = IS_FALSE;  break;
8293
0
        case MAY_BE_TRUE:   type = IS_TRUE;   break;
8294
0
        case MAY_BE_LONG:   type = IS_LONG;   break;
8295
0
        case MAY_BE_DOUBLE: type = IS_DOUBLE; break;
8296
0
        case MAY_BE_STRING: type = IS_STRING; break;
8297
0
        case MAY_BE_ARRAY:  type = IS_ARRAY;  break;
8298
0
        case MAY_BE_OBJECT: type = IS_OBJECT; break;
8299
0
        case MAY_BE_ANY - MAY_BE_NULL:     type = IS_NULL;   invert = true; break;
8300
0
        case MAY_BE_ANY - MAY_BE_FALSE:    type = IS_FALSE;  invert = true; break;
8301
0
        case MAY_BE_ANY - MAY_BE_TRUE:     type = IS_TRUE;   invert = true; break;
8302
0
        case MAY_BE_ANY - MAY_BE_LONG:     type = IS_LONG;   invert = true; break;
8303
0
        case MAY_BE_ANY - MAY_BE_DOUBLE:   type = IS_DOUBLE; invert = true; break;
8304
0
        case MAY_BE_ANY - MAY_BE_STRING:   type = IS_STRING; invert = true; break;
8305
0
        case MAY_BE_ANY - MAY_BE_ARRAY:    type = IS_ARRAY;  invert = true; break;
8306
0
        case MAY_BE_ANY - MAY_BE_OBJECT:   type = IS_OBJECT; invert = true; break;
8307
0
        case MAY_BE_ANY - MAY_BE_RESOURCE: type = IS_OBJECT; invert = true; break;
8308
0
        default:
8309
0
          type = 0;
8310
0
      }
8311
8312
0
      if (op1_info & MAY_BE_REF) {
8313
0
        ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8314
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
8315
0
        op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8316
0
      }
8317
0
      if (type == 0) {
8318
0
        ref = ir_AND_U32(ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE(jit, op1_addr)), ir_CONST_U32(mask));
8319
0
        if (!smart_branch_opcode) {
8320
0
          ref = ir_NE(ref, ir_CONST_U32(0));
8321
0
        }
8322
0
      } else if (invert) {
8323
0
        ref = ir_NE(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(type));
8324
0
      } else {
8325
0
        ref = ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(type));
8326
0
      }
8327
8328
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
8329
8330
0
      if (exit_addr) {
8331
0
        if (smart_branch_opcode == ZEND_JMPZ) {
8332
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8333
0
        } else {
8334
0
          ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
8335
0
        }
8336
0
        if (end_inputs) {
8337
0
          ir_END_list(end_inputs);
8338
0
        }
8339
0
      } else if (smart_branch_opcode) {
8340
0
        ir_ref if_val = ir_IF(ref);
8341
0
        ir_IF_TRUE(if_val);
8342
0
        ir_END_list(true_inputs);
8343
0
        ir_IF_FALSE(if_val);
8344
0
        ir_END_list(false_inputs);
8345
0
      } else {
8346
0
        jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
8347
0
          ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
8348
0
        ir_END_list(end_inputs);
8349
0
      }
8350
0
      }
8351
0
  }
8352
8353
0
  if (!smart_branch_opcode || exit_addr) {
8354
0
    if (end_inputs) {
8355
0
      ir_MERGE_list(end_inputs);
8356
0
    } else if (exit_addr && !jit->ctx.control) {
8357
0
      ir_BEGIN(IR_UNUSED); /* unreachable block */
8358
0
    }
8359
0
  } else {
8360
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
8361
0
  }
8362
8363
0
  return 1;
8364
0
}
8365
8366
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)
8367
0
{
8368
0
  zend_jit_addr res_addr = RES_ADDR();
8369
0
  uint32_t true_label = -1, false_label = -1;
8370
0
  ir_ref end_inputs = IR_UNUSED, true_inputs = IR_UNUSED, false_inputs = IR_UNUSED;
8371
8372
  // TODO: support for empty() ???
8373
0
  ZEND_ASSERT(opline->extended_value != MAY_BE_RESOURCE);
8374
8375
0
  if (smart_branch_opcode && !exit_addr) {
8376
0
    if (smart_branch_opcode == ZEND_JMPZ) {
8377
0
      true_label = target_label2;
8378
0
      false_label = target_label;
8379
0
    } else if (smart_branch_opcode == ZEND_JMPNZ) {
8380
0
      true_label = target_label;
8381
0
      false_label = target_label2;
8382
0
    } else {
8383
0
      ZEND_UNREACHABLE();
8384
0
    }
8385
0
  } else {
8386
0
    res_addr = RES_ADDR();
8387
0
  }
8388
8389
0
  if (op1_info & MAY_BE_REF) {
8390
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
8391
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
8392
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
8393
0
  }
8394
8395
0
  if (!(op1_info & (MAY_BE_UNDEF|MAY_BE_NULL))) {
8396
0
    if (exit_addr) {
8397
0
      ZEND_ASSERT(smart_branch_opcode == ZEND_JMPZ);
8398
0
    } else if (smart_branch_opcode) {
8399
0
      ir_END_list(true_inputs);
8400
0
    } else {
8401
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
8402
0
      ir_END_list(end_inputs);
8403
0
    }
8404
0
  } else if (!(op1_info & (MAY_BE_ANY - MAY_BE_NULL))) {
8405
0
    if (exit_addr) {
8406
0
      ZEND_ASSERT(smart_branch_opcode == ZEND_JMPNZ);
8407
0
    } else if (smart_branch_opcode) {
8408
0
      ir_END_list(false_inputs);
8409
0
    } else {
8410
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
8411
0
      ir_END_list(end_inputs);
8412
0
    }
8413
0
  } else {
8414
0
    ir_ref ref = ir_GT(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_NULL));
8415
0
    if (exit_addr) {
8416
0
      if (smart_branch_opcode == ZEND_JMPNZ) {
8417
0
        ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
8418
0
      } else {
8419
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8420
0
      }
8421
0
    } else if (smart_branch_opcode) {
8422
0
      ir_ref if_val = ir_IF(ref);
8423
0
      ir_IF_TRUE(if_val);
8424
0
      ir_END_list(true_inputs);
8425
0
      ir_IF_FALSE(if_val);
8426
0
      ir_END_list(false_inputs);
8427
0
    } else {
8428
0
      jit_set_Z_TYPE_INFO_ref(jit, jit_ZVAL_ADDR(jit, res_addr),
8429
0
        ir_ADD_U32(ir_ZEXT_U32(ref), ir_CONST_U32(IS_FALSE)));
8430
0
      ir_END_list(end_inputs);
8431
0
    }
8432
0
  }
8433
8434
0
  if (!smart_branch_opcode || exit_addr) {
8435
0
    if (end_inputs) {
8436
0
      ir_MERGE_list(end_inputs);
8437
0
    }
8438
0
  } else {
8439
0
    _zend_jit_merge_smart_branch_inputs(jit, true_label, false_label, true_inputs, false_inputs);
8440
0
  }
8441
8442
0
  return 1;
8443
0
}
8444
8445
/* copy of hidden zend_closure */
8446
typedef struct _zend_closure {
8447
  zend_object       std;
8448
  zend_function     func;
8449
  zval              this_ptr;
8450
  zend_class_entry *called_scope;
8451
  zif_handler       orig_internal_handler;
8452
} zend_closure;
8453
8454
static int zend_jit_stack_check(zend_jit_ctx *jit, const zend_op *opline, uint32_t used_stack)
8455
0
{
8456
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
8457
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8458
8459
0
  if (!exit_addr) {
8460
0
    return 0;
8461
0
  }
8462
8463
  // JIT: if (EG(vm_stack_end) - EG(vm_stack_top) < used_stack)
8464
0
  ir_GUARD(
8465
0
    ir_UGE(
8466
0
      ir_SUB_A(ir_LOAD_A(jit_EG(vm_stack_end)), ir_LOAD_A(jit_EG(vm_stack_top))),
8467
0
      ir_CONST_ADDR(used_stack)),
8468
0
    ir_CONST_ADDR(exit_addr));
8469
8470
0
  return 1;
8471
0
}
8472
8473
static int zend_jit_free_trampoline(zend_jit_ctx *jit, ir_ref func)
8474
0
{
8475
  // JIT: if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
8476
0
  ir_ref if_trampoline = ir_IF(ir_AND_U32(
8477
0
    ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, common.fn_flags))),
8478
0
    ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE)));
8479
8480
0
  ir_IF_TRUE(if_trampoline);
8481
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_free_trampoline_helper), func);
8482
0
  ir_MERGE_WITH_EMPTY_FALSE(if_trampoline);
8483
8484
0
  return 1;
8485
0
}
8486
8487
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)
8488
0
{
8489
0
  uint32_t used_stack;
8490
0
  ir_ref used_stack_ref = IR_UNUSED;
8491
0
  bool stack_check = true;
8492
0
  ir_ref rx, ref, top, if_enough_stack, cold_path = IR_UNUSED;
8493
8494
0
  ZEND_ASSERT(func_ref != IR_NULL);
8495
0
  if (func) {
8496
0
    used_stack = zend_vm_calc_used_stack(opline->extended_value, func);
8497
0
    if ((int)used_stack <= checked_stack) {
8498
0
      stack_check = false;
8499
0
    }
8500
0
    used_stack_ref = ir_CONST_ADDR(used_stack);
8501
0
  } else {
8502
0
    ir_ref num_args_ref;
8503
0
    ir_ref if_internal_func = IR_UNUSED;
8504
0
    const size_t func_type_offset = is_closure ? offsetof(zend_closure, func.type) : offsetof(zend_function, type);
8505
8506
0
    used_stack = (ZEND_CALL_FRAME_SLOT + opline->extended_value + ZEND_OBSERVER_ENABLED) * sizeof(zval);
8507
0
    used_stack_ref = ir_CONST_ADDR(used_stack);
8508
0
    used_stack_ref = ir_HARD_COPY_A(used_stack_ref); /* load constant once */
8509
8510
    // JIT: if (EXPECTED(ZEND_USER_CODE(func->type))) {
8511
0
    ir_ref tmp = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, func_type_offset));
8512
0
    if_internal_func = ir_IF(ir_AND_U8(tmp, ir_CONST_U8(1)));
8513
0
    ir_IF_FALSE(if_internal_func);
8514
8515
    // JIT: used_stack += (func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args)) * sizeof(zval);
8516
0
    num_args_ref = ir_CONST_U32(opline->extended_value);
8517
0
    if (!is_closure) {
8518
0
      ref = ir_SUB_U32(
8519
0
        ir_SUB_U32(
8520
0
          ir_MIN_U32(
8521
0
            num_args_ref,
8522
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.num_args)))),
8523
0
          ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.last_var)))),
8524
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, op_array.T))));
8525
0
    } else {
8526
0
      ref = ir_SUB_U32(
8527
0
        ir_SUB_U32(
8528
0
          ir_MIN_U32(
8529
0
            num_args_ref,
8530
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.num_args)))),
8531
0
          ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.last_var)))),
8532
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.op_array.T))));
8533
0
    }
8534
0
    ref = ir_MUL_U32(ref, ir_CONST_U32(sizeof(zval)));
8535
0
    if (sizeof(void*) == 8) {
8536
0
      ref = ir_SEXT_A(ref);
8537
0
    }
8538
0
    ref = ir_SUB_A(used_stack_ref, ref);
8539
8540
0
    ir_MERGE_WITH_EMPTY_TRUE(if_internal_func);
8541
0
    used_stack_ref = ir_PHI_2(IR_ADDR, ref, used_stack_ref);
8542
0
  }
8543
8544
0
  zend_jit_start_reuse_ip(jit);
8545
8546
  // JIT: if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
8547
0
  jit_STORE_IP(jit, ir_LOAD_A(jit_EG(vm_stack_top)));
8548
8549
0
  if (stack_check) {
8550
    // JIT: Check Stack Overflow
8551
0
    ref = ir_UGE(
8552
0
      ir_SUB_A(
8553
0
        ir_LOAD_A(jit_EG(vm_stack_end)),
8554
0
        jit_IP(jit)),
8555
0
      used_stack_ref);
8556
8557
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8558
0
      bool may_be_trampoline = !func && (opline->opcode == ZEND_INIT_METHOD_CALL);
8559
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline,
8560
0
        may_be_trampoline ?
8561
0
          (ZEND_JIT_EXIT_TO_VM | ZEND_JIT_EXIT_METHOD_CALL) : ZEND_JIT_EXIT_TO_VM);
8562
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8563
8564
0
      if (!exit_addr) {
8565
0
        return 0;
8566
0
      }
8567
8568
0
      if (may_be_trampoline) {
8569
0
        jit->trace->exit_info[exit_point].poly_func.ref = func_ref;
8570
0
        jit->trace->exit_info[exit_point].poly_this.ref = this_ref;
8571
0
      }
8572
8573
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8574
0
    } else {
8575
0
      if_enough_stack = ir_IF(ref);
8576
0
      ir_IF_FALSE_cold(if_enough_stack);
8577
8578
#ifdef _WIN32
8579
      if (0) {
8580
#else
8581
0
      if (opline->opcode == ZEND_INIT_FCALL && func && func->type == ZEND_INTERNAL_FUNCTION) {
8582
0
#endif
8583
0
        jit_SET_EX_OPLINE(jit, opline);
8584
0
        ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_int_extend_stack_helper), used_stack_ref);
8585
0
      } else {
8586
0
        if (!is_closure) {
8587
0
          ref = func_ref;
8588
0
        } else {
8589
0
          ref = ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func));
8590
0
        }
8591
0
        jit_SET_EX_OPLINE(jit, opline);
8592
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_extend_stack_helper),
8593
0
          used_stack_ref, ref);
8594
0
      }
8595
0
      jit_STORE_IP(jit, ref);
8596
8597
0
      cold_path = ir_END();
8598
0
      ir_IF_TRUE(if_enough_stack);
8599
0
    }
8600
0
  }
8601
8602
0
  ref = jit_EG(vm_stack_top);
8603
0
  rx = jit_IP(jit);
8604
0
#if !OPTIMIZE_FOR_SIZE
8605
  /* JIT: EG(vm_stack_top) = (zval*)((char*)call + used_stack);
8606
   * This vesions is longer but faster
8607
   *    mov EG(vm_stack_top), %CALL
8608
   *    lea size(%call), %tmp
8609
   *    mov %tmp, EG(vm_stack_top)
8610
   */
8611
0
  top = rx;
8612
#else
8613
  /* JIT: EG(vm_stack_top) += used_stack;
8614
   * Use ir_emit() because ir_LOAD() makes load forwarding and doesn't allow load/store fusion
8615
   *    mov EG(vm_stack_top), %CALL
8616
   *    add $size, EG(vm_stack_top)
8617
   */
8618
  top = jit->ctx.control = ir_emit2(&jit->ctx, IR_OPT(IR_LOAD, IR_ADDR), jit->ctx.control, ref);
8619
#endif
8620
0
  ir_STORE(ref, ir_ADD_A(top, used_stack_ref));
8621
8622
  // JIT: zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
8623
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE || opline->opcode != ZEND_INIT_METHOD_CALL) {
8624
    // JIT: ZEND_SET_CALL_INFO(call, 0, call_info);
8625
0
    ir_STORE(jit_CALL(rx, This.u1.type_info), ir_CONST_U32(IS_UNDEF | ZEND_CALL_NESTED_FUNCTION));
8626
0
  }
8627
#ifdef _WIN32
8628
  if (0) {
8629
#else
8630
0
  if (opline->opcode == ZEND_INIT_FCALL && func && func->type == ZEND_INTERNAL_FUNCTION) {
8631
0
#endif
8632
0
    if (cold_path) {
8633
0
      ir_MERGE_WITH(cold_path);
8634
0
      rx = jit_IP(jit);
8635
0
    }
8636
8637
    // JIT: call->func = func;
8638
0
    ir_STORE(jit_CALL(rx, func), func_ref);
8639
0
  } else {
8640
0
    if (!is_closure) {
8641
      // JIT: call->func = func;
8642
0
      ir_STORE(jit_CALL(rx, func), func_ref);
8643
0
    } else {
8644
      // JIT: call->func = &closure->func;
8645
0
      ir_STORE(jit_CALL(rx, func), ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func)));
8646
0
    }
8647
0
    if (cold_path) {
8648
0
      ir_MERGE_WITH(cold_path);
8649
0
      rx = jit_IP(jit);
8650
0
    }
8651
0
  }
8652
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL) {
8653
    // JIT: Z_PTR(call->This) = obj;
8654
0
    ZEND_ASSERT(this_ref != IR_NULL);
8655
0
    ir_STORE(jit_CALL(rx, This.value.ptr), this_ref);
8656
0
      if (opline->op1_type == IS_UNUSED || delayed_fetch_this) {
8657
      // JIT: call->call_info |= ZEND_CALL_HAS_THIS;
8658
0
      ref = jit_CALL(rx, This.u1.type_info);
8659
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8660
0
        ir_STORE(ref, ir_CONST_U32( ZEND_CALL_HAS_THIS));
8661
0
      } else {
8662
0
        ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_HAS_THIS)));
8663
0
      }
8664
0
      } else {
8665
0
      if (opline->op1_type == IS_CV) {
8666
        // JIT: GC_ADDREF(obj);
8667
0
        jit_GC_ADDREF(jit, this_ref);
8668
0
      }
8669
8670
      // JIT: call->call_info |= ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS;
8671
0
      ref = jit_CALL(rx, This.u1.type_info);
8672
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8673
0
        ir_STORE(ref, ir_CONST_U32( ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS));
8674
0
      } else {
8675
0
        ir_STORE(ref,
8676
0
          ir_OR_U32(ir_LOAD_U32(ref),
8677
0
            ir_CONST_U32(ZEND_CALL_HAS_THIS | ZEND_CALL_RELEASE_THIS)));
8678
0
      }
8679
0
      }
8680
0
  } else if (opline->opcode == ZEND_INIT_STATIC_METHOD_CALL) {
8681
    // JIT: Z_CE(call->This) = called_scope;
8682
0
    ir_STORE(jit_CALL(rx, This), this_ref);
8683
0
  } else if (!is_closure) {
8684
    // JIT: Z_CE(call->This) = called_scope;
8685
0
    ir_STORE(jit_CALL(rx, This), IR_NULL);
8686
0
  } else {
8687
0
    ir_ref object_or_called_scope, call_info, call_info2, object, if_cond;
8688
0
    ir_ref if_cond_user = IR_UNUSED;
8689
8690
0
    if (opline->op2_type == IS_CV) {
8691
      // JIT: GC_ADDREF(closure);
8692
0
      jit_GC_ADDREF(jit, func_ref);
8693
0
    }
8694
8695
    // JIT: RX(object_or_called_scope) = closure->called_scope;
8696
0
    object_or_called_scope = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, called_scope)));
8697
8698
    // JIT: call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE |
8699
    //      (closure->func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE);
8700
0
    call_info = ir_OR_U32(
8701
0
      ir_AND_U32(
8702
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.common.fn_flags))),
8703
0
        ir_CONST_U32(ZEND_ACC_FAKE_CLOSURE)),
8704
0
      ir_CONST_U32(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE));
8705
    // JIT: if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
8706
0
    if_cond = ir_IF(ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, this_ptr.u1.v.type))));
8707
0
    ir_IF_TRUE(if_cond);
8708
8709
    // JIT: call_info |= ZEND_CALL_HAS_THIS;
8710
0
    call_info2 = ir_OR_U32(call_info, ir_CONST_U32(ZEND_CALL_HAS_THIS));
8711
8712
    // JIT: object_or_called_scope = Z_OBJ(closure->this_ptr);
8713
0
    object = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, this_ptr.value.ptr)));
8714
8715
0
    ir_MERGE_WITH_EMPTY_FALSE(if_cond);
8716
0
    call_info = ir_PHI_2(IR_U32, call_info2, call_info);
8717
0
    object_or_called_scope = ir_PHI_2(IR_ADDR, object, object_or_called_scope);
8718
8719
    // JIT: ZEND_SET_CALL_INFO(call, 0, call_info);
8720
0
    ref = jit_CALL(rx, This.u1.type_info);
8721
0
    ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), call_info));
8722
8723
    // JIT: Z_PTR(call->This) = object_or_called_scope;
8724
0
    ir_STORE(jit_CALL(rx, This.value.ptr), object_or_called_scope);
8725
8726
0
    if (!func) {
8727
      // JIT: if (closure->func.common.type & ZEND_USER_FUNCTION)
8728
0
      ir_ref type = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func.type)));
8729
0
      if_cond_user = ir_IF(ir_AND_U8(type, ir_CONST_U8(ZEND_USER_FUNCTION)));
8730
0
      ir_IF_TRUE(if_cond_user);
8731
0
    }
8732
8733
0
    if (!func || func->common.type == ZEND_USER_FUNCTION) {
8734
      // JIT: zend_jit_init_func_run_time_cache_helper(closure->func);
8735
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_init_func_run_time_cache_helper),
8736
0
        ir_ADD_OFFSET(func_ref, offsetof(zend_closure, func)));
8737
0
    }
8738
8739
0
    if (!func) {
8740
0
      ir_MERGE_WITH_EMPTY_FALSE(if_cond_user);
8741
0
    }
8742
0
  }
8743
8744
  // JIT: ZEND_CALL_NUM_ARGS(call) = num_args;
8745
0
  ir_STORE(jit_CALL(rx, This.u2.num_args), ir_CONST_U32(opline->extended_value));
8746
8747
0
  return 1;
8748
0
}
8749
8750
static int zend_jit_func_guard(zend_jit_ctx *jit, ir_ref func_ref, const zend_function *func, const void *exit_addr)
8751
0
{
8752
0
  if (func->type == ZEND_USER_FUNCTION &&
8753
0
      (!(func->common.fn_flags & ZEND_ACC_IMMUTABLE) ||
8754
0
       (func->common.fn_flags & ZEND_ACC_CLOSURE) ||
8755
0
       !func->common.function_name)) {
8756
0
    const zend_op *opcodes = func->op_array.opcodes;
8757
8758
    // JIT: if (call->func.op_array.opcodes != opcodes) goto exit_addr;
8759
0
    ir_GUARD(
8760
0
      ir_EQ(
8761
0
        ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes))),
8762
0
        ir_CONST_ADDR(opcodes)),
8763
0
      ir_CONST_ADDR(exit_addr));
8764
#ifdef ZEND_WIN32
8765
  } else if (func->type == ZEND_INTERNAL_FUNCTION) {
8766
    // ASLR may cause different addresses in different workers. Check for the internal function handler.
8767
    // JIT: if (call->func.internal_function.handler != handler) goto exit_addr;
8768
    ir_GUARD(
8769
      ir_EQ(
8770
        ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_internal_function, handler))),
8771
        ir_CONST_FC_FUNC(func->internal_function.handler)),
8772
      ir_CONST_ADDR(exit_addr));
8773
#endif
8774
0
  } else {
8775
    // JIT: if (call->func != func) goto exit_addr;
8776
0
    ir_GUARD(ir_EQ(func_ref, ir_CONST_ADDR(func)), ir_CONST_ADDR(exit_addr));
8777
0
  }
8778
8779
0
  return 1;
8780
0
}
8781
8782
static int zend_jit_init_fcall_guard(zend_jit_ctx *jit, uint32_t level, const zend_function *func, const zend_op *to_opline)
8783
0
{
8784
0
  int32_t exit_point;
8785
0
  const void *exit_addr;
8786
0
  ir_ref call;
8787
8788
0
  if (func->type == ZEND_USER_FUNCTION
8789
0
   && !zend_accel_in_shm(func->op_array.opcodes)) {
8790
    /* op_array and op_array->opcodes are not persistent. We can't link. */
8791
0
    return 0;
8792
0
  }
8793
8794
0
  exit_point = zend_jit_trace_get_exit_point(to_opline, ZEND_JIT_EXIT_POLYMORPHISM);
8795
0
  exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8796
0
  if (!exit_addr) {
8797
0
    return 0;
8798
0
  }
8799
8800
  // call = EX(call);
8801
0
  call = ir_LOAD_A(jit_EX(call));
8802
0
  while (level > 0) {
8803
    // call = call->prev_execute_data
8804
0
    call = ir_LOAD_A(jit_CALL(call, prev_execute_data));
8805
0
    level--;
8806
0
  }
8807
8808
0
  return zend_jit_func_guard(jit, ir_LOAD_A(jit_CALL(call, func)), func, exit_addr);
8809
0
}
8810
8811
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)
8812
0
{
8813
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
8814
0
  zend_call_info *call_info = NULL;
8815
0
  zend_function *func = NULL;
8816
0
  ir_ref func_ref = IR_UNUSED;
8817
8818
0
  if (jit->delayed_call_level) {
8819
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
8820
0
      return 0;
8821
0
    }
8822
0
  }
8823
8824
0
  if (info) {
8825
0
    call_info = info->callee_info;
8826
0
    while (call_info && call_info->caller_init_opline != opline) {
8827
0
      call_info = call_info->next_callee;
8828
0
    }
8829
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
8830
0
      func = call_info->callee_func;
8831
0
    }
8832
0
  }
8833
8834
0
  if (!func
8835
0
   && trace
8836
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL) {
8837
#ifdef _WIN32
8838
    /* ASLR */
8839
    if (trace->func->type != ZEND_INTERNAL_FUNCTION) {
8840
      func = (zend_function*)trace->func;
8841
    }
8842
#else
8843
0
    func = (zend_function*)trace->func;
8844
0
#endif
8845
0
  }
8846
8847
#ifdef _WIN32
8848
  if (0) {
8849
#else
8850
0
  if (opline->opcode == ZEND_INIT_FCALL
8851
0
   && func
8852
0
   && func->type == ZEND_INTERNAL_FUNCTION) {
8853
0
#endif
8854
    /* load constant address later */
8855
0
    func_ref = ir_CONST_ADDR(func);
8856
0
  } else if (func && op_array == &func->op_array) {
8857
    /* recursive call */
8858
0
    if (!(func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)
8859
0
     || zend_jit_prefer_const_addr_load(jit, (uintptr_t)func)) {
8860
0
      func_ref = ir_LOAD_A(jit_EX(func));
8861
0
    } else {
8862
0
      func_ref = ir_CONST_ADDR(func);
8863
0
    }
8864
0
  } else {
8865
0
    ir_ref if_func, cache_slot_ref, ref;
8866
8867
    // JIT: if (CACHED_PTR(opline->result.num))
8868
0
    cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num);
8869
0
    func_ref = ir_LOAD_A(cache_slot_ref);
8870
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
8871
0
     && func
8872
0
     && (func->common.fn_flags & ZEND_ACC_IMMUTABLE)
8873
0
     && opline->opcode != ZEND_INIT_FCALL) {
8874
      /* Called func may be changed because of recompilation. See ext/opcache/tests/jit/init_fcall_003.phpt */
8875
0
      if_func = ir_IF(ir_EQ(func_ref, ir_CONST_ADDR(func)));
8876
0
    } else {
8877
0
      if_func = ir_IF(func_ref);
8878
0
    }
8879
0
    ir_IF_FALSE_cold(if_func);
8880
0
    if (opline->opcode == ZEND_INIT_FCALL
8881
0
     && func
8882
0
     && func->type == ZEND_USER_FUNCTION
8883
0
     && (func->op_array.fn_flags & ZEND_ACC_IMMUTABLE)) {
8884
0
      ref = ir_HARD_COPY_A(ir_CONST_ADDR(func)); /* load constant once */
8885
0
        ir_STORE(cache_slot_ref, ref);
8886
0
      ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_init_func_run_time_cache_helper), ref);
8887
0
    } else {
8888
0
      zval *zv = RT_CONSTANT(opline, opline->op2);
8889
8890
0
      if (opline->opcode == ZEND_INIT_FCALL) {
8891
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_func_helper),
8892
0
          ir_CONST_ADDR(Z_STR_P(zv)),
8893
0
          cache_slot_ref);
8894
0
      } else if (opline->opcode == ZEND_INIT_FCALL_BY_NAME) {
8895
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_func_helper),
8896
0
          ir_CONST_ADDR(Z_STR_P(zv + 1)),
8897
0
          cache_slot_ref);
8898
0
      } else if (opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
8899
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_ns_func_helper),
8900
0
          ir_CONST_ADDR(zv),
8901
0
          cache_slot_ref);
8902
0
      } else {
8903
0
        ZEND_UNREACHABLE();
8904
0
      }
8905
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
8906
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline,
8907
0
          func && (func->common.fn_flags & ZEND_ACC_IMMUTABLE) ? ZEND_JIT_EXIT_INVALIDATE : 0);
8908
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
8909
8910
0
        if (!exit_addr) {
8911
0
          return 0;
8912
0
        }
8913
0
        if (!func || opline->opcode == ZEND_INIT_FCALL) {
8914
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
8915
0
        } else if (!zend_jit_func_guard(jit, ref, func, exit_addr)) {
8916
0
          return 0;
8917
0
        }
8918
0
      } else {
8919
0
jit_SET_EX_OPLINE(jit, opline);
8920
0
        ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_undefined_function));
8921
0
      }
8922
0
    }
8923
0
    ir_MERGE_WITH_EMPTY_TRUE(if_func);
8924
0
    func_ref = ir_PHI_2(IR_ADDR, ref, func_ref);
8925
0
  }
8926
8927
0
  if (!zend_jit_push_call_frame(jit, opline, op_array, func, false, false, checked_stack, func_ref, IR_UNUSED)) {
8928
0
    return 0;
8929
0
  }
8930
8931
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
8932
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
8933
0
      return 0;
8934
0
    }
8935
0
  } else {
8936
0
    ZEND_ASSERT(call_level > 0);
8937
0
    jit->delayed_call_level = call_level;
8938
0
    delayed_call_chain = true;
8939
0
  }
8940
8941
0
  if (trace
8942
0
   && trace->op == ZEND_JIT_TRACE_END
8943
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
8944
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
8945
0
      return 0;
8946
0
    }
8947
0
  }
8948
8949
0
  return 1;
8950
0
}
8951
8952
static int zend_jit_init_method_call(zend_jit_ctx         *jit,
8953
                                     const zend_op        *opline,
8954
                                     uint32_t              b,
8955
                                     const zend_op_array  *op_array,
8956
                                     zend_ssa             *ssa,
8957
                                     const zend_ssa_op    *ssa_op,
8958
                                     int                   call_level,
8959
                                     uint32_t              op1_info,
8960
                                     zend_jit_addr         op1_addr,
8961
                                     zend_class_entry     *ce,
8962
                                     bool                  ce_is_instanceof,
8963
                                     bool                  on_this,
8964
                                     bool                  delayed_fetch_this,
8965
                                     zend_class_entry     *trace_ce,
8966
                                     zend_jit_trace_rec   *trace,
8967
                                     int                   checked_stack,
8968
                                     ir_ref                func_ref,
8969
                                     ir_ref                this_ref,
8970
                                     bool                  polymorphic_side_trace)
8971
0
{
8972
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
8973
0
  zend_call_info *call_info = NULL;
8974
0
  zend_function *func = NULL;
8975
0
  zval *function_name;
8976
0
  ir_ref if_static = IR_UNUSED, cold_path;
8977
8978
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
8979
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
8980
8981
0
  function_name = RT_CONSTANT(opline, opline->op2);
8982
8983
0
  if (info) {
8984
0
    call_info = info->callee_info;
8985
0
    while (call_info && call_info->caller_init_opline != opline) {
8986
0
      call_info = call_info->next_callee;
8987
0
    }
8988
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
8989
0
      func = call_info->callee_func;
8990
0
    }
8991
0
  }
8992
8993
0
  if (polymorphic_side_trace) {
8994
    /* function is passed from parent snapshot */
8995
0
    ZEND_ASSERT(func_ref != IR_UNUSED && this_ref != IR_UNUSED);
8996
0
  } else {
8997
0
    ir_ref ref, ref2, if_found, fast_path, run_time_cache, this_ref2;
8998
8999
0
    if (on_this) {
9000
0
      zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
9001
0
      this_ref = jit_Z_PTR(jit, this_addr);
9002
0
    } else {
9003
0
        if (op1_info & MAY_BE_REF) {
9004
0
        if (opline->op1_type == IS_CV) {
9005
          // JIT: ZVAL_DEREF(op1)
9006
0
          ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
9007
0
          ref = jit_ZVAL_DEREF_ref(jit, ref);
9008
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
9009
0
        } else {
9010
0
          ir_ref if_ref;
9011
9012
          /* Hack: Convert reference to regular value to simplify JIT code */
9013
0
          ZEND_ASSERT(Z_REG(op1_addr) == ZREG_FP);
9014
9015
0
          if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9016
0
          ir_IF_TRUE(if_ref);
9017
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_unref_helper), jit_ZVAL_ADDR(jit, op1_addr));
9018
9019
0
          ir_MERGE_WITH_EMPTY_FALSE(if_ref);
9020
0
        }
9021
0
      }
9022
0
      if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
9023
0
        if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9024
0
          int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9025
0
          const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9026
9027
0
          if (!exit_addr) {
9028
0
            return 0;
9029
0
          }
9030
0
          ir_GUARD(ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_OBJECT)),
9031
0
            ir_CONST_ADDR(exit_addr));
9032
0
        } else {
9033
0
          ir_ref if_object = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
9034
9035
0
          ir_IF_FALSE_cold(if_object);
9036
9037
0
          jit_SET_EX_OPLINE(jit, opline);
9038
0
          if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9039
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_method_call_tmp),
9040
0
              jit_ZVAL_ADDR(jit, op1_addr));
9041
0
          } else {
9042
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_method_call),
9043
0
              jit_ZVAL_ADDR(jit, op1_addr));
9044
0
          }
9045
0
          ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
9046
0
          ir_IF_TRUE(if_object);
9047
0
        }
9048
0
      }
9049
9050
0
      this_ref = jit_Z_PTR(jit, op1_addr);
9051
0
    }
9052
9053
0
    if (jit->delayed_call_level) {
9054
0
      if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9055
0
        return 0;
9056
0
      }
9057
0
    }
9058
9059
0
    if (func) {
9060
      // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9061
0
      ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num + sizeof(void*)));
9062
9063
0
      if_found = ir_IF(ref);
9064
0
      ir_IF_TRUE(if_found);
9065
0
      fast_path = ir_END();
9066
0
    } else {
9067
      // JIT: if (CACHED_PTR(opline->result.num) == obj->ce)) {
9068
0
      run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
9069
0
      ref = ir_EQ(
9070
0
        ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->result.num)),
9071
0
        ir_LOAD_A(ir_ADD_OFFSET(this_ref, offsetof(zend_object, ce))));
9072
0
      if_found = ir_IF(ref);
9073
0
      ir_IF_TRUE(if_found);
9074
9075
      // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9076
0
      ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->result.num + sizeof(void*)));
9077
0
      fast_path = ir_END();
9078
9079
0
    }
9080
9081
0
    ir_IF_FALSE_cold(if_found);
9082
0
    jit_SET_EX_OPLINE(jit, opline);
9083
9084
0
    if (!jit->ctx.fixed_call_stack_size) {
9085
      // JIT: alloca(sizeof(void*));
9086
0
      this_ref2 = ir_ALLOCA(ir_CONST_ADDR(0x10));
9087
0
    } else {
9088
#ifdef _WIN64
9089
      this_ref2 = ir_HARD_COPY_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
9090
#else
9091
0
      this_ref2 = ir_HARD_COPY_A(ir_RLOAD_A(IR_REG_SP));
9092
0
#endif
9093
0
    }
9094
0
    ir_STORE(this_ref2, this_ref);
9095
9096
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9097
0
      ref2 = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_method_tmp_helper),
9098
0
          this_ref,
9099
0
          ir_CONST_ADDR(function_name),
9100
0
          this_ref2);
9101
0
    } else {
9102
0
      ref2 = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_method_helper),
9103
0
          this_ref,
9104
0
          ir_CONST_ADDR(function_name),
9105
0
          this_ref2);
9106
0
    }
9107
9108
9109
0
    if (!jit->ctx.fixed_call_stack_size) {
9110
0
      this_ref2 = ir_LOAD_A(ir_RLOAD_A(IR_REG_SP));
9111
      // JIT: revert alloca
9112
0
      ir_AFREE(ir_CONST_ADDR(0x10));
9113
0
    } else {
9114
#ifdef _WIN64
9115
      this_ref2 = ir_LOAD_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
9116
#else
9117
0
      this_ref2 = ir_LOAD_A(ir_RLOAD_A(IR_REG_SP));
9118
0
#endif
9119
0
    }
9120
9121
0
    ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9122
9123
0
    ir_MERGE_WITH(fast_path);
9124
0
    func_ref = ir_PHI_2(IR_ADDR, ref2, ref);
9125
0
    this_ref = ir_PHI_2(IR_ADDR, this_ref2, this_ref);
9126
0
  }
9127
9128
0
  if ((!func || zend_jit_may_be_modified(func, op_array))
9129
0
   && trace
9130
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9131
0
   && trace->func) {
9132
0
    int32_t exit_point;
9133
0
    const void *exit_addr;
9134
9135
0
    exit_point = zend_jit_trace_get_exit_point(opline, func ? ZEND_JIT_EXIT_INVALIDATE : ZEND_JIT_EXIT_METHOD_CALL);
9136
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9137
0
    if (!exit_addr) {
9138
0
      return 0;
9139
0
    }
9140
9141
0
    jit->trace->exit_info[exit_point].poly_func.ref = func_ref;
9142
0
    jit->trace->exit_info[exit_point].poly_this.ref = this_ref;
9143
9144
0
    func = (zend_function*)trace->func;
9145
9146
0
    if (!zend_jit_func_guard(jit, func_ref, func, exit_addr)) {
9147
0
      return 0;
9148
0
    }
9149
0
  }
9150
9151
0
  if (!func) {
9152
    // JIT: if (fbc->common.fn_flags & ZEND_ACC_STATIC) {
9153
0
    if_static = ir_IF(ir_AND_U32(
9154
0
      ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
9155
0
      ir_CONST_U32(ZEND_ACC_STATIC)));
9156
0
    ir_IF_TRUE_cold(if_static);
9157
0
  }
9158
9159
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC) != 0) {
9160
0
    ir_ref ret;
9161
9162
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this) {
9163
0
      ret = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_static_method_call_frame_tmp),
9164
0
          this_ref,
9165
0
          func_ref,
9166
0
          ir_CONST_U32(opline->extended_value));
9167
0
    } else {
9168
0
      ret = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_static_method_call_frame),
9169
0
          this_ref,
9170
0
          func_ref,
9171
0
          ir_CONST_U32(opline->extended_value));
9172
0
    }
9173
9174
0
    if ((opline->op1_type & (IS_VAR|IS_TMP_VAR) && !delayed_fetch_this)) {
9175
0
      ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9176
0
    }
9177
0
    jit_STORE_IP(jit, ret);
9178
0
  }
9179
9180
0
  if (!func) {
9181
0
    cold_path = ir_END();
9182
0
    ir_IF_FALSE(if_static);
9183
0
  }
9184
9185
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC) == 0) {
9186
0
    if (!zend_jit_push_call_frame(jit, opline, NULL, func, false, delayed_fetch_this, checked_stack, func_ref, this_ref)) {
9187
0
      return 0;
9188
0
    }
9189
0
  }
9190
9191
0
  if (!func) {
9192
0
    ir_MERGE_WITH(cold_path);
9193
0
  }
9194
0
  zend_jit_start_reuse_ip(jit);
9195
9196
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9197
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9198
0
      return 0;
9199
0
    }
9200
0
  } else {
9201
0
    ZEND_ASSERT(call_level > 0);
9202
0
    delayed_call_chain = true;
9203
0
    jit->delayed_call_level = call_level;
9204
0
  }
9205
9206
0
  if (trace
9207
0
   && trace->op == ZEND_JIT_TRACE_END
9208
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9209
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9210
0
      return 0;
9211
0
    }
9212
0
  }
9213
9214
0
  return 1;
9215
0
}
9216
9217
static int zend_jit_init_static_method_call(zend_jit_ctx         *jit,
9218
                                            const zend_op        *opline,
9219
                                            uint32_t              b,
9220
                                            const zend_op_array  *op_array,
9221
                                            zend_ssa             *ssa,
9222
                                            const zend_ssa_op    *ssa_op,
9223
                                            int                   call_level,
9224
                                            zend_jit_trace_rec   *trace,
9225
                                            int                   checked_stack)
9226
0
{
9227
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
9228
0
  zend_call_info *call_info = NULL;
9229
0
  zend_class_entry *ce;
9230
0
  zend_function *func = NULL;
9231
0
  ir_ref func_ref, func_ref2, scope_ref, scope_ref2, if_cached, cold_path, ref;
9232
0
  ir_ref if_static = IR_UNUSED;
9233
9234
0
  if (info) {
9235
0
    call_info = info->callee_info;
9236
0
    while (call_info && call_info->caller_init_opline != opline) {
9237
0
      call_info = call_info->next_callee;
9238
0
    }
9239
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
9240
0
      func = call_info->callee_func;
9241
0
    }
9242
0
  }
9243
9244
0
  ce = zend_get_known_class(op_array, opline, opline->op1_type, opline->op1);
9245
0
  if (!func && ce && (opline->op1_type == IS_CONST || !(ce->ce_flags & ZEND_ACC_TRAIT))) {
9246
0
    zval *zv = RT_CONSTANT(opline, opline->op2);
9247
0
    zend_string *method_name;
9248
9249
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
9250
0
    method_name = Z_STR_P(zv);
9251
0
    zv = zend_hash_find(&ce->function_table, method_name);
9252
0
    if (zv) {
9253
0
      zend_function *fn = Z_PTR_P(zv);
9254
9255
0
      if (fn->common.scope == op_array->scope
9256
0
       || (fn->common.fn_flags & ZEND_ACC_PUBLIC)
9257
0
       || ((fn->common.fn_flags & ZEND_ACC_PROTECTED)
9258
0
        && op_array->scope
9259
0
        && instanceof_function_slow(op_array->scope, fn->common.scope))) {
9260
0
        func = fn;
9261
0
      }
9262
0
    }
9263
0
  }
9264
9265
0
  if (jit->delayed_call_level) {
9266
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9267
0
      return 0;
9268
0
    }
9269
0
  }
9270
9271
  // JIT: fbc = CACHED_PTR(opline->result.num + sizeof(void*));
9272
0
  func_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num + sizeof(void*)));
9273
9274
  // JIT: if (fbc)
9275
0
  if_cached = ir_IF(func_ref);
9276
0
  ir_IF_FALSE_cold(if_cached);
9277
9278
0
  jit_SET_EX_OPLINE(jit, opline);
9279
0
  scope_ref2 = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_class_helper), jit_FP(jit));
9280
0
  ir_GUARD(scope_ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9281
9282
0
  func_ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_find_static_method_helper), jit_FP(jit), scope_ref2);
9283
0
  ir_GUARD(func_ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9284
9285
0
  cold_path = ir_END();
9286
9287
0
  ir_IF_TRUE(if_cached);
9288
0
  if (ce && (ce->ce_flags & ZEND_ACC_IMMUTABLE) && (ce->ce_flags & ZEND_ACC_LINKED)) {
9289
0
    scope_ref = ir_CONST_ADDR(ce);
9290
0
  } else {
9291
0
    scope_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->result.num));
9292
0
  }
9293
9294
0
  ir_MERGE_2(cold_path, ir_END());
9295
0
  func_ref = ir_PHI_2(IR_ADDR, func_ref2, func_ref);
9296
0
  scope_ref = ir_PHI_2(IR_ADDR, scope_ref2, scope_ref);
9297
9298
0
  if ((!func || zend_jit_may_be_modified(func, op_array))
9299
0
   && trace
9300
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9301
0
   && trace->func) {
9302
0
    int32_t exit_point;
9303
0
    const void *exit_addr;
9304
9305
0
    exit_point = zend_jit_trace_get_exit_point(opline, func ? ZEND_JIT_EXIT_INVALIDATE : 0);
9306
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9307
0
    if (!exit_addr) {
9308
0
      return 0;
9309
0
    }
9310
9311
//    jit->trace->exit_info[exit_point].poly_func_ref = func_ref;
9312
//    jit->trace->exit_info[exit_point].poly_this_ref = scope_ref;
9313
9314
0
    func = (zend_function*)trace->func;
9315
9316
0
    if (!zend_jit_func_guard(jit, func_ref, func, exit_addr)) {
9317
0
      return 0;
9318
0
    }
9319
0
  }
9320
9321
0
  if (!func || !(func->common.fn_flags & ZEND_ACC_STATIC)) {
9322
0
    if (!func) {
9323
      // JIT: if (fbc->common.fn_flags & ZEND_ACC_STATIC) {
9324
0
      if_static = ir_IF(ir_AND_U32(
9325
0
        ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_function, common.fn_flags))),
9326
0
        ir_CONST_U32(ZEND_ACC_STATIC)));
9327
0
      ir_IF_FALSE_cold(if_static);
9328
0
    }
9329
9330
0
    jit_SET_EX_OPLINE(jit, opline);
9331
0
    ref = ir_CALL_3(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_push_this_method_call_frame),
9332
0
        scope_ref,
9333
0
        func_ref,
9334
0
        ir_CONST_U32(opline->extended_value));
9335
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9336
0
    jit_STORE_IP(jit, ref);
9337
9338
0
    if (!func) {
9339
0
      cold_path = ir_END();
9340
0
      ir_IF_TRUE(if_static);
9341
0
    }
9342
0
  }
9343
9344
0
  if (!func || (func->common.fn_flags & ZEND_ACC_STATIC)) {
9345
0
    if (opline->op1_type == IS_UNUSED
9346
0
     && ((opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT ||
9347
0
         (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF)) {
9348
0
      if (op_array->fn_flags & ZEND_ACC_STATIC) {
9349
0
        scope_ref = ir_LOAD_A(jit_EX(This.value.ref));
9350
0
      } else if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
9351
0
        ir_ref if_object, values = IR_UNUSED;
9352
9353
0
        if_object = ir_IF(ir_EQ(jit_Z_TYPE_ref(jit, jit_EX(This)), ir_CONST_U8(IS_OBJECT)));
9354
0
        ir_IF_TRUE(if_object);
9355
0
        ir_END_PHI_list(values,
9356
0
          ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(This.value.ref)), offsetof(zend_object, ce))));
9357
0
        ir_IF_FALSE(if_object);
9358
0
        ir_END_PHI_list(values, ir_LOAD_A(jit_EX(This.value.ref)));
9359
0
        ir_PHI_list(values);
9360
0
      } else {
9361
0
        scope_ref = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(This.value.ref)), offsetof(zend_object, ce)));
9362
0
      }
9363
0
    }
9364
0
    if (!zend_jit_push_call_frame(jit, opline, op_array, func, false, false, checked_stack, func_ref, scope_ref)) {
9365
0
      return 0;
9366
0
    }
9367
9368
0
    if (!func) {
9369
0
      ir_MERGE_2(cold_path, ir_END());
9370
0
    }
9371
0
  }
9372
9373
0
  zend_jit_start_reuse_ip(jit);
9374
0
  if (zend_jit_needs_call_chain(call_info, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9375
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9376
0
      return 0;
9377
0
    }
9378
0
  } else {
9379
0
    ZEND_ASSERT(call_level > 0);
9380
0
    jit->delayed_call_level = call_level;
9381
0
    delayed_call_chain = true;
9382
0
  }
9383
9384
0
  if (trace
9385
0
   && trace->op == ZEND_JIT_TRACE_END
9386
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9387
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9388
0
      return 0;
9389
0
    }
9390
0
  }
9391
9392
0
  return 1;
9393
0
}
9394
9395
static int zend_jit_init_closure_call(zend_jit_ctx         *jit,
9396
                                      const zend_op        *opline,
9397
                                      uint32_t              b,
9398
                                      const zend_op_array  *op_array,
9399
                                      zend_ssa             *ssa,
9400
                                      const zend_ssa_op    *ssa_op,
9401
                                      int                   call_level,
9402
                                      zend_jit_trace_rec   *trace,
9403
                                      int                   checked_stack)
9404
0
{
9405
0
  zend_function *func = NULL;
9406
0
  zend_jit_addr op2_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
9407
0
  ir_ref ref;
9408
9409
0
  ref = jit_Z_PTR(jit, op2_addr);
9410
9411
0
  if (ssa->var_info[ssa_op->op2_use].ce != zend_ce_closure
9412
0
   && !(ssa->var_info[ssa_op->op2_use].type & MAY_BE_CLASS_GUARD)) {
9413
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9414
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9415
9416
0
    if (!exit_addr) {
9417
0
      return 0;
9418
0
    }
9419
9420
0
    ir_GUARD(
9421
0
      ir_EQ(
9422
0
        ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_object, ce))),
9423
0
        ir_CONST_ADDR(zend_ce_closure)),
9424
0
      ir_CONST_ADDR(exit_addr));
9425
9426
0
    if (ssa->var_info && ssa_op->op2_use >= 0) {
9427
0
      ssa->var_info[ssa_op->op2_use].type |= MAY_BE_CLASS_GUARD;
9428
0
      ssa->var_info[ssa_op->op2_use].ce = zend_ce_closure;
9429
0
      ssa->var_info[ssa_op->op2_use].is_instanceof = 0;
9430
0
    }
9431
0
  }
9432
9433
0
  if (trace
9434
0
   && trace->op == ZEND_JIT_TRACE_INIT_CALL
9435
0
   && trace->func
9436
0
   && trace->func->type == ZEND_USER_FUNCTION) {
9437
0
    const zend_op *opcodes;
9438
0
    int32_t exit_point;
9439
0
    const void *exit_addr;
9440
9441
0
    func = (zend_function*)trace->func;
9442
0
    opcodes = func->op_array.opcodes;
9443
0
    exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_CLOSURE_CALL);
9444
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9445
0
    if (!exit_addr) {
9446
0
      return 0;
9447
0
    }
9448
9449
0
    ir_GUARD(
9450
0
      ir_EQ(
9451
0
        ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_closure, func.op_array.opcodes))),
9452
0
        ir_CONST_ADDR(opcodes)),
9453
0
      ir_CONST_ADDR(exit_addr));
9454
0
  }
9455
9456
0
  if (jit->delayed_call_level) {
9457
0
    if (!zend_jit_save_call_chain(jit, jit->delayed_call_level)) {
9458
0
      return 0;
9459
0
    }
9460
0
  }
9461
9462
0
  if (!zend_jit_push_call_frame(jit, opline, NULL, func, true, false, checked_stack, ref, IR_UNUSED)) {
9463
0
    return 0;
9464
0
  }
9465
9466
0
  if (zend_jit_needs_call_chain(NULL, b, op_array, ssa, ssa_op, opline, call_level, trace)) {
9467
0
    if (!zend_jit_save_call_chain(jit, call_level)) {
9468
0
      return 0;
9469
0
    }
9470
0
  } else {
9471
0
    ZEND_ASSERT(call_level > 0);
9472
0
    delayed_call_chain = true;
9473
0
    jit->delayed_call_level = call_level;
9474
0
  }
9475
9476
0
  if (trace
9477
0
   && trace->op == ZEND_JIT_TRACE_END
9478
0
   && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
9479
0
    if (!zend_jit_set_ip(jit, opline + 1)) {
9480
0
      return 0;
9481
0
    }
9482
0
  }
9483
9484
0
  return 1;
9485
0
}
9486
9487
static int zend_jit_send_val(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr)
9488
0
{
9489
0
  uint32_t arg_num = opline->op2.num;
9490
0
  zend_jit_addr arg_addr;
9491
9492
0
  ZEND_ASSERT(opline->opcode == ZEND_SEND_VAL || arg_num <= MAX_ARG_FLAG_NUM);
9493
9494
0
  if (!zend_jit_reuse_ip(jit)) {
9495
0
    return 0;
9496
0
  }
9497
9498
0
  if (opline->opcode == ZEND_SEND_VAL_EX) {
9499
0
    uint32_t mask = ZEND_SEND_BY_REF << ((arg_num + 3) * 2);
9500
9501
0
    ZEND_ASSERT(arg_num <= MAX_ARG_FLAG_NUM);
9502
9503
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9504
0
     && JIT_G(current_frame)
9505
0
     && JIT_G(current_frame)->call
9506
0
     && JIT_G(current_frame)->call->func) {
9507
0
      if (ARG_MUST_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9508
        /* Don't generate code that always throws exception */
9509
0
        return 0;
9510
0
      }
9511
0
    } else {
9512
0
      ir_ref cond = ir_AND_U32(
9513
0
        ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_RX(func)), offsetof(zend_function, quick_arg_flags))),
9514
0
        ir_CONST_U32(mask));
9515
9516
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9517
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9518
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9519
0
        if (!exit_addr) {
9520
0
          return 0;
9521
0
        }
9522
0
        ir_GUARD_NOT(cond, ir_CONST_ADDR(exit_addr));
9523
0
      } else {
9524
0
        ir_ref if_pass_by_ref;
9525
9526
0
        if_pass_by_ref = ir_IF(cond);
9527
9528
0
        ir_IF_TRUE_cold(if_pass_by_ref);
9529
0
        if (Z_MODE(op1_addr) == IS_REG) {
9530
          /* set type to avoid zval_ptr_dtor() on uninitialized value */
9531
0
          zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
9532
0
          jit_set_Z_TYPE_INFO(jit, addr, IS_UNDEF);
9533
0
        }
9534
0
        jit_SET_EX_OPLINE(jit, opline);
9535
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_throw_cannot_pass_by_ref));
9536
9537
0
        ir_IF_FALSE(if_pass_by_ref);
9538
0
      }
9539
0
    }
9540
0
  }
9541
9542
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9543
9544
0
  if (opline->op1_type == IS_CONST) {
9545
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
9546
9547
0
    jit_ZVAL_COPY_CONST(jit,
9548
0
      arg_addr,
9549
0
      MAY_BE_ANY, MAY_BE_ANY,
9550
0
      zv, true);
9551
0
  } else {
9552
0
    jit_ZVAL_COPY(jit,
9553
0
      arg_addr,
9554
0
      MAY_BE_ANY,
9555
0
      op1_addr, op1_info, false);
9556
0
  }
9557
9558
0
  return 1;
9559
0
}
9560
9561
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)
9562
0
{
9563
0
  zend_jit_addr op1_addr, arg_addr, ref_addr;
9564
0
  ir_ref ref_path = IR_UNUSED;
9565
9566
0
  op1_addr = OP1_ADDR();
9567
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9568
9569
0
  if (!zend_jit_reuse_ip(jit)) {
9570
0
    return 0;
9571
0
  }
9572
9573
0
  if (opline->op1_type == IS_VAR) {
9574
0
    if (op1_info & MAY_BE_INDIRECT) {
9575
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
9576
0
    }
9577
0
  } else if (opline->op1_type == IS_CV) {
9578
0
    if (op1_info & MAY_BE_UNDEF) {
9579
0
      if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9580
        // JIT: if (Z_TYPE_P(op1) == IS_UNDEF)
9581
0
        ir_ref if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
9582
0
        ir_IF_FALSE(if_def);
9583
        // JIT: ZVAL_NULL(op1)
9584
0
        jit_set_Z_TYPE_INFO(jit,op1_addr, IS_NULL);
9585
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def);
9586
0
      }
9587
0
      op1_info &= ~MAY_BE_UNDEF;
9588
0
      op1_info |= MAY_BE_NULL;
9589
0
    }
9590
0
  } else {
9591
0
    ZEND_UNREACHABLE();
9592
0
  }
9593
9594
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) {
9595
0
    ir_ref ref, ref2;
9596
9597
0
    if (op1_info & MAY_BE_REF) {
9598
0
      ir_ref if_ref;
9599
9600
      // JIT: if (Z_TYPE_P(op1) == IS_UNDEF)
9601
0
      if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9602
0
      ir_IF_TRUE(if_ref);
9603
      // JIT: ref = Z_PTR_P(op1)
9604
0
      ref = jit_Z_PTR(jit, op1_addr);
9605
      // JIT: GC_ADDREF(ref)
9606
0
      jit_GC_ADDREF(jit, ref);
9607
      // JIT: ZVAL_REFERENCE(arg, ref)
9608
0
      jit_set_Z_PTR(jit, arg_addr, ref);
9609
0
      jit_set_Z_TYPE_INFO(jit, arg_addr, IS_REFERENCE_EX);
9610
0
      ref_path = ir_END();
9611
0
      ir_IF_FALSE(if_ref);
9612
0
    }
9613
9614
    // JIT: ZVAL_NEW_REF(arg, varptr);
9615
    // JIT: ref = emalloc(sizeof(zend_reference));
9616
0
    ref = jit_EMALLOC(jit, sizeof(zend_reference), op_array, opline);
9617
    // JIT: GC_REFCOUNT(ref) = 2
9618
0
    jit_set_GC_REFCOUNT(jit, ref, 2);
9619
    // JIT: GC_TYPE(ref) = GC_REFERENCE
9620
0
    ir_STORE(ir_ADD_OFFSET(ref, offsetof(zend_reference, gc.u.type_info)), ir_CONST_U32(GC_REFERENCE));
9621
0
    ir_STORE(ir_ADD_OFFSET(ref, offsetof(zend_reference, sources.ptr)), IR_NULL);
9622
0
    ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
9623
0
    ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
9624
9625
        // JIT: ZVAL_COPY_VALUE(&ref->val, op1)
9626
0
    jit_ZVAL_COPY(jit,
9627
0
      ref_addr,
9628
0
      MAY_BE_ANY,
9629
0
      op1_addr, op1_info, false);
9630
9631
    // JIT: ZVAL_REFERENCE(arg, ref)
9632
0
    jit_set_Z_PTR(jit, op1_addr, ref);
9633
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
9634
9635
    // JIT: ZVAL_REFERENCE(arg, ref)
9636
0
    jit_set_Z_PTR(jit, arg_addr, ref);
9637
0
    jit_set_Z_TYPE_INFO(jit, arg_addr, IS_REFERENCE_EX);
9638
0
  }
9639
9640
0
  if (ref_path) {
9641
0
    ir_MERGE_WITH(ref_path);
9642
0
  }
9643
9644
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
9645
9646
0
  return 1;
9647
0
}
9648
9649
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)
9650
0
{
9651
0
  uint32_t arg_num = opline->op2.num;
9652
0
  zend_jit_addr arg_addr;
9653
0
  ir_ref end_inputs = IR_UNUSED;
9654
9655
0
  ZEND_ASSERT((opline->opcode != ZEND_SEND_VAR_EX &&
9656
0
       opline->opcode != ZEND_SEND_VAR_NO_REF_EX) ||
9657
0
      arg_num <= MAX_ARG_FLAG_NUM);
9658
9659
0
  arg_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, opline->result.var);
9660
9661
0
  if (!zend_jit_reuse_ip(jit)) {
9662
0
    return 0;
9663
0
  }
9664
9665
0
  if (opline->opcode == ZEND_SEND_VAR_EX) {
9666
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9667
0
     && JIT_G(current_frame)
9668
0
     && JIT_G(current_frame)->call
9669
0
     && JIT_G(current_frame)->call->func) {
9670
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9671
0
        if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 0)) {
9672
0
          return 0;
9673
0
        }
9674
0
        return 1;
9675
0
      }
9676
0
    } else {
9677
0
      uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9678
9679
      // JIT: if (RX->func->quick_arg_flags & mask)
9680
0
      ir_ref if_send_by_ref = ir_IF(ir_AND_U32(
9681
0
        ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_RX(func)), offsetof(zend_function, quick_arg_flags))),
9682
0
        ir_CONST_U32(mask)));
9683
0
      ir_IF_TRUE_cold(if_send_by_ref);
9684
9685
0
      if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 1)) {
9686
0
        return 0;
9687
0
      }
9688
9689
0
      ir_END_list(end_inputs);
9690
0
      ir_IF_FALSE(if_send_by_ref);
9691
0
    }
9692
0
  } else if (opline->opcode == ZEND_SEND_VAR_NO_REF_EX) {
9693
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9694
0
     && JIT_G(current_frame)
9695
0
     && JIT_G(current_frame)->call
9696
0
     && JIT_G(current_frame)->call->func) {
9697
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9698
9699
            // JIT: ZVAL_COPY_VALUE(arg, op1)
9700
0
        jit_ZVAL_COPY(jit,
9701
0
          arg_addr,
9702
0
          MAY_BE_ANY,
9703
0
          op1_addr, op1_info, false);
9704
9705
0
        if (!ARG_MAY_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9706
0
          if (!(op1_info & MAY_BE_REF)) {
9707
            /* Don't generate code that always throws exception */
9708
0
            return 0;
9709
0
          } else {
9710
0
            int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9711
0
            const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9712
0
            if (!exit_addr) {
9713
0
              return 0;
9714
0
            }
9715
9716
            // JIT: if (Z_TYPE_P(op1) != IS_REFERENCE)
9717
0
            ir_GUARD(ir_EQ(jit_Z_TYPE(jit, op1_addr), ir_CONST_U32(IS_REFERENCE)),
9718
0
              ir_CONST_ADDR(exit_addr));
9719
0
          }
9720
0
        }
9721
0
        return 1;
9722
0
      }
9723
0
    } else {
9724
0
      uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9725
0
      ir_ref func, if_send_by_ref, if_prefer_ref;
9726
9727
      // JIT: if (RX->func->quick_arg_flags & mask)
9728
0
      func = ir_LOAD_A(jit_RX(func));
9729
0
      if_send_by_ref = ir_IF(ir_AND_U32(
9730
0
        ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, quick_arg_flags))),
9731
0
        ir_CONST_U32(mask)));
9732
0
      ir_IF_TRUE_cold(if_send_by_ref);
9733
9734
0
      mask = ZEND_SEND_PREFER_REF << ((arg_num + 3) * 2);
9735
9736
          // JIT: ZVAL_COPY_VALUE(arg, op1)
9737
0
      jit_ZVAL_COPY(jit,
9738
0
        arg_addr,
9739
0
        MAY_BE_ANY,
9740
0
        op1_addr, op1_info, false);
9741
9742
0
      if (op1_info & MAY_BE_REF) {
9743
0
        ir_ref if_ref = jit_if_Z_TYPE(jit, arg_addr, IS_REFERENCE);
9744
0
        ir_IF_TRUE(if_ref);
9745
0
        ir_END_list(end_inputs);
9746
0
        ir_IF_FALSE(if_ref);
9747
0
      }
9748
9749
      // JIT: if (RX->func->quick_arg_flags & mask)
9750
0
      if_prefer_ref = ir_IF(ir_AND_U32(
9751
0
        ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, quick_arg_flags))),
9752
0
        ir_CONST_U32(mask)));
9753
0
      ir_IF_TRUE(if_prefer_ref);
9754
0
      ir_END_list(end_inputs);
9755
0
      ir_IF_FALSE(if_prefer_ref);
9756
9757
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9758
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9759
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9760
0
        if (!exit_addr) {
9761
0
          return 0;
9762
0
        }
9763
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
9764
0
      } else {
9765
0
        jit_SET_EX_OPLINE(jit, opline);
9766
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_only_vars_by_reference),
9767
0
          jit_ZVAL_ADDR(jit, arg_addr));
9768
0
        zend_jit_check_exception(jit);
9769
0
        ir_END_list(end_inputs);
9770
0
      }
9771
9772
0
      ir_IF_FALSE(if_send_by_ref);
9773
0
    }
9774
0
  } else if (opline->opcode == ZEND_SEND_FUNC_ARG) {
9775
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9776
0
     && JIT_G(current_frame)
9777
0
     && JIT_G(current_frame)->call
9778
0
     && JIT_G(current_frame)->call->func) {
9779
0
      if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9780
0
        if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 0)) {
9781
0
          return 0;
9782
0
        }
9783
0
        return 1;
9784
0
      }
9785
0
    } else {
9786
      // JIT: if (RX->This.u1.type_info & ZEND_CALL_SEND_ARG_BY_REF)
9787
0
      ir_ref if_send_by_ref = ir_IF(ir_AND_U32(
9788
0
        ir_LOAD_U32(jit_RX(This.u1.type_info)),
9789
0
        ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9790
0
      ir_IF_TRUE_cold(if_send_by_ref);
9791
9792
0
      if (!zend_jit_send_ref(jit, opline, op_array, op1_info, 1)) {
9793
0
        return 0;
9794
0
      }
9795
9796
0
      ir_END_list(end_inputs);
9797
0
      ir_IF_FALSE(if_send_by_ref);
9798
0
    }
9799
0
  }
9800
9801
0
  if (op1_info & MAY_BE_UNDEF) {
9802
0
    ir_ref ref, if_def = IR_UNUSED;
9803
9804
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9805
0
      if_def = jit_if_not_Z_TYPE(jit, op1_addr, IS_UNDEF);
9806
0
      ir_IF_FALSE_cold(if_def);
9807
0
    }
9808
9809
    // JIT: zend_jit_undefined_op_helper(opline->op1.var)
9810
0
    jit_SET_EX_OPLINE(jit, opline);
9811
0
    ref = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
9812
0
      ir_CONST_U32(opline->op1.var));
9813
9814
    // JIT: ZVAL_NULL(arg)
9815
0
    jit_set_Z_TYPE_INFO(jit, arg_addr, IS_NULL);
9816
9817
    // JIT: check_exception
9818
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
9819
9820
0
    if (op1_info & (MAY_BE_ANY|MAY_BE_REF)) {
9821
0
      ir_END_list(end_inputs);
9822
0
      ir_IF_TRUE(if_def);
9823
0
    } else {
9824
0
      if (end_inputs) {
9825
0
        ir_END_list(end_inputs);
9826
0
        ir_MERGE_list(end_inputs);
9827
0
      }
9828
0
      return 1;
9829
0
    }
9830
0
  }
9831
9832
0
  if (opline->opcode == ZEND_SEND_VAR_NO_REF) {
9833
        // JIT: ZVAL_COPY_VALUE(arg, op1)
9834
0
    jit_ZVAL_COPY(jit,
9835
0
      arg_addr,
9836
0
      MAY_BE_ANY,
9837
0
      op1_addr, op1_info, false);
9838
0
    if (op1_info & MAY_BE_REF) {
9839
        // JIT: if (Z_TYPE_P(arg) == IS_REFERENCE)
9840
0
        ir_ref if_ref = jit_if_Z_TYPE(jit, arg_addr, IS_REFERENCE);
9841
0
        ir_IF_TRUE(if_ref);
9842
0
        ir_END_list(end_inputs);
9843
0
        ir_IF_FALSE(if_ref);
9844
0
    }
9845
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
9846
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
9847
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
9848
0
      if (!exit_addr) {
9849
0
        return 0;
9850
0
      }
9851
0
      ir_GUARD(IR_FALSE, ir_CONST_ADDR(exit_addr));
9852
0
    } else {
9853
0
      jit_SET_EX_OPLINE(jit, opline);
9854
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_only_vars_by_reference),
9855
0
        jit_ZVAL_ADDR(jit, arg_addr));
9856
0
      zend_jit_check_exception(jit);
9857
0
    }
9858
0
  } else {
9859
0
    if (op1_info & MAY_BE_REF) {
9860
0
      if (opline->op1_type == IS_CV) {
9861
0
        ir_ref ref;
9862
9863
        // JIT: ZVAL_DEREF(op1)
9864
0
        ref = jit_ZVAL_ADDR(jit, op1_addr);
9865
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
9866
0
        op1_addr = ZEND_ADDR_REF_ZVAL(ref);
9867
9868
            // JIT: ZVAL_COPY(arg, op1)
9869
0
        jit_ZVAL_COPY(jit,
9870
0
          arg_addr,
9871
0
          MAY_BE_ANY,
9872
0
          op1_addr, op1_info, true);
9873
0
      } else {
9874
0
        ir_ref if_ref, ref, ref2, refcount, if_not_zero, if_refcounted;
9875
0
        zend_jit_addr ref_addr;
9876
9877
        // JIT: if (Z_TYPE_P(op1) == IS_REFERENCE)
9878
0
        if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
9879
0
        ir_IF_TRUE_cold(if_ref);
9880
9881
        // JIT: ref = Z_COUNTED_P(op1);
9882
0
        ref = jit_Z_PTR(jit, op1_addr);
9883
0
        ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
9884
0
        ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
9885
9886
        // JIT: ZVAL_COPY_VALUE(arg, op1);
9887
0
        jit_ZVAL_COPY(jit,
9888
0
          arg_addr,
9889
0
          MAY_BE_ANY,
9890
0
          ref_addr, op1_info, false);
9891
9892
        // JIT: if (GC_DELREF(ref) != 0)
9893
0
        refcount = jit_GC_DELREF(jit, ref);
9894
0
        if_not_zero = ir_IF(refcount);
9895
0
        ir_IF_TRUE(if_not_zero);
9896
9897
                // JIT: if (Z_REFCOUNTED_P(arg)
9898
0
        if_refcounted = jit_if_REFCOUNTED(jit, arg_addr);
9899
0
        ir_IF_TRUE(if_refcounted);
9900
        // JIT: Z_ADDREF_P(arg)
9901
0
        jit_GC_ADDREF(jit, jit_Z_PTR(jit, arg_addr));
9902
0
        ir_END_list(end_inputs);
9903
0
        ir_IF_FALSE(if_refcounted);
9904
0
        ir_END_list(end_inputs);
9905
9906
0
        ir_IF_FALSE(if_not_zero);
9907
9908
        // JIT: efree(ref)
9909
0
        jit_EFREE(jit, ref, sizeof(zend_reference), op_array, opline);
9910
0
        ir_END_list(end_inputs);
9911
9912
0
        ir_IF_FALSE(if_ref);
9913
9914
        // JIT: ZVAL_COPY_VALUE(arg, op1);
9915
0
        jit_ZVAL_COPY(jit,
9916
0
          arg_addr,
9917
0
          MAY_BE_ANY,
9918
0
          op1_addr, op1_info, false);
9919
0
      }
9920
0
    } else {
9921
0
      if (op1_addr != op1_def_addr) {
9922
0
        if (!zend_jit_update_regs(jit, opline->op1.var, op1_addr, op1_def_addr, op1_info)) {
9923
0
          return 0;
9924
0
        }
9925
0
        if (Z_MODE(op1_def_addr) == IS_REG && Z_MODE(op1_addr) != IS_REG) {
9926
0
          op1_addr = op1_def_addr;
9927
0
        }
9928
0
      }
9929
9930
          // JIT: ZVAL_COPY_VALUE(arg, op1)
9931
0
      jit_ZVAL_COPY(jit,
9932
0
        arg_addr,
9933
0
        MAY_BE_ANY,
9934
0
        op1_addr, op1_info, opline->op1_type == IS_CV);
9935
0
    }
9936
0
  }
9937
9938
0
  if (end_inputs) {
9939
0
    ir_END_list(end_inputs);
9940
0
    ir_MERGE_list(end_inputs);
9941
0
  }
9942
9943
0
  return 1;
9944
0
}
9945
9946
static int zend_jit_check_func_arg(zend_jit_ctx *jit, const zend_op *opline)
9947
0
{
9948
0
  uint32_t arg_num = opline->op2.num;
9949
0
  ir_ref ref;
9950
9951
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
9952
0
   && JIT_G(current_frame)
9953
0
   && JIT_G(current_frame)->call
9954
0
   && JIT_G(current_frame)->call->func) {
9955
0
    if (ARG_SHOULD_BE_SENT_BY_REF(JIT_G(current_frame)->call->func, arg_num)) {
9956
0
      if (!TRACE_FRAME_IS_LAST_SEND_BY_REF(JIT_G(current_frame)->call)) {
9957
0
        TRACE_FRAME_SET_LAST_SEND_BY_REF(JIT_G(current_frame)->call);
9958
        // JIT: ZEND_ADD_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9959
0
        if (jit->reuse_ip) {
9960
0
          ref = jit_IP(jit);
9961
0
        } else {
9962
0
          ref = ir_LOAD_A(jit_EX(call));
9963
0
        }
9964
0
        ref = jit_CALL(ref, This.u1.type_info);
9965
0
        ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
9966
0
      }
9967
0
    } else {
9968
0
      if (!TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
9969
0
        TRACE_FRAME_SET_LAST_SEND_BY_VAL(JIT_G(current_frame)->call);
9970
        // JIT: ZEND_DEL_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9971
0
        if (jit->reuse_ip) {
9972
0
          ref = jit_IP(jit);
9973
0
        } else {
9974
0
          ref = ir_LOAD_A(jit_EX(call));
9975
0
        }
9976
0
        ref = jit_CALL(ref, This.u1.type_info);
9977
0
        ir_STORE(ref, ir_AND_U32(ir_LOAD_U32(ref), ir_CONST_U32(~ZEND_CALL_SEND_ARG_BY_REF)));
9978
0
      }
9979
0
    }
9980
0
  } else {
9981
    // JIT: if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(EX(call)->func, arg_num)) {
9982
0
    uint32_t mask = (ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF) << ((arg_num + 3) * 2);
9983
0
    ir_ref rx, if_ref, cold_path;
9984
9985
0
    if (!zend_jit_reuse_ip(jit)) {
9986
0
      return 0;
9987
0
    }
9988
9989
0
    rx = jit_IP(jit);
9990
9991
0
    ref = ir_AND_U32(
9992
0
      ir_LOAD_U32(ir_ADD_OFFSET(ir_LOAD_A(jit_CALL(rx, func)), offsetof(zend_function, quick_arg_flags))),
9993
0
      ir_CONST_U32(mask));
9994
0
    if_ref = ir_IF(ref);
9995
0
    ir_IF_TRUE_cold(if_ref);
9996
9997
    // JIT: ZEND_ADD_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
9998
0
    ref = jit_CALL(rx, This.u1.type_info);
9999
0
    ir_STORE(ref, ir_OR_U32(ir_LOAD_U32(ref), ir_CONST_U32(ZEND_CALL_SEND_ARG_BY_REF)));
10000
10001
0
    cold_path = ir_END();
10002
0
    ir_IF_FALSE(if_ref);
10003
10004
    // JIT: ZEND_DEL_CALL_FLAG(EX(call), ZEND_CALL_SEND_ARG_BY_REF);
10005
0
    ref = jit_CALL(rx, This.u1.type_info);
10006
0
    ir_STORE(ref, ir_AND_U32(ir_LOAD_U32(ref), ir_CONST_U32(~ZEND_CALL_SEND_ARG_BY_REF)));
10007
10008
0
    ir_MERGE_WITH(cold_path);
10009
0
  }
10010
10011
0
  return 1;
10012
0
}
10013
10014
static int zend_jit_check_undef_args(zend_jit_ctx *jit, const zend_op *opline)
10015
0
{
10016
0
  ir_ref call, if_may_have_undef, ret;
10017
10018
0
  if (jit->reuse_ip) {
10019
0
    call = jit_IP(jit);
10020
0
  } else {
10021
0
    call = ir_LOAD_A(jit_EX(call));
10022
0
  }
10023
10024
0
  if_may_have_undef = ir_IF(ir_AND_U8(
10025
0
    ir_LOAD_U8(ir_ADD_OFFSET(call, offsetof(zend_execute_data, This.u1.type_info) + 3)),
10026
0
    ir_CONST_U8(ZEND_CALL_MAY_HAVE_UNDEF >> 24)));
10027
10028
0
  ir_IF_TRUE_cold(if_may_have_undef);
10029
0
  jit_SET_EX_OPLINE(jit, opline);
10030
0
  ret = ir_CALL_1(IR_I32, ir_CONST_FC_FUNC(zend_handle_undef_args), call);
10031
0
  ir_GUARD_NOT(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10032
0
  ir_MERGE_WITH_EMPTY_FALSE(if_may_have_undef);
10033
10034
0
  return 1;
10035
0
}
10036
10037
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)
10038
0
{
10039
0
  zend_func_info *info = ZEND_FUNC_INFO(op_array);
10040
0
  zend_call_info *call_info = NULL;
10041
0
  const zend_function *func = NULL;
10042
0
  uint32_t i;
10043
0
  uint32_t call_num_args = 0;
10044
0
  bool unknown_num_args = false;
10045
0
  const void *exit_addr = NULL;
10046
0
  const zend_op *prev_opline;
10047
0
  ir_ref rx, func_ref = IR_UNUSED, if_user = IR_UNUSED, user_path = IR_UNUSED;
10048
10049
0
  prev_opline = opline - 1;
10050
0
  while (prev_opline->opcode == ZEND_EXT_FCALL_BEGIN || prev_opline->opcode == ZEND_TICKS) {
10051
0
    prev_opline--;
10052
0
  }
10053
0
  if (prev_opline->opcode == ZEND_SEND_UNPACK || prev_opline->opcode == ZEND_SEND_ARRAY ||
10054
0
      prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS) {
10055
0
    unknown_num_args = true;
10056
0
  }
10057
10058
0
  if (info) {
10059
0
    call_info = info->callee_info;
10060
0
    while (call_info && call_info->caller_call_opline != opline) {
10061
0
      call_info = call_info->next_callee;
10062
0
    }
10063
0
    if (call_info && call_info->callee_func && !call_info->is_prototype) {
10064
0
      func = call_info->callee_func;
10065
0
    }
10066
0
    if ((op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)
10067
0
     && (!JIT_G(current_frame)
10068
0
      || !JIT_G(current_frame)->call
10069
0
      || !JIT_G(current_frame)->call->func)) {
10070
0
      call_info = NULL; func = NULL; /* megamorphic call from trait */
10071
0
    }
10072
0
  }
10073
0
  if (!func) {
10074
    /* resolve function at run time */
10075
0
  } else if (func->type == ZEND_USER_FUNCTION) {
10076
0
    ZEND_ASSERT(opline->opcode != ZEND_DO_ICALL);
10077
0
    call_num_args = call_info->num_args;
10078
0
  } else if (func->type == ZEND_INTERNAL_FUNCTION) {
10079
0
    ZEND_ASSERT(opline->opcode != ZEND_DO_UCALL);
10080
0
    call_num_args = call_info->num_args;
10081
0
  } else {
10082
0
    ZEND_UNREACHABLE();
10083
0
  }
10084
10085
0
  if (trace && !func) {
10086
0
    if (trace->op == ZEND_JIT_TRACE_DO_ICALL) {
10087
0
      ZEND_ASSERT(!trace->func || trace->func->type == ZEND_INTERNAL_FUNCTION);
10088
0
#ifndef ZEND_WIN32
10089
      // TODO: ASLR may cause different addresses in different workers ???
10090
0
      func = trace->func;
10091
0
      if (JIT_G(current_frame) &&
10092
0
          JIT_G(current_frame)->call &&
10093
0
          TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call) >= 0) {
10094
0
        call_num_args = TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call);
10095
0
      } else {
10096
0
        unknown_num_args = true;
10097
0
      }
10098
0
#endif
10099
0
    } else if (trace->op == ZEND_JIT_TRACE_ENTER) {
10100
0
      ZEND_ASSERT(trace->func->type == ZEND_USER_FUNCTION);
10101
0
      if (zend_accel_in_shm(trace->func->op_array.opcodes)) {
10102
0
        func = trace->func;
10103
0
        if (JIT_G(current_frame) &&
10104
0
            JIT_G(current_frame)->call &&
10105
0
            TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call) >= 0) {
10106
0
          call_num_args = TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)->call);
10107
0
        } else {
10108
0
          unknown_num_args = true;
10109
0
        }
10110
0
      }
10111
0
    }
10112
0
  }
10113
10114
0
  bool may_have_extra_named_params =
10115
0
    opline->extended_value == ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS &&
10116
0
    (!func || func->common.fn_flags & ZEND_ACC_VARIADIC);
10117
10118
0
  if (!jit->reuse_ip) {
10119
0
    zend_jit_start_reuse_ip(jit);
10120
    // JIT: call = EX(call);
10121
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(call)));
10122
0
  }
10123
0
  rx = jit_IP(jit);
10124
0
  zend_jit_stop_reuse_ip(jit);
10125
10126
0
  jit_SET_EX_OPLINE(jit, opline);
10127
10128
0
  if (opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) {
10129
0
    if (!func) {
10130
0
      if (trace) {
10131
0
        uint32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10132
10133
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10134
0
        if (!exit_addr) {
10135
0
          return 0;
10136
0
        }
10137
10138
0
        func_ref = ir_LOAD_A(jit_CALL(rx, func));
10139
0
        ir_GUARD_NOT(
10140
0
          ir_AND_U32(
10141
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10142
0
            ir_CONST_U32(ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD)),
10143
0
          ir_CONST_ADDR(exit_addr));
10144
0
      }
10145
0
    }
10146
0
  }
10147
10148
0
  if (!jit->delayed_call_level) {
10149
    // JIT: EX(call) = call->prev_execute_data;
10150
0
    ir_STORE(jit_EX(call),
10151
0
      (call_level == 1) ? IR_NULL : ir_LOAD_A(jit_CALL(rx, prev_execute_data)));
10152
0
  }
10153
0
  delayed_call_chain = false;
10154
0
  jit->delayed_call_level = 0;
10155
10156
  // JIT: call->prev_execute_data = execute_data;
10157
0
  ir_STORE(jit_CALL(rx, prev_execute_data), jit_FP(jit));
10158
10159
0
  if (!func) {
10160
0
    if (!func_ref) {
10161
0
      func_ref = ir_LOAD_A(jit_CALL(rx, func));
10162
0
    }
10163
0
  }
10164
10165
0
  if (opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) {
10166
0
    if (!func) {
10167
0
      if (!trace) {
10168
0
        ir_ref if_deprecated_nodiscard, ret;
10169
10170
0
        uint32_t no_discard = RETURN_VALUE_USED(opline) ? 0 : ZEND_ACC_NODISCARD;
10171
10172
0
        if_deprecated_nodiscard = ir_IF(ir_AND_U32(
10173
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10174
0
            ir_CONST_U32(ZEND_ACC_DEPRECATED|no_discard)));
10175
0
        ir_IF_TRUE_cold(if_deprecated_nodiscard);
10176
10177
0
        ir_ref helper = ir_CONST_FC_FUNC(no_discard ? zend_jit_deprecated_nodiscard_helper : zend_jit_deprecated_helper);
10178
0
        if (GCC_GLOBAL_REGS) {
10179
0
          ret = ir_CALL(IR_BOOL, helper);
10180
0
        } else {
10181
0
          ret = ir_CALL_1(IR_BOOL, helper, rx);
10182
0
        }
10183
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10184
0
        ir_MERGE_WITH_EMPTY_FALSE(if_deprecated_nodiscard);
10185
0
      }
10186
0
    } else {
10187
0
      if (func->common.fn_flags & ZEND_ACC_DEPRECATED) {
10188
0
        ir_ref ret;
10189
10190
0
        if (GCC_GLOBAL_REGS) {
10191
0
          ret = ir_CALL(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_deprecated_helper));
10192
0
        } else {
10193
0
          ret = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_deprecated_helper), rx);
10194
0
        }
10195
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10196
0
      }
10197
10198
0
      if ((func->common.fn_flags & ZEND_ACC_NODISCARD) && !RETURN_VALUE_USED(opline)) {
10199
0
        ir_ref ret;
10200
10201
0
        if (GCC_GLOBAL_REGS) {
10202
0
          ret = ir_CALL(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_nodiscard_helper));
10203
0
        } else {
10204
0
          ret = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_nodiscard_helper), rx);
10205
0
        }
10206
0
        ir_GUARD(ret, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10207
0
      }
10208
0
    }
10209
0
  }
10210
10211
0
  if (!func
10212
0
   && opline->opcode != ZEND_DO_UCALL
10213
0
   && opline->opcode != ZEND_DO_ICALL) {
10214
0
    ir_ref type_ref = ir_LOAD_U8(ir_ADD_OFFSET(func_ref, offsetof(zend_function, type)));
10215
0
    if_user = ir_IF(ir_EQ(type_ref, ir_CONST_U8(ZEND_USER_FUNCTION)));
10216
0
    ir_IF_TRUE(if_user);
10217
0
  }
10218
10219
0
  if ((!func || func->type == ZEND_USER_FUNCTION)
10220
0
   && opline->opcode != ZEND_DO_ICALL) {
10221
0
    bool recursive_call_through_jmp = false;
10222
0
    uint32_t num_args = 0;
10223
10224
    // JIT: EX(call) = NULL;
10225
0
    ir_STORE(jit_CALL(rx, call), IR_NULL);
10226
10227
    // JIT: EX(return_value) = RETURN_VALUE_USED(opline) ? EX_VAR(opline->result.var) : 0;
10228
0
    ir_STORE(jit_CALL(rx, return_value),
10229
0
      RETURN_VALUE_USED(opline) ?
10230
0
        jit_ZVAL_ADDR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var)) :
10231
0
        IR_NULL);
10232
10233
    // JIT: EX_LOAD_RUN_TIME_CACHE(op_array);
10234
0
    if (!func || func->op_array.cache_size) {
10235
0
      ir_ref run_time_cache;
10236
10237
0
      if (func && op_array == &func->op_array) {
10238
        /* recursive call */
10239
0
        run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
10240
0
      } else if (func
10241
0
       && !(func->op_array.fn_flags & ZEND_ACC_CLOSURE)
10242
0
       && ZEND_MAP_PTR_IS_OFFSET(func->op_array.run_time_cache)) {
10243
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_CG(map_ptr_base)),
10244
0
          (uintptr_t)ZEND_MAP_PTR(func->op_array.run_time_cache)));
10245
0
      } else if ((func && (func->op_array.fn_flags & ZEND_ACC_CLOSURE)) ||
10246
0
          (JIT_G(current_frame) &&
10247
0
           JIT_G(current_frame)->call &&
10248
0
           TRACE_FRAME_IS_CLOSURE_CALL(JIT_G(current_frame)->call))) {
10249
        /* Closures always use direct pointers */
10250
0
        ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
10251
10252
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(local_func_ref, offsetof(zend_op_array, run_time_cache__ptr)));
10253
0
      } else {
10254
0
        ir_ref if_odd, run_time_cache2;
10255
0
        ir_ref local_func_ref = func_ref ? func_ref : ir_LOAD_A(jit_CALL(rx, func));
10256
10257
0
        run_time_cache = ir_LOAD_A(ir_ADD_OFFSET(local_func_ref, offsetof(zend_op_array, run_time_cache__ptr)));
10258
0
        if_odd = ir_IF(ir_AND_A(run_time_cache, ir_CONST_ADDR(1)));
10259
0
        ir_IF_TRUE(if_odd);
10260
10261
0
        run_time_cache2 = ir_LOAD_A(ir_ADD_A(run_time_cache, ir_LOAD_A(jit_CG(map_ptr_base))));
10262
10263
0
        ir_MERGE_WITH_EMPTY_FALSE(if_odd);
10264
0
        run_time_cache = ir_PHI_2(IR_ADDR, run_time_cache2, run_time_cache);
10265
0
      }
10266
10267
0
      ir_STORE(jit_CALL(rx, run_time_cache), run_time_cache);
10268
0
    }
10269
10270
    // JIT: EG(current_execute_data) = execute_data = call;
10271
0
    ir_STORE(jit_EG(current_execute_data), rx);
10272
0
    jit_STORE_FP(jit, rx);
10273
10274
    // JIT: opline = op_array->opcodes;
10275
0
    if (func && !unknown_num_args) {
10276
10277
0
      for (i = call_num_args; i < func->op_array.last_var; i++) {
10278
0
        uint32_t n = EX_NUM_TO_VAR(i);
10279
0
        zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, n);
10280
10281
0
        jit_set_Z_TYPE_INFO_ex(jit, var_addr, ir_CONST_U32(IS_UNDEF));
10282
0
      }
10283
10284
0
      if (call_num_args <= func->op_array.num_args) {
10285
0
        if (!trace || (trace->op == ZEND_JIT_TRACE_END
10286
0
         && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10287
0
          if ((func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0) {
10288
0
            if (trace) {
10289
0
              num_args = 0;
10290
0
            } else if (call_info) {
10291
0
              num_args = skip_valid_arguments(op_array, ssa, call_info);
10292
0
            } else {
10293
0
              num_args = call_num_args;
10294
0
            }
10295
0
          } else {
10296
0
            num_args = call_num_args;
10297
0
          }
10298
0
          if (zend_accel_in_shm(func->op_array.opcodes)) {
10299
0
            jit_LOAD_IP_ADDR(jit, func->op_array.opcodes + num_args);
10300
0
          } else {
10301
0
            if (!func_ref) {
10302
0
              func_ref = ir_LOAD_A(jit_CALL(rx, func));
10303
0
            }
10304
0
            ir_ref ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10305
0
            if (num_args) {
10306
0
              ip = ir_ADD_OFFSET(ip, num_args * sizeof(zend_op));
10307
0
            }
10308
0
            jit_STORE_IP(jit, ip);
10309
0
          }
10310
10311
0
          if (!trace && op_array == &func->op_array && call_num_args >= op_array->required_num_args) {
10312
            /* recursive call */
10313
0
            recursive_call_through_jmp = true;
10314
0
          }
10315
0
        }
10316
0
      } else {
10317
0
        ir_ref helper;
10318
0
        if (!trace || (trace->op == ZEND_JIT_TRACE_END
10319
0
         && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10320
0
          ir_ref ip;
10321
10322
0
          if (zend_accel_in_shm(func->op_array.opcodes)) {
10323
0
            ip = ir_CONST_ADDR(func->op_array.opcodes);
10324
0
          } else {
10325
0
            if (!func_ref) {
10326
0
              func_ref = ir_LOAD_A(jit_CALL(rx, func));
10327
0
            }
10328
0
            ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10329
0
          }
10330
0
          jit_STORE_IP(jit, ip);
10331
0
          helper = ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper);
10332
0
        } else {
10333
0
          helper = ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper_no_skip_recv);
10334
0
        }
10335
0
        if (GCC_GLOBAL_REGS) {
10336
0
          ir_CALL(IR_VOID, helper);
10337
0
        } else if (ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
10338
0
          ir_CALL_2(IR_ADDR, helper, jit_FP(jit), jit_IP(jit));
10339
0
        } else {
10340
0
          ir_ref ref = ir_CALL_2(IR_ADDR, helper, jit_FP(jit), jit_IP(jit));
10341
0
          jit_STORE_IP(jit, ref);
10342
0
        }
10343
0
      }
10344
0
    } else {
10345
0
      ir_ref ip;
10346
0
      ir_ref merge_inputs = IR_UNUSED;
10347
10348
      // JIT: opline = op_array->opcodes
10349
0
      if (func && zend_accel_in_shm(func->op_array.opcodes)) {
10350
0
        ip = ir_CONST_ADDR(func->op_array.opcodes);
10351
0
      } else {
10352
0
        if (!func_ref) {
10353
0
          func_ref = ir_LOAD_A(jit_CALL(rx, func));
10354
0
        }
10355
0
        ip = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, opcodes)));
10356
0
      }
10357
0
      jit_STORE_IP(jit, ip);
10358
10359
      // JIT: num_args = EX_NUM_ARGS();
10360
0
      ir_ref num_args, first_extra_arg;
10361
10362
0
      num_args = ir_LOAD_U32(jit_EX(This.u2.num_args));
10363
0
      if (func) {
10364
0
        first_extra_arg = ir_CONST_U32(func->op_array.num_args);
10365
0
      } else {
10366
        // JIT: first_extra_arg = op_array->num_args;
10367
0
        ZEND_ASSERT(func_ref);
10368
0
        first_extra_arg = ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, num_args)));
10369
0
      }
10370
10371
      // JIT: if (UNEXPECTED(num_args > first_extra_arg))
10372
0
      ir_ref if_extra_args = ir_IF(ir_GT(num_args, first_extra_arg));
10373
0
      ir_IF_TRUE_cold(if_extra_args);
10374
0
      if (GCC_GLOBAL_REGS) {
10375
0
        ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_copy_extra_args_helper));
10376
0
      } else {
10377
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));
10378
0
        jit_STORE_IP(jit, ref);
10379
0
      }
10380
0
      ir_END_list(merge_inputs);
10381
0
      ir_IF_FALSE(if_extra_args);
10382
0
      if (!func || (func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0) {
10383
0
        if (!func) {
10384
          // JIT: if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0))
10385
0
          ir_ref if_has_type_hints = ir_IF(ir_AND_U32(
10386
0
            ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, fn_flags))),
10387
0
            ir_CONST_U32(ZEND_ACC_HAS_TYPE_HINTS)));
10388
0
          ir_IF_TRUE(if_has_type_hints);
10389
0
          ir_END_list(merge_inputs);
10390
0
          ir_IF_FALSE(if_has_type_hints);
10391
0
        }
10392
        // JIT: opline += num_args;
10393
10394
0
        ir_ref ref = ir_MUL_U32(num_args, ir_CONST_U32(sizeof(zend_op)));
10395
10396
0
        if (sizeof(void*) == 8) {
10397
0
          ref = ir_ZEXT_A(ref);
10398
0
        }
10399
10400
0
        jit_STORE_IP(jit, ir_ADD_A(jit_IP(jit), ref));
10401
0
      }
10402
10403
0
      ir_END_list(merge_inputs);
10404
0
      ir_MERGE_list(merge_inputs);
10405
10406
      // JIT: if (EXPECTED((int)num_args < op_array->last_var)) {
10407
0
      ir_ref last_var;
10408
10409
0
      if (func) {
10410
0
        last_var = ir_CONST_U32(func->op_array.last_var);
10411
0
      } else {
10412
0
        ZEND_ASSERT(func_ref);
10413
0
        last_var = ir_LOAD_U32(ir_ADD_OFFSET(func_ref, offsetof(zend_op_array, last_var)));
10414
0
      }
10415
10416
0
      ir_ref idx = ir_SUB_U32(last_var, num_args);
10417
0
      ir_ref if_need = ir_IF(ir_GT(idx, ir_CONST_U32(0)));
10418
0
      ir_IF_TRUE(if_need);
10419
10420
      // JIT: zval *var = EX_VAR_NUM(num_args);
10421
0
      if (sizeof(void*) == 8) {
10422
0
        num_args = ir_ZEXT_A(num_args);
10423
0
      }
10424
0
      ir_ref var_ref = ir_ADD_OFFSET(
10425
0
        ir_ADD_A(jit_FP(jit), ir_MUL_A(num_args, ir_CONST_ADDR(sizeof(zval)))),
10426
0
        (ZEND_CALL_FRAME_SLOT * sizeof(zval)) + offsetof(zval, u1.type_info));
10427
10428
0
      ir_ref loop = ir_LOOP_BEGIN(ir_END());
10429
0
      var_ref = ir_PHI_2(IR_ADDR, var_ref, IR_UNUSED);
10430
0
      idx = ir_PHI_2(IR_U32, idx, IR_UNUSED);
10431
0
      ir_STORE(var_ref, ir_CONST_I32(IS_UNDEF));
10432
0
      ir_PHI_SET_OP(var_ref, 2, ir_ADD_OFFSET(var_ref, sizeof(zval)));
10433
0
      ir_ref idx2 = ir_SUB_U32(idx, ir_CONST_U32(1));
10434
0
      ir_PHI_SET_OP(idx, 2, idx2);
10435
0
      ir_ref if_not_zero = ir_IF(idx2);
10436
0
      ir_IF_TRUE(if_not_zero);
10437
0
      ir_MERGE_SET_OP(loop, 2, ir_LOOP_END());
10438
0
      ir_IF_FALSE(if_not_zero);
10439
0
      ir_MERGE_WITH_EMPTY_FALSE(if_need);
10440
0
    }
10441
10442
0
    if (ZEND_OBSERVER_ENABLED && (!func || (func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0)) {
10443
0
      ir_ref observer_handler;
10444
0
      ir_ref rx = jit_FP(jit);
10445
0
      const zend_op *observer_opline = NULL;
10446
0
      struct jit_observer_fcall_is_unobserved_data unobserved_data = jit_observer_fcall_is_unobserved_start(jit, func, &observer_handler, rx, func_ref);
10447
0
      if (trace && (trace->op != ZEND_JIT_TRACE_END || trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
10448
0
        ZEND_ASSERT(trace[1].op == ZEND_JIT_TRACE_VM || trace[1].op == ZEND_JIT_TRACE_END);
10449
0
        observer_opline = trace[1].opline;
10450
0
        jit_SET_EX_OPLINE(jit, observer_opline);
10451
0
      } else {
10452
        // EX(opline) = opline
10453
0
        ir_STORE(jit_EX(opline), jit_IP(jit));
10454
0
      }
10455
0
      jit_observer_fcall_begin(jit, rx, observer_handler);
10456
10457
0
      zend_jit_check_timeout(jit, observer_opline, NULL);
10458
10459
0
      jit_observer_fcall_is_unobserved_end(jit, &unobserved_data);
10460
0
    }
10461
10462
0
    if (trace) {
10463
0
      if (!func && (opline->opcode != ZEND_DO_UCALL)) {
10464
0
        user_path = ir_END();
10465
0
      }
10466
0
    } else {
10467
0
      zend_basic_block *bb;
10468
10469
0
      do {
10470
0
        if (recursive_call_through_jmp) {
10471
0
          ir_ref begin, end;
10472
0
          ir_insn *insn;
10473
10474
          /* attempt to convert direct recursive call into loop */
10475
0
          begin = jit->bb_start_ref[num_args];
10476
0
          ZEND_ASSERT(begin != IR_UNUSED);
10477
0
          insn = &jit->ctx.ir_base[begin];
10478
0
          if (insn->op == IR_BEGIN) {
10479
0
            end = ir_LOOP_END();
10480
0
            insn = &jit->ctx.ir_base[begin];
10481
0
            insn->op = IR_LOOP_BEGIN;
10482
0
            insn->inputs_count = 2;
10483
0
            insn->op2 = end;
10484
0
            break;
10485
0
          } else if ((insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN)
10486
0
              && insn->inputs_count == 2) {
10487
0
            end = ir_LOOP_END();
10488
0
            insn = &jit->ctx.ir_base[begin];
10489
0
            insn->op = IR_LOOP_BEGIN;
10490
0
            insn->inputs_count = 3;
10491
0
            insn->op3 = end;
10492
0
            break;
10493
0
          } else if (insn->op == IR_LOOP_BEGIN && insn->inputs_count == 3) {
10494
0
            ZEND_ASSERT(jit->ctx.ir_base[insn->op3].op == IR_LOOP_END);
10495
0
            jit->ctx.ir_base[insn->op3].op = IR_END;
10496
0
            ir_MERGE_2(insn->op3, ir_END());
10497
0
            end = ir_LOOP_END();
10498
0
            insn = &jit->ctx.ir_base[begin];
10499
0
            insn->op3 = end;
10500
0
            break;
10501
0
          }
10502
0
        }
10503
        /* fallback to indirect JMP or RETURN */
10504
0
        if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
10505
0
          zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
10506
0
        } else {
10507
0
          zend_jit_vm_enter(jit, jit_IP(jit));
10508
0
        }
10509
0
      } while (0);
10510
10511
0
      bb = &jit->ssa->cfg.blocks[jit->b];
10512
0
      if (bb->successors_count > 0) {
10513
0
        int succ;
10514
0
        ir_ref ref;
10515
10516
0
        ZEND_ASSERT(bb->successors_count == 1);
10517
0
        succ = bb->successors[0];
10518
        /* Add a fake control edge from UNREACHABLE/RETURN to the following ENTRY */
10519
0
        ref = jit->ctx.insns_count - 1;
10520
0
        ZEND_ASSERT(jit->ctx.ir_base[ref].op == IR_UNREACHABLE
10521
0
          || jit->ctx.ir_base[ref].op == IR_RETURN
10522
0
          || jit->ctx.ir_base[ref].op == IR_LOOP_END);
10523
0
        ZEND_ASSERT(jit->ssa->cfg.blocks[succ].flags & ZEND_BB_ENTRY);
10524
0
        ref = zend_jit_continue_entry(jit, ref, jit->ssa->cfg.blocks[succ].start);
10525
0
        if (func || (opline->opcode == ZEND_DO_UCALL)) {
10526
0
          _zend_jit_add_predecessor_ref(jit, succ, jit->b, ref);
10527
0
          jit->b = -1;
10528
0
        } else {
10529
0
          user_path = ref;
10530
0
        }
10531
0
      }
10532
0
    }
10533
0
  }
10534
10535
0
  if ((!func || func->type == ZEND_INTERNAL_FUNCTION)
10536
0
   && (opline->opcode != ZEND_DO_UCALL)) {
10537
0
    if (!func && (opline->opcode != ZEND_DO_ICALL)) {
10538
0
      ir_IF_FALSE(if_user);
10539
0
    }
10540
10541
    // JIT: EG(current_execute_data) = execute_data;
10542
0
    ir_STORE(jit_EG(current_execute_data), rx);
10543
10544
0
    bool may_have_observer = ZEND_OBSERVER_ENABLED && (!func || (func->common.fn_flags & (ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_GENERATOR)) == 0);
10545
0
    if (may_have_observer) {
10546
0
      ir_ref observer_handler;
10547
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)));
10548
0
      jit_observer_fcall_begin(jit, rx, observer_handler);
10549
0
      jit_observer_fcall_is_unobserved_end(jit, &unobserved_data);
10550
0
    }
10551
10552
    // JIT: ZVAL_NULL(EX_VAR(opline->result.var));
10553
0
    ir_ref res_addr = IR_UNUSED, func_ptr;
10554
10555
0
    if (RETURN_VALUE_USED(opline)) {
10556
0
      res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10557
0
    } else {
10558
      /* CPU stack allocated temporary zval */
10559
0
      ir_ref ptr;
10560
10561
0
      if (!jit->ctx.fixed_call_stack_size) {
10562
        // JIT: alloca(sizeof(void*));
10563
0
        ptr = ir_ALLOCA(ir_CONST_ADDR(sizeof(zval)));
10564
0
      } else {
10565
#ifdef _WIN64
10566
        ptr = ir_HARD_COPY_A(jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS));
10567
#else
10568
0
        ptr = ir_HARD_COPY_A(ir_RLOAD_A(IR_REG_SP));
10569
0
#endif
10570
0
      }
10571
0
      res_addr = ZEND_ADDR_REF_ZVAL(ptr);
10572
0
    }
10573
10574
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
10575
10576
0
    zend_jit_reset_last_valid_opline(jit);
10577
10578
    // JIT: (zend_execute_internal ? zend_execute_internal : fbc->internal_function.handler)(call, ret);
10579
0
    ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
10580
0
    if (zend_execute_internal) {
10581
0
      ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_execute_internal), rx, res_ref);
10582
0
    } else {
10583
0
      if (func) {
10584
0
        func_ptr = ir_CONST_FC_FUNC(func->internal_function.handler);
10585
0
      } else {
10586
0
        func_ptr = ir_LOAD_A(ir_ADD_OFFSET(func_ref, offsetof(zend_internal_function, handler)));
10587
#if defined(IR_TARGET_X86)
10588
        func_ptr = ir_CAST_FC_FUNC(func_ptr);
10589
#endif
10590
0
      }
10591
0
      ir_CALL_2(IR_VOID, func_ptr, rx, res_ref);
10592
0
    }
10593
10594
0
    if (may_have_observer) {
10595
0
      jit_observer_fcall_end(jit, rx, res_ref);
10596
0
    }
10597
10598
    /* When zend_interrupt_function is set, it gets called while
10599
     * the frame is still on top. This is less efficient than
10600
     * doing it later once it's popped off. There is code further
10601
     * down that handles when there isn't an interrupt function.
10602
     */
10603
0
    if (zend_interrupt_function) {
10604
      // JIT: if (EG(vm_interrupt)) zend_fcall_interrupt(execute_data);
10605
0
      ir_ref if_interrupt = ir_IF(ir_LOAD_U8(jit_EG(vm_interrupt)));
10606
0
      ir_IF_TRUE_cold(if_interrupt);
10607
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_fcall_interrupt), rx);
10608
0
      ir_MERGE_WITH_EMPTY_FALSE(if_interrupt);
10609
0
    }
10610
10611
    // JIT: EG(current_execute_data) = execute_data;
10612
0
    ir_STORE(jit_EG(current_execute_data), jit_FP(jit));
10613
10614
    // JIT: zend_vm_stack_free_args(call);
10615
0
    if (func && !unknown_num_args) {
10616
0
      for (i = 0; i < call_num_args; i++ ) {
10617
0
        if (zend_jit_needs_arg_dtor(func, i, call_info)) {
10618
0
          uint32_t offset = EX_NUM_TO_VAR(i);
10619
0
          zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, offset);
10620
10621
0
          jit_ZVAL_PTR_DTOR(jit, var_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, false,
10622
0
                opline);
10623
0
        }
10624
0
      }
10625
0
    } else {
10626
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_vm_stack_free_args_helper), rx);
10627
0
    }
10628
10629
0
    if (may_have_extra_named_params) {
10630
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS))
10631
0
      ir_ref if_has_named = ir_IF(ir_AND_U8(
10632
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 3)),
10633
0
        ir_CONST_U8(ZEND_CALL_HAS_EXTRA_NAMED_PARAMS >> 24)));
10634
0
      ir_IF_TRUE_cold(if_has_named);
10635
10636
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_free_extra_named_params),
10637
0
        ir_LOAD_A(jit_CALL(rx, extra_named_params)));
10638
10639
0
      ir_MERGE_WITH_EMPTY_FALSE(if_has_named);
10640
0
    }
10641
10642
0
    if (opline->opcode == ZEND_DO_FCALL) {
10643
      // TODO: optimize ???
10644
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS))
10645
0
      ir_ref if_release_this = ir_IF(ir_AND_U8(
10646
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 2)),
10647
0
        ir_CONST_U8(ZEND_CALL_RELEASE_THIS >> 16)));
10648
0
      ir_IF_TRUE_cold(if_release_this);
10649
10650
      // JIT: OBJ_RELEASE(Z_OBJ(RX->This));
10651
0
      jit_OBJ_RELEASE(jit, ir_LOAD_A(jit_CALL(rx, This.value.obj)));
10652
10653
0
      ir_MERGE_WITH_EMPTY_FALSE(if_release_this);
10654
0
    }
10655
10656
10657
0
    ir_ref allocated_path = IR_UNUSED;
10658
10659
0
    if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
10660
0
        !JIT_G(current_frame) ||
10661
0
        !JIT_G(current_frame)->call ||
10662
0
        !TRACE_FRAME_IS_NESTED(JIT_G(current_frame)->call) ||
10663
0
        prev_opline->opcode == ZEND_SEND_UNPACK ||
10664
0
        prev_opline->opcode == ZEND_SEND_ARRAY ||
10665
0
      prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS) {
10666
10667
      // JIT: zend_vm_stack_free_call_frame(call);
10668
      // JIT: if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_ALLOCATED))
10669
0
      ir_ref if_allocated = ir_IF(ir_AND_U8(
10670
0
        ir_LOAD_U8(ir_ADD_OFFSET(rx, offsetof(zend_execute_data, This.u1.type_info) + 2)),
10671
0
        ir_CONST_U8(ZEND_CALL_ALLOCATED >> 16)));
10672
0
      ir_IF_TRUE_cold(if_allocated);
10673
10674
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_free_call_frame), rx);
10675
10676
0
      allocated_path = ir_END();
10677
0
      ir_IF_FALSE(if_allocated);
10678
0
    }
10679
10680
0
    ir_STORE(jit_EG(vm_stack_top), rx);
10681
10682
0
    if (allocated_path) {
10683
0
      ir_MERGE_WITH(allocated_path);
10684
0
    }
10685
10686
0
    if (!RETURN_VALUE_USED(opline)) {
10687
0
      zend_class_entry *ce;
10688
0
      bool ce_is_instanceof;
10689
0
      uint32_t func_info = call_info ?
10690
0
        zend_get_func_info(call_info, ssa, &ce, &ce_is_instanceof) :
10691
0
        (MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN);
10692
10693
      /* If an exception is thrown, the return_value may stay at the
10694
       * original value of null. */
10695
0
      func_info |= MAY_BE_NULL;
10696
10697
0
      if (func_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
10698
0
        ir_ref sp;
10699
0
        if (!jit->ctx.fixed_call_stack_size) {
10700
0
          sp = ir_RLOAD_A(IR_REG_SP);
10701
0
        } else {
10702
#ifdef _WIN64
10703
          sp = jit_ADD_OFFSET(jit, ir_RLOAD_A(IR_REG_SP), IR_SHADOW_ARGS);
10704
#else
10705
0
          sp = ir_RLOAD_A(IR_REG_SP);
10706
0
#endif
10707
0
        }
10708
0
        res_addr = ZEND_ADDR_REF_ZVAL(sp);
10709
0
        jit_ZVAL_PTR_DTOR(jit, res_addr, func_info, true, opline);
10710
0
      }
10711
0
      if (!jit->ctx.fixed_call_stack_size) {
10712
        // JIT: revert alloca
10713
0
        ir_AFREE(ir_CONST_ADDR(sizeof(zval)));
10714
0
      }
10715
0
    }
10716
10717
    // JIT: if (UNEXPECTED(EG(exception) != NULL)) {
10718
0
    ir_GUARD_NOT(ir_LOAD_A(jit_EG_exception(jit)),
10719
0
      jit_STUB_ADDR(jit, jit_stub_icall_throw));
10720
10721
    /* If there isn't a zend_interrupt_function, the timeout is
10722
     * handled here because it's more efficient.
10723
     */
10724
0
    if (!zend_interrupt_function) {
10725
      // TODO: Can we avoid checking for interrupts after each call ???
10726
0
      if (trace && jit->last_valid_opline != opline) {
10727
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline + 1, ZEND_JIT_EXIT_TO_VM);
10728
10729
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10730
0
        if (!exit_addr) {
10731
0
          return 0;
10732
0
        }
10733
0
      } else {
10734
0
        exit_addr = NULL;
10735
0
      }
10736
10737
0
      zend_jit_check_timeout(jit, opline + 1, exit_addr);
10738
0
    }
10739
10740
0
    if ((!trace || !func) && opline->opcode != ZEND_DO_ICALL) {
10741
0
      jit_LOAD_IP_ADDR(jit, opline + 1);
10742
0
    } else if (trace
10743
0
     && trace->op == ZEND_JIT_TRACE_END
10744
0
     && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
10745
0
      jit_LOAD_IP_ADDR(jit, opline + 1);
10746
0
    }
10747
0
  }
10748
10749
0
  if (user_path) {
10750
0
    ir_MERGE_WITH(user_path);
10751
0
  }
10752
10753
0
  return 1;
10754
0
}
10755
10756
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)
10757
0
{
10758
0
  ir_ref if_skip_constructor = jit_IF_ex(jit, jit_CMP_IP(jit, IR_NE, opline), next_block);
10759
10760
0
  ir_IF_FALSE(if_skip_constructor);
10761
10762
0
  if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
10763
0
    if (!zend_jit_tail_handler(jit, opline)) {
10764
0
      return 0;
10765
0
    }
10766
0
  } else {
10767
0
    if (!zend_jit_do_fcall(jit, opline, op_array, ssa, call_level, next_block, NULL)) {
10768
0
      return 0;
10769
0
    }
10770
0
  }
10771
10772
  /* override predecessors of the next block */
10773
0
  ZEND_ASSERT(jit->ssa->cfg.blocks[next_block].predecessors_count == 1);
10774
0
  if (!jit->ctx.control) {
10775
0
    ZEND_ASSERT(jit->bb_edges[jit->bb_predecessors[next_block]]);
10776
0
    ir_IF_TRUE(if_skip_constructor);
10777
0
    ir_MERGE_2(jit->bb_edges[jit->bb_predecessors[next_block]], ir_END());
10778
0
    jit->bb_edges[jit->bb_predecessors[next_block]] = ir_END();
10779
0
  } else {
10780
0
    ZEND_ASSERT(!jit->bb_edges[jit->bb_predecessors[next_block]]);
10781
    /* merge current control path with the true branch of constructor skip condition */
10782
0
    ir_MERGE_WITH_EMPTY_TRUE(if_skip_constructor);
10783
0
    jit->bb_edges[jit->bb_predecessors[next_block]] = ir_END();
10784
10785
0
    jit->b = -1;
10786
0
  }
10787
10788
0
  return 1;
10789
0
}
10790
10791
static int zend_jit_verify_arg_type(zend_jit_ctx *jit, const zend_op *opline, zend_arg_info *arg_info, bool check_exception)
10792
0
{
10793
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10794
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type) & MAY_BE_ANY;
10795
0
  ir_ref ref, fast_path = IR_UNUSED;
10796
10797
0
  ref = jit_ZVAL_ADDR(jit, res_addr);
10798
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
10799
0
   && JIT_G(current_frame)
10800
0
   && JIT_G(current_frame)->prev) {
10801
0
    zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
10802
0
    uint8_t type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var));
10803
10804
0
    if (type != IS_UNKNOWN && (type_mask & (1u << type))) {
10805
0
      return 1;
10806
0
    }
10807
0
  }
10808
10809
0
  if (ZEND_ARG_SEND_MODE(arg_info)) {
10810
0
    if (opline->opcode == ZEND_RECV_INIT) {
10811
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
10812
0
    } else {
10813
0
      ref = jit_Z_PTR_ref(jit, ref);
10814
0
      ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
10815
0
    }
10816
0
  }
10817
10818
0
  if (type_mask != 0) {
10819
0
    if (is_power_of_two(type_mask)) {
10820
0
      uint32_t type_code = concrete_type(type_mask);
10821
0
      ir_ref if_ok = jit_if_Z_TYPE_ref(jit, ref, ir_CONST_U8(type_code));
10822
0
      ir_IF_TRUE(if_ok);
10823
0
      fast_path = ir_END();
10824
0
      ir_IF_FALSE_cold(if_ok);
10825
0
    } else {
10826
0
      ir_ref if_ok = ir_IF(ir_AND_U32(
10827
0
        ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE_ref(jit, ref)),
10828
0
        ir_CONST_U32(type_mask)));
10829
0
      ir_IF_TRUE(if_ok);
10830
0
      fast_path = ir_END();
10831
0
      ir_IF_FALSE_cold(if_ok);
10832
0
    }
10833
0
  }
10834
10835
0
  jit_SET_EX_OPLINE(jit, opline);
10836
0
  ref = ir_CALL_2(IR_BOOL, ir_CONST_FC_FUNC(zend_jit_verify_arg_slow),
10837
0
    ref, ir_CONST_ADDR(arg_info));
10838
10839
0
  if (check_exception) {
10840
0
    ir_GUARD(ref, jit_STUB_ADDR(jit, jit_stub_exception_handler));
10841
0
  }
10842
10843
0
  if (fast_path) {
10844
0
    ir_MERGE_WITH(fast_path);
10845
0
  }
10846
10847
0
  return 1;
10848
0
}
10849
10850
static int zend_jit_recv(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array)
10851
0
{
10852
0
  uint32_t arg_num = opline->op1.num;
10853
0
  zend_arg_info *arg_info = NULL;
10854
10855
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
10856
0
    if (EXPECTED(arg_num <= op_array->num_args)) {
10857
0
      arg_info = &op_array->arg_info[arg_num-1];
10858
0
    } else if (UNEXPECTED(op_array->fn_flags & ZEND_ACC_VARIADIC)) {
10859
0
      arg_info = &op_array->arg_info[op_array->num_args];
10860
0
    }
10861
0
    if (arg_info && !ZEND_TYPE_IS_SET(arg_info->type)) {
10862
0
      arg_info = NULL;
10863
0
    }
10864
0
  }
10865
10866
0
  if (arg_info || (opline+1)->opcode != ZEND_RECV) {
10867
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
10868
0
      if (!JIT_G(current_frame) ||
10869
0
          TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) < 0 ||
10870
0
          arg_num > TRACE_FRAME_NUM_ARGS(JIT_G(current_frame))) {
10871
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
10872
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
10873
10874
0
        if (!exit_addr) {
10875
0
          return 0;
10876
0
        }
10877
0
        ir_GUARD(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)),
10878
0
          ir_CONST_ADDR(exit_addr));
10879
0
      }
10880
0
    } else {
10881
0
      ir_ref if_ok =ir_IF(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)));
10882
0
      ir_IF_FALSE_cold(if_ok);
10883
10884
0
      jit_SET_EX_OPLINE(jit, opline);
10885
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_missing_arg_error), jit_FP(jit));
10886
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
10887
0
      ir_IF_TRUE(if_ok);
10888
0
    }
10889
0
  }
10890
10891
0
  if (arg_info) {
10892
0
    if (!zend_jit_verify_arg_type(jit, opline, arg_info, true)) {
10893
0
      return 0;
10894
0
    }
10895
0
  }
10896
10897
0
  return 1;
10898
0
}
10899
10900
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)
10901
0
{
10902
0
  uint32_t arg_num = opline->op1.num;
10903
0
  zval *zv = RT_CONSTANT(opline, opline->op2);
10904
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
10905
0
  ir_ref ref, if_fail, skip_path = IR_UNUSED;
10906
10907
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
10908
0
   && JIT_G(current_frame)
10909
0
   && TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) >= 0) {
10910
0
    if (arg_num > TRACE_FRAME_NUM_ARGS(JIT_G(current_frame))) {
10911
0
      jit_ZVAL_COPY_CONST(jit,
10912
0
        res_addr,
10913
0
        -1, -1,
10914
0
        zv, true);
10915
0
    }
10916
0
  } else {
10917
0
    if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
10918
0
        (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
10919
0
      ir_ref if_skip = ir_IF(ir_GE(ir_LOAD_U32(jit_EX(This.u2.num_args)), ir_CONST_U32(arg_num)));
10920
0
      ir_IF_TRUE(if_skip);
10921
0
      skip_path = ir_END();
10922
0
      ir_IF_FALSE(if_skip);
10923
0
    }
10924
0
    jit_ZVAL_COPY_CONST(jit,
10925
0
      res_addr,
10926
0
      -1, -1,
10927
0
      zv, true);
10928
0
  }
10929
10930
0
  if (Z_CONSTANT_P(zv)) {
10931
0
    jit_SET_EX_OPLINE(jit, opline);
10932
0
    ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zval_update_constant_ex),
10933
0
      jit_ZVAL_ADDR(jit, res_addr),
10934
0
      ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(jit_EX(func)), offsetof(zend_op_array, scope))));
10935
10936
0
    if_fail = ir_IF(ref);
10937
0
    ir_IF_TRUE_cold(if_fail);
10938
0
    jit_ZVAL_PTR_DTOR(jit, res_addr, MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, true, opline);
10939
0
    ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
10940
0
    ir_IF_FALSE(if_fail);
10941
0
  }
10942
10943
0
  if (skip_path) {
10944
0
    ir_MERGE_WITH(skip_path);
10945
0
  }
10946
10947
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
10948
0
    do {
10949
0
      zend_arg_info *arg_info;
10950
10951
0
      if (arg_num <= op_array->num_args) {
10952
0
        arg_info = &op_array->arg_info[arg_num-1];
10953
0
      } else if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
10954
0
        arg_info = &op_array->arg_info[op_array->num_args];
10955
0
      } else {
10956
0
        break;
10957
0
      }
10958
0
      if (!ZEND_TYPE_IS_SET(arg_info->type)) {
10959
0
        break;
10960
0
      }
10961
0
      if (!zend_jit_verify_arg_type(jit, opline, arg_info, may_throw)) {
10962
0
        return 0;
10963
0
      }
10964
0
    } while (0);
10965
0
  }
10966
10967
0
  return 1;
10968
0
}
10969
10970
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)
10971
0
{
10972
0
  zend_arg_info *arg_info = &op_array->arg_info[-1];
10973
0
  ZEND_ASSERT(ZEND_TYPE_IS_SET(arg_info->type));
10974
0
  zend_jit_addr op1_addr = OP1_ADDR();
10975
0
  bool needs_slow_check = true;
10976
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type) & MAY_BE_ANY;
10977
0
  ir_ref fast_path = IR_UNUSED;
10978
10979
0
  if (type_mask != 0) {
10980
0
    if (((op1_info & MAY_BE_ANY) & type_mask) == 0) {
10981
      /* pass */
10982
0
    } else if (((op1_info & MAY_BE_ANY) | type_mask) == type_mask) {
10983
0
      needs_slow_check = false;
10984
0
    } else if (is_power_of_two(type_mask)) {
10985
0
      uint32_t type_code = concrete_type(type_mask);
10986
0
      ir_ref if_ok = jit_if_Z_TYPE(jit, op1_addr, type_code);
10987
10988
0
      ir_IF_TRUE(if_ok);
10989
0
      fast_path = ir_END();
10990
0
      ir_IF_FALSE_cold(if_ok);
10991
0
    } else {
10992
0
      ir_ref if_ok = ir_IF(ir_AND_U32(
10993
0
        ir_SHL_U32(ir_CONST_U32(1), jit_Z_TYPE(jit, op1_addr)),
10994
0
        ir_CONST_U32(type_mask)));
10995
10996
0
      ir_IF_TRUE(if_ok);
10997
0
      fast_path = ir_END();
10998
0
      ir_IF_FALSE_cold(if_ok);
10999
0
    }
11000
0
  }
11001
0
  if (needs_slow_check) {
11002
0
    ir_ref ref;
11003
11004
0
    jit_SET_EX_OPLINE(jit, opline);
11005
0
    ref = jit_ZVAL_ADDR(jit, op1_addr);
11006
0
    if (op1_info & MAY_BE_UNDEF) {
11007
0
      ref = zend_jit_zval_check_undef(jit, ref, opline->op1.var, NULL, true);
11008
0
    }
11009
11010
0
    ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_verify_return_slow),
11011
0
      ref,
11012
0
      ir_LOAD_A(jit_EX(func)),
11013
0
      ir_CONST_ADDR(arg_info));
11014
11015
0
    zend_jit_check_exception(jit);
11016
11017
0
    if (fast_path) {
11018
0
      ir_MERGE_WITH(fast_path);
11019
0
    }
11020
0
  }
11021
11022
0
  return true;
11023
0
}
11024
11025
static int zend_jit_leave_frame(zend_jit_ctx *jit)
11026
0
{
11027
  // JIT: EG(current_execute_data) = EX(prev_execute_data);
11028
0
  ir_STORE(jit_EG(current_execute_data), ir_LOAD_A(jit_EX(prev_execute_data)));
11029
0
  return 1;
11030
0
}
11031
11032
static int zend_jit_free_cvs(zend_jit_ctx *jit)
11033
0
{
11034
  // JIT: EG(current_execute_data) = EX(prev_execute_data);
11035
0
  ir_STORE(jit_EG(current_execute_data), ir_LOAD_A(jit_EX(prev_execute_data)));
11036
11037
  // JIT: zend_free_compiled_variables(execute_data);
11038
0
  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_free_compiled_variables), jit_FP(jit));
11039
0
  return 1;
11040
0
}
11041
11042
static int zend_jit_free_cv(zend_jit_ctx *jit, uint32_t info, uint32_t var)
11043
0
{
11044
0
  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11045
0
    zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
11046
11047
0
    jit_ZVAL_PTR_DTOR(jit, var_addr, info, true, NULL);
11048
0
  }
11049
0
  return 1;
11050
0
}
11051
11052
static int zend_jit_free_op(zend_jit_ctx *jit, const zend_op *opline, uint32_t info, uint32_t var_offset)
11053
0
{
11054
0
  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11055
0
    jit_ZVAL_PTR_DTOR(jit, ZEND_ADDR_MEM_ZVAL(ZREG_FP, var_offset), info, false, opline);
11056
0
  }
11057
0
  return 1;
11058
0
}
11059
11060
static int zend_jit_leave_func(zend_jit_ctx         *jit,
11061
                               const zend_op_array  *op_array,
11062
                               const zend_op        *opline,
11063
                               uint32_t              op1_info,
11064
                               bool             left_frame,
11065
                               zend_jit_trace_rec   *trace,
11066
                               zend_jit_trace_info  *trace_info,
11067
                               int                   indirect_var_access,
11068
                               int                   may_throw)
11069
0
{
11070
0
  bool may_be_top_frame =
11071
0
    JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11072
0
    !JIT_G(current_frame) ||
11073
0
    !TRACE_FRAME_IS_NESTED(JIT_G(current_frame));
11074
0
  bool may_need_call_helper =
11075
0
    indirect_var_access || /* may have symbol table */
11076
0
    !op_array->function_name || /* may have symbol table */
11077
0
    may_be_top_frame ||
11078
0
    (op_array->fn_flags & ZEND_ACC_VARIADIC) || /* may have extra named args */
11079
0
    JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11080
0
    !JIT_G(current_frame) ||
11081
0
    TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) == -1 || /* unknown number of args */
11082
0
    (uint32_t)TRACE_FRAME_NUM_ARGS(JIT_G(current_frame)) > op_array->num_args; /* extra args */
11083
0
  bool may_need_release_this =
11084
0
    !(op_array->fn_flags & ZEND_ACC_CLOSURE) &&
11085
0
    op_array->scope &&
11086
0
    !(op_array->fn_flags & ZEND_ACC_STATIC) &&
11087
0
    (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11088
0
     !JIT_G(current_frame) ||
11089
0
     !TRACE_FRAME_NO_NEED_RELEASE_THIS(JIT_G(current_frame)));
11090
0
  ir_ref call_info = IR_UNUSED, ref, cold_path = IR_UNUSED;
11091
11092
0
  if (may_need_call_helper) {
11093
0
    if (!left_frame) {
11094
0
      left_frame = true;
11095
0
        if (!zend_jit_leave_frame(jit)) {
11096
0
        return 0;
11097
0
        }
11098
0
    }
11099
    /* ZEND_CALL_FAKE_CLOSURE handled on slow path to eliminate check for ZEND_CALL_CLOSURE on fast path */
11100
0
    call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
11101
0
    ref = ir_AND_U32(call_info,
11102
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));
11103
0
    if (trace && trace->op != ZEND_JIT_TRACE_END) {
11104
0
      ir_ref if_slow = ir_IF(ref);
11105
11106
0
      ir_IF_TRUE_cold(if_slow);
11107
0
      if (!GCC_GLOBAL_REGS) {
11108
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_leave_func_helper), jit_FP(jit), jit_IP(jit));
11109
0
      } else {
11110
0
        ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_leave_func_helper));
11111
0
      }
11112
11113
0
      if (may_be_top_frame) {
11114
        // TODO: try to avoid this check ???
11115
0
        if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
11116
#if 0
11117
          /* this check should be handled by the following OPLINE guard */
11118
          | cmp IP, zend_jit_halt_op
11119
          | je ->trace_halt
11120
#endif
11121
0
        } else if (GCC_GLOBAL_REGS) {
11122
0
          ir_GUARD(jit_IP(jit), jit_STUB_ADDR(jit, jit_stub_trace_halt));
11123
0
        } else {
11124
0
          ir_GUARD(ir_NE(ref, ir_CONST_ADDR(ZEND_VM_ENTER_BIT)), jit_STUB_ADDR(jit, jit_stub_trace_halt));
11125
0
          jit_STORE_IP(jit, ref);
11126
0
        }
11127
0
      }
11128
11129
0
      if (!GCC_GLOBAL_REGS) {
11130
        // execute_data = EG(current_execute_data)
11131
0
        jit_STORE_FP(jit, ir_LOAD_A(jit_EG(current_execute_data)));
11132
0
      }
11133
0
      cold_path = ir_END();
11134
0
      ir_IF_FALSE(if_slow);
11135
0
    } else {
11136
0
      ir_GUARD_NOT(ref, jit_STUB_ADDR(jit, jit_stub_leave_function_handler));
11137
0
    }
11138
0
  }
11139
11140
0
  if ((op_array->fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) == ZEND_ACC_CLOSURE) {
11141
0
    if (!left_frame) {
11142
0
      left_frame = true;
11143
0
        if (!zend_jit_leave_frame(jit)) {
11144
0
        return 0;
11145
0
        }
11146
0
    }
11147
    // JIT: OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
11148
0
    jit_OBJ_RELEASE(jit, ir_ADD_OFFSET(ir_LOAD_A(jit_EX(func)), -sizeof(zend_object)));
11149
0
  } else if (may_need_release_this) {
11150
0
    ir_ref if_release, fast_path = IR_UNUSED;
11151
11152
0
    if (!left_frame) {
11153
0
      left_frame = true;
11154
0
        if (!zend_jit_leave_frame(jit)) {
11155
0
        return 0;
11156
0
        }
11157
0
    }
11158
0
    if (!JIT_G(current_frame) || !TRACE_FRAME_ALWAYS_RELEASE_THIS(JIT_G(current_frame))) {
11159
      // JIT: if (call_info & ZEND_CALL_RELEASE_THIS)
11160
0
      if (!call_info) {
11161
0
        call_info = ir_LOAD_U32(jit_EX(This.u1.type_info));
11162
0
      }
11163
0
      if_release = ir_IF(ir_AND_U32(call_info, ir_CONST_U32(ZEND_CALL_RELEASE_THIS)));
11164
0
      ir_IF_FALSE(if_release);
11165
0
      fast_path = ir_END();
11166
0
      ir_IF_TRUE(if_release);
11167
0
    }
11168
    // JIT: OBJ_RELEASE(execute_data->This))
11169
0
    jit_OBJ_RELEASE(jit, ir_LOAD_A(jit_EX(This.value.obj)));
11170
0
    if (fast_path) {
11171
0
      ir_MERGE_WITH(fast_path);
11172
0
    }
11173
    // TODO: avoid EG(excption) check for $this->foo() calls
11174
0
    may_throw = 1;
11175
0
  }
11176
11177
  // JIT: EG(vm_stack_top) = (zval*)execute_data
11178
0
  ir_STORE(jit_EG(vm_stack_top), jit_FP(jit));
11179
11180
  // JITL execute_data = EX(prev_execute_data)
11181
0
  jit_STORE_FP(jit, ir_LOAD_A(jit_EX(prev_execute_data)));
11182
11183
0
  if (!left_frame) {
11184
    // JIT: EG(current_execute_data) = execute_data
11185
0
    ir_STORE(jit_EG(current_execute_data), jit_FP(jit));
11186
0
  }
11187
11188
0
  if (trace) {
11189
0
    if (trace->op != ZEND_JIT_TRACE_END
11190
0
     && (JIT_G(current_frame) && !TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame)))) {
11191
0
      zend_jit_reset_last_valid_opline(jit);
11192
0
    } else {
11193
      /* We add extra RLOAD and RSTORE to make fusion for persistent register
11194
       *     mov (%FP), %IP
11195
       *     add $0x1c, %IP
11196
       * The naive (commented) code leads to extra register allocation and move.
11197
       *     mov (%FP), %tmp
11198
       *     add $0x1c, %tmp
11199
       *     mov %tmp, %FP
11200
       */
11201
#if 0
11202
      jit_STORE_IP(jit, ir_ADD_OFFSET(ir_LOAD_A(jit_EX(opline)), sizeof(zend_op)));
11203
#else
11204
0
      jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
11205
0
      jit_STORE_IP(jit, ir_ADD_OFFSET(jit_IP(jit), sizeof(zend_op)));
11206
0
#endif
11207
0
    }
11208
11209
0
    if (cold_path) {
11210
0
      ir_MERGE_WITH(cold_path);
11211
0
    }
11212
11213
0
    if (trace->op == ZEND_JIT_TRACE_BACK
11214
0
     && (!JIT_G(current_frame) || TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame)))) {
11215
0
      const zend_op *next_opline = trace->opline;
11216
11217
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
11218
0
       && (op1_info & MAY_BE_RC1)
11219
0
       && (op1_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
11220
        /* exception might be thrown during destruction of unused return value */
11221
        // JIT: if (EG(exception))
11222
0
        ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11223
0
      }
11224
0
      do {
11225
0
        trace++;
11226
0
      } while (trace->op == ZEND_JIT_TRACE_INIT_CALL);
11227
0
      ZEND_ASSERT(trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END);
11228
0
      next_opline = trace->opline;
11229
0
      ZEND_ASSERT(next_opline != NULL);
11230
11231
0
      if (trace->op == ZEND_JIT_TRACE_END
11232
0
       && trace->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
11233
0
        trace_info->flags |= ZEND_JIT_TRACE_LOOP;
11234
11235
0
        ir_ref if_eq = ir_IF(jit_CMP_IP(jit, IR_EQ, next_opline));
11236
11237
0
        ir_IF_TRUE(if_eq);
11238
0
        ZEND_ASSERT(jit->trace_loop_ref);
11239
0
        ZEND_ASSERT(jit->ctx.ir_base[jit->trace_loop_ref].op2 == IR_UNUSED);
11240
0
        ir_MERGE_SET_OP(jit->trace_loop_ref, 2, ir_END());
11241
0
        ir_IF_FALSE(if_eq);
11242
11243
#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE
11244
        ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
11245
#else
11246
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_escape));
11247
0
#endif
11248
0
      } else {
11249
0
        ir_GUARD(jit_CMP_IP(jit, IR_EQ, next_opline), jit_STUB_ADDR(jit, jit_stub_trace_escape));
11250
0
      }
11251
11252
0
      zend_jit_set_last_valid_opline(jit, trace->opline);
11253
11254
0
      return 1;
11255
0
    } else if (may_throw ||
11256
0
        (((opline->op1_type & (IS_VAR|IS_TMP_VAR))
11257
0
          && (op1_info & MAY_BE_RC1)
11258
0
          && (op1_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)))
11259
0
         && (!JIT_G(current_frame) || TRACE_FRAME_IS_RETURN_VALUE_UNUSED(JIT_G(current_frame))))) {
11260
      // JIT: if (EG(exception))
11261
0
      ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11262
0
    }
11263
11264
0
    return 1;
11265
0
  } else {
11266
    // JIT: if (EG(exception))
11267
0
    ir_GUARD_NOT(ir_LOAD_A(jit_EG(exception)), jit_STUB_ADDR(jit, jit_stub_leave_throw));
11268
    // JIT: opline = EX(opline) + 1
11269
0
    jit_STORE_IP(jit, ir_LOAD_A(jit_EX(opline)));
11270
0
    jit_STORE_IP(jit, ir_ADD_OFFSET(jit_IP(jit), sizeof(zend_op)));
11271
0
  }
11272
11273
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
11274
0
    zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
11275
0
  } else {
11276
0
    zend_jit_vm_leave(jit, jit_IP(jit));
11277
0
  }
11278
11279
0
  jit->b = -1;
11280
11281
0
  return 1;
11282
0
}
11283
11284
static void zend_jit_common_return(zend_jit_ctx *jit)
11285
0
{
11286
0
  ZEND_ASSERT(jit->return_inputs);
11287
0
  ir_MERGE_list(jit->return_inputs);
11288
0
}
11289
11290
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)
11291
0
{
11292
0
  zend_jit_addr ret_addr;
11293
0
  int8_t return_value_used = -1;
11294
0
  ir_ref return_value = IR_UNUSED, ref, refcount, if_return_value_used = IR_UNUSED;
11295
11296
0
  ZEND_ASSERT(op_array->type != ZEND_EVAL_CODE && op_array->function_name);
11297
0
  ZEND_ASSERT(!(op1_info & MAY_BE_UNDEF));
11298
11299
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
11300
0
    jit->return_inputs = IR_UNUSED;
11301
0
    if (JIT_G(current_frame)) {
11302
0
      if (TRACE_FRAME_IS_RETURN_VALUE_USED(JIT_G(current_frame))) {
11303
0
        return_value_used = 1;
11304
0
      } else if (TRACE_FRAME_IS_RETURN_VALUE_UNUSED(JIT_G(current_frame))) {
11305
0
        return_value_used = 0;
11306
0
      } else {
11307
0
        return_value_used = -1;
11308
0
      }
11309
0
    }
11310
0
  }
11311
11312
0
  if (ZEND_OBSERVER_ENABLED) {
11313
0
    if (Z_MODE(op1_addr) == IS_REG) {
11314
0
      zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
11315
11316
0
      if (!zend_jit_spill_store_inv(jit, op1_addr, dst, op1_info)) {
11317
0
        return 0;
11318
0
      }
11319
0
      op1_addr = dst;
11320
0
    }
11321
0
    jit_observer_fcall_end(jit, jit_FP(jit), jit_ZVAL_ADDR(jit, op1_addr));
11322
0
  }
11323
11324
  // JIT: if (!EX(return_value))
11325
0
  return_value = ir_LOAD_A(jit_EX(return_value));
11326
0
  ret_addr = ZEND_ADDR_REF_ZVAL(return_value);
11327
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) &&
11328
0
      (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11329
0
    if (return_value_used == -1) {
11330
0
      if_return_value_used = ir_IF(return_value);
11331
0
      ir_IF_FALSE_cold(if_return_value_used);
11332
0
    }
11333
0
    if (return_value_used != 1) {
11334
0
      if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-(MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11335
0
        ir_ref if_refcounted = jit_if_REFCOUNTED(jit, op1_addr);
11336
0
        ir_IF_FALSE(if_refcounted);
11337
0
        ir_END_list(jit->return_inputs);
11338
0
        ir_IF_TRUE(if_refcounted);
11339
0
      }
11340
0
      ref = jit_Z_PTR(jit, op1_addr);
11341
0
      refcount = jit_GC_DELREF(jit, ref);
11342
11343
0
      if (RC_MAY_BE_1(op1_info)) {
11344
0
        if (RC_MAY_BE_N(op1_info)) {
11345
0
          ir_ref if_non_zero = ir_IF(refcount);
11346
0
          ir_IF_TRUE(if_non_zero);
11347
0
          ir_END_list(jit->return_inputs);
11348
0
          ir_IF_FALSE(if_non_zero);
11349
0
        }
11350
0
        jit_ZVAL_DTOR(jit, ref, op1_info, opline);
11351
0
      }
11352
0
      if (return_value_used == -1) {
11353
0
        ir_END_list(jit->return_inputs);
11354
0
      }
11355
0
    }
11356
0
  } else if (return_value_used == -1) {
11357
0
    if_return_value_used = ir_IF(return_value);
11358
0
    ir_IF_FALSE_cold(if_return_value_used);
11359
0
    ir_END_list(jit->return_inputs);
11360
0
  }
11361
11362
0
  if (if_return_value_used) {
11363
0
    ir_IF_TRUE(if_return_value_used);
11364
0
  }
11365
11366
0
  if (return_value_used == 0) {
11367
0
    if (jit->return_inputs) {
11368
0
      ZEND_ASSERT(JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE);
11369
0
      ir_END_list(jit->return_inputs);
11370
0
      ir_MERGE_list(jit->return_inputs);
11371
0
      jit->return_inputs = IR_UNUSED;
11372
0
    }
11373
0
    return 1;
11374
0
  }
11375
11376
0
  if (opline->op1_type == IS_CONST) {
11377
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
11378
11379
0
    jit_ZVAL_COPY_CONST(jit, ret_addr, MAY_BE_ANY, MAY_BE_ANY, zv, true);
11380
0
  } else if (opline->op1_type == IS_TMP_VAR) {
11381
0
    jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11382
0
  } else if (opline->op1_type == IS_CV) {
11383
0
    if (op1_info & MAY_BE_REF) {
11384
0
      ref = jit_ZVAL_ADDR(jit, op1_addr);
11385
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
11386
0
      op1_addr = ZEND_ADDR_REF_ZVAL(ref);
11387
0
    }
11388
11389
0
    if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
11390
0
      if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE ||
11391
0
          (op1_info & (MAY_BE_REF|MAY_BE_OBJECT)) ||
11392
0
          !op_array->function_name) {
11393
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, true);
11394
0
      } else if (return_value_used != 1) {
11395
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11396
        // JIT: if (EXPECTED(!(EX_CALL_INFO() & ZEND_CALL_CODE))) ZVAL_NULL(retval_ptr);
11397
0
        jit_set_Z_TYPE_INFO(jit, op1_addr, IS_NULL);
11398
0
      } else {
11399
0
        jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11400
0
      }
11401
0
    } else {
11402
0
      jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11403
0
    }
11404
0
  } else {
11405
0
    if (op1_info & MAY_BE_REF) {
11406
0
      ir_ref if_ref, ref2, if_non_zero;
11407
0
      zend_jit_addr ref_addr;
11408
11409
0
      if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
11410
0
      ir_IF_TRUE_cold(if_ref);
11411
11412
      // JIT: zend_refcounted *ref = Z_COUNTED_P(retval_ptr)
11413
0
      ref = jit_Z_PTR(jit, op1_addr);
11414
11415
      // JIT: ZVAL_COPY_VALUE(return_value, &ref->value)
11416
0
      ref2 = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
11417
0
      ref_addr = ZEND_ADDR_REF_ZVAL(ref2);
11418
0
      jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, ref_addr, op1_info, false);
11419
0
      ref2 = jit_GC_DELREF(jit, ref);
11420
0
      if_non_zero = ir_IF(ref2);
11421
0
      ir_IF_TRUE(if_non_zero);
11422
11423
      // JIT: if (IS_REFCOUNTED())
11424
0
      ir_ref if_refcounted = jit_if_REFCOUNTED(jit, ret_addr);
11425
0
      ir_IF_FALSE(if_refcounted);
11426
0
      ir_END_list(jit->return_inputs);
11427
0
      ir_IF_TRUE(if_refcounted);
11428
11429
      // JIT: ADDREF
11430
0
      ref2 = jit_Z_PTR(jit, ret_addr);
11431
0
      jit_GC_ADDREF(jit, ref2);
11432
0
      ir_END_list(jit->return_inputs);
11433
11434
0
      ir_IF_FALSE(if_non_zero);
11435
11436
0
      jit_EFREE(jit, ref, sizeof(zend_reference), op_array, opline);
11437
0
      ir_END_list(jit->return_inputs);
11438
11439
0
      ir_IF_FALSE(if_ref);
11440
0
    }
11441
0
    jit_ZVAL_COPY(jit, ret_addr, MAY_BE_ANY, op1_addr, op1_info, false);
11442
0
  }
11443
11444
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
11445
0
    if (jit->return_inputs) {
11446
0
      ir_END_list(jit->return_inputs);
11447
0
      ir_MERGE_list(jit->return_inputs);
11448
0
      jit->return_inputs = IR_UNUSED;
11449
0
    }
11450
0
  } else {
11451
0
    ir_END_list(jit->return_inputs);
11452
0
    jit->b = -1;
11453
0
  }
11454
11455
0
  return 1;
11456
0
}
11457
11458
static int zend_jit_bind_global(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
11459
0
{
11460
0
  zend_jit_addr op1_addr = OP1_ADDR();
11461
0
  zend_string *varname = Z_STR_P(RT_CONSTANT(opline, opline->op2));
11462
0
  ir_ref cache_slot_ref, idx_ref, num_used_ref, bucket_ref, ref, ref2;
11463
0
  ir_ref if_fit, if_reference, if_same_key, fast_path;
11464
0
  ir_ref slow_inputs = IR_UNUSED, end_inputs = IR_UNUSED;
11465
11466
  // JIT: idx = (uintptr_t)CACHED_PTR(opline->extended_value) - 1;
11467
0
  cache_slot_ref = ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), opline->extended_value);
11468
0
  idx_ref = ir_SUB_A(ir_LOAD_A(cache_slot_ref), ir_CONST_ADDR(1));
11469
11470
  // JIT: if (EXPECTED(idx < EG(symbol_table).nNumUsed * sizeof(Bucket)))
11471
0
  num_used_ref = ir_MUL_U32(ir_LOAD_U32(jit_EG(symbol_table.nNumUsed)),
11472
0
    ir_CONST_U32(sizeof(Bucket)));
11473
0
  if (sizeof(void*) == 8) {
11474
0
    num_used_ref = ir_ZEXT_A(num_used_ref);
11475
0
  }
11476
0
  if_fit = ir_IF(ir_ULT(idx_ref, num_used_ref));
11477
0
  ir_IF_FALSE_cold(if_fit);
11478
0
  ir_END_list(slow_inputs);
11479
0
  ir_IF_TRUE(if_fit);
11480
11481
  // JIT: Bucket *p = (Bucket*)((char*)EG(symbol_table).arData + idx);
11482
0
  bucket_ref = ir_ADD_A(ir_LOAD_A(jit_EG(symbol_table.arData)), idx_ref);
11483
0
  if_reference = jit_if_Z_TYPE_ref(jit, bucket_ref, ir_CONST_U8(IS_REFERENCE));
11484
0
  ir_IF_FALSE_cold(if_reference);
11485
0
  ir_END_list(slow_inputs);
11486
0
  ir_IF_TRUE(if_reference);
11487
11488
  // JIT: (EXPECTED(p->key == varname))
11489
0
  if_same_key = ir_IF(ir_EQ(ir_LOAD_A(ir_ADD_OFFSET(bucket_ref, offsetof(Bucket, key))), ir_CONST_ADDR(varname)));
11490
0
  ir_IF_FALSE_cold(if_same_key);
11491
0
  ir_END_list(slow_inputs);
11492
0
  ir_IF_TRUE(if_same_key);
11493
11494
  // JIT: GC_ADDREF(Z_PTR(p->val))
11495
0
  ref = jit_Z_PTR_ref(jit, bucket_ref);
11496
0
  jit_GC_ADDREF(jit, ref);
11497
11498
0
  fast_path = ir_END();
11499
0
  ir_MERGE_list(slow_inputs);
11500
11501
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_global_helper),
11502
0
    ir_CONST_ADDR(varname),
11503
0
    cache_slot_ref);
11504
11505
0
  ir_MERGE_WITH(fast_path);
11506
0
  ref = ir_PHI_2(IR_ADDR, ref2, ref);
11507
11508
0
  if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11509
0
    ir_ref if_refcounted = IR_UNUSED, refcount, if_non_zero, if_may_not_leak;
11510
11511
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11512
      // JIT: if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr)))
11513
0
      if_refcounted = jit_if_REFCOUNTED(jit, op1_addr);
11514
0
      ir_IF_TRUE_cold(if_refcounted);
11515
0
    }
11516
11517
    // JIT:zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
11518
0
    ref2 = jit_Z_PTR(jit, op1_addr);
11519
11520
    // JIT: ZVAL_REF(variable_ptr, ref)
11521
0
    jit_set_Z_PTR(jit, op1_addr, ref);
11522
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
11523
11524
    // JIT: if (GC_DELREF(garbage) == 0)
11525
0
    refcount = jit_GC_DELREF(jit, ref2);
11526
0
    if_non_zero = ir_IF(refcount);
11527
0
    if (!(op1_info & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT))) {
11528
0
      ir_IF_TRUE(if_non_zero);
11529
0
      ir_END_list(end_inputs);
11530
0
    }
11531
0
    ir_IF_FALSE(if_non_zero);
11532
11533
0
    jit_ZVAL_DTOR(jit, ref2, op1_info, opline);
11534
0
    if (op1_info & (MAY_BE_REF|MAY_BE_ARRAY|MAY_BE_OBJECT)) {
11535
0
      ir_END_list(end_inputs);
11536
0
      ir_IF_TRUE(if_non_zero);
11537
11538
      // JIT: GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr)
11539
0
      if_may_not_leak = jit_if_GC_MAY_NOT_LEAK(jit, ref2);
11540
0
      ir_IF_TRUE(if_may_not_leak);
11541
0
      ir_END_list(end_inputs);
11542
0
      ir_IF_FALSE(if_may_not_leak);
11543
0
      if (opline) {
11544
0
        jit_SET_EX_OPLINE(jit, opline);
11545
0
      }
11546
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(gc_possible_root), ref2);
11547
0
    }
11548
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11549
0
      ir_END_list(end_inputs);
11550
0
      ir_IF_FALSE(if_refcounted);
11551
0
    }
11552
0
  }
11553
11554
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
11555
    // JIT: ZVAL_REF(variable_ptr, ref)
11556
0
    jit_set_Z_PTR(jit, op1_addr, ref);
11557
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_REFERENCE_EX);
11558
0
  }
11559
11560
0
  if (end_inputs) {
11561
0
    ir_END_list(end_inputs);
11562
0
    ir_MERGE_list(end_inputs);
11563
0
  }
11564
11565
0
  return 1;
11566
0
}
11567
11568
static int zend_jit_free(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, int may_throw)
11569
0
{
11570
0
  zend_jit_addr op1_addr = OP1_ADDR();
11571
11572
0
  if (op1_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
11573
0
    if (may_throw) {
11574
0
      jit_SET_EX_OPLINE(jit, opline);
11575
0
    }
11576
0
    if (opline->opcode == ZEND_FE_FREE && (op1_info & (MAY_BE_OBJECT|MAY_BE_REF))) {
11577
0
      ir_ref ref, if_array, if_exists, end_inputs = IR_UNUSED;
11578
11579
0
      if (op1_info & MAY_BE_ARRAY) {
11580
0
        if_array = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
11581
0
        ir_IF_TRUE(if_array);
11582
0
        ir_END_list(end_inputs);
11583
0
        ir_IF_FALSE(if_array);
11584
0
      }
11585
0
      ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_iter_idx)));
11586
0
      if_exists = ir_IF(ir_EQ(ref, ir_CONST_U32(-1)));
11587
0
      ir_IF_TRUE(if_exists);
11588
0
      ir_END_list(end_inputs);
11589
0
      ir_IF_FALSE(if_exists);
11590
11591
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_hash_iterator_del), ref);
11592
11593
0
      ir_END_list(end_inputs);
11594
0
      ir_MERGE_list(end_inputs);
11595
0
    }
11596
11597
0
    jit_ZVAL_PTR_DTOR(jit, op1_addr, op1_info, false, opline);
11598
11599
0
    if (may_throw) {
11600
0
      zend_jit_check_exception(jit);
11601
0
    }
11602
0
  }
11603
11604
0
  return 1;
11605
0
}
11606
11607
static int zend_jit_echo(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
11608
0
{
11609
0
  if (opline->op1_type == IS_CONST) {
11610
0
    zval *zv;
11611
0
    size_t len;
11612
11613
0
    zv = RT_CONSTANT(opline, opline->op1);
11614
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11615
0
    len = Z_STRLEN_P(zv);
11616
11617
0
    if (len > 0) {
11618
0
      const char *str = Z_STRVAL_P(zv);
11619
11620
0
      jit_SET_EX_OPLINE(jit, opline);
11621
0
      ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_write),
11622
0
        ir_CONST_ADDR(str), ir_CONST_ADDR(len));
11623
11624
0
      zend_jit_check_exception(jit);
11625
0
    }
11626
0
  } else {
11627
0
    zend_jit_addr op1_addr = OP1_ADDR();
11628
0
    ir_ref ref;
11629
11630
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11631
11632
0
    jit_SET_EX_OPLINE(jit, opline);
11633
11634
0
    ref = jit_Z_PTR(jit, op1_addr);
11635
0
    ir_CALL_2(IR_VOID, ir_CONST_FUNC(zend_write),
11636
0
      ir_ADD_OFFSET(ref, offsetof(zend_string, val)),
11637
0
      ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_string, len))));
11638
11639
0
    if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
11640
0
      jit_ZVAL_PTR_DTOR(jit, op1_addr, op1_info, false, opline);
11641
0
    }
11642
11643
0
    zend_jit_check_exception(jit);
11644
0
  }
11645
0
  return 1;
11646
0
}
11647
11648
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)
11649
0
{
11650
0
  if (opline->op1_type == IS_CONST) {
11651
0
    zval *zv;
11652
0
    size_t len;
11653
11654
0
    zv = RT_CONSTANT(opline, opline->op1);
11655
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11656
0
    len = Z_STRLEN_P(zv);
11657
11658
0
    jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(len));
11659
0
    if (Z_MODE(res_addr) != IS_REG) {
11660
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11661
0
    } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11662
0
      return 0;
11663
0
    }
11664
0
  } else {
11665
0
    ir_ref ref;
11666
11667
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11668
11669
0
    ref = jit_Z_PTR(jit, op1_addr);
11670
0
    ref = ir_LOAD_L(ir_ADD_OFFSET(ref, offsetof(zend_string, len)));
11671
0
    jit_set_Z_LVAL(jit, res_addr, ref);
11672
11673
0
    if (Z_MODE(res_addr) == IS_REG) {
11674
0
      if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11675
0
        return 0;
11676
0
      }
11677
0
    } else {
11678
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11679
0
    }
11680
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
11681
0
  }
11682
0
  return 1;
11683
0
}
11684
11685
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)
11686
0
{
11687
0
  if (opline->op1_type == IS_CONST) {
11688
0
    zval *zv;
11689
0
    zend_long count;
11690
11691
0
    zv = RT_CONSTANT(opline, opline->op1);
11692
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
11693
0
    count = zend_hash_num_elements(Z_ARRVAL_P(zv));
11694
11695
0
    jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(count));
11696
0
    if (Z_MODE(res_addr) != IS_REG) {
11697
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11698
0
    } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11699
0
      return 0;
11700
0
    }
11701
0
  } else {
11702
0
    ir_ref ref;
11703
11704
0
    ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_ARRAY);
11705
    // Note: See the implementation of ZEND_COUNT in Zend/zend_vm_def.h - arrays do not contain IS_UNDEF starting in php 8.1+.
11706
11707
0
    ref = jit_Z_PTR(jit, op1_addr);
11708
0
    if (sizeof(void*) == 8) {
11709
0
      ref = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(HashTable, nNumOfElements)));
11710
0
      ref = ir_ZEXT_L(ref);
11711
0
    } else {
11712
0
      ref = ir_LOAD_L(ir_ADD_OFFSET(ref, offsetof(HashTable, nNumOfElements)));
11713
0
    }
11714
0
    jit_set_Z_LVAL(jit, res_addr, ref);
11715
11716
0
    if (Z_MODE(res_addr) == IS_REG) {
11717
0
      if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, MAY_BE_LONG)) {
11718
0
        return 0;
11719
0
      }
11720
0
    } else {
11721
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
11722
0
    }
11723
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
11724
0
  }
11725
11726
0
  if (may_throw) {
11727
0
    zend_jit_check_exception(jit);
11728
0
  }
11729
0
  return 1;
11730
0
}
11731
11732
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)
11733
0
{
11734
0
  HashTable *ht = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
11735
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
11736
0
  ir_ref ref;
11737
11738
0
  ZEND_ASSERT(opline->op1_type != IS_VAR && opline->op1_type != IS_TMP_VAR);
11739
0
  ZEND_ASSERT((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == MAY_BE_STRING);
11740
11741
  // JIT: result = zend_hash_find_ex(ht, Z_STR_P(op1), OP1_TYPE == IS_CONST);
11742
0
  if (opline->op1_type != IS_CONST) {
11743
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find),
11744
0
      ir_CONST_ADDR(ht),
11745
0
      jit_Z_PTR(jit, op1_addr));
11746
0
  } else {
11747
0
    zend_string *str = Z_STR_P(RT_CONSTANT(opline, opline->op1));
11748
11749
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash),
11750
0
      ir_CONST_ADDR(ht), ir_CONST_ADDR(str));
11751
0
  }
11752
11753
0
  if (exit_addr) {
11754
0
    if (smart_branch_opcode == ZEND_JMPZ) {
11755
0
      ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
11756
0
    } else {
11757
0
      ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
11758
0
    }
11759
0
  } else if (smart_branch_opcode) {
11760
0
    zend_basic_block *bb;
11761
11762
0
    ZEND_ASSERT(jit->b >= 0);
11763
0
    bb = &jit->ssa->cfg.blocks[jit->b];
11764
0
    ZEND_ASSERT(bb->successors_count == 2);
11765
0
    ref = jit_IF_ex(jit, ref,
11766
0
      (smart_branch_opcode == ZEND_JMPZ) ? target_label2 : target_label);
11767
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
11768
0
    _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ref);
11769
0
    jit->b = -1;
11770
0
  } else {
11771
0
    jit_set_Z_TYPE_INFO_ex(jit, res_addr,
11772
0
      ir_ADD_U32(ir_ZEXT_U32(ir_NE(ref, IR_NULL)), ir_CONST_U32(IS_FALSE)));
11773
0
  }
11774
11775
0
  return 1;
11776
0
}
11777
11778
static int zend_jit_rope(zend_jit_ctx *jit, const zend_op *opline, uint32_t op2_info)
11779
0
{
11780
0
  uint32_t offset;
11781
11782
0
  offset = (opline->opcode == ZEND_ROPE_INIT) ?
11783
0
    opline->result.var :
11784
0
    opline->op1.var + opline->extended_value * sizeof(zend_string*);
11785
11786
0
  if (opline->op2_type == IS_CONST) {
11787
0
    zval *zv = RT_CONSTANT(opline, opline->op2);
11788
0
    zend_string *str;
11789
11790
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
11791
0
    str = Z_STR_P(zv);
11792
11793
0
    ir_STORE(ir_ADD_OFFSET(jit_FP(jit), offset), ir_CONST_ADDR(str));
11794
0
  } else {
11795
0
    zend_jit_addr op2_addr = OP2_ADDR();
11796
0
    ir_ref ref;
11797
11798
0
    ZEND_ASSERT((op2_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_STRING);
11799
11800
0
    ref = jit_Z_PTR(jit, op2_addr);
11801
0
    ir_STORE(ir_ADD_OFFSET(jit_FP(jit), offset), ref);
11802
0
    if (opline->op2_type == IS_CV) {
11803
0
      ir_ref if_refcounted, long_path;
11804
11805
0
      if_refcounted = jit_if_REFCOUNTED(jit, op2_addr);
11806
0
      ir_IF_TRUE(if_refcounted);
11807
0
      jit_GC_ADDREF(jit, ref);
11808
0
      long_path = ir_END();
11809
11810
0
      ir_IF_FALSE(if_refcounted);
11811
0
      ir_MERGE_WITH(long_path);
11812
0
    }
11813
0
  }
11814
11815
0
  if (opline->opcode == ZEND_ROPE_END) {
11816
0
    zend_jit_addr res_addr = RES_ADDR();
11817
0
    ir_ref ref;
11818
11819
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_rope_end),
11820
0
      ir_ADD_OFFSET(jit_FP(jit), opline->op1.var),
11821
0
      ir_CONST_U32(opline->extended_value));
11822
11823
0
    jit_set_Z_PTR(jit, res_addr, ref);
11824
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING_EX);
11825
0
  }
11826
11827
0
  return 1;
11828
0
}
11829
11830
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)
11831
0
{
11832
0
  ir_ref if_type, val;
11833
11834
0
  if (res_info == MAY_BE_LONG) {
11835
0
    if_type = ir_IF(ir_EQ(type, ir_CONST_U32(IS_LONG)));
11836
0
    ir_IF_TRUE(if_type);
11837
0
    val = jit_ZVAL_ADDR(jit, val_addr);
11838
0
    ir_END_PHI_list(*values, val);
11839
0
    ir_IF_FALSE(if_type);
11840
0
    val = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
11841
0
    ir_END_PHI_list(*values, val);
11842
0
  } else if (res_info == MAY_BE_DOUBLE) {
11843
0
    if_type = ir_IF(ir_EQ(type, ir_CONST_U32(IS_DOUBLE)));
11844
0
    ir_IF_TRUE(if_type);
11845
0
    val = jit_ZVAL_ADDR(jit, val_addr);
11846
0
    ir_END_PHI_list(*values, val);
11847
0
    ir_IF_FALSE(if_type);
11848
0
    val = ir_ADD_OFFSET(jit_Z_PTR(jit, val_addr), offsetof(zend_reference, val));
11849
0
    ir_END_PHI_list(*values, val);
11850
0
  } else {
11851
0
    ZEND_UNREACHABLE();
11852
0
  }
11853
0
  return 1;
11854
0
}
11855
11856
static int zend_jit_zval_copy_deref(zend_jit_ctx *jit, zend_jit_addr res_addr, zend_jit_addr val_addr, ir_ref type)
11857
0
{
11858
0
  ir_ref if_refcounted, if_reference, if_refcounted2, ptr, val2, ptr2, type2;
11859
0
  ir_refs *merge_inputs, *types, *ptrs;
11860
#if SIZEOF_ZEND_LONG == 4
11861
  ir_ref val = jit_ZVAL_ADDR(jit, val_addr);
11862
  ir_refs *values; /* we need this only for zval.w2 copy */
11863
#endif
11864
11865
0
  ir_refs_init(merge_inputs, 4);
11866
0
  ir_refs_init(types, 4);
11867
0
  ir_refs_init(ptrs, 4);
11868
#if SIZEOF_ZEND_LONG == 4
11869
  ir_refs_init(values, 4);
11870
#endif
11871
11872
  // JIT: ptr = Z_PTR_P(val);
11873
0
  ptr = jit_Z_PTR(jit, val_addr);
11874
11875
  // JIT: if (Z_OPT_REFCOUNTED_P(val)) {
11876
0
  if_refcounted = ir_IF(ir_AND_U32(type, ir_CONST_U32(Z_TYPE_FLAGS_MASK)));
11877
0
  ir_IF_FALSE_cold(if_refcounted);
11878
0
  ir_refs_add(merge_inputs, ir_END());
11879
0
  ir_refs_add(types, type);
11880
0
  ir_refs_add(ptrs, ptr);
11881
#if SIZEOF_ZEND_LONG == 4
11882
  ir_refs_add(values, val);
11883
#endif
11884
11885
0
  ir_IF_TRUE(if_refcounted);
11886
11887
  // JIT: if (UNEXPECTED(Z_OPT_ISREF_P(val))) {
11888
0
  if_reference = ir_IF(ir_EQ(type, ir_CONST_U32(IS_REFERENCE_EX)));
11889
//  if_reference = ir_IF(ir_EQ(ir_TRUNC_U8(type), ir_CONST_U8(IS_REFERENCE))); // TODO: fix IR to avoid need for extra register ???
11890
0
  ir_IF_TRUE(if_reference);
11891
11892
  // JIT: val = Z_REFVAL_P(val);
11893
0
  val2 = ir_ADD_OFFSET(ptr, offsetof(zend_reference, val));
11894
0
  type2 = jit_Z_TYPE_INFO_ref(jit, val2);
11895
0
  ptr2 = jit_Z_PTR_ref(jit, val2);
11896
11897
  // JIT: if (Z_OPT_REFCOUNTED_P(val)) {
11898
0
  if_refcounted2 = ir_IF(ir_AND_U32(type2, ir_CONST_U32(Z_TYPE_FLAGS_MASK)));
11899
0
  ir_IF_FALSE_cold(if_refcounted2);
11900
0
  ir_refs_add(merge_inputs, ir_END());
11901
0
  ir_refs_add(types, type2);
11902
0
  ir_refs_add(ptrs, ptr2);
11903
#if SIZEOF_ZEND_LONG == 4
11904
  ir_refs_add(values, val2);
11905
#endif
11906
11907
0
  ir_IF_TRUE(if_refcounted2);
11908
0
  ir_MERGE_WITH_EMPTY_FALSE(if_reference);
11909
0
  type = ir_PHI_2(IR_U32, type2, type);
11910
0
  ptr = ir_PHI_2(IR_ADDR, ptr2, ptr);
11911
#if SIZEOF_ZEND_LONG == 4
11912
  val = ir_PHI_2(IR_ADDR, val2, val);
11913
#endif
11914
11915
  // JIT: Z_ADDREF_P(val);
11916
0
  jit_GC_ADDREF(jit, ptr);
11917
0
  ir_refs_add(merge_inputs, ir_END());
11918
0
  ir_refs_add(types, type);
11919
0
  ir_refs_add(ptrs, ptr);
11920
#if SIZEOF_ZEND_LONG == 4
11921
  ir_refs_add(values, val);
11922
#endif
11923
11924
0
  ir_MERGE_N(merge_inputs->count, merge_inputs->refs);
11925
0
  type = ir_PHI_N(IR_U32, types->count, types->refs);
11926
0
  ptr = ir_PHI_N(IR_ADDR, ptrs->count, ptrs->refs);
11927
#if SIZEOF_ZEND_LONG == 4
11928
  val = ir_PHI_N(IR_ADDR, values->count, values->refs);
11929
  val_addr = ZEND_ADDR_REF_ZVAL(val);
11930
#endif
11931
11932
  // JIT: Z_PTR_P(res) = ptr;
11933
0
  jit_set_Z_PTR(jit, res_addr, ptr);
11934
#if SIZEOF_ZEND_LONG == 4
11935
  jit_set_Z_W2(jit, res_addr, jit_Z_W2(jit, val_addr));
11936
#endif
11937
0
  jit_set_Z_TYPE_INFO_ex(jit, res_addr, type);
11938
11939
0
  return 1;
11940
0
}
11941
11942
static int zend_jit_fetch_dimension_address_inner(zend_jit_ctx  *jit,
11943
                                                  const zend_op *opline,
11944
                                                  uint32_t       type,
11945
                                                  uint32_t       op1_info,
11946
                                                  uint32_t       op2_info,
11947
                                                  zend_jit_addr  op2_addr,
11948
                                                  zend_ssa_range *op2_range,
11949
                                                  uint8_t        dim_type,
11950
                                                  const void    *found_exit_addr,
11951
                                                  const void    *not_found_exit_addr,
11952
                                                  const void    *exit_addr,
11953
                                                  bool           result_type_guard,
11954
                                                  ir_ref         ht_ref,
11955
                                                  ir_refs       *found_inputs,
11956
                                                  ir_refs       *found_vals,
11957
                                                  ir_ref        *end_inputs,
11958
                                                  ir_ref        *not_found_inputs)
11959
0
{
11960
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
11961
0
  ir_ref ref = IR_UNUSED, cond, if_found;
11962
0
  ir_ref if_type = IS_UNUSED;
11963
0
  ir_refs *test_zval_inputs, *test_zval_values;
11964
11965
0
  ir_refs_init(test_zval_inputs, 4);
11966
0
  ir_refs_init(test_zval_values, 4);
11967
11968
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
11969
0
   && type == BP_VAR_R
11970
0
   && !exit_addr) {
11971
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
11972
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
11973
0
    if (!exit_addr) {
11974
0
      return 0;
11975
0
    }
11976
0
  }
11977
11978
0
  if (op2_info & MAY_BE_LONG) {
11979
0
    bool op2_loaded = false;
11980
0
    bool packed_loaded = false;
11981
0
    bool bad_packed_key = false;
11982
0
    ir_ref if_packed = IS_UNDEF;
11983
0
    ir_ref h = IR_UNUSED;
11984
0
    ir_ref idx_not_found_inputs = IR_UNUSED;
11985
11986
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_LONG)) {
11987
      // JIT: if (EXPECTED(Z_TYPE_P(dim) == IS_LONG))
11988
0
      if_type = jit_if_Z_TYPE(jit, op2_addr, IS_LONG);
11989
0
      ir_IF_TRUE(if_type);
11990
0
    }
11991
0
    if (op1_info & MAY_BE_PACKED_GUARD) {
11992
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_PACKED_GUARD);
11993
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
11994
11995
0
      if (!exit_addr) {
11996
0
        return 0;
11997
0
      }
11998
0
      cond = ir_AND_U32(
11999
0
        ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
12000
0
        ir_CONST_U32(HASH_FLAG_PACKED));
12001
0
      if (op1_info & MAY_BE_ARRAY_PACKED) {
12002
0
        ir_GUARD(cond, ir_CONST_ADDR(exit_addr));
12003
0
      } else {
12004
0
        ir_GUARD_NOT(cond, ir_CONST_ADDR(exit_addr));
12005
0
      }
12006
0
    }
12007
0
    if (type == BP_VAR_W) {
12008
      // JIT: hval = Z_LVAL_P(dim);
12009
0
      h = jit_Z_LVAL(jit, op2_addr);
12010
0
      op2_loaded = true;
12011
0
    }
12012
0
    if (op1_info & MAY_BE_ARRAY_PACKED) {
12013
0
      zend_long val = -1;
12014
12015
0
      if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
12016
0
        val = Z_LVAL_P(Z_ZV(op2_addr));
12017
0
        if (val >= 0 && val < HT_MAX_SIZE) {
12018
0
          packed_loaded = true;
12019
0
        } else {
12020
0
          bad_packed_key = true;
12021
0
        }
12022
0
        h = ir_CONST_LONG(val);
12023
0
      } else {
12024
0
        if (!op2_loaded) {
12025
          // JIT: hval = Z_LVAL_P(dim);
12026
0
          h = jit_Z_LVAL(jit, op2_addr);
12027
0
          op2_loaded = true;
12028
0
        }
12029
0
        packed_loaded = true;
12030
0
      }
12031
12032
0
      if (dim_type == IS_UNDEF && type == BP_VAR_W && packed_loaded) {
12033
        /* don't generate "fast" code for packed array */
12034
0
        packed_loaded = false;
12035
0
      }
12036
12037
0
      if (packed_loaded) {
12038
        // JIT: ZEND_HASH_INDEX_FIND(ht, hval, retval, num_undef);
12039
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12040
0
          if_packed = ir_IF(
12041
0
            ir_AND_U32(
12042
0
              ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
12043
0
              ir_CONST_U32(HASH_FLAG_PACKED)));
12044
0
          ir_IF_TRUE(if_packed);
12045
0
        }
12046
        // JIT: if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed))
12047
0
        ref = ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed)));
12048
0
#if SIZEOF_ZEND_LONG == 8
12049
0
        if ((Z_MODE(op2_addr) == IS_CONST_ZVAL && val >= 0 && val <= UINT32_MAX)
12050
0
         || (op2_range && op2_range->min >= 0 && op2_range->max <= UINT32_MAX)) {
12051
          /* comapre only the lower 32-bits to allow load fusion on x86_64 */
12052
0
          cond = ir_ULT(ir_TRUNC_U32(h), ref);
12053
0
        } else {
12054
0
          cond = ir_ULT(h, ir_ZEXT_L(ref));
12055
0
        }
12056
#else
12057
        cond = ir_ULT(h, ref);
12058
#endif
12059
0
        if (type == BP_JIT_IS) {
12060
0
          if (not_found_exit_addr) {
12061
0
            ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12062
0
          } else {
12063
0
            ir_ref if_fit = ir_IF(cond);
12064
0
            ir_IF_FALSE(if_fit);
12065
0
            ir_END_list(*end_inputs);
12066
0
            ir_IF_TRUE(if_fit);
12067
0
          }
12068
0
        } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12069
0
          ir_GUARD(cond, ir_CONST_ADDR(exit_addr));
12070
0
        } else if (type == BP_VAR_IS && not_found_exit_addr) {
12071
0
          ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12072
0
        } else if (type == BP_VAR_RW && not_found_exit_addr) {
12073
0
          ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12074
0
        } else if (type == BP_VAR_IS && result_type_guard) {
12075
0
          ir_ref if_fit = ir_IF(cond);
12076
0
          ir_IF_FALSE(if_fit);
12077
0
          ir_END_list(*not_found_inputs);
12078
0
          ir_IF_TRUE(if_fit);
12079
0
        } else {
12080
0
          ir_ref if_fit = ir_IF(cond);
12081
0
          ir_IF_FALSE(if_fit);
12082
0
          ir_END_list(idx_not_found_inputs);
12083
0
          ir_IF_TRUE(if_fit);
12084
0
        }
12085
        // JIT: _ret = &_ht->arPacked[h];
12086
0
        ref = ir_MUL_L(h, ir_CONST_LONG(sizeof(zval)));
12087
0
        ref = ir_BITCAST_A(ref);
12088
0
        ref = ir_ADD_A(ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arPacked))), ref);
12089
0
        if (type == BP_JIT_IS) {
12090
0
          ir_refs_add(test_zval_values, ref);
12091
0
          ir_refs_add(test_zval_inputs, ir_END());
12092
0
        }
12093
0
      }
12094
0
    }
12095
0
    switch (type) {
12096
0
      case BP_JIT_IS:
12097
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12098
0
          if (if_packed) {
12099
0
            ir_IF_FALSE(if_packed);
12100
0
            if_packed = IR_UNUSED;
12101
0
          }
12102
0
          if (!op2_loaded) {
12103
            // JIT: hval = Z_LVAL_P(dim);
12104
0
            h = jit_Z_LVAL(jit, op2_addr);
12105
0
            op2_loaded = true;
12106
0
          }
12107
0
          if (packed_loaded) {
12108
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(_zend_hash_index_find), ht_ref, h);
12109
0
          } else {
12110
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_find), ht_ref, h);
12111
0
          }
12112
0
          if (not_found_exit_addr) {
12113
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12114
0
          } else {
12115
0
            if_found = ir_IF(ref);
12116
0
            ir_IF_FALSE(if_found);
12117
0
            ir_END_list(*end_inputs);
12118
0
            ir_IF_TRUE(if_found);
12119
0
          }
12120
0
          ir_refs_add(test_zval_values, ref);
12121
0
          ir_refs_add(test_zval_inputs, ir_END());
12122
0
        } else if (!not_found_exit_addr && !packed_loaded) {
12123
0
          ir_END_list(*end_inputs);
12124
0
        }
12125
0
        break;
12126
0
      case BP_VAR_R:
12127
0
      case BP_VAR_IS:
12128
0
      case BP_VAR_UNSET:
12129
0
        if (packed_loaded) {
12130
0
          ir_ref type_ref = jit_Z_TYPE_ref(jit, ref);
12131
12132
0
          if (result_type_guard) {
12133
            /* perform IS_UNDEF check only after result type guard (during deoptimization) */
12134
0
          } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12135
0
            ir_GUARD(type_ref, ir_CONST_ADDR(exit_addr));
12136
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12137
0
            ir_GUARD(type_ref, ir_CONST_ADDR(not_found_exit_addr));
12138
0
          } else {
12139
0
            ir_ref if_def = ir_IF(type_ref);
12140
0
            ir_IF_FALSE(if_def);
12141
0
            ir_END_list(idx_not_found_inputs);
12142
0
            ir_IF_TRUE(if_def);
12143
0
          }
12144
0
          ir_refs_add(found_inputs, ir_END());
12145
0
          ir_refs_add(found_vals, ref);
12146
0
        }
12147
0
        if (op1_info & MAY_BE_ARRAY_NUMERIC_HASH) {
12148
0
          if (if_packed) {
12149
0
            ir_IF_FALSE(if_packed);
12150
0
            if_packed = IR_UNUSED;
12151
0
          }
12152
0
          if (!op2_loaded) {
12153
            // JIT: hval = Z_LVAL_P(dim);
12154
0
            h = jit_Z_LVAL(jit, op2_addr);
12155
0
            op2_loaded = true;
12156
0
          }
12157
0
          if (packed_loaded) {
12158
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(_zend_hash_index_find), ht_ref, h);
12159
0
          } else {
12160
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_find), ht_ref, h);
12161
0
          }
12162
0
          if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12163
0
            ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
12164
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12165
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12166
0
          } else if (type == BP_VAR_IS && result_type_guard) {
12167
0
            if_found = ir_IF(ref);
12168
0
            ir_IF_FALSE(if_found);
12169
0
            ir_END_list(*not_found_inputs);
12170
0
            ir_IF_TRUE(if_found);
12171
0
          } else {
12172
0
            if_found = ir_IF(ref);
12173
0
            ir_IF_FALSE(if_found);
12174
0
            ir_END_list(idx_not_found_inputs);
12175
0
            ir_IF_TRUE(if_found);
12176
0
          }
12177
0
          ir_refs_add(found_inputs, ir_END());
12178
0
          ir_refs_add(found_vals, ref);
12179
0
        } else if (!packed_loaded) {
12180
0
          if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12181
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
12182
0
          } else if (type == BP_VAR_IS && not_found_exit_addr) {
12183
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(not_found_exit_addr));
12184
0
          } else if (type == BP_VAR_IS && result_type_guard) {
12185
0
            ir_END_list(*not_found_inputs);
12186
0
          } else {
12187
0
            ir_END_list(idx_not_found_inputs);
12188
0
          }
12189
0
        }
12190
12191
0
        if (idx_not_found_inputs) {
12192
0
          ir_MERGE_list(idx_not_found_inputs);
12193
0
          switch (type) {
12194
0
            case BP_VAR_R:
12195
0
              ZEND_ASSERT(JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE);
12196
              // JIT: zend_error(E_WARNING,"Undefined array key " ZEND_LONG_FMT, hval);
12197
              // JIT: retval = &EG(uninitialized_zval);
12198
0
              jit_SET_EX_OPLINE(jit, opline);
12199
0
              if (Z_MODE(op2_addr) == IS_REG) {
12200
0
                if (!op2_loaded) {
12201
                  // JIT: hval = Z_LVAL_P(dim);
12202
0
                  h = jit_Z_LVAL(jit, op2_addr);
12203
0
                }
12204
0
                if (GCC_GLOBAL_REGS) {
12205
0
                  ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key_ex), h);
12206
0
                } else {
12207
0
                  ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_long_key_ex), h, jit_FP(jit));
12208
0
                }
12209
0
              } else {
12210
0
                ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_undefined_offset, IR_FASTCALL_FUNC));
12211
0
              }
12212
0
              ir_END_list(*end_inputs);
12213
0
              break;
12214
0
            case BP_VAR_IS:
12215
0
            case BP_VAR_UNSET:
12216
0
              if (!not_found_exit_addr) {
12217
                // JIT: retval = &EG(uninitialized_zval);
12218
0
                jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12219
0
                ir_END_list(*end_inputs);
12220
0
              }
12221
0
              break;
12222
0
            default:
12223
0
              ZEND_UNREACHABLE();
12224
0
          }
12225
0
                }
12226
0
        break;
12227
0
      case BP_VAR_RW:
12228
0
        if (packed_loaded) {
12229
0
          if (not_found_exit_addr) {
12230
0
            ir_refs_add(found_inputs, ir_END());
12231
0
            ir_refs_add(found_vals, ref);
12232
0
          } else {
12233
0
            ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
12234
0
            ir_IF_TRUE(if_def);
12235
0
            ir_refs_add(found_inputs, ir_END());
12236
0
            ir_refs_add(found_vals, ref);
12237
0
            ir_IF_FALSE_cold(if_def);
12238
0
            ir_END_list(idx_not_found_inputs);
12239
0
          }
12240
0
        }
12241
0
        if (!packed_loaded ||
12242
0
            !not_found_exit_addr ||
12243
0
            (op1_info & MAY_BE_ARRAY_NUMERIC_HASH)) {
12244
0
          if (if_packed) {
12245
0
            ir_IF_FALSE(if_packed);
12246
0
            if_packed = IR_UNUSED;
12247
0
            ir_END_list(idx_not_found_inputs);
12248
0
          } else if (!packed_loaded) {
12249
0
            ir_END_list(idx_not_found_inputs);
12250
0
          }
12251
12252
0
          ir_MERGE_list(idx_not_found_inputs);
12253
0
          if (!op2_loaded) {
12254
            // JIT: hval = Z_LVAL_P(dim);
12255
0
            h = jit_Z_LVAL(jit, op2_addr);
12256
0
          }
12257
0
          if (packed_loaded) {
12258
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_index_lookup_rw_no_packed),
12259
0
              ht_ref, h);
12260
0
          } else {
12261
0
            ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_index_lookup_rw), ht_ref, h);
12262
0
          }
12263
0
          if (not_found_exit_addr) {
12264
0
            ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12265
0
          } else {
12266
0
            if_found = ir_IF(ref);
12267
0
            ir_IF_FALSE(if_found);
12268
0
            ir_END_list(*end_inputs);
12269
0
            ir_IF_TRUE(if_found);
12270
0
          }
12271
0
          ir_refs_add(found_inputs, ir_END());
12272
0
          ir_refs_add(found_vals, ref);
12273
0
        }
12274
0
        break;
12275
0
      case BP_VAR_W:
12276
0
        if (packed_loaded) {
12277
0
          ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
12278
0
          ir_IF_TRUE_cold(if_def);
12279
0
          ir_refs_add(found_inputs, ir_END());
12280
0
          ir_refs_add(found_vals, ref);
12281
0
          ir_IF_FALSE(if_def);
12282
0
          ir_END_list(idx_not_found_inputs);
12283
0
        }
12284
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) {
12285
0
          if (if_packed) {
12286
0
            ir_IF_FALSE(if_packed);
12287
0
            if_packed = IR_UNUSED;
12288
0
            ir_END_list(idx_not_found_inputs);
12289
0
          } else if (!packed_loaded) {
12290
0
            ir_END_list(idx_not_found_inputs);
12291
0
          }
12292
0
          ir_MERGE_list(idx_not_found_inputs);
12293
0
          if (!op2_loaded) {
12294
            // JIT: hval = Z_LVAL_P(dim);
12295
0
            h = jit_Z_LVAL(jit, op2_addr);
12296
0
          }
12297
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_index_lookup), ht_ref, h);
12298
0
          ir_refs_add(found_inputs, ir_END());
12299
0
          ir_refs_add(found_vals, ref);
12300
0
        }
12301
0
        break;
12302
0
      default:
12303
0
        ZEND_UNREACHABLE();
12304
0
    }
12305
0
  }
12306
12307
0
  if (op2_info & MAY_BE_STRING) {
12308
0
    ir_ref key;
12309
12310
0
    if (if_type) {
12311
0
      ir_IF_FALSE(if_type);
12312
0
      if_type = IS_UNUSED;
12313
0
    }
12314
12315
0
    if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
12316
      // JIT: if (EXPECTED(Z_TYPE_P(dim) == IS_STRING))
12317
0
      if_type = jit_if_Z_TYPE(jit, op2_addr, IS_STRING);
12318
0
      ir_IF_TRUE(if_type);
12319
0
    }
12320
12321
    // JIT: offset_key = Z_STR_P(dim);
12322
0
    key = jit_Z_PTR(jit, op2_addr);
12323
12324
    // JIT: retval = zend_hash_find(ht, offset_key);
12325
0
    switch (type) {
12326
0
      case BP_JIT_IS:
12327
0
        if (opline->op2_type != IS_CONST) {
12328
0
          ir_ref if_num, end1, ref2;
12329
12330
0
          if_num = ir_IF(
12331
0
            ir_ULE(
12332
0
              ir_LOAD_C(ir_ADD_OFFSET(key, offsetof(zend_string, val))),
12333
0
              ir_CONST_CHAR('9')));
12334
0
          ir_IF_TRUE_cold(if_num);
12335
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_find), ht_ref, key);
12336
0
          end1 = ir_END();
12337
0
          ir_IF_FALSE(if_num);
12338
0
          ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find), ht_ref, key);
12339
0
          ir_MERGE_WITH(end1);
12340
0
          ref = ir_PHI_2(IR_ADDR, ref2, ref);
12341
0
        } else {
12342
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash), ht_ref, key);
12343
0
        }
12344
0
        if (not_found_exit_addr) {
12345
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12346
0
        } else {
12347
0
          if_found = ir_IF(ref);
12348
0
          ir_IF_FALSE(if_found);
12349
0
          ir_END_list(*end_inputs);
12350
0
          ir_IF_TRUE(if_found);
12351
0
        }
12352
0
        ir_refs_add(test_zval_values, ref);
12353
0
        ir_refs_add(test_zval_inputs, ir_END());
12354
0
        break;
12355
0
      case BP_VAR_R:
12356
0
      case BP_VAR_IS:
12357
0
      case BP_VAR_UNSET:
12358
0
        if (opline->op2_type != IS_CONST) {
12359
0
          ir_ref if_num, end1, ref2;
12360
12361
0
          if_num = ir_IF(
12362
0
            ir_ULE(
12363
0
              ir_LOAD_C(ir_ADD_OFFSET(key, offsetof(zend_string, val))),
12364
0
              ir_CONST_CHAR('9')));
12365
0
          ir_IF_TRUE_cold(if_num);
12366
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_find), ht_ref, key);
12367
0
          end1 = ir_END();
12368
0
          ir_IF_FALSE(if_num);
12369
0
          ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find), ht_ref, key);
12370
0
          ir_MERGE_WITH(end1);
12371
0
          ref = ir_PHI_2(IR_ADDR, ref2, ref);
12372
0
        } else {
12373
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_find_known_hash), ht_ref, key);
12374
0
        }
12375
0
        if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && type == BP_VAR_R) {
12376
0
          ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
12377
0
        } else if (type == BP_VAR_IS && not_found_exit_addr) {
12378
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12379
0
        } else if (type == BP_VAR_IS && result_type_guard) {
12380
0
          if_found = ir_IF(ref);
12381
0
          ir_IF_FALSE(if_found);
12382
0
          ir_END_list(*not_found_inputs);
12383
0
          ir_IF_TRUE(if_found);
12384
0
        } else {
12385
0
          if_found = ir_IF(ref);
12386
0
          switch (type) {
12387
0
            case BP_VAR_R:
12388
0
              ir_IF_FALSE_cold(if_found);
12389
              // JIT: zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(offset_key));
12390
0
              jit_SET_EX_OPLINE(jit, opline);
12391
0
              ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_undefined_key, IR_FASTCALL_FUNC));
12392
0
              ir_END_list(*end_inputs);
12393
0
              break;
12394
0
            case BP_VAR_IS:
12395
0
            case BP_VAR_UNSET:
12396
0
              ir_IF_FALSE(if_found);
12397
              // JIT: retval = &EG(uninitialized_zval);
12398
0
              jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12399
0
              ir_END_list(*end_inputs);
12400
0
              break;
12401
0
            default:
12402
0
              ZEND_UNREACHABLE();
12403
0
          }
12404
0
          ir_IF_TRUE(if_found);
12405
0
        }
12406
0
        ir_refs_add(found_inputs, ir_END());
12407
0
        ir_refs_add(found_vals, ref);
12408
0
        break;
12409
0
      case BP_VAR_RW:
12410
0
        if (opline->op2_type != IS_CONST) {
12411
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_lookup_rw), ht_ref, key);
12412
0
        } else {
12413
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_hash_lookup_rw), ht_ref, key);
12414
0
        }
12415
0
        if (not_found_exit_addr) {
12416
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12417
0
        } else {
12418
0
          if_found = ir_IF(ref);
12419
0
          ir_IF_FALSE(if_found);
12420
0
          ir_END_list(*end_inputs);
12421
0
          ir_IF_TRUE(if_found);
12422
0
        }
12423
0
        ir_refs_add(found_inputs, ir_END());
12424
0
        ir_refs_add(found_vals, ref);
12425
0
        break;
12426
0
      case BP_VAR_W:
12427
0
        if (opline->op2_type != IS_CONST) {
12428
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_symtable_lookup_w), ht_ref, key);
12429
0
        } else {
12430
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_lookup), ht_ref, key);
12431
0
        }
12432
0
        ir_refs_add(found_inputs, ir_END());
12433
0
        ir_refs_add(found_vals, ref);
12434
0
        break;
12435
0
      default:
12436
0
        ZEND_UNREACHABLE();
12437
0
    }
12438
0
  }
12439
12440
0
  if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
12441
0
      if (if_type) {
12442
0
      ir_IF_FALSE_cold(if_type);
12443
0
      if_type = IS_UNDEF;
12444
0
    }
12445
0
    if (type != BP_VAR_RW) {
12446
0
      jit_SET_EX_OPLINE(jit, opline);
12447
0
    }
12448
0
    ref = jit_ZVAL_ADDR(jit, op2_addr);
12449
0
    switch (type) {
12450
0
      case BP_VAR_R:
12451
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_r_helper),
12452
0
          ht_ref,
12453
0
          ref,
12454
0
          jit_ZVAL_ADDR(jit, res_addr));
12455
0
        ir_END_list(*end_inputs);
12456
0
        break;
12457
0
      case BP_JIT_IS:
12458
0
        ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_fetch_dim_isset_helper), ht_ref, ref);
12459
0
        if (not_found_exit_addr) {
12460
0
          ir_GUARD(ref, ir_CONST_ADDR(not_found_exit_addr));
12461
0
          ir_refs_add(found_inputs, ir_END());
12462
0
        } else if (found_exit_addr) {
12463
0
          ir_GUARD_NOT(ref, ir_CONST_ADDR(found_exit_addr));
12464
0
          ir_END_list(*end_inputs);
12465
0
        } else {
12466
0
          if_found = ir_IF(ref);
12467
0
          ir_IF_TRUE(if_found);
12468
0
          ir_refs_add(found_inputs, ir_END());
12469
0
          ir_IF_FALSE(if_found);
12470
0
          ir_END_list(*end_inputs);
12471
0
        }
12472
0
        break;
12473
0
      case BP_VAR_IS:
12474
0
      case BP_VAR_UNSET:
12475
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_is_helper),
12476
0
          ht_ref,
12477
0
          ref,
12478
0
          jit_ZVAL_ADDR(jit, res_addr));
12479
0
        ir_END_list(*end_inputs);
12480
0
        break;
12481
0
      case BP_VAR_RW:
12482
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_rw_helper), ht_ref, ref);
12483
0
        if_found = ir_IF(ref);
12484
0
        ir_IF_TRUE(if_found);
12485
0
        ir_refs_add(found_inputs, ir_END());
12486
0
        ir_refs_add(found_vals, ref);
12487
0
        ir_IF_FALSE(if_found);
12488
0
        ir_END_list(*end_inputs);
12489
0
        break;
12490
0
      case BP_VAR_W:
12491
0
        ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref);
12492
0
        if_found = ir_IF(ref);
12493
0
        ir_IF_TRUE(if_found);
12494
0
        ir_refs_add(found_inputs, ir_END());
12495
0
        ir_refs_add(found_vals, ref);
12496
0
        ir_IF_FALSE(if_found);
12497
0
        ir_END_list(*end_inputs);
12498
0
        break;
12499
0
      default:
12500
0
        ZEND_UNREACHABLE();
12501
0
    }
12502
0
  }
12503
12504
0
  if (type == BP_JIT_IS
12505
0
   && !(op2_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
12506
    /* dead code */
12507
0
    ir_END_list(*end_inputs);
12508
0
  } else if (type == BP_JIT_IS
12509
0
   && (op1_info & MAY_BE_ARRAY)
12510
0
   && (op2_info & (MAY_BE_LONG|MAY_BE_STRING))
12511
0
   && test_zval_inputs->count) {
12512
12513
0
    ir_MERGE_N(test_zval_inputs->count, test_zval_inputs->refs);
12514
0
    ref = ir_PHI_N(IR_ADDR, test_zval_values->count, test_zval_values->refs);
12515
12516
0
    if (op1_info & MAY_BE_ARRAY_OF_REF) {
12517
0
      ref = jit_ZVAL_DEREF_ref(jit, ref);
12518
0
    }
12519
0
    cond = ir_GT(jit_Z_TYPE_ref(jit, ref), ir_CONST_U8(IS_NULL));
12520
0
    if (not_found_exit_addr) {
12521
0
      ir_GUARD(cond, ir_CONST_ADDR(not_found_exit_addr));
12522
0
      ir_refs_add(found_inputs, ir_END());
12523
0
    } else if (found_exit_addr) {
12524
0
      ir_GUARD_NOT(cond, ir_CONST_ADDR(found_exit_addr));
12525
0
      ir_END_list(*end_inputs);
12526
0
    } else {
12527
0
      ir_ref if_set = ir_IF(cond);
12528
0
      ir_IF_FALSE(if_set);
12529
0
      ir_END_list(*end_inputs);
12530
0
      ir_IF_TRUE(if_set);
12531
0
      ir_refs_add(found_inputs, ir_END());
12532
0
    }
12533
0
  }
12534
12535
0
  return 1;
12536
0
}
12537
12538
static int zend_jit_fetch_dim_read(zend_jit_ctx       *jit,
12539
                                   const zend_op      *opline,
12540
                                   zend_ssa           *ssa,
12541
                                   const zend_ssa_op  *ssa_op,
12542
                                   uint32_t            op1_info,
12543
                                   zend_jit_addr       op1_addr,
12544
                                   bool           op1_avoid_refcounting,
12545
                                   uint32_t            op2_info,
12546
                                   zend_jit_addr       op2_addr,
12547
                                   zend_ssa_range     *op2_range,
12548
                                   uint32_t            res_info,
12549
                                   zend_jit_addr       res_addr,
12550
                                   uint8_t             dim_type)
12551
0
{
12552
0
  zend_jit_addr orig_op1_addr;
12553
0
  const void *exit_addr = NULL;
12554
0
  const void *not_found_exit_addr = NULL;
12555
0
  bool result_type_guard = false;
12556
0
  bool result_avoid_refcounting = false;
12557
0
  uint32_t may_be_string = (opline->opcode != ZEND_FETCH_LIST_R) ? MAY_BE_STRING : 0;
12558
0
  int may_throw = 0;
12559
0
  ir_ref if_type = IR_UNUSED;
12560
0
  ir_ref end_inputs = IR_UNUSED;
12561
0
  ir_ref not_found_inputs = IR_UNUSED;
12562
12563
0
  orig_op1_addr = OP1_ADDR();
12564
12565
0
  if (opline->opcode != ZEND_FETCH_DIM_IS
12566
0
   && JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
12567
0
   && !has_concrete_type(op1_info)) {
12568
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
12569
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
12570
0
    if (!exit_addr) {
12571
0
      return 0;
12572
0
    }
12573
0
  }
12574
12575
0
  if ((res_info & MAY_BE_GUARD)
12576
0
   && JIT_G(current_frame)
12577
0
   && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
12578
12579
0
    if (!(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF) - (MAY_BE_STRING|MAY_BE_LONG)))) {
12580
0
      result_type_guard = true;
12581
0
      res_info &= ~MAY_BE_GUARD;
12582
0
      ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
12583
0
    }
12584
12585
0
    if ((opline->result_type & (IS_VAR|IS_TMP_VAR))
12586
0
     && (opline->opcode == ZEND_FETCH_LIST_R
12587
0
      || !(opline->op1_type & (IS_VAR|IS_TMP_VAR))
12588
0
      || op1_avoid_refcounting)
12589
0
     && (res_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
12590
0
     && (ssa_op+1)->op1_use == ssa_op->result_def
12591
0
     && !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF) - (MAY_BE_STRING|MAY_BE_LONG)))
12592
0
     && zend_jit_may_avoid_refcounting(opline+1, res_info)) {
12593
0
      result_avoid_refcounting = true;
12594
0
      ssa->var_info[ssa_op->result_def].avoid_refcounting = 1;
12595
0
    }
12596
12597
0
    if (opline->opcode == ZEND_FETCH_DIM_IS
12598
0
     && !(res_info & MAY_BE_NULL)) {
12599
0
      uint32_t flags = 0;
12600
0
      uint32_t old_op1_info = 0;
12601
0
      uint32_t old_info;
12602
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
12603
0
      int32_t exit_point;
12604
12605
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
12606
0
       && !op1_avoid_refcounting) {
12607
0
        flags |= ZEND_JIT_EXIT_FREE_OP1;
12608
0
      }
12609
0
      if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
12610
0
       && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
12611
0
        flags |= ZEND_JIT_EXIT_FREE_OP2;
12612
0
      }
12613
12614
0
      if (op1_avoid_refcounting) {
12615
0
        old_op1_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
12616
0
        SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
12617
0
      }
12618
12619
0
      old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
12620
0
      SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_NULL, 0);
12621
0
      SET_STACK_REG_EX(stack, EX_VAR_TO_NUM(opline->result.var), ZREG_NONE, ZREG_TYPE_ONLY);
12622
0
      exit_point = zend_jit_trace_get_exit_point(opline+1, flags);
12623
0
      SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
12624
0
      not_found_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
12625
0
      if (!not_found_exit_addr) {
12626
0
        return 0;
12627
0
      }
12628
12629
0
      if (op1_avoid_refcounting) {
12630
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_op1_info);
12631
0
      }
12632
0
    }
12633
0
  }
12634
12635
0
  if (op1_info & MAY_BE_REF) {
12636
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
12637
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
12638
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
12639
0
  }
12640
12641
0
  if (op1_info & MAY_BE_ARRAY) {
12642
0
    ir_ref ht_ref, ref;
12643
0
    zend_jit_addr val_addr;
12644
0
    ir_refs *found_inputs, *found_vals;
12645
12646
0
    ir_refs_init(found_inputs, 10);
12647
0
    ir_refs_init(found_vals, 10);
12648
12649
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
12650
0
      if (exit_addr && !(op1_info & (MAY_BE_OBJECT|may_be_string))) {
12651
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_ARRAY, exit_addr);
12652
0
      } else {
12653
0
        if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
12654
0
        ir_IF_TRUE(if_type);
12655
0
      }
12656
0
    }
12657
12658
0
    ht_ref = jit_Z_PTR(jit, op1_addr);
12659
12660
0
    if ((op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) ||
12661
0
        (opline->opcode != ZEND_FETCH_DIM_IS && JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE)) {
12662
0
      may_throw = 1;
12663
0
    }
12664
12665
0
    if (!zend_jit_fetch_dimension_address_inner(jit, opline,
12666
0
        (opline->opcode != ZEND_FETCH_DIM_IS) ? BP_VAR_R : BP_VAR_IS,
12667
0
        op1_info, op2_info, op2_addr, op2_range, dim_type, NULL, not_found_exit_addr, exit_addr,
12668
0
        result_type_guard, ht_ref, found_inputs, found_vals,
12669
0
        &end_inputs, &not_found_inputs)) {
12670
0
      return 0;
12671
0
    }
12672
12673
0
    if (found_inputs->count) {
12674
0
      ir_MERGE_N(found_inputs->count, found_inputs->refs);
12675
0
      ref = ir_PHI_N(IR_ADDR, found_vals->count, found_vals->refs);
12676
0
      val_addr = ZEND_ADDR_REF_ZVAL(ref);
12677
12678
0
      if (result_type_guard) {
12679
0
        uint8_t type = concrete_type(res_info);
12680
0
        uint32_t flags = 0;
12681
12682
0
        if (opline->opcode != ZEND_FETCH_LIST_R
12683
0
         && (opline->op1_type & (IS_VAR|IS_TMP_VAR))
12684
0
         && !op1_avoid_refcounting) {
12685
0
          flags |= ZEND_JIT_EXIT_FREE_OP1;
12686
0
        }
12687
0
        if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
12688
0
         && (op2_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
12689
0
          flags |= ZEND_JIT_EXIT_FREE_OP2;
12690
0
        }
12691
12692
0
        val_addr = zend_jit_guard_fetch_result_type(jit, opline, val_addr, type,
12693
0
          (op1_info & MAY_BE_ARRAY_OF_REF) != 0, flags, op1_avoid_refcounting);
12694
0
        if (!val_addr) {
12695
0
          return 0;
12696
0
        }
12697
12698
0
        if (not_found_inputs) {
12699
0
          ir_END_list(not_found_inputs);
12700
0
          ir_MERGE_list(not_found_inputs);
12701
0
        }
12702
12703
        // ZVAL_COPY
12704
0
        jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, !result_avoid_refcounting);
12705
0
        if (Z_MODE(res_addr) != IS_REG) {
12706
0
        } else if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
12707
0
          return 0;
12708
0
        }
12709
0
      } else if (op1_info & MAY_BE_ARRAY_OF_REF) {
12710
        // ZVAL_COPY_DEREF
12711
0
        ir_ref type_info = jit_Z_TYPE_INFO(jit, val_addr);
12712
0
        if (!zend_jit_zval_copy_deref(jit, res_addr, val_addr, type_info)) {
12713
0
          return 0;
12714
0
        }
12715
0
      } else  {
12716
        // ZVAL_COPY
12717
0
        jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, true);
12718
0
      }
12719
12720
0
      ir_END_list(end_inputs);
12721
0
    } else if (not_found_inputs) {
12722
0
      ir_MERGE_list(not_found_inputs);
12723
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12724
0
      ir_END_list(end_inputs);
12725
0
    } else if (!end_inputs && jit->ctx.control) {
12726
0
      ir_END_list(end_inputs); /* dead code */
12727
0
    }
12728
0
  }
12729
12730
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY)) {
12731
0
    if (if_type) {
12732
0
      ir_IF_FALSE_cold(if_type);
12733
0
      if_type = IS_UNDEF;
12734
0
    }
12735
12736
0
    if (opline->opcode != ZEND_FETCH_LIST_R && (op1_info & MAY_BE_STRING)) {
12737
0
      ir_ref str_ref;
12738
12739
0
      may_throw = 1;
12740
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_STRING))) {
12741
0
        if (exit_addr && !(op1_info & MAY_BE_OBJECT)) {
12742
0
          jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, exit_addr);
12743
0
        } else {
12744
0
          if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
12745
0
          ir_IF_TRUE(if_type);
12746
0
        }
12747
0
      }
12748
0
      jit_SET_EX_OPLINE(jit, opline);
12749
0
      str_ref = jit_Z_PTR(jit, op1_addr);
12750
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12751
0
        ir_ref ref;
12752
12753
0
        if ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG) {
12754
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_offset_r_helper),
12755
0
            str_ref, jit_Z_LVAL(jit, op2_addr));
12756
0
        } else {
12757
0
          ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_r_helper),
12758
0
            str_ref, jit_ZVAL_ADDR(jit, op2_addr));
12759
0
        }
12760
0
        jit_set_Z_PTR(jit, res_addr, ref);
12761
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING);
12762
0
      } else {
12763
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_str_is_helper),
12764
0
          str_ref,
12765
0
          jit_ZVAL_ADDR(jit, op2_addr),
12766
0
          jit_ZVAL_ADDR(jit, res_addr));
12767
0
      }
12768
0
      ir_END_list(end_inputs);
12769
0
    }
12770
12771
0
    if (op1_info & MAY_BE_OBJECT) {
12772
0
      ir_ref arg2;
12773
12774
0
      if (if_type) {
12775
0
        ir_IF_FALSE_cold(if_type);
12776
0
        if_type = IS_UNDEF;
12777
0
      }
12778
12779
0
      may_throw = 1;
12780
0
      if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string))) {
12781
0
        if (exit_addr) {
12782
0
          jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
12783
0
        } else {
12784
0
          if_type = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
12785
0
          ir_IF_TRUE(if_type);
12786
0
        }
12787
0
      }
12788
12789
0
      jit_SET_EX_OPLINE(jit, opline);
12790
0
      if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
12791
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
12792
0
        arg2 = ir_CONST_ADDR(Z_ZV(op2_addr)+1);
12793
0
      } else {
12794
0
        arg2 = jit_ZVAL_ADDR(jit, op2_addr);
12795
0
      }
12796
12797
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12798
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_r_helper),
12799
0
          jit_ZVAL_ADDR(jit, op1_addr),
12800
0
          arg2,
12801
0
          jit_ZVAL_ADDR(jit, res_addr));
12802
0
      } else {
12803
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_is_helper),
12804
0
          jit_ZVAL_ADDR(jit, op1_addr),
12805
0
          arg2,
12806
0
          jit_ZVAL_ADDR(jit, res_addr));
12807
0
      }
12808
12809
0
      ir_END_list(end_inputs);
12810
0
    }
12811
12812
0
    if ((op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string)))
12813
0
     && (!exit_addr || !(op1_info & (MAY_BE_ARRAY|MAY_BE_OBJECT|may_be_string)))) {
12814
12815
0
      if (if_type) {
12816
0
        ir_IF_FALSE_cold(if_type);
12817
0
        if_type = IS_UNDEF;
12818
0
      }
12819
12820
0
      if ((opline->opcode != ZEND_FETCH_DIM_IS && (op1_info & MAY_BE_UNDEF)) || (op2_info & MAY_BE_UNDEF)) {
12821
0
        jit_SET_EX_OPLINE(jit, opline);
12822
0
        if (opline->opcode != ZEND_FETCH_DIM_IS && (op1_info & MAY_BE_UNDEF)) {
12823
0
          may_throw = 1;
12824
0
          zend_jit_type_check_undef(jit, jit_Z_TYPE(jit, op1_addr), opline->op1.var, NULL,
12825
0
                  false, true, false);
12826
0
        }
12827
12828
0
        if (op2_info & MAY_BE_UNDEF) {
12829
0
          may_throw = 1;
12830
0
          zend_jit_type_check_undef(jit, jit_Z_TYPE(jit, op2_addr), opline->op2.var, NULL,
12831
0
                  false, true, false);
12832
0
        }
12833
0
      }
12834
12835
0
      if (opline->opcode != ZEND_FETCH_DIM_IS) {
12836
0
        ir_ref ref;
12837
12838
0
        may_throw = 1;
12839
0
        if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
12840
0
          ref = jit_ZVAL_ADDR(jit, orig_op1_addr);
12841
0
        } else {
12842
0
          jit_SET_EX_OPLINE(jit, opline);
12843
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
12844
0
        }
12845
0
        if (opline->opcode == ZEND_FETCH_LIST_R) {
12846
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_array_use), ref);
12847
0
        } else {
12848
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_array_access), ref);
12849
0
        }
12850
0
      }
12851
12852
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
12853
0
      ir_END_list(end_inputs);
12854
0
    }
12855
0
  }
12856
12857
0
  if (end_inputs) {
12858
0
    ir_MERGE_list(end_inputs);
12859
12860
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
12861
0
    if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & MAY_BE_OBJECT)) {
12862
      /* Magic offsetGet() may increase refcount of the key */
12863
0
      op2_info |= MAY_BE_RCN;
12864
0
    }
12865
0
#endif
12866
12867
0
    if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
12868
0
      if ((op2_info & MAY_HAVE_DTOR) && (op2_info & MAY_BE_RC1)) {
12869
0
        may_throw = 1;
12870
0
      }
12871
0
      jit_FREE_OP(jit,  opline->op2_type, opline->op2, op2_info, opline);
12872
0
    }
12873
0
    if (opline->opcode != ZEND_FETCH_LIST_R && !op1_avoid_refcounting) {
12874
0
      if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
12875
0
        if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
12876
0
          may_throw = 1;
12877
0
        }
12878
0
        jit_FREE_OP(jit,  opline->op1_type, opline->op1, op1_info, opline);
12879
0
      }
12880
0
    }
12881
12882
0
    if (may_throw) {
12883
0
      zend_jit_check_exception(jit);
12884
0
    }
12885
0
  } else if (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) {
12886
0
    ir_BEGIN(IR_UNUSED); /* unreachable tail */
12887
0
  }
12888
12889
0
  return 1;
12890
0
}
12891
12892
static zend_jit_addr zend_jit_prepare_array_update(zend_jit_ctx   *jit,
12893
                                                   const zend_op  *opline,
12894
                                                   uint32_t       *op1_info_ptr,
12895
                                                   zend_jit_addr   op1_addr,
12896
                                                   ir_ref         *if_type,
12897
                                                   ir_ref         *ht_ref,
12898
                                                   int            *may_throw)
12899
0
{
12900
0
  ir_ref ref = IR_UNUSED;
12901
0
  ir_ref array_reference_end = IR_UNUSED, array_reference_ref = IR_UNUSED;
12902
0
  ir_refs *array_inputs, *array_values;
12903
0
  uint32_t op1_info = *op1_info_ptr;
12904
12905
0
  ir_refs_init(array_inputs, 4);
12906
0
  ir_refs_init(array_values, 4);
12907
12908
0
  ref = jit_ZVAL_ADDR(jit, op1_addr);
12909
0
  if (op1_info & MAY_BE_REF) {
12910
0
    ir_ref if_reference, if_array, end1, ref2;
12911
12912
0
    *may_throw = 1;
12913
0
    if_reference = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
12914
0
    ir_IF_FALSE(if_reference);
12915
0
    end1 = ir_END();
12916
0
    ir_IF_TRUE_cold(if_reference);
12917
0
    array_reference_ref = ir_ADD_OFFSET(jit_Z_PTR_ref(jit, ref), offsetof(zend_reference, val));
12918
0
    if_array = jit_if_Z_TYPE_ref(jit, array_reference_ref, ir_CONST_U8(IS_ARRAY));
12919
0
    ir_IF_TRUE(if_array);
12920
0
    array_reference_end = ir_END();
12921
0
    ir_IF_FALSE_cold(if_array);
12922
0
    if (opline->opcode != ZEND_FETCH_DIM_RW && opline->opcode != ZEND_ASSIGN_DIM_OP) {
12923
0
      jit_SET_EX_OPLINE(jit, opline);
12924
0
    }
12925
0
    ref2 = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_prepare_assign_dim_ref), ref);
12926
0
    ir_GUARD(ref2, jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
12927
12928
0
    ir_MERGE_WITH(end1);
12929
0
    ref = ir_PHI_2(IR_ADDR, ref2, ref);
12930
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
12931
0
  }
12932
12933
0
  if (op1_info & MAY_BE_ARRAY) {
12934
0
    ir_ref op1_ref = ref;
12935
12936
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
12937
0
      *if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
12938
0
      ir_IF_TRUE(*if_type);
12939
0
    }
12940
0
    if (array_reference_end) {
12941
0
      ir_MERGE_WITH(array_reference_end);
12942
0
      op1_ref = ir_PHI_2(IR_ADDR, ref, array_reference_ref);
12943
0
    }
12944
    // JIT: SEPARATE_ARRAY()
12945
0
    ref = jit_Z_PTR_ref(jit, op1_ref);
12946
0
    if (RC_MAY_BE_N(op1_info)) {
12947
0
      if (RC_MAY_BE_1(op1_info)) {
12948
0
        ir_ref if_refcount_1 = ir_IF(ir_EQ(jit_GC_REFCOUNT(jit, ref), ir_CONST_U32(1)));
12949
0
        ir_IF_TRUE(if_refcount_1);
12950
0
        ir_refs_add(array_inputs, ir_END());
12951
0
        ir_refs_add(array_values, ref);
12952
0
        ir_IF_FALSE(if_refcount_1);
12953
0
      }
12954
0
      ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_zval_array_dup), op1_ref);
12955
0
    }
12956
0
    if (array_inputs->count || (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL))) {
12957
0
      ir_refs_add(array_inputs, ir_END());
12958
0
      ir_refs_add(array_values, ref);
12959
0
    }
12960
0
  }
12961
12962
0
  if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL)) {
12963
0
    if (*if_type) {
12964
0
      ir_IF_FALSE_cold(*if_type);
12965
0
      *if_type = IR_UNUSED;
12966
0
    }
12967
0
    if (op1_info & (MAY_BE_ANY-(MAY_BE_NULL|MAY_BE_ARRAY))) {
12968
0
      *if_type = ir_IF(ir_LE(jit_Z_TYPE(jit, op1_addr), ir_CONST_U8(IS_NULL)));
12969
0
      ir_IF_TRUE(*if_type);
12970
0
    }
12971
0
    if ((op1_info & MAY_BE_UNDEF)
12972
0
     && (opline->opcode == ZEND_FETCH_DIM_RW || opline->opcode == ZEND_ASSIGN_DIM_OP)) {
12973
0
      ir_ref end1 = IR_UNUSED;
12974
12975
0
      *may_throw = 1;
12976
0
      if (op1_info & MAY_BE_NULL) {
12977
0
        ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
12978
0
        ir_IF_TRUE(if_def);
12979
0
        end1 = ir_END();
12980
0
        ir_IF_FALSE(if_def);
12981
0
      }
12982
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op1.var));
12983
0
      if (end1) {
12984
0
        ir_MERGE_WITH(end1);
12985
0
      }
12986
0
    }
12987
    // JIT: ZVAL_ARR(container, zend_new_array(8));
12988
0
    ref = ir_CALL_1(IR_ADDR,
12989
0
      jit_STUB_FUNC_ADDR(jit, jit_stub_new_array, IR_FASTCALL_FUNC),
12990
0
      jit_ZVAL_ADDR(jit, op1_addr));
12991
0
    if (array_inputs->count) {
12992
0
      ir_refs_add(array_inputs, ir_END());
12993
0
      ir_refs_add(array_values, ref);
12994
0
    }
12995
0
    op1_info &= ~(MAY_BE_UNDEF | MAY_BE_NULL);
12996
0
    op1_info |= MAY_BE_ARRAY | MAY_BE_RC1;
12997
0
    *op1_info_ptr = op1_info;
12998
0
  }
12999
13000
0
  if (array_inputs->count) {
13001
0
    ir_MERGE_N(array_inputs->count, array_inputs->refs);
13002
0
    ref = ir_PHI_N(IR_ADDR, array_values->count, array_values->refs);
13003
0
  }
13004
13005
0
  *ht_ref = ref;
13006
0
  return op1_addr;
13007
0
}
13008
13009
static int zend_jit_fetch_dim(zend_jit_ctx   *jit,
13010
                              const zend_op  *opline,
13011
                              uint32_t        op1_info,
13012
                              zend_jit_addr   op1_addr,
13013
                              uint32_t        op2_info,
13014
                              zend_jit_addr   op2_addr,
13015
                              zend_ssa_range *op2_range,
13016
                              zend_jit_addr   res_addr,
13017
                              uint8_t         dim_type)
13018
0
{
13019
0
  int may_throw = 0;
13020
0
  ir_ref end_inputs = IR_UNUSED;
13021
0
  ir_ref ref, if_type = IR_UNUSED, ht_ref;
13022
13023
0
  if (opline->opcode == ZEND_FETCH_DIM_RW) {
13024
0
    jit_SET_EX_OPLINE(jit, opline);
13025
0
  }
13026
13027
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13028
13029
0
  if (op1_info & MAY_BE_ARRAY) {
13030
0
    ir_refs *found_inputs, *found_vals;
13031
13032
0
    ir_refs_init(found_inputs, 8);
13033
0
    ir_refs_init(found_vals, 8);
13034
13035
0
    if (opline->op2_type == IS_UNUSED) {
13036
0
      ir_ref if_ok;
13037
13038
0
      may_throw = 1;
13039
      // JIT:var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13040
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13041
0
        ht_ref, jit_EG(uninitialized_zval));
13042
13043
      // JIT: if (UNEXPECTED(!var_ptr)) {
13044
0
      if_ok = ir_IF(ref);
13045
0
      ir_IF_FALSE_cold(if_ok);
13046
0
      if (opline->opcode != ZEND_FETCH_DIM_RW) {
13047
0
        jit_SET_EX_OPLINE(jit, opline);
13048
0
      }
13049
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13050
0
      ir_END_list(end_inputs);
13051
13052
0
      ir_IF_TRUE(if_ok);
13053
0
      jit_set_Z_PTR(jit, res_addr, ref);
13054
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
13055
13056
0
      ir_END_list(end_inputs);
13057
0
    } else {
13058
0
      uint32_t type;
13059
13060
0
      switch (opline->opcode) {
13061
0
        case ZEND_FETCH_DIM_W:
13062
0
        case ZEND_FETCH_LIST_W:
13063
0
          type = BP_VAR_W;
13064
0
          break;
13065
0
        case ZEND_FETCH_DIM_RW:
13066
0
          may_throw = 1;
13067
0
          type = BP_VAR_RW;
13068
0
          break;
13069
0
        case ZEND_FETCH_DIM_UNSET:
13070
0
          type = BP_VAR_UNSET;
13071
0
          break;
13072
0
        default:
13073
0
          ZEND_UNREACHABLE();
13074
0
      }
13075
13076
0
      if (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING))) {
13077
0
        may_throw = 1;
13078
0
      }
13079
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, type, op1_info,
13080
0
          op2_info, op2_addr, op2_range, dim_type, NULL, NULL, NULL,
13081
0
          false, ht_ref, found_inputs, found_vals, &end_inputs, NULL)) {
13082
0
        return 0;
13083
0
      }
13084
13085
0
      if (type == BP_VAR_RW || (op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING)))) {
13086
0
        if (end_inputs) {
13087
0
          ir_MERGE_list(end_inputs);
13088
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
13089
0
          end_inputs = ir_END();
13090
0
        }
13091
0
      } else if (!(op2_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
13092
        /* impossible dead path */
13093
0
        end_inputs = ir_END();
13094
0
      } else {
13095
0
        ZEND_ASSERT(end_inputs == IR_UNUSED);
13096
0
      }
13097
13098
0
      if (found_inputs->count) {
13099
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13100
0
        ref = ir_PHI_N(IR_ADDR, found_vals->count, found_vals->refs);
13101
0
        jit_set_Z_PTR(jit, res_addr, ref);
13102
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
13103
0
        ir_END_list(end_inputs);
13104
0
      }
13105
13106
0
    }
13107
0
  }
13108
13109
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13110
0
    ir_ref arg2;
13111
13112
0
    may_throw = 1;
13113
13114
0
    if (if_type) {
13115
0
      ir_IF_FALSE(if_type);
13116
0
      if_type = IR_UNUSED;
13117
0
    }
13118
13119
0
    if (opline->opcode != ZEND_FETCH_DIM_RW) {
13120
0
      jit_SET_EX_OPLINE(jit, opline);
13121
0
    }
13122
13123
0
      if (opline->op2_type == IS_UNUSED) {
13124
0
      arg2 = IR_NULL;
13125
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13126
0
      ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13127
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13128
0
    } else {
13129
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13130
0
    }
13131
13132
0
    switch (opline->opcode) {
13133
0
      case ZEND_FETCH_DIM_W:
13134
0
      case ZEND_FETCH_LIST_W:
13135
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_w_helper),
13136
0
          jit_ZVAL_ADDR(jit, op1_addr),
13137
0
          arg2,
13138
0
          jit_ZVAL_ADDR(jit, res_addr));
13139
0
        break;
13140
0
      case ZEND_FETCH_DIM_RW:
13141
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_dim_obj_rw_helper),
13142
0
          jit_ZVAL_ADDR(jit, op1_addr),
13143
0
          arg2,
13144
0
          jit_ZVAL_ADDR(jit, res_addr));
13145
0
        break;
13146
//      case ZEND_FETCH_DIM_UNSET:
13147
//        | EXT_CALL zend_jit_fetch_dim_obj_unset_helper, r0
13148
//        break;
13149
0
      default:
13150
0
        ZEND_UNREACHABLE();
13151
0
      }
13152
13153
0
    if (op1_info & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_ARRAY)) {
13154
0
      ir_END_list(end_inputs);
13155
0
    }
13156
0
  }
13157
13158
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13159
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))) {
13160
    /* ASSIGN_DIM may increase refcount of the key */
13161
0
    op2_info |= MAY_BE_RCN;
13162
0
  }
13163
0
#endif
13164
13165
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR))
13166
0
   && (op2_info & MAY_HAVE_DTOR)
13167
0
   && (op2_info & MAY_BE_RC1)) {
13168
0
    may_throw = 1;
13169
0
  }
13170
13171
0
  if (end_inputs) {
13172
0
    ir_MERGE_list(end_inputs);
13173
0
  }
13174
13175
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13176
13177
0
  if (may_throw) {
13178
0
    zend_jit_check_exception(jit);
13179
0
  }
13180
13181
0
  return 1;
13182
0
}
13183
13184
static int zend_jit_isset_isempty_dim(zend_jit_ctx   *jit,
13185
                                      const zend_op  *opline,
13186
                                      uint32_t        op1_info,
13187
                                      zend_jit_addr   op1_addr,
13188
                                      bool       op1_avoid_refcounting,
13189
                                      uint32_t        op2_info,
13190
                                      zend_jit_addr   op2_addr,
13191
                                      zend_ssa_range *op2_range,
13192
                                      uint8_t         dim_type,
13193
                                      int             may_throw,
13194
                                      uint8_t         smart_branch_opcode,
13195
                                      uint32_t        target_label,
13196
                                      uint32_t        target_label2,
13197
                                      const void     *exit_addr)
13198
0
{
13199
0
  zend_jit_addr res_addr;
13200
0
  ir_ref if_type = IR_UNUSED;
13201
0
  ir_ref false_inputs = IR_UNUSED, end_inputs = IR_UNUSED;
13202
0
  ir_refs *true_inputs;
13203
13204
0
  ir_refs_init(true_inputs, 8);
13205
13206
  // TODO: support for empty() ???
13207
0
  ZEND_ASSERT(!(opline->extended_value & ZEND_ISEMPTY));
13208
13209
0
  res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
13210
13211
0
  if (op1_info & MAY_BE_REF) {
13212
0
    ir_ref ref = jit_ZVAL_ADDR(jit, op1_addr);
13213
0
    ref = jit_ZVAL_DEREF_ref(jit, ref);
13214
0
    op1_addr = ZEND_ADDR_REF_ZVAL(ref);
13215
0
  }
13216
13217
0
  if (op1_info & MAY_BE_ARRAY) {
13218
0
    const void *found_exit_addr = NULL;
13219
0
    const void *not_found_exit_addr = NULL;
13220
0
    ir_ref ht_ref;
13221
13222
0
    if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY)) {
13223
0
      if_type = jit_if_Z_TYPE(jit, op1_addr, IS_ARRAY);
13224
0
      ir_IF_TRUE(if_type);
13225
0
    }
13226
13227
0
    ht_ref = jit_Z_PTR(jit, op1_addr);
13228
13229
0
    if (exit_addr
13230
0
     && !(op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY))
13231
0
     && !may_throw
13232
0
     && (!(opline->op1_type & (IS_TMP_VAR|IS_VAR)) || op1_avoid_refcounting)
13233
0
     && (!(opline->op2_type & (IS_TMP_VAR|IS_VAR)) || !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)))) {
13234
0
      if (smart_branch_opcode == ZEND_JMPNZ) {
13235
0
        found_exit_addr = exit_addr;
13236
0
      } else {
13237
0
        not_found_exit_addr = exit_addr;
13238
0
      }
13239
0
    }
13240
0
    if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_JIT_IS, op1_info,
13241
0
        op2_info, op2_addr, op2_range, dim_type, found_exit_addr, not_found_exit_addr, NULL,
13242
0
        false, ht_ref, true_inputs, NULL, &false_inputs, NULL)) {
13243
0
      return 0;
13244
0
    }
13245
13246
0
    if (found_exit_addr) {
13247
0
      ir_MERGE_list(false_inputs);
13248
0
      return 1;
13249
0
    } else if (not_found_exit_addr) {
13250
0
      ir_MERGE_N(true_inputs->count, true_inputs->refs);
13251
0
      return 1;
13252
0
    }
13253
0
  }
13254
13255
0
  if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_ARRAY)) {
13256
0
    if (if_type) {
13257
0
      ir_IF_FALSE(if_type);
13258
0
      if_type = IR_UNUSED;
13259
0
    }
13260
13261
0
    if (op1_info & (MAY_BE_STRING|MAY_BE_OBJECT)) {
13262
0
      ir_ref ref, arg1, arg2, if_true;
13263
13264
0
      jit_SET_EX_OPLINE(jit, opline);
13265
0
      arg1 = jit_ZVAL_ADDR(jit, op1_addr);
13266
0
      if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13267
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13268
0
        arg2 = ir_CONST_ADDR(Z_ZV(op2_addr)+1);
13269
0
      } else {
13270
0
        arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13271
0
      }
13272
0
      ref = ir_CALL_2(IR_I32, ir_CONST_FC_FUNC(zend_jit_isset_dim_helper), arg1, arg2);
13273
0
      if_true = ir_IF(ref);
13274
0
      ir_IF_TRUE(if_true);
13275
0
      ir_refs_add(true_inputs, ir_END());
13276
0
      ir_IF_FALSE(if_true);
13277
0
      ir_END_list(false_inputs);
13278
0
    } else {
13279
0
      if (op2_info & MAY_BE_UNDEF) {
13280
0
        ir_ref end1 = IR_UNUSED;
13281
13282
0
        if (op2_info & MAY_BE_ANY) {
13283
0
          ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op2_addr));
13284
0
          ir_IF_TRUE(if_def);
13285
0
          end1 = ir_END();
13286
0
          ir_IF_FALSE(if_def);
13287
0
        }
13288
0
        jit_SET_EX_OPLINE(jit, opline);
13289
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper), ir_CONST_U32(opline->op2.var));
13290
0
        if (end1) {
13291
0
          ir_MERGE_WITH(end1);
13292
0
        }
13293
0
      }
13294
0
      ir_END_list(false_inputs);
13295
0
    }
13296
0
  }
13297
13298
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13299
0
  if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) && (op1_info & MAY_BE_OBJECT)) {
13300
    /* Magic offsetExists() may increase refcount of the key */
13301
0
    op2_info |= MAY_BE_RCN;
13302
0
  }
13303
0
#endif
13304
13305
0
  if (true_inputs->count) {
13306
0
    ir_MERGE_N(true_inputs->count, true_inputs->refs);
13307
13308
0
    jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13309
0
    if (!op1_avoid_refcounting) {
13310
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13311
0
    }
13312
0
    if (may_throw) {
13313
0
      zend_jit_check_exception_undef_result(jit, opline);
13314
0
    }
13315
0
    if (!(opline->extended_value & ZEND_ISEMPTY)) {
13316
0
      if (exit_addr) {
13317
0
        if (smart_branch_opcode == ZEND_JMPNZ) {
13318
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13319
0
        } else {
13320
0
          ir_END_list(end_inputs);
13321
0
        }
13322
0
      } else if (smart_branch_opcode) {
13323
0
        if (smart_branch_opcode == ZEND_JMPZ) {
13324
0
          _zend_jit_add_predecessor_ref(jit, target_label2, jit->b, ir_END());
13325
0
        } else if (smart_branch_opcode == ZEND_JMPNZ) {
13326
0
          _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
13327
0
        } else {
13328
0
          ZEND_UNREACHABLE();
13329
0
        }
13330
0
      } else {
13331
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_TRUE);
13332
0
        ir_END_list(end_inputs);
13333
0
      }
13334
0
    } else {
13335
0
      ZEND_UNREACHABLE(); // TODO: support for empty()
13336
0
    }
13337
0
  }
13338
13339
0
  ir_MERGE_list(false_inputs);
13340
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13341
0
  if (!op1_avoid_refcounting) {
13342
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13343
0
  }
13344
0
  if (may_throw) {
13345
0
    zend_jit_check_exception_undef_result(jit, opline);
13346
0
  }
13347
0
  if (!(opline->extended_value & ZEND_ISEMPTY)) {
13348
0
    if (exit_addr) {
13349
0
      if (smart_branch_opcode == ZEND_JMPZ) {
13350
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13351
0
      } else {
13352
0
        ir_END_list(end_inputs);
13353
0
      }
13354
0
    } else if (smart_branch_opcode) {
13355
0
      if (smart_branch_opcode == ZEND_JMPZ) {
13356
0
        _zend_jit_add_predecessor_ref(jit, target_label, jit->b, ir_END());
13357
0
      } else if (smart_branch_opcode == ZEND_JMPNZ) {
13358
0
        _zend_jit_add_predecessor_ref(jit, target_label2, jit->b, ir_END());
13359
0
      } else {
13360
0
        ZEND_UNREACHABLE();
13361
0
      }
13362
0
    } else {
13363
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_FALSE);
13364
0
      ir_END_list(end_inputs);
13365
0
    }
13366
0
  } else {
13367
0
    ZEND_UNREACHABLE(); // TODO: support for empty()
13368
0
  }
13369
13370
0
    if (!exit_addr && smart_branch_opcode) {
13371
0
    jit->b = -1;
13372
0
    } else {
13373
0
    ir_MERGE_list(end_inputs);
13374
0
    }
13375
13376
0
  return 1;
13377
0
}
13378
13379
static int zend_jit_assign_dim(zend_jit_ctx  *jit,
13380
                               const zend_op *opline,
13381
                               uint32_t       op1_info,
13382
                               zend_jit_addr  op1_addr,
13383
                               bool           op1_indirect,
13384
                               uint32_t       op2_info,
13385
                               zend_jit_addr  op2_addr,
13386
                               zend_ssa_range *op2_range,
13387
                               uint32_t       val_info,
13388
                               zend_jit_addr  op3_addr,
13389
                               zend_jit_addr  op3_def_addr,
13390
                               zend_jit_addr  res_addr,
13391
                               uint8_t        dim_type,
13392
                               int            may_throw)
13393
0
{
13394
0
  ir_ref if_type = IR_UNUSED;
13395
0
  ir_ref end_inputs = IR_UNUSED, ht_ref;
13396
13397
0
  if (op3_addr != op3_def_addr && op3_def_addr) {
13398
0
    if (!zend_jit_update_regs(jit, (opline+1)->op1.var, op3_addr, op3_def_addr, val_info)) {
13399
0
      return 0;
13400
0
    }
13401
0
    if (Z_MODE(op3_def_addr) == IS_REG && Z_MODE(op3_addr) != IS_REG) {
13402
0
      op3_addr = op3_def_addr;
13403
0
    }
13404
0
  }
13405
13406
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && (val_info & MAY_BE_UNDEF)) {
13407
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
13408
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13409
13410
0
    if (!exit_addr) {
13411
0
      return 0;
13412
0
    }
13413
13414
0
    jit_guard_not_Z_TYPE(jit, op3_addr, IS_UNDEF, exit_addr);
13415
13416
0
    val_info &= ~MAY_BE_UNDEF;
13417
0
  }
13418
13419
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13420
13421
0
  if (op1_info & MAY_BE_ARRAY) {
13422
0
    if (opline->op2_type == IS_UNUSED) {
13423
0
      uint32_t var_info = MAY_BE_NULL;
13424
0
      ir_ref if_ok, ref;
13425
0
      zend_jit_addr var_addr;
13426
13427
      // JIT: var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13428
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13429
0
        ht_ref, jit_EG(uninitialized_zval));
13430
13431
      // JIT: if (UNEXPECTED(!var_ptr)) {
13432
0
      if_ok = ir_IF(ref);
13433
0
      ir_IF_FALSE_cold(if_ok);
13434
13435
      // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
13436
0
      jit_SET_EX_OPLINE(jit, opline);
13437
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13438
13439
0
      ir_END_list(end_inputs);
13440
13441
0
      ir_IF_TRUE(if_ok);
13442
0
      var_addr = ZEND_ADDR_REF_ZVAL(ref);
13443
0
      if (!zend_jit_simple_assign(jit, opline, var_addr, var_info, -1, (opline+1)->op1_type, op3_addr, val_info, res_addr, false)) {
13444
0
        return 0;
13445
0
      }
13446
0
    } else {
13447
0
      uint32_t var_info = zend_array_element_type(op1_info, opline->op1_type, 0, 0);
13448
0
      zend_jit_addr var_addr;
13449
0
      ir_ref ref;
13450
0
      ir_refs *found_inputs, *found_values;
13451
13452
0
      ir_refs_init(found_inputs, 8);
13453
0
      ir_refs_init(found_values, 8);
13454
13455
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_VAR_W, op1_info,
13456
0
          op2_info, op2_addr, op2_range, dim_type, NULL, NULL, NULL,
13457
0
          false, ht_ref, found_inputs, found_values, &end_inputs, NULL)) {
13458
0
        return 0;
13459
0
      }
13460
13461
0
      if (op1_info & (MAY_BE_ARRAY_OF_REF|MAY_BE_OBJECT)) {
13462
0
        var_info |= MAY_BE_REF;
13463
0
      }
13464
0
      if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
13465
0
        var_info |= MAY_BE_RC1;
13466
0
      }
13467
13468
0
      if (found_inputs->count) {
13469
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13470
0
        ref = ir_PHI_N(IR_ADDR, found_values->count, found_values->refs);
13471
0
        var_addr = ZEND_ADDR_REF_ZVAL(ref);
13472
13473
        // JIT: value = zend_assign_to_variable(variable_ptr, value, OP_DATA_TYPE);
13474
0
        if (opline->op1_type == IS_VAR
13475
0
         && Z_MODE(op3_addr) != IS_REG
13476
0
         && opline->result_type == IS_UNUSED
13477
0
         && (res_addr == 0 || Z_MODE(res_addr) != IS_REG)) {
13478
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)) {
13479
0
            return 0;
13480
0
          }
13481
0
        } else {
13482
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)) {
13483
0
            return 0;
13484
0
          }
13485
0
        }
13486
0
      }
13487
0
    }
13488
13489
0
    ir_END_list(end_inputs);
13490
0
  }
13491
13492
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13493
0
    ir_ref arg2, arg4;
13494
13495
0
    if (if_type) {
13496
0
      ir_IF_FALSE_cold(if_type);
13497
0
      if_type = IR_UNUSED;
13498
0
    }
13499
13500
0
    jit_SET_EX_OPLINE(jit, opline);
13501
13502
0
      if (opline->op2_type == IS_UNUSED) {
13503
0
      arg2 = IR_NULL;
13504
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13505
0
        ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13506
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13507
0
    } else {
13508
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13509
0
    }
13510
13511
0
    if (opline->result_type == IS_UNUSED) {
13512
0
      arg4 = IR_NULL;
13513
0
    } else {
13514
0
      arg4 = jit_ZVAL_ADDR(jit, res_addr);
13515
0
    }
13516
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_helper),
13517
0
      jit_ZVAL_ADDR(jit, op1_addr),
13518
0
      arg2,
13519
0
      jit_ZVAL_ADDR(jit, op3_addr),
13520
0
      arg4);
13521
13522
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13523
0
    if (((opline+1)->op1_type & (IS_TMP_VAR|IS_VAR)) && (val_info & MAY_BE_RC1)) {
13524
      /* ASSIGN_DIM may increase refcount of the value */
13525
0
      val_info |= MAY_BE_RCN;
13526
0
    }
13527
0
#endif
13528
13529
0
    jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, NULL);
13530
13531
0
    ir_END_list(end_inputs);
13532
0
  }
13533
13534
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
13535
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))) {
13536
    /* ASSIGN_DIM may increase refcount of the key */
13537
0
    op2_info |= MAY_BE_RCN;
13538
0
  }
13539
0
#endif
13540
13541
0
  ir_MERGE_list(end_inputs);
13542
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, opline);
13543
13544
0
  if (!op1_indirect) {
13545
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
13546
0
  }
13547
13548
0
  if (may_throw) {
13549
0
    zend_jit_check_exception(jit);
13550
0
  }
13551
13552
0
  return 1;
13553
0
}
13554
13555
static int zend_jit_assign_dim_op(zend_jit_ctx   *jit,
13556
                                  const zend_op  *opline,
13557
                                  uint32_t        op1_info,
13558
                                  uint32_t        op1_def_info,
13559
                                  zend_jit_addr   op1_addr,
13560
                                  bool            op1_indirect,
13561
                                  uint32_t        op2_info,
13562
                                  zend_jit_addr   op2_addr,
13563
                                  zend_ssa_range *op2_range,
13564
                                  uint32_t        op1_data_info,
13565
                                  zend_jit_addr   op3_addr,
13566
                                  zend_ssa_range *op1_data_range,
13567
                                  uint8_t         dim_type,
13568
                                  int             may_throw)
13569
0
{
13570
0
  zend_jit_addr var_addr = IS_UNUSED;
13571
0
  const void *not_found_exit_addr = NULL;
13572
0
  uint32_t var_info = MAY_BE_NULL;
13573
0
  ir_ref if_type = IS_UNUSED;
13574
0
  ir_ref end_inputs = IR_UNUSED, ht_ref;
13575
0
  bool emit_fast_path = true;
13576
13577
0
  ZEND_ASSERT(opline->result_type == IS_UNUSED);
13578
13579
0
  if (may_throw) {
13580
0
    jit_SET_EX_OPLINE(jit, opline);
13581
0
  }
13582
13583
0
  op1_addr = zend_jit_prepare_array_update(jit, opline, &op1_info, op1_addr, &if_type, &ht_ref, &may_throw);
13584
13585
0
  if (Z_MODE(op3_addr) == IS_REG
13586
0
   && Z_LOAD(op3_addr)
13587
0
   && jit->ra[Z_SSA_VAR(op3_addr)].ref == IR_NULL) {
13588
    /* Force load */
13589
0
    zend_jit_use_reg(jit, op3_addr);
13590
0
  }
13591
13592
0
  if (op1_info & MAY_BE_ARRAY) {
13593
0
    uint32_t var_def_info = zend_array_element_type(op1_def_info, opline->op1_type, 1, 0);
13594
13595
0
    if (opline->op2_type == IS_UNUSED) {
13596
0
      var_info = MAY_BE_NULL;
13597
0
      ir_ref if_ok, ref;
13598
13599
      // JIT: var_ptr = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
13600
0
      ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_hash_next_index_insert),
13601
0
        ht_ref, jit_EG(uninitialized_zval));
13602
13603
      // JIT: if (UNEXPECTED(!var_ptr)) {
13604
0
      if_ok = ir_IF(ref);
13605
0
      ir_IF_FALSE_cold(if_ok);
13606
13607
      // JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
13608
0
      ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13609
13610
0
      ir_END_list(end_inputs);
13611
13612
0
      ir_IF_TRUE(if_ok);
13613
0
      var_addr = ZEND_ADDR_REF_ZVAL(ref);
13614
0
    } else {
13615
0
      ir_ref ref;
13616
0
      ir_refs *found_inputs, *found_values;
13617
13618
0
      ir_refs_init(found_inputs, 8);
13619
0
      ir_refs_init(found_values, 8);
13620
13621
0
      var_info = zend_array_element_type(op1_info, opline->op1_type, 0, 0);
13622
0
      if (op1_info & (MAY_BE_ARRAY_OF_REF|MAY_BE_OBJECT)) {
13623
0
        var_info |= MAY_BE_REF;
13624
0
      }
13625
0
      if (var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
13626
0
        var_info |= MAY_BE_RC1;
13627
0
      }
13628
13629
0
      if (dim_type != IS_UNKNOWN
13630
0
       && dim_type != IS_UNDEF
13631
0
       && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY
13632
0
       && (op2_info & (MAY_BE_LONG|MAY_BE_STRING))
13633
0
       && !(op2_info & ((MAY_BE_ANY|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_STRING)))) {
13634
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
13635
0
        not_found_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13636
0
        if (!not_found_exit_addr) {
13637
0
          return 0;
13638
0
        }
13639
0
      }
13640
13641
0
      if (!zend_jit_fetch_dimension_address_inner(jit, opline, BP_VAR_RW, op1_info,
13642
0
          op2_info, op2_addr, op2_range, dim_type, NULL, not_found_exit_addr, NULL,
13643
0
          false, ht_ref, found_inputs, found_values, &end_inputs, NULL)) {
13644
0
        return 0;
13645
0
      }
13646
13647
0
      if (found_inputs->count) {
13648
0
        ir_MERGE_N(found_inputs->count, found_inputs->refs);
13649
0
        ref = ir_PHI_N(IR_ADDR, found_values->count, found_values->refs);
13650
0
        var_addr = ZEND_ADDR_REF_ZVAL(ref);
13651
13652
0
        if (not_found_exit_addr && dim_type != IS_REFERENCE) {
13653
0
          jit_guard_Z_TYPE(jit, var_addr, dim_type, not_found_exit_addr);
13654
0
          var_info = (1 << dim_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
13655
0
        }
13656
0
        if (var_info & MAY_BE_REF) {
13657
0
          binary_op_type binary_op = get_binary_op(opline->extended_value);
13658
0
          ir_ref if_ref, if_typed, noref_path, ref_path, ref, reference, ref2, arg2;
13659
13660
0
          ref = jit_ZVAL_ADDR(jit, var_addr);
13661
0
          if_ref = jit_if_Z_TYPE(jit, var_addr, IS_REFERENCE);
13662
0
          ir_IF_FALSE(if_ref);
13663
0
          noref_path = ir_END();
13664
0
          ir_IF_TRUE(if_ref);
13665
13666
0
          reference = jit_Z_PTR_ref(jit, ref);
13667
0
          ref2 = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
13668
0
          if_typed = jit_if_TYPED_REF(jit, reference);
13669
0
          ir_IF_FALSE(if_typed);
13670
0
          ref_path = ir_END();
13671
0
          ir_IF_TRUE_cold(if_typed);
13672
13673
0
          if (Z_MODE(op3_addr) == IS_REG) {
13674
0
            zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
13675
0
            if (!zend_jit_spill_store_inv(jit, op3_addr, real_addr, op1_data_info)) {
13676
0
              return 0;
13677
0
            }
13678
0
            op3_addr = real_addr;
13679
0
          }
13680
0
          arg2 = jit_ZVAL_ADDR(jit, op3_addr);
13681
0
          ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
13682
0
            reference, arg2, ir_CONST_FC_FUNC(binary_op));
13683
13684
0
          ir_END_list(end_inputs);
13685
13686
0
          ir_MERGE_2(noref_path, ref_path);
13687
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
13688
0
          var_addr = ZEND_ADDR_REF_ZVAL(ref);
13689
0
        }
13690
0
      } else {
13691
0
        emit_fast_path = false;
13692
0
      }
13693
0
    }
13694
13695
0
    if (emit_fast_path) {
13696
0
      uint8_t val_op_type = (opline+1)->op1_type;
13697
13698
0
      if (val_op_type & (IS_TMP_VAR|IS_VAR)) {
13699
        /* prevent FREE_OP in the helpers */
13700
0
        val_op_type = IS_CV;
13701
0
      }
13702
13703
0
      switch (opline->extended_value) {
13704
0
        case ZEND_ADD:
13705
0
        case ZEND_SUB:
13706
0
        case ZEND_MUL:
13707
0
        case ZEND_DIV:
13708
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,
13709
0
              1 /* may overflow */, may_throw)) {
13710
0
            return 0;
13711
0
          }
13712
0
          break;
13713
0
        case ZEND_BW_OR:
13714
0
        case ZEND_BW_AND:
13715
0
        case ZEND_BW_XOR:
13716
0
        case ZEND_SL:
13717
0
        case ZEND_SR:
13718
0
        case ZEND_MOD:
13719
0
          if (!zend_jit_long_math_helper(jit, opline, opline->extended_value,
13720
0
              IS_CV, opline->op1, var_addr, var_info, NULL,
13721
0
              val_op_type, (opline+1)->op1, op3_addr, op1_data_info,
13722
0
              op1_data_range,
13723
0
              0, var_addr, var_def_info, var_info, may_throw)) {
13724
0
            return 0;
13725
0
          }
13726
0
          break;
13727
0
        case ZEND_CONCAT:
13728
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,
13729
0
              may_throw)) {
13730
0
            return 0;
13731
0
          }
13732
0
          break;
13733
0
        default:
13734
0
          ZEND_UNREACHABLE();
13735
0
      }
13736
13737
0
      ir_END_list(end_inputs);
13738
0
    }
13739
0
  }
13740
13741
0
  if (op1_info & (MAY_BE_ANY-MAY_BE_ARRAY)) {
13742
0
    binary_op_type binary_op;
13743
0
    ir_ref arg2;
13744
13745
0
    if (if_type) {
13746
0
      ir_IF_FALSE_cold(if_type);
13747
0
      if_type = IS_UNUSED;
13748
0
    }
13749
13750
0
      if (opline->op2_type == IS_UNUSED) {
13751
0
      arg2 = IR_NULL;
13752
0
    } else if (opline->op2_type == IS_CONST && Z_EXTRA_P(RT_CONSTANT(opline, opline->op2)) == ZEND_EXTRA_VALUE) {
13753
0
      ZEND_ASSERT(Z_MODE(op2_addr) == IS_CONST_ZVAL);
13754
0
      arg2 = ir_CONST_ADDR(Z_ZV(op2_addr) + 1);
13755
0
    } else {
13756
0
      arg2 = jit_ZVAL_ADDR(jit, op2_addr);
13757
0
    }
13758
0
    binary_op = get_binary_op(opline->extended_value);
13759
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_dim_op_helper),
13760
0
      jit_ZVAL_ADDR(jit, op1_addr),
13761
0
      arg2,
13762
0
      jit_ZVAL_ADDR(jit, op3_addr),
13763
0
      ir_CONST_FC_FUNC(binary_op));
13764
0
    ir_END_list(end_inputs);
13765
0
  }
13766
13767
0
  if (end_inputs) {
13768
0
    ir_MERGE_list(end_inputs);
13769
0
  }
13770
13771
0
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, op1_data_info, NULL);
13772
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
13773
0
  if (!op1_indirect) {
13774
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
13775
0
  }
13776
0
  if (may_throw) {
13777
0
    zend_jit_check_exception(jit);
13778
0
  }
13779
13780
0
  return 1;
13781
0
}
13782
13783
static int zend_jit_fe_reset(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
13784
0
{
13785
0
  zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
13786
13787
  // JIT: ZVAL_COPY(res, value);
13788
0
  if (opline->op1_type == IS_CONST) {
13789
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
13790
13791
0
    jit_ZVAL_COPY_CONST(jit, res_addr, MAY_BE_ANY, MAY_BE_ANY, zv, true);
13792
0
  } else {
13793
0
    zend_jit_addr op1_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
13794
13795
0
    jit_ZVAL_COPY(jit, res_addr, -1, op1_addr, op1_info, opline->op1_type == IS_CV);
13796
0
  }
13797
13798
  // JIT: Z_FE_POS_P(res) = 0;
13799
0
  ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->result.var + offsetof(zval, u2.fe_pos)), ir_CONST_U32(0));
13800
13801
0
  return 1;
13802
0
}
13803
13804
static int zend_jit_packed_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var, uint32_t op_info)
13805
0
{
13806
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_PACKED_GUARD);
13807
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
13808
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
13809
0
  ir_ref ref;
13810
13811
0
  if (!exit_addr) {
13812
0
    return 0;
13813
0
  }
13814
13815
0
  ref = ir_AND_U32(
13816
0
    ir_LOAD_U32(ir_ADD_OFFSET(jit_Z_PTR(jit, addr), offsetof(zend_array, u.flags))),
13817
0
    ir_CONST_U32(HASH_FLAG_PACKED));
13818
0
  if (op_info & MAY_BE_ARRAY_PACKED) {
13819
0
    ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13820
0
  } else {
13821
0
    ir_GUARD_NOT(ref, ir_CONST_ADDR(exit_addr));
13822
0
  }
13823
13824
0
  return 1;
13825
0
}
13826
13827
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)
13828
0
{
13829
0
  zend_jit_addr op1_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op1.var);
13830
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;
13831
0
  ir_ref if_def_hash = IR_UNUSED, if_def_packed = IR_UNUSED;
13832
0
  ir_ref exit_inputs = IR_UNUSED;
13833
13834
0
  if (!MAY_BE_HASH(op1_info) && !MAY_BE_PACKED(op1_info)) {
13835
    /* empty array */
13836
0
    if (exit_addr) {
13837
0
      if (exit_opcode == ZEND_JMP) {
13838
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
13839
0
      }
13840
0
    } else {
13841
0
      zend_basic_block *bb;
13842
13843
0
      ZEND_ASSERT(jit->b >= 0);
13844
0
      bb = &jit->ssa->cfg.blocks[jit->b];
13845
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ir_END());
13846
0
      jit->b = -1;
13847
0
    }
13848
0
    return 1;
13849
0
  }
13850
13851
  // JIT: array = EX_VAR(opline->op1.var);
13852
  // JIT: fe_ht = Z_ARRVAL_P(array);
13853
0
  ht_ref = jit_Z_PTR(jit, op1_addr);
13854
13855
0
  if (op1_info & MAY_BE_PACKED_GUARD) {
13856
0
    if (!zend_jit_packed_guard(jit, opline, opline->op1.var, op1_info)) {
13857
0
      return 0;
13858
0
    }
13859
0
  }
13860
13861
  // JIT: pos = Z_FE_POS_P(array);
13862
0
  hash_pos_ref = packed_pos_ref = ir_LOAD_U32(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)));
13863
13864
0
  if (MAY_BE_HASH(op1_info)) {
13865
0
    ir_ref loop_ref, pos2_ref, p2_ref;
13866
13867
0
    if (MAY_BE_PACKED(op1_info)) {
13868
0
      ref = ir_AND_U32(
13869
0
        ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, u.flags))),
13870
0
        ir_CONST_U32(HASH_FLAG_PACKED));
13871
0
      if_packed = ir_IF(ref);
13872
0
      ir_IF_FALSE(if_packed);
13873
0
    }
13874
13875
    // JIT: p = fe_ht->arData + pos;
13876
0
    if (sizeof(void*) == 8) {
13877
0
      ref = ir_ZEXT_A(hash_pos_ref);
13878
0
    } else {
13879
0
      ref = ir_BITCAST_A(hash_pos_ref);
13880
0
    }
13881
0
    hash_p_ref = ir_ADD_A(
13882
0
      ir_MUL_A(ref, ir_CONST_ADDR(sizeof(Bucket))),
13883
0
      ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arData))));
13884
13885
0
    loop_ref = ir_LOOP_BEGIN(ir_END());
13886
0
    hash_pos_ref = ir_PHI_2(IR_U32, hash_pos_ref, IR_UNUSED);
13887
0
    hash_p_ref = ir_PHI_2(IR_ADDR, hash_p_ref, IR_UNUSED);
13888
13889
    // JIT: if (UNEXPECTED(pos >= fe_ht->nNumUsed)) {
13890
0
    ref = ir_ULT(hash_pos_ref,
13891
0
      ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed))));
13892
13893
    // JIT: ZEND_VM_SET_RELATIVE_OPCODE(opline, opline->extended_value);
13894
    // JIT: ZEND_VM_CONTINUE();
13895
13896
0
    if (exit_addr) {
13897
0
      if (exit_opcode == ZEND_JMP) {
13898
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13899
0
      } else {
13900
0
        ir_ref if_fit = ir_IF(ref);
13901
0
        ir_IF_FALSE(if_fit);
13902
0
        ir_END_list(exit_inputs);
13903
0
        ir_IF_TRUE(if_fit);
13904
0
      }
13905
0
    } else {
13906
0
      ir_ref if_fit = ir_IF(ref);
13907
0
      ir_IF_FALSE(if_fit);
13908
0
      ir_END_list(exit_inputs);
13909
0
      ir_IF_TRUE(if_fit);
13910
0
    }
13911
13912
    // JIT: pos++;
13913
0
    pos2_ref = ir_ADD_U32(hash_pos_ref, ir_CONST_U32(1));
13914
13915
    // JIT: value_type = Z_TYPE_INFO_P(value);
13916
    // JIT: if (EXPECTED(value_type != IS_UNDEF)) {
13917
0
    if (!exit_addr || exit_opcode == ZEND_JMP) {
13918
0
      if_def_hash = ir_IF(jit_Z_TYPE_ref(jit, hash_p_ref));
13919
0
      ir_IF_FALSE(if_def_hash);
13920
0
    } else {
13921
0
      ir_GUARD_NOT(jit_Z_TYPE_ref(jit, hash_p_ref), ir_CONST_ADDR(exit_addr));
13922
0
    }
13923
13924
    // JIT: p++;
13925
0
    p2_ref = ir_ADD_OFFSET(hash_p_ref, sizeof(Bucket));
13926
13927
0
    ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
13928
0
    ir_PHI_SET_OP(hash_pos_ref, 2, pos2_ref);
13929
0
    ir_PHI_SET_OP(hash_p_ref, 2, p2_ref);
13930
13931
0
    if (MAY_BE_PACKED(op1_info)) {
13932
0
      ir_IF_TRUE(if_packed);
13933
0
    }
13934
0
  }
13935
0
  if (MAY_BE_PACKED(op1_info)) {
13936
0
    ir_ref loop_ref, pos2_ref, p2_ref;
13937
13938
    // JIT: p = fe_ht->arPacked + pos;
13939
0
    if (sizeof(void*) == 8) {
13940
0
      ref = ir_ZEXT_A(packed_pos_ref);
13941
0
    } else {
13942
0
      ref = ir_BITCAST_A(packed_pos_ref);
13943
0
    }
13944
0
    packed_p_ref = ir_ADD_A(
13945
0
      ir_MUL_A(ref, ir_CONST_ADDR(sizeof(zval))),
13946
0
      ir_LOAD_A(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, arPacked))));
13947
13948
0
    loop_ref = ir_LOOP_BEGIN(ir_END());
13949
0
    packed_pos_ref = ir_PHI_2(IR_U32, packed_pos_ref, IR_UNUSED);
13950
0
    packed_p_ref = ir_PHI_2(IR_ADDR, packed_p_ref, IR_UNUSED);
13951
13952
    // JIT: if (UNEXPECTED(pos >= fe_ht->nNumUsed)) {
13953
0
    ref = ir_ULT(packed_pos_ref,
13954
0
      ir_LOAD_U32(ir_ADD_OFFSET(ht_ref, offsetof(zend_array, nNumUsed))));
13955
13956
    // JIT: ZEND_VM_SET_RELATIVE_OPCODE(opline, opline->extended_value);
13957
    // JIT: ZEND_VM_CONTINUE();
13958
0
    if (exit_addr) {
13959
0
      if (exit_opcode == ZEND_JMP) {
13960
0
        ir_GUARD(ref, ir_CONST_ADDR(exit_addr));
13961
0
      } else {
13962
0
        ir_ref if_fit = ir_IF(ref);
13963
0
        ir_IF_FALSE(if_fit);
13964
0
        ir_END_list(exit_inputs);
13965
0
        ir_IF_TRUE(if_fit);
13966
0
      }
13967
0
    } else {
13968
0
      ir_ref if_fit = ir_IF(ref);
13969
0
      ir_IF_FALSE(if_fit);
13970
0
      ir_END_list(exit_inputs);
13971
0
      ir_IF_TRUE(if_fit);
13972
0
    }
13973
13974
    // JIT: pos++;
13975
0
    pos2_ref = ir_ADD_U32(packed_pos_ref, ir_CONST_U32(1));
13976
13977
    // JIT: value_type = Z_TYPE_INFO_P(value);
13978
    // JIT: if (EXPECTED(value_type != IS_UNDEF)) {
13979
0
    if (!exit_addr || exit_opcode == ZEND_JMP) {
13980
0
      if_def_packed = ir_IF(jit_Z_TYPE_ref(jit, packed_p_ref));
13981
0
      ir_IF_FALSE(if_def_packed);
13982
0
    } else {
13983
0
      ir_GUARD_NOT(jit_Z_TYPE_ref(jit, packed_p_ref), ir_CONST_ADDR(exit_addr));
13984
0
    }
13985
13986
    // JIT: p++;
13987
0
    p2_ref = ir_ADD_OFFSET(packed_p_ref, sizeof(zval));
13988
13989
0
    ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
13990
0
    ir_PHI_SET_OP(packed_pos_ref, 2, pos2_ref);
13991
0
    ir_PHI_SET_OP(packed_p_ref, 2, p2_ref);
13992
0
  }
13993
13994
0
  if (!exit_addr || exit_opcode == ZEND_JMP) {
13995
0
    zend_jit_addr val_addr;
13996
0
    zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->op2.var);
13997
0
    uint32_t val_info;
13998
0
    ir_ref p_ref = IR_UNUSED, hash_path = IR_UNUSED;
13999
14000
0
    if (RETURN_VALUE_USED(opline)) {
14001
0
      zend_jit_addr res_addr = RES_ADDR();
14002
14003
0
      if (MAY_BE_HASH(op1_info)) {
14004
0
        ir_ref key_ref = IR_UNUSED, if_key = IR_UNUSED, key_path = IR_UNUSED;
14005
14006
0
        ZEND_ASSERT(if_def_hash);
14007
0
        ir_IF_TRUE(if_def_hash);
14008
14009
        // JIT: Z_FE_POS_P(array) = pos + 1;
14010
0
        ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14011
0
          ir_ADD_U32(hash_pos_ref, ir_CONST_U32(1)));
14012
14013
0
        if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14014
0
          key_ref = ir_LOAD_A(ir_ADD_OFFSET(hash_p_ref, offsetof(Bucket, key)));
14015
0
        }
14016
0
        if ((op1_info & MAY_BE_ARRAY_KEY_LONG)
14017
0
         && (op1_info & MAY_BE_ARRAY_KEY_STRING)) {
14018
          // JIT: if (!p->key) {
14019
0
          if_key = ir_IF(key_ref);
14020
0
          ir_IF_TRUE(if_key);
14021
0
        }
14022
0
        if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14023
0
          ir_ref if_interned, interned_path;
14024
14025
          // JIT: ZVAL_STR_COPY(EX_VAR(opline->result.var), p->key);
14026
0
          jit_set_Z_PTR(jit, res_addr, key_ref);
14027
0
          ref = ir_AND_U32(
14028
0
            ir_LOAD_U32(ir_ADD_OFFSET(key_ref, offsetof(zend_refcounted, gc.u.type_info))),
14029
0
            ir_CONST_U32(IS_STR_INTERNED));
14030
0
          if_interned = ir_IF(ref);
14031
0
          ir_IF_TRUE(if_interned);
14032
14033
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING);
14034
14035
0
          interned_path = ir_END();
14036
0
          ir_IF_FALSE(if_interned);
14037
14038
0
          jit_GC_ADDREF(jit, key_ref);
14039
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_STRING_EX);
14040
14041
0
          ir_MERGE_WITH(interned_path);
14042
14043
0
          if (op1_info & MAY_BE_ARRAY_KEY_LONG) {
14044
0
            key_path = ir_END();
14045
0
          }
14046
0
        }
14047
0
        if (op1_info & MAY_BE_ARRAY_KEY_LONG) {
14048
0
          if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14049
0
            ir_IF_FALSE(if_key);
14050
0
          }
14051
          // JIT: ZVAL_LONG(EX_VAR(opline->result.var), p->h);
14052
0
          ref = ir_LOAD_L(ir_ADD_OFFSET(hash_p_ref, offsetof(Bucket, h)));
14053
0
          jit_set_Z_LVAL(jit, res_addr, ref);
14054
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
14055
14056
0
          if (op1_info & MAY_BE_ARRAY_KEY_STRING) {
14057
0
            ir_MERGE_WITH(key_path);
14058
0
          }
14059
0
        }
14060
0
        if (MAY_BE_PACKED(op1_info)) {
14061
0
          hash_path = ir_END();
14062
0
        } else {
14063
0
          p_ref = hash_p_ref;
14064
0
        }
14065
0
      }
14066
0
      if (MAY_BE_PACKED(op1_info)) {
14067
0
        ZEND_ASSERT(if_def_packed);
14068
0
        ir_IF_TRUE(if_def_packed);
14069
14070
        // JIT: Z_FE_POS_P(array) = pos + 1;
14071
0
        ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14072
0
          ir_ADD_U32(packed_pos_ref, ir_CONST_U32(1)));
14073
14074
        // JIT: ZVAL_LONG(EX_VAR(opline->result.var), pos);
14075
0
        if (sizeof(zend_long) == 8) {
14076
0
          packed_pos_ref = ir_ZEXT_L(packed_pos_ref);
14077
0
        } else {
14078
0
          packed_pos_ref = ir_BITCAST_L(packed_pos_ref);
14079
0
        }
14080
0
        jit_set_Z_LVAL(jit, res_addr, packed_pos_ref);
14081
0
        jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
14082
14083
0
        if (MAY_BE_HASH(op1_info)) {
14084
0
          ir_MERGE_WITH(hash_path);
14085
0
          p_ref = ir_PHI_2(IR_ADDR, packed_p_ref, hash_p_ref);
14086
0
        } else {
14087
0
          p_ref = packed_p_ref;
14088
0
        }
14089
0
      }
14090
0
    } else {
14091
0
      ir_ref pos_ref = IR_UNUSED;
14092
14093
0
      if (if_def_hash && if_def_packed) {
14094
0
        ir_IF_TRUE(if_def_hash);
14095
0
        ir_MERGE_WITH_EMPTY_TRUE(if_def_packed);
14096
0
        pos_ref = ir_PHI_2(IR_U32, hash_pos_ref, packed_pos_ref);
14097
0
        p_ref = ir_PHI_2(IR_ADDR, hash_p_ref, packed_p_ref);
14098
0
      } else if (if_def_hash) {
14099
0
        ir_IF_TRUE(if_def_hash);
14100
0
        pos_ref = hash_pos_ref;
14101
0
        p_ref = hash_p_ref;
14102
0
      } else if (if_def_packed) {
14103
0
        ir_IF_TRUE(if_def_packed);
14104
0
        pos_ref = packed_pos_ref;
14105
0
        p_ref = packed_p_ref;
14106
0
      } else {
14107
0
        ZEND_UNREACHABLE();
14108
0
      }
14109
14110
      // JIT: Z_FE_POS_P(array) = pos + 1;
14111
0
      ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->op1.var + offsetof(zval, u2.fe_pos)),
14112
0
        ir_ADD_U32(pos_ref, ir_CONST_U32(1)));
14113
0
    }
14114
14115
0
    val_info = ((op1_info & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
14116
0
    if (val_info & MAY_BE_ARRAY) {
14117
0
      val_info |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
14118
0
    }
14119
0
    if (op1_info & MAY_BE_ARRAY_OF_REF) {
14120
0
      val_info |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY |
14121
0
        MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
14122
0
    } else if (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
14123
0
      val_info |= MAY_BE_RC1 | MAY_BE_RCN;
14124
0
    }
14125
14126
0
    val_addr = ZEND_ADDR_REF_ZVAL(p_ref);
14127
0
    if (opline->op2_type == IS_CV) {
14128
      // JIT: zend_assign_to_variable(variable_ptr, value, IS_CV, EX_USES_STRICT_TYPES());
14129
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)) {
14130
0
        return 0;
14131
0
      }
14132
0
    } else {
14133
      // JIT: ZVAL_DEREF(value);
14134
0
      if (val_info & MAY_BE_REF) {
14135
0
        ir_ref ref = jit_ZVAL_ADDR(jit, val_addr);
14136
0
        ref = jit_ZVAL_DEREF_ref(jit, ref);
14137
0
        val_addr = ZEND_ADDR_REF_ZVAL(ref);
14138
0
        val_info &= ~MAY_BE_REF;
14139
0
      }
14140
      // JIT: ZVAL_COPY(res, value);
14141
0
      jit_ZVAL_COPY(jit, var_addr, -1, val_addr, val_info, true);
14142
0
    }
14143
14144
0
    if (!exit_addr) {
14145
0
      zend_basic_block *bb;
14146
14147
0
      ZEND_ASSERT(jit->b >= 0);
14148
0
      bb = &jit->ssa->cfg.blocks[jit->b];
14149
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[1], jit->b, ir_END());
14150
0
      ZEND_ASSERT(exit_inputs);
14151
0
      if (!jit->ctx.ir_base[exit_inputs].op2) {
14152
0
        ref = exit_inputs;
14153
0
      } else {
14154
0
        ir_MERGE_list(exit_inputs);
14155
0
        ref = ir_END();
14156
0
      }
14157
0
      _zend_jit_add_predecessor_ref(jit, bb->successors[0], jit->b, ref);
14158
0
      jit->b = -1;
14159
0
    }
14160
0
  } else {
14161
0
    ZEND_ASSERT(exit_inputs);
14162
0
    ir_MERGE_list(exit_inputs);
14163
0
  }
14164
14165
0
  return 1;
14166
0
}
14167
14168
static int zend_jit_load_this(zend_jit_ctx *jit, uint32_t var)
14169
0
{
14170
0
  zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14171
0
  zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
14172
0
  ir_ref ref = jit_Z_PTR(jit, this_addr);
14173
14174
0
  jit_set_Z_PTR(jit, var_addr, ref);
14175
0
  jit_set_Z_TYPE_INFO(jit, var_addr, IS_OBJECT_EX);
14176
0
  jit_GC_ADDREF(jit, ref);
14177
14178
0
  return 1;
14179
0
}
14180
14181
static int zend_jit_fetch_this(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array, bool check_only)
14182
0
{
14183
0
  if (!op_array->scope ||
14184
0
      (op_array->fn_flags & ZEND_ACC_STATIC) ||
14185
0
      ((op_array->fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_IMMUTABLE)) == ZEND_ACC_CLOSURE)) {
14186
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14187
0
      if (!JIT_G(current_frame) ||
14188
0
          !TRACE_FRAME_IS_THIS_CHECKED(JIT_G(current_frame))) {
14189
14190
0
        zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14191
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14192
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14193
14194
0
        if (!exit_addr) {
14195
0
          return 0;
14196
0
        }
14197
14198
0
        jit_guard_Z_TYPE(jit, this_addr, IS_OBJECT, exit_addr);
14199
14200
0
        if (JIT_G(current_frame)) {
14201
0
          TRACE_FRAME_SET_THIS_CHECKED(JIT_G(current_frame));
14202
0
        }
14203
0
      }
14204
0
    } else {
14205
0
      zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14206
0
      ir_ref if_object = jit_if_Z_TYPE(jit, this_addr, IS_OBJECT);
14207
14208
0
      ir_IF_FALSE_cold(if_object);
14209
0
      jit_SET_EX_OPLINE(jit, opline);
14210
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_invalid_this));
14211
14212
0
      ir_IF_TRUE(if_object);
14213
0
    }
14214
0
  }
14215
14216
0
  if (!check_only) {
14217
0
    if (!zend_jit_load_this(jit, opline->result.var)) {
14218
0
      return 0;
14219
0
    }
14220
0
  }
14221
14222
0
  return 1;
14223
0
}
14224
14225
static int zend_jit_class_guard(zend_jit_ctx *jit, const zend_op *opline, ir_ref obj_ref, zend_class_entry *ce)
14226
0
{
14227
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
14228
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14229
14230
0
  if (!exit_addr) {
14231
0
    return 0;
14232
0
  }
14233
14234
0
  ir_GUARD(ir_EQ(ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce))), ir_CONST_ADDR(ce)),
14235
0
    ir_CONST_ADDR(exit_addr));
14236
14237
0
  return 1;
14238
0
}
14239
14240
static int zend_jit_fetch_obj(zend_jit_ctx         *jit,
14241
                              const zend_op        *opline,
14242
                              const zend_op_array  *op_array,
14243
                              zend_ssa             *ssa,
14244
                              const zend_ssa_op    *ssa_op,
14245
                              uint32_t              op1_info,
14246
                              zend_jit_addr         op1_addr,
14247
                              bool                  op1_indirect,
14248
                              zend_class_entry     *ce,
14249
                              bool                  ce_is_instanceof,
14250
                              bool                  on_this,
14251
                              bool                  delayed_fetch_this,
14252
                              bool                  op1_avoid_refcounting,
14253
                              zend_class_entry     *trace_ce,
14254
                              zend_jit_addr         res_addr,
14255
                              uint8_t               prop_type,
14256
                              int                   may_throw)
14257
0
{
14258
0
  zval *member;
14259
0
  zend_property_info *prop_info;
14260
0
  bool may_be_dynamic = true;
14261
0
  zend_jit_addr prop_addr;
14262
0
  uint32_t res_info = RES_INFO();
14263
0
  ir_ref prop_type_ref = IR_UNUSED;
14264
0
  ir_ref obj_ref = IR_UNUSED;
14265
0
  ir_ref prop_ref = IR_UNUSED;
14266
0
  ir_ref end_inputs = IR_UNUSED;
14267
0
  ir_ref slow_inputs = IR_UNUSED;
14268
0
  ir_ref end_values = IR_UNUSED;
14269
14270
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
14271
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
14272
14273
0
  member = RT_CONSTANT(opline, opline->op2);
14274
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
14275
0
  prop_info = zend_get_known_property_info(op_array, ce, Z_STR_P(member), on_this, op_array->filename);
14276
14277
0
  if (on_this) {
14278
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14279
0
    obj_ref = jit_Z_PTR(jit, this_addr);
14280
0
  } else {
14281
0
    if (opline->op1_type == IS_VAR
14282
0
     && opline->opcode == ZEND_FETCH_OBJ_W
14283
0
     && (op1_info & MAY_BE_INDIRECT)
14284
0
     && Z_REG(op1_addr) == ZREG_FP) {
14285
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
14286
0
    }
14287
0
    if (op1_info & MAY_BE_REF) {
14288
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
14289
0
    }
14290
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
14291
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14292
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14293
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14294
14295
0
        if (!exit_addr) {
14296
0
          return 0;
14297
0
        }
14298
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
14299
0
      } else {
14300
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
14301
14302
0
        ir_IF_FALSE_cold(if_obj);
14303
0
        if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14304
0
          ir_ref op1_ref = IR_UNUSED;
14305
14306
0
          jit_SET_EX_OPLINE(jit, opline);
14307
0
          if (opline->opcode != ZEND_FETCH_OBJ_W && (op1_info & MAY_BE_UNDEF)) {
14308
0
            zend_jit_addr orig_op1_addr = OP1_ADDR();
14309
0
            ir_ref fast_path = IR_UNUSED;
14310
14311
0
            if (op1_info & MAY_BE_ANY) {
14312
0
              ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
14313
0
              ir_IF_TRUE(if_def);
14314
0
              fast_path = ir_END();
14315
0
              ir_IF_FALSE_cold(if_def);
14316
0
            }
14317
0
            ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
14318
0
              ir_CONST_U32(opline->op1.var));
14319
0
            if (fast_path) {
14320
0
              ir_MERGE_WITH(fast_path);
14321
0
            }
14322
0
            op1_ref = jit_ZVAL_ADDR(jit, orig_op1_addr);
14323
0
          } else {
14324
0
            op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
14325
0
          }
14326
0
          if (opline->opcode == ZEND_FETCH_OBJ_W) {
14327
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_write),
14328
0
              op1_ref, ir_CONST_ADDR(Z_STRVAL_P(member)));
14329
0
            jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14330
0
          } else {
14331
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_read),
14332
0
              op1_ref, ir_CONST_ADDR(Z_STRVAL_P(member)));
14333
0
            jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14334
0
          }
14335
0
        } else {
14336
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14337
0
        }
14338
0
        ir_END_list(end_inputs);
14339
14340
0
        ir_IF_TRUE(if_obj);
14341
0
      }
14342
0
    }
14343
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
14344
0
  }
14345
14346
0
  ZEND_ASSERT(obj_ref);
14347
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
14348
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, Z_STR_P(member), on_this, op_array->filename);
14349
0
    if (prop_info) {
14350
0
      ce = trace_ce;
14351
0
      ce_is_instanceof = false;
14352
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
14353
0
        if (on_this && JIT_G(current_frame)
14354
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
14355
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
14356
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
14357
0
          if (on_this && JIT_G(current_frame)) {
14358
0
            JIT_G(current_frame)->ce = ce;
14359
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
14360
0
          }
14361
0
        } else {
14362
0
          return 0;
14363
0
        }
14364
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
14365
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
14366
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
14367
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
14368
0
        }
14369
0
      }
14370
0
    }
14371
0
  }
14372
14373
0
  if (!prop_info) {
14374
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
14375
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
14376
0
    ir_ref if_same = ir_IF(ir_EQ(ref,
14377
0
      ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
14378
14379
0
    ir_IF_FALSE_cold(if_same);
14380
0
    ir_END_list(slow_inputs);
14381
14382
0
    ir_IF_TRUE(if_same);
14383
0
    ir_ref offset_ref = ir_LOAD_A(
14384
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
14385
14386
0
    may_be_dynamic = zend_may_be_dynamic_property(ce, Z_STR_P(member), opline->op1_type == IS_UNUSED, op_array);
14387
0
    if (may_be_dynamic) {
14388
0
      ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
14389
0
      if (opline->opcode == ZEND_FETCH_OBJ_W) {
14390
0
        ir_IF_TRUE_cold(if_dynamic);
14391
0
        ir_END_list(slow_inputs);
14392
0
      } else {
14393
0
        ir_IF_TRUE_cold(if_dynamic);
14394
0
        jit_SET_EX_OPLINE(jit, opline);
14395
14396
0
        if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14397
0
          if (Z_MODE(res_addr) == IS_REG) {
14398
0
            ir_ref val_addr = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_dynamic_ex),
14399
0
              obj_ref, offset_ref);
14400
0
            ir_END_PHI_list(end_values, val_addr);
14401
0
          } else {
14402
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_dynamic),
14403
0
              obj_ref, offset_ref);
14404
0
            ir_END_list(end_inputs);
14405
0
          }
14406
0
        } else {
14407
0
          if (Z_MODE(res_addr) == IS_REG) {
14408
0
            ir_ref val_addr = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_dynamic_ex),
14409
0
              obj_ref, offset_ref);
14410
0
            ir_END_PHI_list(end_values, val_addr);
14411
0
          } else {
14412
0
            ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_dynamic),
14413
0
              obj_ref, offset_ref);
14414
0
            ir_END_list(end_inputs);
14415
0
          }
14416
0
        }
14417
0
      }
14418
0
      ir_IF_FALSE(if_dynamic);
14419
0
    }
14420
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
14421
0
    prop_type_ref = jit_Z_TYPE_ref(jit, prop_ref);
14422
0
    ir_ref if_def = ir_IF(prop_type_ref);
14423
0
    ir_IF_FALSE_cold(if_def);
14424
0
    ir_END_list(slow_inputs);
14425
0
    ir_IF_TRUE(if_def);
14426
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14427
0
    if (opline->opcode == ZEND_FETCH_OBJ_W
14428
0
     && (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT)))) {
14429
0
      uint32_t flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
14430
14431
0
      ir_ref allowed_inputs = IR_UNUSED;
14432
0
      ir_ref forbidden_inputs = IR_UNUSED;
14433
14434
0
      ir_ref prop_info_ref = ir_LOAD_A(
14435
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
14436
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
14437
14438
0
      ir_IF_TRUE_cold(if_has_prop_info);
14439
14440
0
      ir_ref prop_flags = ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, flags)));
14441
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)));
14442
14443
0
      ir_IF_FALSE(if_readonly_or_avis);
14444
0
      ir_END_list(allowed_inputs);
14445
14446
0
      ir_IF_TRUE_cold(if_readonly_or_avis);
14447
14448
0
      ir_ref if_readonly = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY)));
14449
0
      ir_IF_TRUE(if_readonly);
14450
0
      ir_END_list(forbidden_inputs);
14451
14452
0
      ir_IF_FALSE(if_readonly);
14453
0
      ir_ref has_avis_access = ir_CALL_1(IR_BOOL, ir_CONST_FC_FUNC(zend_asymmetric_property_has_set_access), prop_info_ref);
14454
0
      ir_ref if_avis_access = ir_IF(has_avis_access);
14455
0
      ir_IF_TRUE(if_avis_access);
14456
0
      ir_END_list(allowed_inputs);
14457
14458
0
      ir_IF_FALSE(if_avis_access);
14459
0
      ir_END_list(forbidden_inputs);
14460
14461
0
      ir_MERGE_list(forbidden_inputs);
14462
14463
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14464
0
      ir_IF_TRUE(if_prop_obj);
14465
0
      ref = jit_Z_PTR(jit, prop_addr);
14466
0
      jit_GC_ADDREF(jit, ref);
14467
0
      jit_set_Z_PTR(jit, res_addr, ref);
14468
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14469
0
      ir_END_list(end_inputs);
14470
14471
0
      ir_IF_FALSE_cold(if_prop_obj);
14472
14473
0
      jit_SET_EX_OPLINE(jit, opline);
14474
0
      if_readonly = ir_IF(ir_AND_U32(prop_flags, ir_CONST_U32(ZEND_ACC_READONLY)));
14475
0
      ir_IF_TRUE(if_readonly);
14476
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_readonly_property_indirect_modification_error), prop_info_ref);
14477
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14478
0
      ir_END_list(end_inputs);
14479
14480
0
      ir_IF_FALSE(if_readonly);
14481
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_asymmetric_visibility_property_modification_error),
14482
0
        prop_info_ref, ir_CONST_ADDR("indirectly modify"));
14483
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14484
0
      ir_END_list(end_inputs);
14485
14486
0
      ir_MERGE_list(allowed_inputs);
14487
14488
0
      if (flags == ZEND_FETCH_DIM_WRITE) {
14489
0
        jit_SET_EX_OPLINE(jit, opline);
14490
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_array_promotion),
14491
0
          prop_ref, prop_info_ref);
14492
0
        ir_END_list(end_inputs);
14493
0
        ir_IF_FALSE(if_has_prop_info);
14494
0
      } else if (flags == ZEND_FETCH_REF) {
14495
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_create_typed_ref),
14496
0
          prop_ref,
14497
0
          prop_info_ref,
14498
0
          jit_ZVAL_ADDR(jit, res_addr));
14499
0
        ir_END_list(end_inputs);
14500
0
        ir_IF_FALSE(if_has_prop_info);
14501
0
      } else {
14502
0
        ZEND_ASSERT(flags == 0);
14503
0
        ir_MERGE_WITH_EMPTY_FALSE(if_has_prop_info);
14504
0
      }
14505
0
    }
14506
0
  } else {
14507
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
14508
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14509
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14510
0
      if (opline->opcode == ZEND_FETCH_OBJ_W || !(res_info & MAY_BE_GUARD) || !JIT_G(current_frame)) {
14511
        /* perform IS_UNDEF check only after result type guard (during deoptimization) */
14512
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14513
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14514
14515
0
        if (!exit_addr) {
14516
0
          return 0;
14517
0
        }
14518
0
        prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14519
0
        ir_GUARD(prop_type_ref, ir_CONST_ADDR(exit_addr));
14520
0
      }
14521
0
    } else {
14522
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14523
0
      ir_ref if_def = ir_IF(prop_type_ref);
14524
0
      ir_IF_FALSE_cold(if_def);
14525
0
      ir_END_list(slow_inputs);
14526
0
      ir_IF_TRUE(if_def);
14527
0
    }
14528
0
    if (opline->opcode == ZEND_FETCH_OBJ_W && (prop_info->flags & ZEND_ACC_READONLY)) {
14529
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14530
0
      ir_IF_TRUE(if_prop_obj);
14531
0
      ir_ref ref = jit_Z_PTR(jit, prop_addr);
14532
0
      jit_GC_ADDREF(jit, ref);
14533
0
      jit_set_Z_PTR(jit, res_addr, ref);
14534
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14535
0
      ir_END_list(end_inputs);
14536
14537
0
      ir_IF_FALSE_cold(if_prop_obj);
14538
0
      jit_SET_EX_OPLINE(jit, opline);
14539
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_readonly_property_indirect_modification_error), ir_CONST_ADDR(prop_info));
14540
0
      jit_set_Z_TYPE_INFO(jit, res_addr, _IS_ERROR);
14541
0
      ir_END_list(end_inputs);
14542
14543
0
      goto result_fetched;
14544
0
    } else if (opline->opcode == ZEND_FETCH_OBJ_W && (prop_info->flags & ZEND_ACC_PPP_SET_MASK)) {
14545
      /* Readonly properties which are also asymmetric are never mutable indirectly, which is
14546
       * handled by the previous branch. */
14547
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));
14548
14549
0
      ir_ref if_access = ir_IF(has_access);
14550
0
      ir_IF_FALSE_cold(if_access);
14551
14552
0
      ir_ref if_prop_obj = jit_if_Z_TYPE(jit, prop_addr, IS_OBJECT);
14553
0
      ir_IF_TRUE(if_prop_obj);
14554
0
      ir_ref ref = jit_Z_PTR(jit, prop_addr);
14555
0
      jit_GC_ADDREF(jit, ref);
14556
0
      jit_set_Z_PTR(jit, res_addr, ref);
14557
0
      jit_set_Z_TYPE_INFO(jit, res_addr, IS_OBJECT_EX);
14558
0
      ir_END_list(end_inputs);
14559
14560
0
      ir_IF_FALSE_cold(if_prop_obj);
14561
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_asymmetric_visibility_property_modification_error),
14562
0
        ir_CONST_ADDR(prop_info), ir_CONST_ADDR("indirectly modify"));
14563
0
      ir_END_list(end_inputs);
14564
14565
0
      ir_IF_TRUE(if_access);
14566
0
    }
14567
14568
0
    if (opline->opcode == ZEND_FETCH_OBJ_W
14569
0
     && (opline->extended_value & ZEND_FETCH_OBJ_FLAGS)
14570
0
     && ZEND_TYPE_IS_SET(prop_info->type)) {
14571
0
      uint32_t flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
14572
14573
0
      if (flags == ZEND_FETCH_DIM_WRITE) {
14574
0
        if ((ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_ARRAY) == 0) {
14575
0
          if (!prop_type_ref) {
14576
0
            prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14577
0
          }
14578
0
          ir_ref if_null_or_flase = ir_IF(ir_LE(prop_type_ref, ir_CONST_U32(IR_FALSE)));
14579
0
          ir_IF_TRUE_cold(if_null_or_flase);
14580
0
          jit_SET_EX_OPLINE(jit, opline);
14581
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_array_promotion),
14582
0
            prop_ref, ir_CONST_ADDR(prop_info));
14583
0
          ir_END_list(end_inputs);
14584
0
          ir_IF_FALSE(if_null_or_flase);
14585
0
        }
14586
0
      } else if (flags == ZEND_FETCH_REF) {
14587
0
        ir_ref ref;
14588
14589
0
        if (!prop_type_ref) {
14590
0
          prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14591
0
        }
14592
14593
0
        ir_ref if_reference = ir_IF(ir_EQ(prop_type_ref, ir_CONST_U32(IS_REFERENCE_EX)));
14594
0
        ir_IF_FALSE(if_reference);
14595
0
        if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
14596
0
          ref = ir_CONST_ADDR(prop_info);
14597
0
        } else {
14598
0
          int prop_info_offset =
14599
0
            (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
14600
14601
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
14602
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
14603
0
          ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
14604
0
        }
14605
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_create_typed_ref),
14606
0
          prop_ref,
14607
0
          ref,
14608
0
          jit_ZVAL_ADDR(jit, res_addr));
14609
0
        ir_END_list(end_inputs);
14610
0
        ir_IF_TRUE(if_reference);
14611
0
      } else {
14612
0
        ZEND_UNREACHABLE();
14613
0
      }
14614
0
    }
14615
0
  }
14616
14617
0
  if (opline->opcode == ZEND_FETCH_OBJ_W) {
14618
0
    ZEND_ASSERT(prop_ref);
14619
0
    jit_set_Z_PTR(jit, res_addr, prop_ref);
14620
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
14621
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && prop_info) {
14622
0
      ssa->var_info[ssa_op->result_def].indirect_reference = 1;
14623
0
    }
14624
0
    ir_END_list(end_inputs);
14625
0
  } else {
14626
0
    if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame) && prop_info) {
14627
0
      ir_END_PHI_list(end_values, jit_ZVAL_ADDR(jit, prop_addr));
14628
0
    } else if ((res_info & MAY_BE_GUARD) && Z_MODE(res_addr) == IS_REG) {
14629
0
      ir_END_PHI_list(end_values, jit_ZVAL_ADDR(jit, prop_addr));
14630
0
    } else if (Z_MODE(res_addr) == IS_REG) {
14631
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14632
14633
0
      if (!zend_jit_zval_copy_deref_reg(jit, res_addr, res_info & ~MAY_BE_GUARD, prop_addr, prop_type_ref, &end_values)) {
14634
0
        return 0;
14635
0
      }
14636
0
    } else {
14637
0
      prop_type_ref = jit_Z_TYPE_INFO(jit, prop_addr);
14638
14639
0
      if (!zend_jit_zval_copy_deref(jit, res_addr, prop_addr, prop_type_ref)) {
14640
0
        return 0;
14641
0
      }
14642
0
      ir_END_list(end_inputs);
14643
0
    }
14644
0
  }
14645
14646
0
result_fetched:
14647
0
  if (op1_avoid_refcounting) {
14648
0
    SET_STACK_REG(JIT_G(current_frame)->stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
14649
0
  }
14650
14651
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE || !prop_info) {
14652
0
    ir_MERGE_list(slow_inputs);
14653
0
    jit_SET_EX_OPLINE(jit, opline);
14654
14655
0
    op1_info |= MAY_BE_RC1 | MAY_BE_RCN; /* object may be captured/released in magic handler */
14656
0
    if (opline->opcode == ZEND_FETCH_OBJ_W) {
14657
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_w_slow), obj_ref);
14658
0
      ir_END_list(end_inputs);
14659
0
    } else if (opline->opcode != ZEND_FETCH_OBJ_IS) {
14660
0
      if (Z_MODE(res_addr) == IS_REG) {
14661
0
        ir_ref val_ref = ir_CALL_1(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_slow_ex), obj_ref);
14662
0
        ir_END_PHI_list(end_values, val_ref);
14663
0
      } else {
14664
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_r_slow), obj_ref);
14665
0
        ir_END_list(end_inputs);
14666
0
      }
14667
0
    } else {
14668
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_fetch_obj_is_slow), obj_ref);
14669
0
      ir_END_list(end_inputs);
14670
0
    }
14671
0
  }
14672
14673
0
  if (end_values) {
14674
0
    ir_ref val_ref = ir_PHI_list(end_values);
14675
0
    zend_jit_addr val_addr = ZEND_ADDR_REF_ZVAL(val_ref);
14676
0
    bool result_avoid_refcounting = false;
14677
14678
0
    ZEND_ASSERT(opline->opcode == ZEND_FETCH_OBJ_R
14679
0
      || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
14680
0
      || opline->opcode == ZEND_FETCH_OBJ_IS);
14681
0
    ZEND_ASSERT(end_inputs == IR_UNUSED);
14682
0
    if ((res_info & MAY_BE_GUARD) && JIT_G(current_frame)) {
14683
0
      uint8_t type = concrete_type(res_info);
14684
0
      uint32_t flags = ZEND_JIT_EXIT_CHECK_EXCEPTION;
14685
14686
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
14687
0
       && !delayed_fetch_this
14688
0
       && !op1_avoid_refcounting) {
14689
0
        flags |= ZEND_JIT_EXIT_FREE_OP1;
14690
0
      }
14691
14692
0
      if ((opline->result_type & (IS_VAR|IS_TMP_VAR))
14693
0
       && !(flags & ZEND_JIT_EXIT_FREE_OP1)
14694
0
       && (res_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))
14695
0
       && (ssa_op+1)->op1_use == ssa_op->result_def
14696
0
       && zend_jit_may_avoid_refcounting(opline+1, res_info)) {
14697
0
        result_avoid_refcounting = true;
14698
0
        ssa->var_info[ssa_op->result_def].avoid_refcounting = 1;
14699
0
      }
14700
14701
0
      val_addr = zend_jit_guard_fetch_result_type(jit, opline, val_addr, type,
14702
0
        true, flags, op1_avoid_refcounting);
14703
0
      if (!val_addr) {
14704
0
        return 0;
14705
0
      }
14706
14707
0
      res_info &= ~MAY_BE_GUARD;
14708
0
      ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
14709
0
    }
14710
14711
    // ZVAL_COPY
14712
0
    jit_ZVAL_COPY(jit, res_addr, -1, val_addr, res_info, !result_avoid_refcounting);
14713
14714
0
    if (!zend_jit_store_var_if_necessary(jit, opline->result.var, res_addr, res_info)) {
14715
0
      return 0;
14716
0
    }
14717
0
  } else {
14718
0
    ir_MERGE_list(end_inputs);
14719
0
  }
14720
14721
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
14722
0
    if (opline->op1_type == IS_VAR
14723
0
     && opline->opcode == ZEND_FETCH_OBJ_W
14724
0
     && (op1_info & MAY_BE_RC1)) {
14725
0
      zend_jit_addr orig_op1_addr = OP1_ADDR();
14726
0
      ir_ref if_refcounted, ptr, refcount, if_non_zero;
14727
0
      ir_ref merge_inputs = IR_UNUSED;
14728
14729
0
      if_refcounted = jit_if_REFCOUNTED(jit, orig_op1_addr);
14730
0
      ir_IF_FALSE( if_refcounted);
14731
0
      ir_END_list(merge_inputs);
14732
0
      ir_IF_TRUE( if_refcounted);
14733
0
      ptr = jit_Z_PTR(jit, orig_op1_addr);
14734
0
      refcount = jit_GC_DELREF(jit, ptr);
14735
0
      if_non_zero = ir_IF(refcount);
14736
0
      ir_IF_TRUE( if_non_zero);
14737
0
      ir_END_list(merge_inputs);
14738
0
      ir_IF_FALSE( if_non_zero);
14739
0
      jit_SET_EX_OPLINE(jit, opline);
14740
0
      ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_extract_helper), ptr);
14741
0
      ir_END_list(merge_inputs);
14742
0
      ir_MERGE_list(merge_inputs);
14743
0
    } else if (!op1_avoid_refcounting) {
14744
0
      if (on_this) {
14745
0
        op1_info &= ~MAY_BE_RC1;
14746
0
      }
14747
0
      jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
14748
0
    }
14749
0
  }
14750
14751
0
  if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
14752
0
   && prop_info
14753
0
   && (opline->opcode != ZEND_FETCH_OBJ_W ||
14754
0
       !(opline->extended_value & ZEND_FETCH_OBJ_FLAGS) ||
14755
0
       !ZEND_TYPE_IS_SET(prop_info->type))
14756
0
   && (!(opline->op1_type & (IS_VAR|IS_TMP_VAR)) || on_this || op1_indirect)) {
14757
0
    may_throw = 0;
14758
0
  }
14759
14760
0
  if (may_throw) {
14761
0
    if (Z_MODE(res_addr) == IS_REG) {
14762
0
      zend_jit_check_exception_undef_result(jit, opline);
14763
0
    } else {
14764
0
      zend_jit_check_exception(jit);
14765
0
    }
14766
0
  }
14767
14768
0
  return 1;
14769
0
}
14770
14771
static int zend_jit_assign_obj(zend_jit_ctx         *jit,
14772
                               const zend_op        *opline,
14773
                               const zend_op_array  *op_array,
14774
                               zend_ssa             *ssa,
14775
                               const zend_ssa_op    *ssa_op,
14776
                               uint32_t              op1_info,
14777
                               zend_jit_addr         op1_addr,
14778
                               uint32_t              val_info,
14779
                               zend_jit_addr         val_addr,
14780
                               zend_jit_addr         val_def_addr,
14781
                               zend_jit_addr         res_addr,
14782
                               bool                  op1_indirect,
14783
                               zend_class_entry     *ce,
14784
                               bool                  ce_is_instanceof,
14785
                               bool                  on_this,
14786
                               bool                  delayed_fetch_this,
14787
                               zend_class_entry     *trace_ce,
14788
                               uint8_t               prop_type,
14789
                               int                   may_throw)
14790
0
{
14791
0
  zval *member;
14792
0
  zend_string *name;
14793
0
  zend_property_info *prop_info;
14794
0
  zend_jit_addr prop_addr;
14795
0
  ir_ref obj_ref = IR_UNUSED;
14796
0
  ir_ref prop_ref = IR_UNUSED;
14797
0
  ir_ref delayed_end_input = IR_UNUSED;
14798
0
  ir_ref end_inputs = IR_UNUSED;
14799
0
  ir_ref slow_inputs = IR_UNUSED;
14800
0
  uint32_t res_info = RES_INFO();
14801
14802
0
  if (Z_MODE(val_addr) == IS_REG
14803
0
   && Z_LOAD(val_addr)
14804
0
   && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
14805
    /* Force load */
14806
0
    zend_jit_use_reg(jit, val_addr);
14807
0
  }
14808
14809
0
  if (val_addr != val_def_addr && val_def_addr) {
14810
0
    if (!zend_jit_update_regs(jit, (opline+1)->op1.var, val_addr, val_def_addr, val_info)) {
14811
0
      return 0;
14812
0
    }
14813
0
    if (Z_MODE(val_def_addr) == IS_REG && Z_MODE(val_addr) != IS_REG) {
14814
0
      val_addr = val_def_addr;
14815
0
    }
14816
0
  }
14817
14818
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
14819
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
14820
14821
0
  member = RT_CONSTANT(opline, opline->op2);
14822
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
14823
0
  name = Z_STR_P(member);
14824
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
14825
14826
0
  if (on_this) {
14827
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
14828
0
    obj_ref = jit_Z_PTR(jit, this_addr);
14829
0
  } else {
14830
0
    if (opline->op1_type == IS_VAR
14831
0
     && (op1_info & MAY_BE_INDIRECT)
14832
0
     && Z_REG(op1_addr) == ZREG_FP) {
14833
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
14834
0
    }
14835
0
    if (op1_info & MAY_BE_REF) {
14836
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
14837
0
    }
14838
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
14839
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
14840
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14841
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14842
14843
0
        if (!exit_addr) {
14844
0
          return 0;
14845
0
        }
14846
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
14847
0
      } else {
14848
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
14849
0
        ir_IF_FALSE_cold(if_obj);
14850
14851
0
        jit_SET_EX_OPLINE(jit, opline);
14852
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_assign),
14853
0
          jit_ZVAL_ADDR(jit, op1_addr),
14854
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
14855
14856
0
        if (RETURN_VALUE_USED(opline) && Z_MODE(res_addr) != IS_REG) {
14857
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
14858
0
        }
14859
14860
0
        ir_END_list(end_inputs);
14861
14862
0
        ir_IF_TRUE(if_obj);
14863
0
      }
14864
0
    }
14865
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
14866
0
  }
14867
14868
0
  ZEND_ASSERT(obj_ref);
14869
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
14870
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
14871
0
    if (prop_info) {
14872
0
      ce = trace_ce;
14873
0
      ce_is_instanceof = false;
14874
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
14875
0
        if (on_this && JIT_G(current_frame)
14876
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
14877
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
14878
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
14879
0
          if (on_this && JIT_G(current_frame)) {
14880
0
            JIT_G(current_frame)->ce = ce;
14881
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
14882
0
          }
14883
0
        } else {
14884
0
          return 0;
14885
0
        }
14886
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
14887
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
14888
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
14889
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
14890
0
        }
14891
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
14892
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
14893
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
14894
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
14895
0
        }
14896
0
      }
14897
0
    }
14898
0
  }
14899
14900
0
  if (!prop_info) {
14901
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
14902
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
14903
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
14904
14905
0
    ir_IF_FALSE_cold(if_same);
14906
0
    ir_END_list(slow_inputs);
14907
14908
0
    ir_IF_TRUE(if_same);
14909
0
    ir_ref offset_ref = ir_LOAD_A(
14910
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
14911
14912
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
14913
0
    ir_IF_TRUE_cold(if_dynamic);
14914
0
    ir_END_list(slow_inputs);
14915
14916
0
    ir_IF_FALSE(if_dynamic);
14917
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
14918
0
    ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
14919
0
    ir_IF_FALSE_cold(if_def);
14920
0
    ir_END_list(slow_inputs);
14921
14922
0
    ir_IF_TRUE(if_def);
14923
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14924
14925
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
14926
0
      ir_ref arg3, arg4;
14927
0
      ir_ref prop_info_ref = ir_LOAD_A(
14928
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
14929
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
14930
0
      ir_IF_TRUE_cold(if_has_prop_info);
14931
14932
0
      if (Z_MODE(val_addr) == IS_REG) {
14933
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
14934
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
14935
0
          return 0;
14936
0
        }
14937
0
        arg3 = jit_ZVAL_ADDR(jit, real_addr);
14938
0
      } else {
14939
0
        arg3 = jit_ZVAL_ADDR(jit, val_addr);
14940
0
      }
14941
14942
0
      if (!RETURN_VALUE_USED(opline)) {
14943
0
        arg4 = IR_NULL;
14944
0
      } else if (Z_MODE(res_addr) == IS_REG) {
14945
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
14946
0
        arg4 = jit_ZVAL_ADDR(jit, real_addr);
14947
0
      } else {
14948
0
        arg4 = jit_ZVAL_ADDR(jit, res_addr);
14949
0
      }
14950
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
14951
0
      jit_SET_EX_OPLINE(jit, opline);
14952
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_to_typed_prop),
14953
0
        prop_ref,
14954
0
        prop_info_ref,
14955
0
        arg3,
14956
0
        arg4);
14957
14958
0
      if ((opline+1)->op1_type == IS_CONST) {
14959
        // TODO: ???
14960
        // if (Z_TYPE_P(value) == orig_type) {
14961
        // CACHE_PTR_EX(cache_slot + 2, NULL);
14962
0
      }
14963
14964
0
      ir_END_list(end_inputs);
14965
0
      ir_IF_FALSE(if_has_prop_info);
14966
0
    }
14967
0
  } else {
14968
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
14969
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
14970
    /* With the exception of __clone(), readonly assignment always happens on IS_UNDEF, doding
14971
     * the fast path. Thus, the fast path is not useful. */
14972
0
    if (prop_info->flags & ZEND_ACC_READONLY) {
14973
0
      ZEND_ASSERT(slow_inputs == IR_UNUSED);
14974
0
      goto slow_path;
14975
0
    }
14976
14977
    // Undefined property with potential magic __get()/__set() or lazy object
14978
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE && prop_type != IS_UNDEF) {
14979
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
14980
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
14981
14982
0
      if (!exit_addr) {
14983
0
        return 0;
14984
0
      }
14985
0
      ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
14986
0
    } else {
14987
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
14988
0
      ir_IF_FALSE_cold(if_def);
14989
0
      ir_END_list(slow_inputs);
14990
0
      ir_IF_TRUE(if_def);
14991
0
    }
14992
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
14993
0
      ir_ref ref, arg3, arg4;
14994
14995
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
14996
0
      jit_SET_EX_OPLINE(jit, opline);
14997
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
14998
0
        ref = ir_CONST_ADDR(prop_info);
14999
0
      } else {
15000
0
        int prop_info_offset =
15001
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15002
15003
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15004
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15005
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15006
0
      }
15007
0
      if (Z_MODE(val_addr) == IS_REG) {
15008
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15009
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15010
0
          return 0;
15011
0
        }
15012
0
        arg3 = jit_ZVAL_ADDR(jit, real_addr);
15013
0
      } else {
15014
0
        arg3 = jit_ZVAL_ADDR(jit, val_addr);
15015
0
      }
15016
0
      if (!RETURN_VALUE_USED(opline)) {
15017
0
        arg4 = IR_NULL;
15018
0
      } else if (Z_MODE(res_addr) == IS_REG) {
15019
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15020
0
        arg4 = jit_ZVAL_ADDR(jit, real_addr);
15021
0
      } else {
15022
0
        arg4 = jit_ZVAL_ADDR(jit, res_addr);
15023
0
      }
15024
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_to_typed_prop),
15025
0
        prop_ref,
15026
0
        ref,
15027
0
        arg3,
15028
0
        arg4);
15029
15030
0
      ir_END_list(end_inputs);
15031
0
    }
15032
0
  }
15033
15034
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15035
0
    if (Z_MODE(val_addr) != IS_REG
15036
0
     && (res_addr == 0 || Z_MODE(res_addr) != IS_REG)
15037
0
     && opline->result_type == IS_UNUSED) {
15038
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)) {
15039
0
        return 0;
15040
0
      }
15041
0
    } else {
15042
0
      zend_jit_addr real_res_addr;
15043
15044
0
      if (res_addr && Z_MODE(res_addr) == IS_REG) {
15045
0
        real_res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15046
0
      } else {
15047
0
        real_res_addr = res_addr;
15048
0
      }
15049
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)) {
15050
0
        return 0;
15051
0
      }
15052
0
    }
15053
0
    if (end_inputs || slow_inputs) {
15054
0
      if (((opline+1)->op1_type & (IS_VAR|IS_TMP_VAR))
15055
0
       && (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15056
        /* skip FREE_OP_DATA() */
15057
0
        delayed_end_input = ir_END();
15058
0
      } else {
15059
0
        ir_END_list(end_inputs);
15060
0
      }
15061
0
    }
15062
0
  }
15063
15064
0
  if (slow_inputs) {
15065
0
    ir_ref arg3, arg5;
15066
15067
0
    ir_MERGE_list(slow_inputs);
15068
15069
0
slow_path:
15070
0
    jit_SET_EX_OPLINE(jit, opline);
15071
15072
0
    if (Z_MODE(val_addr) == IS_REG) {
15073
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15074
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15075
0
        return 0;
15076
0
      }
15077
0
      arg3 = jit_ZVAL_ADDR(jit, real_addr);
15078
0
    } else {
15079
0
      arg3 = jit_ZVAL_ADDR(jit, val_addr);
15080
0
    }
15081
0
    if (!RETURN_VALUE_USED(opline)) {
15082
0
      arg5 = IR_NULL;
15083
0
    } else if (Z_MODE(res_addr) == IS_REG) {
15084
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15085
0
      arg5 = jit_ZVAL_ADDR(jit, real_addr);
15086
0
    } else {
15087
0
      arg5 = jit_ZVAL_ADDR(jit, res_addr);
15088
0
    }
15089
15090
    // JIT: value = zobj->handlers->write_property(zobj, name, value, CACHE_ADDR(opline->extended_value));
15091
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15092
0
    ir_CALL_5(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_obj_helper),
15093
0
      obj_ref,
15094
0
      ir_CONST_ADDR(name),
15095
0
      arg3,
15096
0
      ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15097
0
      arg5);
15098
15099
0
    ir_END_list(end_inputs);
15100
0
  }
15101
15102
0
  if (end_inputs) {
15103
0
    ir_MERGE_list(end_inputs);
15104
15105
0
    if (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15106
0
      val_info |= MAY_BE_RC1|MAY_BE_RCN;
15107
0
    }
15108
0
    jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, opline);
15109
15110
0
    if (delayed_end_input) {
15111
0
      ir_MERGE_WITH(delayed_end_input);
15112
0
    }
15113
0
  }
15114
15115
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
15116
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
15117
0
  }
15118
15119
0
  if (RETURN_VALUE_USED(opline) && Z_MODE(res_addr) == IS_REG) {
15120
0
    zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15121
0
    if (!zend_jit_load_reg(jit, real_addr, res_addr, res_info)) {
15122
0
      return 0;
15123
0
    }
15124
0
  }
15125
15126
0
  if (may_throw) {
15127
0
    zend_jit_check_exception(jit);
15128
0
  }
15129
15130
0
  return 1;
15131
0
}
15132
15133
static int zend_jit_assign_obj_op(zend_jit_ctx         *jit,
15134
                                  const zend_op        *opline,
15135
                                  const zend_op_array  *op_array,
15136
                                  zend_ssa             *ssa,
15137
                                  const zend_ssa_op    *ssa_op,
15138
                                  uint32_t              op1_info,
15139
                                  zend_jit_addr         op1_addr,
15140
                                  uint32_t              val_info,
15141
                                  zend_jit_addr         val_addr,
15142
                                  zend_ssa_range       *val_range,
15143
                                  bool                  op1_indirect,
15144
                                  zend_class_entry     *ce,
15145
                                  bool                  ce_is_instanceof,
15146
                                  bool                  on_this,
15147
                                  bool                  delayed_fetch_this,
15148
                                  zend_class_entry     *trace_ce,
15149
                                  uint8_t               prop_type)
15150
0
{
15151
0
  zval *member;
15152
0
  zend_string *name;
15153
0
  zend_property_info *prop_info;
15154
0
  zend_jit_addr prop_addr;
15155
0
  bool use_prop_guard = false;
15156
0
  bool may_throw = false;
15157
0
  binary_op_type binary_op = get_binary_op(opline->extended_value);
15158
0
  ir_ref obj_ref = IR_UNUSED;
15159
0
  ir_ref prop_ref = IR_UNUSED;
15160
0
  ir_ref end_inputs = IR_UNUSED;
15161
0
  ir_ref slow_inputs = IR_UNUSED;
15162
15163
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
15164
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
15165
0
  ZEND_ASSERT(opline->result_type == IS_UNUSED);
15166
15167
0
  member = RT_CONSTANT(opline, opline->op2);
15168
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
15169
0
  name = Z_STR_P(member);
15170
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
15171
15172
0
  if (on_this) {
15173
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
15174
0
    obj_ref = jit_Z_PTR(jit, this_addr);
15175
0
  } else {
15176
0
    if (opline->op1_type == IS_VAR
15177
0
     && (op1_info & MAY_BE_INDIRECT)
15178
0
     && Z_REG(op1_addr) == ZREG_FP) {
15179
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
15180
0
    }
15181
0
    if (op1_info & MAY_BE_REF) {
15182
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
15183
0
    }
15184
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
15185
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15186
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15187
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15188
15189
0
        if (!exit_addr) {
15190
0
          return 0;
15191
0
        }
15192
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
15193
0
      } else {
15194
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
15195
0
        ir_IF_FALSE_cold(if_obj);
15196
15197
0
        jit_SET_EX_OPLINE(jit, opline);
15198
0
        ir_CALL_2(IR_VOID,
15199
0
          (op1_info & MAY_BE_UNDEF) ?
15200
0
            ir_CONST_FC_FUNC(zend_jit_invalid_property_assign_op) :
15201
0
            ir_CONST_FC_FUNC(zend_jit_invalid_property_assign),
15202
0
          jit_ZVAL_ADDR(jit, op1_addr),
15203
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
15204
15205
0
        may_throw = true;
15206
15207
0
        ir_END_list(end_inputs);
15208
0
        ir_IF_TRUE(if_obj);
15209
0
      }
15210
0
    }
15211
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
15212
0
  }
15213
15214
0
  ZEND_ASSERT(obj_ref);
15215
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
15216
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
15217
0
    if (prop_info) {
15218
0
      ce = trace_ce;
15219
0
      ce_is_instanceof = false;
15220
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
15221
0
        if (on_this && JIT_G(current_frame)
15222
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
15223
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
15224
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
15225
0
          if (on_this && JIT_G(current_frame)) {
15226
0
            JIT_G(current_frame)->ce = ce;
15227
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
15228
0
          }
15229
0
        } else {
15230
0
          return 0;
15231
0
        }
15232
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
15233
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
15234
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
15235
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
15236
0
        }
15237
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
15238
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
15239
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
15240
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
15241
0
        }
15242
0
      }
15243
0
    }
15244
0
  }
15245
15246
0
  use_prop_guard = (prop_type != IS_UNKNOWN
15247
0
    && prop_type != IS_UNDEF
15248
0
    && prop_type != IS_REFERENCE
15249
0
    && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_OBJECT);
15250
15251
0
  if (Z_MODE(val_addr) == IS_REG
15252
0
   && Z_LOAD(val_addr)
15253
0
   && jit->ra[Z_SSA_VAR(val_addr)].ref == IR_NULL) {
15254
    /* Force load */
15255
0
    zend_jit_use_reg(jit, val_addr);
15256
0
  }
15257
15258
0
  if (!prop_info) {
15259
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15260
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, (opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
15261
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
15262
15263
0
    ir_IF_FALSE_cold(if_same);
15264
0
    ir_END_list(slow_inputs);
15265
15266
0
    ir_IF_TRUE(if_same);
15267
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
15268
0
      ir_ref prop_info_ref = ir_LOAD_A(
15269
0
        ir_ADD_OFFSET(run_time_cache, ((opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
15270
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
15271
0
      ir_IF_TRUE_cold(if_has_prop_info);
15272
0
      ir_END_list(slow_inputs);
15273
15274
0
      ir_IF_FALSE(if_has_prop_info);
15275
0
    }
15276
0
    ir_ref offset_ref = ir_LOAD_A(
15277
0
      ir_ADD_OFFSET(run_time_cache, ((opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
15278
15279
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
15280
0
    ir_IF_TRUE_cold(if_dynamic);
15281
0
    ir_END_list(slow_inputs);
15282
15283
0
    ir_IF_FALSE(if_dynamic);
15284
15285
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
15286
0
    if (!use_prop_guard) {
15287
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
15288
0
      ir_IF_FALSE_cold(if_def);
15289
0
      ir_END_list(slow_inputs);
15290
15291
0
      ir_IF_TRUE(if_def);
15292
0
    }
15293
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15294
0
  } else {
15295
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
15296
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15297
15298
0
    if (ZEND_TYPE_IS_SET(prop_info->type) || !use_prop_guard) {
15299
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15300
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15301
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15302
15303
0
        if (!exit_addr) {
15304
0
          return 0;
15305
0
        }
15306
0
        ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15307
0
      } else {
15308
0
        ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15309
0
        ir_IF_FALSE_cold(if_def);
15310
0
        ir_END_list(slow_inputs);
15311
0
        ir_IF_TRUE(if_def);
15312
0
      }
15313
0
    }
15314
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15315
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref, arg2;
15316
15317
0
      may_throw = true;
15318
15319
0
      jit_SET_EX_OPLINE(jit, opline);
15320
15321
0
      if (Z_MODE(val_addr) == IS_REG) {
15322
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15323
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15324
0
          return 0;
15325
0
        }
15326
0
        arg2 = jit_ZVAL_ADDR(jit, real_addr);
15327
0
      } else {
15328
0
        arg2 = jit_ZVAL_ADDR(jit, val_addr);
15329
0
      }
15330
15331
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15332
0
      ir_IF_FALSE(if_ref);
15333
0
      noref_path = ir_END();
15334
0
      ir_IF_TRUE(if_ref);
15335
15336
0
      reference = jit_Z_PTR(jit, prop_addr);
15337
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15338
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15339
0
      ir_IF_FALSE(if_typed);
15340
0
      ref_path = ir_END();
15341
0
      ir_IF_TRUE_cold(if_typed);
15342
15343
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
15344
0
        reference,
15345
0
        arg2,
15346
0
        ir_CONST_FC_FUNC(binary_op));
15347
15348
0
      ir_END_list(end_inputs);
15349
15350
0
      ir_MERGE_2(noref_path, ref_path);
15351
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15352
0
      prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15353
15354
      // JIT: value = zend_assign_to_typed_prop(prop_info, property_val, value EXECUTE_DATA_CC);
15355
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15356
0
        ref = ir_CONST_ADDR(prop_info);
15357
0
      } else {
15358
0
        int prop_info_offset =
15359
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15360
15361
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15362
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15363
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15364
0
      }
15365
15366
0
      ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_prop),
15367
0
        prop_ref,
15368
0
        ref,
15369
0
        arg2,
15370
0
        ir_CONST_FC_FUNC(binary_op));
15371
15372
0
      ir_END_list(end_inputs);
15373
0
    }
15374
0
  }
15375
15376
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15377
0
    zend_jit_addr var_addr = prop_addr;
15378
0
    uint32_t var_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15379
0
    uint32_t var_def_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15380
15381
0
    if (use_prop_guard) {
15382
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
15383
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15384
0
      if (!exit_addr) {
15385
0
        return 0;
15386
0
      }
15387
15388
0
      jit_guard_Z_TYPE(jit, prop_addr, prop_type, exit_addr);
15389
0
      var_info = (1 << prop_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
15390
0
    }
15391
15392
0
    if (var_info & MAY_BE_REF) {
15393
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref, arg2;
15394
15395
0
      may_throw = true;
15396
15397
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15398
0
      ir_IF_FALSE(if_ref);
15399
0
      noref_path = ir_END();
15400
0
      ir_IF_TRUE(if_ref);
15401
15402
0
      reference = jit_Z_PTR(jit, var_addr);
15403
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15404
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15405
0
      ir_IF_FALSE(if_typed);
15406
0
      ref_path = ir_END();
15407
0
      ir_IF_TRUE_cold(if_typed);
15408
15409
0
      jit_SET_EX_OPLINE(jit, opline);
15410
15411
0
      if (Z_MODE(val_addr) == IS_REG) {
15412
0
        zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15413
0
        if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15414
0
          return 0;
15415
0
        }
15416
0
        arg2 = jit_ZVAL_ADDR(jit, real_addr);
15417
0
      } else {
15418
0
        arg2 = jit_ZVAL_ADDR(jit, val_addr);
15419
0
      }
15420
0
      ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_op_to_typed_ref),
15421
0
        reference,
15422
0
        arg2,
15423
0
        ir_CONST_FC_FUNC(binary_op));
15424
15425
0
      ir_END_list(end_inputs);
15426
15427
0
      ir_MERGE_2(noref_path, ref_path);
15428
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15429
0
      var_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15430
15431
0
      var_info &= ~MAY_BE_REF;
15432
0
    }
15433
15434
0
    uint8_t val_op_type = (opline+1)->op1_type;
15435
0
    if (val_op_type & (IS_TMP_VAR|IS_VAR)) {
15436
      /* prevent FREE_OP in the helpers */
15437
0
      val_op_type = IS_CV;
15438
0
    }
15439
15440
0
    switch (opline->extended_value) {
15441
0
      case ZEND_ADD:
15442
0
      case ZEND_SUB:
15443
0
      case ZEND_MUL:
15444
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15445
0
            (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15446
0
          if (opline->extended_value != ZEND_ADD ||
15447
0
              (var_info & MAY_BE_ANY) != MAY_BE_ARRAY ||
15448
0
              (val_info & MAY_BE_ANY) == MAY_BE_ARRAY) {
15449
0
            may_throw = true;
15450
0
          }
15451
0
        }
15452
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,
15453
0
            1 /* may overflow */, 0)) {
15454
0
          return 0;
15455
0
        }
15456
0
        break;
15457
0
      case ZEND_BW_OR:
15458
0
      case ZEND_BW_AND:
15459
0
      case ZEND_BW_XOR:
15460
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15461
0
            (val_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15462
0
          if ((var_info & MAY_BE_ANY) != MAY_BE_STRING ||
15463
0
              (val_info & MAY_BE_ANY) != MAY_BE_STRING) {
15464
0
            may_throw = true;
15465
0
          }
15466
0
        }
15467
0
        goto long_math;
15468
0
      case ZEND_SL:
15469
0
      case ZEND_SR:
15470
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15471
0
            (val_info & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15472
0
          may_throw = true;
15473
0
        }
15474
0
        if (val_op_type != IS_CONST ||
15475
0
            Z_TYPE_P(RT_CONSTANT((opline+1), (opline+1)->op1)) != IS_LONG ||
15476
0
            Z_LVAL_P(RT_CONSTANT((opline+1), (opline+1)->op1)) < 0) {
15477
0
          may_throw = true;
15478
0
        }
15479
0
        goto long_math;
15480
0
      case ZEND_MOD:
15481
0
        if ((var_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
15482
0
            (val_info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
15483
0
          may_throw = true;
15484
0
        }
15485
0
        if (val_op_type != IS_CONST ||
15486
0
            Z_TYPE_P(RT_CONSTANT((opline+1), (opline+1)->op1)) != IS_LONG ||
15487
0
            Z_LVAL_P(RT_CONSTANT((opline+1), (opline+1)->op1)) == 0) {
15488
0
          may_throw = true;
15489
0
        }
15490
0
long_math:
15491
0
        if (!zend_jit_long_math_helper(jit, opline, opline->extended_value,
15492
0
            IS_CV, opline->op1, var_addr, var_info, NULL,
15493
0
            val_op_type, (opline+1)->op1, val_addr, val_info,
15494
0
            val_range,
15495
0
            0, var_addr, var_def_info, var_info, /* may throw */ 1)) {
15496
0
          return 0;
15497
0
        }
15498
0
        break;
15499
0
      case ZEND_CONCAT:
15500
0
        may_throw = true;
15501
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,
15502
0
            0)) {
15503
0
          return 0;
15504
0
        }
15505
0
        break;
15506
0
      default:
15507
0
        ZEND_UNREACHABLE();
15508
0
    }
15509
0
    if (end_inputs || slow_inputs) {
15510
0
      ir_END_list(end_inputs);
15511
0
    }
15512
0
  }
15513
15514
0
  if (slow_inputs) {
15515
0
    ir_ref arg3;
15516
15517
0
    ir_MERGE_list(slow_inputs);
15518
15519
0
    may_throw = true;
15520
15521
0
    if (Z_MODE(val_addr) == IS_REG) {
15522
0
      zend_jit_addr real_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (opline+1)->op1.var);
15523
0
      if (!zend_jit_spill_store_inv(jit, val_addr, real_addr, val_info)) {
15524
0
        return 0;
15525
0
      }
15526
0
      arg3 = jit_ZVAL_ADDR(jit, real_addr);
15527
0
    } else {
15528
0
      arg3 = jit_ZVAL_ADDR(jit, val_addr);
15529
0
    }
15530
0
    jit_SET_EX_OPLINE(jit, opline);
15531
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15532
0
    ir_CALL_5(IR_VOID, ir_CONST_FC_FUNC(zend_jit_assign_obj_op_helper),
15533
0
      obj_ref,
15534
0
      ir_CONST_ADDR(name),
15535
0
      arg3,
15536
0
      ir_ADD_OFFSET(run_time_cache, (opline+1)->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
15537
0
      ir_CONST_FC_FUNC(binary_op));
15538
15539
0
    ir_END_list(end_inputs);
15540
0
  }
15541
15542
0
  if (end_inputs) {
15543
0
    ir_MERGE_list(end_inputs);
15544
0
  }
15545
15546
0
  if (val_info & (MAY_BE_REF|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15547
0
    val_info |= MAY_BE_RC1|MAY_BE_RCN;
15548
0
  }
15549
15550
  // JIT: FREE_OP_DATA();
15551
0
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, opline);
15552
15553
0
  if (opline->op1_type != IS_UNUSED && !delayed_fetch_this && !op1_indirect) {
15554
0
    if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
15555
0
      may_throw = true;
15556
0
    }
15557
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
15558
0
  }
15559
15560
0
  if (may_throw) {
15561
0
    zend_jit_check_exception(jit);
15562
0
  }
15563
15564
0
  return 1;
15565
0
}
15566
15567
static int zend_jit_incdec_obj(zend_jit_ctx         *jit,
15568
                               const zend_op        *opline,
15569
                               const zend_op_array  *op_array,
15570
                               zend_ssa             *ssa,
15571
                               const zend_ssa_op    *ssa_op,
15572
                               uint32_t              op1_info,
15573
                               zend_jit_addr         op1_addr,
15574
                               bool                  op1_indirect,
15575
                               zend_class_entry     *ce,
15576
                               bool                  ce_is_instanceof,
15577
                               bool                  on_this,
15578
                               bool                  delayed_fetch_this,
15579
                               zend_class_entry     *trace_ce,
15580
                               uint8_t               prop_type)
15581
0
{
15582
0
  zval *member;
15583
0
  zend_string *name;
15584
0
  zend_property_info *prop_info;
15585
0
  zend_jit_addr res_addr = 0;
15586
0
  zend_jit_addr prop_addr;
15587
0
  bool use_prop_guard = false;
15588
0
  bool may_throw = false;
15589
0
  uint32_t res_info = (opline->result_type != IS_UNDEF) ? RES_INFO() : 0;
15590
0
  ir_ref obj_ref = IR_UNUSED;
15591
0
  ir_ref prop_ref = IR_UNUSED;
15592
0
  ir_ref end_inputs = IR_UNUSED;
15593
0
  ir_ref slow_inputs = IR_UNUSED;
15594
15595
0
  ZEND_ASSERT(opline->op2_type == IS_CONST);
15596
0
  ZEND_ASSERT(op1_info & MAY_BE_OBJECT);
15597
15598
0
  if (opline->result_type != IS_UNUSED) {
15599
0
    res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);
15600
0
  }
15601
15602
0
  member = RT_CONSTANT(opline, opline->op2);
15603
0
  ZEND_ASSERT(Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] != '\0');
15604
0
  name = Z_STR_P(member);
15605
0
  prop_info = zend_get_known_property_info(op_array, ce, name, on_this, op_array->filename);
15606
15607
0
  if (on_this) {
15608
0
    zend_jit_addr this_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, offsetof(zend_execute_data, This));
15609
0
    obj_ref = jit_Z_PTR(jit, this_addr);
15610
0
  } else {
15611
0
    if (opline->op1_type == IS_VAR
15612
0
     && (op1_info & MAY_BE_INDIRECT)
15613
0
     && Z_REG(op1_addr) == ZREG_FP) {
15614
0
      op1_addr = jit_ZVAL_INDIRECT_DEREF(jit, op1_addr);
15615
0
    }
15616
0
    if (op1_info & MAY_BE_REF) {
15617
0
      op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
15618
0
    }
15619
0
    if (op1_info & ((MAY_BE_UNDEF|MAY_BE_ANY)- MAY_BE_OBJECT)) {
15620
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15621
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15622
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15623
15624
0
        if (!exit_addr) {
15625
0
          return 0;
15626
0
        }
15627
0
        jit_guard_Z_TYPE(jit, op1_addr, IS_OBJECT, exit_addr);
15628
0
      } else {
15629
0
        ir_ref if_obj = jit_if_Z_TYPE(jit, op1_addr, IS_OBJECT);
15630
0
        ir_IF_FALSE_cold(if_obj);
15631
15632
0
        jit_SET_EX_OPLINE(jit, opline);
15633
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_invalid_property_incdec),
15634
0
          jit_ZVAL_ADDR(jit, op1_addr),
15635
0
          ir_CONST_ADDR(ZSTR_VAL(name)));
15636
15637
0
        ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler));
15638
0
        ir_IF_TRUE(if_obj);
15639
0
      }
15640
0
    }
15641
0
    obj_ref = jit_Z_PTR(jit, op1_addr);
15642
0
  }
15643
15644
0
  ZEND_ASSERT(obj_ref);
15645
0
  if (!prop_info && trace_ce && (trace_ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
15646
0
    prop_info = zend_get_known_property_info(op_array, trace_ce, name, on_this, op_array->filename);
15647
0
    if (prop_info) {
15648
0
      ce = trace_ce;
15649
0
      ce_is_instanceof = false;
15650
0
      if (!(op1_info & MAY_BE_CLASS_GUARD)) {
15651
0
        if (on_this && JIT_G(current_frame)
15652
0
         && TRACE_FRAME_IS_THIS_CLASS_CHECKED(JIT_G(current_frame))) {
15653
0
          ZEND_ASSERT(JIT_G(current_frame)->ce == ce);
15654
0
        } else if (zend_jit_class_guard(jit, opline, obj_ref, ce)) {
15655
0
          if (on_this && JIT_G(current_frame)) {
15656
0
            JIT_G(current_frame)->ce = ce;
15657
0
            TRACE_FRAME_SET_THIS_CLASS_CHECKED(JIT_G(current_frame));
15658
0
          }
15659
0
        } else {
15660
0
          return 0;
15661
0
        }
15662
0
        if (ssa->var_info && ssa_op->op1_use >= 0) {
15663
0
          ssa->var_info[ssa_op->op1_use].type |= MAY_BE_CLASS_GUARD;
15664
0
          ssa->var_info[ssa_op->op1_use].ce = ce;
15665
0
          ssa->var_info[ssa_op->op1_use].is_instanceof = ce_is_instanceof;
15666
0
        }
15667
0
        if (ssa->var_info && ssa_op->op1_def >= 0) {
15668
0
          ssa->var_info[ssa_op->op1_def].type |= MAY_BE_CLASS_GUARD;
15669
0
          ssa->var_info[ssa_op->op1_def].ce = ce;
15670
0
          ssa->var_info[ssa_op->op1_def].is_instanceof = ce_is_instanceof;
15671
0
        }
15672
0
      }
15673
0
    }
15674
0
  }
15675
15676
0
  use_prop_guard = (prop_type != IS_UNKNOWN
15677
0
    && prop_type != IS_UNDEF
15678
0
    && prop_type != IS_REFERENCE
15679
0
    && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_OBJECT);
15680
15681
0
  if (!prop_info) {
15682
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
15683
0
    ir_ref ref = ir_LOAD_A(ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS));
15684
0
    ir_ref if_same = ir_IF(ir_EQ(ref, ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)))));
15685
15686
0
    ir_IF_FALSE_cold(if_same);
15687
0
    ir_END_list(slow_inputs);
15688
15689
0
    ir_IF_TRUE(if_same);
15690
0
    if (!ce || ce_is_instanceof || (ce->ce_flags & (ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_TRAIT))) {
15691
0
      ir_ref prop_info_ref = ir_LOAD_A(
15692
0
        ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) * 2));
15693
0
      ir_ref if_has_prop_info = ir_IF(prop_info_ref);
15694
0
      ir_IF_TRUE_cold(if_has_prop_info);
15695
0
      ir_END_list(slow_inputs);
15696
15697
0
      ir_IF_FALSE(if_has_prop_info);
15698
0
    }
15699
0
    ir_ref offset_ref = ir_LOAD_A(
15700
0
      ir_ADD_OFFSET(run_time_cache, (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*)));
15701
15702
0
    ir_ref if_dynamic = ir_IF(ir_LT(offset_ref, ir_CONST_ADDR(ZEND_FIRST_PROPERTY_OFFSET)));
15703
0
    ir_IF_TRUE_cold(if_dynamic);
15704
0
    ir_END_list(slow_inputs);
15705
15706
0
    ir_IF_FALSE(if_dynamic);
15707
15708
0
    prop_ref = ir_ADD_A(obj_ref, offset_ref);
15709
0
    if (!use_prop_guard) {
15710
0
      ir_ref if_def = ir_IF(jit_Z_TYPE_ref(jit, prop_ref));
15711
0
      ir_IF_FALSE_cold(if_def);
15712
0
      ir_END_list(slow_inputs);
15713
15714
0
      ir_IF_TRUE(if_def);
15715
0
    }
15716
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15717
0
  } else {
15718
0
    prop_ref = ir_ADD_OFFSET(obj_ref, prop_info->offset);
15719
0
    prop_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15720
15721
0
    if (ZEND_TYPE_IS_SET(prop_info->type) || !use_prop_guard) {
15722
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
15723
0
        int32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
15724
0
        const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15725
15726
0
        if (!exit_addr) {
15727
0
          return 0;
15728
0
        }
15729
0
        ir_GUARD(jit_Z_TYPE_INFO(jit, prop_addr), ir_CONST_ADDR(exit_addr));
15730
0
      } else {
15731
0
        ir_ref if_def = ir_IF(jit_Z_TYPE_INFO(jit, prop_addr));
15732
0
        ir_IF_FALSE_cold(if_def);
15733
0
        ir_END_list(slow_inputs);
15734
0
        ir_IF_TRUE(if_def);
15735
0
      }
15736
0
    }
15737
15738
0
    if (ZEND_TYPE_IS_SET(prop_info->type)) {
15739
0
      const void *func;
15740
0
      ir_ref ref;
15741
15742
0
      may_throw = true;
15743
0
      jit_SET_EX_OPLINE(jit, opline);
15744
15745
0
      if (ce && ce->ce_flags & ZEND_ACC_IMMUTABLE) {
15746
0
        ref = ir_CONST_ADDR(prop_info);
15747
0
      } else {
15748
0
        int prop_info_offset =
15749
0
          (((prop_info->offset - (sizeof(zend_object) - sizeof(zval))) / sizeof(zval)) * sizeof(void*));
15750
15751
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(obj_ref, offsetof(zend_object, ce)));
15752
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, offsetof(zend_class_entry, properties_info_table)));
15753
0
        ref = ir_LOAD_A(ir_ADD_OFFSET(ref, prop_info_offset));
15754
0
      }
15755
15756
0
      if (opline->result_type == IS_UNUSED) {
15757
0
        switch (opline->opcode) {
15758
0
          case ZEND_PRE_INC_OBJ:
15759
0
          case ZEND_POST_INC_OBJ:
15760
0
            func = zend_jit_inc_typed_prop;
15761
0
            break;
15762
0
          case ZEND_PRE_DEC_OBJ:
15763
0
          case ZEND_POST_DEC_OBJ:
15764
0
            func = zend_jit_dec_typed_prop;
15765
0
            break;
15766
0
          default:
15767
0
            ZEND_UNREACHABLE();
15768
0
        }
15769
15770
0
        ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(func), prop_ref, ref);
15771
0
      } else {
15772
0
        switch (opline->opcode) {
15773
0
          case ZEND_PRE_INC_OBJ:
15774
0
            func = zend_jit_pre_inc_typed_prop;
15775
0
            break;
15776
0
          case ZEND_PRE_DEC_OBJ:
15777
0
            func = zend_jit_pre_dec_typed_prop;
15778
0
            break;
15779
0
          case ZEND_POST_INC_OBJ:
15780
0
            func = zend_jit_post_inc_typed_prop;
15781
0
            break;
15782
0
          case ZEND_POST_DEC_OBJ:
15783
0
            func = zend_jit_post_dec_typed_prop;
15784
0
            break;
15785
0
          default:
15786
0
            ZEND_UNREACHABLE();
15787
0
        }
15788
0
        ir_CALL_3(IR_VOID, ir_CONST_FC_FUNC(func),
15789
0
          prop_ref,
15790
0
          ref,
15791
0
          jit_ZVAL_ADDR(jit, res_addr));
15792
0
      }
15793
0
      ir_END_list(end_inputs);
15794
0
    }
15795
0
  }
15796
15797
0
  if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
15798
0
    uint32_t var_info = MAY_BE_ANY|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
15799
0
    zend_jit_addr var_addr = prop_addr;
15800
0
    ir_ref if_long = IR_UNUSED;
15801
0
    ir_ref if_overflow = IR_UNUSED;
15802
15803
0
    if (use_prop_guard) {
15804
0
      int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
15805
0
      const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15806
0
      if (!exit_addr) {
15807
0
        return 0;
15808
0
      }
15809
15810
0
      jit_guard_Z_TYPE(jit, prop_addr, prop_type, exit_addr);
15811
0
      var_info = (1 << prop_type) | (var_info & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF));
15812
0
    }
15813
15814
0
    if (var_info & MAY_BE_REF) {
15815
0
      const void *func;
15816
0
      ir_ref if_ref, if_typed, noref_path, ref_path, reference, ref;
15817
15818
0
      if_ref = jit_if_Z_TYPE(jit, prop_addr, IS_REFERENCE);
15819
0
      ir_IF_FALSE(if_ref);
15820
0
      noref_path = ir_END();
15821
0
      ir_IF_TRUE(if_ref);
15822
15823
0
      reference = jit_Z_PTR(jit, var_addr);
15824
0
      ref = ir_ADD_OFFSET(reference, offsetof(zend_reference, val));
15825
0
      if_typed = jit_if_TYPED_REF(jit, reference);
15826
0
      ir_IF_FALSE(if_typed);
15827
0
      ref_path = ir_END();
15828
0
      ir_IF_TRUE_cold(if_typed);
15829
15830
0
      switch (opline->opcode) {
15831
0
        case ZEND_PRE_INC_OBJ:
15832
0
          func = zend_jit_pre_inc_typed_ref;
15833
0
          break;
15834
0
        case ZEND_PRE_DEC_OBJ:
15835
0
          func = zend_jit_pre_dec_typed_ref;
15836
0
          break;
15837
0
        case ZEND_POST_INC_OBJ:
15838
0
          func = zend_jit_post_inc_typed_ref;
15839
0
          break;
15840
0
        case ZEND_POST_DEC_OBJ:
15841
0
          func = zend_jit_post_dec_typed_ref;
15842
0
          break;
15843
0
        default:
15844
0
          ZEND_UNREACHABLE();
15845
0
      }
15846
15847
0
      may_throw = true;
15848
0
      jit_SET_EX_OPLINE(jit, opline);
15849
0
      ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(func),
15850
0
        reference,
15851
0
        (opline->result_type == IS_UNUSED) ? IR_NULL : jit_ZVAL_ADDR(jit, res_addr));
15852
15853
0
      ir_END_list(end_inputs);
15854
15855
0
      ir_MERGE_2(noref_path, ref_path);
15856
0
      prop_ref = ir_PHI_2(IR_ADDR, prop_ref, ref);
15857
0
      var_addr = ZEND_ADDR_REF_ZVAL(prop_ref);
15858
15859
0
      var_info &= ~MAY_BE_REF;
15860
0
    }
15861
15862
0
    if (var_info & MAY_BE_LONG) {
15863
0
      ir_ref addr, ref;
15864
15865
0
      if (var_info & (MAY_BE_ANY - MAY_BE_LONG)) {
15866
0
        if_long = jit_if_Z_TYPE(jit, var_addr, IS_LONG);
15867
0
        ir_IF_TRUE(if_long);
15868
0
      }
15869
15870
0
      addr = jit_ZVAL_ADDR(jit, var_addr);
15871
0
      ref = ir_LOAD_L(addr);
15872
0
      if (opline->opcode == ZEND_POST_INC_OBJ || opline->opcode == ZEND_POST_DEC_OBJ) {
15873
0
        if (opline->result_type != IS_UNUSED) {
15874
0
          jit_set_Z_LVAL(jit, res_addr, ref);
15875
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
15876
0
        }
15877
0
      }
15878
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15879
0
        ref = ir_ADD_OV_L(ref, ir_CONST_LONG(1));
15880
0
      } else {
15881
0
        ref = ir_SUB_OV_L(ref, ir_CONST_LONG(1));
15882
0
      }
15883
15884
0
      ir_STORE(addr, ref);
15885
0
      if_overflow = ir_IF(ir_OVERFLOW(ref));
15886
0
      ir_IF_FALSE(if_overflow);
15887
15888
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ) {
15889
0
        if (opline->result_type != IS_UNUSED) {
15890
0
          jit_set_Z_LVAL(jit, res_addr, ref);
15891
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_LONG);
15892
0
        }
15893
0
      }
15894
0
      ir_END_list(end_inputs);
15895
0
    }
15896
15897
0
    if (var_info & (MAY_BE_ANY - MAY_BE_LONG)) {
15898
0
      if (var_info & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
15899
0
        may_throw = true;
15900
0
      }
15901
0
      if (if_long) {
15902
0
        ir_IF_FALSE_cold(if_long);
15903
0
      }
15904
0
      if (opline->opcode == ZEND_POST_INC_OBJ || opline->opcode == ZEND_POST_DEC_OBJ) {
15905
0
        jit_ZVAL_COPY(jit, res_addr, -1, var_addr, var_info, true);
15906
0
      }
15907
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15908
0
        if (opline->opcode == ZEND_PRE_INC_OBJ && opline->result_type != IS_UNUSED) {
15909
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_inc),
15910
0
            jit_ZVAL_ADDR(jit, var_addr),
15911
0
            jit_ZVAL_ADDR(jit, res_addr));
15912
0
        } else {
15913
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(increment_function),
15914
0
            jit_ZVAL_ADDR(jit, var_addr));
15915
0
        }
15916
0
      } else {
15917
0
        if (opline->opcode == ZEND_PRE_DEC_OBJ && opline->result_type != IS_UNUSED) {
15918
0
          ir_CALL_2(IR_VOID, ir_CONST_FC_FUNC(zend_jit_pre_dec),
15919
0
            jit_ZVAL_ADDR(jit, var_addr),
15920
0
            jit_ZVAL_ADDR(jit, res_addr));
15921
0
        } else {
15922
0
          ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(decrement_function),
15923
0
            jit_ZVAL_ADDR(jit, var_addr));
15924
0
        }
15925
0
      }
15926
15927
0
      ir_END_list(end_inputs);
15928
0
    }
15929
0
    if (var_info & MAY_BE_LONG) {
15930
0
      ir_IF_TRUE_cold(if_overflow);
15931
0
      if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_POST_INC_OBJ) {
15932
#if SIZEOF_ZEND_LONG == 4
15933
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0));
15934
        jit_set_Z_W2(jit, var_addr, ir_CONST_U32(0x41e00000));
15935
#else
15936
0
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0x43e0000000000000));
15937
0
#endif
15938
0
        jit_set_Z_TYPE_INFO(jit, var_addr, IS_DOUBLE);
15939
0
        if (opline->opcode == ZEND_PRE_INC_OBJ && opline->result_type != IS_UNUSED) {
15940
#if SIZEOF_ZEND_LONG == 4
15941
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0));
15942
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0x41e00000));
15943
#else
15944
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x43e0000000000000));
15945
0
#endif
15946
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
15947
0
        }
15948
0
      } else {
15949
#if SIZEOF_ZEND_LONG == 4
15950
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0x00200000));
15951
        jit_set_Z_W2(jit, var_addr, ir_CONST_U32(0xc1e00000));
15952
#else
15953
0
        jit_set_Z_LVAL(jit, var_addr, ir_CONST_LONG(0xc3e0000000000000));
15954
0
#endif
15955
0
        jit_set_Z_TYPE_INFO(jit, var_addr, IS_DOUBLE);
15956
0
        if (opline->opcode == ZEND_PRE_DEC_OBJ && opline->result_type != IS_UNUSED) {
15957
#if SIZEOF_ZEND_LONG == 4
15958
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0x00200000));
15959
          jit_set_Z_W2(jit, res_addr, ir_CONST_U32(0xc1e00000));
15960
#else
15961
0
          jit_set_Z_LVAL(jit, res_addr, ir_CONST_LONG(0xc3e0000000000000));
15962
0
#endif
15963
0
          jit_set_Z_TYPE_INFO(jit, res_addr, IS_DOUBLE);
15964
0
        }
15965
0
      }
15966
0
      if (opline->result_type != IS_UNUSED
15967
0
       && (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ)
15968
0
       && prop_info
15969
0
       && !ZEND_TYPE_IS_SET(prop_info->type)
15970
0
       && (res_info & MAY_BE_GUARD)
15971
0
       && (res_info & MAY_BE_LONG)) {
15972
0
        zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
15973
0
        uint32_t old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
15974
0
        int32_t exit_point;
15975
0
        const void *exit_addr;
15976
15977
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_DOUBLE, 0);
15978
0
        exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
15979
0
        exit_addr = zend_jit_trace_get_exit_addr(exit_point);
15980
0
        if (!exit_addr) {
15981
0
          return 0;
15982
0
        }
15983
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
15984
0
        ssa->var_info[ssa_op->result_def].type = res_info & ~MAY_BE_GUARD;
15985
0
        jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
15986
0
      } else {
15987
0
        ir_END_list(end_inputs);
15988
0
      }
15989
0
    }
15990
0
  }
15991
15992
0
  if (slow_inputs) {
15993
0
    const void *func;
15994
15995
0
    ir_MERGE_list(slow_inputs);
15996
15997
    // JIT: zend_jit_pre_inc_obj_helper(zobj, name, CACHE_ADDR(opline->extended_value), result);
15998
0
    switch (opline->opcode) {
15999
0
      case ZEND_PRE_INC_OBJ:
16000
0
        func = zend_jit_pre_inc_obj_helper;
16001
0
        break;
16002
0
      case ZEND_PRE_DEC_OBJ:
16003
0
        func = zend_jit_pre_dec_obj_helper;
16004
0
        break;
16005
0
      case ZEND_POST_INC_OBJ:
16006
0
        func = zend_jit_post_inc_obj_helper;
16007
0
        break;
16008
0
      case ZEND_POST_DEC_OBJ:
16009
0
        func = zend_jit_post_dec_obj_helper;
16010
0
        break;
16011
0
      default:
16012
0
        ZEND_UNREACHABLE();
16013
0
    }
16014
16015
0
    may_throw = true;
16016
0
    jit_SET_EX_OPLINE(jit, opline);
16017
0
    ir_ref run_time_cache = ir_LOAD_A(jit_EX(run_time_cache));
16018
0
    ir_CALL_4(IR_VOID, ir_CONST_FC_FUNC(func),
16019
0
      obj_ref,
16020
0
      ir_CONST_ADDR(name),
16021
0
      ir_ADD_OFFSET(run_time_cache, opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS),
16022
0
      (opline->result_type == IS_UNUSED) ? IR_NULL : jit_ZVAL_ADDR(jit, res_addr));
16023
16024
0
    ir_END_list(end_inputs);
16025
0
  }
16026
16027
0
  if (end_inputs) {
16028
0
    ir_MERGE_list(end_inputs);
16029
0
  }
16030
16031
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !delayed_fetch_this && !op1_indirect) {
16032
0
    if ((op1_info & MAY_HAVE_DTOR) && (op1_info & MAY_BE_RC1)) {
16033
0
      may_throw = true;
16034
0
    }
16035
0
    jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, opline);
16036
0
  }
16037
16038
0
  if (may_throw) {
16039
0
    zend_jit_check_exception(jit);
16040
0
  }
16041
16042
0
  return 1;
16043
0
}
16044
16045
static int zend_jit_fetch_static_prop(zend_jit_ctx *jit, const zend_op *opline, const zend_op_array *op_array)
16046
0
{
16047
0
  zend_jit_addr res_addr = RES_ADDR();
16048
0
  uint32_t cache_slot = opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS;
16049
0
  uint32_t flags;
16050
0
  ir_ref ref, ref2, if_cached, fast_path, cold_path, prop_info_ref, if_typed, if_def;
16051
0
  int fetch_type;
16052
0
  zend_property_info *known_prop_info = NULL;
16053
0
  zend_class_entry *ce;
16054
16055
0
  ce = zend_get_known_class(op_array, opline, opline->op2_type, opline->op2);
16056
0
  if (ce && (opline->op2_type == IS_CONST || !(ce->ce_flags & ZEND_ACC_TRAIT))) {
16057
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
16058
0
    zend_string *prop_name;
16059
16060
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
16061
0
    prop_name = Z_STR_P(zv);
16062
0
    zv = zend_hash_find(&ce->properties_info, prop_name);
16063
0
    if (zv) {
16064
0
      zend_property_info *prop_info = Z_PTR_P(zv);
16065
16066
0
      if (prop_info->flags & ZEND_ACC_STATIC) {
16067
0
        if (prop_info->ce == op_array->scope
16068
0
         || (prop_info->flags & ZEND_ACC_PUBLIC)
16069
0
         || ((prop_info->flags & ZEND_ACC_PROTECTED)
16070
0
          && op_array->scope
16071
0
          && instanceof_function_slow(op_array->scope, prop_info->ce))) {
16072
0
          known_prop_info = prop_info;
16073
0
        }
16074
0
      }
16075
0
    }
16076
0
  }
16077
16078
0
  switch (opline->opcode) {
16079
0
    case ZEND_FETCH_STATIC_PROP_R:
16080
0
    case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
16081
0
      fetch_type = BP_VAR_R;
16082
0
      break;
16083
0
    case ZEND_FETCH_STATIC_PROP_IS:
16084
0
      fetch_type = BP_VAR_IS;
16085
0
      break;
16086
0
    case ZEND_FETCH_STATIC_PROP_W:
16087
0
      fetch_type = BP_VAR_W;
16088
0
      break;
16089
0
    case ZEND_FETCH_STATIC_PROP_RW:
16090
0
      fetch_type = BP_VAR_RW;
16091
0
      break;
16092
0
    case ZEND_FETCH_STATIC_PROP_UNSET:
16093
0
      fetch_type = BP_VAR_UNSET;
16094
0
      break;
16095
0
    default: ZEND_UNREACHABLE();
16096
0
  }
16097
16098
  // JIT: result = CACHED_PTR(cache_slot + sizeof(void *));
16099
0
  ref = ir_LOAD_A(
16100
0
    ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*)));
16101
16102
  // JIT: if (result)
16103
0
  if_cached = ir_IF(ref);
16104
0
  ir_IF_TRUE(if_cached);
16105
16106
0
  if (fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW) {
16107
0
    if (!known_prop_info || ZEND_TYPE_IS_SET(known_prop_info->type)) {
16108
0
      ir_ref merge = IR_UNUSED;
16109
16110
      // JIT: if (UNEXPECTED(Z_TYPE_P(result) == IS_UNDEF)
16111
0
      if_typed = IR_UNUSED;
16112
0
      if_def = ir_IF(jit_Z_TYPE_ref(jit, ref));
16113
0
      ir_IF_FALSE_cold(if_def);
16114
0
      if (!known_prop_info) {
16115
        // JIT: if (ZEND_TYPE_IS_SET(property_info->type))
16116
0
        prop_info_ref = ir_LOAD_L(
16117
0
          ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*) * 2));
16118
0
        if_typed = ir_IF(ir_AND_U32(
16119
0
          ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, type.type_mask))),
16120
0
          ir_CONST_U32(_ZEND_TYPE_MASK)));
16121
0
        ir_IF_FALSE(if_typed);
16122
0
        ir_END_list(merge);
16123
0
        ir_IF_TRUE(if_typed);
16124
0
      }
16125
      // JIT: zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
16126
      //      ZSTR_VAL(property_info->ce->name),
16127
      //      zend_get_unmangled_property_name(property_info->name));
16128
0
      jit_SET_EX_OPLINE(jit, opline);
16129
0
      ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_uninit_static_prop));
16130
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
16131
16132
0
      ir_IF_TRUE(if_def);
16133
0
      if (!known_prop_info) {
16134
0
        ir_END_list(merge);
16135
0
        ir_MERGE_list(merge);
16136
0
      }
16137
0
    }
16138
0
  } else if (fetch_type == BP_VAR_W) {
16139
0
    flags = opline->extended_value & ZEND_FETCH_OBJ_FLAGS;
16140
0
    if (flags && (!known_prop_info || ZEND_TYPE_IS_SET(known_prop_info->type))) {
16141
0
        ir_ref merge = IR_UNUSED;
16142
16143
0
      if (!known_prop_info) {
16144
        // JIT: if (ZEND_TYPE_IS_SET(property_info->type))
16145
0
        prop_info_ref = ir_LOAD_L(
16146
0
          ir_ADD_OFFSET(ir_LOAD_A(jit_EX(run_time_cache)), cache_slot + sizeof(void*) * 2));
16147
0
        if_typed = ir_IF(ir_AND_U32(
16148
0
          ir_LOAD_U32(ir_ADD_OFFSET(prop_info_ref, offsetof(zend_property_info, type.type_mask))),
16149
0
          ir_CONST_U32(_ZEND_TYPE_MASK)));
16150
0
        ir_IF_FALSE(if_typed);
16151
0
        ir_END_list(merge);
16152
0
        ir_IF_TRUE(if_typed);
16153
0
      } else {
16154
0
        prop_info_ref = ir_CONST_ADDR(known_prop_info);
16155
0
      }
16156
16157
      // JIT: zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
16158
0
      ir_ref if_ok = ir_IF(ir_CALL_5(IR_BOOL, ir_CONST_FUNC(zend_handle_fetch_obj_flags),
16159
0
        IR_NULL, ref, IR_NULL, prop_info_ref, ir_CONST_U32(flags)));
16160
0
      ir_IF_FALSE_cold(if_ok);
16161
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler_undef));
16162
0
      ir_IF_TRUE(if_ok);
16163
0
      if (!known_prop_info) {
16164
0
        ir_END_list(merge);
16165
0
        ir_MERGE_list(merge);
16166
0
      }
16167
0
    }
16168
0
  }
16169
16170
0
  fast_path = ir_END();
16171
16172
0
  ir_IF_FALSE_cold(if_cached);
16173
0
  jit_SET_EX_OPLINE(jit, opline);
16174
0
  ref2 = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_fetch_static_property), jit_FP(jit), ir_CONST_I32(fetch_type));
16175
0
  zend_jit_check_exception_undef_result(jit, opline);
16176
0
  cold_path = ir_END();
16177
16178
0
  ir_MERGE_2(fast_path, cold_path);
16179
0
  ref = ir_PHI_2(IR_ADDR, ref, ref2);
16180
16181
0
  if (fetch_type == BP_VAR_R || fetch_type == BP_VAR_IS) {
16182
    // JIT: ZVAL_COPY_DEREF(EX_VAR(opline->result.var), result);
16183
0
    if (!zend_jit_zval_copy_deref(jit, res_addr, ZEND_ADDR_REF_ZVAL(ref),
16184
0
        jit_Z_TYPE_INFO_ref(jit, ref))) {
16185
0
      return 0;
16186
0
    }
16187
0
  } else {
16188
    // JIT: ZVAL_INDIRECT(EX_VAR(opline->result.var), result);
16189
0
    jit_set_Z_PTR(jit, res_addr, ref);
16190
0
    jit_set_Z_TYPE_INFO(jit, res_addr, IS_INDIRECT);
16191
0
  }
16192
16193
0
  return 1;
16194
0
}
16195
16196
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)
16197
0
{
16198
0
  HashTable *jumptable = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
16199
0
  const zend_op *next_opline = NULL;
16200
0
  ir_refs *slow_inputs;
16201
16202
0
  ir_refs_init(slow_inputs, 8);
16203
16204
0
  if (trace) {
16205
0
    ZEND_ASSERT(trace->op == ZEND_JIT_TRACE_VM || trace->op == ZEND_JIT_TRACE_END);
16206
0
    ZEND_ASSERT(trace->opline != NULL);
16207
0
    next_opline = trace->opline;
16208
0
  }
16209
16210
0
  if (opline->op1_type == IS_CONST) {
16211
0
    zval *zv = RT_CONSTANT(opline, opline->op1);
16212
0
    zval *jump_zv = NULL;
16213
0
    int b;
16214
16215
0
    if (opline->opcode == ZEND_SWITCH_LONG) {
16216
0
      if (Z_TYPE_P(zv) == IS_LONG) {
16217
0
        jump_zv = zend_hash_index_find(jumptable, Z_LVAL_P(zv));
16218
0
      }
16219
0
    } else if (opline->opcode == ZEND_SWITCH_STRING) {
16220
0
      if (Z_TYPE_P(zv) == IS_STRING) {
16221
0
        jump_zv = zend_hash_find_known_hash(jumptable, Z_STR_P(zv));
16222
0
      }
16223
0
    } else if (opline->opcode == ZEND_MATCH) {
16224
0
      if (Z_TYPE_P(zv) == IS_LONG) {
16225
0
        jump_zv = zend_hash_index_find(jumptable, Z_LVAL_P(zv));
16226
0
      } else if (Z_TYPE_P(zv) == IS_STRING) {
16227
0
        jump_zv = zend_hash_find_known_hash(jumptable, Z_STR_P(zv));
16228
0
      }
16229
0
    } else {
16230
0
      ZEND_UNREACHABLE();
16231
0
    }
16232
0
    if (next_opline) {
16233
0
      const zend_op *target;
16234
16235
0
      if (jump_zv != NULL) {
16236
0
        target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(jump_zv));
16237
0
      } else {
16238
0
        target = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
16239
0
      }
16240
0
      ZEND_ASSERT(target == next_opline);
16241
0
    } else {
16242
0
      if (jump_zv != NULL) {
16243
0
        b = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(jump_zv)) - op_array->opcodes];
16244
0
      } else {
16245
0
        b = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) - op_array->opcodes];
16246
0
      }
16247
0
      _zend_jit_add_predecessor_ref(jit, b, jit->b, ir_END());
16248
0
      jit->b = -1;
16249
0
    }
16250
0
  } else {
16251
0
    zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[opline - op_array->opcodes] : NULL;
16252
0
    uint32_t op1_info = OP1_INFO();
16253
0
    zend_jit_addr op1_addr = OP1_ADDR();
16254
0
    const zend_op *default_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
16255
0
    const zend_op *target;
16256
0
    int default_b = next_opline ? -1 : ssa->cfg.map[default_opline - op_array->opcodes];
16257
0
    int b;
16258
0
    int32_t exit_point;
16259
0
    const void *exit_addr;
16260
0
    const void *default_label = NULL;
16261
0
    zval *zv;
16262
16263
0
    if (next_opline) {
16264
0
      if (next_opline != default_opline) {
16265
0
        exit_point = zend_jit_trace_get_exit_point(default_opline, 0);
16266
0
        default_label = zend_jit_trace_get_exit_addr(exit_point);
16267
0
        if (!default_label) {
16268
0
          return 0;
16269
0
        }
16270
0
      }
16271
0
    }
16272
16273
0
    if (opline->opcode == ZEND_SWITCH_LONG) {
16274
0
      if (op1_info & MAY_BE_LONG) {
16275
0
        const void *fallback_label = NULL;
16276
16277
0
        if (next_opline) {
16278
0
          if (next_opline != opline + 1) {
16279
0
            exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
16280
0
            fallback_label = zend_jit_trace_get_exit_addr(exit_point);
16281
0
            if (!fallback_label) {
16282
0
              return 0;
16283
0
            }
16284
0
          }
16285
0
        }
16286
0
        if (op1_info & MAY_BE_REF) {
16287
0
          ir_ref ref, if_long, fast_path, ref2;
16288
16289
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
16290
0
          if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16291
0
          ir_IF_TRUE(if_long);
16292
0
          fast_path = ir_END();
16293
0
          ir_IF_FALSE_cold(if_long);
16294
16295
          // JIT: ZVAL_DEREF(op)
16296
0
          if (fallback_label) {
16297
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_REFERENCE, fallback_label);
16298
0
          } else {
16299
0
            ir_ref if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_REFERENCE);
16300
0
            ir_IF_FALSE_cold(if_ref);
16301
0
            ir_refs_add(slow_inputs, ir_END());
16302
0
            ir_IF_TRUE(if_ref);
16303
0
          }
16304
16305
0
          ref2 = ir_ADD_OFFSET(jit_Z_PTR(jit, op1_addr), offsetof(zend_reference, val));
16306
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref2);
16307
16308
0
          if (fallback_label) {
16309
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16310
0
          } else {
16311
0
            if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16312
0
            ir_IF_FALSE_cold(if_long);
16313
0
            ir_refs_add(slow_inputs, ir_END());
16314
0
            ir_IF_TRUE(if_long);
16315
0
          }
16316
16317
0
          ir_MERGE_2(fast_path, ir_END());
16318
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
16319
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
16320
0
        } else if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
16321
0
          if (fallback_label) {
16322
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16323
0
          } else {
16324
0
            ir_ref if_long = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16325
0
            ir_IF_FALSE_cold(if_long);
16326
0
            ir_refs_add(slow_inputs, ir_END());
16327
0
            ir_IF_TRUE(if_long);
16328
0
          }
16329
0
        }
16330
0
        ir_ref ref = jit_Z_LVAL(jit, op1_addr);
16331
16332
0
        if (!HT_IS_PACKED(jumptable)) {
16333
0
          ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_index_find),
16334
0
            ir_CONST_ADDR(jumptable), ref);
16335
0
          ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16336
          /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16337
0
          if (sizeof(Bucket) == 32) {
16338
0
            ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16339
0
          } else {
16340
0
            ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16341
0
          }
16342
0
        }
16343
0
        ref = ir_SWITCH(ref);
16344
16345
0
        if (next_opline) {
16346
0
          ir_ref continue_list = IR_UNUSED;
16347
16348
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16349
0
            ir_ref idx;
16350
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16351
16352
0
            if (HT_IS_PACKED(jumptable)) {
16353
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16354
0
            } else {
16355
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16356
0
            }
16357
0
            ir_CASE_VAL(ref, idx);
16358
0
            if (target == next_opline) {
16359
0
              ir_END_list(continue_list);
16360
0
            } else {
16361
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16362
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16363
0
              if (!exit_addr) {
16364
0
                return 0;
16365
0
              }
16366
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16367
0
            }
16368
0
          } ZEND_HASH_FOREACH_END();
16369
16370
0
          ir_CASE_DEFAULT(ref);
16371
0
          if (next_opline == default_opline) {
16372
0
            ir_END_list(continue_list);
16373
0
          } else {
16374
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16375
0
          }
16376
0
          if (continue_list) {
16377
0
            ir_MERGE_list(continue_list);
16378
0
          } else {
16379
0
            ZEND_ASSERT(slow_inputs->count);
16380
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16381
0
          }
16382
0
        } else {
16383
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16384
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16385
0
            b = ssa->cfg.map[target - op_array->opcodes];
16386
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16387
0
          } ZEND_HASH_FOREACH_END();
16388
16389
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16390
0
          if (slow_inputs->count) {
16391
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16392
0
            _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16393
0
          }
16394
0
          jit->b = -1;
16395
0
        }
16396
0
      } else if (!next_opline) {
16397
0
        _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16398
0
        jit->b = -1;
16399
0
      }
16400
0
    } else if (opline->opcode == ZEND_SWITCH_STRING) {
16401
0
      if (op1_info & MAY_BE_STRING) {
16402
0
        const void *fallback_label = NULL;
16403
16404
0
        if (next_opline) {
16405
0
          if (next_opline != opline + 1) {
16406
0
            exit_point = zend_jit_trace_get_exit_point(opline + 1, 0);
16407
0
            fallback_label = zend_jit_trace_get_exit_addr(exit_point);
16408
0
            if (!fallback_label) {
16409
0
              return 0;
16410
0
            }
16411
0
          }
16412
0
        }
16413
0
        if (op1_info & MAY_BE_REF) {
16414
0
          ir_ref ref, if_string, fast_path, ref2;
16415
16416
0
          ref = jit_ZVAL_ADDR(jit, op1_addr);
16417
0
          if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16418
0
          ir_IF_TRUE(if_string);
16419
0
          fast_path = ir_END();
16420
0
          ir_IF_FALSE_cold(if_string);
16421
16422
          // JIT: ZVAL_DEREF(op)
16423
0
          if (fallback_label) {
16424
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_REFERENCE, fallback_label);
16425
0
          } else {
16426
0
            ir_ref if_ref = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16427
0
            ir_IF_FALSE_cold(if_ref);
16428
0
            ir_refs_add(slow_inputs, ir_END());
16429
0
            ir_IF_TRUE(if_ref);
16430
0
          }
16431
16432
0
          ref2 = ir_ADD_OFFSET(jit_Z_PTR(jit, op1_addr), offsetof(zend_reference, val));
16433
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref2);
16434
16435
0
          if (fallback_label) {
16436
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, fallback_label);
16437
0
          } else {
16438
0
            if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16439
0
            ir_IF_FALSE_cold(if_string);
16440
0
            ir_refs_add(slow_inputs, ir_END());
16441
0
            ir_IF_TRUE(if_string);
16442
0
          }
16443
16444
0
          ir_MERGE_2(fast_path, ir_END());
16445
0
          ref = ir_PHI_2(IR_ADDR, ref, ref2);
16446
0
          op1_addr = ZEND_ADDR_REF_ZVAL(ref);
16447
0
        } else if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_STRING)) {
16448
0
          if (fallback_label) {
16449
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, fallback_label);
16450
0
          } else {
16451
0
            ir_ref if_string = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16452
0
            ir_IF_FALSE_cold(if_string);
16453
0
            ir_refs_add(slow_inputs, ir_END());
16454
0
            ir_IF_TRUE(if_string);
16455
0
          }
16456
0
        }
16457
16458
0
        ir_ref ref = jit_Z_PTR(jit, op1_addr);
16459
0
        ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_find),
16460
0
          ir_CONST_ADDR(jumptable), ref);
16461
0
        ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16462
        /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16463
0
        if (sizeof(Bucket) == 32) {
16464
0
          ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16465
0
        } else {
16466
0
          ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16467
0
        }
16468
0
        ref = ir_SWITCH(ref);
16469
16470
0
        if (next_opline) {
16471
0
          ir_ref continue_list = IR_UNUSED;
16472
16473
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16474
0
            ir_ref idx;
16475
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16476
16477
0
            if (HT_IS_PACKED(jumptable)) {
16478
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16479
0
            } else {
16480
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16481
0
            }
16482
0
            ir_CASE_VAL(ref, idx);
16483
0
            if (target == next_opline) {
16484
0
              ir_END_list(continue_list);
16485
0
            } else {
16486
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16487
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16488
0
              if (!exit_addr) {
16489
0
                return 0;
16490
0
              }
16491
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16492
0
            }
16493
0
          } ZEND_HASH_FOREACH_END();
16494
16495
0
          ir_CASE_DEFAULT(ref);
16496
0
          if (next_opline == default_opline) {
16497
0
            ir_END_list(continue_list);
16498
0
          } else {
16499
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16500
0
          }
16501
0
          if (continue_list) {
16502
0
            ir_MERGE_list(continue_list);
16503
0
          } else {
16504
0
            ZEND_ASSERT(slow_inputs->count);
16505
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16506
0
          }
16507
0
        } else {
16508
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16509
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16510
0
            b = ssa->cfg.map[target - op_array->opcodes];
16511
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16512
0
          } ZEND_HASH_FOREACH_END();
16513
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16514
0
          if (slow_inputs->count) {
16515
0
            ir_MERGE_N(slow_inputs->count, slow_inputs->refs);
16516
0
            _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16517
0
          }
16518
0
          jit->b = -1;
16519
0
        }
16520
0
      } else if (!next_opline) {
16521
0
        _zend_jit_add_predecessor_ref(jit, jit->b + 1, jit->b, ir_END());
16522
0
        jit->b = -1;
16523
0
      }
16524
0
    } else if (opline->opcode == ZEND_MATCH) {
16525
0
      ir_ref if_type = IR_UNUSED, default_input_list = IR_UNUSED, ref = IR_UNUSED;
16526
0
      ir_ref continue_list = IR_UNUSED;
16527
16528
0
      if (op1_info & (MAY_BE_LONG|MAY_BE_STRING)) {
16529
0
        ir_ref long_path = IR_UNUSED;
16530
16531
0
        if (op1_info & MAY_BE_REF) {
16532
0
          op1_addr = jit_ZVAL_DEREF(jit, op1_addr);
16533
0
        }
16534
0
        if (op1_info & MAY_BE_LONG) {
16535
0
          if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
16536
0
            if (op1_info & (MAY_BE_STRING|MAY_BE_UNDEF)) {
16537
0
              if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16538
0
              ir_IF_TRUE(if_type);
16539
0
            } else if (default_label) {
16540
0
              jit_guard_Z_TYPE(jit, op1_addr, IS_LONG, default_label);
16541
0
            } else if (next_opline) {
16542
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16543
0
              ir_IF_FALSE(if_type);
16544
0
              ir_END_list(continue_list);
16545
0
              ir_IF_TRUE(if_type);
16546
0
            } else {
16547
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_LONG);
16548
0
              ir_IF_FALSE(if_type);
16549
0
              ir_END_list(default_input_list);
16550
0
              ir_IF_TRUE(if_type);
16551
0
            }
16552
0
          }
16553
0
          ref = jit_Z_LVAL(jit, op1_addr);
16554
0
          ref = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_index_find),
16555
0
            ir_CONST_ADDR(jumptable), ref);
16556
0
          if (op1_info & MAY_BE_STRING) {
16557
0
            long_path = ir_END();
16558
0
          }
16559
0
        }
16560
0
        if (op1_info & MAY_BE_STRING) {
16561
0
          if (if_type) {
16562
0
            ir_IF_FALSE(if_type);
16563
0
            if_type = IS_UNUSED;
16564
0
          }
16565
0
          if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_STRING))) {
16566
0
            if (op1_info & MAY_BE_UNDEF) {
16567
0
              if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16568
0
              ir_IF_TRUE(if_type);
16569
0
            } else if (default_label) {
16570
0
              jit_guard_Z_TYPE(jit, op1_addr, IS_STRING, default_label);
16571
0
            } else if (next_opline) {
16572
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16573
0
              ir_IF_FALSE(if_type);
16574
0
              ir_END_list(continue_list);
16575
0
              ir_IF_TRUE(if_type);
16576
0
            } else {
16577
0
              ir_ref if_type = jit_if_Z_TYPE(jit, op1_addr, IS_STRING);
16578
0
              ir_IF_FALSE(if_type);
16579
0
              ir_END_list(default_input_list);
16580
0
              ir_IF_TRUE(if_type);
16581
0
            }
16582
0
          }
16583
0
          ir_ref ref2 = jit_Z_PTR(jit, op1_addr);
16584
0
          ref2 = ir_CALL_2(IR_LONG, ir_CONST_FC_FUNC(zend_hash_find),
16585
0
            ir_CONST_ADDR(jumptable), ref2);
16586
0
          if (op1_info & MAY_BE_LONG) {
16587
0
            ir_MERGE_WITH(long_path);
16588
0
            ref = ir_PHI_2(IR_LONG, ref2, ref);
16589
0
          } else {
16590
0
            ref = ref2;
16591
0
          }
16592
0
        }
16593
16594
0
        ref = ir_SUB_L(ref, ir_CONST_LONG((uintptr_t)jumptable->arData));
16595
        /* Signed DIV by power of 2 may be optimized into SHR only for positive operands */
16596
0
        if (HT_IS_PACKED(jumptable)) {
16597
0
          ZEND_ASSERT(sizeof(zval) == 16);
16598
0
          ref = ir_SHR_L(ref, ir_CONST_LONG(4));
16599
0
        } else {
16600
0
          if (sizeof(Bucket) == 32) {
16601
0
            ref = ir_SHR_L(ref, ir_CONST_LONG(5));
16602
0
          } else {
16603
0
            ref = ir_DIV_L(ref, ir_CONST_LONG(sizeof(Bucket)));
16604
0
          }
16605
0
        }
16606
0
        ref = ir_SWITCH(ref);
16607
16608
0
        if (next_opline) {
16609
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16610
0
            ir_ref idx;
16611
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16612
16613
0
            if (HT_IS_PACKED(jumptable)) {
16614
0
              idx = ir_CONST_LONG(zv - jumptable->arPacked);
16615
0
            } else {
16616
0
              idx = ir_CONST_LONG((Bucket*)zv - jumptable->arData);
16617
0
            }
16618
0
            ir_CASE_VAL(ref, idx);
16619
0
            if (target == next_opline) {
16620
0
              ir_END_list(continue_list);
16621
0
            } else {
16622
0
              exit_point = zend_jit_trace_get_exit_point(target, 0);
16623
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16624
0
              if (!exit_addr) {
16625
0
                return 0;
16626
0
              }
16627
0
              jit_SIDE_EXIT(jit, ir_CONST_ADDR(exit_addr));
16628
0
            }
16629
0
          } ZEND_HASH_FOREACH_END();
16630
16631
0
          ir_CASE_DEFAULT(ref);
16632
0
          if (next_opline == default_opline) {
16633
0
            ir_END_list(continue_list);
16634
0
          } else {
16635
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16636
0
          }
16637
0
        } else {
16638
0
          ZEND_HASH_FOREACH_VAL(jumptable, zv) {
16639
0
            target = ZEND_OFFSET_TO_OPLINE(opline, Z_LVAL_P(zv));
16640
0
            b = ssa->cfg.map[target - op_array->opcodes];
16641
0
            _zend_jit_add_predecessor_ref(jit, b, jit->b, ref);
16642
0
          } ZEND_HASH_FOREACH_END();
16643
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ref);
16644
0
        }
16645
0
      } else if (!(op1_info & MAY_BE_UNDEF)) {
16646
0
        if (next_opline) {
16647
0
          if (next_opline == default_opline) {
16648
0
            ir_END_list(continue_list);
16649
0
          } else {
16650
0
            jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16651
0
          }
16652
0
        } else {
16653
0
          _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ir_END());
16654
0
        }
16655
0
      }
16656
16657
0
      if (op1_info & MAY_BE_UNDEF) {
16658
0
        if (if_type) {
16659
0
          ir_IF_FALSE(if_type);
16660
0
          if_type = IS_UNUSED;
16661
0
        }
16662
0
        if (op1_info & (MAY_BE_ANY-(MAY_BE_LONG|MAY_BE_STRING))) {
16663
0
          if (default_label) {
16664
0
            jit_guard_Z_TYPE(jit, op1_addr, IS_UNDEF, default_label);
16665
0
          } else if (next_opline) {
16666
0
            ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
16667
0
            ir_IF_TRUE(if_def);
16668
0
            ir_END_list(continue_list);
16669
0
            ir_IF_FALSE_cold(if_def);
16670
0
          } else {
16671
0
            ir_ref if_def = ir_IF(jit_Z_TYPE(jit, op1_addr));
16672
0
            ir_IF_TRUE(if_def);
16673
0
            ir_END_list(default_input_list);
16674
0
            ir_IF_FALSE_cold(if_def);
16675
0
          }
16676
0
        }
16677
16678
0
        jit_SET_EX_OPLINE(jit, opline);
16679
0
        ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_op_helper),
16680
0
          ir_CONST_U32(opline->op1.var));
16681
0
        zend_jit_check_exception_undef_result(jit, opline);
16682
0
        if (default_label) {
16683
0
          jit_SIDE_EXIT(jit, ir_CONST_ADDR(default_label));
16684
0
        } else if (next_opline) {
16685
0
          ir_END_list(continue_list);
16686
0
        } else {
16687
0
          ir_END_list(default_input_list);
16688
0
        }
16689
0
      }
16690
0
      if (next_opline) {
16691
0
        ZEND_ASSERT(continue_list);
16692
0
        ir_MERGE_list(continue_list);
16693
0
      } else {
16694
0
        if (default_input_list) {
16695
0
          if (jit->ctx.ir_base[ref].op == IR_SWITCH) {
16696
0
            ZEND_ASSERT(jit->ctx.ir_base[ref].op3 == IR_UNUSED);
16697
0
            jit->ctx.ir_base[ref].op3 = default_input_list;
16698
0
          } else {
16699
0
            ir_MERGE_list(default_input_list);
16700
0
            _zend_jit_add_predecessor_ref(jit, default_b, jit->b, ir_END());
16701
0
          }
16702
0
        }
16703
0
        jit->b = -1;
16704
0
      }
16705
0
    } else {
16706
0
      ZEND_UNREACHABLE();
16707
0
    }
16708
0
  }
16709
0
  return 1;
16710
0
}
16711
16712
static int zend_jit_start(zend_jit_ctx *jit, const zend_op_array *op_array, zend_ssa *ssa)
16713
0
{
16714
0
  uint32_t i, count;
16715
0
  zend_basic_block *bb;
16716
16717
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));
16718
16719
0
  jit->ctx.spill_base = ZREG_FP;
16720
16721
0
  jit->op_array = jit->current_op_array = op_array;
16722
0
  jit->ssa = ssa;
16723
0
  jit->bb_start_ref = zend_arena_calloc(&CG(arena), ssa->cfg.blocks_count * 2, sizeof(ir_ref));
16724
0
  jit->bb_predecessors = jit->bb_start_ref + ssa->cfg.blocks_count;
16725
16726
0
  count = 0;
16727
0
  for (i = 0, bb = ssa->cfg.blocks; i < ssa->cfg.blocks_count; i++, bb++) {
16728
0
    jit->bb_predecessors[i] = count;
16729
0
    count += bb->predecessors_count;
16730
0
  }
16731
0
  jit->bb_edges = zend_arena_calloc(&CG(arena), count, sizeof(ir_ref));
16732
16733
0
  if (!GCC_GLOBAL_REGS) {
16734
0
    if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
16735
0
      ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
16736
0
      ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
16737
0
      jit_STORE_FP(jit, execute_data_ref);
16738
0
      jit_STORE_IP(jit, opline_ref);
16739
0
    }
16740
0
    jit->ctx.flags |= IR_FASTCALL_FUNC;
16741
0
  }
16742
16743
0
  return 1;
16744
0
}
16745
16746
static zend_vm_opcode_handler_t zend_jit_finish(zend_jit_ctx *jit)
16747
0
{
16748
0
  void *entry;
16749
0
  size_t size;
16750
0
  zend_string *str = NULL;
16751
16752
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP|
16753
0
      ZEND_JIT_DEBUG_IR_SRC|ZEND_JIT_DEBUG_IR_AFTER_SCCP|ZEND_JIT_DEBUG_IR_AFTER_SCCP|
16754
0
      ZEND_JIT_DEBUG_IR_AFTER_SCHEDULE|ZEND_JIT_DEBUG_IR_AFTER_REGS|ZEND_JIT_DEBUG_IR_FINAL|ZEND_JIT_DEBUG_IR_CODEGEN)) {
16755
0
    if (jit->name) {
16756
0
      str = zend_string_copy(jit->name);
16757
0
    } else {
16758
0
      str = zend_jit_func_name(jit->op_array);
16759
0
    }
16760
0
  }
16761
16762
0
  if (jit->op_array) {
16763
    /* Only for function JIT */
16764
0
    _zend_jit_fix_merges(jit);
16765
#if defined(IR_TARGET_AARCH64)
16766
  } else if (jit->trace) {
16767
    jit->ctx.deoptimization_exits = jit->trace->exit_count;
16768
    jit->ctx.get_exit_addr = zend_jit_trace_get_exit_addr;
16769
#endif
16770
0
  } else {
16771
0
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
16772
0
    jit->ctx.flags |= IR_GEN_CACHE_DEMOTE;
16773
0
#endif
16774
0
  }
16775
16776
0
  entry = zend_jit_ir_compile(&jit->ctx, &size, str ? ZSTR_VAL(str) : NULL);
16777
0
  if (entry) {
16778
0
    if (JIT_G(debug) & (ZEND_JIT_DEBUG_ASM|ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
16779
#ifdef HAVE_CAPSTONE
16780
      if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
16781
        if (str) {
16782
          ir_disasm_add_symbol(ZSTR_VAL(str), (uintptr_t)entry, size);
16783
        }
16784
        ir_disasm(str ? ZSTR_VAL(str) : "unknown",
16785
          entry, size,
16786
          (JIT_G(debug) & ZEND_JIT_DEBUG_ASM_ADDR) != 0,
16787
          &jit->ctx, stderr);
16788
      }
16789
#endif
16790
0
#ifndef _WIN32
16791
0
      if (str) {
16792
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_GDB) {
16793
0
          uintptr_t sp_offset = 0;
16794
16795
//          ir_mem_unprotect(entry, size);
16796
0
          if (!(jit->ctx.flags & IR_FUNCTION)
16797
0
           && ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
16798
0
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)
16799
0
            sp_offset = zend_jit_hybrid_vm_sp_adj;
16800
#else
16801
            sp_offset = sizeof(void*);
16802
#endif
16803
0
          } else {
16804
0
            sp_offset = sizeof(void*);
16805
0
          }
16806
0
          ir_gdb_register(ZSTR_VAL(str), entry, size, sp_offset, 0);
16807
//          ir_mem_protect(entry, size);
16808
0
        }
16809
16810
0
        if (JIT_G(debug) & (ZEND_JIT_DEBUG_PERF|ZEND_JIT_DEBUG_PERF_DUMP)) {
16811
0
          ir_perf_map_register(ZSTR_VAL(str), entry, size);
16812
0
          if (JIT_G(debug) & ZEND_JIT_DEBUG_PERF_DUMP) {
16813
0
            ir_perf_jitdump_register(ZSTR_VAL(str), entry, size);
16814
0
          }
16815
0
        }
16816
0
      }
16817
0
#endif
16818
0
    }
16819
16820
0
    if (jit->op_array) {
16821
      /* Only for function JIT */
16822
0
      const zend_op_array *op_array = jit->op_array;
16823
0
      zend_op *opline = (zend_op*)op_array->opcodes;
16824
16825
0
      if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
16826
0
        while (opline->opcode == ZEND_RECV) {
16827
0
          opline++;
16828
0
        }
16829
0
      }
16830
0
      opline->handler = (zend_vm_opcode_handler_t)entry;
16831
16832
0
      if (jit->ctx.entries_count) {
16833
        /* For all entries */
16834
0
        int i = jit->ctx.entries_count;
16835
0
        do {
16836
0
          ir_insn *insn = &jit->ctx.ir_base[jit->ctx.entries[--i]];
16837
0
          op_array->opcodes[insn->op2].handler = (zend_vm_opcode_handler_t)((char*)entry + insn->op3);
16838
0
        } while (i != 0);
16839
0
      }
16840
0
    } else {
16841
      /* Only for tracing JIT */
16842
0
      zend_jit_trace_info *t = jit->trace;
16843
0
      zend_jit_trace_stack *stack;
16844
0
      uint32_t i;
16845
16846
0
      if (t) {
16847
0
        for (i = 0; i < t->stack_map_size; i++) {
16848
0
          stack = t->stack_map + i;
16849
0
          if (stack->flags & ZREG_SPILL_SLOT) {
16850
0
            stack->reg = (jit->ctx.flags & IR_USE_FRAME_POINTER) ? IR_REG_FP : IR_REG_SP;
16851
0
            stack->ref = ir_get_spill_slot_offset(&jit->ctx, stack->ref);
16852
0
          }
16853
0
        }
16854
0
      }
16855
16856
0
      zend_jit_trace_add_code(entry, size);
16857
0
    }
16858
0
  }
16859
16860
0
  if (str) {
16861
0
    zend_string_release(str);
16862
0
  }
16863
16864
0
  return (zend_vm_opcode_handler_t)entry;
16865
0
}
16866
16867
static const void *zend_jit_trace_allocate_exit_group(uint32_t n)
16868
0
{
16869
0
  const void *entry;
16870
0
  size_t size;
16871
0
  ir_code_buffer code_buffer;
16872
16873
0
  code_buffer.start = dasm_buf;
16874
0
  code_buffer.end = dasm_end;
16875
0
  code_buffer.pos = *dasm_ptr;
16876
16877
0
  entry = ir_emit_exitgroup(n, ZEND_JIT_EXIT_POINTS_PER_GROUP, zend_jit_stub_handlers[jit_stub_trace_exit],
16878
0
    &code_buffer, &size);
16879
16880
0
  *dasm_ptr = code_buffer.pos;
16881
16882
0
  if (entry) {
16883
#ifdef HAVE_CAPSTONE
16884
    if (JIT_G(debug) & ZEND_JIT_DEBUG_ASM) {
16885
      uint32_t i;
16886
      char name[32];
16887
16888
      for (i = 0; i < ZEND_JIT_EXIT_POINTS_PER_GROUP; i++) {
16889
        snprintf(name, sizeof(name), "jit$$trace_exit_%d", n + i);
16890
        ir_disasm_add_symbol(name, (uintptr_t)entry + (i * ZEND_JIT_EXIT_POINTS_SPACING), ZEND_JIT_EXIT_POINTS_SPACING);
16891
      }
16892
    }
16893
#endif
16894
0
  }
16895
16896
0
  return entry;
16897
0
}
16898
16899
static int zend_jit_type_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var, uint8_t type)
16900
0
{
16901
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16902
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16903
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
16904
16905
0
  if (!exit_addr) {
16906
0
    return 0;
16907
0
  }
16908
0
  ir_GUARD(ir_EQ(jit_Z_TYPE(jit, addr), ir_CONST_U8(type)), ir_CONST_ADDR(exit_addr));
16909
16910
0
  return 1;
16911
0
}
16912
16913
static int zend_jit_scalar_type_guard(zend_jit_ctx *jit, const zend_op *opline, uint32_t var)
16914
0
{
16915
0
  int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16916
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16917
0
  zend_jit_addr addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);
16918
16919
0
  if (!exit_addr) {
16920
0
    return 0;
16921
0
  }
16922
0
  ir_GUARD(ir_LT(jit_Z_TYPE(jit, addr), ir_CONST_U8(IS_STRING)), ir_CONST_ADDR(exit_addr));
16923
16924
0
  return 1;
16925
0
}
16926
16927
static bool zend_jit_noref_guard(zend_jit_ctx *jit, const zend_op *opline, zend_jit_addr var_addr)
16928
0
{
16929
0
  uint32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16930
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16931
16932
0
  if (!exit_addr) {
16933
0
    return false;
16934
0
  }
16935
0
  ir_GUARD(ir_NE(jit_Z_TYPE(jit, var_addr), ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16936
16937
0
  return true;
16938
0
}
16939
16940
static int zend_jit_trace_opline_guard(zend_jit_ctx *jit, const zend_op *opline)
16941
0
{
16942
0
  uint32_t exit_point = zend_jit_trace_get_exit_point(NULL, 0);
16943
0
  const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16944
16945
0
  if (!exit_addr) {
16946
0
    return 0;
16947
0
  }
16948
16949
0
  ir_GUARD(jit_CMP_IP(jit, IR_EQ, opline), ir_CONST_ADDR(exit_addr));
16950
0
  zend_jit_set_last_valid_opline(jit, opline);
16951
16952
0
  return 1;
16953
0
}
16954
16955
static bool zend_jit_guard_reference(zend_jit_ctx  *jit,
16956
                                     const zend_op *opline,
16957
                                     zend_jit_addr *var_addr_ptr,
16958
                                     zend_jit_addr *ref_addr_ptr,
16959
                                     bool           add_ref_guard)
16960
0
{
16961
0
  zend_jit_addr var_addr = *var_addr_ptr;
16962
0
  const void *exit_addr = NULL;
16963
0
  ir_ref ref;
16964
16965
0
  if (add_ref_guard) {
16966
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
16967
16968
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
16969
0
    if (!exit_addr) {
16970
0
      return false;
16971
0
    }
16972
16973
0
    ref = jit_Z_TYPE(jit, var_addr);
16974
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
16975
0
  }
16976
16977
0
  ref = jit_Z_PTR(jit, var_addr);
16978
0
  *ref_addr_ptr = ZEND_ADDR_REF_ZVAL(ref);
16979
0
  ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
16980
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
16981
0
  *var_addr_ptr = var_addr;
16982
16983
0
  return true;
16984
0
}
16985
16986
static bool zend_jit_fetch_reference(zend_jit_ctx  *jit,
16987
                                     const zend_op *opline,
16988
                                     uint8_t        var_type,
16989
                                     uint32_t      *var_info_ptr,
16990
                                     zend_jit_addr *var_addr_ptr,
16991
                                     bool           add_ref_guard,
16992
                                     bool           add_type_guard)
16993
0
{
16994
0
  zend_jit_addr var_addr = *var_addr_ptr;
16995
0
  uint32_t var_info = *var_info_ptr;
16996
0
  const void *exit_addr = NULL;
16997
0
  ir_ref ref;
16998
16999
0
  if (add_ref_guard || add_type_guard) {
17000
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
17001
17002
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17003
0
    if (!exit_addr) {
17004
0
      return false;
17005
0
    }
17006
0
  }
17007
17008
0
  if (add_ref_guard) {
17009
0
    ref = jit_Z_TYPE(jit, var_addr);
17010
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(IS_REFERENCE)), ir_CONST_ADDR(exit_addr));
17011
0
  }
17012
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL && opline->op1_type == IS_VAR) {
17013
    /* Hack: Convert reference to regular value to simplify JIT code for INIT_METHOD_CALL */
17014
0
    ir_CALL_1(IR_VOID, ir_CONST_FC_FUNC(zend_jit_unref_helper),
17015
0
      jit_ZVAL_ADDR(jit, var_addr));
17016
0
    *var_addr_ptr = var_addr;
17017
0
  } else {
17018
0
    ref = jit_Z_PTR(jit, var_addr);
17019
0
    ref = ir_ADD_OFFSET(ref, offsetof(zend_reference, val));
17020
0
    var_addr = ZEND_ADDR_REF_ZVAL(ref);
17021
0
    *var_addr_ptr = var_addr;
17022
0
  }
17023
17024
0
  if (var_type != IS_UNKNOWN) {
17025
0
    var_type &= ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED);
17026
0
  }
17027
0
  if (add_type_guard
17028
0
   && var_type != IS_UNKNOWN
17029
0
   && (var_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << var_type)) {
17030
0
    ref = jit_Z_TYPE(jit, var_addr);
17031
0
    ir_GUARD(ir_EQ(ref, ir_CONST_U8(var_type)), ir_CONST_ADDR(exit_addr));
17032
17033
0
    ZEND_ASSERT(var_info & (1 << var_type));
17034
0
    if (var_type < IS_STRING) {
17035
0
      var_info = (1 << var_type);
17036
0
    } else if (var_type != IS_ARRAY) {
17037
0
      var_info = (1 << var_type) | (var_info & (MAY_BE_RC1|MAY_BE_RCN));
17038
0
    } else {
17039
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));
17040
0
    }
17041
17042
0
    *var_info_ptr = var_info;
17043
0
  } else {
17044
0
    var_info &= ~MAY_BE_REF;
17045
0
    *var_info_ptr = var_info;
17046
0
  }
17047
0
  *var_info_ptr |= MAY_BE_GUARD; /* prevent generation of specialized zval dtor */
17048
17049
0
  return true;
17050
0
}
17051
17052
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)
17053
0
{
17054
0
  zend_jit_addr var_addr = *var_addr_ptr;
17055
0
  uint32_t var_info = *var_info_ptr;
17056
0
  int32_t exit_point;
17057
0
  const void *exit_addr;
17058
0
  ir_ref ref = IR_UNUSED;
17059
17060
0
  if (add_indirect_guard) {
17061
0
    int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0);
17062
0
    const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17063
17064
0
    if (!exit_addr) {
17065
0
      return false;
17066
0
    }
17067
0
    jit_guard_Z_TYPE(jit, var_addr, IS_INDIRECT, exit_addr);
17068
0
    ref = jit_Z_PTR(jit, var_addr);
17069
0
  } else {
17070
    /* This LOAD of INDIRECT VAR, stored by the previous FETCH_(DIM/OBJ)_W,
17071
     * is eliminated by store forwarding (S2L) */
17072
0
    ref = jit_Z_PTR(jit, var_addr);
17073
0
  }
17074
0
  *var_info_ptr &= ~MAY_BE_INDIRECT;
17075
0
  var_addr = ZEND_ADDR_REF_ZVAL(ref);
17076
0
  *var_addr_ptr = var_addr;
17077
17078
0
  if (var_type != IS_UNKNOWN) {
17079
0
    var_type &= ~(IS_TRACE_INDIRECT|IS_TRACE_PACKED);
17080
0
  }
17081
0
  if (!(var_type & IS_TRACE_REFERENCE)
17082
0
   && var_type != IS_UNKNOWN
17083
0
   && (var_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << var_type)) {
17084
0
    exit_point = zend_jit_trace_get_exit_point(opline, 0);
17085
0
    exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17086
17087
0
    if (!exit_addr) {
17088
0
      return false;
17089
0
    }
17090
17091
0
    jit_guard_Z_TYPE(jit, var_addr, var_type, exit_addr);
17092
17093
    //var_info = zend_jit_trace_type_to_info_ex(var_type, var_info);
17094
0
    ZEND_ASSERT(var_info & (1 << var_type));
17095
0
    if (var_type < IS_STRING) {
17096
0
      var_info = (1 << var_type);
17097
0
    } else if (var_type != IS_ARRAY) {
17098
0
      var_info = (1 << var_type) | (var_info & (MAY_BE_RC1|MAY_BE_RCN));
17099
0
    } else {
17100
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));
17101
0
    }
17102
17103
0
    *var_info_ptr = var_info;
17104
0
  }
17105
17106
0
  return true;
17107
0
}
17108
17109
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)
17110
0
{
17111
0
  zend_jit_op_array_trace_extension *jit_extension =
17112
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
17113
0
  size_t offset = jit_extension->offset;
17114
0
  zend_vm_opcode_handler_func_t handler =
17115
0
    ZEND_OP_TRACE_INFO(opline, offset)->call_handler;
17116
0
  ir_ref ref;
17117
17118
0
  zend_jit_set_ip(jit, opline);
17119
0
  if (GCC_GLOBAL_REGS) {
17120
0
    ir_CALL(IR_VOID, ir_CONST_FUNC(handler));
17121
0
  } else {
17122
0
    ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(handler), jit_FP(jit), jit_IP(jit));
17123
0
    if (opline->opcode == ZEND_RETURN ||
17124
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17125
0
        opline->opcode == ZEND_DO_UCALL ||
17126
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
17127
0
        opline->opcode == ZEND_DO_FCALL ||
17128
0
        opline->opcode == ZEND_GENERATOR_CREATE ||
17129
0
        opline->opcode == ZEND_INCLUDE_OR_EVAL) {
17130
17131
0
      jit_STORE_IP(jit, ir_AND_A(ref, ir_CONST_ADDR(~ZEND_VM_ENTER_BIT)));
17132
0
    } else {
17133
0
      jit_STORE_IP(jit, ref);
17134
0
    }
17135
0
  }
17136
0
  if (may_throw
17137
0
   && opline->opcode != ZEND_RETURN
17138
0
   && opline->opcode != ZEND_RETURN_BY_REF) {
17139
0
    zend_jit_check_exception(jit);
17140
0
  }
17141
17142
0
  while (trace->op != ZEND_JIT_TRACE_VM && trace->op != ZEND_JIT_TRACE_END) {
17143
0
    trace++;
17144
0
  }
17145
17146
0
  if ((!GCC_GLOBAL_REGS
17147
0
   && (trace->op != ZEND_JIT_TRACE_END || trace->stop != ZEND_JIT_TRACE_STOP_RETURN))
17148
0
   || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
17149
0
    if (opline->opcode == ZEND_RETURN ||
17150
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17151
0
        opline->opcode == ZEND_DO_UCALL ||
17152
0
        opline->opcode == ZEND_DO_FCALL_BY_NAME ||
17153
0
        opline->opcode == ZEND_DO_FCALL ||
17154
0
        opline->opcode == ZEND_GENERATOR_CREATE ||
17155
0
        opline->opcode == ZEND_INCLUDE_OR_EVAL) {
17156
17157
0
      ir_ref addr = jit_EG(current_execute_data);
17158
17159
0
      jit_STORE_FP(jit, ir_LOAD_A(addr));
17160
0
    }
17161
0
  }
17162
17163
0
  if (zend_jit_trace_may_exit(op_array, opline)) {
17164
0
    if (opline->opcode == ZEND_RETURN ||
17165
0
        opline->opcode == ZEND_RETURN_BY_REF ||
17166
0
        opline->opcode == ZEND_GENERATOR_CREATE) {
17167
17168
0
      if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
17169
0
        if (trace->op != ZEND_JIT_TRACE_END ||
17170
0
            (trace->stop != ZEND_JIT_TRACE_STOP_RETURN &&
17171
0
             trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
17172
          /* this check may be handled by the following OPLINE guard or jmp [IP] */
17173
0
          ir_GUARD(ir_NE(jit_IP(jit), ir_CONST_ADDR(zend_jit_halt_op)),
17174
0
            jit_STUB_ADDR(jit, jit_stub_trace_halt));
17175
0
        }
17176
0
      } else {
17177
        /* IP has been cleared of ZEND_VM_ENTER_BIT already */
17178
0
        ir_GUARD(jit_IP(jit), jit_STUB_ADDR(jit, jit_stub_trace_halt));
17179
0
      }
17180
0
    } else if (opline->opcode == ZEND_GENERATOR_RETURN ||
17181
0
               opline->opcode == ZEND_YIELD ||
17182
0
               opline->opcode == ZEND_YIELD_FROM) {
17183
0
      ir_IJMP(jit_STUB_ADDR(jit, jit_stub_trace_halt));
17184
0
      ir_BEGIN(IR_UNUSED); /* unreachable block */
17185
0
    }
17186
0
    if (trace->op != ZEND_JIT_TRACE_END ||
17187
0
        (trace->stop != ZEND_JIT_TRACE_STOP_RETURN &&
17188
0
         trace->stop < ZEND_JIT_TRACE_STOP_INTERPRETER)) {
17189
17190
0
      const zend_op *next_opline = trace->opline;
17191
0
      const zend_op *exit_opline = NULL;
17192
0
      uint32_t exit_point;
17193
0
      const void *exit_addr;
17194
0
      uint32_t old_info = 0;
17195
0
      uint32_t old_res_info = 0;
17196
0
      zend_jit_trace_stack *stack = JIT_G(current_frame)->stack;
17197
17198
0
      if (zend_is_smart_branch(opline)) {
17199
0
        bool exit_if_true = false;
17200
0
        exit_opline = zend_jit_trace_get_exit_opline(trace, opline + 1, &exit_if_true);
17201
0
      } else {
17202
0
        switch (opline->opcode) {
17203
0
          case ZEND_JMPZ:
17204
0
          case ZEND_JMPNZ:
17205
0
          case ZEND_JMPZ_EX:
17206
0
          case ZEND_JMPNZ_EX:
17207
0
          case ZEND_JMP_SET:
17208
0
          case ZEND_COALESCE:
17209
0
          case ZEND_JMP_NULL:
17210
0
          case ZEND_FE_RESET_R:
17211
0
          case ZEND_FE_RESET_RW:
17212
0
            exit_opline = (trace->opline == opline + 1) ?
17213
0
              OP_JMP_ADDR(opline, opline->op2) :
17214
0
              opline + 1;
17215
0
            break;
17216
0
          case ZEND_FE_FETCH_R:
17217
0
          case ZEND_FE_FETCH_RW:
17218
0
            exit_opline = (trace->opline == opline + 1) ?
17219
0
              ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) :
17220
0
              opline + 1;
17221
0
            break;
17222
17223
0
        }
17224
0
      }
17225
17226
0
      switch (opline->opcode) {
17227
0
        case ZEND_FE_FETCH_R:
17228
0
        case ZEND_FE_FETCH_RW:
17229
0
          if (opline->op2_type != IS_UNUSED) {
17230
0
            old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op2.var));
17231
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var), IS_UNKNOWN, 1);
17232
0
          }
17233
0
          break;
17234
0
        case ZEND_FE_RESET_RW:
17235
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
17236
0
          if (opline->op1_type == IS_CV) {
17237
0
            old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
17238
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17239
0
          }
17240
0
          break;
17241
0
      }
17242
0
      if (opline->result_type == IS_VAR || opline->result_type == IS_TMP_VAR) {
17243
0
        old_res_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
17244
0
        SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
17245
0
      }
17246
0
      exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
17247
0
      exit_addr = zend_jit_trace_get_exit_addr(exit_point);
17248
17249
0
      if (opline->result_type == IS_VAR || opline->result_type == IS_TMP_VAR) {
17250
0
        SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_res_info);
17251
0
      }
17252
0
      switch (opline->opcode) {
17253
0
        case ZEND_FE_FETCH_R:
17254
0
        case ZEND_FE_FETCH_RW:
17255
0
          if (opline->op2_type != IS_UNUSED) {
17256
0
            SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op2.var), old_info);
17257
0
          }
17258
0
          break;
17259
0
        case ZEND_FE_RESET_RW:
17260
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
17261
0
          if (opline->op1_type == IS_CV) {
17262
0
            SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_info);
17263
0
          }
17264
0
          break;
17265
0
      }
17266
17267
0
      if (!exit_addr) {
17268
0
        return 0;
17269
0
      }
17270
0
      ir_GUARD(jit_CMP_IP(jit, IR_EQ, next_opline), ir_CONST_ADDR(exit_addr));
17271
0
    }
17272
0
  }
17273
17274
0
  zend_jit_set_last_valid_opline(jit, trace->opline);
17275
17276
0
  return 1;
17277
0
}
17278
17279
static int zend_jit_deoptimizer_start(zend_jit_ctx        *jit,
17280
                                      zend_string         *name,
17281
                                      uint32_t             trace_num,
17282
                                      uint32_t             exit_num)
17283
0
{
17284
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);
17285
17286
0
  jit->ctx.spill_base = ZREG_FP;
17287
17288
0
  jit->op_array = NULL;
17289
0
  jit->ssa = NULL;
17290
0
  jit->name = zend_string_copy(name);
17291
17292
0
  jit->ctx.flags |= IR_SKIP_PROLOGUE;
17293
17294
0
  return 1;
17295
0
}
17296
17297
static int zend_jit_trace_start(zend_jit_ctx        *jit,
17298
                                const zend_op_array *op_array,
17299
                                zend_ssa            *ssa,
17300
                                zend_string         *name,
17301
                                uint32_t             trace_num,
17302
                                zend_jit_trace_info *parent,
17303
                                uint32_t             exit_num)
17304
0
{
17305
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);
17306
17307
0
  jit->ctx.spill_base = ZREG_FP;
17308
17309
0
  jit->op_array = NULL;
17310
0
  jit->current_op_array = op_array;
17311
0
  jit->ssa = ssa;
17312
0
  jit->name = zend_string_copy(name);
17313
17314
0
  if (!GCC_GLOBAL_REGS) {
17315
0
    if (!parent) {
17316
0
      if (ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL) {
17317
0
        ir_ref execute_data_ref = ir_PARAM(IR_ADDR, "execute_data", 1);
17318
0
        ir_ref opline_ref = ir_PARAM(IR_ADDR, "opline", 2);
17319
0
        jit_STORE_FP(jit, execute_data_ref);
17320
0
        jit_STORE_IP(jit, opline_ref);
17321
0
      }
17322
0
      jit->ctx.flags |= IR_FASTCALL_FUNC;
17323
0
    }
17324
0
  }
17325
17326
0
  if (parent) {
17327
0
    jit->ctx.flags |= IR_SKIP_PROLOGUE;
17328
0
  }
17329
17330
0
  if (parent) {
17331
0
    int i;
17332
0
    int parent_vars_count = parent->exit_info[exit_num].stack_size;
17333
0
    zend_jit_trace_stack *parent_stack = parent_vars_count == 0 ? NULL :
17334
0
      parent->stack_map +
17335
0
      parent->exit_info[exit_num].stack_offset;
17336
17337
    /* prevent clobbering of registers used for deoptimization */
17338
0
    for (i = 0; i < parent_vars_count; i++) {
17339
0
      if (STACK_FLAGS(parent_stack, i) != ZREG_CONST
17340
0
       && STACK_REG(parent_stack, i) != ZREG_NONE) {
17341
0
        int32_t reg = STACK_REG(parent_stack, i);
17342
0
        ir_type type;
17343
17344
0
        if (STACK_FLAGS(parent_stack, i) == ZREG_ZVAL_COPY) {
17345
0
          type = IR_ADDR;
17346
0
        } else if (STACK_TYPE(parent_stack, i) == IS_LONG) {
17347
0
          type = IR_LONG;
17348
0
        } else if (STACK_TYPE(parent_stack, i) == IS_DOUBLE) {
17349
0
          type = IR_DOUBLE;
17350
0
        } else {
17351
0
          ZEND_UNREACHABLE();
17352
0
        }
17353
0
        if (ssa && ssa->vars[i].no_val) {
17354
          /* pass */
17355
0
        } else {
17356
0
          ir_ref ref = ir_RLOAD(type, reg);
17357
17358
0
          if (STACK_FLAGS(parent_stack, i) & (ZREG_LOAD|ZREG_STORE)) {
17359
            /* op3 is used as a flag that the value is already stored in memory.
17360
             * In case the IR framework decides to spill the result of IR_LOAD,
17361
             * it doesn't have to store the value once again.
17362
             *
17363
             * See: insn->op3 check in ir_emit_rload()
17364
             */
17365
0
            ir_set_op(&jit->ctx, ref, 3, EX_NUM_TO_VAR(i));
17366
0
          }
17367
0
        }
17368
0
      }
17369
0
    }
17370
0
  }
17371
17372
0
  if (parent && parent->exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL) {
17373
0
    ZEND_ASSERT(parent->exit_info[exit_num].poly_func.reg >= 0 && parent->exit_info[exit_num].poly_this.reg >= 0);
17374
0
    if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_func.reg)) {
17375
0
      ir_RLOAD_A(parent->exit_info[exit_num].poly_func.reg);
17376
0
    }
17377
0
    if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_this.reg)) {
17378
0
      ir_RLOAD_A(parent->exit_info[exit_num].poly_this.reg);
17379
0
    }
17380
0
  }
17381
17382
0
  ir_STORE(jit_EG(jit_trace_num), ir_CONST_U32(trace_num));
17383
17384
0
  return 1;
17385
0
}
17386
17387
static int zend_jit_trace_begin_loop(zend_jit_ctx *jit)
17388
0
{
17389
0
  return ir_LOOP_BEGIN(ir_END());
17390
0
}
17391
17392
static void zend_jit_trace_gen_phi(zend_jit_ctx *jit, zend_ssa_phi *phi)
17393
0
{
17394
0
  int dst_var = phi->ssa_var;
17395
0
  int src_var = phi->sources[0];
17396
0
  ir_ref ref;
17397
17398
0
  ZEND_ASSERT(!(jit->ra[dst_var].flags & ZREG_LOAD));
17399
0
  ZEND_ASSERT(jit->ra[src_var].ref != IR_UNUSED && jit->ra[src_var].ref != IR_NULL);
17400
17401
0
  ref = ir_PHI_2(
17402
0
    (jit->ssa->var_info[src_var].type & MAY_BE_LONG) ? IR_LONG : IR_DOUBLE,
17403
0
    zend_jit_use_reg(jit, ZEND_ADDR_REG(src_var)), IR_UNUSED);
17404
17405
0
  src_var = phi->sources[1];
17406
0
  ZEND_ASSERT(jit->ra[src_var].ref == IR_NULL);
17407
0
  jit->ra[src_var].flags |= ZREG_FORWARD;
17408
17409
0
  zend_jit_def_reg(jit, ZEND_ADDR_REG(dst_var), ref);
17410
0
}
17411
17412
static int zend_jit_trace_end_loop(zend_jit_ctx *jit, int loop_ref, const void *timeout_exit_addr)
17413
0
{
17414
0
  if (timeout_exit_addr) {
17415
0
    zend_jit_check_timeout(jit, NULL, timeout_exit_addr);
17416
0
  }
17417
0
  ZEND_ASSERT(jit->ctx.ir_base[loop_ref].op2 == IR_UNUSED);
17418
0
  ir_MERGE_SET_OP(loop_ref, 2, ir_LOOP_END());
17419
0
  return 1;
17420
0
}
17421
17422
static int zend_jit_trace_return(zend_jit_ctx *jit, bool original_handler, const zend_op *opline)
17423
0
{
17424
0
  if (GCC_GLOBAL_REGS || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL) {
17425
0
    if (!original_handler) {
17426
0
      zend_jit_tailcall_handler(jit, ir_LOAD_A(jit_IP(jit)));
17427
0
    } else {
17428
0
      zend_jit_tailcall_handler(jit, zend_jit_orig_opline_handler(jit));
17429
0
    }
17430
0
  } else {
17431
0
    if (original_handler) {
17432
0
      ir_ref ref;
17433
0
      ir_ref addr = zend_jit_orig_opline_handler(jit);
17434
17435
#if defined(IR_TARGET_X86)
17436
      addr = ir_CAST_FC_FUNC(addr);
17437
#endif
17438
0
      ref = ir_CALL_2(IR_ADDR, addr, jit_FP(jit), jit_IP(jit));
17439
0
      zend_jit_vm_enter(jit, ref);
17440
0
      return 1;
17441
0
    }
17442
0
    zend_jit_vm_enter(jit, jit_IP(jit));
17443
0
  }
17444
0
  return 1;
17445
0
}
17446
17447
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)
17448
0
{
17449
0
  return ir_patch(code, size, jmp_table_size, zend_jit_trace_get_exit_addr(exit_num), addr);
17450
0
}
17451
17452
static int zend_jit_trace_link_to_root(zend_jit_ctx *jit, zend_jit_trace_info *t, const void *timeout_exit_addr)
17453
0
{
17454
0
  const void *link_addr;
17455
17456
  /* Skip prologue. */
17457
0
  ZEND_ASSERT(zend_jit_trace_prologue_size != (size_t)-1);
17458
0
  link_addr = (const void*)((const char*)t->code_start + zend_jit_trace_prologue_size);
17459
17460
0
  if (timeout_exit_addr) {
17461
0
    zend_jit_check_timeout(jit, NULL, timeout_exit_addr);
17462
0
  }
17463
0
  ir_IJMP(ir_CONST_ADDR(link_addr));
17464
17465
0
  return 1;
17466
0
}
17467
17468
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)
17469
0
{
17470
0
  uint32_t op1_info, op2_info;
17471
17472
0
  switch (opline->opcode) {
17473
0
    case ZEND_SEND_VAR:
17474
0
    case ZEND_SEND_VAL:
17475
0
    case ZEND_SEND_VAL_EX:
17476
0
      return (opline->op2_type != IS_CONST) && (opline->opcode != ZEND_SEND_VAL_EX || opline->op2.num <= MAX_ARG_FLAG_NUM);
17477
0
    case ZEND_QM_ASSIGN:
17478
0
    case ZEND_IS_SMALLER:
17479
0
    case ZEND_IS_SMALLER_OR_EQUAL:
17480
0
    case ZEND_IS_EQUAL:
17481
0
    case ZEND_IS_NOT_EQUAL:
17482
0
    case ZEND_IS_IDENTICAL:
17483
0
    case ZEND_IS_NOT_IDENTICAL:
17484
0
    case ZEND_CASE:
17485
0
      return true;
17486
0
    case ZEND_RETURN:
17487
0
      return (op_array->type != ZEND_EVAL_CODE && op_array->function_name);
17488
0
    case ZEND_ASSIGN:
17489
0
      return (opline->op1_type == IS_CV);
17490
0
    case ZEND_ASSIGN_OP:
17491
0
      if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
17492
0
        return false;
17493
0
      }
17494
0
      op1_info = OP1_INFO();
17495
0
      op2_info = OP2_INFO();
17496
0
      return zend_jit_supported_binary_op(opline->extended_value, op1_info, op2_info);
17497
0
    case ZEND_ADD:
17498
0
    case ZEND_SUB:
17499
0
    case ZEND_MUL:
17500
0
      op1_info = OP1_INFO();
17501
0
      op2_info = OP2_INFO();
17502
0
      if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
17503
0
        return false;
17504
0
      }
17505
0
      if (trace && trace->op1_type != IS_UNKNOWN) {
17506
0
        op1_info &= 1U << (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17507
0
      }
17508
0
      if (trace && trace->op2_type != IS_UNKNOWN) {
17509
0
        op2_info &= 1U << (trace->op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17510
0
      }
17511
0
      return !(op1_info & MAY_BE_UNDEF)
17512
0
        && !(op2_info & MAY_BE_UNDEF)
17513
0
        && (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))
17514
0
        && (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE));
17515
0
    case ZEND_BW_OR:
17516
0
    case ZEND_BW_AND:
17517
0
    case ZEND_BW_XOR:
17518
0
    case ZEND_SL:
17519
0
    case ZEND_SR:
17520
0
    case ZEND_MOD:
17521
0
      op1_info = OP1_INFO();
17522
0
      op2_info = OP2_INFO();
17523
0
      if (trace && trace->op1_type != IS_UNKNOWN) {
17524
0
        op1_info &= 1U << (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17525
0
      }
17526
0
      if (trace && trace->op2_type != IS_UNKNOWN) {
17527
0
        op2_info &= 1U << (trace->op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
17528
0
      }
17529
0
      return (op1_info & MAY_BE_LONG)
17530
0
        && (op2_info & MAY_BE_LONG);
17531
0
    case ZEND_PRE_INC:
17532
0
    case ZEND_PRE_DEC:
17533
0
    case ZEND_POST_INC:
17534
0
    case ZEND_POST_DEC:
17535
0
      op1_info = OP1_INFO();
17536
0
      return opline->op1_type == IS_CV
17537
0
        && (op1_info & MAY_BE_LONG)
17538
0
        && !(op1_info & MAY_BE_REF);
17539
0
    case ZEND_STRLEN:
17540
0
      op1_info = OP1_INFO();
17541
0
      return (opline->op1_type & (IS_CV|IS_CONST))
17542
0
        && (op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_STRING;
17543
0
    case ZEND_COUNT:
17544
0
      op1_info = OP1_INFO();
17545
0
      return (opline->op1_type & (IS_CV|IS_CONST))
17546
0
        && (op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_ARRAY;
17547
0
    case ZEND_JMPZ:
17548
0
    case ZEND_JMPNZ:
17549
0
      if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE) {
17550
0
        if (!ssa->cfg.map) {
17551
0
          return false;
17552
0
        }
17553
0
        if (opline > op_array->opcodes + ssa->cfg.blocks[ssa->cfg.map[opline-op_array->opcodes]].start &&
17554
0
            ((opline-1)->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
17555
0
          return false;
17556
0
        }
17557
0
      }
17558
0
      ZEND_FALLTHROUGH;
17559
0
    case ZEND_BOOL:
17560
0
    case ZEND_BOOL_NOT:
17561
0
    case ZEND_JMPZ_EX:
17562
0
    case ZEND_JMPNZ_EX:
17563
0
      return true;
17564
0
    case ZEND_FETCH_CONSTANT:
17565
0
      return true;
17566
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
17567
0
      if ((opline->extended_value & ZEND_ISEMPTY)) {
17568
0
        return false;
17569
0
      }
17570
0
      ZEND_FALLTHROUGH;
17571
0
    case ZEND_FETCH_DIM_R:
17572
0
    case ZEND_FETCH_DIM_IS:
17573
0
    case ZEND_FETCH_LIST_R:
17574
0
      op1_info = OP1_INFO();
17575
0
      op2_info = OP2_INFO();
17576
0
      if (trace
17577
0
       && trace->op1_type != IS_UNKNOWN
17578
0
       && (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED)) == IS_ARRAY) {
17579
0
        op1_info &= ~((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY);
17580
0
      }
17581
0
      return ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) &&
17582
0
          (((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) ||
17583
0
           ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_STRING));
17584
0
    case ZEND_ASSIGN_DIM_OP:
17585
0
      if (opline->result_type != IS_UNUSED) {
17586
0
        return false;
17587
0
      }
17588
0
      if (!zend_jit_supported_binary_op(opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
17589
0
        return false;
17590
0
      }
17591
0
      ZEND_FALLTHROUGH;
17592
0
    case ZEND_ASSIGN_DIM:
17593
0
    case ZEND_FETCH_DIM_W:
17594
0
    case ZEND_FETCH_DIM_RW:
17595
0
    case ZEND_FETCH_LIST_W:
17596
0
      op1_info = OP1_INFO();
17597
0
      op2_info = OP2_INFO();
17598
0
      if (trace) {
17599
0
        if (opline->op1_type == IS_CV) {
17600
0
          if ((opline->opcode == ZEND_ASSIGN_DIM
17601
0
            || opline->opcode == ZEND_ASSIGN_DIM_OP)
17602
0
           && (opline+1)->op1_type == IS_CV
17603
0
           && (opline+1)->op1.var == opline->op1.var) {
17604
            /* skip $a[x] = $a; */
17605
0
            return false;
17606
0
          }
17607
0
        } else if (opline->op1_type == IS_VAR) {
17608
0
          if (trace->op1_type == IS_UNKNOWN
17609
0
           || !(trace->op1_type & IS_TRACE_INDIRECT)
17610
0
           || opline->result_type != IS_UNUSED) {
17611
0
            return false;
17612
0
          }
17613
0
        }
17614
0
        if (trace->op1_type != IS_UNKNOWN
17615
0
         && (trace->op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED)) == IS_ARRAY) {
17616
0
          op1_info &= ~((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_ARRAY);
17617
0
        }
17618
0
      } else {
17619
0
        if (opline->op1_type != IS_CV) {
17620
0
          return false;
17621
0
        }
17622
0
      }
17623
0
      return ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) &&
17624
0
          (((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) ||
17625
0
           ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_STRING));
17626
0
    case ZEND_ASSIGN_OBJ_OP:
17627
0
      if (opline->result_type != IS_UNUSED) {
17628
0
        return false;
17629
0
      }
17630
0
      if (!zend_jit_supported_binary_op(opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
17631
0
        return false;
17632
0
      }
17633
0
      ZEND_FALLTHROUGH;
17634
0
    case ZEND_FETCH_OBJ_R:
17635
0
    case ZEND_ASSIGN_OBJ:
17636
0
      if (opline->op2_type != IS_CONST
17637
0
       || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
17638
0
       || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
17639
0
        return false;
17640
0
      }
17641
0
      op1_info = OP1_INFO();
17642
0
      return opline->op1_type == IS_UNUSED || (op1_info & MAY_BE_OBJECT);
17643
0
  }
17644
0
  return false;
17645
0
}
17646
17647
static bool zend_jit_var_supports_reg(zend_ssa *ssa, int var)
17648
0
{
17649
0
  if (ssa->vars[var].no_val) {
17650
    /* we don't need the value */
17651
0
    return false;
17652
0
  }
17653
17654
0
  if (!(JIT_G(opt_flags) & ZEND_JIT_REG_ALLOC_GLOBAL)) {
17655
    /* Disable global register allocation,
17656
     * register allocation for SSA variables connected through Phi functions
17657
     */
17658
0
    if (ssa->vars[var].definition_phi) {
17659
0
      return false;
17660
0
    }
17661
0
    if (ssa->vars[var].phi_use_chain) {
17662
0
      zend_ssa_phi *phi = ssa->vars[var].phi_use_chain;
17663
0
      do {
17664
0
        if (!ssa->vars[phi->ssa_var].no_val) {
17665
0
          return false;
17666
0
        }
17667
0
        phi = zend_ssa_next_use_phi(ssa, var, phi);
17668
0
      } while (phi);
17669
0
    }
17670
0
  }
17671
17672
0
  if (((ssa->var_info[var].type & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_DOUBLE) &&
17673
0
      ((ssa->var_info[var].type & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_LONG)) {
17674
      /* bad type */
17675
0
    return false;
17676
0
  }
17677
17678
0
  return true;
17679
0
}
17680
17681
static bool zend_jit_may_be_in_reg(const zend_op_array *op_array, zend_ssa *ssa, int var)
17682
0
{
17683
0
  if (!zend_jit_var_supports_reg(ssa, var)) {
17684
0
    return false;
17685
0
  }
17686
17687
0
  if (ssa->vars[var].definition >= 0) {
17688
0
    uint32_t def = ssa->vars[var].definition;
17689
0
    if (!zend_jit_opline_supports_reg(op_array, ssa, op_array->opcodes + def, ssa->ops + def, NULL)) {
17690
0
      return false;
17691
0
    }
17692
0
  }
17693
17694
0
  if (ssa->vars[var].use_chain >= 0) {
17695
0
    int use = ssa->vars[var].use_chain;
17696
17697
0
    do {
17698
0
      if (!zend_ssa_is_no_val_use(op_array->opcodes + use, ssa->ops + use, var) &&
17699
0
          !zend_jit_opline_supports_reg(op_array, ssa, op_array->opcodes + use, ssa->ops + use, NULL)) {
17700
0
        return false;
17701
0
      }
17702
0
      use = zend_ssa_next_use(ssa->ops, var, use);
17703
0
    } while (use >= 0);
17704
0
  }
17705
17706
0
  if (JIT_G(trigger) != ZEND_JIT_ON_HOT_TRACE) {
17707
0
    uint32_t def_block, use_block;
17708
0
    int b, use;
17709
0
    uint32_t j;
17710
0
    zend_basic_block *bb;
17711
0
    zend_ssa_phi *p;
17712
0
    bool ret = true;
17713
0
    zend_worklist worklist;
17714
0
    ALLOCA_FLAG(use_heap)
17715
17716
    /* Check if live range is split by ENTRY block */
17717
0
    if (ssa->vars[var].definition >= 0) {
17718
0
      def_block =ssa->cfg.map[ssa->vars[var].definition];
17719
0
    } else {
17720
0
      ZEND_ASSERT(ssa->vars[var].definition_phi);
17721
0
      def_block = ssa->vars[var].definition_phi->block;
17722
0
    }
17723
17724
0
    ZEND_WORKLIST_ALLOCA(&worklist, ssa->cfg.blocks_count, use_heap);
17725
17726
0
    if (ssa->vars[var].use_chain >= 0) {
17727
0
      use = ssa->vars[var].use_chain;
17728
0
      do {
17729
0
        use_block = ssa->cfg.map[use];
17730
0
        if (use_block != def_block) {
17731
0
          zend_worklist_push(&worklist, use_block);
17732
0
        }
17733
0
        use = zend_ssa_next_use(ssa->ops, var, use);
17734
0
      } while (use >= 0);
17735
0
    }
17736
17737
0
    p = ssa->vars[var].phi_use_chain;
17738
0
    while (p) {
17739
0
      use_block = p->block;
17740
0
      if (use_block != def_block) {
17741
0
        bb = &ssa->cfg.blocks[use_block];
17742
0
        for (j = 0; j < bb->predecessors_count; j++) {
17743
0
          if (p->sources[j] == var) {
17744
0
            use_block = ssa->cfg.predecessors[bb->predecessor_offset + j];
17745
0
            if (use_block != def_block) {
17746
0
              zend_worklist_push(&worklist, use_block);
17747
0
            }
17748
0
          }
17749
0
        }
17750
0
      }
17751
0
      p = zend_ssa_next_use_phi(ssa, var, p);
17752
0
    }
17753
17754
0
    while (zend_worklist_len(&worklist) != 0) {
17755
0
      b = zend_worklist_pop(&worklist);
17756
0
      bb = &ssa->cfg.blocks[b];
17757
0
      if (bb->flags & (ZEND_BB_ENTRY|ZEND_BB_RECV_ENTRY)) {
17758
0
        ret = false;
17759
0
        break;
17760
0
      }
17761
0
      for (j = 0; j < bb->predecessors_count; j++) {
17762
0
        b = ssa->cfg.predecessors[bb->predecessor_offset + j];
17763
0
        if (b != def_block) {
17764
0
          zend_worklist_push(&worklist, b);
17765
0
        }
17766
0
      }
17767
0
    }
17768
17769
0
    ZEND_WORKLIST_FREE_ALLOCA(&worklist, use_heap);
17770
17771
0
    return ret;
17772
0
  }
17773
17774
0
  return true;
17775
0
}
17776
17777
0
static ir_ref jit_frameless_observer(zend_jit_ctx *jit, const zend_op *opline) {
17778
  // JIT: zend_observer_handler_is_unobserved(ZEND_OBSERVER_DATA(fbc))
17779
0
  ir_ref observer_handler;
17780
0
  zend_function *fbc = ZEND_FLF_FUNC(opline);
17781
  // Not need for runtime cache or generator checks here, we just need if_unobserved
17782
0
  ir_ref if_unobserved = jit_observer_fcall_is_unobserved_start(jit, fbc, &observer_handler, IR_UNUSED, IR_UNUSED).if_unobserved;
17783
17784
  // Call zend_frameless_observed_call for the main logic.
17785
0
  ir_CALL_1(IR_VOID, ir_CONST_ADDR((size_t)zend_frameless_observed_call), jit_FP(jit));
17786
17787
0
  ir_ref skip = ir_END();
17788
0
  ir_IF_TRUE(if_unobserved);
17789
0
  return skip;
17790
0
}
17791
17792
static void jit_frameless_icall0(zend_jit_ctx *jit, const zend_op *opline)
17793
0
{
17794
0
  jit_SET_EX_OPLINE(jit, opline);
17795
17796
0
  void *function = ZEND_FLF_HANDLER(opline);
17797
0
  zend_jit_addr res_addr = RES_ADDR();
17798
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17799
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17800
17801
0
  ir_ref skip_observer = IR_UNUSED;
17802
0
  if (ZEND_OBSERVER_ENABLED) {
17803
0
    skip_observer = jit_frameless_observer(jit, opline);
17804
0
  }
17805
17806
0
  ir_CALL_1(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref);
17807
17808
0
  if (skip_observer != IR_UNUSED) {
17809
0
    ir_MERGE_WITH(skip_observer);
17810
0
  }
17811
17812
0
  zend_jit_check_exception(jit);
17813
0
}
17814
17815
static void jit_frameless_icall1(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
17816
0
{
17817
0
  jit_SET_EX_OPLINE(jit, opline);
17818
17819
  /* Avoid dropping RC check in case op escapes. */
17820
0
  if (op1_info & MAY_BE_RC1) {
17821
0
    op1_info |= MAY_BE_RCN;
17822
0
  }
17823
17824
0
  void *function = ZEND_FLF_HANDLER(opline);
17825
0
  zend_jit_addr res_addr = RES_ADDR();
17826
0
  zend_jit_addr op1_addr = OP1_ADDR();
17827
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17828
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17829
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17830
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17831
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17832
0
    op1_info &= ~MAY_BE_UNDEF;
17833
0
    op1_info |= MAY_BE_NULL;
17834
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17835
0
  }
17836
0
  if (op1_info & MAY_BE_REF) {
17837
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17838
0
  }
17839
17840
0
  ir_ref skip_observer = IR_UNUSED;
17841
0
  if (ZEND_OBSERVER_ENABLED) {
17842
0
    skip_observer = jit_frameless_observer(jit, opline);
17843
0
  }
17844
17845
0
  ir_CALL_2(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref);
17846
17847
0
  if (skip_observer != IR_UNUSED) {
17848
0
    ir_MERGE_WITH(skip_observer);
17849
0
  }
17850
17851
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17852
0
  zend_jit_check_exception(jit);
17853
0
}
17854
17855
static void jit_frameless_icall2(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, uint32_t op2_info)
17856
0
{
17857
0
  jit_SET_EX_OPLINE(jit, opline);
17858
17859
  /* Avoid dropping RC check in case op escapes. */
17860
0
  if (op1_info & MAY_BE_RC1) {
17861
0
    op1_info |= MAY_BE_RCN;
17862
0
  }
17863
0
  if (op2_info & MAY_BE_RC1) {
17864
0
    op2_info |= MAY_BE_RCN;
17865
0
  }
17866
17867
0
  void *function = ZEND_FLF_HANDLER(opline);
17868
0
  zend_jit_addr res_addr = RES_ADDR();
17869
0
  zend_jit_addr op1_addr = OP1_ADDR();
17870
0
  zend_jit_addr op2_addr = OP2_ADDR();
17871
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17872
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17873
0
  ir_ref op2_ref = jit_ZVAL_ADDR(jit, op2_addr);
17874
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17875
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17876
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17877
0
    op1_info &= ~MAY_BE_UNDEF;
17878
0
    op1_info |= MAY_BE_NULL;
17879
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17880
0
  }
17881
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
17882
0
    op2_ref = zend_jit_zval_check_undef(jit, op2_ref, opline->op2.var, opline, true);
17883
0
    op2_info &= ~MAY_BE_UNDEF;
17884
0
    op2_info |= MAY_BE_NULL;
17885
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2_ref);
17886
0
  }
17887
0
  if (op1_info & MAY_BE_REF) {
17888
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17889
0
  }
17890
0
  if (op2_info & MAY_BE_REF) {
17891
0
    op2_ref = jit_ZVAL_DEREF_ref(jit, op2_ref);
17892
0
  }
17893
17894
0
  ir_ref skip_observer = IR_UNUSED;
17895
0
  if (ZEND_OBSERVER_ENABLED) {
17896
0
    skip_observer = jit_frameless_observer(jit, opline);
17897
0
  }
17898
17899
0
  ir_CALL_3(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref, op2_ref);
17900
17901
0
  if (skip_observer != IR_UNUSED) {
17902
0
    ir_MERGE_WITH(skip_observer);
17903
0
  }
17904
17905
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17906
  /* Set OP1 to UNDEF in case FREE_OP2() throws. */
17907
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) != 0
17908
0
   && (opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
17909
0
   && (op2_info & MAY_BE_RC1)
17910
0
   && (op2_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
17911
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_UNDEF);
17912
0
    if (JIT_G(current_frame)) {
17913
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
17914
0
        EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
17915
0
    }
17916
0
  }
17917
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
17918
0
  zend_jit_check_exception(jit);
17919
0
}
17920
17921
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)
17922
0
{
17923
0
  jit_SET_EX_OPLINE(jit, opline);
17924
17925
  /* Avoid dropping RC check in case op escapes. */
17926
0
  if (op1_info & MAY_BE_RC1) {
17927
0
    op1_info |= MAY_BE_RCN;
17928
0
  }
17929
0
  if (op2_info & MAY_BE_RC1) {
17930
0
    op2_info |= MAY_BE_RCN;
17931
0
  }
17932
0
  if (op1_data_info & MAY_BE_RC1) {
17933
0
    op1_data_info |= MAY_BE_RCN;
17934
0
  }
17935
17936
0
  void *function = ZEND_FLF_HANDLER(opline);
17937
0
  uint8_t op_data_type = (opline + 1)->op1_type;
17938
0
  zend_jit_addr res_addr = RES_ADDR();
17939
0
  zend_jit_addr op1_addr = OP1_ADDR();
17940
0
  zend_jit_addr op2_addr = OP2_ADDR();
17941
0
  zend_jit_addr op3_addr = OP1_DATA_ADDR();
17942
0
  ir_ref res_ref = jit_ZVAL_ADDR(jit, res_addr);
17943
0
  ir_ref op1_ref = jit_ZVAL_ADDR(jit, op1_addr);
17944
0
  ir_ref op2_ref = jit_ZVAL_ADDR(jit, op2_addr);
17945
0
  ir_ref op3_ref = jit_ZVAL_ADDR(jit, op3_addr);
17946
0
  jit_set_Z_TYPE_INFO(jit, res_addr, IS_NULL);
17947
0
  if (opline->op1_type == IS_CV && (op1_info & MAY_BE_UNDEF)) {
17948
0
    op1_ref = zend_jit_zval_check_undef(jit, op1_ref, opline->op1.var, opline, true);
17949
0
    op1_info &= ~MAY_BE_UNDEF;
17950
0
    op1_info |= MAY_BE_NULL;
17951
0
    op1_addr = ZEND_ADDR_REF_ZVAL(op1_ref);
17952
0
  }
17953
0
  if (opline->op2_type == IS_CV && (op2_info & MAY_BE_UNDEF)) {
17954
0
    op2_ref = zend_jit_zval_check_undef(jit, op2_ref, opline->op2.var, opline, true);
17955
0
    op2_info &= ~MAY_BE_UNDEF;
17956
0
    op2_info |= MAY_BE_NULL;
17957
0
    op2_addr = ZEND_ADDR_REF_ZVAL(op2_ref);
17958
0
  }
17959
0
  if ((opline+1)->op1_type == IS_CV && (op1_data_info & MAY_BE_UNDEF)) {
17960
0
    op3_ref = zend_jit_zval_check_undef(jit, op3_ref, (opline+1)->op1.var, opline, true);
17961
0
    op1_data_info &= ~MAY_BE_UNDEF;
17962
0
    op1_data_info |= MAY_BE_NULL;
17963
0
    op3_addr = ZEND_ADDR_REF_ZVAL(op3_ref);
17964
0
  }
17965
0
  if (op1_info & MAY_BE_REF) {
17966
0
    op1_ref = jit_ZVAL_DEREF_ref(jit, op1_ref);
17967
0
  }
17968
0
  if (op2_info & MAY_BE_REF) {
17969
0
    op2_ref = jit_ZVAL_DEREF_ref(jit, op2_ref);
17970
0
  }
17971
0
  if (op1_data_info & MAY_BE_REF) {
17972
0
    op3_ref = jit_ZVAL_DEREF_ref(jit, op3_ref);
17973
0
  }
17974
17975
0
  ir_ref skip_observer = IR_UNUSED;
17976
0
  if (ZEND_OBSERVER_ENABLED) {
17977
0
    skip_observer = jit_frameless_observer(jit, opline);
17978
0
  }
17979
17980
0
  ir_CALL_4(IR_VOID, ir_CONST_ADDR((size_t)function), res_ref, op1_ref, op2_ref, op3_ref);
17981
17982
0
  if (skip_observer != IR_UNUSED) {
17983
0
    ir_MERGE_WITH(skip_observer);
17984
0
  }
17985
17986
0
  jit_FREE_OP(jit, opline->op1_type, opline->op1, op1_info, NULL);
17987
  /* Set OP1 to UNDEF in case FREE_OP2() throws. */
17988
0
  bool op1_undef = false;
17989
0
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
17990
0
   && (((opline->op2_type & (IS_VAR|IS_TMP_VAR))
17991
0
     && (op2_info & MAY_BE_RC1)
17992
0
     && (op2_info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)))
17993
0
    || ((op_data_type & (IS_VAR|IS_TMP_VAR))
17994
0
     && (op1_data_info & MAY_BE_RC1)
17995
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))))) {
17996
0
      op1_undef = true;
17997
0
    jit_set_Z_TYPE_INFO(jit, op1_addr, IS_UNDEF);
17998
0
    if (JIT_G(current_frame)) {
17999
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
18000
0
        EX_VAR_TO_NUM(opline->op1.var), IS_UNKNOWN, 1);
18001
0
    }
18002
0
  }
18003
0
  jit_FREE_OP(jit, opline->op2_type, opline->op2, op2_info, NULL);
18004
  /* If OP1 is set to UNDEF, we don't need to set OP2 to UNDEF on free because
18005
   * zend_fetch_debug_backtrace aborts when it encounters the first UNDEF TMP|VAR. */
18006
0
  if (!op1_undef
18007
0
   && (opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
18008
0
   && (op_data_type & (IS_VAR|IS_TMP_VAR)) != 0
18009
0
   && (op1_data_info & MAY_BE_RC1)
18010
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))) {
18011
0
    jit_set_Z_TYPE_INFO(jit, op2_addr, IS_UNDEF);
18012
0
    if (JIT_G(current_frame)) {
18013
0
      SET_STACK_TYPE(JIT_G(current_frame)->stack,
18014
0
        EX_VAR_TO_NUM(opline->op2.var), IS_UNKNOWN, 1);
18015
0
    }
18016
0
  }
18017
  jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, op1_data_info, NULL);
18018
0
  zend_jit_check_exception(jit);
18019
0
}
18020
18021
/*
18022
 * Local variables:
18023
 * tab-width: 4
18024
 * c-basic-offset: 4
18025
 * indent-tabs-mode: t
18026
 * End:
18027
 */