Skip to content

Commit 7865dfb

Browse files
committed
Eliminated run-time constant fetching for TRUE, FALSE and NULL
1 parent 056bea6 commit 7865dfb

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? Mar 2006, PHP 5.1.3RC2
4+
- Eliminated run-time constant fetching for TRUE, FALSE and NULL. (Dmitry)
45
- Fixed offset/length parameter validation in substr_compare() function. (Ilia)
56
- Added overflow checks to wordwrap() function. (Ilia)
67
- Removed the E_STRICT deprecation notice from "var". (Ilia)

Zend/zend_compile.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,20 +3095,49 @@ void zend_do_end_new_object(znode *result, znode *new_token, znode *argument_lis
30953095
*result = CG(active_op_array)->opcodes[new_token->u.opline_num].result;
30963096
}
30973097

3098+
static int zend_constant_ct_subst(znode *result, zval *const_name TSRMLS_DC)
3099+
{
3100+
zend_constant *c = NULL;
3101+
3102+
if (zend_hash_find(EG(zend_constants), Z_STRVAL_P(const_name), Z_STRLEN_P(const_name)+1, (void **) &c) == FAILURE) {
3103+
char *lookup_name = zend_str_tolower_dup(Z_STRVAL_P(const_name), Z_STRLEN_P(const_name));
3104+
3105+
if (zend_hash_find(EG(zend_constants), lookup_name, Z_STRLEN_P(const_name)+1, (void **) &c)==SUCCESS) {
3106+
if ((c->flags & CONST_CS) && memcmp(c->name, Z_STRVAL_P(const_name), Z_STRLEN_P(const_name))!=0) {
3107+
c = NULL;
3108+
}
3109+
} else {
3110+
c = NULL;
3111+
}
3112+
efree(lookup_name);
3113+
}
3114+
if (c && (c->flags & CONST_CT_SUBST)) {
3115+
zval_dtor(const_name);
3116+
result->op_type = IS_CONST;
3117+
result->u.constant = c->value;
3118+
zval_copy_ctor(&result->u.constant);
3119+
INIT_PZVAL(&result->u.constant);
3120+
return 1;
3121+
}
3122+
return 0;
3123+
}
3124+
30983125
void zend_do_fetch_constant(znode *result, znode *constant_container, znode *constant_name, int mode TSRMLS_DC)
30993126
{
31003127
switch (mode) {
31013128
case ZEND_CT:
31023129
if (constant_container) {
31033130
zend_do_fetch_class_name(NULL, constant_container, constant_name TSRMLS_CC);
31043131
*result = *constant_container;
3105-
} else {
3132+
result->u.constant.type = IS_CONSTANT;
3133+
} else if (!zend_constant_ct_subst(result, &constant_name->u.constant TSRMLS_CC)) {
31063134
*result = *constant_name;
3135+
result->u.constant.type = IS_CONSTANT;
31073136
}
3108-
result->u.constant.type = IS_CONSTANT;
31093137
break;
31103138
case ZEND_RT:
3111-
{
3139+
if (constant_container ||
3140+
!zend_constant_ct_subst(result, &constant_name->u.constant TSRMLS_CC)) {
31123141
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
31133142

31143143
opline->opcode = ZEND_FETCH_CONSTANT;

Zend/zend_constants.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ void zend_register_standard_constants(TSRMLS_D)
114114
{
115115
zend_constant c;
116116

117-
c.value.type = IS_BOOL;
118-
c.flags = CONST_PERSISTENT;
117+
c.flags = CONST_PERSISTENT | CONST_CT_SUBST;
119118
c.module_number = 0;
120119

121120
c.name = zend_strndup(ZEND_STRL("TRUE"));
@@ -130,16 +129,18 @@ void zend_register_standard_constants(TSRMLS_D)
130129
c.value.type = IS_BOOL;
131130
zend_register_constant(&c TSRMLS_CC);
132131

132+
c.name = zend_strndup(ZEND_STRL("NULL"));
133+
c.name_len = sizeof("NULL");
134+
c.value.type = IS_NULL;
135+
zend_register_constant(&c TSRMLS_CC);
136+
137+
c.flags = CONST_PERSISTENT;
138+
133139
c.name = zend_strndup(ZEND_STRL("ZEND_THREAD_SAFE"));
134140
c.name_len = sizeof("ZEND_THREAD_SAFE");
135141
c.value.value.lval = ZTS_V;
136142
c.value.type = IS_BOOL;
137143
zend_register_constant(&c TSRMLS_CC);
138-
139-
c.name = zend_strndup(ZEND_STRL("NULL"));
140-
c.name_len = sizeof("NULL");
141-
c.value.type = IS_NULL;
142-
zend_register_constant(&c TSRMLS_CC);
143144
}
144145
}
145146

Zend/zend_constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#define CONST_CS (1<<0) /* Case Sensitive */
2828
#define CONST_PERSISTENT (1<<1) /* Persistent */
29+
#define CONST_CT_SUBST (2<<1) /* Allow compile-time substitution */
2930

3031
#define PHP_USER_CONSTANT INT_MAX /* a constant defined in user space */
3132

0 commit comments

Comments
 (0)