Skip to content

Commit c5ad440

Browse files
committed
Store original AST as string for zend_ast_op_array
1 parent 45057db commit c5ad440

File tree

6 files changed

+35
-88
lines changed

6 files changed

+35
-88
lines changed

Zend/zend_ast.c

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, ze
100100
return (zend_ast *) ast;
101101
}
102102

103-
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array, zend_ast *original_ast, zend_ast_attr attr) {
103+
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array, zend_string *original_ast) {
104104
zend_ast_op_array *ast;
105105

106106
ast = zend_ast_alloc(sizeof(zend_ast_op_array));
107107
ast->kind = ZEND_AST_OP_ARRAY;
108-
ast->attr = attr;
108+
ast->attr = 0;
109+
ast->lineno = CG(zend_lineno);
109110
ast->op_array = op_array;
110-
ast->ast = original_ast;
111+
ZEND_ASSERT(ZSTR_IS_INTERNED(original_ast));
112+
ast->original_ast = original_ast;
111113

112114
return (zend_ast *) ast;
113115
}
@@ -1091,7 +1093,7 @@ static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
10911093
if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
10921094
size = sizeof(zend_ast_zval);
10931095
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
1094-
size = sizeof(zend_ast_op_array) + zend_ast_tree_size(zend_ast_get_op_array(ast)->ast);
1096+
size = sizeof(zend_ast_op_array);
10951097
} else if (zend_ast_is_list(ast)) {
10961098
uint32_t i;
10971099
zend_ast_list *list = zend_ast_get_list(ast);
@@ -1103,13 +1105,8 @@ static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
11031105
}
11041106
}
11051107
} else if (zend_ast_is_decl(ast)) {
1106-
zend_ast_decl *decl = (zend_ast_decl*)ast;
1107-
size = sizeof(zend_ast_decl);
1108-
for (size_t i = 0; i < 5; i++) {
1109-
if (decl->child[i]) {
1110-
size += zend_ast_tree_size(decl->child[i]);
1111-
}
1112-
}
1108+
/* Not implemented. */
1109+
ZEND_UNREACHABLE();
11131110
} else {
11141111
uint32_t i, children = zend_ast_get_num_children(ast);
11151112

@@ -1161,38 +1158,13 @@ static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
11611158
zend_ast_op_array *new = (zend_ast_op_array*)buf;
11621159
new->kind = old->kind;
11631160
new->attr = old->attr;
1161+
new->lineno = old->lineno;
11641162
new->op_array = old->op_array;
1163+
new->original_ast = old->original_ast;
11651164
buf = (void*)((char*)buf + sizeof(zend_ast_op_array));
1166-
new->ast = (zend_ast*)buf;
1167-
buf = zend_ast_tree_copy(old->ast, buf);
11681165
} else if (zend_ast_is_decl(ast)) {
1169-
zend_ast_decl *old = (zend_ast_decl*)ast;
1170-
zend_ast_decl *new = (zend_ast_decl*)buf;
1171-
new->kind = old->kind;
1172-
new->attr = old->attr;
1173-
new->start_lineno = old->start_lineno;
1174-
new->end_lineno = old->end_lineno;
1175-
new->flags = old->flags;
1176-
if (old->doc_comment) {
1177-
new->doc_comment = zend_string_copy(old->doc_comment);
1178-
} else {
1179-
new->doc_comment = NULL;
1180-
}
1181-
if (old->name) {
1182-
new->name = zend_string_copy(old->name);
1183-
} else {
1184-
new->name = NULL;
1185-
}
1186-
1187-
buf = (void*)((char*)buf + sizeof(zend_ast_decl));
1188-
for (size_t i = 0; i < 5; i++) {
1189-
if (old->child[i]) {
1190-
new->child[i] = (zend_ast*)buf;
1191-
buf = zend_ast_tree_copy(old->child[i], buf);
1192-
} else {
1193-
new->child[i] = NULL;
1194-
}
1195-
}
1166+
/* Not implemented. */
1167+
ZEND_UNREACHABLE();
11961168
} else {
11971169
uint32_t i, children = zend_ast_get_num_children(ast);
11981170
zend_ast *new = (zend_ast*)buf;
@@ -1257,8 +1229,8 @@ ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
12571229
} else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
12581230
zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
12591231
} else if (EXPECTED(ast->kind == ZEND_AST_OP_ARRAY)) {
1260-
zend_ast_destroy(zend_ast_get_op_array(ast)->ast);
1261-
} else if (zend_ast_is_decl(ast)) {
1232+
zend_string_release_ex(zend_ast_get_op_array(ast)->original_ast, 0);
1233+
} else if (EXPECTED(zend_ast_is_decl(ast))) {
12621234
zend_ast_decl *decl = (zend_ast_decl *) ast;
12631235

12641236
if (decl->name) {
@@ -1875,10 +1847,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
18751847
smart_str_appendl(str, ZSTR_VAL(name), ZSTR_LEN(name));
18761848
break;
18771849
}
1878-
case ZEND_AST_OP_ARRAY: {
1879-
zend_ast_export_ex(str, zend_ast_get_op_array(ast)->ast, priority, indent);
1850+
case ZEND_AST_OP_ARRAY:
1851+
smart_str_append(str, zend_ast_get_op_array(ast)->original_ast);
18801852
break;
1881-
}
18821853
case ZEND_AST_CONSTANT_CLASS:
18831854
smart_str_appendl(str, "__CLASS__", sizeof("__CLASS__")-1);
18841855
break;

Zend/zend_ast.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ typedef struct _zend_op_array zend_op_array;
212212
typedef struct _zend_ast_op_array {
213213
zend_ast_kind kind;
214214
zend_ast_attr attr;
215+
uint32_t lineno;
215216
zend_op_array *op_array;
216-
zend_ast *ast;
217+
zend_string *original_ast;
217218
} zend_ast_op_array;
218219

219220
/* Separate structure for function and class declaration, as they need extra information. */
@@ -240,7 +241,7 @@ ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval)
240241
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr);
241242
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name);
242243

243-
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array, zend_ast *original_ast, zend_ast_attr attr);
244+
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array, zend_string *original_ast);
244245

245246
#if ZEND_AST_SPEC
246247
# define ZEND_AST_SPEC_CALL(name, ...) \
@@ -386,8 +387,6 @@ static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
386387
} else if (ast->kind == ZEND_AST_CONSTANT) {
387388
zval *zv = &((zend_ast_zval *) ast)->val;
388389
return Z_LINENO_P(zv);
389-
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
390-
return zend_ast_get_op_array(ast)->ast->lineno;
391390
} else {
392391
return ast->lineno;
393392
}

Zend/zend_compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11222,7 +11222,10 @@ static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
1122211222
znode node;
1122311223
zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
1122411224

11225-
*ast_ptr = zend_ast_create_op_array(op, (zend_ast*)closure_ast, 0);
11225+
zend_string *original_ast = zend_new_interned_string(zend_ast_export("", *ast_ptr, ""));
11226+
zend_ast_destroy(*ast_ptr);
11227+
*ast_ptr = zend_ast_create_op_array(op, original_ast);
11228+
1122611229
}
1122711230

1122811231
static void zend_compile_const_expr_args(zend_ast **ast_ptr)

ext/opcache/zend_file_cache.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -367,20 +367,10 @@ static void zend_file_cache_serialize_ast(zend_ast *ast,
367367
/* The op_array itself will be serialized as part of the dynamic_func_defs. */
368368
SERIALIZE_PTR(((zend_ast_op_array*)ast)->op_array);
369369

370-
SERIALIZE_PTR(((zend_ast_op_array*)ast)->ast);
371-
tmp = ((zend_ast_op_array*)ast)->ast;
372-
UNSERIALIZE_PTR(tmp);
373-
zend_file_cache_serialize_ast(tmp, script, info, buf);
370+
SERIALIZE_STR(((zend_ast_op_array*)ast)->original_ast);
374371
} else if (zend_ast_is_decl(ast)) {
375-
zend_ast_decl *decl = (zend_ast_decl*)ast;
376-
for (i = 0; i < 5; i++) {
377-
if (decl->child[i] && !IS_SERIALIZED(decl->child[i])) {
378-
SERIALIZE_PTR(decl->child[i]);
379-
tmp = decl->child[i];
380-
UNSERIALIZE_PTR(tmp);
381-
zend_file_cache_serialize_ast(tmp, script, info, buf);
382-
}
383-
}
372+
/* Not implemented. */
373+
ZEND_UNREACHABLE();
384374
} else {
385375
uint32_t children = zend_ast_get_num_children(ast);
386376
for (i = 0; i < children; i++) {
@@ -1264,16 +1254,10 @@ static void zend_file_cache_unserialize_ast(zend_ast *ast,
12641254
/* The op_array itself will be unserialized as part of the dynamic_func_defs. */
12651255
UNSERIALIZE_PTR(((zend_ast_op_array*)ast)->op_array);
12661256

1267-
UNSERIALIZE_PTR(((zend_ast_op_array*)ast)->ast);
1268-
zend_file_cache_unserialize_ast(((zend_ast_op_array*)ast)->ast, script, buf);
1257+
UNSERIALIZE_STR(((zend_ast_op_array*)ast)->original_ast);
12691258
} else if (zend_ast_is_decl(ast)) {
1270-
zend_ast_decl *decl = (zend_ast_decl*)ast;
1271-
for (i = 0; i < 5; i++) {
1272-
if (decl->child[i] && !IS_UNSERIALIZED(decl->child[i])) {
1273-
UNSERIALIZE_PTR(decl->child[i]);
1274-
zend_file_cache_unserialize_ast(decl->child[i], script, buf);
1275-
}
1276-
}
1259+
/* Not implemented. */
1260+
ZEND_UNREACHABLE();
12771261
} else {
12781262
uint32_t children = zend_ast_get_num_children(ast);
12791263
for (i = 0; i < children; i++) {

ext/opcache/zend_persist.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,11 @@ static zend_ast *zend_persist_ast(zend_ast *ast)
194194
ZVAL_PTR(&z, copy->op_array);
195195
zend_persist_op_array(&z);
196196
copy->op_array = Z_PTR(z);
197-
copy->ast = zend_persist_ast(copy->ast);
197+
zend_accel_store_interned_string(copy->original_ast);
198198
node = (zend_ast *) copy;
199199
} else if (zend_ast_is_decl(ast)) {
200-
zend_ast_decl *copy = zend_shared_memdup(ast, sizeof(zend_ast_decl));
201-
for (i = 0; i < 5; i++) {
202-
if (copy->child[i]) {
203-
copy->child[i] = zend_persist_ast(copy->child[i]);
204-
}
205-
}
206-
node = (zend_ast *) copy;
200+
/* Not implemented. */
201+
ZEND_UNREACHABLE();
207202
} else {
208203
uint32_t children = zend_ast_get_num_children(ast);
209204
node = zend_shared_memdup(ast, zend_ast_size(children));

ext/opcache/zend_persist_calc.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,10 @@ static void zend_persist_ast_calc(zend_ast *ast)
9191
zval z;
9292
ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array);
9393
zend_persist_op_array_calc(&z);
94-
zend_persist_ast_calc(zend_ast_get_op_array(ast)->ast);
94+
ADD_INTERNED_STRING(zend_ast_get_op_array(ast)->original_ast);
9595
} else if (zend_ast_is_decl(ast)) {
96-
zend_ast_decl *decl = (zend_ast_decl*)ast;
97-
ADD_SIZE(sizeof(zend_ast_decl));
98-
for (size_t i = 0; i < 5; i++) {
99-
if (decl->child[i]) {
100-
zend_persist_ast_calc(decl->child[i]);
101-
}
102-
}
96+
/* Not implemented. */
97+
ZEND_UNREACHABLE();
10398
} else {
10499
uint32_t children = zend_ast_get_num_children(ast);
105100
ADD_SIZE(zend_ast_size(children));

0 commit comments

Comments
 (0)