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