Skip to content

Commit f7b7800

Browse files
committed
Add infrastructure for JIT initialization of auto globals
1 parent da0d5a1 commit f7b7800

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Zend/zend.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static void compiler_globals_ctor(zend_compiler_globals *compiler_globals TSRMLS
417417

418418
compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
419419
zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, NULL, 1, 0);
420-
zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL, NULL, sizeof(void *) /* empty element */);
420+
zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL, NULL, sizeof(zend_auto_global) /* empty element */);
421421
}
422422

423423

@@ -573,7 +573,7 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions, i
573573

574574
#ifdef ZTS
575575
zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 20, NULL, ZEND_CONSTANT_DTOR, 1, 0);
576-
zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, NULL, 1, 0);
576+
zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, (dtor_func_t) zend_auto_global_dtor, 1, 0);
577577
ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
578578
ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
579579
ts_allocate_id(&language_scanner_globals_id, sizeof(zend_scanner_globals), (ts_allocate_ctor) scanner_globals_ctor, NULL);
@@ -590,7 +590,7 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions, i
590590
zend_hash_destroy(executor_globals->zend_constants);
591591
*executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
592592
#else
593-
zend_hash_init_ex(CG(auto_globals), 8, NULL, NULL, 1, 0);
593+
zend_hash_init_ex(CG(auto_globals), 8, NULL, (dtor_func_t) zend_auto_global_dtor, 1, 0);
594594
scanner_globals_ctor(&ini_scanner_globals TSRMLS_CC);
595595
scanner_globals_ctor(&language_scanner_globals TSRMLS_CC);
596596
zend_startup_constants();
@@ -600,6 +600,7 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions, i
600600
#endif
601601
register_standard_class(TSRMLS_C);
602602
zend_register_standard_constants(TSRMLS_C);
603+
zend_register_auto_global("GLOBALS", sizeof("GLOBALS")-1, NULL TSRMLS_CC);
603604

604605
#ifndef ZTS
605606
zend_init_rsrc_plist(TSRMLS_C);

Zend/zend_compile.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ static void build_runtime_defined_function_key(zval *result, char *name, int nam
5858
}
5959

6060

61+
int zend_auto_global_arm(zend_auto_global *auto_global TSRMLS_DC)
62+
{
63+
auto_global->armed = (auto_global->auto_global_callback ? 1 : 0);
64+
return 0;
65+
}
66+
67+
6168
static void init_compiler_declarables(TSRMLS_D)
6269
{
6370
CG(declarables).ticks.type = IS_LONG;
@@ -84,8 +91,8 @@ void zend_init_compiler_data_structures(TSRMLS_D)
8491
CG(start_lineno) = 0;
8592
init_compiler_declarables(TSRMLS_C);
8693
CG(throw_list) = NULL;
87-
zend_register_auto_global("GLOBALS", sizeof("GLOBALS")-1 TSRMLS_CC);
8894
CG(in_clone_method) = 0;
95+
zend_hash_apply(CG(auto_globals), (apply_func_t) zend_auto_global_arm TSRMLS_CC);
8996
}
9097

9198

@@ -277,8 +284,13 @@ void fetch_simple_variable_ex(znode *result, znode *varname, int bp, zend_uchar
277284

278285
opline_ptr->op2.u.EA.type = ZEND_FETCH_LOCAL;
279286
if (varname->op_type == IS_CONST && varname->u.constant.type == IS_STRING) {
280-
if (zend_hash_exists(CG(auto_globals), varname->u.constant.value.str.val, varname->u.constant.value.str.len+1)) {
287+
zend_auto_global *auto_global;
288+
289+
if (zend_hash_find(CG(auto_globals), varname->u.constant.value.str.val, varname->u.constant.value.str.len+1, (void **) &auto_global)==SUCCESS) {
281290
opline_ptr->op2.u.EA.type = ZEND_FETCH_GLOBAL;
291+
if (auto_global->armed) {
292+
auto_global->armed = auto_global->auto_global_callback(auto_global->name, auto_global->name_len TSRMLS_CC);
293+
}
282294
} else {
283295
/* if (CG(active_op_array)->static_variables && zend_hash_exists(CG(active_op_array)->static_variables, varname->u.constant.value.str.val, varname->u.constant.value.str.len+1)) {
284296
opline_ptr->op2.u.EA.type = ZEND_FETCH_STATIC;
@@ -3152,9 +3164,20 @@ void zend_do_ticks(TSRMLS_D)
31523164
}
31533165

31543166

3155-
int zend_register_auto_global(char *name, uint name_len TSRMLS_DC)
3167+
void zend_auto_global_dtor(zend_auto_global *auto_global)
3168+
{
3169+
free(auto_global->name);
3170+
}
3171+
3172+
int zend_register_auto_global(char *name, uint name_len, zend_auto_global_callback auto_global_callback TSRMLS_DC)
31563173
{
3157-
return zend_hash_add_empty_element(CG(auto_globals), name, name_len+1);
3174+
zend_auto_global auto_global;
3175+
3176+
auto_global.name = zend_strndup(name, name_len);
3177+
auto_global.name_len = name_len;
3178+
auto_global.auto_global_callback = auto_global_callback;
3179+
3180+
return zend_hash_add(CG(auto_globals), name, name_len+1, &auto_global, sizeof(zend_auto_global), NULL);
31583181
}
31593182

31603183

Zend/zend_compile.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,16 @@ ZEND_API zend_bool zend_is_compiling(TSRMLS_D);
462462
ZEND_API char *zend_make_compiled_string_description(char *name TSRMLS_DC);
463463
void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers TSRMLS_DC);
464464

465+
typedef zend_bool (*zend_auto_global_callback)(char *name, uint name_len TSRMLS_DC);
466+
typedef struct _zend_auto_global {
467+
char *name;
468+
uint name_len;
469+
zend_auto_global_callback auto_global_callback;
470+
zend_bool armed;
471+
} zend_auto_global;
465472

466-
int zend_register_auto_global(char *name, uint name_len TSRMLS_DC);
473+
void zend_auto_global_dtor(zend_auto_global *auto_global);
474+
int zend_register_auto_global(char *name, uint name_len, zend_auto_global_callback auto_global_callback TSRMLS_DC);
467475

468476
int zendlex(znode *zendlval TSRMLS_DC);
469477

0 commit comments

Comments
 (0)