Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 511 lines (379 sloc) 14.95 kb
573b460 @andigutmans Zend Library
andigutmans authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski |
6 +----------------------------------------------------------------------+
7 | This source file is subject to the Zend license, that is bundled |
8 | with this package in the file LICENSE. If you did not receive a |
9 | copy of the Zend license, please mail us at zend@zend.com so we can |
10 | send you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Authors: Andi Gutmans <andi@zend.com> |
13 | Zeev Suraski <zeev@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifndef _COMPILE_H
18 #define _COMPILE_H
19
20 #include "zend.h"
fa76f0c @zsuraski Compile fix
zsuraski authored
21
22 #if HAVE_STDARG_H
23 #include <stdarg.h>
24 #endif
25
573b460 @andigutmans Zend Library
andigutmans authored
26 #include "zend_llist.h"
27 #define YYSTYPE znode
28 #include "zend-parser.h"
29
30 #define DEBUG_ZEND 0
31 #define SUPPORT_INTERACTIVE 1
32
33 #define FREE_PNODE(znode) zval_dtor(&znode->u.constant);
34 #define FREE_OP(op, should_free) if (should_free) zval_dtor(&Ts[(op)->u.var].tmp_var);
35
36
37 #if SUPPORT_INTERACTIVE
38 #define INC_BPC(op_array) ((op_array)->backpatch_count++)
39 #define DEC_BPC(op_array) ((op_array)->backpatch_count--)
40 #define HANDLE_INTERACTIVE() if (EG(interactive)) { execute_new_code(CLS_C); }
41 #else
42 #define INC_BPC(op_array)
43 #define DEC_BPC(op_array)
44 #define HANDLE_INTERACTIVE()
45 #endif
46
47 typedef struct _zend_op_array zend_op_array;
48
49 typedef struct {
50 int op_type;
51 union {
52 zval constant;
53
54 int var;
55 int opline_num;
56 zend_op_array *op_array;
57 struct {
58 int var; /* dummy */
59 int type;
60 } EA;
61 } u;
62 } znode;
63
64
65 typedef struct _zend_op {
66 int opcode;
67 znode result;
68 znode op1;
69 znode op2;
39a7f4c @zsuraski This patch is a go. Not fully optimized yet, but working properly.
zsuraski authored
70 ulong extended_value;
573b460 @andigutmans Zend Library
andigutmans authored
71 char *filename;
72 uint lineno;
73 } zend_op;
74
75
76 typedef struct {
77 int cont;
78 int brk;
79 int parent;
80 } zend_brk_cont_element;
81
82
83 struct _zend_op_array {
84 int type; /* MUST be the first element of this struct! */
85
86 unsigned char *arg_types; /* MUST be the second element of this struct! */
87
88 int *refcount;
89
90 zend_op *opcodes;
91 int last, size;
92
93 int T;
94
95 char *function_name;
96
97 zend_brk_cont_element *brk_cont_array;
98 int last_brk_cont;
99 int current_brk_cont;
17bdb70 @zsuraski $GLOBALS support
zsuraski authored
100 unsigned char initialized_globals;
573b460 @andigutmans Zend Library
andigutmans authored
101
102 /* static variables support */
103 HashTable *static_variables;
104
105 #if SUPPORT_INTERACTIVE
106 int start_op_number, end_op_number;
107 int last_executed_op_number;
108 int backpatch_count;
109 #endif
110
111 void *reserved[4];
112 };
113
114
115 typedef struct {
116 int type; /* MUST be the first element of this struct! */
117
118 unsigned char *arg_types; /* MUST be the second element of this struct */
119
120 void (*handler)(INTERNAL_FUNCTION_PARAMETERS);
121 char *function_name;
122 } zend_internal_function;
123
124
125 typedef union {
126 int type; /* MUST be the first element of this struct! */
127 struct {
128 int type; /* never used */
129 unsigned char *arg_types;
130 } common;
131
132 zend_op_array op_array;
133 zend_internal_function internal_function;
134 } zend_function;
135
136
137 typedef struct {
138 HashTable *function_symbol_table;
139 zend_function *function;
140 void *reserved[4];
141 } zend_function_state;
142
143
144 typedef struct {
145 znode cond;
146 int default_case;
147 int control_var;
148 } zend_switch_entry;
149
150
151 typedef struct {
152 znode var;
153 zend_llist dimensions;
154 znode value;
155 } list_llist_element;
156
157
39f9487 @andigutmans -Tiny patches
andigutmans authored
158 typedef struct _zend_file_handle {
573b460 @andigutmans Zend Library
andigutmans authored
159 int type;
160 char *filename;
161 union {
162 int fd;
163 FILE *fp;
164 #ifdef __cplusplus
165 istream *is;
166 #endif
167 } handle;
168 } zend_file_handle;
169
170
171 #include "zend_globals.h"
172
173 #define IS_CONST (1<<0)
174 #define IS_TMP_VAR (1<<1)
175 #define IS_VAR (1<<2)
176 #define IS_UNUSED (1<<3) /* Unused variable */
177
178
179 #define EXT_TYPE_UNUSED (1<<0)
180
181 void init_compiler(CLS_D ELS_DC);
182 void shutdown_compiler(CLS_D);
183
39f9487 @andigutmans -Tiny patches
andigutmans authored
184 extern ZEND_API zend_op_array *(*zend_compile_files)(int mark_as_ref CLS_DC, int file_count, ...);
573b460 @andigutmans Zend Library
andigutmans authored
185
186 int lex_scan(zval *zendlval CLS_DC);
187 void reset_scanner(CLS_D);
188 void startup_scanner(CLS_D);
189 void shutdown_scanner(CLS_D);
190
191 ZEND_API void zend_set_compiled_filename(char *new_compiled_filename);
192 ZEND_API void zend_restore_compiled_filename(char *original_compiled_filename);
193 ZEND_API char *zend_get_compiled_filename();
194
195
196 /* parser-driven code generators */
197 void do_binary_op(int op, znode *result, znode *op1, znode *op2 CLS_DC);
198 void do_unary_op(int op, znode *result, znode *op1 CLS_DC);
199 void do_binary_assign_op(int op, znode *result, znode *op1, znode *op2 CLS_DC);
200 void do_assign(znode *result, znode *variable, znode *value CLS_DC);
201 void do_assign_ref(znode *result, znode *lvar, znode *rvar CLS_DC);
202 void fetch_simple_variable(znode *result, znode *varname, int bp CLS_DC);
203 void do_indirect_references(znode *result, znode *num_references, znode *variable CLS_DC);
204 void do_fetch_global_or_static_variable(znode *varname, znode *static_assignment, int fetch_type CLS_DC);
17bdb70 @zsuraski $GLOBALS support
zsuraski authored
205 void do_fetch_globals(znode *varname);
573b460 @andigutmans Zend Library
andigutmans authored
206
207 void fetch_array_begin(znode *result, znode *varname, znode *first_dim CLS_DC);
208 void fetch_array_dim(znode *result, znode *parent, znode *dim CLS_DC);
209 void do_print(znode *result, znode *arg CLS_DC);
210 void do_echo(znode *arg CLS_DC);
211 ZEND_API void *get_unary_op(int opcode);
212 ZEND_API void *get_binary_op(int opcode);
213
214 void do_while_cond(znode *expr, znode *close_bracket_token CLS_DC);
215 void do_while_end(znode *while_token, znode *close_bracket_token CLS_DC);
216 void do_do_while_begin(CLS_D);
217 void do_do_while_end(znode *do_token, znode *expr CLS_DC);
218
219
220 void do_if_cond(znode *cond, znode *closing_bracket_token CLS_DC);
221 void do_if_after_statement(znode *closing_bracket_token, unsigned char initialize CLS_DC);
222 void do_if_end(CLS_D);
223
224 void do_for_cond(znode *expr, znode *second_semicolon_token CLS_DC);
225 void do_for_before_statement(znode *cond_start, znode *second_semicolon_token CLS_DC);
226 void do_for_end(znode *second_semicolon_token CLS_DC);
227
228 void do_pre_incdec(znode *result, znode *op1, int op CLS_DC);
229 void do_post_incdec(znode *result, znode *op1, int op CLS_DC);
230
231 void do_begin_variable_parse(CLS_D);
232 void do_end_variable_parse(int type CLS_DC);
233
234 void do_free(znode *op1 CLS_DC);
235
236 void do_init_string(znode *result);
237 void do_add_char(znode *result, znode *op1, znode *op2 CLS_DC);
238 void do_add_string(znode *result, znode *op1, znode *op2 CLS_DC);
239 void do_add_variable(znode *result, znode *op1, znode *op2 CLS_DC);
240
241 void do_begin_function_declaration(znode *function_token, znode *function_name, int is_method CLS_DC);
242 void do_end_function_declaration(znode *function_token CLS_DC);
243 void do_receive_arg(int op, znode *var, znode *offset, znode *initialization, unsigned char pass_type CLS_DC);
244 void do_begin_function_call(znode *function_name CLS_DC);
245 void do_begin_dynamic_function_call(znode *function_name CLS_DC);
246 void do_begin_class_member_function_call(znode *class_name, znode *function_name CLS_DC);
39a7f4c @zsuraski This patch is a go. Not fully optimized yet, but working properly.
zsuraski authored
247 void do_end_function_call(znode *function_name, znode *result, znode *argument_list, int is_method CLS_DC);
573b460 @andigutmans Zend Library
andigutmans authored
248 void do_return(znode *expr CLS_DC);
249
250 void do_pass_param(znode *param, int op, int offset CLS_DC);
251
252
253 void do_boolean_or_begin(znode *expr1, znode *op_token CLS_DC);
254 void do_boolean_or_end(znode *result, znode *expr1, znode *expr2, znode *op_token CLS_DC);
255 void do_boolean_and_begin(znode *expr1, znode *op_token CLS_DC);
256 void do_boolean_and_end(znode *result, znode *expr1, znode *expr2, znode *op_token CLS_DC);
257
258 void do_brk_cont(int op, znode *expr CLS_DC);
259
260 void do_switch_cond(znode *cond CLS_DC);
261 void do_switch_end(znode *case_list CLS_DC);
262 void do_case_before_statement(znode *case_list, znode *case_token, znode *case_expr CLS_DC);
263 void do_case_after_statement(znode *result, znode *case_token CLS_DC);
264 void do_default_before_statement(znode *case_list, znode *default_token CLS_DC);
265
266 void do_begin_class_declaration(znode *class_name, znode *parent_class_name CLS_DC);
267 void do_end_class_declaration(CLS_D);
268 void do_declare_property(znode *var_name, znode *value CLS_DC);
269
270 void do_fetch_property(znode *result, znode *object, znode *property CLS_DC);
271
272
273 void do_push_object(znode *object CLS_DC);
274 void do_pop_object(znode *object CLS_DC);
275
276
277 void do_begin_new_object(znode *result, znode *variable, znode *new_token, znode *class_name CLS_DC);
39a7f4c @zsuraski This patch is a go. Not fully optimized yet, but working properly.
zsuraski authored
278 void do_end_new_object(znode *class_name, znode *new_token, znode *argument_list CLS_DC);
573b460 @andigutmans Zend Library
andigutmans authored
279
280 void do_fetch_constant(znode *result, znode *constant_name, int mode CLS_DC);
281
282 void do_shell_exec(znode *result, znode *cmd CLS_DC);
283
284 void do_init_array(znode *result, znode *expr, znode *offset CLS_DC);
285 void do_add_array_element(znode *result, znode *expr, znode *offset CLS_DC);
286 void do_add_static_array_element(znode *result, znode *expr, znode *offset);
287 void do_list_init();
288 void do_list_end(znode *result, znode *expr CLS_DC);
289 void do_add_list_element(znode *element CLS_DC);
290 void do_new_list_begin(CLS_D);
291 void do_new_list_end(CLS_D);
292
293 void do_cast(znode *result, znode *expr, int type CLS_DC);
294 void do_include_or_eval(int type, znode *result, znode *op1 CLS_DC);
295
296 void do_unset(znode *variable CLS_DC);
297 void do_isset_or_isempty(int type, znode *result, znode *variable CLS_DC);
298
299 void do_foreach_begin(znode *foreach_token, znode *array, znode *open_brackets_token, znode *as_token CLS_DC);
300 void do_foreach_cont(znode *value, znode *key, znode *as_token CLS_DC);
301 void do_foreach_end(znode *foreach_token, znode *open_brackets_token CLS_DC);
302
303 void do_end_heredoc(CLS_D);
304
305 void do_exit(znode *result, znode *message CLS_DC);
306
307 void do_begin_silence(znode *strudel_token CLS_DC);
308 void do_end_silence(znode *strudel_token CLS_DC);
309
310 void do_begin_qm_op(znode *cond, znode *qm_token CLS_DC);
311 void do_qm_true(znode *true_value, znode *qm_token, znode *colon_token CLS_DC);
312 void do_qm_false(znode *result, znode *false_value, znode *qm_token, znode *colon_token CLS_DC);
313
314 void do_extended_info(CLS_D);
315 void do_extended_fcall_begin(CLS_D);
316 void do_extended_fcall_end(CLS_D);
317
318
319 /* helper functions in zend-scanner.l */
320 ZEND_API int require_file(zend_file_handle *file_handle CLS_DC);
321 ZEND_API int require_filename(char *filename CLS_DC);
322 ZEND_API zend_op_array *compile_files(int mark_as_ref CLS_DC, int file_count, ...);
39f9487 @andigutmans -Tiny patches
andigutmans authored
323 ZEND_API zend_op_array *v_compile_files(int mark_as_ref ELS_DC, int file_count, va_list files);
573b460 @andigutmans Zend Library
andigutmans authored
324 ZEND_API zend_op_array *compile_string(zval *source_string CLS_DC);
325 ZEND_API zend_op_array *compile_filename(zval *filename CLS_DC);
326 inline int open_file_for_scanning(zend_file_handle *file_handle CLS_DC);
327
5cb576d @andigutmans - I guess print $GLOBALS and print "$GLOBALS" should yield the same r…
andigutmans authored
328 #define INITIAL_OP_ARRAY_SIZE 64
573b460 @andigutmans Zend Library
andigutmans authored
329
330
331 ZEND_API void init_op_array(zend_op_array *op_array, int initial_ops_size);
332 ZEND_API void destroy_op_array(zend_op_array *op_array);
333
39f9487 @andigutmans -Tiny patches
andigutmans authored
334 ZEND_API void destroy_zend_function(zend_function *function);
335 ZEND_API void destroy_zend_class(zend_class_entry *ce);
573b460 @andigutmans Zend Library
andigutmans authored
336 zend_op *get_next_op(zend_op_array *op_array CLS_DC);
337 int get_next_op_number(zend_op_array *op_array);
338 int print_class(zend_class_entry *class_entry);
339 void print_op_array(zend_op_array *op_array, int optimizations);
340 int pass_two(zend_op_array *op_array);
341 void pass_include_eval(zend_op_array *op_array);
342 zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array);
343
344
345 int zendlex(znode *zendlval CLS_DC);
346
347
348 #define ZEND_NOP 0
349
350 #define ZEND_ADD 1
351 #define ZEND_SUB 2
352 #define ZEND_MUL 3
353 #define ZEND_DIV 4
354 #define ZEND_MOD 5
355 #define ZEND_SL 6
356 #define ZEND_SR 7
357 #define ZEND_CONCAT 8
358 #define ZEND_BW_OR 9
359 #define ZEND_BW_AND 10
360 #define ZEND_BW_XOR 11
361 #define ZEND_BW_NOT 12
362 #define ZEND_BOOL_NOT 13
363 #define ZEND_BOOL_XOR 14
364 #define ZEND_IS_EQUAL 15
365 #define ZEND_IS_NOT_EQUAL 16
366 #define ZEND_IS_SMALLER 17
367 #define ZEND_IS_SMALLER_OR_EQUAL 18
368 #define ZEND_CAST 19
369 #define ZEND_QM_ASSIGN 20
370
371 #define ZEND_ASSIGN_ADD 21
372 #define ZEND_ASSIGN_SUB 22
373 #define ZEND_ASSIGN_MUL 23
374 #define ZEND_ASSIGN_DIV 24
375 #define ZEND_ASSIGN_MOD 25
376 #define ZEND_ASSIGN_SL 26
377 #define ZEND_ASSIGN_SR 27
378 #define ZEND_ASSIGN_CONCAT 28
379 #define ZEND_ASSIGN_BW_OR 29
380 #define ZEND_ASSIGN_BW_AND 30
381 #define ZEND_ASSIGN_BW_XOR 31
382
383 #define ZEND_PRE_INC 32
384 #define ZEND_PRE_DEC 33
385 #define ZEND_POST_INC 34
386 #define ZEND_POST_DEC 35
387
388 #define ZEND_ASSIGN 36
389 #define ZEND_ASSIGN_REF 37
390
391 #define ZEND_ECHO_OP 38
392 #define ZEND_PRINT_OP 39
393
394 #define ZEND_JMP 40
395 #define ZEND_JMPZ 41
396 #define ZEND_JMPNZ 42
397 #define ZEND_JMPZNZ 43
398 #define ZEND_JMPZ_EX 44
399 #define ZEND_JMPNZ_EX 45
400 #define ZEND_CASE 46
401 #define ZEND_BRK 47
402 #define ZEND_CONT 48
403 #define ZEND_BOOL 49
404
405 #define ZEND_INIT_STRING 50
406 #define ZEND_ADD_CHAR 51
407 #define ZEND_ADD_STRING 52
408 #define ZEND_ADD_VAR 53
409
410 #define ZEND_BEGIN_SILENCE 54
411 #define ZEND_END_SILENCE 55
412
413 #define ZEND_INIT_FCALL 56
414 #define ZEND_INIT_FCALL_BY_NAME 57
415 #define ZEND_DO_FCALL 58
416 #define ZEND_RETURN 59
417
418 #define ZEND_RECV 60
419 #define ZEND_RECV_INIT 61
420
421 #define ZEND_SEND_VAL 62
422 #define ZEND_SEND_VAR 63
423 #define ZEND_SEND_REF 64
424
425 #define ZEND_NEW 65
426 #define ZEND_JMP_NO_CTOR 66
427 #define ZEND_FREE 67
428
429 #define ZEND_INIT_ARRAY 68
430 #define ZEND_ADD_ARRAY_ELEMENT 69
431
432 #define ZEND_INCLUDE_OR_EVAL 70
433
434 #define ZEND_UNSET_VAR 71
435 #define ZEND_UNSET_DIM_OBJ 72
436 #define ZEND_ISSET_ISEMPTY 73
437
438 #define ZEND_FE_RESET 74
439 #define ZEND_FE_FETCH 75
440
441 #define ZEND_EXIT 76
442
443
444 /* the following 12 opcodes are 4 groups of 3 opcodes each, and must
445 * remain in that order!
446 */
447 #define ZEND_FETCH_R 77
448 #define ZEND_FETCH_DIM_R 78
449 #define ZEND_FETCH_OBJ_R 79
450 #define ZEND_FETCH_W 80
451 #define ZEND_FETCH_DIM_W 81
452 #define ZEND_FETCH_OBJ_W 82
453 #define ZEND_FETCH_RW 83
454 #define ZEND_FETCH_DIM_RW 84
455 #define ZEND_FETCH_OBJ_RW 85
456 #define ZEND_FETCH_IS 86
457 #define ZEND_FETCH_DIM_IS 87
458 #define ZEND_FETCH_OBJ_IS 88
459
460 #define ZEND_FETCH_DIM_TMP_VAR 89
461 #define ZEND_FETCH_CONSTANT 90
17bdb70 @zsuraski $GLOBALS support
zsuraski authored
462
463 #define ZEND_INIT_GLOBALS 91
464
465 #define ZEND_EXT_STMT 92
466 #define ZEND_EXT_FCALL_BEGIN 93
467 #define ZEND_EXT_FCALL_END 94
468 #define ZEND_EXT_NOP 95
573b460 @andigutmans Zend Library
andigutmans authored
469
470 /* end of block */
471
472
473
474
475 /* global/local fetches */
476 #define ZEND_FETCH_GLOBAL 0
477 #define ZEND_FETCH_LOCAL 1
478 #define ZEND_FETCH_STATIC 2
479
480 /* var status for backpatching */
481 #define BP_VAR_R 0
482 #define BP_VAR_W 1
483 #define BP_VAR_RW 2
484 #define BP_VAR_IS 3
485 #define BP_VAR_NA 4 /* if not applicable */
486
487
488 #define ZEND_INTERNAL_FUNCTION 1
489 #define ZEND_USER_FUNCTION 2
490 #define ZEND_OVERLOADED_FUNCTION 3
491
492 #define ZEND_INTERNAL_CLASS 1
493 #define ZEND_USER_CLASS 2
494
495 #define ZEND_EVAL (1<<0)
496 #define ZEND_INCLUDE (1<<1)
497
498 #define ZEND_ISSET (1<<0)
499 #define ZEND_ISEMPTY (1<<1)
500
501 #define ZEND_CT (1<<0)
502 #define ZEND_RT (1<<1)
503
504
505 #define ZEND_HANDLE_FILENAME 0
506 #define ZEND_HANDLE_FD 1
507 #define ZEND_HANDLE_FP 2
508 #define ZEND_HANDLE_ISTREAM 3
509
510 #endif /* _COMPILE_H */
Something went wrong with that request. Please try again.