1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2009 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: zend_opcode.c 281737 2009-06-05 23:20:59Z shire $ */
21 :
22 : #include <stdio.h>
23 :
24 : #include "zend.h"
25 : #include "zend_alloc.h"
26 : #include "zend_compile.h"
27 : #include "zend_extensions.h"
28 : #include "zend_API.h"
29 :
30 : #include "zend_vm.h"
31 :
32 : static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
33 0 : {
34 0 : if (extension->op_array_ctor) {
35 0 : extension->op_array_ctor(op_array);
36 : }
37 0 : }
38 :
39 : static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
40 0 : {
41 0 : if (extension->op_array_dtor) {
42 0 : extension->op_array_dtor(op_array);
43 : }
44 0 : }
45 :
46 : static void op_array_alloc_ops(zend_op_array *op_array)
47 62582 : {
48 62582 : op_array->opcodes = erealloc(op_array->opcodes, (op_array->size)*sizeof(zend_op));
49 62582 : }
50 :
51 : void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size TSRMLS_DC)
52 55014 : {
53 55014 : op_array->type = type;
54 :
55 55014 : op_array->backpatch_count = 0;
56 55014 : if (CG(interactive)) {
57 : /* We must avoid a realloc() on the op_array in interactive mode, since pointers to constants
58 : * will become invalid
59 : */
60 0 : initial_ops_size = 8192;
61 : }
62 :
63 55014 : op_array->refcount = (zend_uint *) emalloc(sizeof(zend_uint));
64 55014 : *op_array->refcount = 1;
65 55014 : op_array->size = initial_ops_size;
66 55014 : op_array->last = 0;
67 55014 : op_array->opcodes = NULL;
68 55014 : op_array_alloc_ops(op_array);
69 :
70 55014 : op_array->size_var = 0; /* FIXME:??? */
71 55014 : op_array->last_var = 0;
72 55014 : op_array->vars = NULL;
73 :
74 55014 : op_array->T = 0;
75 :
76 55014 : op_array->function_name = NULL;
77 55014 : op_array->filename = zend_get_compiled_filename(TSRMLS_C);
78 55014 : op_array->doc_comment = NULL;
79 55014 : op_array->doc_comment_len = 0;
80 :
81 55014 : op_array->arg_info = NULL;
82 55014 : op_array->num_args = 0;
83 55014 : op_array->required_num_args = 0;
84 :
85 55014 : op_array->scope = NULL;
86 :
87 55014 : op_array->brk_cont_array = NULL;
88 55014 : op_array->try_catch_array = NULL;
89 55014 : op_array->last_brk_cont = 0;
90 55014 : op_array->current_brk_cont = -1;
91 :
92 55014 : op_array->static_variables = NULL;
93 55014 : op_array->last_try_catch = 0;
94 :
95 55014 : op_array->return_reference = 0;
96 55014 : op_array->done_pass_two = 0;
97 :
98 55014 : op_array->this_var = -1;
99 :
100 55014 : op_array->start_op = NULL;
101 :
102 55014 : op_array->fn_flags = CG(interactive)?ZEND_ACC_INTERACTIVE:0;
103 :
104 55014 : op_array->early_binding = -1;
105 :
106 55014 : memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
107 :
108 55014 : zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array TSRMLS_CC);
109 55014 : }
110 :
111 : ZEND_API void destroy_zend_function(zend_function *function TSRMLS_DC)
112 78531778 : {
113 78531778 : switch (function->type) {
114 : case ZEND_USER_FUNCTION:
115 42539 : destroy_op_array((zend_op_array *) function TSRMLS_CC);
116 : break;
117 : case ZEND_INTERNAL_FUNCTION:
118 : /* do nothing */
119 : break;
120 : }
121 78531778 : }
122 :
123 : ZEND_API void zend_function_dtor(zend_function *function)
124 78531778 : {
125 : TSRMLS_FETCH();
126 :
127 78531778 : destroy_zend_function(function TSRMLS_CC);
128 78531778 : }
129 :
130 : static void zend_cleanup_op_array_data(zend_op_array *op_array)
131 36168 : {
132 36168 : if (op_array->static_variables) {
133 1006 : zend_hash_clean(op_array->static_variables);
134 : }
135 36168 : }
136 :
137 : ZEND_API int zend_cleanup_function_data(zend_function *function TSRMLS_DC)
138 33649 : {
139 33649 : if (function->type == ZEND_USER_FUNCTION) {
140 15998 : zend_cleanup_op_array_data((zend_op_array *) function);
141 15998 : return ZEND_HASH_APPLY_KEEP;
142 : } else {
143 17651 : return ZEND_HASH_APPLY_STOP;
144 : }
145 : }
146 :
147 : ZEND_API int zend_cleanup_function_data_full(zend_function *function TSRMLS_DC)
148 93129 : {
149 93129 : if (function->type == ZEND_USER_FUNCTION) {
150 20170 : zend_cleanup_op_array_data((zend_op_array *) function);
151 : }
152 93129 : return 0;
153 : }
154 :
155 : ZEND_API int zend_cleanup_class_data(zend_class_entry **pce TSRMLS_DC)
156 2778815 : {
157 2778815 : if ((*pce)->type == ZEND_USER_CLASS) {
158 : /* Clean all parts that can contain run-time data */
159 : /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
160 : not contain objects and thus are not probelmatic */
161 7608 : zend_hash_apply(&(*pce)->function_table, (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
162 7608 : (*pce)->static_members = NULL;
163 2771207 : } else if (CE_STATIC_MEMBERS(*pce)) {
164 10594 : zend_hash_destroy(CE_STATIC_MEMBERS(*pce));
165 10594 : FREE_HASHTABLE(CE_STATIC_MEMBERS(*pce));
166 : #ifdef ZTS
167 : CG(static_members)[(zend_intptr_t)((*pce)->static_members)] = NULL;
168 : #else
169 10594 : (*pce)->static_members = NULL;
170 : #endif
171 : }
172 2778815 : return 0;
173 : }
174 :
175 : ZEND_API void destroy_zend_class(zend_class_entry **pce)
176 2785783 : {
177 2785783 : zend_class_entry *ce = *pce;
178 :
179 2785783 : if (--ce->refcount > 0) {
180 6144 : return;
181 : }
182 2779639 : switch (ce->type) {
183 : case ZEND_USER_CLASS:
184 6233 : zend_hash_destroy(&ce->default_properties);
185 6233 : zend_hash_destroy(&ce->properties_info);
186 6233 : zend_hash_destroy(&ce->default_static_members);
187 6233 : efree(ce->name);
188 6233 : zend_hash_destroy(&ce->function_table);
189 6233 : zend_hash_destroy(&ce->constants_table);
190 6233 : if (ce->num_interfaces > 0 && ce->interfaces) {
191 478 : efree(ce->interfaces);
192 : }
193 6233 : if (ce->doc_comment) {
194 12 : efree(ce->doc_comment);
195 : }
196 6233 : efree(ce);
197 6233 : break;
198 : case ZEND_INTERNAL_CLASS:
199 2773406 : zend_hash_destroy(&ce->default_properties);
200 2773406 : zend_hash_destroy(&ce->properties_info);
201 2773406 : zend_hash_destroy(&ce->default_static_members);
202 2773406 : free(ce->name);
203 2773406 : zend_hash_destroy(&ce->function_table);
204 2773406 : zend_hash_destroy(&ce->constants_table);
205 2773406 : if (ce->num_interfaces > 0) {
206 953910 : free(ce->interfaces);
207 : }
208 2773406 : if (ce->doc_comment) {
209 0 : free(ce->doc_comment);
210 : }
211 2773406 : free(ce);
212 : break;
213 : }
214 : }
215 :
216 : void zend_class_add_ref(zend_class_entry **ce)
217 0 : {
218 0 : (*ce)->refcount++;
219 0 : }
220 :
221 : ZEND_API void destroy_op_array(zend_op_array *op_array TSRMLS_DC)
222 67530 : {
223 67530 : zend_op *opline = op_array->opcodes;
224 67530 : zend_op *end = op_array->opcodes+op_array->last;
225 : zend_uint i;
226 :
227 67530 : if (op_array->static_variables) {
228 1044 : zend_hash_destroy(op_array->static_variables);
229 1044 : FREE_HASHTABLE(op_array->static_variables);
230 : }
231 :
232 67530 : if (--(*op_array->refcount)>0) {
233 13734 : return;
234 : }
235 :
236 53796 : efree(op_array->refcount);
237 :
238 53796 : if (op_array->vars) {
239 40072 : i = op_array->last_var;
240 312285 : while (i > 0) {
241 232141 : i--;
242 232141 : efree(op_array->vars[i].name);
243 : }
244 40072 : efree(op_array->vars);
245 : }
246 :
247 1891021 : while (opline<end) {
248 1783429 : if (opline->op1.op_type==IS_CONST) {
249 : #if DEBUG_ZEND>2
250 : printf("Reducing refcount for %x 1=>0 (destroying)\n", &opline->op1.u.constant);
251 : #endif
252 741614 : zval_dtor(&opline->op1.u.constant);
253 : }
254 1783429 : if (opline->op2.op_type==IS_CONST) {
255 : #if DEBUG_ZEND>2
256 : printf("Reducing refcount for %x 1=>0 (destroying)\n", &opline->op2.u.constant);
257 : #endif
258 240553 : zval_dtor(&opline->op2.u.constant);
259 : }
260 1783429 : opline++;
261 : }
262 53796 : efree(op_array->opcodes);
263 :
264 53796 : if (op_array->function_name) {
265 28903 : efree(op_array->function_name);
266 : }
267 53796 : if (op_array->doc_comment) {
268 1972 : efree(op_array->doc_comment);
269 : }
270 53796 : if (op_array->brk_cont_array) {
271 8123 : efree(op_array->brk_cont_array);
272 : }
273 53796 : if (op_array->try_catch_array) {
274 1417 : efree(op_array->try_catch_array);
275 : }
276 53796 : if (op_array->done_pass_two) {
277 53783 : zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array TSRMLS_CC);
278 : }
279 53796 : if (op_array->arg_info) {
280 142772 : for (i=0; i<op_array->num_args; i++) {
281 121384 : efree((char*)op_array->arg_info[i].name);
282 121384 : if (op_array->arg_info[i].class_name) {
283 134 : efree((char*)op_array->arg_info[i].class_name);
284 : }
285 : }
286 21388 : efree(op_array->arg_info);
287 : }
288 : }
289 :
290 : void init_op(zend_op *op TSRMLS_DC)
291 1966112 : {
292 1966112 : memset(op, 0, sizeof(zend_op));
293 1966112 : op->lineno = CG(zend_lineno);
294 1966112 : SET_UNUSED(op->result);
295 1966112 : }
296 :
297 : zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC)
298 1867581 : {
299 1867581 : zend_uint next_op_num = op_array->last++;
300 : zend_op *next_op;
301 :
302 1867581 : if (next_op_num >= op_array->size) {
303 7568 : if (op_array->fn_flags & ZEND_ACC_INTERACTIVE) {
304 : /* we messed up */
305 0 : zend_printf("Ran out of opcode space!\n"
306 : "You should probably consider writing this huge script into a file!\n");
307 0 : zend_bailout();
308 : }
309 7568 : op_array->size *= 4;
310 7568 : op_array_alloc_ops(op_array);
311 : }
312 :
313 1867581 : next_op = &(op_array->opcodes[next_op_num]);
314 :
315 1867581 : init_op(next_op TSRMLS_CC);
316 :
317 1867581 : return next_op;
318 : }
319 :
320 : int get_next_op_number(zend_op_array *op_array)
321 764640 : {
322 764640 : return op_array->last;
323 : }
324 :
325 : zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
326 11967 : {
327 11967 : op_array->last_brk_cont++;
328 11967 : op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
329 11967 : return &op_array->brk_cont_array[op_array->last_brk_cont-1];
330 : }
331 :
332 : static void zend_update_extended_info(zend_op_array *op_array TSRMLS_DC)
333 0 : {
334 0 : zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
335 :
336 0 : while (opline<end) {
337 0 : if (opline->opcode == ZEND_EXT_STMT) {
338 0 : if (opline+1<end) {
339 0 : if ((opline+1)->opcode == ZEND_EXT_STMT) {
340 0 : opline->opcode = ZEND_NOP;
341 0 : opline++;
342 0 : continue;
343 : }
344 0 : if (opline+1<end) {
345 0 : opline->lineno = (opline+1)->lineno;
346 : }
347 : } else {
348 0 : opline->opcode = ZEND_NOP;
349 : }
350 : }
351 0 : opline++;
352 : }
353 0 : }
354 :
355 : static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
356 0 : {
357 0 : if (extension->op_array_handler) {
358 0 : extension->op_array_handler(op_array);
359 : }
360 0 : }
361 :
362 : ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC)
363 54833 : {
364 : zend_op *opline, *end;
365 :
366 54833 : if (op_array->type!=ZEND_USER_FUNCTION && op_array->type!=ZEND_EVAL_CODE) {
367 0 : return 0;
368 : }
369 54833 : if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
370 0 : zend_update_extended_info(op_array TSRMLS_CC);
371 : }
372 54833 : if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
373 54666 : zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array TSRMLS_CC);
374 : }
375 :
376 54833 : if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && op_array->size != op_array->last) {
377 54727 : op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
378 54727 : op_array->size = op_array->last;
379 : }
380 :
381 54833 : opline = op_array->opcodes;
382 54833 : end = opline + op_array->last;
383 1974940 : while (opline < end) {
384 1865277 : if (opline->op1.op_type == IS_CONST) {
385 814851 : Z_SET_ISREF(opline->op1.u.constant);
386 814851 : Z_SET_REFCOUNT(opline->op1.u.constant, 2); /* Make sure is_ref won't be reset */
387 : }
388 1865277 : if (opline->op2.op_type == IS_CONST) {
389 243422 : Z_SET_ISREF(opline->op2.u.constant);
390 243422 : Z_SET_REFCOUNT(opline->op2.u.constant, 2);
391 : }
392 1865277 : switch (opline->opcode) {
393 : case ZEND_GOTO:
394 16 : if (Z_TYPE(opline->op2.u.constant) != IS_LONG) {
395 15 : zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
396 : }
397 : /* break omitted intentionally */
398 : case ZEND_JMP:
399 98874 : opline->op1.u.jmp_addr = &op_array->opcodes[opline->op1.u.opline_num];
400 98874 : break;
401 : case ZEND_JMPZ:
402 : case ZEND_JMPNZ:
403 : case ZEND_JMPZ_EX:
404 : case ZEND_JMPNZ_EX:
405 : case ZEND_JMP_SET:
406 89046 : opline->op2.u.jmp_addr = &op_array->opcodes[opline->op2.u.opline_num];
407 : break;
408 : }
409 1865274 : ZEND_VM_SET_OPCODE_HANDLER(opline);
410 1865274 : opline++;
411 : }
412 :
413 54830 : op_array->done_pass_two = 1;
414 54830 : return 0;
415 : }
416 :
417 : int print_class(zend_class_entry *class_entry TSRMLS_DC)
418 0 : {
419 0 : printf("Class %s:\n", class_entry->name);
420 0 : zend_hash_apply(&class_entry->function_table, (apply_func_t) pass_two TSRMLS_CC);
421 0 : printf("End of class %s.\n\n", class_entry->name);
422 0 : return 0;
423 : }
424 :
425 : ZEND_API unary_op_type get_unary_op(int opcode)
426 0 : {
427 0 : switch (opcode) {
428 : case ZEND_BW_NOT:
429 0 : return (unary_op_type) bitwise_not_function;
430 : break;
431 : case ZEND_BOOL_NOT:
432 0 : return (unary_op_type) boolean_not_function;
433 : break;
434 : default:
435 0 : return (unary_op_type) NULL;
436 : break;
437 : }
438 : }
439 :
440 : ZEND_API binary_op_type get_binary_op(int opcode)
441 0 : {
442 0 : switch (opcode) {
443 : case ZEND_ADD:
444 : case ZEND_ASSIGN_ADD:
445 0 : return (binary_op_type) add_function;
446 : break;
447 : case ZEND_SUB:
448 : case ZEND_ASSIGN_SUB:
449 0 : return (binary_op_type) sub_function;
450 : break;
451 : case ZEND_MUL:
452 : case ZEND_ASSIGN_MUL:
453 0 : return (binary_op_type) mul_function;
454 : break;
455 : case ZEND_DIV:
456 : case ZEND_ASSIGN_DIV:
457 0 : return (binary_op_type) div_function;
458 : break;
459 : case ZEND_MOD:
460 : case ZEND_ASSIGN_MOD:
461 0 : return (binary_op_type) mod_function;
462 : break;
463 : case ZEND_SL:
464 : case ZEND_ASSIGN_SL:
465 0 : return (binary_op_type) shift_left_function;
466 : break;
467 : case ZEND_SR:
468 : case ZEND_ASSIGN_SR:
469 0 : return (binary_op_type) shift_right_function;
470 : break;
471 : case ZEND_CONCAT:
472 : case ZEND_ASSIGN_CONCAT:
473 0 : return (binary_op_type) concat_function;
474 : break;
475 : case ZEND_IS_IDENTICAL:
476 0 : return (binary_op_type) is_identical_function;
477 : break;
478 : case ZEND_IS_NOT_IDENTICAL:
479 0 : return (binary_op_type) is_not_identical_function;
480 : break;
481 : case ZEND_IS_EQUAL:
482 0 : return (binary_op_type) is_equal_function;
483 : break;
484 : case ZEND_IS_NOT_EQUAL:
485 0 : return (binary_op_type) is_not_equal_function;
486 : break;
487 : case ZEND_IS_SMALLER:
488 0 : return (binary_op_type) is_smaller_function;
489 : break;
490 : case ZEND_IS_SMALLER_OR_EQUAL:
491 0 : return (binary_op_type) is_smaller_or_equal_function;
492 : break;
493 : case ZEND_BW_OR:
494 : case ZEND_ASSIGN_BW_OR:
495 0 : return (binary_op_type) bitwise_or_function;
496 : break;
497 : case ZEND_BW_AND:
498 : case ZEND_ASSIGN_BW_AND:
499 0 : return (binary_op_type) bitwise_and_function;
500 : break;
501 : case ZEND_BW_XOR:
502 : case ZEND_ASSIGN_BW_XOR:
503 0 : return (binary_op_type) bitwise_xor_function;
504 : break;
505 : case ZEND_BOOL_XOR:
506 0 : return (binary_op_type) boolean_xor_function;
507 : break;
508 : default:
509 0 : return (binary_op_type) NULL;
510 : break;
511 : }
512 : }
513 :
514 : /*
515 : * Local variables:
516 : * tab-width: 4
517 : * c-basic-offset: 4
518 : * indent-tabs-mode: t
519 : * End:
520 : */
|