4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
28 * A Perl program is compiled into a tree of OP nodes. Each op contains:
29 * * structural OP pointers to its children and siblings (op_sibling,
30 * op_first etc) that define the tree structure;
31 * * execution order OP pointers (op_next, plus sometimes op_other,
32 * op_lastop etc) that define the execution sequence plus variants;
33 * * a pointer to the C "pp" function that would execute the op;
34 * * any data specific to that op.
35 * For example, an OP_CONST op points to the pp_const() function and to an
36 * SV containing the constant value. When pp_const() is executed, its job
37 * is to push that SV onto the stack.
39 * OPs are mainly created by the newFOO() functions, which are mainly
40 * called from the parser (in perly.y) as the code is parsed. For example
41 * the Perl code $a + $b * $c would cause the equivalent of the following
42 * to be called (oversimplifying a bit):
44 * newBINOP(OP_ADD, flags,
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
49 * As the parser reduces low-level rules, it creates little op subtrees;
50 * as higher-level rules are resolved, these subtrees get joined together
51 * as branches on a bigger subtree, until eventually a top-level rule like
52 * a subroutine definition is reduced, at which point there is one large
55 * The execution order pointers (op_next) are generated as the subtrees
56 * are joined together. Consider this sub-expression: A*B + C/D: at the
57 * point when it's just been parsed, the op tree looks like:
65 * with the intended execution order being:
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
69 * At this point all the nodes' op_next pointers will have been set,
71 * * we don't know what the [NEXT] node will be yet;
72 * * we don't know what the [PREV] node will be yet, but when it gets
73 * created and needs its op_next set, it needs to be set to point to
74 * A, which is non-obvious.
75 * To handle both those cases, we temporarily set the top node's
76 * op_next to point to the first node to be executed in this subtree (A in
77 * this case). This means that initially a subtree's op_next chain,
78 * starting from the top node, will visit each node in execution sequence
79 * then point back at the top node.
80 * When we embed this subtree in a larger tree, its top op_next is used
81 * to get the start node, then is set to point to its new neighbour.
82 * For example the two separate [*],A,B and [/],C,D subtrees would
84 * [*] => A; A => B; B => [*]
86 * [/] => C; C => D; D => [/]
87 * When these two subtrees were joined together to make the [+] subtree,
88 * [+]'s op_next was set to [*]'s op_next, i.e. A; then [*]'s op_next was
89 * set to point to [/]'s op_next, i.e. C.
91 * This op_next linking is done by the LINKLIST() macro and its underlying
92 * op_linklist() function. Given a top-level op, if its op_next is
93 * non-null, it's already been linked, so leave it. Otherwise link it with
94 * its children as described above, possibly recursively if any of the
95 * children have a null op_next.
97 * In summary: given a subtree, its top-level node's op_next will either
99 * NULL: the subtree hasn't been LINKLIST()ed yet;
100 * fake: points to the start op for this subtree;
101 * real: once the subtree has been embedded into a larger tree
106 Here's an older description from Larry.
108 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
112 An execution-order pass
114 The bottom-up pass is represented by all the "newOP" routines and
115 the ck_ routines. The bottom-upness is actually driven by yacc.
116 So at the point that a ck_ routine fires, we have no idea what the
117 context is, either upward in the syntax tree, or either forward or
118 backward in the execution order. (The bottom-up parser builds that
119 part of the execution order it knows about, but if you follow the "next"
120 links around, you'll find it's actually a closed loop through the
123 Whenever the bottom-up parser gets to a node that supplies context to
124 its components, it invokes that portion of the top-down pass that applies
125 to that part of the subtree (and marks the top node as processed, so
126 if a node further up supplies context, it doesn't have to take the
127 plunge again). As a particular subcase of this, as the new node is
128 built, it takes all the closed execution loops of its subcomponents
129 and links them into a new closed loop for the higher level node. But
130 it's still not the real execution order.
132 The actual execution order is not known till we get a grammar reduction
133 to a top-level unit like a subroutine or file that will be called by
134 "name" rather than via a "next" pointer. At that point, we can call
135 into peep() to do that code's portion of the 3rd pass. It has to be
136 recursive, but it's recursive on basic blocks, not on tree nodes.
139 /* To implement user lexical pragmas, there needs to be a way at run time to
140 get the compile time state of %^H for that block. Storing %^H in every
141 block (or even COP) would be very expensive, so a different approach is
142 taken. The (running) state of %^H is serialised into a tree of HE-like
143 structs. Stores into %^H are chained onto the current leaf as a struct
144 refcounted_he * with the key and the value. Deletes from %^H are saved
145 with a value of PL_sv_placeholder. The state of %^H at any point can be
146 turned back into a regular HV by walking back up the tree from that point's
147 leaf, ignoring any key you've already seen (placeholder or not), storing
148 the rest into the HV structure, then removing the placeholders. Hence
149 memory is only used to store the %^H deltas from the enclosing COP, rather
150 than the entire %^H on each COP.
152 To cause actions on %^H to write out the serialisation records, it has
153 magic type 'H'. This magic (itself) does nothing, but its presence causes
154 the values to gain magic type 'h', which has entries for set and clear.
155 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
156 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
157 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
158 it will be correctly restored when any inner compiling scope is exited.
164 #include "keywords.h"
167 #include "invlist_inline.h"
169 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
170 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
171 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
173 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
175 /* remove any leading "empty" ops from the op_next chain whose first
176 * node's address is stored in op_p. Store the updated address of the
177 * first node in op_p.
181 S_prune_chain_head(OP** op_p)
184 && ( (*op_p)->op_type == OP_NULL
185 || (*op_p)->op_type == OP_SCOPE
186 || (*op_p)->op_type == OP_SCALAR
187 || (*op_p)->op_type == OP_LINESEQ)
189 *op_p = (*op_p)->op_next;
193 /* See the explanatory comments above struct opslab in op.h. */
195 #ifdef PERL_DEBUG_READONLY_OPS
196 # define PERL_SLAB_SIZE 128
197 # define PERL_MAX_SLAB_SIZE 4096
198 # include <sys/mman.h>
201 #ifndef PERL_SLAB_SIZE
202 # define PERL_SLAB_SIZE 64
204 #ifndef PERL_MAX_SLAB_SIZE
205 # define PERL_MAX_SLAB_SIZE 2048
208 /* rounds up to nearest pointer */
209 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
210 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
212 /* requires double parens and aTHX_ */
213 #define DEBUG_S_warn(args) \
215 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
219 /* malloc a new op slab (suitable for attaching to PL_compcv).
220 * sz is in units of pointers */
223 S_new_slab(pTHX_ OPSLAB *head, size_t sz)
227 /* opslot_offset is only U16 */
228 assert(sz < U16_MAX);
230 #ifdef PERL_DEBUG_READONLY_OPS
231 slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
232 PROT_READ|PROT_WRITE,
233 MAP_ANON|MAP_PRIVATE, -1, 0);
234 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
235 (unsigned long) sz, slab));
236 if (slab == MAP_FAILED) {
237 perror("mmap failed");
241 slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
243 slab->opslab_size = (U16)sz;
246 /* The context is unused in non-Windows */
249 slab->opslab_free_space = sz - DIFF(slab, &slab->opslab_slots);
250 slab->opslab_head = head ? head : slab;
251 DEBUG_S_warn((aTHX_ "allocated new op slab sz 0x%x, %p, head slab %p",
252 (unsigned int)slab->opslab_size, (void*)slab,
253 (void*)(slab->opslab_head)));
257 /* opslot_size includes the size of the slot header, and an op can't be smaller than BASEOP */
258 #define OPSLOT_SIZE_BASE (SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
259 #define OPSLOT_SIZE_TO_INDEX(sz) ((sz) - OPSLOT_SIZE_BASE)
261 #define link_freed_op(slab, o) S_link_freed_op(aTHX_ slab, o)
263 S_link_freed_op(pTHX_ OPSLAB *slab, OP *o) {
264 U16 sz = OpSLOT(o)->opslot_size;
265 U16 index = OPSLOT_SIZE_TO_INDEX(sz);
267 assert(sz >= OPSLOT_SIZE_BASE);
268 /* make sure the array is large enough to include ops this large */
269 if (!slab->opslab_freed) {
270 /* we don't have a free list array yet, make a new one */
271 slab->opslab_freed_size = index+1;
272 slab->opslab_freed = (OP**)PerlMemShared_calloc((slab->opslab_freed_size), sizeof(OP*));
274 if (!slab->opslab_freed)
277 else if (index >= slab->opslab_freed_size) {
278 /* It's probably not worth doing exponential expansion here, the number of op sizes
281 /* We already have a list that isn't large enough, expand it */
282 size_t newsize = index+1;
283 OP **p = (OP **)PerlMemShared_realloc(slab->opslab_freed, newsize * sizeof(OP*));
288 Zero(p+slab->opslab_freed_size, newsize - slab->opslab_freed_size, OP *);
290 slab->opslab_freed = p;
291 slab->opslab_freed_size = newsize;
294 o->op_next = slab->opslab_freed[index];
295 slab->opslab_freed[index] = o;
298 /* Returns a sz-sized block of memory (suitable for holding an op) from
299 * a free slot in the chain of op slabs attached to PL_compcv.
300 * Allocates a new slab if necessary.
301 * if PL_compcv isn't compiling, malloc() instead.
305 Perl_Slab_Alloc(pTHX_ size_t sz)
307 OPSLAB *head_slab; /* first slab in the chain */
313 /* We only allocate ops from the slab during subroutine compilation.
314 We find the slab via PL_compcv, hence that must be non-NULL. It could
315 also be pointing to a subroutine which is now fully set up (CvROOT()
316 pointing to the top of the optree for that sub), or a subroutine
317 which isn't using the slab allocator. If our sanity checks aren't met,
318 don't use a slab, but allocate the OP directly from the heap. */
319 if (!PL_compcv || CvROOT(PL_compcv)
320 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
322 o = (OP*)PerlMemShared_calloc(1, sz);
326 /* While the subroutine is under construction, the slabs are accessed via
327 CvSTART(), to avoid needing to expand PVCV by one pointer for something
328 unneeded at runtime. Once a subroutine is constructed, the slabs are
329 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
330 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
332 if (!CvSTART(PL_compcv)) {
334 (OP *)(head_slab = S_new_slab(aTHX_ NULL, PERL_SLAB_SIZE));
335 CvSLABBED_on(PL_compcv);
336 head_slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
338 else ++(head_slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
340 opsz = SIZE_TO_PSIZE(sz);
341 sz = opsz + OPSLOT_HEADER_P;
343 /* The head slab for each CV maintains a free list of OPs. In particular, constant folding
344 will free up OPs, so it makes sense to re-use them where possible. A
345 freed up slot is used in preference to a new allocation. */
346 if (head_slab->opslab_freed &&
347 OPSLOT_SIZE_TO_INDEX(sz) < head_slab->opslab_freed_size) {
350 /* look for a large enough size with any freed ops */
351 for (base_index = OPSLOT_SIZE_TO_INDEX(sz);
352 base_index < head_slab->opslab_freed_size && !head_slab->opslab_freed[base_index];
356 if (base_index < head_slab->opslab_freed_size) {
357 /* found a freed op */
358 o = head_slab->opslab_freed[base_index];
360 DEBUG_S_warn((aTHX_ "realloced op at %p, slab %p, head slab %p",
362 (I32**)OpSLOT(o) - OpSLOT(o)->opslot_offset,
364 head_slab->opslab_freed[base_index] = o->op_next;
365 Zero(o, opsz, I32 *);
371 #define INIT_OPSLOT(s) \
372 slot->opslot_offset = DIFF(slab2, slot) ; \
373 slot->opslot_size = s; \
374 slab2->opslab_free_space -= s; \
375 o = &slot->opslot_op; \
378 /* The partially-filled slab is next in the chain. */
379 slab2 = head_slab->opslab_next ? head_slab->opslab_next : head_slab;
380 if (slab2->opslab_free_space < sz) {
381 /* Remaining space is too small. */
382 /* If we can fit a BASEOP, add it to the free chain, so as not
384 if (slab2->opslab_free_space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
385 slot = &slab2->opslab_slots;
386 INIT_OPSLOT(slab2->opslab_free_space);
387 o->op_type = OP_FREED;
388 link_freed_op(head_slab, o);
391 /* Create a new slab. Make this one twice as big. */
392 slab2 = S_new_slab(aTHX_ head_slab,
393 slab2->opslab_size > PERL_MAX_SLAB_SIZE / 2
395 : slab2->opslab_size * 2);
396 slab2->opslab_next = head_slab->opslab_next;
397 head_slab->opslab_next = slab2;
399 assert(slab2->opslab_size >= sz);
401 /* Create a new op slot */
403 ((I32 **)&slab2->opslab_slots
404 + slab2->opslab_free_space - sz);
405 assert(slot >= &slab2->opslab_slots);
407 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p, head slab %p",
408 (void*)o, (void*)slab2, (void*)head_slab));
411 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
412 assert(!o->op_moresib);
413 assert(!o->op_sibparent);
420 #ifdef PERL_DEBUG_READONLY_OPS
422 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
424 PERL_ARGS_ASSERT_SLAB_TO_RO;
426 if (slab->opslab_readonly) return;
427 slab->opslab_readonly = 1;
428 for (; slab; slab = slab->opslab_next) {
429 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
430 (unsigned long) slab->opslab_size, slab));*/
431 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
432 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
433 (unsigned long)slab->opslab_size, errno);
438 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
442 PERL_ARGS_ASSERT_SLAB_TO_RW;
444 if (!slab->opslab_readonly) return;
446 for (; slab2; slab2 = slab2->opslab_next) {
447 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
448 (unsigned long) size, slab2));*/
449 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
450 PROT_READ|PROT_WRITE)) {
451 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
452 (unsigned long)slab2->opslab_size, errno);
455 slab->opslab_readonly = 0;
459 # define Slab_to_rw(op) NOOP
462 /* This cannot possibly be right, but it was copied from the old slab
463 allocator, to which it was originally added, without explanation, in
466 # define PerlMemShared PerlMem
469 /* make freed ops die if they're inadvertently executed */
474 DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
479 /* Return the block of memory used by an op to the free list of
480 * the OP slab associated with that op.
484 Perl_Slab_Free(pTHX_ void *op)
486 OP * const o = (OP *)op;
489 PERL_ARGS_ASSERT_SLAB_FREE;
492 o->op_ppaddr = S_pp_freed;
495 if (!o->op_slabbed) {
497 PerlMemShared_free(op);
502 /* If this op is already freed, our refcount will get screwy. */
503 assert(o->op_type != OP_FREED);
504 o->op_type = OP_FREED;
505 link_freed_op(slab, o);
506 DEBUG_S_warn((aTHX_ "freeing op at %p, slab %p, head slab %p",
508 (I32**)OpSLOT(o) - OpSLOT(o)->opslot_offset,
510 OpslabREFCNT_dec_padok(slab);
514 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
516 const bool havepad = !!PL_comppad;
517 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
520 PAD_SAVE_SETNULLPAD();
526 /* Free a chain of OP slabs. Should only be called after all ops contained
527 * in it have been freed. At this point, its reference count should be 1,
528 * because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1,
529 * and just directly calls opslab_free().
530 * (Note that the reference count which PL_compcv held on the slab should
531 * have been removed once compilation of the sub was complete).
537 Perl_opslab_free(pTHX_ OPSLAB *slab)
540 PERL_ARGS_ASSERT_OPSLAB_FREE;
542 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
543 assert(slab->opslab_refcnt == 1);
544 PerlMemShared_free(slab->opslab_freed);
546 slab2 = slab->opslab_next;
548 slab->opslab_refcnt = ~(size_t)0;
550 #ifdef PERL_DEBUG_READONLY_OPS
551 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
553 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
554 perror("munmap failed");
558 PerlMemShared_free(slab);
564 /* like opslab_free(), but first calls op_free() on any ops in the slab
565 * not marked as OP_FREED
569 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
573 size_t savestack_count = 0;
575 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
578 OPSLOT *slot = (OPSLOT*)
579 ((I32**)&slab2->opslab_slots + slab2->opslab_free_space);
580 OPSLOT *end = (OPSLOT*)
581 ((I32**)slab2 + slab2->opslab_size);
583 slot = (OPSLOT*) ((I32**)slot + slot->opslot_size) )
585 if (slot->opslot_op.op_type != OP_FREED
586 && !(slot->opslot_op.op_savefree
592 assert(slot->opslot_op.op_slabbed);
593 op_free(&slot->opslot_op);
594 if (slab->opslab_refcnt == 1) goto free;
597 } while ((slab2 = slab2->opslab_next));
598 /* > 1 because the CV still holds a reference count. */
599 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
601 assert(savestack_count == slab->opslab_refcnt-1);
603 /* Remove the CV’s reference count. */
604 slab->opslab_refcnt--;
611 #ifdef PERL_DEBUG_READONLY_OPS
613 Perl_op_refcnt_inc(pTHX_ OP *o)
616 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
617 if (slab && slab->opslab_readonly) {
630 Perl_op_refcnt_dec(pTHX_ OP *o)
633 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
635 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
637 if (slab && slab->opslab_readonly) {
639 result = --o->op_targ;
642 result = --o->op_targ;
648 * In the following definition, the ", (OP*)0" is just to make the compiler
649 * think the expression is of the right type: croak actually does a Siglongjmp.
651 #define CHECKOP(type,o) \
652 ((PL_op_mask && PL_op_mask[type]) \
653 ? ( op_free((OP*)o), \
654 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
656 : PL_check[type](aTHX_ (OP*)o))
658 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
660 #define OpTYPE_set(o,type) \
662 o->op_type = (OPCODE)type; \
663 o->op_ppaddr = PL_ppaddr[type]; \
667 S_no_fh_allowed(pTHX_ OP *o)
669 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
671 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
677 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
679 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
680 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
685 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
687 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
689 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
694 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
696 PERL_ARGS_ASSERT_BAD_TYPE_PV;
698 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
699 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
702 /* remove flags var, its unused in all callers, move to to right end since gv
703 and kid are always the same */
705 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
707 SV * const namesv = cv_name((CV *)gv, NULL, 0);
708 PERL_ARGS_ASSERT_BAD_TYPE_GV;
710 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
711 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
715 S_no_bareword_allowed(pTHX_ OP *o)
717 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
719 qerror(Perl_mess(aTHX_
720 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
722 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
725 /* "register" allocation */
728 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
731 const bool is_our = (PL_parser->in_my == KEY_our);
733 PERL_ARGS_ASSERT_ALLOCMY;
735 if (flags & ~SVf_UTF8)
736 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
739 /* complain about "my $<special_var>" etc etc */
743 || ( (flags & SVf_UTF8)
744 && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
745 || (name[1] == '_' && len > 2)))
747 const char * const type =
748 PL_parser->in_my == KEY_sigvar ? "subroutine signature" :
749 PL_parser->in_my == KEY_state ? "\"state\"" : "\"my\"";
751 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
753 && (!isPRINT(name[1]) || memCHRs("\t\n\r\f", name[1]))) {
754 /* diag_listed_as: Can't use global %s in %s */
755 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in %s",
756 name[0], toCTRL(name[1]),
757 (int)(len - 2), name + 2,
760 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in %s",
762 type), flags & SVf_UTF8);
766 /* allocate a spare slot and store the name in that slot */
768 off = pad_add_name_pvn(name, len,
769 (is_our ? padadd_OUR :
770 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
771 PL_parser->in_my_stash,
773 /* $_ is always in main::, even with our */
774 ? (PL_curstash && !memEQs(name,len,"$_")
780 /* anon sub prototypes contains state vars should always be cloned,
781 * otherwise the state var would be shared between anon subs */
783 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
784 CvCLONE_on(PL_compcv);
790 =head1 Optree Manipulation Functions
792 =for apidoc alloccopstash
794 Available only under threaded builds, this function allocates an entry in
795 C<PL_stashpad> for the stash passed to it.
802 Perl_alloccopstash(pTHX_ HV *hv)
804 PADOFFSET off = 0, o = 1;
805 bool found_slot = FALSE;
807 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
809 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
811 for (; o < PL_stashpadmax; ++o) {
812 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
813 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
814 found_slot = TRUE, off = o;
817 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
818 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
819 off = PL_stashpadmax;
820 PL_stashpadmax += 10;
823 PL_stashpad[PL_stashpadix = off] = hv;
828 /* free the body of an op without examining its contents.
829 * Always use this rather than FreeOp directly */
832 S_op_destroy(pTHX_ OP *o)
842 Free an op and its children. Only use this when an op is no longer linked
849 Perl_op_free(pTHX_ OP *o)
855 bool went_up = FALSE; /* whether we reached the current node by
856 following the parent pointer from a child, and
857 so have already seen this node */
859 if (!o || o->op_type == OP_FREED)
862 if (o->op_private & OPpREFCOUNTED) {
863 /* if base of tree is refcounted, just decrement */
864 switch (o->op_type) {
874 refcnt = OpREFCNT_dec(o);
877 /* Need to find and remove any pattern match ops from
878 * the list we maintain for reset(). */
879 find_and_forget_pmops(o);
892 /* free child ops before ourself, (then free ourself "on the
895 if (!went_up && o->op_flags & OPf_KIDS) {
896 next_op = cUNOPo->op_first;
900 /* find the next node to visit, *then* free the current node
901 * (can't rely on o->op_* fields being valid after o has been
904 /* The next node to visit will be either the sibling, or the
905 * parent if no siblings left, or NULL if we've worked our way
906 * back up to the top node in the tree */
907 next_op = (o == top_op) ? NULL : o->op_sibparent;
908 went_up = cBOOL(!OpHAS_SIBLING(o)); /* parents are already visited */
910 /* Now process the current node */
912 /* Though ops may be freed twice, freeing the op after its slab is a
914 assert(!o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
915 /* During the forced freeing of ops after compilation failure, kidops
916 may be freed before their parents. */
917 if (!o || o->op_type == OP_FREED)
922 /* an op should only ever acquire op_private flags that we know about.
923 * If this fails, you may need to fix something in regen/op_private.
924 * Don't bother testing if:
925 * * the op_ppaddr doesn't match the op; someone may have
926 * overridden the op and be doing strange things with it;
927 * * we've errored, as op flags are often left in an
928 * inconsistent state then. Note that an error when
929 * compiling the main program leaves PL_parser NULL, so
930 * we can't spot faults in the main code, only
931 * evaled/required code */
933 if ( o->op_ppaddr == PL_ppaddr[type]
935 && !PL_parser->error_count)
937 assert(!(o->op_private & ~PL_op_private_valid[type]));
942 /* Call the op_free hook if it has been set. Do it now so that it's called
943 * at the right time for refcounted ops, but still before all of the kids
948 type = (OPCODE)o->op_targ;
951 Slab_to_rw(OpSLAB(o));
953 /* COP* is not cleared by op_clear() so that we may track line
954 * numbers etc even after null() */
955 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
967 /* S_op_clear_gv(): free a GV attached to an OP */
971 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
973 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
977 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
978 || o->op_type == OP_MULTIDEREF)
981 ? ((GV*)PAD_SVl(*ixp)) : NULL;
983 ? (GV*)(*svp) : NULL;
985 /* It's possible during global destruction that the GV is freed
986 before the optree. Whilst the SvREFCNT_inc is happy to bump from
987 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
988 will trigger an assertion failure, because the entry to sv_clear
989 checks that the scalar is not already freed. A check of for
990 !SvIS_FREED(gv) turns out to be invalid, because during global
991 destruction the reference count can be forced down to zero
992 (with SVf_BREAK set). In which case raising to 1 and then
993 dropping to 0 triggers cleanup before it should happen. I
994 *think* that this might actually be a general, systematic,
995 weakness of the whole idea of SVf_BREAK, in that code *is*
996 allowed to raise and lower references during global destruction,
997 so any *valid* code that happens to do this during global
998 destruction might well trigger premature cleanup. */
999 bool still_valid = gv && SvREFCNT(gv);
1002 SvREFCNT_inc_simple_void(gv);
1005 pad_swipe(*ixp, TRUE);
1013 int try_downgrade = SvREFCNT(gv) == 2;
1014 SvREFCNT_dec_NN(gv);
1016 gv_try_downgrade(gv);
1022 Perl_op_clear(pTHX_ OP *o)
1027 PERL_ARGS_ASSERT_OP_CLEAR;
1029 switch (o->op_type) {
1030 case OP_NULL: /* Was holding old type, if any. */
1033 case OP_ENTEREVAL: /* Was holding hints. */
1034 case OP_ARGDEFELEM: /* Was holding signature index. */
1038 if (!(o->op_flags & OPf_REF) || !OP_IS_STAT(o->op_type))
1045 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
1047 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
1050 case OP_METHOD_REDIR:
1051 case OP_METHOD_REDIR_SUPER:
1053 if (cMETHOPx(o)->op_rclass_targ) {
1054 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
1055 cMETHOPx(o)->op_rclass_targ = 0;
1058 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
1059 cMETHOPx(o)->op_rclass_sv = NULL;
1062 case OP_METHOD_NAMED:
1063 case OP_METHOD_SUPER:
1064 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
1065 cMETHOPx(o)->op_u.op_meth_sv = NULL;
1068 pad_swipe(o->op_targ, 1);
1075 SvREFCNT_dec(cSVOPo->op_sv);
1076 cSVOPo->op_sv = NULL;
1079 Even if op_clear does a pad_free for the target of the op,
1080 pad_free doesn't actually remove the sv that exists in the pad;
1081 instead it lives on. This results in that it could be reused as
1082 a target later on when the pad was reallocated.
1085 pad_swipe(o->op_targ,1);
1095 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
1100 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
1101 && (o->op_private & OPpTRANS_USE_SVOP))
1104 if (cPADOPo->op_padix > 0) {
1105 pad_swipe(cPADOPo->op_padix, TRUE);
1106 cPADOPo->op_padix = 0;
1109 SvREFCNT_dec(cSVOPo->op_sv);
1110 cSVOPo->op_sv = NULL;
1114 PerlMemShared_free(cPVOPo->op_pv);
1115 cPVOPo->op_pv = NULL;
1119 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1123 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1124 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1126 if (o->op_private & OPpSPLIT_LEX)
1127 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1130 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1132 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1139 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1140 op_free(cPMOPo->op_code_list);
1141 cPMOPo->op_code_list = NULL;
1142 forget_pmop(cPMOPo);
1143 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1144 /* we use the same protection as the "SAFE" version of the PM_ macros
1145 * here since sv_clean_all might release some PMOPs
1146 * after PL_regex_padav has been cleared
1147 * and the clearing of PL_regex_padav needs to
1148 * happen before sv_clean_all
1151 if(PL_regex_pad) { /* We could be in destruction */
1152 const IV offset = (cPMOPo)->op_pmoffset;
1153 ReREFCNT_dec(PM_GETRE(cPMOPo));
1154 PL_regex_pad[offset] = &PL_sv_undef;
1155 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1159 ReREFCNT_dec(PM_GETRE(cPMOPo));
1160 PM_SETRE(cPMOPo, NULL);
1166 PerlMemShared_free(cUNOP_AUXo->op_aux);
1169 case OP_MULTICONCAT:
1171 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux;
1172 /* aux[PERL_MULTICONCAT_IX_PLAIN_PV] and/or
1173 * aux[PERL_MULTICONCAT_IX_UTF8_PV] point to plain and/or
1174 * utf8 shared strings */
1175 char *p1 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
1176 char *p2 = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
1178 PerlMemShared_free(p1);
1180 PerlMemShared_free(p2);
1181 PerlMemShared_free(aux);
1187 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1188 UV actions = items->uv;
1190 bool is_hash = FALSE;
1193 switch (actions & MDEREF_ACTION_MASK) {
1196 actions = (++items)->uv;
1199 case MDEREF_HV_padhv_helem:
1202 case MDEREF_AV_padav_aelem:
1203 pad_free((++items)->pad_offset);
1206 case MDEREF_HV_gvhv_helem:
1209 case MDEREF_AV_gvav_aelem:
1211 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1213 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1217 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1220 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1222 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1224 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1226 goto do_vivify_rv2xv_elem;
1228 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1231 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1232 pad_free((++items)->pad_offset);
1233 goto do_vivify_rv2xv_elem;
1235 case MDEREF_HV_pop_rv2hv_helem:
1236 case MDEREF_HV_vivify_rv2hv_helem:
1239 do_vivify_rv2xv_elem:
1240 case MDEREF_AV_pop_rv2av_aelem:
1241 case MDEREF_AV_vivify_rv2av_aelem:
1243 switch (actions & MDEREF_INDEX_MASK) {
1244 case MDEREF_INDEX_none:
1247 case MDEREF_INDEX_const:
1251 pad_swipe((++items)->pad_offset, 1);
1253 SvREFCNT_dec((++items)->sv);
1259 case MDEREF_INDEX_padsv:
1260 pad_free((++items)->pad_offset);
1262 case MDEREF_INDEX_gvsv:
1264 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1266 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1271 if (actions & MDEREF_FLAG_last)
1284 actions >>= MDEREF_SHIFT;
1287 /* start of malloc is at op_aux[-1], where the length is
1289 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1294 if (o->op_targ > 0) {
1295 pad_free(o->op_targ);
1301 S_cop_free(pTHX_ COP* cop)
1303 PERL_ARGS_ASSERT_COP_FREE;
1306 if (! specialWARN(cop->cop_warnings))
1307 PerlMemShared_free(cop->cop_warnings);
1308 cophh_free(CopHINTHASH_get(cop));
1309 if (PL_curcop == cop)
1314 S_forget_pmop(pTHX_ PMOP *const o)
1316 HV * const pmstash = PmopSTASH(o);
1318 PERL_ARGS_ASSERT_FORGET_PMOP;
1320 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1321 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1323 PMOP **const array = (PMOP**) mg->mg_ptr;
1324 U32 count = mg->mg_len / sizeof(PMOP**);
1328 if (array[i] == o) {
1329 /* Found it. Move the entry at the end to overwrite it. */
1330 array[i] = array[--count];
1331 mg->mg_len = count * sizeof(PMOP**);
1332 /* Could realloc smaller at this point always, but probably
1333 not worth it. Probably worth free()ing if we're the
1336 Safefree(mg->mg_ptr);
1350 S_find_and_forget_pmops(pTHX_ OP *o)
1354 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1357 switch (o->op_type) {
1362 forget_pmop((PMOP*)o);
1365 if (o->op_flags & OPf_KIDS) {
1366 o = cUNOPo->op_first;
1372 return; /* at top; no parents/siblings to try */
1373 if (OpHAS_SIBLING(o)) {
1374 o = o->op_sibparent; /* process next sibling */
1377 o = o->op_sibparent; /*try parent's next sibling */
1386 Neutralizes an op when it is no longer needed, but is still linked to from
1393 Perl_op_null(pTHX_ OP *o)
1397 PERL_ARGS_ASSERT_OP_NULL;
1399 if (o->op_type == OP_NULL)
1402 o->op_targ = o->op_type;
1403 OpTYPE_set(o, OP_NULL);
1407 Perl_op_refcnt_lock(pTHX)
1408 PERL_TSA_ACQUIRE(PL_op_mutex)
1413 PERL_UNUSED_CONTEXT;
1418 Perl_op_refcnt_unlock(pTHX)
1419 PERL_TSA_RELEASE(PL_op_mutex)
1424 PERL_UNUSED_CONTEXT;
1430 =for apidoc op_sibling_splice
1432 A general function for editing the structure of an existing chain of
1433 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1434 you to delete zero or more sequential nodes, replacing them with zero or
1435 more different nodes. Performs the necessary op_first/op_last
1436 housekeeping on the parent node and op_sibling manipulation on the
1437 children. The last deleted node will be marked as as the last node by
1438 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1440 Note that op_next is not manipulated, and nodes are not freed; that is the
1441 responsibility of the caller. It also won't create a new list op for an
1442 empty list etc; use higher-level functions like op_append_elem() for that.
1444 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1445 the splicing doesn't affect the first or last op in the chain.
1447 C<start> is the node preceding the first node to be spliced. Node(s)
1448 following it will be deleted, and ops will be inserted after it. If it is
1449 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1452 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1453 If -1 or greater than or equal to the number of remaining kids, all
1454 remaining kids are deleted.
1456 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1457 If C<NULL>, no nodes are inserted.
1459 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1464 action before after returns
1465 ------ ----- ----- -------
1468 splice(P, A, 2, X-Y-Z) | | B-C
1472 splice(P, NULL, 1, X-Y) | | A
1476 splice(P, NULL, 3, NULL) | | A-B-C
1480 splice(P, B, 0, X-Y) | | NULL
1484 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1485 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1491 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1495 OP *last_del = NULL;
1496 OP *last_ins = NULL;
1499 first = OpSIBLING(start);
1503 first = cLISTOPx(parent)->op_first;
1505 assert(del_count >= -1);
1507 if (del_count && first) {
1509 while (--del_count && OpHAS_SIBLING(last_del))
1510 last_del = OpSIBLING(last_del);
1511 rest = OpSIBLING(last_del);
1512 OpLASTSIB_set(last_del, NULL);
1519 while (OpHAS_SIBLING(last_ins))
1520 last_ins = OpSIBLING(last_ins);
1521 OpMAYBESIB_set(last_ins, rest, NULL);
1527 OpMAYBESIB_set(start, insert, NULL);
1531 cLISTOPx(parent)->op_first = insert;
1533 parent->op_flags |= OPf_KIDS;
1535 parent->op_flags &= ~OPf_KIDS;
1539 /* update op_last etc */
1546 /* ought to use OP_CLASS(parent) here, but that can't handle
1547 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1549 type = parent->op_type;
1550 if (type == OP_CUSTOM) {
1552 type = XopENTRYCUSTOM(parent, xop_class);
1555 if (type == OP_NULL)
1556 type = parent->op_targ;
1557 type = PL_opargs[type] & OA_CLASS_MASK;
1560 lastop = last_ins ? last_ins : start ? start : NULL;
1561 if ( type == OA_BINOP
1562 || type == OA_LISTOP
1566 cLISTOPx(parent)->op_last = lastop;
1569 OpLASTSIB_set(lastop, parent);
1571 return last_del ? first : NULL;
1574 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1578 =for apidoc op_parent
1580 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1586 Perl_op_parent(OP *o)
1588 PERL_ARGS_ASSERT_OP_PARENT;
1589 while (OpHAS_SIBLING(o))
1591 return o->op_sibparent;
1594 /* replace the sibling following start with a new UNOP, which becomes
1595 * the parent of the original sibling; e.g.
1597 * op_sibling_newUNOP(P, A, unop-args...)
1605 * where U is the new UNOP.
1607 * parent and start args are the same as for op_sibling_splice();
1608 * type and flags args are as newUNOP().
1610 * Returns the new UNOP.
1614 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1618 kid = op_sibling_splice(parent, start, 1, NULL);
1619 newop = newUNOP(type, flags, kid);
1620 op_sibling_splice(parent, start, 0, newop);
1625 /* lowest-level newLOGOP-style function - just allocates and populates
1626 * the struct. Higher-level stuff should be done by S_new_logop() /
1627 * newLOGOP(). This function exists mainly to avoid op_first assignment
1628 * being spread throughout this file.
1632 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1637 NewOp(1101, logop, 1, LOGOP);
1638 OpTYPE_set(logop, type);
1639 logop->op_first = first;
1640 logop->op_other = other;
1642 logop->op_flags = OPf_KIDS;
1643 while (kid && OpHAS_SIBLING(kid))
1644 kid = OpSIBLING(kid);
1646 OpLASTSIB_set(kid, (OP*)logop);
1651 /* Contextualizers */
1654 =for apidoc op_contextualize
1656 Applies a syntactic context to an op tree representing an expression.
1657 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1658 or C<G_VOID> to specify the context to apply. The modified op tree
1665 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1667 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1669 case G_SCALAR: return scalar(o);
1670 case G_ARRAY: return list(o);
1671 case G_VOID: return scalarvoid(o);
1673 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1680 =for apidoc op_linklist
1681 This function is the implementation of the L</LINKLIST> macro. It should
1682 not be called directly.
1689 Perl_op_linklist(pTHX_ OP *o)
1696 PERL_ARGS_ASSERT_OP_LINKLIST;
1699 /* Descend down the tree looking for any unprocessed subtrees to
1702 if (o->op_flags & OPf_KIDS) {
1703 o = cUNOPo->op_first;
1706 o->op_next = o; /* leaf node; link to self initially */
1709 /* if we're at the top level, there either weren't any children
1710 * to process, or we've worked our way back to the top. */
1714 /* o is now processed. Next, process any sibling subtrees */
1716 if (OpHAS_SIBLING(o)) {
1721 /* Done all the subtrees at this level. Go back up a level and
1722 * link the parent in with all its (processed) children.
1725 o = o->op_sibparent;
1726 assert(!o->op_next);
1727 prevp = &(o->op_next);
1728 kid = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1730 *prevp = kid->op_next;
1731 prevp = &(kid->op_next);
1732 kid = OpSIBLING(kid);
1740 S_scalarkids(pTHX_ OP *o)
1742 if (o && o->op_flags & OPf_KIDS) {
1744 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1751 S_scalarboolean(pTHX_ OP *o)
1753 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1755 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1756 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1757 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1758 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1759 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1760 if (ckWARN(WARN_SYNTAX)) {
1761 const line_t oldline = CopLINE(PL_curcop);
1763 if (PL_parser && PL_parser->copline != NOLINE) {
1764 /* This ensures that warnings are reported at the first line
1765 of the conditional, not the last. */
1766 CopLINE_set(PL_curcop, PL_parser->copline);
1768 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1769 CopLINE_set(PL_curcop, oldline);
1776 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1779 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1780 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1782 const char funny = o->op_type == OP_PADAV
1783 || o->op_type == OP_RV2AV ? '@' : '%';
1784 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1786 if (cUNOPo->op_first->op_type != OP_GV
1787 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1789 return varname(gv, funny, 0, NULL, 0, subscript_type);
1792 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1797 S_op_varname(pTHX_ const OP *o)
1799 return S_op_varname_subscript(aTHX_ o, 1);
1803 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1804 { /* or not so pretty :-) */
1805 if (o->op_type == OP_CONST) {
1807 if (SvPOK(*retsv)) {
1809 *retsv = sv_newmortal();
1810 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1811 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1813 else if (!SvOK(*retsv))
1816 else *retpv = "...";
1820 S_scalar_slice_warning(pTHX_ const OP *o)
1823 const bool h = o->op_type == OP_HSLICE
1824 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
1830 SV *keysv = NULL; /* just to silence compiler warnings */
1831 const char *key = NULL;
1833 if (!(o->op_private & OPpSLICEWARNING))
1835 if (PL_parser && PL_parser->error_count)
1836 /* This warning can be nonsensical when there is a syntax error. */
1839 kid = cLISTOPo->op_first;
1840 kid = OpSIBLING(kid); /* get past pushmark */
1841 /* weed out false positives: any ops that can return lists */
1842 switch (kid->op_type) {
1868 /* Don't warn if we have a nulled list either. */
1869 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1872 assert(OpSIBLING(kid));
1873 name = S_op_varname(aTHX_ OpSIBLING(kid));
1874 if (!name) /* XS module fiddling with the op tree */
1876 S_op_pretty(aTHX_ kid, &keysv, &key);
1877 assert(SvPOK(name));
1878 sv_chop(name,SvPVX(name)+1);
1880 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1881 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1882 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1884 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1885 lbrack, key, rbrack);
1887 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1888 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1889 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1891 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1892 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1897 /* apply scalar context to the o subtree */
1900 Perl_scalar(pTHX_ OP *o)
1905 OP *next_kid = NULL; /* what op (if any) to process next */
1908 /* assumes no premature commitment */
1909 if (!o || (PL_parser && PL_parser->error_count)
1910 || (o->op_flags & OPf_WANT)
1911 || o->op_type == OP_RETURN)
1916 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1918 switch (o->op_type) {
1920 scalar(cBINOPo->op_first);
1921 /* convert what initially looked like a list repeat into a
1922 * scalar repeat, e.g. $s = (1) x $n
1924 if (o->op_private & OPpREPEAT_DOLIST) {
1925 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1926 assert(kid->op_type == OP_PUSHMARK);
1927 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1928 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1929 o->op_private &=~ OPpREPEAT_DOLIST;
1937 /* impose scalar context on everything except the condition */
1938 next_kid = OpSIBLING(cUNOPo->op_first);
1942 if (o->op_flags & OPf_KIDS)
1943 next_kid = cUNOPo->op_first; /* do all kids */
1946 /* the children of these ops are usually a list of statements,
1947 * except the leaves, whose first child is a corresponding enter
1952 kid = cLISTOPo->op_first;
1956 kid = cLISTOPo->op_first;
1958 kid = OpSIBLING(kid);
1961 OP *sib = OpSIBLING(kid);
1962 /* Apply void context to all kids except the last, which
1963 * is scalar (ignoring a trailing ex-nextstate in determining
1964 * if it's the last kid). E.g.
1965 * $scalar = do { void; void; scalar }
1966 * Except that 'when's are always scalar, e.g.
1967 * $scalar = do { given(..) {
1968 * when (..) { scalar }
1969 * when (..) { scalar }
1974 || ( !OpHAS_SIBLING(sib)
1975 && sib->op_type == OP_NULL
1976 && ( sib->op_targ == OP_NEXTSTATE
1977 || sib->op_targ == OP_DBSTATE )
1981 /* tail call optimise calling scalar() on the last kid */
1985 else if (kid->op_type == OP_LEAVEWHEN)
1991 NOT_REACHED; /* NOTREACHED */
1995 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
2001 /* Warn about scalar context */
2002 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
2003 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
2006 const char *key = NULL;
2008 /* This warning can be nonsensical when there is a syntax error. */
2009 if (PL_parser && PL_parser->error_count)
2012 if (!ckWARN(WARN_SYNTAX)) break;
2014 kid = cLISTOPo->op_first;
2015 kid = OpSIBLING(kid); /* get past pushmark */
2016 assert(OpSIBLING(kid));
2017 name = S_op_varname(aTHX_ OpSIBLING(kid));
2018 if (!name) /* XS module fiddling with the op tree */
2020 S_op_pretty(aTHX_ kid, &keysv, &key);
2021 assert(SvPOK(name));
2022 sv_chop(name,SvPVX(name)+1);
2024 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
2025 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2026 "%%%" SVf "%c%s%c in scalar context better written "
2027 "as $%" SVf "%c%s%c",
2028 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
2029 lbrack, key, rbrack);
2031 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
2032 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2033 "%%%" SVf "%c%" SVf "%c in scalar context better "
2034 "written as $%" SVf "%c%" SVf "%c",
2035 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
2036 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
2040 /* If next_kid is set, someone in the code above wanted us to process
2041 * that kid and all its remaining siblings. Otherwise, work our way
2042 * back up the tree */
2046 return top_op; /* at top; no parents/siblings to try */
2047 if (OpHAS_SIBLING(o))
2048 next_kid = o->op_sibparent;
2050 o = o->op_sibparent; /*try parent's next sibling */
2051 switch (o->op_type) {
2057 /* should really restore PL_curcop to its old value, but
2058 * setting it to PL_compiling is better than do nothing */
2059 PL_curcop = &PL_compiling;
2068 /* apply void context to the optree arg */
2071 Perl_scalarvoid(pTHX_ OP *arg)
2078 PERL_ARGS_ASSERT_SCALARVOID;
2082 SV *useless_sv = NULL;
2083 const char* useless = NULL;
2084 OP * next_kid = NULL;
2086 if (o->op_type == OP_NEXTSTATE
2087 || o->op_type == OP_DBSTATE
2088 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
2089 || o->op_targ == OP_DBSTATE)))
2090 PL_curcop = (COP*)o; /* for warning below */
2092 /* assumes no premature commitment */
2093 want = o->op_flags & OPf_WANT;
2094 if ((want && want != OPf_WANT_SCALAR)
2095 || (PL_parser && PL_parser->error_count)
2096 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
2101 if ((o->op_private & OPpTARGET_MY)
2102 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2104 /* newASSIGNOP has already applied scalar context, which we
2105 leave, as if this op is inside SASSIGN. */
2109 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
2111 switch (o->op_type) {
2113 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
2117 if (o->op_flags & OPf_STACKED)
2119 if (o->op_type == OP_REPEAT)
2120 scalar(cBINOPo->op_first);
2123 if ((o->op_flags & OPf_STACKED) &&
2124 !(o->op_private & OPpCONCAT_NESTED))
2128 if (o->op_private == 4)
2163 case OP_GETSOCKNAME:
2164 case OP_GETPEERNAME:
2169 case OP_GETPRIORITY:
2194 useless = OP_DESC(o);
2204 case OP_AELEMFAST_LEX:
2208 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
2209 /* Otherwise it's "Useless use of grep iterator" */
2210 useless = OP_DESC(o);
2214 if (!(o->op_private & OPpSPLIT_ASSIGN))
2215 useless = OP_DESC(o);
2219 kid = cUNOPo->op_first;
2220 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2221 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2224 useless = "negative pattern binding (!~)";
2228 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2229 useless = "non-destructive substitution (s///r)";
2233 useless = "non-destructive transliteration (tr///r)";
2240 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2241 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2242 useless = "a variable";
2247 if (cSVOPo->op_private & OPpCONST_STRICT)
2248 no_bareword_allowed(o);
2250 if (ckWARN(WARN_VOID)) {
2252 /* don't warn on optimised away booleans, eg
2253 * use constant Foo, 5; Foo || print; */
2254 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2256 /* the constants 0 and 1 are permitted as they are
2257 conventionally used as dummies in constructs like
2258 1 while some_condition_with_side_effects; */
2259 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2261 else if (SvPOK(sv)) {
2262 SV * const dsv = newSVpvs("");
2264 = Perl_newSVpvf(aTHX_
2266 pv_pretty(dsv, SvPVX_const(sv),
2267 SvCUR(sv), 32, NULL, NULL,
2269 | PERL_PV_ESCAPE_NOCLEAR
2270 | PERL_PV_ESCAPE_UNI_DETECT));
2271 SvREFCNT_dec_NN(dsv);
2273 else if (SvOK(sv)) {
2274 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2277 useless = "a constant (undef)";
2280 op_null(o); /* don't execute or even remember it */
2284 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2288 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2292 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2296 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2301 UNOP *refgen, *rv2cv;
2304 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2307 rv2gv = ((BINOP *)o)->op_last;
2308 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2311 refgen = (UNOP *)((BINOP *)o)->op_first;
2313 if (!refgen || (refgen->op_type != OP_REFGEN
2314 && refgen->op_type != OP_SREFGEN))
2317 exlist = (LISTOP *)refgen->op_first;
2318 if (!exlist || exlist->op_type != OP_NULL
2319 || exlist->op_targ != OP_LIST)
2322 if (exlist->op_first->op_type != OP_PUSHMARK
2323 && exlist->op_first != exlist->op_last)
2326 rv2cv = (UNOP*)exlist->op_last;
2328 if (rv2cv->op_type != OP_RV2CV)
2331 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2332 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2333 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2335 o->op_private |= OPpASSIGN_CV_TO_GV;
2336 rv2gv->op_private |= OPpDONT_INIT_GV;
2337 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2349 kid = cLOGOPo->op_first;
2350 if (kid->op_type == OP_NOT
2351 && (kid->op_flags & OPf_KIDS)) {
2352 if (o->op_type == OP_AND) {
2353 OpTYPE_set(o, OP_OR);
2355 OpTYPE_set(o, OP_AND);
2365 next_kid = OpSIBLING(cUNOPo->op_first);
2369 if (o->op_flags & OPf_STACKED)
2376 if (!(o->op_flags & OPf_KIDS))
2387 next_kid = cLISTOPo->op_first;
2390 /* If the first kid after pushmark is something that the padrange
2391 optimisation would reject, then null the list and the pushmark.
2393 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2394 && ( !(kid = OpSIBLING(kid))
2395 || ( kid->op_type != OP_PADSV
2396 && kid->op_type != OP_PADAV
2397 && kid->op_type != OP_PADHV)
2398 || kid->op_private & ~OPpLVAL_INTRO
2399 || !(kid = OpSIBLING(kid))
2400 || ( kid->op_type != OP_PADSV
2401 && kid->op_type != OP_PADAV
2402 && kid->op_type != OP_PADHV)
2403 || kid->op_private & ~OPpLVAL_INTRO)
2405 op_null(cUNOPo->op_first); /* NULL the pushmark */
2406 op_null(o); /* NULL the list */
2418 /* mortalise it, in case warnings are fatal. */
2419 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2420 "Useless use of %" SVf " in void context",
2421 SVfARG(sv_2mortal(useless_sv)));
2424 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2425 "Useless use of %s in void context",
2430 /* if a kid hasn't been nominated to process, continue with the
2431 * next sibling, or if no siblings left, go back to the parent's
2432 * siblings and so on
2436 return arg; /* at top; no parents/siblings to try */
2437 if (OpHAS_SIBLING(o))
2438 next_kid = o->op_sibparent;
2440 o = o->op_sibparent; /*try parent's next sibling */
2450 S_listkids(pTHX_ OP *o)
2452 if (o && o->op_flags & OPf_KIDS) {
2454 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2461 /* apply list context to the o subtree */
2464 Perl_list(pTHX_ OP *o)
2469 OP *next_kid = NULL; /* what op (if any) to process next */
2473 /* assumes no premature commitment */
2474 if (!o || (o->op_flags & OPf_WANT)
2475 || (PL_parser && PL_parser->error_count)
2476 || o->op_type == OP_RETURN)
2481 if ((o->op_private & OPpTARGET_MY)
2482 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2484 goto do_next; /* As if inside SASSIGN */
2487 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2489 switch (o->op_type) {
2491 if (o->op_private & OPpREPEAT_DOLIST
2492 && !(o->op_flags & OPf_STACKED))
2494 list(cBINOPo->op_first);
2495 kid = cBINOPo->op_last;
2496 /* optimise away (.....) x 1 */
2497 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2498 && SvIVX(kSVOP_sv) == 1)
2500 op_null(o); /* repeat */
2501 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2503 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2511 /* impose list context on everything except the condition */
2512 next_kid = OpSIBLING(cUNOPo->op_first);
2516 if (!(o->op_flags & OPf_KIDS))
2518 /* possibly flatten 1..10 into a constant array */
2519 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2520 list(cBINOPo->op_first);
2521 gen_constant_list(o);
2524 next_kid = cUNOPo->op_first; /* do all kids */
2528 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2529 op_null(cUNOPo->op_first); /* NULL the pushmark */
2530 op_null(o); /* NULL the list */
2532 if (o->op_flags & OPf_KIDS)
2533 next_kid = cUNOPo->op_first; /* do all kids */
2536 /* the children of these ops are usually a list of statements,
2537 * except the leaves, whose first child is a corresponding enter
2541 kid = cLISTOPo->op_first;
2545 kid = cLISTOPo->op_first;
2547 kid = OpSIBLING(kid);
2550 OP *sib = OpSIBLING(kid);
2551 /* Apply void context to all kids except the last, which
2553 * @a = do { void; void; list }
2554 * Except that 'when's are always list context, e.g.
2555 * @a = do { given(..) {
2556 * when (..) { list }
2557 * when (..) { list }
2562 /* tail call optimise calling list() on the last kid */
2566 else if (kid->op_type == OP_LEAVEWHEN)
2572 NOT_REACHED; /* NOTREACHED */
2577 /* If next_kid is set, someone in the code above wanted us to process
2578 * that kid and all its remaining siblings. Otherwise, work our way
2579 * back up the tree */
2583 return top_op; /* at top; no parents/siblings to try */
2584 if (OpHAS_SIBLING(o))
2585 next_kid = o->op_sibparent;
2587 o = o->op_sibparent; /*try parent's next sibling */
2588 switch (o->op_type) {
2594 /* should really restore PL_curcop to its old value, but
2595 * setting it to PL_compiling is better than do nothing */
2596 PL_curcop = &PL_compiling;
2608 S_scalarseq(pTHX_ OP *o)
2611 const OPCODE type = o->op_type;
2613 if (type == OP_LINESEQ || type == OP_SCOPE ||
2614 type == OP_LEAVE || type == OP_LEAVETRY)
2617 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2618 if ((sib = OpSIBLING(kid))
2619 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2620 || ( sib->op_targ != OP_NEXTSTATE
2621 && sib->op_targ != OP_DBSTATE )))
2626 PL_curcop = &PL_compiling;
2628 o->op_flags &= ~OPf_PARENS;
2629 if (PL_hints & HINT_BLOCK_SCOPE)
2630 o->op_flags |= OPf_PARENS;
2633 o = newOP(OP_STUB, 0);
2638 S_modkids(pTHX_ OP *o, I32 type)
2640 if (o && o->op_flags & OPf_KIDS) {
2642 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2643 op_lvalue(kid, type);
2649 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2650 * const fields. Also, convert CONST keys to HEK-in-SVs.
2651 * rop is the op that retrieves the hash;
2652 * key_op is the first key
2653 * real if false, only check (and possibly croak); don't update op
2657 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real)
2663 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2665 if (rop->op_first->op_type == OP_PADSV)
2666 /* @$hash{qw(keys here)} */
2667 rop = (UNOP*)rop->op_first;
2669 /* @{$hash}{qw(keys here)} */
2670 if (rop->op_first->op_type == OP_SCOPE
2671 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2673 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2680 lexname = NULL; /* just to silence compiler warnings */
2681 fields = NULL; /* just to silence compiler warnings */
2685 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2686 SvPAD_TYPED(lexname))
2687 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2688 && isGV(*fields) && GvHV(*fields);
2690 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2692 if (key_op->op_type != OP_CONST)
2694 svp = cSVOPx_svp(key_op);
2696 /* make sure it's not a bareword under strict subs */
2697 if (key_op->op_private & OPpCONST_BARE &&
2698 key_op->op_private & OPpCONST_STRICT)
2700 no_bareword_allowed((OP*)key_op);
2703 /* Make the CONST have a shared SV */
2704 if ( !SvIsCOW_shared_hash(sv = *svp)
2705 && SvTYPE(sv) < SVt_PVMG
2711 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2712 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2713 SvREFCNT_dec_NN(sv);
2718 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2720 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2721 "in variable %" PNf " of type %" HEKf,
2722 SVfARG(*svp), PNfARG(lexname),
2723 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2728 /* info returned by S_sprintf_is_multiconcatable() */
2730 struct sprintf_ismc_info {
2731 SSize_t nargs; /* num of args to sprintf (not including the format) */
2732 char *start; /* start of raw format string */
2733 char *end; /* bytes after end of raw format string */
2734 STRLEN total_len; /* total length (in bytes) of format string, not
2735 including '%s' and half of '%%' */
2736 STRLEN variant; /* number of bytes by which total_len_p would grow
2737 if upgraded to utf8 */
2738 bool utf8; /* whether the format is utf8 */
2742 /* is the OP_SPRINTF o suitable for converting into a multiconcat op?
2743 * i.e. its format argument is a const string with only '%s' and '%%'
2744 * formats, and the number of args is known, e.g.
2745 * sprintf "a=%s f=%s", $a[0], scalar(f());
2747 * sprintf "i=%d a=%s f=%s", $i, @a, f();
2749 * If successful, the sprintf_ismc_info struct pointed to by info will be
2754 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info)
2756 OP *pm, *constop, *kid;
2759 SSize_t nargs, nformats;
2760 STRLEN cur, total_len, variant;
2763 /* if sprintf's behaviour changes, die here so that someone
2764 * can decide whether to enhance this function or skip optimising
2765 * under those new circumstances */
2766 assert(!(o->op_flags & OPf_STACKED));
2767 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX));
2768 assert(!(o->op_private & ~OPpARG4_MASK));
2770 pm = cUNOPo->op_first;
2771 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */
2773 constop = OpSIBLING(pm);
2774 if (!constop || constop->op_type != OP_CONST)
2776 sv = cSVOPx_sv(constop);
2777 if (SvMAGICAL(sv) || !SvPOK(sv))
2783 /* Scan format for %% and %s and work out how many %s there are.
2784 * Abandon if other format types are found.
2791 for (p = s; p < e; p++) {
2794 if (!UTF8_IS_INVARIANT(*p))
2800 return FALSE; /* lone % at end gives "Invalid conversion" */
2809 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG)
2812 utf8 = cBOOL(SvUTF8(sv));
2816 /* scan args; they must all be in scalar cxt */
2819 kid = OpSIBLING(constop);
2822 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
2825 kid = OpSIBLING(kid);
2828 if (nargs != nformats)
2829 return FALSE; /* e.g. sprintf("%s%s", $a); */
2832 info->nargs = nargs;
2835 info->total_len = total_len;
2836 info->variant = variant;
2844 /* S_maybe_multiconcat():
2846 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly
2847 * convert it (and its children) into an OP_MULTICONCAT. See the code
2848 * comments just before pp_multiconcat() for the full details of what
2849 * OP_MULTICONCAT supports.
2851 * Basically we're looking for an optree with a chain of OP_CONCATS down
2852 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or
2853 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g.
2861 * STRINGIFY -- PADSV[$x]
2864 * ex-PUSHMARK -- CONCAT/S
2866 * CONCAT/S -- PADSV[$d]
2868 * CONCAT -- CONST["-"]
2870 * PADSV[$a] -- PADSV[$b]
2872 * Note that at this stage the OP_SASSIGN may have already been optimised
2873 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT.
2877 S_maybe_multiconcat(pTHX_ OP *o)
2880 OP *lastkidop; /* the right-most of any kids unshifted onto o */
2881 OP *topop; /* the top-most op in the concat tree (often equals o,
2882 unless there are assign/stringify ops above it */
2883 OP *parentop; /* the parent op of topop (or itself if no parent) */
2884 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */
2885 OP *targetop; /* the op corresponding to target=... or target.=... */
2886 OP *stringop; /* the OP_STRINGIFY op, if any */
2887 OP *nextop; /* used for recreating the op_next chain without consts */
2888 OP *kid; /* general-purpose op pointer */
2890 UNOP_AUX_item *lenp;
2891 char *const_str, *p;
2892 struct sprintf_ismc_info sprintf_info;
2894 /* store info about each arg in args[];
2895 * toparg is the highest used slot; argp is a general
2896 * pointer to args[] slots */
2898 void *p; /* initially points to const sv (or null for op);
2899 later, set to SvPV(constsv), with ... */
2900 STRLEN len; /* ... len set to SvPV(..., len) */
2901 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1];
2905 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */
2908 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op;
2909 the last-processed arg will the LHS of one,
2910 as args are processed in reverse order */
2911 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */
2912 STRLEN total_len = 0; /* sum of the lengths of the const segments */
2913 U8 flags = 0; /* what will become the op_flags and ... */
2914 U8 private_flags = 0; /* ... op_private of the multiconcat op */
2915 bool is_sprintf = FALSE; /* we're optimising an sprintf */
2916 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */
2917 bool prev_was_const = FALSE; /* previous arg was a const */
2919 /* -----------------------------------------------------------------
2922 * Examine the optree non-destructively to determine whether it's
2923 * suitable to be converted into an OP_MULTICONCAT. Accumulate
2924 * information about the optree in args[].
2934 assert( o->op_type == OP_SASSIGN
2935 || o->op_type == OP_CONCAT
2936 || o->op_type == OP_SPRINTF
2937 || o->op_type == OP_STRINGIFY);
2939 Zero(&sprintf_info, 1, struct sprintf_ismc_info);
2941 /* first see if, at the top of the tree, there is an assign,
2942 * append and/or stringify */
2944 if (topop->op_type == OP_SASSIGN) {
2946 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN])
2948 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
2950 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */
2953 topop = cBINOPo->op_first;
2954 targetop = OpSIBLING(topop);
2955 if (!targetop) /* probably some sort of syntax error */
2958 /* don't optimise away assign in 'local $foo = ....' */
2959 if ( (targetop->op_private & OPpLVAL_INTRO)
2960 /* these are the common ops which do 'local', but
2962 && ( targetop->op_type == OP_GVSV
2963 || targetop->op_type == OP_RV2SV
2964 || targetop->op_type == OP_AELEM
2965 || targetop->op_type == OP_HELEM
2970 else if ( topop->op_type == OP_CONCAT
2971 && (topop->op_flags & OPf_STACKED)
2972 && (!(topop->op_private & OPpCONCAT_NESTED))
2977 /* OPpTARGET_MY shouldn't be able to be set here. If it is,
2978 * decide what to do about it */
2979 assert(!(o->op_private & OPpTARGET_MY));
2981 /* barf on unknown flags */
2982 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY)));
2983 private_flags |= OPpMULTICONCAT_APPEND;
2984 targetop = cBINOPo->op_first;
2986 topop = OpSIBLING(targetop);
2988 /* $x .= <FOO> gets optimised to rcatline instead */
2989 if (topop->op_type == OP_READLINE)
2994 /* Can targetop (the LHS) if it's a padsv, be be optimised
2995 * away and use OPpTARGET_MY instead?
2997 if ( (targetop->op_type == OP_PADSV)
2998 && !(targetop->op_private & OPpDEREF)
2999 && !(targetop->op_private & OPpPAD_STATE)
3000 /* we don't support 'my $x .= ...' */
3001 && ( o->op_type == OP_SASSIGN
3002 || !(targetop->op_private & OPpLVAL_INTRO))
3007 if (topop->op_type == OP_STRINGIFY) {
3008 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY])
3012 /* barf on unknown flags */
3013 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY)));
3015 if ((topop->op_private & OPpTARGET_MY)) {
3016 if (o->op_type == OP_SASSIGN)
3017 return; /* can't have two assigns */
3021 private_flags |= OPpMULTICONCAT_STRINGIFY;
3023 topop = cBINOPx(topop)->op_first;
3024 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK));
3025 topop = OpSIBLING(topop);
3028 if (topop->op_type == OP_SPRINTF) {
3029 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF])
3031 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) {
3032 nargs = sprintf_info.nargs;
3033 total_len = sprintf_info.total_len;
3034 variant = sprintf_info.variant;
3035 utf8 = sprintf_info.utf8;
3037 private_flags |= OPpMULTICONCAT_FAKE;
3039 /* we have an sprintf op rather than a concat optree.
3040 * Skip most of the code below which is associated with
3041 * processing that optree. We also skip phase 2, determining
3042 * whether its cost effective to optimise, since for sprintf,
3043 * multiconcat is *always* faster */
3046 /* note that even if the sprintf itself isn't multiconcatable,
3047 * the expression as a whole may be, e.g. in
3048 * $x .= sprintf("%d",...)
3049 * the sprintf op will be left as-is, but the concat/S op may
3050 * be upgraded to multiconcat
3053 else if (topop->op_type == OP_CONCAT) {
3054 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT])
3057 if ((topop->op_private & OPpTARGET_MY)) {
3058 if (o->op_type == OP_SASSIGN || targmyop)
3059 return; /* can't have two assigns */
3064 /* Is it safe to convert a sassign/stringify/concat op into
3066 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP);
3067 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP);
3068 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP);
3069 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP);
3070 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last)
3071 == STRUCT_OFFSET(UNOP_AUX, op_aux));
3072 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last)
3073 == STRUCT_OFFSET(UNOP_AUX, op_aux));
3075 /* Now scan the down the tree looking for a series of
3076 * CONCAT/OPf_STACKED ops on the LHS (with the last one not
3077 * stacked). For example this tree:
3082 * CONCAT/STACKED -- EXPR5
3084 * CONCAT/STACKED -- EXPR4
3090 * corresponds to an expression like
3092 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5)
3094 * Record info about each EXPR in args[]: in particular, whether it is
3095 * a stringifiable OP_CONST and if so what the const sv is.
3097 * The reason why the last concat can't be STACKED is the difference
3100 * ((($a .= $a) .= $a) .= $a) .= $a
3103 * $a . $a . $a . $a . $a
3105 * The main difference between the optrees for those two constructs
3106 * is the presence of the last STACKED. As well as modifying $a,
3107 * the former sees the changed $a between each concat, so if $s is
3108 * initially 'a', the first returns 'a' x 16, while the latter returns
3109 * 'a' x 5. And pp_multiconcat can't handle that kind of thing.
3119 if ( kid->op_type == OP_CONCAT
3123 k1 = cUNOPx(kid)->op_first;
3125 /* shouldn't happen except maybe after compile err? */
3129 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */
3130 if (kid->op_private & OPpTARGET_MY)
3133 stacked_last = (kid->op_flags & OPf_STACKED);
3145 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2
3146 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2)
3148 /* At least two spare slots are needed to decompose both
3149 * concat args. If there are no slots left, continue to
3150 * examine the rest of the optree, but don't push new values
3151 * on args[]. If the optree as a whole is legal for conversion
3152 * (in particular that the last concat isn't STACKED), then
3153 * the first PERL_MULTICONCAT_MAXARG elements of the optree
3154 * can be converted into an OP_MULTICONCAT now, with the first
3155 * child of that op being the remainder of the optree -
3156 * which may itself later be converted to a multiconcat op
3160 /* the last arg is the rest of the optree */
3165 else if ( argop->op_type == OP_CONST
3166 && ((sv = cSVOPx_sv(argop)))
3167 /* defer stringification until runtime of 'constant'
3168 * things that might stringify variantly, e.g. the radix
3169 * point of NVs, or overloaded RVs */
3170 && (SvPOK(sv) || SvIOK(sv))
3171 && (!SvGMAGICAL(sv))
3173 if (argop->op_private & OPpCONST_STRICT)
3174 no_bareword_allowed(argop);
3176 utf8 |= cBOOL(SvUTF8(sv));
3179 /* this const may be demoted back to a plain arg later;
3180 * make sure we have enough arg slots left */
3182 prev_was_const = !prev_was_const;
3187 prev_was_const = FALSE;
3197 return; /* we don't support ((A.=B).=C)...) */
3199 /* look for two adjacent consts and don't fold them together:
3202 * $o->concat("a")->concat("b")
3205 * (but $o .= "a" . "b" should still fold)
3208 bool seen_nonconst = FALSE;
3209 for (argp = toparg; argp >= args; argp--) {
3210 if (argp->p == NULL) {
3211 seen_nonconst = TRUE;
3217 /* both previous and current arg were constants;
3218 * leave the current OP_CONST as-is */
3226 /* -----------------------------------------------------------------
3229 * At this point we have determined that the optree *can* be converted
3230 * into a multiconcat. Having gathered all the evidence, we now decide
3231 * whether it *should*.
3235 /* we need at least one concat action, e.g.:
3241 * otherwise we could be doing something like $x = "foo", which
3242 * if treated as as a concat, would fail to COW.
3244 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2)
3247 /* Benchmarking seems to indicate that we gain if:
3248 * * we optimise at least two actions into a single multiconcat
3249 * (e.g concat+concat, sassign+concat);
3250 * * or if we can eliminate at least 1 OP_CONST;
3251 * * or if we can eliminate a padsv via OPpTARGET_MY
3255 /* eliminated at least one OP_CONST */
3257 /* eliminated an OP_SASSIGN */
3258 || o->op_type == OP_SASSIGN
3259 /* eliminated an OP_PADSV */
3260 || (!targmyop && is_targable)
3262 /* definitely a net gain to optimise */
3265 /* ... if not, what else? */
3267 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1):
3268 * multiconcat is faster (due to not creating a temporary copy of
3269 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is
3275 && topop->op_type == OP_CONCAT
3277 PADOFFSET t = targmyop->op_targ;
3278 OP *k1 = cBINOPx(topop)->op_first;
3279 OP *k2 = cBINOPx(topop)->op_last;
3280 if ( k2->op_type == OP_PADSV
3282 && ( k1->op_type != OP_PADSV
3283 || k1->op_targ != t)
3288 /* need at least two concats */
3289 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3)
3294 /* -----------------------------------------------------------------
3297 * At this point the optree has been verified as ok to be optimised
3298 * into an OP_MULTICONCAT. Now start changing things.
3303 /* stringify all const args and determine utf8ness */
3306 for (argp = args; argp <= toparg; argp++) {
3307 SV *sv = (SV*)argp->p;
3309 continue; /* not a const op */
3310 if (utf8 && !SvUTF8(sv))
3311 sv_utf8_upgrade_nomg(sv);
3312 argp->p = SvPV_nomg(sv, argp->len);
3313 total_len += argp->len;
3315 /* see if any strings would grow if converted to utf8 */
3317 variant += variant_under_utf8_count((U8 *) argp->p,
3318 (U8 *) argp->p + argp->len);
3322 /* create and populate aux struct */
3326 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
3327 sizeof(UNOP_AUX_item)
3329 PERL_MULTICONCAT_HEADER_SIZE
3330 + ((nargs + 1) * (variant ? 2 : 1))
3333 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1);
3335 /* Extract all the non-const expressions from the concat tree then
3336 * dispose of the old tree, e.g. convert the tree from this:
3340 * STRINGIFY -- TARGET
3342 * ex-PUSHMARK -- CONCAT
3357 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET
3359 * except that if EXPRi is an OP_CONST, it's discarded.
3361 * During the conversion process, EXPR ops are stripped from the tree
3362 * and unshifted onto o. Finally, any of o's remaining original
3363 * childen are discarded and o is converted into an OP_MULTICONCAT.
3365 * In this middle of this, o may contain both: unshifted args on the
3366 * left, and some remaining original args on the right. lastkidop
3367 * is set to point to the right-most unshifted arg to delineate
3368 * between the two sets.
3373 /* create a copy of the format with the %'s removed, and record
3374 * the sizes of the const string segments in the aux struct */
3376 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3378 p = sprintf_info.start;
3381 for (; p < sprintf_info.end; p++) {
3385 (lenp++)->ssize = q - oldq;
3392 lenp->ssize = q - oldq;
3393 assert((STRLEN)(q - const_str) == total_len);
3395 /* Attach all the args (i.e. the kids of the sprintf) to o (which
3396 * may or may not be topop) The pushmark and const ops need to be
3397 * kept in case they're an op_next entry point.
3399 lastkidop = cLISTOPx(topop)->op_last;
3400 kid = cUNOPx(topop)->op_first; /* pushmark */
3402 op_null(OpSIBLING(kid)); /* const */
3404 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */
3405 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */
3406 lastkidop->op_next = o;
3411 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3415 /* Concatenate all const strings into const_str.
3416 * Note that args[] contains the RHS args in reverse order, so
3417 * we scan args[] from top to bottom to get constant strings
3420 for (argp = toparg; argp >= args; argp--) {
3422 /* not a const op */
3423 (++lenp)->ssize = -1;
3425 STRLEN l = argp->len;
3426 Copy(argp->p, p, l, char);
3428 if (lenp->ssize == -1)
3439 for (argp = args; argp <= toparg; argp++) {
3440 /* only keep non-const args, except keep the first-in-next-chain
3441 * arg no matter what it is (but nulled if OP_CONST), because it
3442 * may be the entry point to this subtree from the previous
3445 bool last = (argp == toparg);
3448 /* set prev to the sibling *before* the arg to be cut out,
3449 * e.g. when cutting EXPR:
3454 * prev= CONCAT -- EXPR
3457 if (argp == args && kid->op_type != OP_CONCAT) {
3458 /* in e.g. '$x .= f(1)' there's no RHS concat tree
3459 * so the expression to be cut isn't kid->op_last but
3462 /* find the op before kid */
3464 o2 = cUNOPx(parentop)->op_first;
3465 while (o2 && o2 != kid) {
3473 else if (kid == o && lastkidop)
3474 prev = last ? lastkidop : OpSIBLING(lastkidop);
3476 prev = last ? NULL : cUNOPx(kid)->op_first;
3478 if (!argp->p || last) {
3480 OP *aop = op_sibling_splice(kid, prev, 1, NULL);
3481 /* and unshift to front of o */
3482 op_sibling_splice(o, NULL, 0, aop);
3483 /* record the right-most op added to o: later we will
3484 * free anything to the right of it */
3487 aop->op_next = nextop;
3490 /* null the const at start of op_next chain */
3494 nextop = prev->op_next;
3497 /* the last two arguments are both attached to the same concat op */
3498 if (argp < toparg - 1)
3503 /* Populate the aux struct */
3505 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs;
3506 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str;
3507 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len;
3508 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str;
3509 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len;
3511 /* if variant > 0, calculate a variant const string and lengths where
3512 * the utf8 version of the string will take 'variant' more bytes than
3516 char *p = const_str;
3517 STRLEN ulen = total_len + variant;
3518 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
3519 UNOP_AUX_item *ulens = lens + (nargs + 1);
3520 char *up = (char*)PerlMemShared_malloc(ulen);
3523 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up;
3524 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen;
3526 for (n = 0; n < (nargs + 1); n++) {
3528 char * orig_up = up;
3529 for (i = (lens++)->ssize; i > 0; i--) {
3531 append_utf8_from_native_byte(c, (U8**)&up);
3533 (ulens++)->ssize = (i < 0) ? i : up - orig_up;
3538 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep
3539 * that op's first child - an ex-PUSHMARK - because the op_next of
3540 * the previous op may point to it (i.e. it's the entry point for
3545 ? op_sibling_splice(o, lastkidop, 1, NULL)
3546 : op_sibling_splice(stringop, NULL, 1, NULL);
3547 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK));
3548 op_sibling_splice(o, NULL, 0, pmop);
3555 * target .= A.B.C...
3561 if (o->op_type == OP_SASSIGN) {
3562 /* Move the target subtree from being the last of o's children
3563 * to being the last of o's preserved children.
3564 * Note the difference between 'target = ...' and 'target .= ...':
3565 * for the former, target is executed last; for the latter,
3568 kid = OpSIBLING(lastkidop);
3569 op_sibling_splice(o, kid, 1, NULL); /* cut target op */
3570 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */
3571 lastkidop->op_next = kid->op_next;
3572 lastkidop = targetop;
3575 /* Move the target subtree from being the first of o's
3576 * original children to being the first of *all* o's children.
3579 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */
3580 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/
3583 /* if the RHS of .= doesn't contain a concat (e.g.
3584 * $x .= "foo"), it gets missed by the "strip ops from the
3585 * tree and add to o" loop earlier */
3586 assert(topop->op_type != OP_CONCAT);
3588 /* in e.g. $x .= "$y", move the $y expression
3589 * from being a child of OP_STRINGIFY to being the
3590 * second child of the OP_CONCAT
3592 assert(cUNOPx(stringop)->op_first == topop);
3593 op_sibling_splice(stringop, NULL, 1, NULL);
3594 op_sibling_splice(o, cUNOPo->op_first, 0, topop);
3596 assert(topop == OpSIBLING(cBINOPo->op_first));
3605 * my $lex = A.B.C...
3608 * The original padsv op is kept but nulled in case it's the
3609 * entry point for the optree (which it will be for
3612 private_flags |= OPpTARGET_MY;
3613 private_flags |= (targetop->op_private & OPpLVAL_INTRO);
3614 o->op_targ = targetop->op_targ;
3615 targetop->op_targ = 0;
3619 flags |= OPf_STACKED;
3621 else if (targmyop) {
3622 private_flags |= OPpTARGET_MY;
3623 if (o != targmyop) {
3624 o->op_targ = targmyop->op_targ;
3625 targmyop->op_targ = 0;
3629 /* detach the emaciated husk of the sprintf/concat optree and free it */
3631 kid = op_sibling_splice(o, lastkidop, 1, NULL);
3637 /* and convert o into a multiconcat */
3639 o->op_flags = (flags|OPf_KIDS|stacked_last
3640 |(o->op_flags & (OPf_WANT|OPf_PARENS)));
3641 o->op_private = private_flags;
3642 o->op_type = OP_MULTICONCAT;
3643 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT];
3644 cUNOP_AUXo->op_aux = aux;
3648 /* do all the final processing on an optree (e.g. running the peephole
3649 * optimiser on it), then attach it to cv (if cv is non-null)
3653 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
3657 /* XXX for some reason, evals, require and main optrees are
3658 * never attached to their CV; instead they just hang off
3659 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
3660 * and get manually freed when appropriate */
3662 startp = &CvSTART(cv);
3664 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
3667 optree->op_private |= OPpREFCOUNTED;
3668 OpREFCNT_set(optree, 1);
3669 optimize_optree(optree);
3671 finalize_optree(optree);
3672 S_prune_chain_head(startp);
3675 /* now that optimizer has done its work, adjust pad values */
3676 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
3677 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
3683 =for apidoc optimize_optree
3685 This function applies some optimisations to the optree in top-down order.
3686 It is called before the peephole optimizer, which processes ops in
3687 execution order. Note that finalize_optree() also does a top-down scan,
3688 but is called *after* the peephole optimizer.
3694 Perl_optimize_optree(pTHX_ OP* o)
3696 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE;
3699 SAVEVPTR(PL_curcop);
3707 /* helper for optimize_optree() which optimises one op then recurses
3708 * to optimise any children.
3712 S_optimize_op(pTHX_ OP* o)
3716 PERL_ARGS_ASSERT_OPTIMIZE_OP;
3719 OP * next_kid = NULL;
3721 assert(o->op_type != OP_FREED);
3723 switch (o->op_type) {
3726 PL_curcop = ((COP*)o); /* for warnings */
3734 S_maybe_multiconcat(aTHX_ o);
3738 if (cPMOPo->op_pmreplrootu.op_pmreplroot) {
3739 /* we can't assume that op_pmreplroot->op_sibparent == o
3740 * and that it is thus possible to walk back up the tree
3741 * past op_pmreplroot. So, although we try to avoid
3742 * recursing through op trees, do it here. After all,
3743 * there are unlikely to be many nested s///e's within
3744 * the replacement part of a s///e.
3746 optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3754 if (o->op_flags & OPf_KIDS)
3755 next_kid = cUNOPo->op_first;
3757 /* if a kid hasn't been nominated to process, continue with the
3758 * next sibling, or if no siblings left, go back to the parent's
3759 * siblings and so on
3763 return; /* at top; no parents/siblings to try */
3764 if (OpHAS_SIBLING(o))
3765 next_kid = o->op_sibparent;
3767 o = o->op_sibparent; /*try parent's next sibling */
3770 /* this label not yet used. Goto here if any code above sets
3780 =for apidoc finalize_optree
3782 This function finalizes the optree. Should be called directly after
3783 the complete optree is built. It does some additional
3784 checking which can't be done in the normal C<ck_>xxx functions and makes
3785 the tree thread-safe.
3790 Perl_finalize_optree(pTHX_ OP* o)
3792 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
3795 SAVEVPTR(PL_curcop);
3803 /* Relocate sv to the pad for thread safety.
3804 * Despite being a "constant", the SV is written to,
3805 * for reference counts, sv_upgrade() etc. */
3806 PERL_STATIC_INLINE void
3807 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
3810 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
3812 ix = pad_alloc(OP_CONST, SVf_READONLY);
3813 SvREFCNT_dec(PAD_SVl(ix));
3814 PAD_SETSV(ix, *svp);
3815 /* XXX I don't know how this isn't readonly already. */
3816 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
3823 =for apidoc traverse_op_tree
3825 Return the next op in a depth-first traversal of the op tree,
3826 returning NULL when the traversal is complete.
3828 The initial call must supply the root of the tree as both top and o.
3830 For now it's static, but it may be exposed to the API in the future.
3836 S_traverse_op_tree(pTHX_ OP *top, OP *o) {
3839 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;
3841 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {
3842 return cUNOPo->op_first;
3844 else if ((sib = OpSIBLING(o))) {
3848 OP *parent = o->op_sibparent;
3849 assert(!(o->op_moresib));
3850 while (parent && parent != top) {
3851 OP *sib = OpSIBLING(parent);
3854 parent = parent->op_sibparent;
3862 S_finalize_op(pTHX_ OP* o)
3865 PERL_ARGS_ASSERT_FINALIZE_OP;
3868 assert(o->op_type != OP_FREED);
3870 switch (o->op_type) {
3873 PL_curcop = ((COP*)o); /* for warnings */
3876 if (OpHAS_SIBLING(o)) {
3877 OP *sib = OpSIBLING(o);
3878 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
3879 && ckWARN(WARN_EXEC)
3880 && OpHAS_SIBLING(sib))
3882 const OPCODE type = OpSIBLING(sib)->op_type;
3883 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
3884 const line_t oldline = CopLINE(PL_curcop);
3885 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
3886 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3887 "Statement unlikely to be reached");
3888 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3889 "\t(Maybe you meant system() when you said exec()?)\n");
3890 CopLINE_set(PL_curcop, oldline);
3897 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
3898 GV * const gv = cGVOPo_gv;
3899 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
3900 /* XXX could check prototype here instead of just carping */
3901 SV * const sv = sv_newmortal();
3902 gv_efullname3(sv, gv, NULL);
3903 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3904 "%" SVf "() called too early to check prototype",
3911 if (cSVOPo->op_private & OPpCONST_STRICT)
3912 no_bareword_allowed(o);
3916 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
3921 /* Relocate all the METHOP's SVs to the pad for thread safety. */
3922 case OP_METHOD_NAMED:
3923 case OP_METHOD_SUPER:
3924 case OP_METHOD_REDIR:
3925 case OP_METHOD_REDIR_SUPER:
3926 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
3935 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
3938 rop = (UNOP*)((BINOP*)o)->op_first;
3943 S_scalar_slice_warning(aTHX_ o);
3947 kid = OpSIBLING(cLISTOPo->op_first);
3948 if (/* I bet there's always a pushmark... */
3949 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
3950 && OP_TYPE_ISNT_NN(kid, OP_CONST))
3955 key_op = (SVOP*)(kid->op_type == OP_CONST
3957 : OpSIBLING(kLISTOP->op_first));
3959 rop = (UNOP*)((LISTOP*)o)->op_last;
3962 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
3964 S_check_hash_fields_and_hekify(aTHX_ rop, key_op, 1);
3968 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
3972 S_scalar_slice_warning(aTHX_ o);
3976 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
3977 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3985 if (o->op_flags & OPf_KIDS) {
3988 /* check that op_last points to the last sibling, and that
3989 * the last op_sibling/op_sibparent field points back to the
3990 * parent, and that the only ops with KIDS are those which are
3991 * entitled to them */
3992 U32 type = o->op_type;
3996 if (type == OP_NULL) {
3998 /* ck_glob creates a null UNOP with ex-type GLOB
3999 * (which is a list op. So pretend it wasn't a listop */
4000 if (type == OP_GLOB)
4003 family = PL_opargs[type] & OA_CLASS_MASK;
4005 has_last = ( family == OA_BINOP
4006 || family == OA_LISTOP
4007 || family == OA_PMOP
4008 || family == OA_LOOP
4010 assert( has_last /* has op_first and op_last, or ...
4011 ... has (or may have) op_first: */
4012 || family == OA_UNOP
4013 || family == OA_UNOP_AUX
4014 || family == OA_LOGOP
4015 || family == OA_BASEOP_OR_UNOP
4016 || family == OA_FILESTATOP
4017 || family == OA_LOOPEXOP
4018 || family == OA_METHOP
4019 || type == OP_CUSTOM
4020 || type == OP_NULL /* new_logop does this */
4023 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
4024 if (!OpHAS_SIBLING(kid)) {
4026 assert(kid == cLISTOPo->op_last);
4027 assert(kid->op_sibparent == o);
4032 } while (( o = traverse_op_tree(top, o)) != NULL);
4036 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
4039 PadnameLVALUE_on(pn);
4040 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
4042 /* RT #127786: cv can be NULL due to an eval within the DB package
4043 * called from an anon sub - anon subs don't have CvOUTSIDE() set
4044 * unless they contain an eval, but calling eval within DB
4045 * pretends the eval was done in the caller's scope.
4049 assert(CvPADLIST(cv));
4051 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
4052 assert(PadnameLEN(pn));
4053 PadnameLVALUE_on(pn);
4058 S_vivifies(const OPCODE type)
4061 case OP_RV2AV: case OP_ASLICE:
4062 case OP_RV2HV: case OP_KVASLICE:
4063 case OP_RV2SV: case OP_HSLICE:
4064 case OP_AELEMFAST: case OP_KVHSLICE:
4073 /* apply lvalue reference (aliasing) context to the optree o.
4076 * o would be the list ($x,$y) and type would be OP_AASSIGN.
4077 * It may descend and apply this to children too, for example in
4078 * \( $cond ? $x, $y) = (...)
4082 S_lvref(pTHX_ OP *o, I32 type)
4089 switch (o->op_type) {
4091 o = OpSIBLING(cUNOPo->op_first);
4098 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
4099 o->op_flags |= OPf_STACKED;
4100 if (o->op_flags & OPf_PARENS) {
4101 if (o->op_private & OPpLVAL_INTRO) {
4102 yyerror(Perl_form(aTHX_ "Can't modify reference to "
4103 "localized parenthesized array in list assignment"));
4107 OpTYPE_set(o, OP_LVAVREF);
4108 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
4109 o->op_flags |= OPf_MOD|OPf_REF;
4112 o->op_private |= OPpLVREF_AV;
4116 kid = cUNOPo->op_first;
4117 if (kid->op_type == OP_NULL)
4118 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
4120 o->op_private = OPpLVREF_CV;
4121 if (kid->op_type == OP_GV)
4122 o->op_flags |= OPf_STACKED;
4123 else if (kid->op_type == OP_PADCV) {
4124 o->op_targ = kid->op_targ;
4126 op_free(cUNOPo->op_first);
4127 cUNOPo->op_first = NULL;
4128 o->op_flags &=~ OPf_KIDS;
4134 if (o->op_flags & OPf_PARENS) {
4136 yyerror(Perl_form(aTHX_ "Can't modify reference to "
4137 "parenthesized hash in list assignment"));
4140 o->op_private |= OPpLVREF_HV;
4144 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
4145 o->op_flags |= OPf_STACKED;
4149 if (o->op_flags & OPf_PARENS) goto parenhash;
4150 o->op_private |= OPpLVREF_HV;
4153 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
4157 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
4158 if (o->op_flags & OPf_PARENS) goto slurpy;
4159 o->op_private |= OPpLVREF_AV;
4164 o->op_private |= OPpLVREF_ELEM;
4165 o->op_flags |= OPf_STACKED;
4170 OpTYPE_set(o, OP_LVREFSLICE);
4171 o->op_private &= OPpLVAL_INTRO;
4175 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4177 else if (!(o->op_flags & OPf_KIDS))
4180 /* the code formerly only recursed into the first child of
4181 * a non ex-list OP_NULL. if we ever encounter such a null op with
4182 * more than one child, need to decide whether its ok to process
4183 * *all* its kids or not */
4184 assert(o->op_targ == OP_LIST
4185 || !(OpHAS_SIBLING(cBINOPo->op_first)));
4188 o = cLISTOPo->op_first;
4192 if (o->op_flags & OPf_PARENS)
4197 /* diag_listed_as: Can't modify reference to %s in %s assignment */
4198 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
4199 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
4206 OpTYPE_set(o, OP_LVREF);
4208 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
4209 if (type == OP_ENTERLOOP)
4210 o->op_private |= OPpLVREF_ITER;
4215 return; /* at top; no parents/siblings to try */
4216 if (OpHAS_SIBLING(o)) {
4217 o = o->op_sibparent;
4220 o = o->op_sibparent; /*try parent's next sibling */
4226 PERL_STATIC_INLINE bool
4227 S_potential_mod_type(I32 type)
4229 /* Types that only potentially result in modification. */
4230 return type == OP_GREPSTART || type == OP_ENTERSUB
4231 || type == OP_REFGEN || type == OP_LEAVESUBLV;
4236 =for apidoc op_lvalue
4238 Propagate lvalue ("modifiable") context to an op and its children.
4239 C<type> represents the context type, roughly based on the type of op that
4240 would do the modifying, although C<local()> is represented by C<OP_NULL>,
4241 because it has no op type of its own (it is signalled by a flag on
4244 This function detects things that can't be modified, such as C<$x+1>, and
4245 generates errors for them. For example, C<$x+1 = 2> would cause it to be
4246 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
4248 It also flags things that need to behave specially in an lvalue context,
4249 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
4253 Perl_op_lvalue_flags() is a non-API lower-level interface to
4254 op_lvalue(). The flags param has these bits:
4255 OP_LVALUE_NO_CROAK: return rather than croaking on error
4260 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
4265 if (!o || (PL_parser && PL_parser->error_count))
4270 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
4272 OP *next_kid = NULL;
4274 if ((o->op_private & OPpTARGET_MY)
4275 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
4280 /* elements of a list might be in void context because the list is
4281 in scalar context or because they are attribute sub calls */
4282 if ((o->op_flags & OPf_WANT) == OPf_WANT_VOID)
4285 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
4287 switch (o->op_type) {
4293 if ((o->op_flags & OPf_PARENS))
4298 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
4299 !(o->op_flags & OPf_STACKED)) {
4300 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
4301 assert(cUNOPo->op_first->op_type == OP_NULL);
4302 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
4305 else { /* lvalue subroutine call */
4306 o->op_private |= OPpLVAL_INTRO;
4307 PL_modcount = RETURN_UNLIMITED_NUMBER;
4308 if (S_potential_mod_type(type)) {
4309 o->op_private |= OPpENTERSUB_INARGS;
4312 else { /* Compile-time error message: */
4313 OP *kid = cUNOPo->op_first;
4318 if (kid->op_type != OP_PUSHMARK) {
4319 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
4321 "panic: unexpected lvalue entersub "
4322 "args: type/targ %ld:%" UVuf,
4323 (long)kid->op_type, (UV)kid->op_targ);
4324 kid = kLISTOP->op_first;
4326 while (OpHAS_SIBLING(kid))
4327 kid = OpSIBLING(kid);
4328 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
4329 break; /* Postpone until runtime */
4332 kid = kUNOP->op_first;
4333 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
4334 kid = kUNOP->op_first;
4335 if (kid->op_type == OP_NULL)
4337 "Unexpected constant lvalue entersub "
4338 "entry via type/targ %ld:%" UVuf,
4339 (long)kid->op_type, (UV)kid->op_targ);
4340 if (kid->op_type != OP_GV) {
4347 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
4348 ? MUTABLE_CV(SvRV(gv))
4354 if (flags & OP_LVALUE_NO_CROAK)
4357 namesv = cv_name(cv, NULL, 0);
4358 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
4359 "subroutine call of &%" SVf " in %s",
4360 SVfARG(namesv), PL_op_desc[type]),
4368 if (flags & OP_LVALUE_NO_CROAK) return NULL;
4369 /* grep, foreach, subcalls, refgen */
4370 if (S_potential_mod_type(type))
4372 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
4373 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
4376 type ? PL_op_desc[type] : "local"));
4389 case OP_RIGHT_SHIFT:
4398 if (!(o->op_flags & OPf_STACKED))
4404 if (o->op_flags & OPf_STACKED) {
4408 if (!(o->op_private & OPpREPEAT_DOLIST))
4411 const I32 mods = PL_modcount;
4412 /* we recurse rather than iterate here because we need to
4413 * calculate and use the delta applied to PL_modcount by the
4414 * first child. So in something like
4415 * ($x, ($y) x 3) = split;
4416 * split knows that 4 elements are wanted
4418 modkids(cBINOPo->op_first, type);
4419 if (type != OP_AASSIGN)
4421 kid = cBINOPo->op_last;
4422 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
4423 const IV iv = SvIV(kSVOP_sv);
4424 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
4426 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
4429 PL_modcount = RETURN_UNLIMITED_NUMBER;
4435 next_kid = OpSIBLING(cUNOPo->op_first);
4440 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
4441 PL_modcount = RETURN_UNLIMITED_NUMBER;
4442 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4443 fiable since some contexts need to know. */
4444 o->op_flags |= OPf_MOD;
4449 if (scalar_mod_type(o, type))
4451 ref(cUNOPo->op_first, o->op_type);
4458 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
4459 if (type == OP_LEAVESUBLV && (
4460 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
4461 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4463 o->op_private |= OPpMAYBE_LVSUB;
4467 PL_modcount = RETURN_UNLIMITED_NUMBER;
4473 if (type == OP_LEAVESUBLV)
4474 o->op_private |= OPpMAYBE_LVSUB;
4478 if (type == OP_LEAVESUBLV
4479 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS)
4480 o->op_private |= OPpMAYBE_LVSUB;
4484 PL_hints |= HINT_BLOCK_SCOPE;
4485 if (type == OP_LEAVESUBLV)
4486 o->op_private |= OPpMAYBE_LVSUB;
4491 ref(cUNOPo->op_first, o->op_type);
4495 PL_hints |= HINT_BLOCK_SCOPE;
4505 case OP_AELEMFAST_LEX:
4512 PL_modcount = RETURN_UNLIMITED_NUMBER;
4513 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
4515 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4516 fiable since some contexts need to know. */
4517 o->op_flags |= OPf_MOD;
4520 if (scalar_mod_type(o, type))
4522 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4523 && type == OP_LEAVESUBLV)
4524 o->op_private |= OPpMAYBE_LVSUB;
4528 if (!type) /* local() */
4529 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
4530 PNfARG(PAD_COMPNAME(o->op_targ)));
4531 if (!(o->op_private & OPpLVAL_INTRO)
4532 || ( type != OP_SASSIGN && type != OP_AASSIGN
4533 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
4534 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
4542 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
4546 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
4552 if (type == OP_LEAVESUBLV)
4553 o->op_private |= OPpMAYBE_LVSUB;
4554 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
4555 /* we recurse rather than iterate here because the child
4556 * needs to be processed with a different 'type' parameter */
4558 /* substr and vec */
4559 /* If this op is in merely potential (non-fatal) modifiable
4560 context, then apply OP_ENTERSUB context to
4561 the kid op (to avoid croaking). Other-
4562 wise pass this op’s own type so the correct op is mentioned
4563 in error messages. */
4564 op_lvalue(OpSIBLING(cBINOPo->op_first),
4565 S_potential_mod_type(type)
4573 ref(cBINOPo->op_first, o->op_type);
4574 if (type == OP_ENTERSUB &&
4575 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
4576 o->op_private |= OPpLVAL_DEFER;
4577 if (type == OP_LEAVESUBLV)
4578 o->op_private |= OPpMAYBE_LVSUB;
4585 o->op_private |= OPpLVALUE;
4591 if (o->op_flags & OPf_KIDS)
4592 next_kid = cLISTOPo->op_last;
4597 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4599 else if (!(o->op_flags & OPf_KIDS))
4602 if (o->op_targ != OP_LIST) {
4603 OP *sib = OpSIBLING(cLISTOPo->op_first);
4604 /* OP_TRANS and OP_TRANSR with argument have a weird optree
4611 * compared with things like OP_MATCH which have the argument
4617 * so handle specially to correctly get "Can't modify" croaks etc
4620 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
4622 /* this should trigger a "Can't modify transliteration" err */
4623 op_lvalue(sib, type);
4625 next_kid = cBINOPo->op_first;
4626 /* we assume OP_NULLs which aren't ex-list have no more than 2
4627 * children. If this assumption is wrong, increase the scan
4629 assert( !OpHAS_SIBLING(next_kid)
4630 || !OpHAS_SIBLING(OpSIBLING(next_kid)));
4636 next_kid = cLISTOPo->op_first;
4644 if (type == OP_LEAVESUBLV
4645 || !S_vivifies(cLOGOPo->op_first->op_type))
4646 next_kid = cLOGOPo->op_first;
4647 else if (type == OP_LEAVESUBLV
4648 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
4649 next_kid = OpSIBLING(cLOGOPo->op_first);
4653 if (type == OP_NULL) { /* local */
4655 if (!FEATURE_MYREF_IS_ENABLED)
4656 Perl_croak(aTHX_ "The experimental declared_refs "
4657 "feature is not enabled");
4658 Perl_ck_warner_d(aTHX_
4659 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
4660 "Declaring references is experimental");
4661 next_kid = cUNOPo->op_first;
4664 if (type != OP_AASSIGN && type != OP_SASSIGN
4665 && type != OP_ENTERLOOP)
4667 /* Don’t bother applying lvalue context to the ex-list. */
4668 kid = cUNOPx(cUNOPo->op_first)->op_first;
4669 assert (!OpHAS_SIBLING(kid));
4672 if (type == OP_NULL) /* local */
4674 if (type != OP_AASSIGN) goto nomod;
4675 kid = cUNOPo->op_first;
4678 const U8 ec = PL_parser ? PL_parser->error_count : 0;
4679 S_lvref(aTHX_ kid, type);
4680 if (!PL_parser || PL_parser->error_count == ec) {
4681 if (!FEATURE_REFALIASING_IS_ENABLED)
4683 "Experimental aliasing via reference not enabled");
4684 Perl_ck_warner_d(aTHX_
4685 packWARN(WARN_EXPERIMENTAL__REFALIASING),
4686 "Aliasing via reference is experimental");
4689 if (o->op_type == OP_REFGEN)
4690 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
4695 if ((o->op_private & OPpSPLIT_ASSIGN)) {
4696 /* This is actually @array = split. */
4697 PL_modcount = RETURN_UNLIMITED_NUMBER;
4703 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
4707 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that
4708 their argument is a filehandle; thus \stat(".") should not set
4710 if (type == OP_REFGEN && OP_IS_STAT(o->op_type))
4713 if (type != OP_LEAVESUBLV)
4714 o->op_flags |= OPf_MOD;
4716 if (type == OP_AASSIGN || type == OP_SASSIGN)
4717 o->op_flags |= OPf_SPECIAL
4718 |(o->op_type == OP_ENTERSUB ? 0 : OPf_REF);
4719 else if (!type) { /* local() */
4722 o->op_private |= OPpLVAL_INTRO;
4723 o->op_flags &= ~OPf_SPECIAL;
4724 PL_hints |= HINT_BLOCK_SCOPE;
4729 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
4730 "Useless localization of %s", OP_DESC(o));
4733 else if (type != OP_GREPSTART && type != OP_ENTERSUB
4734 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB)
4735 o->op_flags |= OPf_REF;
4740 return top_op; /* at top; no parents/siblings to try */
4741 if (OpHAS_SIBLING(o)) {
4742 next_kid = o->op_sibparent;
4743 if (!OpHAS_SIBLING(next_kid)) {
4744 /* a few node types don't recurse into their second child */
4745 OP *parent = next_kid->op_sibparent;
4746 I32 ptype = parent->op_type;
4747 if ( (ptype == OP_NULL && parent->op_targ != OP_LIST)
4748 || ( (ptype == OP_AND || ptype == OP_OR)
4749 && (type != OP_LEAVESUBLV
4750 && S_vivifies(next_kid->op_type))
4753 /*try parent's next sibling */
4760 o = o->op_sibparent; /*try parent's next sibling */
4771 S_scalar_mod_type(const OP *o, I32 type)
4776 if (o && o->op_type == OP_RV2GV)
4800 case OP_RIGHT_SHIFT:
4829 S_is_handle_constructor(const OP *o, I32 numargs)
4831 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
4833 switch (o->op_type) {
4841 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
4854 S_refkids(pTHX_ OP *o, I32 type)
4856 if (o && o->op_flags & OPf_KIDS) {
4858 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
4865 /* Apply reference (autovivification) context to the subtree at o.
4867 * push @{expression}, ....;
4868 * o will be the head of 'expression' and type will be OP_RV2AV.
4869 * It marks the op o (or a suitable child) as autovivifying, e.g. by
4871 * For OP_RV2AV/OP_PADAV and OP_RV2HV/OP_PADHV sets OPf_REF too if
4872 * set_op_ref is true.
4874 * Also calls scalar(o).
4878 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
4883 PERL_ARGS_ASSERT_DOREF;
4885 if (PL_parser && PL_parser->error_count)
4889 switch (o->op_type) {
4891 if ((type == OP_EXISTS || type == OP_DEFINED) &&
4892 !(o->op_flags & OPf_STACKED)) {
4893 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
4894 assert(cUNOPo->op_first->op_type == OP_NULL);
4895 /* disable pushmark */
4896 op_null(((LISTOP*)cUNOPo->op_first)->op_first);
4897 o->op_flags |= OPf_SPECIAL;
4899 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
4900 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
4901 : type == OP_RV2HV ? OPpDEREF_HV
4903 o->op_flags |= OPf_MOD;
4909 o = OpSIBLING(cUNOPo->op_first);
4913 if (type == OP_DEFINED)
4914 o->op_flags |= OPf_SPECIAL; /* don't create GV */
4917 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
4918 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
4919 : type == OP_RV2HV ? OPpDEREF_HV
4921 o->op_flags |= OPf_MOD;
4923 if (o->op_flags & OPf_KIDS) {
4925 o = cUNOPo->op_first;
4933 o->op_flags |= OPf_REF;
4936 if (type == OP_DEFINED)
4937 o->op_flags |= OPf_SPECIAL; /* don't create GV */
4939 o = cUNOPo->op_first;
4945 o->op_flags |= OPf_REF;
4950 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
4952 o = cBINOPo->op_first;
4957 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
4958 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
4959 : type == OP_RV2HV ? OPpDEREF_HV
4961 o->op_flags |= OPf_MOD;
4964 o = cBINOPo->op_first;
4973 if (!(o->op_flags & OPf_KIDS))
4975 o = cLISTOPo->op_last;
4984 return scalar(top_op); /* at top; no parents/siblings to try */
4985 if (OpHAS_SIBLING(o)) {
4986 o = o->op_sibparent;
4987 /* Normally skip all siblings and go straight to the parent;
4988 * the only op that requires two children to be processed
4989 * is OP_COND_EXPR */
4990 if (!OpHAS_SIBLING(o)
4991 && o->op_sibparent->op_type == OP_COND_EXPR)
4995 o = o->op_sibparent; /*try parent's next sibling */
5002 S_dup_attrlist(pTHX_ OP *o)
5006 PERL_ARGS_ASSERT_DUP_ATTRLIST;
5008 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
5009 * where the first kid is OP_PUSHMARK and the remaining ones
5010 * are OP_CONST. We need to push the OP_CONST values.
5012 if (o->op_type == OP_CONST)
5013 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
5015 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
5017 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
5018 if (o->op_type == OP_CONST)
5019 rop = op_append_elem(OP_LIST, rop,
5020 newSVOP(OP_CONST, o->op_flags,
5021 SvREFCNT_inc_NN(cSVOPo->op_sv)));
5028 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
5030 PERL_ARGS_ASSERT_APPLY_ATTRS;
5032 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
5034 /* fake up C<use attributes $pkg,$rv,@attrs> */
5036 #define ATTRSMODULE "attributes"
5037 #define ATTRSMODULE_PM "attributes.pm"
5040 aTHX_ PERL_LOADMOD_IMPORT_OPS,
5041 newSVpvs(ATTRSMODULE),
5043 op_prepend_elem(OP_LIST,
5044 newSVOP(OP_CONST, 0, stashsv),
5045 op_prepend_elem(OP_LIST,
5046 newSVOP(OP_CONST, 0,
5048 dup_attrlist(attrs))));
5053 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
5055 OP *pack, *imop, *arg;
5056 SV *meth, *stashsv, **svp;
5058 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
5063 assert(target->op_type == OP_PADSV ||
5064 target->op_type == OP_PADHV ||
5065 target->op_type == OP_PADAV);
5067 /* Ensure that attributes.pm is loaded. */
5068 /* Don't force the C<use> if we don't need it. */
5069 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
5070 if (svp && *svp != &PL_sv_undef)
5071 NOOP; /* already in %INC */
5073 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5074 newSVpvs(ATTRSMODULE), NULL);
5076 /* Need package name for method call. */
5077 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
5079 /* Build up the real arg-list. */
5080 stashsv = newSVhek(HvNAME_HEK(stash));
5082 arg = newOP(OP_PADSV, 0);
5083 arg->op_targ = target->op_targ;
5084 arg = op_prepend_elem(OP_LIST,
5085 newSVOP(OP_CONST, 0, stashsv),
5086 op_prepend_elem(OP_LIST,
5087 newUNOP(OP_REFGEN, 0,
5089 dup_attrlist(attrs)));
5091 /* Fake up a method call to import */
5092 meth = newSVpvs_share("import");
5093 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
5094 op_append_elem(OP_LIST,
5095 op_prepend_elem(OP_LIST, pack, arg),
5096 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
5098 /* Combine the ops. */
5099 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
5103 =notfor apidoc apply_attrs_string
5105 Attempts to apply a list of attributes specified by the C<attrstr> and
5106 C<len> arguments to the subroutine identified by the C<cv> argument which
5107 is expected to be associated with the package identified by the C<stashpv>
5108 argument (see L<attributes>). It gets this wrong, though, in that it
5109 does not correctly identify the boundaries of the individual attribute
5110 specifications within C<attrstr>. This is not really intended for the
5111 public API, but has to be listed here for systems such as AIX which
5112 need an explicit export list for symbols. (It's called from XS code
5113 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
5114 to respect attribute syntax properly would be welcome.
5120 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
5121 const char *attrstr, STRLEN len)
5125 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
5128 len = strlen(attrstr);
5132 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
5134 const char * const sstr = attrstr;
5135 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
5136 attrs = op_append_elem(OP_LIST, attrs,
5137 newSVOP(OP_CONST, 0,
5138 newSVpvn(sstr, attrstr-sstr)));
5142 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
5143 newSVpvs(ATTRSMODULE),
5144 NULL, op_prepend_elem(OP_LIST,
5145 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
5146 op_prepend_elem(OP_LIST,
5147 newSVOP(OP_CONST, 0,
5148 newRV(MUTABLE_SV(cv))),
5153 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name,
5156 OP *new_proto = NULL;
5161 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
5167 if (o->op_type == OP_CONST) {
5168 pv = SvPV(cSVOPo_sv, pvlen);
5169 if (memBEGINs(pv, pvlen, "prototype(")) {
5170 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
5171 SV ** const tmpo = cSVOPx_svp(o);
5172 SvREFCNT_dec(cSVOPo_sv);
5177 } else if (o->op_type == OP_LIST) {
5179 assert(o->op_flags & OPf_KIDS);
5180 lasto = cLISTOPo->op_first;
5181 assert(lasto->op_type == OP_PUSHMARK);
5182 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
5183 if (o->op_type == OP_CONST) {
5184 pv = SvPV(cSVOPo_sv, pvlen);
5185 if (memBEGINs(pv, pvlen, "prototype(")) {
5186 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
5187 SV ** const tmpo = cSVOPx_svp(o);
5188 SvREFCNT_dec(cSVOPo_sv);
5190 if (new_proto && ckWARN(WARN_MISC)) {
5192 const char * newp = SvPV(cSVOPo_sv, new_len);
5193 Perl_warner(aTHX_ packWARN(WARN_MISC),
5194 "Attribute prototype(%" UTF8f ") discards earlier prototype attribute in same sub",
5195 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
5201 /* excise new_proto from the list */
5202 op_sibling_splice(*attrs, lasto, 1, NULL);
5209 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
5210 would get pulled in with no real need */
5211 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
5220 svname = sv_newmortal();
5221 gv_efullname3(svname, name, NULL);
5223 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
5224 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
5226 svname = (SV *)name;
5227 if (ckWARN(WARN_ILLEGALPROTO))
5228 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE,
5230 if (*proto && ckWARN(WARN_PROTOTYPE)) {
5231 STRLEN old_len, new_len;
5232 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
5233 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
5235 if (curstash && svname == (SV *)name
5236 && !memchr(SvPVX(svname), ':', SvCUR(svname))) {
5237 svname = sv_2mortal(newSVsv(PL_curstname));
5238 sv_catpvs(svname, "::");
5239 sv_catsv(svname, (SV *)name);
5242 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
5243 "Prototype '%" UTF8f "' overridden by attribute 'prototype(%" UTF8f ")'"
5245 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
5246 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
5256 S_cant_declare(pTHX_ OP *o)
5258 if (o->op_type == OP_NULL
5259 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
5260 o = cUNOPo->op_first;
5261 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
5262 o->op_type == OP_NULL
5263 && o->op_flags & OPf_SPECIAL
5266 PL_parser->in_my == KEY_our ? "our" :
5267 PL_parser->in_my == KEY_state ? "state" :
5272 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
5275 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
5277 PERL_ARGS_ASSERT_MY_KID;
5279 if (!o || (PL_parser && PL_parser->error_count))
5284 if (OP_TYPE_IS_OR_WAS(o, OP_LIST)) {
5286 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
5287 my_kid(kid, attrs, imopsp);
5289 } else if (type == OP_UNDEF || type == OP_STUB) {
5291 } else if (type == OP_RV2SV || /* "our" declaration */
5294 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
5295 S_cant_declare(aTHX_ o);
5297 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
5299 PL_parser->in_my = FALSE;
5300 PL_parser->in_my_stash = NULL;
5301 apply_attrs(GvSTASH(gv),
5302 (type == OP_RV2SV ? GvSVn(gv) :
5303 type == OP_RV2AV ? MUTABLE_SV(GvAVn(gv)) :
5304 type == OP_RV2HV ? MUTABLE_SV(GvHVn(gv)) : MUTABLE_SV(gv)),
5307 o->op_private |= OPpOUR_INTRO;
5310 else if (type == OP_REFGEN || type == OP_SREFGEN) {
5311 if (!FEATURE_MYREF_IS_ENABLED)
5312 Perl_croak(aTHX_ "The experimental declared_refs "
5313 "feature is not enabled");
5314 Perl_ck_warner_d(aTHX_
5315 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
5316 "Declaring references is experimental");
5317 /* Kid is a nulled OP_LIST, handled above. */
5318 my_kid(cUNOPo->op_first, attrs, imopsp);
5321 else if (type != OP_PADSV &&
5324 type != OP_PUSHMARK)
5326 S_cant_declare(aTHX_ o);
5329 else if (attrs && type != OP_PUSHMARK) {
5333 PL_parser->in_my = FALSE;
5334 PL_parser->in_my_stash = NULL;
5336 /* check for C<my Dog $spot> when deciding package */
5337 stash = PAD_COMPNAME_TYPE(o->op_targ);
5339 stash = PL_curstash;
5340 apply_attrs_my(stash, o, attrs, imopsp);
5342 o->op_flags |= OPf_MOD;
5343 o->op_private |= OPpLVAL_INTRO;
5345 o->op_private |= OPpPAD_STATE;
5350 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
5353 int maybe_scalar = 0;
5355 PERL_ARGS_ASSERT_MY_ATTRS;
5357 /* [perl #17376]: this appears to be premature, and results in code such as
5358 C< our(%x); > executing in list mode rather than void mode */
5360 if (o->op_flags & OPf_PARENS)
5370 o = my_kid(o, attrs, &rops);
5372 if (maybe_scalar && o->op_type == OP_PADSV) {
5373 o = scalar(op_append_list(OP_LIST, rops, o));
5374 o->op_private |= OPpLVAL_INTRO;
5377 /* The listop in rops might have a pushmark at the beginning,
5378 which will mess up list assignment. */
5379 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
5380 if (rops->op_type == OP_LIST &&
5381 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
5383 OP * const pushmark = lrops->op_first;
5384 /* excise pushmark */
5385 op_sibling_splice(rops, NULL, 1, NULL);
5388 o = op_append_list(OP_LIST, o, rops);
5391 PL_parser->in_my = FALSE;
5392 PL_parser->in_my_stash = NULL;
5397 Perl_sawparens(pTHX_ OP *o)
5399 PERL_UNUSED_CONTEXT;
5401 o->op_flags |= OPf_PARENS;
5406 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
5410 const OPCODE ltype = left->op_type;
5411 const OPCODE rtype = right->op_type;
5413 PERL_ARGS_ASSERT_BIND_MATCH;
5415 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
5416 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
5418 const char * const desc
5420 rtype == OP_SUBST || rtype == OP_TRANS
5421 || rtype == OP_TRANSR
5423 ? (int)rtype : OP_MATCH];
5424 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
5426 S_op_varname(aTHX_ left);
5428 Perl_warner(aTHX_ packWARN(WARN_MISC),
5429 "Applying %s to %" SVf " will act on scalar(%" SVf ")",
5430 desc, SVfARG(name), SVfARG(name));
5432 const char * const sample = (isary
5433 ? "@array" : "%hash");
5434 Perl_warner(aTHX_ packWARN(WARN_MISC),
5435 "Applying %s to %s will act on scalar(%s)",
5436 desc, sample, sample);
5440 if (rtype == OP_CONST &&
5441 cSVOPx(right)->op_private & OPpCONST_BARE &&
5442 cSVOPx(right)->op_private & OPpCONST_STRICT)
5444 no_bareword_allowed(right);
5447 /* !~ doesn't make sense with /r, so error on it for now */
5448 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
5450 /* diag_listed_as: Using !~ with %s doesn't make sense */
5451 yyerror("Using !~ with s///r doesn't make sense");
5452 if (rtype == OP_TRANSR && type == OP_NOT)
5453 /* diag_listed_as: Using !~ with %s doesn't make sense */
5454 yyerror("Using !~ with tr///r doesn't make sense");
5456 ismatchop = (rtype == OP_MATCH ||
5457 rtype == OP_SUBST ||
5458 rtype == OP_TRANS || rtype == OP_TRANSR)
5459 && !(right->op_flags & OPf_SPECIAL);
5460 if (ismatchop && right->op_private & OPpTARGET_MY) {
5462 right->op_private &= ~OPpTARGET_MY;
5464 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
5465 if (left->op_type == OP_PADSV
5466 && !(left->op_private & OPpLVAL_INTRO))
5468 right->op_targ = left->op_targ;
5473 right->op_flags |= OPf_STACKED;
5474 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
5475 ! (rtype == OP_TRANS &&
5476 right->op_private & OPpTRANS_IDENTICAL) &&
5477 ! (rtype == OP_SUBST &&
5478 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
5479 left = op_lvalue(left, rtype);
5480 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
5481 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
5483 o = op_prepend_elem(rtype, scalar(left), right);
5486 return newUNOP(OP_NOT, 0, scalar(o));
5490 return bind_match(type, left,
5491 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
5495 Perl_invert(pTHX_ OP *o)
5499 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
5503 Perl_cmpchain_start(pTHX_ I32 type, OP *left, OP *right)
5510 left = newOP(OP_NULL, 0);
5512 right = newOP(OP_NULL, 0);
5515 NewOp(0, bop, 1, BINOP);
5517 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP);
5518 OpTYPE_set(op, type);
5519 cBINOPx(op)->op_flags = OPf_KIDS;
5520 cBINOPx(op)->op_private = 2;
5521 cBINOPx(op)->op_first = left;
5522 cBINOPx(op)->op_last = right;
5523 OpMORESIB_set(left, right);
5524 OpLASTSIB_set(right, op);
5529 Perl_cmpchain_extend(pTHX_ I32 type, OP *ch, OP *right)
5535 PERL_ARGS_ASSERT_CMPCHAIN_EXTEND;
5537 right = newOP(OP_NULL, 0);
5539 NewOp(0, bop, 1, BINOP);
5541 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP);
5542 OpTYPE_set(op, type);
5543 if (ch->op_type != OP_NULL) {
5545 OP *nch, *cleft, *cright;
5546 NewOp(0, lch, 1, UNOP);
5548 OpTYPE_set(nch, OP_NULL);
5549 nch->op_flags = OPf_KIDS;
5550 cleft = cBINOPx(ch)->op_first;
5551 cright = cBINOPx(ch)->op_last;
5552 cBINOPx(ch)->op_first = NULL;
5553 cBINOPx(ch)->op_last = NULL;
5554 cBINOPx(ch)->op_private = 0;
5555 cBINOPx(ch)->op_flags = 0;
5556 cUNOPx(nch)->op_first = cright;
5557 OpMORESIB_set(cright, ch);
5558 OpMORESIB_set(ch, cleft);
5559 OpLASTSIB_set(cleft, nch);
5562 OpMORESIB_set(right, op);
5563 OpMORESIB_set(op, cUNOPx(ch)->op_first);
5564 cUNOPx(ch)->op_first = right;
5569 Perl_cmpchain_finish(pTHX_ OP *ch)
5573 PERL_ARGS_ASSERT_CMPCHAIN_FINISH;
5574 if (ch->op_type != OP_NULL) {
5575 OPCODE cmpoptype = ch->op_type;
5576 ch = CHECKOP(cmpoptype, ch);
5577 if(!ch->op_next && ch->op_type == cmpoptype)
5578 ch = fold_constants(op_integerize(op_std_init(ch)));
5582 OP *rightarg = cUNOPx(ch)->op_first;
5583 cUNOPx(ch)->op_first = OpSIBLING(rightarg);
5584 OpLASTSIB_set(rightarg, NULL);
5586 OP *cmpop = cUNOPx(ch)->op_first;
5587 OP *leftarg = OpSIBLING(cmpop);
5588 OPCODE cmpoptype = cmpop->op_type;
5591 is_last = !(cUNOPx(ch)->op_first = OpSIBLING(leftarg));
5592 OpLASTSIB_set(cmpop, NULL);
5593 OpLASTSIB_set(leftarg, NULL);
5597 nextrightarg = NULL;
5599 nextrightarg = newUNOP(OP_CMPCHAIN_DUP, 0, leftarg);
5600 leftarg = newOP(OP_NULL, 0);
5602 cBINOPx(cmpop)->op_first = leftarg;
5603 cBINOPx(cmpop)->op_last = rightarg;
5604 OpMORESIB_set(leftarg, rightarg);
5605 OpLASTSIB_set(rightarg, cmpop);
5606 cmpop->op_flags = OPf_KIDS;
5607 cmpop->op_private = 2;
5608 cmpop = CHECKOP(cmpoptype, cmpop);
5609 if(!cmpop->op_next && cmpop->op_type == cmpoptype)
5610 cmpop = fold_constants(op_integerize(op_std_init(cmpop)));
5611 condop = condop ? newLOGOP(OP_CMPCHAIN_AND, 0, cmpop, condop) :
5615 rightarg = nextrightarg;
5621 =for apidoc op_scope
5623 Wraps up an op tree with some additional ops so that at runtime a dynamic
5624 scope will be created. The original ops run in the new dynamic scope,
5625 and then, provided that they exit normally, the scope will be unwound.
5626 The additional ops used to create and unwind the dynamic scope will
5627 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
5628 instead if the ops are simple enough to not need the full dynamic scope
5635 Perl_op_scope(pTHX_ OP *o)
5639 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
5640 o = op_prepend_elem(OP_LINESEQ,
5641 newOP(OP_ENTER, (o->op_flags & OPf_WANT)), o);
5642 OpTYPE_set(o, OP_LEAVE);
5644 else if (o->op_type == OP_LINESEQ) {
5646 OpTYPE_set(o, OP_SCOPE);
5647 kid = ((LISTOP*)o)->op_first;
5648 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
5651 /* The following deals with things like 'do {1 for 1}' */
5652 kid = OpSIBLING(kid);
5654 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
5659 o = newLISTOP(OP_SCOPE, 0, o, NULL);
5665 Perl_op_unscope(pTHX_ OP *o)
5667 if (o && o->op_type == OP_LINESEQ) {
5668 OP *kid = cLISTOPo->op_first;
5669 for(; kid; kid = OpSIBLING(kid))
5670 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
5677 =for apidoc block_start
5679 Handles compile-time scope entry.
5680 Arranges for hints to be restored on block
5681 exit and also handles pad sequence numbers to make lexical variables scope
5682 right. Returns a savestack index for use with C<block_end>.
5688 Perl_block_start(pTHX_ int full)
5690 const int retval = PL_savestack_ix;
5692 PL_compiling.cop_seq = PL_cop_seqmax;
5694 pad_block_start(full);
5696 PL_hints &= ~HINT_BLOCK_SCOPE;
5697 SAVECOMPILEWARNINGS();
5698 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
5699 SAVEI32(PL_compiling.cop_seq);
5700 PL_compiling.cop_seq = 0;
5702 CALL_BLOCK_HOOKS(bhk_start, full);
5708 =for apidoc block_end
5710 Handles compile-time scope exit. C<floor>
5711 is the savestack index returned by
5712 C<block_start>, and C<seq> is the body of the block. Returns the block,
5719 Perl_block_end(pTHX_ I32 floor, OP *seq)
5721 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
5722 OP* retval = scalarseq(seq);
5725 /* XXX Is the null PL_parser check necessary here? */
5726 assert(PL_parser); /* Let’s find out under debugging builds. */
5727 if (PL_parser && PL_parser->parsed_sub) {
5728 o = newSTATEOP(0, NULL, NULL);
5730 retval = op_append_elem(OP_LINESEQ, retval, o);
5733 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
5737 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
5741 /* pad_leavemy has created a sequence of introcv ops for all my
5742 subs declared in the block. We have to replicate that list with
5743 clonecv ops, to deal with this situation:
5748 sub s1 { state sub foo { \&s2 } }
5751 Originally, I was going to have introcv clone the CV and turn
5752 off the stale flag. Since &s1 is declared before &s2, the
5753 introcv op for &s1 is executed (on sub entry) before the one for
5754 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
5755 cloned, since it is a state sub) closes over &s2 and expects
5756 to see it in its outer CV’s pad. If the introcv op clones &s1,
5757 then &s2 is still marked stale. Since &s1 is not active, and
5758 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
5759 ble will not stay shared’ warning. Because it is the same stub
5760 that will be used when the introcv op for &s2 is executed, clos-
5761 ing over it is safe. Hence, we have to turn off the stale flag
5762 on all lexical subs in the block before we clone any of them.
5763 Hence, having introcv clone the sub cannot work. So we create a
5764 list of ops like this:
5788 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
5789 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
5790 for (;; kid = OpSIBLING(kid)) {
5791 OP *newkid = newOP(OP_CLONECV, 0);
5792 newkid->op_targ = kid->op_targ;
5793 o = op_append_elem(OP_LINESEQ, o, newkid);
5794 if (kid == last) break;
5796 retval = op_prepend_elem(OP_LINESEQ, o, retval);
5799 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
5805 =head1 Compile-time scope hooks
5807 =for apidoc blockhook_register
5809 Register a set of hooks to be called when the Perl lexical scope changes
5810 at compile time. See L<perlguts/"Compile-time scope hooks">.
5816 Perl_blockhook_register(pTHX_ BHK *hk)
5818 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
5820 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
5824 Perl_newPROG(pTHX_ OP *o)
5828 PERL_ARGS_ASSERT_NEWPROG;
5835 PL_eval_root = newUNOP(OP_LEAVEEVAL,
5836 ((PL_in_eval & EVAL_KEEPERR)
5837 ? OPf_SPECIAL : 0), o);
5840 assert(CxTYPE(cx) == CXt_EVAL);
5842 if ((cx->blk_gimme & G_WANT) == G_VOID)
5843 scalarvoid(PL_eval_root);
5844 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
5847 scalar(PL_eval_root);
5849 start = op_linklist(PL_eval_root);
5850 PL_eval_root->op_next = 0;
5851 i = PL_savestack_ix;
5854 S_process_optree(aTHX_ NULL, PL_eval_root, start);
5856 PL_savestack_ix = i;
5859 if (o->op_type == OP_STUB) {
5860 /* This block is entered if nothing is compiled for the main
5861 program. This will be the case for an genuinely empty main
5862 program, or one which only has BEGIN blocks etc, so already
5865 Historically (5.000) the guard above was !o. However, commit
5866 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
5867 c71fccf11fde0068, changed perly.y so that newPROG() is now
5868 called with the output of block_end(), which returns a new
5869 OP_STUB for the case of an empty optree. ByteLoader (and
5870 maybe other things) also take this path, because they set up
5871 PL_main_start and PL_main_root directly, without generating an
5874 If the parsing the main program aborts (due to parse errors,
5875 or due to BEGIN or similar calling exit), then newPROG()
5876 isn't even called, and hence this code path and its cleanups
5877 are skipped. This shouldn't make a make a difference:
5878 * a non-zero return from perl_parse is a failure, and
5879 perl_destruct() should be called immediately.
5880 * however, if exit(0) is called during the parse, then
5881 perl_parse() returns 0, and perl_run() is called. As
5882 PL_main_start will be NULL, perl_run() will return
5883 promptly, and the exit code will remain 0.
5886 PL_comppad_name = 0;
5888 S_op_destroy(aTHX_ o);
5891 PL_main_root = op_scope(sawparens(scalarvoid(o)));
5892 PL_curcop = &PL_compiling;
5893 start = LINKLIST(PL_main_root);
5894 PL_main_root->op_next = 0;
5895 S_process_optree(aTHX_ NULL, PL_main_root, start);
5896 if (!PL_parser->error_count)
5897 /* on error, leave CV slabbed so that ops left lying around
5898 * will eb cleaned up. Else unslab */
5899 cv_forget_slab(PL_compcv);
5902 /* Register with debugger */
5904 CV * const cv = get_cvs("DB::postponed", 0);
5908 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
5910 call_sv(MUTABLE_SV(cv), G_DISCARD);
5917 Perl_localize(pTHX_ OP *o, I32 lex)
5919 PERL_ARGS_ASSERT_LOCALIZE;
5921 if (o->op_flags & OPf_PARENS)
5922 /* [perl #17376]: this appears to be premature, and results in code such as
5923 C< our(%x); > executing in list mode rather than void mode */
5930 if ( PL_parser->bufptr > PL_parser->oldbufptr
5931 && PL_parser->bufptr[-1] == ','
5932 && ckWARN(WARN_PARENTHESIS))
5934 char *s = PL_parser->bufptr;
5937 /* some heuristics to detect a potential error */
5938 while (*s && (memCHRs(", \t\n", *s)))
5942 if (*s && (memCHRs("@$%", *s) || (!lex && *s == '*'))
5944 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
5947 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
5949 while (*s && (memCHRs(", \t\n", *s)))
5955 if (sigil && (*s == ';' || *s == '=')) {
5956 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
5957 "Parentheses missing around \"%s\" list",
5959 ? (PL_parser->in_my == KEY_our
5961 : PL_parser->in_my == KEY_state
5971 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
5972 PL_parser->in_my = FALSE;
5973 PL_parser->in_my_stash = NULL;
5978 Perl_jmaybe(pTHX_ OP *o)
5980 PERL_ARGS_ASSERT_JMAYBE;
5982 if (o->op_type == OP_LIST) {
5984 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
5985 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
5990 PERL_STATIC_INLINE OP *
5991 S_op_std_init(pTHX_ OP *o)
5993 I32 type = o->op_type;
5995 PERL_ARGS_ASSERT_OP_STD_INIT;
5997 if (PL_opargs[type] & OA_RETSCALAR)
5999 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
6000 o->op_targ = pad_alloc(type, SVs_PADTMP);
6005 PERL_STATIC_INLINE OP *
6006 S_op_integerize(pTHX_ OP *o)
6008 I32 type = o->op_type;
6010 PERL_ARGS_ASSERT_OP_INTEGERIZE;
6012 /* integerize op. */
6013 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
6016 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
6019 if (type == OP_NEGATE)
6020 /* XXX might want a ck_negate() for this */
6021 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
6026 /* This function exists solely to provide a scope to limit
6027 setjmp/longjmp() messing with auto variables. It cannot be inlined because
6031 S_fold_constants_eval(pTHX) {
6047 S_fold_constants(pTHX_ OP *const o)
6052 I32 type = o->op_type;
6057 SV * const oldwarnhook = PL_warnhook;
6058 SV * const olddiehook = PL_diehook;
6060 U8 oldwarn = PL_dowarn;
6063 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
6065 if (!(PL_opargs[type] & OA_FOLDCONST))
6074 #ifdef USE_LOCALE_CTYPE
6075 if (IN_LC_COMPILETIME(LC_CTYPE))
6084 #ifdef USE_LOCALE_COLLATE
6085 if (IN_LC_COMPILETIME(LC_COLLATE))
6090 /* XXX what about the numeric ops? */
6091 #ifdef USE_LOCALE_NUMERIC
6092 if (IN_LC_COMPILETIME(LC_NUMERIC))
6097 if (!OpHAS_SIBLING(cLISTOPo->op_first)
6098 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
6101 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
6102 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
6104 const char *s = SvPVX_const(sv);
6105 while (s < SvEND(sv)) {
6106 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
6113 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
6116 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
6117 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
6121 if (PL_parser && PL_parser->error_count)
6122 goto nope; /* Don't try to run w/ errors */
6124 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
6125 switch (curop->op_type) {
6127 if ( (curop->op_private & OPpCONST_BARE)
6128 && (curop->op_private & OPpCONST_STRICT)) {
6129 no_bareword_allowed(curop);
6137 /* Foldable; move to next op in list */
6141 /* No other op types are considered foldable */
6146 curop = LINKLIST(o);
6147 old_next = o->op_next;
6151 old_cxix = cxstack_ix;
6152 create_eval_scope(NULL, G_FAKINGEVAL);
6154 /* Verify that we don't need to save it: */
6155 assert(PL_curcop == &PL_compiling);
6156 StructCopy(&PL_compiling, ¬_compiling, COP);
6157 PL_curcop = ¬_compiling;
6158 /* The above ensures that we run with all the correct hints of the
6159 currently compiling COP, but that IN_PERL_RUNTIME is true. */
6160 assert(IN_PERL_RUNTIME);
6161 PL_warnhook = PERL_WARNHOOK_FATAL;
6164 /* Effective $^W=1. */
6165 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
6166 PL_dowarn |= G_WARN_ON;
6168 ret = S_fold_constants_eval(aTHX);
6172 sv = *(PL_stack_sp--);
6173 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
6174 pad_swipe(o->op_targ, FALSE);
6176 else if (SvTEMP(sv)) { /* grab mortal temp? */
6177 SvREFCNT_inc_simple_void(sv);
6180 else { assert(SvIMMORTAL(sv)); }
6183 /* Something tried to die. Abandon constant folding. */
6184 /* Pretend the error never happened. */
6186 o->op_next = old_next;
6189 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
6190 PL_warnhook = oldwarnhook;
6191 PL_diehook = olddiehook;
6192 /* XXX note that this croak may fail as we've already blown away
6193 * the stack - eg any nested evals */
6194 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
6196 PL_dowarn = oldwarn;
6197 PL_warnhook = oldwarnhook;
6198 PL_diehook = olddiehook;
6199 PL_curcop = &PL_compiling;
6201 /* if we croaked, depending on how we croaked the eval scope
6202 * may or may not have already been popped */
6203 if (cxstack_ix > old_cxix) {
6204 assert(cxstack_ix == old_cxix + 1);
6205 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
6206 delete_eval_scope();
6211 /* OP_STRINGIFY and constant folding are used to implement qq.
6212 Here the constant folding is an implementation detail that we
6213 want to hide. If the stringify op is itself already marked
6214 folded, however, then it is actually a folded join. */
6215 is_stringify = type == OP_STRINGIFY && !o->op_folded;
6220 else if (!SvIMMORTAL(sv)) {
6224 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
6225 if (!is_stringify) newop->op_folded = 1;
6232 /* convert a constant range in list context into an OP_RV2AV, OP_CONST pair;
6233 * the constant value being an AV holding the flattened range.
6237 S_gen_constant_list(pTHX_ OP *o)
6240 OP *curop, *old_next;
6241 SV * const oldwarnhook = PL_warnhook;
6242 SV * const olddiehook = PL_diehook;
6244 U8 oldwarn = PL_dowarn;
6254 if (PL_parser && PL_parser->error_count)
6255 return; /* Don't attempt to run with errors */
6257 curop = LINKLIST(o);
6258 old_next = o->op_next;
6260 op_was_null = o->op_type == OP_NULL;
6261 if (op_was_null) /* b3698342565fb462291fba4b432cfcd05b6eb4e1 */
6262 o->op_type = OP_CUSTOM;
6265 o->op_type = OP_NULL;
6266 S_prune_chain_head(&curop);
6269 old_cxix = cxstack_ix;
6270 create_eval_scope(NULL, G_FAKINGEVAL);
6272 old_curcop = PL_curcop;
6273 StructCopy(old_curcop, ¬_compiling, COP);
6274 PL_curcop = ¬_compiling;
6275 /* The above ensures that we run with all the correct hints of the
6276 current COP, but that IN_PERL_RUNTIME is true. */
6277 assert(IN_PERL_RUNTIME);
6278 PL_warnhook = PERL_WARNHOOK_FATAL;
6282 /* Effective $^W=1. */
6283 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
6284 PL_dowarn |= G_WARN_ON;
6288 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
6289 PL_curstackinfo->si_stack_hwm = 0; /* stop valgrind complaining */
6291 Perl_pp_pushmark(aTHX);
6294 assert (!(curop->op_flags & OPf_SPECIAL));
6295 assert(curop->op_type == OP_RANGE);
6296 Perl_pp_anonlist(aTHX);
6300 o->op_next = old_next;
6304 PL_warnhook = oldwarnhook;
6305 PL_diehook = olddiehook;
6306 Perl_croak(aTHX_ "panic: gen_constant_list JMPENV_PUSH returned %d",
6311 PL_dowarn = oldwarn;
6312 PL_warnhook = oldwarnhook;
6313 PL_diehook = olddiehook;
6314 PL_curcop = old_curcop;
6316 if (cxstack_ix > old_cxix) {
6317 assert(cxstack_ix == old_cxix + 1);
6318 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
6319 delete_eval_scope();
6324 OpTYPE_set(o, OP_RV2AV);
6325 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
6326 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
6327 o->op_opt = 0; /* needs to be revisited in rpeep() */
6328 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
6330 /* replace subtree with an OP_CONST */
6331 curop = ((UNOP*)o)->op_first;
6332 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
6335 if (AvFILLp(av) != -1)
6336 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
6339 SvREADONLY_on(*svp);
6347 =head1 Optree Manipulation Functions
6350 /* List constructors */
6353 =for apidoc op_append_elem
6355 Append an item to the list of ops contained directly within a list-type
6356 op, returning the lengthened list. C<first> is the list-type op,
6357 and C<last> is the op to append to the list. C<optype> specifies the
6358 intended opcode for the list. If C<first> is not already a list of the
6359 right type, it will be upgraded into one. If either C<first> or C<last>
6360 is null, the other is returned unchanged.
6366 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
6374 if (first->op_type != (unsigned)type
6375 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
6377 return newLISTOP(type, 0, first, last);
6380 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
6381 first->op_flags |= OPf_KIDS;
6386 =for apidoc op_append_list
6388 Concatenate the lists of ops contained directly within two list-type ops,
6389 returning the combined list. C<first> and C<last> are the list-type ops
6390 to concatenate. C<optype> specifies the intended opcode for the list.
6391 If either C<first> or C<last> is not already a list of the right type,
6392 it will be upgraded into one. If either C<first> or C<last> is null,
6393 the other is returned unchanged.
6399 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
6407 if (first->op_type != (unsigned)type)
6408 return op_prepend_elem(type, first, last);
6410 if (last->op_type != (unsigned)type)
6411 return op_append_elem(type, first, last);
6413 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
6414 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
6415 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
6416 first->op_flags |= (last->op_flags & OPf_KIDS);
6418 S_op_destroy(aTHX_ last);
6424 =for apidoc op_prepend_elem
6426 Prepend an item to the list of ops contained directly within a list-type
6427 op, returning the lengthened list. C<first> is the op to prepend to the
6428 list, and C<last> is the list-type op. C<optype> specifies the intended
6429 opcode for the list. If C<last> is not already a list of the right type,
6430 it will be upgraded into one. If either C<first> or C<last> is null,
6431 the other is returned unchanged.
6437 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
6445 if (last->op_type == (unsigned)type) {
6446 if (type == OP_LIST) { /* already a PUSHMARK there */
6447 /* insert 'first' after pushmark */
6448 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
6449 if (!(first->op_flags & OPf_PARENS))
6450 last->op_flags &= ~OPf_PARENS;
6453 op_sibling_splice(last, NULL, 0, first);
6454 last->op_flags |= OPf_KIDS;
6458 return newLISTOP(type, 0, first, last);
6462 =for apidoc op_convert_list
6464 Converts C<o> into a list op if it is not one already, and then converts it
6465 into the specified C<type>, calling its check function, allocating a target if
6466 it needs one, and folding constants.
6468 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
6469 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
6470 C<op_convert_list> to make it the right type.
6476 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
6479 if (type < 0) type = -type, flags |= OPf_SPECIAL;
6480 if (!o || o->op_type != OP_LIST)
6481 o = force_list(o, 0);
6484 o->op_flags &= ~OPf_WANT;
6485 o->op_private &= ~OPpLVAL_INTRO;
6488 if (!(PL_opargs[type] & OA_MARK))
6489 op_null(cLISTOPo->op_first);
6491 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
6492 if (kid2 && kid2->op_type == OP_COREARGS) {
6493 op_null(cLISTOPo->op_first);
6494 kid2->op_private |= OPpCOREARGS_PUSHMARK;
6498 if (type != OP_SPLIT)
6499 /* At this point o is a LISTOP, but OP_SPLIT is a PMOP; let
6500 * ck_split() create a real PMOP and leave the op's type as listop
6501 * for now. Otherwise op_free() etc will crash.
6503 OpTYPE_set(o, type);
6505 o->op_flags |= flags;
6506 if (flags & OPf_FOLDED)
6509 o = CHECKOP(type, o);
6510 if (o->op_type != (unsigned)type)
6513 return fold_constants(op_integerize(op_std_init(o)));
6520 =head1 Optree construction
6522 =for apidoc newNULLLIST
6524 Constructs, checks, and returns a new C<stub> op, which represents an
6525 empty list expression.
6531 Perl_newNULLLIST(pTHX)
6533 return newOP(OP_STUB, 0);
6536 /* promote o and any siblings to be a list if its not already; i.e.
6544 * pushmark - o - A - B
6546 * If nullit it true, the list op is nulled.
6550 S_force_list(pTHX_ OP *o, bool nullit)
6552 if (!o || o->op_type != OP_LIST) {
6555 /* manually detach any siblings then add them back later */
6556 rest = OpSIBLING(o);
6557 OpLASTSIB_set(o, NULL);
6559 o = newLISTOP(OP_LIST, 0, o, NULL);
6561 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
6569 =for apidoc newLISTOP
6571 Constructs, checks, and returns an op of any list type. C<type> is
6572 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
6573 C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
6574 supply up to two ops to be direct children of the list op; they are
6575 consumed by this function and become part of the constructed op tree.
6577 For most list operators, the check function expects all the kid ops to be
6578 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
6579 appropriate. What you want to do in that case is create an op of type
6580 C<OP_LIST>, append more children to it, and then call L</op_convert_list>.
6581 See L</op_convert_list> for more information.
6588 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
6592 /* Note that allocating an OP_PUSHMARK can die under Safe.pm if
6593 * pushmark is banned. So do it now while existing ops are in a
6594 * consistent state, in case they suddenly get freed */
6595 OP* const pushop = type == OP_LIST ? newOP(OP_PUSHMARK, 0) : NULL;
6597 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
6598 || type == OP_CUSTOM);
6600 NewOp(1101, listop, 1, LISTOP);
6601 OpTYPE_set(listop, type);
6604 listop->op_flags = (U8)flags;
6608 else if (!first && last)
6611 OpMORESIB_set(first, last);
6612 listop->op_first = first;
6613 listop->op_last = last;
6616 OpMORESIB_set(pushop, first);
6617 listop->op_first = pushop;
6618 listop->op_flags |= OPf_KIDS;
6620 listop->op_last = pushop;
6622 if (listop->op_last)
6623 OpLASTSIB_set(listop->op_last, (OP*)listop);
6625 return CHECKOP(type, listop);
6631 Constructs, checks, and returns an op of any base type (any type that
6632 has no extra fields). C<type> is the opcode. C<flags> gives the
6633 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
6640 Perl_newOP(pTHX_ I32 type, I32 flags)
6645 if (type == -OP_ENTEREVAL) {
6646 type = OP_ENTEREVAL;
6647 flags |= OPpEVAL_BYTES<<8;
6650 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
6651 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
6652 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
6653 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
6655 NewOp(1101, o, 1, OP);
6656 OpTYPE_set(o, type);
6657 o->op_flags = (U8)flags;
6660 o->op_private = (U8)(0 | (flags >> 8));
6661 if (PL_opargs[type] & OA_RETSCALAR)
6663 if (PL_opargs[type] & OA_TARGET)
6664 o->op_targ = pad_alloc(type, SVs_PADTMP);
6665 return CHECKOP(type, o);
6671 Constructs, checks, and returns an op of any unary type. C<type> is
6672 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
6673 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
6674 bits, the eight bits of C<op_private>, except that the bit with value 1
6675 is automatically set. C<first> supplies an optional op to be the direct
6676 child of the unary op; it is consumed by this function and become part
6677 of the constructed op tree.
6679 =for apidoc Amnh||OPf_KIDS
6685 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
6690 if (type == -OP_ENTEREVAL) {
6691 type = OP_ENTEREVAL;
6692 flags |= OPpEVAL_BYTES<<8;
6695 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
6696 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
6697 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
6698 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
6699 || type == OP_SASSIGN
6700 || type == OP_ENTERTRY
6701 || type == OP_CUSTOM
6702 || type == OP_NULL );
6705 first = newOP(OP_STUB, 0);
6706 if (PL_opargs[type] & OA_MARK)
6707 first = force_list(first, 1);
6709 NewOp(1101, unop, 1, UNOP);
6710 OpTYPE_set(unop, type);
6711 unop->op_first = first;
6712 unop->op_flags = (U8)(flags | OPf_KIDS);
6713 unop->op_private = (U8)(1 | (flags >> 8));
6715 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
6716 OpLASTSIB_set(first, (OP*)unop);
6718 unop = (UNOP*) CHECKOP(type, unop);
6722 return fold_constants(op_integerize(op_std_init((OP *) unop)));
6726 =for apidoc newUNOP_AUX
6728 Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux>
6729 initialised to C<aux>
6735 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
6740 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
6741 || type == OP_CUSTOM);
6743 NewOp(1101, unop, 1, UNOP_AUX);
6744 unop->op_type = (OPCODE)type;
6745 unop->op_ppaddr = PL_ppaddr[type];
6746 unop->op_first = first;
6747 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
6748 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
6751 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
6752 OpLASTSIB_set(first, (OP*)unop);
6754 unop = (UNOP_AUX*) CHECKOP(type, unop);
6756 return op_std_init((OP *) unop);
6760 =for apidoc newMETHOP
6762 Constructs, checks, and returns an op of method type with a method name
6763 evaluated at runtime. C<type> is the opcode. C<flags> gives the eight
6764 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
6765 and, shifted up eight bits, the eight bits of C<op_private>, except that
6766 the bit with value 1 is automatically set. C<dynamic_meth> supplies an
6767 op which evaluates method name; it is consumed by this function and
6768 become part of the constructed op tree.
6769 Supported optypes: C<OP_METHOD>.
6775 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
6779 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP
6780 || type == OP_CUSTOM);
6782 NewOp(1101, methop, 1, METHOP);
6784 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1);
6785 methop->op_flags = (U8)(flags | OPf_KIDS);
6786 methop->op_u.op_first = dynamic_meth;
6787 methop->op_private = (U8)(1 | (flags >> 8));
6789 if (!OpHAS_SIBLING(dynamic_meth))
6790 OpLASTSIB_set(dynamic_meth, (OP*)methop);
6794 methop->op_flags = (U8)(flags & ~OPf_KIDS);
6795 methop->op_u.op_meth_sv = const_meth;
6796 methop->op_private = (U8)(0 | (flags >> 8));
6797 methop->op_next = (OP*)methop;
6801 methop->op_rclass_targ = 0;
6803 methop->op_rclass_sv = NULL;
6806 OpTYPE_set(methop, type);
6807 return CHECKOP(type, methop);
6811 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
6812 PERL_ARGS_ASSERT_NEWMETHOP;
6813 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
6817 =for apidoc newMETHOP_named
6819 Constructs, checks, and returns an op of method type with a constant
6820 method name. C<type> is the opcode. C<flags> gives the eight bits of
6821 C<op_flags>, and, shifted up eight bits, the eight bits of
6822 C<op_private>. C<const_meth> supplies a constant method name;
6823 it must be a shared COW string.
6824 Supported optypes: C<OP_METHOD_NAMED>.
6830 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
6831 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
6832 return newMETHOP_internal(type, flags, NULL, const_meth);
6836 =for apidoc newBINOP
6838 Constructs, checks, and returns an op of any binary type. C<type>
6839 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
6840 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
6841 the eight bits of C<op_private>, except that the bit with value 1 or
6842 2 is automatically set as required. C<first> and C<last> supply up to
6843 two ops to be the direct children of the binary op; they are consumed
6844 by this function and become part of the constructed op tree.
6850 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
6855 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
6856 || type == OP_NULL || type == OP_CUSTOM);
6858 NewOp(1101, binop, 1, BINOP);
6861 first = newOP(OP_NULL, 0);
6863 OpTYPE_set(binop, type);
6864 binop->op_first = first;
6865 binop->op_flags = (U8)(flags | OPf_KIDS);
6868 binop->op_private = (U8)(1 | (flags >> 8));
6871 binop->op_private = (U8)(2 | (flags >> 8));
6872 OpMORESIB_set(first, last);
6875 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */
6876 OpLASTSIB_set(last, (OP*)binop);
6878 binop->op_last = OpSIBLING(binop->op_first);
6880 OpLASTSIB_set(binop->op_last, (OP*)binop);
6882 binop = (BINOP*)CHECKOP(type, binop);
6883 if (binop->op_next || binop->op_type != (OPCODE)type)
6886 return fold_constants(op_integerize(op_std_init((OP *)binop)));
6890 Perl_invmap_dump(pTHX_ SV* invlist, UV *map)
6892 const char indent[] = " ";
6894 UV len = _invlist_len(invlist);
6895 UV * array = invlist_array(invlist);
6898 PERL_ARGS_ASSERT_INVMAP_DUMP;
6900 for (i = 0; i < len; i++) {
6901 UV start = array[i];
6902 UV end = (i + 1 < len) ? array[i+1] - 1 : IV_MAX;
6904 PerlIO_printf(Perl_debug_log, "%s[%" UVuf "] 0x%04" UVXf, indent, i, start);
6905 if (end == IV_MAX) {
6906 PerlIO_printf(Perl_debug_log, " .. INFTY");
6908 else if (end != start) {
6909 PerlIO_printf(Perl_debug_log, " .. 0x%04" UVXf, end);
6912 PerlIO_printf(Perl_debug_log, " ");
6915 PerlIO_printf(Perl_debug_log, "\t");
6917 if (map[i] == TR_UNLISTED) {
6918 PerlIO_printf(Perl_debug_log, "TR_UNLISTED\n");
6920 else if (map[i] == TR_SPECIAL_HANDLING) {
6921 PerlIO_printf(Perl_debug_log, "TR_SPECIAL_HANDLING\n");
6924 PerlIO_printf(Perl_debug_log, "0x%04" UVXf "\n", map[i]);
6929 /* Given an OP_TRANS / OP_TRANSR op o, plus OP_CONST ops expr and repl
6930 * containing the search and replacement strings, assemble into
6931 * a translation table attached as o->op_pv.
6932 * Free expr and repl.
6933 * It expects the toker to have already set the
6934 * OPpTRANS_COMPLEMENT
6937 * flags as appropriate; this function may add
6939 * OPpTRANS_CAN_FORCE_UTF8
6940 * OPpTRANS_IDENTICAL
6946 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
6948 /* This function compiles a tr///, from data gathered from toke.c, into a
6949 * form suitable for use by do_trans() in doop.c at runtime.
6951 * It first normalizes the data, while discarding extraneous inputs; then
6952 * writes out the compiled data. The normalization allows for complete
6953 * analysis, and avoids some false negatives and positives earlier versions
6956 * The normalization form is an inversion map (described below in detail).
6957 * This is essentially the compiled form for tr///'s that require UTF-8,
6958 * and its easy to use it to write the 257-byte table for tr///'s that
6959 * don't need UTF-8. That table is identical to what's been in use for
6960 * many perl versions, except that it doesn't handle some edge cases that
6961 * it used to, involving code points above 255. The UTF-8 form now handles
6962 * these. (This could be changed with extra coding should it shown to be
6965 * If the complement (/c) option is specified, the lhs string (tstr) is
6966 * parsed into an inversion list. Complementing these is trivial. Then a
6967 * complemented tstr is built from that, and used thenceforth. This hides
6968 * the fact that it was complemented from almost all successive code.
6970 * One of the important characteristics to know about the input is whether
6971 * the transliteration may be done in place, or does a temporary need to be
6972 * allocated, then copied. If the replacement for every character in every
6973 * possible string takes up no more bytes than the the character it
6974 * replaces, then it can be edited in place. Otherwise the replacement
6975 * could overwrite a byte we are about to read, depending on the strings
6976 * being processed. The comments and variable names here refer to this as
6977 * "growing". Some inputs won't grow, and might even shrink under /d, but
6978 * some inputs could grow, so we have to assume any given one might grow.
6979 * On very long inputs, the temporary could eat up a lot of memory, so we
6980 * want to avoid it if possible. For non-UTF-8 inputs, everything is
6981 * single-byte, so can be edited in place, unless there is something in the
6982 * pattern that could force it into UTF-8. The inversion map makes it
6983 * feasible to determine this. Previous versions of this code pretty much
6984 * punted on determining if UTF-8 could be edited in place. Now, this code
6985 * is rigorous in making that determination.
6987 * Another characteristic we need to know is whether the lhs and rhs are
6988 * identical. If so, and no other flags are present, the only effect of
6989 * the tr/// is to count the characters present in the input that are
6990 * mentioned in the lhs string. The implementation of that is easier and
6991 * runs faster than the more general case. Normalizing here allows for
6992 * accurate determination of this. Previously there were false negatives
6995 * Instead of 'transliterated', the comments here use 'unmapped' for the
6996 * characters that are left unchanged by the operation; otherwise they are
6999 * The lhs of the tr/// is here referred to as the t side.
7000 * The rhs of the tr/// is here referred to as the r side.
7003 SV * const tstr = ((SVOP*)expr)->op_sv;
7004 SV * const rstr = ((SVOP*)repl)->op_sv;
7007 const U8 * t0 = (U8*)SvPV_const(tstr, tlen);
7008 const U8 * r0 = (U8*)SvPV_const(rstr, rlen);
7011 UV t_count = 0, r_count = 0; /* Number of characters in search and
7012 replacement lists */
7014 /* khw thinks some of the private flags for this op are quaintly named.
7015 * OPpTRANS_GROWS for example is TRUE if the replacement for some lhs
7016 * character when represented in UTF-8 is longer than the original
7017 * character's UTF-8 representation */
7018 const bool complement = cBOOL(o->op_private & OPpTRANS_COMPLEMENT);
7019 const bool squash = cBOOL(o->op_private & OPpTRANS_SQUASH);
7020 const bool del = cBOOL(o->op_private & OPpTRANS_DELETE);
7022 /* Set to true if there is some character < 256 in the lhs that maps to
7023 * above 255. If so, a non-UTF-8 match string can be forced into being in
7024 * UTF-8 by a tr/// operation. */
7025 bool can_force_utf8 = FALSE;
7027 /* What is the maximum expansion factor in UTF-8 transliterations. If a
7028 * 2-byte UTF-8 encoded character is to be replaced by a 3-byte one, its
7029 * expansion factor is 1.5. This number is used at runtime to calculate
7030 * how much space to allocate for non-inplace transliterations. Without
7031 * this number, the worst case is 14, which is extremely unlikely to happen
7032 * in real life, and could require significant memory overhead. */
7033 NV max_expansion = 1.;
7035 UV t_range_count, r_range_count, min_range_count;
7040 UV t_cp_end = (UV) -1;
7044 UV final_map = TR_UNLISTED; /* The final character in the replacement
7045 list, updated as we go along. Initialize
7046 to something illegal */
7048 bool rstr_utf8 = cBOOL(SvUTF8(rstr));
7049 bool tstr_utf8 = cBOOL(SvUTF8(tstr));
7051 const U8* tend = t + tlen;
7052 const U8* rend = r + rlen;
7054 SV * inverted_tstr = NULL;
7059 /* This routine implements detection of a transliteration having a longer
7060 * UTF-8 representation than its source, by partitioning all the possible
7061 * code points of the platform into equivalence classes of the same UTF-8
7062 * byte length in the first pass. As it constructs the mappings, it carves
7063 * these up into smaller chunks, but doesn't merge any together. This
7064 * makes it easy to find the instances it's looking for. A second pass is
7065 * done after this has been determined which merges things together to
7066 * shrink the table for runtime. For ASCII platforms, the table is
7067 * trivial, given below, and uses the fundamental characteristics of UTF-8
7068 * to construct the values. For EBCDIC, it isn't so, and we rely on a
7069 * table constructed by the perl script that generates these kinds of
7072 UV PL_partition_by_byte_length[] = {
7074 0x80, /* Below this is 1 byte representations */
7075 (32 * (1UL << ( UTF_ACCUMULATION_SHIFT))), /* 2 bytes below this */
7076 (16 * (1UL << (2 * UTF_ACCUMULATION_SHIFT))), /* 3 bytes below this */
7077 ( 8 * (1UL << (3 * UTF_ACCUMULATION_SHIFT))), /* 4 bytes below this */
7078 ( 4 * (1UL << (4 * UTF_ACCUMULATION_SHIFT))), /* 5 bytes below this */
7079 ( 2 * (1UL << (5 * UTF_ACCUMULATION_SHIFT))) /* 6 bytes below this */
7083 ( ((UV) 1U << (6 * UTF_ACCUMULATION_SHIFT))) /* 7 bytes below this */
7090 PERL_ARGS_ASSERT_PMTRANS;
7092 PL_hints |= HINT_BLOCK_SCOPE;
7094 /* If /c, the search list is sorted and complemented. This is now done by
7095 * creating an inversion list from it, and then trivially inverting that.
7096 * The previous implementation used qsort, but creating the list
7097 * automatically keeps it sorted as we go along */
7100 SV * inverted_tlist = _new_invlist(tlen);
7103 DEBUG_y(PerlIO_printf(Perl_debug_log,
7104 "%s: %d: tstr before inversion=\n%s\n",
7105 __FILE__, __LINE__, _byte_dump_string(t, tend - t, 0)));
7109 /* Non-utf8 strings don't have ranges, so each character is listed
7112 inverted_tlist = add_cp_to_invlist(inverted_tlist, *t);
7115 else { /* But UTF-8 strings have been parsed in toke.c to have
7116 * ranges if appropriate. */
7120 /* Get the first character */
7121 t_cp = valid_utf8_to_uvchr(t, &t_char_len);
7124 /* If the next byte indicates that this wasn't the first
7125 * element of a range, the range is just this one */
7126 if (t >= tend || *t != RANGE_INDICATOR) {
7127 inverted_tlist = add_cp_to_invlist(inverted_tlist, t_cp);
7129 else { /* Otherwise, ignore the indicator byte, and get the
7130 final element, and add the whole range */
7132 t_cp_end = valid_utf8_to_uvchr(t, &t_char_len);
7135 inverted_tlist = _add_range_to_invlist(inverted_tlist,
7139 } /* End of parse through tstr */
7141 /* The inversion list is done; now invert it */
7142 _invlist_invert(inverted_tlist);
7144 /* Now go through the inverted list and create a new tstr for the rest
7145 * of the routine to use. Since the UTF-8 version can have ranges, and
7146 * can be much more compact than the non-UTF-8 version, we create the
7147 * string in UTF-8 even if not necessary. (This is just an intermediate
7148 * value that gets thrown away anyway.) */
7149 invlist_iterinit(inverted_tlist);
7150 inverted_tstr = newSVpvs("");
7151 while (invlist_iternext(inverted_tlist, &start, &end)) {
7152 U8 temp[UTF8_MAXBYTES];
7155 /* IV_MAX keeps things from going out of bounds */
7156 start = MIN(IV_MAX, start);
7157 end = MIN(IV_MAX, end);
7159 temp_end_pos = uvchr_to_utf8(temp, start);
7160 sv_catpvn(inverted_tstr, (char *) temp, temp_end_pos - temp);
7163 Perl_sv_catpvf(aTHX_ inverted_tstr, "%c", RANGE_INDICATOR);
7164 temp_end_pos = uvchr_to_utf8(temp, end);
7165 sv_catpvn(inverted_tstr, (char *) temp, temp_end_pos - temp);
7169 /* Set up so the remainder of the routine uses this complement, instead
7170 * of the actual input */
7171 t0 = t = (U8*)SvPV_const(inverted_tstr, temp_len);
7172 tend = t0 + temp_len;
7175 SvREFCNT_dec_NN(inverted_tlist);
7178 /* For non-/d, an empty rhs means to use the lhs */
7179 if (rlen == 0 && ! del) {
7182 rstr_utf8 = tstr_utf8;
7185 t_invlist = _new_invlist(1);
7187 /* Initialize to a single range */
7188 t_invlist = _add_range_to_invlist(t_invlist, 0, UV_MAX);
7190 /* For the first pass, the lhs is partitioned such that the
7191 * number of UTF-8 bytes required to represent a code point in each
7192 * partition is the same as the number for any other code point in
7193 * that partion. We copy the pre-compiled partion. */
7194 len = C_ARRAY_LENGTH(PL_partition_by_byte_length);
7195 invlist_extend(t_invlist, len);
7196 t_array = invlist_array(t_invlist);
7197 Copy(PL_partition_by_byte_length, t_array, len, UV);
7198 invlist_set_len(t_invlist, len, *(get_invlist_offset_addr(t_invlist)));
7199 Newx(r_map, len + 1, UV);
7201 /* Parse the (potentially adjusted) input, creating the inversion map.
7202 * This is done in two passes. The first pass is to determine if the
7203 * transliteration can be done in place. The inversion map it creates
7204 * could be used, but generally would be larger and slower to run than the
7205 * output of the second pass, which starts with a more compact table and
7206 * allows more ranges to be merged */
7207 for (pass2 = 0; pass2 < 2; pass2++) {
7209 /* Initialize to a single range */
7210 t_invlist = _add_range_to_invlist(t_invlist, 0, UV_MAX);
7212 /* In the second pass, we just have the single range */
7214 t_array = invlist_array(t_invlist);
7217 /* And the mapping of each of the ranges is initialized. Initially,
7218 * everything is TR_UNLISTED. */
7219 for (i = 0; i < len; i++) {
7220 r_map[i] = TR_UNLISTED;
7227 t_range_count = r_range_count = 0;
7229 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d:\ntstr=%s\n",
7230 __FILE__, __LINE__, _byte_dump_string(t, tend - t, 0)));
7231 DEBUG_y(PerlIO_printf(Perl_debug_log, "rstr=%s\n",
7232 _byte_dump_string(r, rend - r, 0)));
7233 DEBUG_y(PerlIO_printf(Perl_debug_log, "/c=%d; /s=%d; /d=%d\n",
7234 complement, squash, del));
7235 DEBUG_y(invmap_dump(t_invlist, r_map));
7237 /* Now go through the search list constructing an inversion map. The
7238 * input is not necessarily in any particular order. Making it an
7239 * inversion map orders it, potentially simplifying, and makes it easy
7240 * to deal with at run time. This is the only place in core that
7241 * generates an inversion map; if others were introduced, it might be
7242 * better to create general purpose routines to handle them.
7243 * (Inversion maps are created in perl in other places.)
7245 * An inversion map consists of two parallel arrays. One is
7246 * essentially an inversion list: an ordered list of code points such
7247 * that each element gives the first code point of a range of
7248 * consecutive code points that map to the element in the other array
7249 * that has the same index as this one (in other words, the
7250 * corresponding element). Thus the range extends up to (but not
7251 * including) the code point given by the next higher element. In a
7252 * true inversion map, the corresponding element in the other array
7253 * gives the mapping of the first code point in the range, with the
7254 * understanding that the next higher code point in the inversion
7255 * list's range will map to the next higher code point in the map.
7257 * So if at element [i], let's say we have:
7262 * This means that A => a, B => b, C => c.... Let's say that the
7263 * situation is such that:
7267 * This means the sequence that started at [i] stops at K => k. This
7268 * illustrates that you need to look at the next element to find where
7269 * a sequence stops. Except, the highest element in the inversion list
7270 * begins a range that is understood to extend to the platform's
7273 * This routine modifies traditional inversion maps to reserve two
7276 * TR_UNLISTED (or -1) indicates that no code point in the range
7277 * is listed in the tr/// searchlist. At runtime, these are
7278 * always passed through unchanged. In the inversion map, all
7279 * points in the range are mapped to -1, instead of increasing,
7280 * like the 'L' in the example above.
7282 * We start the parse with every code point mapped to this, and as
7283 * we parse and find ones that are listed in the search list, we
7284 * carve out ranges as we go along that override that.
7286 * TR_SPECIAL_HANDLING (or -2) indicates that every code point in the
7287 * range needs special handling. Again, all code points in the
7288 * range are mapped to -2, instead of increasing.
7290 * Under /d this value means the code point should be deleted from
7291 * the transliteration when encountered.
7293 * Otherwise, it marks that every code point in the range is to
7294 * map to the final character in the replacement list. This
7295 * happens only when the replacement list is shorter than the
7296 * search one, so there are things in the search list that have no
7297 * correspondence in the replacement list. For example, in
7298 * tr/a-z/A/, 'A' is the final value, and the inversion map
7299 * generated for this would be like this:
7304 * 'A' appears once, then the remainder of the range maps to -2.
7305 * The use of -2 isn't strictly necessary, as an inversion map is
7306 * capable of representing this situation, but not nearly so
7307 * compactly, and this is actually quite commonly encountered.
7308 * Indeed, the original design of this code used a full inversion
7309 * map for this. But things like
7311 * generated huge data structures, slowly, and the execution was
7312 * also slow. So the current scheme was implemented.
7314 * So, if the next element in our example is:
7318 * Then all of L, M, N, O, and P map to TR_UNLISTED. If the next
7322 * [i+4] S TR_UNLISTED
7324 * Then Q => q; R => z; and S => TR_UNLISTED. If [i+4] (the 'S') is
7325 * the final element in the arrays, every code point from S to infinity
7326 * maps to TR_UNLISTED.
7329 /* Finish up range started in what otherwise would
7330 * have been the final iteration */
7331 while (t < tend || t_range_count > 0) {
7332 bool adjacent_to_range_above = FALSE;
7333 bool adjacent_to_range_below = FALSE;
7335 bool merge_with_range_above = FALSE;
7336 bool merge_with_range_below = FALSE;
7338 UV span, invmap_range_length_remaining;
7342 /* If we are in the middle of processing a range in the 'target'
7343 * side, the previous iteration has set us up. Otherwise, look at
7344 * the next character in the search list */
7345 if (t_range_count <= 0) {
7348 /* Here, not in the middle of a range, and not UTF-8. The
7349 * next code point is the single byte where we're at */
7357 /* Here, not in the middle of a range, and is UTF-8. The
7358 * next code point is the next UTF-8 char in the input. We
7359 * know the input is valid, because the toker constructed
7361 t_cp = valid_utf8_to_uvchr(t, &t_char_len);
7364 /* UTF-8 strings (only) have been parsed in toke.c to have
7365 * ranges. See if the next byte indicates that this was
7366 * the first element of a range. If so, get the final
7367 * element and calculate the range size. If not, the range
7369 if (t < tend && *t == RANGE_INDICATOR) {
7371 t_range_count = valid_utf8_to_uvchr(t, &t_char_len)
7380 /* Count the total number of listed code points * */
7381 t_count += t_range_count;
7384 /* Similarly, get the next character in the replacement list */
7385 if (r_range_count <= 0) {
7388 /* But if we've exhausted the rhs, there is nothing to map
7389 * to, except the special handling one, and we make the
7390 * range the same size as the lhs one. */
7391 r_cp = TR_SPECIAL_HANDLING;
7392 r_range_count = t_range_count;
7395 DEBUG_yv(PerlIO_printf(Perl_debug_log,
7396 "final_map =%" UVXf "\n", final_map));
7408 r_cp = valid_utf8_to_uvchr(r, &r_char_len);
7410 if (r < rend && *r == RANGE_INDICATOR) {
7412 r_range_count = valid_utf8_to_uvchr(r,
7413 &r_char_len) - r_cp + 1;
7421 if (r_cp == TR_SPECIAL_HANDLING) {
7422 r_range_count = t_range_count;
7425 /* This is the final character so far */
7426 final_map = r_cp + r_range_count - 1;
7428 r_count += r_range_count;
7432 /* Here, we have the next things ready in both sides. They are
7433 * potentially ranges. We try to process as big a chunk as
7434 * possible at once, but the lhs and rhs must be synchronized, so
7435 * things like tr/A-Z/a-ij-z/ will need to be processed in 2 chunks
7437 min_range_count = MIN(t_range_count, r_range_count);
7439 /* Search the inversion list for the entry that contains the input
7440 * code point <cp>. The inversion map was initialized to cover the
7441 * entire range of possible inputs, so this should not fail. So
7442 * the return value is the index into the list's array of the range
7443 * that contains <cp>, that is, 'i' such that array[i] <= cp <
7445 j = _invlist_search(t_invlist, t_cp);
7449 /* Here, the data structure might look like:
7452 * [i-1] J j # J-L => j-l
7453 * [i] M -1 # M => default; as do N, O, P, Q
7454 * [i+1] R x # R => x, S => x+1, T => x+2
7455 * [i+2] U y # U => y, V => y+1, ...
7457 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
7459 * where 'x' and 'y' above are not to be taken literally.
7461 * The maximum chunk we can handle in this loop iteration, is the
7462 * smallest of the three components: the lhs 't_', the rhs 'r_',
7463 * and the remainder of the range in element [i]. (In pass 1, that
7464 * range will have everything in it be of the same class; we can't
7465 * cross into another class.) 'min_range_count' already contains
7466 * the smallest of the first two values. The final one is
7467 * irrelevant if the map is to the special indicator */
7469 invmap_range_length_remaining = (i + 1 < len)
7470 ? t_array[i+1] - t_cp
7472 span = MAX(1, MIN(min_range_count, invmap_range_length_remaining));
7474 /* The end point of this chunk is where we are, plus the span, but
7475 * never larger than the platform's infinity */
7476 t_cp_end = MIN(IV_MAX, t_cp + span - 1);
7478 if (r_cp == TR_SPECIAL_HANDLING) {
7480 /* If unmatched lhs code points map to the final map, use that
7481 * value. This being set to TR_SPECIAL_HANDLING indicates that
7482 * we don't have a final map: unmatched lhs code points are
7484 r_cp_end = (del) ? TR_SPECIAL_HANDLING : final_map;
7487 r_cp_end = MIN(IV_MAX, r_cp + span - 1);
7489 /* If something on the lhs is below 256, and something on the
7490 * rhs is above, there is a potential mapping here across that
7491 * boundary. Indeed the only way there isn't is if both sides
7492 * start at the same point. That means they both cross at the
7493 * same time. But otherwise one crosses before the other */
7494 if (t_cp < 256 && r_cp_end > 255 && r_cp != t_cp) {
7495 can_force_utf8 = TRUE;
7499 /* If a character appears in the search list more than once, the
7500 * 2nd and succeeding occurrences are ignored, so only do this
7501 * range if haven't already processed this character. (The range
7502 * has been set up so that all members in it will be of the same
7504 if (r_map[i] == TR_UNLISTED) {
7505 DEBUG_yv(PerlIO_printf(Perl_debug_log,
7506 "Processing %" UVxf "-%" UVxf " => %" UVxf "-%" UVxf "\n",
7507 t_cp, t_cp_end, r_cp, r_cp_end));
7509 /* This is the first definition for this chunk, hence is valid
7510 * and needs to be processed. Here and in the comments below,
7511 * we use the above sample data. The t_cp chunk must be any
7512 * contiguous subset of M, N, O, P, and/or Q.
7514 * In the first pass, calculate if there is any possible input
7515 * string that has a character whose transliteration will be
7516 * longer than it. If none, the transliteration may be done
7517 * in-place, as it can't write over a so-far unread byte.
7518 * Otherwise, a copy must first be made. This could be
7519 * expensive for long inputs.
7521 * In the first pass, the t_invlist has been partitioned so
7522 * that all elements in any single range have the same number
7523 * of bytes in their UTF-8 representations. And the r space is
7524 * either a single byte, or a range of strictly monotonically
7525 * increasing code points. So the final element in the range
7526 * will be represented by no fewer bytes than the initial one.
7527 * That means that if the final code point in the t range has
7528 * at least as many bytes as the final code point in the r,
7529 * then all code points in the t range have at least as many
7530 * bytes as their corresponding r range element. But if that's
7531 * not true, the transliteration of at least the final code
7532 * point grows in length. As an example, suppose we had
7533 * tr/\x{fff0}-\x{fff1}/\x{ffff}-\x{10000}/
7534 * The UTF-8 for all but 10000 occupies 3 bytes on ASCII
7535 * platforms. We have deliberately set up the data structure
7536 * so that any range in the lhs gets split into chunks for
7537 * processing, such that every code point in a chunk has the
7538 * same number of UTF-8 bytes. We only have to check the final
7539 * code point in the rhs against any code point in the lhs. */
7541 && r_cp_end != TR_SPECIAL_HANDLING
7542 && UVCHR_SKIP(t_cp_end) < UVCHR_SKIP(r_cp_end))
7544 /* Here, we will need to make a copy of the input string
7545 * before doing the transliteration. The worst possible
7546 * case is an expansion ratio of 14:1. This is rare, and
7547 * we'd rather allocate only the necessary amount of extra
7548 * memory for that copy. We can calculate the worst case
7549 * for this particular transliteration is by keeping track
7550 * of the expansion factor for each range.
7552 * Consider tr/\xCB/\X{E000}/. The maximum expansion
7553 * factor is 1 byte going to 3 if the target string is not
7554 * UTF-8, but 2 bytes going to 3 if it is in UTF-8. We
7555 * could pass two different values so doop could choose
7556 * based on the UTF-8ness of the target. But khw thinks
7557 * (perhaps wrongly) that is overkill. It is used only to
7558 * make sure we malloc enough space.
7560 * If no target string can force the result to be UTF-8,
7561 * then we don't have to worry about the case of the target
7562 * string not being UTF-8 */
7563 NV t_size = (can_force_utf8 && t_cp < 256)
7565 : UVCHR_SKIP(t_cp_end);
7566 NV ratio = UVCHR_SKIP(r_cp_end) / t_size;
7568 o->op_private |= OPpTRANS_GROWS;
7570 /* Now that we know it grows, we can keep track of the
7572 if (ratio > max_expansion) {
7573 max_expansion = ratio;
7574 DEBUG_y(PerlIO_printf(Perl_debug_log,
7575 "New expansion factor: %" NVgf "\n",
7580 /* The very first range is marked as adjacent to the
7581 * non-existent range below it, as it causes things to "just
7584 * If the lowest code point in this chunk is M, it adjoins the
7586 if (t_cp == t_array[i]) {
7587 adjacent_to_range_below = TRUE;
7589 /* And if the map has the same offset from the beginning of
7590 * the range as does this new code point (or both are for
7591 * TR_SPECIAL_HANDLING), this chunk can be completely
7592 * merged with the range below. EXCEPT, in the first pass,
7593 * we don't merge ranges whose UTF-8 byte representations
7594 * have different lengths, so that we can more easily
7595 * detect if a replacement is longer than the source, that
7596 * is if it 'grows'. But in the 2nd pass, there's no
7597 * reason to not merge */
7598 if ( (i > 0 && ( pass2
7599 || UVCHR_SKIP(t_array[i-1])
7600 == UVCHR_SKIP(t_cp)))
7601 && ( ( r_cp == TR_SPECIAL_HANDLING
7602 && r_map[i-1] == TR_SPECIAL_HANDLING)
7603 || ( r_cp != TR_SPECIAL_HANDLING
7604 && r_cp - r_map[i-1] == t_cp - t_array[i-1])))
7606 merge_with_range_below = TRUE;
7610 /* Similarly, if the highest code point in this chunk is 'Q',
7611 * it adjoins the range above, and if the map is suitable, can
7612 * be merged with it */
7613 if ( t_cp_end >= IV_MAX - 1
7615 && t_cp_end + 1 == t_array[i+1]))
7617 adjacent_to_range_above = TRUE;
7620 || UVCHR_SKIP(t_cp) == UVCHR_SKIP(t_array[i+1]))
7621 && ( ( r_cp == TR_SPECIAL_HANDLING
7622 && r_map[i+1] == (UV) TR_SPECIAL_HANDLING)
7623 || ( r_cp != TR_SPECIAL_HANDLING
7624 && r_cp_end == r_map[i+1] - 1)))
7626 merge_with_range_above = TRUE;
7630 if (merge_with_range_below && merge_with_range_above) {
7632 /* Here the new chunk looks like M => m, ... Q => q; and
7633 * the range above is like R => r, .... Thus, the [i-1]
7634 * and [i+1] ranges should be seamlessly melded so the
7637 * [i-1] J j # J-T => j-t
7638 * [i] U y # U => y, V => y+1, ...
7640 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
7642 Move(t_array + i + 2, t_array + i, len - i - 2, UV);
7643 Move(r_map + i + 2, r_map + i, len - i - 2, UV);
7645 invlist_set_len(t_invlist,
7647 *(get_invlist_offset_addr(t_invlist)));
7649 else if (merge_with_range_below) {
7651 /* Here the new chunk looks like M => m, .... But either
7652 * (or both) it doesn't extend all the way up through Q; or
7653 * the range above doesn't start with R => r. */
7654 if (! adjacent_to_range_above) {
7656 /* In the first case, let's say the new chunk extends
7657 * through O. We then want:
7659 * [i-1] J j # J-O => j-o
7660 * [i] P -1 # P => -1, Q => -1
7661 * [i+1] R x # R => x, S => x+1, T => x+2
7662 * [i+2] U y # U => y, V => y+1, ...
7664 * [-1] Z -1 # Z => default; as do Z+1, ...
7667 t_array[i] = t_cp_end + 1;
7668 r_map[i] = TR_UNLISTED;
7670 else { /* Adjoins the range above, but can't merge with it
7671 (because 'x' is not the next map after q) */
7673 * [i-1] J j # J-Q => j-q
7674 * [i] R x # R => x, S => x+1, T => x+2
7675 * [i+1] U y # U => y, V => y+1, ...
7677 * [-1] Z -1 # Z => default; as do Z+1, ...
7681 Move(t_array + i + 1, t_array + i, len - i - 1, UV);
7682 Move(r_map + i + 1, r_map + i, len - i - 1, UV);
7684 invlist_set_len(t_invlist, len,
7685 *(get_invlist_offset_addr(t_invlist)));
7688 else if (merge_with_range_above) {
7690 /* Here the new chunk ends with Q => q, and the range above
7691 * must start with R => r, so the two can be merged. But
7692 * either (or both) the new chunk doesn't extend all the
7693 * way down to M; or the mapping of the final code point
7694 * range below isn't m */
7695 if (! adjacent_to_range_below) {
7697 /* In the first case, let's assume the new chunk starts
7698 * with P => p. Then, because it's merge-able with the
7699 * range above, that range must be R => r. We want:
7701 * [i-1] J j # J-L => j-l
7702 * [i] M -1 # M => -1, N => -1
7703 * [i+1] P p # P-T => p-t
7704 * [i+2] U y # U => y, V => y+1, ...
7706 * [-1] Z -1 # Z => default; as do Z+1, ...
7709 t_array[i+1] = t_cp;
7712 else { /* Adjoins the range below, but can't merge with it
7715 * [i-1] J j # J-L => j-l
7716 * [i] M x # M-T => x-5 .. x+2
7717 * [i+1] U y # U => y, V => y+1, ...
7719 * [-1] Z -1 # Z => default; as do Z+1, ...
7722 Move(t_array + i + 1, t_array + i, len - i - 1, UV);
7723 Move(r_map + i + 1, r_map + i, len - i - 1, UV);
7727 invlist_set_len(t_invlist, len,
7728 *(get_invlist_offset_addr(t_invlist)));
7731 else if (adjacent_to_range_below && adjacent_to_range_above) {
7732 /* The new chunk completely fills the gap between the
7733 * ranges on either side, but can't merge with either of
7736 * [i-1] J j # J-L => j-l
7737 * [i] M z # M => z, N => z+1 ... Q => z+4
7738 * [i+1] R x # R => x, S => x+1, T => x+2
7739 * [i+2] U y # U => y, V => y+1, ...
7741 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
7745 else if (adjacent_to_range_below) {
7746 /* The new chunk adjoins the range below, but not the range
7747 * above, and can't merge. Let's assume the chunk ends at
7750 * [i-1] J j # J-L => j-l
7751 * [i] M z # M => z, N => z+1, O => z+2
7752 * [i+1] P -1 # P => -1, Q => -1
7753 * [i+2] R x # R => x, S => x+1, T => x+2
7754 * [i+3] U y # U => y, V => y+1, ...
7756 * [-w] Z -1 # Z => default; as do Z+1, ... infinity
7758 invlist_extend(t_invlist, len + 1);
7759 t_array = invlist_array(t_invlist);
7760 Renew(r_map, len + 1, UV);
7762 Move(t_array + i + 1, t_array + i + 2, len - i - 1, UV);
7763 Move(r_map + i + 1, r_map + i + 2, len - i - 1, UV);
7765 t_array[i+1] = t_cp_end + 1;
7766 r_map[i+1] = TR_UNLISTED;
7768 invlist_set_len(t_invlist, len,
7769 *(get_invlist_offset_addr(t_invlist)));
7771 else if (adjacent_to_range_above) {
7772 /* The new chunk adjoins the range above, but not the range
7773 * below, and can't merge. Let's assume the new chunk
7776 * [i-1] J j # J-L => j-l
7777 * [i] M -1 # M => default, N => default
7778 * [i+1] O z # O => z, P => z+1, Q => z+2
7779 * [i+2] R x # R => x, S => x+1, T => x+2
7780 * [i+3] U y # U => y, V => y+1, ...
7782 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
7784 invlist_extend(t_invlist, len + 1);
7785 t_array = invlist_array(t_invlist);
7786 Renew(r_map, len + 1, UV);
7788 Move(t_array + i + 1, t_array + i + 2, len - i - 1, UV);
7789 Move(r_map + i + 1, r_map + i + 2, len - i - 1, UV);
7790 t_array[i+1] = t_cp;
7793 invlist_set_len(t_invlist, len,
7794 *(get_invlist_offset_addr(t_invlist)));
7797 /* The new chunk adjoins neither the range above, nor the
7798 * range below. Lets assume it is N..P => n..p
7800 * [i-1] J j # J-L => j-l
7801 * [i] M -1 # M => default
7802 * [i+1] N n # N..P => n..p
7803 * [i+2] Q -1 # Q => default
7804 * [i+3] R x # R => x, S => x+1, T => x+2
7805 * [i+4] U y # U => y, V => y+1, ...
7807 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
7810 DEBUG_yv(PerlIO_printf(Perl_debug_log,
7811 "Before fixing up: len=%d, i=%d\n",
7812 (int) len, (int) i));
7813 DEBUG_yv(invmap_dump(t_invlist, r_map));
7815 invlist_extend(t_invlist, len + 2);
7816 t_array = invlist_array(t_invlist);
7817 Renew(r_map, len + 2, UV);
7819 Move(t_array + i + 1,
7820 t_array + i + 2 + 1, len - i - (2 - 1), UV);
7822 r_map + i + 2 + 1, len - i - (2 - 1), UV);
7825 invlist_set_len(t_invlist, len,
7826 *(get_invlist_offset_addr(t_invlist)));
7828 t_array[i+1] = t_cp;
7831 t_array[i+2] = t_cp_end + 1;
7832 r_map[i+2] = TR_UNLISTED;
7834 DEBUG_yv(PerlIO_printf(Perl_debug_log,
7835 "After iteration: span=%" UVuf ", t_range_count=%"
7836 UVuf " r_range_count=%" UVuf "\n",
7837 span, t_range_count, r_range_count));
7838 DEBUG_yv(invmap_dump(t_invlist, r_map));
7839 } /* End of this chunk needs to be processed */
7841 /* Done with this chunk. */
7843 if (t_cp >= IV_MAX) {
7846 t_range_count -= span;
7847 if (r_cp != TR_SPECIAL_HANDLING) {
7849 r_range_count -= span;
7855 } /* End of loop through the search list */
7857 /* We don't need an exact count, but we do need to know if there is
7858 * anything left over in the replacement list. So, just assume it's
7859 * one byte per character */
7863 } /* End of passes */
7865 SvREFCNT_dec(inverted_tstr);
7867 DEBUG_y(PerlIO_printf(Perl_debug_log, "After everything: \n"));
7868 DEBUG_y(invmap_dump(t_invlist, r_map));
7870 /* We now have normalized the input into an inversion map.
7872 * See if the lhs and rhs are equivalent. If so, this tr/// is a no-op
7873 * except for the count, and streamlined runtime code can be used */
7874 if (!del && !squash) {
7876 /* They are identical if they point to same address, or if everything
7877 * maps to UNLISTED or to itself. This catches things that not looking
7878 * at the normalized inversion map doesn't catch, like tr/aa/ab/ or
7879 * tr/\x{100}-\x{104}/\x{100}-\x{102}\x{103}-\x{104} */
7881 for (i = 0; i < len; i++) {
7882 if (r_map[i] != TR_UNLISTED && r_map[i] != t_array[i]) {
7883 goto done_identical_check;
7888 /* Here have gone through entire list, and didn't find any
7889 * non-identical mappings */
7890 o->op_private |= OPpTRANS_IDENTICAL;
7892 done_identical_check: ;
7895 t_array = invlist_array(t_invlist);
7897 /* If has components above 255, we generally need to use the inversion map
7901 && t_array[len-1] > 255
7902 /* If the final range is 0x100-INFINITY and is a special
7903 * mapping, the table implementation can handle it */
7904 && ! ( t_array[len-1] == 256
7905 && ( r_map[len-1] == TR_UNLISTED
7906 || r_map[len-1] == TR_SPECIAL_HANDLING))))
7910 /* A UTF-8 op is generated, indicated by this flag. This op is an
7912 o->op_private |= OPpTRANS_USE_SVOP;
7914 if (can_force_utf8) {
7915 o->op_private |= OPpTRANS_CAN_FORCE_UTF8;
7918 /* The inversion map is pushed; first the list. */
7919 invmap = MUTABLE_AV(newAV());
7920 av_push(invmap, t_invlist);
7922 /* 2nd is the mapping */
7923 r_map_sv = newSVpvn((char *) r_map, len * sizeof(UV));
7924 av_push(invmap, r_map_sv);
7926 /* 3rd is the max possible expansion factor */
7927 av_push(invmap, newSVnv(max_expansion));
7929 /* Characters that are in the search list, but not in the replacement
7930 * list are mapped to the final character in the replacement list */
7931 if (! del && r_count < t_count) {
7932 av_push(invmap, newSVuv(final_map));
7936 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
7937 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
7938 PAD_SETSV(cPADOPo->op_padix, (SV *) invmap);
7939 SvPADTMP_on(invmap);
7940 SvREADONLY_on(invmap);
7942 cSVOPo->op_sv = (SV *) invmap;
7950 /* The OPtrans_map struct already contains one slot; hence the -1. */
7951 SSize_t struct_size = sizeof(OPtrans_map)
7952 + (256 - 1 + 1)*sizeof(short);
7954 /* Non-utf8 case: set o->op_pv to point to a simple 256+ entry lookup
7955 * table. Entries with the value TR_UNMAPPED indicate chars not to be
7956 * translated, while TR_DELETE indicates a search char without a
7957 * corresponding replacement char under /d.
7959 * In addition, an extra slot at the end is used to store the final
7960 * repeating char, or TR_R_EMPTY under an empty replacement list, or
7961 * TR_DELETE under /d; which makes the runtime code easier.
7964 /* Indicate this is an op_pv */
7965 o->op_private &= ~OPpTRANS_USE_SVOP;
7967 tbl = (OPtrans_map*)PerlMemShared_calloc(struct_size, 1);
7969 cPVOPo->op_pv = (char*)tbl;
7971 for (i = 0; i < len; i++) {
7972 STATIC_ASSERT_DECL(TR_SPECIAL_HANDLING == TR_DELETE);
7973 short upper = i >= len - 1 ? 256 : (short) t_array[i+1];
7974 short to = (short) r_map[i];
7976 bool do_increment = TRUE;
7978 /* Any code points above our limit should be irrelevant */
7979 if (t_array[i] >= tbl->size) break;
7981 /* Set up the map */
7982 if (to == (short) TR_SPECIAL_HANDLING && ! del) {
7983 to = (short) final_map;
7984 do_increment = FALSE;
7987 do_increment = FALSE;
7990 /* Create a map for everything in this range. The value increases
7991 * except for the special cases */
7992 for (j = (short) t_array[i]; j < upper; j++) {
7994 if (do_increment) to++;
7998 tbl->map[tbl->size] = del
8002 : (short) TR_R_EMPTY;
8003 DEBUG_y(PerlIO_printf(Perl_debug_log,"%s: %d\n", __FILE__, __LINE__));
8004 for (i = 0; i < tbl->size; i++) {
8005 if (tbl->map[i] < 0) {
8006 DEBUG_y(PerlIO_printf(Perl_debug_log," %02x=>%d",
8007 (unsigned) i, tbl->map[i]));
8010 DEBUG_y(PerlIO_printf(Perl_debug_log," %02x=>%02x",
8011 (unsigned) i, tbl->map[i]));
8013 if ((i+1) % 8 == 0 || i + 1 == (short) tbl->size) {
8014 DEBUG_y(PerlIO_printf(Perl_debug_log,"\n"));
8017 DEBUG_y(PerlIO_printf(Perl_debug_log,"Final map 0x%x=>%02x\n",
8018 (unsigned) tbl->size, tbl->map[tbl->size]));
8020 SvREFCNT_dec(t_invlist);
8022 #if 0 /* code that added excess above-255 chars at the end of the table, in
8023 case we ever want to not use the inversion map implementation for
8030 /* More replacement chars than search chars:
8031 * store excess replacement chars at end of main table.
8034 struct_size += excess;
8035 tbl = (OPtrans_map*)PerlMemShared_realloc(tbl,
8036 struct_size + excess * sizeof(short));
8037 tbl->size += excess;
8038 cPVOPo->op_pv = (char*)tbl;
8040 for (i = 0; i < excess; i++)
8041 tbl->map[i + 256] = r[j+i];
8044 /* no more replacement chars than search chars */
8050 DEBUG_y(PerlIO_printf(Perl_debug_log,
8051 "/d=%d, /s=%d, /c=%d, identical=%d, grows=%d,"
8052 " use_svop=%d, can_force_utf8=%d,\nexpansion=%" NVgf "\n",
8053 del, squash, complement,
8054 cBOOL(o->op_private & OPpTRANS_IDENTICAL),
8055 cBOOL(o->op_private & OPpTRANS_USE_SVOP),
8056 cBOOL(o->op_private & OPpTRANS_GROWS),
8057 cBOOL(o->op_private & OPpTRANS_CAN_FORCE_UTF8),
8062 if(del && rlen != 0 && r_count == t_count) {
8063 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
8064 } else if(r_count > t_count) {
8065 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
8078 Constructs, checks, and returns an op of any pattern matching type.
8079 C<type> is the opcode. C<flags> gives the eight bits of C<op_flags>
8080 and, shifted up eight bits, the eight bits of C<op_private>.
8086 Perl_newPMOP(pTHX_ I32 type, I32 flags)
8091 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP
8092 || type == OP_CUSTOM);
8094 NewOp(1101, pmop, 1, PMOP);
8095 OpTYPE_set(pmop, type);
8096 pmop->op_flags = (U8)flags;
8097 pmop->op_private = (U8)(0 | (flags >> 8));
8098 if (PL_opargs[type] & OA_RETSCALAR)
8101 if (PL_hints & HINT_RE_TAINT)
8102 pmop->op_pmflags |= PMf_RETAINT;
8103 #ifdef USE_LOCALE_CTYPE
8104 if (IN_LC_COMPILETIME(LC_CTYPE)) {
8105 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
8110 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
8112 if (PL_hints & HINT_RE_FLAGS) {
8113 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
8114 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
8116 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
8117 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
8118 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
8120 if (reflags && SvOK(reflags)) {
8121 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
8127 assert(SvPOK(PL_regex_pad[0]));
8128 if (SvCUR(PL_regex_pad[0])) {
8129 /* Pop off the "packed" IV from the end. */
8130 SV *const repointer_list = PL_regex_pad[0];
8131 const char *p = SvEND(repointer_list) - sizeof(IV);
8132 const IV offset = *((IV*)p);
8134 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
8136 SvEND_set(repointer_list, p);
8138 pmop->op_pmoffset = offset;
8139 /* This slot should be free, so assert this: */
8140 assert(PL_regex_pad[offset] == &PL_sv_undef);
8142 SV * const repointer = &PL_sv_undef;
8143 av_push(PL_regex_padav, repointer);
8144 pmop->op_pmoffset = av_tindex(PL_regex_padav);
8145 PL_regex_pad = AvARRAY(PL_regex_padav);
8149 return CHECKOP(type, pmop);
8157 /* Any pad names in scope are potentially lvalues. */
8158 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) {
8159 PADNAME *pn = PAD_COMPNAME_SV(i);
8160 if (!pn || !PadnameLEN(pn))
8162 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax))
8163 S_mark_padname_lvalue(aTHX_ pn);
8167 /* Given some sort of match op o, and an expression expr containing a
8168 * pattern, either compile expr into a regex and attach it to o (if it's
8169 * constant), or convert expr into a runtime regcomp op sequence (if it's
8172 * Flags currently has 2 bits of meaning:
8173 * 1: isreg indicates that the pattern is part of a regex construct, eg
8174 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
8175 * split "pattern", which aren't. In the former case, expr will be a list
8176 * if the pattern contains more than one term (eg /a$b/).
8177 * 2: The pattern is for a split.
8179 * When the pattern has been compiled within a new anon CV (for
8180 * qr/(?{...})/ ), then floor indicates the savestack level just before
8181 * the new sub was created
8183 * tr/// is also handled.
8187 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, UV flags, I32 floor)
8191 I32 repl_has_vars = 0;
8192 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
8193 bool is_compiletime;
8195 bool isreg = cBOOL(flags & 1);
8196 bool is_split = cBOOL(flags & 2);
8198 PERL_ARGS_ASSERT_PMRUNTIME;
8201 return pmtrans(o, expr, repl);
8204 /* find whether we have any runtime or code elements;
8205 * at the same time, temporarily set the op_next of each DO block;
8206 * then when we LINKLIST, this will cause the DO blocks to be excluded
8207 * from the op_next chain (and from having LINKLIST recursively
8208 * applied to them). We fix up the DOs specially later */
8212 if (expr->op_type == OP_LIST) {
8214 for (child = cLISTOPx(expr)->op_first; child; child = OpSIBLING(child)) {
8215 if (child->op_type == OP_NULL && (child->op_flags & OPf_SPECIAL)) {
8217 assert(!child->op_next);
8218 if (UNLIKELY(!OpHAS_SIBLING(child))) {
8219 assert(PL_parser && PL_parser->error_count);
8220 /* This can happen with qr/ (?{(^{})/. Just fake up
8221 the op we were expecting to see, to avoid crashing
8223 op_sibling_splice(expr, child, 0,
8224 newSVOP(OP_CONST, 0, &PL_sv_no));
8226 child->op_next = OpSIBLING(child);
8228 else if (child->op_type != OP_CONST && child->op_type != OP_PUSHMARK)
8232 else if (expr->op_type != OP_CONST)
8237 /* fix up DO blocks; treat each one as a separate little sub;
8238 * also, mark any arrays as LIST/REF */
8240 if (expr->op_type == OP_LIST) {
8242 for (child = cLISTOPx(expr)->op_first; child; child = OpSIBLING(child)) {
8244 if (child->op_type == OP_PADAV || child->op_type == OP_RV2AV) {
8245 assert( !(child->op_flags & OPf_WANT));
8246 /* push the array rather than its contents. The regex
8247 * engine will retrieve and join the elements later */
8248 child->op_flags |= (OPf_WANT_LIST | OPf_REF);
8252 if (!(child->op_type == OP_NULL && (child->op_flags & OPf_SPECIAL)))
8254 child->op_next = NULL; /* undo temporary hack from above */
8257 if (cLISTOPx(child)->op_first->op_type == OP_LEAVE) {
8258 LISTOP *leaveop = cLISTOPx(cLISTOPx(child)->op_first);
8260 assert(leaveop->op_first->op_type == OP_ENTER);
8261 assert(OpHAS_SIBLING(leaveop->op_first));
8262 child->op_next = OpSIBLING(leaveop->op_first);
8264 assert(leaveop->op_flags & OPf_KIDS);
8265 assert(leaveop->op_last->op_next == (OP*)leaveop);
8266 leaveop->op_next = NULL; /* stop on last op */
8267 op_null((OP*)leaveop);
8271 OP *scope = cLISTOPx(child)->op_first;
8272 assert(scope->op_type == OP_SCOPE);
8273 assert(scope->op_flags & OPf_KIDS);
8274 scope->op_next = NULL; /* stop on last op */
8278 /* XXX optimize_optree() must be called on o before
8279 * CALL_PEEP(), as currently S_maybe_multiconcat() can't
8280 * currently cope with a peephole-optimised optree.
8281 * Calling optimize_optree() here ensures that condition
8282 * is met, but may mean optimize_optree() is applied
8283 * to the same optree later (where hopefully it won't do any
8284 * harm as it can't convert an op to multiconcat if it's
8285 * already been converted */
8286 optimize_optree(child);
8288 /* have to peep the DOs individually as we've removed it from
8289 * the op_next chain */
8291 S_prune_chain_head(&(child->op_next));
8293 /* runtime finalizes as part of finalizing whole tree */
8294 finalize_optree(child);
8297 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
8298 assert( !(expr->op_flags & OPf_WANT));
8299 /* push the array rather than its contents. The regex
8300 * engine will retrieve and join the elements later */
8301 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
8304 PL_hints |= HINT_BLOCK_SCOPE;
8306 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
8308 if (is_compiletime) {
8309 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
8310 regexp_engine const *eng = current_re_engine();
8313 /* make engine handle split ' ' specially */
8314 pm->op_pmflags |= PMf_SPLIT;
8315 rx_flags |= RXf_SPLIT;
8318 if (!has_code || !eng->op_comp) {
8319 /* compile-time simple constant pattern */
8321 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
8322 /* whoops! we guessed that a qr// had a code block, but we
8323 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
8324 * that isn't required now. Note that we have to be pretty
8325 * confident that nothing used that CV's pad while the
8326 * regex was parsed, except maybe op targets for \Q etc.
8327 * If there were any op targets, though, they should have
8328 * been stolen by constant folding.
8332 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0);
8333 while (++i <= AvFILLp(PL_comppad)) {
8334 # ifdef USE_PAD_RESET
8335 /* under USE_PAD_RESET, pad swipe replaces a swiped
8336 * folded constant with a fresh padtmp */
8337 assert(!PL_curpad[i] || SvPADTMP(PL_curpad[i]));
8339 assert(!PL_curpad[i]);
8343 /* This LEAVE_SCOPE will restore PL_compcv to point to the
8344 * outer CV (the one whose slab holds the pm op). The
8345 * inner CV (which holds expr) will be freed later, once
8346 * all the entries on the parse stack have been popped on
8347 * return from this function. Which is why its safe to
8348 * call op_free(expr) below.
8351 pm->op_pmflags &= ~PMf_HAS_CV;
8354 /* Skip compiling if parser found an error for this pattern */
8355 if (pm->op_pmflags & PMf_HAS_ERROR) {
8361 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
8362 rx_flags, pm->op_pmflags)
8363 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
8364 rx_flags, pm->op_pmflags)
8369 /* compile-time pattern that includes literal code blocks */
8373 /* Skip compiling if parser found an error for this pattern */
8374 if (pm->op_pmflags & PMf_HAS_ERROR) {
8378 re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
8381 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
8384 if (pm->op_pmflags & PMf_HAS_CV) {
8386 /* this QR op (and the anon sub we embed it in) is never
8387 * actually executed. It's just a placeholder where we can
8388 * squirrel away expr in op_code_list without the peephole
8389 * optimiser etc processing it for a second time */
8390 OP *qr = newPMOP(OP_QR, 0);
8391 ((PMOP*)qr)->op_code_list = expr;
8393 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
8394 SvREFCNT_inc_simple_void(PL_compcv);
8395 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
8396 ReANY(re)->qr_anoncv = cv;
8398 /* attach the anon CV to the pad so that
8399 * pad_fixup_inner_anons() can find it */
8400 (void)pad_add_anon(cv, o->op_type);
8401 SvREFCNT_inc_simple_void(cv);
8404 pm->op_code_list = expr;
8409 /* runtime pattern: build chain of regcomp etc ops */
8411 PADOFFSET cv_targ = 0;
8413 reglist = isreg && expr->op_type == OP_LIST;
8418 pm->op_code_list = expr;
8419 /* don't free op_code_list; its ops are embedded elsewhere too */
8420 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
8424 /* make engine handle split ' ' specially */
8425 pm->op_pmflags |= PMf_SPLIT;
8427 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
8428 * to allow its op_next to be pointed past the regcomp and
8429 * preceding stacking ops;
8430 * OP_REGCRESET is there to reset taint before executing the
8432 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
8433 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
8435 if (pm->op_pmflags & PMf_HAS_CV) {
8436 /* we have a runtime qr with literal code. This means
8437 * that the qr// has been wrapped in a new CV, which
8438 * means that runtime consts, vars etc will have been compiled
8439 * against a new pad. So... we need to execute those ops
8440 * within the environment of the new CV. So wrap them in a call
8441 * to a new anon sub. i.e. for
8445 * we build an anon sub that looks like
8447 * sub { "a", $b, '(?{...})' }
8449 * and call it, passing the returned list to regcomp.
8450 * Or to put it another way, the list of ops that get executed
8454 * ------ -------------------
8455 * pushmark (for regcomp)
8456 * pushmark (for entersub)
8460 * regcreset regcreset
8462 * const("a") const("a")
8464 * const("(?{...})") const("(?{...})")
8469 SvREFCNT_inc_simple_void(PL_compcv);
8470 CvLVALUE_on(PL_compcv);
8471 /* these lines are just an unrolled newANONATTRSUB */
8472 expr = newSVOP(OP_ANONCODE, 0,
8473 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
8474 cv_targ = expr->op_targ;
8475 expr = newUNOP(OP_REFGEN, 0, expr);
8477 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), 1));
8480 rcop = alloc_LOGOP(OP_REGCOMP, scalar(expr), o);
8481 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
8482 | (reglist ? OPf_STACKED : 0);
8483 rcop->op_targ = cv_targ;
8485 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
8486 if (PL_hints & HINT_RE_EVAL)
8487 S_set_haseval(aTHX);
8489 /* establish postfix order */
8490 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
8492 rcop->op_next = expr;
8493 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
8496 rcop->op_next = LINKLIST(expr);
8497 expr->op_next = (OP*)rcop;
8500 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
8506 /* If we are looking at s//.../e with a single statement, get past
8507 the implicit do{}. */
8508 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
8509 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
8510 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS)
8513 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
8514 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid))
8515 && !OpHAS_SIBLING(sib))
8518 if (curop->op_type == OP_CONST)
8520 else if (( (curop->op_type == OP_RV2SV ||
8521 curop->op_type == OP_RV2AV ||
8522 curop->op_type == OP_RV2HV ||
8523 curop->op_type == OP_RV2GV)
8524 && cUNOPx(curop)->op_first
8525 && cUNOPx(curop)->op_first->op_type == OP_GV )
8526 || curop->op_type == OP_PADSV
8527 || curop->op_type == OP_PADAV
8528 || curop->op_type == OP_PADHV
8529 || curop->op_type == OP_PADANY) {
8537 || !RX_PRELEN(PM_GETRE(pm))
8538 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
8540 pm->op_pmflags |= PMf_CONST; /* const for long enough */
8541 op_prepend_elem(o->op_type, scalar(repl), o);
8544 rcop = alloc_LOGOP(OP_SUBSTCONT, scalar(repl), o);
8545 rcop->op_private = 1;
8547 /* establish postfix order */
8548 rcop->op_next = LINKLIST(repl);
8549 repl->op_next = (OP*)rcop;
8551 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
8552 assert(!(pm->op_pmflags & PMf_ONCE));
8553 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
8564 Constructs, checks, and returns an op of any type that involves an
8565 embedded SV. C<type> is the opcode. C<flags> gives the eight bits
8566 of C<op_flags>. C<sv> gives the SV to embed in the op; this function
8567 takes ownership of one reference to it.
8573 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
8578 PERL_ARGS_ASSERT_NEWSVOP;
8580 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
8581 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
8582 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
8583 || type == OP_CUSTOM);
8585 NewOp(1101, svop, 1, SVOP);
8586 OpTYPE_set(svop, type);
8588 svop->op_next = (OP*)svop;
8589 svop->op_flags = (U8)flags;
8590 svop->op_private = (U8)(0 | (flags >> 8));
8591 if (PL_opargs[type] & OA_RETSCALAR)
8593 if (PL_opargs[type] & OA_TARGET)
8594 svop->op_targ = pad_alloc(type, SVs_PADTMP);
8595 return CHECKOP(type, svop);
8599 =for apidoc newDEFSVOP
8601 Constructs and returns an op to access C<$_>.
8607 Perl_newDEFSVOP(pTHX)
8609 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
8615 =for apidoc newPADOP
8617 Constructs, checks, and returns an op of any type that involves a
8618 reference to a pad element. C<type> is the opcode. C<flags> gives the
8619 eight bits of C<op_flags>. A pad slot is automatically allocated, and
8620 is populated with C<sv>; this function takes ownership of one reference
8623 This function only exists if Perl has been compiled to use ithreads.
8629 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
8634 PERL_ARGS_ASSERT_NEWPADOP;
8636 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
8637 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
8638 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
8639 || type == OP_CUSTOM);
8641 NewOp(1101, padop, 1, PADOP);
8642 OpTYPE_set(padop, type);
8644 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP);
8645 SvREFCNT_dec(PAD_SVl(padop->op_padix));
8646 PAD_SETSV(padop->op_padix, sv);
8648 padop->op_next = (OP*)padop;
8649 padop->op_flags = (U8)flags;
8650 if (PL_opargs[type] & OA_RETSCALAR)
8652 if (PL_opargs[type] & OA_TARGET)
8653 padop->op_targ = pad_alloc(type, SVs_PADTMP);
8654 return CHECKOP(type, padop);
8657 #endif /* USE_ITHREADS */
8662 Constructs, checks, and returns an op of any type that involves an
8663 embedded reference to a GV. C<type> is the opcode. C<flags> gives the
8664 eight bits of C<op_flags>. C<gv> identifies the GV that the op should
8665 reference; calling this function does not transfer ownership of any
8672 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
8674 PERL_ARGS_ASSERT_NEWGVOP;
8677 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
8679 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
8686 Constructs, checks, and returns an op of any type that involves an
8687 embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives
8688 the eight bits of C<op_flags>. C<pv> supplies the C-level pointer.
8689 Depending on the op type, the memory referenced by C<pv> may be freed
8690 when the op is destroyed. If the op is of a freeing type, C<pv> must
8691 have been allocated using C<PerlMemShared_malloc>.
8697 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
8700 const bool utf8 = cBOOL(flags & SVf_UTF8);
8705 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
8706 || type == OP_RUNCV || type == OP_CUSTOM
8707 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
8709 NewOp(1101, pvop, 1, PVOP);
8710 OpTYPE_set(pvop, type);
8712 pvop->op_next = (OP*)pvop;
8713 pvop->op_flags = (U8)flags;
8714 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
8715 if (PL_opargs[type] & OA_RETSCALAR)
8717 if (PL_opargs[type] & OA_TARGET)
8718 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
8719 return CHECKOP(type, pvop);
8723 Perl_package(pTHX_ OP *o)
8725 SV *const sv = cSVOPo->op_sv;
8727 PERL_ARGS_ASSERT_PACKAGE;
8729 SAVEGENERICSV(PL_curstash);
8730 save_item(PL_curstname);
8732 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
8734 sv_setsv(PL_curstname, sv);
8736 PL_hints |= HINT_BLOCK_SCOPE;
8737 PL_parser->copline = NOLINE;
8743 Perl_package_version( pTHX_ OP *v )
8745 U32 savehints = PL_hints;
8746 PERL_ARGS_ASSERT_PACKAGE_VERSION;
8747 PL_hints &= ~HINT_STRICT_VARS;
8748 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
8749 PL_hints = savehints;
8754 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
8759 SV *use_version = NULL;
8761 PERL_ARGS_ASSERT_UTILIZE;
8763 if (idop->op_type != OP_CONST)
8764 Perl_croak(aTHX_ "Module name must be constant");
8769 SV * const vesv = ((SVOP*)version)->op_sv;
8771 if (!arg && !SvNIOKp(vesv)) {
8778 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
8779 Perl_croak(aTHX_ "Version number must be a constant number");
8781 /* Make copy of idop so we don't free it twice */
8782 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
8784 /* Fake up a method call to VERSION */
8785 meth = newSVpvs_share("VERSION");
8786 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
8787 op_append_elem(OP_LIST,
8788 op_prepend_elem(OP_LIST, pack, version),
8789 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
8793 /* Fake up an import/unimport */
8794 if (arg && arg->op_type == OP_STUB) {
8795 imop = arg; /* no import on explicit () */
8797 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
8798 imop = NULL; /* use 5.0; */
8800 use_version = ((SVOP*)idop)->op_sv;
8802 idop->op_private |= OPpCONST_NOVER;
8807 /* Make copy of idop so we don't free it twice */
8808 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
8810 /* Fake up a method call to import/unimport */
8812 ? newSVpvs_share("import") : newSVpvs_share("unimport");
8813 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
8814 op_append_elem(OP_LIST,
8815 op_prepend_elem(OP_LIST, pack, arg),
8816 newMETHOP_named(OP_METHOD_NAMED, 0, meth)
8820 /* Fake up the BEGIN {}, which does its thing immediately. */
8822 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
8825 op_append_elem(OP_LINESEQ,
8826 op_append_elem(OP_LINESEQ,
8827 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
8828 newSTATEOP(0, NULL, veop)),
8829 newSTATEOP(0, NULL, imop) ));
8833 * feature bundle that corresponds to the required version. */
8834 use_version = sv_2mortal(new_version(use_version));
8835 S_enable_feature_bundle(aTHX_ use_version);
8837 /* If a version >= 5.11.0 is requested, strictures are on by default! */
8838 if (vcmp(use_version,
8839 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
8840 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
8841 PL_hints |= HINT_STRICT_REFS;
8842 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
8843 PL_hints |= HINT_STRICT_SUBS;
8844 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
8845 PL_hints |= HINT_STRICT_VARS;
8847 /* otherwise they are off */
8849 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
8850 PL_hints &= ~HINT_STRICT_REFS;
8851 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
8852 PL_hints &= ~HINT_STRICT_SUBS;
8853 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
8854 PL_hints &= ~HINT_STRICT_VARS;
8858 /* The "did you use incorrect case?" warning used to be here.
8859 * The problem is that on case-insensitive filesystems one
8860 * might get false positives for "use" (and "require"):
8861 * "use Strict" or "require CARP" will work. This causes
8862 * portability problems for the script: in case-strict
8863 * filesystems the script will stop working.
8865 * The "incorrect case" warning checked whether "use Foo"
8866 * imported "Foo" to your namespace, but that is wrong, too:
8867 * there is no requirement nor promise in the language that
8868 * a Foo.pm should or would contain anything in package "Foo".
8870 * There is very little Configure-wise that can be done, either:
8871 * the case-sensitivity of the build filesystem of Perl does not
8872 * help in guessing the case-sensitivity of the runtime environment.
8875 PL_hints |= HINT_BLOCK_SCOPE;
8876 PL_parser->copline = NOLINE;
8877 COP_SEQMAX_INC; /* Purely for B::*'s benefit */
8881 =head1 Embedding Functions
8883 =for apidoc load_module
8885 Loads the module whose name is pointed to by the string part of C<name>.
8886 Note that the actual module name, not its filename, should be given.
8887 Eg, "Foo::Bar" instead of "Foo/Bar.pm". ver, if specified and not NULL,
8888 provides version semantics similar to C<use Foo::Bar VERSION>. The optional
8889 trailing arguments can be used to specify arguments to the module's C<import()>
8890 method, similar to C<use Foo::Bar VERSION LIST>; their precise handling depends
8891 on the flags. The flags argument is a bitwise-ORed collection of any of
8892 C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS>
8893 (or 0 for no flags).
8895 If C<PERL_LOADMOD_NOIMPORT> is set, the module is loaded as if with an empty
8896 import list, as in C<use Foo::Bar ()>; this is the only circumstance in which
8897 the trailing optional arguments may be omitted entirely. Otherwise, if
8898 C<PERL_LOADMOD_IMPORT_OPS> is set, the trailing arguments must consist of
8899 exactly one C<OP*>, containing the op tree that produces the relevant import
8900 arguments. Otherwise, the trailing arguments must all be C<SV*> values that
8901 will be used as import arguments; and the list must be terminated with C<(SV*)
8902 NULL>. If neither C<PERL_LOADMOD_NOIMPORT> nor C<PERL_LOADMOD_IMPORT_OPS> is
8903 set, the trailing C<NULL> pointer is needed even if no import arguments are
8904 desired. The reference count for each specified C<SV*> argument is
8905 decremented. In addition, the C<name> argument is modified.
8907 If C<PERL_LOADMOD_DENY> is set, the module is loaded as if with C<no> rather
8910 =for apidoc Amnh||PERL_LOADMOD_DENY
8911 =for apidoc Amnh||PERL_LOADMOD_NOIMPORT
8912 =for apidoc Amnh||PERL_LOADMOD_IMPORT_OPS
8917 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
8921 PERL_ARGS_ASSERT_LOAD_MODULE;
8923 va_start(args, ver);
8924 vload_module(flags, name, ver, &args);
8928 #ifdef PERL_IMPLICIT_CONTEXT
8930 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
8934 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
8935 va_start(args, ver);
8936 vload_module(flags, name, ver, &args);
8942 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
8948 PERL_ARGS_ASSERT_VLOAD_MODULE;
8950 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
8951 * that it has a PL_parser to play with while doing that, and also
8952 * that it doesn't mess with any existing parser, by creating a tmp
8953 * new parser with lex_start(). This won't actually be used for much,
8954 * since pp_require() will create another parser for the real work.
8955 * The ENTER/LEAVE pair protect callers from any side effects of use.
8957 * start_subparse() creates a new PL_compcv. This means that any ops
8958 * allocated below will be allocated from that CV's op slab, and so
8959 * will be automatically freed if the utilise() fails
8963 SAVEVPTR(PL_curcop);
8964 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
8965 floor = start_subparse(FALSE, 0);
8967 modname = newSVOP(OP_CONST, 0, name);
8968 modname->op_private |= OPpCONST_BARE;
8970 veop = newSVOP(OP_CONST, 0, ver);
8974 if (flags & PERL_LOADMOD_NOIMPORT) {
8975 imop = sawparens(newNULLLIST());
8977 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
8978 imop = va_arg(*args, OP*);
8983 sv = va_arg(*args, SV*);
8985 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
8986 sv = va_arg(*args, SV*);
8990 utilize(!(flags & PERL_LOADMOD_DENY), floor, veop, modname, imop);
8994 PERL_STATIC_INLINE OP *
8995 S_new_entersubop(pTHX_ GV *gv, OP *arg)
8997 return newUNOP(OP_ENTERSUB, OPf_STACKED,
8998 newLISTOP(OP_LIST, 0, arg,
8999 newUNOP(OP_RV2CV, 0,
9000 newGVOP(OP_GV, 0, gv))));
9004 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
9009 PERL_ARGS_ASSERT_DOFILE;
9011 if (!force_builtin && (gv = gv_override("do", 2))) {
9012 doop = S_new_entersubop(aTHX_ gv, term);
9015 doop = newUNOP(OP_DOFILE, 0, scalar(term));
9021 =head1 Optree construction
9023 =for apidoc newSLICEOP
9025 Constructs, checks, and returns an C<lslice> (list slice) op. C<flags>
9026 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
9027 be set automatically, and, shifted up eight bits, the eight bits of
9028 C<op_private>, except that the bit with value 1 or 2 is automatically
9029 set as required. C<listval> and C<subscript> supply the parameters of
9030 the slice; they are consumed by this function and become part of the
9031 constructed op tree.
9037 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
9039 return newBINOP(OP_LSLICE, flags,
9040 list(force_list(subscript, 1)),
9041 list(force_list(listval, 1)) );
9044 #define ASSIGN_SCALAR 0
9045 #define ASSIGN_LIST 1
9046 #define ASSIGN_REF 2
9048 /* given the optree o on the LHS of an assignment, determine whether its:
9049 * ASSIGN_SCALAR $x = ...
9050 * ASSIGN_LIST ($x) = ...
9051 * ASSIGN_REF \$x = ...
9055 S_assignment_type(pTHX_ const OP *o)
9064 if (o->op_type == OP_SREFGEN)
9066 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first;
9067 type = kid->op_type;
9068 flags = o->op_flags | kid->op_flags;
9069 if (!(flags & OPf_PARENS)
9070 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV ||
9071 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV ))
9075 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
9076 o = cUNOPo->op_first;
9077 flags = o->op_flags;
9079 ret = ASSIGN_SCALAR;
9082 if (type == OP_COND_EXPR) {
9083 OP * const sib = OpSIBLING(cLOGOPo->op_first);
9084 const I32 t = assignment_type(sib);
9085 const I32 f = assignment_type(OpSIBLING(sib));
9087 if (t == ASSIGN_LIST && f == ASSIGN_LIST)
9089 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST))
9090 yyerror("Assignment to both a list and a scalar");
9091 return ASSIGN_SCALAR;
9094 if (type == OP_LIST &&
9095 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
9096 o->op_private & OPpLVAL_INTRO)
9099 if (type == OP_LIST || flags & OPf_PARENS ||
9100 type == OP_RV2AV || type == OP_RV2HV ||
9101 type == OP_ASLICE || type == OP_HSLICE ||
9102 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN)
9105 if (type == OP_PADAV || type == OP_PADHV)
9108 if (type == OP_RV2SV)
9115 S_newONCEOP(pTHX_ OP *initop, OP *padop)
9118 const PADOFFSET target = padop->op_targ;
9119 OP *const other = newOP(OP_PADSV,
9121 | ((padop->op_private & ~OPpLVAL_INTRO) << 8));
9122 OP *const first = newOP(OP_NULL, 0);
9123 OP *const nullop = newCONDOP(0, first, initop, other);
9124 /* XXX targlex disabled for now; see ticket #124160
9125 newCONDOP(0, first, S_maybe_targlex(aTHX_ initop), other);
9127 OP *const condop = first->op_next;
9129 OpTYPE_set(condop, OP_ONCE);
9130 other->op_targ = target;
9131 nullop->op_flags |= OPf_WANT_SCALAR;
9133 /* Store the initializedness of state vars in a separate
9136 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0);
9137 /* hijacking PADSTALE for uninitialized state variables */
9138 SvPADSTALE_on(PAD_SVl(condop->op_targ));
9144 =for apidoc newASSIGNOP
9146 Constructs, checks, and returns an assignment op. C<left> and C<right>
9147 supply the parameters of the assignment; they are consumed by this
9148 function and become part of the constructed op tree.
9150 If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
9151 a suitable conditional optree is constructed. If C<optype> is the opcode
9152 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
9153 performs the binary operation and assigns the result to the left argument.
9154 Either way, if C<optype> is non-zero then C<flags> has no effect.
9156 If C<optype> is zero, then a plain scalar or list assignment is
9157 constructed. Which type of assignment it is is automatically determined.
9158 C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
9159 will be set automatically, and, shifted up eight bits, the eight bits
9160 of C<op_private>, except that the bit with value 1 or 2 is automatically
9167 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
9173 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
9174 right = scalar(right);
9175 return newLOGOP(optype, 0,
9176 op_lvalue(scalar(left), optype),
9177 newBINOP(OP_SASSIGN, OPpASSIGN_BACKWARDS<<8, right, right));
9180 return newBINOP(optype, OPf_STACKED,
9181 op_lvalue(scalar(left), optype), scalar(right));
9185 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) {
9186 OP *state_var_op = NULL;
9187 static const char no_list_state[] = "Initialization of state variables"
9188 " in list currently forbidden";
9191 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
9192 left->op_private &= ~ OPpSLICEWARNING;
9195 left = op_lvalue(left, OP_AASSIGN);
9196 curop = list(force_list(left, 1));
9197 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, 1)), curop);
9198 o->op_private = (U8)(0 | (flags >> 8));
9200 if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
9202 OP *lop = ((LISTOP*)left)->op_first, *vop, *eop;
9203 if (!(left->op_flags & OPf_PARENS) &&
9204 lop->op_type == OP_PUSHMARK &&
9205 (vop = OpSIBLING(lop)) &&
9206 (vop->op_type == OP_PADAV || vop->op_type == OP_PADHV) &&
9207 !(vop->op_flags & OPf_PARENS) &&
9208 (vop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) ==
9209 (OPpLVAL_INTRO|OPpPAD_STATE) &&
9210 (eop = OpSIBLING(vop)) &&
9211 eop->op_type == OP_ENTERSUB &&
9212 !OpHAS_SIBLING(eop)) {
9216 if ((lop->op_type == OP_PADSV ||
9217 lop->op_type == OP_PADAV ||
9218 lop->op_type == OP_PADHV ||
9219 lop->op_type == OP_PADANY)
9220 && (lop->op_private & OPpPAD_STATE)
9222 yyerror(no_list_state);
9223 lop = OpSIBLING(lop);
9227 else if ( (left->op_private & OPpLVAL_INTRO)
9228 && (left->op_private & OPpPAD_STATE)
9229 && ( left->op_type == OP_PADSV
9230 || left->op_type == OP_PADAV
9231 || left->op_type == OP_PADHV
9232 || left->op_type == OP_PADANY)
9234 /* All single variable list context state assignments, hence
9244 if (left->op_flags & OPf_PARENS)
9245 yyerror(no_list_state);
9247 state_var_op = left;
9250 /* optimise @a = split(...) into:
9251 * @{expr}: split(..., @{expr}) (where @a is not flattened)
9252 * @a, my @a, local @a: split(...) (where @a is attached to
9253 * the split op itself)
9257 && right->op_type == OP_SPLIT
9258 /* don't do twice, e.g. @b = (@a = split) */
9259 && !(right->op_private & OPpSPLIT_ASSIGN))
9263 if ( ( left->op_type == OP_RV2AV
9264 && (gvop=((UNOP*)left)->op_first)->op_type==OP_GV)
9265 || left->op_type == OP_PADAV)
9267 /* @pkg or @lex or local @pkg' or 'my @lex' */
9271 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff
9272 = cPADOPx(gvop)->op_padix;
9273 cPADOPx(gvop)->op_padix = 0; /* steal it */
9275 ((PMOP*)right)->op_pmreplrootu.op_pmtargetgv
9276 = MUTABLE_GV(cSVOPx(gvop)->op_sv);
9277 cSVOPx(gvop)->op_sv = NULL; /* steal it */
9279 right->op_private |=
9280 left->op_private & OPpOUR_INTRO;
9283 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff = left->op_targ;
9284 left->op_targ = 0; /* steal it */
9285 right->op_private |= OPpSPLIT_LEX;
9287 right->op_private |= left->op_private & OPpLVAL_INTRO;
9290 tmpop = cUNOPo->op_first; /* to list (nulled) */
9291 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
9292 assert(OpSIBLING(tmpop) == right);
9293 assert(!OpHAS_SIBLING(right));
9294 /* detach the split subtreee from the o tree,
9295 * then free the residual o tree */
9296 op_sibling_splice(cUNOPo->op_first, tmpop, 1, NULL);
9297 op_free(o); /* blow off assign */
9298 right->op_private |= OPpSPLIT_ASSIGN;
9299 right->op_flags &= ~OPf_WANT;
9300 /* "I don't know and I don't care." */
9303 else if (left->op_type == OP_RV2AV) {
9306 OP *pushop = cUNOPx(cBINOPo->op_last)->op_first;
9307 assert(OpSIBLING(pushop) == left);
9308 /* Detach the array ... */
9309 op_sibling_splice(cBINOPo->op_last, pushop, 1, NULL);
9310 /* ... and attach it to the split. */
9311 op_sibling_splice(right, cLISTOPx(right)->op_last,
9313 right->op_flags |= OPf_STACKED;
9314 /* Detach split and expunge aassign as above. */
9317 else if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
9318 ((LISTOP*)right)->op_last->op_type == OP_CONST)
9320 /* convert split(...,0) to split(..., PL_modcount+1) */
9322 &((SVOP*)((LISTOP*)right)->op_last)->op_sv;
9323 SV * const sv = *svp;
9324 if (SvIOK(sv) && SvIVX(sv) == 0)
9326 if (right->op_private & OPpSPLIT_IMPLIM) {
9327 /* our own SV, created in ck_split */
9329 sv_setiv(sv, PL_modcount+1);
9332 /* SV may belong to someone else */
9334 *svp = newSViv(PL_modcount+1);
9341 o = S_newONCEOP(aTHX_ o, state_var_op);
9344 if (assign_type == ASSIGN_REF)
9345 return newBINOP(OP_REFASSIGN, flags, scalar(right), left);
9347 right = newOP(OP_UNDEF, 0);
9348 if (right->op_type == OP_READLINE) {
9349 right->op_flags |= OPf_STACKED;
9350 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
9354 o = newBINOP(OP_SASSIGN, flags,
9355 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
9361 =for apidoc newSTATEOP
9363 Constructs a state op (COP). The state op is normally a C<nextstate> op,
9364 but will be a C<dbstate> op if debugging is enabled for currently-compiled
9365 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>).
9366 If C<label> is non-null, it supplies the name of a label to attach to
9367 the state op; this function takes ownership of the memory pointed at by
9368 C<label>, and will free it. C<flags> gives the eight bits of C<op_flags>
9371 If C<o> is null, the state op is returned. Otherwise the state op is
9372 combined with C<o> into a C<lineseq> list op, which is returned. C<o>
9373 is consumed by this function and becomes part of the returned op tree.
9379 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
9382 const U32 seq = intro_my();
9383 const U32 utf8 = flags & SVf_UTF8;
9386 PL_parser->parsed_sub = 0;
9390 NewOp(1101, cop, 1, COP);
9391 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
9392 OpTYPE_set(cop, OP_DBSTATE);
9395 OpTYPE_set(cop, OP_NEXTSTATE);
9397 cop->op_flags = (U8)flags;
9398 CopHINTS_set(cop, PL_hints);
9400 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
9402 cop->op_next = (OP*)cop;
9405 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
9406 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
9408 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
9410 PL_hints |= HINT_BLOCK_SCOPE;
9411 /* It seems that we need to defer freeing this pointer, as other parts
9412 of the grammar end up wanting to copy it after this op has been
9417 if (PL_parser->preambling != NOLINE) {
9418 CopLINE_set(cop, PL_parser->preambling);
9419 PL_parser->copline = NOLINE;
9421 else if (PL_parser->copline == NOLINE)
9422 CopLINE_set(cop, CopLINE(PL_curcop));
9424 CopLINE_set(cop, PL_parser->copline);
9425 PL_parser->copline = NOLINE;
9428 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
9430 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
9432 CopSTASH_set(cop, PL_curstash);
9434 if (cop->op_type == OP_DBSTATE) {
9435 /* this line can have a breakpoint - store the cop in IV */
9436 AV *av = CopFILEAVx(PL_curcop);
9438 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
9439 if (svp && *svp != &PL_sv_undef ) {
9440 (void)SvIOK_on(*svp);
9441 SvIV_set(*svp, PTR2IV(cop));
9446 if (flags & OPf_SPECIAL)
9448 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
9452 =for apidoc newLOGOP
9454 Constructs, checks, and returns a logical (flow control) op. C<type>
9455 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
9456 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
9457 the eight bits of C<op_private>, except that the bit with value 1 is
9458 automatically set. C<first> supplies the expression controlling the
9459 flow, and C<other> supplies the side (alternate) chain of ops; they are
9460 consumed by this function and become part of the constructed op tree.
9466 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
9468 PERL_ARGS_ASSERT_NEWLOGOP;
9470 return new_logop(type, flags, &first, &other);
9474 /* See if the optree o contains a single OP_CONST (plus possibly
9475 * surrounding enter/nextstate/null etc). If so, return it, else return
9480 S_search_const(pTHX_ OP *o)
9482 PERL_ARGS_ASSERT_SEARCH_CONST;
9485 switch (o->op_type) {
9489 if (o->op_flags & OPf_KIDS) {
9490 o = cUNOPo->op_first;
9499 if (!(o->op_flags & OPf_KIDS))
9501 kid = cLISTOPo->op_first;
9504 switch (kid->op_type) {
9508 kid = OpSIBLING(kid);
9511 if (kid != cLISTOPo->op_last)
9518 kid = cLISTOPo->op_last;
9530 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
9538 int prepend_not = 0;
9540 PERL_ARGS_ASSERT_NEW_LOGOP;
9545 /* [perl #59802]: Warn about things like "return $a or $b", which
9546 is parsed as "(return $a) or $b" rather than "return ($a or
9547 $b)". NB: This also applies to xor, which is why we do it
9550 switch (first->op_type) {
9554 /* XXX: Perhaps we should emit a stronger warning for these.
9555 Even with the high-precedence operator they don't seem to do
9558 But until we do, fall through here.
9564 /* XXX: Currently we allow people to "shoot themselves in the
9565 foot" by explicitly writing "(return $a) or $b".
9567 Warn unless we are looking at the result from folding or if
9568 the programmer explicitly grouped the operators like this.
9569 The former can occur with e.g.
9571 use constant FEATURE => ( $] >= ... );
9572 sub { not FEATURE and return or do_stuff(); }
9574 if (!first->op_folded && !(first->op_flags & OPf_PARENS))
9575 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
9576 "Possible precedence issue with control flow operator");
9577 /* XXX: Should we optimze this to "return $a;" (i.e. remove
9583 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
9584 return newBINOP(type, flags, scalar(first), scalar(other));
9586 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP
9587 || type == OP_CUSTOM);
9589 scalarboolean(first);
9591 /* search for a constant op that could let us fold the test */
9592 if ((cstop = search_const(first))) {
9593 if (cstop->op_private & OPpCONST_STRICT)
9594 no_bareword_allowed(cstop);
9595 else if ((cstop->op_private & OPpCONST_BARE))
9596 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
9597 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
9598 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
9599 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
9600 /* Elide the (constant) lhs, since it can't affect the outcome */
9602 if (other->op_type == OP_CONST)
9603 other->op_private |= OPpCONST_SHORTCIRCUIT;
9605 if (other->op_type == OP_LEAVE)
9606 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
9607 else if (other->op_type == OP_MATCH
9608 || other->op_type == OP_SUBST
9609 || other->op_type == OP_TRANSR
9610 || other->op_type == OP_TRANS)
9611 /* Mark the op as being unbindable with =~ */
9612 other->op_flags |= OPf_SPECIAL;
9614 other->op_folded = 1;
9618 /* Elide the rhs, since the outcome is entirely determined by
9619 * the (constant) lhs */
9621 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
9622 const OP *o2 = other;
9623 if ( ! (o2->op_type == OP_LIST
9624 && (( o2 = cUNOPx(o2)->op_first))
9625 && o2->op_type == OP_PUSHMARK
9626 && (( o2 = OpSIBLING(o2))) )
9629 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
9630 || o2->op_type == OP_PADHV)
9631 && o2->op_private & OPpLVAL_INTRO
9632 && !(o2->op_private & OPpPAD_STATE))
9634 Perl_croak(aTHX_ "This use of my() in false conditional is "
9635 "no longer allowed");
9639 if (cstop->op_type == OP_CONST)
9640 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
9645 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
9646 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
9648 const OP * const k1 = ((UNOP*)first)->op_first;
9649 const OP * const k2 = OpSIBLING(k1);
9651 switch (first->op_type)
9654 if (k2 && k2->op_type == OP_READLINE
9655 && (k2->op_flags & OPf_STACKED)
9656 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
9658 warnop = k2->op_type;
9663 if (k1->op_type == OP_READDIR
9664 || k1->op_type == OP_GLOB
9665 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
9666 || k1->op_type == OP_EACH
9667 || k1->op_type == OP_AEACH)
9669 warnop = ((k1->op_type == OP_NULL)
9670 ? (OPCODE)k1->op_targ : k1->op_type);
9675 const line_t oldline = CopLINE(PL_curcop);
9676 /* This ensures that warnings are reported at the first line
9677 of the construction, not the last. */
9678 CopLINE_set(PL_curcop, PL_parser->copline);
9679 Perl_warner(aTHX_ packWARN(WARN_MISC),
9680 "Value of %s%s can be \"0\"; test with defined()",
9682 ((warnop == OP_READLINE || warnop == OP_GLOB)
9683 ? " construct" : "() operator"));
9684 CopLINE_set(PL_curcop, oldline);
9688 /* optimize AND and OR ops that have NOTs as children */
9689 if (first->op_type == OP_NOT
9690 && (first->op_flags & OPf_KIDS)
9691 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
9692 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
9694 if (type == OP_AND || type == OP_OR) {
9700 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
9702 prepend_not = 1; /* prepend a NOT op later */
9707 logop = alloc_LOGOP(type, first, LINKLIST(other));
9708 logop->op_flags |= (U8)flags;
9709 logop->op_private = (U8)(1 | (flags >> 8));
9711 /* establish postfix order */
9712 logop->op_next = LINKLIST(first);
9713 first->op_next = (OP*)logop;
9714 assert(!OpHAS_SIBLING(first));
9715 op_sibling_splice((OP*)logop, first, 0, other);
9717 CHECKOP(type,logop);
9719 o = newUNOP(prepend_not ? OP_NOT : OP_NULL,
9720 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0,
9728 =for apidoc newCONDOP
9730 Constructs, checks, and returns a conditional-expression (C<cond_expr>)
9731 op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
9732 will be set automatically, and, shifted up eight bits, the eight bits of
9733 C<op_private>, except that the bit with value 1 is automatically set.
9734 C<first> supplies the expression selecting between the two branches,
9735 and C<trueop> and C<falseop> supply the branches; they are consumed by
9736 this function and become part of the constructed op tree.
9742 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
9750 PERL_ARGS_ASSERT_NEWCONDOP;
9753 return newLOGOP(OP_AND, 0, first, trueop);
9755 return newLOGOP(OP_OR, 0, first, falseop);
9757 scalarboolean(first);
9758 if ((cstop = search_const(first))) {
9759 /* Left or right arm of the conditional? */
9760 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
9761 OP *live = left ? trueop : falseop;
9762 OP *const dead = left ? falseop : trueop;
9763 if (cstop->op_private & OPpCONST_BARE &&
9764 cstop->op_private & OPpCONST_STRICT) {
9765 no_bareword_allowed(cstop);
9769 if (live->op_type == OP_LEAVE)
9770 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
9771 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
9772 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
9773 /* Mark the op as being unbindable with =~ */
9774 live->op_flags |= OPf_SPECIAL;
9775 live->op_folded = 1;
9778 logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop));
9779 logop->op_flags |= (U8)flags;
9780 logop->op_private = (U8)(1 | (flags >> 8));
9781 logop->op_next = LINKLIST(falseop);
9783 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
9786 /* establish postfix order */
9787 start = LINKLIST(first);
9788 first->op_next = (OP*)logop;
9790 /* make first, trueop, falseop siblings */
9791 op_sibling_splice((OP*)logop, first, 0, trueop);
9792 op_sibling_splice((OP*)logop, trueop, 0, falseop);
9794 o = newUNOP(OP_NULL, 0, (OP*)logop);
9796 trueop->op_next = falseop->op_next = o;
9803 =for apidoc newRANGE
9805 Constructs and returns a C<range> op, with subordinate C<flip> and
9806 C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the
9807 C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
9808 for both the C<flip> and C<range> ops, except that the bit with value
9809 1 is automatically set. C<left> and C<right> supply the expressions
9810 controlling the endpoints of the range; they are consumed by this function
9811 and become part of the constructed op tree.
9817 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
9825 PERL_ARGS_ASSERT_NEWRANGE;
9827 range = alloc_LOGOP(OP_RANGE, left, LINKLIST(right));
9828 range->op_flags = OPf_KIDS;
9829 leftstart = LINKLIST(left);
9830 range->op_private = (U8)(1 | (flags >> 8));
9832 /* make left and right siblings */
9833 op_sibling_splice((OP*)range, left, 0, right);
9835 range->op_next = (OP*)range;
9836 flip = newUNOP(OP_FLIP, flags, (OP*)range);
9837 flop = newUNOP(OP_FLOP, 0, flip);
9838 o = newUNOP(OP_NULL, 0, flop);
9840 range->op_next = leftstart;
9842 left->op_next = flip;
9843 right->op_next = flop;
9846 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);
9847 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
9849 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);;
9850 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
9851 SvPADTMP_on(PAD_SV(flip->op_targ));
9853 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
9854 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
9856 /* check barewords before they might be optimized aways */
9857 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
9858 no_bareword_allowed(left);
9859 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
9860 no_bareword_allowed(right);
9863 if (!flip->op_private || !flop->op_private)
9864 LINKLIST(o); /* blow off optimizer unless constant */
9870 =for apidoc newLOOPOP
9872 Constructs, checks, and returns an op tree expressing a loop. This is
9873 only a loop in the control flow through the op tree; it does not have
9874 the heavyweight loop structure that allows exiting the loop by C<last>
9875 and suchlike. C<flags> gives the eight bits of C<op_flags> for the
9876 top-level op, except that some bits will be set automatically as required.
9877 C<expr> supplies the expression controlling loop iteration, and C<block>
9878 supplies the body of the loop; they are consumed by this function and
9879 become part of the constructed op tree. C<debuggable> is currently
9880 unused and should always be 1.
9886 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
9890 const bool once = block && block->op_flags & OPf_SPECIAL &&
9891 block->op_type == OP_NULL;
9893 PERL_UNUSED_ARG(debuggable);
9897 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
9898 || ( expr->op_type == OP_NOT
9899 && cUNOPx(expr)->op_first->op_type == OP_CONST
9900 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
9903 /* Return the block now, so that S_new_logop does not try to
9907 return block; /* do {} while 0 does once */
9910 if (expr->op_type == OP_READLINE
9911 || expr->op_type == OP_READDIR
9912 || expr->op_type == OP_GLOB
9913 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
9914 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
9915 expr = newUNOP(OP_DEFINED, 0,
9916 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
9917 } else if (expr->op_flags & OPf_KIDS) {
9918 const OP * const k1 = ((UNOP*)expr)->op_first;
9919 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL;
9920 switch (expr->op_type) {
9922 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
9923 && (k2->op_flags & OPf_STACKED)
9924 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
9925 expr = newUNOP(OP_DEFINED, 0, expr);
9929 if (k1 && (k1->op_type == OP_READDIR
9930 || k1->op_type == OP_GLOB
9931 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
9932 || k1->op_type == OP_EACH
9933 || k1->op_type == OP_AEACH))
9934 expr = newUNOP(OP_DEFINED, 0, expr);
9940 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
9941 * op, in listop. This is wrong. [perl #27024] */
9943 block = newOP(OP_NULL, 0);
9944 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
9945 o = new_logop(OP_AND, 0, &expr, &listop);
9952 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
9954 if (once && o != listop)
9956 assert(cUNOPo->op_first->op_type == OP_AND
9957 || cUNOPo->op_first->op_type == OP_OR);
9958 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
9962 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
9964 o->op_flags |= flags;
9966 o->op_flags |= OPf_SPECIAL; /* suppress cx_popblock() curpm restoration*/
9971 =for apidoc newWHILEOP
9973 Constructs, checks, and returns an op tree expressing a C<while> loop.
9974 This is a heavyweight loop, with structure that allows exiting the loop
9975 by C<last> and suchlike.
9977 C<loop> is an optional preconstructed C<enterloop> op to use in the
9978 loop; if it is null then a suitable op will be constructed automatically.
9979 C<expr> supplies the loop's controlling expression. C<block> supplies the
9980 main body of the loop, and C<cont> optionally supplies a C<continue> block
9981 that operates as a second half of the body. All of these optree inputs
9982 are consumed by this function and become part of the constructed op tree.
9984 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
9985 op and, shifted up eight bits, the eight bits of C<op_private> for
9986 the C<leaveloop> op, except that (in both cases) some bits will be set
9987 automatically. C<debuggable> is currently unused and should always be 1.
9988 C<has_my> can be supplied as true to force the
9989 loop body to be enclosed in its own scope.
9995 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
9996 OP *expr, OP *block, OP *cont, I32 has_my)
10005 PERL_UNUSED_ARG(debuggable);
10008 if (expr->op_type == OP_READLINE
10009 || expr->op_type == OP_READDIR
10010 || expr->op_type == OP_GLOB
10011 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
10012 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
10013 expr = newUNOP(OP_DEFINED, 0,
10014 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
10015 } else if (expr->op_flags & OPf_KIDS) {
10016 const OP * const k1 = ((UNOP*)expr)->op_first;
10017 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL;
10018 switch (expr->op_type) {
10020 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
10021 && (k2->op_flags & OPf_STACKED)
10022 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
10023 expr = newUNOP(OP_DEFINED, 0, expr);
10027 if (k1 && (k1->op_type == OP_READDIR
10028 || k1->op_type == OP_GLOB
10029 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
10030 || k1->op_type == OP_EACH
10031 || k1->op_type == OP_AEACH))
10032 expr = newUNOP(OP_DEFINED, 0, expr);
10039 block = newOP(OP_NULL, 0);
10040 else if (cont || has_my) {
10041 block = op_scope(block);
10045 next = LINKLIST(cont);
10048 OP * const unstack = newOP(OP_UNSTACK, 0);
10051 cont = op_append_elem(OP_LINESEQ, cont, unstack);
10055 listop = op_append_list(OP_LINESEQ, block, cont);
10057 redo = LINKLIST(listop);
10061 o = new_logop(OP_AND, 0, &expr, &listop);
10062 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
10063 op_free((OP*)loop);
10064 return expr; /* listop already freed by new_logop */
10067 ((LISTOP*)listop)->op_last->op_next =
10068 (o == listop ? redo : LINKLIST(o));
10074 NewOp(1101,loop,1,LOOP);
10075 OpTYPE_set(loop, OP_ENTERLOOP);
10076 loop->op_private = 0;
10077 loop->op_next = (OP*)loop;
10080 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
10082 loop->op_redoop = redo;
10083 loop->op_lastop = o;
10084 o->op_private |= loopflags;
10087 loop->op_nextop = next;
10089 loop->op_nextop = o;
10091 o->op_flags |= flags;
10092 o->op_private |= (flags >> 8);
10097 =for apidoc newFOROP
10099 Constructs, checks, and returns an op tree expressing a C<foreach>
10100 loop (iteration through a list of values). This is a heavyweight loop,
10101 with structure that allows exiting the loop by C<last> and suchlike.
10103 C<sv> optionally supplies the variable that will be aliased to each
10104 item in turn; if null, it defaults to C<$_>.
10105 C<expr> supplies the list of values to iterate over. C<block> supplies
10106 the main body of the loop, and C<cont> optionally supplies a C<continue>
10107 block that operates as a second half of the body. All of these optree
10108 inputs are consumed by this function and become part of the constructed
10111 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
10112 op and, shifted up eight bits, the eight bits of C<op_private> for
10113 the C<leaveloop> op, except that (in both cases) some bits will be set
10120 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
10125 PADOFFSET padoff = 0;
10127 I32 iterpflags = 0;
10129 PERL_ARGS_ASSERT_NEWFOROP;
10132 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
10133 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
10134 OpTYPE_set(sv, OP_RV2GV);
10136 /* The op_type check is needed to prevent a possible segfault
10137 * if the loop variable is undeclared and 'strict vars' is in
10138 * effect. This is illegal but is nonetheless parsed, so we
10139 * may reach this point with an OP_CONST where we're expecting
10142 if (cUNOPx(sv)->op_first->op_type == OP_GV
10143 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
10144 iterpflags |= OPpITER_DEF;
10146 else if (sv->op_type == OP_PADSV) { /* private variable */
10147 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
10148 padoff = sv->op_targ;
10152 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX);
10154 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN)
10157 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
10159 PADNAME * const pn = PAD_COMPNAME(padoff);
10160 const char * const name = PadnamePV(pn);
10162 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_')
10163 iterpflags |= OPpITER_DEF;
10167 sv = newGVOP(OP_GV, 0, PL_defgv);
10168 iterpflags |= OPpITER_DEF;
10171 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
10172 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), 1), OP_GREPSTART);
10173 iterflags |= OPf_STACKED;
10175 else if (expr->op_type == OP_NULL &&
10176 (expr->op_flags & OPf_KIDS) &&
10177 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
10179 /* Basically turn for($x..$y) into the same as for($x,$y), but we
10180 * set the STACKED flag to indicate that these values are to be
10181 * treated as min/max values by 'pp_enteriter'.
10183 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
10184 LOGOP* const range = (LOGOP*) flip->op_first;
10185 OP* const left = range->op_first;
10186 OP* const right = OpSIBLING(left);
10189 range->op_flags &= ~OPf_KIDS;
10190 /* detach range's children */
10191 op_sibling_splice((OP*)range, NULL, -1, NULL);
10193 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
10194 listop->op_first->op_next = range->op_next;
10195 left->op_next = range->op_other;
10196 right->op_next = (OP*)listop;
10197 listop->op_next = listop->op_first;
10200 expr = (OP*)(listop);
10202 iterflags |= OPf_STACKED;
10205 expr = op_lvalue(force_list(expr, 1), OP_GREPSTART);
10208 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags,
10209 op_append_elem(OP_LIST, list(expr),
10211 assert(!loop->op_next);
10212 /* for my $x () sets OPpLVAL_INTRO;
10213 * for our $x () sets OPpOUR_INTRO */
10214 loop->op_private = (U8)iterpflags;
10216 /* upgrade loop from a LISTOP to a LOOPOP;
10217 * keep it in-place if there's space */
10218 if (loop->op_slabbed
10219 && OpSLOT(loop)->opslot_size
10220 < SIZE_TO_PSIZE(sizeof(LOOP)) + OPSLOT_HEADER_P)
10222 /* no space; allocate new op */
10224 NewOp(1234,tmp,1,LOOP);
10225 Copy(loop,tmp,1,LISTOP);
10226 assert(loop->op_last->op_sibparent == (OP*)loop);
10227 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */
10228 S_op_destroy(aTHX_ (OP*)loop);
10231 else if (!loop->op_slabbed)
10233 /* loop was malloc()ed */
10234 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
10235 OpLASTSIB_set(loop->op_last, (OP*)loop);
10237 loop->op_targ = padoff;
10238 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
10243 =for apidoc newLOOPEX
10245 Constructs, checks, and returns a loop-exiting op (such as C<goto>
10246 or C<last>). C<type> is the opcode. C<label> supplies the parameter
10247 determining the target of the op; it is consumed by this function and
10248 becomes part of the constructed op tree.
10254 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
10258 PERL_ARGS_ASSERT_NEWLOOPEX;
10260 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
10261 || type == OP_CUSTOM);
10263 if (type != OP_GOTO) {
10264 /* "last()" means "last" */
10265 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
10266 o = newOP(type, OPf_SPECIAL);
10270 /* Check whether it's going to be a goto &function */
10271 if (label->op_type == OP_ENTERSUB
10272 && !(label->op_flags & OPf_STACKED))
10273 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
10276 /* Check for a constant argument */
10277 if (label->op_type == OP_CONST) {
10278 SV * const sv = ((SVOP *)label)->op_sv;
10280 const char *s = SvPV_const(sv,l);
10281 if (l == strlen(s)) {
10283 SvUTF8(((SVOP*)label)->op_sv),
10285 SvPV_nolen_const(((SVOP*)label)->op_sv)));
10289 /* If we have already created an op, we do not need the label. */
10292 else o = newUNOP(type, OPf_STACKED, label);
10294 PL_hints |= HINT_BLOCK_SCOPE;
10298 /* if the condition is a literal array or hash
10299 (or @{ ... } etc), make a reference to it.
10302 S_ref_array_or_hash(pTHX_ OP *cond)
10305 && (cond->op_type == OP_RV2AV
10306 || cond->op_type == OP_PADAV
10307 || cond->op_type == OP_RV2HV
10308 || cond->op_type == OP_PADHV))
10310 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
10313 && (cond->op_type == OP_ASLICE
10314 || cond->op_type == OP_KVASLICE
10315 || cond->op_type == OP_HSLICE
10316 || cond->op_type == OP_KVHSLICE)) {
10318 /* anonlist now needs a list from this op, was previously used in
10319 * scalar context */
10320 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF);
10321 cond->op_flags |= OPf_WANT_LIST;
10323 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
10330 /* These construct the optree fragments representing given()
10333 entergiven and enterwhen are LOGOPs; the op_other pointer
10334 points up to the associated leave op. We need this so we
10335 can put it in the context and make break/continue work.
10336 (Also, of course, pp_enterwhen will jump straight to
10337 op_other if the match fails.)
10341 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
10342 I32 enter_opcode, I32 leave_opcode,
10343 PADOFFSET entertarg)
10349 PERL_ARGS_ASSERT_NEWGIVWHENOP;
10350 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */
10352 enterop = alloc_LOGOP(enter_opcode, block, NULL);
10353 enterop->op_targ = 0;
10354 enterop->op_private = 0;
10356 o = newUNOP(leave_opcode, 0, (OP *) enterop);
10359 /* prepend cond if we have one */
10360 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond));
10362 o->op_next = LINKLIST(cond);
10363 cond->op_next = (OP *) enterop;
10366 /* This is a default {} block */
10367 enterop->op_flags |= OPf_SPECIAL;
10368 o ->op_flags |= OPf_SPECIAL;
10370 o->op_next = (OP *) enterop;
10373 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
10374 entergiven and enterwhen both
10377 enterop->op_next = LINKLIST(block);
10378 block->op_next = enterop->op_other = o;
10384 /* For the purposes of 'when(implied_smartmatch)'
10385 * versus 'when(boolean_expression)',
10386 * does this look like a boolean operation? For these purposes
10387 a boolean operation is:
10388 - a subroutine call [*]
10389 - a logical connective
10390 - a comparison operator
10391 - a filetest operator, with the exception of -s -M -A -C
10392 - defined(), exists() or eof()
10393 - /$re/ or $foo =~ /$re/
10395 [*] possibly surprising
10398 S_looks_like_bool(pTHX_ const OP *o)
10400 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
10402 switch(o->op_type) {
10405 return looks_like_bool(cLOGOPo->op_first);
10409 OP* sibl = OpSIBLING(cLOGOPo->op_first);
10412 looks_like_bool(cLOGOPo->op_first)
10413 && looks_like_bool(sibl));
10419 o->op_flags & OPf_KIDS
10420 && looks_like_bool(cUNOPo->op_first));
10424 case OP_NOT: case OP_XOR:
10426 case OP_EQ: case OP_NE: case OP_LT:
10427 case OP_GT: case OP_LE: case OP_GE:
10429 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
10430 case OP_I_GT: case OP_I_LE: case OP_I_GE:
10432 case OP_SEQ: case OP_SNE: case OP_SLT:
10433 case OP_SGT: case OP_SLE: case OP_SGE:
10435 case OP_SMARTMATCH:
10437 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
10438 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
10439 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
10440 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
10441 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
10442 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
10443 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
10444 case OP_FTTEXT: case OP_FTBINARY:
10446 case OP_DEFINED: case OP_EXISTS:
10447 case OP_MATCH: case OP_EOF:
10455 /* optimised-away (index() != -1) or similar comparison */
10456 if (o->op_private & OPpTRUEBOOL)
10461 /* Detect comparisons that have been optimized away */
10462 if (cSVOPo->op_sv == &PL_sv_yes
10463 || cSVOPo->op_sv == &PL_sv_no)
10476 =for apidoc newGIVENOP
10478 Constructs, checks, and returns an op tree expressing a C<given> block.
10479 C<cond> supplies the expression to whose value C<$_> will be locally
10480 aliased, and C<block> supplies the body of the C<given> construct; they
10481 are consumed by this function and become part of the constructed op tree.
10482 C<defsv_off> must be zero (it used to identity the pad slot of lexical $_).
10488 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
10490 PERL_ARGS_ASSERT_NEWGIVENOP;
10491 PERL_UNUSED_ARG(defsv_off);
10493 assert(!defsv_off);
10494 return newGIVWHENOP(
10495 ref_array_or_hash(cond),
10497 OP_ENTERGIVEN, OP_LEAVEGIVEN,
10502 =for apidoc newWHENOP
10504 Constructs, checks, and returns an op tree expressing a C<when> block.
10505 C<cond> supplies the test expression, and C<block> supplies the block
10506 that will be executed if the test evaluates to true; they are consumed
10507 by this function and become part of the constructed op tree. C<cond>
10508 will be interpreted DWIMically, often as a comparison against C<$_>,
10509 and may be null to generate a C<default> block.
10515 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
10517 const bool cond_llb = (!cond || looks_like_bool(cond));
10520 PERL_ARGS_ASSERT_NEWWHENOP;
10525 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
10527 scalar(ref_array_or_hash(cond)));
10530 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
10533 /* must not conflict with SVf_UTF8 */
10534 #define CV_CKPROTO_CURSTASH 0x1
10537 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
10538 const STRLEN len, const U32 flags)
10540 SV *name = NULL, *msg;
10541 const char * cvp = SvROK(cv)
10542 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV
10543 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv))
10546 STRLEN clen = CvPROTOLEN(cv), plen = len;
10548 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
10550 if (p == NULL && cvp == NULL)
10553 if (!ckWARN_d(WARN_PROTOTYPE))
10557 p = S_strip_spaces(aTHX_ p, &plen);
10558 cvp = S_strip_spaces(aTHX_ cvp, &clen);
10559 if ((flags & SVf_UTF8) == SvUTF8(cv)) {
10560 if (plen == clen && memEQ(cvp, p, plen))
10563 if (flags & SVf_UTF8) {
10564 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
10568 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
10574 msg = sv_newmortal();
10579 gv_efullname3(name = sv_newmortal(), gv, NULL);
10580 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
10581 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
10582 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) {
10583 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash)));
10584 sv_catpvs(name, "::");
10586 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV);
10587 assert (CvNAMED(SvRV_const(gv)));
10588 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv))));
10590 else sv_catsv(name, (SV *)gv);
10592 else name = (SV *)gv;
10594 sv_setpvs(msg, "Prototype mismatch:");
10596 Perl_sv_catpvf(aTHX_ msg, " sub %" SVf, SVfARG(name));
10598 Perl_sv_catpvf(aTHX_ msg, " (%" UTF8f ")",
10599 UTF8fARG(SvUTF8(cv),clen,cvp)
10602 sv_catpvs(msg, ": none");
10603 sv_catpvs(msg, " vs ");
10605 Perl_sv_catpvf(aTHX_ msg, "(%" UTF8f ")", UTF8fARG(flags & SVf_UTF8,len,p));
10607 sv_catpvs(msg, "none");
10608 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%" SVf, SVfARG(msg));
10611 static void const_sv_xsub(pTHX_ CV* cv);
10612 static void const_av_xsub(pTHX_ CV* cv);
10616 =head1 Optree Manipulation Functions
10618 =for apidoc cv_const_sv
10620 If C<cv> is a constant sub eligible for inlining, returns the constant
10621 value returned by the sub. Otherwise, returns C<NULL>.
10623 Constant subs can be created with C<newCONSTSUB> or as described in
10624 L<perlsub/"Constant Functions">.
10629 Perl_cv_const_sv(const CV *const cv)
10634 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
10636 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
10637 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
10642 Perl_cv_const_sv_or_av(const CV * const cv)
10646 if (SvROK(cv)) return SvRV((SV *)cv);
10647 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
10648 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
10651 /* op_const_sv: examine an optree to determine whether it's in-lineable.
10652 * Can be called in 2 ways:
10655 * look for a single OP_CONST with attached value: return the value
10657 * allow_lex && !CvCONST(cv);
10659 * examine the clone prototype, and if contains only a single
10660 * OP_CONST, return the value; or if it contains a single PADSV ref-
10661 * erencing an outer lexical, turn on CvCONST to indicate the CV is
10662 * a candidate for "constizing" at clone time, and return NULL.
10666 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex)
10669 bool padsv = FALSE;
10674 for (; o; o = o->op_next) {
10675 const OPCODE type = o->op_type;
10677 if (type == OP_NEXTSTATE || type == OP_LINESEQ
10679 || type == OP_PUSHMARK)
10681 if (type == OP_DBSTATE)
10683 if (type == OP_LEAVESUB)
10687 if (type == OP_CONST && cSVOPo->op_sv)
10688 sv = cSVOPo->op_sv;
10689 else if (type == OP_UNDEF && !o->op_private) {
10693 else if (allow_lex && type == OP_PADSV) {
10694 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER)
10696 sv = &PL_sv_undef; /* an arbitrary non-null value */
10714 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
10715 PADNAME * const name, SV ** const const_svp)
10718 assert (o || name);
10719 assert (const_svp);
10721 if (CvFLAGS(PL_compcv)) {
10722 /* might have had built-in attrs applied */
10723 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
10724 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
10725 && ckWARN(WARN_MISC))
10727 /* protect against fatal warnings leaking compcv */
10728 SAVEFREESV(PL_compcv);
10729 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
10730 SvREFCNT_inc_simple_void_NN(PL_compcv);
10733 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
10734 & ~(CVf_LVALUE * pureperl));
10739 /* redundant check for speed: */
10740 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
10741 const line_t oldline = CopLINE(PL_curcop);
10744 : sv_2mortal(newSVpvn_utf8(
10745 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
10747 if (PL_parser && PL_parser->copline != NOLINE)
10748 /* This ensures that warnings are reported at the first
10749 line of a redefinition, not the last. */
10750 CopLINE_set(PL_curcop, PL_parser->copline);
10751 /* protect against fatal warnings leaking compcv */
10752 SAVEFREESV(PL_compcv);
10753 report_redefined_cv(namesv, cv, const_svp);
10754 SvREFCNT_inc_simple_void_NN(PL_compcv);
10755 CopLINE_set(PL_curcop, oldline);
10762 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
10767 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
10770 CV *compcv = PL_compcv;
10773 PADOFFSET pax = o->op_targ;
10774 CV *outcv = CvOUTSIDE(PL_compcv);
10777 bool reusable = FALSE;
10779 #ifdef PERL_DEBUG_READONLY_OPS
10780 OPSLAB *slab = NULL;
10783 PERL_ARGS_ASSERT_NEWMYSUB;
10785 PL_hints |= HINT_BLOCK_SCOPE;
10787 /* Find the pad slot for storing the new sub.
10788 We cannot use PL_comppad, as it is the pad owned by the new sub. We
10789 need to look in CvOUTSIDE and find the pad belonging to the enclos-
10790 ing sub. And then we need to dig deeper if this is a lexical from
10792 my sub foo; sub { sub foo { } }
10795 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
10796 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
10797 pax = PARENT_PAD_INDEX(name);
10798 outcv = CvOUTSIDE(outcv);
10803 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
10804 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
10805 spot = (CV **)svspot;
10807 if (!(PL_parser && PL_parser->error_count))
10808 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name), 0);
10811 assert(proto->op_type == OP_CONST);
10812 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
10813 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
10823 if (PL_parser && PL_parser->error_count) {
10825 SvREFCNT_dec(PL_compcv);
10830 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
10832 svspot = (SV **)(spot = &clonee);
10834 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
10837 assert (SvTYPE(*spot) == SVt_PVCV);
10838 if (CvNAMED(*spot))
10839 hek = CvNAME_HEK(*spot);
10843 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
10844 CvNAME_HEK_set(*spot, hek =
10847 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
10851 CvLEXICAL_on(*spot);
10853 cv = PadnamePROTOCV(name);
10854 svspot = (SV **)(spot = &PadnamePROTOCV(name));
10858 /* This makes sub {}; work as expected. */
10859 if (block->op_type == OP_STUB) {
10860 const line_t l = PL_parser->copline;
10862 block = newSTATEOP(0, NULL, 0);
10863 PL_parser->copline = l;
10865 block = CvLVALUE(compcv)
10866 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv))
10867 ? newUNOP(OP_LEAVESUBLV, 0,
10868 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
10869 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
10870 start = LINKLIST(block);
10871 block->op_next = 0;
10872 if (ps && !*ps && !attrs && !CvLVALUE(compcv))
10873 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE);
10881 const bool exists = CvROOT(cv) || CvXSUB(cv);
10883 /* if the subroutine doesn't exist and wasn't pre-declared
10884 * with a prototype, assume it will be AUTOLOADed,
10885 * skipping the prototype check
10887 if (exists || SvPOK(cv))
10888 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len,
10890 /* already defined? */
10892 S_already_defined(aTHX_ cv, block, NULL, name, &const_sv);
10898 /* just a "sub foo;" when &foo is already defined */
10899 SAVEFREESV(compcv);
10903 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
10910 SvREFCNT_inc_simple_void_NN(const_sv);
10911 SvFLAGS(const_sv) |= SVs_PADTMP;
10913 assert(!CvROOT(cv) && !CvCONST(cv));
10914 cv_forget_slab(cv);
10917 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
10918 CvFILE_set_from_cop(cv, PL_curcop);
10919 CvSTASH_set(cv, PL_curstash);
10922 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
10923 CvXSUBANY(cv).any_ptr = const_sv;
10924 CvXSUB(cv) = const_sv_xsub;
10928 CvFLAGS(cv) |= CvMETHOD(compcv);
10930 SvREFCNT_dec(compcv);
10935 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
10936 determine whether this sub definition is in the same scope as its
10937 declaration. If this sub definition is inside an inner named pack-
10938 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
10939 the package sub. So check PadnameOUTER(name) too.
10941 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
10942 assert(!CvWEAKOUTSIDE(compcv));
10943 SvREFCNT_dec(CvOUTSIDE(compcv));
10944 CvWEAKOUTSIDE_on(compcv);
10946 /* XXX else do we have a circular reference? */
10948 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
10949 /* transfer PL_compcv to cv */
10951 bool free_file = CvFILE(cv) && CvDYNFILE(cv);
10952 cv_flags_t preserved_flags =
10953 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
10954 PADLIST *const temp_padl = CvPADLIST(cv);
10955 CV *const temp_cv = CvOUTSIDE(cv);
10956 const cv_flags_t other_flags =
10957 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
10958 OP * const cvstart = CvSTART(cv);
10962 CvFLAGS(compcv) | preserved_flags;
10963 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
10964 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
10965 CvPADLIST_set(cv, CvPADLIST(compcv));
10966 CvOUTSIDE(compcv) = temp_cv;
10967 CvPADLIST_set(compcv, temp_padl);
10968 CvSTART(cv) = CvSTART(compcv);
10969 CvSTART(compcv) = cvstart;
10970 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
10971 CvFLAGS(compcv) |= other_flags;
10974 Safefree(CvFILE(cv));
10978 /* inner references to compcv must be fixed up ... */
10979 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
10980 if (PERLDB_INTER)/* Advice debugger on the new sub. */
10981 ++PL_sub_generation;
10984 /* Might have had built-in attributes applied -- propagate them. */
10985 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
10987 /* ... before we throw it away */
10988 SvREFCNT_dec(compcv);
10989 PL_compcv = compcv = cv;
10998 if (!CvNAME_HEK(cv)) {
10999 if (hek) (void)share_hek_hek(hek);
11003 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
11004 hek = share_hek(PadnamePV(name)+1,
11005 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
11008 CvNAME_HEK_set(cv, hek);
11014 if (CvFILE(cv) && CvDYNFILE(cv))
11015 Safefree(CvFILE(cv));
11016 CvFILE_set_from_cop(cv, PL_curcop);
11017 CvSTASH_set(cv, PL_curstash);
11020 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
11022 SvUTF8_on(MUTABLE_SV(cv));
11026 /* If we assign an optree to a PVCV, then we've defined a
11027 * subroutine that the debugger could be able to set a breakpoint
11028 * in, so signal to pp_entereval that it should not throw away any
11029 * saved lines at scope exit. */
11031 PL_breakable_sub_gen++;
11032 CvROOT(cv) = block;
11033 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
11034 itself has a refcount. */
11036 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
11037 #ifdef PERL_DEBUG_READONLY_OPS
11038 slab = (OPSLAB *)CvSTART(cv);
11040 S_process_optree(aTHX_ cv, block, start);
11045 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
11046 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
11050 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
11051 SV * const tmpstr = sv_newmortal();
11052 GV * const db_postponed = gv_fetchpvs("DB::postponed",
11053 GV_ADDMULTI, SVt_PVHV);
11055 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
11056 CopFILE(PL_curcop),
11058 (long)CopLINE(PL_curcop));
11059 if (HvNAME_HEK(PL_curstash)) {
11060 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
11061 sv_catpvs(tmpstr, "::");
11064 sv_setpvs(tmpstr, "__ANON__::");
11066 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
11067 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
11068 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
11069 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
11070 hv = GvHVn(db_postponed);
11071 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
11072 CV * const pcv = GvCV(db_postponed);
11078 call_sv(MUTABLE_SV(pcv), G_DISCARD);
11086 assert(CvDEPTH(outcv));
11088 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
11090 cv_clone_into(clonee, *spot);
11091 else *spot = cv_clone(clonee);
11092 SvREFCNT_dec_NN(clonee);
11096 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
11097 PADOFFSET depth = CvDEPTH(outcv);
11100 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
11102 *svspot = SvREFCNT_inc_simple_NN(cv);
11103 SvREFCNT_dec(oldcv);
11109 PL_parser->copline = NOLINE;
11110 LEAVE_SCOPE(floor);
11111 #ifdef PERL_DEBUG_READONLY_OPS
11120 =for apidoc newATTRSUB_x
11122 Construct a Perl subroutine, also performing some surrounding jobs.
11124 This function is expected to be called in a Perl compilation context,
11125 and some aspects of the subroutine are taken from global variables
11126 associated with compilation. In particular, C<PL_compcv> represents
11127 the subroutine that is currently being compiled. It must be non-null
11128 when this function is called, and some aspects of the subroutine being
11129 constructed are taken from it. The constructed subroutine may actually
11130 be a reuse of the C<PL_compcv> object, but will not necessarily be so.
11132 If C<block> is null then the subroutine will have no body, and for the
11133 time being it will be an error to call it. This represents a forward
11134 subroutine declaration such as S<C<sub foo ($$);>>. If C<block> is
11135 non-null then it provides the Perl code of the subroutine body, which
11136 will be executed when the subroutine is called. This body includes
11137 any argument unwrapping code resulting from a subroutine signature or
11138 similar. The pad use of the code must correspond to the pad attached
11139 to C<PL_compcv>. The code is not expected to include a C<leavesub> or
11140 C<leavesublv> op; this function will add such an op. C<block> is consumed
11141 by this function and will become part of the constructed subroutine.
11143 C<proto> specifies the subroutine's prototype, unless one is supplied
11144 as an attribute (see below). If C<proto> is null, then the subroutine
11145 will not have a prototype. If C<proto> is non-null, it must point to a
11146 C<const> op whose value is a string, and the subroutine will have that
11147 string as its prototype. If a prototype is supplied as an attribute, the
11148 attribute takes precedence over C<proto>, but in that case C<proto> should
11149 preferably be null. In any case, C<proto> is consumed by this function.
11151 C<attrs> supplies attributes to be applied the subroutine. A handful of
11152 attributes take effect by built-in means, being applied to C<PL_compcv>
11153 immediately when seen. Other attributes are collected up and attached
11154 to the subroutine by this route. C<attrs> may be null to supply no
11155 attributes, or point to a C<const> op for a single attribute, or point
11156 to a C<list> op whose children apart from the C<pushmark> are C<const>
11157 ops for one or more attributes. Each C<const> op must be a string,
11158 giving the attribute name optionally followed by parenthesised arguments,
11159 in the manner in which attributes appear in Perl source. The attributes
11160 will be applied to the sub by this function. C<attrs> is consumed by
11163 If C<o_is_gv> is false and C<o> is null, then the subroutine will
11164 be anonymous. If C<o_is_gv> is false and C<o> is non-null, then C<o>
11165 must point to a C<const> op, which will be consumed by this function,
11166 and its string value supplies a name for the subroutine. The name may
11167 be qualified or unqualified, and if it is unqualified then a default
11168 stash will be selected in some manner. If C<o_is_gv> is true, then C<o>
11169 doesn't point to an C<OP> at all, but is instead a cast pointer to a C<GV>
11170 by which the subroutine will be named.
11172 If there is already a subroutine of the specified name, then the new
11173 sub will either replace the existing one in the glob or be merged with
11174 the existing one. A warning may be generated about redefinition.
11176 If the subroutine has one of a few special names, such as C<BEGIN> or
11177 C<END>, then it will be claimed by the appropriate queue for automatic
11178 running of phase-related subroutines. In this case the relevant glob will
11179 be left not containing any subroutine, even if it did contain one before.
11180 In the case of C<BEGIN>, the subroutine will be executed and the reference
11181 to it disposed of before this function returns.
11183 The function returns a pointer to the constructed subroutine. If the sub
11184 is anonymous then ownership of one counted reference to the subroutine
11185 is transferred to the caller. If the sub is named then the caller does
11186 not get ownership of a reference. In most such cases, where the sub
11187 has a non-phase name, the sub will be alive at the point it is returned
11188 by virtue of being contained in the glob that names it. A phase-named
11189 subroutine will usually be alive by virtue of the reference owned by the
11190 phase's automatic run queue. But a C<BEGIN> subroutine, having already
11191 been executed, will quite likely have been destroyed already by the
11192 time this function returns, making it erroneous for the caller to make
11193 any use of the returned pointer. It is the caller's responsibility to
11194 ensure that it knows which of these situations applies.
11199 /* _x = extended */
11201 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
11202 OP *block, bool o_is_gv)
11206 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
11208 CV *cv = NULL; /* the previous CV with this name, if any */
11210 const bool ec = PL_parser && PL_parser->error_count;
11211 /* If the subroutine has no body, no attributes, and no builtin attributes
11212 then it's just a sub declaration, and we may be able to get away with
11213 storing with a placeholder scalar in the symbol table, rather than a
11214 full CV. If anything is present then it will take a full CV to
11216 const I32 gv_fetch_flags
11217 = ec ? GV_NOADD_NOINIT :
11218 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
11219 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
11221 const char * const name =
11222 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
11224 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
11225 bool evanescent = FALSE;
11227 #ifdef PERL_DEBUG_READONLY_OPS
11228 OPSLAB *slab = NULL;
11236 /* Try to optimise and avoid creating a GV. Instead, the CV’s name
11237 hek and CvSTASH pointer together can imply the GV. If the name
11238 contains a package name, then GvSTASH(CvGV(cv)) may differ from
11239 CvSTASH, so forego the optimisation if we find any.
11240 Also, we may be called from load_module at run time, so
11241 PL_curstash (which sets CvSTASH) may not point to the stash the
11242 sub is stored in. */
11243 /* XXX This optimization is currently disabled for packages other
11244 than main, since there was too much CPAN breakage. */
11246 ec ? GV_NOADD_NOINIT
11247 : (IN_PERL_RUNTIME && PL_curstash != CopSTASH(PL_curcop))
11248 || PL_curstash != PL_defstash
11249 || memchr(name, ':', namlen) || memchr(name, '\'', namlen)
11251 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL;
11252 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV);
11254 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
11255 SV * const sv = sv_newmortal();
11256 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%" IVdf "]",
11257 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
11258 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
11259 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
11261 } else if (PL_curstash) {
11262 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
11265 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
11271 move_proto_attr(&proto, &attrs, gv, 0);
11274 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv, 1);
11279 assert(proto->op_type == OP_CONST);
11280 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
11281 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
11297 SvREFCNT_dec(PL_compcv);
11302 if (name && block) {
11303 const char *s = (char *) my_memrchr(name, ':', namlen);
11304 s = s ? s+1 : name;
11305 if (strEQ(s, "BEGIN")) {
11306 if (PL_in_eval & EVAL_KEEPERR)
11307 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
11309 SV * const errsv = ERRSV;
11310 /* force display of errors found but not reported */
11311 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
11312 Perl_croak_nocontext("%" SVf, SVfARG(errsv));
11319 if (!block && SvTYPE(gv) != SVt_PVGV) {
11320 /* If we are not defining a new sub and the existing one is not a
11322 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) {
11323 /* We are applying attributes to an existing sub, so we need it
11324 upgraded if it is a constant. */
11325 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)
11326 gv_init_pvn(gv, PL_curstash, name, namlen,
11327 SVf_UTF8 * name_is_utf8);
11329 else { /* Maybe prototype now, and had at maximum
11330 a prototype or const/sub ref before. */
11331 if (SvTYPE(gv) > SVt_NULL) {
11332 cv_ckproto_len_flags((const CV *)gv,
11333 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
11339 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
11341 SvUTF8_on(MUTABLE_SV(gv));
11344 sv_setiv(MUTABLE_SV(gv), -1);
11347 SvREFCNT_dec(PL_compcv);
11348 cv = PL_compcv = NULL;
11353 cv = (!name || (isGV(gv) && GvCVGEN(gv)))
11357 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
11363 /* This makes sub {}; work as expected. */
11364 if (block->op_type == OP_STUB) {
11365 const line_t l = PL_parser->copline;
11367 block = newSTATEOP(0, NULL, 0);
11368 PL_parser->copline = l;
11370 block = CvLVALUE(PL_compcv)
11371 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)
11372 && (!isGV(gv) || !GvASSUMECV(gv)))
11373 ? newUNOP(OP_LEAVESUBLV, 0,
11374 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
11375 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
11376 start = LINKLIST(block);
11377 block->op_next = 0;
11378 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv))
11380 S_op_const_sv(aTHX_ start, PL_compcv,
11381 cBOOL(CvCLONE(PL_compcv)));
11388 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) {
11389 cv_ckproto_len_flags((const CV *)gv,
11390 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
11391 ps_len, ps_utf8|CV_CKPROTO_CURSTASH);
11393 /* All the other code for sub redefinition warnings expects the
11394 clobbered sub to be a CV. Instead of making all those code
11395 paths more complex, just inline the RV version here. */
11396 const line_t oldline = CopLINE(PL_curcop);
11397 assert(IN_PERL_COMPILETIME);
11398 if (PL_parser && PL_parser->copline != NOLINE)
11399 /* This ensures that warnings are reported at the first
11400 line of a redefinition, not the last. */
11401 CopLINE_set(PL_curcop, PL_parser->copline);
11402 /* protect against fatal warnings leaking compcv */
11403 SAVEFREESV(PL_compcv);
11405 if (ckWARN(WARN_REDEFINE)
11406 || ( ckWARN_d(WARN_REDEFINE)
11407 && ( !const_sv || SvRV(gv) == const_sv
11408 || sv_cmp(SvRV(gv), const_sv) ))) {
11410 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
11411 "Constant subroutine %" SVf " redefined",
11412 SVfARG(cSVOPo->op_sv));
11415 SvREFCNT_inc_simple_void_NN(PL_compcv);
11416 CopLINE_set(PL_curcop, oldline);
11417 SvREFCNT_dec(SvRV(gv));
11422 const bool exists = CvROOT(cv) || CvXSUB(cv);
11424 /* if the subroutine doesn't exist and wasn't pre-declared
11425 * with a prototype, assume it will be AUTOLOADed,
11426 * skipping the prototype check
11428 if (exists || SvPOK(cv))
11429 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
11430 /* already defined (or promised)? */
11431 if (exists || (isGV(gv) && GvASSUMECV(gv))) {
11432 S_already_defined(aTHX_ cv, block, o, NULL, &const_sv);
11438 /* just a "sub foo;" when &foo is already defined */
11439 SAVEFREESV(PL_compcv);
11446 SvREFCNT_inc_simple_void_NN(const_sv);
11447 SvFLAGS(const_sv) |= SVs_PADTMP;
11449 assert(!CvROOT(cv) && !CvCONST(cv));
11450 cv_forget_slab(cv);
11451 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
11452 CvXSUBANY(cv).any_ptr = const_sv;
11453 CvXSUB(cv) = const_sv_xsub;
11457 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
11460 if (isGV(gv) || CvMETHOD(PL_compcv)) {
11461 if (name && isGV(gv))
11462 GvCV_set(gv, NULL);
11463 cv = newCONSTSUB_flags(
11464 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
11468 assert(SvREFCNT((SV*)cv) != 0);
11469 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
11473 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
11474 prepare_SV_for_RV((SV *)gv);
11475 SvOK_off((SV *)gv);
11478 SvRV_set(gv, const_sv);
11482 SvREFCNT_dec(PL_compcv);
11487 /* don't copy new BEGIN CV to old BEGIN CV - RT #129099 */
11488 if (name && cv && *name == 'B' && strEQ(name, "BEGIN"))
11491 if (cv) { /* must reuse cv if autoloaded */
11492 /* transfer PL_compcv to cv */
11494 bool free_file = CvFILE(cv) && CvDYNFILE(cv);
11495 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
11496 PADLIST *const temp_av = CvPADLIST(cv);
11497 CV *const temp_cv = CvOUTSIDE(cv);
11498 const cv_flags_t other_flags =
11499 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
11500 OP * const cvstart = CvSTART(cv);
11504 assert(!CvCVGV_RC(cv));
11505 assert(CvGV(cv) == gv);
11510 PERL_HASH(hash, name, namlen);
11520 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs
11522 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
11523 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
11524 CvPADLIST_set(cv,CvPADLIST(PL_compcv));
11525 CvOUTSIDE(PL_compcv) = temp_cv;
11526 CvPADLIST_set(PL_compcv, temp_av);
11527 CvSTART(cv) = CvSTART(PL_compcv);
11528 CvSTART(PL_compcv) = cvstart;
11529 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
11530 CvFLAGS(PL_compcv) |= other_flags;
11533 Safefree(CvFILE(cv));
11535 CvFILE_set_from_cop(cv, PL_curcop);
11536 CvSTASH_set(cv, PL_curstash);
11538 /* inner references to PL_compcv must be fixed up ... */
11539 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
11540 if (PERLDB_INTER)/* Advice debugger on the new sub. */
11541 ++PL_sub_generation;
11544 /* Might have had built-in attributes applied -- propagate them. */
11545 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
11547 /* ... before we throw it away */
11548 SvREFCNT_dec(PL_compcv);
11553 if (name && isGV(gv)) {
11556 if (HvENAME_HEK(GvSTASH(gv)))
11557 /* sub Foo::bar { (shift)+1 } */
11558 gv_method_changed(gv);
11562 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
11563 prepare_SV_for_RV((SV *)gv);
11564 SvOK_off((SV *)gv);
11567 SvRV_set(gv, (SV *)cv);
11568 if (HvENAME_HEK(PL_curstash))
11569 mro_method_changed_in(PL_curstash);
11573 assert(SvREFCNT((SV*)cv) != 0);
11575 if (!CvHASGV(cv)) {
11581 PERL_HASH(hash, name, namlen);
11582 CvNAME_HEK_set(cv, share_hek(name,
11588 CvFILE_set_from_cop(cv, PL_curcop);
11589 CvSTASH_set(cv, PL_curstash);
11593 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
11595 SvUTF8_on(MUTABLE_SV(cv));
11599 /* If we assign an optree to a PVCV, then we've defined a
11600 * subroutine that the debugger could be able to set a breakpoint
11601 * in, so signal to pp_entereval that it should not throw away any
11602 * saved lines at scope exit. */
11604 PL_breakable_sub_gen++;
11605 CvROOT(cv) = block;
11606 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
11607 itself has a refcount. */
11609 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
11610 #ifdef PERL_DEBUG_READONLY_OPS
11611 slab = (OPSLAB *)CvSTART(cv);
11613 S_process_optree(aTHX_ cv, block, start);
11618 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
11619 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv))
11620 ? GvSTASH(CvGV(cv))
11624 apply_attrs(stash, MUTABLE_SV(cv), attrs);
11626 SvREFCNT_inc_simple_void_NN(cv);
11629 if (block && has_name) {
11630 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
11631 SV * const tmpstr = cv_name(cv,NULL,0);
11632 GV * const db_postponed = gv_fetchpvs("DB::postponed",
11633 GV_ADDMULTI, SVt_PVHV);
11635 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
11636 CopFILE(PL_curcop),
11638 (long)CopLINE(PL_curcop));
11639 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
11640 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
11641 hv = GvHVn(db_postponed);
11642 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
11643 CV * const pcv = GvCV(db_postponed);
11649 call_sv(MUTABLE_SV(pcv), G_DISCARD);
11655 if (PL_parser && PL_parser->error_count)
11656 clear_special_blocks(name, gv, cv);
11659 process_special_blocks(floor, name, gv, cv);
11665 assert(!cv || evanescent || SvREFCNT((SV*)cv) != 0);
11667 PL_parser->copline = NOLINE;
11668 LEAVE_SCOPE(floor);
11670 assert(!cv || evanescent || SvREFCNT((SV*)cv) != 0);
11672 #ifdef PERL_DEBUG_READONLY_OPS
11676 if (cv && name && block && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv)))
11677 pad_add_weakref(cv);
11683 S_clear_special_blocks(pTHX_ const char *const fullname,
11684 GV *const gv, CV *const cv) {
11688 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS;
11690 colon = strrchr(fullname,':');
11691 name = colon ? colon + 1 : fullname;
11693 if ((*name == 'B' && strEQ(name, "BEGIN"))
11694 || (*name == 'E' && strEQ(name, "END"))
11695 || (*name == 'U' && strEQ(name, "UNITCHECK"))
11696 || (*name == 'C' && strEQ(name, "CHECK"))
11697 || (*name == 'I' && strEQ(name, "INIT"))) {
11702 GvCV_set(gv, NULL);
11703 SvREFCNT_dec_NN(MUTABLE_SV(cv));
11707 /* Returns true if the sub has been freed. */
11709 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
11713 const char *const colon = strrchr(fullname,':');
11714 const char *const name = colon ? colon + 1 : fullname;
11716 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
11718 if (*name == 'B') {
11719 if (strEQ(name, "BEGIN")) {
11720 const I32 oldscope = PL_scopestack_ix;
11723 if (floor) LEAVE_SCOPE(floor);
11725 PUSHSTACKi(PERLSI_REQUIRE);
11726 SAVECOPFILE(&PL_compiling);
11727 SAVECOPLINE(&PL_compiling);
11728 SAVEVPTR(PL_curcop);
11730 DEBUG_x( dump_sub(gv) );
11731 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
11732 GvCV_set(gv,0); /* cv has been hijacked */
11733 call_list(oldscope, PL_beginav);
11737 return !PL_savebegin;
11742 if (*name == 'E') {
11743 if (strEQ(name, "END")) {
11744 DEBUG_x( dump_sub(gv) );
11745 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
11748 } else if (*name == 'U') {
11749 if (strEQ(name, "UNITCHECK")) {
11750 /* It's never too late to run a unitcheck block */
11751 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
11755 } else if (*name == 'C') {
11756 if (strEQ(name, "CHECK")) {
11758 /* diag_listed_as: Too late to run %s block */
11759 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
11760 "Too late to run CHECK block");
11761 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
11765 } else if (*name == 'I') {
11766 if (strEQ(name, "INIT")) {
11768 /* diag_listed_as: Too late to run %s block */
11769 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
11770 "Too late to run INIT block");
11771 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
11777 DEBUG_x( dump_sub(gv) );
11779 GvCV_set(gv,0); /* cv has been hijacked */
11785 =for apidoc newCONSTSUB
11787 Behaves like L</newCONSTSUB_flags>, except that C<name> is nul-terminated
11788 rather than of counted length, and no flags are set. (This means that
11789 C<name> is always interpreted as Latin-1.)
11795 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
11797 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
11801 =for apidoc newCONSTSUB_flags
11803 Construct a constant subroutine, also performing some surrounding
11804 jobs. A scalar constant-valued subroutine is eligible for inlining
11805 at compile-time, and in Perl code can be created by S<C<sub FOO () {
11806 123 }>>. Other kinds of constant subroutine have other treatment.
11808 The subroutine will have an empty prototype and will ignore any arguments
11809 when called. Its constant behaviour is determined by C<sv>. If C<sv>
11810 is null, the subroutine will yield an empty list. If C<sv> points to a
11811 scalar, the subroutine will always yield that scalar. If C<sv> points
11812 to an array, the subroutine will always yield a list of the elements of
11813 that array in list context, or the number of elements in the array in
11814 scalar context. This function takes ownership of one counted reference
11815 to the scalar or array, and will arrange for the object to live as long
11816 as the subroutine does. If C<sv> points to a scalar then the inlining
11817 assumes that the value of the scalar will never change, so the caller
11818 must ensure that the scalar is not subsequently written to. If C<sv>
11819 points to an array then no such assumption is made, so it is ostensibly
11820 safe to mutate the array or its elements, but whether this is really
11821 supported has not been determined.
11823 The subroutine will have C<CvFILE> set according to C<PL_curcop>.
11824 Other aspects of the subroutine will be left in their default state.
11825 The caller is free to mutate the subroutine beyond its initial state
11826 after this function has returned.
11828 If C<name> is null then the subroutine will be anonymous, with its
11829 C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the
11830 subroutine will be named accordingly, referenced by the appropriate glob.
11831 C<name> is a string of length C<len> bytes giving a sigilless symbol
11832 name, in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1
11833 otherwise. The name may be either qualified or unqualified. If the
11834 name is unqualified then it defaults to being in the stash specified by
11835 C<stash> if that is non-null, or to C<PL_curstash> if C<stash> is null.
11836 The symbol is always added to the stash if necessary, with C<GV_ADDMULTI>
11839 C<flags> should not have bits set other than C<SVf_UTF8>.
11841 If there is already a subroutine of the specified name, then the new sub
11842 will replace the existing one in the glob. A warning may be generated
11843 about the redefinition.
11845 If the subroutine has one of a few special names, such as C<BEGIN> or
11846 C<END>, then it will be claimed by the appropriate queue for automatic
11847 running of phase-related subroutines. In this case the relevant glob will
11848 be left not containing any subroutine, even if it did contain one before.
11849 Execution of the subroutine will likely be a no-op, unless C<sv> was
11850 a tied array or the caller modified the subroutine in some interesting
11851 way before it was executed. In the case of C<BEGIN>, the treatment is
11852 buggy: the sub will be executed when only half built, and may be deleted
11853 prematurely, possibly causing a crash.
11855 The function returns a pointer to the constructed subroutine. If the sub
11856 is anonymous then ownership of one counted reference to the subroutine
11857 is transferred to the caller. If the sub is named then the caller does
11858 not get ownership of a reference. In most such cases, where the sub
11859 has a non-phase name, the sub will be alive at the point it is returned
11860 by virtue of being contained in the glob that names it. A phase-named
11861 subroutine will usually be alive by virtue of the reference owned by
11862 the phase's automatic run queue. A C<BEGIN> subroutine may have been
11863 destroyed already by the time this function returns, but currently bugs
11864 occur in that case before the caller gets control. It is the caller's
11865 responsibility to ensure that it knows which of these situations applies.
11871 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
11875 const char *const file = CopFILE(PL_curcop);
11879 if (IN_PERL_RUNTIME) {
11880 /* at runtime, it's not safe to manipulate PL_curcop: it may be
11881 * an op shared between threads. Use a non-shared COP for our
11883 SAVEVPTR(PL_curcop);
11884 SAVECOMPILEWARNINGS();
11885 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
11886 PL_curcop = &PL_compiling;
11888 SAVECOPLINE(PL_curcop);
11889 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
11892 PL_hints &= ~HINT_BLOCK_SCOPE;
11895 SAVEGENERICSV(PL_curstash);
11896 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
11899 /* Protect sv against leakage caused by fatal warnings. */
11900 if (sv) SAVEFREESV(sv);
11902 /* file becomes the CvFILE. For an XS, it's usually static storage,
11903 and so doesn't get free()d. (It's expected to be from the C pre-
11904 processor __FILE__ directive). But we need a dynamically allocated one,
11905 and we need it to get freed. */
11906 cv = newXS_len_flags(name, len,
11907 sv && SvTYPE(sv) == SVt_PVAV
11910 file ? file : "", "",
11911 &sv, XS_DYNAMIC_FILENAME | flags);
11913 assert(SvREFCNT((SV*)cv) != 0);
11914 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
11925 Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be
11926 static storage, as it is used directly as CvFILE(), without a copy being made.
11932 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
11934 PERL_ARGS_ASSERT_NEWXS;
11935 return newXS_len_flags(
11936 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
11941 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
11942 const char *const filename, const char *const proto,
11945 PERL_ARGS_ASSERT_NEWXS_FLAGS;
11946 return newXS_len_flags(
11947 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
11952 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr)
11954 PERL_ARGS_ASSERT_NEWXS_DEFFILE;
11955 return newXS_len_flags(
11956 name, strlen(name), subaddr, NULL, NULL, NULL, 0
11961 =for apidoc newXS_len_flags
11963 Construct an XS subroutine, also performing some surrounding jobs.
11965 The subroutine will have the entry point C<subaddr>. It will have
11966 the prototype specified by the nul-terminated string C<proto>, or
11967 no prototype if C<proto> is null. The prototype string is copied;
11968 the caller can mutate the supplied string afterwards. If C<filename>
11969 is non-null, it must be a nul-terminated filename, and the subroutine
11970 will have its C<CvFILE> set accordingly. By default C<CvFILE> is set to
11971 point directly to the supplied string, which must be static. If C<flags>
11972 has the C<XS_DYNAMIC_FILENAME> bit set, then a copy of the string will
11975 Other aspects of the subroutine will be left in their default state.
11976 If anything else needs to be done to the subroutine for it to function
11977 correctly, it is the caller's responsibility to do that after this
11978 function has constructed it. However, beware of the subroutine
11979 potentially being destroyed before this function returns, as described
11982 If C<name> is null then the subroutine will be anonymous, with its
11983 C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the
11984 subroutine will be named accordingly, referenced by the appropriate glob.
11985 C<name> is a string of length C<len> bytes giving a sigilless symbol name,
11986 in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1 otherwise.
11987 The name may be either qualified or unqualified, with the stash defaulting
11988 in the same manner as for C<gv_fetchpvn_flags>. C<flags> may contain
11989 flag bits understood by C<gv_fetchpvn_flags> with the same meaning as
11990 they have there, such as C<GV_ADDWARN>. The symbol is always added to
11991 the stash if necessary, with C<GV_ADDMULTI> semantics.
11993 If there is already a subroutine of the specified name, then the new sub
11994 will replace the existing one in the glob. A warning may be generated
11995 about the redefinition. If the old subroutine was C<CvCONST> then the
11996 decision about whether to warn is influenced by an expectation about
11997 whether the new subroutine will become a constant of similar value.
11998 That expectation is determined by C<const_svp>. (Note that the call to
11999 this function doesn't make the new subroutine C<CvCONST> in any case;
12000 that is left to the caller.) If C<const_svp> is null then it indicates
12001 that the new subroutine will not become a constant. If C<const_svp>
12002 is non-null then it indicates that the new subroutine will become a
12003 constant, and it points to an C<SV*> that provides the constant value
12004 that the subroutine will have.
12006 If the subroutine has one of a few special names, such as C<BEGIN> or
12007 C<END>, then it will be claimed by the appropriate queue for automatic
12008 running of phase-related subroutines. In this case the relevant glob will
12009 be left not containing any subroutine, even if it did contain one before.
12010 In the case of C<BEGIN>, the subroutine will be executed and the reference
12011 to it disposed of before this function returns, and also before its
12012 prototype is set. If a C<BEGIN> subroutine would not be sufficiently
12013 constructed by this function to be ready for execution then the caller
12014 must prevent this happening by giving the subroutine a different name.
12016 The function returns a pointer to the constructed subroutine. If the sub
12017 is anonymous then ownership of one counted reference to the subroutine
12018 is transferred to the caller. If the sub is named then the caller does
12019 not get ownership of a reference. In most such cases, where the sub
12020 has a non-phase name, the sub will be alive at the point it is returned
12021 by virtue of being contained in the glob that names it. A phase-named
12022 subroutine will usually be alive by virtue of the reference owned by the
12023 phase's automatic run queue. But a C<BEGIN> subroutine, having already
12024 been executed, will quite likely have been destroyed already by the
12025 time this function returns, making it erroneous for the caller to make
12026 any use of the returned pointer. It is the caller's responsibility to
12027 ensure that it knows which of these situations applies.
12033 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
12034 XSUBADDR_t subaddr, const char *const filename,
12035 const char *const proto, SV **const_svp,
12039 bool interleave = FALSE;
12040 bool evanescent = FALSE;
12042 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
12045 GV * const gv = gv_fetchpvn(
12046 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
12047 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
12048 sizeof("__ANON__::__ANON__") - 1,
12049 GV_ADDMULTI | flags, SVt_PVCV);
12051 if ((cv = (name ? GvCV(gv) : NULL))) {
12053 /* just a cached method */
12057 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
12058 /* already defined (or promised) */
12059 /* Redundant check that allows us to avoid creating an SV
12060 most of the time: */
12061 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
12062 report_redefined_cv(newSVpvn_flags(
12063 name,len,(flags&SVf_UTF8)|SVs_TEMP
12074 if (cv) /* must reuse cv if autoloaded */
12077 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
12081 if (HvENAME_HEK(GvSTASH(gv)))
12082 gv_method_changed(gv); /* newXS */
12086 assert(SvREFCNT((SV*)cv) != 0);
12090 /* XSUBs can't be perl lang/perl5db.pl debugged
12091 if (PERLDB_LINE_OR_SAVESRC)
12092 (void)gv_fetchfile(filename); */
12093 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
12094 if (flags & XS_DYNAMIC_FILENAME) {
12096 CvFILE(cv) = savepv(filename);
12098 /* NOTE: not copied, as it is expected to be an external constant string */
12099 CvFILE(cv) = (char *)filename;
12102 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename);
12103 CvFILE(cv) = (char*)PL_xsubfilename;
12106 CvXSUB(cv) = subaddr;
12107 #ifndef PERL_IMPLICIT_CONTEXT
12108 CvHSCXT(cv) = &PL_stack_sp;
12114 evanescent = process_special_blocks(0, name, gv, cv);
12117 } /* <- not a conditional branch */
12120 assert(evanescent || SvREFCNT((SV*)cv) != 0);
12122 if (!evanescent) sv_setpv(MUTABLE_SV(cv), proto);
12123 if (interleave) LEAVE;
12124 assert(evanescent || SvREFCNT((SV*)cv) != 0);
12128 /* Add a stub CV to a typeglob.
12129 * This is the implementation of a forward declaration, 'sub foo';'
12133 Perl_newSTUB(pTHX_ GV *gv, bool fake)
12135 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
12137 PERL_ARGS_ASSERT_NEWSTUB;
12138 assert(!GvCVu(gv));
12141 if (!fake && GvSTASH(gv) && HvENAME_HEK(GvSTASH(gv)))
12142 gv_method_changed(gv);
12144 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
12148 CvGV_set(cv, cvgv);
12149 CvFILE_set_from_cop(cv, PL_curcop);
12150 CvSTASH_set(cv, PL_curstash);
12156 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
12163 if (PL_parser && PL_parser->error_count) {
12169 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
12170 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
12173 if ((cv = GvFORM(gv))) {
12174 if (ckWARN(WARN_REDEFINE)) {
12175 const line_t oldline = CopLINE(PL_curcop);
12176 if (PL_parser && PL_parser->copline != NOLINE)
12177 CopLINE_set(PL_curcop, PL_parser->copline);
12179 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
12180 "Format %" SVf " redefined", SVfARG(cSVOPo->op_sv));
12182 /* diag_listed_as: Format %s redefined */
12183 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
12184 "Format STDOUT redefined");
12186 CopLINE_set(PL_curcop, oldline);
12191 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
12193 CvFILE_set_from_cop(cv, PL_curcop);
12196 root = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
12198 start = LINKLIST(root);
12200 S_process_optree(aTHX_ cv, root, start);
12201 cv_forget_slab(cv);
12206 PL_parser->copline = NOLINE;
12207 LEAVE_SCOPE(floor);
12208 PL_compiling.cop_seq = 0;
12212 Perl_newANONLIST(pTHX_ OP *o)
12214 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o);
12218 Perl_newANONHASH(pTHX_ OP *o)
12220 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o);
12224 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
12226 return newANONATTRSUB(floor, proto, NULL, block);
12230 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
12232 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block));
12234 newSVOP(OP_ANONCODE, 0,
12236 if (CvANONCONST(cv))
12237 anoncode = newUNOP(OP_ANONCONST, 0,
12238 op_convert_list(OP_ENTERSUB,
12239 OPf_STACKED|OPf_WANT_SCALAR,
12241 return newUNOP(OP_REFGEN, 0, anoncode);
12245 Perl_oopsAV(pTHX_ OP *o)
12249 PERL_ARGS_ASSERT_OOPSAV;
12251 switch (o->op_type) {
12254 OpTYPE_set(o, OP_PADAV);
12255 return ref(o, OP_RV2AV);
12259 OpTYPE_set(o, OP_RV2AV);
12264 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
12271 Perl_oopsHV(pTHX_ OP *o)
12275 PERL_ARGS_ASSERT_OOPSHV;
12277 switch (o->op_type) {
12280 OpTYPE_set(o, OP_PADHV);
12281 return ref(o, OP_RV2HV);
12285 OpTYPE_set(o, OP_RV2HV);
12286 /* rv2hv steals the bottom bit for its own uses */
12287 o->op_private &= ~OPpARG1_MASK;
12292 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
12299 Perl_newAVREF(pTHX_ OP *o)
12303 PERL_ARGS_ASSERT_NEWAVREF;
12305 if (o->op_type == OP_PADANY) {
12306 OpTYPE_set(o, OP_PADAV);
12309 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
12310 Perl_croak(aTHX_ "Can't use an array as a reference");
12312 return newUNOP(OP_RV2AV, 0, scalar(o));
12316 Perl_newGVREF(pTHX_ I32 type, OP *o)
12318 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
12319 return newUNOP(OP_NULL, 0, o);
12320 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
12324 Perl_newHVREF(pTHX_ OP *o)
12328 PERL_ARGS_ASSERT_NEWHVREF;
12330 if (o->op_type == OP_PADANY) {
12331 OpTYPE_set(o, OP_PADHV);
12334 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
12335 Perl_croak(aTHX_ "Can't use a hash as a reference");
12337 return newUNOP(OP_RV2HV, 0, scalar(o));
12341 Perl_newCVREF(pTHX_ I32 flags, OP *o)
12343 if (o->op_type == OP_PADANY) {
12345 OpTYPE_set(o, OP_PADCV);
12347 return newUNOP(OP_RV2CV, flags, scalar(o));
12351 Perl_newSVREF(pTHX_ OP *o)
12355 PERL_ARGS_ASSERT_NEWSVREF;
12357 if (o->op_type == OP_PADANY) {
12358 OpTYPE_set(o, OP_PADSV);
12362 return newUNOP(OP_RV2SV, 0, scalar(o));
12365 /* Check routines. See the comments at the top of this file for details
12366 * on when these are called */
12369 Perl_ck_anoncode(pTHX_ OP *o)
12371 PERL_ARGS_ASSERT_CK_ANONCODE;
12373 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
12374 cSVOPo->op_sv = NULL;
12379 S_io_hints(pTHX_ OP *o)
12381 #if O_BINARY != 0 || O_TEXT != 0
12383 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
12385 SV **svp = hv_fetchs(table, "open_IN", FALSE);
12388 const char *d = SvPV_const(*svp, len);
12389 const I32 mode = mode_from_discipline(d, len);
12390 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
12392 if (mode & O_BINARY)
12393 o->op_private |= OPpOPEN_IN_RAW;
12397 o->op_private |= OPpOPEN_IN_CRLF;
12401 svp = hv_fetchs(table, "open_OUT", FALSE);
12404 const char *d = SvPV_const(*svp, len);
12405 const I32 mode = mode_from_discipline(d, len);
12406 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
12408 if (mode & O_BINARY)
12409 o->op_private |= OPpOPEN_OUT_RAW;
12413 o->op_private |= OPpOPEN_OUT_CRLF;
12418 PERL_UNUSED_CONTEXT;
12419 PERL_UNUSED_ARG(o);
12424 Perl_ck_backtick(pTHX_ OP *o)
12429 PERL_ARGS_ASSERT_CK_BACKTICK;
12431 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
12432 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first))
12433 && (gv = gv_override("readpipe",8)))
12435 /* detach rest of siblings from o and its first child */
12436 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
12437 newop = S_new_entersubop(aTHX_ gv, sibl);
12439 else if (!(o->op_flags & OPf_KIDS))
12440 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
12445 S_io_hints(aTHX_ o);
12450 Perl_ck_bitop(pTHX_ OP *o)
12452 PERL_ARGS_ASSERT_CK_BITOP;
12454 o->op_private = (U8)(PL_hints & HINT_INTEGER);
12456 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
12457 && OP_IS_INFIX_BIT(o->op_type))
12459 const OP * const left = cBINOPo->op_first;
12460 const OP * const right = OpSIBLING(left);
12461 if ((OP_IS_NUMCOMPARE(left->op_type) &&
12462 (left->op_flags & OPf_PARENS) == 0) ||
12463 (OP_IS_NUMCOMPARE(right->op_type) &&
12464 (right->op_flags & OPf_PARENS) == 0))
12465 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
12466 "Possible precedence problem on bitwise %s operator",
12467 o->op_type == OP_BIT_OR
12468 ||o->op_type == OP_NBIT_OR ? "|"
12469 : o->op_type == OP_BIT_AND
12470 ||o->op_type == OP_NBIT_AND ? "&"
12471 : o->op_type == OP_BIT_XOR
12472 ||o->op_type == OP_NBIT_XOR ? "^"
12473 : o->op_type == OP_SBIT_OR ? "|."
12474 : o->op_type == OP_SBIT_AND ? "&." : "^."
12480 PERL_STATIC_INLINE bool
12481 is_dollar_bracket(pTHX_ const OP * const o)
12484 PERL_UNUSED_CONTEXT;
12485 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
12486 && (kid = cUNOPx(o)->op_first)
12487 && kid->op_type == OP_GV
12488 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
12491 /* for lt, gt, le, ge, eq, ne and their i_ variants */
12494 Perl_ck_cmp(pTHX_ OP *o)
12500 OP *indexop, *constop, *start;
12504 PERL_ARGS_ASSERT_CK_CMP;
12506 is_eq = ( o->op_type == OP_EQ
12507 || o->op_type == OP_NE
12508 || o->op_type == OP_I_EQ
12509 || o->op_type == OP_I_NE);
12511 if (!is_eq && ckWARN(WARN_SYNTAX)) {
12512 const OP *kid = cUNOPo->op_first;
12515 ( is_dollar_bracket(aTHX_ kid)
12516 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST
12518 || ( kid->op_type == OP_CONST
12519 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid)
12523 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12524 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
12527 /* convert (index(...) == -1) and variations into
12528 * (r)index/BOOL(,NEG)
12533 indexop = cUNOPo->op_first;
12534 constop = OpSIBLING(indexop);
12536 if (indexop->op_type == OP_CONST) {
12538 indexop = OpSIBLING(constop);
12543 if (indexop->op_type != OP_INDEX && indexop->op_type != OP_RINDEX)
12546 /* ($lex = index(....)) == -1 */
12547 if (indexop->op_private & OPpTARGET_MY)
12550 if (constop->op_type != OP_CONST)
12553 sv = cSVOPx_sv(constop);
12554 if (!(sv && SvIOK_notUV(sv)))
12558 if (iv != -1 && iv != 0)
12562 if (o->op_type == OP_LT || o->op_type == OP_I_LT) {
12563 if (!(iv0 ^ reverse))
12567 else if (o->op_type == OP_LE || o->op_type == OP_I_LE) {
12572 else if (o->op_type == OP_GE || o->op_type == OP_I_GE) {
12573 if (!(iv0 ^ reverse))
12577 else if (o->op_type == OP_GT || o->op_type == OP_I_GT) {
12582 else if (o->op_type == OP_EQ || o->op_type == OP_I_EQ) {
12588 assert(o->op_type == OP_NE || o->op_type == OP_I_NE);
12594 indexop->op_flags &= ~OPf_PARENS;
12595 indexop->op_flags |= (o->op_flags & OPf_PARENS);
12596 indexop->op_private |= OPpTRUEBOOL;
12598 indexop->op_private |= OPpINDEX_BOOLNEG;
12599 /* cut out the index op and free the eq,const ops */
12600 (void)op_sibling_splice(o, start, 1, NULL);
12608 Perl_ck_concat(pTHX_ OP *o)
12610 const OP * const kid = cUNOPo->op_first;
12612 PERL_ARGS_ASSERT_CK_CONCAT;
12613 PERL_UNUSED_CONTEXT;
12615 /* reuse the padtmp returned by the concat child */
12616 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
12617 !(kUNOP->op_first->op_flags & OPf_MOD))
12619 o->op_flags |= OPf_STACKED;
12620 o->op_private |= OPpCONCAT_NESTED;
12626 Perl_ck_spair(pTHX_ OP *o)
12630 PERL_ARGS_ASSERT_CK_SPAIR;
12632 if (o->op_flags & OPf_KIDS) {
12636 const OPCODE type = o->op_type;
12637 o = modkids(ck_fun(o), type);
12638 kid = cUNOPo->op_first;
12639 kidkid = kUNOP->op_first;
12640 newop = OpSIBLING(kidkid);
12642 const OPCODE type = newop->op_type;
12643 if (OpHAS_SIBLING(newop))
12645 if (o->op_type == OP_REFGEN
12646 && ( type == OP_RV2CV
12647 || ( !(newop->op_flags & OPf_PARENS)
12648 && ( type == OP_RV2AV || type == OP_PADAV
12649 || type == OP_RV2HV || type == OP_PADHV))))
12650 NOOP; /* OK (allow srefgen for \@a and \%h) */
12651 else if (OP_GIMME(newop,0) != G_SCALAR)
12654 /* excise first sibling */
12655 op_sibling_splice(kid, NULL, 1, NULL);
12658 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
12659 * and OP_CHOMP into OP_SCHOMP */
12660 o->op_ppaddr = PL_ppaddr[++o->op_type];
12665 Perl_ck_delete(pTHX_ OP *o)
12667 PERL_ARGS_ASSERT_CK_DELETE;
12671 if (o->op_flags & OPf_KIDS) {
12672 OP * const kid = cUNOPo->op_first;
12673 switch (kid->op_type) {
12675 o->op_flags |= OPf_SPECIAL;
12678 o->op_private |= OPpSLICE;
12681 o->op_flags |= OPf_SPECIAL;
12686 o->op_flags |= OPf_SPECIAL;
12689 o->op_private |= OPpKVSLICE;
12692 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
12693 "element or slice");
12695 if (kid->op_private & OPpLVAL_INTRO)
12696 o->op_private |= OPpLVAL_INTRO;
12703 Perl_ck_eof(pTHX_ OP *o)
12705 PERL_ARGS_ASSERT_CK_EOF;
12707 if (o->op_flags & OPf_KIDS) {
12709 if (cLISTOPo->op_first->op_type == OP_STUB) {
12711 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
12716 kid = cLISTOPo->op_first;
12717 if (kid->op_type == OP_RV2GV)
12718 kid->op_private |= OPpALLOW_FAKE;
12725 Perl_ck_eval(pTHX_ OP *o)
12729 PERL_ARGS_ASSERT_CK_EVAL;
12731 PL_hints |= HINT_BLOCK_SCOPE;
12732 if (o->op_flags & OPf_KIDS) {
12733 SVOP * const kid = (SVOP*)cUNOPo->op_first;
12736 if (o->op_type == OP_ENTERTRY) {
12739 /* cut whole sibling chain free from o */
12740 op_sibling_splice(o, NULL, -1, NULL);
12743 enter = alloc_LOGOP(OP_ENTERTRY, NULL, NULL);
12745 /* establish postfix order */
12746 enter->op_next = (OP*)enter;
12748 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
12749 OpTYPE_set(o, OP_LEAVETRY);
12750 enter->op_other = o;
12755 S_set_haseval(aTHX);
12759 const U8 priv = o->op_private;
12761 /* the newUNOP will recursively call ck_eval(), which will handle
12762 * all the stuff at the end of this function, like adding
12765 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
12767 o->op_targ = (PADOFFSET)PL_hints;
12768 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
12769 if ((PL_hints & HINT_LOCALIZE_HH) != 0
12770 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
12771 /* Store a copy of %^H that pp_entereval can pick up. */
12772 HV *hh = hv_copy_hints_hv(GvHV(PL_hintgv));
12774 STOREFEATUREBITSHH(hh);
12775 hhop = newSVOP(OP_HINTSEVAL, 0, MUTABLE_SV(hh));
12776 /* append hhop to only child */
12777 op_sibling_splice(o, cUNOPo->op_first, 0, hhop);
12779 o->op_private |= OPpEVAL_HAS_HH;
12781 if (!(o->op_private & OPpEVAL_BYTES)
12782 && FEATURE_UNIEVAL_IS_ENABLED)
12783 o->op_private |= OPpEVAL_UNICODE;
12788 Perl_ck_exec(pTHX_ OP *o)
12790 PERL_ARGS_ASSERT_CK_EXEC;
12792 if (o->op_flags & OPf_STACKED) {
12795 kid = OpSIBLING(cUNOPo->op_first);
12796 if (kid->op_type == OP_RV2GV)
12805 Perl_ck_exists(pTHX_ OP *o)
12807 PERL_ARGS_ASSERT_CK_EXISTS;
12810 if (o->op_flags & OPf_KIDS) {
12811 OP * const kid = cUNOPo->op_first;
12812 if (kid->op_type == OP_ENTERSUB) {
12813 (void) ref(kid, o->op_type);
12814 if (kid->op_type != OP_RV2CV
12815 && !(PL_parser && PL_parser->error_count))
12817 "exists argument is not a subroutine name");
12818 o->op_private |= OPpEXISTS_SUB;
12820 else if (kid->op_type == OP_AELEM)
12821 o->op_flags |= OPf_SPECIAL;
12822 else if (kid->op_type != OP_HELEM)
12823 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
12824 "element or a subroutine");
12831 Perl_ck_rvconst(pTHX_ OP *o)
12834 SVOP * const kid = (SVOP*)cUNOPo->op_first;
12836 PERL_ARGS_ASSERT_CK_RVCONST;
12838 if (o->op_type == OP_RV2HV)
12839 /* rv2hv steals the bottom bit for its own uses */
12840 o->op_private &= ~OPpARG1_MASK;
12842 o->op_private |= (PL_hints & HINT_STRICT_REFS);
12844 if (kid->op_type == OP_CONST) {
12847 SV * const kidsv = kid->op_sv;
12849 /* Is it a constant from cv_const_sv()? */
12850 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) {
12853 if (SvTYPE(kidsv) == SVt_PVAV) return o;
12854 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
12855 const char *badthing;
12856 switch (o->op_type) {
12858 badthing = "a SCALAR";
12861 badthing = "an ARRAY";
12864 badthing = "a HASH";
12872 "Can't use bareword (\"%" SVf "\") as %s ref while \"strict refs\" in use",
12873 SVfARG(kidsv), badthing);
12876 * This is a little tricky. We only want to add the symbol if we
12877 * didn't add it in the lexer. Otherwise we get duplicate strict
12878 * warnings. But if we didn't add it in the lexer, we must at
12879 * least pretend like we wanted to add it even if it existed before,
12880 * or we get possible typo warnings. OPpCONST_ENTERED says
12881 * whether the lexer already added THIS instance of this symbol.
12883 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0;
12884 gv = gv_fetchsv(kidsv,
12885 o->op_type == OP_RV2CV
12886 && o->op_private & OPpMAY_RETURN_CONSTANT
12888 : iscv | !(kid->op_private & OPpCONST_ENTERED),
12891 : o->op_type == OP_RV2SV
12893 : o->op_type == OP_RV2AV
12895 : o->op_type == OP_RV2HV
12902 if (!(o->op_private & OPpMAY_RETURN_CONSTANT)
12903 && SvTYPE(SvRV(gv)) != SVt_PVCV)
12904 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV);
12906 OpTYPE_set(kid, OP_GV);
12907 SvREFCNT_dec(kid->op_sv);
12908 #ifdef USE_ITHREADS
12909 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
12910 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP));
12911 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY);
12912 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
12913 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
12915 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
12917 kid->op_private = 0;
12918 /* FAKE globs in the symbol table cause weird bugs (#77810) */
12926 Perl_ck_ftst(pTHX_ OP *o)
12929 const I32 type = o->op_type;
12931 PERL_ARGS_ASSERT_CK_FTST;
12933 if (o->op_flags & OPf_REF) {
12936 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
12937 SVOP * const kid = (SVOP*)cUNOPo->op_first;
12938 const OPCODE kidtype = kid->op_type;
12940 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
12941 && !kid->op_folded) {
12942 OP * const newop = newGVOP(type, OPf_REF,
12943 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
12948 if ((kidtype == OP_RV2AV || kidtype == OP_PADAV) && ckWARN(WARN_SYNTAX)) {
12949 SV *name = S_op_varname_subscript(aTHX_ (OP*)kid, 2);
12951 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
12952 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s (did you want stat %" SVf "?)",
12953 array_passed_to_stat, name);
12956 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
12957 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s", array_passed_to_stat);
12960 scalar((OP *) kid);
12961 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
12962 o->op_private |= OPpFT_ACCESS;
12963 if (OP_IS_FILETEST(type)
12964 && OP_IS_FILETEST(kidtype)
12966 o->op_private |= OPpFT_STACKED;
12967 kid->op_private |= OPpFT_STACKING;
12968 if (kidtype == OP_FTTTY && (
12969 !(kid->op_private & OPpFT_STACKED)
12970 || kid->op_private & OPpFT_AFTER_t
12972 o->op_private |= OPpFT_AFTER_t;
12977 if (type == OP_FTTTY)
12978 o = newGVOP(type, OPf_REF, PL_stdingv);
12980 o = newUNOP(type, 0, newDEFSVOP());
12986 Perl_ck_fun(pTHX_ OP *o)
12988 const int type = o->op_type;
12989 I32 oa = PL_opargs[type] >> OASHIFT;
12991 PERL_ARGS_ASSERT_CK_FUN;
12993 if (o->op_flags & OPf_STACKED) {
12994 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
12995 oa &= ~OA_OPTIONAL;
12997 return no_fh_allowed(o);
13000 if (o->op_flags & OPf_KIDS) {
13001 OP *prev_kid = NULL;
13002 OP *kid = cLISTOPo->op_first;
13004 bool seen_optional = FALSE;
13006 if (kid->op_type == OP_PUSHMARK ||
13007 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
13010 kid = OpSIBLING(kid);
13012 if (kid && kid->op_type == OP_COREARGS) {
13013 bool optional = FALSE;
13016 if (oa & OA_OPTIONAL) optional = TRUE;
13019 if (optional) o->op_private |= numargs;
13024 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
13025 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) {
13026 kid = newDEFSVOP();
13027 /* append kid to chain */
13028 op_sibling_splice(o, prev_kid, 0, kid);
13030 seen_optional = TRUE;
13037 /* list seen where single (scalar) arg expected? */
13038 if (numargs == 1 && !(oa >> 4)
13039 && kid->op_type == OP_LIST && type != OP_SCALAR)
13041 return too_many_arguments_pv(o,PL_op_desc[type], 0);
13043 if (type != OP_DELETE) scalar(kid);
13054 if ((type == OP_PUSH || type == OP_UNSHIFT)
13055 && !OpHAS_SIBLING(kid))
13056 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13057 "Useless use of %s with no values",
13060 if (kid->op_type == OP_CONST
13061 && ( !SvROK(cSVOPx_sv(kid))
13062 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
13064 bad_type_pv(numargs, "array", o, kid);
13065 else if (kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV
13066 || kid->op_type == OP_RV2GV) {
13067 bad_type_pv(1, "array", o, kid);
13069 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) {
13070 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden",
13071 PL_op_desc[type]), 0);
13074 op_lvalue(kid, type);
13078 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
13079 bad_type_pv(numargs, "hash", o, kid);
13080 op_lvalue(kid, type);
13084 /* replace kid with newop in chain */
13086 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0);
13087 newop->op_next = newop;
13092 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
13093 if (kid->op_type == OP_CONST &&
13094 (kid->op_private & OPpCONST_BARE))
13096 OP * const newop = newGVOP(OP_GV, 0,
13097 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
13098 /* replace kid with newop in chain */
13099 op_sibling_splice(o, prev_kid, 1, newop);
13103 else if (kid->op_type == OP_READLINE) {
13104 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
13105 bad_type_pv(numargs, "HANDLE", o, kid);
13108 I32 flags = OPf_SPECIAL;
13110 PADOFFSET targ = 0;
13112 /* is this op a FH constructor? */
13113 if (is_handle_constructor(o,numargs)) {
13114 const char *name = NULL;
13117 bool want_dollar = TRUE;
13120 /* Set a flag to tell rv2gv to vivify
13121 * need to "prove" flag does not mean something
13122 * else already - NI-S 1999/05/07
13125 if (kid->op_type == OP_PADSV) {
13127 = PAD_COMPNAME_SV(kid->op_targ);
13128 name = PadnamePV (pn);
13129 len = PadnameLEN(pn);
13130 name_utf8 = PadnameUTF8(pn);
13132 else if (kid->op_type == OP_RV2SV
13133 && kUNOP->op_first->op_type == OP_GV)
13135 GV * const gv = cGVOPx_gv(kUNOP->op_first);
13137 len = GvNAMELEN(gv);
13138 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
13140 else if (kid->op_type == OP_AELEM
13141 || kid->op_type == OP_HELEM)
13144 OP *op = ((BINOP*)kid)->op_first;
13148 const char * const a =
13149 kid->op_type == OP_AELEM ?
13151 if (((op->op_type == OP_RV2AV) ||
13152 (op->op_type == OP_RV2HV)) &&
13153 (firstop = ((UNOP*)op)->op_first) &&
13154 (firstop->op_type == OP_GV)) {
13155 /* packagevar $a[] or $h{} */
13156 GV * const gv = cGVOPx_gv(firstop);
13159 Perl_newSVpvf(aTHX_
13164 else if (op->op_type == OP_PADAV
13165 || op->op_type == OP_PADHV) {
13166 /* lexicalvar $a[] or $h{} */
13167 const char * const padname =
13168 PAD_COMPNAME_PV(op->op_targ);
13171 Perl_newSVpvf(aTHX_
13177 name = SvPV_const(tmpstr, len);
13178 name_utf8 = SvUTF8(tmpstr);
13179 sv_2mortal(tmpstr);
13183 name = "__ANONIO__";
13185 want_dollar = FALSE;
13187 op_lvalue(kid, type);
13191 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
13192 namesv = PAD_SVl(targ);
13193 if (want_dollar && *name != '$')
13194 sv_setpvs(namesv, "$");
13197 sv_catpvn(namesv, name, len);
13198 if ( name_utf8 ) SvUTF8_on(namesv);
13202 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid,
13204 kid->op_targ = targ;
13205 kid->op_private |= priv;
13211 if ((type == OP_UNDEF || type == OP_POS)
13212 && numargs == 1 && !(oa >> 4)
13213 && kid->op_type == OP_LIST)
13214 return too_many_arguments_pv(o,PL_op_desc[type], 0);
13215 op_lvalue(scalar(kid), type);
13220 kid = OpSIBLING(kid);
13222 /* FIXME - should the numargs or-ing move after the too many
13223 * arguments check? */
13224 o->op_private |= numargs;
13226 return too_many_arguments_pv(o,OP_DESC(o), 0);
13229 else if (PL_opargs[type] & OA_DEFGV) {
13230 /* Ordering of these two is important to keep f_map.t passing. */
13232 return newUNOP(type, 0, newDEFSVOP());
13236 while (oa & OA_OPTIONAL)
13238 if (oa && oa != OA_LIST)
13239 return too_few_arguments_pv(o,OP_DESC(o), 0);
13245 Perl_ck_glob(pTHX_ OP *o)
13249 PERL_ARGS_ASSERT_CK_GLOB;
13252 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first))
13253 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
13255 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
13259 * \ null - const(wildcard)
13264 * \ mark - glob - rv2cv
13265 * | \ gv(CORE::GLOBAL::glob)
13267 * \ null - const(wildcard)
13269 o->op_flags |= OPf_SPECIAL;
13270 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
13271 o = S_new_entersubop(aTHX_ gv, o);
13272 o = newUNOP(OP_NULL, 0, o);
13273 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
13276 else o->op_flags &= ~OPf_SPECIAL;
13277 #if !defined(PERL_EXTERNAL_GLOB)
13278 if (!PL_globhook) {
13280 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
13281 newSVpvs("File::Glob"), NULL, NULL, NULL);
13284 #endif /* !PERL_EXTERNAL_GLOB */
13285 gv = (GV *)newSV(0);
13286 gv_init(gv, 0, "", 0, 0);
13288 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
13289 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
13295 Perl_ck_grep(pTHX_ OP *o)
13299 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
13301 PERL_ARGS_ASSERT_CK_GREP;
13303 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
13305 if (o->op_flags & OPf_STACKED) {
13306 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first;
13307 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
13308 return no_fh_allowed(o);
13309 o->op_flags &= ~OPf_STACKED;
13311 kid = OpSIBLING(cLISTOPo->op_first);
13312 if (type == OP_MAPWHILE)
13317 if (PL_parser && PL_parser->error_count)
13319 kid = OpSIBLING(cLISTOPo->op_first);
13320 if (kid->op_type != OP_NULL)
13321 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
13322 kid = kUNOP->op_first;
13324 gwop = alloc_LOGOP(type, o, LINKLIST(kid));
13325 kid->op_next = (OP*)gwop;
13326 o->op_private = gwop->op_private = 0;
13327 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
13329 kid = OpSIBLING(cLISTOPo->op_first);
13330 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid))
13331 op_lvalue(kid, OP_GREPSTART);
13337 Perl_ck_index(pTHX_ OP *o)
13339 PERL_ARGS_ASSERT_CK_INDEX;
13341 if (o->op_flags & OPf_KIDS) {
13342 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13344 kid = OpSIBLING(kid); /* get past "big" */
13345 if (kid && kid->op_type == OP_CONST) {
13346 const bool save_taint = TAINT_get;
13347 SV *sv = kSVOP->op_sv;
13348 if ( (!SvPOK(sv) || SvNIOKp(sv) || isREGEXP(sv))
13349 && SvOK(sv) && !SvROK(sv))
13352 sv_copypv(sv, kSVOP->op_sv);
13353 SvREFCNT_dec_NN(kSVOP->op_sv);
13356 if (SvOK(sv)) fbm_compile(sv, 0);
13357 TAINT_set(save_taint);
13358 #ifdef NO_TAINT_SUPPORT
13359 PERL_UNUSED_VAR(save_taint);
13367 Perl_ck_lfun(pTHX_ OP *o)
13369 const OPCODE type = o->op_type;
13371 PERL_ARGS_ASSERT_CK_LFUN;
13373 return modkids(ck_fun(o), type);
13377 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
13379 PERL_ARGS_ASSERT_CK_DEFINED;
13381 if ((o->op_flags & OPf_KIDS)) {
13382 switch (cUNOPo->op_first->op_type) {
13385 Perl_croak(aTHX_ "Can't use 'defined(@array)'"
13386 " (Maybe you should just omit the defined()?)");
13387 NOT_REACHED; /* NOTREACHED */
13391 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'"
13392 " (Maybe you should just omit the defined()?)");
13393 NOT_REACHED; /* NOTREACHED */
13404 Perl_ck_readline(pTHX_ OP *o)
13406 PERL_ARGS_ASSERT_CK_READLINE;
13408 if (o->op_flags & OPf_KIDS) {
13409 OP *kid = cLISTOPo->op_first;
13410 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
13415 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
13423 Perl_ck_rfun(pTHX_ OP *o)
13425 const OPCODE type = o->op_type;
13427 PERL_ARGS_ASSERT_CK_RFUN;
13429 return refkids(ck_fun(o), type);
13433 Perl_ck_listiob(pTHX_ OP *o)
13437 PERL_ARGS_ASSERT_CK_LISTIOB;
13439 kid = cLISTOPo->op_first;
13441 o = force_list(o, 1);
13442 kid = cLISTOPo->op_first;
13444 if (kid->op_type == OP_PUSHMARK)
13445 kid = OpSIBLING(kid);
13446 if (kid && o->op_flags & OPf_STACKED)
13447 kid = OpSIBLING(kid);
13448 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */
13449 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
13450 && !kid->op_folded) {
13451 o->op_flags |= OPf_STACKED; /* make it a filehandle */
13453 /* replace old const op with new OP_RV2GV parent */
13454 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first,
13455 OP_RV2GV, OPf_REF);
13456 kid = OpSIBLING(kid);
13461 op_append_elem(o->op_type, o, newDEFSVOP());
13463 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
13464 return listkids(o);
13468 Perl_ck_smartmatch(pTHX_ OP *o)
13471 PERL_ARGS_ASSERT_CK_SMARTMATCH;
13472 if (0 == (o->op_flags & OPf_SPECIAL)) {
13473 OP *first = cBINOPo->op_first;
13474 OP *second = OpSIBLING(first);
13476 /* Implicitly take a reference to an array or hash */
13478 /* remove the original two siblings, then add back the
13479 * (possibly different) first and second sibs.
13481 op_sibling_splice(o, NULL, 1, NULL);
13482 op_sibling_splice(o, NULL, 1, NULL);
13483 first = ref_array_or_hash(first);
13484 second = ref_array_or_hash(second);
13485 op_sibling_splice(o, NULL, 0, second);
13486 op_sibling_splice(o, NULL, 0, first);
13488 /* Implicitly take a reference to a regular expression */
13489 if (first->op_type == OP_MATCH && !(first->op_flags & OPf_STACKED)) {
13490 OpTYPE_set(first, OP_QR);
13492 if (second->op_type == OP_MATCH && !(second->op_flags & OPf_STACKED)) {
13493 OpTYPE_set(second, OP_QR);
13502 S_maybe_targlex(pTHX_ OP *o)
13504 OP * const kid = cLISTOPo->op_first;
13505 /* has a disposable target? */
13506 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
13507 && !(kid->op_flags & OPf_STACKED)
13508 /* Cannot steal the second time! */
13509 && !(kid->op_private & OPpTARGET_MY)
13512 OP * const kkid = OpSIBLING(kid);
13514 /* Can just relocate the target. */
13515 if (kkid && kkid->op_type == OP_PADSV
13516 && (!(kkid->op_private & OPpLVAL_INTRO)
13517 || kkid->op_private & OPpPAD_STATE))
13519 kid->op_targ = kkid->op_targ;
13521 /* Now we do not need PADSV and SASSIGN.
13522 * Detach kid and free the rest. */
13523 op_sibling_splice(o, NULL, 1, NULL);
13525 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
13533 Perl_ck_sassign(pTHX_ OP *o)
13536 OP * const kid = cBINOPo->op_first;
13538 PERL_ARGS_ASSERT_CK_SASSIGN;
13540 if (OpHAS_SIBLING(kid)) {
13541 OP *kkid = OpSIBLING(kid);
13542 /* For state variable assignment with attributes, kkid is a list op
13543 whose op_last is a padsv. */
13544 if ((kkid->op_type == OP_PADSV ||
13545 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
13546 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
13549 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
13550 == (OPpLVAL_INTRO|OPpPAD_STATE)) {
13551 return S_newONCEOP(aTHX_ o, kkid);
13554 return S_maybe_targlex(aTHX_ o);
13559 Perl_ck_match(pTHX_ OP *o)
13561 PERL_UNUSED_CONTEXT;
13562 PERL_ARGS_ASSERT_CK_MATCH;
13568 Perl_ck_method(pTHX_ OP *o)
13570 SV *sv, *methsv, *rclass;
13571 const char* method;
13574 STRLEN len, nsplit = 0, i;
13576 OP * const kid = cUNOPo->op_first;
13578 PERL_ARGS_ASSERT_CK_METHOD;
13579 if (kid->op_type != OP_CONST) return o;
13583 /* replace ' with :: */
13584 while ((compatptr = (char *) memchr(SvPVX(sv), '\'',
13585 SvEND(sv) - SvPVX(sv) )))
13588 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1);
13591 method = SvPVX_const(sv);
13593 utf8 = SvUTF8(sv) ? -1 : 1;
13595 for (i = len - 1; i > 0; --i) if (method[i] == ':') {
13600 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0);
13602 if (!nsplit) { /* $proto->method() */
13604 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv);
13607 if (memEQs(method, nsplit, "SUPER::")) { /* $proto->SUPER::method() */
13609 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv);
13612 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */
13613 if (nsplit >= 9 && strBEGINs(method+nsplit-9, "::SUPER::")) {
13614 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0);
13615 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv);
13617 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0);
13618 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv);
13620 #ifdef USE_ITHREADS
13621 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ);
13623 cMETHOPx(new_op)->op_rclass_sv = rclass;
13630 Perl_ck_null(pTHX_ OP *o)
13632 PERL_ARGS_ASSERT_CK_NULL;
13633 PERL_UNUSED_CONTEXT;
13638 Perl_ck_open(pTHX_ OP *o)
13640 PERL_ARGS_ASSERT_CK_OPEN;
13642 S_io_hints(aTHX_ o);
13644 /* In case of three-arg dup open remove strictness
13645 * from the last arg if it is a bareword. */
13646 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
13647 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
13651 if ((last->op_type == OP_CONST) && /* The bareword. */
13652 (last->op_private & OPpCONST_BARE) &&
13653 (last->op_private & OPpCONST_STRICT) &&
13654 (oa = OpSIBLING(first)) && /* The fh. */
13655 (oa = OpSIBLING(oa)) && /* The mode. */
13656 (oa->op_type == OP_CONST) &&
13657 SvPOK(((SVOP*)oa)->op_sv) &&
13658 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
13659 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
13660 (last == OpSIBLING(oa))) /* The bareword. */
13661 last->op_private &= ~OPpCONST_STRICT;
13667 Perl_ck_prototype(pTHX_ OP *o)
13669 PERL_ARGS_ASSERT_CK_PROTOTYPE;
13670 if (!(o->op_flags & OPf_KIDS)) {
13672 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP());
13678 Perl_ck_refassign(pTHX_ OP *o)
13680 OP * const right = cLISTOPo->op_first;
13681 OP * const left = OpSIBLING(right);
13682 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first;
13685 PERL_ARGS_ASSERT_CK_REFASSIGN;
13687 assert (left->op_type == OP_SREFGEN);
13690 /* we use OPpPAD_STATE in refassign to mean either of those things,
13691 * and the code assumes the two flags occupy the same bit position
13692 * in the various ops below */
13693 assert(OPpPAD_STATE == OPpOUR_INTRO);
13695 switch (varop->op_type) {
13697 o->op_private |= OPpLVREF_AV;
13700 o->op_private |= OPpLVREF_HV;
13704 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
13705 o->op_targ = varop->op_targ;
13706 varop->op_targ = 0;
13707 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
13711 o->op_private |= OPpLVREF_AV;
13713 NOT_REACHED; /* NOTREACHED */
13715 o->op_private |= OPpLVREF_HV;
13719 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO));
13720 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad;
13722 /* Point varop to its GV kid, detached. */
13723 varop = op_sibling_splice(varop, NULL, -1, NULL);
13727 OP * const kidparent =
13728 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first);
13729 OP * const kid = cUNOPx(kidparent)->op_first;
13730 o->op_private |= OPpLVREF_CV;
13731 if (kid->op_type == OP_GV) {
13732 SV *sv = (SV*)cGVOPx_gv(kid);
13734 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
13735 /* a CVREF here confuses pp_refassign, so make sure
13737 CV *const cv = (CV*)SvRV(sv);
13738 SV *name_sv = sv_2mortal(newSVhek(CvNAME_HEK(cv)));
13739 (void)gv_init_sv((GV*)sv, CvSTASH(cv), name_sv, 0);
13740 assert(SvTYPE(sv) == SVt_PVGV);
13742 goto detach_and_stack;
13744 if (kid->op_type != OP_PADCV) goto bad;
13745 o->op_targ = kid->op_targ;
13751 o->op_private |= (varop->op_private & OPpLVAL_INTRO);
13752 o->op_private |= OPpLVREF_ELEM;
13755 /* Detach varop. */
13756 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL);
13760 /* diag_listed_as: Can't modify reference to %s in %s assignment */
13761 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar "
13766 if (!FEATURE_REFALIASING_IS_ENABLED)
13768 "Experimental aliasing via reference not enabled");
13769 Perl_ck_warner_d(aTHX_
13770 packWARN(WARN_EXPERIMENTAL__REFALIASING),
13771 "Aliasing via reference is experimental");
13773 o->op_flags |= OPf_STACKED;
13774 op_sibling_splice(o, right, 1, varop);
13777 o->op_flags &=~ OPf_STACKED;
13778 op_sibling_splice(o, right, 1, NULL);
13785 Perl_ck_repeat(pTHX_ OP *o)
13787 PERL_ARGS_ASSERT_CK_REPEAT;
13789 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
13791 o->op_private |= OPpREPEAT_DOLIST;
13792 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */
13793 kids = force_list(kids, 1); /* promote it to a list */
13794 op_sibling_splice(o, NULL, 0, kids); /* and add back */
13802 Perl_ck_require(pTHX_ OP *o)
13806 PERL_ARGS_ASSERT_CK_REQUIRE;
13808 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
13809 SVOP * const kid = (SVOP*)cUNOPo->op_first;
13813 if (kid->op_type == OP_CONST) {
13814 SV * const sv = kid->op_sv;
13815 U32 const was_readonly = SvREADONLY(sv);
13816 if (kid->op_private & OPpCONST_BARE) {
13821 if (was_readonly) {
13822 SvREADONLY_off(sv);
13825 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
13830 /* treat ::foo::bar as foo::bar */
13831 if (len >= 2 && s[0] == ':' && s[1] == ':')
13832 DIE(aTHX_ "Bareword in require must not start with a double-colon: \"%s\"\n", s);
13834 DIE(aTHX_ "Bareword in require maps to empty filename");
13836 for (; s < end; s++) {
13837 if (*s == ':' && s[1] == ':') {
13839 Move(s+2, s+1, end - s - 1, char);
13843 SvEND_set(sv, end);
13844 sv_catpvs(sv, ".pm");
13845 PERL_HASH(hash, SvPVX(sv), SvCUR(sv));
13846 hek = share_hek(SvPVX(sv),
13847 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1),
13849 sv_sethek(sv, hek);
13851 SvFLAGS(sv) |= was_readonly;
13853 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv)
13856 if (SvREFCNT(sv) > 1) {
13857 kid->op_sv = newSVpvn_share(
13858 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0);
13859 SvREFCNT_dec_NN(sv);
13864 if (was_readonly) SvREADONLY_off(sv);
13865 PERL_HASH(hash, s, len);
13867 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len,
13869 sv_sethek(sv, hek);
13871 SvFLAGS(sv) |= was_readonly;
13877 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
13878 /* handle override, if any */
13879 && (gv = gv_override("require", 7))) {
13881 if (o->op_flags & OPf_KIDS) {
13882 kid = cUNOPo->op_first;
13883 op_sibling_splice(o, NULL, -1, NULL);
13886 kid = newDEFSVOP();
13889 newop = S_new_entersubop(aTHX_ gv, kid);
13897 Perl_ck_return(pTHX_ OP *o)
13901 PERL_ARGS_ASSERT_CK_RETURN;
13903 kid = OpSIBLING(cLISTOPo->op_first);
13904 if (PL_compcv && CvLVALUE(PL_compcv)) {
13905 for (; kid; kid = OpSIBLING(kid))
13906 op_lvalue(kid, OP_LEAVESUBLV);
13913 Perl_ck_select(pTHX_ OP *o)
13918 PERL_ARGS_ASSERT_CK_SELECT;
13920 if (o->op_flags & OPf_KIDS) {
13921 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13922 if (kid && OpHAS_SIBLING(kid)) {
13923 OpTYPE_set(o, OP_SSELECT);
13925 return fold_constants(op_integerize(op_std_init(o)));
13929 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13930 if (kid && kid->op_type == OP_RV2GV)
13931 kid->op_private &= ~HINT_STRICT_REFS;
13936 Perl_ck_shift(pTHX_ OP *o)
13938 const I32 type = o->op_type;
13940 PERL_ARGS_ASSERT_CK_SHIFT;
13942 if (!(o->op_flags & OPf_KIDS)) {
13945 if (!CvUNIQUE(PL_compcv)) {
13946 o->op_flags |= OPf_SPECIAL;
13950 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
13952 return newUNOP(type, 0, scalar(argop));
13954 return scalar(ck_fun(o));
13958 Perl_ck_sort(pTHX_ OP *o)
13962 HV * const hinthv =
13963 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
13966 PERL_ARGS_ASSERT_CK_SORT;
13969 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
13971 const I32 sorthints = (I32)SvIV(*svp);
13972 if ((sorthints & HINT_SORT_STABLE) != 0)
13973 o->op_private |= OPpSORT_STABLE;
13974 if ((sorthints & HINT_SORT_UNSTABLE) != 0)
13975 o->op_private |= OPpSORT_UNSTABLE;
13979 if (o->op_flags & OPf_STACKED)
13981 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13983 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
13984 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
13986 /* if the first arg is a code block, process it and mark sort as
13988 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
13990 if (kid->op_type == OP_LEAVE)
13991 op_null(kid); /* wipe out leave */
13992 /* Prevent execution from escaping out of the sort block. */
13995 /* provide scalar context for comparison function/block */
13996 kid = scalar(firstkid);
13997 kid->op_next = kid;
13998 o->op_flags |= OPf_SPECIAL;
14000 else if (kid->op_type == OP_CONST
14001 && kid->op_private & OPpCONST_BARE) {
14005 const char * const name = SvPV(kSVOP_sv, len);
14007 assert (len < 256);
14008 Copy(name, tmpbuf+1, len, char);
14009 off = pad_findmy_pvn(tmpbuf, len+1, 0);
14010 if (off != NOT_IN_PAD) {
14011 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
14013 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off)));
14014 sv_catpvs(fq, "::");
14015 sv_catsv(fq, kSVOP_sv);
14016 SvREFCNT_dec_NN(kSVOP_sv);
14020 OP * const padop = newOP(OP_PADCV, 0);
14021 padop->op_targ = off;
14022 /* replace the const op with the pad op */
14023 op_sibling_splice(firstkid, NULL, 1, padop);
14029 firstkid = OpSIBLING(firstkid);
14032 for (kid = firstkid; kid; kid = OpSIBLING(kid)) {
14033 /* provide list context for arguments */
14036 op_lvalue(kid, OP_GREPSTART);
14042 /* for sort { X } ..., where X is one of
14043 * $a <=> $b, $b <=> $a, $a cmp $b, $b cmp $a
14044 * elide the second child of the sort (the one containing X),
14045 * and set these flags as appropriate
14049 * Also, check and warn on lexical $a, $b.
14053 S_simplify_sort(pTHX_ OP *o)
14055 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
14059 const char *gvname;
14062 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
14064 kid = kUNOP->op_first; /* get past null */
14065 if (!(have_scopeop = kid->op_type == OP_SCOPE)
14066 && kid->op_type != OP_LEAVE)
14068 kid = kLISTOP->op_last; /* get past scope */
14069 switch(kid->op_type) {
14073 if (!have_scopeop) goto padkids;
14078 k = kid; /* remember this node*/
14079 if (kBINOP->op_first->op_type != OP_RV2SV
14080 || kBINOP->op_last ->op_type != OP_RV2SV)
14083 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
14084 then used in a comparison. This catches most, but not
14085 all cases. For instance, it catches
14086 sort { my($a); $a <=> $b }
14088 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
14089 (although why you'd do that is anyone's guess).
14093 if (!ckWARN(WARN_SYNTAX)) return;
14094 kid = kBINOP->op_first;
14096 if (kid->op_type == OP_PADSV) {
14097 PADNAME * const name = PAD_COMPNAME(kid->op_targ);
14098 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$'
14099 && ( PadnamePV(name)[1] == 'a'
14100 || PadnamePV(name)[1] == 'b' ))
14101 /* diag_listed_as: "my %s" used in sort comparison */
14102 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
14103 "\"%s %s\" used in sort comparison",
14104 PadnameIsSTATE(name)
14109 } while ((kid = OpSIBLING(kid)));
14112 kid = kBINOP->op_first; /* get past cmp */
14113 if (kUNOP->op_first->op_type != OP_GV)
14115 kid = kUNOP->op_first; /* get past rv2sv */
14117 if (GvSTASH(gv) != PL_curstash)
14119 gvname = GvNAME(gv);
14120 if (*gvname == 'a' && gvname[1] == '\0')
14122 else if (*gvname == 'b' && gvname[1] == '\0')
14127 kid = k; /* back to cmp */
14128 /* already checked above that it is rv2sv */
14129 kid = kBINOP->op_last; /* down to 2nd arg */
14130 if (kUNOP->op_first->op_type != OP_GV)
14132 kid = kUNOP->op_first; /* get past rv2sv */
14134 if (GvSTASH(gv) != PL_curstash)
14136 gvname = GvNAME(gv);
14138 ? !(*gvname == 'a' && gvname[1] == '\0')
14139 : !(*gvname == 'b' && gvname[1] == '\0'))
14141 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
14143 o->op_private |= OPpSORT_DESCEND;
14144 if (k->op_type == OP_NCMP)
14145 o->op_private |= OPpSORT_NUMERIC;
14146 if (k->op_type == OP_I_NCMP)
14147 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
14148 kid = OpSIBLING(cLISTOPo->op_first);
14149 /* cut out and delete old block (second sibling) */
14150 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL);
14155 Perl_ck_split(pTHX_ OP *o)
14161 PERL_ARGS_ASSERT_CK_SPLIT;
14163 assert(o->op_type == OP_LIST);
14165 if (o->op_flags & OPf_STACKED)
14166 return no_fh_allowed(o);
14168 kid = cLISTOPo->op_first;
14169 /* delete leading NULL node, then add a CONST if no other nodes */
14170 assert(kid->op_type == OP_NULL);
14171 op_sibling_splice(o, NULL, 1,
14172 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" ")));
14174 kid = cLISTOPo->op_first;
14176 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
14177 /* remove match expression, and replace with new optree with
14178 * a match op at its head */
14179 op_sibling_splice(o, NULL, 1, NULL);
14180 /* pmruntime will handle split " " behavior with flag==2 */
14181 kid = pmruntime(newPMOP(OP_MATCH, 0), kid, NULL, 2, 0);
14182 op_sibling_splice(o, NULL, 0, kid);
14185 assert(kid->op_type == OP_MATCH || kid->op_type == OP_SPLIT);
14187 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
14188 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
14189 "Use of /g modifier is meaningless in split");
14192 /* eliminate the split op, and move the match op (plus any children)
14193 * into its place, then convert the match op into a split op. i.e.
14195 * SPLIT MATCH SPLIT(ex-MATCH)
14197 * MATCH - A - B - C => R - A - B - C => R - A - B - C
14203 * (R, if it exists, will be a regcomp op)
14206 op_sibling_splice(o, NULL, 1, NULL); /* detach match op from o */
14207 sibs = op_sibling_splice(o, NULL, -1, NULL); /* detach any other sibs */
14208 op_sibling_splice(kid, cLISTOPx(kid)->op_last, 0, sibs); /* and reattach */
14209 OpTYPE_set(kid, OP_SPLIT);
14210 kid->op_flags = (o->op_flags | (kid->op_flags & OPf_KIDS));
14211 kid->op_private = o->op_private;
14214 kid = sibs; /* kid is now the string arg of the split */
14217 kid = newDEFSVOP();
14218 op_append_elem(OP_SPLIT, o, kid);
14222 kid = OpSIBLING(kid);
14224 kid = newSVOP(OP_CONST, 0, newSViv(0));
14225 op_append_elem(OP_SPLIT, o, kid);
14226 o->op_private |= OPpSPLIT_IMPLIM;
14230 if (OpHAS_SIBLING(kid))
14231 return too_many_arguments_pv(o,OP_DESC(o), 0);
14237 Perl_ck_stringify(pTHX_ OP *o)
14239 OP * const kid = OpSIBLING(cUNOPo->op_first);
14240 PERL_ARGS_ASSERT_CK_STRINGIFY;
14241 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA
14242 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST
14243 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST)
14244 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */
14246 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
14254 Perl_ck_join(pTHX_ OP *o)
14256 OP * const kid = OpSIBLING(cLISTOPo->op_first);
14258 PERL_ARGS_ASSERT_CK_JOIN;
14260 if (kid && kid->op_type == OP_MATCH) {
14261 if (ckWARN(WARN_SYNTAX)) {
14262 const REGEXP *re = PM_GETRE(kPMOP);
14264 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
14265 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
14266 : newSVpvs_flags( "STRING", SVs_TEMP );
14267 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
14268 "/%" SVf "/ should probably be written as \"%" SVf "\"",
14269 SVfARG(msg), SVfARG(msg));
14273 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */
14274 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO))
14275 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV
14276 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))))
14278 const OP * const bairn = OpSIBLING(kid); /* the list */
14279 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */
14280 && OP_GIMME(bairn,0) == G_SCALAR)
14282 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED,
14283 op_sibling_splice(o, kid, 1, NULL));
14293 =for apidoc rv2cv_op_cv
14295 Examines an op, which is expected to identify a subroutine at runtime,
14296 and attempts to determine at compile time which subroutine it identifies.
14297 This is normally used during Perl compilation to determine whether
14298 a prototype can be applied to a function call. C<cvop> is the op
14299 being considered, normally an C<rv2cv> op. A pointer to the identified
14300 subroutine is returned, if it could be determined statically, and a null
14301 pointer is returned if it was not possible to determine statically.
14303 Currently, the subroutine can be identified statically if the RV that the
14304 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
14305 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
14306 suitable if the constant value must be an RV pointing to a CV. Details of
14307 this process may change in future versions of Perl. If the C<rv2cv> op
14308 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
14309 the subroutine statically: this flag is used to suppress compile-time
14310 magic on a subroutine call, forcing it to use default runtime behaviour.
14312 If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
14313 of a GV reference is modified. If a GV was examined and its CV slot was
14314 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
14315 If the op is not optimised away, and the CV slot is later populated with
14316 a subroutine having a prototype, that flag eventually triggers the warning
14317 "called too early to check prototype".
14319 If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
14320 of returning a pointer to the subroutine it returns a pointer to the
14321 GV giving the most appropriate name for the subroutine in this context.
14322 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
14323 (C<CvANON>) subroutine that is referenced through a GV it will be the
14324 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
14325 A null pointer is returned as usual if there is no statically-determinable
14328 =for apidoc Amnh||OPpEARLY_CV
14329 =for apidoc Amnh||OPpENTERSUB_AMPER
14330 =for apidoc Amnh||RV2CVOPCV_MARK_EARLY
14331 =for apidoc Amnh||RV2CVOPCV_RETURN_NAME_GV
14336 /* shared by toke.c:yylex */
14338 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
14340 PADNAME *name = PAD_COMPNAME(off);
14341 CV *compcv = PL_compcv;
14342 while (PadnameOUTER(name)) {
14343 assert(PARENT_PAD_INDEX(name));
14344 compcv = CvOUTSIDE(compcv);
14345 name = PadlistNAMESARRAY(CvPADLIST(compcv))
14346 [off = PARENT_PAD_INDEX(name)];
14348 assert(!PadnameIsOUR(name));
14349 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) {
14350 return PadnamePROTOCV(name);
14352 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
14356 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
14361 PERL_ARGS_ASSERT_RV2CV_OP_CV;
14362 if (flags & ~RV2CVOPCV_FLAG_MASK)
14363 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
14364 if (cvop->op_type != OP_RV2CV)
14366 if (cvop->op_private & OPpENTERSUB_AMPER)
14368 if (!(cvop->op_flags & OPf_KIDS))
14370 rvop = cUNOPx(cvop)->op_first;
14371 switch (rvop->op_type) {
14373 gv = cGVOPx_gv(rvop);
14375 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) {
14376 cv = MUTABLE_CV(SvRV(gv));
14380 if (flags & RV2CVOPCV_RETURN_STUB)
14386 if (flags & RV2CVOPCV_MARK_EARLY)
14387 rvop->op_private |= OPpEARLY_CV;
14392 SV *rv = cSVOPx_sv(rvop);
14395 cv = (CV*)SvRV(rv);
14399 cv = find_lexical_cv(rvop->op_targ);
14404 } NOT_REACHED; /* NOTREACHED */
14406 if (SvTYPE((SV*)cv) != SVt_PVCV)
14408 if (flags & RV2CVOPCV_RETURN_NAME_GV) {
14409 if ((!CvANON(cv) && !CvLEXICAL(cv)) || !gv)
14413 else if (flags & RV2CVOPCV_MAYBE_NAME_GV) {
14414 if (CvLEXICAL(cv) || CvNAMED(cv))
14416 if (!CvANON(cv) || !gv)
14426 =for apidoc ck_entersub_args_list
14428 Performs the default fixup of the arguments part of an C<entersub>
14429 op tree. This consists of applying list context to each of the
14430 argument ops. This is the standard treatment used on a call marked
14431 with C<&>, or a method call, or a call through a subroutine reference,
14432 or any other call where the callee can't be identified at compile time,
14433 or a call where the callee has no prototype.
14439 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
14443 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
14445 aop = cUNOPx(entersubop)->op_first;
14446 if (!OpHAS_SIBLING(aop))
14447 aop = cUNOPx(aop)->op_first;
14448 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
14449 /* skip the extra attributes->import() call implicitly added in
14450 * something like foo(my $x : bar)
14452 if ( aop->op_type == OP_ENTERSUB
14453 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID
14457 op_lvalue(aop, OP_ENTERSUB);
14463 =for apidoc ck_entersub_args_proto
14465 Performs the fixup of the arguments part of an C<entersub> op tree
14466 based on a subroutine prototype. This makes various modifications to
14467 the argument ops, from applying context up to inserting C<refgen> ops,
14468 and checking the number and syntactic types of arguments, as directed by
14469 the prototype. This is the standard treatment used on a subroutine call,
14470 not marked with C<&>, where the callee can be identified at compile time
14471 and has a prototype.
14473 C<protosv> supplies the subroutine prototype to be applied to the call.
14474 It may be a normal defined scalar, of which the string value will be used.
14475 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
14476 that has been cast to C<SV*>) which has a prototype. The prototype
14477 supplied, in whichever form, does not need to match the actual callee
14478 referenced by the op tree.
14480 If the argument ops disagree with the prototype, for example by having
14481 an unacceptable number of arguments, a valid op tree is returned anyway.
14482 The error is reflected in the parser state, normally resulting in a single
14483 exception at the top level of parsing which covers all the compilation
14484 errors that occurred. In the error message, the callee is referred to
14485 by the name defined by the C<namegv> parameter.
14491 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
14494 const char *proto, *proto_end;
14495 OP *aop, *prev, *cvop, *parent;
14498 I32 contextclass = 0;
14499 const char *e = NULL;
14500 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
14501 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
14502 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
14503 "flags=%lx", (unsigned long) SvFLAGS(protosv));
14504 if (SvTYPE(protosv) == SVt_PVCV)
14505 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
14506 else proto = SvPV(protosv, proto_len);
14507 proto = S_strip_spaces(aTHX_ proto, &proto_len);
14508 proto_end = proto + proto_len;
14509 parent = entersubop;
14510 aop = cUNOPx(entersubop)->op_first;
14511 if (!OpHAS_SIBLING(aop)) {
14513 aop = cUNOPx(aop)->op_first;
14516 aop = OpSIBLING(aop);
14517 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
14518 while (aop != cvop) {
14521 if (proto >= proto_end)
14523 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
14524 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
14525 SVfARG(namesv)), SvUTF8(namesv));
14535 /* _ must be at the end */
14536 if (proto[1] && !memCHRs(";@%", proto[1]))
14552 if ( o3->op_type != OP_UNDEF
14553 && (o3->op_type != OP_SREFGEN
14554 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
14556 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
14558 bad_type_gv(arg, namegv, o3,
14559 arg == 1 ? "block or sub {}" : "sub {}");
14562 /* '*' allows any scalar type, including bareword */
14565 if (o3->op_type == OP_RV2GV)
14566 goto wrapref; /* autoconvert GLOB -> GLOBref */
14567 else if (o3->op_type == OP_CONST)
14568 o3->op_private &= ~OPpCONST_STRICT;
14574 if (o3->op_type == OP_RV2AV ||
14575 o3->op_type == OP_PADAV ||
14576 o3->op_type == OP_RV2HV ||
14577 o3->op_type == OP_PADHV
14583 case '[': case ']':
14590 switch (*proto++) {
14592 if (contextclass++ == 0) {
14593 e = (char *) memchr(proto, ']', proto_end - proto);
14594 if (!e || e == proto)
14602 if (contextclass) {
14603 const char *p = proto;
14604 const char *const end = proto;
14606 while (*--p != '[')
14607 /* \[$] accepts any scalar lvalue */
14609 && Perl_op_lvalue_flags(aTHX_
14611 OP_READ, /* not entersub */
14614 bad_type_gv(arg, namegv, o3,
14615 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p));
14620 if (o3->op_type == OP_RV2GV)
14623 bad_type_gv(arg, namegv, o3, "symbol");
14626 if (o3->op_type == OP_ENTERSUB
14627 && !(o3->op_flags & OPf_STACKED))
14630 bad_type_gv(arg, namegv, o3, "subroutine");
14633 if (o3->op_type == OP_RV2SV ||
14634 o3->op_type == OP_PADSV ||
14635 o3->op_type == OP_HELEM ||
14636 o3->op_type == OP_AELEM)
14638 if (!contextclass) {
14639 /* \$ accepts any scalar lvalue */
14640 if (Perl_op_lvalue_flags(aTHX_
14642 OP_READ, /* not entersub */
14645 bad_type_gv(arg, namegv, o3, "scalar");
14649 if (o3->op_type == OP_RV2AV ||
14650 o3->op_type == OP_PADAV)
14652 o3->op_flags &=~ OPf_PARENS;
14656 bad_type_gv(arg, namegv, o3, "array");
14659 if (o3->op_type == OP_RV2HV ||
14660 o3->op_type == OP_PADHV)
14662 o3->op_flags &=~ OPf_PARENS;
14666 bad_type_gv(arg, namegv, o3, "hash");
14669 aop = S_op_sibling_newUNOP(aTHX_ parent, prev,
14671 if (contextclass && e) {
14676 default: goto oops;
14686 Perl_croak(aTHX_ "Malformed prototype for %" SVf ": %" SVf,
14687 SVfARG(cv_name((CV *)namegv, NULL, 0)),
14692 op_lvalue(aop, OP_ENTERSUB);
14694 aop = OpSIBLING(aop);
14696 if (aop == cvop && *proto == '_') {
14697 /* generate an access to $_ */
14698 op_sibling_splice(parent, prev, 0, newDEFSVOP());
14700 if (!optional && proto_end > proto &&
14701 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
14703 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
14704 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %" SVf,
14705 SVfARG(namesv)), SvUTF8(namesv));
14711 =for apidoc ck_entersub_args_proto_or_list
14713 Performs the fixup of the arguments part of an C<entersub> op tree either
14714 based on a subroutine prototype or using default list-context processing.
14715 This is the standard treatment used on a subroutine call, not marked
14716 with C<&>, where the callee can be identified at compile time.
14718 C<protosv> supplies the subroutine prototype to be applied to the call,
14719 or indicates that there is no prototype. It may be a normal scalar,
14720 in which case if it is defined then the string value will be used
14721 as a prototype, and if it is undefined then there is no prototype.
14722 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
14723 that has been cast to C<SV*>), of which the prototype will be used if it
14724 has one. The prototype (or lack thereof) supplied, in whichever form,
14725 does not need to match the actual callee referenced by the op tree.
14727 If the argument ops disagree with the prototype, for example by having
14728 an unacceptable number of arguments, a valid op tree is returned anyway.
14729 The error is reflected in the parser state, normally resulting in a single
14730 exception at the top level of parsing which covers all the compilation
14731 errors that occurred. In the error message, the callee is referred to
14732 by the name defined by the C<namegv> parameter.
14738 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
14739 GV *namegv, SV *protosv)
14741 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
14742 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
14743 return ck_entersub_args_proto(entersubop, namegv, protosv);
14745 return ck_entersub_args_list(entersubop);
14749 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
14751 IV cvflags = SvIVX(protosv);
14752 int opnum = cvflags & 0xffff;
14753 OP *aop = cUNOPx(entersubop)->op_first;
14755 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
14759 if (!OpHAS_SIBLING(aop))
14760 aop = cUNOPx(aop)->op_first;
14761 aop = OpSIBLING(aop);
14762 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ;
14764 SV *namesv = cv_name((CV *)namegv, NULL, CV_NAME_NOTQUAL);
14765 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
14766 SVfARG(namesv)), SvUTF8(namesv));
14769 op_free(entersubop);
14770 switch(cvflags >> 16) {
14771 case 'F': return newSVOP(OP_CONST, 0,
14772 newSVpv(CopFILE(PL_curcop),0));
14773 case 'L': return newSVOP(
14775 Perl_newSVpvf(aTHX_
14776 "%" IVdf, (IV)CopLINE(PL_curcop)
14779 case 'P': return newSVOP(OP_CONST, 0,
14781 ? newSVhek(HvNAME_HEK(PL_curstash))
14786 NOT_REACHED; /* NOTREACHED */
14789 OP *prev, *cvop, *first, *parent;
14792 parent = entersubop;
14793 if (!OpHAS_SIBLING(aop)) {
14795 aop = cUNOPx(aop)->op_first;
14798 first = prev = aop;
14799 aop = OpSIBLING(aop);
14800 /* find last sibling */
14802 OpHAS_SIBLING(cvop);
14803 prev = cvop, cvop = OpSIBLING(cvop))
14805 if (!(cvop->op_private & OPpENTERSUB_NOPAREN)
14806 /* Usually, OPf_SPECIAL on an op with no args means that it had
14807 * parens, but these have their own meaning for that flag: */
14808 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH
14809 && opnum != OP_DELETE && opnum != OP_EXISTS)
14810 flags |= OPf_SPECIAL;
14811 /* excise cvop from end of sibling chain */
14812 op_sibling_splice(parent, prev, 1, NULL);
14814 if (aop == cvop) aop = NULL;
14816 /* detach remaining siblings from the first sibling, then
14817 * dispose of original optree */
14820 op_sibling_splice(parent, first, -1, NULL);
14821 op_free(entersubop);
14823 if (cvflags == (OP_ENTEREVAL | (1<<16)))
14824 flags |= OPpEVAL_BYTES <<8;
14826 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
14828 case OA_BASEOP_OR_UNOP:
14829 case OA_FILESTATOP:
14831 return newOP(opnum,flags); /* zero args */
14833 return newUNOP(opnum,flags,aop); /* one arg */
14834 /* too many args */
14841 namesv = cv_name((CV *)namegv, NULL, CV_NAME_NOTQUAL);
14842 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
14843 SVfARG(namesv)), SvUTF8(namesv));
14845 nextop = OpSIBLING(aop);
14851 return opnum == OP_RUNCV
14852 ? newPVOP(OP_RUNCV,0,NULL)
14855 return op_convert_list(opnum,0,aop);
14858 NOT_REACHED; /* NOTREACHED */
14863 =for apidoc cv_get_call_checker_flags
14865 Retrieves the function that will be used to fix up a call to C<cv>.
14866 Specifically, the function is applied to an C<entersub> op tree for a
14867 subroutine call, not marked with C<&>, where the callee can be identified
14868 at compile time as C<cv>.
14870 The C-level function pointer is returned in C<*ckfun_p>, an SV argument
14871 for it is returned in C<*ckobj_p>, and control flags are returned in
14872 C<*ckflags_p>. The function is intended to be called in this manner:
14874 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
14876 In this call, C<entersubop> is a pointer to the C<entersub> op,
14877 which may be replaced by the check function, and C<namegv> supplies
14878 the name that should be used by the check function to refer
14879 to the callee of the C<entersub> op if it needs to emit any diagnostics.
14880 It is permitted to apply the check function in non-standard situations,
14881 such as to a call to a different subroutine or to a method call.
14883 C<namegv> may not actually be a GV. If the C<CALL_CHECKER_REQUIRE_GV>
14884 bit is clear in C<*ckflags_p>, it is permitted to pass a CV or other SV
14885 instead, anything that can be used as the first argument to L</cv_name>.
14886 If the C<CALL_CHECKER_REQUIRE_GV> bit is set in C<*ckflags_p> then the
14887 check function requires C<namegv> to be a genuine GV.
14889 By default, the check function is
14890 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
14891 the SV parameter is C<cv> itself, and the C<CALL_CHECKER_REQUIRE_GV>
14892 flag is clear. This implements standard prototype processing. It can
14893 be changed, for a particular subroutine, by L</cv_set_call_checker_flags>.
14895 If the C<CALL_CHECKER_REQUIRE_GV> bit is set in C<gflags> then it
14896 indicates that the caller only knows about the genuine GV version of
14897 C<namegv>, and accordingly the corresponding bit will always be set in
14898 C<*ckflags_p>, regardless of the check function's recorded requirements.
14899 If the C<CALL_CHECKER_REQUIRE_GV> bit is clear in C<gflags> then it
14900 indicates the caller knows about the possibility of passing something
14901 other than a GV as C<namegv>, and accordingly the corresponding bit may
14902 be either set or clear in C<*ckflags_p>, indicating the check function's
14903 recorded requirements.
14905 C<gflags> is a bitset passed into C<cv_get_call_checker_flags>, in which
14906 only the C<CALL_CHECKER_REQUIRE_GV> bit currently has a defined meaning
14907 (for which see above). All other bits should be clear.
14909 =for apidoc Amnh||CALL_CHECKER_REQUIRE_GV
14911 =for apidoc cv_get_call_checker
14913 The original form of L</cv_get_call_checker_flags>, which does not return
14914 checker flags. When using a checker function returned by this function,
14915 it is only safe to call it with a genuine GV as its C<namegv> argument.
14921 Perl_cv_get_call_checker_flags(pTHX_ CV *cv, U32 gflags,
14922 Perl_call_checker *ckfun_p, SV **ckobj_p, U32 *ckflags_p)
14925 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER_FLAGS;
14926 PERL_UNUSED_CONTEXT;
14927 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
14929 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
14930 *ckobj_p = callmg->mg_obj;
14931 *ckflags_p = (callmg->mg_flags | gflags) & MGf_REQUIRE_GV;
14933 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
14934 *ckobj_p = (SV*)cv;
14935 *ckflags_p = gflags & MGf_REQUIRE_GV;
14940 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
14943 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
14944 PERL_UNUSED_CONTEXT;
14945 cv_get_call_checker_flags(cv, CALL_CHECKER_REQUIRE_GV, ckfun_p, ckobj_p,
14950 =for apidoc cv_set_call_checker_flags
14952 Sets the function that will be used to fix up a call to C<cv>.
14953 Specifically, the function is applied to an C<entersub> op tree for a
14954 subroutine call, not marked with C<&>, where the callee can be identified
14955 at compile time as C<cv>.
14957 The C-level function pointer is supplied in C<ckfun>, an SV argument for
14958 it is supplied in C<ckobj>, and control flags are supplied in C<ckflags>.
14959 The function should be defined like this:
14961 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
14963 It is intended to be called in this manner:
14965 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
14967 In this call, C<entersubop> is a pointer to the C<entersub> op,
14968 which may be replaced by the check function, and C<namegv> supplies
14969 the name that should be used by the check function to refer
14970 to the callee of the C<entersub> op if it needs to emit any diagnostics.
14971 It is permitted to apply the check function in non-standard situations,
14972 such as to a call to a different subroutine or to a method call.
14974 C<namegv> may not actually be a GV. For efficiency, perl may pass a
14975 CV or other SV instead. Whatever is passed can be used as the first
14976 argument to L</cv_name>. You can force perl to pass a GV by including
14977 C<CALL_CHECKER_REQUIRE_GV> in the C<ckflags>.
14979 C<ckflags> is a bitset, in which only the C<CALL_CHECKER_REQUIRE_GV>
14980 bit currently has a defined meaning (for which see above). All other
14981 bits should be clear.
14983 The current setting for a particular CV can be retrieved by
14984 L</cv_get_call_checker_flags>.
14986 =for apidoc cv_set_call_checker
14988 The original form of L</cv_set_call_checker_flags>, which passes it the
14989 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility. The effect
14990 of that flag setting is that the check function is guaranteed to get a
14991 genuine GV as its C<namegv> argument.
14997 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
14999 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
15000 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV);
15004 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun,
15005 SV *ckobj, U32 ckflags)
15007 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS;
15008 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
15009 if (SvMAGICAL((SV*)cv))
15010 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
15013 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
15014 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
15016 if (callmg->mg_flags & MGf_REFCOUNTED) {
15017 SvREFCNT_dec(callmg->mg_obj);
15018 callmg->mg_flags &= ~MGf_REFCOUNTED;
15020 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
15021 callmg->mg_obj = ckobj;
15022 if (ckobj != (SV*)cv) {
15023 SvREFCNT_inc_simple_void_NN(ckobj);
15024 callmg->mg_flags |= MGf_REFCOUNTED;
15026 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV)
15027 | (U8)(ckflags & MGf_REQUIRE_GV) | MGf_COPY;
15032 S_entersub_alloc_targ(pTHX_ OP * const o)
15034 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP);
15035 o->op_private |= OPpENTERSUB_HASTARG;
15039 Perl_ck_subr(pTHX_ OP *o)
15044 SV **const_class = NULL;
15046 PERL_ARGS_ASSERT_CK_SUBR;
15048 aop = cUNOPx(o)->op_first;
15049 if (!OpHAS_SIBLING(aop))
15050 aop = cUNOPx(aop)->op_first;
15051 aop = OpSIBLING(aop);
15052 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
15053 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
15054 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL;
15056 o->op_private &= ~1;
15057 o->op_private |= (PL_hints & HINT_STRICT_REFS);
15058 if (PERLDB_SUB && PL_curstash != PL_debstash)
15059 o->op_private |= OPpENTERSUB_DB;
15060 switch (cvop->op_type) {
15062 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
15066 case OP_METHOD_NAMED:
15067 case OP_METHOD_SUPER:
15068 case OP_METHOD_REDIR:
15069 case OP_METHOD_REDIR_SUPER:
15070 o->op_flags |= OPf_REF;
15071 if (aop->op_type == OP_CONST) {
15072 aop->op_private &= ~OPpCONST_STRICT;
15073 const_class = &cSVOPx(aop)->op_sv;
15075 else if (aop->op_type == OP_LIST) {
15076 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first);
15077 if (sib && sib->op_type == OP_CONST) {
15078 sib->op_private &= ~OPpCONST_STRICT;
15079 const_class = &cSVOPx(sib)->op_sv;
15082 /* make class name a shared cow string to speedup method calls */
15083 /* constant string might be replaced with object, f.e. bigint */
15084 if (const_class && SvPOK(*const_class)) {
15086 const char* str = SvPV(*const_class, len);
15088 SV* const shared = newSVpvn_share(
15089 str, SvUTF8(*const_class)
15090 ? -(SSize_t)len : (SSize_t)len,
15093 if (SvREADONLY(*const_class))
15094 SvREADONLY_on(shared);
15095 SvREFCNT_dec(*const_class);
15096 *const_class = shared;
15103 S_entersub_alloc_targ(aTHX_ o);
15104 return ck_entersub_args_list(o);
15106 Perl_call_checker ckfun;
15109 cv_get_call_checker_flags(cv, 0, &ckfun, &ckobj, &ckflags);
15110 if (CvISXSUB(cv) || !CvROOT(cv))
15111 S_entersub_alloc_targ(aTHX_ o);
15113 /* The original call checker API guarantees that a GV will be
15114 be provided with the right name. So, if the old API was
15115 used (or the REQUIRE_GV flag was passed), we have to reify
15116 the CV’s GV, unless this is an anonymous sub. This is not
15117 ideal for lexical subs, as its stringification will include
15118 the package. But it is the best we can do. */
15119 if (ckflags & CALL_CHECKER_REQUIRE_GV) {
15120 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv)))
15123 else namegv = MUTABLE_GV(cv);
15124 /* After a syntax error in a lexical sub, the cv that
15125 rv2cv_op_cv returns may be a nameless stub. */
15126 if (!namegv) return ck_entersub_args_list(o);
15129 return ckfun(aTHX_ o, namegv, ckobj);
15134 Perl_ck_svconst(pTHX_ OP *o)
15136 SV * const sv = cSVOPo->op_sv;
15137 PERL_ARGS_ASSERT_CK_SVCONST;
15138 PERL_UNUSED_CONTEXT;
15139 #ifdef PERL_COPY_ON_WRITE
15140 /* Since the read-only flag may be used to protect a string buffer, we
15141 cannot do copy-on-write with existing read-only scalars that are not
15142 already copy-on-write scalars. To allow $_ = "hello" to do COW with
15143 that constant, mark the constant as COWable here, if it is not
15144 already read-only. */
15145 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
15148 # ifdef PERL_DEBUG_READONLY_COW
15158 Perl_ck_trunc(pTHX_ OP *o)
15160 PERL_ARGS_ASSERT_CK_TRUNC;
15162 if (o->op_flags & OPf_KIDS) {
15163 SVOP *kid = (SVOP*)cUNOPo->op_first;
15165 if (kid->op_type == OP_NULL)
15166 kid = (SVOP*)OpSIBLING(kid);
15167 if (kid && kid->op_type == OP_CONST &&
15168 (kid->op_private & OPpCONST_BARE) &&
15171 o->op_flags |= OPf_SPECIAL;
15172 kid->op_private &= ~OPpCONST_STRICT;
15179 Perl_ck_substr(pTHX_ OP *o)
15181 PERL_ARGS_ASSERT_CK_SUBSTR;
15184 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
15185 OP *kid = cLISTOPo->op_first;
15187 if (kid->op_type == OP_NULL)
15188 kid = OpSIBLING(kid);
15190 /* Historically, substr(delete $foo{bar},...) has been allowed
15191 with 4-arg substr. Keep it working by applying entersub
15193 op_lvalue(kid, OP_ENTERSUB);
15200 Perl_ck_tell(pTHX_ OP *o)
15202 PERL_ARGS_ASSERT_CK_TELL;
15204 if (o->op_flags & OPf_KIDS) {
15205 OP *kid = cLISTOPo->op_first;
15206 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid);
15207 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
15213 Perl_ck_each(pTHX_ OP *o)
15216 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
15217 const unsigned orig_type = o->op_type;
15219 PERL_ARGS_ASSERT_CK_EACH;
15222 switch (kid->op_type) {
15228 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH
15229 : orig_type == OP_KEYS ? OP_AKEYS
15233 if (kid->op_private == OPpCONST_BARE
15234 || !SvROK(cSVOPx_sv(kid))
15235 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
15236 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
15241 qerror(Perl_mess(aTHX_
15242 "Experimental %s on scalar is now forbidden",
15243 PL_op_desc[orig_type]));
15245 bad_type_pv(1, "hash or array", o, kid);
15253 Perl_ck_length(pTHX_ OP *o)
15255 PERL_ARGS_ASSERT_CK_LENGTH;
15259 if (ckWARN(WARN_SYNTAX)) {
15260 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
15264 const bool hash = kid->op_type == OP_PADHV
15265 || kid->op_type == OP_RV2HV;
15266 switch (kid->op_type) {
15271 name = S_op_varname(aTHX_ kid);
15277 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
15278 "length() used on %" SVf " (did you mean \"scalar(%s%" SVf
15280 SVfARG(name), hash ? "keys " : "", SVfARG(name)
15283 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
15284 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
15285 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
15287 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
15288 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
15289 "length() used on @array (did you mean \"scalar(@array)\"?)");
15298 Perl_ck_isa(pTHX_ OP *o)
15300 OP *classop = cBINOPo->op_last;
15302 PERL_ARGS_ASSERT_CK_ISA;
15304 /* Convert barename into PV */
15305 if(classop->op_type == OP_CONST && classop->op_private & OPpCONST_BARE) {
15306 /* TODO: Optionally convert package to raw HV here */
15307 classop->op_private &= ~(OPpCONST_BARE|OPpCONST_STRICT);
15315 ---------------------------------------------------------
15317 Common vars in list assignment
15319 There now follows some enums and static functions for detecting
15320 common variables in list assignments. Here is a little essay I wrote
15321 for myself when trying to get my head around this. DAPM.
15325 First some random observations:
15327 * If a lexical var is an alias of something else, e.g.
15328 for my $x ($lex, $pkg, $a[0]) {...}
15329 then the act of aliasing will increase the reference count of the SV
15331 * If a package var is an alias of something else, it may still have a
15332 reference count of 1, depending on how the alias was created, e.g.
15333 in *a = *b, $a may have a refcount of 1 since the GP is shared
15334 with a single GvSV pointer to the SV. So If it's an alias of another
15335 package var, then RC may be 1; if it's an alias of another scalar, e.g.
15336 a lexical var or an array element, then it will have RC > 1.
15338 * There are many ways to create a package alias; ultimately, XS code
15339 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so
15340 run-time tracing mechanisms are unlikely to be able to catch all cases.
15342 * When the LHS is all my declarations, the same vars can't appear directly
15343 on the RHS, but they can indirectly via closures, aliasing and lvalue
15344 subs. But those techniques all involve an increase in the lexical
15345 scalar's ref count.
15347 * When the LHS is all lexical vars (but not necessarily my declarations),
15348 it is possible for the same lexicals to appear directly on the RHS, and
15349 without an increased ref count, since the stack isn't refcounted.
15350 This case can be detected at compile time by scanning for common lex
15351 vars with PL_generation.
15353 * lvalue subs defeat common var detection, but they do at least
15354 return vars with a temporary ref count increment. Also, you can't
15355 tell at compile time whether a sub call is lvalue.
15360 A: There are a few circumstances where there definitely can't be any
15363 LHS empty: () = (...);
15364 RHS empty: (....) = ();
15365 RHS contains only constants or other 'can't possibly be shared'
15366 elements (e.g. ops that return PADTMPs): (...) = (1,2, length)
15367 i.e. they only contain ops not marked as dangerous, whose children
15368 are also not dangerous;
15370 LHS contains a single scalar element: e.g. ($x) = (....); because
15371 after $x has been modified, it won't be used again on the RHS;
15372 RHS contains a single element with no aggregate on LHS: e.g.
15373 ($a,$b,$c) = ($x); again, once $a has been modified, its value
15374 won't be used again.
15376 B: If LHS are all 'my' lexical var declarations (or safe ops, which
15379 my ($a, $b, @c) = ...;
15381 Due to closure and goto tricks, these vars may already have content.
15382 For the same reason, an element on the RHS may be a lexical or package
15383 alias of one of the vars on the left, or share common elements, for
15386 my ($x,$y) = f(); # $x and $y on both sides
15387 sub f : lvalue { ($x,$y) = (1,2); $y, $x }
15392 my @a = @$ra; # elements of @a on both sides
15393 sub f { @a = 1..4; \@a }
15396 First, just consider scalar vars on LHS:
15398 RHS is safe only if (A), or in addition,
15399 * contains only lexical *scalar* vars, where neither side's
15400 lexicals have been flagged as aliases
15402 If RHS is not safe, then it's always legal to check LHS vars for
15403 RC==1, since the only RHS aliases will always be associated
15406 Note that in particular, RHS is not safe if:
15408 * it contains package scalar vars; e.g.:
15411 my ($x, $y) = (2, $x_alias);
15412 sub f { $x = 1; *x_alias = \$x; }
15414 * It contains other general elements, such as flattened or
15415 * spliced or single array or hash elements, e.g.
15418 my ($x,$y) = @a; # or $a[0] or @a{@b} etc
15422 use feature 'refaliasing';
15423 \($a[0], $a[1]) = \($y,$x);
15426 It doesn't matter if the array/hash is lexical or package.
15428 * it contains a function call that happens to be an lvalue
15429 sub which returns one or more of the above, e.g.
15440 (so a sub call on the RHS should be treated the same
15441 as having a package var on the RHS).
15443 * any other "dangerous" thing, such an op or built-in that
15444 returns one of the above, e.g. pp_preinc
15447 If RHS is not safe, what we can do however is at compile time flag
15448 that the LHS are all my declarations, and at run time check whether
15449 all the LHS have RC == 1, and if so skip the full scan.
15451 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...;
15453 Here the issue is whether there can be elements of @a on the RHS
15454 which will get prematurely freed when @a is cleared prior to
15455 assignment. This is only a problem if the aliasing mechanism
15456 is one which doesn't increase the refcount - only if RC == 1
15457 will the RHS element be prematurely freed.
15459 Because the array/hash is being INTROed, it or its elements
15460 can't directly appear on the RHS:
15462 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE
15464 but can indirectly, e.g.:
15468 sub f { @a = 1..3; \@a }
15470 So if the RHS isn't safe as defined by (A), we must always
15471 mortalise and bump the ref count of any remaining RHS elements
15472 when assigning to a non-empty LHS aggregate.
15474 Lexical scalars on the RHS aren't safe if they've been involved in
15477 use feature 'refaliasing';
15480 \(my $lex) = \$pkg;
15481 my @a = ($lex,3); # equivalent to ($a[0],3)
15488 Similarly with lexical arrays and hashes on the RHS:
15502 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g.
15503 my $a; ($a, my $b) = (....);
15505 The difference between (B) and (C) is that it is now physically
15506 possible for the LHS vars to appear on the RHS too, where they
15507 are not reference counted; but in this case, the compile-time
15508 PL_generation sweep will detect such common vars.
15510 So the rules for (C) differ from (B) in that if common vars are
15511 detected, the runtime "test RC==1" optimisation can no longer be used,
15512 and a full mark and sweep is required
15514 D: As (C), but in addition the LHS may contain package vars.
15516 Since package vars can be aliased without a corresponding refcount
15517 increase, all bets are off. It's only safe if (A). E.g.
15519 my ($x, $y) = (1,2);
15521 for $x_alias ($x) {
15522 ($x_alias, $y) = (3, $x); # whoops
15525 Ditto for LHS aggregate package vars.
15527 E: Any other dangerous ops on LHS, e.g.
15528 (f(), $a[0], @$r) = (...);
15530 this is similar to (E) in that all bets are off. In addition, it's
15531 impossible to determine at compile time whether the LHS
15532 contains a scalar or an aggregate, e.g.
15534 sub f : lvalue { @a }
15537 * ---------------------------------------------------------
15541 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates
15542 * that at least one of the things flagged was seen.
15546 AAS_MY_SCALAR = 0x001, /* my $scalar */
15547 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */
15548 AAS_LEX_SCALAR = 0x004, /* $lexical */
15549 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */
15550 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */
15551 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */
15552 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */
15553 AAS_DANGEROUS = 0x080, /* an op (other than the above)
15554 that's flagged OA_DANGEROUS */
15555 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's
15556 not in any of the categories above */
15557 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */
15562 /* helper function for S_aassign_scan().
15563 * check a PAD-related op for commonality and/or set its generation number.
15564 * Returns a boolean indicating whether its shared */
15567 S_aassign_padcheck(pTHX_ OP* o, bool rhs)
15569 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX)
15570 /* lexical used in aliasing */
15574 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation);
15576 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation);
15583 Helper function for OPpASSIGN_COMMON* detection in rpeep().
15584 It scans the left or right hand subtree of the aassign op, and returns a
15585 set of flags indicating what sorts of things it found there.
15586 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we
15587 set PL_generation on lexical vars; if the latter, we see if
15588 PL_generation matches.
15589 'scalars_p' is a pointer to a counter of the number of scalar SVs seen.
15590 This fn will increment it by the number seen. It's not intended to
15591 be an accurate count (especially as many ops can push a variable
15592 number of SVs onto the stack); rather it's used as to test whether there
15593 can be at most 1 SV pushed; so it's only meanings are "0, 1, many".
15597 S_aassign_scan(pTHX_ OP* o, bool rhs, int *scalars_p)
15600 OP *effective_top_op = o;
15604 bool top = o == effective_top_op;
15606 OP* next_kid = NULL;
15608 /* first, look for a solitary @_ on the RHS */
15611 && (o->op_flags & OPf_KIDS)
15612 && OP_TYPE_IS_OR_WAS(o, OP_LIST)
15614 OP *kid = cUNOPo->op_first;
15615 if ( ( kid->op_type == OP_PUSHMARK
15616 || kid->op_type == OP_PADRANGE) /* ex-pushmark */
15617 && ((kid = OpSIBLING(kid)))
15618 && !OpHAS_SIBLING(kid)
15619 && kid->op_type == OP_RV2AV
15620 && !(kid->op_flags & OPf_REF)
15621 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
15622 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST)
15623 && ((kid = cUNOPx(kid)->op_first))
15624 && kid->op_type == OP_GV
15625 && cGVOPx_gv(kid) == PL_defgv
15630 switch (o->op_type) {
15633 all_flags |= AAS_PKG_SCALAR;
15639 /* if !top, could be e.g. @a[0,1] */
15640 all_flags |= (top && (o->op_flags & OPf_REF))
15641 ? ((o->op_private & OPpLVAL_INTRO)
15642 ? AAS_MY_AGG : AAS_LEX_AGG)
15648 int comm = S_aassign_padcheck(aTHX_ o, rhs)
15649 ? AAS_LEX_SCALAR_COMM : 0;
15651 all_flags |= (o->op_private & OPpLVAL_INTRO)
15652 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm);
15660 if (cUNOPx(o)->op_first->op_type != OP_GV)
15661 all_flags |= AAS_DANGEROUS; /* @{expr}, %{expr} */
15663 /* if !top, could be e.g. @a[0,1] */
15664 else if (top && (o->op_flags & OPf_REF))
15665 all_flags |= AAS_PKG_AGG;
15667 all_flags |= AAS_DANGEROUS;
15672 if (cUNOPx(o)->op_first->op_type != OP_GV) {
15674 all_flags |= AAS_DANGEROUS; /* ${expr} */
15677 all_flags |= AAS_PKG_SCALAR; /* $pkg */
15681 if (o->op_private & OPpSPLIT_ASSIGN) {
15682 /* the assign in @a = split() has been optimised away
15683 * and the @a attached directly to the split op
15684 * Treat the array as appearing on the RHS, i.e.
15685 * ... = (@a = split)
15690 if (o->op_flags & OPf_STACKED) {
15691 /* @{expr} = split() - the array expression is tacked
15692 * on as an extra child to split - process kid */
15693 next_kid = cLISTOPo->op_last;
15697 /* ... else array is directly attached to split op */
15699 all_flags |= (PL_op->op_private & OPpSPLIT_LEX)
15700 ? ((o->op_private & OPpLVAL_INTRO)
15701 ? AAS_MY_AGG : AAS_LEX_AGG)
15706 /* other args of split can't be returned */
15707 all_flags |= AAS_SAFE_SCALAR;
15711 /* undef counts as a scalar on the RHS:
15712 * (undef, $x) = ...; # only 1 scalar on LHS: always safe
15713 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe
15717 flags = AAS_SAFE_SCALAR;
15722 /* these are all no-ops; they don't push a potentially common SV
15723 * onto the stack, so they are neither AAS_DANGEROUS nor
15724 * AAS_SAFE_SCALAR */
15727 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */
15732 /* these do nothing, but may have children */
15736 if (PL_opargs[o->op_type] & OA_DANGEROUS) {
15738 flags = AAS_DANGEROUS;
15742 if ( (PL_opargs[o->op_type] & OA_TARGLEX)
15743 && (o->op_private & OPpTARGET_MY))
15746 all_flags |= S_aassign_padcheck(aTHX_ o, rhs)
15747 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR;
15751 /* if its an unrecognised, non-dangerous op, assume that it
15752 * it the cause of at least one safe scalar */
15754 flags = AAS_SAFE_SCALAR;
15758 all_flags |= flags;
15760 /* by default, process all kids next
15761 * XXX this assumes that all other ops are "transparent" - i.e. that
15762 * they can return some of their children. While this true for e.g.
15763 * sort and grep, it's not true for e.g. map. We really need a
15764 * 'transparent' flag added to regen/opcodes
15766 if (o->op_flags & OPf_KIDS) {
15767 next_kid = cUNOPo->op_first;
15768 /* these ops do nothing but may have children; but their
15769 * children should also be treated as top-level */
15770 if ( o == effective_top_op
15771 && (o->op_type == OP_NULL || o->op_type == OP_LIST)
15773 effective_top_op = next_kid;
15777 /* If next_kid is set, someone in the code above wanted us to process
15778 * that kid and all its remaining siblings. Otherwise, work our way
15779 * back up the tree */
15781 while (!next_kid) {
15783 return all_flags; /* at top; no parents/siblings to try */
15784 if (OpHAS_SIBLING(o)) {
15785 next_kid = o->op_sibparent;
15786 if (o == effective_top_op)
15787 effective_top_op = next_kid;
15790 if (o == effective_top_op)
15791 effective_top_op = o->op_sibparent;
15792 o = o->op_sibparent; /* try parent's next sibling */
15801 /* Check for in place reverse and sort assignments like "@a = reverse @a"
15802 and modify the optree to make them work inplace */
15805 S_inplace_aassign(pTHX_ OP *o) {
15807 OP *modop, *modop_pushmark;
15809 OP *oleft, *oleft_pushmark;
15811 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
15813 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
15815 assert(cUNOPo->op_first->op_type == OP_NULL);
15816 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
15817 assert(modop_pushmark->op_type == OP_PUSHMARK);
15818 modop = OpSIBLING(modop_pushmark);
15820 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
15823 /* no other operation except sort/reverse */
15824 if (OpHAS_SIBLING(modop))
15827 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
15828 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return;
15830 if (modop->op_flags & OPf_STACKED) {
15831 /* skip sort subroutine/block */
15832 assert(oright->op_type == OP_NULL);
15833 oright = OpSIBLING(oright);
15836 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL);
15837 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first;
15838 assert(oleft_pushmark->op_type == OP_PUSHMARK);
15839 oleft = OpSIBLING(oleft_pushmark);
15841 /* Check the lhs is an array */
15843 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
15844 || OpHAS_SIBLING(oleft)
15845 || (oleft->op_private & OPpLVAL_INTRO)
15849 /* Only one thing on the rhs */
15850 if (OpHAS_SIBLING(oright))
15853 /* check the array is the same on both sides */
15854 if (oleft->op_type == OP_RV2AV) {
15855 if (oright->op_type != OP_RV2AV
15856 || !cUNOPx(oright)->op_first
15857 || cUNOPx(oright)->op_first->op_type != OP_GV
15858 || cUNOPx(oleft )->op_first->op_type != OP_GV
15859 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
15860 cGVOPx_gv(cUNOPx(oright)->op_first)
15864 else if (oright->op_type != OP_PADAV
15865 || oright->op_targ != oleft->op_targ
15869 /* This actually is an inplace assignment */
15871 modop->op_private |= OPpSORT_INPLACE;
15873 /* transfer MODishness etc from LHS arg to RHS arg */
15874 oright->op_flags = oleft->op_flags;
15876 /* remove the aassign op and the lhs */
15878 op_null(oleft_pushmark);
15879 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
15880 op_null(cUNOPx(oleft)->op_first);
15886 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start'
15887 * that potentially represent a series of one or more aggregate derefs
15888 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert
15889 * the whole chain to a single OP_MULTIDEREF op (maybe with a few
15890 * additional ops left in too).
15892 * The caller will have already verified that the first few ops in the
15893 * chain following 'start' indicate a multideref candidate, and will have
15894 * set 'orig_o' to the point further on in the chain where the first index
15895 * expression (if any) begins. 'orig_action' specifies what type of
15896 * beginning has already been determined by the ops between start..orig_o
15897 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc).
15899 * 'hints' contains any hints flags that need adding (currently just
15900 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller.
15904 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints)
15908 UNOP_AUX_item *arg_buf = NULL;
15909 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */
15910 int index_skip = -1; /* don't output index arg on this action */
15912 /* similar to regex compiling, do two passes; the first pass
15913 * determines whether the op chain is convertible and calculates the
15914 * buffer size; the second pass populates the buffer and makes any
15915 * changes necessary to ops (such as moving consts to the pad on
15916 * threaded builds).
15918 * NB: for things like Coverity, note that both passes take the same
15919 * path through the logic tree (except for 'if (pass)' bits), since
15920 * both passes are following the same op_next chain; and in
15921 * particular, if it would return early on the second pass, it would
15922 * already have returned early on the first pass.
15924 for (pass = 0; pass < 2; pass++) {
15926 UV action = orig_action;
15927 OP *first_elem_op = NULL; /* first seen aelem/helem */
15928 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */
15929 int action_count = 0; /* number of actions seen so far */
15930 int action_ix = 0; /* action_count % (actions per IV) */
15931 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */
15932 bool is_last = FALSE; /* no more derefs to follow */
15933 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */
15934 UV action_word = 0; /* all actions so far */
15935 UNOP_AUX_item *arg = arg_buf;
15936 UNOP_AUX_item *action_ptr = arg_buf;
15938 arg++; /* reserve slot for first action word */
15941 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
15942 case MDEREF_HV_gvhv_helem:
15943 next_is_hash = TRUE;
15945 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
15946 case MDEREF_AV_gvav_aelem:
15948 #ifdef USE_ITHREADS
15949 arg->pad_offset = cPADOPx(start)->op_padix;
15950 /* stop it being swiped when nulled */
15951 cPADOPx(start)->op_padix = 0;
15953 arg->sv = cSVOPx(start)->op_sv;
15954 cSVOPx(start)->op_sv = NULL;
15960 case MDEREF_HV_padhv_helem:
15961 case MDEREF_HV_padsv_vivify_rv2hv_helem:
15962 next_is_hash = TRUE;
15964 case MDEREF_AV_padav_aelem:
15965 case MDEREF_AV_padsv_vivify_rv2av_aelem:
15967 arg->pad_offset = start->op_targ;
15968 /* we skip setting op_targ = 0 for now, since the intact
15969 * OP_PADXV is needed by S_check_hash_fields_and_hekify */
15970 reset_start_targ = TRUE;
15975 case MDEREF_HV_pop_rv2hv_helem:
15976 next_is_hash = TRUE;
15978 case MDEREF_AV_pop_rv2av_aelem:
15982 NOT_REACHED; /* NOTREACHED */
15987 /* look for another (rv2av/hv; get index;
15988 * aelem/helem/exists/delele) sequence */
15993 UV index_type = MDEREF_INDEX_none;
15995 if (action_count) {
15996 /* if this is not the first lookup, consume the rv2av/hv */
15998 /* for N levels of aggregate lookup, we normally expect
15999 * that the first N-1 [ah]elem ops will be flagged as
16000 * /DEREF (so they autovivifiy if necessary), and the last
16001 * lookup op not to be.
16002 * For other things (like @{$h{k1}{k2}}) extra scope or
16003 * leave ops can appear, so abandon the effort in that
16005 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
16008 /* rv2av or rv2hv sKR/1 */
16010 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
16011 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
16012 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
16015 /* at this point, we wouldn't expect any of these
16016 * possible private flags:
16017 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO
16018 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only)
16020 ASSUME(!(o->op_private &
16021 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING)));
16023 hints = (o->op_private & OPpHINT_STRICT_REFS);
16025 /* make sure the type of the previous /DEREF matches the
16026 * type of the next lookup */
16027 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV));
16030 action = next_is_hash
16031 ? MDEREF_HV_vivify_rv2hv_helem
16032 : MDEREF_AV_vivify_rv2av_aelem;
16036 /* if this is the second pass, and we're at the depth where
16037 * previously we encountered a non-simple index expression,
16038 * stop processing the index at this point */
16039 if (action_count != index_skip) {
16041 /* look for one or more simple ops that return an array
16042 * index or hash key */
16044 switch (o->op_type) {
16046 /* it may be a lexical var index */
16047 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS
16048 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
16049 ASSUME(!(o->op_private &
16050 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
16052 if ( OP_GIMME(o,0) == G_SCALAR
16053 && !(o->op_flags & (OPf_REF|OPf_MOD))
16054 && o->op_private == 0)
16057 arg->pad_offset = o->op_targ;
16059 index_type = MDEREF_INDEX_padsv;
16065 if (next_is_hash) {
16066 /* it's a constant hash index */
16067 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK)))
16068 /* "use constant foo => FOO; $h{+foo}" for
16069 * some weird FOO, can leave you with constants
16070 * that aren't simple strings. It's not worth
16071 * the extra hassle for those edge cases */
16076 OP * helem_op = o->op_next;
16078 ASSUME( helem_op->op_type == OP_HELEM
16079 || helem_op->op_type == OP_NULL
16081 if (helem_op->op_type == OP_HELEM) {
16082 rop = (UNOP*)(((BINOP*)helem_op)->op_first);
16083 if ( helem_op->op_private & OPpLVAL_INTRO
16084 || rop->op_type != OP_RV2HV
16088 /* on first pass just check; on second pass
16090 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo,
16095 #ifdef USE_ITHREADS
16096 /* Relocate sv to the pad for thread safety */
16097 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
16098 arg->pad_offset = o->op_targ;
16101 arg->sv = cSVOPx_sv(o);
16106 /* it's a constant array index */
16108 SV *ix_sv = cSVOPo->op_sv;
16113 if ( action_count == 0
16116 && ( action == MDEREF_AV_padav_aelem
16117 || action == MDEREF_AV_gvav_aelem)
16119 maybe_aelemfast = TRUE;
16123 SvREFCNT_dec_NN(cSVOPo->op_sv);
16127 /* we've taken ownership of the SV */
16128 cSVOPo->op_sv = NULL;
16130 index_type = MDEREF_INDEX_const;
16135 /* it may be a package var index */
16137 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS|OPf_SPECIAL)));
16138 ASSUME(!(o->op_private & ~(OPpEARLY_CV)));
16139 if ( (o->op_flags & ~(OPf_PARENS|OPf_SPECIAL)) != OPf_WANT_SCALAR
16140 || o->op_private != 0
16145 if (kid->op_type != OP_RV2SV)
16148 ASSUME(!(kid->op_flags &
16149 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF
16150 |OPf_SPECIAL|OPf_PARENS)));
16151 ASSUME(!(kid->op_private &
16153 |OPpHINT_STRICT_REFS|OPpOUR_INTRO
16154 |OPpDEREF|OPpLVAL_INTRO)));
16155 if( (kid->op_flags &~ OPf_PARENS)
16156 != (OPf_WANT_SCALAR|OPf_KIDS)
16157 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS))
16162 #ifdef USE_ITHREADS
16163 arg->pad_offset = cPADOPx(o)->op_padix;
16164 /* stop it being swiped when nulled */
16165 cPADOPx(o)->op_padix = 0;
16167 arg->sv = cSVOPx(o)->op_sv;
16168 cSVOPo->op_sv = NULL;
16172 index_type = MDEREF_INDEX_gvsv;
16177 } /* action_count != index_skip */
16179 action |= index_type;
16182 /* at this point we have either:
16183 * * detected what looks like a simple index expression,
16184 * and expect the next op to be an [ah]elem, or
16185 * an nulled [ah]elem followed by a delete or exists;
16186 * * found a more complex expression, so something other
16187 * than the above follows.
16190 /* possibly an optimised away [ah]elem (where op_next is
16191 * exists or delete) */
16192 if (o->op_type == OP_NULL)
16195 /* at this point we're looking for an OP_AELEM, OP_HELEM,
16196 * OP_EXISTS or OP_DELETE */
16198 /* if a custom array/hash access checker is in scope,
16199 * abandon optimisation attempt */
16200 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
16201 && PL_check[o->op_type] != Perl_ck_null)
16203 /* similarly for customised exists and delete */
16204 if ( (o->op_type == OP_EXISTS)
16205 && PL_check[o->op_type] != Perl_ck_exists)
16207 if ( (o->op_type == OP_DELETE)
16208 && PL_check[o->op_type] != Perl_ck_delete)
16211 if ( o->op_type != OP_AELEM
16212 || (o->op_private &
16213 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB))
16215 maybe_aelemfast = FALSE;
16217 /* look for aelem/helem/exists/delete. If it's not the last elem
16218 * lookup, it *must* have OPpDEREF_AV/HV, but not many other
16219 * flags; if it's the last, then it mustn't have
16220 * OPpDEREF_AV/HV, but may have lots of other flags, like
16221 * OPpLVAL_INTRO etc
16224 if ( index_type == MDEREF_INDEX_none
16225 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM
16226 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE)
16230 /* we have aelem/helem/exists/delete with valid simple index */
16232 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
16233 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV
16234 || (o->op_private & OPpDEREF) == OPpDEREF_HV);
16236 /* This doesn't make much sense but is legal:
16237 * @{ local $x[0][0] } = 1
16238 * Since scope exit will undo the autovivification,
16239 * don't bother in the first place. The OP_LEAVE
16240 * assertion is in case there are other cases of both
16241 * OPpLVAL_INTRO and OPpDEREF which don't include a scope
16242 * exit that would undo the local - in which case this
16243 * block of code would need rethinking.
16245 if (is_deref && (o->op_private & OPpLVAL_INTRO)) {
16247 OP *n = o->op_next;
16248 while (n && ( n->op_type == OP_NULL
16249 || n->op_type == OP_LIST
16250 || n->op_type == OP_SCALAR))
16252 assert(n && n->op_type == OP_LEAVE);
16254 o->op_private &= ~OPpDEREF;
16259 ASSUME(!(o->op_flags &
16260 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS)));
16261 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF)));
16263 ok = (o->op_flags &~ OPf_PARENS)
16264 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD)
16265 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK));
16267 else if (o->op_type == OP_EXISTS) {
16268 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
16269 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
16270 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB)));
16271 ok = !(o->op_private & ~OPpARG1_MASK);
16273 else if (o->op_type == OP_DELETE) {
16274 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
16275 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
16276 ASSUME(!(o->op_private &
16277 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO)));
16278 /* don't handle slices or 'local delete'; the latter
16279 * is fairly rare, and has a complex runtime */
16280 ok = !(o->op_private & ~OPpARG1_MASK);
16281 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM))
16282 /* skip handling run-tome error */
16283 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL));
16286 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM);
16287 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD
16288 |OPf_PARENS|OPf_REF|OPf_SPECIAL)));
16289 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB
16290 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO)));
16291 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV;
16296 if (!first_elem_op)
16300 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV);
16305 action |= MDEREF_FLAG_last;
16309 /* at this point we have something that started
16310 * promisingly enough (with rv2av or whatever), but failed
16311 * to find a simple index followed by an
16312 * aelem/helem/exists/delete. If this is the first action,
16313 * give up; but if we've already seen at least one
16314 * aelem/helem, then keep them and add a new action with
16315 * MDEREF_INDEX_none, which causes it to do the vivify
16316 * from the end of the previous lookup, and do the deref,
16317 * but stop at that point. So $a[0][expr] will do one
16318 * av_fetch, vivify and deref, then continue executing at
16323 index_skip = action_count;
16324 action |= MDEREF_FLAG_last;
16325 if (index_type != MDEREF_INDEX_none)
16329 action_word |= (action << (action_ix * MDEREF_SHIFT));
16332 /* if there's no space for the next action, reserve a new slot
16333 * for it *before* we start adding args for that action */
16334 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) {
16336 action_ptr->uv = action_word;
16342 } /* while !is_last */
16347 /* slot reserved for next action word not now needed */
16350 action_ptr->uv = action_word;
16356 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf);
16357 if (index_skip == -1) {
16358 mderef->op_flags = o->op_flags
16359 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0));
16360 if (o->op_type == OP_EXISTS)
16361 mderef->op_private = OPpMULTIDEREF_EXISTS;
16362 else if (o->op_type == OP_DELETE)
16363 mderef->op_private = OPpMULTIDEREF_DELETE;
16365 mderef->op_private = o->op_private
16366 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO);
16368 /* accumulate strictness from every level (although I don't think
16369 * they can actually vary) */
16370 mderef->op_private |= hints;
16372 /* integrate the new multideref op into the optree and the
16375 * In general an op like aelem or helem has two child
16376 * sub-trees: the aggregate expression (a_expr) and the
16377 * index expression (i_expr):
16383 * The a_expr returns an AV or HV, while the i-expr returns an
16384 * index. In general a multideref replaces most or all of a
16385 * multi-level tree, e.g.
16401 * With multideref, all the i_exprs will be simple vars or
16402 * constants, except that i_expr1 may be arbitrary in the case
16403 * of MDEREF_INDEX_none.
16405 * The bottom-most a_expr will be either:
16406 * 1) a simple var (so padXv or gv+rv2Xv);
16407 * 2) a simple scalar var dereferenced (e.g. $r->[0]):
16408 * so a simple var with an extra rv2Xv;
16409 * 3) or an arbitrary expression.
16411 * 'start', the first op in the execution chain, will point to
16412 * 1),2): the padXv or gv op;
16413 * 3): the rv2Xv which forms the last op in the a_expr
16414 * execution chain, and the top-most op in the a_expr
16417 * For all cases, the 'start' node is no longer required,
16418 * but we can't free it since one or more external nodes
16419 * may point to it. E.g. consider
16420 * $h{foo} = $a ? $b : $c
16421 * Here, both the op_next and op_other branches of the
16422 * cond_expr point to the gv[*h] of the hash expression, so
16423 * we can't free the 'start' op.
16425 * For expr->[...], we need to save the subtree containing the
16426 * expression; for the other cases, we just need to save the
16428 * So in all cases, we null the start op and keep it around by
16429 * making it the child of the multideref op; for the expr->
16430 * case, the expr will be a subtree of the start node.
16432 * So in the simple 1,2 case the optree above changes to
16438 * ex-gv (or ex-padxv)
16440 * with the op_next chain being
16442 * -> ex-gv -> multideref -> op-following-ex-exists ->
16444 * In the 3 case, we have
16457 * -> rest-of-a_expr subtree ->
16458 * ex-rv2xv -> multideref -> op-following-ex-exists ->
16461 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none,
16462 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the
16463 * multideref attached as the child, e.g.
16469 * ex-rv2av - i_expr1
16477 /* if we free this op, don't free the pad entry */
16478 if (reset_start_targ)
16479 start->op_targ = 0;
16482 /* Cut the bit we need to save out of the tree and attach to
16483 * the multideref op, then free the rest of the tree */
16485 /* find parent of node to be detached (for use by splice) */
16487 if ( orig_action == MDEREF_AV_pop_rv2av_aelem
16488 || orig_action == MDEREF_HV_pop_rv2hv_helem)
16490 /* there is an arbitrary expression preceding us, e.g.
16491 * expr->[..]? so we need to save the 'expr' subtree */
16492 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE)
16493 p = cUNOPx(p)->op_first;
16494 ASSUME( start->op_type == OP_RV2AV
16495 || start->op_type == OP_RV2HV);
16498 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem
16499 * above for exists/delete. */
16500 while ( (p->op_flags & OPf_KIDS)
16501 && cUNOPx(p)->op_first != start
16503 p = cUNOPx(p)->op_first;
16505 ASSUME(cUNOPx(p)->op_first == start);
16507 /* detach from main tree, and re-attach under the multideref */
16508 op_sibling_splice(mderef, NULL, 0,
16509 op_sibling_splice(p, NULL, 1, NULL));
16512 start->op_next = mderef;
16514 mderef->op_next = index_skip == -1 ? o->op_next : o;
16516 /* excise and free the original tree, and replace with
16517 * the multideref op */
16518 p = op_sibling_splice(top_op, NULL, -1, mderef);
16527 Size_t size = arg - arg_buf;
16529 if (maybe_aelemfast && action_count == 1)
16532 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc(
16533 sizeof(UNOP_AUX_item) * (size + 1));
16534 /* for dumping etc: store the length in a hidden first slot;
16535 * we set the op_aux pointer to the second slot */
16536 arg_buf->uv = size;
16539 } /* for (pass = ...) */
16542 /* See if the ops following o are such that o will always be executed in
16543 * boolean context: that is, the SV which o pushes onto the stack will
16544 * only ever be consumed by later ops via SvTRUE(sv) or similar.
16545 * If so, set a suitable private flag on o. Normally this will be
16546 * bool_flag; but see below why maybe_flag is needed too.
16548 * Typically the two flags you pass will be the generic OPpTRUEBOOL and
16549 * OPpMAYBE_TRUEBOOL, buts it's possible that for some ops those bits may
16550 * already be taken, so you'll have to give that op two different flags.
16552 * More explanation of 'maybe_flag' and 'safe_and' parameters.
16553 * The binary logical ops &&, ||, // (plus 'if' and 'unless' which use
16554 * those underlying ops) short-circuit, which means that rather than
16555 * necessarily returning a truth value, they may return the LH argument,
16556 * which may not be boolean. For example in $x = (keys %h || -1), keys
16557 * should return a key count rather than a boolean, even though its
16558 * sort-of being used in boolean context.
16560 * So we only consider such logical ops to provide boolean context to
16561 * their LH argument if they themselves are in void or boolean context.
16562 * However, sometimes the context isn't known until run-time. In this
16563 * case the op is marked with the maybe_flag flag it.
16565 * Consider the following.
16567 * sub f { ....; if (%h) { .... } }
16569 * This is actually compiled as
16571 * sub f { ....; %h && do { .... } }
16573 * Here we won't know until runtime whether the final statement (and hence
16574 * the &&) is in void context and so is safe to return a boolean value.
16575 * So mark o with maybe_flag rather than the bool_flag.
16576 * Note that there is cost associated with determining context at runtime
16577 * (e.g. a call to block_gimme()), so it may not be worth setting (at
16578 * compile time) and testing (at runtime) maybe_flag if the scalar verses
16579 * boolean costs savings are marginal.
16581 * However, we can do slightly better with && (compared to || and //):
16582 * this op only returns its LH argument when that argument is false. In
16583 * this case, as long as the op promises to return a false value which is
16584 * valid in both boolean and scalar contexts, we can mark an op consumed
16585 * by && with bool_flag rather than maybe_flag.
16586 * For example as long as pp_padhv and pp_rv2hv return &PL_sv_zero rather
16587 * than &PL_sv_no for a false result in boolean context, then it's safe. An
16588 * op which promises to handle this case is indicated by setting safe_and
16593 S_check_for_bool_cxt(OP*o, bool safe_and, U8 bool_flag, U8 maybe_flag)
16598 assert((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR);
16600 /* OPpTARGET_MY and boolean context probably don't mix well.
16601 * If someone finds a valid use case, maybe add an extra flag to this
16602 * function which indicates its safe to do so for this op? */
16603 assert(!( (PL_opargs[o->op_type] & OA_TARGLEX)
16604 && (o->op_private & OPpTARGET_MY)));
16609 switch (lop->op_type) {
16614 /* these two consume the stack argument in the scalar case,
16615 * and treat it as a boolean in the non linenumber case */
16618 if ( ((lop->op_flags & OPf_WANT) == OPf_WANT_LIST)
16619 || (lop->op_private & OPpFLIP_LINENUM))
16625 /* these never leave the original value on the stack */
16634 /* OR DOR and AND evaluate their arg as a boolean, but then may
16635 * leave the original scalar value on the stack when following the
16636 * op_next route. If not in void context, we need to ensure
16637 * that whatever follows consumes the arg only in boolean context
16649 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) {
16653 else if (!(lop->op_flags & OPf_WANT)) {
16654 /* unknown context - decide at runtime */
16666 lop = lop->op_next;
16669 o->op_private |= flag;
16674 /* mechanism for deferring recursion in rpeep() */
16676 #define MAX_DEFERRED 4
16680 if (defer_ix == (MAX_DEFERRED-1)) { \
16681 OP **defer = defer_queue[defer_base]; \
16682 CALL_RPEEP(*defer); \
16683 S_prune_chain_head(defer); \
16684 defer_base = (defer_base + 1) % MAX_DEFERRED; \
16687 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
16690 #define IS_AND_OP(o) (o->op_type == OP_AND)
16691 #define IS_OR_OP(o) (o->op_type == OP_OR)
16694 /* A peephole optimizer. We visit the ops in the order they're to execute.
16695 * See the comments at the top of this file for more details about when
16696 * peep() is called */
16699 Perl_rpeep(pTHX_ OP *o)
16703 OP* oldoldop = NULL;
16704 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
16705 int defer_base = 0;
16708 if (!o || o->op_opt)
16711 assert(o->op_type != OP_FREED);
16715 SAVEVPTR(PL_curcop);
16716 for (;; o = o->op_next) {
16717 if (o && o->op_opt)
16720 while (defer_ix >= 0) {
16722 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
16723 CALL_RPEEP(*defer);
16724 S_prune_chain_head(defer);
16731 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */
16732 assert(!oldoldop || oldoldop->op_next == oldop);
16733 assert(!oldop || oldop->op_next == o);
16735 /* By default, this op has now been optimised. A couple of cases below
16736 clear this again. */
16740 /* look for a series of 1 or more aggregate derefs, e.g.
16741 * $a[1]{foo}[$i]{$k}
16742 * and replace with a single OP_MULTIDEREF op.
16743 * Each index must be either a const, or a simple variable,
16745 * First, look for likely combinations of starting ops,
16746 * corresponding to (global and lexical variants of)
16748 * $r->[...] $r->{...}
16749 * (preceding expression)->[...]
16750 * (preceding expression)->{...}
16751 * and if so, call maybe_multideref() to do a full inspection
16752 * of the op chain and if appropriate, replace with an
16760 switch (o2->op_type) {
16762 /* $pkg[..] : gv[*pkg]
16763 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */
16765 /* Fail if there are new op flag combinations that we're
16766 * not aware of, rather than:
16767 * * silently failing to optimise, or
16768 * * silently optimising the flag away.
16769 * If this ASSUME starts failing, examine what new flag
16770 * has been added to the op, and decide whether the
16771 * optimisation should still occur with that flag, then
16772 * update the code accordingly. This applies to all the
16773 * other ASSUMEs in the block of code too.
16775 ASSUME(!(o2->op_flags &
16776 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL)));
16777 ASSUME(!(o2->op_private & ~OPpEARLY_CV));
16781 if (o2->op_type == OP_RV2AV) {
16782 action = MDEREF_AV_gvav_aelem;
16786 if (o2->op_type == OP_RV2HV) {
16787 action = MDEREF_HV_gvhv_helem;
16791 if (o2->op_type != OP_RV2SV)
16794 /* at this point we've seen gv,rv2sv, so the only valid
16795 * construct left is $pkg->[] or $pkg->{} */
16797 ASSUME(!(o2->op_flags & OPf_STACKED));
16798 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
16799 != (OPf_WANT_SCALAR|OPf_MOD))
16802 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS
16803 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO)));
16804 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO))
16806 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV
16807 && (o2->op_private & OPpDEREF) != OPpDEREF_HV)
16811 if (o2->op_type == OP_RV2AV) {
16812 action = MDEREF_AV_gvsv_vivify_rv2av_aelem;
16815 if (o2->op_type == OP_RV2HV) {
16816 action = MDEREF_HV_gvsv_vivify_rv2hv_helem;
16822 /* $lex->[...]: padsv[$lex] sM/DREFAV */
16824 ASSUME(!(o2->op_flags &
16825 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL)));
16826 if ((o2->op_flags &
16827 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
16828 != (OPf_WANT_SCALAR|OPf_MOD))
16831 ASSUME(!(o2->op_private &
16832 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
16833 /* skip if state or intro, or not a deref */
16834 if ( o2->op_private != OPpDEREF_AV
16835 && o2->op_private != OPpDEREF_HV)
16839 if (o2->op_type == OP_RV2AV) {
16840 action = MDEREF_AV_padsv_vivify_rv2av_aelem;
16843 if (o2->op_type == OP_RV2HV) {
16844 action = MDEREF_HV_padsv_vivify_rv2hv_helem;
16851 /* $lex[..]: padav[@lex:1,2] sR *
16852 * or $lex{..}: padhv[%lex:1,2] sR */
16853 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS|
16854 OPf_REF|OPf_SPECIAL)));
16855 if ((o2->op_flags &
16856 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
16857 != (OPf_WANT_SCALAR|OPf_REF))
16859 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF))
16861 /* OPf_PARENS isn't currently used in this case;
16862 * if that changes, let us know! */
16863 ASSUME(!(o2->op_flags & OPf_PARENS));
16865 /* at this point, we wouldn't expect any of the remaining
16866 * possible private flags:
16867 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL,
16868 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB
16870 * OPpSLICEWARNING shouldn't affect runtime
16872 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING)));
16874 action = o2->op_type == OP_PADAV
16875 ? MDEREF_AV_padav_aelem
16876 : MDEREF_HV_padhv_helem;
16878 S_maybe_multideref(aTHX_ o, o2, action, 0);
16884 action = o2->op_type == OP_RV2AV
16885 ? MDEREF_AV_pop_rv2av_aelem
16886 : MDEREF_HV_pop_rv2hv_helem;
16889 /* (expr)->[...]: rv2av sKR/1;
16890 * (expr)->{...}: rv2hv sKR/1; */
16892 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV);
16894 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
16895 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL)));
16896 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
16899 /* at this point, we wouldn't expect any of these
16900 * possible private flags:
16901 * OPpMAYBE_LVSUB, OPpLVAL_INTRO
16902 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only)
16904 ASSUME(!(o2->op_private &
16905 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING
16907 hints |= (o2->op_private & OPpHINT_STRICT_REFS);
16911 S_maybe_multideref(aTHX_ o, o2, action, hints);
16920 switch (o->op_type) {
16922 PL_curcop = ((COP*)o); /* for warnings */
16925 PL_curcop = ((COP*)o); /* for warnings */
16927 /* Optimise a "return ..." at the end of a sub to just be "...".
16928 * This saves 2 ops. Before:
16929 * 1 <;> nextstate(main 1 -e:1) v ->2
16930 * 4 <@> return K ->5
16931 * 2 <0> pushmark s ->3
16932 * - <1> ex-rv2sv sK/1 ->4
16933 * 3 <#> gvsv[*cat] s ->4
16936 * - <@> return K ->-
16937 * - <0> pushmark s ->2
16938 * - <1> ex-rv2sv sK/1 ->-
16939 * 2 <$> gvsv(*cat) s ->3
16942 OP *next = o->op_next;
16943 OP *sibling = OpSIBLING(o);
16944 if ( OP_TYPE_IS(next, OP_PUSHMARK)
16945 && OP_TYPE_IS(sibling, OP_RETURN)
16946 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
16947 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
16948 ||OP_TYPE_IS(sibling->op_next->op_next,
16950 && cUNOPx(sibling)->op_first == next
16951 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next
16954 /* Look through the PUSHMARK's siblings for one that
16955 * points to the RETURN */
16956 OP *top = OpSIBLING(next);
16957 while (top && top->op_next) {
16958 if (top->op_next == sibling) {
16959 top->op_next = sibling->op_next;
16960 o->op_next = next->op_next;
16963 top = OpSIBLING(top);
16968 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
16970 * This latter form is then suitable for conversion into padrange
16971 * later on. Convert:
16973 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
16977 * nextstate1 -> listop -> nextstate3
16979 * pushmark -> padop1 -> padop2
16981 if (o->op_next && (
16982 o->op_next->op_type == OP_PADSV
16983 || o->op_next->op_type == OP_PADAV
16984 || o->op_next->op_type == OP_PADHV
16986 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
16987 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
16988 && o->op_next->op_next->op_next && (
16989 o->op_next->op_next->op_next->op_type == OP_PADSV
16990 || o->op_next->op_next->op_next->op_type == OP_PADAV
16991 || o->op_next->op_next->op_next->op_type == OP_PADHV
16993 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
16994 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
16995 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
16996 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
16998 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm;
17001 ns2 = pad1->op_next;
17002 pad2 = ns2->op_next;
17003 ns3 = pad2->op_next;
17005 /* we assume here that the op_next chain is the same as
17006 * the op_sibling chain */
17007 assert(OpSIBLING(o) == pad1);
17008 assert(OpSIBLING(pad1) == ns2);
17009 assert(OpSIBLING(ns2) == pad2);
17010 assert(OpSIBLING(pad2) == ns3);
17012 /* excise and delete ns2 */
17013 op_sibling_splice(NULL, pad1, 1, NULL);
17016 /* excise pad1 and pad2 */
17017 op_sibling_splice(NULL, o, 2, NULL);
17019 /* create new listop, with children consisting of:
17020 * a new pushmark, pad1, pad2. */
17021 newop = newLISTOP(OP_LIST, 0, pad1, pad2);
17022 newop->op_flags |= OPf_PARENS;
17023 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
17025 /* insert newop between o and ns3 */
17026 op_sibling_splice(NULL, o, 0, newop);
17028 /*fixup op_next chain */
17029 newpm = cUNOPx(newop)->op_first; /* pushmark */
17030 o ->op_next = newpm;
17031 newpm->op_next = pad1;
17032 pad1 ->op_next = pad2;
17033 pad2 ->op_next = newop; /* listop */
17034 newop->op_next = ns3;
17036 /* Ensure pushmark has this flag if padops do */
17037 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) {
17038 newpm->op_flags |= OPf_MOD;
17044 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
17045 to carry two labels. For now, take the easier option, and skip
17046 this optimisation if the first NEXTSTATE has a label. */
17047 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
17048 OP *nextop = o->op_next;
17050 switch (nextop->op_type) {
17055 nextop = nextop->op_next;
17061 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
17064 oldop->op_next = nextop;
17066 /* Skip (old)oldop assignment since the current oldop's
17067 op_next already points to the next op. */
17074 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
17075 if (o->op_next->op_private & OPpTARGET_MY) {
17076 if (o->op_flags & OPf_STACKED) /* chained concats */
17077 break; /* ignore_optimization */
17079 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
17080 o->op_targ = o->op_next->op_targ;
17081 o->op_next->op_targ = 0;
17082 o->op_private |= OPpTARGET_MY;
17085 op_null(o->op_next);
17089 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
17090 break; /* Scalar stub must produce undef. List stub is noop */
17094 if (o->op_targ == OP_NEXTSTATE
17095 || o->op_targ == OP_DBSTATE)
17097 PL_curcop = ((COP*)o);
17099 /* XXX: We avoid setting op_seq here to prevent later calls
17100 to rpeep() from mistakenly concluding that optimisation
17101 has already occurred. This doesn't fix the real problem,
17102 though (See 20010220.007 (#5874)). AMS 20010719 */
17103 /* op_seq functionality is now replaced by op_opt */
17111 oldop->op_next = o->op_next;
17125 convert repeat into a stub with no kids.
17127 if (o->op_next->op_type == OP_CONST
17128 || ( o->op_next->op_type == OP_PADSV
17129 && !(o->op_next->op_private & OPpLVAL_INTRO))
17130 || ( o->op_next->op_type == OP_GV
17131 && o->op_next->op_next->op_type == OP_RV2SV
17132 && !(o->op_next->op_next->op_private
17133 & (OPpLVAL_INTRO|OPpOUR_INTRO))))
17135 const OP *kid = o->op_next->op_next;
17136 if (o->op_next->op_type == OP_GV)
17137 kid = kid->op_next;
17138 /* kid is now the ex-list. */
17139 if (kid->op_type == OP_NULL
17140 && (kid = kid->op_next)->op_type == OP_CONST
17141 /* kid is now the repeat count. */
17142 && kid->op_next->op_type == OP_REPEAT
17143 && kid->op_next->op_private & OPpREPEAT_DOLIST
17144 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST
17145 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0
17148 o = kid->op_next; /* repeat */
17149 oldop->op_next = o;
17150 op_free(cBINOPo->op_first);
17151 op_free(cBINOPo->op_last );
17152 o->op_flags &=~ OPf_KIDS;
17153 /* stub is a baseop; repeat is a binop */
17154 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP));
17155 OpTYPE_set(o, OP_STUB);
17161 /* Convert a series of PAD ops for my vars plus support into a
17162 * single padrange op. Basically
17164 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
17166 * becomes, depending on circumstances, one of
17168 * padrange ----------------------------------> (list) -> rest
17169 * padrange --------------------------------------------> rest
17171 * where all the pad indexes are sequential and of the same type
17173 * We convert the pushmark into a padrange op, then skip
17174 * any other pad ops, and possibly some trailing ops.
17175 * Note that we don't null() the skipped ops, to make it
17176 * easier for Deparse to undo this optimisation (and none of
17177 * the skipped ops are holding any resourses). It also makes
17178 * it easier for find_uninit_var(), as it can just ignore
17179 * padrange, and examine the original pad ops.
17183 OP *followop = NULL; /* the op that will follow the padrange op */
17186 PADOFFSET base = 0; /* init only to stop compiler whining */
17187 bool gvoid = 0; /* init only to stop compiler whining */
17188 bool defav = 0; /* seen (...) = @_ */
17189 bool reuse = 0; /* reuse an existing padrange op */
17191 /* look for a pushmark -> gv[_] -> rv2av */
17196 if ( p->op_type == OP_GV
17197 && cGVOPx_gv(p) == PL_defgv
17198 && (rv2av = p->op_next)
17199 && rv2av->op_type == OP_RV2AV
17200 && !(rv2av->op_flags & OPf_REF)
17201 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
17202 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
17204 q = rv2av->op_next;
17205 if (q->op_type == OP_NULL)
17207 if (q->op_type == OP_PUSHMARK) {
17217 /* scan for PAD ops */
17219 for (p = p->op_next; p; p = p->op_next) {
17220 if (p->op_type == OP_NULL)
17223 if (( p->op_type != OP_PADSV
17224 && p->op_type != OP_PADAV
17225 && p->op_type != OP_PADHV
17227 /* any private flag other than INTRO? e.g. STATE */
17228 || (p->op_private & ~OPpLVAL_INTRO)
17232 /* let $a[N] potentially be optimised into AELEMFAST_LEX
17234 if ( p->op_type == OP_PADAV
17236 && p->op_next->op_type == OP_CONST
17237 && p->op_next->op_next
17238 && p->op_next->op_next->op_type == OP_AELEM
17242 /* for 1st padop, note what type it is and the range
17243 * start; for the others, check that it's the same type
17244 * and that the targs are contiguous */
17246 intro = (p->op_private & OPpLVAL_INTRO);
17248 gvoid = OP_GIMME(p,0) == G_VOID;
17251 if ((p->op_private & OPpLVAL_INTRO) != intro)
17253 /* Note that you'd normally expect targs to be
17254 * contiguous in my($a,$b,$c), but that's not the case
17255 * when external modules start doing things, e.g.
17256 * Function::Parameters */
17257 if (p->op_targ != base + count)
17259 assert(p->op_targ == base + count);
17260 /* Either all the padops or none of the padops should
17261 be in void context. Since we only do the optimisa-
17262 tion for av/hv when the aggregate itself is pushed
17263 on to the stack (one item), there is no need to dis-
17264 tinguish list from scalar context. */
17265 if (gvoid != (OP_GIMME(p,0) == G_VOID))
17269 /* for AV, HV, only when we're not flattening */
17270 if ( p->op_type != OP_PADSV
17272 && !(p->op_flags & OPf_REF)
17276 if (count >= OPpPADRANGE_COUNTMASK)
17279 /* there's a biggest base we can fit into a
17280 * SAVEt_CLEARPADRANGE in pp_padrange.
17281 * (The sizeof() stuff will be constant-folded, and is
17282 * intended to avoid getting "comparison is always false"
17283 * compiler warnings. See the comments above
17284 * MEM_WRAP_CHECK for more explanation on why we do this
17285 * in a weird way to avoid compiler warnings.)
17288 && (8*sizeof(base) >
17289 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT
17291 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
17293 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
17297 /* Success! We've got another valid pad op to optimise away */
17299 followop = p->op_next;
17302 if (count < 1 || (count == 1 && !defav))
17305 /* pp_padrange in specifically compile-time void context
17306 * skips pushing a mark and lexicals; in all other contexts
17307 * (including unknown till runtime) it pushes a mark and the
17308 * lexicals. We must be very careful then, that the ops we
17309 * optimise away would have exactly the same effect as the
17311 * In particular in void context, we can only optimise to
17312 * a padrange if we see the complete sequence
17313 * pushmark, pad*v, ...., list
17314 * which has the net effect of leaving the markstack as it
17315 * was. Not pushing onto the stack (whereas padsv does touch
17316 * the stack) makes no difference in void context.
17320 if (followop->op_type == OP_LIST
17321 && OP_GIMME(followop,0) == G_VOID
17324 followop = followop->op_next; /* skip OP_LIST */
17326 /* consolidate two successive my(...);'s */
17329 && oldoldop->op_type == OP_PADRANGE
17330 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
17331 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
17332 && !(oldoldop->op_flags & OPf_SPECIAL)
17335 assert(oldoldop->op_next == oldop);
17336 assert( oldop->op_type == OP_NEXTSTATE
17337 || oldop->op_type == OP_DBSTATE);
17338 assert(oldop->op_next == o);
17341 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
17343 /* Do not assume pad offsets for $c and $d are con-
17348 if ( oldoldop->op_targ + old_count == base
17349 && old_count < OPpPADRANGE_COUNTMASK - count) {
17350 base = oldoldop->op_targ;
17351 count += old_count;
17356 /* if there's any immediately following singleton
17357 * my var's; then swallow them and the associated
17359 * my ($a,$b); my $c; my $d;
17361 * my ($a,$b,$c,$d);
17364 while ( ((p = followop->op_next))
17365 && ( p->op_type == OP_PADSV
17366 || p->op_type == OP_PADAV
17367 || p->op_type == OP_PADHV)
17368 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
17369 && (p->op_private & OPpLVAL_INTRO) == intro
17370 && !(p->op_private & ~OPpLVAL_INTRO)
17372 && ( p->op_next->op_type == OP_NEXTSTATE
17373 || p->op_next->op_type == OP_DBSTATE)
17374 && count < OPpPADRANGE_COUNTMASK
17375 && base + count == p->op_targ
17378 followop = p->op_next;
17386 assert(oldoldop->op_type == OP_PADRANGE);
17387 oldoldop->op_next = followop;
17388 oldoldop->op_private = (intro | count);
17394 /* Convert the pushmark into a padrange.
17395 * To make Deparse easier, we guarantee that a padrange was
17396 * *always* formerly a pushmark */
17397 assert(o->op_type == OP_PUSHMARK);
17398 o->op_next = followop;
17399 OpTYPE_set(o, OP_PADRANGE);
17401 /* bit 7: INTRO; bit 6..0: count */
17402 o->op_private = (intro | count);
17403 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
17404 | gvoid * OPf_WANT_VOID
17405 | (defav ? OPf_SPECIAL : 0));
17411 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17412 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
17417 /*'keys %h' in void or scalar context: skip the OP_KEYS
17418 * and perform the functionality directly in the RV2HV/PADHV
17421 if (o->op_flags & OPf_REF) {
17422 OP *k = o->op_next;
17423 U8 want = (k->op_flags & OPf_WANT);
17425 && k->op_type == OP_KEYS
17426 && ( want == OPf_WANT_VOID
17427 || want == OPf_WANT_SCALAR)
17428 && !(k->op_private & OPpMAYBE_LVSUB)
17429 && !(k->op_flags & OPf_MOD)
17431 o->op_next = k->op_next;
17432 o->op_flags &= ~(OPf_REF|OPf_WANT);
17433 o->op_flags |= want;
17434 o->op_private |= (o->op_type == OP_PADHV ?
17435 OPpPADHV_ISKEYS : OPpRV2HV_ISKEYS);
17436 /* for keys(%lex), hold onto the OP_KEYS's targ
17437 * since padhv doesn't have its own targ to return
17439 if (!(o->op_type ==OP_PADHV && want == OPf_WANT_SCALAR))
17444 /* see if %h is used in boolean context */
17445 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17446 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
17449 if (o->op_type != OP_PADHV)
17453 if ( o->op_type == OP_PADAV
17454 && (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR
17456 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
17459 /* Skip over state($x) in void context. */
17460 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO)
17461 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID)
17463 oldop->op_next = o->op_next;
17464 goto redo_nextstate;
17466 if (o->op_type != OP_PADAV)
17470 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
17471 OP* const pop = (o->op_type == OP_PADAV) ?
17472 o->op_next : o->op_next->op_next;
17474 if (pop && pop->op_type == OP_CONST &&
17475 ((PL_op = pop->op_next)) &&
17476 pop->op_next->op_type == OP_AELEM &&
17477 !(pop->op_next->op_private &
17478 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
17479 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
17482 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
17483 no_bareword_allowed(pop);
17484 if (o->op_type == OP_GV)
17485 op_null(o->op_next);
17486 op_null(pop->op_next);
17488 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
17489 o->op_next = pop->op_next->op_next;
17490 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
17491 o->op_private = (U8)i;
17492 if (o->op_type == OP_GV) {
17495 o->op_type = OP_AELEMFAST;
17498 o->op_type = OP_AELEMFAST_LEX;
17500 if (o->op_type != OP_GV)
17504 /* Remove $foo from the op_next chain in void context. */
17506 && ( o->op_next->op_type == OP_RV2SV
17507 || o->op_next->op_type == OP_RV2AV
17508 || o->op_next->op_type == OP_RV2HV )
17509 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
17510 && !(o->op_next->op_private & OPpLVAL_INTRO))
17512 oldop->op_next = o->op_next->op_next;
17513 /* Reprocess the previous op if it is a nextstate, to
17514 allow double-nextstate optimisation. */
17516 if (oldop->op_type == OP_NEXTSTATE) {
17523 o = oldop->op_next;
17526 else if (o->op_next->op_type == OP_RV2SV) {
17527 if (!(o->op_next->op_private & OPpDEREF)) {
17528 op_null(o->op_next);
17529 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
17531 o->op_next = o->op_next->op_next;
17532 OpTYPE_set(o, OP_GVSV);
17535 else if (o->op_next->op_type == OP_READLINE
17536 && o->op_next->op_next->op_type == OP_CONCAT
17537 && (o->op_next->op_next->op_flags & OPf_STACKED))
17539 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
17540 OpTYPE_set(o, OP_RCATLINE);
17541 o->op_flags |= OPf_STACKED;
17542 op_null(o->op_next->op_next);
17543 op_null(o->op_next);
17554 case OP_CMPCHAIN_AND:
17555 while (cLOGOP->op_other->op_type == OP_NULL)
17556 cLOGOP->op_other = cLOGOP->op_other->op_next;
17557 while (o->op_next && ( o->op_type == o->op_next->op_type
17558 || o->op_next->op_type == OP_NULL))
17559 o->op_next = o->op_next->op_next;
17561 /* If we're an OR and our next is an AND in void context, we'll
17562 follow its op_other on short circuit, same for reverse.
17563 We can't do this with OP_DOR since if it's true, its return
17564 value is the underlying value which must be evaluated
17568 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
17569 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
17571 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
17573 o->op_next = ((LOGOP*)o->op_next)->op_other;
17575 DEFER(cLOGOP->op_other);
17580 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17581 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
17590 case OP_ARGDEFELEM:
17591 while (cLOGOP->op_other->op_type == OP_NULL)
17592 cLOGOP->op_other = cLOGOP->op_other->op_next;
17593 DEFER(cLOGOP->op_other);
17598 while (cLOOP->op_redoop->op_type == OP_NULL)
17599 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
17600 while (cLOOP->op_nextop->op_type == OP_NULL)
17601 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
17602 while (cLOOP->op_lastop->op_type == OP_NULL)
17603 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
17604 /* a while(1) loop doesn't have an op_next that escapes the
17605 * loop, so we have to explicitly follow the op_lastop to
17606 * process the rest of the code */
17607 DEFER(cLOOP->op_lastop);
17611 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
17612 DEFER(cLOGOPo->op_other);
17616 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17617 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
17618 assert(!(cPMOP->op_pmflags & PMf_ONCE));
17619 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
17620 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
17621 cPMOP->op_pmstashstartu.op_pmreplstart
17622 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
17623 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
17629 if (o->op_flags & OPf_SPECIAL) {
17630 /* first arg is a code block */
17631 OP * const nullop = OpSIBLING(cLISTOP->op_first);
17632 OP * kid = cUNOPx(nullop)->op_first;
17634 assert(nullop->op_type == OP_NULL);
17635 assert(kid->op_type == OP_SCOPE
17636 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
17637 /* since OP_SORT doesn't have a handy op_other-style
17638 * field that can point directly to the start of the code
17639 * block, store it in the otherwise-unused op_next field
17640 * of the top-level OP_NULL. This will be quicker at
17641 * run-time, and it will also allow us to remove leading
17642 * OP_NULLs by just messing with op_nexts without
17643 * altering the basic op_first/op_sibling layout. */
17644 kid = kLISTOP->op_first;
17646 (kid->op_type == OP_NULL
17647 && ( kid->op_targ == OP_NEXTSTATE
17648 || kid->op_targ == OP_DBSTATE ))
17649 || kid->op_type == OP_STUB
17650 || kid->op_type == OP_ENTER
17651 || (PL_parser && PL_parser->error_count));
17652 nullop->op_next = kid->op_next;
17653 DEFER(nullop->op_next);
17656 /* check that RHS of sort is a single plain array */
17657 oright = cUNOPo->op_first;
17658 if (!oright || oright->op_type != OP_PUSHMARK)
17661 if (o->op_private & OPpSORT_INPLACE)
17664 /* reverse sort ... can be optimised. */
17665 if (!OpHAS_SIBLING(cUNOPo)) {
17666 /* Nothing follows us on the list. */
17667 OP * const reverse = o->op_next;
17669 if (reverse->op_type == OP_REVERSE &&
17670 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
17671 OP * const pushmark = cUNOPx(reverse)->op_first;
17672 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
17673 && (OpSIBLING(cUNOPx(pushmark)) == o)) {
17674 /* reverse -> pushmark -> sort */
17675 o->op_private |= OPpSORT_REVERSE;
17677 pushmark->op_next = oright->op_next;
17687 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
17689 LISTOP *enter, *exlist;
17691 if (o->op_private & OPpSORT_INPLACE)
17694 enter = (LISTOP *) o->op_next;
17697 if (enter->op_type == OP_NULL) {
17698 enter = (LISTOP *) enter->op_next;
17702 /* for $a (...) will have OP_GV then OP_RV2GV here.
17703 for (...) just has an OP_GV. */
17704 if (enter->op_type == OP_GV) {
17705 gvop = (OP *) enter;
17706 enter = (LISTOP *) enter->op_next;
17709 if (enter->op_type == OP_RV2GV) {
17710 enter = (LISTOP *) enter->op_next;
17716 if (enter->op_type != OP_ENTERITER)
17719 iter = enter->op_next;
17720 if (!iter || iter->op_type != OP_ITER)
17723 expushmark = enter->op_first;
17724 if (!expushmark || expushmark->op_type != OP_NULL
17725 || expushmark->op_targ != OP_PUSHMARK)
17728 exlist = (LISTOP *) OpSIBLING(expushmark);
17729 if (!exlist || exlist->op_type != OP_NULL
17730 || exlist->op_targ != OP_LIST)
17733 if (exlist->op_last != o) {
17734 /* Mmm. Was expecting to point back to this op. */
17737 theirmark = exlist->op_first;
17738 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
17741 if (OpSIBLING(theirmark) != o) {
17742 /* There's something between the mark and the reverse, eg
17743 for (1, reverse (...))
17748 ourmark = ((LISTOP *)o)->op_first;
17749 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
17752 ourlast = ((LISTOP *)o)->op_last;
17753 if (!ourlast || ourlast->op_next != o)
17756 rv2av = OpSIBLING(ourmark);
17757 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av)
17758 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
17759 /* We're just reversing a single array. */
17760 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
17761 enter->op_flags |= OPf_STACKED;
17764 /* We don't have control over who points to theirmark, so sacrifice
17766 theirmark->op_next = ourmark->op_next;
17767 theirmark->op_flags = ourmark->op_flags;
17768 ourlast->op_next = gvop ? gvop : (OP *) enter;
17771 enter->op_private |= OPpITER_REVERSED;
17772 iter->op_private |= OPpITER_REVERSED;
17776 o = oldop->op_next;
17778 NOT_REACHED; /* NOTREACHED */
17784 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
17785 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
17790 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)
17791 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb)))
17794 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
17796 sv = newRV((SV *)PL_compcv);
17800 OpTYPE_set(o, OP_CONST);
17801 o->op_flags |= OPf_SPECIAL;
17802 cSVOPo->op_sv = sv;
17807 if (OP_GIMME(o,0) == G_VOID
17808 || ( o->op_next->op_type == OP_LINESEQ
17809 && ( o->op_next->op_next->op_type == OP_LEAVESUB
17810 || ( o->op_next->op_next->op_type == OP_RETURN
17811 && !CvLVALUE(PL_compcv)))))
17813 OP *right = cBINOP->op_first;
17832 OP *left = OpSIBLING(right);
17833 if (left->op_type == OP_SUBSTR
17834 && (left->op_private & 7) < 4) {
17836 /* cut out right */
17837 op_sibling_splice(o, NULL, 1, NULL);
17838 /* and insert it as second child of OP_SUBSTR */
17839 op_sibling_splice(left, cBINOPx(left)->op_first, 0,
17841 left->op_private |= OPpSUBSTR_REPL_FIRST;
17843 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
17850 int l, r, lr, lscalars, rscalars;
17852 /* handle common vars detection, e.g. ($a,$b) = ($b,$a).
17853 Note that we do this now rather than in newASSIGNOP(),
17854 since only by now are aliased lexicals flagged as such
17856 See the essay "Common vars in list assignment" above for
17857 the full details of the rationale behind all the conditions
17860 PL_generation sorcery:
17861 To detect whether there are common vars, the global var
17862 PL_generation is incremented for each assign op we scan.
17863 Then we run through all the lexical variables on the LHS,
17864 of the assignment, setting a spare slot in each of them to
17865 PL_generation. Then we scan the RHS, and if any lexicals
17866 already have that value, we know we've got commonality.
17867 Also, if the generation number is already set to
17868 PERL_INT_MAX, then the variable is involved in aliasing, so
17869 we also have potential commonality in that case.
17875 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, &lscalars);
17878 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, &rscalars);
17882 /* After looking for things which are *always* safe, this main
17883 * if/else chain selects primarily based on the type of the
17884 * LHS, gradually working its way down from the more dangerous
17885 * to the more restrictive and thus safer cases */
17887 if ( !l /* () = ....; */
17888 || !r /* .... = (); */
17889 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */
17890 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */
17891 || (lscalars < 2) /* ($x, undef) = ... */
17893 NOOP; /* always safe */
17895 else if (l & AAS_DANGEROUS) {
17896 /* always dangerous */
17897 o->op_private |= OPpASSIGN_COMMON_SCALAR;
17898 o->op_private |= OPpASSIGN_COMMON_AGG;
17900 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) {
17901 /* package vars are always dangerous - too many
17902 * aliasing possibilities */
17903 if (l & AAS_PKG_SCALAR)
17904 o->op_private |= OPpASSIGN_COMMON_SCALAR;
17905 if (l & AAS_PKG_AGG)
17906 o->op_private |= OPpASSIGN_COMMON_AGG;
17908 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG
17909 |AAS_LEX_SCALAR|AAS_LEX_AGG))
17911 /* LHS contains only lexicals and safe ops */
17913 if (l & (AAS_MY_AGG|AAS_LEX_AGG))
17914 o->op_private |= OPpASSIGN_COMMON_AGG;
17916 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) {
17917 if (lr & AAS_LEX_SCALAR_COMM)
17918 o->op_private |= OPpASSIGN_COMMON_SCALAR;
17919 else if ( !(l & AAS_LEX_SCALAR)
17920 && (r & AAS_DEFAV))
17924 * as scalar-safe for performance reasons.
17925 * (it will still have been marked _AGG if necessary */
17928 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS))
17929 /* if there are only lexicals on the LHS and no
17930 * common ones on the RHS, then we assume that the
17931 * only way those lexicals could also get
17932 * on the RHS is via some sort of dereffing or
17935 * ($lex, $x) = (1, $$r)
17936 * and in this case we assume the var must have
17937 * a bumped ref count. So if its ref count is 1,
17938 * it must only be on the LHS.
17940 o->op_private |= OPpASSIGN_COMMON_RC1;
17945 * may have to handle aggregate on LHS, but we can't
17946 * have common scalars. */
17949 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1);
17951 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17952 S_check_for_bool_cxt(o, 1, OPpASSIGN_TRUEBOOL, 0);
17957 /* see if ref() is used in boolean context */
17958 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17959 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
17963 /* see if the op is used in known boolean context,
17964 * but not if OA_TARGLEX optimisation is enabled */
17965 if ( (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR
17966 && !(o->op_private & OPpTARGET_MY)
17968 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
17972 /* see if the op is used in known boolean context */
17973 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
17974 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
17978 Perl_cpeep_t cpeep =
17979 XopENTRYCUSTOM(o, xop_peep);
17981 cpeep(aTHX_ o, oldop);
17986 /* did we just null the current op? If so, re-process it to handle
17987 * eliding "empty" ops from the chain */
17988 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
18001 Perl_peep(pTHX_ OP *o)
18007 =head1 Custom Operators
18009 =for apidoc Perl_custom_op_xop
18010 Return the XOP structure for a given custom op. This macro should be
18011 considered internal to C<OP_NAME> and the other access macros: use them instead.
18012 This macro does call a function. Prior
18013 to 5.19.6, this was implemented as a
18020 /* use PERL_MAGIC_ext to call a function to free the xop structure when
18021 * freeing PL_custom_ops */
18024 custom_op_register_free(pTHX_ SV *sv, MAGIC *mg)
18028 PERL_UNUSED_ARG(mg);
18029 xop = INT2PTR(XOP *, SvIV(sv));
18030 Safefree(xop->xop_name);
18031 Safefree(xop->xop_desc);
18037 static const MGVTBL custom_op_register_vtbl = {
18042 custom_op_register_free, /* free */
18052 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
18058 static const XOP xop_null = { 0, 0, 0, 0, 0 };
18060 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
18061 assert(o->op_type == OP_CUSTOM);
18063 /* This is wrong. It assumes a function pointer can be cast to IV,
18064 * which isn't guaranteed, but this is what the old custom OP code
18065 * did. In principle it should be safer to Copy the bytes of the
18066 * pointer into a PV: since the new interface is hidden behind
18067 * functions, this can be changed later if necessary. */
18068 /* Change custom_op_xop if this ever happens */
18069 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
18072 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
18074 /* See if the op isn't registered, but its name *is* registered.
18075 * That implies someone is using the pre-5.14 API,where only name and
18076 * description could be registered. If so, fake up a real
18078 * We only check for an existing name, and assume no one will have
18079 * just registered a desc */
18080 if (!he && PL_custom_op_names &&
18081 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
18086 /* XXX does all this need to be shared mem? */
18087 Newxz(xop, 1, XOP);
18088 pv = SvPV(HeVAL(he), l);
18089 XopENTRY_set(xop, xop_name, savepvn(pv, l));
18090 if (PL_custom_op_descs &&
18091 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
18093 pv = SvPV(HeVAL(he), l);
18094 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
18096 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
18097 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
18098 /* add magic to the SV so that the xop struct (pointed to by
18099 * SvIV(sv)) is freed. Normally a static xop is registered, but
18100 * for this backcompat hack, we've alloced one */
18101 (void)sv_magicext(HeVAL(he), NULL, PERL_MAGIC_ext,
18102 &custom_op_register_vtbl, NULL, 0);
18107 xop = (XOP *)&xop_null;
18109 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
18113 if(field == XOPe_xop_ptr) {
18116 const U32 flags = XopFLAGS(xop);
18117 if(flags & field) {
18119 case XOPe_xop_name:
18120 any.xop_name = xop->xop_name;
18122 case XOPe_xop_desc:
18123 any.xop_desc = xop->xop_desc;
18125 case XOPe_xop_class:
18126 any.xop_class = xop->xop_class;
18128 case XOPe_xop_peep:
18129 any.xop_peep = xop->xop_peep;
18132 NOT_REACHED; /* NOTREACHED */
18137 case XOPe_xop_name:
18138 any.xop_name = XOPd_xop_name;
18140 case XOPe_xop_desc:
18141 any.xop_desc = XOPd_xop_desc;
18143 case XOPe_xop_class:
18144 any.xop_class = XOPd_xop_class;
18146 case XOPe_xop_peep:
18147 any.xop_peep = XOPd_xop_peep;
18150 NOT_REACHED; /* NOTREACHED */
18155 /* On some platforms (HP-UX, IA64) gcc emits a warning for this function:
18156 * op.c: In function 'Perl_custom_op_get_field':
18157 * op.c:...: warning: 'any.xop_name' may be used uninitialized in this function [-Wmaybe-uninitialized]
18158 * This is because on those platforms (with -DEBUGGING) NOT_REACHED
18159 * expands to assert(0), which expands to ((0) ? (void)0 :
18160 * __assert(...)), and gcc doesn't know that __assert can never return. */
18166 =for apidoc custom_op_register
18167 Register a custom op. See L<perlguts/"Custom Operators">.
18173 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
18177 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
18179 /* see the comment in custom_op_xop */
18180 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
18182 if (!PL_custom_ops)
18183 PL_custom_ops = newHV();
18185 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
18186 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
18191 =for apidoc core_prototype
18193 This function assigns the prototype of the named core function to C<sv>, or
18194 to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or
18195 C<NULL> if the core function has no prototype. C<code> is a code as returned
18196 by C<keyword()>. It must not be equal to 0.
18202 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
18205 int i = 0, n = 0, seen_question = 0, defgv = 0;
18207 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
18208 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
18209 bool nullret = FALSE;
18211 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
18215 if (!sv) sv = sv_newmortal();
18217 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
18219 switch (code < 0 ? -code : code) {
18220 case KEY_and : case KEY_chop: case KEY_chomp:
18221 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
18222 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
18223 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
18224 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
18225 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
18226 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
18227 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
18228 case KEY_x : case KEY_xor :
18229 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
18230 case KEY_glob: retsetpvs("_;", OP_GLOB);
18231 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS);
18232 case KEY_values: retsetpvs("\\[%@]", OP_VALUES);
18233 case KEY_each: retsetpvs("\\[%@]", OP_EACH);
18234 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
18235 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
18237 case KEY_evalbytes:
18238 name = "entereval"; break;
18246 while (i < MAXO) { /* The slow way. */
18247 if (strEQ(name, PL_op_name[i])
18248 || strEQ(name, PL_op_desc[i]))
18250 if (nullret) { assert(opnum); *opnum = i; return NULL; }
18257 defgv = PL_opargs[i] & OA_DEFGV;
18258 oa = PL_opargs[i] >> OASHIFT;
18260 if (oa & OA_OPTIONAL && !seen_question && (
18261 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
18266 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
18267 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
18268 /* But globs are already references (kinda) */
18269 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
18273 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
18274 && !scalar_mod_type(NULL, i)) {
18279 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
18283 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
18284 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
18285 str[n-1] = '_'; defgv = 0;
18289 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
18291 sv_setpvn(sv, str, n - 1);
18292 if (opnum) *opnum = i;
18297 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
18300 OP * const argop = (opnum == OP_SELECT && code) ? NULL :
18301 newSVOP(OP_COREARGS,0,coreargssv);
18304 PERL_ARGS_ASSERT_CORESUB_OP;
18308 return op_append_elem(OP_LINESEQ,
18311 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
18318 o = newUNOP(OP_AVHVSWITCH,0,argop);
18319 o->op_private = opnum-OP_EACH;
18321 case OP_SELECT: /* which represents OP_SSELECT as well */
18326 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
18327 newSVOP(OP_CONST, 0, newSVuv(1))
18329 coresub_op(newSVuv((UV)OP_SSELECT), 0,
18331 coresub_op(coreargssv, 0, OP_SELECT)
18335 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
18337 return op_append_elem(
18340 opnum == OP_WANTARRAY || opnum == OP_RUNCV
18341 ? OPpOFFBYONE << 8 : 0)
18343 case OA_BASEOP_OR_UNOP:
18344 if (opnum == OP_ENTEREVAL) {
18345 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
18346 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
18348 else o = newUNOP(opnum,0,argop);
18349 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
18352 if (is_handle_constructor(o, 1))
18353 argop->op_private |= OPpCOREARGS_DEREF1;
18354 if (scalar_mod_type(NULL, opnum))
18355 argop->op_private |= OPpCOREARGS_SCALARMOD;
18359 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
18360 if (is_handle_constructor(o, 2))
18361 argop->op_private |= OPpCOREARGS_DEREF2;
18362 if (opnum == OP_SUBSTR) {
18363 o->op_private |= OPpMAYBE_LVSUB;
18372 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
18373 SV * const *new_const_svp)
18375 const char *hvname;
18376 bool is_const = !!CvCONST(old_cv);
18377 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
18379 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
18381 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
18383 /* They are 2 constant subroutines generated from
18384 the same constant. This probably means that
18385 they are really the "same" proxy subroutine
18386 instantiated in 2 places. Most likely this is
18387 when a constant is exported twice. Don't warn.
18390 (ckWARN(WARN_REDEFINE)
18392 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
18393 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
18394 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
18395 strEQ(hvname, "autouse"))
18399 && ckWARN_d(WARN_REDEFINE)
18400 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
18403 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
18405 ? "Constant subroutine %" SVf " redefined"
18406 : "Subroutine %" SVf " redefined",
18411 =head1 Hook manipulation
18413 These functions provide convenient and thread-safe means of manipulating
18420 =for apidoc wrap_op_checker
18422 Puts a C function into the chain of check functions for a specified op
18423 type. This is the preferred way to manipulate the L</PL_check> array.
18424 C<opcode> specifies which type of op is to be affected. C<new_checker>
18425 is a pointer to the C function that is to be added to that opcode's
18426 check chain, and C<old_checker_p> points to the storage location where a
18427 pointer to the next function in the chain will be stored. The value of
18428 C<new_checker> is written into the L</PL_check> array, while the value
18429 previously stored there is written to C<*old_checker_p>.
18431 L</PL_check> is global to an entire process, and a module wishing to
18432 hook op checking may find itself invoked more than once per process,
18433 typically in different threads. To handle that situation, this function
18434 is idempotent. The location C<*old_checker_p> must initially (once
18435 per process) contain a null pointer. A C variable of static duration
18436 (declared at file scope, typically also marked C<static> to give
18437 it internal linkage) will be implicitly initialised appropriately,
18438 if it does not have an explicit initialiser. This function will only
18439 actually modify the check chain if it finds C<*old_checker_p> to be null.
18440 This function is also thread safe on the small scale. It uses appropriate
18441 locking to avoid race conditions in accessing L</PL_check>.
18443 When this function is called, the function referenced by C<new_checker>
18444 must be ready to be called, except for C<*old_checker_p> being unfilled.
18445 In a threading situation, C<new_checker> may be called immediately,
18446 even before this function has returned. C<*old_checker_p> will always
18447 be appropriately set before C<new_checker> is called. If C<new_checker>
18448 decides not to do anything special with an op that it is given (which
18449 is the usual case for most uses of op check hooking), it must chain the
18450 check function referenced by C<*old_checker_p>.
18452 Taken all together, XS code to hook an op checker should typically look
18453 something like this:
18455 static Perl_check_t nxck_frob;
18456 static OP *myck_frob(pTHX_ OP *op) {
18458 op = nxck_frob(aTHX_ op);
18463 wrap_op_checker(OP_FROB, myck_frob, &nxck_frob);
18465 If you want to influence compilation of calls to a specific subroutine,
18466 then use L</cv_set_call_checker_flags> rather than hooking checking of
18467 all C<entersub> ops.
18473 Perl_wrap_op_checker(pTHX_ Optype opcode,
18474 Perl_check_t new_checker, Perl_check_t *old_checker_p)
18478 PERL_UNUSED_CONTEXT;
18479 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
18480 if (*old_checker_p) return;
18481 OP_CHECK_MUTEX_LOCK;
18482 if (!*old_checker_p) {
18483 *old_checker_p = PL_check[opcode];
18484 PL_check[opcode] = new_checker;
18486 OP_CHECK_MUTEX_UNLOCK;
18491 /* Efficient sub that returns a constant scalar value. */
18493 const_sv_xsub(pTHX_ CV* cv)
18496 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
18497 PERL_UNUSED_ARG(items);
18507 const_av_xsub(pTHX_ CV* cv)
18510 AV * const av = MUTABLE_AV(XSANY.any_ptr);
18518 if (SvRMAGICAL(av))
18519 Perl_croak(aTHX_ "Magical list constants are not supported");
18520 if (GIMME_V != G_ARRAY) {
18522 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
18525 EXTEND(SP, AvFILLp(av)+1);
18526 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
18527 XSRETURN(AvFILLp(av)+1);
18530 /* Copy an existing cop->cop_warnings field.
18531 * If it's one of the standard addresses, just re-use the address.
18532 * This is the e implementation for the DUP_WARNINGS() macro
18536 Perl_dup_warnings(pTHX_ STRLEN* warnings)
18539 STRLEN *new_warnings;
18541 if (warnings == NULL || specialWARN(warnings))
18544 size = sizeof(*warnings) + *warnings;
18546 new_warnings = (STRLEN*)PerlMemShared_malloc(size);
18547 Copy(warnings, new_warnings, size, char);
18548 return new_warnings;
18552 * ex: set ts=8 sts=4 sw=4 et: