-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
zend_compile.c
8660 lines (7388 loc) · 250 KB
/
zend_compile.c
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2018 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <[email protected]> |
| Zeev Suraski <[email protected]> |
| Nikita Popov <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include <zend_language_parser.h>
#include "zend.h"
#include "zend_compile.h"
#include "zend_constants.h"
#include "zend_llist.h"
#include "zend_API.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_virtual_cwd.h"
#include "zend_multibyte.h"
#include "zend_language_scanner.h"
#include "zend_inheritance.h"
#include "zend_vm.h"
#define SET_NODE(target, src) do { \
target ## _type = (src)->op_type; \
if ((src)->op_type == IS_CONST) { \
target.constant = zend_add_literal(CG(active_op_array), &(src)->u.constant); \
} else { \
target = (src)->u.op; \
} \
} while (0)
#define GET_NODE(target, src) do { \
(target)->op_type = src ## _type; \
if ((target)->op_type == IS_CONST) { \
ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
} else { \
(target)->u.op = src; \
} \
} while (0)
#define FC(member) (CG(file_context).member)
typedef struct _zend_loop_var {
zend_uchar opcode;
zend_uchar var_type;
uint32_t var_num;
union {
uint32_t try_catch_offset;
uint32_t live_range_offset;
} u;
} zend_loop_var;
static inline void zend_alloc_cache_slot(uint32_t literal) {
zend_op_array *op_array = CG(active_op_array);
Z_CACHE_SLOT(op_array->literals[literal]) = op_array->cache_size;
op_array->cache_size += sizeof(void*);
}
#define POLYMORPHIC_CACHE_SLOT_SIZE 2
static inline void zend_alloc_polymorphic_cache_slot(uint32_t literal) {
zend_op_array *op_array = CG(active_op_array);
Z_CACHE_SLOT(op_array->literals[literal]) = op_array->cache_size;
op_array->cache_size += POLYMORPHIC_CACHE_SLOT_SIZE * sizeof(void*);
}
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);
#ifndef ZTS
ZEND_API zend_compiler_globals compiler_globals;
ZEND_API zend_executor_globals executor_globals;
#endif
static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2);
static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
{
zend_property_info *property_info = Z_PTR_P(zv);
zend_string_release(property_info->name);
free(property_info);
}
/* }}} */
static void zend_destroy_class_constant_internal(zval *zv) /* {{{ */
{
free(Z_PTR_P(zv));
}
/* }}} */
static zend_string *zend_new_interned_string_safe(zend_string *str) /* {{{ */ {
zend_string *interned_str;
zend_string_addref(str);
interned_str = zend_new_interned_string(str);
if (str != interned_str) {
return interned_str;
} else {
zend_string_release(str);
return str;
}
}
/* }}} */
static zend_string *zend_build_runtime_definition_key(zend_string *name, unsigned char *lex_pos) /* {{{ */
{
zend_string *result;
char char_pos_buf[32];
size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
zend_string *filename = CG(active_op_array)->filename;
/* NULL, name length, filename length, last accepting char position length */
result = zend_string_alloc(1 + ZSTR_LEN(name) + ZSTR_LEN(filename) + char_pos_len, 0);
sprintf(ZSTR_VAL(result), "%c%s%s%s", '\0', ZSTR_VAL(name), ZSTR_VAL(filename), char_pos_buf);
return zend_new_interned_string(result);
}
/* }}} */
static zend_bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
{
const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (ns_separator != NULL) {
*result = ns_separator + 1;
*result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
return 1;
}
return 0;
}
/* }}} */
struct reserved_class_name {
const char *name;
size_t len;
};
static const struct reserved_class_name reserved_class_names[] = {
{ZEND_STRL("bool")},
{ZEND_STRL("false")},
{ZEND_STRL("float")},
{ZEND_STRL("int")},
{ZEND_STRL("null")},
{ZEND_STRL("parent")},
{ZEND_STRL("self")},
{ZEND_STRL("static")},
{ZEND_STRL("string")},
{ZEND_STRL("true")},
{ZEND_STRL("void")},
{ZEND_STRL("iterable")},
{ZEND_STRL("object")},
{NULL, 0}
};
static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
{
const struct reserved_class_name *reserved = reserved_class_names;
const char *uqname = ZSTR_VAL(name);
size_t uqname_len = ZSTR_LEN(name);
zend_get_unqualified_name(name, &uqname, &uqname_len);
for (; reserved->name; ++reserved) {
if (uqname_len == reserved->len
&& zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
) {
return 1;
}
}
return 0;
}
/* }}} */
void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
{
if (zend_is_reserved_class_name(name)) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
}
}
/* }}} */
typedef struct _builtin_type_info {
const char* name;
const size_t name_len;
const zend_uchar type;
} builtin_type_info;
static const builtin_type_info builtin_types[] = {
{ZEND_STRL("int"), IS_LONG},
{ZEND_STRL("float"), IS_DOUBLE},
{ZEND_STRL("string"), IS_STRING},
{ZEND_STRL("bool"), _IS_BOOL},
{ZEND_STRL("void"), IS_VOID},
{ZEND_STRL("iterable"), IS_ITERABLE},
{ZEND_STRL("object"), IS_OBJECT},
{NULL, 0, IS_UNDEF}
};
static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
{
const builtin_type_info *info = &builtin_types[0];
for (; info->name; ++info) {
if (ZSTR_LEN(name) == info->name_len
&& zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
) {
return info->type;
}
}
return 0;
}
/* }}} */
void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
{
*prev_context = CG(context);
CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
CG(context).vars_size = 0;
CG(context).literals_size = 0;
CG(context).backpatch_count = 0;
CG(context).fast_call_var = -1;
CG(context).try_catch_offset = -1;
CG(context).current_brk_cont = -1;
CG(context).last_brk_cont = 0;
CG(context).brk_cont_array = NULL;
CG(context).labels = NULL;
}
/* }}} */
void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
{
if (CG(context).brk_cont_array) {
efree(CG(context).brk_cont_array);
CG(context).brk_cont_array = NULL;
}
if (CG(context).labels) {
zend_hash_destroy(CG(context).labels);
FREE_HASHTABLE(CG(context).labels);
CG(context).labels = NULL;
}
CG(context) = *prev_context;
}
/* }}} */
static void zend_reset_import_tables(void) /* {{{ */
{
if (FC(imports)) {
zend_hash_destroy(FC(imports));
efree(FC(imports));
FC(imports) = NULL;
}
if (FC(imports_function)) {
zend_hash_destroy(FC(imports_function));
efree(FC(imports_function));
FC(imports_function) = NULL;
}
if (FC(imports_const)) {
zend_hash_destroy(FC(imports_const));
efree(FC(imports_const));
FC(imports_const) = NULL;
}
}
/* }}} */
static void zend_end_namespace(void) /* {{{ */ {
FC(in_namespace) = 0;
zend_reset_import_tables();
if (FC(current_namespace)) {
zend_string_release(FC(current_namespace));
FC(current_namespace) = NULL;
}
}
/* }}} */
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
{
*prev_context = CG(file_context);
FC(imports) = NULL;
FC(imports_function) = NULL;
FC(imports_const) = NULL;
FC(current_namespace) = NULL;
FC(in_namespace) = 0;
FC(has_bracketed_namespaces) = 0;
FC(declarables).ticks = 0;
zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
}
/* }}} */
void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
{
zend_end_namespace();
zend_hash_destroy(&FC(seen_symbols));
CG(file_context) = *prev_context;
}
/* }}} */
void zend_init_compiler_data_structures(void) /* {{{ */
{
zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
CG(active_class_entry) = NULL;
CG(in_compilation) = 0;
CG(start_lineno) = 0;
CG(encoding_declared) = 0;
}
/* }}} */
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
zval *zv = zend_hash_find(&FC(seen_symbols), name);
if (zv) {
Z_LVAL_P(zv) |= kind;
} else {
zval tmp;
ZVAL_LONG(&tmp, kind);
zend_hash_add_new(&FC(seen_symbols), name, &tmp);
}
}
static zend_bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
zval *zv = zend_hash_find(&FC(seen_symbols), name);
return zv && (Z_LVAL_P(zv) & kind) != 0;
}
ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
{
zend_file_handle_dtor(fh);
}
/* }}} */
void init_compiler(void) /* {{{ */
{
CG(arena) = zend_arena_create(64 * 1024);
CG(active_op_array) = NULL;
memset(&CG(context), 0, sizeof(CG(context)));
zend_init_compiler_data_structures();
zend_init_rsrc_list();
zend_hash_init(&CG(filenames_table), 8, NULL, ZVAL_PTR_DTOR, 0);
zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
CG(unclean_shutdown) = 0;
}
/* }}} */
void shutdown_compiler(void) /* {{{ */
{
zend_stack_destroy(&CG(loop_var_stack));
zend_stack_destroy(&CG(delayed_oplines_stack));
zend_hash_destroy(&CG(filenames_table));
zend_arena_destroy(CG(arena));
}
/* }}} */
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
{
zval *p, rv;
if ((p = zend_hash_find(&CG(filenames_table), new_compiled_filename))) {
ZEND_ASSERT(Z_TYPE_P(p) == IS_STRING);
CG(compiled_filename) = Z_STR_P(p);
return Z_STR_P(p);
}
ZVAL_STR_COPY(&rv, new_compiled_filename);
zend_hash_update(&CG(filenames_table), new_compiled_filename, &rv);
CG(compiled_filename) = new_compiled_filename;
return new_compiled_filename;
}
/* }}} */
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
{
CG(compiled_filename) = original_compiled_filename;
}
/* }}} */
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
{
return CG(compiled_filename);
}
/* }}} */
ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
{
return CG(zend_lineno);
}
/* }}} */
ZEND_API zend_bool zend_is_compiling(void) /* {{{ */
{
return CG(in_compilation);
}
/* }}} */
static uint32_t get_temporary_variable(zend_op_array *op_array) /* {{{ */
{
return (uint32_t)op_array->T++;
}
/* }}} */
static int lookup_cv(zend_op_array *op_array, zend_string* name) /* {{{ */{
int i = 0;
zend_ulong hash_value = zend_string_hash_val(name);
while (i < op_array->last_var) {
if (ZSTR_VAL(op_array->vars[i]) == ZSTR_VAL(name) ||
(ZSTR_H(op_array->vars[i]) == hash_value &&
ZSTR_LEN(op_array->vars[i]) == ZSTR_LEN(name) &&
memcmp(ZSTR_VAL(op_array->vars[i]), ZSTR_VAL(name), ZSTR_LEN(name)) == 0)) {
zend_string_release(name);
return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
}
i++;
}
i = op_array->last_var;
op_array->last_var++;
if (op_array->last_var > CG(context).vars_size) {
CG(context).vars_size += 16; /* FIXME */
op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
}
op_array->vars[i] = zend_new_interned_string(name);
return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
}
/* }}} */
void zend_del_literal(zend_op_array *op_array, int n) /* {{{ */
{
zval_dtor(CT_CONSTANT_EX(op_array, n));
if (n + 1 == op_array->last_literal) {
op_array->last_literal--;
} else {
ZVAL_UNDEF(CT_CONSTANT_EX(op_array, n));
}
}
/* }}} */
/* Common part of zend_add_literal and zend_append_individual_literal */
static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
{
if (Z_TYPE_P(zv) == IS_STRING || Z_TYPE_P(zv) == IS_CONSTANT) {
zend_string_hash_val(Z_STR_P(zv));
Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
Z_TYPE_FLAGS_P(zv) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
}
}
ZVAL_COPY_VALUE(CT_CONSTANT_EX(op_array, literal_position), zv);
Z_CACHE_SLOT(op_array->literals[literal_position]) = -1;
}
/* }}} */
/* Is used while compiling a function, using the context to keep track
of an approximate size to avoid to relocate to often.
Literals are truncated to actual size in the second compiler pass (pass_two()). */
int zend_add_literal(zend_op_array *op_array, zval *zv) /* {{{ */
{
int i = op_array->last_literal;
op_array->last_literal++;
if (i >= CG(context).literals_size) {
while (i >= CG(context).literals_size) {
CG(context).literals_size += 16; /* FIXME */
}
op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
}
zend_insert_literal(op_array, zv, i);
return i;
}
/* }}} */
static inline int zend_add_literal_string(zend_op_array *op_array, zend_string **str) /* {{{ */
{
int ret;
zval zv;
ZVAL_STR(&zv, *str);
ret = zend_add_literal(op_array, &zv);
*str = Z_STR(zv);
return ret;
}
/* }}} */
static int zend_add_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
{
/* Original name */
int ret = zend_add_literal_string(op_array, &name);
/* Lowercased name */
zend_string *lc_name = zend_string_tolower(name);
zend_add_literal_string(op_array, &lc_name);
return ret;
}
/* }}} */
static int zend_add_ns_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
{
const char *unqualified_name;
size_t unqualified_name_len;
/* Original name */
int ret = zend_add_literal_string(op_array, &name);
/* Lowercased name */
zend_string *lc_name = zend_string_tolower(name);
zend_add_literal_string(op_array, &lc_name);
/* Lowercased unqualfied name */
if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
lc_name = zend_string_alloc(unqualified_name_len, 0);
zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
zend_add_literal_string(op_array, &lc_name);
}
return ret;
}
/* }}} */
static int zend_add_class_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
{
/* Original name */
int ret = zend_add_literal_string(op_array, &name);
/* Lowercased name */
zend_string *lc_name = zend_string_tolower(name);
zend_add_literal_string(op_array, &lc_name);
zend_alloc_cache_slot(ret);
return ret;
}
/* }}} */
static int zend_add_const_name_literal(zend_op_array *op_array, zend_string *name, zend_bool unqualified) /* {{{ */
{
zend_string *tmp_name;
int ret = zend_add_literal_string(op_array, &name);
size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (after_ns) {
after_ns += 1;
ns_len = after_ns - ZSTR_VAL(name) - 1;
after_ns_len = ZSTR_LEN(name) - ns_len - 1;
/* lowercased namespace name & original constant name */
tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
zend_add_literal_string(op_array, &tmp_name);
/* lowercased namespace name & lowercased constant name */
tmp_name = zend_string_tolower(name);
zend_add_literal_string(op_array, &tmp_name);
if (!unqualified) {
return ret;
}
} else {
after_ns = ZSTR_VAL(name);
}
/* original unqualified constant name */
tmp_name = zend_string_init(after_ns, after_ns_len, 0);
zend_add_literal_string(op_array, &tmp_name);
/* lowercased unqualified constant name */
tmp_name = zend_string_alloc(after_ns_len, 0);
zend_str_tolower_copy(ZSTR_VAL(tmp_name), after_ns, after_ns_len);
zend_add_literal_string(op_array, &tmp_name);
return ret;
}
/* }}} */
#define LITERAL_STR(op, str) do { \
zval _c; \
ZVAL_STR(&_c, str); \
op.constant = zend_add_literal(CG(active_op_array), &_c); \
} while (0)
void zend_stop_lexing(void)
{
if (LANG_SCNG(on_event)) {
LANG_SCNG(on_event)(ON_STOP, END, 0, LANG_SCNG(on_event_context));
}
LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
}
static uint32_t zend_start_live_range(zend_op_array *op_array, uint32_t start) /* {{{ */
{
zend_live_range *range;
op_array->last_live_range++;
op_array->live_range = erealloc(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
range = op_array->live_range + op_array->last_live_range - 1;
range->start = start;
return op_array->last_live_range - 1;
}
/* }}} */
static uint32_t zend_start_live_range_ex(zend_op_array *op_array, uint32_t start) /* {{{ */
{
if (op_array->last_live_range == 0 ||
op_array->live_range[op_array->last_live_range - 1].start <= start) {
return zend_start_live_range(op_array, start);
} else {
/* Live ranges have to be sorted by "start" field */
uint32_t n = op_array->last_live_range;
/* move early ranges to make a room */
op_array->last_live_range = n + 1;
op_array->live_range = erealloc(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
do {
op_array->live_range[n] = op_array->live_range[n-1];
n--;
} while (n != 0 && op_array->live_range[n-1].start > start);
/* initialize new range */
op_array->live_range[n].start = start;
/* update referens to live-ranges from stack */
if (!zend_stack_is_empty(&CG(loop_var_stack))) {
zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
zend_loop_var *base = zend_stack_base(&CG(loop_var_stack));
int check_opcodes = 0;
for (; loop_var >= base; loop_var--) {
if (loop_var->opcode == ZEND_RETURN) {
/* Stack separator */
break;
} else if (loop_var->opcode == ZEND_FREE ||
loop_var->opcode == ZEND_FE_FREE) {
if (loop_var->u.live_range_offset >= n) {
loop_var->u.live_range_offset++;
check_opcodes = 1;
} else {
break;
}
}
}
/* update previously generated FREE/FE_FREE opcodes */
if (check_opcodes) {
zend_op *opline = op_array->opcodes + op_array->live_range[n+1].start;
zend_op *end = op_array->opcodes + op_array->last;
while (opline < end) {
if ((opline->opcode == ZEND_FREE ||
opline->opcode == ZEND_FE_FREE) &&
(opline->extended_value & ZEND_FREE_ON_RETURN) &&
opline->op2.num >= n) {
opline->op2.num++;
}
opline++;
}
}
}
return n;
}
}
/* }}} */
static void zend_end_live_range(zend_op_array *op_array, uint32_t offset, uint32_t end, uint32_t kind, uint32_t var) /* {{{ */
{
zend_live_range *range = op_array->live_range + offset;
if (range->start == end && offset == (uint32_t)op_array->last_live_range - 1) {
op_array->last_live_range--;
} else {
range->end = end;
range->var = (var * sizeof(zval)) | kind;
}
}
/* }}} */
static inline void zend_begin_loop(zend_uchar free_opcode, const znode *loop_var) /* {{{ */
{
zend_brk_cont_element *brk_cont_element;
int parent = CG(context).current_brk_cont;
zend_loop_var info = {0};
CG(context).current_brk_cont = CG(context).last_brk_cont;
brk_cont_element = get_next_brk_cont_element();
brk_cont_element->parent = parent;
if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
uint32_t start = get_next_op_number(CG(active_op_array));
info.opcode = free_opcode;
info.var_type = loop_var->op_type;
info.var_num = loop_var->u.op.var;
info.u.live_range_offset = zend_start_live_range(CG(active_op_array), start);
brk_cont_element->start = start;
} else {
info.opcode = ZEND_NOP;
/* The start field is used to free temporary variables in case of exceptions.
* We won't try to free something of we don't have loop variable. */
brk_cont_element->start = -1;
}
zend_stack_push(&CG(loop_var_stack), &info);
}
/* }}} */
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
{
uint32_t end = get_next_op_number(CG(active_op_array));
zend_brk_cont_element *brk_cont_element
= &CG(context).brk_cont_array[CG(context).current_brk_cont];
brk_cont_element->cont = cont_addr;
brk_cont_element->brk = end;
CG(context).current_brk_cont = brk_cont_element->parent;
if (brk_cont_element->start != -1) {
zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
zend_end_live_range(CG(active_op_array), loop_var->u.live_range_offset, end,
loop_var->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR,
var_node->u.op.var);
}
zend_stack_del_top(&CG(loop_var_stack));
}
/* }}} */
void zend_do_free(znode *op1) /* {{{ */
{
if (op1->op_type == IS_TMP_VAR) {
zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
while (opline->opcode == ZEND_END_SILENCE) {
opline--;
}
if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
if (opline->opcode == ZEND_BOOL || opline->opcode == ZEND_BOOL_NOT) {
return;
}
}
zend_emit_op(NULL, ZEND_FREE, op1, NULL);
} else if (op1->op_type == IS_VAR) {
zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
while (opline->opcode == ZEND_END_SILENCE ||
opline->opcode == ZEND_EXT_FCALL_END ||
opline->opcode == ZEND_OP_DATA) {
opline--;
}
if (opline->result_type == IS_VAR
&& opline->result.var == op1->u.op.var) {
if (opline->opcode == ZEND_FETCH_R ||
opline->opcode == ZEND_FETCH_DIM_R ||
opline->opcode == ZEND_FETCH_OBJ_R ||
opline->opcode == ZEND_FETCH_STATIC_PROP_R) {
/* It's very rare and useless case. It's better to use
additional FREE opcode and simplify the FETCH handlers
their selves */
zend_emit_op(NULL, ZEND_FREE, op1, NULL);
} else if (opline->opcode == ZEND_FETCH_THIS) {
opline->opcode = ZEND_NOP;
opline->result_type = IS_UNUSED;
} else {
opline->result_type = IS_UNUSED;
}
} else {
while (opline >= CG(active_op_array)->opcodes) {
if (opline->opcode == ZEND_FETCH_LIST &&
opline->op1_type == IS_VAR &&
opline->op1.var == op1->u.op.var) {
zend_emit_op(NULL, ZEND_FREE, op1, NULL);
return;
}
if (opline->result_type == IS_VAR
&& opline->result.var == op1->u.op.var) {
if (opline->opcode == ZEND_NEW) {
zend_emit_op(NULL, ZEND_FREE, op1, NULL);
}
break;
}
opline--;
}
}
} else if (op1->op_type == IS_CONST) {
/* 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_ptr_dtor_nogc(&op1->u.constant);
}
}
/* }}} */
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
{
uint32_t new_flags = flags | new_flag;
if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
zend_error_noreturn(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
}
if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
}
if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class");
}
return new_flags;
}
/* }}} */
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
{
uint32_t new_flags = flags | new_flag;
if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
zend_error_noreturn(E_COMPILE_ERROR, "Multiple access type modifiers are not allowed");
}
if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
zend_error_noreturn(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
}
if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
zend_error_noreturn(E_COMPILE_ERROR, "Multiple static modifiers are not allowed");
}
if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
}
if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class member");
}
return new_flags;
}
/* }}} */
zend_string *zend_concat3(char *str1, size_t str1_len, char *str2, size_t str2_len, char *str3, size_t str3_len) /* {{{ */
{
size_t len = str1_len + str2_len + str3_len;
zend_string *res = zend_string_alloc(len, 0);
memcpy(ZSTR_VAL(res), str1, str1_len);
memcpy(ZSTR_VAL(res) + str1_len, str2, str2_len);
memcpy(ZSTR_VAL(res) + str1_len + str2_len, str3, str3_len);
ZSTR_VAL(res)[len] = '\0';
return res;
}
zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
return zend_concat3(name1, name1_len, "\\", 1, name2, name2_len);
}
zend_string *zend_prefix_with_ns(zend_string *name) {
if (FC(current_namespace)) {
zend_string *ns = FC(current_namespace);
return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
} else {
return zend_string_copy(name);
}
}
void *zend_hash_find_ptr_lc(HashTable *ht, const char *str, size_t len) {
void *result;
zend_string *lcname;
ALLOCA_FLAG(use_heap);
ZSTR_ALLOCA_ALLOC(lcname, len, use_heap);
zend_str_tolower_copy(ZSTR_VAL(lcname), str, len);
result = zend_hash_find_ptr(ht, lcname);
ZSTR_ALLOCA_FREE(lcname, use_heap);
return result;
}
zend_string *zend_resolve_non_class_name(
zend_string *name, uint32_t type, zend_bool *is_fully_qualified,
zend_bool case_sensitive, HashTable *current_import_sub
) {
char *compound;
*is_fully_qualified = 0;
if (ZSTR_VAL(name)[0] == '\\') {
/* Remove \ prefix (only relevant if this is a string rather than a label) */
*is_fully_qualified = 1;
return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
}
if (type == ZEND_NAME_FQ) {
*is_fully_qualified = 1;
return zend_string_copy(name);
}
if (type == ZEND_NAME_RELATIVE) {
*is_fully_qualified = 1;
return zend_prefix_with_ns(name);
}
if (current_import_sub) {
/* If an unqualified name is a function/const alias, replace it. */
zend_string *import_name;
if (case_sensitive) {
import_name = zend_hash_find_ptr(current_import_sub, name);
} else {
import_name = zend_hash_find_ptr_lc(current_import_sub, ZSTR_VAL(name), ZSTR_LEN(name));
}
if (import_name) {
*is_fully_qualified = 1;
return zend_string_copy(import_name);
}
}
compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (compound) {
*is_fully_qualified = 1;
}
if (compound && FC(imports)) {
/* If the first part of a qualified name is an alias, substitute it. */
size_t len = compound - ZSTR_VAL(name);
zend_string *import_name = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
if (import_name) {
return zend_concat_names(
ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
}
}
return zend_prefix_with_ns(name);
}
/* }}} */
zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */
{
return zend_resolve_non_class_name(
name, type, is_fully_qualified, 0, FC(imports_function));
}
/* }}} */
zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */ {
return zend_resolve_non_class_name(
name, type, is_fully_qualified, 1, FC(imports_const));
}
/* }}} */
zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
{
char *compound;
if (type == ZEND_NAME_RELATIVE) {
return zend_prefix_with_ns(name);
}
if (type == ZEND_NAME_FQ || ZSTR_VAL(name)[0] == '\\') {
/* Remove \ prefix (only relevant if this is a string rather than a label) */
if (ZSTR_VAL(name)[0] == '\\') {
name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
} else {
zend_string_addref(name);
}
/* Ensure that \self, \parent and \static are not used */
if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
zend_error_noreturn(E_COMPILE_ERROR, "'\\%s' is an invalid class name", ZSTR_VAL(name));
}
return name;
}
if (FC(imports)) {
compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (compound) {
/* If the first part of a qualified name is an alias, substitute it. */
size_t len = compound - ZSTR_VAL(name);
zend_string *import_name =
zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
if (import_name) {
return zend_concat_names(
ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
}
} else {
/* If an unqualified name is an alias, replace it. */
zend_string *import_name
= zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), ZSTR_LEN(name));