1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Bob Weinand <bwoebi@php.net> |
| Dmitry Stogov <dmitry@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "zend_ast.h"
#include "zend_API.h"
#include "zend_operators.h"
static inline void *zend_ast_alloc(size_t size TSRMLS_DC) {
return zend_arena_alloc(&CG(ast_arena), size);
}
static inline void *zend_ast_realloc(void *old, size_t old_size, size_t new_size TSRMLS_DC) {
void *new = zend_ast_alloc(new_size TSRMLS_CC);
memcpy(new, old, old_size);
return new;
}
size_t zend_ast_size(zend_uint children) {
return sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
}
size_t zend_ast_list_size(zend_uint children) {
return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
}
ZEND_API zend_ast *zend_ast_create_znode(znode *node) {
TSRMLS_FETCH();
zend_ast_znode *ast = zend_ast_alloc(sizeof(zend_ast_znode) TSRMLS_CC);
ast->kind = ZEND_AST_ZNODE;
ast->attr = 0;
ast->lineno = CG(zend_lineno);
ast->node = *node;
return (zend_ast *) ast;
}
ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr) {
TSRMLS_FETCH();
zend_ast_zval *ast = zend_ast_alloc(sizeof(zend_ast_zval) TSRMLS_CC);
ast->kind = ZEND_AST_ZVAL;
ast->attr = attr;
ZVAL_COPY_VALUE(&ast->val, zv);
ast->val.u2.lineno = CG(zend_lineno);
return (zend_ast *) ast;
}
ZEND_API zend_ast *zend_ast_create_decl(
zend_ast_kind kind, zend_uint flags, zend_uint start_lineno, zend_string *doc_comment,
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2
) {
TSRMLS_FETCH();
zend_ast_decl *ast = zend_ast_alloc(sizeof(zend_ast_decl) TSRMLS_CC);
ast->kind = kind;
ast->attr = 0;
ast->start_lineno = start_lineno;
ast->end_lineno = CG(zend_lineno);
ast->flags = flags;
ast->lex_pos = LANG_SCNG(yy_text);
ast->doc_comment = doc_comment;
ast->name = name;
ast->child[0] = child0;
ast->child[1] = child1;
ast->child[2] = child2;
return (zend_ast *) ast;
}
static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) {
TSRMLS_FETCH();
zend_uint i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
zend_ast *ast = zend_ast_alloc(zend_ast_size(children) TSRMLS_CC);
ast->kind = kind;
ast->attr = attr;
ast->lineno = UINT_MAX;
for (i = 0; i < children; ++i) {
ast->child[i] = va_arg(va, zend_ast *);
if (ast->child[i] != NULL) {
zend_uint lineno = zend_ast_get_lineno(ast->child[i]);
if (lineno < ast->lineno) {
ast->lineno = lineno;
}
}
}
if (ast->lineno == UINT_MAX) {
ast->lineno = CG(zend_lineno);
}
return ast;
}
ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) {
va_list va;
zend_ast *ast;
va_start(va, attr);
ast = zend_ast_create_from_va_list(kind, attr, va);
va_end(va);
return ast;
}
ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) {
va_list va;
zend_ast *ast;
va_start(va, kind);
ast = zend_ast_create_from_va_list(kind, 0, va);
va_end(va);
return ast;
}
ZEND_API zend_ast_list *zend_ast_create_list(zend_uint init_children, zend_ast_kind kind, ...) {
TSRMLS_FETCH();
zend_ast_list *list = zend_ast_alloc(zend_ast_list_size(4) TSRMLS_CC);
list->kind = kind;
list->attr = 0;
list->lineno = CG(zend_lineno);
list->children = 0;
{
va_list va;
zend_uint i;
va_start(va, kind);
for (i = 0; i < init_children; ++i) {
list = zend_ast_list_add(list, va_arg(va, zend_ast *));
}
va_end(va);
}
return list;
}
static inline zend_bool is_power_of_two(unsigned short n) {
return n == (n & -n);
}
ZEND_API zend_ast_list *zend_ast_list_add(zend_ast_list *list, zend_ast *op) {
if (list->children >= 4 && is_power_of_two(list->children)) {
TSRMLS_FETCH();
list = zend_ast_realloc(list,
zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2) TSRMLS_CC);
}
list->child[list->children++] = op;
return list;
}
static void zend_ast_add_array_element(zval *result, zval *offset, zval *expr TSRMLS_DC)
{
switch (Z_TYPE_P(offset)) {
case IS_UNDEF:
zend_hash_next_index_insert(Z_ARRVAL_P(result), expr);
break;
case IS_STRING:
zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
zval_dtor(offset);
break;
case IS_NULL:
zend_symtable_update(Z_ARRVAL_P(result), STR_EMPTY_ALLOC(), expr);
break;
case IS_LONG:
zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
break;
case IS_FALSE:
zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
break;
case IS_TRUE:
zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
break;
case IS_DOUBLE:
zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr);
break;
default:
zend_error(E_ERROR, "Illegal offset type");
break;
}
}
ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC)
{
zval op1, op2;
switch (ast->kind) {
case ZEND_AST_BINARY_OP:
{
binary_op_type op = get_binary_op(ast->attr);
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
op(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op1);
zval_dtor(&op2);
break;
}
case ZEND_AST_GREATER:
case ZEND_AST_GREATER_EQUAL:
{
/* op1 > op2 is the same as op2 < op1 */
binary_op_type op = ast->kind == ZEND_AST_GREATER
? is_smaller_function : is_smaller_or_equal_function;
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
op(result, &op2, &op1 TSRMLS_CC);
zval_dtor(&op1);
zval_dtor(&op2);
break;
}
case ZEND_AST_UNARY_OP:
{
unary_op_type op = get_unary_op(ast->attr);
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
op(result, &op1 TSRMLS_CC);
zval_dtor(&op1);
break;
}
case ZEND_AST_ZVAL:
{
zval *zv = zend_ast_get_zval(ast);
if (scope) {
/* class constants may be updated in-place */
if (Z_OPT_CONSTANT_P(zv)) {
zval_update_constant_ex(zv, 1, scope TSRMLS_CC);
}
ZVAL_DUP(result, zv);
} else {
ZVAL_DUP(result, zv);
if (Z_OPT_CONSTANT_P(result)) {
zval_update_constant_ex(result, 1, scope TSRMLS_CC);
}
}
break;
}
case ZEND_AST_AND:
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
if (zend_is_true(&op1 TSRMLS_CC)) {
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
ZVAL_BOOL(result, zend_is_true(&op2 TSRMLS_CC));
zval_dtor(&op2);
} else {
ZVAL_BOOL(result, 0);
}
zval_dtor(&op1);
break;
case ZEND_AST_OR:
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
if (zend_is_true(&op1 TSRMLS_CC)) {
ZVAL_BOOL(result, 1);
} else {
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
ZVAL_BOOL(result, zend_is_true(&op2 TSRMLS_CC));
zval_dtor(&op2);
}
zval_dtor(&op1);
break;
case ZEND_AST_CONDITIONAL:
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
if (zend_is_true(&op1 TSRMLS_CC)) {
if (!ast->child[1]) {
*result = op1;
} else {
zend_ast_evaluate(result, ast->child[1], scope TSRMLS_CC);
zval_dtor(&op1);
}
} else {
zend_ast_evaluate(result, ast->child[2], scope TSRMLS_CC);
zval_dtor(&op1);
}
break;
case ZEND_AST_UNARY_PLUS:
ZVAL_LONG(&op1, 0);
zend_ast_evaluate(&op2, ast->child[0], scope TSRMLS_CC);
add_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op2);
break;
case ZEND_AST_UNARY_MINUS:
ZVAL_LONG(&op1, 0);
zend_ast_evaluate(&op2, ast->child[0], scope TSRMLS_CC);
sub_function(result, &op1, &op2 TSRMLS_CC);
zval_dtor(&op2);
break;
case ZEND_AST_ARRAY:
array_init(result);
{
zend_uint i;
zend_ast_list *list = zend_ast_get_list(ast);
for (i = 0; i < list->children; i++) {
zend_ast *elem = list->child[i];
if (elem->child[1]) {
zend_ast_evaluate(&op1, elem->child[1], scope TSRMLS_CC);
} else {
ZVAL_UNDEF(&op1);
}
zend_ast_evaluate(&op2, elem->child[0], scope TSRMLS_CC);
zend_ast_add_array_element(result, &op1, &op2 TSRMLS_CC);
}
}
break;
case ZEND_AST_DIM:
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
{
zval tmp;
zend_fetch_dimension_by_zval(&tmp, &op1, &op2 TSRMLS_CC);
ZVAL_ZVAL(result, &tmp, 1, 1);
}
zval_dtor(&op1);
zval_dtor(&op2);
break;
default:
zend_error(E_ERROR, "Unsupported constant expression");
}
}
ZEND_API zend_ast *zend_ast_copy(zend_ast *ast)
{
if (ast == NULL) {
return NULL;
} else if (ast->kind == ZEND_AST_ZVAL) {
zend_ast_zval *new = emalloc(sizeof(zend_ast_zval));
new->kind = ZEND_AST_ZVAL;
new->attr = ast->attr;
ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
return (zend_ast *) new;
} else if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
zend_ast_list *new = emalloc(zend_ast_list_size(list->children));
zend_uint i;
new->kind = list->kind;
new->attr = list->attr;
new->children = list->children;
for (i = 0; i < list->children; i++) {
new->child[i] = zend_ast_copy(list->child[i]);
}
return (zend_ast *) new;
} else {
zend_uint i, children = zend_ast_get_num_children(ast);
zend_ast *new = emalloc(zend_ast_size(children));
new->kind = ast->kind;
new->attr = ast->attr;
for (i = 0; i < children; i++) {
new->child[i] = zend_ast_copy(ast->child[i]);
}
return new;
}
}
static void zend_ast_destroy_ex(zend_ast *ast, zend_bool free) {
if (!ast) {
return;
}
switch (ast->kind) {
case ZEND_AST_ZVAL:
{
/* Destroy value without using GC: When opcache moves arrays into SHM it will
* free the zend_array structure, so references to it from outside the op array
* become invalid. GC would cause such a reference in the root buffer. */
zval *zv = zend_ast_get_zval(ast);
if (Z_REFCOUNTED_P(zv) && !Z_DELREF_P(zv)) {
_zval_dtor_func_for_ptr(Z_COUNTED_P(zv) ZEND_FILE_LINE_CC);
}
break;
}
case ZEND_AST_FUNC_DECL:
case ZEND_AST_CLOSURE:
case ZEND_AST_METHOD:
case ZEND_AST_CLASS:
{
zend_ast_decl *decl = (zend_ast_decl *) ast;
STR_RELEASE(decl->name);
if (decl->doc_comment) {
STR_RELEASE(decl->doc_comment);
}
zend_ast_destroy_ex(decl->child[0], free);
zend_ast_destroy_ex(decl->child[1], free);
zend_ast_destroy_ex(decl->child[2], free);
break;
}
default:
if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
zend_uint i;
for (i = 0; i < list->children; i++) {
zend_ast_destroy_ex(list->child[i], free);
}
} else {
zend_uint i, children = zend_ast_get_num_children(ast);
for (i = 0; i < children; i++) {
zend_ast_destroy_ex(ast->child[i], free);
}
}
}
if (free) {
efree(ast);
}
}
ZEND_API void zend_ast_destroy(zend_ast *ast) {
zend_ast_destroy_ex(ast, 0);
}
ZEND_API void zend_ast_destroy_and_free(zend_ast *ast) {
zend_ast_destroy_ex(ast, 1);
}
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn TSRMLS_DC) {
if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
zend_uint i;
for (i = 0; i < list->children; ++i) {
fn(&list->child[i] TSRMLS_CC);
}
} else {
zend_uint i, children = zend_ast_get_num_children(ast);
for (i = 0; i < children; ++i) {
fn(&ast->child[i] TSRMLS_CC);
}
}
}
|