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