Index: zend_compile.c =================================================================== RCS file: /repository/ZendEngine2/zend_compile.c,v retrieving revision 1.408 diff -u -r1.408 zend_compile.c --- zend_compile.c 3 Apr 2003 14:44:41 -0000 1.408 +++ zend_compile.c 8 Apr 2003 01:27:25 -0000 @@ -166,6 +166,42 @@ } +void zend_do_fold_binary_op(zend_uchar op, znode *result, znode *op1, znode *op2 TSRMLS_DC) +{ + int (*do_op)(zval *, zval *, zval *); + zend_op *opline; + + if (op == ZEND_SL) { + do_op = shift_left_function; + } else if (op == ZEND_SR) { + do_op = shift_right_function; + } else if (op == ZEND_BW_OR) { + do_op = bitwise_or_function; + } else if (op == ZEND_BW_AND) { + do_op = bitwise_and_function; + } else if (op == ZEND_BW_XOR) { + do_op = bitwise_xor_function; + } else if (op == ZEND_CONCAT) { + do_op = concat_function; + } else if (op == ZEND_ADD) { + do_op = add_function; + } else if (op == ZEND_SUB) { + do_op = sub_function; + } else if (op == ZEND_MUL) { + do_op = mul_function; + } else if (op == ZEND_DIV) { + do_op = div_function; + } else if (op == ZEND_MOD) { + do_op = mod_function; + } else if (op == ZEND_BW_NOT) { + do_op = bitwise_not_function; + } else if (op == ZEND_BOOL_XOR) { + do_op = boolean_xor_function; + } + + do_op(&result->u.constant, &op1->u.constant, &op2->u.constant); +} + void zend_do_binary_op(zend_uchar op, znode *result, znode *op1, znode *op2 TSRMLS_DC) { zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); Index: zend_compile.h =================================================================== RCS file: /repository/ZendEngine2/zend_compile.h,v retrieving revision 1.235 diff -u -r1.235 zend_compile.h --- zend_compile.h 7 Apr 2003 21:48:17 -0000 1.235 +++ zend_compile.h 8 Apr 2003 01:27:26 -0000 @@ -274,6 +274,7 @@ /* parser-driven code generators */ +void zend_do_fold_binary_op(zend_uchar op, znode *result, znode *op1, znode *op2 TSRMLS_DC); void zend_do_binary_op(zend_uchar op, znode *result, znode *op1, znode *op2 TSRMLS_DC); void zend_do_unary_op(zend_uchar op, znode *result, znode *op1 TSRMLS_DC); void zend_do_binary_assign_op(zend_uchar op, znode *result, znode *op1, znode *op2 TSRMLS_DC); Index: zend_language_parser.y =================================================================== RCS file: /repository/ZendEngine2/zend_language_parser.y,v retrieving revision 1.110 diff -u -r1.110 zend_language_parser.y --- zend_language_parser.y 2 Apr 2003 16:51:49 -0000 1.110 +++ zend_language_parser.y 8 Apr 2003 01:27:29 -0000 @@ -328,12 +328,12 @@ namespace_var_declaration: T_VARIABLE { zend_do_declare_namespace_var(&$1, NULL TSRMLS_CC); } - | T_VARIABLE '=' static_scalar { zend_do_declare_namespace_var(&$1, &$3 TSRMLS_CC); } + | T_VARIABLE '=' static_scalar_expr { zend_do_declare_namespace_var(&$1, &$3 TSRMLS_CC); } ; namespace_const_declaration: - namespace_const_declaration ',' T_STRING '=' static_scalar { zend_do_declare_namespace_constant(&$3, &$5 TSRMLS_CC); } - | T_CONST T_STRING '=' static_scalar { zend_do_declare_namespace_constant(&$2, &$4 TSRMLS_CC); } + namespace_const_declaration ',' T_STRING '=' static_scalar_expr { zend_do_declare_namespace_constant(&$3, &$5 TSRMLS_CC); } + | T_CONST T_STRING '=' static_scalar_expr { zend_do_declare_namespace_constant(&$2, &$4 TSRMLS_CC); } ; extends_from: @@ -375,8 +375,8 @@ declare_list: - T_STRING '=' static_scalar { zend_do_declare_stmt(&$1, &$3 TSRMLS_CC); } - | declare_list ',' T_STRING '=' static_scalar { zend_do_declare_stmt(&$3, &$5 TSRMLS_CC); } + T_STRING '=' static_scalar_expr { zend_do_declare_stmt(&$1, &$3 TSRMLS_CC); } + | declare_list ',' T_STRING '=' static_scalar_expr { zend_do_declare_stmt(&$3, &$5 TSRMLS_CC); } ; @@ -487,9 +487,9 @@ static_var_list: static_var_list ',' T_VARIABLE { zend_do_fetch_static_variable(&$3, NULL, ZEND_FETCH_STATIC TSRMLS_CC); } - | static_var_list ',' T_VARIABLE '=' static_scalar { zend_do_fetch_static_variable(&$3, &$5, ZEND_FETCH_STATIC TSRMLS_CC); } + | static_var_list ',' T_VARIABLE '=' static_scalar_expr { zend_do_fetch_static_variable(&$3, &$5, ZEND_FETCH_STATIC TSRMLS_CC); } | T_VARIABLE { zend_do_fetch_static_variable(&$1, NULL, ZEND_FETCH_STATIC TSRMLS_CC); } - | T_VARIABLE '=' static_scalar { zend_do_fetch_static_variable(&$1, &$3, ZEND_FETCH_STATIC TSRMLS_CC); } + | T_VARIABLE '=' static_scalar_expr { zend_do_fetch_static_variable(&$1, &$3, ZEND_FETCH_STATIC TSRMLS_CC); } ; @@ -539,14 +539,14 @@ class_variable_declaration: class_variable_declaration ',' T_VARIABLE { zend_do_declare_property(&$3, NULL, CG(access_type) TSRMLS_CC); } - | class_variable_declaration ',' T_VARIABLE '=' static_scalar { zend_do_declare_property(&$3, &$5, CG(access_type) TSRMLS_CC); } + | class_variable_declaration ',' T_VARIABLE '=' static_scalar_expr { zend_do_declare_property(&$3, &$5, CG(access_type) TSRMLS_CC); } | T_VARIABLE { zend_do_declare_property(&$1, NULL, CG(access_type) TSRMLS_CC); } - | T_VARIABLE '=' static_scalar { zend_do_declare_property(&$1, &$3, CG(access_type) TSRMLS_CC); } + | T_VARIABLE '=' static_scalar_expr { zend_do_declare_property(&$1, &$3, CG(access_type) TSRMLS_CC); } ; class_constant_declaration: - class_constant_declaration ',' T_STRING '=' static_scalar { zend_do_declare_class_constant(&$3, &$5 TSRMLS_CC); } - | T_CONST T_STRING '=' static_scalar { zend_do_declare_class_constant(&$2, &$4 TSRMLS_CC); } + class_constant_declaration ',' T_STRING '=' static_scalar_expr { zend_do_declare_class_constant(&$3, &$5 TSRMLS_CC); } + | T_CONST T_STRING '=' static_scalar_expr { zend_do_declare_class_constant(&$2, &$4 TSRMLS_CC); } ; echo_expr_list: @@ -699,12 +699,32 @@ | T_NAMESPACE_C { $$ = $1; } ; +static_scalar_expr: /* compile-time evaluated scalar expressions */ + static_scalar { $$ = $1; } + | static_scalar_expr_list { $$ = $1; } +; + +static_scalar_expr_list: + static_scalar_expr T_SL static_scalar_expr { zend_do_fold_binary_op(ZEND_SL, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr T_SR static_scalar_expr { zend_do_fold_binary_op(ZEND_SR, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr T_LOGICAL_XOR static_scalar_expr { zend_do_fold_binary_op(ZEND_BOOL_XOR, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '|' static_scalar_expr { zend_do_fold_binary_op(ZEND_BW_OR, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '&' static_scalar_expr { zend_do_fold_binary_op(ZEND_BW_AND, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '^' static_scalar_expr { zend_do_fold_binary_op(ZEND_BW_XOR, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '.' static_scalar_expr { zend_do_fold_binary_op(ZEND_CONCAT, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '+' static_scalar_expr { zend_do_fold_binary_op(ZEND_ADD, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '-' static_scalar_expr { zend_do_fold_binary_op(ZEND_SUB, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '*' static_scalar_expr { zend_do_fold_binary_op(ZEND_MUL, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '/' static_scalar_expr { zend_do_fold_binary_op(ZEND_DIV, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '%' static_scalar_expr { zend_do_fold_binary_op(ZEND_MOD, &$$, &$1, &$3 TSRMLS_CC); } + | static_scalar_expr '~' static_scalar_expr { zend_do_fold_binary_op(ZEND_BW_NOT, &$$, &$1, &$3 TSRMLS_CC); } +; static_scalar: /* compile-time evaluated scalars */ common_scalar { $$ = $1; } | T_STRING { zend_do_fetch_constant(&$$, NULL, &$1, ZEND_CT TSRMLS_CC); } | '+' static_scalar { $$ = $2; } - | '-' static_scalar { zval minus_one; minus_one.type = IS_LONG; minus_one.value.lval = -1; mul_function(&$2.u.constant, &$2.u.constant, &minus_one TSRMLS_CC); $$ = $2; } + | '-' static_scalar { zval minus_one; minus_one.type = IS_LONG; minus_one.value.lval = -1; mul_function(&$2.u.constant, &$2.u.constant, &minus_one TSRMLS_CC); $$ = $2; } | T_ARRAY '(' static_array_pair_list ')' { $$ = $3; $$.u.constant.type = IS_CONSTANT_ARRAY; } | class_or_namespace_constant { /* FIXME */ } ; @@ -732,10 +752,10 @@ ; non_empty_static_array_pair_list: - non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW static_scalar { zend_do_add_static_array_element(&$$, &$3, &$5); } - | non_empty_static_array_pair_list ',' static_scalar { zend_do_add_static_array_element(&$$, NULL, &$3); } - | static_scalar T_DOUBLE_ARROW static_scalar { $$.op_type = IS_CONST; INIT_PZVAL(&$$.u.constant); array_init(&$$.u.constant); zend_do_add_static_array_element(&$$, &$1, &$3); } - | static_scalar { $$.op_type = IS_CONST; INIT_PZVAL(&$$.u.constant); array_init(&$$.u.constant); zend_do_add_static_array_element(&$$, NULL, &$1); } + non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW static_scalar_expr { zend_do_add_static_array_element(&$$, &$3, &$5); } + | non_empty_static_array_pair_list ',' static_scalar_expr { zend_do_add_static_array_element(&$$, NULL, &$3); } + | static_scalar T_DOUBLE_ARROW static_scalar_expr { $$.op_type = IS_CONST; INIT_PZVAL(&$$.u.constant); array_init(&$$.u.constant); zend_do_add_static_array_element(&$$, &$1, &$3); } + | static_scalar_expr { $$.op_type = IS_CONST; INIT_PZVAL(&$$.u.constant); array_init(&$$.u.constant); zend_do_add_static_array_element(&$$, NULL, &$1); } ; expr: