Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:777 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6021 invoked from network); 8 Apr 2003 03:05:13 -0000 Received: from unknown (HELO carmine.bestweb.net) (209.94.102.73) by pb1.pair.com with SMTP; 8 Apr 2003 03:05:13 -0000 Received: from [192.168.1.101] (ip216-179-71-153.cust.bestweb.net [216.179.71.153]) by carmine.bestweb.net (Postfix) with ESMTP id AC296232C6; Mon, 7 Apr 2003 22:05:16 -0500 (EST) To: internals@lists.php.net Cc: andi@zend.com, Zeev Suraski In-Reply-To: <1049764010.24472.618.camel@hasele> References: <1049764010.24472.618.camel@hasele> Content-Type: multipart/mixed; boundary="=-XEWxV8GwIgHn4Mn7AuSo" Organization: Message-ID: <1049769838.24472.719.camel@hasele> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.3 Date: 07 Apr 2003 22:43:58 -0400 Subject: Re: [PHP-DEV] [PATCH] Allow constant expressions From: sterling@bumblebury.com (Sterling Hughes) --=-XEWxV8GwIgHn4Mn7AuSo Content-Type: text/plain Content-Transfer-Encoding: 7bit I've updated my patch (attached), which allows for parentheses groupings such as : const foo = (1<<2) | (2 & 5); -Sterling On Mon, 2003-04-07 at 21:06, Sterling Hughes wrote: > Hi, > > The attached patch allows constant expressions within class variables > and constants. I came into this problem when I wanted the following > sequence: > > class foo { > const A = 1<<0; > const B = 1<<1; > const C = 1<<2; > } > > And the current parser doesn't allow such rules. I therefore added the > attached patch which allows operators to be used on constants within > these expressions (and also within array declarations). This is > achieved by folding statements such as '1<<0' into their equivalent > values at compile time. The following operators are supported: << >> | > & + - * / . % ^ ~ xor > > Please review it, and let me know if its ok to commit. :) > > -Sterling -- "Science is like sex: sometimes something useful comes out, but that is not the reason we are doing it." - Richard Feynman --=-XEWxV8GwIgHn4Mn7AuSo Content-Disposition: attachment; filename=const_fold.diff Content-Type: text/plain; name=const_fold.diff; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit ? const_fold.diff 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 03:03:28 -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 03:03:30 -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 03:03:32 -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,33 @@ | 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_expr ')' { $$ = $2; } +; 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 +753,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: --=-XEWxV8GwIgHn4Mn7AuSo--