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