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