-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathop.c
15715 lines (13736 loc) · 513 KB
/
op.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
#line 2 "op.c"
/* op.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
* our Mr. Bilbo's first cousin on the mother's side (her mother being the
* youngest of the Old Took's daughters); and Mr. Drogo was his second
* cousin. So Mr. Frodo is his first *and* second cousin, once removed
* either way, as the saying is, if you follow me.' --the Gaffer
*
* [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
*/
/* This file contains the functions that create and manipulate the OP
* structures that hold a compiled perl program.
*
* Note that during the build of miniperl, a temporary copy of this file
* is made, called opmini.c.
*
* A Perl program is compiled into a tree of OP nodes. Each op contains:
* * structural OP pointers to its children and siblings (op_sibling,
* op_first etc) that define the tree structure;
* * execution order OP pointers (op_next, plus sometimes op_other,
* op_lastop etc) that define the execution sequence plus variants;
* * a pointer to the C "pp" function that would execute the op;
* * any data specific to that op.
* For example, an OP_CONST op points to the pp_const() function and to an
* SV containing the constant value. When pp_const() is executed, its job
* is to push that SV onto the stack.
*
* OPs are mainly created by the newFOO() functions, which are mainly
* called from the parser (in perly.y) as the code is parsed. For example
* the Perl code $a + $b * $c would cause the equivalent of the following
* to be called (oversimplifying a bit):
*
* newBINOP(OP_ADD, flags,
* newSVREF($a),
* newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
* )
*
* As the parser reduces low-level rules, it creates little op subtrees;
* as higher-level rules are resolved, these subtrees get joined together
* as branches on a bigger subtree, until eventually a top-level rule like
* a subroutine definition is reduced, at which point there is one large
* parse tree left.
*
* The execution order pointers (op_next) are generated as the subtrees
* are joined together. Consider this sub-expression: A*B + C/D: at the
* point when it's just been parsed, the op tree looks like:
*
* [+]
* |
* [*]------[/]
* | |
* A---B C---D
*
* with the intended execution order being:
*
* [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
*
* At this point all the nodes' op_next pointers will have been set,
* except that:
* * we don't know what the [NEXT] node will be yet;
* * we don't know what the [PREV] node will be yet, but when it gets
* created and needs its op_next set, it needs to be set to point to
* A, which is non-obvious.
* To handle both those cases, we temporarily set the top node's
* op_next to point to the first node to be executed in this subtree (A in
* this case). This means that initially a subtree's op_next chain,
* starting from the top node, will visit each node in execution sequence
* then point back at the top node.
* When we embed this subtree in a larger tree, its top op_next is used
* to get the start node, then is set to point to its new neighbour.
* For example the two separate [*],A,B and [/],C,D subtrees would
* initially have had:
* [*] => A; A => B; B => [*]
* and
* [/] => C; C => D; D => [/]
* When these two subtrees were joined together to make the [+] subtree,
* [+]'s op_next was set to [*]'s op_next, i.e. A; then [*]'s op_next was
* set to point to [/]'s op_next, i.e. C.
*
* This op_next linking is done by the LINKLIST() macro and its underlying
* op_linklist() function. Given a top-level op, if its op_next is
* non-null, it's already been linked, so leave it. Otherwise link it with
* its children as described above, possibly recursively if any of the
* children have a null op_next.
*
* In summary: given a subtree, its top-level node's op_next will either
* be:
* NULL: the subtree hasn't been LINKLIST()ed yet;
* fake: points to the start op for this subtree;
* real: once the subtree has been embedded into a larger tree
*/
/*
Here's an older description from Larry.
Perl's compiler is essentially a 3-pass compiler with interleaved phases:
A bottom-up pass
A top-down pass
An execution-order pass
The bottom-up pass is represented by all the "newOP" routines and
the ck_ routines. The bottom-upness is actually driven by yacc.
So at the point that a ck_ routine fires, we have no idea what the
context is, either upward in the syntax tree, or either forward or
backward in the execution order. (The bottom-up parser builds that
part of the execution order it knows about, but if you follow the "next"
links around, you'll find it's actually a closed loop through the
top level node.)
Whenever the bottom-up parser gets to a node that supplies context to
its components, it invokes that portion of the top-down pass that applies
to that part of the subtree (and marks the top node as processed, so
if a node further up supplies context, it doesn't have to take the
plunge again). As a particular subcase of this, as the new node is
built, it takes all the closed execution loops of its subcomponents
and links them into a new closed loop for the higher level node. But
it's still not the real execution order.
The actual execution order is not known till we get a grammar reduction
to a top-level unit like a subroutine or file that will be called by
"name" rather than via a "next" pointer. At that point, we can call
into peep() to do that code's portion of the 3rd pass. It has to be
recursive, but it's recursive on basic blocks, not on tree nodes.
*/
/* To implement user lexical pragmas, there needs to be a way at run time to
get the compile time state of %^H for that block. Storing %^H in every
block (or even COP) would be very expensive, so a different approach is
taken. The (running) state of %^H is serialised into a tree of HE-like
structs. Stores into %^H are chained onto the current leaf as a struct
refcounted_he * with the key and the value. Deletes from %^H are saved
with a value of PL_sv_placeholder. The state of %^H at any point can be
turned back into a regular HV by walking back up the tree from that point's
leaf, ignoring any key you've already seen (placeholder or not), storing
the rest into the HV structure, then removing the placeholders. Hence
memory is only used to store the %^H deltas from the enclosing COP, rather
than the entire %^H on each COP.
To cause actions on %^H to write out the serialisation records, it has
magic type 'H'. This magic (itself) does nothing, but its presence causes
the values to gain magic type 'h', which has entries for set and clear.
C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
it will be correctly restored when any inner compiling scope is exited.
*/
#include "EXTERN.h"
#define PERL_IN_OP_C
#include "perl.h"
#include "keywords.h"
#include "feature.h"
#include "regcomp.h"
#include "invlist_inline.h"
#define CALL_PEEP(o) PL_peepp(aTHX_ o)
#define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
/* remove any leading "empty" ops from the op_next chain whose first
* node's address is stored in op_p. Store the updated address of the
* first node in op_p.
*/
void
Perl_op_prune_chain_head(OP** op_p)
{
PERL_ARGS_ASSERT_OP_PRUNE_CHAIN_HEAD;
while (*op_p
&& ( (*op_p)->op_type == OP_NULL
|| (*op_p)->op_type == OP_SCOPE
|| (*op_p)->op_type == OP_SCALAR
|| (*op_p)->op_type == OP_LINESEQ)
)
*op_p = (*op_p)->op_next;
}
/* See the explanatory comments above struct opslab in op.h. */
#ifdef PERL_DEBUG_READONLY_OPS
# define PERL_SLAB_SIZE 128
# define PERL_MAX_SLAB_SIZE 4096
# include <sys/mman.h>
#endif
#ifndef PERL_SLAB_SIZE
# define PERL_SLAB_SIZE 64
#endif
#ifndef PERL_MAX_SLAB_SIZE
# define PERL_MAX_SLAB_SIZE 2048
#endif
/* rounds up to nearest pointer */
#define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
#define DIFF(o,p) \
(assert(((char *)(p) - (char *)(o)) % sizeof(I32**) == 0), \
((size_t)((I32 **)(p) - (I32**)(o))))
/* requires double parens and aTHX_ */
#define DEBUG_S_warn(args) \
DEBUG_S( \
PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
)
/* opslot_size includes the size of the slot header, and an op can't be smaller than BASEOP */
#define OPSLOT_SIZE_BASE (SIZE_TO_PSIZE(sizeof(OPSLOT)))
/* the number of bytes to allocate for a slab with sz * sizeof(I32 **) space for op */
#define OpSLABSizeBytes(sz) \
((sz) * sizeof(I32 *) + STRUCT_OFFSET(OPSLAB, opslab_slots))
/* malloc a new op slab (suitable for attaching to PL_compcv).
* sz is in units of pointers from the beginning of opslab_opslots */
static OPSLAB *
S_new_slab(pTHX_ OPSLAB *head, size_t sz)
{
OPSLAB *slab;
size_t sz_bytes = OpSLABSizeBytes(sz);
/* opslot_offset is only U16 */
assert(sz < U16_MAX);
/* room for at least one op */
assert(sz >= OPSLOT_SIZE_BASE);
#ifdef PERL_DEBUG_READONLY_OPS
slab = (OPSLAB *) mmap(0, sz_bytes,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
(unsigned long) sz, slab));
if (slab == MAP_FAILED) {
perror("mmap failed");
abort();
}
#else
slab = (OPSLAB *)PerlMemShared_malloc(sz_bytes);
Zero(slab, sz_bytes, char);
#endif
slab->opslab_size = (U16)sz;
#ifndef WIN32
/* The context is unused in non-Windows */
PERL_UNUSED_CONTEXT;
#endif
slab->opslab_free_space = sz;
slab->opslab_head = head ? head : slab;
DEBUG_S_warn((aTHX_ "allocated new op slab sz 0x%x, %p, head slab %p",
(unsigned int)slab->opslab_size, (void*)slab,
(void*)(slab->opslab_head)));
return slab;
}
#define OPSLOT_SIZE_TO_INDEX(sz) ((sz) - OPSLOT_SIZE_BASE)
#define link_freed_op(slab, o) S_link_freed_op(aTHX_ slab, o)
static void
S_link_freed_op(pTHX_ OPSLAB *slab, OP *o) {
U16 sz = OpSLOT(o)->opslot_size;
U16 index = OPSLOT_SIZE_TO_INDEX(sz);
assert(sz >= OPSLOT_SIZE_BASE);
/* make sure the array is large enough to include ops this large */
if (!slab->opslab_freed) {
/* we don't have a free list array yet, make a new one */
slab->opslab_freed_size = index+1;
slab->opslab_freed = (OP**)PerlMemShared_calloc((slab->opslab_freed_size), sizeof(OP*));
if (!slab->opslab_freed)
croak_no_mem();
}
else if (index >= slab->opslab_freed_size) {
/* It's probably not worth doing exponential expansion here, the number of op sizes
is small.
*/
/* We already have a list that isn't large enough, expand it */
size_t newsize = index+1;
OP **p = (OP **)PerlMemShared_realloc(slab->opslab_freed, newsize * sizeof(OP*));
if (!p)
croak_no_mem();
Zero(p+slab->opslab_freed_size, newsize - slab->opslab_freed_size, OP *);
slab->opslab_freed = p;
slab->opslab_freed_size = newsize;
}
o->op_next = slab->opslab_freed[index];
slab->opslab_freed[index] = o;
}
/* Returns a sz-sized block of memory (suitable for holding an op) from
* a free slot in the chain of op slabs attached to PL_compcv.
* Allocates a new slab if necessary.
* if PL_compcv isn't compiling, malloc() instead.
*/
void *
Perl_Slab_Alloc(pTHX_ size_t sz)
{
OPSLAB *head_slab; /* first slab in the chain */
OPSLAB *slab2;
OPSLOT *slot;
OP *o;
size_t sz_in_p; /* size in pointer units, including the OPSLOT header */
/* We only allocate ops from the slab during subroutine compilation.
We find the slab via PL_compcv, hence that must be non-NULL. It could
also be pointing to a subroutine which is now fully set up (CvROOT()
pointing to the top of the optree for that sub), or a subroutine
which isn't using the slab allocator. If our sanity checks aren't met,
don't use a slab, but allocate the OP directly from the heap. */
if (!PL_compcv || CvROOT(PL_compcv)
|| (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
{
o = (OP*)PerlMemShared_calloc(1, sz);
goto gotit;
}
/* While the subroutine is under construction, the slabs are accessed via
CvSTART(), to avoid needing to expand PVCV by one pointer for something
unneeded at runtime. Once a subroutine is constructed, the slabs are
accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
allocated yet. See the commit message for 8be227ab5eaa23f2 for more
details. */
if (!CvSTART(PL_compcv)) {
CvSTART(PL_compcv) =
(OP *)(head_slab = S_new_slab(aTHX_ NULL, PERL_SLAB_SIZE));
CvSLABBED_on(PL_compcv);
head_slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
}
else ++(head_slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
sz_in_p = SIZE_TO_PSIZE(sz + OPSLOT_HEADER);
/* The head slab for each CV maintains a free list of OPs. In particular, constant folding
will free up OPs, so it makes sense to re-use them where possible. A
freed up slot is used in preference to a new allocation. */
if (head_slab->opslab_freed &&
OPSLOT_SIZE_TO_INDEX(sz_in_p) < head_slab->opslab_freed_size) {
U16 base_index;
/* look for a large enough size with any freed ops */
for (base_index = OPSLOT_SIZE_TO_INDEX(sz_in_p);
base_index < head_slab->opslab_freed_size && !head_slab->opslab_freed[base_index];
++base_index) {
}
if (base_index < head_slab->opslab_freed_size) {
/* found a freed op */
o = head_slab->opslab_freed[base_index];
DEBUG_S_warn((aTHX_ "realloced op at %p, slab %p, head slab %p",
(void *)o, (void *)OpMySLAB(o), (void *)head_slab));
head_slab->opslab_freed[base_index] = o->op_next;
Zero(o, sz, char);
o->op_slabbed = 1;
goto gotit;
}
}
#define INIT_OPSLOT(s) \
slot->opslot_offset = DIFF(&slab2->opslab_slots, slot) ; \
slot->opslot_size = s; \
slab2->opslab_free_space -= s; \
o = &slot->opslot_op; \
o->op_slabbed = 1
/* The partially-filled slab is next in the chain. */
slab2 = head_slab->opslab_next ? head_slab->opslab_next : head_slab;
if (slab2->opslab_free_space < sz_in_p) {
/* Remaining space is too small. */
/* If we can fit a BASEOP, add it to the free chain, so as not
to waste it. */
if (slab2->opslab_free_space >= OPSLOT_SIZE_BASE) {
slot = &slab2->opslab_slots;
INIT_OPSLOT(slab2->opslab_free_space);
o->op_type = OP_FREED;
DEBUG_S_warn((aTHX_ "linked unused op space at %p, slab %p, head slab %p",
(void *)o, (void *)slab2, (void *)head_slab));
link_freed_op(head_slab, o);
}
/* Create a new slab. Make this one twice as big. */
slab2 = S_new_slab(aTHX_ head_slab,
slab2->opslab_size > PERL_MAX_SLAB_SIZE / 2
? PERL_MAX_SLAB_SIZE
: slab2->opslab_size * 2);
slab2->opslab_next = head_slab->opslab_next;
head_slab->opslab_next = slab2;
}
assert(slab2->opslab_size >= sz_in_p);
/* Create a new op slot */
slot = OpSLOToff(slab2, slab2->opslab_free_space - sz_in_p);
assert(slot >= &slab2->opslab_slots);
INIT_OPSLOT(sz_in_p);
DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p, head slab %p",
(void*)o, (void*)slab2, (void*)head_slab));
gotit:
/* moresib == 0, op_sibling == 0 implies a solitary unattached op */
assert(!o->op_moresib);
assert(!o->op_sibparent);
return (void *)o;
}
#undef INIT_OPSLOT
#ifdef PERL_DEBUG_READONLY_OPS
void
Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
{
PERL_ARGS_ASSERT_SLAB_TO_RO;
if (slab->opslab_readonly) return;
slab->opslab_readonly = 1;
for (; slab; slab = slab->opslab_next) {
/*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
(unsigned long) slab->opslab_size, (void *)slab));*/
if (mprotect(slab, OpSLABSizeBytes(slab->opslab_size), PROT_READ))
Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", (void *)slab,
(unsigned long)slab->opslab_size, errno);
}
}
void
Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
{
OPSLAB *slab2;
PERL_ARGS_ASSERT_SLAB_TO_RW;
if (!slab->opslab_readonly) return;
slab2 = slab;
for (; slab2; slab2 = slab2->opslab_next) {
/*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
(unsigned long) size, (void *)slab2));*/
if (mprotect((void *)slab2, OpSLABSizeBytes(slab2->opslab_size),
PROT_READ|PROT_WRITE)) {
Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", (void *)slab,
(unsigned long)slab2->opslab_size, errno);
}
}
slab->opslab_readonly = 0;
}
#else
# define Slab_to_rw(op) NOOP
#endif
/* make freed ops die if they're inadvertently executed */
#ifdef DEBUGGING
static OP *
S_pp_freed(pTHX)
{
DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
}
#endif
/* Return the block of memory used by an op to the free list of
* the OP slab associated with that op.
*/
void
Perl_Slab_Free(pTHX_ void *op)
{
OP * const o = (OP *)op;
OPSLAB *slab;
PERL_ARGS_ASSERT_SLAB_FREE;
#ifdef DEBUGGING
o->op_ppaddr = S_pp_freed;
#endif
if (!o->op_slabbed) {
if (!o->op_static)
PerlMemShared_free(op);
return;
}
slab = OpSLAB(o);
/* If this op is already freed, our refcount will get screwy. */
assert(o->op_type != OP_FREED);
o->op_type = OP_FREED;
link_freed_op(slab, o);
DEBUG_S_warn((aTHX_ "freeing op at %p, slab %p, head slab %p",
(void*)o, (void *)OpMySLAB(o), (void*)slab));
OpslabREFCNT_dec_padok(slab);
}
void
Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
{
const bool havepad = cBOOL(PL_comppad);
PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
if (havepad) {
ENTER;
PAD_SAVE_SETNULLPAD();
}
opslab_free(slab);
if (havepad) LEAVE;
}
/* Free a chain of OP slabs. Should only be called after all ops contained
* in it have been freed. At this point, its reference count should be 1,
* because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1,
* and just directly calls opslab_free().
* (Note that the reference count which PL_compcv held on the slab should
* have been removed once compilation of the sub was complete).
*
*
*/
void
Perl_opslab_free(pTHX_ OPSLAB *slab)
{
OPSLAB *slab2;
PERL_ARGS_ASSERT_OPSLAB_FREE;
PERL_UNUSED_CONTEXT;
DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
assert(slab->opslab_refcnt == 1);
PerlMemShared_free(slab->opslab_freed);
do {
slab2 = slab->opslab_next;
#ifdef DEBUGGING
slab->opslab_refcnt = ~(size_t)0;
#endif
#ifdef PERL_DEBUG_READONLY_OPS
DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
(void*)slab));
if (munmap(slab, OpSLABSizeBytes(slab->opslab_size))) {
perror("munmap failed");
abort();
}
#else
PerlMemShared_free(slab);
#endif
slab = slab2;
} while (slab);
}
/* like opslab_free(), but first calls op_free() on any ops in the slab
* not marked as OP_FREED
*/
void
Perl_opslab_force_free(pTHX_ OPSLAB *slab)
{
OPSLAB *slab2;
#ifdef DEBUGGING
size_t savestack_count = 0;
#endif
PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
slab2 = slab;
do {
OPSLOT *slot = OpSLOToff(slab2, slab2->opslab_free_space);
OPSLOT *end = OpSLOToff(slab2, slab2->opslab_size);
for (; slot < end;
slot = (OPSLOT*) ((I32**)slot + slot->opslot_size) )
{
if (slot->opslot_op.op_type != OP_FREED
&& !(slot->opslot_op.op_savefree
#ifdef DEBUGGING
&& ++savestack_count
#endif
)
) {
assert(slot->opslot_op.op_slabbed);
op_free(&slot->opslot_op);
if (slab->opslab_refcnt == 1) goto free;
}
}
} while ((slab2 = slab2->opslab_next));
/* > 1 because the CV still holds a reference count. */
if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
#ifdef DEBUGGING
assert(savestack_count == slab->opslab_refcnt-1);
#endif
/* Remove the CV’s reference count. */
slab->opslab_refcnt--;
return;
}
free:
opslab_free(slab);
}
#ifdef PERL_DEBUG_READONLY_OPS
OP *
Perl_op_refcnt_inc(pTHX_ OP *o)
{
if(o) {
OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
if (slab && slab->opslab_readonly) {
Slab_to_rw(slab);
++o->op_targ;
Slab_to_ro(slab);
} else {
++o->op_targ;
}
}
return o;
}
PADOFFSET
Perl_op_refcnt_dec(pTHX_ OP *o)
{
PADOFFSET result;
OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
PERL_ARGS_ASSERT_OP_REFCNT_DEC;
if (slab && slab->opslab_readonly) {
Slab_to_rw(slab);
result = --o->op_targ;
Slab_to_ro(slab);
} else {
result = --o->op_targ;
}
return result;
}
#endif
/*
* In the following definition, the ", (OP*)0" is just to make the compiler
* think the expression is of the right type: croak actually does a Siglongjmp.
*/
#define CHECKOP(type,o) \
((PL_op_mask && PL_op_mask[type]) \
? ( op_free((OP*)o), \
Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
(OP*)0 ) \
: PL_check[type](aTHX_ (OP*)o))
#define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
STATIC OP *
S_no_fh_allowed(pTHX_ OP *o)
{
PERL_ARGS_ASSERT_NO_FH_ALLOWED;
yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
OP_DESC(o)));
return o;
}
STATIC OP *
S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
{
PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
return o;
}
STATIC OP *
S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
{
PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
return o;
}
STATIC void
S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
{
PERL_ARGS_ASSERT_BAD_TYPE_PV;
yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
(int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
}
STATIC void
S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
{
SV * const namesv = cv_name((CV *)gv, NULL, 0);
PERL_ARGS_ASSERT_BAD_TYPE_GV;
yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
(int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
}
void
Perl_no_bareword_allowed(pTHX_ OP *o)
{
PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
qerror(Perl_mess(aTHX_
"Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
SVfARG(cSVOPo_sv)));
o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
}
void
Perl_no_bareword_filehandle(pTHX_ const char *fhname) {
PERL_ARGS_ASSERT_NO_BAREWORD_FILEHANDLE;
if (strNE(fhname, "STDERR")
&& strNE(fhname, "STDOUT")
&& strNE(fhname, "STDIN")
&& strNE(fhname, "_")
&& strNE(fhname, "ARGV")
&& strNE(fhname, "ARGVOUT")
&& strNE(fhname, "DATA")) {
qerror(Perl_mess(aTHX_ "Bareword filehandle \"%s\" not allowed under 'no feature \"bareword_filehandles\"'", fhname));
}
}
/* "register" allocation */
PADOFFSET
Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
{
PADOFFSET off;
bool is_idfirst, is_default;
const bool is_our = (PL_parser->in_my == KEY_our);
PERL_ARGS_ASSERT_ALLOCMY;
if (flags & ~SVf_UTF8)
Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
(UV)flags);
is_idfirst = flags & SVf_UTF8
? isIDFIRST_utf8_safe((U8*)name + 1, name + len)
: isIDFIRST_A(name[1]);
/* $_, @_, etc. */
is_default = len == 2 && name[1] == '_';
/* complain about "my $<special_var>" etc etc */
if (!is_our && (!is_idfirst || is_default)) {
const char * const type =
PL_parser->in_my == KEY_sigvar ? "subroutine signature" :
PL_parser->in_my == KEY_state ? "\"state\"" : "\"my\"";
if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
&& isASCII(name[1])
&& (!isPRINT(name[1]) || memCHRs("\t\n\r\f", name[1]))) {
/* diag_listed_as: Can't use global %s in %s */
yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in %s",
name[0], toCTRL(name[1]),
(int)(len - 2), name + 2,
type));
} else {
yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in %s",
(int) len, name,
type), flags & SVf_UTF8);
}
}
/* allocate a spare slot and store the name in that slot */
U32 addflags = 0;
if(is_our)
addflags |= padadd_OUR;
else if(PL_parser->in_my == KEY_state)
addflags |= padadd_STATE;
else if(PL_parser->in_my == KEY_field)
addflags |= padadd_FIELD;
off = pad_add_name_pvn(name, len, addflags,
PL_parser->in_my_stash,
(is_our
/* $_ is always in main::, even with our */
? (PL_curstash && !memEQs(name,len,"$_")
? PL_curstash
: PL_defstash)
: NULL
)
);
/* anon sub prototypes contains state vars should always be cloned,
* otherwise the state var would be shared between anon subs */
if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
CvCLONE_on(PL_compcv);
return off;
}
/*
=for apidoc_section $optree_manipulation
=for apidoc alloccopstash
Available only under threaded builds, this function allocates an entry in
C<PL_stashpad> for the stash passed to it.
=cut
*/
#ifdef USE_ITHREADS
PADOFFSET
Perl_alloccopstash(pTHX_ HV *hv)
{
PADOFFSET off = 0, o = 1;
bool found_slot = FALSE;
PERL_ARGS_ASSERT_ALLOCCOPSTASH;
if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
for (; o < PL_stashpadmax; ++o) {
if (PL_stashpad[o] == hv) return PL_stashpadix = o;
if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
found_slot = TRUE, off = o;
}
if (!found_slot) {
Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
off = PL_stashpadmax;
PL_stashpadmax += 10;
}
PL_stashpad[PL_stashpadix = off] = hv;
return off;
}
#endif
/* free the body of an op without examining its contents.
* Always use this rather than FreeOp directly */
static void
S_op_destroy(pTHX_ OP *o)
{
FreeOp(o);
}
/* Destructor */
/*
=for apidoc op_free
Free an op and its children. Only use this when an op is no longer linked
to from any optree.
Remember that any op with C<OPf_KIDS> set is expected to have a valid
C<op_first> pointer. If you are attempting to free an op but preserve its
child op, make sure to clear that flag before calling C<op_free()>. For
example:
OP *kid = o->op_first; o->op_first = NULL;
o->op_flags &= ~OPf_KIDS;
op_free(o);
=cut
*/
void
Perl_op_free(pTHX_ OP *o)
{
OPCODE type;
OP *top_op = o;
OP *next_op = o;
bool went_up = FALSE; /* whether we reached the current node by
following the parent pointer from a child, and
so have already seen this node */
if (!o || o->op_type == OP_FREED)
return;
if (o->op_private & OPpREFCOUNTED) {
/* if base of tree is refcounted, just decrement */
switch (o->op_type) {
case OP_LEAVESUB:
case OP_LEAVESUBLV:
case OP_LEAVEEVAL:
case OP_LEAVE:
case OP_SCOPE:
case OP_LEAVEWRITE:
{
PADOFFSET refcnt;
OP_REFCNT_LOCK;
refcnt = OpREFCNT_dec(o);
OP_REFCNT_UNLOCK;
if (refcnt) {
/* Need to find and remove any pattern match ops from
* the list we maintain for reset(). */
find_and_forget_pmops(o);
return;
}
}
break;
default:
break;
}
}
while (next_op) {
o = next_op;
/* free child ops before ourself, (then free ourself "on the
* way back up") */
/* Ensure the caller maintains the relationship between OPf_KIDS and
* op_first != NULL when restructuring the tree
* https://github.com/Perl/perl5/issues/20764
*/
assert(!(o->op_flags & OPf_KIDS) || cUNOPo->op_first);
if (!went_up && o->op_flags & OPf_KIDS) {
next_op = cUNOPo->op_first;
continue;
}
/* find the next node to visit, *then* free the current node
* (can't rely on o->op_* fields being valid after o has been
* freed) */
/* The next node to visit will be either the sibling, or the
* parent if no siblings left, or NULL if we've worked our way
* back up to the top node in the tree */
next_op = (o == top_op) ? NULL : o->op_sibparent;
went_up = cBOOL(!OpHAS_SIBLING(o)); /* parents are already visited */
/* Now process the current node */
/* Though ops may be freed twice, freeing the op after its slab is a
big no-no. */
assert(!o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
/* During the forced freeing of ops after compilation failure, kidops
may be freed before their parents. */
if (!o || o->op_type == OP_FREED)
continue;
type = o->op_type;
/* an op should only ever acquire op_private flags that we know about.
* If this fails, you may need to fix something in regen/op_private.
* Don't bother testing if:
* * the op_ppaddr doesn't match the op; someone may have
* overridden the op and be doing strange things with it;
* * we've errored, as op flags are often left in an
* inconsistent state then. Note that an error when
* compiling the main program leaves PL_parser NULL, so
* we can't spot faults in the main code, only
* evaled/required code;
* * it's a banned op - we may be croaking before the op is
* fully formed. - see CHECKOP. */
#ifdef DEBUGGING
if ( o->op_ppaddr == PL_ppaddr[type]
&& PL_parser
&& !PL_parser->error_count
&& !(PL_op_mask && PL_op_mask[type])
)
{
assert(!(o->op_private & ~PL_op_private_valid[type]));
}
#endif
/* Call the op_free hook if it has been set. Do it now so that it's called
* at the right time for refcounted ops, but still before all of the kids
* are freed. */
CALL_OPFREEHOOK(o);
if (type == OP_NULL)
type = (OPCODE)o->op_targ;
if (o->op_slabbed)
Slab_to_rw(OpSLAB(o));
/* COP* is not cleared by op_clear() so that we may track line
* numbers etc even after null() */
if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
cop_free((COP*)o);
}
op_clear(o);
FreeOp(o);
if (PL_op == o)
PL_op = NULL;
}
}
/* S_op_clear_gv(): free a GV attached to an OP */
STATIC