1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend 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 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #ifndef ZEND_COMPILE_H
21 #define ZEND_COMPILE_H
22
23 #include "zend.h"
24 #include "zend_ast.h"
25
26 #ifdef HAVE_STDARG_H
27 # include <stdarg.h>
28 #endif
29
30 #include "zend_llist.h"
31
32 #define SET_UNUSED(op) op ## _type = IS_UNUSED
33
34 #define MAKE_NOP(opline) do { \
35 (opline)->op1.num = 0; \
36 (opline)->op2.num = 0; \
37 (opline)->result.num = 0; \
38 (opline)->opcode = ZEND_NOP; \
39 (opline)->op1_type = IS_UNUSED; \
40 (opline)->op2_type = IS_UNUSED; \
41 (opline)->result_type = IS_UNUSED; \
42 } while (0)
43
44 #define RESET_DOC_COMMENT() do { \
45 if (CG(doc_comment)) { \
46 zend_string_release_ex(CG(doc_comment), 0); \
47 CG(doc_comment) = NULL; \
48 } \
49 } while (0)
50
51 typedef struct _zend_op_array zend_op_array;
52 typedef struct _zend_op zend_op;
53
54 /* On 64-bit systems less optimal, but more compact VM code leads to better
55 * performance. So on 32-bit systems we use absolute addresses for jump
56 * targets and constants, but on 64-bit systems realtive 32-bit offsets */
57 #if SIZEOF_SIZE_T == 4
58 # define ZEND_USE_ABS_JMP_ADDR 1
59 # define ZEND_USE_ABS_CONST_ADDR 1
60 # define ZEND_EX_USE_RUN_TIME_CACHE 1
61 #else
62 # define ZEND_USE_ABS_JMP_ADDR 0
63 # define ZEND_USE_ABS_CONST_ADDR 0
64 # define ZEND_EX_USE_RUN_TIME_CACHE 1
65 #endif
66
67 typedef union _znode_op {
68 uint32_t constant;
69 uint32_t var;
70 uint32_t num;
71 uint32_t opline_num; /* Needs to be signed */
72 #if ZEND_USE_ABS_JMP_ADDR
73 zend_op *jmp_addr;
74 #else
75 uint32_t jmp_offset;
76 #endif
77 #if ZEND_USE_ABS_CONST_ADDR
78 zval *zv;
79 #endif
80 } znode_op;
81
82 typedef struct _znode { /* used only during compilation */
83 zend_uchar op_type;
84 zend_uchar flag;
85 union {
86 znode_op op;
87 zval constant; /* replaced by literal/zv */
88 } u;
89 } znode;
90
91 /* Temporarily defined here, to avoid header ordering issues */
92 typedef struct _zend_ast_znode {
93 zend_ast_kind kind;
94 zend_ast_attr attr;
95 uint32_t lineno;
96 znode node;
97 } zend_ast_znode;
98
99 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(znode *node);
100
zend_ast_get_znode(zend_ast * ast)101 static zend_always_inline znode *zend_ast_get_znode(zend_ast *ast) {
102 return &((zend_ast_znode *) ast)->node;
103 }
104
105 typedef struct _zend_declarables {
106 zend_long ticks;
107 } zend_declarables;
108
109 /* Compilation context that is different for each file, but shared between op arrays. */
110 typedef struct _zend_file_context {
111 zend_declarables declarables;
112 znode implementing_class;
113
114 zend_string *current_namespace;
115 zend_bool in_namespace;
116 zend_bool has_bracketed_namespaces;
117
118 HashTable *imports;
119 HashTable *imports_function;
120 HashTable *imports_const;
121
122 HashTable seen_symbols;
123 } zend_file_context;
124
125 typedef union _zend_parser_stack_elem {
126 zend_ast *ast;
127 zend_string *str;
128 zend_ulong num;
129 } zend_parser_stack_elem;
130
131 void zend_compile_top_stmt(zend_ast *ast);
132 void zend_compile_stmt(zend_ast *ast);
133 void zend_compile_expr(znode *node, zend_ast *ast);
134 void zend_compile_var(znode *node, zend_ast *ast, uint32_t type);
135 void zend_eval_const_expr(zend_ast **ast_ptr);
136 void zend_const_expr_to_zval(zval *result, zend_ast *ast);
137
138 typedef int (*user_opcode_handler_t) (zend_execute_data *execute_data);
139
140 struct _zend_op {
141 const void *handler;
142 znode_op op1;
143 znode_op op2;
144 znode_op result;
145 uint32_t extended_value;
146 uint32_t lineno;
147 zend_uchar opcode;
148 zend_uchar op1_type;
149 zend_uchar op2_type;
150 zend_uchar result_type;
151 };
152
153
154 typedef struct _zend_brk_cont_element {
155 int start;
156 int cont;
157 int brk;
158 int parent;
159 zend_bool is_switch;
160 } zend_brk_cont_element;
161
162 typedef struct _zend_label {
163 int brk_cont;
164 uint32_t opline_num;
165 } zend_label;
166
167 typedef struct _zend_try_catch_element {
168 uint32_t try_op;
169 uint32_t catch_op; /* ketchup! */
170 uint32_t finally_op;
171 uint32_t finally_end;
172 } zend_try_catch_element;
173
174 #define ZEND_LIVE_TMPVAR 0
175 #define ZEND_LIVE_LOOP 1
176 #define ZEND_LIVE_SILENCE 2
177 #define ZEND_LIVE_ROPE 3
178 #define ZEND_LIVE_MASK 3
179
180 typedef struct _zend_live_range {
181 uint32_t var; /* low bits are used for variable type (ZEND_LIVE_* macros) */
182 uint32_t start;
183 uint32_t end;
184 } zend_live_range;
185
186 /* Compilation context that is different for each op array. */
187 typedef struct _zend_oparray_context {
188 uint32_t opcodes_size;
189 int vars_size;
190 int literals_size;
191 int backpatch_count;
192 uint32_t fast_call_var;
193 uint32_t try_catch_offset;
194 int current_brk_cont;
195 int last_brk_cont;
196 zend_brk_cont_element *brk_cont_array;
197 HashTable *labels;
198 } zend_oparray_context;
199
200 /* Class, property and method flags class|meth.|prop.|const*/
201 /* | | | */
202 /* Common flags | | | */
203 /* ============ | | | */
204 /* | | | */
205 /* Staic method or property | | | */
206 #define ZEND_ACC_STATIC (1 << 0) /* | X | X | */
207 /* | | | */
208 /* Final class or method | | | */
209 #define ZEND_ACC_FINAL (1 << 2) /* X | X | | */
210 /* | | | */
211 /* Visibility flags (public < protected < private) | | | */
212 #define ZEND_ACC_PUBLIC (1 << 8) /* | X | X | X */
213 #define ZEND_ACC_PROTECTED (1 << 9) /* | X | X | X */
214 #define ZEND_ACC_PRIVATE (1 << 10) /* | X | X | X */
215 /* | | | */
216 /* TODO: explain the name ??? | | | */
217 #define ZEND_ACC_CHANGED (1 << 11) /* | X | X | */
218 /* | | | */
219 /* TODO: used only by ext/reflection ??? | | | */
220 #define ZEND_ACC_IMPLICIT_PUBLIC (1 << 12) /* | ? | ? | ? */
221 /* | | | */
222 /* Shadow of parent's private method/property | | | */
223 #define ZEND_ACC_SHADOW (1 << 17) /* | ? | X | */
224 /* | | | */
225 /* Class Flags (unused: 0, 1, 3, 11-18, 21, 25...) | | | */
226 /* =========== | | | */
227 /* | | | */
228 /* class is abstarct, since it is set by any | | | */
229 /* abstract method | | | */
230 #define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS (1 << 4) /* X | | | */
231 /* | | | */
232 /* Class is explicitly defined as abstract by using | | | */
233 /* the keyword. | | | */
234 #define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS (1 << 5) /* X | | | */
235 /* | | | */
236 /* Special class types | | | */
237 #define ZEND_ACC_INTERFACE (1 << 6) /* X | | | */
238 #define ZEND_ACC_TRAIT (1 << 7) /* X | | | */
239 #define ZEND_ACC_ANON_CLASS (1 << 8) /* X | | | */
240 /* | | | */
241 /* Bound anonymous class | | | */
242 #define ZEND_ACC_ANON_BOUND (1 << 9) /* X | | | */
243 /* | | | */
244 /* Class extends another class | | | */
245 #define ZEND_ACC_INHERITED (1 << 10) /* X | | | */
246 /* | | | */
247 /* Class implements interface(s) | | | */
248 #define ZEND_ACC_IMPLEMENT_INTERFACES (1 << 19) /* X | | | */
249 /* | | | */
250 /* Class constants updated | | | */
251 #define ZEND_ACC_CONSTANTS_UPDATED (1 << 20) /* X | | | */
252 /* | | | */
253 /* Class uses trait(s) | | | */
254 #define ZEND_ACC_IMPLEMENT_TRAITS (1 << 22) /* X | | | */
255 /* | | | */
256 /* User class has methods with static variables | | | */
257 #define ZEND_HAS_STATIC_IN_METHODS (1 << 23) /* X | | | */
258 /* | | | */
259 /* Class has magic methods __get/__set/__unset/ | | | */
260 /* __isset that use guards | | | */
261 #define ZEND_ACC_USE_GUARDS (1 << 24) /* X | | | */
262 /* | | | */
263 /* Function Flags (unused: 4, 5, 17?) | | | */
264 /* ============== | | | */
265 /* | | | */
266 /* Abstarct method | | | */
267 #define ZEND_ACC_ABSTRACT (1 << 1) /* | X | | */
268 /* | | | */
269 /* TODO: used only during inheritance ??? | | | */
270 #define ZEND_ACC_IMPLEMENTED_ABSTRACT (1 << 3) /* | X | | */
271 /* | | | */
272 #define ZEND_ACC_FAKE_CLOSURE (1 << 6) /* | X | | */
273 /* | | | */
274 /* method flag used by Closure::__invoke() | | | */
275 #define ZEND_ACC_USER_ARG_INFO (1 << 7) /* | X | | */
276 /* | | | */
277 /* method flags (special method detection) | | | */
278 #define ZEND_ACC_CTOR (1 << 13) /* | X | | */
279 #define ZEND_ACC_DTOR (1 << 14) /* | X | | */
280 /* | | | */
281 /* "main" op_array with | | | */
282 /* ZEND_DECLARE_INHERITED_CLASS_DELAYED opcodes | | | */
283 #define ZEND_ACC_EARLY_BINDING (1 << 15) /* | X | | */
284 /* | | | */
285 /* method flag (bc only), any method that has this | | | */
286 /* flag can be used statically and non statically. | | | */
287 #define ZEND_ACC_ALLOW_STATIC (1 << 16) /* | X | | */
288 /* | | | */
289 /* deprecation flag | | | */
290 #define ZEND_ACC_DEPRECATED (1 << 18) /* | X | | */
291 /* | | | */
292 #define ZEND_ACC_NO_RT_ARENA (1 << 19) /* | X | | */
293 /* | | | */
294 #define ZEND_ACC_CLOSURE (1 << 20) /* | X | | */
295 /* | | | */
296 /* call through user function trampoline. e.g. | | | */
297 /* __call, __callstatic | | | */
298 #define ZEND_ACC_CALL_VIA_TRAMPOLINE (1 << 21) /* | X | | */
299 /* | | | */
300 /* disable inline caching | | | */
301 #define ZEND_ACC_NEVER_CACHE (1 << 22) /* | X | | */
302 /* | | | */
303 #define ZEND_ACC_GENERATOR (1 << 23) /* | X | | */
304 /* | | | */
305 /* Function with varable number of arguments | | | */
306 #define ZEND_ACC_VARIADIC (1 << 24) /* | X | | */
307 /* | | | */
308 /* Immutable op_array (lazy loading) | | | */
309 #define ZEND_ACC_IMMUTABLE (1 << 25) /* | X | | */
310 /* | | | */
311 /* Function returning by reference | | | */
312 #define ZEND_ACC_RETURN_REFERENCE (1 << 26) /* | X | | */
313 /* | | | */
314 #define ZEND_ACC_DONE_PASS_TWO (1 << 27) /* | X | | */
315 /* | | | */
316 /* Function has typed arguments | | | */
317 #define ZEND_ACC_HAS_TYPE_HINTS (1 << 28) /* | X | | */
318 /* | | | */
319 /* op_array has finally blocks (user only) | | | */
320 #define ZEND_ACC_HAS_FINALLY_BLOCK (1 << 29) /* | X | | */
321 /* | | | */
322 /* internal function is allocated at arena (int only) | | | */
323 #define ZEND_ACC_ARENA_ALLOCATED (1 << 29) /* | X | | */
324 /* | | | */
325 /* Function has a return type | | | */
326 #define ZEND_ACC_HAS_RETURN_TYPE (1 << 30) /* | X | | */
327 /* | | | */
328 /* op_array uses strict mode types | | | */
329 #define ZEND_ACC_STRICT_TYPES (1 << 31) /* | X | | */
330
331
332 #define ZEND_ACC_PPP_MASK (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)
333
334 /* call through internal function handler. e.g. Closure::invoke() */
335 #define ZEND_ACC_CALL_VIA_HANDLER ZEND_ACC_CALL_VIA_TRAMPOLINE
336
337 char *zend_visibility_string(uint32_t fn_flags);
338
339 typedef struct _zend_property_info {
340 uint32_t offset; /* property offset for object properties or
341 property index for static properties */
342 uint32_t flags;
343 zend_string *name;
344 zend_string *doc_comment;
345 zend_class_entry *ce;
346 } zend_property_info;
347
348 #define OBJ_PROP(obj, offset) \
349 ((zval*)((char*)(obj) + offset))
350 #define OBJ_PROP_NUM(obj, num) \
351 (&(obj)->properties_table[(num)])
352 #define OBJ_PROP_TO_OFFSET(num) \
353 ((uint32_t)(zend_uintptr_t)OBJ_PROP_NUM(((zend_object*)NULL), num))
354 #define OBJ_PROP_TO_NUM(offset) \
355 ((offset - OBJ_PROP_TO_OFFSET(0)) / sizeof(zval))
356
357 typedef struct _zend_class_constant {
358 zval value; /* access flags are stored in reserved: zval.u2.access_flags */
359 zend_string *doc_comment;
360 zend_class_entry *ce;
361 } zend_class_constant;
362
363 /* arg_info for internal functions */
364 typedef struct _zend_internal_arg_info {
365 const char *name;
366 zend_type type;
367 zend_uchar pass_by_reference;
368 zend_bool is_variadic;
369 } zend_internal_arg_info;
370
371 /* arg_info for user functions */
372 typedef struct _zend_arg_info {
373 zend_string *name;
374 zend_type type;
375 zend_uchar pass_by_reference;
376 zend_bool is_variadic;
377 } zend_arg_info;
378
379 /* the following structure repeats the layout of zend_internal_arg_info,
380 * but its fields have different meaning. It's used as the first element of
381 * arg_info array to define properties of internal functions.
382 * It's also used for the return type.
383 */
384 typedef struct _zend_internal_function_info {
385 zend_uintptr_t required_num_args;
386 zend_type type;
387 zend_bool return_reference;
388 zend_bool _is_variadic;
389 } zend_internal_function_info;
390
391 struct _zend_op_array {
392 /* Common elements */
393 zend_uchar type;
394 zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
395 uint32_t fn_flags;
396 zend_string *function_name;
397 zend_class_entry *scope;
398 zend_function *prototype;
399 uint32_t num_args;
400 uint32_t required_num_args;
401 zend_arg_info *arg_info;
402 /* END of common elements */
403
404 int cache_size; /* number of run_time_cache_slots * sizeof(void*) */
405 int last_var; /* number of CV variables */
406 uint32_t T; /* number of temporary variables */
407 uint32_t last; /* number of opcodes */
408
409 zend_op *opcodes;
410 void **run_time_cache;
411 HashTable *static_variables;
412 zend_string **vars; /* names of CV variables */
413
414 uint32_t *refcount;
415
416 int last_live_range;
417 int last_try_catch;
418 zend_live_range *live_range;
419 zend_try_catch_element *try_catch_array;
420
421 zend_string *filename;
422 uint32_t line_start;
423 uint32_t line_end;
424 zend_string *doc_comment;
425
426 int last_literal;
427 zval *literals;
428
429 void *reserved[ZEND_MAX_RESERVED_RESOURCES];
430 };
431
432
433 #define ZEND_RETURN_VALUE 0
434 #define ZEND_RETURN_REFERENCE 1
435
436 /* zend_internal_function_handler */
437 typedef void (ZEND_FASTCALL *zif_handler)(INTERNAL_FUNCTION_PARAMETERS);
438
439 typedef struct _zend_internal_function {
440 /* Common elements */
441 zend_uchar type;
442 zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
443 uint32_t fn_flags;
444 zend_string* function_name;
445 zend_class_entry *scope;
446 zend_function *prototype;
447 uint32_t num_args;
448 uint32_t required_num_args;
449 zend_internal_arg_info *arg_info;
450 /* END of common elements */
451
452 zif_handler handler;
453 struct _zend_module_entry *module;
454 void *reserved[ZEND_MAX_RESERVED_RESOURCES];
455 } zend_internal_function;
456
457 #define ZEND_FN_SCOPE_NAME(function) ((function) && (function)->common.scope ? ZSTR_VAL((function)->common.scope->name) : "")
458
459 union _zend_function {
460 zend_uchar type; /* MUST be the first element of this struct! */
461 uint32_t quick_arg_flags;
462
463 struct {
464 zend_uchar type; /* never used */
465 zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
466 uint32_t fn_flags;
467 zend_string *function_name;
468 zend_class_entry *scope;
469 union _zend_function *prototype;
470 uint32_t num_args;
471 uint32_t required_num_args;
472 zend_arg_info *arg_info;
473 } common;
474
475 zend_op_array op_array;
476 zend_internal_function internal_function;
477 };
478
479 typedef enum _zend_call_kind {
480 ZEND_CALL_NESTED_FUNCTION, /* stackless VM call to function */
481 ZEND_CALL_NESTED_CODE, /* stackless VM call to include/require/eval */
482 ZEND_CALL_TOP_FUNCTION, /* direct VM call to function from external C code */
483 ZEND_CALL_TOP_CODE /* direct VM call to "main" code from external C code */
484 } zend_call_kind;
485
486 struct _zend_execute_data {
487 const zend_op *opline; /* executed opline */
488 zend_execute_data *call; /* current call */
489 zval *return_value;
490 zend_function *func; /* executed function */
491 zval This; /* this + call_info + num_args */
492 zend_execute_data *prev_execute_data;
493 zend_array *symbol_table;
494 #if ZEND_EX_USE_RUN_TIME_CACHE
495 void **run_time_cache; /* cache op_array->run_time_cache */
496 #endif
497 };
498
499 #define ZEND_CALL_FUNCTION (0 << 0)
500 #define ZEND_CALL_CODE (1 << 0)
501 #define ZEND_CALL_NESTED (0 << 1)
502 #define ZEND_CALL_TOP (1 << 1)
503 #define ZEND_CALL_FREE_EXTRA_ARGS (1 << 2)
504 #define ZEND_CALL_CTOR (1 << 3)
505 #define ZEND_CALL_HAS_SYMBOL_TABLE (1 << 4)
506 #define ZEND_CALL_CLOSURE (1 << 5)
507 #define ZEND_CALL_RELEASE_THIS (1 << 6)
508 #define ZEND_CALL_ALLOCATED (1 << 7)
509 #define ZEND_CALL_GENERATOR (1 << 8)
510 #define ZEND_CALL_DYNAMIC (1 << 9)
511 #define ZEND_CALL_FAKE_CLOSURE (1 << 10)
512 #define ZEND_CALL_SEND_ARG_BY_REF (1 << 11)
513
514 #define ZEND_CALL_INFO_SHIFT 16
515
516 #define ZEND_CALL_INFO(call) \
517 (Z_TYPE_INFO((call)->This) >> ZEND_CALL_INFO_SHIFT)
518
519 #define ZEND_CALL_KIND_EX(call_info) \
520 (call_info & (ZEND_CALL_CODE | ZEND_CALL_TOP))
521
522 #define ZEND_CALL_KIND(call) \
523 ZEND_CALL_KIND_EX(ZEND_CALL_INFO(call))
524
525 #define ZEND_SET_CALL_INFO(call, object, info) do { \
526 Z_TYPE_INFO((call)->This) = ((object) ? IS_OBJECT_EX : IS_UNDEF) | ((info) << ZEND_CALL_INFO_SHIFT); \
527 } while (0)
528
529 #define ZEND_ADD_CALL_FLAG_EX(call_info, flag) do { \
530 call_info |= ((flag) << ZEND_CALL_INFO_SHIFT); \
531 } while (0)
532
533 #define ZEND_DEL_CALL_FLAG_EX(call_info, flag) do { \
534 call_info &= ~((flag) << ZEND_CALL_INFO_SHIFT); \
535 } while (0)
536
537 #define ZEND_ADD_CALL_FLAG(call, flag) do { \
538 ZEND_ADD_CALL_FLAG_EX(Z_TYPE_INFO((call)->This), flag); \
539 } while (0)
540
541 #define ZEND_DEL_CALL_FLAG(call, flag) do { \
542 ZEND_DEL_CALL_FLAG_EX(Z_TYPE_INFO((call)->This), flag); \
543 } while (0)
544
545 #define ZEND_CALL_NUM_ARGS(call) \
546 (call)->This.u2.num_args
547
548 #define ZEND_CALL_FRAME_SLOT \
549 ((int)((ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval))))
550
551 #define ZEND_CALL_VAR(call, n) \
552 ((zval*)(((char*)(call)) + ((int)(n))))
553
554 #define ZEND_CALL_VAR_NUM(call, n) \
555 (((zval*)(call)) + (ZEND_CALL_FRAME_SLOT + ((int)(n))))
556
557 #define ZEND_CALL_ARG(call, n) \
558 ZEND_CALL_VAR_NUM(call, ((int)(n)) - 1)
559
560 #define EX(element) ((execute_data)->element)
561
562 #define EX_CALL_INFO() ZEND_CALL_INFO(execute_data)
563 #define EX_CALL_KIND() ZEND_CALL_KIND(execute_data)
564 #define EX_NUM_ARGS() ZEND_CALL_NUM_ARGS(execute_data)
565
566 #define ZEND_CALL_USES_STRICT_TYPES(call) \
567 (((call)->func->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0)
568
569 #define EX_USES_STRICT_TYPES() \
570 ZEND_CALL_USES_STRICT_TYPES(execute_data)
571
572 #define ZEND_ARG_USES_STRICT_TYPES() \
573 (EG(current_execute_data)->prev_execute_data && \
574 EG(current_execute_data)->prev_execute_data->func && \
575 ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)->prev_execute_data))
576
577 #define ZEND_RET_USES_STRICT_TYPES() \
578 ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))
579
580 #define EX_VAR(n) ZEND_CALL_VAR(execute_data, n)
581 #define EX_VAR_NUM(n) ZEND_CALL_VAR_NUM(execute_data, n)
582
583 #define EX_VAR_TO_NUM(n) ((uint32_t)(ZEND_CALL_VAR(NULL, n) - ZEND_CALL_VAR_NUM(NULL, 0)))
584
585 #define ZEND_OPLINE_TO_OFFSET(opline, target) \
586 ((char*)(target) - (char*)(opline))
587
588 #define ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline_num) \
589 ((char*)&(op_array)->opcodes[opline_num] - (char*)(opline))
590
591 #define ZEND_OFFSET_TO_OPLINE(base, offset) \
592 ((zend_op*)(((char*)(base)) + (int)offset))
593
594 #define ZEND_OFFSET_TO_OPLINE_NUM(op_array, base, offset) \
595 (ZEND_OFFSET_TO_OPLINE(base, offset) - op_array->opcodes)
596
597 #if ZEND_USE_ABS_JMP_ADDR
598
599 /* run-time jump target */
600 # define OP_JMP_ADDR(opline, node) \
601 (node).jmp_addr
602
603 # define ZEND_SET_OP_JMP_ADDR(opline, node, val) do { \
604 (node).jmp_addr = (val); \
605 } while (0)
606
607 /* convert jump target from compile-time to run-time */
608 # define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
609 (node).jmp_addr = (op_array)->opcodes + (node).opline_num; \
610 } while (0)
611
612 /* convert jump target back from run-time to compile-time */
613 # define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
614 (node).opline_num = (node).jmp_addr - (op_array)->opcodes; \
615 } while (0)
616
617 #else
618
619 /* run-time jump target */
620 # define OP_JMP_ADDR(opline, node) \
621 ZEND_OFFSET_TO_OPLINE(opline, (node).jmp_offset)
622
623 # define ZEND_SET_OP_JMP_ADDR(opline, node, val) do { \
624 (node).jmp_offset = ZEND_OPLINE_TO_OFFSET(opline, val); \
625 } while (0)
626
627 /* convert jump target from compile-time to run-time */
628 # define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
629 (node).jmp_offset = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, (node).opline_num); \
630 } while (0)
631
632 /* convert jump target back from run-time to compile-time */
633 # define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
634 (node).opline_num = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, (node).jmp_offset); \
635 } while (0)
636
637 #endif
638
639 /* constant-time constant */
640 # define CT_CONSTANT_EX(op_array, num) \
641 ((op_array)->literals + (num))
642
643 # define CT_CONSTANT(node) \
644 CT_CONSTANT_EX(CG(active_op_array), (node).constant)
645
646 #if ZEND_USE_ABS_CONST_ADDR
647
648 /* run-time constant */
649 # define RT_CONSTANT(opline, node) \
650 (node).zv
651
652 /* convert constant from compile-time to run-time */
653 # define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, node) do { \
654 (node).zv = CT_CONSTANT_EX(op_array, (node).constant); \
655 } while (0)
656
657 #else
658
659 /* At run-time, constants are allocated together with op_array->opcodes
660 * and addressed relatively to current opline.
661 */
662
663 /* run-time constant */
664 # define RT_CONSTANT(opline, node) \
665 ((zval*)(((char*)(opline)) + (int32_t)(node).constant))
666
667 /* convert constant from compile-time to run-time */
668 # define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, node) do { \
669 (node).constant = \
670 (((char*)CT_CONSTANT_EX(op_array, (node).constant)) - \
671 ((char*)opline)); \
672 } while (0)
673
674 #endif
675
676 /* convert constant back from run-time to compile-time */
677 #define ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline, node) do { \
678 (node).constant = RT_CONSTANT(opline, node) - (op_array)->literals; \
679 } while (0)
680
681 #if ZEND_EX_USE_RUN_TIME_CACHE
682
683 # define EX_RUN_TIME_CACHE() \
684 EX(run_time_cache)
685
686 # define EX_LOAD_RUN_TIME_CACHE(op_array) do { \
687 EX(run_time_cache) = (op_array)->run_time_cache; \
688 } while (0)
689
690 #else
691
692 # define EX_RUN_TIME_CACHE() \
693 EX(func)->op_array.run_time_cache
694
695 # define EX_LOAD_RUN_TIME_CACHE(op_array) do { \
696 } while (0)
697
698 #endif
699
700 #define IS_UNUSED 0 /* Unused operand */
701 #define IS_CONST (1<<0)
702 #define IS_TMP_VAR (1<<1)
703 #define IS_VAR (1<<2)
704 #define IS_CV (1<<3) /* Compiled variable */
705
706 #define ZEND_EXTRA_VALUE 1
707
708 #include "zend_globals.h"
709
710 BEGIN_EXTERN_C()
711
712 void init_compiler(void);
713 void shutdown_compiler(void);
714 void zend_init_compiler_data_structures(void);
715
716 void zend_oparray_context_begin(zend_oparray_context *prev_context);
717 void zend_oparray_context_end(zend_oparray_context *prev_context);
718 void zend_file_context_begin(zend_file_context *prev_context);
719 void zend_file_context_end(zend_file_context *prev_context);
720
721 extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
722 extern ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);
723
724 ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem);
725 void startup_scanner(void);
726 void shutdown_scanner(void);
727
728 ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename);
729 ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename);
730 ZEND_API zend_string *zend_get_compiled_filename(void);
731 ZEND_API int zend_get_compiled_lineno(void);
732 ZEND_API size_t zend_get_scanned_file_offset(void);
733
734 ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var);
735
736 #ifdef ZTS
737 const char *zend_get_zendtext(void);
738 int zend_get_zendleng(void);
739 #endif
740
741 typedef int (ZEND_FASTCALL *unary_op_type)(zval *, zval *);
742 typedef int (ZEND_FASTCALL *binary_op_type)(zval *, zval *, zval *);
743
744 ZEND_API unary_op_type get_unary_op(int opcode);
745 ZEND_API binary_op_type get_binary_op(int opcode);
746
747 void zend_stop_lexing(void);
748 void zend_emit_final_return(int return_one);
749
750 /* Used during AST construction */
751 zend_ast *zend_ast_append_str(zend_ast *left, zend_ast *right);
752 zend_ast *zend_negate_num_string(zend_ast *ast);
753 uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
754 uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag);
755 zend_bool zend_handle_encoding_declaration(zend_ast *ast);
756
757 /* parser-driven code generators */
758 void zend_do_free(znode *op1);
759
760 ZEND_API int do_bind_function(const zend_op_array *op_array, const zend_op *opline, HashTable *function_table, zend_bool compile_time);
761 ZEND_API zend_class_entry *do_bind_class(const zend_op_array *op_array, const zend_op *opline, HashTable *class_table, zend_bool compile_time);
762 ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op_array *op_array, const zend_op *opline, HashTable *class_table, zend_class_entry *parent_ce, zend_bool compile_time);
763 ZEND_API uint32_t zend_build_delayed_early_binding_list(const zend_op_array *op_array);
764 ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array, uint32_t first_early_binding_opline);
765
766 void zend_do_extended_info(void);
767 void zend_do_extended_fcall_begin(void);
768 void zend_do_extended_fcall_end(void);
769
770 void zend_verify_namespace(void);
771
772 void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline);
773
774 ZEND_API void function_add_ref(zend_function *function);
775
776 #define INITIAL_OP_ARRAY_SIZE 64
777
778
779 /* helper functions in zend_language_scanner.l */
780 ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
781 ZEND_API zend_op_array *compile_string(zval *source_string, char *filename);
782 ZEND_API zend_op_array *compile_filename(int type, zval *filename);
783 ZEND_API void zend_try_exception_handler();
784 ZEND_API int zend_execute_scripts(int type, zval *retval, int file_count, ...);
785 ZEND_API int open_file_for_scanning(zend_file_handle *file_handle);
786 ZEND_API void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size);
787 ZEND_API void destroy_op_array(zend_op_array *op_array);
788 ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle);
789 ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce);
790 ZEND_API void zend_cleanup_internal_classes(void);
791
792 ZEND_API void destroy_zend_function(zend_function *function);
793 ZEND_API void zend_function_dtor(zval *zv);
794 ZEND_API void destroy_zend_class(zval *zv);
795 void zend_class_add_ref(zval *zv);
796
797 ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, int internal);
798 #define zend_unmangle_property_name(mangled_property, class_name, prop_name) \
799 zend_unmangle_property_name_ex(mangled_property, class_name, prop_name, NULL)
800 ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len);
801
802 #define ZEND_FUNCTION_DTOR zend_function_dtor
803 #define ZEND_CLASS_DTOR destroy_zend_class
804
805 ZEND_API int pass_two(zend_op_array *op_array);
806 ZEND_API zend_bool zend_is_compiling(void);
807 ZEND_API char *zend_make_compiled_string_description(const char *name);
808 ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers);
809 uint32_t zend_get_class_fetch_type(zend_string *name);
810 ZEND_API zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc);
811 ZEND_API int zend_is_smart_branch(zend_op *opline);
812
get_next_op_number(zend_op_array * op_array)813 static zend_always_inline uint32_t get_next_op_number(zend_op_array *op_array)
814 {
815 return op_array->last;
816 }
817
818 typedef zend_bool (*zend_auto_global_callback)(zend_string *name);
819 typedef struct _zend_auto_global {
820 zend_string *name;
821 zend_auto_global_callback auto_global_callback;
822 zend_bool jit;
823 zend_bool armed;
824 } zend_auto_global;
825
826 ZEND_API int zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback);
827 ZEND_API void zend_activate_auto_globals(void);
828 ZEND_API zend_bool zend_is_auto_global(zend_string *name);
829 ZEND_API zend_bool zend_is_auto_global_str(char *name, size_t len);
830 ZEND_API size_t zend_dirname(char *path, size_t len);
831 ZEND_API void zend_set_function_arg_flags(zend_function *func);
832
833 int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem);
834
835 int zend_add_literal(zend_op_array *op_array, zval *zv);
836
837 void zend_assert_valid_class_name(const zend_string *const_name);
838
839 /* BEGIN: OPCODES */
840
841 #include "zend_vm_opcodes.h"
842
843 /* END: OPCODES */
844
845 /* class fetches */
846 #define ZEND_FETCH_CLASS_DEFAULT 0
847 #define ZEND_FETCH_CLASS_SELF 1
848 #define ZEND_FETCH_CLASS_PARENT 2
849 #define ZEND_FETCH_CLASS_STATIC 3
850 #define ZEND_FETCH_CLASS_AUTO 4
851 #define ZEND_FETCH_CLASS_INTERFACE 5
852 #define ZEND_FETCH_CLASS_TRAIT 6
853 #define ZEND_FETCH_CLASS_MASK 0x0f
854 #define ZEND_FETCH_CLASS_NO_AUTOLOAD 0x80
855 #define ZEND_FETCH_CLASS_SILENT 0x0100
856 #define ZEND_FETCH_CLASS_EXCEPTION 0x0200
857
858 #define ZEND_PARAM_REF (1<<0)
859 #define ZEND_PARAM_VARIADIC (1<<1)
860
861 #define ZEND_NAME_FQ 0
862 #define ZEND_NAME_NOT_FQ 1
863 #define ZEND_NAME_RELATIVE 2
864
865 #define ZEND_TYPE_NULLABLE (1<<8)
866
867 #define ZEND_ARRAY_SYNTAX_LIST 1 /* list() */
868 #define ZEND_ARRAY_SYNTAX_LONG 2 /* array() */
869 #define ZEND_ARRAY_SYNTAX_SHORT 3 /* [] */
870
871 /* var status for backpatching */
872 #define BP_VAR_R 0
873 #define BP_VAR_W 1
874 #define BP_VAR_RW 2
875 #define BP_VAR_IS 3
876 #define BP_VAR_FUNC_ARG 4
877 #define BP_VAR_UNSET 5
878
879 #define ZEND_INTERNAL_FUNCTION 1
880 #define ZEND_USER_FUNCTION 2
881 #define ZEND_OVERLOADED_FUNCTION 3
882 #define ZEND_EVAL_CODE 4
883 #define ZEND_OVERLOADED_FUNCTION_TEMPORARY 5
884
885 /* A quick check (type == ZEND_USER_FUNCTION || type == ZEND_EVAL_CODE) */
886 #define ZEND_USER_CODE(type) ((type & 1) == 0)
887
888 #define ZEND_INTERNAL_CLASS 1
889 #define ZEND_USER_CLASS 2
890
891 #define ZEND_EVAL (1<<0)
892 #define ZEND_INCLUDE (1<<1)
893 #define ZEND_INCLUDE_ONCE (1<<2)
894 #define ZEND_REQUIRE (1<<3)
895 #define ZEND_REQUIRE_ONCE (1<<4)
896
897 #define ZEND_CT (1<<0)
898 #define ZEND_RT (1<<1)
899
900 /* global/local fetches */
901 #define ZEND_FETCH_GLOBAL (1<<1)
902 #define ZEND_FETCH_LOCAL (1<<2)
903 #define ZEND_FETCH_GLOBAL_LOCK (1<<3)
904
905 #define ZEND_FETCH_TYPE_MASK 0xe
906
907 #define ZEND_ISEMPTY (1<<0)
908
909 #define ZEND_LAST_CATCH (1<<0)
910
911 #define ZEND_FREE_ON_RETURN (1<<0)
912
913 #define ZEND_SEND_BY_VAL 0
914 #define ZEND_SEND_BY_REF 1
915 #define ZEND_SEND_PREFER_REF 2
916
917 #define ZEND_DIM_IS 1
918
919 #define IS_CONSTANT_UNQUALIFIED 0x010
920 #define IS_CONSTANT_CLASS 0x080 /* __CLASS__ in trait */
921 #define IS_CONSTANT_IN_NAMESPACE 0x100
922
zend_check_arg_send_type(const zend_function * zf,uint32_t arg_num,uint32_t mask)923 static zend_always_inline int zend_check_arg_send_type(const zend_function *zf, uint32_t arg_num, uint32_t mask)
924 {
925 arg_num--;
926 if (UNEXPECTED(arg_num >= zf->common.num_args)) {
927 if (EXPECTED((zf->common.fn_flags & ZEND_ACC_VARIADIC) == 0)) {
928 return 0;
929 }
930 arg_num = zf->common.num_args;
931 }
932 return UNEXPECTED((zf->common.arg_info[arg_num].pass_by_reference & mask) != 0);
933 }
934
935 #define ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
936 zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF)
937
938 #define ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
939 zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
940
941 #define ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
942 zend_check_arg_send_type(zf, arg_num, ZEND_SEND_PREFER_REF)
943
944 /* Quick API to check firat 12 arguments */
945 #define MAX_ARG_FLAG_NUM 12
946
947 #ifdef WORDS_BIGENDIAN
948 # define ZEND_SET_ARG_FLAG(zf, arg_num, mask) do { \
949 (zf)->quick_arg_flags |= ((mask) << ((arg_num) - 1) * 2); \
950 } while (0)
951 # define ZEND_CHECK_ARG_FLAG(zf, arg_num, mask) \
952 (((zf)->quick_arg_flags >> (((arg_num) - 1) * 2)) & (mask))
953 #else
954 # define ZEND_SET_ARG_FLAG(zf, arg_num, mask) do { \
955 (zf)->quick_arg_flags |= (((mask) << 6) << (arg_num) * 2); \
956 } while (0)
957 # define ZEND_CHECK_ARG_FLAG(zf, arg_num, mask) \
958 (((zf)->quick_arg_flags >> (((arg_num) + 3) * 2)) & (mask))
959 #endif
960
961 #define QUICK_ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
962 ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_BY_REF)
963
964 #define QUICK_ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
965 ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
966
967 #define QUICK_ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
968 ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_PREFER_REF)
969
970 #define ZEND_RETURN_VAL 0
971 #define ZEND_RETURN_REF 1
972
973 #define ZEND_BIND_VAL 0
974 #define ZEND_BIND_REF 1
975
976 #define ZEND_RETURNS_FUNCTION (1<<0)
977 #define ZEND_RETURNS_VALUE (1<<1)
978
979 #define ZEND_ARRAY_ELEMENT_REF (1<<0)
980 #define ZEND_ARRAY_NOT_PACKED (1<<1)
981 #define ZEND_ARRAY_SIZE_SHIFT 2
982
983 /* For "use" AST nodes and the seen symbol table */
984 #define ZEND_SYMBOL_CLASS (1<<0)
985 #define ZEND_SYMBOL_FUNCTION (1<<1)
986 #define ZEND_SYMBOL_CONST (1<<2)
987
988 /* Pseudo-opcodes that are used only temporarily during compilation */
989 #define ZEND_GOTO 253
990 #define ZEND_BRK 254
991 #define ZEND_CONT 255
992
993
994 END_EXTERN_C()
995
996 #define ZEND_CLONE_FUNC_NAME "__clone"
997 #define ZEND_CONSTRUCTOR_FUNC_NAME "__construct"
998 #define ZEND_DESTRUCTOR_FUNC_NAME "__destruct"
999 #define ZEND_GET_FUNC_NAME "__get"
1000 #define ZEND_SET_FUNC_NAME "__set"
1001 #define ZEND_UNSET_FUNC_NAME "__unset"
1002 #define ZEND_ISSET_FUNC_NAME "__isset"
1003 #define ZEND_CALL_FUNC_NAME "__call"
1004 #define ZEND_CALLSTATIC_FUNC_NAME "__callstatic"
1005 #define ZEND_TOSTRING_FUNC_NAME "__tostring"
1006 #define ZEND_AUTOLOAD_FUNC_NAME "__autoload"
1007 #define ZEND_INVOKE_FUNC_NAME "__invoke"
1008 #define ZEND_DEBUGINFO_FUNC_NAME "__debuginfo"
1009
1010 /* The following constants may be combined in CG(compiler_options)
1011 * to change the default compiler behavior */
1012
1013 /* generate extended debug information */
1014 #define ZEND_COMPILE_EXTENDED_INFO (1<<0)
1015
1016 /* call op_array handler of extendions */
1017 #define ZEND_COMPILE_HANDLE_OP_ARRAY (1<<1)
1018
1019 /* generate ZEND_INIT_FCALL_BY_NAME for internal functions instead of ZEND_INIT_FCALL */
1020 #define ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS (1<<2)
1021
1022 /* don't perform early binding for classes inherited form internal ones;
1023 * in namespaces assume that internal class that doesn't exist at compile-time
1024 * may apper in run-time */
1025 #define ZEND_COMPILE_IGNORE_INTERNAL_CLASSES (1<<3)
1026
1027 /* generate ZEND_DECLARE_INHERITED_CLASS_DELAYED opcode to delay early binding */
1028 #define ZEND_COMPILE_DELAYED_BINDING (1<<4)
1029
1030 /* disable constant substitution at compile-time */
1031 #define ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION (1<<5)
1032
1033 /* disable usage of builtin instruction for strlen() */
1034 #define ZEND_COMPILE_NO_BUILTIN_STRLEN (1<<6)
1035
1036 /* disable substitution of persistent constants at compile-time */
1037 #define ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION (1<<7)
1038
1039 /* generate ZEND_INIT_FCALL_BY_NAME for userland functions instead of ZEND_INIT_FCALL */
1040 #define ZEND_COMPILE_IGNORE_USER_FUNCTIONS (1<<8)
1041
1042 /* force ZEND_ACC_USE_GUARDS for all classes */
1043 #define ZEND_COMPILE_GUARDS (1<<9)
1044
1045 /* disable builtin special case function calls */
1046 #define ZEND_COMPILE_NO_BUILTINS (1<<10)
1047
1048 /* result of compilation may be stored in file cache */
1049 #define ZEND_COMPILE_WITH_FILE_CACHE (1<<11)
1050
1051 /* disable jumptable optimization for switch statements */
1052 #define ZEND_COMPILE_NO_JUMPTABLES (1<<12)
1053
1054 /* The default value for CG(compiler_options) */
1055 #define ZEND_COMPILE_DEFAULT ZEND_COMPILE_HANDLE_OP_ARRAY
1056
1057 /* The default value for CG(compiler_options) during eval() */
1058 #define ZEND_COMPILE_DEFAULT_FOR_EVAL 0
1059
1060 ZEND_API zend_bool zend_binary_op_produces_numeric_string_error(uint32_t opcode, zval *op1, zval *op2);
1061
1062 #endif /* ZEND_COMPILE_H */
1063
1064 /*
1065 * Local variables:
1066 * tab-width: 4
1067 * c-basic-offset: 4
1068 * indent-tabs-mode: t
1069 * End:
1070 * vim600: sw=4 ts=4 fdm=marker
1071 * vim<600: sw=4 ts=4
1072 */
1073