php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_inference.h
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Zend Engine, e-SSA based Type & Range Inference |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | https://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Dmitry Stogov <dmitry@php.net> |
16 +----------------------------------------------------------------------+
17*/
18
19#ifndef ZEND_INFERENCE_H
20#define ZEND_INFERENCE_H
21
22#include "zend_optimizer.h"
23#include "zend_ssa.h"
24#include "zend_bitset.h"
25
26/* Bitmask for type inference (zend_ssa_var_info.type) */
27#include "zend_type_info.h"
28
29#include <stdint.h>
30
31#define MAY_BE_PACKED_GUARD (1<<27) /* needs packed array guard */
32#define MAY_BE_CLASS_GUARD (1<<27) /* needs class guard */
33#define MAY_BE_GUARD (1<<28) /* needs type guard */
34
35#define MAY_HAVE_DTOR \
36 (MAY_BE_OBJECT|MAY_BE_RESOURCE \
37 |MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE)
38
39#define DEFINE_SSA_OP_HAS_RANGE(opN) \
40 static zend_always_inline bool _ssa_##opN##_has_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
41 { \
42 if (opline->opN##_type == IS_CONST) { \
43 zval *zv = CRT_CONSTANT(opline->opN); \
44 return (Z_TYPE_P(zv) == IS_LONG); \
45 } else { \
46 return (opline->opN##_type != IS_UNUSED && \
47 ssa->var_info && \
48 ssa_op->opN##_use >= 0 && \
49 ssa->var_info[ssa_op->opN##_use].has_range); \
50 } \
51 return 0; \
52 } \
53
54#define DEFINE_SSA_OP_MIN_RANGE(opN) \
55 static zend_always_inline zend_long _ssa_##opN##_min_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
56 { \
57 if (opline->opN##_type == IS_CONST) { \
58 zval *zv = CRT_CONSTANT(opline->opN); \
59 if (Z_TYPE_P(zv) == IS_LONG) { \
60 return Z_LVAL_P(zv); \
61 } \
62 } else if (opline->opN##_type != IS_UNUSED && \
63 ssa->var_info && \
64 ssa_op->opN##_use >= 0 && \
65 ssa->var_info[ssa_op->opN##_use].has_range) { \
66 return ssa->var_info[ssa_op->opN##_use].range.min; \
67 } \
68 return ZEND_LONG_MIN; \
69 } \
70
71#define DEFINE_SSA_OP_MAX_RANGE(opN) \
72 static zend_always_inline zend_long _ssa_##opN##_max_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
73 { \
74 if (opline->opN##_type == IS_CONST) { \
75 zval *zv = CRT_CONSTANT(opline->opN); \
76 if (Z_TYPE_P(zv) == IS_LONG) { \
77 return Z_LVAL_P(zv); \
78 } \
79 } else if (opline->opN##_type != IS_UNUSED && \
80 ssa->var_info && \
81 ssa_op->opN##_use >= 0 && \
82 ssa->var_info[ssa_op->opN##_use].has_range) { \
83 return ssa->var_info[ssa_op->opN##_use].range.max; \
84 } \
85 return ZEND_LONG_MAX; \
86 } \
87
88#define DEFINE_SSA_OP_RANGE_UNDERFLOW(opN) \
89 static zend_always_inline char _ssa_##opN##_range_underflow(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
90 { \
91 if (opline->opN##_type == IS_CONST) { \
92 zval *zv = CRT_CONSTANT(opline->opN); \
93 if (Z_TYPE_P(zv) == IS_LONG) { \
94 return 0; \
95 } \
96 } else if (opline->opN##_type != IS_UNUSED && \
97 ssa->var_info && \
98 ssa_op->opN##_use >= 0 && \
99 ssa->var_info[ssa_op->opN##_use].has_range) { \
100 return ssa->var_info[ssa_op->opN##_use].range.underflow; \
101 } \
102 return 1; \
103 } \
104
105#define DEFINE_SSA_OP_RANGE_OVERFLOW(opN) \
106 static zend_always_inline char _ssa_##opN##_range_overflow(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
107 { \
108 if (opline->opN##_type == IS_CONST) { \
109 zval *zv = CRT_CONSTANT(opline->opN); \
110 if (Z_TYPE_P(zv) == IS_LONG) { \
111 return 0; \
112 } \
113 } else if (opline->opN##_type != IS_UNUSED && \
114 ssa->var_info && \
115 ssa_op->opN##_use >= 0 && \
116 ssa->var_info[ssa_op->opN##_use].has_range) { \
117 return ssa->var_info[ssa_op->opN##_use].range.overflow; \
118 } \
119 return 1; \
120 } \
121
132
133#define OP1_HAS_RANGE() (_ssa_op1_has_range (op_array, ssa, opline, ssa_op))
134#define OP1_MIN_RANGE() (_ssa_op1_min_range (op_array, ssa, opline, ssa_op))
135#define OP1_MAX_RANGE() (_ssa_op1_max_range (op_array, ssa, opline, ssa_op))
136#define OP1_RANGE_UNDERFLOW() (_ssa_op1_range_underflow (op_array, ssa, opline, ssa_op))
137#define OP1_RANGE_OVERFLOW() (_ssa_op1_range_overflow (op_array, ssa, opline, ssa_op))
138#define OP2_HAS_RANGE() (_ssa_op2_has_range (op_array, ssa, opline, ssa_op))
139#define OP2_MIN_RANGE() (_ssa_op2_min_range (op_array, ssa, opline, ssa_op))
140#define OP2_MAX_RANGE() (_ssa_op2_max_range (op_array, ssa, opline, ssa_op))
141#define OP2_RANGE_UNDERFLOW() (_ssa_op2_range_underflow (op_array, ssa, opline, ssa_op))
142#define OP2_RANGE_OVERFLOW() (_ssa_op2_range_overflow (op_array, ssa, opline, ssa_op))
143
147
148static zend_always_inline uint32_t _const_op_type(const zval *zv) {
149 if (Z_TYPE_P(zv) == IS_CONSTANT_AST) {
151 } else if (Z_TYPE_P(zv) == IS_ARRAY) {
152 return zend_array_type_info(zv);
153 } else {
154 uint32_t tmp = (1 << Z_TYPE_P(zv));
155
156 if (Z_REFCOUNTED_P(zv)) {
157 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
158 } else if (Z_TYPE_P(zv) == IS_STRING) {
159 tmp |= MAY_BE_RCN;
160 }
161 return tmp;
162 }
163}
164
165static zend_always_inline uint32_t get_ssa_var_info(const zend_ssa *ssa, int ssa_var_num)
166{
167 if (ssa->var_info && ssa_var_num >= 0) {
168 return ssa->var_info[ssa_var_num].type;
169 } else {
171 }
172}
173
174#define DEFINE_SSA_OP_INFO(opN) \
175 static zend_always_inline uint32_t _ssa_##opN##_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
176 { \
177 if (opline->opN##_type == IS_CONST) { \
178 return _const_op_type(CRT_CONSTANT(opline->opN)); \
179 } else { \
180 return get_ssa_var_info(ssa, ssa->var_info ? ssa_op->opN##_use : -1); \
181 } \
182 } \
183
184#define DEFINE_SSA_OP_DEF_INFO(opN) \
185 static zend_always_inline uint32_t _ssa_##opN##_def_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
186 { \
187 return get_ssa_var_info(ssa, ssa->var_info ? ssa_op->opN##_def : -1); \
188 } \
189
190
197
198#define OP1_INFO() (_ssa_op1_info(op_array, ssa, opline, ssa_op))
199#define OP2_INFO() (_ssa_op2_info(op_array, ssa, opline, ssa_op))
200#define OP1_DATA_INFO() (_ssa_op1_info(op_array, ssa, (opline+1), ssa_op ? (ssa_op+1) : NULL))
201#define OP2_DATA_INFO() (_ssa_op2_info(op_array, ssa, (opline+1), ssa_op ? (ssa_op+1) : NULL))
202#define RES_USE_INFO() (_ssa_result_info(op_array, ssa, opline, ssa_op))
203#define OP1_DEF_INFO() (_ssa_op1_def_info(op_array, ssa, opline, ssa_op))
204#define OP2_DEF_INFO() (_ssa_op2_def_info(op_array, ssa, opline, ssa_op))
205#define OP1_DATA_DEF_INFO() (_ssa_op1_def_info(op_array, ssa, (opline+1), ssa_op ? (ssa_op+1) : NULL))
206#define OP2_DATA_DEF_INFO() (_ssa_op2_def_info(op_array, ssa, (opline+1), ssa_op ? (ssa_op+1) : NULL))
207#define RES_INFO() (_ssa_result_def_info(op_array, ssa, opline, ssa_op))
208
209static zend_always_inline bool zend_add_will_overflow(zend_long a, zend_long b) {
210 return (b > 0 && a > ZEND_LONG_MAX - b)
211 || (b < 0 && a < ZEND_LONG_MIN - b);
212}
213static zend_always_inline bool zend_sub_will_overflow(zend_long a, zend_long b) {
214 return (b > 0 && a < ZEND_LONG_MIN + b)
215 || (b < 0 && a > ZEND_LONG_MAX + b);
216}
217
219
221ZEND_API void zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa);
222ZEND_API zend_result zend_ssa_inference(zend_arena **raena, const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level);
223
224ZEND_API uint32_t zend_array_element_type(uint32_t t1, uint8_t op_type, int write, int insert);
225
226ZEND_API bool zend_inference_propagate_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op* ssa_op, int var, zend_ssa_range *tmp);
227
229 const zend_script *script, const zend_arg_info *arg_info, zend_class_entry **pce);
231 const zend_op_array *op_array, const zend_script *script, zend_ssa_var_info *ret);
233 const zend_function *func, const zend_script *script,
234 zend_class_entry **ce, bool *ce_is_instanceof, bool use_tentative_return_info);
235
236ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa, uint32_t t1, uint32_t t2);
237ZEND_API bool zend_may_throw(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa);
238
240 const zend_op_array *op_array, zend_ssa *ssa, const zend_script *script,
241 zend_op *opline, zend_ssa_op *ssa_op, const zend_op **ssa_opcodes,
242 zend_long optimization_level);
243
245
246#endif /* ZEND_INFERENCE_H */
zval * zv
Definition ffi.c:3975
#define t1
#define t2
zend_ssa_var_info * var_info
Definition zend_ssa.h:142
$obj a
Definition test.php:84
struct _zend_arena zend_arena
Definition zend_arena.h:26
struct _zval_struct zval
execute_data func
struct _zend_op zend_op
struct _zend_op_array zend_op_array
struct _zend_arg_info zend_arg_info
#define ZEND_API
union _zend_function zend_function
ZEND_API uint32_t zend_array_element_type(uint32_t t1, uint8_t op_type, int write, int insert)
uint32_t zend_get_return_info_from_signature_only(const zend_function *func, const zend_script *script, zend_class_entry **ce, bool *ce_is_instanceof, bool use_tentative_return_info)
ZEND_API bool zend_inference_propagate_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op, int var, zend_ssa_range *tmp)
ZEND_API void zend_init_func_return_info(const zend_op_array *op_array, const zend_script *script, zend_ssa_var_info *ret)
ZEND_API bool zend_may_throw(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa)
ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa, uint32_t t1, uint32_t t2)
ZEND_API void zend_ssa_find_false_dependencies(const zend_op_array *op_array, zend_ssa *ssa)
ZEND_API zend_result zend_update_type_info(const zend_op_array *op_array, zend_ssa *ssa, const zend_script *script, zend_op *opline, zend_ssa_op *ssa_op, const zend_op **ssa_opcodes, zend_long optimization_level)
ZEND_API void zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa)
ZEND_API uint32_t ZEND_FASTCALL zend_array_type_info(const zval *zv)
ZEND_API uint32_t zend_fetch_arg_info_type(const zend_script *script, const zend_arg_info *arg_info, zend_class_entry **pce)
ZEND_API zend_result zend_ssa_inference(zend_arena **arena, const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level)
#define DEFINE_SSA_OP_MAX_RANGE(opN)
#define DEFINE_SSA_OP_MIN_RANGE(opN)
#define DEFINE_SSA_OP_RANGE_UNDERFLOW(opN)
#define DEFINE_SSA_OP_HAS_RANGE(opN)
ZEND_API uint32_t ZEND_FASTCALL zend_array_type_info(const zval *zv)
#define DEFINE_SSA_OP_INFO(opN)
#define DEFINE_SSA_OP_RANGE_OVERFLOW(opN)
#define DEFINE_SSA_OP_DEF_INFO(opN)
int32_t zend_long
Definition zend_long.h:42
#define ZEND_LONG_MIN
Definition zend_long.h:46
#define ZEND_LONG_MAX
Definition zend_long.h:45
struct _zend_script zend_script
#define END_EXTERN_C()
#define zend_always_inline
#define ZEND_FASTCALL
#define BEGIN_EXTERN_C()
struct _zend_class_entry zend_class_entry
struct _zend_ssa_range zend_ssa_range
struct _zend_ssa zend_ssa
struct _zend_ssa_op zend_ssa_op
struct _zend_ssa_var_info zend_ssa_var_info
#define MAY_BE_REF
#define MAY_BE_ARRAY_OF_ANY
#define MAY_BE_RC1
#define MAY_BE_ARRAY_OF_REF
#define MAY_BE_UNDEF
#define MAY_BE_ANY
#define MAY_BE_ARRAY_KEY_ANY
#define MAY_BE_INDIRECT
#define MAY_BE_RCN
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_STRING
Definition zend_types.h:606
#define Z_REFCOUNTED_P(zval_p)
Definition zend_types.h:921
#define IS_ARRAY
Definition zend_types.h:607
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define IS_CONSTANT_AST
Definition zend_types.h:611
bool result
op2
op1
zval * ret