This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Implement get_fork_TTY for tmux
[perl5.git] / op.c
1 #line 2 "op.c"
2 /*    op.c
3  *
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
6  *
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.
9  *
10  */
11
12 /*
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
18  *
19  *     [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
20  */
21
22 /* This file contains the functions that create, manipulate and optimize
23  * the OP structures that hold a compiled perl program.
24  *
25  * A Perl program is compiled into a tree of OPs. Each op contains
26  * structural pointers (eg to its siblings and the next op in the
27  * execution sequence), a pointer to the function that would execute the
28  * op, plus any data specific to that op. For example, an OP_CONST op
29  * points to the pp_const() function and to an SV containing the constant
30  * value. When pp_const() is executed, its job is to push that SV onto the
31  * stack.
32  *
33  * OPs are mainly created by the newFOO() functions, which are mainly
34  * called from the parser (in perly.y) as the code is parsed. For example
35  * the Perl code $a + $b * $c would cause the equivalent of the following
36  * to be called (oversimplifying a bit):
37  *
38  *  newBINOP(OP_ADD, flags,
39  *      newSVREF($a),
40  *      newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
41  *  )
42  *
43  * Note that during the build of miniperl, a temporary copy of this file
44  * is made, called opmini.c.
45  */
46
47 /*
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
49
50     A bottom-up pass
51     A top-down pass
52     An execution-order pass
53
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines.  The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order.  (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
61 top level node.)
62
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again).  As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node.  But
70 it's still not the real execution order.
71
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer.  At that point, we can call
75 into peep() to do that code's portion of the 3rd pass.  It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
77 */
78
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80    get the compile time state of %^H for that block.  Storing %^H in every
81    block (or even COP) would be very expensive, so a different approach is
82    taken.  The (running) state of %^H is serialised into a tree of HE-like
83    structs.  Stores into %^H are chained onto the current leaf as a struct
84    refcounted_he * with the key and the value.  Deletes from %^H are saved
85    with a value of PL_sv_placeholder.  The state of %^H at any point can be
86    turned back into a regular HV by walking back up the tree from that point's
87    leaf, ignoring any key you've already seen (placeholder or not), storing
88    the rest into the HV structure, then removing the placeholders. Hence
89    memory is only used to store the %^H deltas from the enclosing COP, rather
90    than the entire %^H on each COP.
91
92    To cause actions on %^H to write out the serialisation records, it has
93    magic type 'H'. This magic (itself) does nothing, but its presence causes
94    the values to gain magic type 'h', which has entries for set and clear.
95    C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96    record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97    saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98    it will be correctly restored when any inner compiling scope is exited.
99 */
100
101 #include "EXTERN.h"
102 #define PERL_IN_OP_C
103 #include "perl.h"
104 #include "keywords.h"
105 #include "feature.h"
106 #include "regcomp.h"
107
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
111
112 /* remove any leading "empty" ops from the op_next chain whose first
113  * node's address is stored in op_p. Store the updated address of the
114  * first node in op_p.
115  */
116
117 STATIC void
118 S_prune_chain_head(pTHX_ OP** op_p)
119 {
120     while (*op_p
121         && (   (*op_p)->op_type == OP_NULL
122             || (*op_p)->op_type == OP_SCOPE
123             || (*op_p)->op_type == OP_SCALAR
124             || (*op_p)->op_type == OP_LINESEQ)
125     )
126         *op_p = (*op_p)->op_next;
127 }
128
129
130 /* See the explanatory comments above struct opslab in op.h. */
131
132 #ifdef PERL_DEBUG_READONLY_OPS
133 #  define PERL_SLAB_SIZE 128
134 #  define PERL_MAX_SLAB_SIZE 4096
135 #  include <sys/mman.h>
136 #endif
137
138 #ifndef PERL_SLAB_SIZE
139 #  define PERL_SLAB_SIZE 64
140 #endif
141 #ifndef PERL_MAX_SLAB_SIZE
142 #  define PERL_MAX_SLAB_SIZE 2048
143 #endif
144
145 /* rounds up to nearest pointer */
146 #define SIZE_TO_PSIZE(x)        (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
147 #define DIFF(o,p)               ((size_t)((I32 **)(p) - (I32**)(o)))
148
149 static OPSLAB *
150 S_new_slab(pTHX_ size_t sz)
151 {
152 #ifdef PERL_DEBUG_READONLY_OPS
153     OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
154                                    PROT_READ|PROT_WRITE,
155                                    MAP_ANON|MAP_PRIVATE, -1, 0);
156     DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
157                           (unsigned long) sz, slab));
158     if (slab == MAP_FAILED) {
159         perror("mmap failed");
160         abort();
161     }
162     slab->opslab_size = (U16)sz;
163 #else
164     OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
165 #endif
166     slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
167     return slab;
168 }
169
170 /* requires double parens and aTHX_ */
171 #define DEBUG_S_warn(args)                                             \
172     DEBUG_S(                                                            \
173         PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
174     )
175
176 void *
177 Perl_Slab_Alloc(pTHX_ size_t sz)
178 {
179     dVAR;
180     OPSLAB *slab;
181     OPSLAB *slab2;
182     OPSLOT *slot;
183     OP *o;
184     size_t opsz, space;
185
186     /* We only allocate ops from the slab during subroutine compilation.
187        We find the slab via PL_compcv, hence that must be non-NULL. It could
188        also be pointing to a subroutine which is now fully set up (CvROOT()
189        pointing to the top of the optree for that sub), or a subroutine
190        which isn't using the slab allocator. If our sanity checks aren't met,
191        don't use a slab, but allocate the OP directly from the heap.  */
192     if (!PL_compcv || CvROOT(PL_compcv)
193      || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
194         return PerlMemShared_calloc(1, sz);
195
196     /* While the subroutine is under construction, the slabs are accessed via
197        CvSTART(), to avoid needing to expand PVCV by one pointer for something
198        unneeded at runtime. Once a subroutine is constructed, the slabs are
199        accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
200        allocated yet.  See the commit message for 8be227ab5eaa23f2 for more
201        details.  */
202     if (!CvSTART(PL_compcv)) {
203         CvSTART(PL_compcv) =
204             (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
205         CvSLABBED_on(PL_compcv);
206         slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
207     }
208     else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
209
210     opsz = SIZE_TO_PSIZE(sz);
211     sz = opsz + OPSLOT_HEADER_P;
212
213     /* The slabs maintain a free list of OPs. In particular, constant folding
214        will free up OPs, so it makes sense to re-use them where possible. A
215        freed up slot is used in preference to a new allocation.  */
216     if (slab->opslab_freed) {
217         OP **too = &slab->opslab_freed;
218         o = *too;
219         DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", o, slab));
220         while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
221             DEBUG_S_warn((aTHX_ "Alas! too small"));
222             o = *(too = &o->op_next);
223             if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", o)); }
224         }
225         if (o) {
226             *too = o->op_next;
227             Zero(o, opsz, I32 *);
228             o->op_slabbed = 1;
229             return (void *)o;
230         }
231     }
232
233 #define INIT_OPSLOT \
234             slot->opslot_slab = slab;                   \
235             slot->opslot_next = slab2->opslab_first;    \
236             slab2->opslab_first = slot;                 \
237             o = &slot->opslot_op;                       \
238             o->op_slabbed = 1
239
240     /* The partially-filled slab is next in the chain. */
241     slab2 = slab->opslab_next ? slab->opslab_next : slab;
242     if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
243         /* Remaining space is too small. */
244
245         /* If we can fit a BASEOP, add it to the free chain, so as not
246            to waste it. */
247         if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
248             slot = &slab2->opslab_slots;
249             INIT_OPSLOT;
250             o->op_type = OP_FREED;
251             o->op_next = slab->opslab_freed;
252             slab->opslab_freed = o;
253         }
254
255         /* Create a new slab.  Make this one twice as big. */
256         slot = slab2->opslab_first;
257         while (slot->opslot_next) slot = slot->opslot_next;
258         slab2 = S_new_slab(aTHX_
259                             (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
260                                         ? PERL_MAX_SLAB_SIZE
261                                         : (DIFF(slab2, slot)+1)*2);
262         slab2->opslab_next = slab->opslab_next;
263         slab->opslab_next = slab2;
264     }
265     assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
266
267     /* Create a new op slot */
268     slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
269     assert(slot >= &slab2->opslab_slots);
270     if (DIFF(&slab2->opslab_slots, slot)
271          < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
272         slot = &slab2->opslab_slots;
273     INIT_OPSLOT;
274     DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", o, slab));
275     return (void *)o;
276 }
277
278 #undef INIT_OPSLOT
279
280 #ifdef PERL_DEBUG_READONLY_OPS
281 void
282 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
283 {
284     PERL_ARGS_ASSERT_SLAB_TO_RO;
285
286     if (slab->opslab_readonly) return;
287     slab->opslab_readonly = 1;
288     for (; slab; slab = slab->opslab_next) {
289         /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
290                               (unsigned long) slab->opslab_size, slab));*/
291         if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
292             Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
293                              (unsigned long)slab->opslab_size, errno);
294     }
295 }
296
297 void
298 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
299 {
300     OPSLAB *slab2;
301
302     PERL_ARGS_ASSERT_SLAB_TO_RW;
303
304     if (!slab->opslab_readonly) return;
305     slab2 = slab;
306     for (; slab2; slab2 = slab2->opslab_next) {
307         /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
308                               (unsigned long) size, slab2));*/
309         if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
310                      PROT_READ|PROT_WRITE)) {
311             Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
312                              (unsigned long)slab2->opslab_size, errno);
313         }
314     }
315     slab->opslab_readonly = 0;
316 }
317
318 #else
319 #  define Slab_to_rw(op)    NOOP
320 #endif
321
322 /* This cannot possibly be right, but it was copied from the old slab
323    allocator, to which it was originally added, without explanation, in
324    commit 083fcd5. */
325 #ifdef NETWARE
326 #    define PerlMemShared PerlMem
327 #endif
328
329 void
330 Perl_Slab_Free(pTHX_ void *op)
331 {
332     dVAR;
333     OP * const o = (OP *)op;
334     OPSLAB *slab;
335
336     PERL_ARGS_ASSERT_SLAB_FREE;
337
338     if (!o->op_slabbed) {
339         if (!o->op_static)
340             PerlMemShared_free(op);
341         return;
342     }
343
344     slab = OpSLAB(o);
345     /* If this op is already freed, our refcount will get screwy. */
346     assert(o->op_type != OP_FREED);
347     o->op_type = OP_FREED;
348     o->op_next = slab->opslab_freed;
349     slab->opslab_freed = o;
350     DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", o, slab));
351     OpslabREFCNT_dec_padok(slab);
352 }
353
354 void
355 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
356 {
357     dVAR;
358     const bool havepad = !!PL_comppad;
359     PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
360     if (havepad) {
361         ENTER;
362         PAD_SAVE_SETNULLPAD();
363     }
364     opslab_free(slab);
365     if (havepad) LEAVE;
366 }
367
368 void
369 Perl_opslab_free(pTHX_ OPSLAB *slab)
370 {
371     dVAR;
372     OPSLAB *slab2;
373     PERL_ARGS_ASSERT_OPSLAB_FREE;
374     DEBUG_S_warn((aTHX_ "freeing slab %p", slab));
375     assert(slab->opslab_refcnt == 1);
376     for (; slab; slab = slab2) {
377         slab2 = slab->opslab_next;
378 #ifdef DEBUGGING
379         slab->opslab_refcnt = ~(size_t)0;
380 #endif
381 #ifdef PERL_DEBUG_READONLY_OPS
382         DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
383                                                slab));
384         if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
385             perror("munmap failed");
386             abort();
387         }
388 #else
389         PerlMemShared_free(slab);
390 #endif
391     }
392 }
393
394 void
395 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
396 {
397     OPSLAB *slab2;
398     OPSLOT *slot;
399 #ifdef DEBUGGING
400     size_t savestack_count = 0;
401 #endif
402     PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
403     slab2 = slab;
404     do {
405         for (slot = slab2->opslab_first;
406              slot->opslot_next;
407              slot = slot->opslot_next) {
408             if (slot->opslot_op.op_type != OP_FREED
409              && !(slot->opslot_op.op_savefree
410 #ifdef DEBUGGING
411                   && ++savestack_count
412 #endif
413                  )
414             ) {
415                 assert(slot->opslot_op.op_slabbed);
416                 op_free(&slot->opslot_op);
417                 if (slab->opslab_refcnt == 1) goto free;
418             }
419         }
420     } while ((slab2 = slab2->opslab_next));
421     /* > 1 because the CV still holds a reference count. */
422     if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
423 #ifdef DEBUGGING
424         assert(savestack_count == slab->opslab_refcnt-1);
425 #endif
426         /* Remove the CV’s reference count. */
427         slab->opslab_refcnt--;
428         return;
429     }
430    free:
431     opslab_free(slab);
432 }
433
434 #ifdef PERL_DEBUG_READONLY_OPS
435 OP *
436 Perl_op_refcnt_inc(pTHX_ OP *o)
437 {
438     if(o) {
439         OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
440         if (slab && slab->opslab_readonly) {
441             Slab_to_rw(slab);
442             ++o->op_targ;
443             Slab_to_ro(slab);
444         } else {
445             ++o->op_targ;
446         }
447     }
448     return o;
449
450 }
451
452 PADOFFSET
453 Perl_op_refcnt_dec(pTHX_ OP *o)
454 {
455     PADOFFSET result;
456     OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
457
458     PERL_ARGS_ASSERT_OP_REFCNT_DEC;
459
460     if (slab && slab->opslab_readonly) {
461         Slab_to_rw(slab);
462         result = --o->op_targ;
463         Slab_to_ro(slab);
464     } else {
465         result = --o->op_targ;
466     }
467     return result;
468 }
469 #endif
470 /*
471  * In the following definition, the ", (OP*)0" is just to make the compiler
472  * think the expression is of the right type: croak actually does a Siglongjmp.
473  */
474 #define CHECKOP(type,o) \
475     ((PL_op_mask && PL_op_mask[type])                           \
476      ? ( op_free((OP*)o),                                       \
477          Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]),  \
478          (OP*)0 )                                               \
479      : PL_check[type](aTHX_ (OP*)o))
480
481 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
482
483 #define CHANGE_TYPE(o,type) \
484     STMT_START {                                \
485         o->op_type = (OPCODE)type;              \
486         o->op_ppaddr = PL_ppaddr[type];         \
487     } STMT_END
488
489 STATIC SV*
490 S_gv_ename(pTHX_ GV *gv)
491 {
492     SV* const tmpsv = sv_newmortal();
493
494     PERL_ARGS_ASSERT_GV_ENAME;
495
496     gv_efullname3(tmpsv, gv, NULL);
497     return tmpsv;
498 }
499
500 STATIC OP *
501 S_no_fh_allowed(pTHX_ OP *o)
502 {
503     PERL_ARGS_ASSERT_NO_FH_ALLOWED;
504
505     yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
506                  OP_DESC(o)));
507     return o;
508 }
509
510 STATIC OP *
511 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
512 {
513     PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
514     yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
515                                     SvUTF8(namesv) | flags);
516     return o;
517 }
518
519 STATIC OP *
520 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
521 {
522     PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
523     yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
524     return o;
525 }
526  
527 STATIC OP *
528 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
529 {
530     PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
531
532     yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
533     return o;
534 }
535
536 STATIC OP *
537 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
538 {
539     PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
540
541     yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
542                 SvUTF8(namesv) | flags);
543     return o;
544 }
545
546 STATIC void
547 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
548 {
549     PERL_ARGS_ASSERT_BAD_TYPE_PV;
550
551     yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
552                  (int)n, name, t, OP_DESC(kid)), flags);
553 }
554
555 STATIC void
556 S_bad_type_gv(pTHX_ I32 n, const char *t, GV *gv, U32 flags, const OP *kid)
557 {
558     SV * const namesv = gv_ename(gv);
559     PERL_ARGS_ASSERT_BAD_TYPE_GV;
560  
561     yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
562                  (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
563 }
564
565 STATIC void
566 S_no_bareword_allowed(pTHX_ OP *o)
567 {
568     PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
569
570     if (PL_madskills)
571         return;         /* various ok barewords are hidden in extra OP_NULL */
572     qerror(Perl_mess(aTHX_
573                      "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
574                      SVfARG(cSVOPo_sv)));
575     o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
576 }
577
578 /* "register" allocation */
579
580 PADOFFSET
581 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
582 {
583     dVAR;
584     PADOFFSET off;
585     const bool is_our = (PL_parser->in_my == KEY_our);
586
587     PERL_ARGS_ASSERT_ALLOCMY;
588
589     if (flags & ~SVf_UTF8)
590         Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
591                    (UV)flags);
592
593     /* Until we're using the length for real, cross check that we're being
594        told the truth.  */
595     assert(strlen(name) == len);
596
597     /* complain about "my $<special_var>" etc etc */
598     if (len &&
599         !(is_our ||
600           isALPHA(name[1]) ||
601           ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
602           (name[1] == '_' && (*name == '$' || len > 2))))
603     {
604         /* name[2] is true if strlen(name) > 2  */
605         if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
606          && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
607             yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
608                               name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
609                               PL_parser->in_my == KEY_state ? "state" : "my"));
610         } else {
611             yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
612                               PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
613         }
614     }
615     else if (len == 2 && name[1] == '_' && !is_our)
616         /* diag_listed_as: Use of my $_ is experimental */
617         Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
618                               "Use of %s $_ is experimental",
619                                PL_parser->in_my == KEY_state
620                                  ? "state"
621                                  : "my");
622
623     /* allocate a spare slot and store the name in that slot */
624
625     off = pad_add_name_pvn(name, len,
626                        (is_our ? padadd_OUR :
627                         PL_parser->in_my == KEY_state ? padadd_STATE : 0)
628                             | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
629                     PL_parser->in_my_stash,
630                     (is_our
631                         /* $_ is always in main::, even with our */
632                         ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
633                         : NULL
634                     )
635     );
636     /* anon sub prototypes contains state vars should always be cloned,
637      * otherwise the state var would be shared between anon subs */
638
639     if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
640         CvCLONE_on(PL_compcv);
641
642     return off;
643 }
644
645 /*
646 =for apidoc alloccopstash
647
648 Available only under threaded builds, this function allocates an entry in
649 C<PL_stashpad> for the stash passed to it.
650
651 =cut
652 */
653
654 #ifdef USE_ITHREADS
655 PADOFFSET
656 Perl_alloccopstash(pTHX_ HV *hv)
657 {
658     PADOFFSET off = 0, o = 1;
659     bool found_slot = FALSE;
660
661     PERL_ARGS_ASSERT_ALLOCCOPSTASH;
662
663     if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
664
665     for (; o < PL_stashpadmax; ++o) {
666         if (PL_stashpad[o] == hv) return PL_stashpadix = o;
667         if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
668             found_slot = TRUE, off = o;
669     }
670     if (!found_slot) {
671         Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
672         Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
673         off = PL_stashpadmax;
674         PL_stashpadmax += 10;
675     }
676
677     PL_stashpad[PL_stashpadix = off] = hv;
678     return off;
679 }
680 #endif
681
682 /* free the body of an op without examining its contents.
683  * Always use this rather than FreeOp directly */
684
685 static void
686 S_op_destroy(pTHX_ OP *o)
687 {
688     FreeOp(o);
689 }
690
691 /* Destructor */
692
693 /*
694 =for apidoc Am|void|op_free|OP *o
695
696 Free an op.  Only use this when an op is no longer linked to from any
697 optree.
698
699 =cut
700 */
701
702 void
703 Perl_op_free(pTHX_ OP *o)
704 {
705     dVAR;
706     OPCODE type;
707
708     /* Though ops may be freed twice, freeing the op after its slab is a
709        big no-no. */
710     assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0); 
711     /* During the forced freeing of ops after compilation failure, kidops
712        may be freed before their parents. */
713     if (!o || o->op_type == OP_FREED)
714         return;
715
716     type = o->op_type;
717     if (o->op_private & OPpREFCOUNTED) {
718         switch (type) {
719         case OP_LEAVESUB:
720         case OP_LEAVESUBLV:
721         case OP_LEAVEEVAL:
722         case OP_LEAVE:
723         case OP_SCOPE:
724         case OP_LEAVEWRITE:
725             {
726             PADOFFSET refcnt;
727             OP_REFCNT_LOCK;
728             refcnt = OpREFCNT_dec(o);
729             OP_REFCNT_UNLOCK;
730             if (refcnt) {
731                 /* Need to find and remove any pattern match ops from the list
732                    we maintain for reset().  */
733                 find_and_forget_pmops(o);
734                 return;
735             }
736             }
737             break;
738         default:
739             break;
740         }
741     }
742
743     /* Call the op_free hook if it has been set. Do it now so that it's called
744      * at the right time for refcounted ops, but still before all of the kids
745      * are freed. */
746     CALL_OPFREEHOOK(o);
747
748     if (o->op_flags & OPf_KIDS) {
749         OP *kid, *nextkid;
750         for (kid = cUNOPo->op_first; kid; kid = nextkid) {
751             nextkid = kid->op_sibling; /* Get before next freeing kid */
752             op_free(kid);
753         }
754     }
755     if (type == OP_NULL)
756         type = (OPCODE)o->op_targ;
757
758     if (o->op_slabbed)
759         Slab_to_rw(OpSLAB(o));
760
761     /* COP* is not cleared by op_clear() so that we may track line
762      * numbers etc even after null() */
763     if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
764         cop_free((COP*)o);
765     }
766
767     op_clear(o);
768     FreeOp(o);
769 #ifdef DEBUG_LEAKING_SCALARS
770     if (PL_op == o)
771         PL_op = NULL;
772 #endif
773 }
774
775 void
776 Perl_op_clear(pTHX_ OP *o)
777 {
778
779     dVAR;
780
781     PERL_ARGS_ASSERT_OP_CLEAR;
782
783 #ifdef PERL_MAD
784     mad_free(o->op_madprop);
785     o->op_madprop = 0;
786 #endif    
787
788  retry:
789     switch (o->op_type) {
790     case OP_NULL:       /* Was holding old type, if any. */
791         if (PL_madskills && o->op_targ != OP_NULL) {
792             o->op_type = (Optype)o->op_targ;
793             o->op_targ = 0;
794             goto retry;
795         }
796     case OP_ENTERTRY:
797     case OP_ENTEREVAL:  /* Was holding hints. */
798         o->op_targ = 0;
799         break;
800     default:
801         if (!(o->op_flags & OPf_REF)
802             || (PL_check[o->op_type] != Perl_ck_ftst))
803             break;
804         /* FALL THROUGH */
805     case OP_GVSV:
806     case OP_GV:
807     case OP_AELEMFAST:
808         {
809             GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
810 #ifdef USE_ITHREADS
811                         && PL_curpad
812 #endif
813                         ? cGVOPo_gv : NULL;
814             /* It's possible during global destruction that the GV is freed
815                before the optree. Whilst the SvREFCNT_inc is happy to bump from
816                0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
817                will trigger an assertion failure, because the entry to sv_clear
818                checks that the scalar is not already freed.  A check of for
819                !SvIS_FREED(gv) turns out to be invalid, because during global
820                destruction the reference count can be forced down to zero
821                (with SVf_BREAK set).  In which case raising to 1 and then
822                dropping to 0 triggers cleanup before it should happen.  I
823                *think* that this might actually be a general, systematic,
824                weakness of the whole idea of SVf_BREAK, in that code *is*
825                allowed to raise and lower references during global destruction,
826                so any *valid* code that happens to do this during global
827                destruction might well trigger premature cleanup.  */
828             bool still_valid = gv && SvREFCNT(gv);
829
830             if (still_valid)
831                 SvREFCNT_inc_simple_void(gv);
832 #ifdef USE_ITHREADS
833             if (cPADOPo->op_padix > 0) {
834                 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
835                  * may still exist on the pad */
836                 pad_swipe(cPADOPo->op_padix, TRUE);
837                 cPADOPo->op_padix = 0;
838             }
839 #else
840             SvREFCNT_dec(cSVOPo->op_sv);
841             cSVOPo->op_sv = NULL;
842 #endif
843             if (still_valid) {
844                 int try_downgrade = SvREFCNT(gv) == 2;
845                 SvREFCNT_dec_NN(gv);
846                 if (try_downgrade)
847                     gv_try_downgrade(gv);
848             }
849         }
850         break;
851     case OP_METHOD_NAMED:
852     case OP_CONST:
853     case OP_HINTSEVAL:
854         SvREFCNT_dec(cSVOPo->op_sv);
855         cSVOPo->op_sv = NULL;
856 #ifdef USE_ITHREADS
857         /** Bug #15654
858           Even if op_clear does a pad_free for the target of the op,
859           pad_free doesn't actually remove the sv that exists in the pad;
860           instead it lives on. This results in that it could be reused as 
861           a target later on when the pad was reallocated.
862         **/
863         if(o->op_targ) {
864           pad_swipe(o->op_targ,1);
865           o->op_targ = 0;
866         }
867 #endif
868         break;
869     case OP_DUMP:
870     case OP_GOTO:
871     case OP_NEXT:
872     case OP_LAST:
873     case OP_REDO:
874         if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
875             break;
876         /* FALL THROUGH */
877     case OP_TRANS:
878     case OP_TRANSR:
879         if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
880             assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
881 #ifdef USE_ITHREADS
882             if (cPADOPo->op_padix > 0) {
883                 pad_swipe(cPADOPo->op_padix, TRUE);
884                 cPADOPo->op_padix = 0;
885             }
886 #else
887             SvREFCNT_dec(cSVOPo->op_sv);
888             cSVOPo->op_sv = NULL;
889 #endif
890         }
891         else {
892             PerlMemShared_free(cPVOPo->op_pv);
893             cPVOPo->op_pv = NULL;
894         }
895         break;
896     case OP_SUBST:
897         op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
898         goto clear_pmop;
899     case OP_PUSHRE:
900 #ifdef USE_ITHREADS
901         if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
902             /* No GvIN_PAD_off here, because other references may still
903              * exist on the pad */
904             pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
905         }
906 #else
907         SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
908 #endif
909         /* FALL THROUGH */
910     case OP_MATCH:
911     case OP_QR:
912 clear_pmop:
913         if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
914             op_free(cPMOPo->op_code_list);
915         cPMOPo->op_code_list = NULL;
916         forget_pmop(cPMOPo);
917         cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
918         /* we use the same protection as the "SAFE" version of the PM_ macros
919          * here since sv_clean_all might release some PMOPs
920          * after PL_regex_padav has been cleared
921          * and the clearing of PL_regex_padav needs to
922          * happen before sv_clean_all
923          */
924 #ifdef USE_ITHREADS
925         if(PL_regex_pad) {        /* We could be in destruction */
926             const IV offset = (cPMOPo)->op_pmoffset;
927             ReREFCNT_dec(PM_GETRE(cPMOPo));
928             PL_regex_pad[offset] = &PL_sv_undef;
929             sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
930                            sizeof(offset));
931         }
932 #else
933         ReREFCNT_dec(PM_GETRE(cPMOPo));
934         PM_SETRE(cPMOPo, NULL);
935 #endif
936
937         break;
938     }
939
940     if (o->op_targ > 0) {
941         pad_free(o->op_targ);
942         o->op_targ = 0;
943     }
944 }
945
946 STATIC void
947 S_cop_free(pTHX_ COP* cop)
948 {
949     PERL_ARGS_ASSERT_COP_FREE;
950
951     CopFILE_free(cop);
952     if (! specialWARN(cop->cop_warnings))
953         PerlMemShared_free(cop->cop_warnings);
954     cophh_free(CopHINTHASH_get(cop));
955     if (PL_curcop == cop)
956        PL_curcop = NULL;
957 }
958
959 STATIC void
960 S_forget_pmop(pTHX_ PMOP *const o
961               )
962 {
963     HV * const pmstash = PmopSTASH(o);
964
965     PERL_ARGS_ASSERT_FORGET_PMOP;
966
967     if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
968         MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
969         if (mg) {
970             PMOP **const array = (PMOP**) mg->mg_ptr;
971             U32 count = mg->mg_len / sizeof(PMOP**);
972             U32 i = count;
973
974             while (i--) {
975                 if (array[i] == o) {
976                     /* Found it. Move the entry at the end to overwrite it.  */
977                     array[i] = array[--count];
978                     mg->mg_len = count * sizeof(PMOP**);
979                     /* Could realloc smaller at this point always, but probably
980                        not worth it. Probably worth free()ing if we're the
981                        last.  */
982                     if(!count) {
983                         Safefree(mg->mg_ptr);
984                         mg->mg_ptr = NULL;
985                     }
986                     break;
987                 }
988             }
989         }
990     }
991     if (PL_curpm == o) 
992         PL_curpm = NULL;
993 }
994
995 STATIC void
996 S_find_and_forget_pmops(pTHX_ OP *o)
997 {
998     PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
999
1000     if (o->op_flags & OPf_KIDS) {
1001         OP *kid = cUNOPo->op_first;
1002         while (kid) {
1003             switch (kid->op_type) {
1004             case OP_SUBST:
1005             case OP_PUSHRE:
1006             case OP_MATCH:
1007             case OP_QR:
1008                 forget_pmop((PMOP*)kid);
1009             }
1010             find_and_forget_pmops(kid);
1011             kid = kid->op_sibling;
1012         }
1013     }
1014 }
1015
1016 /*
1017 =for apidoc Am|void|op_null|OP *o
1018
1019 Neutralizes an op when it is no longer needed, but is still linked to from
1020 other ops.
1021
1022 =cut
1023 */
1024
1025 void
1026 Perl_op_null(pTHX_ OP *o)
1027 {
1028     dVAR;
1029
1030     PERL_ARGS_ASSERT_OP_NULL;
1031
1032     if (o->op_type == OP_NULL)
1033         return;
1034     if (!PL_madskills)
1035         op_clear(o);
1036     o->op_targ = o->op_type;
1037     o->op_type = OP_NULL;
1038     o->op_ppaddr = PL_ppaddr[OP_NULL];
1039 }
1040
1041 void
1042 Perl_op_refcnt_lock(pTHX)
1043 {
1044     dVAR;
1045     PERL_UNUSED_CONTEXT;
1046     OP_REFCNT_LOCK;
1047 }
1048
1049 void
1050 Perl_op_refcnt_unlock(pTHX)
1051 {
1052     dVAR;
1053     PERL_UNUSED_CONTEXT;
1054     OP_REFCNT_UNLOCK;
1055 }
1056
1057 /* Contextualizers */
1058
1059 /*
1060 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1061
1062 Applies a syntactic context to an op tree representing an expression.
1063 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1064 or C<G_VOID> to specify the context to apply.  The modified op tree
1065 is returned.
1066
1067 =cut
1068 */
1069
1070 OP *
1071 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1072 {
1073     PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1074     switch (context) {
1075         case G_SCALAR: return scalar(o);
1076         case G_ARRAY:  return list(o);
1077         case G_VOID:   return scalarvoid(o);
1078         default:
1079             Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1080                        (long) context);
1081             return o;
1082     }
1083 }
1084
1085 /*
1086 =head1 Optree Manipulation Functions
1087
1088 =for apidoc Am|OP*|op_linklist|OP *o
1089 This function is the implementation of the L</LINKLIST> macro.  It should
1090 not be called directly.
1091
1092 =cut
1093 */
1094
1095 OP *
1096 Perl_op_linklist(pTHX_ OP *o)
1097 {
1098     OP *first;
1099
1100     PERL_ARGS_ASSERT_OP_LINKLIST;
1101
1102     if (o->op_next)
1103         return o->op_next;
1104
1105     /* establish postfix order */
1106     first = cUNOPo->op_first;
1107     if (first) {
1108         OP *kid;
1109         o->op_next = LINKLIST(first);
1110         kid = first;
1111         for (;;) {
1112             if (kid->op_sibling) {
1113                 kid->op_next = LINKLIST(kid->op_sibling);
1114                 kid = kid->op_sibling;
1115             } else {
1116                 kid->op_next = o;
1117                 break;
1118             }
1119         }
1120     }
1121     else
1122         o->op_next = o;
1123
1124     return o->op_next;
1125 }
1126
1127 static OP *
1128 S_scalarkids(pTHX_ OP *o)
1129 {
1130     if (o && o->op_flags & OPf_KIDS) {
1131         OP *kid;
1132         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1133             scalar(kid);
1134     }
1135     return o;
1136 }
1137
1138 STATIC OP *
1139 S_scalarboolean(pTHX_ OP *o)
1140 {
1141     dVAR;
1142
1143     PERL_ARGS_ASSERT_SCALARBOOLEAN;
1144
1145     if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1146      && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1147         if (ckWARN(WARN_SYNTAX)) {
1148             const line_t oldline = CopLINE(PL_curcop);
1149
1150             if (PL_parser && PL_parser->copline != NOLINE) {
1151                 /* This ensures that warnings are reported at the first line
1152                    of the conditional, not the last.  */
1153                 CopLINE_set(PL_curcop, PL_parser->copline);
1154             }
1155             Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1156             CopLINE_set(PL_curcop, oldline);
1157         }
1158     }
1159     return scalar(o);
1160 }
1161
1162 static SV *
1163 S_op_varname(pTHX_ const OP *o)
1164 {
1165     assert(o);
1166     assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1167            o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1168     {
1169         const char funny  = o->op_type == OP_PADAV
1170                          || o->op_type == OP_RV2AV ? '@' : '%';
1171         if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1172             GV *gv;
1173             if (cUNOPo->op_first->op_type != OP_GV
1174              || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1175                 return NULL;
1176             return varname(gv, funny, 0, NULL, 0, 1);
1177         }
1178         return
1179             varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1180     }
1181 }
1182
1183 static void
1184 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1185 { /* or not so pretty :-) */
1186     if (o->op_type == OP_CONST) {
1187         *retsv = cSVOPo_sv;
1188         if (SvPOK(*retsv)) {
1189             SV *sv = *retsv;
1190             *retsv = sv_newmortal();
1191             pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1192                       PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1193         }
1194         else if (!SvOK(*retsv))
1195             *retpv = "undef";
1196     }
1197     else *retpv = "...";
1198 }
1199
1200 static void
1201 S_scalar_slice_warning(pTHX_ const OP *o)
1202 {
1203     OP *kid;
1204     const char lbrack =
1205         o->op_type == OP_HSLICE ? '{' : '[';
1206     const char rbrack =
1207         o->op_type == OP_HSLICE ? '}' : ']';
1208     SV *name;
1209     SV *keysv = NULL; /* just to silence compiler warnings */
1210     const char *key = NULL;
1211
1212     if (!(o->op_private & OPpSLICEWARNING))
1213         return;
1214     if (PL_parser && PL_parser->error_count)
1215         /* This warning can be nonsensical when there is a syntax error. */
1216         return;
1217
1218     kid = cLISTOPo->op_first;
1219     kid = kid->op_sibling; /* get past pushmark */
1220     /* weed out false positives: any ops that can return lists */
1221     switch (kid->op_type) {
1222     case OP_BACKTICK:
1223     case OP_GLOB:
1224     case OP_READLINE:
1225     case OP_MATCH:
1226     case OP_RV2AV:
1227     case OP_EACH:
1228     case OP_VALUES:
1229     case OP_KEYS:
1230     case OP_SPLIT:
1231     case OP_LIST:
1232     case OP_SORT:
1233     case OP_REVERSE:
1234     case OP_ENTERSUB:
1235     case OP_CALLER:
1236     case OP_LSTAT:
1237     case OP_STAT:
1238     case OP_READDIR:
1239     case OP_SYSTEM:
1240     case OP_TMS:
1241     case OP_LOCALTIME:
1242     case OP_GMTIME:
1243     case OP_ENTEREVAL:
1244     case OP_REACH:
1245     case OP_RKEYS:
1246     case OP_RVALUES:
1247         return;
1248     }
1249
1250     /* Don't warn if we have a nulled list either. */
1251     if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1252         return;
1253
1254     assert(kid->op_sibling);
1255     name = S_op_varname(aTHX_ kid->op_sibling);
1256     if (!name) /* XS module fiddling with the op tree */
1257         return;
1258     S_op_pretty(aTHX_ kid, &keysv, &key);
1259     assert(SvPOK(name));
1260     sv_chop(name,SvPVX(name)+1);
1261     if (key)
1262        /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1263         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1264                    "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1265                    "%c%s%c",
1266                     SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1267                     lbrack, key, rbrack);
1268     else
1269        /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1270         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1271                    "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1272                     SVf"%c%"SVf"%c",
1273                     SVfARG(name), lbrack, keysv, rbrack,
1274                     SVfARG(name), lbrack, keysv, rbrack);
1275 }
1276
1277 OP *
1278 Perl_scalar(pTHX_ OP *o)
1279 {
1280     dVAR;
1281     OP *kid;
1282
1283     /* assumes no premature commitment */
1284     if (!o || (PL_parser && PL_parser->error_count)
1285          || (o->op_flags & OPf_WANT)
1286          || o->op_type == OP_RETURN)
1287     {
1288         return o;
1289     }
1290
1291     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1292
1293     switch (o->op_type) {
1294     case OP_REPEAT:
1295         scalar(cBINOPo->op_first);
1296         break;
1297     case OP_OR:
1298     case OP_AND:
1299     case OP_COND_EXPR:
1300         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1301             scalar(kid);
1302         break;
1303         /* FALL THROUGH */
1304     case OP_SPLIT:
1305     case OP_MATCH:
1306     case OP_QR:
1307     case OP_SUBST:
1308     case OP_NULL:
1309     default:
1310         if (o->op_flags & OPf_KIDS) {
1311             for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1312                 scalar(kid);
1313         }
1314         break;
1315     case OP_LEAVE:
1316     case OP_LEAVETRY:
1317         kid = cLISTOPo->op_first;
1318         scalar(kid);
1319         kid = kid->op_sibling;
1320     do_kids:
1321         while (kid) {
1322             OP *sib = kid->op_sibling;
1323             if (sib && kid->op_type != OP_LEAVEWHEN)
1324                 scalarvoid(kid);
1325             else
1326                 scalar(kid);
1327             kid = sib;
1328         }
1329         PL_curcop = &PL_compiling;
1330         break;
1331     case OP_SCOPE:
1332     case OP_LINESEQ:
1333     case OP_LIST:
1334         kid = cLISTOPo->op_first;
1335         goto do_kids;
1336     case OP_SORT:
1337         Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1338         break;
1339     case OP_KVHSLICE:
1340     case OP_KVASLICE:
1341     {
1342         /* Warn about scalar context */
1343         const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1344         const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1345         SV *name;
1346         SV *keysv;
1347         const char *key = NULL;
1348
1349         /* This warning can be nonsensical when there is a syntax error. */
1350         if (PL_parser && PL_parser->error_count)
1351             break;
1352
1353         if (!ckWARN(WARN_SYNTAX)) break;
1354
1355         kid = cLISTOPo->op_first;
1356         kid = kid->op_sibling; /* get past pushmark */
1357         assert(kid->op_sibling);
1358         name = S_op_varname(aTHX_ kid->op_sibling);
1359         if (!name) /* XS module fiddling with the op tree */
1360             break;
1361         S_op_pretty(aTHX_ kid, &keysv, &key);
1362         assert(SvPOK(name));
1363         sv_chop(name,SvPVX(name)+1);
1364         if (key)
1365   /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1366             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1367                        "%%%"SVf"%c%s%c in scalar context better written "
1368                        "as $%"SVf"%c%s%c",
1369                         SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1370                         lbrack, key, rbrack);
1371         else
1372   /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1373             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1374                        "%%%"SVf"%c%"SVf"%c in scalar context better "
1375                        "written as $%"SVf"%c%"SVf"%c",
1376                         SVfARG(name), lbrack, keysv, rbrack,
1377                         SVfARG(name), lbrack, keysv, rbrack);
1378     }
1379     }
1380     return o;
1381 }
1382
1383 OP *
1384 Perl_scalarvoid(pTHX_ OP *o)
1385 {
1386     dVAR;
1387     OP *kid;
1388     SV *useless_sv = NULL;
1389     const char* useless = NULL;
1390     SV* sv;
1391     U8 want;
1392
1393     PERL_ARGS_ASSERT_SCALARVOID;
1394
1395     /* trailing mad null ops don't count as "there" for void processing */
1396     if (PL_madskills &&
1397         o->op_type != OP_NULL &&
1398         o->op_sibling &&
1399         o->op_sibling->op_type == OP_NULL)
1400     {
1401         OP *sib;
1402         for (sib = o->op_sibling;
1403                 sib && sib->op_type == OP_NULL;
1404                 sib = sib->op_sibling) ;
1405         
1406         if (!sib)
1407             return o;
1408     }
1409
1410     if (o->op_type == OP_NEXTSTATE
1411         || o->op_type == OP_DBSTATE
1412         || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1413                                       || o->op_targ == OP_DBSTATE)))
1414         PL_curcop = (COP*)o;            /* for warning below */
1415
1416     /* assumes no premature commitment */
1417     want = o->op_flags & OPf_WANT;
1418     if ((want && want != OPf_WANT_SCALAR)
1419          || (PL_parser && PL_parser->error_count)
1420          || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1421     {
1422         return o;
1423     }
1424
1425     if ((o->op_private & OPpTARGET_MY)
1426         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1427     {
1428         return scalar(o);                       /* As if inside SASSIGN */
1429     }
1430
1431     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1432
1433     switch (o->op_type) {
1434     default:
1435         if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1436             break;
1437         /* FALL THROUGH */
1438     case OP_REPEAT:
1439         if (o->op_flags & OPf_STACKED)
1440             break;
1441         goto func_ops;
1442     case OP_SUBSTR:
1443         if (o->op_private == 4)
1444             break;
1445         /* FALL THROUGH */
1446     case OP_GVSV:
1447     case OP_WANTARRAY:
1448     case OP_GV:
1449     case OP_SMARTMATCH:
1450     case OP_PADSV:
1451     case OP_PADAV:
1452     case OP_PADHV:
1453     case OP_PADANY:
1454     case OP_AV2ARYLEN:
1455     case OP_REF:
1456     case OP_REFGEN:
1457     case OP_SREFGEN:
1458     case OP_DEFINED:
1459     case OP_HEX:
1460     case OP_OCT:
1461     case OP_LENGTH:
1462     case OP_VEC:
1463     case OP_INDEX:
1464     case OP_RINDEX:
1465     case OP_SPRINTF:
1466     case OP_AELEM:
1467     case OP_AELEMFAST:
1468     case OP_AELEMFAST_LEX:
1469     case OP_ASLICE:
1470     case OP_KVASLICE:
1471     case OP_HELEM:
1472     case OP_HSLICE:
1473     case OP_KVHSLICE:
1474     case OP_UNPACK:
1475     case OP_PACK:
1476     case OP_JOIN:
1477     case OP_LSLICE:
1478     case OP_ANONLIST:
1479     case OP_ANONHASH:
1480     case OP_SORT:
1481     case OP_REVERSE:
1482     case OP_RANGE:
1483     case OP_FLIP:
1484     case OP_FLOP:
1485     case OP_CALLER:
1486     case OP_FILENO:
1487     case OP_EOF:
1488     case OP_TELL:
1489     case OP_GETSOCKNAME:
1490     case OP_GETPEERNAME:
1491     case OP_READLINK:
1492     case OP_TELLDIR:
1493     case OP_GETPPID:
1494     case OP_GETPGRP:
1495     case OP_GETPRIORITY:
1496     case OP_TIME:
1497     case OP_TMS:
1498     case OP_LOCALTIME:
1499     case OP_GMTIME:
1500     case OP_GHBYNAME:
1501     case OP_GHBYADDR:
1502     case OP_GHOSTENT:
1503     case OP_GNBYNAME:
1504     case OP_GNBYADDR:
1505     case OP_GNETENT:
1506     case OP_GPBYNAME:
1507     case OP_GPBYNUMBER:
1508     case OP_GPROTOENT:
1509     case OP_GSBYNAME:
1510     case OP_GSBYPORT:
1511     case OP_GSERVENT:
1512     case OP_GPWNAM:
1513     case OP_GPWUID:
1514     case OP_GGRNAM:
1515     case OP_GGRGID:
1516     case OP_GETLOGIN:
1517     case OP_PROTOTYPE:
1518     case OP_RUNCV:
1519       func_ops:
1520         if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1521             /* Otherwise it's "Useless use of grep iterator" */
1522             useless = OP_DESC(o);
1523         break;
1524
1525     case OP_SPLIT:
1526         kid = cLISTOPo->op_first;
1527         if (kid && kid->op_type == OP_PUSHRE
1528 #ifdef USE_ITHREADS
1529                 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1530 #else
1531                 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1532 #endif
1533             useless = OP_DESC(o);
1534         break;
1535
1536     case OP_NOT:
1537        kid = cUNOPo->op_first;
1538        if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1539            kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1540                 goto func_ops;
1541        }
1542        useless = "negative pattern binding (!~)";
1543        break;
1544
1545     case OP_SUBST:
1546         if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1547             useless = "non-destructive substitution (s///r)";
1548         break;
1549
1550     case OP_TRANSR:
1551         useless = "non-destructive transliteration (tr///r)";
1552         break;
1553
1554     case OP_RV2GV:
1555     case OP_RV2SV:
1556     case OP_RV2AV:
1557     case OP_RV2HV:
1558         if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1559                 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1560             useless = "a variable";
1561         break;
1562
1563     case OP_CONST:
1564         sv = cSVOPo_sv;
1565         if (cSVOPo->op_private & OPpCONST_STRICT)
1566             no_bareword_allowed(o);
1567         else {
1568             if (ckWARN(WARN_VOID)) {
1569                 /* don't warn on optimised away booleans, eg 
1570                  * use constant Foo, 5; Foo || print; */
1571                 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1572                     useless = NULL;
1573                 /* the constants 0 and 1 are permitted as they are
1574                    conventionally used as dummies in constructs like
1575                         1 while some_condition_with_side_effects;  */
1576                 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1577                     useless = NULL;
1578                 else if (SvPOK(sv)) {
1579                     SV * const dsv = newSVpvs("");
1580                     useless_sv
1581                         = Perl_newSVpvf(aTHX_
1582                                         "a constant (%s)",
1583                                         pv_pretty(dsv, SvPVX_const(sv),
1584                                                   SvCUR(sv), 32, NULL, NULL,
1585                                                   PERL_PV_PRETTY_DUMP
1586                                                   | PERL_PV_ESCAPE_NOCLEAR
1587                                                   | PERL_PV_ESCAPE_UNI_DETECT));
1588                     SvREFCNT_dec_NN(dsv);
1589                 }
1590                 else if (SvOK(sv)) {
1591                     useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", sv);
1592                 }
1593                 else
1594                     useless = "a constant (undef)";
1595             }
1596         }
1597         op_null(o);             /* don't execute or even remember it */
1598         break;
1599
1600     case OP_POSTINC:
1601         o->op_type = OP_PREINC;         /* pre-increment is faster */
1602         o->op_ppaddr = PL_ppaddr[OP_PREINC];
1603         break;
1604
1605     case OP_POSTDEC:
1606         o->op_type = OP_PREDEC;         /* pre-decrement is faster */
1607         o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1608         break;
1609
1610     case OP_I_POSTINC:
1611         o->op_type = OP_I_PREINC;       /* pre-increment is faster */
1612         o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1613         break;
1614
1615     case OP_I_POSTDEC:
1616         o->op_type = OP_I_PREDEC;       /* pre-decrement is faster */
1617         o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1618         break;
1619
1620     case OP_SASSIGN: {
1621         OP *rv2gv;
1622         UNOP *refgen, *rv2cv;
1623         LISTOP *exlist;
1624
1625         if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1626             break;
1627
1628         rv2gv = ((BINOP *)o)->op_last;
1629         if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1630             break;
1631
1632         refgen = (UNOP *)((BINOP *)o)->op_first;
1633
1634         if (!refgen || refgen->op_type != OP_REFGEN)
1635             break;
1636
1637         exlist = (LISTOP *)refgen->op_first;
1638         if (!exlist || exlist->op_type != OP_NULL
1639             || exlist->op_targ != OP_LIST)
1640             break;
1641
1642         if (exlist->op_first->op_type != OP_PUSHMARK)
1643             break;
1644
1645         rv2cv = (UNOP*)exlist->op_last;
1646
1647         if (rv2cv->op_type != OP_RV2CV)
1648             break;
1649
1650         assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1651         assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1652         assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1653
1654         o->op_private |= OPpASSIGN_CV_TO_GV;
1655         rv2gv->op_private |= OPpDONT_INIT_GV;
1656         rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1657
1658         break;
1659     }
1660
1661     case OP_AASSIGN: {
1662         inplace_aassign(o);
1663         break;
1664     }
1665
1666     case OP_OR:
1667     case OP_AND:
1668         kid = cLOGOPo->op_first;
1669         if (kid->op_type == OP_NOT
1670             && (kid->op_flags & OPf_KIDS)
1671             && !PL_madskills) {
1672             if (o->op_type == OP_AND) {
1673                 o->op_type = OP_OR;
1674                 o->op_ppaddr = PL_ppaddr[OP_OR];
1675             } else {
1676                 o->op_type = OP_AND;
1677                 o->op_ppaddr = PL_ppaddr[OP_AND];
1678             }
1679             op_null(kid);
1680         }
1681
1682     case OP_DOR:
1683     case OP_COND_EXPR:
1684     case OP_ENTERGIVEN:
1685     case OP_ENTERWHEN:
1686         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1687             scalarvoid(kid);
1688         break;
1689
1690     case OP_NULL:
1691         if (o->op_flags & OPf_STACKED)
1692             break;
1693         /* FALL THROUGH */
1694     case OP_NEXTSTATE:
1695     case OP_DBSTATE:
1696     case OP_ENTERTRY:
1697     case OP_ENTER:
1698         if (!(o->op_flags & OPf_KIDS))
1699             break;
1700         /* FALL THROUGH */
1701     case OP_SCOPE:
1702     case OP_LEAVE:
1703     case OP_LEAVETRY:
1704     case OP_LEAVELOOP:
1705     case OP_LINESEQ:
1706     case OP_LIST:
1707     case OP_LEAVEGIVEN:
1708     case OP_LEAVEWHEN:
1709         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1710             scalarvoid(kid);
1711         break;
1712     case OP_ENTEREVAL:
1713         scalarkids(o);
1714         break;
1715     case OP_SCALAR:
1716         return scalar(o);
1717     }
1718
1719     if (useless_sv) {
1720         /* mortalise it, in case warnings are fatal.  */
1721         Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1722                        "Useless use of %"SVf" in void context",
1723                        sv_2mortal(useless_sv));
1724     }
1725     else if (useless) {
1726        Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1727                       "Useless use of %s in void context",
1728                       useless);
1729     }
1730     return o;
1731 }
1732
1733 static OP *
1734 S_listkids(pTHX_ OP *o)
1735 {
1736     if (o && o->op_flags & OPf_KIDS) {
1737         OP *kid;
1738         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1739             list(kid);
1740     }
1741     return o;
1742 }
1743
1744 OP *
1745 Perl_list(pTHX_ OP *o)
1746 {
1747     dVAR;
1748     OP *kid;
1749
1750     /* assumes no premature commitment */
1751     if (!o || (o->op_flags & OPf_WANT)
1752          || (PL_parser && PL_parser->error_count)
1753          || o->op_type == OP_RETURN)
1754     {
1755         return o;
1756     }
1757
1758     if ((o->op_private & OPpTARGET_MY)
1759         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1760     {
1761         return o;                               /* As if inside SASSIGN */
1762     }
1763
1764     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1765
1766     switch (o->op_type) {
1767     case OP_FLOP:
1768     case OP_REPEAT:
1769         list(cBINOPo->op_first);
1770         break;
1771     case OP_OR:
1772     case OP_AND:
1773     case OP_COND_EXPR:
1774         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1775             list(kid);
1776         break;
1777     default:
1778     case OP_MATCH:
1779     case OP_QR:
1780     case OP_SUBST:
1781     case OP_NULL:
1782         if (!(o->op_flags & OPf_KIDS))
1783             break;
1784         if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1785             list(cBINOPo->op_first);
1786             return gen_constant_list(o);
1787         }
1788     case OP_LIST:
1789         listkids(o);
1790         break;
1791     case OP_LEAVE:
1792     case OP_LEAVETRY:
1793         kid = cLISTOPo->op_first;
1794         list(kid);
1795         kid = kid->op_sibling;
1796     do_kids:
1797         while (kid) {
1798             OP *sib = kid->op_sibling;
1799             if (sib && kid->op_type != OP_LEAVEWHEN)
1800                 scalarvoid(kid);
1801             else
1802                 list(kid);
1803             kid = sib;
1804         }
1805         PL_curcop = &PL_compiling;
1806         break;
1807     case OP_SCOPE:
1808     case OP_LINESEQ:
1809         kid = cLISTOPo->op_first;
1810         goto do_kids;
1811     }
1812     return o;
1813 }
1814
1815 static OP *
1816 S_scalarseq(pTHX_ OP *o)
1817 {
1818     dVAR;
1819     if (o) {
1820         const OPCODE type = o->op_type;
1821
1822         if (type == OP_LINESEQ || type == OP_SCOPE ||
1823             type == OP_LEAVE || type == OP_LEAVETRY)
1824         {
1825             OP *kid;
1826             for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1827                 if (kid->op_sibling) {
1828                     scalarvoid(kid);
1829                 }
1830             }
1831             PL_curcop = &PL_compiling;
1832         }
1833         o->op_flags &= ~OPf_PARENS;
1834         if (PL_hints & HINT_BLOCK_SCOPE)
1835             o->op_flags |= OPf_PARENS;
1836     }
1837     else
1838         o = newOP(OP_STUB, 0);
1839     return o;
1840 }
1841
1842 STATIC OP *
1843 S_modkids(pTHX_ OP *o, I32 type)
1844 {
1845     if (o && o->op_flags & OPf_KIDS) {
1846         OP *kid;
1847         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1848             op_lvalue(kid, type);
1849     }
1850     return o;
1851 }
1852
1853 /*
1854 =for apidoc finalize_optree
1855
1856 This function finalizes the optree.  Should be called directly after
1857 the complete optree is built.  It does some additional
1858 checking which can't be done in the normal ck_xxx functions and makes
1859 the tree thread-safe.
1860
1861 =cut
1862 */
1863 void
1864 Perl_finalize_optree(pTHX_ OP* o)
1865 {
1866     PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1867
1868     ENTER;
1869     SAVEVPTR(PL_curcop);
1870
1871     finalize_op(o);
1872
1873     LEAVE;
1874 }
1875
1876 STATIC void
1877 S_finalize_op(pTHX_ OP* o)
1878 {
1879     PERL_ARGS_ASSERT_FINALIZE_OP;
1880
1881 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1882     {
1883         /* Make sure mad ops are also thread-safe */
1884         MADPROP *mp = o->op_madprop;
1885         while (mp) {
1886             if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1887                 OP *prop_op = (OP *) mp->mad_val;
1888                 /* We only need "Relocate sv to the pad for thread safety.", but this
1889                    easiest way to make sure it traverses everything */
1890                 if (prop_op->op_type == OP_CONST)
1891                     cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1892                 finalize_op(prop_op);
1893             }
1894             mp = mp->mad_next;
1895         }
1896     }
1897 #endif
1898
1899     switch (o->op_type) {
1900     case OP_NEXTSTATE:
1901     case OP_DBSTATE:
1902         PL_curcop = ((COP*)o);          /* for warnings */
1903         break;
1904     case OP_EXEC:
1905         if ( o->op_sibling
1906             && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1907             && ckWARN(WARN_EXEC))
1908             {
1909                 if (o->op_sibling->op_sibling) {
1910                     const OPCODE type = o->op_sibling->op_sibling->op_type;
1911                     if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1912                         const line_t oldline = CopLINE(PL_curcop);
1913                         CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1914                         Perl_warner(aTHX_ packWARN(WARN_EXEC),
1915                             "Statement unlikely to be reached");
1916                         Perl_warner(aTHX_ packWARN(WARN_EXEC),
1917                             "\t(Maybe you meant system() when you said exec()?)\n");
1918                         CopLINE_set(PL_curcop, oldline);
1919                     }
1920                 }
1921             }
1922         break;
1923
1924     case OP_GV:
1925         if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1926             GV * const gv = cGVOPo_gv;
1927             if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1928                 /* XXX could check prototype here instead of just carping */
1929                 SV * const sv = sv_newmortal();
1930                 gv_efullname3(sv, gv, NULL);
1931                 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1932                     "%"SVf"() called too early to check prototype",
1933                     SVfARG(sv));
1934             }
1935         }
1936         break;
1937
1938     case OP_CONST:
1939         if (cSVOPo->op_private & OPpCONST_STRICT)
1940             no_bareword_allowed(o);
1941         /* FALLTHROUGH */
1942 #ifdef USE_ITHREADS
1943     case OP_HINTSEVAL:
1944     case OP_METHOD_NAMED:
1945         /* Relocate sv to the pad for thread safety.
1946          * Despite being a "constant", the SV is written to,
1947          * for reference counts, sv_upgrade() etc. */
1948         if (cSVOPo->op_sv) {
1949             const PADOFFSET ix = pad_alloc(OP_CONST, SVf_READONLY);
1950             SvREFCNT_dec(PAD_SVl(ix));
1951             PAD_SETSV(ix, cSVOPo->op_sv);
1952             /* XXX I don't know how this isn't readonly already. */
1953             if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
1954             cSVOPo->op_sv = NULL;
1955             o->op_targ = ix;
1956         }
1957 #endif
1958         break;
1959
1960     case OP_HELEM: {
1961         UNOP *rop;
1962         SV *lexname;
1963         GV **fields;
1964         SVOP *key_op;
1965         OP *kid;
1966         bool check_fields;
1967
1968         if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
1969             break;
1970
1971         rop = (UNOP*)((BINOP*)o)->op_first;
1972
1973         goto check_keys;
1974
1975     case OP_HSLICE:
1976         S_scalar_slice_warning(aTHX_ o);
1977
1978     case OP_KVHSLICE:
1979         kid = cLISTOPo->op_first->op_sibling;
1980         if (/* I bet there's always a pushmark... */
1981             OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
1982             && OP_TYPE_ISNT_NN(kid, OP_CONST))
1983         {
1984             break;
1985         }
1986
1987         key_op = (SVOP*)(kid->op_type == OP_CONST
1988                                 ? kid
1989                                 : kLISTOP->op_first->op_sibling);
1990
1991         rop = (UNOP*)((LISTOP*)o)->op_last;
1992
1993       check_keys:       
1994         if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
1995             rop = NULL;
1996         else if (rop->op_first->op_type == OP_PADSV)
1997             /* @$hash{qw(keys here)} */
1998             rop = (UNOP*)rop->op_first;
1999         else {
2000             /* @{$hash}{qw(keys here)} */
2001             if (rop->op_first->op_type == OP_SCOPE
2002                 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2003                 {
2004                     rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2005                 }
2006             else
2007                 rop = NULL;
2008         }
2009
2010         lexname = NULL; /* just to silence compiler warnings */
2011         fields  = NULL; /* just to silence compiler warnings */
2012
2013         check_fields =
2014             rop
2015          && (lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE),
2016              SvPAD_TYPED(lexname))
2017          && (fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE))
2018          && isGV(*fields) && GvHV(*fields);
2019         for (; key_op;
2020              key_op = (SVOP*)key_op->op_sibling) {
2021             SV **svp, *sv;
2022             if (key_op->op_type != OP_CONST)
2023                 continue;
2024             svp = cSVOPx_svp(key_op);
2025
2026             /* Make the CONST have a shared SV */
2027             if ((!SvIsCOW_shared_hash(sv = *svp))
2028              && SvTYPE(sv) < SVt_PVMG && SvOK(sv) && !SvROK(sv)) {
2029                 SSize_t keylen;
2030                 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2031                 SV *nsv = newSVpvn_share(key,
2032                                          SvUTF8(sv) ? -keylen : keylen, 0);
2033                 SvREFCNT_dec_NN(sv);
2034                 *svp = nsv;
2035             }
2036
2037             if (check_fields
2038              && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) {
2039                 Perl_croak(aTHX_ "No such class field \"%"SVf"\" " 
2040                            "in variable %"SVf" of type %"HEKf, 
2041                       SVfARG(*svp), SVfARG(lexname),
2042                       HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
2043             }
2044         }
2045         break;
2046     }
2047     case OP_ASLICE:
2048         S_scalar_slice_warning(aTHX_ o);
2049         break;
2050
2051     case OP_SUBST: {
2052         if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2053             finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2054         break;
2055     }
2056     default:
2057         break;
2058     }
2059
2060     if (o->op_flags & OPf_KIDS) {
2061         OP *kid;
2062         for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
2063             finalize_op(kid);
2064     }
2065 }
2066
2067 /*
2068 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2069
2070 Propagate lvalue ("modifiable") context to an op and its children.
2071 I<type> represents the context type, roughly based on the type of op that
2072 would do the modifying, although C<local()> is represented by OP_NULL,
2073 because it has no op type of its own (it is signalled by a flag on
2074 the lvalue op).
2075
2076 This function detects things that can't be modified, such as C<$x+1>, and
2077 generates errors for them.  For example, C<$x+1 = 2> would cause it to be
2078 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2079
2080 It also flags things that need to behave specially in an lvalue context,
2081 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2082
2083 =cut
2084 */
2085
2086 static bool
2087 S_vivifies(const OPCODE type)
2088 {
2089     switch(type) {
2090     case OP_RV2AV:     case   OP_ASLICE:
2091     case OP_RV2HV:     case OP_KVASLICE:
2092     case OP_RV2SV:     case   OP_HSLICE:
2093     case OP_AELEMFAST: case OP_KVHSLICE:
2094     case OP_HELEM:
2095     case OP_AELEM:
2096         return 1;
2097     }
2098     return 0;
2099 }
2100
2101 OP *
2102 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2103 {
2104     dVAR;
2105     OP *kid;
2106     /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2107     int localize = -1;
2108
2109     if (!o || (PL_parser && PL_parser->error_count))
2110         return o;
2111
2112     if ((o->op_private & OPpTARGET_MY)
2113         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2114     {
2115         return o;
2116     }
2117
2118     assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2119
2120     if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2121
2122     switch (o->op_type) {
2123     case OP_UNDEF:
2124         PL_modcount++;
2125         return o;
2126     case OP_STUB:
2127         if ((o->op_flags & OPf_PARENS) || PL_madskills)
2128             break;
2129         goto nomod;
2130     case OP_ENTERSUB:
2131         if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2132             !(o->op_flags & OPf_STACKED)) {
2133             o->op_type = OP_RV2CV;              /* entersub => rv2cv */
2134             /* Both ENTERSUB and RV2CV use this bit, but for different pur-
2135                poses, so we need it clear.  */
2136             o->op_private &= ~1;
2137             o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2138             assert(cUNOPo->op_first->op_type == OP_NULL);
2139             op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2140             break;
2141         }
2142         else {                          /* lvalue subroutine call */
2143             o->op_private |= OPpLVAL_INTRO
2144                            |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
2145             PL_modcount = RETURN_UNLIMITED_NUMBER;
2146             if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
2147                 /* Potential lvalue context: */
2148                 o->op_private |= OPpENTERSUB_INARGS;
2149                 break;
2150             }
2151             else {                      /* Compile-time error message: */
2152                 OP *kid = cUNOPo->op_first;
2153                 CV *cv;
2154
2155                 if (kid->op_type != OP_PUSHMARK) {
2156                     if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2157                         Perl_croak(aTHX_
2158                                 "panic: unexpected lvalue entersub "
2159                                 "args: type/targ %ld:%"UVuf,
2160                                 (long)kid->op_type, (UV)kid->op_targ);
2161                     kid = kLISTOP->op_first;
2162                 }
2163                 while (kid->op_sibling)
2164                     kid = kid->op_sibling;
2165                 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2166                     break;      /* Postpone until runtime */
2167                 }
2168
2169                 kid = kUNOP->op_first;
2170                 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2171                     kid = kUNOP->op_first;
2172                 if (kid->op_type == OP_NULL)
2173                     Perl_croak(aTHX_
2174                                "Unexpected constant lvalue entersub "
2175                                "entry via type/targ %ld:%"UVuf,
2176                                (long)kid->op_type, (UV)kid->op_targ);
2177                 if (kid->op_type != OP_GV) {
2178                     break;
2179                 }
2180
2181                 cv = GvCV(kGVOP_gv);
2182                 if (!cv)
2183                     break;
2184                 if (CvLVALUE(cv))
2185                     break;
2186             }
2187         }
2188         /* FALL THROUGH */
2189     default:
2190       nomod:
2191         if (flags & OP_LVALUE_NO_CROAK) return NULL;
2192         /* grep, foreach, subcalls, refgen */
2193         if (type == OP_GREPSTART || type == OP_ENTERSUB
2194          || type == OP_REFGEN    || type == OP_LEAVESUBLV)
2195             break;
2196         yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2197                      (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2198                       ? "do block"
2199                       : (o->op_type == OP_ENTERSUB
2200                         ? "non-lvalue subroutine call"
2201                         : OP_DESC(o))),
2202                      type ? PL_op_desc[type] : "local"));
2203         return o;
2204
2205     case OP_PREINC:
2206     case OP_PREDEC:
2207     case OP_POW:
2208     case OP_MULTIPLY:
2209     case OP_DIVIDE:
2210     case OP_MODULO:
2211     case OP_REPEAT:
2212     case OP_ADD:
2213     case OP_SUBTRACT:
2214     case OP_CONCAT:
2215     case OP_LEFT_SHIFT:
2216     case OP_RIGHT_SHIFT:
2217     case OP_BIT_AND:
2218     case OP_BIT_XOR:
2219     case OP_BIT_OR:
2220     case OP_I_MULTIPLY:
2221     case OP_I_DIVIDE:
2222     case OP_I_MODULO:
2223     case OP_I_ADD:
2224     case OP_I_SUBTRACT:
2225         if (!(o->op_flags & OPf_STACKED))
2226             goto nomod;
2227         PL_modcount++;
2228         break;
2229
2230     case OP_COND_EXPR:
2231         localize = 1;
2232         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2233             op_lvalue(kid, type);
2234         break;
2235
2236     case OP_RV2AV:
2237     case OP_RV2HV:
2238         if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2239            PL_modcount = RETURN_UNLIMITED_NUMBER;
2240             return o;           /* Treat \(@foo) like ordinary list. */
2241         }
2242         /* FALL THROUGH */
2243     case OP_RV2GV:
2244         if (scalar_mod_type(o, type))
2245             goto nomod;
2246         ref(cUNOPo->op_first, o->op_type);
2247         /* FALL THROUGH */
2248     case OP_ASLICE:
2249     case OP_HSLICE:
2250         localize = 1;
2251         /* FALL THROUGH */
2252     case OP_AASSIGN:
2253         /* Do not apply the lvsub flag for rv2[ah]v in scalar context.  */
2254         if (type == OP_LEAVESUBLV && (
2255                 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2256              || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2257            ))
2258             o->op_private |= OPpMAYBE_LVSUB;
2259         /* FALL THROUGH */
2260     case OP_NEXTSTATE:
2261     case OP_DBSTATE:
2262        PL_modcount = RETURN_UNLIMITED_NUMBER;
2263         break;
2264     case OP_KVHSLICE:
2265     case OP_KVASLICE:
2266         if (type == OP_LEAVESUBLV)
2267             o->op_private |= OPpMAYBE_LVSUB;
2268         goto nomod;
2269     case OP_AV2ARYLEN:
2270         PL_hints |= HINT_BLOCK_SCOPE;
2271         if (type == OP_LEAVESUBLV)
2272             o->op_private |= OPpMAYBE_LVSUB;
2273         PL_modcount++;
2274         break;
2275     case OP_RV2SV:
2276         ref(cUNOPo->op_first, o->op_type);
2277         localize = 1;
2278         /* FALL THROUGH */
2279     case OP_GV:
2280         PL_hints |= HINT_BLOCK_SCOPE;
2281     case OP_SASSIGN:
2282     case OP_ANDASSIGN:
2283     case OP_ORASSIGN:
2284     case OP_DORASSIGN:
2285         PL_modcount++;
2286         break;
2287
2288     case OP_AELEMFAST:
2289     case OP_AELEMFAST_LEX:
2290         localize = -1;
2291         PL_modcount++;
2292         break;
2293
2294     case OP_PADAV:
2295     case OP_PADHV:
2296        PL_modcount = RETURN_UNLIMITED_NUMBER;
2297         if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2298             return o;           /* Treat \(@foo) like ordinary list. */
2299         if (scalar_mod_type(o, type))
2300             goto nomod;
2301         if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2302           && type == OP_LEAVESUBLV)
2303             o->op_private |= OPpMAYBE_LVSUB;
2304         /* FALL THROUGH */
2305     case OP_PADSV:
2306         PL_modcount++;
2307         if (!type) /* local() */
2308             Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2309                  PAD_COMPNAME_SV(o->op_targ));
2310         break;
2311
2312     case OP_PUSHMARK:
2313         localize = 0;
2314         break;
2315
2316     case OP_KEYS:
2317     case OP_RKEYS:
2318         if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2319             goto nomod;
2320         goto lvalue_func;
2321     case OP_SUBSTR:
2322         if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2323             goto nomod;
2324         /* FALL THROUGH */
2325     case OP_POS:
2326     case OP_VEC:
2327       lvalue_func:
2328         if (type == OP_LEAVESUBLV)
2329             o->op_private |= OPpMAYBE_LVSUB;
2330         if (o->op_flags & OPf_KIDS)
2331             op_lvalue(cBINOPo->op_first->op_sibling, type);
2332         break;
2333
2334     case OP_AELEM:
2335     case OP_HELEM:
2336         ref(cBINOPo->op_first, o->op_type);
2337         if (type == OP_ENTERSUB &&
2338              !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2339             o->op_private |= OPpLVAL_DEFER;
2340         if (type == OP_LEAVESUBLV)
2341             o->op_private |= OPpMAYBE_LVSUB;
2342         localize = 1;
2343         PL_modcount++;
2344         break;
2345
2346     case OP_LEAVE:
2347     case OP_LEAVELOOP:
2348         o->op_private |= OPpLVALUE;
2349     case OP_SCOPE:
2350     case OP_ENTER:
2351     case OP_LINESEQ:
2352         localize = 0;
2353         if (o->op_flags & OPf_KIDS)
2354             op_lvalue(cLISTOPo->op_last, type);
2355         break;
2356
2357     case OP_NULL:
2358         localize = 0;
2359         if (o->op_flags & OPf_SPECIAL)          /* do BLOCK */
2360             goto nomod;
2361         else if (!(o->op_flags & OPf_KIDS))
2362             break;
2363         if (o->op_targ != OP_LIST) {
2364             op_lvalue(cBINOPo->op_first, type);
2365             break;
2366         }
2367         /* FALL THROUGH */
2368     case OP_LIST:
2369         localize = 0;
2370         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2371             /* elements might be in void context because the list is
2372                in scalar context or because they are attribute sub calls */
2373             if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2374                 op_lvalue(kid, type);
2375         break;
2376
2377     case OP_RETURN:
2378         if (type != OP_LEAVESUBLV)
2379             goto nomod;
2380         break; /* op_lvalue()ing was handled by ck_return() */
2381
2382     case OP_COREARGS:
2383         return o;
2384
2385     case OP_AND:
2386     case OP_OR:
2387         if (type == OP_LEAVESUBLV
2388          || !S_vivifies(cLOGOPo->op_first->op_type))
2389             op_lvalue(cLOGOPo->op_first, type);
2390         if (type == OP_LEAVESUBLV
2391          || !S_vivifies(cLOGOPo->op_first->op_sibling->op_type))
2392             op_lvalue(cLOGOPo->op_first->op_sibling, type);
2393         goto nomod;
2394     }
2395
2396     /* [20011101.069] File test operators interpret OPf_REF to mean that
2397        their argument is a filehandle; thus \stat(".") should not set
2398        it. AMS 20011102 */
2399     if (type == OP_REFGEN &&
2400         PL_check[o->op_type] == Perl_ck_ftst)
2401         return o;
2402
2403     if (type != OP_LEAVESUBLV)
2404         o->op_flags |= OPf_MOD;
2405
2406     if (type == OP_AASSIGN || type == OP_SASSIGN)
2407         o->op_flags |= OPf_SPECIAL|OPf_REF;
2408     else if (!type) { /* local() */
2409         switch (localize) {
2410         case 1:
2411             o->op_private |= OPpLVAL_INTRO;
2412             o->op_flags &= ~OPf_SPECIAL;
2413             PL_hints |= HINT_BLOCK_SCOPE;
2414             break;
2415         case 0:
2416             break;
2417         case -1:
2418             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2419                            "Useless localization of %s", OP_DESC(o));
2420         }
2421     }
2422     else if (type != OP_GREPSTART && type != OP_ENTERSUB
2423              && type != OP_LEAVESUBLV)
2424         o->op_flags |= OPf_REF;
2425     return o;
2426 }
2427
2428 STATIC bool
2429 S_scalar_mod_type(const OP *o, I32 type)
2430 {
2431     switch (type) {
2432     case OP_POS:
2433     case OP_SASSIGN:
2434         if (o && o->op_type == OP_RV2GV)
2435             return FALSE;
2436         /* FALL THROUGH */
2437     case OP_PREINC:
2438     case OP_PREDEC:
2439     case OP_POSTINC:
2440     case OP_POSTDEC:
2441     case OP_I_PREINC:
2442     case OP_I_PREDEC:
2443     case OP_I_POSTINC:
2444     case OP_I_POSTDEC:
2445     case OP_POW:
2446     case OP_MULTIPLY:
2447     case OP_DIVIDE:
2448     case OP_MODULO:
2449     case OP_REPEAT:
2450     case OP_ADD:
2451     case OP_SUBTRACT:
2452     case OP_I_MULTIPLY:
2453     case OP_I_DIVIDE:
2454     case OP_I_MODULO:
2455     case OP_I_ADD:
2456     case OP_I_SUBTRACT:
2457     case OP_LEFT_SHIFT:
2458     case OP_RIGHT_SHIFT:
2459     case OP_BIT_AND:
2460     case OP_BIT_XOR:
2461     case OP_BIT_OR:
2462     case OP_CONCAT:
2463     case OP_SUBST:
2464     case OP_TRANS:
2465     case OP_TRANSR:
2466     case OP_READ:
2467     case OP_SYSREAD:
2468     case OP_RECV:
2469     case OP_ANDASSIGN:
2470     case OP_ORASSIGN:
2471     case OP_DORASSIGN:
2472         return TRUE;
2473     default:
2474         return FALSE;
2475     }
2476 }
2477
2478 STATIC bool
2479 S_is_handle_constructor(const OP *o, I32 numargs)
2480 {
2481     PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2482
2483     switch (o->op_type) {
2484     case OP_PIPE_OP:
2485     case OP_SOCKPAIR:
2486         if (numargs == 2)
2487             return TRUE;
2488         /* FALL THROUGH */
2489     case OP_SYSOPEN:
2490     case OP_OPEN:
2491     case OP_SELECT:             /* XXX c.f. SelectSaver.pm */
2492     case OP_SOCKET:
2493     case OP_OPEN_DIR:
2494     case OP_ACCEPT:
2495         if (numargs == 1)
2496             return TRUE;
2497         /* FALLTHROUGH */
2498     default:
2499         return FALSE;
2500     }
2501 }
2502
2503 static OP *
2504 S_refkids(pTHX_ OP *o, I32 type)
2505 {
2506     if (o && o->op_flags & OPf_KIDS) {
2507         OP *kid;
2508         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2509             ref(kid, type);
2510     }
2511     return o;
2512 }
2513
2514 OP *
2515 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2516 {
2517     dVAR;
2518     OP *kid;
2519
2520     PERL_ARGS_ASSERT_DOREF;
2521
2522     if (!o || (PL_parser && PL_parser->error_count))
2523         return o;
2524
2525     switch (o->op_type) {
2526     case OP_ENTERSUB:
2527         if ((type == OP_EXISTS || type == OP_DEFINED) &&
2528             !(o->op_flags & OPf_STACKED)) {
2529             o->op_type = OP_RV2CV;             /* entersub => rv2cv */
2530             o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2531             assert(cUNOPo->op_first->op_type == OP_NULL);
2532             op_null(((LISTOP*)cUNOPo->op_first)->op_first);     /* disable pushmark */
2533             o->op_flags |= OPf_SPECIAL;
2534             o->op_private &= ~1;
2535         }
2536         else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2537             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2538                               : type == OP_RV2HV ? OPpDEREF_HV
2539                               : OPpDEREF_SV);
2540             o->op_flags |= OPf_MOD;
2541         }
2542
2543         break;
2544
2545     case OP_COND_EXPR:
2546         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2547             doref(kid, type, set_op_ref);
2548         break;
2549     case OP_RV2SV:
2550         if (type == OP_DEFINED)
2551             o->op_flags |= OPf_SPECIAL;         /* don't create GV */
2552         doref(cUNOPo->op_first, o->op_type, set_op_ref);
2553         /* FALL THROUGH */
2554     case OP_PADSV:
2555         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2556             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2557                               : type == OP_RV2HV ? OPpDEREF_HV
2558                               : OPpDEREF_SV);
2559             o->op_flags |= OPf_MOD;
2560         }
2561         break;
2562
2563     case OP_RV2AV:
2564     case OP_RV2HV:
2565         if (set_op_ref)
2566             o->op_flags |= OPf_REF;
2567         /* FALL THROUGH */
2568     case OP_RV2GV:
2569         if (type == OP_DEFINED)
2570             o->op_flags |= OPf_SPECIAL;         /* don't create GV */
2571         doref(cUNOPo->op_first, o->op_type, set_op_ref);
2572         break;
2573
2574     case OP_PADAV:
2575     case OP_PADHV:
2576         if (set_op_ref)
2577             o->op_flags |= OPf_REF;
2578         break;
2579
2580     case OP_SCALAR:
2581     case OP_NULL:
2582         if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
2583             break;
2584         doref(cBINOPo->op_first, type, set_op_ref);
2585         break;
2586     case OP_AELEM:
2587     case OP_HELEM:
2588         doref(cBINOPo->op_first, o->op_type, set_op_ref);
2589         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2590             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2591                               : type == OP_RV2HV ? OPpDEREF_HV
2592                               : OPpDEREF_SV);
2593             o->op_flags |= OPf_MOD;
2594         }
2595         break;
2596
2597     case OP_SCOPE:
2598     case OP_LEAVE:
2599         set_op_ref = FALSE;
2600         /* FALL THROUGH */
2601     case OP_ENTER:
2602     case OP_LIST:
2603         if (!(o->op_flags & OPf_KIDS))
2604             break;
2605         doref(cLISTOPo->op_last, type, set_op_ref);
2606         break;
2607     default:
2608         break;
2609     }
2610     return scalar(o);
2611
2612 }
2613
2614 STATIC OP *
2615 S_dup_attrlist(pTHX_ OP *o)
2616 {
2617     dVAR;
2618     OP *rop;
2619
2620     PERL_ARGS_ASSERT_DUP_ATTRLIST;
2621
2622     /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2623      * where the first kid is OP_PUSHMARK and the remaining ones
2624      * are OP_CONST.  We need to push the OP_CONST values.
2625      */
2626     if (o->op_type == OP_CONST)
2627         rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2628 #ifdef PERL_MAD
2629     else if (o->op_type == OP_NULL)
2630         rop = NULL;
2631 #endif
2632     else {
2633         assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2634         rop = NULL;
2635         for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2636             if (o->op_type == OP_CONST)
2637                 rop = op_append_elem(OP_LIST, rop,
2638                                   newSVOP(OP_CONST, o->op_flags,
2639                                           SvREFCNT_inc_NN(cSVOPo->op_sv)));
2640         }
2641     }
2642     return rop;
2643 }
2644
2645 STATIC void
2646 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
2647 {
2648     dVAR;
2649     SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2650
2651     PERL_ARGS_ASSERT_APPLY_ATTRS;
2652
2653     /* fake up C<use attributes $pkg,$rv,@attrs> */
2654
2655 #define ATTRSMODULE "attributes"
2656 #define ATTRSMODULE_PM "attributes.pm"
2657
2658     Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2659                          newSVpvs(ATTRSMODULE),
2660                          NULL,
2661                          op_prepend_elem(OP_LIST,
2662                                       newSVOP(OP_CONST, 0, stashsv),
2663                                       op_prepend_elem(OP_LIST,
2664                                                    newSVOP(OP_CONST, 0,
2665                                                            newRV(target)),
2666                                                    dup_attrlist(attrs))));
2667 }
2668
2669 STATIC void
2670 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2671 {
2672     dVAR;
2673     OP *pack, *imop, *arg;
2674     SV *meth, *stashsv, **svp;
2675
2676     PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2677
2678     if (!attrs)
2679         return;
2680
2681     assert(target->op_type == OP_PADSV ||
2682            target->op_type == OP_PADHV ||
2683            target->op_type == OP_PADAV);
2684
2685     /* Ensure that attributes.pm is loaded. */
2686     /* Don't force the C<use> if we don't need it. */
2687     svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2688     if (svp && *svp != &PL_sv_undef)
2689         NOOP;   /* already in %INC */
2690     else
2691         Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2692                                newSVpvs(ATTRSMODULE), NULL);
2693
2694     /* Need package name for method call. */
2695     pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2696
2697     /* Build up the real arg-list. */
2698     stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2699
2700     arg = newOP(OP_PADSV, 0);
2701     arg->op_targ = target->op_targ;
2702     arg = op_prepend_elem(OP_LIST,
2703                        newSVOP(OP_CONST, 0, stashsv),
2704                        op_prepend_elem(OP_LIST,
2705                                     newUNOP(OP_REFGEN, 0,
2706                                             op_lvalue(arg, OP_REFGEN)),
2707                                     dup_attrlist(attrs)));
2708
2709     /* Fake up a method call to import */
2710     meth = newSVpvs_share("import");
2711     imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2712                    op_append_elem(OP_LIST,
2713                                op_prepend_elem(OP_LIST, pack, list(arg)),
2714                                newSVOP(OP_METHOD_NAMED, 0, meth)));
2715
2716     /* Combine the ops. */
2717     *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2718 }
2719
2720 /*
2721 =notfor apidoc apply_attrs_string
2722
2723 Attempts to apply a list of attributes specified by the C<attrstr> and
2724 C<len> arguments to the subroutine identified by the C<cv> argument which
2725 is expected to be associated with the package identified by the C<stashpv>
2726 argument (see L<attributes>).  It gets this wrong, though, in that it
2727 does not correctly identify the boundaries of the individual attribute
2728 specifications within C<attrstr>.  This is not really intended for the
2729 public API, but has to be listed here for systems such as AIX which
2730 need an explicit export list for symbols.  (It's called from XS code
2731 in support of the C<ATTRS:> keyword from F<xsubpp>.)  Patches to fix it
2732 to respect attribute syntax properly would be welcome.
2733
2734 =cut
2735 */
2736
2737 void
2738 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2739                         const char *attrstr, STRLEN len)
2740 {
2741     OP *attrs = NULL;
2742
2743     PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2744
2745     if (!len) {
2746         len = strlen(attrstr);
2747     }
2748
2749     while (len) {
2750         for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2751         if (len) {
2752             const char * const sstr = attrstr;
2753             for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2754             attrs = op_append_elem(OP_LIST, attrs,
2755                                 newSVOP(OP_CONST, 0,
2756                                         newSVpvn(sstr, attrstr-sstr)));
2757         }
2758     }
2759
2760     Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2761                      newSVpvs(ATTRSMODULE),
2762                      NULL, op_prepend_elem(OP_LIST,
2763                                   newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2764                                   op_prepend_elem(OP_LIST,
2765                                                newSVOP(OP_CONST, 0,
2766                                                        newRV(MUTABLE_SV(cv))),
2767                                                attrs)));
2768 }
2769
2770 STATIC void
2771 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
2772 {
2773     OP *new_proto = NULL;
2774     STRLEN pvlen;
2775     char *pv;
2776     OP *o;
2777
2778     PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
2779
2780     if (!*attrs)
2781         return;
2782
2783     o = *attrs;
2784     if (o->op_type == OP_CONST) {
2785         pv = SvPV(cSVOPo_sv, pvlen);
2786         if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
2787             SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
2788             SV ** const tmpo = cSVOPx_svp(o);
2789             SvREFCNT_dec(cSVOPo_sv);
2790             *tmpo = tmpsv;
2791             new_proto = o;
2792             *attrs = NULL;
2793         }
2794     } else if (o->op_type == OP_LIST) {
2795         OP * lasto = NULL;
2796         assert(o->op_flags & OPf_KIDS);
2797         assert(cLISTOPo->op_first->op_type == OP_PUSHMARK);
2798         /* Counting on the first op to hit the lasto = o line */
2799         for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2800             if (o->op_type == OP_CONST) {
2801                 pv = SvPV(cSVOPo_sv, pvlen);
2802                 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
2803                     SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
2804                     SV ** const tmpo = cSVOPx_svp(o);
2805                     SvREFCNT_dec(cSVOPo_sv);
2806                     *tmpo = tmpsv;
2807                     if (new_proto && ckWARN(WARN_MISC)) {
2808                         STRLEN new_len;
2809                         const char * newp = SvPV(cSVOPo_sv, new_len);
2810                         Perl_warner(aTHX_ packWARN(WARN_MISC),
2811                             "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
2812                             UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
2813                         op_free(new_proto);
2814                     }
2815                     else if (new_proto)
2816                         op_free(new_proto);
2817                     new_proto = o;
2818                     lasto->op_sibling = o->op_sibling;
2819                     continue;
2820                 }
2821             }
2822             lasto = o;
2823         }
2824         /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
2825            would get pulled in with no real need */
2826         if (!cLISTOPx(*attrs)->op_first->op_sibling) {
2827             op_free(*attrs);
2828             *attrs = NULL;
2829         }
2830     }
2831
2832     if (new_proto) {
2833         SV *svname;
2834         if (isGV(name)) {
2835             svname = sv_newmortal();
2836             gv_efullname3(svname, name, NULL);
2837         }
2838         else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
2839             svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
2840         else
2841             svname = (SV *)name;
2842         if (ckWARN(WARN_ILLEGALPROTO))
2843             (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
2844         if (*proto && ckWARN(WARN_PROTOTYPE)) {
2845             STRLEN old_len, new_len;
2846             const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
2847             const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
2848
2849             Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2850                 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
2851                 " in %"SVf,
2852                 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
2853                 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
2854                 SVfARG(svname));
2855         }
2856         if (*proto)
2857             op_free(*proto);
2858         *proto = new_proto;
2859     }
2860 }
2861
2862 static void
2863 S_cant_declare(pTHX_ OP *o)
2864 {
2865     if (o->op_type == OP_NULL
2866      && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
2867         o = cUNOPo->op_first;
2868     yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2869                              o->op_type == OP_NULL
2870                                && o->op_flags & OPf_SPECIAL
2871                                  ? "do block"
2872                                  : OP_DESC(o),
2873                              PL_parser->in_my == KEY_our   ? "our"   :
2874                              PL_parser->in_my == KEY_state ? "state" :
2875                                                              "my"));
2876 }
2877
2878 STATIC OP *
2879 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2880 {
2881     dVAR;
2882     I32 type;
2883     const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2884
2885     PERL_ARGS_ASSERT_MY_KID;
2886
2887     if (!o || (PL_parser && PL_parser->error_count))
2888         return o;
2889
2890     type = o->op_type;
2891     if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2892         (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2893         return o;
2894     }
2895
2896     if (type == OP_LIST) {
2897         OP *kid;
2898         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2899             my_kid(kid, attrs, imopsp);
2900         return o;
2901     } else if (type == OP_UNDEF || type == OP_STUB) {
2902         return o;
2903     } else if (type == OP_RV2SV ||      /* "our" declaration */
2904                type == OP_RV2AV ||
2905                type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2906         if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2907             S_cant_declare(aTHX_ o);
2908         } else if (attrs) {
2909             GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2910             PL_parser->in_my = FALSE;
2911             PL_parser->in_my_stash = NULL;
2912             apply_attrs(GvSTASH(gv),
2913                         (type == OP_RV2SV ? GvSV(gv) :
2914                          type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2915                          type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2916                         attrs);
2917         }
2918         o->op_private |= OPpOUR_INTRO;
2919         return o;
2920     }
2921     else if (type != OP_PADSV &&
2922              type != OP_PADAV &&
2923              type != OP_PADHV &&
2924              type != OP_PUSHMARK)
2925     {
2926         S_cant_declare(aTHX_ o);
2927         return o;
2928     }
2929     else if (attrs && type != OP_PUSHMARK) {
2930         HV *stash;
2931
2932         PL_parser->in_my = FALSE;
2933         PL_parser->in_my_stash = NULL;
2934
2935         /* check for C<my Dog $spot> when deciding package */
2936         stash = PAD_COMPNAME_TYPE(o->op_targ);
2937         if (!stash)
2938             stash = PL_curstash;
2939         apply_attrs_my(stash, o, attrs, imopsp);
2940     }
2941     o->op_flags |= OPf_MOD;
2942     o->op_private |= OPpLVAL_INTRO;
2943     if (stately)
2944         o->op_private |= OPpPAD_STATE;
2945     return o;
2946 }
2947
2948 OP *
2949 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2950 {
2951     dVAR;
2952     OP *rops;
2953     int maybe_scalar = 0;
2954
2955     PERL_ARGS_ASSERT_MY_ATTRS;
2956
2957 /* [perl #17376]: this appears to be premature, and results in code such as
2958    C< our(%x); > executing in list mode rather than void mode */
2959 #if 0
2960     if (o->op_flags & OPf_PARENS)
2961         list(o);
2962     else
2963         maybe_scalar = 1;
2964 #else
2965     maybe_scalar = 1;
2966 #endif
2967     if (attrs)
2968         SAVEFREEOP(attrs);
2969     rops = NULL;
2970     o = my_kid(o, attrs, &rops);
2971     if (rops) {
2972         if (maybe_scalar && o->op_type == OP_PADSV) {
2973             o = scalar(op_append_list(OP_LIST, rops, o));
2974             o->op_private |= OPpLVAL_INTRO;
2975         }
2976         else {
2977             /* The listop in rops might have a pushmark at the beginning,
2978                which will mess up list assignment. */
2979             LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2980             if (rops->op_type == OP_LIST && 
2981                 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2982             {
2983                 OP * const pushmark = lrops->op_first;
2984                 lrops->op_first = pushmark->op_sibling;
2985                 op_free(pushmark);
2986             }
2987             o = op_append_list(OP_LIST, o, rops);
2988         }
2989     }
2990     PL_parser->in_my = FALSE;
2991     PL_parser->in_my_stash = NULL;
2992     return o;
2993 }
2994
2995 OP *
2996 Perl_sawparens(pTHX_ OP *o)
2997 {
2998     PERL_UNUSED_CONTEXT;
2999     if (o)
3000         o->op_flags |= OPf_PARENS;
3001     return o;
3002 }
3003
3004 OP *
3005 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3006 {
3007     OP *o;
3008     bool ismatchop = 0;
3009     const OPCODE ltype = left->op_type;
3010     const OPCODE rtype = right->op_type;
3011
3012     PERL_ARGS_ASSERT_BIND_MATCH;
3013
3014     if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3015           || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3016     {
3017       const char * const desc
3018           = PL_op_desc[(
3019                           rtype == OP_SUBST || rtype == OP_TRANS
3020                        || rtype == OP_TRANSR
3021                        )
3022                        ? (int)rtype : OP_MATCH];
3023       const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3024       SV * const name =
3025         S_op_varname(aTHX_ left);
3026       if (name)
3027         Perl_warner(aTHX_ packWARN(WARN_MISC),
3028              "Applying %s to %"SVf" will act on scalar(%"SVf")",
3029              desc, name, name);
3030       else {
3031         const char * const sample = (isary
3032              ? "@array" : "%hash");
3033         Perl_warner(aTHX_ packWARN(WARN_MISC),
3034              "Applying %s to %s will act on scalar(%s)",
3035              desc, sample, sample);
3036       }
3037     }
3038
3039     if (rtype == OP_CONST &&
3040         cSVOPx(right)->op_private & OPpCONST_BARE &&
3041         cSVOPx(right)->op_private & OPpCONST_STRICT)
3042     {
3043         no_bareword_allowed(right);
3044     }
3045
3046     /* !~ doesn't make sense with /r, so error on it for now */
3047     if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3048         type == OP_NOT)
3049         /* diag_listed_as: Using !~ with %s doesn't make sense */
3050         yyerror("Using !~ with s///r doesn't make sense");
3051     if (rtype == OP_TRANSR && type == OP_NOT)
3052         /* diag_listed_as: Using !~ with %s doesn't make sense */
3053         yyerror("Using !~ with tr///r doesn't make sense");
3054
3055     ismatchop = (rtype == OP_MATCH ||
3056                  rtype == OP_SUBST ||
3057                  rtype == OP_TRANS || rtype == OP_TRANSR)
3058              && !(right->op_flags & OPf_SPECIAL);
3059     if (ismatchop && right->op_private & OPpTARGET_MY) {
3060         right->op_targ = 0;
3061         right->op_private &= ~OPpTARGET_MY;
3062     }
3063     if (!(right->op_flags & OPf_STACKED) && ismatchop) {
3064         OP *newleft;
3065
3066         right->op_flags |= OPf_STACKED;
3067         if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3068             ! (rtype == OP_TRANS &&
3069                right->op_private & OPpTRANS_IDENTICAL) &&
3070             ! (rtype == OP_SUBST &&
3071                (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3072             newleft = op_lvalue(left, rtype);
3073         else
3074             newleft = left;
3075         if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3076             o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
3077         else
3078             o = op_prepend_elem(rtype, scalar(newleft), right);
3079         if (type == OP_NOT)
3080             return newUNOP(OP_NOT, 0, scalar(o));
3081         return o;
3082     }
3083     else
3084         return bind_match(type, left,
3085                 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
3086 }
3087
3088 OP *
3089 Perl_invert(pTHX_ OP *o)
3090 {
3091     if (!o)
3092         return NULL;
3093     return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3094 }
3095
3096 /*
3097 =for apidoc Amx|OP *|op_scope|OP *o
3098
3099 Wraps up an op tree with some additional ops so that at runtime a dynamic
3100 scope will be created.  The original ops run in the new dynamic scope,
3101 and then, provided that they exit normally, the scope will be unwound.
3102 The additional ops used to create and unwind the dynamic scope will
3103 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3104 instead if the ops are simple enough to not need the full dynamic scope
3105 structure.
3106
3107 =cut
3108 */
3109
3110 OP *
3111 Perl_op_scope(pTHX_ OP *o)
3112 {
3113     dVAR;
3114     if (o) {
3115         if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3116             o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3117             o->op_type = OP_LEAVE;
3118             o->op_ppaddr = PL_ppaddr[OP_LEAVE];
3119         }
3120         else if (o->op_type == OP_LINESEQ) {
3121             OP *kid;
3122             o->op_type = OP_SCOPE;
3123             o->op_ppaddr = PL_ppaddr[OP_SCOPE];
3124             kid = ((LISTOP*)o)->op_first;
3125             if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3126                 op_null(kid);
3127
3128                 /* The following deals with things like 'do {1 for 1}' */
3129                 kid = kid->op_sibling;
3130                 if (kid &&
3131                     (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3132                     op_null(kid);
3133             }
3134         }
3135         else
3136             o = newLISTOP(OP_SCOPE, 0, o, NULL);
3137     }
3138     return o;
3139 }
3140
3141 OP *
3142 Perl_op_unscope(pTHX_ OP *o)
3143 {
3144     if (o && o->op_type == OP_LINESEQ) {
3145         OP *kid = cLISTOPo->op_first;
3146         for(; kid; kid = kid->op_sibling)
3147             if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3148                 op_null(kid);
3149     }
3150     return o;
3151 }
3152
3153 int
3154 Perl_block_start(pTHX_ int full)
3155 {
3156     dVAR;
3157     const int retval = PL_savestack_ix;
3158
3159     pad_block_start(full);
3160     SAVEHINTS();
3161     PL_hints &= ~HINT_BLOCK_SCOPE;
3162     SAVECOMPILEWARNINGS();
3163     PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3164
3165     CALL_BLOCK_HOOKS(bhk_start, full);
3166
3167     return retval;
3168 }
3169
3170 OP*
3171 Perl_block_end(pTHX_ I32 floor, OP *seq)
3172 {
3173     dVAR;
3174     const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3175     OP* retval = scalarseq(seq);
3176     OP *o;
3177
3178     CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3179
3180     LEAVE_SCOPE(floor);
3181     if (needblockscope)
3182         PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3183     o = pad_leavemy();
3184
3185     if (o) {
3186         /* pad_leavemy has created a sequence of introcv ops for all my
3187            subs declared in the block.  We have to replicate that list with
3188            clonecv ops, to deal with this situation:
3189
3190                sub {
3191                    my sub s1;
3192                    my sub s2;
3193                    sub s1 { state sub foo { \&s2 } }
3194                }->()
3195
3196            Originally, I was going to have introcv clone the CV and turn
3197            off the stale flag.  Since &s1 is declared before &s2, the
3198            introcv op for &s1 is executed (on sub entry) before the one for
3199            &s2.  But the &foo sub inside &s1 (which is cloned when &s1 is
3200            cloned, since it is a state sub) closes over &s2 and expects
3201            to see it in its outer CV’s pad.  If the introcv op clones &s1,
3202            then &s2 is still marked stale.  Since &s1 is not active, and
3203            &foo closes over &s1’s implicit entry for &s2, we get a â€˜Varia-
3204            ble will not stay shared’ warning.  Because it is the same stub
3205            that will be used when the introcv op for &s2 is executed, clos-
3206            ing over it is safe.  Hence, we have to turn off the stale flag
3207            on all lexical subs in the block before we clone any of them.
3208            Hence, having introcv clone the sub cannot work.  So we create a
3209            list of ops like this:
3210
3211                lineseq
3212                   |
3213                   +-- introcv
3214                   |
3215                   +-- introcv
3216                   |
3217                   +-- introcv
3218                   |
3219                   .
3220                   .
3221                   .
3222                   |
3223                   +-- clonecv
3224                   |
3225                   +-- clonecv
3226                   |
3227                   +-- clonecv
3228                   |
3229                   .
3230                   .
3231                   .
3232          */
3233         OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3234         OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3235         for (;; kid = kid->op_sibling) {
3236             OP *newkid = newOP(OP_CLONECV, 0);
3237             newkid->op_targ = kid->op_targ;
3238             o = op_append_elem(OP_LINESEQ, o, newkid);
3239             if (kid == last) break;
3240         }
3241         retval = op_prepend_elem(OP_LINESEQ, o, retval);
3242     }
3243
3244     CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3245
3246     return retval;
3247 }
3248
3249 /*
3250 =head1 Compile-time scope hooks
3251
3252 =for apidoc Aox||blockhook_register
3253
3254 Register a set of hooks to be called when the Perl lexical scope changes
3255 at compile time.  See L<perlguts/"Compile-time scope hooks">.
3256
3257 =cut
3258 */
3259
3260 void
3261 Perl_blockhook_register(pTHX_ BHK *hk)
3262 {
3263     PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3264
3265     Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3266 }
3267
3268 STATIC OP *
3269 S_newDEFSVOP(pTHX)
3270 {
3271     dVAR;
3272     const PADOFFSET offset = pad_findmy_pvs("$_", 0);
3273     if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
3274         return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
3275     }
3276     else {
3277         OP * const o = newOP(OP_PADSV, 0);
3278         o->op_targ = offset;
3279         return o;
3280     }
3281 }
3282
3283 void
3284 Perl_newPROG(pTHX_ OP *o)
3285 {
3286     dVAR;
3287
3288     PERL_ARGS_ASSERT_NEWPROG;
3289
3290     if (PL_in_eval) {
3291         PERL_CONTEXT *cx;
3292         I32 i;
3293         if (PL_eval_root)
3294                 return;
3295         PL_eval_root = newUNOP(OP_LEAVEEVAL,
3296                                ((PL_in_eval & EVAL_KEEPERR)
3297                                 ? OPf_SPECIAL : 0), o);
3298
3299         cx = &cxstack[cxstack_ix];
3300         assert(CxTYPE(cx) == CXt_EVAL);
3301
3302         if ((cx->blk_gimme & G_WANT) == G_VOID)
3303             scalarvoid(PL_eval_root);
3304         else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3305             list(PL_eval_root);
3306         else
3307             scalar(PL_eval_root);
3308
3309         PL_eval_start = op_linklist(PL_eval_root);
3310         PL_eval_root->op_private |= OPpREFCOUNTED;
3311         OpREFCNT_set(PL_eval_root, 1);
3312         PL_eval_root->op_next = 0;
3313         i = PL_savestack_ix;
3314         SAVEFREEOP(o);
3315         ENTER;
3316         CALL_PEEP(PL_eval_start);
3317         finalize_optree(PL_eval_root);
3318         S_prune_chain_head(aTHX_ &PL_eval_start);
3319         LEAVE;
3320         PL_savestack_ix = i;
3321     }
3322     else {
3323         if (o->op_type == OP_STUB) {
3324             /* This block is entered if nothing is compiled for the main
3325                program. This will be the case for an genuinely empty main
3326                program, or one which only has BEGIN blocks etc, so already
3327                run and freed.
3328
3329                Historically (5.000) the guard above was !o. However, commit
3330                f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3331                c71fccf11fde0068, changed perly.y so that newPROG() is now
3332                called with the output of block_end(), which returns a new
3333                OP_STUB for the case of an empty optree. ByteLoader (and
3334                maybe other things) also take this path, because they set up
3335                PL_main_start and PL_main_root directly, without generating an
3336                optree.
3337
3338                If the parsing the main program aborts (due to parse errors,
3339                or due to BEGIN or similar calling exit), then newPROG()
3340                isn't even called, and hence this code path and its cleanups
3341                are skipped. This shouldn't make a make a difference:
3342                * a non-zero return from perl_parse is a failure, and
3343                  perl_destruct() should be called immediately.
3344                * however, if exit(0) is called during the parse, then
3345                  perl_parse() returns 0, and perl_run() is called. As
3346                  PL_main_start will be NULL, perl_run() will return
3347                  promptly, and the exit code will remain 0.
3348             */
3349
3350             PL_comppad_name = 0;
3351             PL_compcv = 0;
3352             S_op_destroy(aTHX_ o);
3353             return;
3354         }
3355         PL_main_root = op_scope(sawparens(scalarvoid(o)));
3356         PL_curcop = &PL_compiling;
3357         PL_main_start = LINKLIST(PL_main_root);
3358         PL_main_root->op_private |= OPpREFCOUNTED;
3359         OpREFCNT_set(PL_main_root, 1);
3360         PL_main_root->op_next = 0;
3361         CALL_PEEP(PL_main_start);
3362         finalize_optree(PL_main_root);
3363         S_prune_chain_head(aTHX_ &PL_main_start);
3364         cv_forget_slab(PL_compcv);
3365         PL_compcv = 0;
3366
3367         /* Register with debugger */
3368         if (PERLDB_INTER) {
3369             CV * const cv = get_cvs("DB::postponed", 0);
3370             if (cv) {
3371                 dSP;
3372                 PUSHMARK(SP);
3373                 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3374                 PUTBACK;
3375                 call_sv(MUTABLE_SV(cv), G_DISCARD);
3376             }
3377         }
3378     }
3379 }
3380
3381 OP *
3382 Perl_localize(pTHX_ OP *o, I32 lex)
3383 {
3384     dVAR;
3385
3386     PERL_ARGS_ASSERT_LOCALIZE;
3387
3388     if (o->op_flags & OPf_PARENS)
3389 /* [perl #17376]: this appears to be premature, and results in code such as
3390    C< our(%x); > executing in list mode rather than void mode */
3391 #if 0
3392         list(o);
3393 #else
3394         NOOP;
3395 #endif
3396     else {
3397         if ( PL_parser->bufptr > PL_parser->oldbufptr
3398             && PL_parser->bufptr[-1] == ','
3399             && ckWARN(WARN_PARENTHESIS))
3400         {
3401             char *s = PL_parser->bufptr;
3402             bool sigil = FALSE;
3403
3404             /* some heuristics to detect a potential error */
3405             while (*s && (strchr(", \t\n", *s)))
3406                 s++;
3407
3408             while (1) {
3409                 if (*s && strchr("@$%*", *s) && *++s
3410                        && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
3411                     s++;
3412                     sigil = TRUE;
3413                     while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
3414                         s++;
3415                     while (*s && (strchr(", \t\n", *s)))
3416                         s++;
3417                 }
3418                 else
3419                     break;
3420             }
3421             if (sigil && (*s == ';' || *s == '=')) {
3422                 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3423                                 "Parentheses missing around \"%s\" list",
3424                                 lex
3425                                     ? (PL_parser->in_my == KEY_our
3426                                         ? "our"
3427                                         : PL_parser->in_my == KEY_state
3428                                             ? "state"
3429                                             : "my")
3430                                     : "local");
3431             }
3432         }
3433     }
3434     if (lex)
3435         o = my(o);
3436     else
3437         o = op_lvalue(o, OP_NULL);              /* a bit kludgey */
3438     PL_parser->in_my = FALSE;
3439     PL_parser->in_my_stash = NULL;
3440     return o;
3441 }
3442
3443 OP *
3444 Perl_jmaybe(pTHX_ OP *o)
3445 {
3446     PERL_ARGS_ASSERT_JMAYBE;
3447
3448     if (o->op_type == OP_LIST) {
3449         OP * const o2
3450             = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3451         o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3452     }
3453     return o;
3454 }
3455
3456 PERL_STATIC_INLINE OP *
3457 S_op_std_init(pTHX_ OP *o)
3458 {
3459     I32 type = o->op_type;
3460
3461     PERL_ARGS_ASSERT_OP_STD_INIT;
3462
3463     if (PL_opargs[type] & OA_RETSCALAR)
3464         scalar(o);
3465     if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3466         o->op_targ = pad_alloc(type, SVs_PADTMP);
3467
3468     return o;
3469 }
3470
3471 PERL_STATIC_INLINE OP *
3472 S_op_integerize(pTHX_ OP *o)
3473 {
3474     I32 type = o->op_type;
3475
3476     PERL_ARGS_ASSERT_OP_INTEGERIZE;
3477
3478     /* integerize op. */
3479     if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3480     {
3481         dVAR;
3482         o->op_ppaddr = PL_ppaddr[++(o->op_type)];
3483     }
3484
3485     if (type == OP_NEGATE)
3486         /* XXX might want a ck_negate() for this */
3487         cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3488
3489     return o;
3490 }
3491
3492 static OP *
3493 S_fold_constants(pTHX_ OP *o)
3494 {
3495     dVAR;
3496     OP * VOL curop;
3497     OP *newop;
3498     VOL I32 type = o->op_type;
3499     SV * VOL sv = NULL;
3500     int ret = 0;
3501     I32 oldscope;
3502     OP *old_next;
3503     SV * const oldwarnhook = PL_warnhook;
3504     SV * const olddiehook  = PL_diehook;
3505     COP not_compiling;
3506     dJMPENV;
3507
3508     PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3509
3510     if (!(PL_opargs[type] & OA_FOLDCONST))
3511         goto nope;
3512
3513     switch (type) {
3514     case OP_UCFIRST:
3515     case OP_LCFIRST:
3516     case OP_UC:
3517     case OP_LC:
3518     case OP_FC:
3519     case OP_SLT:
3520     case OP_SGT:
3521     case OP_SLE:
3522     case OP_SGE:
3523     case OP_SCMP:
3524     case OP_SPRINTF:
3525         /* XXX what about the numeric ops? */
3526         if (IN_LOCALE_COMPILETIME)
3527             goto nope;
3528         break;
3529     case OP_PACK:
3530         if (!cLISTOPo->op_first->op_sibling
3531           || cLISTOPo->op_first->op_sibling->op_type != OP_CONST)
3532             goto nope;
3533         {
3534             SV * const sv = cSVOPx_sv(cLISTOPo->op_first->op_sibling);
3535             if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3536             {
3537                 const char *s = SvPVX_const(sv);
3538                 while (s < SvEND(sv)) {
3539                     if (*s == 'p' || *s == 'P') goto nope;
3540                     s++;
3541                 }
3542             }
3543         }
3544         break;
3545     case OP_REPEAT:
3546         if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3547         break;
3548     case OP_SREFGEN:
3549         if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
3550          || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
3551             goto nope;
3552     }
3553
3554     if (PL_parser && PL_parser->error_count)
3555         goto nope;              /* Don't try to run w/ errors */
3556
3557     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3558         const OPCODE type = curop->op_type;
3559         if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3560             type != OP_LIST &&
3561             type != OP_SCALAR &&
3562             type != OP_NULL &&
3563             type != OP_PUSHMARK)
3564         {
3565             goto nope;
3566         }
3567     }
3568
3569     curop = LINKLIST(o);
3570     old_next = o->op_next;
3571     o->op_next = 0;
3572     PL_op = curop;
3573
3574     oldscope = PL_scopestack_ix;
3575     create_eval_scope(G_FAKINGEVAL);
3576
3577     /* Verify that we don't need to save it:  */
3578     assert(PL_curcop == &PL_compiling);
3579     StructCopy(&PL_compiling, &not_compiling, COP);
3580     PL_curcop = &not_compiling;
3581     /* The above ensures that we run with all the correct hints of the
3582        currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3583     assert(IN_PERL_RUNTIME);
3584     PL_warnhook = PERL_WARNHOOK_FATAL;
3585     PL_diehook  = NULL;
3586     JMPENV_PUSH(ret);
3587
3588     switch (ret) {
3589     case 0:
3590         CALLRUNOPS(aTHX);
3591         sv = *(PL_stack_sp--);
3592         if (o->op_targ && sv == PAD_SV(o->op_targ)) {   /* grab pad temp? */
3593 #ifdef PERL_MAD
3594             /* Can't simply swipe the SV from the pad, because that relies on
3595                the op being freed "real soon now". Under MAD, this doesn't
3596                happen (see the #ifdef below).  */
3597             sv = newSVsv(sv);
3598 #else
3599             pad_swipe(o->op_targ,  FALSE);
3600 #endif
3601         }
3602         else if (SvTEMP(sv)) {                  /* grab mortal temp? */
3603             SvREFCNT_inc_simple_void(sv);
3604             SvTEMP_off(sv);
3605         }
3606         else { assert(SvIMMORTAL(sv)); }
3607         break;
3608     case 3:
3609         /* Something tried to die.  Abandon constant folding.  */
3610         /* Pretend the error never happened.  */
3611         CLEAR_ERRSV();
3612         o->op_next = old_next;
3613         break;
3614     default:
3615         JMPENV_POP;
3616         /* Don't expect 1 (setjmp failed) or 2 (something called my_exit)  */
3617         PL_warnhook = oldwarnhook;
3618         PL_diehook  = olddiehook;
3619         /* XXX note that this croak may fail as we've already blown away
3620          * the stack - eg any nested evals */
3621         Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3622     }
3623     JMPENV_POP;
3624     PL_warnhook = oldwarnhook;
3625     PL_diehook  = olddiehook;
3626     PL_curcop = &PL_compiling;
3627
3628     if (PL_scopestack_ix > oldscope)
3629         delete_eval_scope();
3630
3631     if (ret)
3632         goto nope;
3633
3634 #ifndef PERL_MAD
3635     op_free(o);
3636 #endif
3637     assert(sv);
3638     if (type == OP_STRINGIFY) SvPADTMP_off(sv);
3639     else if (!SvIMMORTAL(sv)) {
3640         SvPADTMP_on(sv);
3641         SvREADONLY_on(sv);
3642     }
3643     if (type == OP_RV2GV)
3644         newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3645     else
3646     {
3647         newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3648         if (type != OP_STRINGIFY) newop->op_folded = 1;
3649     }
3650     op_getmad(o,newop,'f');
3651     return newop;
3652
3653  nope:
3654     return o;
3655 }
3656
3657 static OP *
3658 S_gen_constant_list(pTHX_ OP *o)
3659 {
3660     dVAR;
3661     OP *curop;
3662     const SSize_t oldtmps_floor = PL_tmps_floor;
3663     SV **svp;
3664     AV *av;
3665
3666     list(o);
3667     if (PL_parser && PL_parser->error_count)
3668         return o;               /* Don't attempt to run with errors */
3669
3670     curop = LINKLIST(o);
3671     o->op_next = 0;
3672     CALL_PEEP(curop);
3673     S_prune_chain_head(aTHX_ &curop);
3674     PL_op = curop;
3675     Perl_pp_pushmark(aTHX);
3676     CALLRUNOPS(aTHX);
3677     PL_op = curop;
3678     assert (!(curop->op_flags & OPf_SPECIAL));
3679     assert(curop->op_type == OP_RANGE);
3680     Perl_pp_anonlist(aTHX);
3681     PL_tmps_floor = oldtmps_floor;
3682
3683     o->op_type = OP_RV2AV;
3684     o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3685     o->op_flags &= ~OPf_REF;    /* treat \(1..2) like an ordinary list */
3686     o->op_flags |= OPf_PARENS;  /* and flatten \(1..2,3) */
3687     o->op_opt = 0;              /* needs to be revisited in rpeep() */
3688     curop = ((UNOP*)o)->op_first;
3689     av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
3690     ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, (SV *)av);
3691     if (AvFILLp(av) != -1)
3692         for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
3693         {
3694             SvPADTMP_on(*svp);
3695             SvREADONLY_on(*svp);
3696         }
3697 #ifdef PERL_MAD
3698     op_getmad(curop,o,'O');
3699 #else
3700     op_free(curop);
3701 #endif
3702     LINKLIST(o);
3703     return list(o);
3704 }
3705
3706 OP *
3707 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3708 {
3709     dVAR;
3710     if (type < 0) type = -type, flags |= OPf_SPECIAL;
3711     if (!o || o->op_type != OP_LIST)
3712         o = newLISTOP(OP_LIST, 0, o, NULL);
3713     else
3714         o->op_flags &= ~OPf_WANT;
3715
3716     if (!(PL_opargs[type] & OA_MARK))
3717         op_null(cLISTOPo->op_first);
3718     else {
3719         OP * const kid2 = cLISTOPo->op_first->op_sibling;
3720         if (kid2 && kid2->op_type == OP_COREARGS) {
3721             op_null(cLISTOPo->op_first);
3722             kid2->op_private |= OPpCOREARGS_PUSHMARK;
3723         }
3724     }   
3725
3726     o->op_type = (OPCODE)type;
3727     o->op_ppaddr = PL_ppaddr[type];
3728     o->op_flags |= flags;
3729
3730     o = CHECKOP(type, o);
3731     if (o->op_type != (unsigned)type)
3732         return o;
3733
3734     return fold_constants(op_integerize(op_std_init(o)));
3735 }
3736
3737 /*
3738 =head1 Optree Manipulation Functions
3739 */
3740
3741 /* List constructors */
3742
3743 /*
3744 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3745
3746 Append an item to the list of ops contained directly within a list-type
3747 op, returning the lengthened list.  I<first> is the list-type op,
3748 and I<last> is the op to append to the list.  I<optype> specifies the
3749 intended opcode for the list.  If I<first> is not already a list of the
3750 right type, it will be upgraded into one.  If either I<first> or I<last>
3751 is null, the other is returned unchanged.
3752
3753 =cut
3754 */
3755
3756 OP *
3757 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3758 {
3759     if (!first)
3760         return last;
3761
3762     if (!last)
3763         return first;
3764
3765     if (first->op_type != (unsigned)type
3766         || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3767     {
3768         return newLISTOP(type, 0, first, last);
3769     }
3770
3771     if (first->op_flags & OPf_KIDS)
3772         ((LISTOP*)first)->op_last->op_sibling = last;
3773     else {
3774         first->op_flags |= OPf_KIDS;
3775         ((LISTOP*)first)->op_first = last;
3776     }
3777     ((LISTOP*)first)->op_last = last;
3778     return first;
3779 }
3780
3781 /*
3782 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3783
3784 Concatenate the lists of ops contained directly within two list-type ops,
3785 returning the combined list.  I<first> and I<last> are the list-type ops
3786 to concatenate.  I<optype> specifies the intended opcode for the list.
3787 If either I<first> or I<last> is not already a list of the right type,
3788 it will be upgraded into one.  If either I<first> or I<last> is null,
3789 the other is returned unchanged.
3790
3791 =cut
3792 */
3793
3794 OP *
3795 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3796 {
3797     if (!first)
3798         return last;
3799
3800     if (!last)
3801         return first;
3802
3803     if (first->op_type != (unsigned)type)
3804         return op_prepend_elem(type, first, last);
3805
3806     if (last->op_type != (unsigned)type)
3807         return op_append_elem(type, first, last);
3808
3809     ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3810     ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3811     first->op_flags |= (last->op_flags & OPf_KIDS);
3812
3813 #ifdef PERL_MAD
3814     if (((LISTOP*)last)->op_first && first->op_madprop) {
3815         MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3816         if (mp) {
3817             while (mp->mad_next)
3818                 mp = mp->mad_next;
3819             mp->mad_next = first->op_madprop;
3820         }
3821         else {
3822             ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3823         }
3824     }
3825     first->op_madprop = last->op_madprop;
3826     last->op_madprop = 0;
3827 #endif
3828
3829     S_op_destroy(aTHX_ last);
3830
3831     return first;
3832 }
3833
3834 /*
3835 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3836
3837 Prepend an item to the list of ops contained directly within a list-type
3838 op, returning the lengthened list.  I<first> is the op to prepend to the
3839 list, and I<last> is the list-type op.  I<optype> specifies the intended
3840 opcode for the list.  If I<last> is not already a list of the right type,
3841 it will be upgraded into one.  If either I<first> or I<last> is null,
3842 the other is returned unchanged.
3843
3844 =cut
3845 */
3846
3847 OP *
3848 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3849 {
3850     if (!first)
3851         return last;
3852
3853     if (!last)
3854         return first;
3855
3856     if (last->op_type == (unsigned)type) {
3857         if (type == OP_LIST) {  /* already a PUSHMARK there */
3858             first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3859             ((LISTOP*)last)->op_first->op_sibling = first;
3860             if (!(first->op_flags & OPf_PARENS))
3861                 last->op_flags &= ~OPf_PARENS;
3862         }
3863         else {
3864             if (!(last->op_flags & OPf_KIDS)) {
3865                 ((LISTOP*)last)->op_last = first;
3866                 last->op_flags |= OPf_KIDS;
3867             }
3868             first->op_sibling = ((LISTOP*)last)->op_first;
3869             ((LISTOP*)last)->op_first = first;
3870         }
3871         last->op_flags |= OPf_KIDS;
3872         return last;
3873     }
3874
3875     return newLISTOP(type, 0, first, last);
3876 }
3877
3878 /* Constructors */
3879
3880 #ifdef PERL_MAD
3881  
3882 TOKEN *
3883 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3884 {
3885     TOKEN *tk;
3886     Newxz(tk, 1, TOKEN);
3887     tk->tk_type = (OPCODE)optype;
3888     tk->tk_type = 12345;
3889     tk->tk_lval = lval;
3890     tk->tk_mad = madprop;
3891     return tk;
3892 }
3893
3894 void
3895 Perl_token_free(pTHX_ TOKEN* tk)
3896 {
3897     PERL_ARGS_ASSERT_TOKEN_FREE;
3898
3899     if (tk->tk_type != 12345)
3900         return;
3901     mad_free(tk->tk_mad);
3902     Safefree(tk);
3903 }
3904
3905 void
3906 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3907 {
3908     MADPROP* mp;
3909     MADPROP* tm;
3910
3911     PERL_ARGS_ASSERT_TOKEN_GETMAD;
3912
3913     if (tk->tk_type != 12345) {
3914         Perl_warner(aTHX_ packWARN(WARN_MISC),
3915              "Invalid TOKEN object ignored");
3916         return;
3917     }
3918     tm = tk->tk_mad;
3919     if (!tm)
3920         return;
3921
3922     /* faked up qw list? */
3923     if (slot == '(' &&
3924         tm->mad_type == MAD_SV &&
3925         SvPVX((SV *)tm->mad_val)[0] == 'q')
3926             slot = 'x';
3927
3928     if (o) {
3929         mp = o->op_madprop;
3930         if (mp) {
3931             for (;;) {
3932                 /* pretend constant fold didn't happen? */
3933                 if (mp->mad_key == 'f' &&
3934                     (o->op_type == OP_CONST ||
3935                      o->op_type == OP_GV) )
3936                 {
3937                     token_getmad(tk,(OP*)mp->mad_val,slot);
3938                     return;
3939                 }
3940                 if (!mp->mad_next)
3941                     break;
3942                 mp = mp->mad_next;
3943             }
3944             mp->mad_next = tm;
3945             mp = mp->mad_next;
3946         }
3947         else {
3948             o->op_madprop = tm;
3949             mp = o->op_madprop;
3950         }
3951         if (mp->mad_key == 'X')
3952             mp->mad_key = slot; /* just change the first one */
3953
3954         tk->tk_mad = 0;
3955     }
3956     else
3957         mad_free(tm);
3958     Safefree(tk);
3959 }
3960
3961 void
3962 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3963 {
3964     MADPROP* mp;
3965     if (!from)
3966         return;
3967     if (o) {
3968         mp = o->op_madprop;
3969         if (mp) {
3970             for (;;) {
3971                 /* pretend constant fold didn't happen? */
3972                 if (mp->mad_key == 'f' &&
3973                     (o->op_type == OP_CONST ||
3974                      o->op_type == OP_GV) )
3975                 {
3976                     op_getmad(from,(OP*)mp->mad_val,slot);
3977                     return;
3978                 }
3979                 if (!mp->mad_next)
3980                     break;
3981                 mp = mp->mad_next;
3982             }
3983             mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3984         }
3985         else {
3986             o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3987         }
3988     }
3989 }
3990
3991 void
3992 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3993 {
3994     MADPROP* mp;
3995     if (!from)
3996         return;
3997     if (o) {
3998         mp = o->op_madprop;
3999         if (mp) {
4000             for (;;) {
4001                 /* pretend constant fold didn't happen? */
4002                 if (mp->mad_key == 'f' &&
4003                     (o->op_type == OP_CONST ||
4004                      o->op_type == OP_GV) )
4005                 {
4006                     op_getmad(from,(OP*)mp->mad_val,slot);
4007                     return;
4008                 }
4009                 if (!mp->mad_next)
4010                     break;
4011                 mp = mp->mad_next;
4012             }
4013             mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
4014         }
4015         else {
4016             o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
4017         }
4018     }
4019     else {
4020         PerlIO_printf(PerlIO_stderr(),
4021                       "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
4022         op_free(from);
4023     }
4024 }
4025
4026 void
4027 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
4028 {
4029     MADPROP* tm;
4030     if (!mp || !o)
4031         return;
4032     if (slot)
4033         mp->mad_key = slot;
4034     tm = o->op_madprop;
4035     o->op_madprop = mp;
4036     for (;;) {
4037         if (!mp->mad_next)
4038             break;
4039         mp = mp->mad_next;
4040     }
4041     mp->mad_next = tm;
4042 }
4043
4044 void
4045 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
4046 {
4047     if (!o)
4048         return;
4049     addmad(tm, &(o->op_madprop), slot);
4050 }
4051
4052 void
4053 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
4054 {
4055     MADPROP* mp;
4056     if (!tm || !root)
4057         return;
4058     if (slot)
4059         tm->mad_key = slot;
4060     mp = *root;
4061     if (!mp) {
4062         *root = tm;
4063         return;
4064     }
4065     for (;;) {
4066         if (!mp->mad_next)
4067             break;
4068         mp = mp->mad_next;
4069     }
4070     mp->mad_next = tm;
4071 }
4072
4073 MADPROP *
4074 Perl_newMADsv(pTHX_ char key, SV* sv)
4075 {
4076     PERL_ARGS_ASSERT_NEWMADSV;
4077
4078     return newMADPROP(key, MAD_SV, sv, 0);
4079 }
4080
4081 MADPROP *
4082 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
4083 {
4084     MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
4085     mp->mad_next = 0;
4086     mp->mad_key = key;
4087     mp->mad_vlen = vlen;
4088     mp->mad_type = type;
4089     mp->mad_val = val;
4090 /*    PerlIO_printf(PerlIO_stderr(), "NEW  mp = %0x\n", mp);  */
4091     return mp;
4092 }
4093
4094 void
4095 Perl_mad_free(pTHX_ MADPROP* mp)
4096 {
4097 /*    PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
4098     if (!mp)
4099         return;
4100     if (mp->mad_next)
4101         mad_free(mp->mad_next);
4102 /*    if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
4103         PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
4104     switch (mp->mad_type) {
4105     case MAD_NULL:
4106         break;
4107     case MAD_PV:
4108         Safefree(mp->mad_val);
4109         break;
4110     case MAD_OP:
4111         if (mp->mad_vlen)       /* vlen holds "strong/weak" boolean */
4112             op_free((OP*)mp->mad_val);
4113         break;
4114     case MAD_SV:
4115         sv_free(MUTABLE_SV(mp->mad_val));
4116         break;
4117     default:
4118         PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
4119         break;
4120     }
4121     PerlMemShared_free(mp);
4122 }
4123
4124 #endif
4125
4126 /*
4127 =head1 Optree construction
4128
4129 =for apidoc Am|OP *|newNULLLIST
4130
4131 Constructs, checks, and returns a new C<stub> op, which represents an
4132 empty list expression.
4133
4134 =cut
4135 */
4136
4137 OP *
4138 Perl_newNULLLIST(pTHX)
4139 {
4140     return newOP(OP_STUB, 0);
4141 }
4142
4143 static OP *
4144 S_force_list(pTHX_ OP *o)
4145 {
4146     if (!o || o->op_type != OP_LIST)
4147         o = newLISTOP(OP_LIST, 0, o, NULL);
4148     op_null(o);
4149     return o;
4150 }
4151
4152 /*
4153 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4154
4155 Constructs, checks, and returns an op of any list type.  I<type> is
4156 the opcode.  I<flags> gives the eight bits of C<op_flags>, except that
4157 C<OPf_KIDS> will be set automatically if required.  I<first> and I<last>
4158 supply up to two ops to be direct children of the list op; they are
4159 consumed by this function and become part of the constructed op tree.
4160
4161 =cut
4162 */
4163
4164 OP *
4165 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4166 {
4167     dVAR;
4168     LISTOP *listop;
4169
4170     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
4171
4172     NewOp(1101, listop, 1, LISTOP);
4173
4174     listop->op_type = (OPCODE)type;
4175     listop->op_ppaddr = PL_ppaddr[type];
4176     if (first || last)
4177         flags |= OPf_KIDS;
4178     listop->op_flags = (U8)flags;
4179
4180     if (!last && first)
4181         last = first;
4182     else if (!first && last)
4183         first = last;
4184     else if (first)
4185         first->op_sibling = last;
4186     listop->op_first = first;
4187     listop->op_last = last;
4188     if (type == OP_LIST) {
4189         OP* const pushop = newOP(OP_PUSHMARK, 0);
4190         pushop->op_sibling = first;
4191         listop->op_first = pushop;
4192         listop->op_flags |= OPf_KIDS;
4193         if (!last)
4194             listop->op_last = pushop;
4195     }
4196
4197     return CHECKOP(type, listop);
4198 }
4199
4200 /*
4201 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4202
4203 Constructs, checks, and returns an op of any base type (any type that
4204 has no extra fields).  I<type> is the opcode.  I<flags> gives the
4205 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4206 of C<op_private>.
4207
4208 =cut
4209 */
4210
4211 OP *
4212 Perl_newOP(pTHX_ I32 type, I32 flags)
4213 {
4214     dVAR;
4215     OP *o;
4216
4217     if (type == -OP_ENTEREVAL) {
4218         type = OP_ENTEREVAL;
4219         flags |= OPpEVAL_BYTES<<8;
4220     }
4221
4222     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4223         || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4224         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4225         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4226
4227     NewOp(1101, o, 1, OP);
4228     o->op_type = (OPCODE)type;
4229     o->op_ppaddr = PL_ppaddr[type];
4230     o->op_flags = (U8)flags;
4231
4232     o->op_next = o;
4233     o->op_private = (U8)(0 | (flags >> 8));
4234     if (PL_opargs[type] & OA_RETSCALAR)
4235         scalar(o);
4236     if (PL_opargs[type] & OA_TARGET)
4237         o->op_targ = pad_alloc(type, SVs_PADTMP);
4238     return CHECKOP(type, o);
4239 }
4240
4241 /*
4242 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4243
4244 Constructs, checks, and returns an op of any unary type.  I<type> is
4245 the opcode.  I<flags> gives the eight bits of C<op_flags>, except that
4246 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4247 bits, the eight bits of C<op_private>, except that the bit with value 1
4248 is automatically set.  I<first> supplies an optional op to be the direct
4249 child of the unary op; it is consumed by this function and become part
4250 of the constructed op tree.
4251
4252 =cut
4253 */
4254
4255 OP *
4256 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4257 {
4258     dVAR;
4259     UNOP *unop;
4260
4261     if (type == -OP_ENTEREVAL) {
4262         type = OP_ENTEREVAL;
4263         flags |= OPpEVAL_BYTES<<8;
4264     }
4265
4266     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4267         || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4268         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4269         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4270         || type == OP_SASSIGN
4271         || type == OP_ENTERTRY
4272         || type == OP_NULL );
4273
4274     if (!first)
4275         first = newOP(OP_STUB, 0);
4276     if (PL_opargs[type] & OA_MARK)
4277         first = force_list(first);
4278
4279     NewOp(1101, unop, 1, UNOP);
4280     unop->op_type = (OPCODE)type;
4281     unop->op_ppaddr = PL_ppaddr[type];
4282     unop->op_first = first;
4283     unop->op_flags = (U8)(flags | OPf_KIDS);
4284     unop->op_private = (U8)(1 | (flags >> 8));
4285     unop = (UNOP*) CHECKOP(type, unop);
4286     if (unop->op_next)
4287         return (OP*)unop;
4288
4289     return fold_constants(op_integerize(op_std_init((OP *) unop)));
4290 }
4291
4292 /*
4293 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
4294
4295 Constructs, checks, and returns an op of any binary type.  I<type>
4296 is the opcode.  I<flags> gives the eight bits of C<op_flags>, except
4297 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
4298 the eight bits of C<op_private>, except that the bit with value 1 or
4299 2 is automatically set as required.  I<first> and I<last> supply up to
4300 two ops to be the direct children of the binary op; they are consumed
4301 by this function and become part of the constructed op tree.
4302
4303 =cut
4304 */
4305
4306 OP *
4307 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4308 {
4309     dVAR;
4310     BINOP *binop;
4311
4312     ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4313         || type == OP_SASSIGN || type == OP_NULL );
4314
4315     NewOp(1101, binop, 1, BINOP);
4316
4317     if (!first)
4318         first = newOP(OP_NULL, 0);
4319
4320     binop->op_type = (OPCODE)type;
4321     binop->op_ppaddr = PL_ppaddr[type];
4322     binop->op_first = first;
4323     binop->op_flags = (U8)(flags | OPf_KIDS);
4324     if (!last) {
4325         last = first;
4326         binop->op_private = (U8)(1 | (flags >> 8));
4327     }
4328     else {
4329         binop->op_private = (U8)(2 | (flags >> 8));
4330         first->op_sibling = last;
4331     }
4332
4333     binop = (BINOP*)CHECKOP(type, binop);
4334     if (binop->op_next || binop->op_type != (OPCODE)type)
4335         return (OP*)binop;
4336
4337     binop->op_last = binop->op_first->op_sibling;
4338
4339     return fold_constants(op_integerize(op_std_init((OP *)binop)));
4340 }
4341
4342 static int uvcompare(const void *a, const void *b)
4343     __attribute__nonnull__(1)
4344     __attribute__nonnull__(2)
4345     __attribute__pure__;
4346 static int uvcompare(const void *a, const void *b)
4347 {
4348     if (*((const UV *)a) < (*(const UV *)b))
4349         return -1;
4350     if (*((const UV *)a) > (*(const UV *)b))
4351         return 1;
4352     if (*((const UV *)a+1) < (*(const UV *)b+1))
4353         return -1;
4354     if (*((const UV *)a+1) > (*(const UV *)b+1))
4355         return 1;
4356     return 0;
4357 }
4358
4359 static OP *
4360 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4361 {
4362     dVAR;
4363     SV * const tstr = ((SVOP*)expr)->op_sv;
4364     SV * const rstr =
4365 #ifdef PERL_MAD
4366                         (repl->op_type == OP_NULL)
4367                             ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
4368 #endif
4369                               ((SVOP*)repl)->op_sv;
4370     STRLEN tlen;
4371     STRLEN rlen;
4372     const U8 *t = (U8*)SvPV_const(tstr, tlen);
4373     const U8 *r = (U8*)SvPV_const(rstr, rlen);
4374     I32 i;
4375     I32 j;
4376     I32 grows = 0;
4377     short *tbl;
4378
4379     const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4380     const I32 squash     = o->op_private & OPpTRANS_SQUASH;
4381     I32 del              = o->op_private & OPpTRANS_DELETE;
4382     SV* swash;
4383
4384     PERL_ARGS_ASSERT_PMTRANS;
4385
4386     PL_hints |= HINT_BLOCK_SCOPE;
4387
4388     if (SvUTF8(tstr))
4389         o->op_private |= OPpTRANS_FROM_UTF;
4390
4391     if (SvUTF8(rstr))
4392         o->op_private |= OPpTRANS_TO_UTF;
4393
4394     if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4395         SV* const listsv = newSVpvs("# comment\n");
4396         SV* transv = NULL;
4397         const U8* tend = t + tlen;
4398         const U8* rend = r + rlen;
4399         STRLEN ulen;
4400         UV tfirst = 1;
4401         UV tlast = 0;
4402         IV tdiff;
4403         UV rfirst = 1;
4404         UV rlast = 0;
4405         IV rdiff;
4406         IV diff;
4407         I32 none = 0;
4408         U32 max = 0;
4409         I32 bits;
4410         I32 havefinal = 0;
4411         U32 final = 0;
4412         const I32 from_utf  = o->op_private & OPpTRANS_FROM_UTF;
4413         const I32 to_utf    = o->op_private & OPpTRANS_TO_UTF;
4414         U8* tsave = NULL;
4415         U8* rsave = NULL;
4416         const U32 flags = UTF8_ALLOW_DEFAULT;
4417
4418         if (!from_utf) {
4419             STRLEN len = tlen;
4420             t = tsave = bytes_to_utf8(t, &len);
4421             tend = t + len;
4422         }
4423         if (!to_utf && rlen) {
4424             STRLEN len = rlen;
4425             r = rsave = bytes_to_utf8(r, &len);
4426             rend = r + len;
4427         }
4428
4429 /* There is a  snag with this code on EBCDIC: scan_const() in toke.c has
4430  * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
4431  * odd.  */
4432
4433         if (complement) {
4434             U8 tmpbuf[UTF8_MAXBYTES+1];
4435             UV *cp;
4436             UV nextmin = 0;
4437             Newx(cp, 2*tlen, UV);
4438             i = 0;
4439             transv = newSVpvs("");
4440             while (t < tend) {
4441                 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4442                 t += ulen;
4443                 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
4444                     t++;
4445                     cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4446                     t += ulen;
4447                 }
4448                 else {
4449                  cp[2*i+1] = cp[2*i];
4450                 }
4451                 i++;
4452             }
4453             qsort(cp, i, 2*sizeof(UV), uvcompare);
4454             for (j = 0; j < i; j++) {
4455                 UV  val = cp[2*j];
4456                 diff = val - nextmin;
4457                 if (diff > 0) {
4458                     t = uvchr_to_utf8(tmpbuf,nextmin);
4459                     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4460                     if (diff > 1) {
4461                         U8  range_mark = ILLEGAL_UTF8_BYTE;
4462                         t = uvchr_to_utf8(tmpbuf, val - 1);
4463                         sv_catpvn(transv, (char *)&range_mark, 1);
4464                         sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4465                     }
4466                 }
4467                 val = cp[2*j+1];
4468                 if (val >= nextmin)
4469                     nextmin = val + 1;
4470             }
4471             t = uvchr_to_utf8(tmpbuf,nextmin);
4472             sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4473             {
4474                 U8 range_mark = ILLEGAL_UTF8_BYTE;
4475                 sv_catpvn(transv, (char *)&range_mark, 1);
4476             }
4477             t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
4478             sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4479             t = (const U8*)SvPVX_const(transv);
4480             tlen = SvCUR(transv);
4481             tend = t + tlen;
4482             Safefree(cp);
4483         }
4484         else if (!rlen && !del) {
4485             r = t; rlen = tlen; rend = tend;
4486         }
4487         if (!squash) {
4488                 if ((!rlen && !del) || t == r ||
4489                     (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4490                 {
4491                     o->op_private |= OPpTRANS_IDENTICAL;
4492                 }
4493         }
4494
4495         while (t < tend || tfirst <= tlast) {
4496             /* see if we need more "t" chars */
4497             if (tfirst > tlast) {
4498                 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4499                 t += ulen;
4500                 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {      /* illegal utf8 val indicates range */
4501                     t++;
4502                     tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4503                     t += ulen;
4504                 }
4505                 else
4506                     tlast = tfirst;
4507             }
4508
4509             /* now see if we need more "r" chars */
4510             if (rfirst > rlast) {
4511                 if (r < rend) {
4512                     rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4513                     r += ulen;
4514                     if (r < rend && *r == ILLEGAL_UTF8_BYTE) {  /* illegal utf8 val indicates range */
4515                         r++;
4516                         rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4517                         r += ulen;
4518                     }
4519                     else
4520                         rlast = rfirst;
4521                 }
4522                 else {
4523                     if (!havefinal++)
4524                         final = rlast;
4525                     rfirst = rlast = 0xffffffff;
4526                 }
4527             }
4528
4529             /* now see which range will peter our first, if either. */
4530             tdiff = tlast - tfirst;
4531             rdiff = rlast - rfirst;
4532
4533             if (tdiff <= rdiff)
4534                 diff = tdiff;
4535             else
4536                 diff = rdiff;
4537
4538             if (rfirst == 0xffffffff) {
4539                 diff = tdiff;   /* oops, pretend rdiff is infinite */
4540                 if (diff > 0)
4541                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4542                                    (long)tfirst, (long)tlast);
4543                 else
4544                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4545             }
4546             else {
4547                 if (diff > 0)
4548                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4549                                    (long)tfirst, (long)(tfirst + diff),
4550                                    (long)rfirst);
4551                 else
4552                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4553                                    (long)tfirst, (long)rfirst);
4554
4555                 if (rfirst + diff > max)
4556                     max = rfirst + diff;
4557                 if (!grows)
4558                     grows = (tfirst < rfirst &&
4559                              UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4560                 rfirst += diff + 1;
4561             }
4562             tfirst += diff + 1;
4563         }
4564
4565         none = ++max;
4566         if (del)
4567             del = ++max;
4568
4569         if (max > 0xffff)
4570             bits = 32;
4571         else if (max > 0xff)
4572             bits = 16;
4573         else
4574             bits = 8;
4575
4576         swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4577 #ifdef USE_ITHREADS
4578         cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
4579         SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4580         PAD_SETSV(cPADOPo->op_padix, swash);
4581         SvPADTMP_on(swash);
4582         SvREADONLY_on(swash);
4583 #else
4584         cSVOPo->op_sv = swash;
4585 #endif
4586         SvREFCNT_dec(listsv);
4587         SvREFCNT_dec(transv);
4588
4589         if (!del && havefinal && rlen)
4590             (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4591                            newSVuv((UV)final), 0);
4592
4593         if (grows)
4594             o->op_private |= OPpTRANS_GROWS;
4595
4596         Safefree(tsave);
4597         Safefree(rsave);
4598
4599 #ifdef PERL_MAD
4600         op_getmad(expr,o,'e');
4601         op_getmad(repl,o,'r');
4602 #else
4603         op_free(expr);
4604         op_free(repl);
4605 #endif
4606         return o;
4607     }
4608
4609     tbl = (short*)PerlMemShared_calloc(
4610         (o->op_private & OPpTRANS_COMPLEMENT) &&
4611             !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4612         sizeof(short));
4613     cPVOPo->op_pv = (char*)tbl;
4614     if (complement) {
4615         for (i = 0; i < (I32)tlen; i++)
4616             tbl[t[i]] = -1;
4617         for (i = 0, j = 0; i < 256; i++) {
4618             if (!tbl[i]) {
4619                 if (j >= (I32)rlen) {
4620                     if (del)
4621                         tbl[i] = -2;
4622                     else if (rlen)
4623                         tbl[i] = r[j-1];
4624                     else
4625                         tbl[i] = (short)i;
4626                 }
4627                 else {
4628                     if (i < 128 && r[j] >= 128)
4629                         grows = 1;
4630                     tbl[i] = r[j++];
4631                 }
4632             }
4633         }
4634         if (!del) {
4635             if (!rlen) {
4636                 j = rlen;
4637                 if (!squash)
4638                     o->op_private |= OPpTRANS_IDENTICAL;
4639             }
4640             else if (j >= (I32)rlen)
4641                 j = rlen - 1;
4642             else {
4643                 tbl = 
4644                     (short *)
4645                     PerlMemShared_realloc(tbl,
4646                                           (0x101+rlen-j) * sizeof(short));
4647                 cPVOPo->op_pv = (char*)tbl;
4648             }
4649             tbl[0x100] = (short)(rlen - j);
4650             for (i=0; i < (I32)rlen - j; i++)
4651                 tbl[0x101+i] = r[j+i];
4652         }
4653     }
4654     else {
4655         if (!rlen && !del) {
4656             r = t; rlen = tlen;
4657             if (!squash)
4658                 o->op_private |= OPpTRANS_IDENTICAL;
4659         }
4660         else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4661             o->op_private |= OPpTRANS_IDENTICAL;
4662         }
4663         for (i = 0; i < 256; i++)
4664             tbl[i] = -1;
4665         for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4666             if (j >= (I32)rlen) {
4667                 if (del) {
4668                     if (tbl[t[i]] == -1)
4669                         tbl[t[i]] = -2;
4670                     continue;
4671                 }
4672                 --j;
4673             }
4674             if (tbl[t[i]] == -1) {
4675                 if (t[i] < 128 && r[j] >= 128)
4676                     grows = 1;
4677                 tbl[t[i]] = r[j];
4678             }
4679         }
4680     }
4681
4682     if(del && rlen == tlen) {
4683         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); 
4684     } else if(rlen > tlen && !complement) {
4685         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4686     }
4687
4688     if (grows)
4689         o->op_private |= OPpTRANS_GROWS;
4690 #ifdef PERL_MAD
4691     op_getmad(expr,o,'e');
4692     op_getmad(repl,o,'r');
4693 #else
4694     op_free(expr);
4695     op_free(repl);
4696 #endif
4697
4698     return o;
4699 }
4700
4701 /*
4702 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4703
4704 Constructs, checks, and returns an op of any pattern matching type.
4705 I<type> is the opcode.  I<flags> gives the eight bits of C<op_flags>
4706 and, shifted up eight bits, the eight bits of C<op_private>.
4707
4708 =cut
4709 */
4710
4711 OP *
4712 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4713 {
4714     dVAR;
4715     PMOP *pmop;
4716
4717     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4718
4719     NewOp(1101, pmop, 1, PMOP);
4720     pmop->op_type = (OPCODE)type;
4721     pmop->op_ppaddr = PL_ppaddr[type];
4722     pmop->op_flags = (U8)flags;
4723     pmop->op_private = (U8)(0 | (flags >> 8));
4724
4725     if (PL_hints & HINT_RE_TAINT)
4726         pmop->op_pmflags |= PMf_RETAINT;
4727     if (IN_LOCALE_COMPILETIME) {
4728         set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4729     }
4730     else if ((! (PL_hints & HINT_BYTES))
4731                 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4732              && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4733     {
4734         set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4735     }
4736     if (PL_hints & HINT_RE_FLAGS) {
4737         SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4738          PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4739         );
4740         if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4741         reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4742          PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4743         );
4744         if (reflags && SvOK(reflags)) {
4745             set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4746         }
4747     }
4748
4749
4750 #ifdef USE_ITHREADS
4751     assert(SvPOK(PL_regex_pad[0]));
4752     if (SvCUR(PL_regex_pad[0])) {
4753         /* Pop off the "packed" IV from the end.  */
4754         SV *const repointer_list = PL_regex_pad[0];
4755         const char *p = SvEND(repointer_list) - sizeof(IV);
4756         const IV offset = *((IV*)p);
4757
4758         assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4759
4760         SvEND_set(repointer_list, p);
4761
4762         pmop->op_pmoffset = offset;
4763         /* This slot should be free, so assert this:  */
4764         assert(PL_regex_pad[offset] == &PL_sv_undef);
4765     } else {
4766         SV * const repointer = &PL_sv_undef;
4767         av_push(PL_regex_padav, repointer);
4768         pmop->op_pmoffset = av_tindex(PL_regex_padav);
4769         PL_regex_pad = AvARRAY(PL_regex_padav);
4770     }
4771 #endif
4772
4773     return CHECKOP(type, pmop);
4774 }
4775
4776 /* Given some sort of match op o, and an expression expr containing a
4777  * pattern, either compile expr into a regex and attach it to o (if it's
4778  * constant), or convert expr into a runtime regcomp op sequence (if it's
4779  * not)
4780  *
4781  * isreg indicates that the pattern is part of a regex construct, eg
4782  * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4783  * split "pattern", which aren't. In the former case, expr will be a list
4784  * if the pattern contains more than one term (eg /a$b/) or if it contains
4785  * a replacement, ie s/// or tr///.
4786  *
4787  * When the pattern has been compiled within a new anon CV (for
4788  * qr/(?{...})/ ), then floor indicates the savestack level just before
4789  * the new sub was created
4790  */
4791
4792 OP *
4793 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4794 {
4795     dVAR;
4796     PMOP *pm;
4797     LOGOP *rcop;
4798     I32 repl_has_vars = 0;
4799     OP* repl = NULL;
4800     bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4801     bool is_compiletime;
4802     bool has_code;
4803
4804     PERL_ARGS_ASSERT_PMRUNTIME;
4805
4806     /* for s/// and tr///, last element in list is the replacement; pop it */
4807
4808     if (is_trans || o->op_type == OP_SUBST) {
4809         OP* kid;
4810         repl = cLISTOPx(expr)->op_last;
4811         kid = cLISTOPx(expr)->op_first;
4812         while (kid->op_sibling != repl)
4813             kid = kid->op_sibling;
4814         kid->op_sibling = NULL;
4815         cLISTOPx(expr)->op_last = kid;
4816     }
4817
4818     /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4819
4820     if (is_trans) {
4821         OP* const oe = expr;
4822         assert(expr->op_type == OP_LIST);
4823         assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4824         assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4825         expr = cLISTOPx(oe)->op_last;
4826         cLISTOPx(oe)->op_first->op_sibling = NULL;
4827         cLISTOPx(oe)->op_last = NULL;
4828         op_free(oe);
4829
4830         return pmtrans(o, expr, repl);
4831     }
4832
4833     /* find whether we have any runtime or code elements;
4834      * at the same time, temporarily set the op_next of each DO block;
4835      * then when we LINKLIST, this will cause the DO blocks to be excluded
4836      * from the op_next chain (and from having LINKLIST recursively
4837      * applied to them). We fix up the DOs specially later */
4838
4839     is_compiletime = 1;
4840     has_code = 0;
4841     if (expr->op_type == OP_LIST) {
4842         OP *o;
4843         for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4844             if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4845                 has_code = 1;
4846                 assert(!o->op_next && o->op_sibling);
4847                 o->op_next = o->op_sibling;
4848             }
4849             else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4850                 is_compiletime = 0;
4851         }
4852     }
4853     else if (expr->op_type != OP_CONST)
4854         is_compiletime = 0;
4855
4856     LINKLIST(expr);
4857
4858     /* fix up DO blocks; treat each one as a separate little sub;
4859      * also, mark any arrays as LIST/REF */
4860
4861     if (expr->op_type == OP_LIST) {
4862         OP *o;
4863         for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4864
4865             if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
4866                 assert( !(o->op_flags  & OPf_WANT));
4867                 /* push the array rather than its contents. The regex
4868                  * engine will retrieve and join the elements later */
4869                 o->op_flags |= (OPf_WANT_LIST | OPf_REF);
4870                 continue;
4871             }
4872
4873             if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4874                 continue;
4875             o->op_next = NULL; /* undo temporary hack from above */
4876             scalar(o);
4877             LINKLIST(o);
4878             if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4879                 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first);
4880                 /* skip ENTER */
4881                 assert(leaveop->op_first->op_type == OP_ENTER);
4882                 assert(leaveop->op_first->op_sibling);
4883                 o->op_next = leaveop->op_first->op_sibling;
4884                 /* skip leave */
4885                 assert(leaveop->op_flags & OPf_KIDS);
4886                 assert(leaveop->op_last->op_next == (OP*)leaveop);
4887                 leaveop->op_next = NULL; /* stop on last op */
4888                 op_null((OP*)leaveop);
4889             }
4890             else {
4891                 /* skip SCOPE */
4892                 OP *scope = cLISTOPo->op_first;
4893                 assert(scope->op_type == OP_SCOPE);
4894                 assert(scope->op_flags & OPf_KIDS);
4895                 scope->op_next = NULL; /* stop on last op */
4896                 op_null(scope);
4897             }
4898             /* have to peep the DOs individually as we've removed it from
4899              * the op_next chain */
4900             CALL_PEEP(o);
4901             S_prune_chain_head(aTHX_ &(o->op_next));
4902             if (is_compiletime)
4903                 /* runtime finalizes as part of finalizing whole tree */
4904                 finalize_optree(o);
4905         }
4906     }
4907     else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
4908         assert( !(expr->op_flags  & OPf_WANT));
4909         /* push the array rather than its contents. The regex
4910          * engine will retrieve and join the elements later */
4911         expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
4912     }
4913
4914     PL_hints |= HINT_BLOCK_SCOPE;
4915     pm = (PMOP*)o;
4916     assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4917
4918     if (is_compiletime) {
4919         U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4920         regexp_engine const *eng = current_re_engine();
4921
4922         if (o->op_flags & OPf_SPECIAL)
4923             rx_flags |= RXf_SPLIT;
4924
4925         if (!has_code || !eng->op_comp) {
4926             /* compile-time simple constant pattern */
4927
4928             if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4929                 /* whoops! we guessed that a qr// had a code block, but we
4930                  * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4931                  * that isn't required now. Note that we have to be pretty
4932                  * confident that nothing used that CV's pad while the
4933                  * regex was parsed */
4934                 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4935                 /* But we know that one op is using this CV's slab. */
4936                 cv_forget_slab(PL_compcv);
4937                 LEAVE_SCOPE(floor);
4938                 pm->op_pmflags &= ~PMf_HAS_CV;
4939             }
4940
4941             PM_SETRE(pm,
4942                 eng->op_comp
4943                     ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4944                                         rx_flags, pm->op_pmflags)
4945                     : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4946                                         rx_flags, pm->op_pmflags)
4947             );
4948 #ifdef PERL_MAD
4949             op_getmad(expr,(OP*)pm,'e');
4950 #else
4951             op_free(expr);
4952 #endif
4953         }
4954         else {
4955             /* compile-time pattern that includes literal code blocks */
4956             REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4957                         rx_flags,
4958                         (pm->op_pmflags |
4959                             ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4960                     );
4961             PM_SETRE(pm, re);
4962             if (pm->op_pmflags & PMf_HAS_CV) {
4963                 CV *cv;
4964                 /* this QR op (and the anon sub we embed it in) is never
4965                  * actually executed. It's just a placeholder where we can
4966                  * squirrel away expr in op_code_list without the peephole
4967                  * optimiser etc processing it for a second time */
4968                 OP *qr = newPMOP(OP_QR, 0);
4969                 ((PMOP*)qr)->op_code_list = expr;
4970
4971                 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4972                 SvREFCNT_inc_simple_void(PL_compcv);
4973                 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4974                 ReANY(re)->qr_anoncv = cv;
4975
4976                 /* attach the anon CV to the pad so that
4977                  * pad_fixup_inner_anons() can find it */
4978                 (void)pad_add_anon(cv, o->op_type);
4979                 SvREFCNT_inc_simple_void(cv);
4980             }
4981             else {
4982                 pm->op_code_list = expr;
4983             }
4984         }
4985     }
4986     else {
4987         /* runtime pattern: build chain of regcomp etc ops */
4988         bool reglist;
4989         PADOFFSET cv_targ = 0;
4990
4991         reglist = isreg && expr->op_type == OP_LIST;
4992         if (reglist)
4993             op_null(expr);
4994
4995         if (has_code) {
4996             pm->op_code_list = expr;
4997             /* don't free op_code_list; its ops are embedded elsewhere too */
4998             pm->op_pmflags |= PMf_CODELIST_PRIVATE;
4999         }
5000
5001         if (o->op_flags & OPf_SPECIAL)
5002             pm->op_pmflags |= PMf_SPLIT;
5003
5004         /* the OP_REGCMAYBE is a placeholder in the non-threaded case
5005          * to allow its op_next to be pointed past the regcomp and
5006          * preceding stacking ops;
5007          * OP_REGCRESET is there to reset taint before executing the
5008          * stacking ops */
5009         if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
5010             expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
5011
5012         if (pm->op_pmflags & PMf_HAS_CV) {
5013             /* we have a runtime qr with literal code. This means
5014              * that the qr// has been wrapped in a new CV, which
5015              * means that runtime consts, vars etc will have been compiled
5016              * against a new pad. So... we need to execute those ops
5017              * within the environment of the new CV. So wrap them in a call
5018              * to a new anon sub. i.e. for
5019              *
5020              *     qr/a$b(?{...})/,
5021              *
5022              * we build an anon sub that looks like
5023              *
5024              *     sub { "a", $b, '(?{...})' }
5025              *
5026              * and call it, passing the returned list to regcomp.
5027              * Or to put it another way, the list of ops that get executed
5028              * are:
5029              *
5030              *     normal              PMf_HAS_CV
5031              *     ------              -------------------
5032              *                         pushmark (for regcomp)
5033              *                         pushmark (for entersub)
5034              *                         pushmark (for refgen)
5035              *                         anoncode
5036              *                         refgen
5037              *                         entersub
5038              *     regcreset                  regcreset
5039              *     pushmark                   pushmark
5040              *     const("a")                 const("a")
5041              *     gvsv(b)                    gvsv(b)
5042              *     const("(?{...})")          const("(?{...})")
5043              *                                leavesub
5044              *     regcomp             regcomp
5045              */
5046
5047             SvREFCNT_inc_simple_void(PL_compcv);
5048             /* these lines are just an unrolled newANONATTRSUB */
5049             expr = newSVOP(OP_ANONCODE, 0,
5050                     MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
5051             cv_targ = expr->op_targ;
5052             expr = newUNOP(OP_REFGEN, 0, expr);
5053
5054             expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
5055         }
5056
5057         NewOp(1101, rcop, 1, LOGOP);
5058         rcop->op_type = OP_REGCOMP;
5059         rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
5060         rcop->op_first = scalar(expr);
5061         rcop->op_flags |= OPf_KIDS
5062                             | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
5063                             | (reglist ? OPf_STACKED : 0);
5064         rcop->op_private = 0;
5065         rcop->op_other = o;
5066         rcop->op_targ = cv_targ;
5067
5068         /* /$x/ may cause an eval, since $x might be qr/(?{..})/  */
5069         if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
5070
5071         /* establish postfix order */
5072         if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
5073             LINKLIST(expr);
5074             rcop->op_next = expr;
5075             ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
5076         }
5077         else {
5078             rcop->op_next = LINKLIST(expr);
5079             expr->op_next = (OP*)rcop;
5080         }
5081
5082         op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
5083     }
5084
5085     if (repl) {
5086         OP *curop = repl;
5087         bool konst;
5088         /* If we are looking at s//.../e with a single statement, get past
5089            the implicit do{}. */
5090         if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
5091          && cUNOPx(curop)->op_first->op_type == OP_SCOPE
5092          && cUNOPx(curop)->op_first->op_flags & OPf_KIDS) {
5093             OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
5094             if (kid->op_type == OP_NULL && kid->op_sibling
5095              && !kid->op_sibling->op_sibling)
5096                 curop = kid->op_sibling;
5097         }
5098         if (curop->op_type == OP_CONST)
5099             konst = TRUE;
5100         else if (( (curop->op_type == OP_RV2SV ||
5101                     curop->op_type == OP_RV2AV ||
5102                     curop->op_type == OP_RV2HV ||
5103                     curop->op_type == OP_RV2GV)
5104                    && cUNOPx(curop)->op_first
5105                    && cUNOPx(curop)->op_first->op_type == OP_GV )
5106                 || curop->op_type == OP_PADSV
5107                 || curop->op_type == OP_PADAV
5108                 || curop->op_type == OP_PADHV
5109                 || curop->op_type == OP_PADANY) {
5110             repl_has_vars = 1;
5111             konst = TRUE;
5112         }
5113         else konst = FALSE;
5114         if (konst
5115             && !(repl_has_vars
5116                  && (!PM_GETRE(pm)
5117                      || !RX_PRELEN(PM_GETRE(pm))
5118                      || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
5119         {
5120             pm->op_pmflags |= PMf_CONST;        /* const for long enough */
5121             op_prepend_elem(o->op_type, scalar(repl), o);
5122         }
5123         else {
5124             NewOp(1101, rcop, 1, LOGOP);
5125             rcop->op_type = OP_SUBSTCONT;
5126             rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
5127             rcop->op_first = scalar(repl);
5128             rcop->op_flags |= OPf_KIDS;
5129             rcop->op_private = 1;
5130             rcop->op_other = o;
5131
5132             /* establish postfix order */
5133             rcop->op_next = LINKLIST(repl);
5134             repl->op_next = (OP*)rcop;
5135
5136             pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
5137             assert(!(pm->op_pmflags & PMf_ONCE));
5138             pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
5139             rcop->op_next = 0;
5140         }
5141     }
5142
5143     return (OP*)pm;
5144 }
5145
5146 /*
5147 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
5148
5149 Constructs, checks, and returns an op of any type that involves an
5150 embedded SV.  I<type> is the opcode.  I<flags> gives the eight bits
5151 of C<op_flags>.  I<sv> gives the SV to embed in the op; this function
5152 takes ownership of one reference to it.
5153
5154 =cut
5155 */
5156
5157 OP *
5158 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
5159 {
5160     dVAR;
5161     SVOP *svop;
5162
5163     PERL_ARGS_ASSERT_NEWSVOP;
5164
5165     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5166         || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5167         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
5168
5169     NewOp(1101, svop, 1, SVOP);
5170     svop->op_type = (OPCODE)type;
5171     svop->op_ppaddr = PL_ppaddr[type];
5172     svop->op_sv = sv;
5173     svop->op_next = (OP*)svop;
5174     svop->op_flags = (U8)flags;
5175     svop->op_private = (U8)(0 | (flags >> 8));
5176     if (PL_opargs[type] & OA_RETSCALAR)
5177         scalar((OP*)svop);
5178     if (PL_opargs[type] & OA_TARGET)
5179         svop->op_targ = pad_alloc(type, SVs_PADTMP);
5180     return CHECKOP(type, svop);
5181 }
5182
5183 #ifdef USE_ITHREADS
5184
5185 /*
5186 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
5187
5188 Constructs, checks, and returns an op of any type that involves a
5189 reference to a pad element.  I<type> is the opcode.  I<flags> gives the
5190 eight bits of C<op_flags>.  A pad slot is automatically allocated, and
5191 is populated with I<sv>; this function takes ownership of one reference
5192 to it.
5193
5194 This function only exists if Perl has been compiled to use ithreads.
5195
5196 =cut
5197 */
5198
5199 OP *
5200 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
5201 {
5202     dVAR;
5203     PADOP *padop;
5204
5205     PERL_ARGS_ASSERT_NEWPADOP;
5206
5207     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5208         || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5209         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
5210
5211     NewOp(1101, padop, 1, PADOP);
5212     padop->op_type = (OPCODE)type;
5213     padop->op_ppaddr = PL_ppaddr[type];
5214     padop->op_padix = pad_alloc(type, SVs_PADTMP);
5215     SvREFCNT_dec(PAD_SVl(padop->op_padix));
5216     PAD_SETSV(padop->op_padix, sv);
5217     assert(sv);
5218     padop->op_next = (OP*)padop;
5219     padop->op_flags = (U8)flags;
5220     if (PL_opargs[type] & OA_RETSCALAR)
5221         scalar((OP*)padop);
5222     if (PL_opargs[type] & OA_TARGET)
5223         padop->op_targ = pad_alloc(type, SVs_PADTMP);
5224     return CHECKOP(type, padop);
5225 }
5226
5227 #endif /* USE_ITHREADS */
5228
5229 /*
5230 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
5231
5232 Constructs, checks, and returns an op of any type that involves an
5233 embedded reference to a GV.  I<type> is the opcode.  I<flags> gives the
5234 eight bits of C<op_flags>.  I<gv> identifies the GV that the op should
5235 reference; calling this function does not transfer ownership of any
5236 reference to it.
5237
5238 =cut
5239 */
5240
5241 OP *
5242 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
5243 {
5244     dVAR;
5245
5246     PERL_ARGS_ASSERT_NEWGVOP;
5247
5248 #ifdef USE_ITHREADS
5249     GvIN_PAD_on(gv);
5250     return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
5251 #else
5252     return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
5253 #endif
5254 }
5255
5256 /*
5257 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
5258
5259 Constructs, checks, and returns an op of any type that involves an
5260 embedded C-level pointer (PV).  I<type> is the opcode.  I<flags> gives
5261 the eight bits of C<op_flags>.  I<pv> supplies the C-level pointer, which
5262 must have been allocated using C<PerlMemShared_malloc>; the memory will
5263 be freed when the op is destroyed.
5264
5265 =cut
5266 */
5267
5268 OP *
5269 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
5270 {
5271     dVAR;
5272     const bool utf8 = cBOOL(flags & SVf_UTF8);
5273     PVOP *pvop;
5274
5275     flags &= ~SVf_UTF8;
5276
5277     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5278         || type == OP_RUNCV
5279         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
5280
5281     NewOp(1101, pvop, 1, PVOP);
5282     pvop->op_type = (OPCODE)type;
5283     pvop->op_ppaddr = PL_ppaddr[type];
5284     pvop->op_pv = pv;
5285     pvop->op_next = (OP*)pvop;
5286     pvop->op_flags = (U8)flags;
5287     pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
5288     if (PL_opargs[type] & OA_RETSCALAR)
5289         scalar((OP*)pvop);
5290     if (PL_opargs[type] & OA_TARGET)
5291         pvop->op_targ = pad_alloc(type, SVs_PADTMP);
5292     return CHECKOP(type, pvop);
5293 }
5294
5295 #ifdef PERL_MAD
5296 OP*
5297 #else
5298 void
5299 #endif
5300 Perl_package(pTHX_ OP *o)
5301 {
5302     dVAR;
5303     SV *const sv = cSVOPo->op_sv;
5304 #ifdef PERL_MAD
5305     OP *pegop;
5306 #endif
5307
5308     PERL_ARGS_ASSERT_PACKAGE;
5309
5310     SAVEGENERICSV(PL_curstash);
5311     save_item(PL_curstname);
5312
5313     PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
5314
5315     sv_setsv(PL_curstname, sv);
5316
5317     PL_hints |= HINT_BLOCK_SCOPE;
5318     PL_parser->copline = NOLINE;
5319     PL_parser->expect = XSTATE;
5320
5321 #ifndef PERL_MAD
5322     op_free(o);
5323 #else
5324     if (!PL_madskills) {
5325         op_free(o);
5326         return NULL;
5327     }
5328
5329     pegop = newOP(OP_NULL,0);
5330     op_getmad(o,pegop,'P');
5331     return pegop;
5332 #endif
5333 }
5334
5335 void
5336 Perl_package_version( pTHX_ OP *v )
5337 {
5338     dVAR;
5339     U32 savehints = PL_hints;
5340     PERL_ARGS_ASSERT_PACKAGE_VERSION;
5341     PL_hints &= ~HINT_STRICT_VARS;
5342     sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
5343     PL_hints = savehints;
5344     op_free(v);
5345 }
5346
5347 #ifdef PERL_MAD
5348 OP*
5349 #else
5350 void
5351 #endif
5352 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
5353 {
5354     dVAR;
5355     OP *pack;
5356     OP *imop;
5357     OP *veop;
5358 #ifdef PERL_MAD
5359     OP *pegop = PL_madskills ? newOP(OP_NULL,0) : NULL;
5360 #endif
5361     SV *use_version = NULL;
5362
5363     PERL_ARGS_ASSERT_UTILIZE;
5364
5365     if (idop->op_type != OP_CONST)
5366         Perl_croak(aTHX_ "Module name must be constant");
5367
5368     if (PL_madskills)
5369         op_getmad(idop,pegop,'U');
5370
5371     veop = NULL;
5372
5373     if (version) {
5374         SV * const vesv = ((SVOP*)version)->op_sv;
5375
5376         if (PL_madskills)
5377             op_getmad(version,pegop,'V');
5378         if (!arg && !SvNIOKp(vesv)) {
5379             arg = version;
5380         }
5381         else {
5382             OP *pack;
5383             SV *meth;
5384
5385             if (version->op_type != OP_CONST || !SvNIOKp(vesv))
5386                 Perl_croak(aTHX_ "Version number must be a constant number");
5387
5388             /* Make copy of idop so we don't free it twice */
5389             pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
5390
5391             /* Fake up a method call to VERSION */
5392             meth = newSVpvs_share("VERSION");
5393             veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
5394                             op_append_elem(OP_LIST,
5395                                         op_prepend_elem(OP_LIST, pack, list(version)),
5396                                         newSVOP(OP_METHOD_NAMED, 0, meth)));
5397         }
5398     }
5399
5400     /* Fake up an import/unimport */
5401     if (arg && arg->op_type == OP_STUB) {
5402         if (PL_madskills)
5403             op_getmad(arg,pegop,'S');
5404         imop = arg;             /* no import on explicit () */
5405     }
5406     else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
5407         imop = NULL;            /* use 5.0; */
5408         if (aver)
5409             use_version = ((SVOP*)idop)->op_sv;
5410         else
5411             idop->op_private |= OPpCONST_NOVER;
5412     }
5413     else {
5414         SV *meth;
5415
5416         if (PL_madskills)
5417             op_getmad(arg,pegop,'A');
5418
5419         /* Make copy of idop so we don't free it twice */
5420         pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
5421
5422         /* Fake up a method call to import/unimport */
5423         meth = aver
5424             ? newSVpvs_share("import") : newSVpvs_share("unimport");
5425         imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
5426                        op_append_elem(OP_LIST,
5427                                    op_prepend_elem(OP_LIST, pack, list(arg)),
5428                                    newSVOP(OP_METHOD_NAMED, 0, meth)));
5429     }
5430
5431     /* Fake up the BEGIN {}, which does its thing immediately. */
5432     newATTRSUB(floor,
5433         newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
5434         NULL,
5435         NULL,
5436         op_append_elem(OP_LINESEQ,
5437             op_append_elem(OP_LINESEQ,
5438                 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
5439                 newSTATEOP(0, NULL, veop)),
5440             newSTATEOP(0, NULL, imop) ));
5441
5442     if (use_version) {
5443         /* Enable the
5444          * feature bundle that corresponds to the required version. */
5445         use_version = sv_2mortal(new_version(use_version));
5446         S_enable_feature_bundle(aTHX_ use_version);
5447
5448         /* If a version >= 5.11.0 is requested, strictures are on by default! */
5449         if (vcmp(use_version,
5450                  sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
5451             if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
5452                 PL_hints |= HINT_STRICT_REFS;
5453             if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
5454                 PL_hints |= HINT_STRICT_SUBS;
5455             if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
5456                 PL_hints |= HINT_STRICT_VARS;
5457         }
5458         /* otherwise they are off */
5459         else {
5460             if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
5461                 PL_hints &= ~HINT_STRICT_REFS;
5462             if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
5463                 PL_hints &= ~HINT_STRICT_SUBS;
5464             if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
5465                 PL_hints &= ~HINT_STRICT_VARS;
5466         }
5467     }
5468
5469     /* The "did you use incorrect case?" warning used to be here.
5470      * The problem is that on case-insensitive filesystems one
5471      * might get false positives for "use" (and "require"):
5472      * "use Strict" or "require CARP" will work.  This causes
5473      * portability problems for the script: in case-strict
5474      * filesystems the script will stop working.
5475      *
5476      * The "incorrect case" warning checked whether "use Foo"
5477      * imported "Foo" to your namespace, but that is wrong, too:
5478      * there is no requirement nor promise in the language that
5479      * a Foo.pm should or would contain anything in package "Foo".
5480      *
5481      * There is very little Configure-wise that can be done, either:
5482      * the case-sensitivity of the build filesystem of Perl does not
5483      * help in guessing the case-sensitivity of the runtime environment.
5484      */
5485
5486     PL_hints |= HINT_BLOCK_SCOPE;
5487     PL_parser->copline = NOLINE;
5488     PL_parser->expect = XSTATE;
5489     PL_cop_seqmax++; /* Purely for B::*'s benefit */
5490     if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */
5491         PL_cop_seqmax++;
5492
5493 #ifdef PERL_MAD
5494     return pegop;
5495 #endif
5496 }
5497
5498 /*
5499 =head1 Embedding Functions
5500
5501 =for apidoc load_module
5502
5503 Loads the module whose name is pointed to by the string part of name.
5504 Note that the actual module name, not its filename, should be given.
5505 Eg, "Foo::Bar" instead of "Foo/Bar.pm".  flags can be any of
5506 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
5507 (or 0 for no flags).  ver, if specified
5508 and not NULL, provides version semantics
5509 similar to C<use Foo::Bar VERSION>.  The optional trailing SV*
5510 arguments can be used to specify arguments to the module's import()
5511 method, similar to C<use Foo::Bar VERSION LIST>.  They must be
5512 terminated with a final NULL pointer.  Note that this list can only
5513 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
5514 Otherwise at least a single NULL pointer to designate the default
5515 import list is required.
5516
5517 The reference count for each specified C<SV*> parameter is decremented.
5518
5519 =cut */
5520
5521 void
5522 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
5523 {
5524     va_list args;
5525
5526     PERL_ARGS_ASSERT_LOAD_MODULE;
5527
5528     va_start(args, ver);
5529     vload_module(flags, name, ver, &args);
5530     va_end(args);
5531 }
5532
5533 #ifdef PERL_IMPLICIT_CONTEXT
5534 void
5535 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
5536 {
5537     dTHX;
5538     va_list args;
5539     PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
5540     va_start(args, ver);
5541     vload_module(flags, name, ver, &args);
5542     va_end(args);
5543 }
5544 #endif
5545
5546 void
5547 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
5548 {
5549     dVAR;
5550     OP *veop, *imop;
5551     OP * const modname = newSVOP(OP_CONST, 0, name);
5552
5553     PERL_ARGS_ASSERT_VLOAD_MODULE;
5554
5555     modname->op_private |= OPpCONST_BARE;
5556     if (ver) {
5557         veop = newSVOP(OP_CONST, 0, ver);
5558     }
5559     else
5560         veop = NULL;
5561     if (flags & PERL_LOADMOD_NOIMPORT) {
5562         imop = sawparens(newNULLLIST());
5563     }
5564     else if (flags & PERL_LOADMOD_IMPORT_OPS) {
5565         imop = va_arg(*args, OP*);
5566     }
5567     else {
5568         SV *sv;
5569         imop = NULL;
5570         sv = va_arg(*args, SV*);
5571         while (sv) {
5572             imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
5573             sv = va_arg(*args, SV*);
5574         }
5575     }
5576
5577     /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
5578      * that it has a PL_parser to play with while doing that, and also
5579      * that it doesn't mess with any existing parser, by creating a tmp
5580      * new parser with lex_start(). This won't actually be used for much,
5581      * since pp_require() will create another parser for the real work.
5582      * The ENTER/LEAVE pair protect callers from any side effects of use.  */
5583
5584     ENTER;
5585     SAVEVPTR(PL_curcop);
5586     lex_start(NULL, NULL, LEX_START_SAME_FILTER);
5587     utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
5588             veop, modname, imop);
5589     LEAVE;
5590 }
5591
5592 PERL_STATIC_INLINE OP *
5593 S_new_entersubop(pTHX_ GV *gv, OP *arg)
5594 {
5595     return newUNOP(OP_ENTERSUB, OPf_STACKED,
5596                    newLISTOP(OP_LIST, 0, arg,
5597                              newUNOP(OP_RV2CV, 0,
5598                                      newGVOP(OP_GV, 0, gv))));
5599 }
5600
5601 OP *
5602 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
5603 {
5604     dVAR;
5605     OP *doop;
5606     GV *gv;
5607
5608     PERL_ARGS_ASSERT_DOFILE;
5609
5610     if (!force_builtin && (gv = gv_override("do", 2))) {
5611         doop = S_new_entersubop(aTHX_ gv, term);
5612     }
5613     else {
5614         doop = newUNOP(OP_DOFILE, 0, scalar(term));
5615     }
5616     return doop;
5617 }
5618
5619 /*
5620 =head1 Optree construction
5621
5622 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
5623
5624 Constructs, checks, and returns an C<lslice> (list slice) op.  I<flags>
5625 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
5626 be set automatically, and, shifted up eight bits, the eight bits of
5627 C<op_private>, except that the bit with value 1 or 2 is automatically
5628 set as required.  I<listval> and I<subscript> supply the parameters of
5629 the slice; they are consumed by this function and become part of the
5630 constructed op tree.
5631
5632 =cut
5633 */
5634
5635 OP *
5636 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
5637 {
5638     return newBINOP(OP_LSLICE, flags,
5639             list(force_list(subscript)),
5640             list(force_list(listval)) );
5641 }
5642
5643 STATIC I32
5644 S_is_list_assignment(pTHX_ const OP *o)
5645 {
5646     unsigned type;
5647     U8 flags;
5648
5649     if (!o)
5650         return TRUE;
5651
5652     if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
5653         o = cUNOPo->op_first;
5654
5655     flags = o->op_flags;
5656     type = o->op_type;
5657     if (type == OP_COND_EXPR) {
5658         const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
5659         const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
5660
5661         if (t && f)
5662             return TRUE;
5663         if (t || f)
5664             yyerror("Assignment to both a list and a scalar");
5665         return FALSE;
5666     }
5667
5668     if (type == OP_LIST &&
5669         (flags & OPf_WANT) == OPf_WANT_SCALAR &&
5670         o->op_private & OPpLVAL_INTRO)
5671         return FALSE;
5672
5673     if (type == OP_LIST || flags & OPf_PARENS ||
5674         type == OP_RV2AV || type == OP_RV2HV ||
5675         type == OP_ASLICE || type == OP_HSLICE ||
5676         type == OP_KVASLICE || type == OP_KVHSLICE)
5677         return TRUE;
5678
5679     if (type == OP_PADAV || type == OP_PADHV)
5680         return TRUE;
5681
5682     if (type == OP_RV2SV)
5683         return FALSE;
5684
5685     return FALSE;
5686 }
5687
5688 /*
5689   Helper function for newASSIGNOP to detection commonality between the
5690   lhs and the rhs.  Marks all variables with PL_generation.  If it
5691   returns TRUE the assignment must be able to handle common variables.
5692 */
5693 PERL_STATIC_INLINE bool
5694 S_aassign_common_vars(pTHX_ OP* o)
5695 {
5696     OP *curop;
5697     for (curop = cUNOPo->op_first; curop; curop=curop->op_sibling) {
5698         if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
5699             if (curop->op_type == OP_GV) {
5700                 GV *gv = cGVOPx_gv(curop);
5701                 if (gv == PL_defgv
5702                     || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5703                     return TRUE;
5704                 GvASSIGN_GENERATION_set(gv, PL_generation);
5705             }
5706             else if (curop->op_type == OP_PADSV ||
5707                 curop->op_type == OP_PADAV ||
5708                 curop->op_type == OP_PADHV ||
5709                 curop->op_type == OP_PADANY)
5710                 {
5711                     if (PAD_COMPNAME_GEN(curop->op_targ)
5712                         == (STRLEN)PL_generation)
5713                         return TRUE;
5714                     PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
5715
5716                 }
5717             else if (curop->op_type == OP_RV2CV)
5718                 return TRUE;
5719             else if (curop->op_type == OP_RV2SV ||
5720                 curop->op_type == OP_RV2AV ||
5721                 curop->op_type == OP_RV2HV ||
5722                 curop->op_type == OP_RV2GV) {
5723                 if (cUNOPx(curop)->op_first->op_type != OP_GV)  /* funny deref? */
5724                     return TRUE;
5725             }
5726             else if (curop->op_type == OP_PUSHRE) {
5727                 GV *const gv =
5728 #ifdef USE_ITHREADS
5729                     ((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff
5730                         ? MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff))
5731                         : NULL;
5732 #else
5733                     ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv;
5734 #endif
5735                 if (gv) {
5736                     if (gv == PL_defgv
5737                         || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5738                         return TRUE;
5739                     GvASSIGN_GENERATION_set(gv, PL_generation);
5740                 }
5741             }
5742             else
5743                 return TRUE;
5744         }
5745
5746         if (curop->op_flags & OPf_KIDS) {
5747             if (aassign_common_vars(curop))
5748                 return TRUE;
5749         }
5750     }
5751     return FALSE;
5752 }
5753
5754 /*
5755 =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right
5756
5757 Constructs, checks, and returns an assignment op.  I<left> and I<right>
5758 supply the parameters of the assignment; they are consumed by this
5759 function and become part of the constructed op tree.
5760
5761 If I<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
5762 a suitable conditional optree is constructed.  If I<optype> is the opcode
5763 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
5764 performs the binary operation and assigns the result to the left argument.
5765 Either way, if I<optype> is non-zero then I<flags> has no effect.
5766
5767 If I<optype> is zero, then a plain scalar or list assignment is
5768 constructed.  Which type of assignment it is is automatically determined.
5769 I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
5770 will be set automatically, and, shifted up eight bits, the eight bits
5771 of C<op_private>, except that the bit with value 1 or 2 is automatically
5772 set as required.
5773
5774 =cut
5775 */
5776
5777 OP *
5778 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
5779 {
5780     dVAR;
5781     OP *o;
5782
5783     if (optype) {
5784         if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
5785             return newLOGOP(optype, 0,
5786                 op_lvalue(scalar(left), optype),
5787                 newUNOP(OP_SASSIGN, 0, scalar(right)));
5788         }
5789         else {
5790             return newBINOP(optype, OPf_STACKED,
5791                 op_lvalue(scalar(left), optype), scalar(right));
5792         }
5793     }
5794
5795     if (is_list_assignment(left)) {
5796         static const char no_list_state[] = "Initialization of state variables"
5797             " in list context currently forbidden";
5798         OP *curop;
5799         bool maybe_common_vars = TRUE;
5800
5801         if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
5802             left->op_private &= ~ OPpSLICEWARNING;
5803
5804         PL_modcount = 0;
5805         left = op_lvalue(left, OP_AASSIGN);
5806         curop = list(force_list(left));
5807         o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
5808         o->op_private = (U8)(0 | (flags >> 8));
5809
5810         if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
5811         {
5812             OP* lop = ((LISTOP*)left)->op_first;
5813             maybe_common_vars = FALSE;
5814             while (lop) {
5815                 if (lop->op_type == OP_PADSV ||
5816                     lop->op_type == OP_PADAV ||
5817                     lop->op_type == OP_PADHV ||
5818                     lop->op_type == OP_PADANY) {
5819                     if (!(lop->op_private & OPpLVAL_INTRO))
5820                         maybe_common_vars = TRUE;
5821
5822                     if (lop->op_private & OPpPAD_STATE) {
5823                         if (left->op_private & OPpLVAL_INTRO) {
5824                             /* Each variable in state($a, $b, $c) = ... */
5825                         }
5826                         else {
5827                             /* Each state variable in
5828                                (state $a, my $b, our $c, $d, undef) = ... */
5829                         }
5830                         yyerror(no_list_state);
5831                     } else {
5832                         /* Each my variable in
5833                            (state $a, my $b, our $c, $d, undef) = ... */
5834                     }
5835                 } else if (lop->op_type == OP_UNDEF ||
5836                            OP_TYPE_IS_OR_WAS(lop, OP_PUSHMARK)) {
5837                     /* undef may be interesting in
5838                        (state $a, undef, state $c) */
5839                 } else {
5840                     /* Other ops in the list. */
5841                     maybe_common_vars = TRUE;
5842                 }
5843                 lop = lop->op_sibling;
5844             }
5845         }
5846         else if ((left->op_private & OPpLVAL_INTRO)
5847                 && (   left->op_type == OP_PADSV
5848                     || left->op_type == OP_PADAV
5849                     || left->op_type == OP_PADHV
5850                     || left->op_type == OP_PADANY))
5851         {
5852             if (left->op_type == OP_PADSV) maybe_common_vars = FALSE;
5853             if (left->op_private & OPpPAD_STATE) {
5854                 /* All single variable list context state assignments, hence
5855                    state ($a) = ...
5856                    (state $a) = ...
5857                    state @a = ...
5858                    state (@a) = ...
5859                    (state @a) = ...
5860                    state %a = ...
5861                    state (%a) = ...
5862                    (state %a) = ...
5863                 */
5864                 yyerror(no_list_state);
5865             }
5866         }
5867
5868         /* PL_generation sorcery:
5869          * an assignment like ($a,$b) = ($c,$d) is easier than
5870          * ($a,$b) = ($c,$a), since there is no need for temporary vars.
5871          * To detect whether there are common vars, the global var
5872          * PL_generation is incremented for each assign op we compile.
5873          * Then, while compiling the assign op, we run through all the
5874          * variables on both sides of the assignment, setting a spare slot
5875          * in each of them to PL_generation. If any of them already have
5876          * that value, we know we've got commonality.  We could use a
5877          * single bit marker, but then we'd have to make 2 passes, first
5878          * to clear the flag, then to test and set it.  To find somewhere
5879          * to store these values, evil chicanery is done with SvUVX().
5880          */
5881
5882         if (maybe_common_vars) {
5883             PL_generation++;
5884             if (aassign_common_vars(o))
5885                 o->op_private |= OPpASSIGN_COMMON;
5886             LINKLIST(o);
5887         }
5888
5889         if (right && right->op_type == OP_SPLIT && !PL_madskills) {
5890             OP* tmpop = ((LISTOP*)right)->op_first;
5891             if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
5892                 PMOP * const pm = (PMOP*)tmpop;
5893                 if (left->op_type == OP_RV2AV &&
5894                     !(left->op_private & OPpLVAL_INTRO) &&
5895                     !(o->op_private & OPpASSIGN_COMMON) )
5896                 {
5897                     tmpop = ((UNOP*)left)->op_first;
5898                     if (tmpop->op_type == OP_GV
5899 #ifdef USE_ITHREADS
5900                         && !pm->op_pmreplrootu.op_pmtargetoff
5901 #else
5902                         && !pm->op_pmreplrootu.op_pmtargetgv
5903 #endif
5904                         ) {
5905 #ifdef USE_ITHREADS
5906                         pm->op_pmreplrootu.op_pmtargetoff
5907                             = cPADOPx(tmpop)->op_padix;
5908                         cPADOPx(tmpop)->op_padix = 0;   /* steal it */
5909 #else
5910                         pm->op_pmreplrootu.op_pmtargetgv
5911                             = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
5912                         cSVOPx(tmpop)->op_sv = NULL;    /* steal it */
5913 #endif
5914                         tmpop = cUNOPo->op_first;       /* to list (nulled) */
5915                         tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
5916                         tmpop->op_sibling = NULL;       /* don't free split */
5917                         right->op_next = tmpop->op_next;  /* fix starting loc */
5918                         op_free(o);                     /* blow off assign */
5919                         right->op_flags &= ~OPf_WANT;
5920                                 /* "I don't know and I don't care." */
5921                         return right;
5922                     }
5923                 }
5924                 else {
5925                    if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
5926                       ((LISTOP*)right)->op_last->op_type == OP_CONST)
5927                     {
5928                         SV ** const svp =
5929                             &((SVOP*)((LISTOP*)right)->op_last)->op_sv;
5930                         SV * const sv = *svp;
5931                         if (SvIOK(sv) && SvIVX(sv) == 0)
5932                         {
5933                           if (right->op_private & OPpSPLIT_IMPLIM) {
5934                             /* our own SV, created in ck_split */
5935                             SvREADONLY_off(sv);
5936                             sv_setiv(sv, PL_modcount+1);
5937                           }
5938                           else {
5939                             /* SV may belong to someone else */
5940                             SvREFCNT_dec(sv);
5941                             *svp = newSViv(PL_modcount+1);
5942                           }
5943                         }
5944                     }
5945                 }
5946             }
5947         }
5948         return o;
5949     }
5950     if (!right)
5951         right = newOP(OP_UNDEF, 0);
5952     if (right->op_type == OP_READLINE) {
5953         right->op_flags |= OPf_STACKED;
5954         return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
5955                 scalar(right));
5956     }
5957     else {
5958         o = newBINOP(OP_SASSIGN, flags,
5959             scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
5960     }
5961     return o;
5962 }
5963
5964 /*
5965 =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o
5966
5967 Constructs a state op (COP).  The state op is normally a C<nextstate> op,
5968 but will be a C<dbstate> op if debugging is enabled for currently-compiled
5969 code.  The state op is populated from C<PL_curcop> (or C<PL_compiling>).
5970 If I<label> is non-null, it supplies the name of a label to attach to
5971 the state op; this function takes ownership of the memory pointed at by
5972 I<label>, and will free it.  I<flags> gives the eight bits of C<op_flags>
5973 for the state op.
5974
5975 If I<o> is null, the state op is returned.  Otherwise the state op is
5976 combined with I<o> into a C<lineseq> list op, which is returned.  I<o>
5977 is consumed by this function and becomes part of the returned op tree.
5978
5979 =cut
5980 */
5981
5982 OP *
5983 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
5984 {
5985     dVAR;
5986     const U32 seq = intro_my();
5987     const U32 utf8 = flags & SVf_UTF8;
5988     COP *cop;
5989
5990     flags &= ~SVf_UTF8;
5991
5992     NewOp(1101, cop, 1, COP);
5993     if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
5994         cop->op_type = OP_DBSTATE;
5995         cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
5996     }
5997     else {
5998         cop->op_type = OP_NEXTSTATE;
5999         cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
6000     }
6001     cop->op_flags = (U8)flags;
6002     CopHINTS_set(cop, PL_hints);
6003 #ifdef NATIVE_HINTS
6004     cop->op_private |= NATIVE_HINTS;
6005 #endif
6006 #ifdef VMS
6007     if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
6008 #endif
6009     cop->op_next = (OP*)cop;
6010
6011     cop->cop_seq = seq;
6012     cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
6013     CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
6014     if (label) {
6015         Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
6016
6017         PL_hints |= HINT_BLOCK_SCOPE;
6018         /* It seems that we need to defer freeing this pointer, as other parts
6019            of the grammar end up wanting to copy it after this op has been
6020            created. */
6021         SAVEFREEPV(label);
6022     }
6023
6024     if (PL_parser->preambling != NOLINE) {
6025         CopLINE_set(cop, PL_parser->preambling);
6026         PL_parser->copline = NOLINE;
6027     }
6028     else if (PL_parser->copline == NOLINE)
6029         CopLINE_set(cop, CopLINE(PL_curcop));
6030     else {
6031         CopLINE_set(cop, PL_parser->copline);
6032         PL_parser->copline = NOLINE;
6033     }
6034 #ifdef USE_ITHREADS
6035     CopFILE_set(cop, CopFILE(PL_curcop));       /* XXX share in a pvtable? */
6036 #else
6037     CopFILEGV_set(cop, CopFILEGV(PL_curcop));
6038 #endif
6039     CopSTASH_set(cop, PL_curstash);
6040
6041     if (cop->op_type == OP_DBSTATE) {
6042         /* this line can have a breakpoint - store the cop in IV */
6043         AV *av = CopFILEAVx(PL_curcop);
6044         if (av) {
6045             SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
6046             if (svp && *svp != &PL_sv_undef ) {
6047                 (void)SvIOK_on(*svp);
6048                 SvIV_set(*svp, PTR2IV(cop));
6049             }
6050         }
6051     }
6052
6053     if (flags & OPf_SPECIAL)
6054         op_null((OP*)cop);
6055     return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
6056 }
6057
6058 /*
6059 =for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other
6060
6061 Constructs, checks, and returns a logical (flow control) op.  I<type>
6062 is the opcode.  I<flags> gives the eight bits of C<op_flags>, except
6063 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
6064 the eight bits of C<op_private>, except that the bit with value 1 is
6065 automatically set.  I<first> supplies the expression controlling the
6066 flow, and I<other> supplies the side (alternate) chain of ops; they are
6067 consumed by this function and become part of the constructed op tree.
6068
6069 =cut
6070 */
6071
6072 OP *
6073 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
6074 {
6075     dVAR;
6076
6077     PERL_ARGS_ASSERT_NEWLOGOP;
6078
6079     return new_logop(type, flags, &first, &other);
6080 }
6081
6082 STATIC OP *
6083 S_search_const(pTHX_ OP *o)
6084 {
6085     PERL_ARGS_ASSERT_SEARCH_CONST;
6086
6087     switch (o->op_type) {
6088         case OP_CONST:
6089             return o;
6090         case OP_NULL:
6091             if (o->op_flags & OPf_KIDS)
6092                 return search_const(cUNOPo->op_first);
6093             break;
6094         case OP_LEAVE:
6095         case OP_SCOPE:
6096         case OP_LINESEQ:
6097         {
6098             OP *kid;
6099             if (!(o->op_flags & OPf_KIDS))
6100                 return NULL;
6101             kid = cLISTOPo->op_first;
6102             do {
6103                 switch (kid->op_type) {
6104                     case OP_ENTER:
6105                     case OP_NULL:
6106                     case OP_NEXTSTATE:
6107                         kid = kid->op_sibling;
6108                         break;
6109                     default:
6110                         if (kid != cLISTOPo->op_last)
6111                             return NULL;
6112                         goto last;
6113                 }
6114             } while (kid);
6115             if (!kid)
6116                 kid = cLISTOPo->op_last;
6117 last:
6118             return search_const(kid);
6119         }
6120     }
6121
6122     return NULL;
6123 }
6124
6125 STATIC OP *
6126 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
6127 {
6128     dVAR;
6129     LOGOP *logop;
6130     OP *o;
6131     OP *first;
6132     OP *other;
6133     OP *cstop = NULL;
6134     int prepend_not = 0;
6135
6136     PERL_ARGS_ASSERT_NEW_LOGOP;
6137
6138     first = *firstp;
6139     other = *otherp;
6140
6141     /* [perl #59802]: Warn about things like "return $a or $b", which
6142        is parsed as "(return $a) or $b" rather than "return ($a or
6143        $b)".  NB: This also applies to xor, which is why we do it
6144        here.
6145      */
6146     switch (first->op_type) {
6147     case OP_NEXT:
6148     case OP_LAST:
6149     case OP_REDO:
6150         /* XXX: Perhaps we should emit a stronger warning for these.
6151            Even with the high-precedence operator they don't seem to do
6152            anything sensible.
6153
6154            But until we do, fall through here.
6155          */
6156     case OP_RETURN:
6157     case OP_EXIT:
6158     case OP_DIE:
6159     case OP_GOTO:
6160         /* XXX: Currently we allow people to "shoot themselves in the
6161            foot" by explicitly writing "(return $a) or $b".
6162
6163            Warn unless we are looking at the result from folding or if
6164            the programmer explicitly grouped the operators like this.
6165            The former can occur with e.g.
6166
6167                 use constant FEATURE => ( $] >= ... );
6168                 sub { not FEATURE and return or do_stuff(); }
6169          */
6170         if (!first->op_folded && !(first->op_flags & OPf_PARENS))
6171             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
6172                            "Possible precedence issue with control flow operator");
6173         /* XXX: Should we optimze this to "return $a;" (i.e. remove
6174            the "or $b" part)?
6175         */
6176         break;
6177     }
6178
6179     if (type == OP_XOR)         /* Not short circuit, but here by precedence. */
6180         return newBINOP(type, flags, scalar(first), scalar(other));
6181
6182     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP);
6183
6184     scalarboolean(first);
6185     /* optimize AND and OR ops that have NOTs as children */
6186     if (first->op_type == OP_NOT
6187         && (first->op_flags & OPf_KIDS)
6188         && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
6189             || (other->op_type == OP_NOT))  /* if (!$x && !$y) { } */
6190         && !PL_madskills) {
6191         if (type == OP_AND || type == OP_OR) {
6192             if (type == OP_AND)
6193                 type = OP_OR;
6194             else
6195                 type = OP_AND;
6196             op_null(first);
6197             if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
6198                 op_null(other);
6199                 prepend_not = 1; /* prepend a NOT op later */
6200             }
6201         }
6202     }
6203     /* search for a constant op that could let us fold the test */
6204     if ((cstop = search_const(first))) {
6205         if (cstop->op_private & OPpCONST_STRICT)
6206             no_bareword_allowed(cstop);
6207         else if ((cstop->op_private & OPpCONST_BARE))
6208                 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
6209         if ((type == OP_AND &&  SvTRUE(((SVOP*)cstop)->op_sv)) ||
6210             (type == OP_OR  && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
6211             (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
6212             *firstp = NULL;
6213             if (other->op_type == OP_CONST)
6214                 other->op_private |= OPpCONST_SHORTCIRCUIT;
6215             if (PL_madskills) {
6216                 OP *newop = newUNOP(OP_NULL, 0, other);
6217                 op_getmad(first, newop, '1');
6218                 newop->op_targ = type;  /* set "was" field */
6219                 return newop;
6220             }
6221             op_free(first);
6222             if (other->op_type == OP_LEAVE)
6223                 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
6224             else if (other->op_type == OP_MATCH
6225                   || other->op_type == OP_SUBST
6226                   || other->op_type == OP_TRANSR
6227                   || other->op_type == OP_TRANS)
6228                 /* Mark the op as being unbindable with =~ */
6229                 other->op_flags |= OPf_SPECIAL;
6230
6231             other->op_folded = 1;
6232             return other;
6233         }
6234         else {
6235             /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
6236             const OP *o2 = other;
6237             if ( ! (o2->op_type == OP_LIST
6238                     && (( o2 = cUNOPx(o2)->op_first))
6239                     && o2->op_type == OP_PUSHMARK
6240                     && (( o2 = o2->op_sibling)) )
6241             )
6242                 o2 = other;
6243             if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
6244                         || o2->op_type == OP_PADHV)
6245                 && o2->op_private & OPpLVAL_INTRO
6246                 && !(o2->op_private & OPpPAD_STATE))
6247             {
6248                 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6249                                  "Deprecated use of my() in false conditional");
6250             }
6251
6252             *otherp = NULL;
6253             if (cstop->op_type == OP_CONST)
6254                 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
6255             if (PL_madskills) {
6256                 first = newUNOP(OP_NULL, 0, first);
6257                 op_getmad(other, first, '2');
6258                 first->op_targ = type;  /* set "was" field */
6259             }
6260             else
6261                 op_free(other);
6262             return first;
6263         }
6264     }
6265     else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
6266         && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
6267     {
6268         const OP * const k1 = ((UNOP*)first)->op_first;
6269         const OP * const k2 = k1->op_sibling;
6270         OPCODE warnop = 0;
6271         switch (first->op_type)
6272         {
6273         case OP_NULL:
6274             if (k2 && k2->op_type == OP_READLINE
6275                   && (k2->op_flags & OPf_STACKED)
6276                   && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6277             {
6278                 warnop = k2->op_type;
6279             }
6280             break;
6281
6282         case OP_SASSIGN:
6283             if (k1->op_type == OP_READDIR
6284                   || k1->op_type == OP_GLOB
6285                   || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6286                  || k1->op_type == OP_EACH
6287                  || k1->op_type == OP_AEACH)
6288             {
6289                 warnop = ((k1->op_type == OP_NULL)
6290                           ? (OPCODE)k1->op_targ : k1->op_type);
6291             }
6292             break;
6293         }
6294         if (warnop) {
6295             const line_t oldline = CopLINE(PL_curcop);
6296             /* This ensures that warnings are reported at the first line
6297                of the construction, not the last.  */
6298             CopLINE_set(PL_curcop, PL_parser->copline);
6299             Perl_warner(aTHX_ packWARN(WARN_MISC),
6300                  "Value of %s%s can be \"0\"; test with defined()",
6301                  PL_op_desc[warnop],
6302                  ((warnop == OP_READLINE || warnop == OP_GLOB)
6303                   ? " construct" : "() operator"));
6304             CopLINE_set(PL_curcop, oldline);
6305         }
6306     }
6307
6308     if (!other)
6309         return first;
6310
6311     if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
6312         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
6313
6314     NewOp(1101, logop, 1, LOGOP);
6315
6316     logop->op_type = (OPCODE)type;
6317     logop->op_ppaddr = PL_ppaddr[type];
6318     logop->op_first = first;
6319     logop->op_flags = (U8)(flags | OPf_KIDS);
6320     logop->op_other = LINKLIST(other);
6321     logop->op_private = (U8)(1 | (flags >> 8));
6322
6323     /* establish postfix order */
6324     logop->op_next = LINKLIST(first);
6325     first->op_next = (OP*)logop;
6326     first->op_sibling = other;
6327
6328     CHECKOP(type,logop);
6329
6330     o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 0, (OP*)logop);
6331     other->op_next = o;
6332
6333     return o;
6334 }
6335
6336 /*
6337 =for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop
6338
6339 Constructs, checks, and returns a conditional-expression (C<cond_expr>)
6340 op.  I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
6341 will be set automatically, and, shifted up eight bits, the eight bits of
6342 C<op_private>, except that the bit with value 1 is automatically set.
6343 I<first> supplies the expression selecting between the two branches,
6344 and I<trueop> and I<falseop> supply the branches; they are consumed by
6345 this function and become part of the constructed op tree.
6346
6347 =cut
6348 */
6349
6350 OP *
6351 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
6352 {
6353     dVAR;
6354     LOGOP *logop;
6355     OP *start;
6356     OP *o;
6357     OP *cstop;
6358
6359     PERL_ARGS_ASSERT_NEWCONDOP;
6360
6361     if (!falseop)
6362         return newLOGOP(OP_AND, 0, first, trueop);
6363     if (!trueop)
6364         return newLOGOP(OP_OR, 0, first, falseop);
6365
6366     scalarboolean(first);
6367     if ((cstop = search_const(first))) {
6368         /* Left or right arm of the conditional?  */
6369         const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
6370         OP *live = left ? trueop : falseop;
6371         OP *const dead = left ? falseop : trueop;
6372         if (cstop->op_private & OPpCONST_BARE &&
6373             cstop->op_private & OPpCONST_STRICT) {
6374             no_bareword_allowed(cstop);
6375         }
6376         if (PL_madskills) {
6377             /* This is all dead code when PERL_MAD is not defined.  */
6378             live = newUNOP(OP_NULL, 0, live);
6379             op_getmad(first, live, 'C');
6380             op_getmad(dead, live, left ? 'e' : 't');
6381         } else {
6382             op_free(first);
6383             op_free(dead);
6384         }
6385         if (live->op_type == OP_LEAVE)
6386             live = newUNOP(OP_NULL, OPf_SPECIAL, live);
6387         else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
6388               || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
6389             /* Mark the op as being unbindable with =~ */
6390             live->op_flags |= OPf_SPECIAL;
6391         live->op_folded = 1;
6392         return live;
6393     }
6394     NewOp(1101, logop, 1, LOGOP);
6395     logop->op_type = OP_COND_EXPR;
6396     logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
6397     logop->op_first = first;
6398     logop->op_flags = (U8)(flags | OPf_KIDS);
6399     logop->op_private = (U8)(1 | (flags >> 8));
6400     logop->op_other = LINKLIST(trueop);
6401     logop->op_next = LINKLIST(falseop);
6402
6403     CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
6404             logop);
6405
6406     /* establish postfix order */
6407     start = LINKLIST(first);
6408     first->op_next = (OP*)logop;
6409
6410     first->op_sibling = trueop;
6411     trueop->op_sibling = falseop;
6412     o = newUNOP(OP_NULL, 0, (OP*)logop);
6413
6414     trueop->op_next = falseop->op_next = o;
6415
6416     o->op_next = start;
6417     return o;
6418 }
6419
6420 /*
6421 =for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right
6422
6423 Constructs and returns a C<range> op, with subordinate C<flip> and
6424 C<flop> ops.  I<flags> gives the eight bits of C<op_flags> for the
6425 C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
6426 for both the C<flip> and C<range> ops, except that the bit with value
6427 1 is automatically set.  I<left> and I<right> supply the expressions
6428 controlling the endpoints of the range; they are consumed by this function
6429 and become part of the constructed op tree.
6430
6431 =cut
6432 */
6433
6434 OP *
6435 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
6436 {
6437     dVAR;
6438     LOGOP *range;
6439     OP *flip;
6440     OP *flop;
6441     OP *leftstart;
6442     OP *o;
6443
6444     PERL_ARGS_ASSERT_NEWRANGE;
6445
6446     NewOp(1101, range, 1, LOGOP);
6447
6448     range->op_type = OP_RANGE;
6449     range->op_ppaddr = PL_ppaddr[OP_RANGE];
6450     range->op_first = left;
6451     range->op_flags = OPf_KIDS;
6452     leftstart = LINKLIST(left);
6453     range->op_other = LINKLIST(right);
6454     range->op_private = (U8)(1 | (flags >> 8));
6455
6456     left->op_sibling = right;
6457
6458     range->op_next = (OP*)range;
6459     flip = newUNOP(OP_FLIP, flags, (OP*)range);
6460     flop = newUNOP(OP_FLOP, 0, flip);
6461     o = newUNOP(OP_NULL, 0, flop);
6462     LINKLIST(flop);
6463     range->op_next = leftstart;
6464
6465     left->op_next = flip;
6466     right->op_next = flop;
6467
6468     range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
6469     sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
6470     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
6471     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
6472
6473     flip->op_private =  left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6474     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6475
6476     /* check barewords before they might be optimized aways */
6477     if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
6478         no_bareword_allowed(left);
6479     if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
6480         no_bareword_allowed(right);
6481
6482     flip->op_next = o;
6483     if (!flip->op_private || !flop->op_private)
6484         LINKLIST(o);            /* blow off optimizer unless constant */
6485
6486     return o;
6487 }
6488
6489 /*
6490 =for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block
6491
6492 Constructs, checks, and returns an op tree expressing a loop.  This is
6493 only a loop in the control flow through the op tree; it does not have
6494 the heavyweight loop structure that allows exiting the loop by C<last>
6495 and suchlike.  I<flags> gives the eight bits of C<op_flags> for the
6496 top-level op, except that some bits will be set automatically as required.
6497 I<expr> supplies the expression controlling loop iteration, and I<block>
6498 supplies the body of the loop; they are consumed by this function and
6499 become part of the constructed op tree.  I<debuggable> is currently
6500 unused and should always be 1.
6501
6502 =cut
6503 */
6504
6505 OP *
6506 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
6507 {
6508     dVAR;
6509     OP* listop;
6510     OP* o;
6511     const bool once = block && block->op_flags & OPf_SPECIAL &&
6512                       block->op_type == OP_NULL;
6513
6514     PERL_UNUSED_ARG(debuggable);
6515
6516     if (expr) {
6517         if (once && (
6518               (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
6519            || (  expr->op_type == OP_NOT
6520               && cUNOPx(expr)->op_first->op_type == OP_CONST
6521               && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
6522               )
6523            ))
6524             /* Return the block now, so that S_new_logop does not try to
6525                fold it away. */
6526             return block;       /* do {} while 0 does once */
6527         if (expr->op_type == OP_READLINE
6528             || expr->op_type == OP_READDIR
6529             || expr->op_type == OP_GLOB
6530             || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
6531             || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
6532             expr = newUNOP(OP_DEFINED, 0,
6533                 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
6534         } else if (expr->op_flags & OPf_KIDS) {
6535             const OP * const k1 = ((UNOP*)expr)->op_first;
6536             const OP * const k2 = k1 ? k1->op_sibling : NULL;
6537             switch (expr->op_type) {
6538               case OP_NULL:
6539                 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
6540                       && (k2->op_flags & OPf_STACKED)
6541                       && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6542                     expr = newUNOP(OP_DEFINED, 0, expr);
6543                 break;
6544
6545               case OP_SASSIGN:
6546                 if (k1 && (k1->op_type == OP_READDIR
6547                       || k1->op_type == OP_GLOB
6548                       || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6549                      || k1->op_type == OP_EACH
6550                      || k1->op_type == OP_AEACH))
6551                     expr = newUNOP(OP_DEFINED, 0, expr);
6552                 break;
6553             }
6554         }
6555     }
6556
6557     /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
6558      * op, in listop. This is wrong. [perl #27024] */
6559     if (!block)
6560         block = newOP(OP_NULL, 0);
6561     listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
6562     o = new_logop(OP_AND, 0, &expr, &listop);
6563
6564     if (once) {
6565         ASSUME(listop);
6566     }
6567
6568     if (listop)
6569         ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
6570
6571     if (once && o != listop)
6572     {
6573         assert(cUNOPo->op_first->op_type == OP_AND
6574             || cUNOPo->op_first->op_type == OP_OR);
6575         o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
6576     }
6577
6578     if (o == listop)
6579         o = newUNOP(OP_NULL, 0, o);     /* or do {} while 1 loses outer block */
6580
6581     o->op_flags |= flags;
6582     o = op_scope(o);
6583     o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
6584     return o;
6585 }
6586
6587 /*
6588 =for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my
6589
6590 Constructs, checks, and returns an op tree expressing a C<while> loop.
6591 This is a heavyweight loop, with structure that allows exiting the loop
6592 by C<last> and suchlike.
6593
6594 I<loop> is an optional preconstructed C<enterloop> op to use in the
6595 loop; if it is null then a suitable op will be constructed automatically.
6596 I<expr> supplies the loop's controlling expression.  I<block> supplies the
6597 main body of the loop, and I<cont> optionally supplies a C<continue> block
6598 that operates as a second half of the body.  All of these optree inputs
6599 are consumed by this function and become part of the constructed op tree.
6600
6601 I<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
6602 op and, shifted up eight bits, the eight bits of C<op_private> for
6603 the C<leaveloop> op, except that (in both cases) some bits will be set
6604 automatically.  I<debuggable> is currently unused and should always be 1.
6605 I<has_my> can be supplied as true to force the
6606 loop body to be enclosed in its own scope.
6607
6608 =cut
6609 */
6610
6611 OP *
6612 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
6613         OP *expr, OP *block, OP *cont, I32 has_my)
6614 {
6615     dVAR;
6616     OP *redo;
6617     OP *next = NULL;
6618     OP *listop;
6619     OP *o;
6620     U8 loopflags = 0;
6621
6622     PERL_UNUSED_ARG(debuggable);
6623
6624     if (expr) {
6625         if (expr->op_type == OP_READLINE
6626          || expr->op_type == OP_READDIR
6627          || expr->op_type == OP_GLOB
6628          || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
6629                      || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
6630             expr = newUNOP(OP_DEFINED, 0,
6631                 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
6632         } else if (expr->op_flags & OPf_KIDS) {
6633             const OP * const k1 = ((UNOP*)expr)->op_first;
6634             const OP * const k2 = (k1) ? k1->op_sibling : NULL;
6635             switch (expr->op_type) {
6636               case OP_NULL:
6637                 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
6638                       && (k2->op_flags & OPf_STACKED)
6639                       && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6640                     expr = newUNOP(OP_DEFINED, 0, expr);
6641                 break;
6642
6643               case OP_SASSIGN:
6644                 if (k1 && (k1->op_type == OP_READDIR
6645                       || k1->op_type == OP_GLOB
6646                       || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6647                      || k1->op_type == OP_EACH
6648                      || k1->op_type == OP_AEACH))
6649                     expr = newUNOP(OP_DEFINED, 0, expr);
6650                 break;
6651             }
6652         }
6653     }
6654
6655     if (!block)
6656         block = newOP(OP_NULL, 0);
6657     else if (cont || has_my) {
6658         block = op_scope(block);
6659     }
6660
6661     if (cont) {
6662         next = LINKLIST(cont);
6663     }
6664     if (expr) {
6665         OP * const unstack = newOP(OP_UNSTACK, 0);
6666         if (!next)
6667             next = unstack;
6668         cont = op_append_elem(OP_LINESEQ, cont, unstack);
6669     }
6670
6671     assert(block);
6672     listop = op_append_list(OP_LINESEQ, block, cont);
6673     assert(listop);
6674     redo = LINKLIST(listop);
6675
6676     if (expr) {
6677         scalar(listop);
6678         o = new_logop(OP_AND, 0, &expr, &listop);
6679         if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
6680             op_free((OP*)loop);
6681             return expr;                /* listop already freed by new_logop */
6682         }
6683         if (listop)
6684             ((LISTOP*)listop)->op_last->op_next =
6685                 (o == listop ? redo : LINKLIST(o));
6686     }
6687     else
6688         o = listop;
6689
6690     if (!loop) {
6691         NewOp(1101,loop,1,LOOP);
6692         loop->op_type = OP_ENTERLOOP;
6693         loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
6694         loop->op_private = 0;
6695         loop->op_next = (OP*)loop;
6696     }
6697
6698     o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
6699
6700     loop->op_redoop = redo;
6701     loop->op_lastop = o;
6702     o->op_private |= loopflags;
6703
6704     if (next)
6705         loop->op_nextop = next;
6706     else
6707         loop->op_nextop = o;
6708
6709     o->op_flags |= flags;
6710     o->op_private |= (flags >> 8);
6711     return o;
6712 }
6713
6714 /*
6715 =for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont
6716
6717 Constructs, checks, and returns an op tree expressing a C<foreach>
6718 loop (iteration through a list of values).  This is a heavyweight loop,
6719 with structure that allows exiting the loop by C<last> and suchlike.
6720
6721 I<sv> optionally supplies the variable that will be aliased to each
6722 item in turn; if null, it defaults to C<$_> (either lexical or global).
6723 I<expr> supplies the list of values to iterate over.  I<block> supplies
6724 the main body of the loop, and I<cont> optionally supplies a C<continue>
6725 block that operates as a second half of the body.  All of these optree
6726 inputs are consumed by this function and become part of the constructed
6727 op tree.
6728
6729 I<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
6730 op and, shifted up eight bits, the eight bits of C<op_private> for
6731 the C<leaveloop> op, except that (in both cases) some bits will be set
6732 automatically.
6733
6734 =cut
6735 */
6736
6737 OP *
6738 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
6739 {
6740     dVAR;
6741     LOOP *loop;
6742     OP *wop;
6743     PADOFFSET padoff = 0;
6744     I32 iterflags = 0;
6745     I32 iterpflags = 0;
6746     OP *madsv = NULL;
6747
6748     PERL_ARGS_ASSERT_NEWFOROP;
6749
6750     if (sv) {
6751         if (sv->op_type == OP_RV2SV) {  /* symbol table variable */
6752             iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
6753             sv->op_type = OP_RV2GV;
6754             sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
6755
6756             /* The op_type check is needed to prevent a possible segfault
6757              * if the loop variable is undeclared and 'strict vars' is in
6758              * effect. This is illegal but is nonetheless parsed, so we
6759              * may reach this point with an OP_CONST where we're expecting
6760              * an OP_GV.
6761              */
6762             if (cUNOPx(sv)->op_first->op_type == OP_GV
6763              && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
6764                 iterpflags |= OPpITER_DEF;
6765         }
6766         else if (sv->op_type == OP_PADSV) { /* private variable */
6767             iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
6768             padoff = sv->op_targ;
6769             if (PL_madskills)
6770                 madsv = sv;
6771             else {
6772                 sv->op_targ = 0;
6773                 op_free(sv);
6774             }
6775             sv = NULL;
6776         }
6777         else
6778             Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
6779         if (padoff) {
6780             SV *const namesv = PAD_COMPNAME_SV(padoff);
6781             STRLEN len;
6782             const char *const name = SvPV_const(namesv, len);
6783
6784             if (len == 2 && name[0] == '$' && name[1] == '_')
6785                 iterpflags |= OPpITER_DEF;
6786         }
6787     }
6788     else {
6789         const PADOFFSET offset = pad_findmy_pvs("$_", 0);
6790         if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
6791             sv = newGVOP(OP_GV, 0, PL_defgv);
6792         }
6793         else {
6794             padoff = offset;
6795         }
6796         iterpflags |= OPpITER_DEF;
6797     }
6798     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
6799         expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
6800         iterflags |= OPf_STACKED;
6801     }
6802     else if (expr->op_type == OP_NULL &&
6803              (expr->op_flags & OPf_KIDS) &&
6804              ((BINOP*)expr)->op_first->op_type == OP_FLOP)
6805     {
6806         /* Basically turn for($x..$y) into the same as for($x,$y), but we
6807          * set the STACKED flag to indicate that these values are to be
6808          * treated as min/max values by 'pp_enteriter'.
6809          */
6810         const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
6811         LOGOP* const range = (LOGOP*) flip->op_first;
6812         OP* const left  = range->op_first;
6813         OP* const right = left->op_sibling;
6814         LISTOP* listop;
6815
6816         range->op_flags &= ~OPf_KIDS;
6817         range->op_first = NULL;
6818
6819         listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
6820         listop->op_first->op_next = range->op_next;
6821         left->op_next = range->op_other;
6822         right->op_next = (OP*)listop;
6823         listop->op_next = listop->op_first;
6824
6825 #ifdef PERL_MAD
6826         op_getmad(expr,(OP*)listop,'O');
6827 #else
6828         op_free(expr);
6829 #endif
6830         expr = (OP*)(listop);
6831         op_null(expr);
6832         iterflags |= OPf_STACKED;
6833     }
6834     else {
6835         expr = op_lvalue(force_list(expr), OP_GREPSTART);
6836     }
6837
6838     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
6839                                op_append_elem(OP_LIST, expr, scalar(sv))));
6840     assert(!loop->op_next);
6841     /* for my  $x () sets OPpLVAL_INTRO;
6842      * for our $x () sets OPpOUR_INTRO */
6843     loop->op_private = (U8)iterpflags;
6844     if (loop->op_slabbed
6845      && DIFF(loop, OpSLOT(loop)->opslot_next)
6846          < SIZE_TO_PSIZE(sizeof(LOOP)))
6847     {
6848         LOOP *tmp;
6849         NewOp(1234,tmp,1,LOOP);
6850         Copy(loop,tmp,1,LISTOP);
6851         S_op_destroy(aTHX_ (OP*)loop);
6852         loop = tmp;
6853     }
6854     else if (!loop->op_slabbed)
6855         loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
6856     loop->op_targ = padoff;
6857     wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
6858     if (madsv)
6859         op_getmad(madsv, (OP*)loop, 'v');
6860     return wop;
6861 }
6862
6863 /*
6864 =for apidoc Am|OP *|newLOOPEX|I32 type|OP *label
6865
6866 Constructs, checks, and returns a loop-exiting op (such as C<goto>
6867 or C<last>).  I<type> is the opcode.  I<label> supplies the parameter
6868 determining the target of the op; it is consumed by this function and
6869 becomes part of the constructed op tree.
6870
6871 =cut
6872 */
6873
6874 OP*
6875 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
6876 {
6877     dVAR;
6878     OP *o = NULL;
6879
6880     PERL_ARGS_ASSERT_NEWLOOPEX;
6881
6882     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
6883
6884     if (type != OP_GOTO) {
6885         /* "last()" means "last" */
6886         if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
6887             o = newOP(type, OPf_SPECIAL);
6888         }
6889     }
6890     else {
6891         /* Check whether it's going to be a goto &function */
6892         if (label->op_type == OP_ENTERSUB
6893                 && !(label->op_flags & OPf_STACKED))
6894             label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
6895     }
6896
6897     /* Check for a constant argument */
6898     if (label->op_type == OP_CONST) {
6899             SV * const sv = ((SVOP *)label)->op_sv;
6900             STRLEN l;
6901             const char *s = SvPV_const(sv,l);
6902             if (l == strlen(s)) {
6903                 o = newPVOP(type,
6904                             SvUTF8(((SVOP*)label)->op_sv),
6905                             savesharedpv(
6906                                 SvPV_nolen_const(((SVOP*)label)->op_sv)));
6907             }
6908     }
6909     
6910     /* If we have already created an op, we do not need the label. */
6911     if (o)
6912 #ifdef PERL_MAD
6913                 op_getmad(label,o,'L');
6914 #else
6915                 op_free(label);
6916 #endif
6917     else o = newUNOP(type, OPf_STACKED, label);
6918
6919     PL_hints |= HINT_BLOCK_SCOPE;
6920     return o;
6921 }
6922
6923 /* if the condition is a literal array or hash
6924    (or @{ ... } etc), make a reference to it.
6925  */
6926 STATIC OP *
6927 S_ref_array_or_hash(pTHX_ OP *cond)
6928 {
6929     if (cond
6930     && (cond->op_type == OP_RV2AV
6931     ||  cond->op_type == OP_PADAV
6932     ||  cond->op_type == OP_RV2HV
6933     ||  cond->op_type == OP_PADHV))
6934
6935         return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
6936
6937     else if(cond
6938     && (cond->op_type == OP_ASLICE
6939     ||  cond->op_type == OP_KVASLICE
6940     ||  cond->op_type == OP_HSLICE
6941     ||  cond->op_type == OP_KVHSLICE)) {
6942
6943         /* anonlist now needs a list from this op, was previously used in
6944          * scalar context */
6945         cond->op_flags |= ~(OPf_WANT_SCALAR | OPf_REF);
6946         cond->op_flags |= OPf_WANT_LIST;
6947
6948         return newANONLIST(op_lvalue(cond, OP_ANONLIST));
6949     }
6950
6951     else
6952         return cond;
6953 }
6954
6955 /* These construct the optree fragments representing given()
6956    and when() blocks.
6957
6958    entergiven and enterwhen are LOGOPs; the op_other pointer
6959    points up to the associated leave op. We need this so we
6960    can put it in the context and make break/continue work.
6961    (Also, of course, pp_enterwhen will jump straight to
6962    op_other if the match fails.)
6963  */
6964
6965 STATIC OP *
6966 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
6967                    I32 enter_opcode, I32 leave_opcode,
6968                    PADOFFSET entertarg)
6969 {
6970     dVAR;
6971     LOGOP *enterop;
6972     OP *o;
6973
6974     PERL_ARGS_ASSERT_NEWGIVWHENOP;
6975
6976     NewOp(1101, enterop, 1, LOGOP);
6977     enterop->op_type = (Optype)enter_opcode;
6978     enterop->op_ppaddr = PL_ppaddr[enter_opcode];
6979     enterop->op_flags =  (U8) OPf_KIDS;
6980     enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg);
6981     enterop->op_private = 0;
6982
6983     o = newUNOP(leave_opcode, 0, (OP *) enterop);
6984
6985     if (cond) {
6986         enterop->op_first = scalar(cond);
6987         cond->op_sibling = block;
6988
6989         o->op_next = LINKLIST(cond);
6990         cond->op_next = (OP *) enterop;
6991     }
6992     else {
6993         /* This is a default {} block */
6994         enterop->op_first = block;
6995         enterop->op_flags |= OPf_SPECIAL;
6996         o      ->op_flags |= OPf_SPECIAL;
6997
6998         o->op_next = (OP *) enterop;
6999     }
7000
7001     CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
7002                                        entergiven and enterwhen both
7003                                        use ck_null() */
7004
7005     enterop->op_next = LINKLIST(block);
7006     block->op_next = enterop->op_other = o;
7007
7008     return o;
7009 }
7010
7011 /* Does this look like a boolean operation? For these purposes
7012    a boolean operation is:
7013      - a subroutine call [*]
7014      - a logical connective
7015      - a comparison operator
7016      - a filetest operator, with the exception of -s -M -A -C
7017      - defined(), exists() or eof()
7018      - /$re/ or $foo =~ /$re/
7019    
7020    [*] possibly surprising
7021  */
7022 STATIC bool
7023 S_looks_like_bool(pTHX_ const OP *o)
7024 {
7025     dVAR;
7026
7027     PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
7028
7029     switch(o->op_type) {
7030         case OP_OR:
7031         case OP_DOR:
7032             return looks_like_bool(cLOGOPo->op_first);
7033
7034         case OP_AND:
7035             return (
7036                 looks_like_bool(cLOGOPo->op_first)
7037              && looks_like_bool(cLOGOPo->op_first->op_sibling));
7038
7039         case OP_NULL:
7040         case OP_SCALAR:
7041             return (
7042                 o->op_flags & OPf_KIDS
7043             && looks_like_bool(cUNOPo->op_first));
7044
7045         case OP_ENTERSUB:
7046
7047         case OP_NOT:    case OP_XOR:
7048
7049         case OP_EQ:     case OP_NE:     case OP_LT:
7050         case OP_GT:     case OP_LE:     case OP_GE:
7051
7052         case OP_I_EQ:   case OP_I_NE:   case OP_I_LT:
7053         case OP_I_GT:   case OP_I_LE:   case OP_I_GE:
7054
7055         case OP_SEQ:    case OP_SNE:    case OP_SLT:
7056         case OP_SGT:    case OP_SLE:    case OP_SGE:
7057         
7058         case OP_SMARTMATCH:
7059         
7060         case OP_FTRREAD:  case OP_FTRWRITE: case OP_FTREXEC:
7061         case OP_FTEREAD:  case OP_FTEWRITE: case OP_FTEEXEC:
7062         case OP_FTIS:     case OP_FTEOWNED: case OP_FTROWNED:
7063         case OP_FTZERO:   case OP_FTSOCK:   case OP_FTCHR:
7064         case OP_FTBLK:    case OP_FTFILE:   case OP_FTDIR:
7065         case OP_FTPIPE:   case OP_FTLINK:   case OP_FTSUID:
7066         case OP_FTSGID:   case OP_FTSVTX:   case OP_FTTTY:
7067         case OP_FTTEXT:   case OP_FTBINARY:
7068         
7069         case OP_DEFINED: case OP_EXISTS:
7070         case OP_MATCH:   case OP_EOF:
7071
7072         case OP_FLOP:
7073
7074             return TRUE;
7075         
7076         case OP_CONST:
7077             /* Detect comparisons that have been optimized away */
7078             if (cSVOPo->op_sv == &PL_sv_yes
7079             ||  cSVOPo->op_sv == &PL_sv_no)
7080             
7081                 return TRUE;
7082             else
7083                 return FALSE;
7084
7085         /* FALL THROUGH */
7086         default:
7087             return FALSE;
7088     }
7089 }
7090
7091 /*
7092 =for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off
7093
7094 Constructs, checks, and returns an op tree expressing a C<given> block.
7095 I<cond> supplies the expression that will be locally assigned to a lexical
7096 variable, and I<block> supplies the body of the C<given> construct; they
7097 are consumed by this function and become part of the constructed op tree.
7098 I<defsv_off> is the pad offset of the scalar lexical variable that will
7099 be affected.  If it is 0, the global $_ will be used.
7100
7101 =cut
7102 */
7103
7104 OP *
7105 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
7106 {
7107     dVAR;
7108     PERL_ARGS_ASSERT_NEWGIVENOP;
7109     return newGIVWHENOP(
7110         ref_array_or_hash(cond),
7111         block,
7112         OP_ENTERGIVEN, OP_LEAVEGIVEN,
7113         defsv_off);
7114 }
7115
7116 /*
7117 =for apidoc Am|OP *|newWHENOP|OP *cond|OP *block
7118
7119 Constructs, checks, and returns an op tree expressing a C<when> block.
7120 I<cond> supplies the test expression, and I<block> supplies the block
7121 that will be executed if the test evaluates to true; they are consumed
7122 by this function and become part of the constructed op tree.  I<cond>
7123 will be interpreted DWIMically, often as a comparison against C<$_>,
7124 and may be null to generate a C<default> block.
7125
7126 =cut
7127 */
7128
7129 OP *
7130 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
7131 {
7132     const bool cond_llb = (!cond || looks_like_bool(cond));
7133     OP *cond_op;
7134
7135     PERL_ARGS_ASSERT_NEWWHENOP;
7136
7137     if (cond_llb)
7138         cond_op = cond;
7139     else {
7140         cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
7141                 newDEFSVOP(),
7142                 scalar(ref_array_or_hash(cond)));
7143     }
7144     
7145     return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
7146 }
7147
7148 void
7149 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
7150                     const STRLEN len, const U32 flags)
7151 {
7152     SV *name = NULL, *msg;
7153     const char * cvp = SvROK(cv) ? "" : CvPROTO(cv);
7154     STRLEN clen = CvPROTOLEN(cv), plen = len;
7155
7156     PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
7157
7158     if (p == NULL && cvp == NULL)
7159         return;
7160
7161     if (!ckWARN_d(WARN_PROTOTYPE))
7162         return;
7163
7164     if (p && cvp) {
7165         p = S_strip_spaces(aTHX_ p, &plen);
7166         cvp = S_strip_spaces(aTHX_ cvp, &clen);
7167         if ((flags & SVf_UTF8) == SvUTF8(cv)) {
7168             if (plen == clen && memEQ(cvp, p, plen))
7169                 return;
7170         } else {
7171             if (flags & SVf_UTF8) {
7172                 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
7173                     return;
7174             }
7175             else {
7176                 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
7177                     return;
7178             }
7179         }
7180     }
7181
7182     msg = sv_newmortal();
7183
7184     if (gv)
7185     {
7186         if (isGV(gv))
7187             gv_efullname3(name = sv_newmortal(), gv, NULL);
7188         else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
7189             name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
7190         else name = (SV *)gv;
7191     }
7192     sv_setpvs(msg, "Prototype mismatch:");
7193     if (name)
7194         Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name));
7195     if (cvp)
7196         Perl_sv_catpvf(aTHX_ msg, " (%"UTF8f")", 
7197             UTF8fARG(SvUTF8(cv),clen,cvp)
7198         );
7199     else
7200         sv_catpvs(msg, ": none");
7201     sv_catpvs(msg, " vs ");
7202     if (p)
7203         Perl_sv_catpvf(aTHX_ msg, "(%"UTF8f")", UTF8fARG(flags & SVf_UTF8,len,p));
7204     else
7205         sv_catpvs(msg, "none");
7206     Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg));
7207 }
7208
7209 static void const_sv_xsub(pTHX_ CV* cv);
7210 static void const_av_xsub(pTHX_ CV* cv);
7211
7212 /*
7213
7214 =head1 Optree Manipulation Functions
7215
7216 =for apidoc cv_const_sv
7217
7218 If C<cv> is a constant sub eligible for inlining, returns the constant
7219 value returned by the sub.  Otherwise, returns NULL.
7220
7221 Constant subs can be created with C<newCONSTSUB> or as described in
7222 L<perlsub/"Constant Functions">.
7223
7224 =cut
7225 */
7226 SV *
7227 Perl_cv_const_sv(pTHX_ const CV *const cv)
7228 {
7229     SV *sv;
7230     PERL_UNUSED_CONTEXT;
7231     if (!cv)
7232         return NULL;
7233     if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
7234         return NULL;
7235     sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7236     if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
7237     return sv;
7238 }
7239
7240 SV *
7241 Perl_cv_const_sv_or_av(pTHX_ const CV * const cv)
7242 {
7243     PERL_UNUSED_CONTEXT;
7244     if (!cv)
7245         return NULL;
7246     assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
7247     return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7248 }
7249
7250 /* op_const_sv:  examine an optree to determine whether it's in-lineable.
7251  * Can be called in 3 ways:
7252  *
7253  * !cv
7254  *      look for a single OP_CONST with attached value: return the value
7255  *
7256  * cv && CvCLONE(cv) && !CvCONST(cv)
7257  *
7258  *      examine the clone prototype, and if contains only a single
7259  *      OP_CONST referencing a pad const, or a single PADSV referencing
7260  *      an outer lexical, return a non-zero value to indicate the CV is
7261  *      a candidate for "constizing" at clone time
7262  *
7263  * cv && CvCONST(cv)
7264  *
7265  *      We have just cloned an anon prototype that was marked as a const
7266  *      candidate. Try to grab the current value, and in the case of
7267  *      PADSV, ignore it if it has multiple references. In this case we
7268  *      return a newly created *copy* of the value.
7269  */
7270
7271 SV *
7272 Perl_op_const_sv(pTHX_ const OP *o, CV *cv)
7273 {
7274     dVAR;
7275     SV *sv = NULL;
7276
7277     if (PL_madskills)
7278         return NULL;
7279
7280     if (!o)
7281         return NULL;
7282
7283     if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
7284         o = cLISTOPo->op_first->op_sibling;
7285
7286     for (; o; o = o->op_next) {
7287         const OPCODE type = o->op_type;
7288
7289         if (sv && o->op_next == o)
7290             return sv;
7291         if (o->op_next != o) {
7292             if (type == OP_NEXTSTATE
7293              || (type == OP_NULL && !(o->op_flags & OPf_KIDS))
7294              || type == OP_PUSHMARK)
7295                 continue;
7296             if (type == OP_DBSTATE)
7297                 continue;
7298         }
7299         if (type == OP_LEAVESUB || type == OP_RETURN)
7300             break;
7301         if (sv)
7302             return NULL;
7303         if (type == OP_CONST && cSVOPo->op_sv)
7304             sv = cSVOPo->op_sv;
7305         else if (cv && type == OP_CONST) {
7306             sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
7307             if (!sv)
7308                 return NULL;
7309         }
7310         else if (cv && type == OP_PADSV) {
7311             if (CvCONST(cv)) { /* newly cloned anon */
7312                 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
7313                 /* the candidate should have 1 ref from this pad and 1 ref
7314                  * from the parent */
7315                 if (!sv || SvREFCNT(sv) != 2)
7316                     return NULL;
7317                 sv = newSVsv(sv);
7318                 SvREADONLY_on(sv);
7319                 return sv;
7320             }
7321             else {
7322                 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
7323                     sv = &PL_sv_undef; /* an arbitrary non-null value */
7324             }
7325         }
7326         else {
7327             return NULL;
7328         }
7329     }
7330     return sv;
7331 }
7332
7333 static bool
7334 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
7335                         PADNAME * const name, SV ** const const_svp)
7336 {
7337     assert (cv);
7338     assert (o || name);
7339     assert (const_svp);
7340     if ((!block
7341 #ifdef PERL_MAD
7342          || block->op_type == OP_NULL
7343 #endif
7344          )) {
7345         if (CvFLAGS(PL_compcv)) {
7346             /* might have had built-in attrs applied */
7347             const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
7348             if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
7349              && ckWARN(WARN_MISC))
7350             {
7351                 /* protect against fatal warnings leaking compcv */
7352                 SAVEFREESV(PL_compcv);
7353                 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
7354                 SvREFCNT_inc_simple_void_NN(PL_compcv);
7355             }
7356             CvFLAGS(cv) |=
7357                 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
7358                   & ~(CVf_LVALUE * pureperl));
7359         }
7360         return FALSE;
7361     }
7362
7363     /* redundant check for speed: */
7364     if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
7365         const line_t oldline = CopLINE(PL_curcop);
7366         SV *namesv = o
7367             ? cSVOPo->op_sv
7368             : sv_2mortal(newSVpvn_utf8(
7369                 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
7370               ));
7371         if (PL_parser && PL_parser->copline != NOLINE)
7372             /* This ensures that warnings are reported at the first
7373                line of a redefinition, not the last.  */
7374             CopLINE_set(PL_curcop, PL_parser->copline);
7375         /* protect against fatal warnings leaking compcv */
7376         SAVEFREESV(PL_compcv);
7377         report_redefined_cv(namesv, cv, const_svp);
7378         SvREFCNT_inc_simple_void_NN(PL_compcv);
7379         CopLINE_set(PL_curcop, oldline);
7380     }
7381 #ifdef PERL_MAD
7382     if (!PL_minus_c)    /* keep old one around for madskills */
7383 #endif
7384     {
7385         /* (PL_madskills unset in used file.) */
7386         SAVEFREESV(cv);
7387     }
7388     return TRUE;
7389 }
7390
7391 CV *
7392 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
7393 {
7394     dVAR;
7395     CV **spot;
7396     SV **svspot;
7397     const char *ps;
7398     STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7399     U32 ps_utf8 = 0;
7400     CV *cv = NULL;
7401     CV *compcv = PL_compcv;
7402     SV *const_sv;
7403     PADNAME *name;
7404     PADOFFSET pax = o->op_targ;
7405     CV *outcv = CvOUTSIDE(PL_compcv);
7406     CV *clonee = NULL;
7407     HEK *hek = NULL;
7408     bool reusable = FALSE;
7409
7410     PERL_ARGS_ASSERT_NEWMYSUB;
7411
7412     /* Find the pad slot for storing the new sub.
7413        We cannot use PL_comppad, as it is the pad owned by the new sub.  We
7414        need to look in CvOUTSIDE and find the pad belonging to the enclos-
7415        ing sub.  And then we need to dig deeper if this is a lexical from
7416        outside, as in:
7417            my sub foo; sub { sub foo { } }
7418      */
7419    redo:
7420     name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
7421     if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
7422         pax = PARENT_PAD_INDEX(name);
7423         outcv = CvOUTSIDE(outcv);
7424         assert(outcv);
7425         goto redo;
7426     }
7427     svspot =
7428         &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
7429                         [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
7430     spot = (CV **)svspot;
7431
7432     if (!(PL_parser && PL_parser->error_count))
7433         move_proto_attr(&proto, &attrs, (GV *)name);
7434
7435     if (proto) {
7436         assert(proto->op_type == OP_CONST);
7437         ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7438         ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7439     }
7440     else
7441         ps = NULL;
7442
7443     if (!PL_madskills) {
7444         if (proto)
7445             SAVEFREEOP(proto);
7446         if (attrs)
7447             SAVEFREEOP(attrs);
7448     }
7449
7450     if (PL_parser && PL_parser->error_count) {
7451         op_free(block);
7452         SvREFCNT_dec(PL_compcv);
7453         PL_compcv = 0;
7454         goto done;
7455     }
7456
7457     if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7458         cv = *spot;
7459         svspot = (SV **)(spot = &clonee);
7460     }
7461     else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
7462         cv = *spot;
7463     else {
7464         MAGIC *mg;
7465         SvUPGRADE(name, SVt_PVMG);
7466         mg = mg_find(name, PERL_MAGIC_proto);
7467         assert (SvTYPE(*spot) == SVt_PVCV);
7468         if (CvNAMED(*spot))
7469             hek = CvNAME_HEK(*spot);
7470         else {
7471             CvNAME_HEK_set(*spot, hek =
7472                 share_hek(
7473                     PadnamePV(name)+1,
7474                     PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1), 0
7475                 )
7476             );
7477         }
7478         if (mg) {
7479             assert(mg->mg_obj);
7480             cv = (CV *)mg->mg_obj;
7481         }
7482         else {
7483             sv_magic(name, &PL_sv_undef, PERL_MAGIC_proto, NULL, 0);
7484             mg = mg_find(name, PERL_MAGIC_proto);
7485         }
7486         spot = (CV **)(svspot = &mg->mg_obj);
7487     }
7488
7489     if (!block || !ps || *ps || attrs
7490         || (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS)
7491 #ifdef PERL_MAD
7492         || block->op_type == OP_NULL
7493 #endif
7494         )
7495         const_sv = NULL;
7496     else
7497         const_sv = op_const_sv(block, NULL);
7498
7499     if (cv) {
7500         const bool exists = CvROOT(cv) || CvXSUB(cv);
7501
7502         /* if the subroutine doesn't exist and wasn't pre-declared
7503          * with a prototype, assume it will be AUTOLOADed,
7504          * skipping the prototype check
7505          */
7506         if (exists || SvPOK(cv))
7507             cv_ckproto_len_flags(cv, (GV *)name, ps, ps_len, ps_utf8);
7508         /* already defined? */
7509         if (exists) {
7510             if (S_already_defined(aTHX_ cv,block,NULL,name,&const_sv))
7511                 cv = NULL;
7512             else {
7513                 if (attrs) goto attrs;
7514                 /* just a "sub foo;" when &foo is already defined */
7515                 SAVEFREESV(compcv);
7516                 goto done;
7517             }
7518         }
7519         else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7520             cv = NULL;
7521             reusable = TRUE;
7522         }
7523     }
7524     if (const_sv) {
7525         SvREFCNT_inc_simple_void_NN(const_sv);
7526         SvFLAGS(const_sv) = (SvFLAGS(const_sv) & ~SVs_PADMY) | SVs_PADTMP;
7527         if (cv) {
7528             assert(!CvROOT(cv) && !CvCONST(cv));
7529             cv_forget_slab(cv);
7530         }
7531         else {
7532             cv = MUTABLE_CV(newSV_type(SVt_PVCV));
7533             CvFILE_set_from_cop(cv, PL_curcop);
7534             CvSTASH_set(cv, PL_curstash);
7535             *spot = cv;
7536         }
7537         sv_setpvs(MUTABLE_SV(cv), "");  /* prototype is "" */
7538         CvXSUBANY(cv).any_ptr = const_sv;
7539         CvXSUB(cv) = const_sv_xsub;
7540         CvCONST_on(cv);
7541         CvISXSUB_on(cv);
7542         if (PL_madskills)
7543             goto install_block;
7544         op_free(block);
7545         SvREFCNT_dec(compcv);
7546         PL_compcv = NULL;
7547         goto setname;
7548     }
7549     /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
7550        determine whether this sub definition is in the same scope as its
7551        declaration.  If this sub definition is inside an inner named pack-
7552        age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
7553        the package sub.  So check PadnameOUTER(name) too.
7554      */
7555     if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) { 
7556         assert(!CvWEAKOUTSIDE(compcv));
7557         SvREFCNT_dec(CvOUTSIDE(compcv));
7558         CvWEAKOUTSIDE_on(compcv);
7559     }
7560     /* XXX else do we have a circular reference? */
7561     if (cv) {   /* must reuse cv in case stub is referenced elsewhere */
7562         /* transfer PL_compcv to cv */
7563         if (block
7564 #ifdef PERL_MAD
7565                   && block->op_type != OP_NULL
7566 #endif
7567         ) {
7568             cv_flags_t preserved_flags =
7569                 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
7570             PADLIST *const temp_padl = CvPADLIST(cv);
7571             CV *const temp_cv = CvOUTSIDE(cv);
7572             const cv_flags_t other_flags =
7573                 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
7574             OP * const cvstart = CvSTART(cv);
7575
7576             SvPOK_off(cv);
7577             CvFLAGS(cv) =
7578                 CvFLAGS(compcv) | preserved_flags;
7579             CvOUTSIDE(cv) = CvOUTSIDE(compcv);
7580             CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
7581             CvPADLIST(cv) = CvPADLIST(compcv);
7582             CvOUTSIDE(compcv) = temp_cv;
7583             CvPADLIST(compcv) = temp_padl;
7584             CvSTART(cv) = CvSTART(compcv);
7585             CvSTART(compcv) = cvstart;
7586             CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
7587             CvFLAGS(compcv) |= other_flags;
7588
7589             if (CvFILE(cv) && CvDYNFILE(cv)) {
7590                 Safefree(CvFILE(cv));
7591             }
7592
7593             /* inner references to compcv must be fixed up ... */
7594             pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
7595             if (PERLDB_INTER)/* Advice debugger on the new sub. */
7596               ++PL_sub_generation;
7597         }
7598         else {
7599             /* Might have had built-in attributes applied -- propagate them. */
7600             CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
7601         }
7602         /* ... before we throw it away */
7603         SvREFCNT_dec(compcv);
7604         PL_compcv = compcv = cv;
7605     }
7606     else {
7607         cv = compcv;
7608         *spot = cv;
7609     }
7610    setname:
7611     if (!CvNAME_HEK(cv)) {
7612         CvNAME_HEK_set(cv,
7613          hek
7614           ? share_hek_hek(hek)
7615           : share_hek(PadnamePV(name)+1,
7616                       PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1),
7617                       0)
7618         );
7619     }
7620     if (const_sv) goto clone;
7621
7622     CvFILE_set_from_cop(cv, PL_curcop);
7623     CvSTASH_set(cv, PL_curstash);
7624
7625     if (ps) {
7626         sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7627         if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7628     }
7629
7630  install_block:
7631     if (!block)
7632         goto attrs;
7633
7634     /* If we assign an optree to a PVCV, then we've defined a subroutine that
7635        the debugger could be able to set a breakpoint in, so signal to
7636        pp_entereval that it should not throw away any saved lines at scope
7637        exit.  */
7638        
7639     PL_breakable_sub_gen++;
7640     /* This makes sub {}; work as expected.  */
7641     if (block->op_type == OP_STUB) {
7642             OP* const newblock = newSTATEOP(0, NULL, 0);
7643 #ifdef PERL_MAD
7644             op_getmad(block,newblock,'B');
7645 #else
7646             op_free(block);
7647 #endif
7648             block = newblock;
7649     }
7650     CvROOT(cv) = CvLVALUE(cv)
7651                    ? newUNOP(OP_LEAVESUBLV, 0,
7652                              op_lvalue(scalarseq(block), OP_LEAVESUBLV))
7653                    : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
7654     CvROOT(cv)->op_private |= OPpREFCOUNTED;
7655     OpREFCNT_set(CvROOT(cv), 1);
7656     /* The cv no longer needs to hold a refcount on the slab, as CvROOT
7657        itself has a refcount. */
7658     CvSLABBED_off(cv);
7659     OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
7660     CvSTART(cv) = LINKLIST(CvROOT(cv));
7661     CvROOT(cv)->op_next = 0;
7662     CALL_PEEP(CvSTART(cv));
7663     finalize_optree(CvROOT(cv));
7664     S_prune_chain_head(aTHX_ &CvSTART(cv));
7665
7666     /* now that optimizer has done its work, adjust pad values */
7667
7668     pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
7669
7670     if (CvCLONE(cv)) {
7671         assert(!CvCONST(cv));
7672         if (ps && !*ps && op_const_sv(block, cv))
7673             CvCONST_on(cv);
7674     }
7675
7676   attrs:
7677     if (attrs) {
7678         /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
7679         apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
7680     }
7681
7682     if (block) {
7683         if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
7684             SV * const tmpstr = sv_newmortal();
7685             GV * const db_postponed = gv_fetchpvs("DB::postponed",
7686                                                   GV_ADDMULTI, SVt_PVHV);
7687             HV *hv;
7688             SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
7689                                           CopFILE(PL_curcop),
7690                                           (long)PL_subline,
7691                                           (long)CopLINE(PL_curcop));
7692             if (HvNAME_HEK(PL_curstash)) {
7693                 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
7694                 sv_catpvs(tmpstr, "::");
7695             }
7696             else sv_setpvs(tmpstr, "__ANON__::");
7697             sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
7698                             PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
7699             (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
7700                     SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
7701             hv = GvHVn(db_postponed);
7702             if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
7703                 CV * const pcv = GvCV(db_postponed);
7704                 if (pcv) {
7705                     dSP;
7706                     PUSHMARK(SP);
7707                     XPUSHs(tmpstr);
7708                     PUTBACK;
7709                     call_sv(MUTABLE_SV(pcv), G_DISCARD);
7710                 }
7711             }
7712         }
7713     }
7714
7715   clone:
7716     if (clonee) {
7717         assert(CvDEPTH(outcv));
7718         spot = (CV **)
7719             &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
7720         if (reusable) cv_clone_into(clonee, *spot);
7721         else *spot = cv_clone(clonee);
7722         SvREFCNT_dec_NN(clonee);
7723         cv = *spot;
7724         SvPADMY_on(cv);
7725     }
7726     if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
7727         PADOFFSET depth = CvDEPTH(outcv);
7728         while (--depth) {
7729             SV *oldcv;
7730             svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
7731             oldcv = *svspot;
7732             *svspot = SvREFCNT_inc_simple_NN(cv);
7733             SvREFCNT_dec(oldcv);
7734         }
7735     }
7736
7737   done:
7738     if (PL_parser)
7739         PL_parser->copline = NOLINE;
7740     LEAVE_SCOPE(floor);
7741     if (o) op_free(o);
7742     return cv;
7743 }
7744
7745 /* _x = extended */
7746 CV *
7747 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
7748                             OP *block, bool o_is_gv)
7749 {
7750     dVAR;
7751     GV *gv;
7752     const char *ps;
7753     STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7754     U32 ps_utf8 = 0;
7755     CV *cv = NULL;
7756     SV *const_sv;
7757     const bool ec = PL_parser && PL_parser->error_count;
7758     /* If the subroutine has no body, no attributes, and no builtin attributes
7759        then it's just a sub declaration, and we may be able to get away with
7760        storing with a placeholder scalar in the symbol table, rather than a
7761        full GV and CV.  If anything is present then it will take a full CV to
7762        store it.  */
7763     const I32 gv_fetch_flags
7764         = ec ? GV_NOADD_NOINIT :
7765          (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
7766            || PL_madskills)
7767         ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
7768     STRLEN namlen = 0;
7769     const char * const name =
7770          o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
7771     bool has_name;
7772     bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
7773 #ifdef PERL_DEBUG_READONLY_OPS
7774     OPSLAB *slab = NULL;
7775 #endif
7776
7777     if (o_is_gv) {
7778         gv = (GV*)o;
7779         o = NULL;
7780         has_name = TRUE;
7781     } else if (name) {
7782         gv = gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV);
7783         has_name = TRUE;
7784     } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
7785         SV * const sv = sv_newmortal();
7786         Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
7787                        PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
7788                        CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
7789         gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
7790         has_name = TRUE;
7791     } else if (PL_curstash) {
7792         gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
7793         has_name = FALSE;
7794     } else {
7795         gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
7796         has_name = FALSE;
7797     }
7798
7799     if (!ec)
7800         move_proto_attr(&proto, &attrs, gv);
7801
7802     if (proto) {
7803         assert(proto->op_type == OP_CONST);
7804         ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7805         ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7806     }
7807     else
7808         ps = NULL;
7809
7810     if (!PL_madskills) {
7811         if (o)
7812             SAVEFREEOP(o);
7813         if (proto)
7814             SAVEFREEOP(proto);
7815         if (attrs)
7816             SAVEFREEOP(attrs);
7817     }
7818
7819     if (ec) {
7820         op_free(block);
7821         if (name) SvREFCNT_dec(PL_compcv);
7822         else cv = PL_compcv;
7823         PL_compcv = 0;
7824         if (name && block) {
7825             const char *s = strrchr(name, ':');
7826             s = s ? s+1 : name;
7827             if (strEQ(s, "BEGIN")) {
7828                 if (PL_in_eval & EVAL_KEEPERR)
7829                     Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
7830                 else {
7831                     SV * const errsv = ERRSV;
7832                     /* force display of errors found but not reported */
7833                     sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
7834                     Perl_croak_nocontext("%"SVf, SVfARG(errsv));
7835                 }
7836             }
7837         }
7838         goto done;
7839     }
7840
7841     if (SvTYPE(gv) != SVt_PVGV) {       /* Maybe prototype now, and had at
7842                                            maximum a prototype before. */
7843         if (SvTYPE(gv) > SVt_NULL) {
7844             cv_ckproto_len_flags((const CV *)gv,
7845                                  o ? (const GV *)cSVOPo->op_sv : NULL, ps,
7846                                  ps_len, ps_utf8);
7847         }
7848         if (ps) {
7849             sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
7850             if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv));
7851         }
7852         else
7853             sv_setiv(MUTABLE_SV(gv), -1);
7854
7855         SvREFCNT_dec(PL_compcv);
7856         cv = PL_compcv = NULL;
7857         goto done;
7858     }
7859
7860     cv = (!name || GvCVGEN(gv)) ? NULL : GvCV(gv);
7861
7862     if (!block || !ps || *ps || attrs
7863         || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
7864 #ifdef PERL_MAD
7865         || block->op_type == OP_NULL
7866 #endif
7867         )
7868         const_sv = NULL;
7869     else
7870         const_sv = op_const_sv(block, NULL);
7871
7872     if (cv) {
7873         const bool exists = CvROOT(cv) || CvXSUB(cv);
7874
7875         /* if the subroutine doesn't exist and wasn't pre-declared
7876          * with a prototype, assume it will be AUTOLOADed,
7877          * skipping the prototype check
7878          */
7879         if (exists || SvPOK(cv))
7880             cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
7881         /* already defined (or promised)? */
7882         if (exists || GvASSUMECV(gv)) {
7883             if (S_already_defined(aTHX_ cv, block, o, NULL, &const_sv))
7884                 cv = NULL;
7885             else {
7886                 if (attrs) goto attrs;
7887                 /* just a "sub foo;" when &foo is already defined */
7888                 SAVEFREESV(PL_compcv);
7889                 goto done;
7890             }
7891         }
7892     }
7893     if (const_sv) {
7894         SvREFCNT_inc_simple_void_NN(const_sv);
7895         SvFLAGS(const_sv) = (SvFLAGS(const_sv) & ~SVs_PADMY) | SVs_PADTMP;
7896         if (cv) {
7897             assert(!CvROOT(cv) && !CvCONST(cv));
7898             cv_forget_slab(cv);
7899             sv_setpvs(MUTABLE_SV(cv), "");  /* prototype is "" */
7900             CvXSUBANY(cv).any_ptr = const_sv;
7901             CvXSUB(cv) = const_sv_xsub;
7902             CvCONST_on(cv);
7903             CvISXSUB_on(cv);
7904         }
7905         else {
7906             GvCV_set(gv, NULL);
7907             cv = newCONSTSUB_flags(
7908                 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
7909                 const_sv
7910             );
7911         }
7912         if (PL_madskills)
7913             goto install_block;
7914         op_free(block);
7915         SvREFCNT_dec(PL_compcv);
7916         PL_compcv = NULL;
7917         goto done;
7918     }
7919     if (cv) {                           /* must reuse cv if autoloaded */
7920         /* transfer PL_compcv to cv */
7921         if (block
7922 #ifdef PERL_MAD
7923                   && block->op_type != OP_NULL
7924 #endif
7925         ) {
7926             cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
7927             PADLIST *const temp_av = CvPADLIST(cv);
7928             CV *const temp_cv = CvOUTSIDE(cv);
7929             const cv_flags_t other_flags =
7930                 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
7931             OP * const cvstart = CvSTART(cv);
7932
7933             CvGV_set(cv,gv);
7934             assert(!CvCVGV_RC(cv));
7935             assert(CvGV(cv) == gv);
7936
7937             SvPOK_off(cv);
7938             CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs;
7939             CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
7940             CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
7941             CvPADLIST(cv) = CvPADLIST(PL_compcv);
7942             CvOUTSIDE(PL_compcv) = temp_cv;
7943             CvPADLIST(PL_compcv) = temp_av;
7944             CvSTART(cv) = CvSTART(PL_compcv);
7945             CvSTART(PL_compcv) = cvstart;
7946             CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
7947             CvFLAGS(PL_compcv) |= other_flags;
7948
7949             if (CvFILE(cv) && CvDYNFILE(cv)) {
7950                 Safefree(CvFILE(cv));
7951     }
7952             CvFILE_set_from_cop(cv, PL_curcop);
7953             CvSTASH_set(cv, PL_curstash);
7954
7955             /* inner references to PL_compcv must be fixed up ... */
7956             pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
7957             if (PERLDB_INTER)/* Advice debugger on the new sub. */
7958               ++PL_sub_generation;
7959         }
7960         else {
7961             /* Might have had built-in attributes applied -- propagate them. */
7962             CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
7963         }
7964         /* ... before we throw it away */
7965         SvREFCNT_dec(PL_compcv);
7966         PL_compcv = cv;
7967     }
7968     else {
7969         cv = PL_compcv;
7970         if (name) {
7971             GvCV_set(gv, cv);
7972             GvCVGEN(gv) = 0;
7973             if (HvENAME_HEK(GvSTASH(gv)))
7974                 /* sub Foo::bar { (shift)+1 } */
7975                 gv_method_changed(gv);
7976         }
7977     }
7978     if (!CvGV(cv)) {
7979         CvGV_set(cv, gv);
7980         CvFILE_set_from_cop(cv, PL_curcop);
7981         CvSTASH_set(cv, PL_curstash);
7982     }
7983
7984     if (ps) {
7985         sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7986         if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7987     }
7988
7989  install_block:
7990     if (!block)
7991         goto attrs;
7992
7993     /* If we assign an optree to a PVCV, then we've defined a subroutine that
7994        the debugger could be able to set a breakpoint in, so signal to
7995        pp_entereval that it should not throw away any saved lines at scope
7996        exit.  */
7997        
7998     PL_breakable_sub_gen++;
7999     /* This makes sub {}; work as expected.  */
8000     if (block->op_type == OP_STUB) {
8001             OP* const newblock = newSTATEOP(0, NULL, 0);
8002 #ifdef PERL_MAD
8003             op_getmad(block,newblock,'B');
8004 #else
8005             op_free(block);
8006 #endif
8007             block = newblock;
8008     }
8009     CvROOT(cv) = CvLVALUE(cv)
8010                    ? newUNOP(OP_LEAVESUBLV, 0,
8011                              op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8012                    : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8013     CvROOT(cv)->op_private |= OPpREFCOUNTED;
8014     OpREFCNT_set(CvROOT(cv), 1);
8015     /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8016        itself has a refcount. */
8017     CvSLABBED_off(cv);
8018     OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8019 #ifdef PERL_DEBUG_READONLY_OPS
8020     slab = (OPSLAB *)CvSTART(cv);
8021 #endif
8022     CvSTART(cv) = LINKLIST(CvROOT(cv));
8023     CvROOT(cv)->op_next = 0;
8024     CALL_PEEP(CvSTART(cv));
8025     finalize_optree(CvROOT(cv));
8026     S_prune_chain_head(aTHX_ &CvSTART(cv));
8027
8028     /* now that optimizer has done its work, adjust pad values */
8029
8030     pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
8031
8032     if (CvCLONE(cv)) {
8033         assert(!CvCONST(cv));
8034         if (ps && !*ps && op_const_sv(block, cv))
8035             CvCONST_on(cv);
8036     }
8037
8038   attrs:
8039     if (attrs) {
8040         /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8041         HV *stash = name && GvSTASH(CvGV(cv)) ? GvSTASH(CvGV(cv)) : PL_curstash;
8042         if (!name) SAVEFREESV(cv);
8043         apply_attrs(stash, MUTABLE_SV(cv), attrs);
8044         if (!name) SvREFCNT_inc_simple_void_NN(cv);
8045     }
8046
8047     if (block && has_name) {
8048         if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8049             SV * const tmpstr = sv_newmortal();
8050             GV * const db_postponed = gv_fetchpvs("DB::postponed",
8051                                                   GV_ADDMULTI, SVt_PVHV);
8052             HV *hv;
8053             SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8054                                           CopFILE(PL_curcop),
8055                                           (long)PL_subline,
8056                                           (long)CopLINE(PL_curcop));
8057             gv_efullname3(tmpstr, gv, NULL);
8058             (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8059                     SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8060             hv = GvHVn(db_postponed);
8061             if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8062                 CV * const pcv = GvCV(db_postponed);
8063                 if (pcv) {
8064                     dSP;
8065                     PUSHMARK(SP);
8066                     XPUSHs(tmpstr);
8067                     PUTBACK;
8068                     call_sv(MUTABLE_SV(pcv), G_DISCARD);
8069                 }
8070             }
8071         }
8072
8073         if (name && ! (PL_parser && PL_parser->error_count))
8074             process_special_blocks(floor, name, gv, cv);
8075     }
8076
8077   done:
8078     if (PL_parser)
8079         PL_parser->copline = NOLINE;
8080     LEAVE_SCOPE(floor);
8081 #ifdef PERL_DEBUG_READONLY_OPS
8082     /* Watch out for BEGIN blocks */
8083     if (slab && gv && isGV(gv) && GvCV(gv)) Slab_to_ro(slab);
8084 #endif
8085     return cv;
8086 }
8087
8088 STATIC void
8089 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
8090                          GV *const gv,
8091                          CV *const cv)
8092 {
8093     const char *const colon = strrchr(fullname,':');
8094     const char *const name = colon ? colon + 1 : fullname;
8095
8096     PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
8097
8098     if (*name == 'B') {
8099         if (strEQ(name, "BEGIN")) {
8100             const I32 oldscope = PL_scopestack_ix;
8101             dSP;
8102             if (floor) LEAVE_SCOPE(floor);
8103             ENTER;
8104             PUSHSTACKi(PERLSI_REQUIRE);
8105             SAVECOPFILE(&PL_compiling);
8106             SAVECOPLINE(&PL_compiling);
8107             SAVEVPTR(PL_curcop);
8108
8109             DEBUG_x( dump_sub(gv) );
8110             Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
8111             GvCV_set(gv,0);             /* cv has been hijacked */
8112             call_list(oldscope, PL_beginav);
8113
8114             POPSTACK;
8115             LEAVE;
8116         }
8117         else
8118             return;
8119     } else {
8120         if (*name == 'E') {
8121             if strEQ(name, "END") {
8122                 DEBUG_x( dump_sub(gv) );
8123                 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
8124             } else
8125                 return;
8126         } else if (*name == 'U') {
8127             if (strEQ(name, "UNITCHECK")) {
8128                 /* It's never too late to run a unitcheck block */
8129                 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
8130             }
8131             else
8132                 return;
8133         } else if (*name == 'C') {
8134             if (strEQ(name, "CHECK")) {
8135                 if (PL_main_start)
8136                     /* diag_listed_as: Too late to run %s block */
8137                     Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8138                                    "Too late to run CHECK block");
8139                 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
8140             }
8141             else
8142                 return;
8143         } else if (*name == 'I') {
8144             if (strEQ(name, "INIT")) {
8145                 if (PL_main_start)
8146                     /* diag_listed_as: Too late to run %s block */
8147                     Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8148                                    "Too late to run INIT block");
8149                 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
8150             }
8151             else
8152                 return;
8153         } else
8154             return;
8155         DEBUG_x( dump_sub(gv) );
8156         GvCV_set(gv,0);         /* cv has been hijacked */
8157     }
8158 }
8159
8160 /*
8161 =for apidoc newCONSTSUB
8162
8163 See L</newCONSTSUB_flags>.
8164
8165 =cut
8166 */
8167
8168 CV *
8169 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
8170 {
8171     return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
8172 }
8173
8174 /*
8175 =for apidoc newCONSTSUB_flags
8176
8177 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
8178 eligible for inlining at compile-time.
8179
8180 Currently, the only useful value for C<flags> is SVf_UTF8.
8181
8182 The newly created subroutine takes ownership of a reference to the passed in
8183 SV.
8184
8185 Passing NULL for SV creates a constant sub equivalent to C<sub BAR () {}>,
8186 which won't be called if used as a destructor, but will suppress the overhead
8187 of a call to C<AUTOLOAD>.  (This form, however, isn't eligible for inlining at
8188 compile time.)
8189
8190 =cut
8191 */
8192
8193 CV *
8194 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
8195                              U32 flags, SV *sv)
8196 {
8197     dVAR;
8198     CV* cv;
8199     const char *const file = CopFILE(PL_curcop);
8200
8201     ENTER;
8202
8203     if (IN_PERL_RUNTIME) {
8204         /* at runtime, it's not safe to manipulate PL_curcop: it may be
8205          * an op shared between threads. Use a non-shared COP for our
8206          * dirty work */
8207          SAVEVPTR(PL_curcop);
8208          SAVECOMPILEWARNINGS();
8209          PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
8210          PL_curcop = &PL_compiling;
8211     }
8212     SAVECOPLINE(PL_curcop);
8213     CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
8214
8215     SAVEHINTS();
8216     PL_hints &= ~HINT_BLOCK_SCOPE;
8217
8218     if (stash) {
8219         SAVEGENERICSV(PL_curstash);
8220         PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
8221     }
8222
8223     /* Protect sv against leakage caused by fatal warnings. */
8224     if (sv) SAVEFREESV(sv);
8225
8226     /* file becomes the CvFILE. For an XS, it's usually static storage,
8227        and so doesn't get free()d.  (It's expected to be from the C pre-
8228        processor __FILE__ directive). But we need a dynamically allocated one,
8229        and we need it to get freed.  */
8230     cv = newXS_len_flags(name, len,
8231                          sv && SvTYPE(sv) == SVt_PVAV
8232                              ? const_av_xsub
8233                              : const_sv_xsub,
8234                          file ? file : "", "",
8235                          &sv, XS_DYNAMIC_FILENAME | flags);
8236     CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
8237     CvCONST_on(cv);
8238
8239     LEAVE;
8240
8241     return cv;
8242 }
8243
8244 CV *
8245 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
8246                  const char *const filename, const char *const proto,
8247                  U32 flags)
8248 {
8249     PERL_ARGS_ASSERT_NEWXS_FLAGS;
8250     return newXS_len_flags(
8251        name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
8252     );
8253 }
8254
8255 CV *
8256 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
8257                            XSUBADDR_t subaddr, const char *const filename,
8258                            const char *const proto, SV **const_svp,
8259                            U32 flags)
8260 {
8261     CV *cv;
8262     bool interleave = FALSE;
8263
8264     PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
8265
8266     {
8267         GV * const gv = gv_fetchpvn(
8268                             name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8269                             name ? len : PL_curstash ? sizeof("__ANON__") - 1:
8270                                 sizeof("__ANON__::__ANON__") - 1,
8271                             GV_ADDMULTI | flags, SVt_PVCV);
8272     
8273         if (!subaddr)
8274             Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
8275     
8276         if ((cv = (name ? GvCV(gv) : NULL))) {
8277             if (GvCVGEN(gv)) {
8278                 /* just a cached method */
8279                 SvREFCNT_dec(cv);
8280                 cv = NULL;
8281             }
8282             else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
8283                 /* already defined (or promised) */
8284                 /* Redundant check that allows us to avoid creating an SV
8285                    most of the time: */
8286                 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
8287                     report_redefined_cv(newSVpvn_flags(
8288                                          name,len,(flags&SVf_UTF8)|SVs_TEMP
8289                                         ),
8290                                         cv, const_svp);
8291                 }
8292                 interleave = TRUE;
8293                 ENTER;
8294                 SAVEFREESV(cv);
8295                 cv = NULL;
8296             }
8297         }
8298     
8299         if (cv)                         /* must reuse cv if autoloaded */
8300             cv_undef(cv);
8301         else {
8302             cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8303             if (name) {
8304                 GvCV_set(gv,cv);
8305                 GvCVGEN(gv) = 0;
8306                 if (HvENAME_HEK(GvSTASH(gv)))
8307                     gv_method_changed(gv); /* newXS */
8308             }
8309         }
8310         if (!name)
8311             CvANON_on(cv);
8312         CvGV_set(cv, gv);
8313         (void)gv_fetchfile(filename);
8314         CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
8315                                     an external constant string */
8316         assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
8317         CvISXSUB_on(cv);
8318         CvXSUB(cv) = subaddr;
8319     
8320         if (name)
8321             process_special_blocks(0, name, gv, cv);
8322     }
8323
8324     if (flags & XS_DYNAMIC_FILENAME) {
8325         CvFILE(cv) = savepv(filename);
8326         CvDYNFILE_on(cv);
8327     }
8328     sv_setpv(MUTABLE_SV(cv), proto);
8329     if (interleave) LEAVE;
8330     return cv;
8331 }
8332
8333 CV *
8334 Perl_newSTUB(pTHX_ GV *gv, bool fake)
8335 {
8336     CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8337     GV *cvgv;
8338     PERL_ARGS_ASSERT_NEWSTUB;
8339     assert(!GvCVu(gv));
8340     GvCV_set(gv, cv);
8341     GvCVGEN(gv) = 0;
8342     if (!fake && HvENAME_HEK(GvSTASH(gv)))
8343         gv_method_changed(gv);
8344     if (SvFAKE(gv)) {
8345         cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
8346         SvFAKE_off(cvgv);
8347     }
8348     else cvgv = gv;
8349     CvGV_set(cv, cvgv);
8350     CvFILE_set_from_cop(cv, PL_curcop);
8351     CvSTASH_set(cv, PL_curstash);
8352     GvMULTI_on(gv);
8353     return cv;
8354 }
8355
8356 /*
8357 =for apidoc U||newXS
8358
8359 Used by C<xsubpp> to hook up XSUBs as Perl subs.  I<filename> needs to be
8360 static storage, as it is used directly as CvFILE(), without a copy being made.
8361
8362 =cut
8363 */
8364
8365 CV *
8366 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
8367 {
8368     PERL_ARGS_ASSERT_NEWXS;
8369     return newXS_len_flags(
8370         name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
8371     );
8372 }
8373
8374 #ifdef PERL_MAD
8375 OP *
8376 #else
8377 void
8378 #endif
8379 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
8380 {
8381     dVAR;
8382     CV *cv;
8383 #ifdef PERL_MAD
8384     OP* pegop = newOP(OP_NULL, 0);
8385 #endif
8386
8387     GV *gv;
8388
8389     if (PL_parser && PL_parser->error_count) {
8390         op_free(block);
8391         goto finish;
8392     }
8393
8394     gv = o
8395         ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
8396         : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
8397
8398     GvMULTI_on(gv);
8399     if ((cv = GvFORM(gv))) {
8400         if (ckWARN(WARN_REDEFINE)) {
8401             const line_t oldline = CopLINE(PL_curcop);
8402             if (PL_parser && PL_parser->copline != NOLINE)
8403                 CopLINE_set(PL_curcop, PL_parser->copline);
8404             if (o) {
8405                 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8406                             "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv));
8407             } else {
8408                 /* diag_listed_as: Format %s redefined */
8409                 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8410                             "Format STDOUT redefined");
8411             }
8412             CopLINE_set(PL_curcop, oldline);
8413         }
8414         SvREFCNT_dec(cv);
8415     }
8416     cv = PL_compcv;
8417     GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
8418     CvGV_set(cv, gv);
8419     CvFILE_set_from_cop(cv, PL_curcop);
8420
8421
8422     pad_tidy(padtidy_FORMAT);
8423     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
8424     CvROOT(cv)->op_private |= OPpREFCOUNTED;
8425     OpREFCNT_set(CvROOT(cv), 1);
8426     CvSTART(cv) = LINKLIST(CvROOT(cv));
8427     CvROOT(cv)->op_next = 0;
8428     CALL_PEEP(CvSTART(cv));
8429     finalize_optree(CvROOT(cv));
8430     S_prune_chain_head(aTHX_ &CvSTART(cv));
8431     cv_forget_slab(cv);
8432
8433   finish:
8434 #ifdef PERL_MAD
8435     op_getmad(o,pegop,'n');
8436     op_getmad_weak(block, pegop, 'b');
8437 #else
8438     op_free(o);
8439 #endif
8440     if (PL_parser)
8441         PL_parser->copline = NOLINE;
8442     LEAVE_SCOPE(floor);
8443 #ifdef PERL_MAD
8444     return pegop;
8445 #endif
8446 }
8447
8448 OP *
8449 Perl_newANONLIST(pTHX_ OP *o)
8450 {
8451     return convert(OP_ANONLIST, OPf_SPECIAL, o);
8452 }
8453
8454 OP *
8455 Perl_newANONHASH(pTHX_ OP *o)
8456 {
8457     return convert(OP_ANONHASH, OPf_SPECIAL, o);
8458 }
8459
8460 OP *
8461 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
8462 {
8463     return newANONATTRSUB(floor, proto, NULL, block);
8464 }
8465
8466 OP *
8467 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
8468 {
8469     return newUNOP(OP_REFGEN, 0,
8470         newSVOP(OP_ANONCODE, 0,
8471                 MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block))));
8472 }
8473
8474 OP *
8475 Perl_oopsAV(pTHX_ OP *o)
8476 {
8477     dVAR;
8478
8479     PERL_ARGS_ASSERT_OOPSAV;
8480
8481     switch (o->op_type) {
8482     case OP_PADSV:
8483     case OP_PADHV:
8484         o->op_type = OP_PADAV;
8485         o->op_ppaddr = PL_ppaddr[OP_PADAV];
8486         return ref(o, OP_RV2AV);
8487
8488     case OP_RV2SV:
8489     case OP_RV2HV:
8490         o->op_type = OP_RV2AV;
8491         o->op_ppaddr = PL_ppaddr[OP_RV2AV];
8492         ref(o, OP_RV2AV);
8493         break;
8494
8495     default:
8496         Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
8497         break;
8498     }
8499     return o;
8500 }
8501
8502 OP *
8503 Perl_oopsHV(pTHX_ OP *o)
8504 {
8505     dVAR;
8506
8507     PERL_ARGS_ASSERT_OOPSHV;
8508
8509     switch (o->op_type) {
8510     case OP_PADSV:
8511     case OP_PADAV:
8512         o->op_type = OP_PADHV;
8513         o->op_ppaddr = PL_ppaddr[OP_PADHV];
8514         return ref(o, OP_RV2HV);
8515
8516     case OP_RV2SV:
8517     case OP_RV2AV:
8518         o->op_type = OP_RV2HV;
8519         o->op_ppaddr = PL_ppaddr[OP_RV2HV];
8520         ref(o, OP_RV2HV);
8521         break;
8522
8523     default:
8524         Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
8525         break;
8526     }
8527     return o;
8528 }
8529
8530 OP *
8531 Perl_newAVREF(pTHX_ OP *o)
8532 {
8533     dVAR;
8534
8535     PERL_ARGS_ASSERT_NEWAVREF;
8536
8537     if (o->op_type == OP_PADANY) {
8538         o->op_type = OP_PADAV;
8539         o->op_ppaddr = PL_ppaddr[OP_PADAV];
8540         return o;
8541     }
8542     else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
8543         Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8544                        "Using an array as a reference is deprecated");
8545     }
8546     return newUNOP(OP_RV2AV, 0, scalar(o));
8547 }
8548
8549 OP *
8550 Perl_newGVREF(pTHX_ I32 type, OP *o)
8551 {
8552     if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
8553         return newUNOP(OP_NULL, 0, o);
8554     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
8555 }
8556
8557 OP *
8558 Perl_newHVREF(pTHX_ OP *o)
8559 {
8560     dVAR;
8561
8562     PERL_ARGS_ASSERT_NEWHVREF;
8563
8564     if (o->op_type == OP_PADANY) {
8565         o->op_type = OP_PADHV;
8566         o->op_ppaddr = PL_ppaddr[OP_PADHV];
8567         return o;
8568     }
8569     else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
8570         Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8571                        "Using a hash as a reference is deprecated");
8572     }
8573     return newUNOP(OP_RV2HV, 0, scalar(o));
8574 }
8575
8576 OP *
8577 Perl_newCVREF(pTHX_ I32 flags, OP *o)
8578 {
8579     if (o->op_type == OP_PADANY) {
8580         dVAR;
8581         o->op_type = OP_PADCV;
8582         o->op_ppaddr = PL_ppaddr[OP_PADCV];
8583     }
8584     return newUNOP(OP_RV2CV, flags, scalar(o));
8585 }
8586
8587 OP *
8588 Perl_newSVREF(pTHX_ OP *o)
8589 {
8590     dVAR;
8591
8592     PERL_ARGS_ASSERT_NEWSVREF;
8593
8594     if (o->op_type == OP_PADANY) {
8595         o->op_type = OP_PADSV;
8596         o->op_ppaddr = PL_ppaddr[OP_PADSV];
8597         return o;
8598     }
8599     return newUNOP(OP_RV2SV, 0, scalar(o));
8600 }
8601
8602 /* Check routines. See the comments at the top of this file for details
8603  * on when these are called */
8604
8605 OP *
8606 Perl_ck_anoncode(pTHX_ OP *o)
8607 {
8608     PERL_ARGS_ASSERT_CK_ANONCODE;
8609
8610     cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
8611     if (!PL_madskills)
8612         cSVOPo->op_sv = NULL;
8613     return o;
8614 }
8615
8616 static void
8617 S_io_hints(pTHX_ OP *o)
8618 {
8619     HV * const table =
8620         PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
8621     if (table) {
8622         SV **svp = hv_fetchs(table, "open_IN", FALSE);
8623         if (svp && *svp) {
8624             STRLEN len = 0;
8625             const char *d = SvPV_const(*svp, len);
8626             const I32 mode = mode_from_discipline(d, len);
8627             if (mode & O_BINARY)
8628                 o->op_private |= OPpOPEN_IN_RAW;
8629             else if (mode & O_TEXT)
8630                 o->op_private |= OPpOPEN_IN_CRLF;
8631         }
8632
8633         svp = hv_fetchs(table, "open_OUT", FALSE);
8634         if (svp && *svp) {
8635             STRLEN len = 0;
8636             const char *d = SvPV_const(*svp, len);
8637             const I32 mode = mode_from_discipline(d, len);
8638             if (mode & O_BINARY)
8639                 o->op_private |= OPpOPEN_OUT_RAW;
8640             else if (mode & O_TEXT)
8641                 o->op_private |= OPpOPEN_OUT_CRLF;
8642         }
8643     }
8644 }
8645
8646 OP *
8647 Perl_ck_backtick(pTHX_ OP *o)
8648 {
8649     GV *gv;
8650     OP *newop = NULL;
8651     PERL_ARGS_ASSERT_CK_BACKTICK;
8652     /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
8653     if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_sibling
8654      && (gv = gv_override("readpipe",8))) {
8655         newop = S_new_entersubop(aTHX_ gv, cUNOPo->op_first->op_sibling);
8656         cUNOPo->op_first->op_sibling = NULL;
8657     }
8658     else if (!(o->op_flags & OPf_KIDS))
8659         newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
8660     if (newop) {
8661 #ifdef PERL_MAD
8662         op_getmad(o,newop,'O');
8663 #else
8664         op_free(o);
8665 #endif
8666         return newop;
8667     }
8668     S_io_hints(aTHX_ o);
8669     return o;
8670 }
8671
8672 OP *
8673 Perl_ck_bitop(pTHX_ OP *o)
8674 {
8675     dVAR;
8676
8677     PERL_ARGS_ASSERT_CK_BITOP;
8678
8679     o->op_private = (U8)(PL_hints & HINT_INTEGER);
8680     if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
8681             && (o->op_type == OP_BIT_OR
8682              || o->op_type == OP_BIT_AND
8683              || o->op_type == OP_BIT_XOR))
8684     {
8685         const OP * const left = cBINOPo->op_first;
8686         const OP * const right = left->op_sibling;
8687         if ((OP_IS_NUMCOMPARE(left->op_type) &&
8688                 (left->op_flags & OPf_PARENS) == 0) ||
8689             (OP_IS_NUMCOMPARE(right->op_type) &&
8690                 (right->op_flags & OPf_PARENS) == 0))
8691             Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8692                            "Possible precedence problem on bitwise %c operator",
8693                            o->op_type == OP_BIT_OR ? '|'
8694                            : o->op_type == OP_BIT_AND ? '&' : '^'
8695                            );
8696     }
8697     return o;
8698 }
8699
8700 PERL_STATIC_INLINE bool
8701 is_dollar_bracket(pTHX_ const OP * const o)
8702 {
8703     const OP *kid;
8704     return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
8705         && (kid = cUNOPx(o)->op_first)
8706         && kid->op_type == OP_GV
8707         && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
8708 }
8709
8710 OP *
8711 Perl_ck_cmp(pTHX_ OP *o)
8712 {
8713     PERL_ARGS_ASSERT_CK_CMP;
8714     if (ckWARN(WARN_SYNTAX)) {
8715         const OP *kid = cUNOPo->op_first;
8716         if (kid && (
8717                 (
8718                    is_dollar_bracket(aTHX_ kid)
8719                 && kid->op_sibling && kid->op_sibling->op_type == OP_CONST
8720                 )
8721              || (  kid->op_type == OP_CONST
8722                 && (kid = kid->op_sibling) && is_dollar_bracket(aTHX_ kid))
8723            ))
8724             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
8725                         "$[ used in %s (did you mean $] ?)", OP_DESC(o));
8726     }
8727     return o;
8728 }
8729
8730 OP *
8731 Perl_ck_concat(pTHX_ OP *o)
8732 {
8733     const OP * const kid = cUNOPo->op_first;
8734
8735     PERL_ARGS_ASSERT_CK_CONCAT;
8736     PERL_UNUSED_CONTEXT;
8737
8738     if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
8739             !(kUNOP->op_first->op_flags & OPf_MOD))
8740         o->op_flags |= OPf_STACKED;
8741     return o;
8742 }
8743
8744 OP *
8745 Perl_ck_spair(pTHX_ OP *o)
8746 {
8747     dVAR;
8748
8749     PERL_ARGS_ASSERT_CK_SPAIR;
8750
8751     if (o->op_flags & OPf_KIDS) {
8752         OP* newop;
8753         OP* kid;
8754         const OPCODE type = o->op_type;
8755         o = modkids(ck_fun(o), type);
8756         kid = cUNOPo->op_first;
8757         newop = kUNOP->op_first->op_sibling;
8758         if (newop) {
8759             const OPCODE type = newop->op_type;
8760             if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
8761                     type == OP_PADAV || type == OP_PADHV ||
8762                     type == OP_RV2AV || type == OP_RV2HV)
8763                 return o;
8764         }
8765 #ifdef PERL_MAD
8766         op_getmad(kUNOP->op_first,newop,'K');
8767 #else
8768         op_free(kUNOP->op_first);
8769 #endif
8770         kUNOP->op_first = newop;
8771     }
8772     /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
8773      * and OP_CHOMP into OP_SCHOMP */
8774     o->op_ppaddr = PL_ppaddr[++o->op_type];
8775     return ck_fun(o);
8776 }
8777
8778 OP *
8779 Perl_ck_delete(pTHX_ OP *o)
8780 {
8781     PERL_ARGS_ASSERT_CK_DELETE;
8782
8783     o = ck_fun(o);
8784     o->op_private = 0;
8785     if (o->op_flags & OPf_KIDS) {
8786         OP * const kid = cUNOPo->op_first;
8787         switch (kid->op_type) {
8788         case OP_ASLICE:
8789             o->op_flags |= OPf_SPECIAL;
8790             /* FALL THROUGH */
8791         case OP_HSLICE:
8792             o->op_private |= OPpSLICE;
8793             break;
8794         case OP_AELEM:
8795             o->op_flags |= OPf_SPECIAL;
8796             /* FALL THROUGH */
8797         case OP_HELEM:
8798             break;
8799         case OP_KVASLICE:
8800             Perl_croak(aTHX_ "delete argument is index/value array slice,"
8801                              " use array slice");
8802         case OP_KVHSLICE:
8803             Perl_croak(aTHX_ "delete argument is key/value hash slice, use"
8804                              " hash slice");
8805         default:
8806             Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
8807                              "element or slice");
8808         }
8809         if (kid->op_private & OPpLVAL_INTRO)
8810             o->op_private |= OPpLVAL_INTRO;
8811         op_null(kid);
8812     }
8813     return o;
8814 }
8815
8816 OP *
8817 Perl_ck_eof(pTHX_ OP *o)
8818 {
8819     dVAR;
8820
8821     PERL_ARGS_ASSERT_CK_EOF;
8822
8823     if (o->op_flags & OPf_KIDS) {
8824         OP *kid;
8825         if (cLISTOPo->op_first->op_type == OP_STUB) {
8826             OP * const newop
8827                 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
8828 #ifdef PERL_MAD
8829             op_getmad(o,newop,'O');
8830 #else
8831             op_free(o);
8832 #endif
8833             o = newop;
8834         }
8835         o = ck_fun(o);
8836         kid = cLISTOPo->op_first;
8837         if (kid->op_type == OP_RV2GV)
8838             kid->op_private |= OPpALLOW_FAKE;
8839     }
8840     return o;
8841 }
8842
8843 OP *
8844 Perl_ck_eval(pTHX_ OP *o)
8845 {
8846     dVAR;
8847
8848     PERL_ARGS_ASSERT_CK_EVAL;
8849
8850     PL_hints |= HINT_BLOCK_SCOPE;
8851     if (o->op_flags & OPf_KIDS) {
8852         SVOP * const kid = (SVOP*)cUNOPo->op_first;
8853         assert(kid);
8854
8855         if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
8856             LOGOP *enter;
8857 #ifdef PERL_MAD
8858             OP* const oldo = o;
8859 #endif
8860
8861             cUNOPo->op_first = 0;
8862 #ifndef PERL_MAD
8863             op_free(o);
8864 #endif
8865
8866             NewOp(1101, enter, 1, LOGOP);
8867             enter->op_type = OP_ENTERTRY;
8868             enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
8869             enter->op_private = 0;
8870
8871             /* establish postfix order */
8872             enter->op_next = (OP*)enter;
8873
8874             o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
8875             o->op_type = OP_LEAVETRY;
8876             o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
8877             enter->op_other = o;
8878             op_getmad(oldo,o,'O');
8879             return o;
8880         }
8881         else {
8882             scalar((OP*)kid);
8883             PL_cv_has_eval = 1;
8884         }
8885     }
8886     else {
8887         const U8 priv = o->op_private;
8888 #ifdef PERL_MAD
8889         OP* const oldo = o;
8890 #else
8891         op_free(o);
8892 #endif
8893         o = newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
8894         op_getmad(oldo,o,'O');
8895     }
8896     o->op_targ = (PADOFFSET)PL_hints;
8897     if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
8898     if ((PL_hints & HINT_LOCALIZE_HH) != 0
8899      && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
8900         /* Store a copy of %^H that pp_entereval can pick up. */
8901         OP *hhop = newSVOP(OP_HINTSEVAL, 0,
8902                            MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
8903         cUNOPo->op_first->op_sibling = hhop;
8904         o->op_private |= OPpEVAL_HAS_HH;
8905     }
8906     if (!(o->op_private & OPpEVAL_BYTES)
8907          && FEATURE_UNIEVAL_IS_ENABLED)
8908             o->op_private |= OPpEVAL_UNICODE;
8909     return o;
8910 }
8911
8912 OP *
8913 Perl_ck_exec(pTHX_ OP *o)
8914 {
8915     PERL_ARGS_ASSERT_CK_EXEC;
8916
8917     if (o->op_flags & OPf_STACKED) {
8918         OP *kid;
8919         o = ck_fun(o);
8920         kid = cUNOPo->op_first->op_sibling;
8921         if (kid->op_type == OP_RV2GV)
8922             op_null(kid);
8923     }
8924     else
8925         o = listkids(o);
8926     return o;
8927 }
8928
8929 OP *
8930 Perl_ck_exists(pTHX_ OP *o)
8931 {
8932     dVAR;
8933
8934     PERL_ARGS_ASSERT_CK_EXISTS;
8935
8936     o = ck_fun(o);
8937     if (o->op_flags & OPf_KIDS) {
8938         OP * const kid = cUNOPo->op_first;
8939         if (kid->op_type == OP_ENTERSUB) {
8940             (void) ref(kid, o->op_type);
8941             if (kid->op_type != OP_RV2CV
8942                         && !(PL_parser && PL_parser->error_count))
8943                 Perl_croak(aTHX_
8944                           "exists argument is not a subroutine name");
8945             o->op_private |= OPpEXISTS_SUB;
8946         }
8947         else if (kid->op_type == OP_AELEM)
8948             o->op_flags |= OPf_SPECIAL;
8949         else if (kid->op_type != OP_HELEM)
8950             Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
8951                              "element or a subroutine");
8952         op_null(kid);
8953     }
8954     return o;
8955 }
8956
8957 OP *
8958 Perl_ck_rvconst(pTHX_ OP *o)
8959 {
8960     dVAR;
8961     SVOP * const kid = (SVOP*)cUNOPo->op_first;
8962
8963     PERL_ARGS_ASSERT_CK_RVCONST;
8964
8965     o->op_private |= (PL_hints & HINT_STRICT_REFS);
8966     if (o->op_type == OP_RV2CV)
8967         o->op_private &= ~1;
8968
8969     if (kid->op_type == OP_CONST) {
8970         int iscv;
8971         GV *gv;
8972         SV * const kidsv = kid->op_sv;
8973
8974         /* Is it a constant from cv_const_sv()? */
8975         if (SvROK(kidsv) && SvREADONLY(kidsv)) {
8976             SV * const rsv = SvRV(kidsv);
8977             const svtype type = SvTYPE(rsv);
8978             const char *badtype = NULL;
8979
8980             switch (o->op_type) {
8981             case OP_RV2SV:
8982                 if (type > SVt_PVMG)
8983                     badtype = "a SCALAR";
8984                 break;
8985             case OP_RV2AV:
8986                 if (type != SVt_PVAV)
8987                     badtype = "an ARRAY";
8988                 break;
8989             case OP_RV2HV:
8990                 if (type != SVt_PVHV)
8991                     badtype = "a HASH";
8992                 break;
8993             case OP_RV2CV:
8994                 if (type != SVt_PVCV)
8995                     badtype = "a CODE";
8996                 break;
8997             }
8998             if (badtype)
8999                 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
9000             return o;
9001         }
9002         if (SvTYPE(kidsv) == SVt_PVAV) return o;
9003         if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
9004             const char *badthing;
9005             switch (o->op_type) {
9006             case OP_RV2SV:
9007                 badthing = "a SCALAR";
9008                 break;
9009             case OP_RV2AV:
9010                 badthing = "an ARRAY";
9011                 break;
9012             case OP_RV2HV:
9013                 badthing = "a HASH";
9014                 break;
9015             default:
9016                 badthing = NULL;
9017                 break;
9018             }
9019             if (badthing)
9020                 Perl_croak(aTHX_
9021                            "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
9022                            SVfARG(kidsv), badthing);
9023         }
9024         /*
9025          * This is a little tricky.  We only want to add the symbol if we
9026          * didn't add it in the lexer.  Otherwise we get duplicate strict
9027          * warnings.  But if we didn't add it in the lexer, we must at
9028          * least pretend like we wanted to add it even if it existed before,
9029          * or we get possible typo warnings.  OPpCONST_ENTERED says
9030          * whether the lexer already added THIS instance of this symbol.
9031          */
9032         iscv = (o->op_type == OP_RV2CV) * 2;
9033         do {
9034             gv = gv_fetchsv(kidsv,
9035                 iscv | !(kid->op_private & OPpCONST_ENTERED),
9036                 iscv
9037                     ? SVt_PVCV
9038                     : o->op_type == OP_RV2SV
9039                         ? SVt_PV
9040                         : o->op_type == OP_RV2AV
9041                             ? SVt_PVAV
9042                             : o->op_type == OP_RV2HV
9043                                 ? SVt_PVHV
9044                                 : SVt_PVGV);
9045         } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
9046         if (gv) {
9047             kid->op_type = OP_GV;
9048             SvREFCNT_dec(kid->op_sv);
9049 #ifdef USE_ITHREADS
9050             /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
9051             assert (sizeof(PADOP) <= sizeof(SVOP));
9052             kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
9053             SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
9054             GvIN_PAD_on(gv);
9055             PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
9056 #else
9057             kid->op_sv = SvREFCNT_inc_simple_NN(gv);
9058 #endif
9059             kid->op_private = 0;
9060             kid->op_ppaddr = PL_ppaddr[OP_GV];
9061             /* FAKE globs in the symbol table cause weird bugs (#77810) */
9062             SvFAKE_off(gv);
9063         }
9064     }
9065     return o;
9066 }
9067
9068 OP *
9069 Perl_ck_ftst(pTHX_ OP *o)
9070 {
9071     dVAR;
9072     const I32 type = o->op_type;
9073
9074     PERL_ARGS_ASSERT_CK_FTST;
9075
9076     if (o->op_flags & OPf_REF) {
9077         NOOP;
9078     }
9079     else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
9080         SVOP * const kid = (SVOP*)cUNOPo->op_first;
9081         const OPCODE kidtype = kid->op_type;
9082
9083         if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
9084          && !kid->op_folded) {
9085             OP * const newop = newGVOP(type, OPf_REF,
9086                 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
9087 #ifdef PERL_MAD
9088             op_getmad(o,newop,'O');
9089 #else
9090             op_free(o);
9091 #endif
9092             return newop;
9093         }
9094         if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
9095             o->op_private |= OPpFT_ACCESS;
9096         if (PL_check[kidtype] == Perl_ck_ftst
9097                 && kidtype != OP_STAT && kidtype != OP_LSTAT) {
9098             o->op_private |= OPpFT_STACKED;
9099             kid->op_private |= OPpFT_STACKING;
9100             if (kidtype == OP_FTTTY && (
9101                    !(kid->op_private & OPpFT_STACKED)
9102                 || kid->op_private & OPpFT_AFTER_t
9103                ))
9104                 o->op_private |= OPpFT_AFTER_t;
9105         }
9106     }
9107     else {
9108 #ifdef PERL_MAD
9109         OP* const oldo = o;
9110 #else
9111         op_free(o);
9112 #endif
9113         if (type == OP_FTTTY)
9114             o = newGVOP(type, OPf_REF, PL_stdingv);
9115         else
9116             o = newUNOP(type, 0, newDEFSVOP());
9117         op_getmad(oldo,o,'O');
9118     }
9119     return o;
9120 }
9121
9122 OP *
9123 Perl_ck_fun(pTHX_ OP *o)
9124 {
9125     dVAR;
9126     const int type = o->op_type;
9127     I32 oa = PL_opargs[type] >> OASHIFT;
9128
9129     PERL_ARGS_ASSERT_CK_FUN;
9130
9131     if (o->op_flags & OPf_STACKED) {
9132         if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
9133             oa &= ~OA_OPTIONAL;
9134         else
9135             return no_fh_allowed(o);
9136     }
9137
9138     if (o->op_flags & OPf_KIDS) {
9139         OP **tokid = &cLISTOPo->op_first;
9140         OP *kid = cLISTOPo->op_first;
9141         OP *sibl;
9142         I32 numargs = 0;
9143         bool seen_optional = FALSE;
9144
9145         if (kid->op_type == OP_PUSHMARK ||
9146             (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
9147         {
9148             tokid = &kid->op_sibling;
9149             kid = kid->op_sibling;
9150         }
9151         if (kid && kid->op_type == OP_COREARGS) {
9152             bool optional = FALSE;
9153             while (oa) {
9154                 numargs++;
9155                 if (oa & OA_OPTIONAL) optional = TRUE;
9156                 oa = oa >> 4;
9157             }
9158             if (optional) o->op_private |= numargs;
9159             return o;
9160         }
9161
9162         while (oa) {
9163             if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
9164                 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV)
9165                     *tokid = kid = newDEFSVOP();
9166                 seen_optional = TRUE;
9167             }
9168             if (!kid) break;
9169
9170             numargs++;
9171             sibl = kid->op_sibling;
9172 #ifdef PERL_MAD
9173             if (!sibl && kid->op_type == OP_STUB) {
9174                 numargs--;
9175                 break;
9176             }
9177 #endif
9178             switch (oa & 7) {
9179             case OA_SCALAR:
9180                 /* list seen where single (scalar) arg expected? */
9181                 if (numargs == 1 && !(oa >> 4)
9182                     && kid->op_type == OP_LIST && type != OP_SCALAR)
9183                 {
9184                     return too_many_arguments_pv(o,PL_op_desc[type], 0);
9185                 }
9186                 if (type != OP_DELETE) scalar(kid);
9187                 break;
9188             case OA_LIST:
9189                 if (oa < 16) {
9190                     kid = 0;
9191                     continue;
9192                 }
9193                 else
9194                     list(kid);
9195                 break;
9196             case OA_AVREF:
9197                 if ((type == OP_PUSH || type == OP_UNSHIFT)
9198                     && !kid->op_sibling)
9199                     Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
9200                                    "Useless use of %s with no values",
9201                                    PL_op_desc[type]);
9202
9203                 if (kid->op_type == OP_CONST &&
9204                     (kid->op_private & OPpCONST_BARE))
9205                 {
9206                     OP * const newop = newAVREF(newGVOP(OP_GV, 0,
9207                         gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
9208                     Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9209                                    "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
9210                                    SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
9211 #ifdef PERL_MAD
9212                     op_getmad(kid,newop,'K');
9213 #else
9214                     op_free(kid);
9215 #endif
9216                     kid = newop;
9217                     kid->op_sibling = sibl;
9218                     *tokid = kid;
9219                 }
9220                 else if (kid->op_type == OP_CONST
9221                       && (  !SvROK(cSVOPx_sv(kid)) 
9222                          || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV  )
9223                         )
9224                     bad_type_pv(numargs, "array", PL_op_desc[type], 0, kid);
9225                 /* Defer checks to run-time if we have a scalar arg */
9226                 if (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV)
9227                     op_lvalue(kid, type);
9228                 else {
9229                     scalar(kid);
9230                     /* diag_listed_as: push on reference is experimental */
9231                     Perl_ck_warner_d(aTHX_
9232                                      packWARN(WARN_EXPERIMENTAL__AUTODEREF),
9233                                     "%s on reference is experimental",
9234                                      PL_op_desc[type]);
9235                 }
9236                 break;
9237             case OA_HVREF:
9238                 if (kid->op_type == OP_CONST &&
9239                     (kid->op_private & OPpCONST_BARE))
9240                 {
9241                     OP * const newop = newHVREF(newGVOP(OP_GV, 0,
9242                         gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
9243                     Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9244                                    "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
9245                                    SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
9246 #ifdef PERL_MAD
9247                     op_getmad(kid,newop,'K');
9248 #else
9249                     op_free(kid);
9250 #endif
9251                     kid = newop;
9252                     kid->op_sibling = sibl;
9253                     *tokid = kid;
9254                 }
9255                 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
9256                     bad_type_pv(numargs, "hash", PL_op_desc[type], 0, kid);
9257                 op_lvalue(kid, type);
9258                 break;
9259             case OA_CVREF:
9260                 {
9261                     OP * const newop = newUNOP(OP_NULL, 0, kid);
9262                     kid->op_sibling = 0;
9263                     newop->op_next = newop;
9264                     kid = newop;
9265                     kid->op_sibling = sibl;
9266                     *tokid = kid;
9267                 }
9268                 break;
9269             case OA_FILEREF:
9270                 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
9271                     if (kid->op_type == OP_CONST &&
9272                         (kid->op_private & OPpCONST_BARE))
9273                     {
9274                         OP * const newop = newGVOP(OP_GV, 0,
9275                             gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
9276                         if (!(o->op_private & 1) && /* if not unop */
9277                             kid == cLISTOPo->op_last)
9278                             cLISTOPo->op_last = newop;
9279 #ifdef PERL_MAD
9280                         op_getmad(kid,newop,'K');
9281 #else
9282                         op_free(kid);
9283 #endif
9284                         kid = newop;
9285                     }
9286                     else if (kid->op_type == OP_READLINE) {
9287                         /* neophyte patrol: open(<FH>), close(<FH>) etc. */
9288                         bad_type_pv(numargs, "HANDLE", OP_DESC(o), 0, kid);
9289                     }
9290                     else {
9291                         I32 flags = OPf_SPECIAL;
9292                         I32 priv = 0;
9293                         PADOFFSET targ = 0;
9294
9295                         /* is this op a FH constructor? */
9296                         if (is_handle_constructor(o,numargs)) {
9297                             const char *name = NULL;
9298                             STRLEN len = 0;
9299                             U32 name_utf8 = 0;
9300                             bool want_dollar = TRUE;
9301
9302                             flags = 0;
9303                             /* Set a flag to tell rv2gv to vivify
9304                              * need to "prove" flag does not mean something
9305                              * else already - NI-S 1999/05/07
9306                              */
9307                             priv = OPpDEREF;
9308                             if (kid->op_type == OP_PADSV) {
9309                                 SV *const namesv
9310                                     = PAD_COMPNAME_SV(kid->op_targ);
9311                                 name = SvPV_const(namesv, len);
9312                                 name_utf8 = SvUTF8(namesv);
9313                             }
9314                             else if (kid->op_type == OP_RV2SV
9315                                      && kUNOP->op_first->op_type == OP_GV)
9316                             {
9317                                 GV * const gv = cGVOPx_gv(kUNOP->op_first);
9318                                 name = GvNAME(gv);
9319                                 len = GvNAMELEN(gv);
9320                                 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
9321                             }
9322                             else if (kid->op_type == OP_AELEM
9323                                      || kid->op_type == OP_HELEM)
9324                             {
9325                                  OP *firstop;
9326                                  OP *op = ((BINOP*)kid)->op_first;
9327                                  name = NULL;
9328                                  if (op) {
9329                                       SV *tmpstr = NULL;
9330                                       const char * const a =
9331                                            kid->op_type == OP_AELEM ?
9332                                            "[]" : "{}";
9333                                       if (((op->op_type == OP_RV2AV) ||
9334                                            (op->op_type == OP_RV2HV)) &&
9335                                           (firstop = ((UNOP*)op)->op_first) &&
9336                                           (firstop->op_type == OP_GV)) {
9337                                            /* packagevar $a[] or $h{} */
9338                                            GV * const gv = cGVOPx_gv(firstop);
9339                                            if (gv)
9340                                                 tmpstr =
9341                                                      Perl_newSVpvf(aTHX_
9342                                                                    "%s%c...%c",
9343                                                                    GvNAME(gv),
9344                                                                    a[0], a[1]);
9345                                       }
9346                                       else if (op->op_type == OP_PADAV
9347                                                || op->op_type == OP_PADHV) {
9348                                            /* lexicalvar $a[] or $h{} */
9349                                            const char * const padname =
9350                                                 PAD_COMPNAME_PV(op->op_targ);
9351                                            if (padname)
9352                                                 tmpstr =
9353                                                      Perl_newSVpvf(aTHX_
9354                                                                    "%s%c...%c",
9355                                                                    padname + 1,
9356                                                                    a[0], a[1]);
9357                                       }
9358                                       if (tmpstr) {
9359                                            name = SvPV_const(tmpstr, len);
9360                                            name_utf8 = SvUTF8(tmpstr);
9361                                            sv_2mortal(tmpstr);
9362                                       }
9363                                  }
9364                                  if (!name) {
9365                                       name = "__ANONIO__";
9366                                       len = 10;
9367                                       want_dollar = FALSE;
9368                                  }
9369                                  op_lvalue(kid, type);
9370                             }
9371                             if (name) {
9372                                 SV *namesv;
9373                                 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
9374                                 namesv = PAD_SVl(targ);
9375                                 if (want_dollar && *name != '$')
9376                                     sv_setpvs(namesv, "$");
9377                                 else
9378                                     sv_setpvs(namesv, "");
9379                                 sv_catpvn(namesv, name, len);
9380                                 if ( name_utf8 ) SvUTF8_on(namesv);
9381                             }
9382                         }
9383                         kid->op_sibling = 0;
9384                         kid = newUNOP(OP_RV2GV, flags, scalar(kid));
9385                         kid->op_targ = targ;
9386                         kid->op_private |= priv;
9387                     }
9388                     kid->op_sibling = sibl;
9389                     *tokid = kid;
9390                 }
9391                 scalar(kid);
9392                 break;
9393             case OA_SCALARREF:
9394                 if ((type == OP_UNDEF || type == OP_POS)
9395                     && numargs == 1 && !(oa >> 4)
9396                     && kid->op_type == OP_LIST)
9397                     return too_many_arguments_pv(o,PL_op_desc[type], 0);
9398                 op_lvalue(scalar(kid), type);
9399                 break;
9400             }
9401             oa >>= 4;
9402             tokid = &kid->op_sibling;
9403             kid = kid->op_sibling;
9404         }
9405 #ifdef PERL_MAD
9406         if (kid && kid->op_type != OP_STUB)
9407             return too_many_arguments_pv(o,OP_DESC(o), 0);
9408         o->op_private |= numargs;
9409 #else
9410         /* FIXME - should the numargs move as for the PERL_MAD case?  */
9411         o->op_private |= numargs;
9412         if (kid)
9413             return too_many_arguments_pv(o,OP_DESC(o), 0);
9414 #endif
9415         listkids(o);
9416     }
9417     else if (PL_opargs[type] & OA_DEFGV) {
9418 #ifdef PERL_MAD
9419         OP *newop = newUNOP(type, 0, newDEFSVOP());
9420         op_getmad(o,newop,'O');
9421         return newop;
9422 #else
9423         /* Ordering of these two is important to keep f_map.t passing.  */
9424         op_free(o);
9425         return newUNOP(type, 0, newDEFSVOP());
9426 #endif
9427     }
9428
9429     if (oa) {
9430         while (oa & OA_OPTIONAL)
9431             oa >>= 4;
9432         if (oa && oa != OA_LIST)
9433             return too_few_arguments_pv(o,OP_DESC(o), 0);
9434     }
9435     return o;
9436 }
9437
9438 OP *
9439 Perl_ck_glob(pTHX_ OP *o)
9440 {
9441     dVAR;
9442     GV *gv;
9443
9444     PERL_ARGS_ASSERT_CK_GLOB;
9445
9446     o = ck_fun(o);
9447     if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
9448         op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
9449
9450     if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
9451     {
9452         /* convert
9453          *     glob
9454          *       \ null - const(wildcard)
9455          * into
9456          *     null
9457          *       \ enter
9458          *            \ list
9459          *                 \ mark - glob - rv2cv
9460          *                             |        \ gv(CORE::GLOBAL::glob)
9461          *                             |
9462          *                              \ null - const(wildcard)
9463          */
9464         o->op_flags |= OPf_SPECIAL;
9465         o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
9466         o = S_new_entersubop(aTHX_ gv, o);
9467         o = newUNOP(OP_NULL, 0, o);
9468         o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
9469         return o;
9470     }
9471     else o->op_flags &= ~OPf_SPECIAL;
9472 #if !defined(PERL_EXTERNAL_GLOB)
9473     if (!PL_globhook) {
9474         ENTER;
9475         Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
9476                                newSVpvs("File::Glob"), NULL, NULL, NULL);
9477         LEAVE;
9478     }
9479 #endif /* !PERL_EXTERNAL_GLOB */
9480     gv = (GV *)newSV(0);
9481     gv_init(gv, 0, "", 0, 0);
9482     gv_IOadd(gv);
9483     op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
9484     SvREFCNT_dec_NN(gv); /* newGVOP increased it */
9485     scalarkids(o);
9486     return o;
9487 }
9488
9489 OP *
9490 Perl_ck_grep(pTHX_ OP *o)
9491 {
9492     dVAR;
9493     LOGOP *gwop;
9494     OP *kid;
9495     const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
9496     PADOFFSET offset;
9497
9498     PERL_ARGS_ASSERT_CK_GREP;
9499
9500     o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
9501     /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
9502
9503     if (o->op_flags & OPf_STACKED) {
9504         kid = cUNOPx(cLISTOPo->op_first->op_sibling)->op_first;
9505         if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
9506             return no_fh_allowed(o);
9507         o->op_flags &= ~OPf_STACKED;
9508     }
9509     kid = cLISTOPo->op_first->op_sibling;
9510     if (type == OP_MAPWHILE)
9511         list(kid);
9512     else
9513         scalar(kid);
9514     o = ck_fun(o);
9515     if (PL_parser && PL_parser->error_count)
9516         return o;
9517     kid = cLISTOPo->op_first->op_sibling;
9518     if (kid->op_type != OP_NULL)
9519         Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
9520     kid = kUNOP->op_first;
9521
9522     NewOp(1101, gwop, 1, LOGOP);
9523     gwop->op_type = type;
9524     gwop->op_ppaddr = PL_ppaddr[type];
9525     gwop->op_first = o;
9526     gwop->op_flags |= OPf_KIDS;
9527     gwop->op_other = LINKLIST(kid);
9528     kid->op_next = (OP*)gwop;
9529     offset = pad_findmy_pvs("$_", 0);
9530     if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
9531         o->op_private = gwop->op_private = 0;
9532         gwop->op_targ = pad_alloc(type, SVs_PADTMP);
9533     }
9534     else {
9535         o->op_private = gwop->op_private = OPpGREP_LEX;
9536         gwop->op_targ = o->op_targ = offset;
9537     }
9538
9539     kid = cLISTOPo->op_first->op_sibling;
9540     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
9541         op_lvalue(kid, OP_GREPSTART);
9542
9543     return (OP*)gwop;
9544 }
9545
9546 OP *
9547 Perl_ck_index(pTHX_ OP *o)
9548 {
9549     PERL_ARGS_ASSERT_CK_INDEX;
9550
9551     if (o->op_flags & OPf_KIDS) {
9552         OP *kid = cLISTOPo->op_first->op_sibling;       /* get past pushmark */
9553         if (kid)
9554             kid = kid->op_sibling;                      /* get past "big" */
9555         if (kid && kid->op_type == OP_CONST) {
9556             const bool save_taint = TAINT_get;
9557             SV *sv = kSVOP->op_sv;
9558             if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) {
9559                 sv = newSV(0);
9560                 sv_copypv(sv, kSVOP->op_sv);
9561                 SvREFCNT_dec_NN(kSVOP->op_sv);
9562                 kSVOP->op_sv = sv;
9563             }
9564             if (SvOK(sv)) fbm_compile(sv, 0);
9565             TAINT_set(save_taint);
9566 #ifdef NO_TAINT_SUPPORT
9567             PERL_UNUSED_VAR(save_taint);
9568 #endif
9569         }
9570     }
9571     return ck_fun(o);
9572 }
9573
9574 OP *
9575 Perl_ck_lfun(pTHX_ OP *o)
9576 {
9577     const OPCODE type = o->op_type;
9578
9579     PERL_ARGS_ASSERT_CK_LFUN;
9580
9581     return modkids(ck_fun(o), type);
9582 }
9583
9584 OP *
9585 Perl_ck_defined(pTHX_ OP *o)            /* 19990527 MJD */
9586 {
9587     PERL_ARGS_ASSERT_CK_DEFINED;
9588
9589     if ((o->op_flags & OPf_KIDS)) {
9590         switch (cUNOPo->op_first->op_type) {
9591         case OP_RV2AV:
9592         case OP_PADAV:
9593         case OP_AASSIGN:                /* Is this a good idea? */
9594             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9595                            "defined(@array) is deprecated");
9596             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9597                            "\t(Maybe you should just omit the defined()?)\n");
9598         break;
9599         case OP_RV2HV:
9600         case OP_PADHV:
9601             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9602                            "defined(%%hash) is deprecated");
9603             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9604                            "\t(Maybe you should just omit the defined()?)\n");
9605             break;
9606         default:
9607             /* no warning */
9608             break;
9609         }
9610     }
9611     return ck_rfun(o);
9612 }
9613
9614 OP *
9615 Perl_ck_readline(pTHX_ OP *o)
9616 {
9617     PERL_ARGS_ASSERT_CK_READLINE;
9618
9619     if (o->op_flags & OPf_KIDS) {
9620          OP *kid = cLISTOPo->op_first;
9621          if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
9622     }
9623     else {
9624         OP * const newop
9625             = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
9626 #ifdef PERL_MAD
9627         op_getmad(o,newop,'O');
9628 #else
9629         op_free(o);
9630 #endif
9631         return newop;
9632     }
9633     return o;
9634 }
9635
9636 OP *
9637 Perl_ck_rfun(pTHX_ OP *o)
9638 {
9639     const OPCODE type = o->op_type;
9640
9641     PERL_ARGS_ASSERT_CK_RFUN;
9642
9643     return refkids(ck_fun(o), type);
9644 }
9645
9646 OP *
9647 Perl_ck_listiob(pTHX_ OP *o)
9648 {
9649     OP *kid;
9650
9651     PERL_ARGS_ASSERT_CK_LISTIOB;
9652
9653     kid = cLISTOPo->op_first;
9654     if (!kid) {
9655         o = force_list(o);
9656         kid = cLISTOPo->op_first;
9657     }
9658     if (kid->op_type == OP_PUSHMARK)
9659         kid = kid->op_sibling;
9660     if (kid && o->op_flags & OPf_STACKED)
9661         kid = kid->op_sibling;
9662     else if (kid && !kid->op_sibling) {         /* print HANDLE; */
9663         if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
9664          && !kid->op_folded) {
9665             o->op_flags |= OPf_STACKED; /* make it a filehandle */
9666             kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
9667             cLISTOPo->op_first->op_sibling = kid;
9668             cLISTOPo->op_last = kid;
9669             kid = kid->op_sibling;
9670         }
9671     }
9672
9673     if (!kid)
9674         op_append_elem(o->op_type, o, newDEFSVOP());
9675
9676     if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
9677     return listkids(o);
9678 }
9679
9680 OP *
9681 Perl_ck_smartmatch(pTHX_ OP *o)
9682 {
9683     dVAR;
9684     PERL_ARGS_ASSERT_CK_SMARTMATCH;
9685     if (0 == (o->op_flags & OPf_SPECIAL)) {
9686         OP *first  = cBINOPo->op_first;
9687         OP *second = first->op_sibling;
9688         
9689         /* Implicitly take a reference to an array or hash */
9690         first->op_sibling = NULL;
9691         first = cBINOPo->op_first = ref_array_or_hash(first);
9692         second = first->op_sibling = ref_array_or_hash(second);
9693         
9694         /* Implicitly take a reference to a regular expression */
9695         if (first->op_type == OP_MATCH) {
9696             first->op_type = OP_QR;
9697             first->op_ppaddr = PL_ppaddr[OP_QR];
9698         }
9699         if (second->op_type == OP_MATCH) {
9700             second->op_type = OP_QR;
9701             second->op_ppaddr = PL_ppaddr[OP_QR];
9702         }
9703     }
9704     
9705     return o;
9706 }
9707
9708
9709 OP *
9710 Perl_ck_sassign(pTHX_ OP *o)
9711 {
9712     dVAR;
9713     OP * const kid = cLISTOPo->op_first;
9714
9715     PERL_ARGS_ASSERT_CK_SASSIGN;
9716
9717     /* has a disposable target? */
9718     if ((PL_opargs[kid->op_type] & OA_TARGLEX)
9719         && !(kid->op_flags & OPf_STACKED)
9720         /* Cannot steal the second time! */
9721         && !(kid->op_private & OPpTARGET_MY)
9722         /* Keep the full thing for madskills */
9723         && !PL_madskills
9724         )
9725     {
9726         OP * const kkid = kid->op_sibling;
9727
9728         /* Can just relocate the target. */
9729         if (kkid && kkid->op_type == OP_PADSV
9730             && !(kkid->op_private & OPpLVAL_INTRO))
9731         {
9732             kid->op_targ = kkid->op_targ;
9733             kkid->op_targ = 0;
9734             /* Now we do not need PADSV and SASSIGN. */
9735             kid->op_sibling = o->op_sibling;    /* NULL */
9736             cLISTOPo->op_first = NULL;
9737             op_free(o);
9738             op_free(kkid);
9739             kid->op_private |= OPpTARGET_MY;    /* Used for context settings */
9740             return kid;
9741         }
9742     }
9743     if (kid->op_sibling) {
9744         OP *kkid = kid->op_sibling;
9745         /* For state variable assignment, kkid is a list op whose op_last
9746            is a padsv. */
9747         if ((kkid->op_type == OP_PADSV ||
9748              (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
9749               (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
9750              )
9751             )
9752                 && (kkid->op_private & OPpLVAL_INTRO)
9753                 && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
9754             const PADOFFSET target = kkid->op_targ;
9755             OP *const other = newOP(OP_PADSV,
9756                                     kkid->op_flags
9757                                     | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
9758             OP *const first = newOP(OP_NULL, 0);
9759             OP *const nullop = newCONDOP(0, first, o, other);
9760             OP *const condop = first->op_next;
9761             /* hijacking PADSTALE for uninitialized state variables */
9762             SvPADSTALE_on(PAD_SVl(target));
9763
9764             condop->op_type = OP_ONCE;
9765             condop->op_ppaddr = PL_ppaddr[OP_ONCE];
9766             condop->op_targ = target;
9767             other->op_targ = target;
9768
9769             /* Because we change the type of the op here, we will skip the
9770                assignment binop->op_last = binop->op_first->op_sibling; at the
9771                end of Perl_newBINOP(). So need to do it here. */
9772             cBINOPo->op_last = cBINOPo->op_first->op_sibling;
9773
9774             return nullop;
9775         }
9776     }
9777     return o;
9778 }
9779
9780 OP *
9781 Perl_ck_match(pTHX_ OP *o)
9782 {
9783     dVAR;
9784
9785     PERL_ARGS_ASSERT_CK_MATCH;
9786
9787     if (o->op_type != OP_QR && PL_compcv) {
9788         const PADOFFSET offset = pad_findmy_pvs("$_", 0);
9789         if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) {
9790             o->op_targ = offset;
9791             o->op_private |= OPpTARGET_MY;
9792         }
9793     }
9794     if (o->op_type == OP_MATCH || o->op_type == OP_QR)
9795         o->op_private |= OPpRUNTIME;
9796     return o;
9797 }
9798
9799 OP *
9800 Perl_ck_method(pTHX_ OP *o)
9801 {
9802     OP * const kid = cUNOPo->op_first;
9803
9804     PERL_ARGS_ASSERT_CK_METHOD;
9805
9806     if (kid->op_type == OP_CONST) {
9807         SV* sv = kSVOP->op_sv;
9808         const char * const method = SvPVX_const(sv);
9809         if (!(strchr(method, ':') || strchr(method, '\''))) {
9810             OP *cmop;
9811             if (!SvIsCOW_shared_hash(sv)) {
9812                 sv = newSVpvn_share(method, SvUTF8(sv) ? -(I32)SvCUR(sv) : (I32)SvCUR(sv), 0);
9813             }
9814             else {
9815                 kSVOP->op_sv = NULL;
9816             }
9817             cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
9818 #ifdef PERL_MAD
9819             op_getmad(o,cmop,'O');
9820 #else
9821             op_free(o);
9822 #endif
9823             return cmop;
9824         }
9825     }
9826     return o;
9827 }
9828
9829 OP *
9830 Perl_ck_null(pTHX_ OP *o)
9831 {
9832     PERL_ARGS_ASSERT_CK_NULL;
9833     PERL_UNUSED_CONTEXT;
9834     return o;
9835 }
9836
9837 OP *
9838 Perl_ck_open(pTHX_ OP *o)
9839 {
9840     dVAR;
9841
9842     PERL_ARGS_ASSERT_CK_OPEN;
9843
9844     S_io_hints(aTHX_ o);
9845     {
9846          /* In case of three-arg dup open remove strictness
9847           * from the last arg if it is a bareword. */
9848          OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
9849          OP * const last  = cLISTOPx(o)->op_last;  /* The bareword. */
9850          OP *oa;
9851          const char *mode;
9852
9853          if ((last->op_type == OP_CONST) &&             /* The bareword. */
9854              (last->op_private & OPpCONST_BARE) &&
9855              (last->op_private & OPpCONST_STRICT) &&
9856              (oa = first->op_sibling) &&                /* The fh. */
9857              (oa = oa->op_sibling) &&                   /* The mode. */
9858              (oa->op_type == OP_CONST) &&
9859              SvPOK(((SVOP*)oa)->op_sv) &&
9860              (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
9861              mode[0] == '>' && mode[1] == '&' &&        /* A dup open. */
9862              (last == oa->op_sibling))                  /* The bareword. */
9863               last->op_private &= ~OPpCONST_STRICT;
9864     }
9865     return ck_fun(o);
9866 }
9867
9868 OP *
9869 Perl_ck_repeat(pTHX_ OP *o)
9870 {
9871     PERL_ARGS_ASSERT_CK_REPEAT;
9872
9873     if (cBINOPo->op_first->op_flags & OPf_PARENS) {
9874         o->op_private |= OPpREPEAT_DOLIST;
9875         cBINOPo->op_first = force_list(cBINOPo->op_first);
9876     }
9877     else
9878         scalar(o);
9879     return o;
9880 }
9881
9882 OP *
9883 Perl_ck_require(pTHX_ OP *o)
9884 {
9885     dVAR;
9886     GV* gv;
9887
9888     PERL_ARGS_ASSERT_CK_REQUIRE;
9889
9890     if (o->op_flags & OPf_KIDS) {       /* Shall we supply missing .pm? */
9891         SVOP * const kid = (SVOP*)cUNOPo->op_first;
9892
9893         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
9894             SV * const sv = kid->op_sv;
9895             U32 was_readonly = SvREADONLY(sv);
9896             char *s;
9897             STRLEN len;
9898             const char *end;
9899
9900             if (was_readonly) {
9901                     SvREADONLY_off(sv);
9902             }   
9903             if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
9904
9905             s = SvPVX(sv);
9906             len = SvCUR(sv);
9907             end = s + len;
9908             for (; s < end; s++) {
9909                 if (*s == ':' && s[1] == ':') {
9910                     *s = '/';
9911                     Move(s+2, s+1, end - s - 1, char);
9912                     --end;
9913                 }
9914             }
9915             SvEND_set(sv, end);
9916             sv_catpvs(sv, ".pm");
9917             SvFLAGS(sv) |= was_readonly;
9918         }
9919     }
9920
9921     if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
9922         /* handle override, if any */
9923      && (gv = gv_override("require", 7))) {
9924         OP *kid, *newop;
9925         if (o->op_flags & OPf_KIDS) {
9926             kid = cUNOPo->op_first;
9927             cUNOPo->op_first = NULL;
9928         }
9929         else {
9930             kid = newDEFSVOP();
9931         }
9932 #ifndef PERL_MAD
9933         op_free(o);
9934 #endif
9935         newop = S_new_entersubop(aTHX_ gv, kid);
9936         op_getmad(o,newop,'O');
9937         return newop;
9938     }
9939
9940     return scalar(ck_fun(o));
9941 }
9942
9943 OP *
9944 Perl_ck_return(pTHX_ OP *o)
9945 {
9946     dVAR;
9947     OP *kid;
9948
9949     PERL_ARGS_ASSERT_CK_RETURN;
9950
9951     kid = cLISTOPo->op_first->op_sibling;
9952     if (CvLVALUE(PL_compcv)) {
9953         for (; kid; kid = kid->op_sibling)
9954             op_lvalue(kid, OP_LEAVESUBLV);
9955     }
9956
9957     return o;
9958 }
9959
9960 OP *
9961 Perl_ck_select(pTHX_ OP *o)
9962 {
9963     dVAR;
9964     OP* kid;
9965
9966     PERL_ARGS_ASSERT_CK_SELECT;
9967
9968     if (o->op_flags & OPf_KIDS) {
9969         kid = cLISTOPo->op_first->op_sibling;   /* get past pushmark */
9970         if (kid && kid->op_sibling) {
9971             o->op_type = OP_SSELECT;
9972             o->op_ppaddr = PL_ppaddr[OP_SSELECT];
9973             o = ck_fun(o);
9974             return fold_constants(op_integerize(op_std_init(o)));
9975         }
9976     }
9977     o = ck_fun(o);
9978     kid = cLISTOPo->op_first->op_sibling;    /* get past pushmark */
9979     if (kid && kid->op_type == OP_RV2GV)
9980         kid->op_private &= ~HINT_STRICT_REFS;
9981     return o;
9982 }
9983
9984 OP *
9985 Perl_ck_shift(pTHX_ OP *o)
9986 {
9987     dVAR;
9988     const I32 type = o->op_type;
9989
9990     PERL_ARGS_ASSERT_CK_SHIFT;
9991
9992     if (!(o->op_flags & OPf_KIDS)) {
9993         OP *argop;
9994
9995         if (!CvUNIQUE(PL_compcv)) {
9996             o->op_flags |= OPf_SPECIAL;
9997             return o;
9998         }
9999
10000         argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
10001 #ifdef PERL_MAD
10002         {
10003             OP * const oldo = o;
10004             o = newUNOP(type, 0, scalar(argop));
10005             op_getmad(oldo,o,'O');
10006             return o;
10007         }
10008 #else
10009         op_free(o);
10010         return newUNOP(type, 0, scalar(argop));
10011 #endif
10012     }
10013     return scalar(ck_fun(o));
10014 }
10015
10016 OP *
10017 Perl_ck_sort(pTHX_ OP *o)
10018 {
10019     dVAR;
10020     OP *firstkid;
10021     OP *kid;
10022     HV * const hinthv =
10023         PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
10024     U8 stacked;
10025
10026     PERL_ARGS_ASSERT_CK_SORT;
10027
10028     if (hinthv) {
10029             SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
10030             if (svp) {
10031                 const I32 sorthints = (I32)SvIV(*svp);
10032                 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
10033                     o->op_private |= OPpSORT_QSORT;
10034                 if ((sorthints & HINT_SORT_STABLE) != 0)
10035                     o->op_private |= OPpSORT_STABLE;
10036             }
10037     }
10038
10039     if (o->op_flags & OPf_STACKED)
10040         simplify_sort(o);
10041     firstkid = cLISTOPo->op_first->op_sibling;          /* get past pushmark */
10042
10043     if ((stacked = o->op_flags & OPf_STACKED)) {        /* may have been cleared */
10044         OP *kid = cUNOPx(firstkid)->op_first;           /* get past null */
10045
10046         /* if the first arg is a code block, process it and mark sort as
10047          * OPf_SPECIAL */
10048         if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
10049             LINKLIST(kid);
10050             if (kid->op_type == OP_LEAVE)
10051                     op_null(kid);                       /* wipe out leave */
10052             /* Prevent execution from escaping out of the sort block. */
10053             kid->op_next = 0;
10054
10055             /* provide scalar context for comparison function/block */
10056             kid = scalar(firstkid);
10057             kid->op_next = kid;
10058             o->op_flags |= OPf_SPECIAL;
10059         }
10060
10061         firstkid = firstkid->op_sibling;
10062     }
10063
10064     for (kid = firstkid; kid; kid = kid->op_sibling) {
10065         /* provide list context for arguments */
10066         list(kid);
10067         if (stacked)
10068             op_lvalue(kid, OP_GREPSTART);
10069     }
10070
10071     return o;
10072 }
10073
10074 /* for sort { X } ..., where X is one of
10075  *   $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a
10076  * elide the second child of the sort (the one containing X),
10077  * and set these flags as appropriate
10078         OPpSORT_NUMERIC;
10079         OPpSORT_INTEGER;
10080         OPpSORT_DESCEND;
10081  * Also, check and warn on lexical $a, $b.
10082  */
10083
10084 STATIC void
10085 S_simplify_sort(pTHX_ OP *o)
10086 {
10087     dVAR;
10088     OP *kid = cLISTOPo->op_first->op_sibling;   /* get past pushmark */
10089     OP *k;
10090     int descending;
10091     GV *gv;
10092     const char *gvname;
10093     bool have_scopeop;
10094
10095     PERL_ARGS_ASSERT_SIMPLIFY_SORT;
10096
10097     kid = kUNOP->op_first;                              /* get past null */
10098     if (!(have_scopeop = kid->op_type == OP_SCOPE)
10099      && kid->op_type != OP_LEAVE)
10100         return;
10101     kid = kLISTOP->op_last;                             /* get past scope */
10102     switch(kid->op_type) {
10103         case OP_NCMP:
10104         case OP_I_NCMP:
10105         case OP_SCMP:
10106             if (!have_scopeop) goto padkids;
10107             break;
10108         default:
10109             return;
10110     }
10111     k = kid;                                            /* remember this node*/
10112     if (kBINOP->op_first->op_type != OP_RV2SV
10113      || kBINOP->op_last ->op_type != OP_RV2SV)
10114     {
10115         /*
10116            Warn about my($a) or my($b) in a sort block, *if* $a or $b is
10117            then used in a comparison.  This catches most, but not
10118            all cases.  For instance, it catches
10119                sort { my($a); $a <=> $b }
10120            but not
10121                sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
10122            (although why you'd do that is anyone's guess).
10123         */
10124
10125        padkids:
10126         if (!ckWARN(WARN_SYNTAX)) return;
10127         kid = kBINOP->op_first;
10128         do {
10129             if (kid->op_type == OP_PADSV) {
10130                 SV * const name = AvARRAY(PL_comppad_name)[kid->op_targ];
10131                 if (SvCUR(name) == 2 && *SvPVX(name) == '$'
10132                  && (SvPVX(name)[1] == 'a' || SvPVX(name)[1] == 'b'))
10133                     /* diag_listed_as: "my %s" used in sort comparison */
10134                     Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10135                                      "\"%s %s\" used in sort comparison",
10136                                       SvPAD_STATE(name) ? "state" : "my",
10137                                       SvPVX(name));
10138             }
10139         } while ((kid = kid->op_sibling));
10140         return;
10141     }
10142     kid = kBINOP->op_first;                             /* get past cmp */
10143     if (kUNOP->op_first->op_type != OP_GV)
10144         return;
10145     kid = kUNOP->op_first;                              /* get past rv2sv */
10146     gv = kGVOP_gv;
10147     if (GvSTASH(gv) != PL_curstash)
10148         return;
10149     gvname = GvNAME(gv);
10150     if (*gvname == 'a' && gvname[1] == '\0')
10151         descending = 0;
10152     else if (*gvname == 'b' && gvname[1] == '\0')
10153         descending = 1;
10154     else
10155         return;
10156
10157     kid = k;                                            /* back to cmp */
10158     /* already checked above that it is rv2sv */
10159     kid = kBINOP->op_last;                              /* down to 2nd arg */
10160     if (kUNOP->op_first->op_type != OP_GV)
10161         return;
10162     kid = kUNOP->op_first;                              /* get past rv2sv */
10163     gv = kGVOP_gv;
10164     if (GvSTASH(gv) != PL_curstash)
10165         return;
10166     gvname = GvNAME(gv);
10167     if ( descending
10168          ? !(*gvname == 'a' && gvname[1] == '\0')
10169          : !(*gvname == 'b' && gvname[1] == '\0'))
10170         return;
10171     o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
10172     if (descending)
10173         o->op_private |= OPpSORT_DESCEND;
10174     if (k->op_type == OP_NCMP)
10175         o->op_private |= OPpSORT_NUMERIC;
10176     if (k->op_type == OP_I_NCMP)
10177         o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
10178     kid = cLISTOPo->op_first->op_sibling;
10179     cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
10180 #ifdef PERL_MAD
10181     op_getmad(kid,o,'S');                             /* then delete it */
10182 #else
10183     op_free(kid);                                     /* then delete it */
10184 #endif
10185 }
10186
10187 OP *
10188 Perl_ck_split(pTHX_ OP *o)
10189 {
10190     dVAR;
10191     OP *kid;
10192
10193     PERL_ARGS_ASSERT_CK_SPLIT;
10194
10195     if (o->op_flags & OPf_STACKED)
10196         return no_fh_allowed(o);
10197
10198     kid = cLISTOPo->op_first;
10199     if (kid->op_type != OP_NULL)
10200         Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type);
10201     kid = kid->op_sibling;
10202     op_free(cLISTOPo->op_first);
10203     if (kid)
10204         cLISTOPo->op_first = kid;
10205     else {
10206         cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
10207         cLISTOPo->op_last = kid; /* There was only one element previously */
10208     }
10209
10210     if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
10211         OP * const sibl = kid->op_sibling;
10212         kid->op_sibling = 0;
10213         kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0, 0); /* OPf_SPECIAL is used to trigger split " " behavior */
10214         if (cLISTOPo->op_first == cLISTOPo->op_last)
10215             cLISTOPo->op_last = kid;
10216         cLISTOPo->op_first = kid;
10217         kid->op_sibling = sibl;
10218     }
10219
10220     kid->op_type = OP_PUSHRE;
10221     kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
10222     scalar(kid);
10223     if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
10224       Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
10225                      "Use of /g modifier is meaningless in split");
10226     }
10227
10228     if (!kid->op_sibling)
10229         op_append_elem(OP_SPLIT, o, newDEFSVOP());
10230
10231     kid = kid->op_sibling;
10232     scalar(kid);
10233
10234     if (!kid->op_sibling)
10235     {
10236         op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
10237         o->op_private |= OPpSPLIT_IMPLIM;
10238     }
10239     assert(kid->op_sibling);
10240
10241     kid = kid->op_sibling;
10242     scalar(kid);
10243
10244     if (kid->op_sibling)
10245         return too_many_arguments_pv(o,OP_DESC(o), 0);
10246
10247     return o;
10248 }
10249
10250 OP *
10251 Perl_ck_join(pTHX_ OP *o)
10252 {
10253     const OP * const kid = cLISTOPo->op_first->op_sibling;
10254
10255     PERL_ARGS_ASSERT_CK_JOIN;
10256
10257     if (kid && kid->op_type == OP_MATCH) {
10258         if (ckWARN(WARN_SYNTAX)) {
10259             const REGEXP *re = PM_GETRE(kPMOP);
10260             const SV *msg = re
10261                     ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
10262                                             SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
10263                     : newSVpvs_flags( "STRING", SVs_TEMP );
10264             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10265                         "/%"SVf"/ should probably be written as \"%"SVf"\"",
10266                         SVfARG(msg), SVfARG(msg));
10267         }
10268     }
10269     return ck_fun(o);
10270 }
10271
10272 /*
10273 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
10274
10275 Examines an op, which is expected to identify a subroutine at runtime,
10276 and attempts to determine at compile time which subroutine it identifies.
10277 This is normally used during Perl compilation to determine whether
10278 a prototype can be applied to a function call.  I<cvop> is the op
10279 being considered, normally an C<rv2cv> op.  A pointer to the identified
10280 subroutine is returned, if it could be determined statically, and a null
10281 pointer is returned if it was not possible to determine statically.
10282
10283 Currently, the subroutine can be identified statically if the RV that the
10284 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
10285 A C<gv> op is suitable if the GV's CV slot is populated.  A C<const> op is
10286 suitable if the constant value must be an RV pointing to a CV.  Details of
10287 this process may change in future versions of Perl.  If the C<rv2cv> op
10288 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
10289 the subroutine statically: this flag is used to suppress compile-time
10290 magic on a subroutine call, forcing it to use default runtime behaviour.
10291
10292 If I<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
10293 of a GV reference is modified.  If a GV was examined and its CV slot was
10294 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
10295 If the op is not optimised away, and the CV slot is later populated with
10296 a subroutine having a prototype, that flag eventually triggers the warning
10297 "called too early to check prototype".
10298
10299 If I<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
10300 of returning a pointer to the subroutine it returns a pointer to the
10301 GV giving the most appropriate name for the subroutine in this context.
10302 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
10303 (C<CvANON>) subroutine that is referenced through a GV it will be the
10304 referencing GV.  The resulting C<GV*> is cast to C<CV*> to be returned.
10305 A null pointer is returned as usual if there is no statically-determinable
10306 subroutine.
10307
10308 =cut
10309 */
10310
10311 /* shared by toke.c:yylex */
10312 CV *
10313 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
10314 {
10315     PADNAME *name = PAD_COMPNAME(off);
10316     CV *compcv = PL_compcv;
10317     while (PadnameOUTER(name)) {
10318         assert(PARENT_PAD_INDEX(name));
10319         compcv = CvOUTSIDE(PL_compcv);
10320         name = PadlistNAMESARRAY(CvPADLIST(compcv))
10321                 [off = PARENT_PAD_INDEX(name)];
10322     }
10323     assert(!PadnameIsOUR(name));
10324     if (!PadnameIsSTATE(name) && SvMAGICAL(name)) {
10325         MAGIC * mg = mg_find(name, PERL_MAGIC_proto);
10326         assert(mg);
10327         assert(mg->mg_obj);
10328         return (CV *)mg->mg_obj;
10329     }
10330     return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
10331 }
10332
10333 CV *
10334 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
10335 {
10336     OP *rvop;
10337     CV *cv;
10338     GV *gv;
10339     PERL_ARGS_ASSERT_RV2CV_OP_CV;
10340     if (flags & ~(RV2CVOPCV_MARK_EARLY|RV2CVOPCV_RETURN_NAME_GV))
10341         Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
10342     if (cvop->op_type != OP_RV2CV)
10343         return NULL;
10344     if (cvop->op_private & OPpENTERSUB_AMPER)
10345         return NULL;
10346     if (!(cvop->op_flags & OPf_KIDS))
10347         return NULL;
10348     rvop = cUNOPx(cvop)->op_first;
10349     switch (rvop->op_type) {
10350         case OP_GV: {
10351             gv = cGVOPx_gv(rvop);
10352             cv = GvCVu(gv);
10353             if (!cv) {
10354                 if (flags & RV2CVOPCV_MARK_EARLY)
10355                     rvop->op_private |= OPpEARLY_CV;
10356                 return NULL;
10357             }
10358         } break;
10359         case OP_CONST: {
10360             SV *rv = cSVOPx_sv(rvop);
10361             if (!SvROK(rv))
10362                 return NULL;
10363             cv = (CV*)SvRV(rv);
10364             gv = NULL;
10365         } break;
10366         case OP_PADCV: {
10367             cv = find_lexical_cv(rvop->op_targ);
10368             gv = NULL;
10369         } break;
10370         default: {
10371             return NULL;
10372         } break;
10373     }
10374     if (SvTYPE((SV*)cv) != SVt_PVCV)
10375         return NULL;
10376     if (flags & RV2CVOPCV_RETURN_NAME_GV) {
10377         if (!CvANON(cv) || !gv)
10378             gv = CvGV(cv);
10379         return (CV*)gv;
10380     } else {
10381         return cv;
10382     }
10383 }
10384
10385 /*
10386 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
10387
10388 Performs the default fixup of the arguments part of an C<entersub>
10389 op tree.  This consists of applying list context to each of the
10390 argument ops.  This is the standard treatment used on a call marked
10391 with C<&>, or a method call, or a call through a subroutine reference,
10392 or any other call where the callee can't be identified at compile time,
10393 or a call where the callee has no prototype.
10394
10395 =cut
10396 */
10397
10398 OP *
10399 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
10400 {
10401     OP *aop;
10402     PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
10403     aop = cUNOPx(entersubop)->op_first;
10404     if (!aop->op_sibling)
10405         aop = cUNOPx(aop)->op_first;
10406     for (aop = aop->op_sibling; aop->op_sibling; aop = aop->op_sibling) {
10407         if (!(PL_madskills && aop->op_type == OP_STUB)) {
10408             list(aop);
10409             op_lvalue(aop, OP_ENTERSUB);
10410         }
10411     }
10412     return entersubop;
10413 }
10414
10415 /*
10416 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
10417
10418 Performs the fixup of the arguments part of an C<entersub> op tree
10419 based on a subroutine prototype.  This makes various modifications to
10420 the argument ops, from applying context up to inserting C<refgen> ops,
10421 and checking the number and syntactic types of arguments, as directed by
10422 the prototype.  This is the standard treatment used on a subroutine call,
10423 not marked with C<&>, where the callee can be identified at compile time
10424 and has a prototype.
10425
10426 I<protosv> supplies the subroutine prototype to be applied to the call.
10427 It may be a normal defined scalar, of which the string value will be used.
10428 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
10429 that has been cast to C<SV*>) which has a prototype.  The prototype
10430 supplied, in whichever form, does not need to match the actual callee
10431 referenced by the op tree.
10432
10433 If the argument ops disagree with the prototype, for example by having
10434 an unacceptable number of arguments, a valid op tree is returned anyway.
10435 The error is reflected in the parser state, normally resulting in a single
10436 exception at the top level of parsing which covers all the compilation
10437 errors that occurred.  In the error message, the callee is referred to
10438 by the name defined by the I<namegv> parameter.
10439
10440 =cut
10441 */
10442
10443 OP *
10444 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
10445 {
10446     STRLEN proto_len;
10447     const char *proto, *proto_end;
10448     OP *aop, *prev, *cvop;
10449     int optional = 0;
10450     I32 arg = 0;
10451     I32 contextclass = 0;
10452     const char *e = NULL;
10453     PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
10454     if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
10455         Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
10456                    "flags=%lx", (unsigned long) SvFLAGS(protosv));
10457     if (SvTYPE(protosv) == SVt_PVCV)
10458          proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
10459     else proto = SvPV(protosv, proto_len);
10460     proto = S_strip_spaces(aTHX_ proto, &proto_len);
10461     proto_end = proto + proto_len;
10462     aop = cUNOPx(entersubop)->op_first;
10463     if (!aop->op_sibling)
10464         aop = cUNOPx(aop)->op_first;
10465     prev = aop;
10466     aop = aop->op_sibling;
10467     for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10468     while (aop != cvop) {
10469         OP* o3;
10470         if (PL_madskills && aop->op_type == OP_STUB) {
10471             aop = aop->op_sibling;
10472             continue;
10473         }
10474         if (PL_madskills && aop->op_type == OP_NULL)
10475             o3 = ((UNOP*)aop)->op_first;
10476         else
10477             o3 = aop;
10478
10479         if (proto >= proto_end)
10480             return too_many_arguments_sv(entersubop, gv_ename(namegv), 0);
10481
10482         switch (*proto) {
10483             case ';':
10484                 optional = 1;
10485                 proto++;
10486                 continue;
10487             case '_':
10488                 /* _ must be at the end */
10489                 if (proto[1] && !strchr(";@%", proto[1]))
10490                     goto oops;
10491             case '$':
10492                 proto++;
10493                 arg++;
10494                 scalar(aop);
10495                 break;
10496             case '%':
10497             case '@':
10498                 list(aop);
10499                 arg++;
10500                 break;
10501             case '&':
10502                 proto++;
10503                 arg++;
10504                 if (o3->op_type != OP_REFGEN && o3->op_type != OP_UNDEF)
10505                     bad_type_gv(arg,
10506                             arg == 1 ? "block or sub {}" : "sub {}",
10507                             namegv, 0, o3);
10508                 break;
10509             case '*':
10510                 /* '*' allows any scalar type, including bareword */
10511                 proto++;
10512                 arg++;
10513                 if (o3->op_type == OP_RV2GV)
10514                     goto wrapref;       /* autoconvert GLOB -> GLOBref */
10515                 else if (o3->op_type == OP_CONST)
10516                     o3->op_private &= ~OPpCONST_STRICT;
10517                 else if (o3->op_type == OP_ENTERSUB) {
10518                     /* accidental subroutine, revert to bareword */
10519                     OP *gvop = ((UNOP*)o3)->op_first;
10520                     if (gvop && gvop->op_type == OP_NULL) {
10521                         gvop = ((UNOP*)gvop)->op_first;
10522                         if (gvop) {
10523                             for (; gvop->op_sibling; gvop = gvop->op_sibling)
10524                                 ;
10525                             if (gvop &&
10526                                     (gvop->op_private & OPpENTERSUB_NOPAREN) &&
10527                                     (gvop = ((UNOP*)gvop)->op_first) &&
10528                                     gvop->op_type == OP_GV)
10529                             {
10530                                 GV * const gv = cGVOPx_gv(gvop);
10531                                 OP * const sibling = aop->op_sibling;
10532                                 SV * const n = newSVpvs("");
10533 #ifdef PERL_MAD
10534                                 OP * const oldaop = aop;
10535 #else
10536                                 op_free(aop);
10537 #endif
10538                                 gv_fullname4(n, gv, "", FALSE);
10539                                 aop = newSVOP(OP_CONST, 0, n);
10540                                 op_getmad(oldaop,aop,'O');
10541                                 prev->op_sibling = aop;
10542                                 aop->op_sibling = sibling;
10543                             }
10544                         }
10545                     }
10546                 }
10547                 scalar(aop);
10548                 break;
10549             case '+':
10550                 proto++;
10551                 arg++;
10552                 if (o3->op_type == OP_RV2AV ||
10553                     o3->op_type == OP_PADAV ||
10554                     o3->op_type == OP_RV2HV ||
10555                     o3->op_type == OP_PADHV
10556                 ) {
10557                     goto wrapref;
10558                 }
10559                 scalar(aop);
10560                 break;
10561             case '[': case ']':
10562                 goto oops;
10563                 break;
10564             case '\\':
10565                 proto++;
10566                 arg++;
10567             again:
10568                 switch (*proto++) {
10569                     case '[':
10570                         if (contextclass++ == 0) {
10571                             e = strchr(proto, ']');
10572                             if (!e || e == proto)
10573                                 goto oops;
10574                         }
10575                         else
10576                             goto oops;
10577                         goto again;
10578                         break;
10579                     case ']':
10580                         if (contextclass) {
10581                             const char *p = proto;
10582                             const char *const end = proto;
10583                             contextclass = 0;
10584                             while (*--p != '[')
10585                                 /* \[$] accepts any scalar lvalue */
10586                                 if (*p == '$'
10587                                  && Perl_op_lvalue_flags(aTHX_
10588                                      scalar(o3),
10589                                      OP_READ, /* not entersub */
10590                                      OP_LVALUE_NO_CROAK
10591                                     )) goto wrapref;
10592                             bad_type_gv(arg, Perl_form(aTHX_ "one of %.*s",
10593                                         (int)(end - p), p),
10594                                     namegv, 0, o3);
10595                         } else
10596                             goto oops;
10597                         break;
10598                     case '*':
10599                         if (o3->op_type == OP_RV2GV)
10600                             goto wrapref;
10601                         if (!contextclass)
10602                             bad_type_gv(arg, "symbol", namegv, 0, o3);
10603                         break;
10604                     case '&':
10605                         if (o3->op_type == OP_ENTERSUB)
10606                             goto wrapref;
10607                         if (!contextclass)
10608                             bad_type_gv(arg, "subroutine entry", namegv, 0,
10609                                     o3);
10610                         break;
10611                     case '$':
10612                         if (o3->op_type == OP_RV2SV ||
10613                                 o3->op_type == OP_PADSV ||
10614                                 o3->op_type == OP_HELEM ||
10615                                 o3->op_type == OP_AELEM)
10616                             goto wrapref;
10617                         if (!contextclass) {
10618                             /* \$ accepts any scalar lvalue */
10619                             if (Perl_op_lvalue_flags(aTHX_
10620                                     scalar(o3),
10621                                     OP_READ,  /* not entersub */
10622                                     OP_LVALUE_NO_CROAK
10623                                )) goto wrapref;
10624                             bad_type_gv(arg, "scalar", namegv, 0, o3);
10625                         }
10626                         break;
10627                     case '@':
10628                         if (o3->op_type == OP_RV2AV ||
10629                                 o3->op_type == OP_PADAV)
10630                             goto wrapref;
10631                         if (!contextclass)
10632                             bad_type_gv(arg, "array", namegv, 0, o3);
10633                         break;
10634                     case '%':
10635                         if (o3->op_type == OP_RV2HV ||
10636                                 o3->op_type == OP_PADHV)
10637                             goto wrapref;
10638                         if (!contextclass)
10639                             bad_type_gv(arg, "hash", namegv, 0, o3);
10640                         break;
10641                     wrapref:
10642                         {
10643                             OP* const kid = aop;
10644                             OP* const sib = kid->op_sibling;
10645                             kid->op_sibling = 0;
10646                             aop = newUNOP(OP_REFGEN, 0, kid);
10647                             aop->op_sibling = sib;
10648                             prev->op_sibling = aop;
10649                         }
10650                         if (contextclass && e) {
10651                             proto = e + 1;
10652                             contextclass = 0;
10653                         }
10654                         break;
10655                     default: goto oops;
10656                 }
10657                 if (contextclass)
10658                     goto again;
10659                 break;
10660             case ' ':
10661                 proto++;
10662                 continue;
10663             default:
10664             oops: {
10665                 SV* const tmpsv = sv_newmortal();
10666                 gv_efullname3(tmpsv, namegv, NULL);
10667                 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf,
10668                         SVfARG(tmpsv), SVfARG(protosv));
10669             }
10670         }
10671
10672         op_lvalue(aop, OP_ENTERSUB);
10673         prev = aop;
10674         aop = aop->op_sibling;
10675     }
10676     if (aop == cvop && *proto == '_') {
10677         /* generate an access to $_ */
10678         aop = newDEFSVOP();
10679         aop->op_sibling = prev->op_sibling;
10680         prev->op_sibling = aop; /* instead of cvop */
10681     }
10682     if (!optional && proto_end > proto &&
10683         (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
10684         return too_few_arguments_sv(entersubop, gv_ename(namegv), 0);
10685     return entersubop;
10686 }
10687
10688 /*
10689 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
10690
10691 Performs the fixup of the arguments part of an C<entersub> op tree either
10692 based on a subroutine prototype or using default list-context processing.
10693 This is the standard treatment used on a subroutine call, not marked
10694 with C<&>, where the callee can be identified at compile time.
10695
10696 I<protosv> supplies the subroutine prototype to be applied to the call,
10697 or indicates that there is no prototype.  It may be a normal scalar,
10698 in which case if it is defined then the string value will be used
10699 as a prototype, and if it is undefined then there is no prototype.
10700 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
10701 that has been cast to C<SV*>), of which the prototype will be used if it
10702 has one.  The prototype (or lack thereof) supplied, in whichever form,
10703 does not need to match the actual callee referenced by the op tree.
10704
10705 If the argument ops disagree with the prototype, for example by having
10706 an unacceptable number of arguments, a valid op tree is returned anyway.
10707 The error is reflected in the parser state, normally resulting in a single
10708 exception at the top level of parsing which covers all the compilation
10709 errors that occurred.  In the error message, the callee is referred to
10710 by the name defined by the I<namegv> parameter.
10711
10712 =cut
10713 */
10714
10715 OP *
10716 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
10717         GV *namegv, SV *protosv)
10718 {
10719     PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
10720     if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
10721         return ck_entersub_args_proto(entersubop, namegv, protosv);
10722     else
10723         return ck_entersub_args_list(entersubop);
10724 }
10725
10726 OP *
10727 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
10728 {
10729     int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
10730     OP *aop = cUNOPx(entersubop)->op_first;
10731
10732     PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
10733
10734     if (!opnum) {
10735         OP *cvop;
10736         if (!aop->op_sibling)
10737             aop = cUNOPx(aop)->op_first;
10738         aop = aop->op_sibling;
10739         for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10740         if (PL_madskills) while (aop != cvop && aop->op_type == OP_STUB) {
10741             aop = aop->op_sibling;
10742         }
10743         if (aop != cvop)
10744             (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
10745         
10746         op_free(entersubop);
10747         switch(GvNAME(namegv)[2]) {
10748         case 'F': return newSVOP(OP_CONST, 0,
10749                                         newSVpv(CopFILE(PL_curcop),0));
10750         case 'L': return newSVOP(
10751                            OP_CONST, 0,
10752                            Perl_newSVpvf(aTHX_
10753                              "%"IVdf, (IV)CopLINE(PL_curcop)
10754                            )
10755                          );
10756         case 'P': return newSVOP(OP_CONST, 0,
10757                                    (PL_curstash
10758                                      ? newSVhek(HvNAME_HEK(PL_curstash))
10759                                      : &PL_sv_undef
10760                                    )
10761                                 );
10762         }
10763         NOT_REACHED;
10764     }
10765     else {
10766         OP *prev, *cvop;
10767         U32 flags;
10768 #ifdef PERL_MAD
10769         bool seenarg = FALSE;
10770 #endif
10771         if (!aop->op_sibling)
10772             aop = cUNOPx(aop)->op_first;
10773         
10774         prev = aop;
10775         aop = aop->op_sibling;
10776         prev->op_sibling = NULL;
10777         for (cvop = aop;
10778              cvop->op_sibling;
10779              prev=cvop, cvop = cvop->op_sibling)
10780 #ifdef PERL_MAD
10781             if (PL_madskills && cvop->op_sibling
10782              && cvop->op_type != OP_STUB) seenarg = TRUE
10783 #endif
10784             ;
10785         prev->op_sibling = NULL;
10786         flags = OPf_SPECIAL * !(cvop->op_private & OPpENTERSUB_NOPAREN);
10787         op_free(cvop);
10788         if (aop == cvop) aop = NULL;
10789         op_free(entersubop);
10790
10791         if (opnum == OP_ENTEREVAL
10792          && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
10793             flags |= OPpEVAL_BYTES <<8;
10794         
10795         switch (PL_opargs[opnum] & OA_CLASS_MASK) {
10796         case OA_UNOP:
10797         case OA_BASEOP_OR_UNOP:
10798         case OA_FILESTATOP:
10799             return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
10800         case OA_BASEOP:
10801             if (aop) {
10802 #ifdef PERL_MAD
10803                 if (!PL_madskills || seenarg)
10804 #endif
10805                     (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
10806                 op_free(aop);
10807             }
10808             return opnum == OP_RUNCV
10809                 ? newPVOP(OP_RUNCV,0,NULL)
10810                 : newOP(opnum,0);
10811         default:
10812             return convert(opnum,0,aop);
10813         }
10814     }
10815     assert(0);
10816     return entersubop;
10817 }
10818
10819 /*
10820 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
10821
10822 Retrieves the function that will be used to fix up a call to I<cv>.
10823 Specifically, the function is applied to an C<entersub> op tree for a
10824 subroutine call, not marked with C<&>, where the callee can be identified
10825 at compile time as I<cv>.
10826
10827 The C-level function pointer is returned in I<*ckfun_p>, and an SV
10828 argument for it is returned in I<*ckobj_p>.  The function is intended
10829 to be called in this manner:
10830
10831     entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
10832
10833 In this call, I<entersubop> is a pointer to the C<entersub> op,
10834 which may be replaced by the check function, and I<namegv> is a GV
10835 supplying the name that should be used by the check function to refer
10836 to the callee of the C<entersub> op if it needs to emit any diagnostics.
10837 It is permitted to apply the check function in non-standard situations,
10838 such as to a call to a different subroutine or to a method call.
10839
10840 By default, the function is
10841 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
10842 and the SV parameter is I<cv> itself.  This implements standard
10843 prototype processing.  It can be changed, for a particular subroutine,
10844 by L</cv_set_call_checker>.
10845
10846 =cut
10847 */
10848
10849 void
10850 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
10851 {
10852     MAGIC *callmg;
10853     PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
10854     callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
10855     if (callmg) {
10856         *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
10857         *ckobj_p = callmg->mg_obj;
10858     } else {
10859         *ckfun_p = Perl_ck_entersub_args_proto_or_list;
10860         *ckobj_p = (SV*)cv;
10861     }
10862 }
10863
10864 /*
10865 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
10866
10867 Sets the function that will be used to fix up a call to I<cv>.
10868 Specifically, the function is applied to an C<entersub> op tree for a
10869 subroutine call, not marked with C<&>, where the callee can be identified
10870 at compile time as I<cv>.
10871
10872 The C-level function pointer is supplied in I<ckfun>, and an SV argument
10873 for it is supplied in I<ckobj>.  The function should be defined like this:
10874
10875     STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
10876
10877 It is intended to be called in this manner:
10878
10879     entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
10880
10881 In this call, I<entersubop> is a pointer to the C<entersub> op,
10882 which may be replaced by the check function, and I<namegv> is a GV
10883 supplying the name that should be used by the check function to refer
10884 to the callee of the C<entersub> op if it needs to emit any diagnostics.
10885 It is permitted to apply the check function in non-standard situations,
10886 such as to a call to a different subroutine or to a method call.
10887
10888 The current setting for a particular CV can be retrieved by
10889 L</cv_get_call_checker>.
10890
10891 =cut
10892 */
10893
10894 void
10895 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
10896 {
10897     PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
10898     if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
10899         if (SvMAGICAL((SV*)cv))
10900             mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
10901     } else {
10902         MAGIC *callmg;
10903         sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
10904         callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
10905         if (callmg->mg_flags & MGf_REFCOUNTED) {
10906             SvREFCNT_dec(callmg->mg_obj);
10907             callmg->mg_flags &= ~MGf_REFCOUNTED;
10908         }
10909         callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
10910         callmg->mg_obj = ckobj;
10911         if (ckobj != (SV*)cv) {
10912             SvREFCNT_inc_simple_void_NN(ckobj);
10913             callmg->mg_flags |= MGf_REFCOUNTED;
10914         }
10915         callmg->mg_flags |= MGf_COPY;
10916     }
10917 }
10918
10919 OP *
10920 Perl_ck_subr(pTHX_ OP *o)
10921 {
10922     OP *aop, *cvop;
10923     CV *cv;
10924     GV *namegv;
10925
10926     PERL_ARGS_ASSERT_CK_SUBR;
10927
10928     aop = cUNOPx(o)->op_first;
10929     if (!aop->op_sibling)
10930         aop = cUNOPx(aop)->op_first;
10931     aop = aop->op_sibling;
10932     for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10933     cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
10934     namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_RETURN_NAME_GV) : NULL;
10935
10936     o->op_private &= ~1;
10937     o->op_private |= OPpENTERSUB_HASTARG;
10938     o->op_private |= (PL_hints & HINT_STRICT_REFS);
10939     if (PERLDB_SUB && PL_curstash != PL_debstash)
10940         o->op_private |= OPpENTERSUB_DB;
10941     if (cvop->op_type == OP_RV2CV) {
10942         o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
10943         op_null(cvop);
10944     } else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
10945         if (aop->op_type == OP_CONST)
10946             aop->op_private &= ~OPpCONST_STRICT;
10947         else if (aop->op_type == OP_LIST) {
10948             OP * const sib = ((UNOP*)aop)->op_first->op_sibling;
10949             if (sib && sib->op_type == OP_CONST)
10950                 sib->op_private &= ~OPpCONST_STRICT;
10951         }
10952     }
10953
10954     if (!cv) {
10955         return ck_entersub_args_list(o);
10956     } else {
10957         Perl_call_checker ckfun;
10958         SV *ckobj;
10959         cv_get_call_checker(cv, &ckfun, &ckobj);
10960         if (!namegv) { /* expletive! */
10961             /* XXX The call checker API is public.  And it guarantees that
10962                    a GV will be provided with the right name.  So we have
10963                    to create a GV.  But it is still not correct, as its
10964                    stringification will include the package.  What we
10965                    really need is a new call checker API that accepts a
10966                    GV or string (or GV or CV). */
10967             HEK * const hek = CvNAME_HEK(cv);
10968             /* After a syntax error in a lexical sub, the cv that
10969                rv2cv_op_cv returns may be a nameless stub. */
10970             if (!hek) return ck_entersub_args_list(o);;
10971             namegv = (GV *)sv_newmortal();
10972             gv_init_pvn(namegv, PL_curstash, HEK_KEY(hek), HEK_LEN(hek),
10973                         SVf_UTF8 * !!HEK_UTF8(hek));
10974         }
10975         return ckfun(aTHX_ o, namegv, ckobj);
10976     }
10977 }
10978
10979 OP *
10980 Perl_ck_svconst(pTHX_ OP *o)
10981 {
10982     SV * const sv = cSVOPo->op_sv;
10983     PERL_ARGS_ASSERT_CK_SVCONST;
10984     PERL_UNUSED_CONTEXT;
10985 #ifdef PERL_OLD_COPY_ON_WRITE
10986     if (SvIsCOW(sv)) sv_force_normal(sv);
10987 #elif defined(PERL_NEW_COPY_ON_WRITE)
10988     /* Since the read-only flag may be used to protect a string buffer, we
10989        cannot do copy-on-write with existing read-only scalars that are not
10990        already copy-on-write scalars.  To allow $_ = "hello" to do COW with
10991        that constant, mark the constant as COWable here, if it is not
10992        already read-only. */
10993     if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
10994         SvIsCOW_on(sv);
10995         CowREFCNT(sv) = 0;
10996 # ifdef PERL_DEBUG_READONLY_COW
10997         sv_buf_to_ro(sv);
10998 # endif
10999     }
11000 #endif
11001     SvREADONLY_on(sv);
11002     return o;
11003 }
11004
11005 OP *
11006 Perl_ck_trunc(pTHX_ OP *o)
11007 {
11008     PERL_ARGS_ASSERT_CK_TRUNC;
11009
11010     if (o->op_flags & OPf_KIDS) {
11011         SVOP *kid = (SVOP*)cUNOPo->op_first;
11012
11013         if (kid->op_type == OP_NULL)
11014             kid = (SVOP*)kid->op_sibling;
11015         if (kid && kid->op_type == OP_CONST &&
11016             (kid->op_private & OPpCONST_BARE) &&
11017             !kid->op_folded)
11018         {
11019             o->op_flags |= OPf_SPECIAL;
11020             kid->op_private &= ~OPpCONST_STRICT;
11021         }
11022     }
11023     return ck_fun(o);
11024 }
11025
11026 OP *
11027 Perl_ck_substr(pTHX_ OP *o)
11028 {
11029     PERL_ARGS_ASSERT_CK_SUBSTR;
11030
11031     o = ck_fun(o);
11032     if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
11033         OP *kid = cLISTOPo->op_first;
11034
11035         if (kid->op_type == OP_NULL)
11036             kid = kid->op_sibling;
11037         if (kid)
11038             kid->op_flags |= OPf_MOD;
11039
11040     }
11041     return o;
11042 }
11043
11044 OP *
11045 Perl_ck_tell(pTHX_ OP *o)
11046 {
11047     PERL_ARGS_ASSERT_CK_TELL;
11048     o = ck_fun(o);
11049     if (o->op_flags & OPf_KIDS) {
11050      OP *kid = cLISTOPo->op_first;
11051      if (kid->op_type == OP_NULL && kid->op_sibling) kid = kid->op_sibling;
11052      if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
11053     }
11054     return o;
11055 }
11056
11057 OP *
11058 Perl_ck_each(pTHX_ OP *o)
11059 {
11060     dVAR;
11061     OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
11062     const unsigned orig_type  = o->op_type;
11063     const unsigned array_type = orig_type == OP_EACH ? OP_AEACH
11064                               : orig_type == OP_KEYS ? OP_AKEYS : OP_AVALUES;
11065     const unsigned ref_type   = orig_type == OP_EACH ? OP_REACH
11066                               : orig_type == OP_KEYS ? OP_RKEYS : OP_RVALUES;
11067
11068     PERL_ARGS_ASSERT_CK_EACH;
11069
11070     if (kid) {
11071         switch (kid->op_type) {
11072             case OP_PADHV:
11073             case OP_RV2HV:
11074                 break;
11075             case OP_PADAV:
11076             case OP_RV2AV:
11077                 CHANGE_TYPE(o, array_type);
11078                 break;
11079             case OP_CONST:
11080                 if (kid->op_private == OPpCONST_BARE
11081                  || !SvROK(cSVOPx_sv(kid))
11082                  || (  SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
11083                     && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV  )
11084                    )
11085                     /* we let ck_fun handle it */
11086                     break;
11087             default:
11088                 CHANGE_TYPE(o, ref_type);
11089                 scalar(kid);
11090         }
11091     }
11092     /* if treating as a reference, defer additional checks to runtime */
11093     if (o->op_type == ref_type) {
11094         /* diag_listed_as: keys on reference is experimental */
11095         Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__AUTODEREF),
11096                               "%s is experimental", PL_op_desc[ref_type]);
11097         return o;
11098     }
11099     return ck_fun(o);
11100 }
11101
11102 OP *
11103 Perl_ck_length(pTHX_ OP *o)
11104 {
11105     PERL_ARGS_ASSERT_CK_LENGTH;
11106
11107     o = ck_fun(o);
11108
11109     if (ckWARN(WARN_SYNTAX)) {
11110         const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
11111
11112         if (kid) {
11113             SV *name = NULL;
11114             const bool hash = kid->op_type == OP_PADHV
11115                            || kid->op_type == OP_RV2HV;
11116             switch (kid->op_type) {
11117                 case OP_PADHV:
11118                 case OP_PADAV:
11119                 case OP_RV2HV:
11120                 case OP_RV2AV:
11121                     name = S_op_varname(aTHX_ kid);
11122                     break;
11123                 default:
11124                     return o;
11125             }
11126             if (name)
11127                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11128                     "length() used on %"SVf" (did you mean \"scalar(%s%"SVf
11129                     ")\"?)",
11130                     name, hash ? "keys " : "", name
11131                 );
11132             else if (hash)
11133      /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11134                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11135                     "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
11136             else
11137      /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11138                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11139                     "length() used on @array (did you mean \"scalar(@array)\"?)");
11140         }
11141     }
11142
11143     return o;
11144 }
11145
11146 /* Check for in place reverse and sort assignments like "@a = reverse @a"
11147    and modify the optree to make them work inplace */
11148
11149 STATIC void
11150 S_inplace_aassign(pTHX_ OP *o) {
11151
11152     OP *modop, *modop_pushmark;
11153     OP *oright;
11154     OP *oleft, *oleft_pushmark;
11155
11156     PERL_ARGS_ASSERT_INPLACE_AASSIGN;
11157
11158     assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
11159
11160     assert(cUNOPo->op_first->op_type == OP_NULL);
11161     modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
11162     assert(modop_pushmark->op_type == OP_PUSHMARK);
11163     modop = modop_pushmark->op_sibling;
11164
11165     if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
11166         return;
11167
11168     /* no other operation except sort/reverse */
11169     if (modop->op_sibling)
11170         return;
11171
11172     assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
11173     if (!(oright = cUNOPx(modop)->op_first->op_sibling)) return;
11174
11175     if (modop->op_flags & OPf_STACKED) {
11176         /* skip sort subroutine/block */
11177         assert(oright->op_type == OP_NULL);
11178         oright = oright->op_sibling;
11179     }
11180
11181     assert(cUNOPo->op_first->op_sibling->op_type == OP_NULL);
11182     oleft_pushmark = cUNOPx(cUNOPo->op_first->op_sibling)->op_first;
11183     assert(oleft_pushmark->op_type == OP_PUSHMARK);
11184     oleft = oleft_pushmark->op_sibling;
11185
11186     /* Check the lhs is an array */
11187     if (!oleft ||
11188         (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
11189         || oleft->op_sibling
11190         || (oleft->op_private & OPpLVAL_INTRO)
11191     )
11192         return;
11193
11194     /* Only one thing on the rhs */
11195     if (oright->op_sibling)
11196         return;
11197
11198     /* check the array is the same on both sides */
11199     if (oleft->op_type == OP_RV2AV) {
11200         if (oright->op_type != OP_RV2AV
11201             || !cUNOPx(oright)->op_first
11202             || cUNOPx(oright)->op_first->op_type != OP_GV
11203             || cUNOPx(oleft )->op_first->op_type != OP_GV
11204             || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
11205                cGVOPx_gv(cUNOPx(oright)->op_first)
11206         )
11207             return;
11208     }
11209     else if (oright->op_type != OP_PADAV
11210         || oright->op_targ != oleft->op_targ
11211     )
11212         return;
11213
11214     /* This actually is an inplace assignment */
11215
11216     modop->op_private |= OPpSORT_INPLACE;
11217
11218     /* transfer MODishness etc from LHS arg to RHS arg */
11219     oright->op_flags = oleft->op_flags;
11220
11221     /* remove the aassign op and the lhs */
11222     op_null(o);
11223     op_null(oleft_pushmark);
11224     if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
11225         op_null(cUNOPx(oleft)->op_first);
11226     op_null(oleft);
11227 }
11228
11229
11230
11231 /* mechanism for deferring recursion in rpeep() */
11232
11233 #define MAX_DEFERRED 4
11234
11235 #define DEFER(o) \
11236   STMT_START { \
11237     if (defer_ix == (MAX_DEFERRED-1)) { \
11238         OP **defer = defer_queue[defer_base]; \
11239         CALL_RPEEP(*defer); \
11240         S_prune_chain_head(aTHX_ defer); \
11241         defer_base = (defer_base + 1) % MAX_DEFERRED; \
11242         defer_ix--; \
11243     } \
11244     defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
11245   } STMT_END
11246
11247 #define IS_AND_OP(o)   (o->op_type == OP_AND)
11248 #define IS_OR_OP(o)    (o->op_type == OP_OR)
11249
11250
11251 STATIC void
11252 S_null_listop_in_list_context(pTHX_ OP *o)
11253 {
11254     OP *kid;
11255
11256     PERL_ARGS_ASSERT_NULL_LISTOP_IN_LIST_CONTEXT;
11257
11258     /* This is an OP_LIST in list context. That means we
11259      * can ditch the OP_LIST and the OP_PUSHMARK within. */
11260
11261     kid = cLISTOPo->op_first;
11262     /* Find the end of the chain of OPs executed within the OP_LIST. */
11263     while (kid->op_next != o)
11264         kid = kid->op_next;
11265
11266     kid->op_next = o->op_next; /* patch list out of exec chain */
11267     op_null(cUNOPo->op_first); /* NULL the pushmark */
11268     op_null(o); /* NULL the list */
11269 }
11270
11271 /* A peephole optimizer.  We visit the ops in the order they're to execute.
11272  * See the comments at the top of this file for more details about when
11273  * peep() is called */
11274
11275 void
11276 Perl_rpeep(pTHX_ OP *o)
11277 {
11278     dVAR;
11279     OP* oldop = NULL;
11280     OP* oldoldop = NULL;
11281     OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
11282     int defer_base = 0;
11283     int defer_ix = -1;
11284
11285     if (!o || o->op_opt)
11286         return;
11287     ENTER;
11288     SAVEOP();
11289     SAVEVPTR(PL_curcop);
11290     for (;; o = o->op_next) {
11291         if (o && o->op_opt)
11292             o = NULL;
11293         if (!o) {
11294             while (defer_ix >= 0) {
11295                 OP **defer =
11296                         defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
11297                 CALL_RPEEP(*defer);
11298                 S_prune_chain_head(aTHX_ defer);
11299             }
11300             break;
11301         }
11302
11303         /* By default, this op has now been optimised. A couple of cases below
11304            clear this again.  */
11305         o->op_opt = 1;
11306         PL_op = o;
11307
11308
11309         /* The following will have the OP_LIST and OP_PUSHMARK
11310          * patched out later IF the OP_LIST is in list context.
11311          * So in that case, we can set the this OP's op_next
11312          * to skip to after the OP_PUSHMARK:
11313          *   a THIS -> b
11314          *   d list -> e
11315          *   b   pushmark -> c
11316          *   c   whatever -> d
11317          *   e whatever
11318          * will eventually become:
11319          *   a THIS -> c
11320          *   - ex-list -> -
11321          *   -   ex-pushmark -> -
11322          *   c   whatever -> e
11323          *   e whatever
11324          */
11325         {
11326             OP *sibling;
11327             OP *other_pushmark;
11328             if (OP_TYPE_IS(o->op_next, OP_PUSHMARK)
11329                 && (sibling = o->op_sibling)
11330                 && sibling->op_type == OP_LIST
11331                 /* This KIDS check is likely superfluous since OP_LIST
11332                  * would otherwise be an OP_STUB. */
11333                 && sibling->op_flags & OPf_KIDS
11334                 && (sibling->op_flags & OPf_WANT) == OPf_WANT_LIST
11335                 && (other_pushmark = cLISTOPx(sibling)->op_first)
11336                 /* Pointer equality also effectively checks that it's a
11337                  * pushmark. */
11338                 && other_pushmark == o->op_next)
11339             {
11340                 o->op_next = other_pushmark->op_next;
11341                 null_listop_in_list_context(sibling);
11342             }
11343         }
11344
11345         switch (o->op_type) {
11346         case OP_DBSTATE:
11347             PL_curcop = ((COP*)o);              /* for warnings */
11348             break;
11349         case OP_NEXTSTATE:
11350             PL_curcop = ((COP*)o);              /* for warnings */
11351
11352             /* Optimise a "return ..." at the end of a sub to just be "...".
11353              * This saves 2 ops. Before:
11354              * 1  <;> nextstate(main 1 -e:1) v ->2
11355              * 4  <@> return K ->5
11356              * 2    <0> pushmark s ->3
11357              * -    <1> ex-rv2sv sK/1 ->4
11358              * 3      <#> gvsv[*cat] s ->4
11359              *
11360              * After:
11361              * -  <@> return K ->-
11362              * -    <0> pushmark s ->2
11363              * -    <1> ex-rv2sv sK/1 ->-
11364              * 2      <$> gvsv(*cat) s ->3
11365              */
11366             {
11367                 OP *next = o->op_next;
11368                 OP *sibling = o->op_sibling;
11369                 if (   OP_TYPE_IS(next, OP_PUSHMARK)
11370                     && OP_TYPE_IS(sibling, OP_RETURN)
11371                     && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
11372                     && OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
11373                     && cUNOPx(sibling)->op_first == next
11374                     && next->op_sibling && next->op_sibling->op_next
11375                     && next->op_next
11376                 ) {
11377                     /* Look through the PUSHMARK's siblings for one that
11378                      * points to the RETURN */
11379                     OP *top = next->op_sibling;
11380                     while (top && top->op_next) {
11381                         if (top->op_next == sibling) {
11382                             top->op_next = sibling->op_next;
11383                             o->op_next = next->op_next;
11384                             break;
11385                         }
11386                         top = top->op_sibling;
11387                     }
11388                 }
11389             }
11390
11391             /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
11392              *
11393              * This latter form is then suitable for conversion into padrange
11394              * later on. Convert:
11395              *
11396              *   nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
11397              *
11398              * into:
11399              *
11400              *   nextstate1 ->     listop     -> nextstate3
11401              *                 /            \
11402              *         pushmark -> padop1 -> padop2
11403              */
11404             if (o->op_next && (
11405                     o->op_next->op_type == OP_PADSV
11406                  || o->op_next->op_type == OP_PADAV
11407                  || o->op_next->op_type == OP_PADHV
11408                 )
11409                 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
11410                 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
11411                 && o->op_next->op_next->op_next && (
11412                     o->op_next->op_next->op_next->op_type == OP_PADSV
11413                  || o->op_next->op_next->op_next->op_type == OP_PADAV
11414                  || o->op_next->op_next->op_next->op_type == OP_PADHV
11415                 )
11416                 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
11417                 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
11418                 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
11419                 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
11420             ) {
11421                 OP *first;
11422                 OP *last;
11423                 OP *newop;
11424
11425                 first = o->op_next;
11426                 last = o->op_next->op_next->op_next;
11427
11428                 newop = newLISTOP(OP_LIST, 0, first, last);
11429                 newop->op_flags |= OPf_PARENS;
11430                 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
11431
11432                 /* Kill nextstate2 between padop1/padop2 */
11433                 op_free(first->op_next);
11434
11435                 first->op_next = last;                /* padop2 */
11436                 first->op_sibling = last;             /* ... */
11437                 o->op_next = cUNOPx(newop)->op_first; /* pushmark */
11438                 o->op_next->op_next = first;          /* padop1 */
11439                 o->op_next->op_sibling = first;       /* ... */
11440                 newop->op_next = last->op_next;       /* nextstate3 */
11441                 newop->op_sibling = last->op_sibling;
11442                 last->op_next = newop;                /* listop */
11443                 last->op_sibling = NULL;
11444                 o->op_sibling = newop;                /* ... */
11445
11446                 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
11447
11448                 /* Ensure pushmark has this flag if padops do */
11449                 if (first->op_flags & OPf_MOD && last->op_flags & OPf_MOD) {
11450                     o->op_next->op_flags |= OPf_MOD;
11451                 }
11452
11453                 break;
11454             }
11455
11456             /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
11457                to carry two labels. For now, take the easier option, and skip
11458                this optimisation if the first NEXTSTATE has a label.  */
11459             if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
11460                 OP *nextop = o->op_next;
11461                 while (nextop && nextop->op_type == OP_NULL)
11462                     nextop = nextop->op_next;
11463
11464                 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
11465                     COP *firstcop = (COP *)o;
11466                     COP *secondcop = (COP *)nextop;
11467                     /* We want the COP pointed to by o (and anything else) to
11468                        become the next COP down the line.  */
11469                     cop_free(firstcop);
11470
11471                     firstcop->op_next = secondcop->op_next;
11472
11473                     /* Now steal all its pointers, and duplicate the other
11474                        data.  */
11475                     firstcop->cop_line = secondcop->cop_line;
11476 #ifdef USE_ITHREADS
11477                     firstcop->cop_stashoff = secondcop->cop_stashoff;
11478                     firstcop->cop_file = secondcop->cop_file;
11479 #else
11480                     firstcop->cop_stash = secondcop->cop_stash;
11481                     firstcop->cop_filegv = secondcop->cop_filegv;
11482 #endif
11483                     firstcop->cop_hints = secondcop->cop_hints;
11484                     firstcop->cop_seq = secondcop->cop_seq;
11485                     firstcop->cop_warnings = secondcop->cop_warnings;
11486                     firstcop->cop_hints_hash = secondcop->cop_hints_hash;
11487
11488 #ifdef USE_ITHREADS
11489                     secondcop->cop_stashoff = 0;
11490                     secondcop->cop_file = NULL;
11491 #else
11492                     secondcop->cop_stash = NULL;
11493                     secondcop->cop_filegv = NULL;
11494 #endif
11495                     secondcop->cop_warnings = NULL;
11496                     secondcop->cop_hints_hash = NULL;
11497
11498                     /* If we use op_null(), and hence leave an ex-COP, some
11499                        warnings are misreported. For example, the compile-time
11500                        error in 'use strict; no strict refs;'  */
11501                     secondcop->op_type = OP_NULL;
11502                     secondcop->op_ppaddr = PL_ppaddr[OP_NULL];
11503                 }
11504             }
11505             break;
11506
11507         case OP_CONCAT:
11508             if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
11509                 if (o->op_next->op_private & OPpTARGET_MY) {
11510                     if (o->op_flags & OPf_STACKED) /* chained concats */
11511                         break; /* ignore_optimization */
11512                     else {
11513                         /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
11514                         o->op_targ = o->op_next->op_targ;
11515                         o->op_next->op_targ = 0;
11516                         o->op_private |= OPpTARGET_MY;
11517                     }
11518                 }
11519                 op_null(o->op_next);
11520             }
11521             break;
11522         case OP_STUB:
11523             if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
11524                 break; /* Scalar stub must produce undef.  List stub is noop */
11525             }
11526             goto nothin;
11527         case OP_NULL:
11528             if (o->op_targ == OP_NEXTSTATE
11529                 || o->op_targ == OP_DBSTATE)
11530             {
11531                 PL_curcop = ((COP*)o);
11532             }
11533             /* XXX: We avoid setting op_seq here to prevent later calls
11534                to rpeep() from mistakenly concluding that optimisation
11535                has already occurred. This doesn't fix the real problem,
11536                though (See 20010220.007). AMS 20010719 */
11537             /* op_seq functionality is now replaced by op_opt */
11538             o->op_opt = 0;
11539             /* FALL THROUGH */
11540         case OP_SCALAR:
11541         case OP_LINESEQ:
11542         case OP_SCOPE:
11543         nothin:
11544             if (oldop) {
11545                 oldop->op_next = o->op_next;
11546                 o->op_opt = 0;
11547                 continue;
11548             }
11549             break;
11550
11551         case OP_PUSHMARK:
11552
11553             /* Convert a series of PAD ops for my vars plus support into a
11554              * single padrange op. Basically
11555              *
11556              *    pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
11557              *
11558              * becomes, depending on circumstances, one of
11559              *
11560              *    padrange  ----------------------------------> (list) -> rest
11561              *    padrange  --------------------------------------------> rest
11562              *
11563              * where all the pad indexes are sequential and of the same type
11564              * (INTRO or not).
11565              * We convert the pushmark into a padrange op, then skip
11566              * any other pad ops, and possibly some trailing ops.
11567              * Note that we don't null() the skipped ops, to make it
11568              * easier for Deparse to undo this optimisation (and none of
11569              * the skipped ops are holding any resourses). It also makes
11570              * it easier for find_uninit_var(), as it can just ignore
11571              * padrange, and examine the original pad ops.
11572              */
11573         {
11574             OP *p;
11575             OP *followop = NULL; /* the op that will follow the padrange op */
11576             U8 count = 0;
11577             U8 intro = 0;
11578             PADOFFSET base = 0; /* init only to stop compiler whining */
11579             U8 gimme       = 0; /* init only to stop compiler whining */
11580             bool defav = 0;  /* seen (...) = @_ */
11581             bool reuse = 0;  /* reuse an existing padrange op */
11582
11583             /* look for a pushmark -> gv[_] -> rv2av */
11584
11585             {
11586                 GV *gv;
11587                 OP *rv2av, *q;
11588                 p = o->op_next;
11589                 if (   p->op_type == OP_GV
11590                     && (gv = cGVOPx_gv(p))
11591                     && GvNAMELEN_get(gv) == 1
11592                     && *GvNAME_get(gv) == '_'
11593                     && GvSTASH(gv) == PL_defstash
11594                     && (rv2av = p->op_next)
11595                     && rv2av->op_type == OP_RV2AV
11596                     && !(rv2av->op_flags & OPf_REF)
11597                     && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
11598                     && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
11599                     && o->op_sibling == rv2av /* these two for Deparse */
11600                     && cUNOPx(rv2av)->op_first == p
11601                 ) {
11602                     q = rv2av->op_next;
11603                     if (q->op_type == OP_NULL)
11604                         q = q->op_next;
11605                     if (q->op_type == OP_PUSHMARK) {
11606                         defav = 1;
11607                         p = q;
11608                     }
11609                 }
11610             }
11611             if (!defav) {
11612                 /* To allow Deparse to pessimise this, it needs to be able
11613                  * to restore the pushmark's original op_next, which it
11614                  * will assume to be the same as op_sibling. */
11615                 if (o->op_next != o->op_sibling)
11616                     break;
11617                 p = o;
11618             }
11619
11620             /* scan for PAD ops */
11621
11622             for (p = p->op_next; p; p = p->op_next) {
11623                 if (p->op_type == OP_NULL)
11624                     continue;
11625
11626                 if ((     p->op_type != OP_PADSV
11627                        && p->op_type != OP_PADAV
11628                        && p->op_type != OP_PADHV
11629                     )
11630                       /* any private flag other than INTRO? e.g. STATE */
11631                    || (p->op_private & ~OPpLVAL_INTRO)
11632                 )
11633                     break;
11634
11635                 /* let $a[N] potentially be optimised into AELEMFAST_LEX
11636                  * instead */
11637                 if (   p->op_type == OP_PADAV
11638                     && p->op_next
11639                     && p->op_next->op_type == OP_CONST
11640                     && p->op_next->op_next
11641                     && p->op_next->op_next->op_type == OP_AELEM
11642                 )
11643                     break;
11644
11645                 /* for 1st padop, note what type it is and the range
11646                  * start; for the others, check that it's the same type
11647                  * and that the targs are contiguous */
11648                 if (count == 0) {
11649                     intro = (p->op_private & OPpLVAL_INTRO);
11650                     base = p->op_targ;
11651                     gimme = (p->op_flags & OPf_WANT);
11652                 }
11653                 else {
11654                     if ((p->op_private & OPpLVAL_INTRO) != intro)
11655                         break;
11656                     /* Note that you'd normally  expect targs to be
11657                      * contiguous in my($a,$b,$c), but that's not the case
11658                      * when external modules start doing things, e.g.
11659                      i* Function::Parameters */
11660                     if (p->op_targ != base + count)
11661                         break;
11662                     assert(p->op_targ == base + count);
11663                     /* all the padops should be in the same context */
11664                     if (gimme != (p->op_flags & OPf_WANT))
11665                         break;
11666                 }
11667
11668                 /* for AV, HV, only when we're not flattening */
11669                 if (   p->op_type != OP_PADSV
11670                     && gimme != OPf_WANT_VOID
11671                     && !(p->op_flags & OPf_REF)
11672                 )
11673                     break;
11674
11675                 if (count >= OPpPADRANGE_COUNTMASK)
11676                     break;
11677
11678                 /* there's a biggest base we can fit into a
11679                  * SAVEt_CLEARPADRANGE in pp_padrange */
11680                 if (intro && base >
11681                         (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)))
11682                     break;
11683
11684                 /* Success! We've got another valid pad op to optimise away */
11685                 count++;
11686                 followop = p->op_next;
11687             }
11688
11689             if (count < 1)
11690                 break;
11691
11692             /* pp_padrange in specifically compile-time void context
11693              * skips pushing a mark and lexicals; in all other contexts
11694              * (including unknown till runtime) it pushes a mark and the
11695              * lexicals. We must be very careful then, that the ops we
11696              * optimise away would have exactly the same effect as the
11697              * padrange.
11698              * In particular in void context, we can only optimise to
11699              * a padrange if see see the complete sequence
11700              *     pushmark, pad*v, ...., list, nextstate
11701              * which has the net effect of of leaving the stack empty
11702              * (for now we leave the nextstate in the execution chain, for
11703              * its other side-effects).
11704              */
11705             assert(followop);
11706             if (gimme == OPf_WANT_VOID) {
11707                 if (OP_TYPE_IS_OR_WAS(followop, OP_LIST)
11708                         && gimme == (followop->op_flags & OPf_WANT)
11709                         && (   followop->op_next->op_type == OP_NEXTSTATE
11710                             || followop->op_next->op_type == OP_DBSTATE))
11711                 {
11712                     followop = followop->op_next; /* skip OP_LIST */
11713
11714                     /* consolidate two successive my(...);'s */
11715
11716                     if (   oldoldop
11717                         && oldoldop->op_type == OP_PADRANGE
11718                         && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
11719                         && (oldoldop->op_private & OPpLVAL_INTRO) == intro
11720                         && !(oldoldop->op_flags & OPf_SPECIAL)
11721                     ) {
11722                         U8 old_count;
11723                         assert(oldoldop->op_next == oldop);
11724                         assert(   oldop->op_type == OP_NEXTSTATE
11725                                || oldop->op_type == OP_DBSTATE);
11726                         assert(oldop->op_next == o);
11727
11728                         old_count
11729                             = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
11730
11731                        /* Do not assume pad offsets for $c and $d are con-
11732                           tiguous in
11733                             my ($a,$b,$c);
11734                             my ($d,$e,$f);
11735                         */
11736                         if (  oldoldop->op_targ + old_count == base
11737                            && old_count < OPpPADRANGE_COUNTMASK - count) {
11738                             base = oldoldop->op_targ;
11739                             count += old_count;
11740                             reuse = 1;
11741                         }
11742                     }
11743
11744                     /* if there's any immediately following singleton
11745                      * my var's; then swallow them and the associated
11746                      * nextstates; i.e.
11747                      *    my ($a,$b); my $c; my $d;
11748                      * is treated as
11749                      *    my ($a,$b,$c,$d);
11750                      */
11751
11752                     while (    ((p = followop->op_next))
11753                             && (  p->op_type == OP_PADSV
11754                                || p->op_type == OP_PADAV
11755                                || p->op_type == OP_PADHV)
11756                             && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
11757                             && (p->op_private & OPpLVAL_INTRO) == intro
11758                             && !(p->op_private & ~OPpLVAL_INTRO)
11759                             && p->op_next
11760                             && (   p->op_next->op_type == OP_NEXTSTATE
11761                                 || p->op_next->op_type == OP_DBSTATE)
11762                             && count < OPpPADRANGE_COUNTMASK
11763                             && base + count == p->op_targ
11764                     ) {
11765                         count++;
11766                         followop = p->op_next;
11767                     }
11768                 }
11769                 else
11770                     break;
11771             }
11772
11773             if (reuse) {
11774                 assert(oldoldop->op_type == OP_PADRANGE);
11775                 oldoldop->op_next = followop;
11776                 oldoldop->op_private = (intro | count);
11777                 o = oldoldop;
11778                 oldop = NULL;
11779                 oldoldop = NULL;
11780             }
11781             else {
11782                 /* Convert the pushmark into a padrange.
11783                  * To make Deparse easier, we guarantee that a padrange was
11784                  * *always* formerly a pushmark */
11785                 assert(o->op_type == OP_PUSHMARK);
11786                 o->op_next = followop;
11787                 o->op_type = OP_PADRANGE;
11788                 o->op_ppaddr = PL_ppaddr[OP_PADRANGE];
11789                 o->op_targ = base;
11790                 /* bit 7: INTRO; bit 6..0: count */
11791                 o->op_private = (intro | count);
11792                 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
11793                                     | gimme | (defav ? OPf_SPECIAL : 0));
11794             }
11795             break;
11796         }
11797
11798         case OP_PADAV:
11799         case OP_GV:
11800             if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
11801                 OP* const pop = (o->op_type == OP_PADAV) ?
11802                             o->op_next : o->op_next->op_next;
11803                 IV i;
11804                 if (pop && pop->op_type == OP_CONST &&
11805                     ((PL_op = pop->op_next)) &&
11806                     pop->op_next->op_type == OP_AELEM &&
11807                     !(pop->op_next->op_private &
11808                       (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
11809                     (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
11810                 {
11811                     GV *gv;
11812                     if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
11813                         no_bareword_allowed(pop);
11814                     if (o->op_type == OP_GV)
11815                         op_null(o->op_next);
11816                     op_null(pop->op_next);
11817                     op_null(pop);
11818                     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
11819                     o->op_next = pop->op_next->op_next;
11820                     o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
11821                     o->op_private = (U8)i;
11822                     if (o->op_type == OP_GV) {
11823                         gv = cGVOPo_gv;
11824                         GvAVn(gv);
11825                         o->op_type = OP_AELEMFAST;
11826                     }
11827                     else
11828                         o->op_type = OP_AELEMFAST_LEX;
11829                 }
11830                 break;
11831             }
11832
11833             if (o->op_next->op_type == OP_RV2SV) {
11834                 if (!(o->op_next->op_private & OPpDEREF)) {
11835                     op_null(o->op_next);
11836                     o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
11837                                                                | OPpOUR_INTRO);
11838                     o->op_next = o->op_next->op_next;
11839                     o->op_type = OP_GVSV;
11840                     o->op_ppaddr = PL_ppaddr[OP_GVSV];
11841                 }
11842             }
11843             else if (o->op_next->op_type == OP_READLINE
11844                     && o->op_next->op_next->op_type == OP_CONCAT
11845                     && (o->op_next->op_next->op_flags & OPf_STACKED))
11846             {
11847                 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
11848                 o->op_type   = OP_RCATLINE;
11849                 o->op_flags |= OPf_STACKED;
11850                 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
11851                 op_null(o->op_next->op_next);
11852                 op_null(o->op_next);
11853             }
11854
11855             break;
11856         
11857         {
11858             OP *fop;
11859             OP *sop;
11860             
11861 #define HV_OR_SCALARHV(op)                                   \
11862     (  (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \
11863        ? (op)                                                  \
11864        : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \
11865        && (  cUNOPx(op)->op_first->op_type == OP_PADHV          \
11866           || cUNOPx(op)->op_first->op_type == OP_RV2HV)          \
11867          ? cUNOPx(op)->op_first                                   \
11868          : NULL)
11869
11870         case OP_NOT:
11871             if ((fop = HV_OR_SCALARHV(cUNOP->op_first)))
11872                 fop->op_private |= OPpTRUEBOOL;
11873             break;
11874
11875         case OP_AND:
11876         case OP_OR:
11877         case OP_DOR:
11878             fop = cLOGOP->op_first;
11879             sop = fop->op_sibling;
11880             while (cLOGOP->op_other->op_type == OP_NULL)
11881                 cLOGOP->op_other = cLOGOP->op_other->op_next;
11882             while (o->op_next && (   o->op_type == o->op_next->op_type
11883                                   || o->op_next->op_type == OP_NULL))
11884                 o->op_next = o->op_next->op_next;
11885
11886             /* if we're an OR and our next is a AND in void context, we'll
11887                follow it's op_other on short circuit, same for reverse.
11888                We can't do this with OP_DOR since if it's true, its return
11889                value is the underlying value which must be evaluated
11890                by the next op */
11891             if (o->op_next &&
11892                 (
11893                     (IS_AND_OP(o) && IS_OR_OP(o->op_next))
11894                  || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
11895                 )
11896                 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
11897             ) {
11898                 o->op_next = ((LOGOP*)o->op_next)->op_other;
11899             }
11900             DEFER(cLOGOP->op_other);
11901           
11902             o->op_opt = 1;
11903             fop = HV_OR_SCALARHV(fop);
11904             if (sop) sop = HV_OR_SCALARHV(sop);
11905             if (fop || sop
11906             ){  
11907                 OP * nop = o;
11908                 OP * lop = o;
11909                 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
11910                     while (nop && nop->op_next) {
11911                         switch (nop->op_next->op_type) {
11912                             case OP_NOT:
11913                             case OP_AND:
11914                             case OP_OR:
11915                             case OP_DOR:
11916                                 lop = nop = nop->op_next;
11917                                 break;
11918                             case OP_NULL:
11919                                 nop = nop->op_next;
11920                                 break;
11921                             default:
11922                                 nop = NULL;
11923                                 break;
11924                         }
11925                     }            
11926                 }
11927                 if (fop) {
11928                     if (  (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
11929                       || o->op_type == OP_AND  )
11930                         fop->op_private |= OPpTRUEBOOL;
11931                     else if (!(lop->op_flags & OPf_WANT))
11932                         fop->op_private |= OPpMAYBE_TRUEBOOL;
11933                 }
11934                 if (  (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
11935                    && sop)
11936                     sop->op_private |= OPpTRUEBOOL;
11937             }                  
11938             
11939             
11940             break;
11941         
11942         case OP_COND_EXPR:
11943             if ((fop = HV_OR_SCALARHV(cLOGOP->op_first)))
11944                 fop->op_private |= OPpTRUEBOOL;
11945 #undef HV_OR_SCALARHV
11946             /* GERONIMO! */
11947         }    
11948
11949         case OP_MAPWHILE:
11950         case OP_GREPWHILE:
11951         case OP_ANDASSIGN:
11952         case OP_ORASSIGN:
11953         case OP_DORASSIGN:
11954         case OP_RANGE:
11955         case OP_ONCE:
11956             while (cLOGOP->op_other->op_type == OP_NULL)
11957                 cLOGOP->op_other = cLOGOP->op_other->op_next;
11958             DEFER(cLOGOP->op_other);
11959             break;
11960
11961         case OP_ENTERLOOP:
11962         case OP_ENTERITER:
11963             while (cLOOP->op_redoop->op_type == OP_NULL)
11964                 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
11965             while (cLOOP->op_nextop->op_type == OP_NULL)
11966                 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
11967             while (cLOOP->op_lastop->op_type == OP_NULL)
11968                 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
11969             /* a while(1) loop doesn't have an op_next that escapes the
11970              * loop, so we have to explicitly follow the op_lastop to
11971              * process the rest of the code */
11972             DEFER(cLOOP->op_lastop);
11973             break;
11974
11975         case OP_ENTERTRY:
11976             assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
11977             DEFER(cLOGOPo->op_other);
11978             break;
11979
11980         case OP_SUBST:
11981             assert(!(cPMOP->op_pmflags & PMf_ONCE));
11982             while (cPMOP->op_pmstashstartu.op_pmreplstart &&
11983                    cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
11984                 cPMOP->op_pmstashstartu.op_pmreplstart
11985                     = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
11986             DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
11987             break;
11988
11989         case OP_SORT: {
11990             OP *oright;
11991
11992             if (o->op_flags & OPf_SPECIAL) {
11993                 /* first arg is a code block */
11994                 OP * const nullop = cLISTOP->op_first->op_sibling;
11995                 OP * kid          = cUNOPx(nullop)->op_first;
11996
11997                 assert(nullop->op_type == OP_NULL);
11998                 assert(kid->op_type == OP_SCOPE
11999                  || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
12000                 /* since OP_SORT doesn't have a handy op_other-style
12001                  * field that can point directly to the start of the code
12002                  * block, store it in the otherwise-unused op_next field
12003                  * of the top-level OP_NULL. This will be quicker at
12004                  * run-time, and it will also allow us to remove leading
12005                  * OP_NULLs by just messing with op_nexts without
12006                  * altering the basic op_first/op_sibling layout. */
12007                 kid = kLISTOP->op_first;
12008                 assert(
12009                       (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
12010                     || kid->op_type == OP_STUB
12011                     || kid->op_type == OP_ENTER);
12012                 nullop->op_next = kLISTOP->op_next;
12013                 DEFER(nullop->op_next);
12014             }
12015
12016             /* check that RHS of sort is a single plain array */
12017             oright = cUNOPo->op_first;
12018             if (!oright || oright->op_type != OP_PUSHMARK)
12019                 break;
12020
12021             if (o->op_private & OPpSORT_INPLACE)
12022                 break;
12023
12024             /* reverse sort ... can be optimised.  */
12025             if (!cUNOPo->op_sibling) {
12026                 /* Nothing follows us on the list. */
12027                 OP * const reverse = o->op_next;
12028
12029                 if (reverse->op_type == OP_REVERSE &&
12030                     (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
12031                     OP * const pushmark = cUNOPx(reverse)->op_first;
12032                     if (pushmark && (pushmark->op_type == OP_PUSHMARK)
12033                         && (cUNOPx(pushmark)->op_sibling == o)) {
12034                         /* reverse -> pushmark -> sort */
12035                         o->op_private |= OPpSORT_REVERSE;
12036                         op_null(reverse);
12037                         pushmark->op_next = oright->op_next;
12038                         op_null(oright);
12039                     }
12040                 }
12041             }
12042
12043             break;
12044         }
12045
12046         case OP_REVERSE: {
12047             OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
12048             OP *gvop = NULL;
12049             LISTOP *enter, *exlist;
12050
12051             if (o->op_private & OPpSORT_INPLACE)
12052                 break;
12053
12054             enter = (LISTOP *) o->op_next;
12055             if (!enter)
12056                 break;
12057             if (enter->op_type == OP_NULL) {
12058                 enter = (LISTOP *) enter->op_next;
12059                 if (!enter)
12060                     break;
12061             }
12062             /* for $a (...) will have OP_GV then OP_RV2GV here.
12063                for (...) just has an OP_GV.  */
12064             if (enter->op_type == OP_GV) {
12065                 gvop = (OP *) enter;
12066                 enter = (LISTOP *) enter->op_next;
12067                 if (!enter)
12068                     break;
12069                 if (enter->op_type == OP_RV2GV) {
12070                   enter = (LISTOP *) enter->op_next;
12071                   if (!enter)
12072                     break;
12073                 }
12074             }
12075
12076             if (enter->op_type != OP_ENTERITER)
12077                 break;
12078
12079             iter = enter->op_next;
12080             if (!iter || iter->op_type != OP_ITER)
12081                 break;
12082             
12083             expushmark = enter->op_first;
12084             if (!expushmark || expushmark->op_type != OP_NULL
12085                 || expushmark->op_targ != OP_PUSHMARK)
12086                 break;
12087
12088             exlist = (LISTOP *) expushmark->op_sibling;
12089             if (!exlist || exlist->op_type != OP_NULL
12090                 || exlist->op_targ != OP_LIST)
12091                 break;
12092
12093             if (exlist->op_last != o) {
12094                 /* Mmm. Was expecting to point back to this op.  */
12095                 break;
12096             }
12097             theirmark = exlist->op_first;
12098             if (!theirmark || theirmark->op_type != OP_PUSHMARK)
12099                 break;
12100
12101             if (theirmark->op_sibling != o) {
12102                 /* There's something between the mark and the reverse, eg
12103                    for (1, reverse (...))
12104                    so no go.  */
12105                 break;
12106             }
12107
12108             ourmark = ((LISTOP *)o)->op_first;
12109             if (!ourmark || ourmark->op_type != OP_PUSHMARK)
12110                 break;
12111
12112             ourlast = ((LISTOP *)o)->op_last;
12113             if (!ourlast || ourlast->op_next != o)
12114                 break;
12115
12116             rv2av = ourmark->op_sibling;
12117             if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
12118                 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
12119                 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
12120                 /* We're just reversing a single array.  */
12121                 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
12122                 enter->op_flags |= OPf_STACKED;
12123             }
12124
12125             /* We don't have control over who points to theirmark, so sacrifice
12126                ours.  */
12127             theirmark->op_next = ourmark->op_next;
12128             theirmark->op_flags = ourmark->op_flags;
12129             ourlast->op_next = gvop ? gvop : (OP *) enter;
12130             op_null(ourmark);
12131             op_null(o);
12132             enter->op_private |= OPpITER_REVERSED;
12133             iter->op_private |= OPpITER_REVERSED;
12134             
12135             break;
12136         }
12137
12138         case OP_QR:
12139         case OP_MATCH:
12140             if (!(cPMOP->op_pmflags & PMf_ONCE)) {
12141                 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
12142             }
12143             break;
12144
12145         case OP_RUNCV:
12146             if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)) {
12147                 SV *sv;
12148                 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
12149                 else {
12150                     sv = newRV((SV *)PL_compcv);
12151                     sv_rvweaken(sv);
12152                     SvREADONLY_on(sv);
12153                 }
12154                 o->op_type = OP_CONST;
12155                 o->op_ppaddr = PL_ppaddr[OP_CONST];
12156                 o->op_flags |= OPf_SPECIAL;
12157                 cSVOPo->op_sv = sv;
12158             }
12159             break;
12160
12161         case OP_SASSIGN:
12162             if (OP_GIMME(o,0) == G_VOID) {
12163                 OP *right = cBINOP->op_first;
12164                 if (right) {
12165                     /*   sassign
12166                     *      RIGHT
12167                     *      substr
12168                     *         pushmark
12169                     *         arg1
12170                     *         arg2
12171                     *         ...
12172                     * becomes
12173                     *
12174                     *  ex-sassign
12175                     *     substr
12176                     *        pushmark
12177                     *        RIGHT
12178                     *        arg1
12179                     *        arg2
12180                     *        ...
12181                     */
12182                     OP *left = right->op_sibling;
12183                     if (left->op_type == OP_SUBSTR
12184                          && (left->op_private & 7) < 4) {
12185                         op_null(o);
12186                         cBINOP->op_first = left;
12187                         right->op_sibling =
12188                             cBINOPx(left)->op_first->op_sibling;
12189                         cBINOPx(left)->op_first->op_sibling = right;
12190                         left->op_private |= OPpSUBSTR_REPL_FIRST;
12191                         left->op_flags =
12192                             (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
12193                     }
12194                 }
12195             }
12196             break;
12197
12198         case OP_CUSTOM: {
12199             Perl_cpeep_t cpeep = 
12200                 XopENTRYCUSTOM(o, xop_peep);
12201             if (cpeep)
12202                 cpeep(aTHX_ o, oldop);
12203             break;
12204         }
12205             
12206         }
12207         /* did we just null the current op? If so, re-process it to handle
12208          * eliding "empty" ops from the chain */
12209         if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
12210             o->op_opt = 0;
12211             o = oldop;
12212         }
12213         else {
12214             oldoldop = oldop;
12215             oldop = o;
12216         }
12217     }
12218     LEAVE;
12219 }
12220
12221 void
12222 Perl_peep(pTHX_ OP *o)
12223 {
12224     CALL_RPEEP(o);
12225 }
12226
12227 /*
12228 =head1 Custom Operators
12229
12230 =for apidoc Ao||custom_op_xop
12231 Return the XOP structure for a given custom op.  This macro should be
12232 considered internal to OP_NAME and the other access macros: use them instead.
12233 This macro does call a function.  Prior
12234 to 5.19.6, this was implemented as a
12235 function.
12236
12237 =cut
12238 */
12239
12240 XOPRETANY
12241 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
12242 {
12243     SV *keysv;
12244     HE *he = NULL;
12245     XOP *xop;
12246
12247     static const XOP xop_null = { 0, 0, 0, 0, 0 };
12248
12249     PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
12250     assert(o->op_type == OP_CUSTOM);
12251
12252     /* This is wrong. It assumes a function pointer can be cast to IV,
12253      * which isn't guaranteed, but this is what the old custom OP code
12254      * did. In principle it should be safer to Copy the bytes of the
12255      * pointer into a PV: since the new interface is hidden behind
12256      * functions, this can be changed later if necessary.  */
12257     /* Change custom_op_xop if this ever happens */
12258     keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
12259
12260     if (PL_custom_ops)
12261         he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
12262
12263     /* assume noone will have just registered a desc */
12264     if (!he && PL_custom_op_names &&
12265         (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
12266     ) {
12267         const char *pv;
12268         STRLEN l;
12269
12270         /* XXX does all this need to be shared mem? */
12271         Newxz(xop, 1, XOP);
12272         pv = SvPV(HeVAL(he), l);
12273         XopENTRY_set(xop, xop_name, savepvn(pv, l));
12274         if (PL_custom_op_descs &&
12275             (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
12276         ) {
12277             pv = SvPV(HeVAL(he), l);
12278             XopENTRY_set(xop, xop_desc, savepvn(pv, l));
12279         }
12280         Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
12281     }
12282     else {
12283         if (!he)
12284             xop = (XOP *)&xop_null;
12285         else
12286             xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
12287     }
12288     {
12289         XOPRETANY any;
12290         if(field == XOPe_xop_ptr) {
12291             any.xop_ptr = xop;
12292         } else {
12293             const U32 flags = XopFLAGS(xop);
12294             if(flags & field) {
12295                 switch(field) {
12296                 case XOPe_xop_name:
12297                     any.xop_name = xop->xop_name;
12298                     break;
12299                 case XOPe_xop_desc:
12300                     any.xop_desc = xop->xop_desc;
12301                     break;
12302                 case XOPe_xop_class:
12303                     any.xop_class = xop->xop_class;
12304                     break;
12305                 case XOPe_xop_peep:
12306                     any.xop_peep = xop->xop_peep;
12307                     break;
12308                 default:
12309                     NOT_REACHED;
12310                     break;
12311                 }
12312             } else {
12313                 switch(field) {
12314                 case XOPe_xop_name:
12315                     any.xop_name = XOPd_xop_name;
12316                     break;
12317                 case XOPe_xop_desc:
12318                     any.xop_desc = XOPd_xop_desc;
12319                     break;
12320                 case XOPe_xop_class:
12321                     any.xop_class = XOPd_xop_class;
12322                     break;
12323                 case XOPe_xop_peep:
12324                     any.xop_peep = XOPd_xop_peep;
12325                     break;
12326                 default:
12327                     NOT_REACHED;
12328                     break;
12329                 }
12330             }
12331         }
12332         return any;
12333     }
12334 }
12335
12336 /*
12337 =for apidoc Ao||custom_op_register
12338 Register a custom op.  See L<perlguts/"Custom Operators">.
12339
12340 =cut
12341 */
12342
12343 void
12344 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
12345 {
12346     SV *keysv;
12347
12348     PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
12349
12350     /* see the comment in custom_op_xop */
12351     keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
12352
12353     if (!PL_custom_ops)
12354         PL_custom_ops = newHV();
12355
12356     if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
12357         Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
12358 }
12359
12360 /*
12361 =head1 Functions in file op.c
12362
12363 =for apidoc core_prototype
12364 This function assigns the prototype of the named core function to C<sv>, or
12365 to a new mortal SV if C<sv> is NULL.  It returns the modified C<sv>, or
12366 NULL if the core function has no prototype.  C<code> is a code as returned
12367 by C<keyword()>.  It must not be equal to 0.
12368
12369 =cut
12370 */
12371
12372 SV *
12373 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
12374                           int * const opnum)
12375 {
12376     int i = 0, n = 0, seen_question = 0, defgv = 0;
12377     I32 oa;
12378 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
12379     char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
12380     bool nullret = FALSE;
12381
12382     PERL_ARGS_ASSERT_CORE_PROTOTYPE;
12383
12384     assert (code);
12385
12386     if (!sv) sv = sv_newmortal();
12387
12388 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
12389
12390     switch (code < 0 ? -code : code) {
12391     case KEY_and   : case KEY_chop: case KEY_chomp:
12392     case KEY_cmp   : case KEY_defined: case KEY_delete: case KEY_exec  :
12393     case KEY_exists: case KEY_eq     : case KEY_ge    : case KEY_goto  :
12394     case KEY_grep  : case KEY_gt     : case KEY_last  : case KEY_le    :
12395     case KEY_lt    : case KEY_map    : case KEY_ne    : case KEY_next  :
12396     case KEY_or    : case KEY_print  : case KEY_printf: case KEY_qr    :
12397     case KEY_redo  : case KEY_require: case KEY_return: case KEY_say   :
12398     case KEY_select: case KEY_sort   : case KEY_split : case KEY_system:
12399     case KEY_x     : case KEY_xor    :
12400         if (!opnum) return NULL; nullret = TRUE; goto findopnum;
12401     case KEY_glob:    retsetpvs("_;", OP_GLOB);
12402     case KEY_keys:    retsetpvs("+", OP_KEYS);
12403     case KEY_values:  retsetpvs("+", OP_VALUES);
12404     case KEY_each:    retsetpvs("+", OP_EACH);
12405     case KEY_push:    retsetpvs("+@", OP_PUSH);
12406     case KEY_unshift: retsetpvs("+@", OP_UNSHIFT);
12407     case KEY_pop:     retsetpvs(";+", OP_POP);
12408     case KEY_shift:   retsetpvs(";+", OP_SHIFT);
12409     case KEY_pos:     retsetpvs(";\\[$*]", OP_POS);
12410     case KEY_splice:
12411         retsetpvs("+;$$@", OP_SPLICE);
12412     case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
12413         retsetpvs("", 0);
12414     case KEY_evalbytes:
12415         name = "entereval"; break;
12416     case KEY_readpipe:
12417         name = "backtick";
12418     }
12419
12420 #undef retsetpvs
12421
12422   findopnum:
12423     while (i < MAXO) {  /* The slow way. */
12424         if (strEQ(name, PL_op_name[i])
12425             || strEQ(name, PL_op_desc[i]))
12426         {
12427             if (nullret) { assert(opnum); *opnum = i; return NULL; }
12428             goto found;
12429         }
12430         i++;
12431     }
12432     return NULL;
12433   found:
12434     defgv = PL_opargs[i] & OA_DEFGV;
12435     oa = PL_opargs[i] >> OASHIFT;
12436     while (oa) {
12437         if (oa & OA_OPTIONAL && !seen_question && (
12438               !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
12439         )) {
12440             seen_question = 1;
12441             str[n++] = ';';
12442         }
12443         if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
12444             && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
12445             /* But globs are already references (kinda) */
12446             && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
12447         ) {
12448             str[n++] = '\\';
12449         }
12450         if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
12451          && !scalar_mod_type(NULL, i)) {
12452             str[n++] = '[';
12453             str[n++] = '$';
12454             str[n++] = '@';
12455             str[n++] = '%';
12456             if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
12457             str[n++] = '*';
12458             str[n++] = ']';
12459         }
12460         else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
12461         if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
12462             str[n-1] = '_'; defgv = 0;
12463         }
12464         oa = oa >> 4;
12465     }
12466     if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
12467     str[n++] = '\0';
12468     sv_setpvn(sv, str, n - 1);
12469     if (opnum) *opnum = i;
12470     return sv;
12471 }
12472
12473 OP *
12474 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
12475                       const int opnum)
12476 {
12477     OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
12478     OP *o;
12479
12480     PERL_ARGS_ASSERT_CORESUB_OP;
12481
12482     switch(opnum) {
12483     case 0:
12484         return op_append_elem(OP_LINESEQ,
12485                        argop,
12486                        newSLICEOP(0,
12487                                   newSVOP(OP_CONST, 0, newSViv(-code % 3)),
12488                                   newOP(OP_CALLER,0)
12489                        )
12490                );
12491     case OP_SELECT: /* which represents OP_SSELECT as well */
12492         if (code)
12493             return newCONDOP(
12494                          0,
12495                          newBINOP(OP_GT, 0,
12496                                   newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
12497                                   newSVOP(OP_CONST, 0, newSVuv(1))
12498                                  ),
12499                          coresub_op(newSVuv((UV)OP_SSELECT), 0,
12500                                     OP_SSELECT),
12501                          coresub_op(coreargssv, 0, OP_SELECT)
12502                    );
12503         /* FALL THROUGH */
12504     default:
12505         switch (PL_opargs[opnum] & OA_CLASS_MASK) {
12506         case OA_BASEOP:
12507             return op_append_elem(
12508                         OP_LINESEQ, argop,
12509                         newOP(opnum,
12510                               opnum == OP_WANTARRAY || opnum == OP_RUNCV
12511                                 ? OPpOFFBYONE << 8 : 0)
12512                    );
12513         case OA_BASEOP_OR_UNOP:
12514             if (opnum == OP_ENTEREVAL) {
12515                 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
12516                 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
12517             }
12518             else o = newUNOP(opnum,0,argop);
12519             if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
12520             else {
12521           onearg:
12522               if (is_handle_constructor(o, 1))
12523                 argop->op_private |= OPpCOREARGS_DEREF1;
12524               if (scalar_mod_type(NULL, opnum))
12525                 argop->op_private |= OPpCOREARGS_SCALARMOD;
12526             }
12527             return o;
12528         default:
12529             o = convert(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
12530             if (is_handle_constructor(o, 2))
12531                 argop->op_private |= OPpCOREARGS_DEREF2;
12532             if (opnum == OP_SUBSTR) {
12533                 o->op_private |= OPpMAYBE_LVSUB;
12534                 return o;
12535             }
12536             else goto onearg;
12537         }
12538     }
12539 }
12540
12541 void
12542 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
12543                                SV * const *new_const_svp)
12544 {
12545     const char *hvname;
12546     bool is_const = !!CvCONST(old_cv);
12547     SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
12548
12549     PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
12550
12551     if (is_const && new_const_svp && old_const_sv == *new_const_svp)
12552         return;
12553         /* They are 2 constant subroutines generated from
12554            the same constant. This probably means that
12555            they are really the "same" proxy subroutine
12556            instantiated in 2 places. Most likely this is
12557            when a constant is exported twice.  Don't warn.
12558         */
12559     if (
12560         (ckWARN(WARN_REDEFINE)
12561          && !(
12562                 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
12563              && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
12564              && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
12565                  strEQ(hvname, "autouse"))
12566              )
12567         )
12568      || (is_const
12569          && ckWARN_d(WARN_REDEFINE)
12570          && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
12571         )
12572     )
12573         Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
12574                           is_const
12575                             ? "Constant subroutine %"SVf" redefined"
12576                             : "Subroutine %"SVf" redefined",
12577                           name);
12578 }
12579
12580 /*
12581 =head1 Hook manipulation
12582
12583 These functions provide convenient and thread-safe means of manipulating
12584 hook variables.
12585
12586 =cut
12587 */
12588
12589 /*
12590 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
12591
12592 Puts a C function into the chain of check functions for a specified op
12593 type.  This is the preferred way to manipulate the L</PL_check> array.
12594 I<opcode> specifies which type of op is to be affected.  I<new_checker>
12595 is a pointer to the C function that is to be added to that opcode's
12596 check chain, and I<old_checker_p> points to the storage location where a
12597 pointer to the next function in the chain will be stored.  The value of
12598 I<new_pointer> is written into the L</PL_check> array, while the value
12599 previously stored there is written to I<*old_checker_p>.
12600
12601 The function should be defined like this:
12602
12603     static OP *new_checker(pTHX_ OP *op) { ... }
12604
12605 It is intended to be called in this manner:
12606
12607     new_checker(aTHX_ op)
12608
12609 I<old_checker_p> should be defined like this:
12610
12611     static Perl_check_t old_checker_p;
12612
12613 L</PL_check> is global to an entire process, and a module wishing to
12614 hook op checking may find itself invoked more than once per process,
12615 typically in different threads.  To handle that situation, this function
12616 is idempotent.  The location I<*old_checker_p> must initially (once
12617 per process) contain a null pointer.  A C variable of static duration
12618 (declared at file scope, typically also marked C<static> to give
12619 it internal linkage) will be implicitly initialised appropriately,
12620 if it does not have an explicit initialiser.  This function will only
12621 actually modify the check chain if it finds I<*old_checker_p> to be null.
12622 This function is also thread safe on the small scale.  It uses appropriate
12623 locking to avoid race conditions in accessing L</PL_check>.
12624
12625 When this function is called, the function referenced by I<new_checker>
12626 must be ready to be called, except for I<*old_checker_p> being unfilled.
12627 In a threading situation, I<new_checker> may be called immediately,
12628 even before this function has returned.  I<*old_checker_p> will always
12629 be appropriately set before I<new_checker> is called.  If I<new_checker>
12630 decides not to do anything special with an op that it is given (which
12631 is the usual case for most uses of op check hooking), it must chain the
12632 check function referenced by I<*old_checker_p>.
12633
12634 If you want to influence compilation of calls to a specific subroutine,
12635 then use L</cv_set_call_checker> rather than hooking checking of all
12636 C<entersub> ops.
12637
12638 =cut
12639 */
12640
12641 void
12642 Perl_wrap_op_checker(pTHX_ Optype opcode,
12643     Perl_check_t new_checker, Perl_check_t *old_checker_p)
12644 {
12645     dVAR;
12646
12647     PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
12648     if (*old_checker_p) return;
12649     OP_CHECK_MUTEX_LOCK;
12650     if (!*old_checker_p) {
12651         *old_checker_p = PL_check[opcode];
12652         PL_check[opcode] = new_checker;
12653     }
12654     OP_CHECK_MUTEX_UNLOCK;
12655 }
12656
12657 #include "XSUB.h"
12658
12659 /* Efficient sub that returns a constant scalar value. */
12660 static void
12661 const_sv_xsub(pTHX_ CV* cv)
12662 {
12663     dVAR;
12664     dXSARGS;
12665     SV *const sv = MUTABLE_SV(XSANY.any_ptr);
12666     PERL_UNUSED_ARG(items);
12667     if (!sv) {
12668         XSRETURN(0);
12669     }
12670     EXTEND(sp, 1);
12671     ST(0) = sv;
12672     XSRETURN(1);
12673 }
12674
12675 static void
12676 const_av_xsub(pTHX_ CV* cv)
12677 {
12678     dVAR;
12679     dXSARGS;
12680     AV * const av = MUTABLE_AV(XSANY.any_ptr);
12681     SP -= items;
12682     assert(av);
12683 #ifndef DEBUGGING
12684     if (!av) {
12685         XSRETURN(0);
12686     }
12687 #endif
12688     if (SvRMAGICAL(av))
12689         Perl_croak(aTHX_ "Magical list constants are not supported");
12690     if (GIMME_V != G_ARRAY) {
12691         EXTEND(SP, 1);
12692         ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
12693         XSRETURN(1);
12694     }
12695     EXTEND(SP, AvFILLp(av)+1);
12696     Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
12697     XSRETURN(AvFILLp(av)+1);
12698 }
12699
12700 /*
12701  * Local variables:
12702  * c-indentation-style: bsd
12703  * c-basic-offset: 4
12704  * indent-tabs-mode: nil
12705  * End:
12706  *
12707  * ex: set ts=8 sts=4 sw=4 et:
12708  */