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