php-src/Zend/zend_opcode.c

418 lines
10 KiB
C
Raw Normal View History

1999-04-07 18:10:10 +00:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2000-03-06 05:26:39 +00:00
| Copyright (c) 1998-2000 Zend Technologies Ltd. (http://www.zend.com) |
1999-04-07 18:10:10 +00:00
+----------------------------------------------------------------------+
2000-03-06 05:26:39 +00:00
| This source file is subject to version 0.92 of the Zend license, |
1999-07-16 14:58:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
2000-03-06 05:26:39 +00:00
| http://www.zend.com/license/0_92.txt. |
1999-07-16 14:58:16 +00:00
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
1999-04-07 18:10:10 +00:00
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
1999-07-16 14:58:16 +00:00
1999-04-07 18:10:10 +00:00
#include <stdio.h>
#include "zend.h"
#include "zend_alloc.h"
#include "zend_compile.h"
#include "zend_variables.h"
#include "zend_operators.h"
#include "zend_extensions.h"
#include "zend_API.h"
1999-04-07 18:10:10 +00:00
static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array)
{
if (extension->op_array_ctor) {
if (extension->resource_number>=0) {
extension->op_array_ctor(&op_array->reserved[extension->resource_number]);
} else {
extension->op_array_ctor(NULL);
}
}
}
static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array)
{
if (extension->op_array_dtor) {
if (extension->resource_number>=0) {
extension->op_array_dtor(&op_array->reserved[extension->resource_number]);
} else {
extension->op_array_dtor(NULL);
}
}
}
static void op_array_alloc_ops(zend_op_array *op_array)
{
op_array->opcodes = erealloc(op_array->opcodes, (op_array->size)*sizeof(zend_op));
}
void init_op_array(zend_op_array *op_array, int initial_ops_size)
{
op_array->type = ZEND_USER_FUNCTION;
#if SUPPORT_INTERACTIVE
{
ELS_FETCH();
1999-04-07 18:10:10 +00:00
op_array->start_op_number = op_array->end_op_number = op_array->last_executed_op_number = 0;
op_array->backpatch_count = 0;
if (EG(interactive)) {
/* We must avoid a realloc() on the op_array in interactive mode, since pointers to constants
* will become invalid
*/
initial_ops_size = 8192;
}
1999-04-07 18:10:10 +00:00
}
#endif
op_array->refcount = (int *) emalloc(sizeof(int));
*op_array->refcount = 1;
op_array->size = initial_ops_size;
op_array->last = 0;
op_array->opcodes = NULL;
op_array_alloc_ops(op_array);
op_array->T = 0;
op_array->function_name = NULL;
op_array->arg_types = NULL;
op_array->brk_cont_array = NULL;
op_array->last_brk_cont = 0;
op_array->current_brk_cont = -1;
op_array->static_variables = NULL;
1999-06-04 13:09:24 +00:00
op_array->uses_globals = 0;
1999-04-08 20:21:36 +00:00
op_array->return_reference = 0;
1999-04-07 18:10:10 +00:00
zend_llist_apply_with_argument(&zend_extensions, (void (*)(void *, void *)) zend_extension_op_array_ctor_handler, op_array);
}
ZEND_API void destroy_zend_function(zend_function *function)
1999-04-07 18:10:10 +00:00
{
switch (function->type) {
case ZEND_USER_FUNCTION:
destroy_op_array((zend_op_array *) function);
break;
case ZEND_INTERNAL_FUNCTION:
/* do nothing */
break;
}
}
1999-04-14 19:53:33 +00:00
ZEND_API void destroy_zend_class(zend_class_entry *ce)
1999-04-07 18:10:10 +00:00
{
if (--(*ce->refcount)>0) {
return;
}
1999-04-07 18:10:10 +00:00
switch (ce->type) {
case ZEND_USER_CLASS:
efree(ce->name);
efree(ce->refcount);
1999-04-07 18:10:10 +00:00
zend_hash_destroy(&ce->function_table);
zend_hash_destroy(&ce->default_properties);
break;
case ZEND_INTERNAL_CLASS:
free(ce->name);
free(ce->refcount);
1999-04-07 18:10:10 +00:00
zend_hash_destroy(&ce->function_table);
zend_hash_destroy(&ce->default_properties);
break;
}
}
void zend_class_add_ref(zend_class_entry *ce)
{
(*ce->refcount)++;
}
1999-04-07 18:10:10 +00:00
ZEND_API void destroy_op_array(zend_op_array *op_array)
{
zend_op *opline = op_array->opcodes;
zend_op *end = op_array->opcodes+op_array->last;
if (op_array->static_variables) {
zend_hash_destroy(op_array->static_variables);
FREE_HASHTABLE(op_array->static_variables);
}
1999-04-07 18:10:10 +00:00
if (--(*op_array->refcount)>0) {
return;
}
efree(op_array->refcount);
while (opline<end) {
if (opline->op1.op_type==IS_CONST) {
#if DEBUG_ZEND>2
printf("Reducing refcount for %x 1=>0 (destroying)\n", &opline->op1.u.constant);
#endif
zval_dtor(&opline->op1.u.constant);
}
if (opline->op2.op_type==IS_CONST) {
#if DEBUG_ZEND>2
printf("Reducing refcount for %x 1=>0 (destroying)\n", &opline->op2.u.constant);
#endif
zval_dtor(&opline->op2.u.constant);
}
opline++;
}
efree(op_array->opcodes);
if (op_array->function_name) {
efree(op_array->function_name);
}
if (op_array->arg_types) {
efree(op_array->arg_types);
}
if (op_array->brk_cont_array) {
efree(op_array->brk_cont_array);
}
zend_llist_apply_with_argument(&zend_extensions, (void (*)(void *, void *)) zend_extension_op_array_dtor_handler, op_array);
}
void init_op(zend_op *op CLS_DC)
{
op->lineno = CG(zend_lineno);
op->filename = zend_get_compiled_filename(CLS_C);
op->result.op_type = IS_UNUSED;
op->extended_value = 0;
op->op1.u.EA.var = 0;
op->op1.u.EA.type = 0;
op->op2.u.EA.var = 0;
op->op2.u.EA.type = 0;
op->result.u.EA.var = 0;
op->result.u.EA.type = 0;
}
1999-04-07 18:10:10 +00:00
zend_op *get_next_op(zend_op_array *op_array CLS_DC)
{
zend_uint next_op_num = op_array->last++;
1999-04-07 18:10:10 +00:00
zend_op *next_op;
if (next_op_num >= op_array->size) {
#if SUPPORT_INTERACTIVE
ELS_FETCH();
if (EG(interactive)) {
/* we messed up */
zend_printf("Ran out of opcode space!\n"
"You should probably consider writing this huge script into a file!\n");
zend_bailout();
}
#endif
op_array->size *= 2;
op_array_alloc_ops(op_array);
}
next_op = &(op_array->opcodes[next_op_num]);
init_op(next_op CLS_CC);
1999-04-07 18:10:10 +00:00
return next_op;
}
int get_next_op_number(zend_op_array *op_array)
{
return op_array->last;
}
zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
{
op_array->last_brk_cont++;
op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
return &op_array->brk_cont_array[op_array->last_brk_cont-1];
}
static void zend_update_extended_info(zend_op_array *op_array CLS_DC)
1999-04-07 18:10:10 +00:00
{
zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
while (opline<end) {
if (opline->opcode == ZEND_EXT_STMT) {
if (opline+1<end) {
if ((opline+1)->opcode == ZEND_EXT_STMT) {
opline->opcode = ZEND_NOP;
opline++;
continue;
}
opline->lineno = (opline+1)->lineno;
opline->filename = (opline+1)->filename;
} else {
opline->opcode = ZEND_NOP;
}
}
opline++;
}
opline = get_next_op(op_array CLS_CC);
1999-04-07 18:10:10 +00:00
opline->opcode = ZEND_EXT_STMT;
opline->op1.op_type = IS_UNUSED;
opline->op2.op_type = IS_UNUSED;
if (op_array->last>0) {
opline->filename = op_array->opcodes[op_array->last-2].filename;
opline->lineno= op_array->opcodes[op_array->last-2].lineno;
}
}
static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
{
if (extension->op_array_handler) {
extension->op_array_handler(op_array);
}
}
int pass_two(zend_op_array *op_array)
{
CLS_FETCH();
if (op_array->type!=ZEND_USER_FUNCTION && op_array->type!=ZEND_EVAL_CODE) {
1999-04-07 18:10:10 +00:00
return 0;
}
if (CG(extended_info)) {
zend_update_extended_info(op_array CLS_CC);
1999-04-07 18:10:10 +00:00
}
if (CG(handle_op_arrays)) {
zend_llist_apply_with_argument(&zend_extensions, (void (*)(void *, void *)) zend_extension_op_array_handler, op_array);
}
return 0;
}
void pass_include_eval(zend_op_array *op_array)
{
zend_op *opline=op_array->opcodes, *end=opline+op_array->last;
while (opline<end) {
if (opline->op1.op_type==IS_CONST) {
opline->op1.u.constant.is_ref = 1;
opline->op1.u.constant.refcount = 2; /* Make sure is_ref won't be reset */
1999-04-07 18:10:10 +00:00
}
if (opline->op2.op_type==IS_CONST) {
opline->op2.u.constant.is_ref = 1;
opline->op2.u.constant.refcount = 2;
1999-04-07 18:10:10 +00:00
}
opline++;
}
}
int print_class(zend_class_entry *class_entry)
{
printf("Class %s:\n", class_entry->name);
zend_hash_apply(&class_entry->function_table, (int (*)(void *)) pass_two);
printf("End of class %s.\n\n", class_entry->name);
return 0;
}
ZEND_API unary_op_type get_unary_op(int opcode)
1999-04-07 18:10:10 +00:00
{
switch(opcode) {
case ZEND_BW_NOT:
return (unary_op_type) bitwise_not_function;
1999-04-07 18:10:10 +00:00
break;
case ZEND_BOOL_NOT:
return (unary_op_type) boolean_not_function;
1999-04-07 18:10:10 +00:00
break;
default:
return (unary_op_type) NULL;
1999-04-07 18:10:10 +00:00
break;
}
}
ZEND_API void *get_binary_op(int opcode)
{
switch (opcode) {
case ZEND_ADD:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_ADD:
1999-04-07 18:10:10 +00:00
return (void *) add_function;
break;
case ZEND_SUB:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_SUB:
1999-04-07 18:10:10 +00:00
return (void *) sub_function;
break;
case ZEND_MUL:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_MUL:
1999-04-07 18:10:10 +00:00
return (void *) mul_function;
break;
case ZEND_DIV:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_DIV:
1999-04-07 18:10:10 +00:00
return (void *) div_function;
break;
case ZEND_MOD:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_MOD:
1999-04-07 18:10:10 +00:00
return (void *) mod_function;
break;
case ZEND_SL:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_SL:
1999-04-07 18:10:10 +00:00
return (void *) shift_left_function;
break;
case ZEND_SR:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_SR:
1999-04-07 18:10:10 +00:00
return (void *) shift_right_function;
break;
case ZEND_CONCAT:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_CONCAT:
1999-04-07 18:10:10 +00:00
return (void *) concat_function;
break;
case ZEND_IS_IDENTICAL:
return (void *) is_identical_function;
break;
1999-04-24 00:12:55 +00:00
case ZEND_IS_EQUAL:
1999-04-07 18:10:10 +00:00
return (void *) is_equal_function;
break;
1999-04-24 00:12:55 +00:00
case ZEND_IS_NOT_EQUAL:
1999-04-07 18:10:10 +00:00
return (void *) is_not_equal_function;
break;
case ZEND_IS_SMALLER:
return (void *) is_smaller_function;
break;
1999-04-24 00:12:55 +00:00
case ZEND_IS_SMALLER_OR_EQUAL:
1999-04-07 18:10:10 +00:00
return (void *) is_smaller_or_equal_function;
break;
case ZEND_BW_OR:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_BW_OR:
1999-04-07 18:10:10 +00:00
return (void *) bitwise_or_function;
break;
case ZEND_BW_AND:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_BW_AND:
1999-04-07 18:10:10 +00:00
return (void *) bitwise_and_function;
break;
case ZEND_BW_XOR:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_BW_XOR:
1999-04-07 18:10:10 +00:00
return (void *) bitwise_xor_function;
break;
default:
return (void *) NULL;
break;
}
}