forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zend_compile.c
7181 lines (6248 loc) · 246 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-2012 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]> |
+----------------------------------------------------------------------+
*/
/* $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 "tsrm_virtual_cwd.h"
#include "zend_multibyte.h"
#include "zend_language_scanner.h"
#define CONSTANT_EX(op_array, op) \
(op_array)->literals[op].constant
#define CONSTANT(op) \
CONSTANT_EX(CG(active_op_array), op)
#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 TSRMLS_CC); \
} else { \
target = (src)->u.op; \
} \
} while (0)
#define GET_NODE(target, src) do { \
(target)->op_type = src ## _type; \
if ((target)->op_type == IS_CONST) { \
(target)->u.constant = CONSTANT(src.constant); \
} else { \
(target)->u.op = src; \
(target)->EA = 0; \
} \
} while (0)
#define COPY_NODE(target, src) do { \
target ## _type = src ## _type; \
target = src; \
} while (0)
#define CALCULATE_LITERAL_HASH(num) do { \
if (IS_INTERNED(Z_STRVAL(CONSTANT(num)))) { \
Z_HASH_P(&CONSTANT(num)) = INTERNED_HASH(Z_STRVAL(CONSTANT(num))); \
} else { \
Z_HASH_P(&CONSTANT(num)) = zend_hash_func(Z_STRVAL(CONSTANT(num)), Z_STRLEN(CONSTANT(num))+1); \
} \
} while (0)
#define GET_CACHE_SLOT(literal) do { \
CG(active_op_array)->literals[literal].cache_slot = CG(active_op_array)->last_cache_slot++; \
if ((CG(active_op_array)->fn_flags & ZEND_ACC_INTERACTIVE) && CG(active_op_array)->run_time_cache) { \
CG(active_op_array)->run_time_cache = erealloc(CG(active_op_array)->run_time_cache, CG(active_op_array)->last_cache_slot * sizeof(void*)); \
CG(active_op_array)->run_time_cache[CG(active_op_array)->last_cache_slot - 1] = NULL; \
} \
} while (0)
#define POLYMORPHIC_CACHE_SLOT_SIZE 2
#define GET_POLYMORPHIC_CACHE_SLOT(literal) do { \
CG(active_op_array)->literals[literal].cache_slot = CG(active_op_array)->last_cache_slot; \
CG(active_op_array)->last_cache_slot += POLYMORPHIC_CACHE_SLOT_SIZE; \
if ((CG(active_op_array)->fn_flags & ZEND_ACC_INTERACTIVE) && CG(active_op_array)->run_time_cache) { \
CG(active_op_array)->run_time_cache = erealloc(CG(active_op_array)->run_time_cache, CG(active_op_array)->last_cache_slot * sizeof(void*)); \
CG(active_op_array)->run_time_cache[CG(active_op_array)->last_cache_slot - 1] = NULL; \
CG(active_op_array)->run_time_cache[CG(active_op_array)->last_cache_slot - 2] = NULL; \
} \
} while (0)
#define FREE_POLYMORPHIC_CACHE_SLOT(literal) do { \
if (CG(active_op_array)->literals[literal].cache_slot != -1 && \
CG(active_op_array)->literals[literal].cache_slot == \
CG(active_op_array)->last_cache_slot - POLYMORPHIC_CACHE_SLOT_SIZE) { \
CG(active_op_array)->literals[literal].cache_slot = -1; \
CG(active_op_array)->last_cache_slot -= POLYMORPHIC_CACHE_SLOT_SIZE; \
} \
} while (0)
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename TSRMLS_DC);
#ifndef ZTS
ZEND_API zend_compiler_globals compiler_globals;
ZEND_API zend_executor_globals executor_globals;
#endif
static void zend_duplicate_property_info(zend_property_info *property_info) /* {{{ */
{
if (!IS_INTERNED(property_info->name)) {
property_info->name = estrndup(property_info->name, property_info->name_length);
}
if (property_info->doc_comment) {
property_info->doc_comment = estrndup(property_info->doc_comment, property_info->doc_comment_len);
}
}
/* }}} */
static void zend_duplicate_property_info_internal(zend_property_info *property_info) /* {{{ */
{
if (!IS_INTERNED(property_info->name)) {
property_info->name = zend_strndup(property_info->name, property_info->name_length);
}
}
/* }}} */
static void zend_destroy_property_info(zend_property_info *property_info) /* {{{ */
{
str_efree(property_info->name);
if (property_info->doc_comment) {
efree((char*)property_info->doc_comment);
}
}
/* }}} */
static void zend_destroy_property_info_internal(zend_property_info *property_info) /* {{{ */
{
str_free((char*)property_info->name);
}
/* }}} */
static void build_runtime_defined_function_key(zval *result, const char *name, int name_length TSRMLS_DC) /* {{{ */
{
char char_pos_buf[32];
uint char_pos_len;
const char *filename;
char_pos_len = zend_sprintf(char_pos_buf, "%p", LANG_SCNG(yy_text));
if (CG(active_op_array)->filename) {
filename = CG(active_op_array)->filename;
} else {
filename = "-";
}
/* NULL, name length, filename length, last accepting char position length */
result->value.str.len = 1+name_length+strlen(filename)+char_pos_len;
/* must be binary safe */
result->value.str.val = (char *) safe_emalloc(result->value.str.len, 1, 1);
result->value.str.val[0] = '\0';
sprintf(result->value.str.val+1, "%s%s%s", name, filename, char_pos_buf);
result->type = IS_STRING;
Z_SET_REFCOUNT_P(result, 1);
}
/* }}} */
static void init_compiler_declarables(TSRMLS_D) /* {{{ */
{
Z_TYPE(CG(declarables).ticks) = IS_LONG;
Z_LVAL(CG(declarables).ticks) = 0;
}
/* }}} */
void zend_init_compiler_context(TSRMLS_D) /* {{{ */
{
CG(context).opcodes_size = (CG(active_op_array)->fn_flags & ZEND_ACC_INTERACTIVE) ? INITIAL_INTERACTIVE_OP_ARRAY_SIZE : INITIAL_OP_ARRAY_SIZE;
CG(context).vars_size = 0;
CG(context).literals_size = 0;
CG(context).current_brk_cont = -1;
CG(context).backpatch_count = 0;
CG(context).labels = NULL;
}
/* }}} */
void zend_init_compiler_data_structures(TSRMLS_D) /* {{{ */
{
zend_stack_init(&CG(bp_stack));
zend_stack_init(&CG(function_call_stack));
zend_stack_init(&CG(switch_cond_stack));
zend_stack_init(&CG(foreach_copy_stack));
zend_stack_init(&CG(object_stack));
zend_stack_init(&CG(declare_stack));
CG(active_class_entry) = NULL;
zend_llist_init(&CG(list_llist), sizeof(list_llist_element), NULL, 0);
zend_llist_init(&CG(dimension_llist), sizeof(int), NULL, 0);
zend_stack_init(&CG(list_stack));
CG(in_compilation) = 0;
CG(start_lineno) = 0;
CG(current_namespace) = NULL;
CG(in_namespace) = 0;
CG(has_bracketed_namespaces) = 0;
CG(current_import) = NULL;
init_compiler_declarables(TSRMLS_C);
zend_stack_init(&CG(context_stack));
CG(encoding_declared) = 0;
}
/* }}} */
ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
{
TSRMLS_FETCH();
zend_file_handle_dtor(fh TSRMLS_CC);
}
/* }}} */
void init_compiler(TSRMLS_D) /* {{{ */
{
CG(active_op_array) = NULL;
zend_init_compiler_data_structures(TSRMLS_C);
zend_init_rsrc_list(TSRMLS_C);
zend_hash_init(&CG(filenames_table), 5, NULL, (dtor_func_t) free_estring, 0);
zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
CG(unclean_shutdown) = 0;
}
/* }}} */
void shutdown_compiler(TSRMLS_D) /* {{{ */
{
zend_stack_destroy(&CG(bp_stack));
zend_stack_destroy(&CG(function_call_stack));
zend_stack_destroy(&CG(switch_cond_stack));
zend_stack_destroy(&CG(foreach_copy_stack));
zend_stack_destroy(&CG(object_stack));
zend_stack_destroy(&CG(declare_stack));
zend_stack_destroy(&CG(list_stack));
zend_hash_destroy(&CG(filenames_table));
zend_llist_destroy(&CG(open_files));
zend_stack_destroy(&CG(context_stack));
}
/* }}} */
ZEND_API char *zend_set_compiled_filename(const char *new_compiled_filename TSRMLS_DC) /* {{{ */
{
char **pp, *p;
int length = strlen(new_compiled_filename);
if (zend_hash_find(&CG(filenames_table), new_compiled_filename, length+1, (void **) &pp) == SUCCESS) {
CG(compiled_filename) = *pp;
return *pp;
}
p = estrndup(new_compiled_filename, length);
zend_hash_update(&CG(filenames_table), new_compiled_filename, length+1, &p, sizeof(char *), (void **) &pp);
CG(compiled_filename) = p;
return p;
}
/* }}} */
ZEND_API void zend_restore_compiled_filename(char *original_compiled_filename TSRMLS_DC) /* {{{ */
{
CG(compiled_filename) = original_compiled_filename;
}
/* }}} */
ZEND_API char *zend_get_compiled_filename(TSRMLS_D) /* {{{ */
{
return CG(compiled_filename);
}
/* }}} */
ZEND_API int zend_get_compiled_lineno(TSRMLS_D) /* {{{ */
{
return CG(zend_lineno);
}
/* }}} */
ZEND_API zend_bool zend_is_compiling(TSRMLS_D) /* {{{ */
{
return CG(in_compilation);
}
/* }}} */
static zend_uint get_temporary_variable(zend_op_array *op_array) /* {{{ */
{
return (op_array->T)++ * ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable));
}
/* }}} */
static int lookup_cv(zend_op_array *op_array, char* name, int name_len, ulong hash TSRMLS_DC) /* {{{ */
{
int i = 0;
ulong hash_value = hash ? hash : zend_inline_hash_func(name, name_len+1);
while (i < op_array->last_var) {
if (op_array->vars[i].name == name ||
(op_array->vars[i].hash_value == hash_value &&
op_array->vars[i].name_len == name_len &&
memcmp(op_array->vars[i].name, name, name_len) == 0)) {
str_efree(name);
return 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_compiled_variable));
}
op_array->vars[i].name = zend_new_interned_string(name, name_len + 1, 1 TSRMLS_CC);
op_array->vars[i].name_len = name_len;
op_array->vars[i].hash_value = hash_value;
return i;
}
/* }}} */
void zend_del_literal(zend_op_array *op_array, int n) /* {{{ */
{
zval_dtor(&CONSTANT_EX(op_array, n));
if (n + 1 == op_array->last_literal) {
op_array->last_literal--;
} else {
Z_TYPE(CONSTANT_EX(op_array, n)) = IS_NULL;
}
}
/* }}} */
/* Common part of zend_add_literal and zend_append_individual_literal */
static inline void zend_insert_literal(zend_op_array *op_array, const zval *zv, int literal_position TSRMLS_DC) /* {{{ */
{
if (Z_TYPE_P(zv) == IS_STRING || Z_TYPE_P(zv) == IS_CONSTANT) {
zval *z = (zval*)zv;
Z_STRVAL_P(z) = (char*)zend_new_interned_string(Z_STRVAL_P(zv), Z_STRLEN_P(zv) + 1, 1 TSRMLS_CC);
}
CONSTANT_EX(op_array, literal_position) = *zv;
Z_SET_REFCOUNT(CONSTANT_EX(op_array, literal_position), 2);
Z_SET_ISREF(CONSTANT_EX(op_array, literal_position));
op_array->literals[literal_position].hash_value = 0;
op_array->literals[literal_position].cache_slot = -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, const zval *zv TSRMLS_DC) /* {{{ */
{
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 = (zend_literal*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zend_literal));
}
zend_insert_literal(op_array, zv, i TSRMLS_CC);
return i;
}
/* }}} */
/* Is used after normal compilation to append an additional literal.
Allocation is done precisely here. */
int zend_append_individual_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) /* {{{ */
{
int i = op_array->last_literal;
op_array->last_literal++;
op_array->literals = (zend_literal*)erealloc(op_array->literals, (i + 1) * sizeof(zend_literal));
zend_insert_literal(op_array, zv, i TSRMLS_CC);
return i;
}
/* }}} */
int zend_add_func_name_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) /* {{{ */
{
int ret;
char *lc_name;
zval c;
int lc_literal;
if (op_array->last_literal > 0 &&
&op_array->literals[op_array->last_literal - 1].constant == zv &&
op_array->literals[op_array->last_literal - 1].cache_slot == -1) {
/* we already have function name as last literal (do nothing) */
ret = op_array->last_literal - 1;
} else {
ret = zend_add_literal(op_array, zv TSRMLS_CC);
}
lc_name = zend_str_tolower_dup(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
ZVAL_STRINGL(&c, lc_name, Z_STRLEN_P(zv), 0);
lc_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(lc_literal);
return ret;
}
/* }}} */
int zend_add_ns_func_name_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) /* {{{ */
{
int ret;
char *lc_name;
const char *ns_separator;
int lc_len;
zval c;
int lc_literal;
if (op_array->last_literal > 0 &&
&op_array->literals[op_array->last_literal - 1].constant == zv &&
op_array->literals[op_array->last_literal - 1].cache_slot == -1) {
/* we already have function name as last literal (do nothing) */
ret = op_array->last_literal - 1;
} else {
ret = zend_add_literal(op_array, zv TSRMLS_CC);
}
lc_name = zend_str_tolower_dup(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
ZVAL_STRINGL(&c, lc_name, Z_STRLEN_P(zv), 0);
lc_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(lc_literal);
ns_separator = (const char*)zend_memrchr(Z_STRVAL_P(zv), '\\', Z_STRLEN_P(zv)) + 1;
lc_len = Z_STRLEN_P(zv) - (ns_separator - Z_STRVAL_P(zv));
lc_name = zend_str_tolower_dup(ns_separator, lc_len);
ZVAL_STRINGL(&c, lc_name, lc_len, 0);
lc_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(lc_literal);
return ret;
}
/* }}} */
int zend_add_class_name_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) /* {{{ */
{
int ret;
char *lc_name;
int lc_len;
zval c;
int lc_literal;
if (op_array->last_literal > 0 &&
&op_array->literals[op_array->last_literal - 1].constant == zv &&
op_array->literals[op_array->last_literal - 1].cache_slot == -1) {
/* we already have function name as last literal (do nothing) */
ret = op_array->last_literal - 1;
} else {
ret = zend_add_literal(op_array, zv TSRMLS_CC);
}
if (Z_STRVAL_P(zv)[0] == '\\') {
lc_len = Z_STRLEN_P(zv) - 1;
lc_name = zend_str_tolower_dup(Z_STRVAL_P(zv) + 1, lc_len);
} else {
lc_len = Z_STRLEN_P(zv);
lc_name = zend_str_tolower_dup(Z_STRVAL_P(zv), lc_len);
}
ZVAL_STRINGL(&c, lc_name, lc_len, 0);
lc_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(lc_literal);
GET_CACHE_SLOT(ret);
return ret;
}
/* }}} */
int zend_add_const_name_literal(zend_op_array *op_array, const zval *zv, int unqualified TSRMLS_DC) /* {{{ */
{
int ret, tmp_literal;
char *name, *tmp_name;
const char *ns_separator;
int name_len, ns_len;
zval c;
if (op_array->last_literal > 0 &&
&op_array->literals[op_array->last_literal - 1].constant == zv &&
op_array->literals[op_array->last_literal - 1].cache_slot == -1) {
/* we already have function name as last literal (do nothing) */
ret = op_array->last_literal - 1;
} else {
ret = zend_add_literal(op_array, zv TSRMLS_CC);
}
/* skip leading '\\' */
if (Z_STRVAL_P(zv)[0] == '\\') {
name_len = Z_STRLEN_P(zv) - 1;
name = Z_STRVAL_P(zv) + 1;
} else {
name_len = Z_STRLEN_P(zv);
name = Z_STRVAL_P(zv);
}
ns_separator = zend_memrchr(name, '\\', name_len);
if (ns_separator) {
ns_len = ns_separator - name;
} else {
ns_len = 0;
}
if (ns_len) {
/* lowercased namespace name & original constant name */
tmp_name = estrndup(name, name_len);
zend_str_tolower(tmp_name, ns_len);
ZVAL_STRINGL(&c, tmp_name, name_len, 0);
tmp_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(tmp_literal);
/* lowercased namespace name & lowercased constant name */
tmp_name = zend_str_tolower_dup(name, name_len);
ZVAL_STRINGL(&c, tmp_name, name_len, 0);
tmp_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(tmp_literal);
}
if (ns_len) {
if (!unqualified) {
return ret;
}
ns_len++;
name += ns_len;
name_len -= ns_len;
}
/* original constant name */
tmp_name = estrndup(name, name_len);
ZVAL_STRINGL(&c, tmp_name, name_len, 0);
tmp_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(tmp_literal);
/* lowercased constant name */
tmp_name = zend_str_tolower_dup(name, name_len);
ZVAL_STRINGL(&c, tmp_name, name_len, 0);
tmp_literal = zend_add_literal(CG(active_op_array), &c TSRMLS_CC);
CALCULATE_LITERAL_HASH(tmp_literal);
return ret;
}
/* }}} */
#define LITERAL_STRINGL(op, str, len, copy) do { \
zval _c; \
ZVAL_STRINGL(&_c, str, len, copy); \
op.constant = zend_add_literal(CG(active_op_array), &_c TSRMLS_CC); \
} while (0)
#define LITERAL_LONG(op, val) do { \
zval _c; \
ZVAL_LONG(&_c, val); \
op.constant = zend_add_literal(CG(active_op_array), &_c TSRMLS_CC); \
} while (0)
#define LITERAL_LONG_EX(op_array, op, val) do { \
zval _c; \
ZVAL_LONG(&_c, val); \
op.constant = zend_add_literal(op_array, &_c TSRMLS_CC); \
} while (0)
#define LITERAL_NULL(op) do { \
zval _c; \
INIT_ZVAL( _c); \
op.constant = zend_add_literal(CG(active_op_array), &_c TSRMLS_CC); \
} while (0)
static inline zend_bool zend_is_function_or_method_call(const znode *variable) /* {{{ */
{
zend_uint type = variable->EA;
return ((type & ZEND_PARSED_METHOD_CALL) || (type == ZEND_PARSED_FUNCTION_CALL));
}
/* }}} */
void zend_do_binary_op(zend_uchar op, znode *result, const znode *op1, const znode *op2 TSRMLS_DC) /* {{{ */
{
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = op;
opline->result_type = IS_TMP_VAR;
opline->result.var = get_temporary_variable(CG(active_op_array));
SET_NODE(opline->op1, op1);
SET_NODE(opline->op2, op2);
GET_NODE(result, opline->result);
}
/* }}} */
void zend_do_unary_op(zend_uchar op, znode *result, const znode *op1 TSRMLS_DC) /* {{{ */
{
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = op;
opline->result_type = IS_TMP_VAR;
opline->result.var = get_temporary_variable(CG(active_op_array));
SET_NODE(opline->op1, op1);
GET_NODE(result, opline->result);
SET_UNUSED(opline->op2);
}
/* }}} */
#define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(opline->result)); memset(&opline->op1,0,sizeof(opline->op1)); memset(&opline->op2,0,sizeof(opline->op2)); opline->result_type=opline->op1_type=opline->op2_type=IS_UNUSED; }
static void zend_do_op_data(zend_op *data_op, const znode *value TSRMLS_DC) /* {{{ */
{
data_op->opcode = ZEND_OP_DATA;
SET_NODE(data_op->op1, value);
SET_UNUSED(data_op->op2);
}
/* }}} */
void zend_do_binary_assign_op(zend_uchar op, znode *result, const znode *op1, const znode *op2 TSRMLS_DC) /* {{{ */
{
int last_op_number = get_next_op_number(CG(active_op_array));
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
if (last_op_number > 0) {
zend_op *last_op = &CG(active_op_array)->opcodes[last_op_number-1];
switch (last_op->opcode) {
case ZEND_FETCH_OBJ_RW:
last_op->opcode = op;
last_op->extended_value = ZEND_ASSIGN_OBJ;
zend_do_op_data(opline, op2 TSRMLS_CC);
SET_UNUSED(opline->result);
GET_NODE(result, last_op->result);
return;
case ZEND_FETCH_DIM_RW:
last_op->opcode = op;
last_op->extended_value = ZEND_ASSIGN_DIM;
zend_do_op_data(opline, op2 TSRMLS_CC);
opline->op2.var = get_temporary_variable(CG(active_op_array));
opline->op2_type = IS_VAR;
SET_UNUSED(opline->result);
GET_NODE(result,last_op->result);
return;
default:
break;
}
}
opline->opcode = op;
SET_NODE(opline->op1, op1);
SET_NODE(opline->op2, op2);
opline->result_type = IS_VAR;
opline->result.var = get_temporary_variable(CG(active_op_array));
GET_NODE(result, opline->result);
}
/* }}} */
void fetch_simple_variable_ex(znode *result, znode *varname, int bp, zend_uchar op TSRMLS_DC) /* {{{ */
{
zend_op opline;
zend_op *opline_ptr;
zend_llist *fetch_list_ptr;
if (varname->op_type == IS_CONST) {
ulong hash = 0;
if (Z_TYPE(varname->u.constant) != IS_STRING) {
convert_to_string(&varname->u.constant);
} else if (IS_INTERNED(Z_STRVAL(varname->u.constant))) {
hash = INTERNED_HASH(Z_STRVAL(varname->u.constant));
}
if (!zend_is_auto_global_quick(varname->u.constant.value.str.val, varname->u.constant.value.str.len, hash TSRMLS_CC) &&
!(varname->u.constant.value.str.len == (sizeof("this")-1) &&
!memcmp(varname->u.constant.value.str.val, "this", sizeof("this"))) &&
(CG(active_op_array)->last == 0 ||
CG(active_op_array)->opcodes[CG(active_op_array)->last-1].opcode != ZEND_BEGIN_SILENCE)) {
result->op_type = IS_CV;
result->u.op.var = lookup_cv(CG(active_op_array), varname->u.constant.value.str.val, varname->u.constant.value.str.len, hash TSRMLS_CC);
varname->u.constant.value.str.val = (char*)CG(active_op_array)->vars[result->u.op.var].name;
result->EA = 0;
return;
}
}
if (bp) {
opline_ptr = &opline;
init_op(opline_ptr TSRMLS_CC);
} else {
opline_ptr = get_next_op(CG(active_op_array) TSRMLS_CC);
}
opline_ptr->opcode = op;
opline_ptr->result_type = IS_VAR;
opline_ptr->result.var = get_temporary_variable(CG(active_op_array));
SET_NODE(opline_ptr->op1, varname);
GET_NODE(result, opline_ptr->result);
SET_UNUSED(opline_ptr->op2);
opline_ptr->extended_value = ZEND_FETCH_LOCAL;
if (varname->op_type == IS_CONST) {
CALCULATE_LITERAL_HASH(opline_ptr->op1.constant);
if (zend_is_auto_global_quick(varname->u.constant.value.str.val, varname->u.constant.value.str.len, Z_HASH_P(&CONSTANT(opline_ptr->op1.constant)) TSRMLS_CC)) {
opline_ptr->extended_value = ZEND_FETCH_GLOBAL;
}
}
if (bp) {
zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
zend_llist_add_element(fetch_list_ptr, opline_ptr);
}
}
/* }}} */
void fetch_simple_variable(znode *result, znode *varname, int bp TSRMLS_DC) /* {{{ */
{
/* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
fetch_simple_variable_ex(result, varname, bp, ZEND_FETCH_W TSRMLS_CC);
}
/* }}} */
void zend_do_fetch_static_member(znode *result, znode *class_name TSRMLS_DC) /* {{{ */
{
znode class_node;
zend_llist *fetch_list_ptr;
zend_llist_element *le;
zend_op *opline_ptr;
zend_op opline;
if (class_name->op_type == IS_CONST &&
ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant))) {
zend_resolve_class_name(class_name, ZEND_FETCH_CLASS_GLOBAL, 1 TSRMLS_CC);
class_node = *class_name;
} else {
zend_do_fetch_class(&class_node, class_name TSRMLS_CC);
}
zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
if (result->op_type == IS_CV) {
init_op(&opline TSRMLS_CC);
opline.opcode = ZEND_FETCH_W;
opline.result_type = IS_VAR;
opline.result.var = get_temporary_variable(CG(active_op_array));
opline.op1_type = IS_CONST;
LITERAL_STRINGL(opline.op1, estrdup(CG(active_op_array)->vars[result->u.op.var].name), CG(active_op_array)->vars[result->u.op.var].name_len, 0);
CALCULATE_LITERAL_HASH(opline.op1.constant);
GET_POLYMORPHIC_CACHE_SLOT(opline.op1.constant);
if (class_node.op_type == IS_CONST) {
opline.op2_type = IS_CONST;
opline.op2.constant =
zend_add_class_name_literal(CG(active_op_array), &class_node.u.constant TSRMLS_CC);
} else {
SET_NODE(opline.op2, &class_node);
}
GET_NODE(result,opline.result);
opline.extended_value |= ZEND_FETCH_STATIC_MEMBER;
opline_ptr = &opline;
zend_llist_add_element(fetch_list_ptr, &opline);
} else {
le = fetch_list_ptr->head;
opline_ptr = (zend_op *)le->data;
if (opline_ptr->opcode != ZEND_FETCH_W && opline_ptr->op1_type == IS_CV) {
init_op(&opline TSRMLS_CC);
opline.opcode = ZEND_FETCH_W;
opline.result_type = IS_VAR;
opline.result.var = get_temporary_variable(CG(active_op_array));
opline.op1_type = IS_CONST;
LITERAL_STRINGL(opline.op1, estrdup(CG(active_op_array)->vars[opline_ptr->op1.var].name), CG(active_op_array)->vars[opline_ptr->op1.var].name_len, 0);
CALCULATE_LITERAL_HASH(opline.op1.constant);
GET_POLYMORPHIC_CACHE_SLOT(opline.op1.constant);
if (class_node.op_type == IS_CONST) {
opline.op2_type = IS_CONST;
opline.op2.constant =
zend_add_class_name_literal(CG(active_op_array), &class_node.u.constant TSRMLS_CC);
} else {
SET_NODE(opline.op2, &class_node);
}
opline.extended_value |= ZEND_FETCH_STATIC_MEMBER;
COPY_NODE(opline_ptr->op1, opline.result);
zend_llist_prepend_element(fetch_list_ptr, &opline);
} else {
if (opline_ptr->op1_type == IS_CONST) {
GET_POLYMORPHIC_CACHE_SLOT(opline_ptr->op1.constant);
}
if (class_node.op_type == IS_CONST) {
opline_ptr->op2_type = IS_CONST;
opline_ptr->op2.constant =
zend_add_class_name_literal(CG(active_op_array), &class_node.u.constant TSRMLS_CC);
} else {
SET_NODE(opline_ptr->op2, &class_node);
}
opline_ptr->extended_value |= ZEND_FETCH_STATIC_MEMBER;
}
}
}
/* }}} */
void fetch_array_begin(znode *result, znode *varname, znode *first_dim TSRMLS_DC) /* {{{ */
{
fetch_simple_variable(result, varname, 1 TSRMLS_CC);
fetch_array_dim(result, result, first_dim TSRMLS_CC);
}
/* }}} */
void fetch_array_dim(znode *result, const znode *parent, const znode *dim TSRMLS_DC) /* {{{ */
{
zend_op opline;
zend_llist *fetch_list_ptr;
zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
if (zend_is_function_or_method_call(parent)) {
init_op(&opline TSRMLS_CC);
opline.opcode = ZEND_SEPARATE;
SET_NODE(opline.op1, parent);
SET_UNUSED(opline.op2);
opline.result_type = IS_VAR;
opline.result.var = opline.op1.var;
zend_llist_add_element(fetch_list_ptr, &opline);
}
init_op(&opline TSRMLS_CC);
opline.opcode = ZEND_FETCH_DIM_W; /* the backpatching routine assumes W */
opline.result_type = IS_VAR;
opline.result.var = get_temporary_variable(CG(active_op_array));
SET_NODE(opline.op1, parent);
SET_NODE(opline.op2, dim);
if (opline.op2_type == IS_CONST && Z_TYPE(CONSTANT(opline.op2.constant)) == IS_STRING) {
ulong index;
int numeric = 0;
ZEND_HANDLE_NUMERIC_EX(Z_STRVAL(CONSTANT(opline.op2.constant)), Z_STRLEN(CONSTANT(opline.op2.constant))+1, index, numeric = 1);
if (numeric) {
zval_dtor(&CONSTANT(opline.op2.constant));
ZVAL_LONG(&CONSTANT(opline.op2.constant), index);
} else {
CALCULATE_LITERAL_HASH(opline.op2.constant);
}
}
GET_NODE(result, opline.result);
zend_llist_add_element(fetch_list_ptr, &opline);
}
/* }}} */
void fetch_string_offset(znode *result, const znode *parent, const znode *offset TSRMLS_DC) /* {{{ */
{
fetch_array_dim(result, parent, offset TSRMLS_CC);
}
/* }}} */
void zend_do_print(znode *result, const znode *arg TSRMLS_DC) /* {{{ */
{
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->result_type = IS_TMP_VAR;
opline->result.var = get_temporary_variable(CG(active_op_array));
opline->opcode = ZEND_PRINT;
SET_NODE(opline->op1, arg);
SET_UNUSED(opline->op2);
GET_NODE(result, opline->result);
}
/* }}} */
void zend_do_echo(const znode *arg TSRMLS_DC) /* {{{ */
{
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_ECHO;
SET_NODE(opline->op1, arg);
SET_UNUSED(opline->op2);
}
/* }}} */
void zend_do_abstract_method(const znode *function_name, znode *modifiers, const znode *body TSRMLS_DC) /* {{{ */
{
char *method_type;
if (CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) {
Z_LVAL(modifiers->u.constant) |= ZEND_ACC_ABSTRACT;
method_type = "Interface";
} else {
method_type = "Abstract";
}
if (modifiers->u.constant.value.lval & ZEND_ACC_ABSTRACT) {
if(modifiers->u.constant.value.lval & ZEND_ACC_PRIVATE) {
zend_error(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private", method_type, CG(active_class_entry)->name, function_name->u.constant.value.str.val);
}
if (Z_LVAL(body->u.constant) == ZEND_ACC_ABSTRACT) {
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_RAISE_ABSTRACT_ERROR;
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
} else {
/* we had code in the function body */
zend_error(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body", method_type, CG(active_class_entry)->name, function_name->u.constant.value.str.val);
}
} else {
if (body->u.constant.value.lval == ZEND_ACC_ABSTRACT) {
zend_error(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body", CG(active_class_entry)->name, function_name->u.constant.value.str.val);
}
}
}
/* }}} */
static zend_bool opline_is_fetch_this(const zend_op *opline TSRMLS_DC) /* {{{ */
{
if ((opline->opcode == ZEND_FETCH_W) && (opline->op1_type == IS_CONST)
&& (Z_TYPE(CONSTANT(opline->op1.constant)) == IS_STRING)
&& (Z_HASH_P(&CONSTANT(opline->op1.constant)) == THIS_HASHVAL)
&& (Z_STRLEN(CONSTANT(opline->op1.constant)) == (sizeof("this")-1))
&& !memcmp(Z_STRVAL(CONSTANT(opline->op1.constant)), "this", sizeof("this"))) {
return 1;
} else {
return 0;
}
}
/* }}} */
void zend_do_assign(znode *result, znode *variable, znode *value TSRMLS_DC) /* {{{ */
{
int last_op_number;
zend_op *opline;
if (value->op_type == IS_CV) {
zend_llist *fetch_list_ptr;
zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
if (fetch_list_ptr && fetch_list_ptr->head) {
opline = (zend_op *)fetch_list_ptr->head->data;
if (opline->opcode == ZEND_FETCH_DIM_W &&
opline->op1_type == IS_CV &&
opline->op1.var == value->u.op.var) {
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_FETCH_R;
opline->result_type = IS_VAR;
opline->result.var = get_temporary_variable(CG(active_op_array));
opline->op1_type = IS_CONST;
LITERAL_STRINGL(opline->op1,
CG(active_op_array)->vars[value->u.op.var].name,
CG(active_op_array)->vars[value->u.op.var].name_len, 1);
CALCULATE_LITERAL_HASH(opline->op1.constant);
SET_UNUSED(opline->op2);
opline->extended_value = ZEND_FETCH_LOCAL;
GET_NODE(value, opline->result);
}
}
}
zend_do_end_variable_parse(variable, BP_VAR_W, 0 TSRMLS_CC);
last_op_number = get_next_op_number(CG(active_op_array));
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
if (variable->op_type == IS_CV) {
if (variable->u.op.var == CG(active_op_array)->this_var) {
zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
}
} else if (variable->op_type == IS_VAR) {
int n = 0;
while (last_op_number - n > 0) {
zend_op *last_op;
last_op = &CG(active_op_array)->opcodes[last_op_number-n-1];
if (last_op->result_type == IS_VAR &&
last_op->result.var == variable->u.op.var) {
if (last_op->opcode == ZEND_FETCH_OBJ_W) {
if (n > 0) {
int opline_no = (opline-CG(active_op_array)->opcodes)/sizeof(*opline);
*opline = *last_op;
MAKE_NOP(last_op);
/* last_op = opline; */
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
/* get_next_op can realloc, we need to move last_op */
last_op = &CG(active_op_array)->opcodes[opline_no];
}
last_op->opcode = ZEND_ASSIGN_OBJ;
zend_do_op_data(opline, value TSRMLS_CC);
SET_UNUSED(opline->result);
GET_NODE(result, last_op->result);
return;
} else if (last_op->opcode == ZEND_FETCH_DIM_W) {
if (n > 0) {
int opline_no = (opline-CG(active_op_array)->opcodes)/sizeof(*opline);
*opline = *last_op;
MAKE_NOP(last_op);
/* last_op = opline; */
/* TBFixed: this can realloc opcodes, leaving last_op pointing wrong */
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
/* get_next_op can realloc, we need to move last_op */
last_op = &CG(active_op_array)->opcodes[opline_no];
}
last_op->opcode = ZEND_ASSIGN_DIM;
zend_do_op_data(opline, value TSRMLS_CC);
opline->op2.var = get_temporary_variable(CG(active_op_array));
opline->op2_type = IS_VAR;
SET_UNUSED(opline->result);
GET_NODE(result, last_op->result);
return;