This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regexp.h: Fix error check to use correct offset
[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
106 #define CALL_A_PEEP(peep, o) CALL_FPTR((peep)->fn)(aTHX_ o, peep)
107
108 #define CALL_PEEP(o)                            \
109     STMT_START {                                \
110         peep_next_t _next_peep;                 \
111         _next_peep.fn        = PL_peepp;        \
112         _next_peep.user_data = NULL;            \
113         CALL_A_PEEP(&_next_peep, o);            \
114     } STMT_END
115
116 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) CALL_FPTR(PL_opfreehook)(aTHX_ o)
117
118 #if defined(PL_OP_SLAB_ALLOC)
119
120 #ifdef PERL_DEBUG_READONLY_OPS
121 #  define PERL_SLAB_SIZE 4096
122 #  include <sys/mman.h>
123 #endif
124
125 #ifndef PERL_SLAB_SIZE
126 #define PERL_SLAB_SIZE 2048
127 #endif
128
129 void *
130 Perl_Slab_Alloc(pTHX_ size_t sz)
131 {
132     dVAR;
133     /*
134      * To make incrementing use count easy PL_OpSlab is an I32 *
135      * To make inserting the link to slab PL_OpPtr is I32 **
136      * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
137      * Add an overhead for pointer to slab and round up as a number of pointers
138      */
139     sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
140     if ((PL_OpSpace -= sz) < 0) {
141 #ifdef PERL_DEBUG_READONLY_OPS
142         /* We need to allocate chunk by chunk so that we can control the VM
143            mapping */
144         PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE,
145                         MAP_ANON|MAP_PRIVATE, -1, 0);
146
147         DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
148                               (unsigned long) PERL_SLAB_SIZE*sizeof(I32*),
149                               PL_OpPtr));
150         if(PL_OpPtr == MAP_FAILED) {
151             perror("mmap failed");
152             abort();
153         }
154 #else
155
156         PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*)); 
157 #endif
158         if (!PL_OpPtr) {
159             return NULL;
160         }
161         /* We reserve the 0'th I32 sized chunk as a use count */
162         PL_OpSlab = (I32 *) PL_OpPtr;
163         /* Reduce size by the use count word, and by the size we need.
164          * Latter is to mimic the '-=' in the if() above
165          */
166         PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
167         /* Allocation pointer starts at the top.
168            Theory: because we build leaves before trunk allocating at end
169            means that at run time access is cache friendly upward
170          */
171         PL_OpPtr += PERL_SLAB_SIZE;
172
173 #ifdef PERL_DEBUG_READONLY_OPS
174         /* We remember this slab.  */
175         /* This implementation isn't efficient, but it is simple. */
176         PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1));
177         PL_slabs[PL_slab_count++] = PL_OpSlab;
178         DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab));
179 #endif
180     }
181     assert( PL_OpSpace >= 0 );
182     /* Move the allocation pointer down */
183     PL_OpPtr   -= sz;
184     assert( PL_OpPtr > (I32 **) PL_OpSlab );
185     *PL_OpPtr   = PL_OpSlab;    /* Note which slab it belongs to */
186     (*PL_OpSlab)++;             /* Increment use count of slab */
187     assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
188     assert( *PL_OpSlab > 0 );
189     return (void *)(PL_OpPtr + 1);
190 }
191
192 #ifdef PERL_DEBUG_READONLY_OPS
193 void
194 Perl_pending_Slabs_to_ro(pTHX) {
195     /* Turn all the allocated op slabs read only.  */
196     U32 count = PL_slab_count;
197     I32 **const slabs = PL_slabs;
198
199     /* Reset the array of pending OP slabs, as we're about to turn this lot
200        read only. Also, do it ahead of the loop in case the warn triggers,
201        and a warn handler has an eval */
202
203     PL_slabs = NULL;
204     PL_slab_count = 0;
205
206     /* Force a new slab for any further allocation.  */
207     PL_OpSpace = 0;
208
209     while (count--) {
210         void *const start = slabs[count];
211         const size_t size = PERL_SLAB_SIZE* sizeof(I32*);
212         if(mprotect(start, size, PROT_READ)) {
213             Perl_warn(aTHX_ "mprotect for %p %lu failed with %d",
214                       start, (unsigned long) size, errno);
215         }
216     }
217
218     free(slabs);
219 }
220
221 STATIC void
222 S_Slab_to_rw(pTHX_ void *op)
223 {
224     I32 * const * const ptr = (I32 **) op;
225     I32 * const slab = ptr[-1];
226
227     PERL_ARGS_ASSERT_SLAB_TO_RW;
228
229     assert( ptr-1 > (I32 **) slab );
230     assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
231     assert( *slab > 0 );
232     if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) {
233         Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d",
234                   slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno);
235     }
236 }
237
238 OP *
239 Perl_op_refcnt_inc(pTHX_ OP *o)
240 {
241     if(o) {
242         Slab_to_rw(o);
243         ++o->op_targ;
244     }
245     return o;
246
247 }
248
249 PADOFFSET
250 Perl_op_refcnt_dec(pTHX_ OP *o)
251 {
252     PERL_ARGS_ASSERT_OP_REFCNT_DEC;
253     Slab_to_rw(o);
254     return --o->op_targ;
255 }
256 #else
257 #  define Slab_to_rw(op)
258 #endif
259
260 void
261 Perl_Slab_Free(pTHX_ void *op)
262 {
263     I32 * const * const ptr = (I32 **) op;
264     I32 * const slab = ptr[-1];
265     PERL_ARGS_ASSERT_SLAB_FREE;
266     assert( ptr-1 > (I32 **) slab );
267     assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
268     assert( *slab > 0 );
269     Slab_to_rw(op);
270     if (--(*slab) == 0) {
271 #  ifdef NETWARE
272 #    define PerlMemShared PerlMem
273 #  endif
274         
275 #ifdef PERL_DEBUG_READONLY_OPS
276         U32 count = PL_slab_count;
277         /* Need to remove this slab from our list of slabs */
278         if (count) {
279             while (count--) {
280                 if (PL_slabs[count] == slab) {
281                     dVAR;
282                     /* Found it. Move the entry at the end to overwrite it.  */
283                     DEBUG_m(PerlIO_printf(Perl_debug_log,
284                                           "Deallocate %p by moving %p from %lu to %lu\n",
285                                           PL_OpSlab,
286                                           PL_slabs[PL_slab_count - 1],
287                                           PL_slab_count, count));
288                     PL_slabs[count] = PL_slabs[--PL_slab_count];
289                     /* Could realloc smaller at this point, but probably not
290                        worth it.  */
291                     if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) {
292                         perror("munmap failed");
293                         abort();
294                     }
295                     break;
296                 }
297             }
298         }
299 #else
300     PerlMemShared_free(slab);
301 #endif
302         if (slab == PL_OpSlab) {
303             PL_OpSpace = 0;
304         }
305     }
306 }
307 #endif
308 /*
309  * In the following definition, the ", (OP*)0" is just to make the compiler
310  * think the expression is of the right type: croak actually does a Siglongjmp.
311  */
312 #define CHECKOP(type,o) \
313     ((PL_op_mask && PL_op_mask[type])                           \
314      ? ( op_free((OP*)o),                                       \
315          Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]),  \
316          (OP*)0 )                                               \
317      : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
318
319 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
320
321 STATIC const char*
322 S_gv_ename(pTHX_ GV *gv)
323 {
324     SV* const tmpsv = sv_newmortal();
325
326     PERL_ARGS_ASSERT_GV_ENAME;
327
328     gv_efullname3(tmpsv, gv, NULL);
329     return SvPV_nolen_const(tmpsv);
330 }
331
332 STATIC OP *
333 S_no_fh_allowed(pTHX_ OP *o)
334 {
335     PERL_ARGS_ASSERT_NO_FH_ALLOWED;
336
337     yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
338                  OP_DESC(o)));
339     return o;
340 }
341
342 STATIC OP *
343 S_too_few_arguments(pTHX_ OP *o, const char *name)
344 {
345     PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS;
346
347     yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
348     return o;
349 }
350
351 STATIC OP *
352 S_too_many_arguments(pTHX_ OP *o, const char *name)
353 {
354     PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS;
355
356     yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
357     return o;
358 }
359
360 STATIC void
361 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
362 {
363     PERL_ARGS_ASSERT_BAD_TYPE;
364
365     yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
366                  (int)n, name, t, OP_DESC(kid)));
367 }
368
369 STATIC void
370 S_no_bareword_allowed(pTHX_ const OP *o)
371 {
372     PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
373
374     if (PL_madskills)
375         return;         /* various ok barewords are hidden in extra OP_NULL */
376     qerror(Perl_mess(aTHX_
377                      "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
378                      SVfARG(cSVOPo_sv)));
379 }
380
381 /* "register" allocation */
382
383 PADOFFSET
384 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
385 {
386     dVAR;
387     PADOFFSET off;
388     const bool is_our = (PL_parser->in_my == KEY_our);
389
390     PERL_ARGS_ASSERT_ALLOCMY;
391
392     if (flags)
393         Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
394                    (UV)flags);
395
396     /* Until we're using the length for real, cross check that we're being
397        told the truth.  */
398     assert(strlen(name) == len);
399
400     /* complain about "my $<special_var>" etc etc */
401     if (len &&
402         !(is_our ||
403           isALPHA(name[1]) ||
404           (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
405           (name[1] == '_' && (*name == '$' || len > 2))))
406     {
407         /* name[2] is true if strlen(name) > 2  */
408         if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
409             yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
410                               name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
411                               PL_parser->in_my == KEY_state ? "state" : "my"));
412         } else {
413             yyerror(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
414                               PL_parser->in_my == KEY_state ? "state" : "my"));
415         }
416     }
417
418     /* allocate a spare slot and store the name in that slot */
419
420     off = pad_add_name(name, len,
421                        is_our ? padadd_OUR :
422                        PL_parser->in_my == KEY_state ? padadd_STATE : 0,
423                     PL_parser->in_my_stash,
424                     (is_our
425                         /* $_ is always in main::, even with our */
426                         ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
427                         : NULL
428                     )
429     );
430     /* anon sub prototypes contains state vars should always be cloned,
431      * otherwise the state var would be shared between anon subs */
432
433     if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
434         CvCLONE_on(PL_compcv);
435
436     return off;
437 }
438
439 /* free the body of an op without examining its contents.
440  * Always use this rather than FreeOp directly */
441
442 static void
443 S_op_destroy(pTHX_ OP *o)
444 {
445     if (o->op_latefree) {
446         o->op_latefreed = 1;
447         return;
448     }
449     FreeOp(o);
450 }
451
452 #ifdef USE_ITHREADS
453 #  define forget_pmop(a,b)      S_forget_pmop(aTHX_ a,b)
454 #else
455 #  define forget_pmop(a,b)      S_forget_pmop(aTHX_ a)
456 #endif
457
458 /* Destructor */
459
460 void
461 Perl_op_free(pTHX_ OP *o)
462 {
463     dVAR;
464     OPCODE type;
465
466     if (!o)
467         return;
468     if (o->op_latefreed) {
469         if (o->op_latefree)
470             return;
471         goto do_free;
472     }
473
474     type = o->op_type;
475     if (o->op_private & OPpREFCOUNTED) {
476         switch (type) {
477         case OP_LEAVESUB:
478         case OP_LEAVESUBLV:
479         case OP_LEAVEEVAL:
480         case OP_LEAVE:
481         case OP_SCOPE:
482         case OP_LEAVEWRITE:
483             {
484             PADOFFSET refcnt;
485             OP_REFCNT_LOCK;
486             refcnt = OpREFCNT_dec(o);
487             OP_REFCNT_UNLOCK;
488             if (refcnt) {
489                 /* Need to find and remove any pattern match ops from the list
490                    we maintain for reset().  */
491                 find_and_forget_pmops(o);
492                 return;
493             }
494             }
495             break;
496         default:
497             break;
498         }
499     }
500
501     /* Call the op_free hook if it has been set. Do it now so that it's called
502      * at the right time for refcounted ops, but still before all of the kids
503      * are freed. */
504     CALL_OPFREEHOOK(o);
505
506     if (o->op_flags & OPf_KIDS) {
507         register OP *kid, *nextkid;
508         for (kid = cUNOPo->op_first; kid; kid = nextkid) {
509             nextkid = kid->op_sibling; /* Get before next freeing kid */
510             op_free(kid);
511         }
512     }
513
514 #ifdef PERL_DEBUG_READONLY_OPS
515     Slab_to_rw(o);
516 #endif
517
518     /* COP* is not cleared by op_clear() so that we may track line
519      * numbers etc even after null() */
520     if (type == OP_NEXTSTATE || type == OP_DBSTATE
521             || (type == OP_NULL /* the COP might have been null'ed */
522                 && ((OPCODE)o->op_targ == OP_NEXTSTATE
523                     || (OPCODE)o->op_targ == OP_DBSTATE))) {
524         cop_free((COP*)o);
525     }
526
527     if (type == OP_NULL)
528         type = (OPCODE)o->op_targ;
529
530     op_clear(o);
531     if (o->op_latefree) {
532         o->op_latefreed = 1;
533         return;
534     }
535   do_free:
536     FreeOp(o);
537 #ifdef DEBUG_LEAKING_SCALARS
538     if (PL_op == o)
539         PL_op = NULL;
540 #endif
541 }
542
543 void
544 Perl_op_clear(pTHX_ OP *o)
545 {
546
547     dVAR;
548
549     PERL_ARGS_ASSERT_OP_CLEAR;
550
551 #ifdef PERL_MAD
552     /* if (o->op_madprop && o->op_madprop->mad_next)
553        abort(); */
554     /* FIXME for MAD - if I uncomment these two lines t/op/pack.t fails with
555        "modification of a read only value" for a reason I can't fathom why.
556        It's the "" stringification of $_, where $_ was set to '' in a foreach
557        loop, but it defies simplification into a small test case.
558        However, commenting them out has caused ext/List/Util/t/weak.t to fail
559        the last test.  */
560     /*
561       mad_free(o->op_madprop);
562       o->op_madprop = 0;
563     */
564 #endif    
565
566  retry:
567     switch (o->op_type) {
568     case OP_NULL:       /* Was holding old type, if any. */
569         if (PL_madskills && o->op_targ != OP_NULL) {
570             o->op_type = (Optype)o->op_targ;
571             o->op_targ = 0;
572             goto retry;
573         }
574     case OP_ENTERTRY:
575     case OP_ENTEREVAL:  /* Was holding hints. */
576         o->op_targ = 0;
577         break;
578     default:
579         if (!(o->op_flags & OPf_REF)
580             || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
581             break;
582         /* FALL THROUGH */
583     case OP_GVSV:
584     case OP_GV:
585     case OP_AELEMFAST:
586         if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
587             /* not an OP_PADAV replacement */
588             GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
589 #ifdef USE_ITHREADS
590                         && PL_curpad
591 #endif
592                         ? cGVOPo_gv : NULL;
593             /* It's possible during global destruction that the GV is freed
594                before the optree. Whilst the SvREFCNT_inc is happy to bump from
595                0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
596                will trigger an assertion failure, because the entry to sv_clear
597                checks that the scalar is not already freed.  A check of for
598                !SvIS_FREED(gv) turns out to be invalid, because during global
599                destruction the reference count can be forced down to zero
600                (with SVf_BREAK set).  In which case raising to 1 and then
601                dropping to 0 triggers cleanup before it should happen.  I
602                *think* that this might actually be a general, systematic,
603                weakness of the whole idea of SVf_BREAK, in that code *is*
604                allowed to raise and lower references during global destruction,
605                so any *valid* code that happens to do this during global
606                destruction might well trigger premature cleanup.  */
607             bool still_valid = gv && SvREFCNT(gv);
608
609             if (still_valid)
610                 SvREFCNT_inc_simple_void(gv);
611 #ifdef USE_ITHREADS
612             if (cPADOPo->op_padix > 0) {
613                 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
614                  * may still exist on the pad */
615                 pad_swipe(cPADOPo->op_padix, TRUE);
616                 cPADOPo->op_padix = 0;
617             }
618 #else
619             SvREFCNT_dec(cSVOPo->op_sv);
620             cSVOPo->op_sv = NULL;
621 #endif
622             if (still_valid) {
623                 int try_downgrade = SvREFCNT(gv) == 2;
624                 SvREFCNT_dec(gv);
625                 if (try_downgrade)
626                     gv_try_downgrade(gv);
627             }
628         }
629         break;
630     case OP_METHOD_NAMED:
631     case OP_CONST:
632     case OP_HINTSEVAL:
633         SvREFCNT_dec(cSVOPo->op_sv);
634         cSVOPo->op_sv = NULL;
635 #ifdef USE_ITHREADS
636         /** Bug #15654
637           Even if op_clear does a pad_free for the target of the op,
638           pad_free doesn't actually remove the sv that exists in the pad;
639           instead it lives on. This results in that it could be reused as 
640           a target later on when the pad was reallocated.
641         **/
642         if(o->op_targ) {
643           pad_swipe(o->op_targ,1);
644           o->op_targ = 0;
645         }
646 #endif
647         break;
648     case OP_GOTO:
649     case OP_NEXT:
650     case OP_LAST:
651     case OP_REDO:
652         if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
653             break;
654         /* FALL THROUGH */
655     case OP_TRANS:
656         if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
657 #ifdef USE_ITHREADS
658             if (cPADOPo->op_padix > 0) {
659                 pad_swipe(cPADOPo->op_padix, TRUE);
660                 cPADOPo->op_padix = 0;
661             }
662 #else
663             SvREFCNT_dec(cSVOPo->op_sv);
664             cSVOPo->op_sv = NULL;
665 #endif
666         }
667         else {
668             PerlMemShared_free(cPVOPo->op_pv);
669             cPVOPo->op_pv = NULL;
670         }
671         break;
672     case OP_SUBST:
673         op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
674         goto clear_pmop;
675     case OP_PUSHRE:
676 #ifdef USE_ITHREADS
677         if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
678             /* No GvIN_PAD_off here, because other references may still
679              * exist on the pad */
680             pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
681         }
682 #else
683         SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
684 #endif
685         /* FALL THROUGH */
686     case OP_MATCH:
687     case OP_QR:
688 clear_pmop:
689         forget_pmop(cPMOPo, 1);
690         cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
691         /* we use the same protection as the "SAFE" version of the PM_ macros
692          * here since sv_clean_all might release some PMOPs
693          * after PL_regex_padav has been cleared
694          * and the clearing of PL_regex_padav needs to
695          * happen before sv_clean_all
696          */
697 #ifdef USE_ITHREADS
698         if(PL_regex_pad) {        /* We could be in destruction */
699             const IV offset = (cPMOPo)->op_pmoffset;
700             ReREFCNT_dec(PM_GETRE(cPMOPo));
701             PL_regex_pad[offset] = &PL_sv_undef;
702             sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
703                            sizeof(offset));
704         }
705 #else
706         ReREFCNT_dec(PM_GETRE(cPMOPo));
707         PM_SETRE(cPMOPo, NULL);
708 #endif
709
710         break;
711     }
712
713     if (o->op_targ > 0) {
714         pad_free(o->op_targ);
715         o->op_targ = 0;
716     }
717 }
718
719 STATIC void
720 S_cop_free(pTHX_ COP* cop)
721 {
722     PERL_ARGS_ASSERT_COP_FREE;
723
724     CopFILE_free(cop);
725     CopSTASH_free(cop);
726     if (! specialWARN(cop->cop_warnings))
727         PerlMemShared_free(cop->cop_warnings);
728     Perl_refcounted_he_free(aTHX_ cop->cop_hints_hash);
729 }
730
731 STATIC void
732 S_forget_pmop(pTHX_ PMOP *const o
733 #ifdef USE_ITHREADS
734               , U32 flags
735 #endif
736               )
737 {
738     HV * const pmstash = PmopSTASH(o);
739
740     PERL_ARGS_ASSERT_FORGET_PMOP;
741
742     if (pmstash && !SvIS_FREED(pmstash)) {
743         MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
744         if (mg) {
745             PMOP **const array = (PMOP**) mg->mg_ptr;
746             U32 count = mg->mg_len / sizeof(PMOP**);
747             U32 i = count;
748
749             while (i--) {
750                 if (array[i] == o) {
751                     /* Found it. Move the entry at the end to overwrite it.  */
752                     array[i] = array[--count];
753                     mg->mg_len = count * sizeof(PMOP**);
754                     /* Could realloc smaller at this point always, but probably
755                        not worth it. Probably worth free()ing if we're the
756                        last.  */
757                     if(!count) {
758                         Safefree(mg->mg_ptr);
759                         mg->mg_ptr = NULL;
760                     }
761                     break;
762                 }
763             }
764         }
765     }
766     if (PL_curpm == o) 
767         PL_curpm = NULL;
768 #ifdef USE_ITHREADS
769     if (flags)
770         PmopSTASH_free(o);
771 #endif
772 }
773
774 STATIC void
775 S_find_and_forget_pmops(pTHX_ OP *o)
776 {
777     PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
778
779     if (o->op_flags & OPf_KIDS) {
780         OP *kid = cUNOPo->op_first;
781         while (kid) {
782             switch (kid->op_type) {
783             case OP_SUBST:
784             case OP_PUSHRE:
785             case OP_MATCH:
786             case OP_QR:
787                 forget_pmop((PMOP*)kid, 0);
788             }
789             find_and_forget_pmops(kid);
790             kid = kid->op_sibling;
791         }
792     }
793 }
794
795 void
796 Perl_op_null(pTHX_ OP *o)
797 {
798     dVAR;
799
800     PERL_ARGS_ASSERT_OP_NULL;
801
802     if (o->op_type == OP_NULL)
803         return;
804     if (!PL_madskills)
805         op_clear(o);
806     o->op_targ = o->op_type;
807     o->op_type = OP_NULL;
808     o->op_ppaddr = PL_ppaddr[OP_NULL];
809 }
810
811 void
812 Perl_op_refcnt_lock(pTHX)
813 {
814     dVAR;
815     PERL_UNUSED_CONTEXT;
816     OP_REFCNT_LOCK;
817 }
818
819 void
820 Perl_op_refcnt_unlock(pTHX)
821 {
822     dVAR;
823     PERL_UNUSED_CONTEXT;
824     OP_REFCNT_UNLOCK;
825 }
826
827 /* Contextualizers */
828
829 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
830
831 static OP *
832 S_linklist(pTHX_ OP *o)
833 {
834     OP *first;
835
836     PERL_ARGS_ASSERT_LINKLIST;
837
838     if (o->op_next)
839         return o->op_next;
840
841     /* establish postfix order */
842     first = cUNOPo->op_first;
843     if (first) {
844         register OP *kid;
845         o->op_next = LINKLIST(first);
846         kid = first;
847         for (;;) {
848             if (kid->op_sibling) {
849                 kid->op_next = LINKLIST(kid->op_sibling);
850                 kid = kid->op_sibling;
851             } else {
852                 kid->op_next = o;
853                 break;
854             }
855         }
856     }
857     else
858         o->op_next = o;
859
860     return o->op_next;
861 }
862
863 static OP *
864 S_scalarkids(pTHX_ OP *o)
865 {
866     if (o && o->op_flags & OPf_KIDS) {
867         OP *kid;
868         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
869             scalar(kid);
870     }
871     return o;
872 }
873
874 STATIC OP *
875 S_scalarboolean(pTHX_ OP *o)
876 {
877     dVAR;
878
879     PERL_ARGS_ASSERT_SCALARBOOLEAN;
880
881     if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
882         if (ckWARN(WARN_SYNTAX)) {
883             const line_t oldline = CopLINE(PL_curcop);
884
885             if (PL_parser && PL_parser->copline != NOLINE)
886                 CopLINE_set(PL_curcop, PL_parser->copline);
887             Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
888             CopLINE_set(PL_curcop, oldline);
889         }
890     }
891     return scalar(o);
892 }
893
894 OP *
895 Perl_scalar(pTHX_ OP *o)
896 {
897     dVAR;
898     OP *kid;
899
900     /* assumes no premature commitment */
901     if (!o || (PL_parser && PL_parser->error_count)
902          || (o->op_flags & OPf_WANT)
903          || o->op_type == OP_RETURN)
904     {
905         return o;
906     }
907
908     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
909
910     switch (o->op_type) {
911     case OP_REPEAT:
912         scalar(cBINOPo->op_first);
913         break;
914     case OP_OR:
915     case OP_AND:
916     case OP_COND_EXPR:
917         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
918             scalar(kid);
919         break;
920         /* FALL THROUGH */
921     case OP_SPLIT:
922     case OP_MATCH:
923     case OP_QR:
924     case OP_SUBST:
925     case OP_NULL:
926     default:
927         if (o->op_flags & OPf_KIDS) {
928             for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
929                 scalar(kid);
930         }
931         break;
932     case OP_LEAVE:
933     case OP_LEAVETRY:
934         kid = cLISTOPo->op_first;
935         scalar(kid);
936         kid = kid->op_sibling;
937     do_kids:
938         while (kid) {
939             OP *sib = kid->op_sibling;
940             if (sib && kid->op_type != OP_LEAVEWHEN) {
941                 if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) {
942                     scalar(kid);
943                     scalarvoid(sib);
944                     break;
945                 } else
946                     scalarvoid(kid);
947             } else
948                 scalar(kid);
949             kid = sib;
950         }
951         PL_curcop = &PL_compiling;
952         break;
953     case OP_SCOPE:
954     case OP_LINESEQ:
955     case OP_LIST:
956         kid = cLISTOPo->op_first;
957         goto do_kids;
958     case OP_SORT:
959         Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
960         break;
961     }
962     return o;
963 }
964
965 OP *
966 Perl_scalarvoid(pTHX_ OP *o)
967 {
968     dVAR;
969     OP *kid;
970     const char* useless = NULL;
971     SV* sv;
972     U8 want;
973
974     PERL_ARGS_ASSERT_SCALARVOID;
975
976     /* trailing mad null ops don't count as "there" for void processing */
977     if (PL_madskills &&
978         o->op_type != OP_NULL &&
979         o->op_sibling &&
980         o->op_sibling->op_type == OP_NULL)
981     {
982         OP *sib;
983         for (sib = o->op_sibling;
984                 sib && sib->op_type == OP_NULL;
985                 sib = sib->op_sibling) ;
986         
987         if (!sib)
988             return o;
989     }
990
991     if (o->op_type == OP_NEXTSTATE
992         || o->op_type == OP_DBSTATE
993         || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
994                                       || o->op_targ == OP_DBSTATE)))
995         PL_curcop = (COP*)o;            /* for warning below */
996
997     /* assumes no premature commitment */
998     want = o->op_flags & OPf_WANT;
999     if ((want && want != OPf_WANT_SCALAR)
1000          || (PL_parser && PL_parser->error_count)
1001          || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1002     {
1003         return o;
1004     }
1005
1006     if ((o->op_private & OPpTARGET_MY)
1007         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1008     {
1009         return scalar(o);                       /* As if inside SASSIGN */
1010     }
1011
1012     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1013
1014     switch (o->op_type) {
1015     default:
1016         if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1017             break;
1018         /* FALL THROUGH */
1019     case OP_REPEAT:
1020         if (o->op_flags & OPf_STACKED)
1021             break;
1022         goto func_ops;
1023     case OP_SUBSTR:
1024         if (o->op_private == 4)
1025             break;
1026         /* FALL THROUGH */
1027     case OP_GVSV:
1028     case OP_WANTARRAY:
1029     case OP_GV:
1030     case OP_SMARTMATCH:
1031     case OP_PADSV:
1032     case OP_PADAV:
1033     case OP_PADHV:
1034     case OP_PADANY:
1035     case OP_AV2ARYLEN:
1036     case OP_REF:
1037     case OP_REFGEN:
1038     case OP_SREFGEN:
1039     case OP_DEFINED:
1040     case OP_HEX:
1041     case OP_OCT:
1042     case OP_LENGTH:
1043     case OP_VEC:
1044     case OP_INDEX:
1045     case OP_RINDEX:
1046     case OP_SPRINTF:
1047     case OP_AELEM:
1048     case OP_AELEMFAST:
1049     case OP_ASLICE:
1050     case OP_HELEM:
1051     case OP_HSLICE:
1052     case OP_UNPACK:
1053     case OP_PACK:
1054     case OP_JOIN:
1055     case OP_LSLICE:
1056     case OP_ANONLIST:
1057     case OP_ANONHASH:
1058     case OP_SORT:
1059     case OP_REVERSE:
1060     case OP_RANGE:
1061     case OP_FLIP:
1062     case OP_FLOP:
1063     case OP_CALLER:
1064     case OP_FILENO:
1065     case OP_EOF:
1066     case OP_TELL:
1067     case OP_GETSOCKNAME:
1068     case OP_GETPEERNAME:
1069     case OP_READLINK:
1070     case OP_TELLDIR:
1071     case OP_GETPPID:
1072     case OP_GETPGRP:
1073     case OP_GETPRIORITY:
1074     case OP_TIME:
1075     case OP_TMS:
1076     case OP_LOCALTIME:
1077     case OP_GMTIME:
1078     case OP_GHBYNAME:
1079     case OP_GHBYADDR:
1080     case OP_GHOSTENT:
1081     case OP_GNBYNAME:
1082     case OP_GNBYADDR:
1083     case OP_GNETENT:
1084     case OP_GPBYNAME:
1085     case OP_GPBYNUMBER:
1086     case OP_GPROTOENT:
1087     case OP_GSBYNAME:
1088     case OP_GSBYPORT:
1089     case OP_GSERVENT:
1090     case OP_GPWNAM:
1091     case OP_GPWUID:
1092     case OP_GGRNAM:
1093     case OP_GGRGID:
1094     case OP_GETLOGIN:
1095     case OP_PROTOTYPE:
1096       func_ops:
1097         if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1098             /* Otherwise it's "Useless use of grep iterator" */
1099             useless = OP_DESC(o);
1100         break;
1101
1102     case OP_SPLIT:
1103         kid = cLISTOPo->op_first;
1104         if (kid && kid->op_type == OP_PUSHRE
1105 #ifdef USE_ITHREADS
1106                 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1107 #else
1108                 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1109 #endif
1110             useless = OP_DESC(o);
1111         break;
1112
1113     case OP_NOT:
1114        kid = cUNOPo->op_first;
1115        if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1116            kid->op_type != OP_TRANS) {
1117                 goto func_ops;
1118        }
1119        useless = "negative pattern binding (!~)";
1120        break;
1121
1122     case OP_SUBST:
1123         if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1124             useless = "Non-destructive substitution (s///r)";
1125         break;
1126
1127     case OP_RV2GV:
1128     case OP_RV2SV:
1129     case OP_RV2AV:
1130     case OP_RV2HV:
1131         if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1132                 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1133             useless = "a variable";
1134         break;
1135
1136     case OP_CONST:
1137         sv = cSVOPo_sv;
1138         if (cSVOPo->op_private & OPpCONST_STRICT)
1139             no_bareword_allowed(o);
1140         else {
1141             if (ckWARN(WARN_VOID)) {
1142                 if (SvOK(sv)) {
1143                     SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1144                                 "a constant (%"SVf")", sv));
1145                     useless = SvPV_nolen(msv);
1146                 }
1147                 else
1148                     useless = "a constant (undef)";
1149                 if (o->op_private & OPpCONST_ARYBASE)
1150                     useless = NULL;
1151                 /* don't warn on optimised away booleans, eg 
1152                  * use constant Foo, 5; Foo || print; */
1153                 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1154                     useless = NULL;
1155                 /* the constants 0 and 1 are permitted as they are
1156                    conventionally used as dummies in constructs like
1157                         1 while some_condition_with_side_effects;  */
1158                 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1159                     useless = NULL;
1160                 else if (SvPOK(sv)) {
1161                   /* perl4's way of mixing documentation and code
1162                      (before the invention of POD) was based on a
1163                      trick to mix nroff and perl code. The trick was
1164                      built upon these three nroff macros being used in
1165                      void context. The pink camel has the details in
1166                      the script wrapman near page 319. */
1167                     const char * const maybe_macro = SvPVX_const(sv);
1168                     if (strnEQ(maybe_macro, "di", 2) ||
1169                         strnEQ(maybe_macro, "ds", 2) ||
1170                         strnEQ(maybe_macro, "ig", 2))
1171                             useless = NULL;
1172                 }
1173             }
1174         }
1175         op_null(o);             /* don't execute or even remember it */
1176         break;
1177
1178     case OP_POSTINC:
1179         o->op_type = OP_PREINC;         /* pre-increment is faster */
1180         o->op_ppaddr = PL_ppaddr[OP_PREINC];
1181         break;
1182
1183     case OP_POSTDEC:
1184         o->op_type = OP_PREDEC;         /* pre-decrement is faster */
1185         o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1186         break;
1187
1188     case OP_I_POSTINC:
1189         o->op_type = OP_I_PREINC;       /* pre-increment is faster */
1190         o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1191         break;
1192
1193     case OP_I_POSTDEC:
1194         o->op_type = OP_I_PREDEC;       /* pre-decrement is faster */
1195         o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1196         break;
1197
1198     case OP_OR:
1199     case OP_AND:
1200         kid = cLOGOPo->op_first;
1201         if (kid->op_type == OP_NOT
1202             && (kid->op_flags & OPf_KIDS)
1203             && !PL_madskills) {
1204             if (o->op_type == OP_AND) {
1205                 o->op_type = OP_OR;
1206                 o->op_ppaddr = PL_ppaddr[OP_OR];
1207             } else {
1208                 o->op_type = OP_AND;
1209                 o->op_ppaddr = PL_ppaddr[OP_AND];
1210             }
1211             op_null(kid);
1212         }
1213
1214     case OP_DOR:
1215     case OP_COND_EXPR:
1216     case OP_ENTERGIVEN:
1217     case OP_ENTERWHEN:
1218         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1219             scalarvoid(kid);
1220         break;
1221
1222     case OP_NULL:
1223         if (o->op_flags & OPf_STACKED)
1224             break;
1225         /* FALL THROUGH */
1226     case OP_NEXTSTATE:
1227     case OP_DBSTATE:
1228     case OP_ENTERTRY:
1229     case OP_ENTER:
1230         if (!(o->op_flags & OPf_KIDS))
1231             break;
1232         /* FALL THROUGH */
1233     case OP_SCOPE:
1234     case OP_LEAVE:
1235     case OP_LEAVETRY:
1236     case OP_LEAVELOOP:
1237     case OP_LINESEQ:
1238     case OP_LIST:
1239     case OP_LEAVEGIVEN:
1240     case OP_LEAVEWHEN:
1241         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1242             scalarvoid(kid);
1243         break;
1244     case OP_ENTEREVAL:
1245         scalarkids(o);
1246         break;
1247     case OP_SCALAR:
1248         return scalar(o);
1249     }
1250     if (useless)
1251         Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
1252     return o;
1253 }
1254
1255 static OP *
1256 S_listkids(pTHX_ OP *o)
1257 {
1258     if (o && o->op_flags & OPf_KIDS) {
1259         OP *kid;
1260         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1261             list(kid);
1262     }
1263     return o;
1264 }
1265
1266 OP *
1267 Perl_list(pTHX_ OP *o)
1268 {
1269     dVAR;
1270     OP *kid;
1271
1272     /* assumes no premature commitment */
1273     if (!o || (o->op_flags & OPf_WANT)
1274          || (PL_parser && PL_parser->error_count)
1275          || o->op_type == OP_RETURN)
1276     {
1277         return o;
1278     }
1279
1280     if ((o->op_private & OPpTARGET_MY)
1281         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1282     {
1283         return o;                               /* As if inside SASSIGN */
1284     }
1285
1286     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1287
1288     switch (o->op_type) {
1289     case OP_FLOP:
1290     case OP_REPEAT:
1291         list(cBINOPo->op_first);
1292         break;
1293     case OP_OR:
1294     case OP_AND:
1295     case OP_COND_EXPR:
1296         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1297             list(kid);
1298         break;
1299     default:
1300     case OP_MATCH:
1301     case OP_QR:
1302     case OP_SUBST:
1303     case OP_NULL:
1304         if (!(o->op_flags & OPf_KIDS))
1305             break;
1306         if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1307             list(cBINOPo->op_first);
1308             return gen_constant_list(o);
1309         }
1310     case OP_LIST:
1311         listkids(o);
1312         break;
1313     case OP_LEAVE:
1314     case OP_LEAVETRY:
1315         kid = cLISTOPo->op_first;
1316         list(kid);
1317         kid = kid->op_sibling;
1318     do_kids:
1319         while (kid) {
1320             OP *sib = kid->op_sibling;
1321             if (sib && kid->op_type != OP_LEAVEWHEN) {
1322                 if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) {
1323                     list(kid);
1324                     scalarvoid(sib);
1325                     break;
1326                 } else
1327                     scalarvoid(kid);
1328             } else
1329                 list(kid);
1330             kid = sib;
1331         }
1332         PL_curcop = &PL_compiling;
1333         break;
1334     case OP_SCOPE:
1335     case OP_LINESEQ:
1336         kid = cLISTOPo->op_first;
1337         goto do_kids;
1338     }
1339     return o;
1340 }
1341
1342 static OP *
1343 S_scalarseq(pTHX_ OP *o)
1344 {
1345     dVAR;
1346     if (o) {
1347         const OPCODE type = o->op_type;
1348
1349         if (type == OP_LINESEQ || type == OP_SCOPE ||
1350             type == OP_LEAVE || type == OP_LEAVETRY)
1351         {
1352             OP *kid;
1353             for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1354                 if (kid->op_sibling) {
1355                     scalarvoid(kid);
1356                 }
1357             }
1358             PL_curcop = &PL_compiling;
1359         }
1360         o->op_flags &= ~OPf_PARENS;
1361         if (PL_hints & HINT_BLOCK_SCOPE)
1362             o->op_flags |= OPf_PARENS;
1363     }
1364     else
1365         o = newOP(OP_STUB, 0);
1366     return o;
1367 }
1368
1369 STATIC OP *
1370 S_modkids(pTHX_ OP *o, I32 type)
1371 {
1372     if (o && o->op_flags & OPf_KIDS) {
1373         OP *kid;
1374         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1375             mod(kid, type);
1376     }
1377     return o;
1378 }
1379
1380 /* Propagate lvalue ("modifiable") context to an op and its children.
1381  * 'type' represents the context type, roughly based on the type of op that
1382  * would do the modifying, although local() is represented by OP_NULL.
1383  * It's responsible for detecting things that can't be modified,  flag
1384  * things that need to behave specially in an lvalue context (e.g., "$$x = 5"
1385  * might have to vivify a reference in $x), and so on.
1386  *
1387  * For example, "$a+1 = 2" would cause mod() to be called with o being
1388  * OP_ADD and type being OP_SASSIGN, and would output an error.
1389  */
1390
1391 OP *
1392 Perl_mod(pTHX_ OP *o, I32 type)
1393 {
1394     dVAR;
1395     OP *kid;
1396     /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1397     int localize = -1;
1398
1399     if (!o || (PL_parser && PL_parser->error_count))
1400         return o;
1401
1402     if ((o->op_private & OPpTARGET_MY)
1403         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1404     {
1405         return o;
1406     }
1407
1408     switch (o->op_type) {
1409     case OP_UNDEF:
1410         localize = 0;
1411         PL_modcount++;
1412         return o;
1413     case OP_CONST:
1414         if (!(o->op_private & OPpCONST_ARYBASE))
1415             goto nomod;
1416         localize = 0;
1417         if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1418             CopARYBASE_set(&PL_compiling,
1419                            (I32)SvIV(cSVOPx(PL_eval_start)->op_sv));
1420             PL_eval_start = 0;
1421         }
1422         else if (!type) {
1423             SAVECOPARYBASE(&PL_compiling);
1424             CopARYBASE_set(&PL_compiling, 0);
1425         }
1426         else if (type == OP_REFGEN)
1427             goto nomod;
1428         else
1429             Perl_croak(aTHX_ "That use of $[ is unsupported");
1430         break;
1431     case OP_STUB:
1432         if ((o->op_flags & OPf_PARENS) || PL_madskills)
1433             break;
1434         goto nomod;
1435     case OP_ENTERSUB:
1436         if ((type == OP_UNDEF || type == OP_REFGEN) &&
1437             !(o->op_flags & OPf_STACKED)) {
1438             o->op_type = OP_RV2CV;              /* entersub => rv2cv */
1439             /* The default is to set op_private to the number of children,
1440                which for a UNOP such as RV2CV is always 1. And w're using
1441                the bit for a flag in RV2CV, so we need it clear.  */
1442             o->op_private &= ~1;
1443             o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1444             assert(cUNOPo->op_first->op_type == OP_NULL);
1445             op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1446             break;
1447         }
1448         else if (o->op_private & OPpENTERSUB_NOMOD)
1449             return o;
1450         else {                          /* lvalue subroutine call */
1451             o->op_private |= OPpLVAL_INTRO;
1452             PL_modcount = RETURN_UNLIMITED_NUMBER;
1453             if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1454                 /* Backward compatibility mode: */
1455                 o->op_private |= OPpENTERSUB_INARGS;
1456                 break;
1457             }
1458             else {                      /* Compile-time error message: */
1459                 OP *kid = cUNOPo->op_first;
1460                 CV *cv;
1461                 OP *okid;
1462
1463                 if (kid->op_type != OP_PUSHMARK) {
1464                     if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1465                         Perl_croak(aTHX_
1466                                 "panic: unexpected lvalue entersub "
1467                                 "args: type/targ %ld:%"UVuf,
1468                                 (long)kid->op_type, (UV)kid->op_targ);
1469                     kid = kLISTOP->op_first;
1470                 }
1471                 while (kid->op_sibling)
1472                     kid = kid->op_sibling;
1473                 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1474                     /* Indirect call */
1475                     if (kid->op_type == OP_METHOD_NAMED
1476                         || kid->op_type == OP_METHOD)
1477                     {
1478                         UNOP *newop;
1479
1480                         NewOp(1101, newop, 1, UNOP);
1481                         newop->op_type = OP_RV2CV;
1482                         newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1483                         newop->op_first = NULL;
1484                         newop->op_next = (OP*)newop;
1485                         kid->op_sibling = (OP*)newop;
1486                         newop->op_private |= OPpLVAL_INTRO;
1487                         newop->op_private &= ~1;
1488                         break;
1489                     }
1490
1491                     if (kid->op_type != OP_RV2CV)
1492                         Perl_croak(aTHX_
1493                                    "panic: unexpected lvalue entersub "
1494                                    "entry via type/targ %ld:%"UVuf,
1495                                    (long)kid->op_type, (UV)kid->op_targ);
1496                     kid->op_private |= OPpLVAL_INTRO;
1497                     break;      /* Postpone until runtime */
1498                 }
1499
1500                 okid = kid;
1501                 kid = kUNOP->op_first;
1502                 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1503                     kid = kUNOP->op_first;
1504                 if (kid->op_type == OP_NULL)
1505                     Perl_croak(aTHX_
1506                                "Unexpected constant lvalue entersub "
1507                                "entry via type/targ %ld:%"UVuf,
1508                                (long)kid->op_type, (UV)kid->op_targ);
1509                 if (kid->op_type != OP_GV) {
1510                     /* Restore RV2CV to check lvalueness */
1511                   restore_2cv:
1512                     if (kid->op_next && kid->op_next != kid) { /* Happens? */
1513                         okid->op_next = kid->op_next;
1514                         kid->op_next = okid;
1515                     }
1516                     else
1517                         okid->op_next = NULL;
1518                     okid->op_type = OP_RV2CV;
1519                     okid->op_targ = 0;
1520                     okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1521                     okid->op_private |= OPpLVAL_INTRO;
1522                     okid->op_private &= ~1;
1523                     break;
1524                 }
1525
1526                 cv = GvCV(kGVOP_gv);
1527                 if (!cv)
1528                     goto restore_2cv;
1529                 if (CvLVALUE(cv))
1530                     break;
1531             }
1532         }
1533         /* FALL THROUGH */
1534     default:
1535       nomod:
1536         /* grep, foreach, subcalls, refgen */
1537         if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1538             break;
1539         yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1540                      (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1541                       ? "do block"
1542                       : (o->op_type == OP_ENTERSUB
1543                         ? "non-lvalue subroutine call"
1544                         : OP_DESC(o))),
1545                      type ? PL_op_desc[type] : "local"));
1546         return o;
1547
1548     case OP_PREINC:
1549     case OP_PREDEC:
1550     case OP_POW:
1551     case OP_MULTIPLY:
1552     case OP_DIVIDE:
1553     case OP_MODULO:
1554     case OP_REPEAT:
1555     case OP_ADD:
1556     case OP_SUBTRACT:
1557     case OP_CONCAT:
1558     case OP_LEFT_SHIFT:
1559     case OP_RIGHT_SHIFT:
1560     case OP_BIT_AND:
1561     case OP_BIT_XOR:
1562     case OP_BIT_OR:
1563     case OP_I_MULTIPLY:
1564     case OP_I_DIVIDE:
1565     case OP_I_MODULO:
1566     case OP_I_ADD:
1567     case OP_I_SUBTRACT:
1568         if (!(o->op_flags & OPf_STACKED))
1569             goto nomod;
1570         PL_modcount++;
1571         break;
1572
1573     case OP_COND_EXPR:
1574         localize = 1;
1575         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1576             mod(kid, type);
1577         break;
1578
1579     case OP_RV2AV:
1580     case OP_RV2HV:
1581         if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1582            PL_modcount = RETURN_UNLIMITED_NUMBER;
1583             return o;           /* Treat \(@foo) like ordinary list. */
1584         }
1585         /* FALL THROUGH */
1586     case OP_RV2GV:
1587         if (scalar_mod_type(o, type))
1588             goto nomod;
1589         ref(cUNOPo->op_first, o->op_type);
1590         /* FALL THROUGH */
1591     case OP_ASLICE:
1592     case OP_HSLICE:
1593         if (type == OP_LEAVESUBLV)
1594             o->op_private |= OPpMAYBE_LVSUB;
1595         localize = 1;
1596         /* FALL THROUGH */
1597     case OP_AASSIGN:
1598     case OP_NEXTSTATE:
1599     case OP_DBSTATE:
1600        PL_modcount = RETURN_UNLIMITED_NUMBER;
1601         break;
1602     case OP_AV2ARYLEN:
1603         PL_hints |= HINT_BLOCK_SCOPE;
1604         if (type == OP_LEAVESUBLV)
1605             o->op_private |= OPpMAYBE_LVSUB;
1606         PL_modcount++;
1607         break;
1608     case OP_RV2SV:
1609         ref(cUNOPo->op_first, o->op_type);
1610         localize = 1;
1611         /* FALL THROUGH */
1612     case OP_GV:
1613         PL_hints |= HINT_BLOCK_SCOPE;
1614     case OP_SASSIGN:
1615     case OP_ANDASSIGN:
1616     case OP_ORASSIGN:
1617     case OP_DORASSIGN:
1618         PL_modcount++;
1619         break;
1620
1621     case OP_AELEMFAST:
1622         localize = -1;
1623         PL_modcount++;
1624         break;
1625
1626     case OP_PADAV:
1627     case OP_PADHV:
1628        PL_modcount = RETURN_UNLIMITED_NUMBER;
1629         if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1630             return o;           /* Treat \(@foo) like ordinary list. */
1631         if (scalar_mod_type(o, type))
1632             goto nomod;
1633         if (type == OP_LEAVESUBLV)
1634             o->op_private |= OPpMAYBE_LVSUB;
1635         /* FALL THROUGH */
1636     case OP_PADSV:
1637         PL_modcount++;
1638         if (!type) /* local() */
1639             Perl_croak(aTHX_ "Can't localize lexical variable %s",
1640                  PAD_COMPNAME_PV(o->op_targ));
1641         break;
1642
1643     case OP_PUSHMARK:
1644         localize = 0;
1645         break;
1646
1647     case OP_KEYS:
1648         if (type != OP_SASSIGN)
1649             goto nomod;
1650         goto lvalue_func;
1651     case OP_SUBSTR:
1652         if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1653             goto nomod;
1654         /* FALL THROUGH */
1655     case OP_POS:
1656     case OP_VEC:
1657         if (type == OP_LEAVESUBLV)
1658             o->op_private |= OPpMAYBE_LVSUB;
1659       lvalue_func:
1660         pad_free(o->op_targ);
1661         o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1662         assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1663         if (o->op_flags & OPf_KIDS)
1664             mod(cBINOPo->op_first->op_sibling, type);
1665         break;
1666
1667     case OP_AELEM:
1668     case OP_HELEM:
1669         ref(cBINOPo->op_first, o->op_type);
1670         if (type == OP_ENTERSUB &&
1671              !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1672             o->op_private |= OPpLVAL_DEFER;
1673         if (type == OP_LEAVESUBLV)
1674             o->op_private |= OPpMAYBE_LVSUB;
1675         localize = 1;
1676         PL_modcount++;
1677         break;
1678
1679     case OP_SCOPE:
1680     case OP_LEAVE:
1681     case OP_ENTER:
1682     case OP_LINESEQ:
1683         localize = 0;
1684         if (o->op_flags & OPf_KIDS)
1685             mod(cLISTOPo->op_last, type);
1686         break;
1687
1688     case OP_NULL:
1689         localize = 0;
1690         if (o->op_flags & OPf_SPECIAL)          /* do BLOCK */
1691             goto nomod;
1692         else if (!(o->op_flags & OPf_KIDS))
1693             break;
1694         if (o->op_targ != OP_LIST) {
1695             mod(cBINOPo->op_first, type);
1696             break;
1697         }
1698         /* FALL THROUGH */
1699     case OP_LIST:
1700         localize = 0;
1701         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1702             mod(kid, type);
1703         break;
1704
1705     case OP_RETURN:
1706         if (type != OP_LEAVESUBLV)
1707             goto nomod;
1708         break; /* mod()ing was handled by ck_return() */
1709     }
1710
1711     /* [20011101.069] File test operators interpret OPf_REF to mean that
1712        their argument is a filehandle; thus \stat(".") should not set
1713        it. AMS 20011102 */
1714     if (type == OP_REFGEN &&
1715         PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1716         return o;
1717
1718     if (type != OP_LEAVESUBLV)
1719         o->op_flags |= OPf_MOD;
1720
1721     if (type == OP_AASSIGN || type == OP_SASSIGN)
1722         o->op_flags |= OPf_SPECIAL|OPf_REF;
1723     else if (!type) { /* local() */
1724         switch (localize) {
1725         case 1:
1726             o->op_private |= OPpLVAL_INTRO;
1727             o->op_flags &= ~OPf_SPECIAL;
1728             PL_hints |= HINT_BLOCK_SCOPE;
1729             break;
1730         case 0:
1731             break;
1732         case -1:
1733             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
1734                            "Useless localization of %s", OP_DESC(o));
1735         }
1736     }
1737     else if (type != OP_GREPSTART && type != OP_ENTERSUB
1738              && type != OP_LEAVESUBLV)
1739         o->op_flags |= OPf_REF;
1740     return o;
1741 }
1742
1743 STATIC bool
1744 S_scalar_mod_type(const OP *o, I32 type)
1745 {
1746     PERL_ARGS_ASSERT_SCALAR_MOD_TYPE;
1747
1748     switch (type) {
1749     case OP_SASSIGN:
1750         if (o->op_type == OP_RV2GV)
1751             return FALSE;
1752         /* FALL THROUGH */
1753     case OP_PREINC:
1754     case OP_PREDEC:
1755     case OP_POSTINC:
1756     case OP_POSTDEC:
1757     case OP_I_PREINC:
1758     case OP_I_PREDEC:
1759     case OP_I_POSTINC:
1760     case OP_I_POSTDEC:
1761     case OP_POW:
1762     case OP_MULTIPLY:
1763     case OP_DIVIDE:
1764     case OP_MODULO:
1765     case OP_REPEAT:
1766     case OP_ADD:
1767     case OP_SUBTRACT:
1768     case OP_I_MULTIPLY:
1769     case OP_I_DIVIDE:
1770     case OP_I_MODULO:
1771     case OP_I_ADD:
1772     case OP_I_SUBTRACT:
1773     case OP_LEFT_SHIFT:
1774     case OP_RIGHT_SHIFT:
1775     case OP_BIT_AND:
1776     case OP_BIT_XOR:
1777     case OP_BIT_OR:
1778     case OP_CONCAT:
1779     case OP_SUBST:
1780     case OP_TRANS:
1781     case OP_READ:
1782     case OP_SYSREAD:
1783     case OP_RECV:
1784     case OP_ANDASSIGN:
1785     case OP_ORASSIGN:
1786     case OP_DORASSIGN:
1787         return TRUE;
1788     default:
1789         return FALSE;
1790     }
1791 }
1792
1793 STATIC bool
1794 S_is_handle_constructor(const OP *o, I32 numargs)
1795 {
1796     PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
1797
1798     switch (o->op_type) {
1799     case OP_PIPE_OP:
1800     case OP_SOCKPAIR:
1801         if (numargs == 2)
1802             return TRUE;
1803         /* FALL THROUGH */
1804     case OP_SYSOPEN:
1805     case OP_OPEN:
1806     case OP_SELECT:             /* XXX c.f. SelectSaver.pm */
1807     case OP_SOCKET:
1808     case OP_OPEN_DIR:
1809     case OP_ACCEPT:
1810         if (numargs == 1)
1811             return TRUE;
1812         /* FALLTHROUGH */
1813     default:
1814         return FALSE;
1815     }
1816 }
1817
1818 static OP *
1819 S_refkids(pTHX_ OP *o, I32 type)
1820 {
1821     if (o && o->op_flags & OPf_KIDS) {
1822         OP *kid;
1823         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1824             ref(kid, type);
1825     }
1826     return o;
1827 }
1828
1829 OP *
1830 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
1831 {
1832     dVAR;
1833     OP *kid;
1834
1835     PERL_ARGS_ASSERT_DOREF;
1836
1837     if (!o || (PL_parser && PL_parser->error_count))
1838         return o;
1839
1840     switch (o->op_type) {
1841     case OP_ENTERSUB:
1842         if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1843             !(o->op_flags & OPf_STACKED)) {
1844             o->op_type = OP_RV2CV;             /* entersub => rv2cv */
1845             o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1846             assert(cUNOPo->op_first->op_type == OP_NULL);
1847             op_null(((LISTOP*)cUNOPo->op_first)->op_first);     /* disable pushmark */
1848             o->op_flags |= OPf_SPECIAL;
1849             o->op_private &= ~1;
1850         }
1851         break;
1852
1853     case OP_COND_EXPR:
1854         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1855             doref(kid, type, set_op_ref);
1856         break;
1857     case OP_RV2SV:
1858         if (type == OP_DEFINED)
1859             o->op_flags |= OPf_SPECIAL;         /* don't create GV */
1860         doref(cUNOPo->op_first, o->op_type, set_op_ref);
1861         /* FALL THROUGH */
1862     case OP_PADSV:
1863         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1864             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1865                               : type == OP_RV2HV ? OPpDEREF_HV
1866                               : OPpDEREF_SV);
1867             o->op_flags |= OPf_MOD;
1868         }
1869         break;
1870
1871     case OP_RV2AV:
1872     case OP_RV2HV:
1873         if (set_op_ref)
1874             o->op_flags |= OPf_REF;
1875         /* FALL THROUGH */
1876     case OP_RV2GV:
1877         if (type == OP_DEFINED)
1878             o->op_flags |= OPf_SPECIAL;         /* don't create GV */
1879         doref(cUNOPo->op_first, o->op_type, set_op_ref);
1880         break;
1881
1882     case OP_PADAV:
1883     case OP_PADHV:
1884         if (set_op_ref)
1885             o->op_flags |= OPf_REF;
1886         break;
1887
1888     case OP_SCALAR:
1889     case OP_NULL:
1890         if (!(o->op_flags & OPf_KIDS))
1891             break;
1892         doref(cBINOPo->op_first, type, set_op_ref);
1893         break;
1894     case OP_AELEM:
1895     case OP_HELEM:
1896         doref(cBINOPo->op_first, o->op_type, set_op_ref);
1897         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1898             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1899                               : type == OP_RV2HV ? OPpDEREF_HV
1900                               : OPpDEREF_SV);
1901             o->op_flags |= OPf_MOD;
1902         }
1903         break;
1904
1905     case OP_SCOPE:
1906     case OP_LEAVE:
1907         set_op_ref = FALSE;
1908         /* FALL THROUGH */
1909     case OP_ENTER:
1910     case OP_LIST:
1911         if (!(o->op_flags & OPf_KIDS))
1912             break;
1913         doref(cLISTOPo->op_last, type, set_op_ref);
1914         break;
1915     default:
1916         break;
1917     }
1918     return scalar(o);
1919
1920 }
1921
1922 STATIC OP *
1923 S_dup_attrlist(pTHX_ OP *o)
1924 {
1925     dVAR;
1926     OP *rop;
1927
1928     PERL_ARGS_ASSERT_DUP_ATTRLIST;
1929
1930     /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1931      * where the first kid is OP_PUSHMARK and the remaining ones
1932      * are OP_CONST.  We need to push the OP_CONST values.
1933      */
1934     if (o->op_type == OP_CONST)
1935         rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
1936 #ifdef PERL_MAD
1937     else if (o->op_type == OP_NULL)
1938         rop = NULL;
1939 #endif
1940     else {
1941         assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1942         rop = NULL;
1943         for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1944             if (o->op_type == OP_CONST)
1945                 rop = append_elem(OP_LIST, rop,
1946                                   newSVOP(OP_CONST, o->op_flags,
1947                                           SvREFCNT_inc_NN(cSVOPo->op_sv)));
1948         }
1949     }
1950     return rop;
1951 }
1952
1953 STATIC void
1954 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1955 {
1956     dVAR;
1957     SV *stashsv;
1958
1959     PERL_ARGS_ASSERT_APPLY_ATTRS;
1960
1961     /* fake up C<use attributes $pkg,$rv,@attrs> */
1962     ENTER;              /* need to protect against side-effects of 'use' */
1963     stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1964
1965 #define ATTRSMODULE "attributes"
1966 #define ATTRSMODULE_PM "attributes.pm"
1967
1968     if (for_my) {
1969         /* Don't force the C<use> if we don't need it. */
1970         SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
1971         if (svp && *svp != &PL_sv_undef)
1972             NOOP;       /* already in %INC */
1973         else
1974             Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1975                              newSVpvs(ATTRSMODULE), NULL);
1976     }
1977     else {
1978         Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1979                          newSVpvs(ATTRSMODULE),
1980                          NULL,
1981                          prepend_elem(OP_LIST,
1982                                       newSVOP(OP_CONST, 0, stashsv),
1983                                       prepend_elem(OP_LIST,
1984                                                    newSVOP(OP_CONST, 0,
1985                                                            newRV(target)),
1986                                                    dup_attrlist(attrs))));
1987     }
1988     LEAVE;
1989 }
1990
1991 STATIC void
1992 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1993 {
1994     dVAR;
1995     OP *pack, *imop, *arg;
1996     SV *meth, *stashsv;
1997
1998     PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
1999
2000     if (!attrs)
2001         return;
2002
2003     assert(target->op_type == OP_PADSV ||
2004            target->op_type == OP_PADHV ||
2005            target->op_type == OP_PADAV);
2006
2007     /* Ensure that attributes.pm is loaded. */
2008     apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
2009
2010     /* Need package name for method call. */
2011     pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2012
2013     /* Build up the real arg-list. */
2014     stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2015
2016     arg = newOP(OP_PADSV, 0);
2017     arg->op_targ = target->op_targ;
2018     arg = prepend_elem(OP_LIST,
2019                        newSVOP(OP_CONST, 0, stashsv),
2020                        prepend_elem(OP_LIST,
2021                                     newUNOP(OP_REFGEN, 0,
2022                                             mod(arg, OP_REFGEN)),
2023                                     dup_attrlist(attrs)));
2024
2025     /* Fake up a method call to import */
2026     meth = newSVpvs_share("import");
2027     imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2028                    append_elem(OP_LIST,
2029                                prepend_elem(OP_LIST, pack, list(arg)),
2030                                newSVOP(OP_METHOD_NAMED, 0, meth)));
2031     imop->op_private |= OPpENTERSUB_NOMOD;
2032
2033     /* Combine the ops. */
2034     *imopsp = append_elem(OP_LIST, *imopsp, imop);
2035 }
2036
2037 /*
2038 =notfor apidoc apply_attrs_string
2039
2040 Attempts to apply a list of attributes specified by the C<attrstr> and
2041 C<len> arguments to the subroutine identified by the C<cv> argument which
2042 is expected to be associated with the package identified by the C<stashpv>
2043 argument (see L<attributes>).  It gets this wrong, though, in that it
2044 does not correctly identify the boundaries of the individual attribute
2045 specifications within C<attrstr>.  This is not really intended for the
2046 public API, but has to be listed here for systems such as AIX which
2047 need an explicit export list for symbols.  (It's called from XS code
2048 in support of the C<ATTRS:> keyword from F<xsubpp>.)  Patches to fix it
2049 to respect attribute syntax properly would be welcome.
2050
2051 =cut
2052 */
2053
2054 void
2055 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2056                         const char *attrstr, STRLEN len)
2057 {
2058     OP *attrs = NULL;
2059
2060     PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2061
2062     if (!len) {
2063         len = strlen(attrstr);
2064     }
2065
2066     while (len) {
2067         for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2068         if (len) {
2069             const char * const sstr = attrstr;
2070             for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2071             attrs = append_elem(OP_LIST, attrs,
2072                                 newSVOP(OP_CONST, 0,
2073                                         newSVpvn(sstr, attrstr-sstr)));
2074         }
2075     }
2076
2077     Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2078                      newSVpvs(ATTRSMODULE),
2079                      NULL, prepend_elem(OP_LIST,
2080                                   newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2081                                   prepend_elem(OP_LIST,
2082                                                newSVOP(OP_CONST, 0,
2083                                                        newRV(MUTABLE_SV(cv))),
2084                                                attrs)));
2085 }
2086
2087 STATIC OP *
2088 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2089 {
2090     dVAR;
2091     I32 type;
2092
2093     PERL_ARGS_ASSERT_MY_KID;
2094
2095     if (!o || (PL_parser && PL_parser->error_count))
2096         return o;
2097
2098     type = o->op_type;
2099     if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2100         (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2101         return o;
2102     }
2103
2104     if (type == OP_LIST) {
2105         OP *kid;
2106         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2107             my_kid(kid, attrs, imopsp);
2108     } else if (type == OP_UNDEF
2109 #ifdef PERL_MAD
2110                || type == OP_STUB
2111 #endif
2112                ) {
2113         return o;
2114     } else if (type == OP_RV2SV ||      /* "our" declaration */
2115                type == OP_RV2AV ||
2116                type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2117         if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2118             yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2119                         OP_DESC(o),
2120                         PL_parser->in_my == KEY_our
2121                             ? "our"
2122                             : PL_parser->in_my == KEY_state ? "state" : "my"));
2123         } else if (attrs) {
2124             GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2125             PL_parser->in_my = FALSE;
2126             PL_parser->in_my_stash = NULL;
2127             apply_attrs(GvSTASH(gv),
2128                         (type == OP_RV2SV ? GvSV(gv) :
2129                          type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2130                          type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2131                         attrs, FALSE);
2132         }
2133         o->op_private |= OPpOUR_INTRO;
2134         return o;
2135     }
2136     else if (type != OP_PADSV &&
2137              type != OP_PADAV &&
2138              type != OP_PADHV &&
2139              type != OP_PUSHMARK)
2140     {
2141         yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2142                           OP_DESC(o),
2143                           PL_parser->in_my == KEY_our
2144                             ? "our"
2145                             : PL_parser->in_my == KEY_state ? "state" : "my"));
2146         return o;
2147     }
2148     else if (attrs && type != OP_PUSHMARK) {
2149         HV *stash;
2150
2151         PL_parser->in_my = FALSE;
2152         PL_parser->in_my_stash = NULL;
2153
2154         /* check for C<my Dog $spot> when deciding package */
2155         stash = PAD_COMPNAME_TYPE(o->op_targ);
2156         if (!stash)
2157             stash = PL_curstash;
2158         apply_attrs_my(stash, o, attrs, imopsp);
2159     }
2160     o->op_flags |= OPf_MOD;
2161     o->op_private |= OPpLVAL_INTRO;
2162     if (PL_parser->in_my == KEY_state)
2163         o->op_private |= OPpPAD_STATE;
2164     return o;
2165 }
2166
2167 OP *
2168 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2169 {
2170     dVAR;
2171     OP *rops;
2172     int maybe_scalar = 0;
2173
2174     PERL_ARGS_ASSERT_MY_ATTRS;
2175
2176 /* [perl #17376]: this appears to be premature, and results in code such as
2177    C< our(%x); > executing in list mode rather than void mode */
2178 #if 0
2179     if (o->op_flags & OPf_PARENS)
2180         list(o);
2181     else
2182         maybe_scalar = 1;
2183 #else
2184     maybe_scalar = 1;
2185 #endif
2186     if (attrs)
2187         SAVEFREEOP(attrs);
2188     rops = NULL;
2189     o = my_kid(o, attrs, &rops);
2190     if (rops) {
2191         if (maybe_scalar && o->op_type == OP_PADSV) {
2192             o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
2193             o->op_private |= OPpLVAL_INTRO;
2194         }
2195         else
2196             o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
2197     }
2198     PL_parser->in_my = FALSE;
2199     PL_parser->in_my_stash = NULL;
2200     return o;
2201 }
2202
2203 OP *
2204 Perl_sawparens(pTHX_ OP *o)
2205 {
2206     PERL_UNUSED_CONTEXT;
2207     if (o)
2208         o->op_flags |= OPf_PARENS;
2209     return o;
2210 }
2211
2212 OP *
2213 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2214 {
2215     OP *o;
2216     bool ismatchop = 0;
2217     const OPCODE ltype = left->op_type;
2218     const OPCODE rtype = right->op_type;
2219
2220     PERL_ARGS_ASSERT_BIND_MATCH;
2221
2222     if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2223           || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2224     {
2225       const char * const desc
2226           = PL_op_desc[(rtype == OP_SUBST || rtype == OP_TRANS)
2227                        ? (int)rtype : OP_MATCH];
2228       const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV)
2229              ? "@array" : "%hash");
2230       Perl_warner(aTHX_ packWARN(WARN_MISC),
2231              "Applying %s to %s will act on scalar(%s)",
2232              desc, sample, sample);
2233     }
2234
2235     if (rtype == OP_CONST &&
2236         cSVOPx(right)->op_private & OPpCONST_BARE &&
2237         cSVOPx(right)->op_private & OPpCONST_STRICT)
2238     {
2239         no_bareword_allowed(right);
2240     }
2241
2242     /* !~ doesn't make sense with s///r, so error on it for now */
2243     if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2244         type == OP_NOT)
2245         yyerror("Using !~ with s///r doesn't make sense");
2246
2247     ismatchop = rtype == OP_MATCH ||
2248                 rtype == OP_SUBST ||
2249                 rtype == OP_TRANS;
2250     if (ismatchop && right->op_private & OPpTARGET_MY) {
2251         right->op_targ = 0;
2252         right->op_private &= ~OPpTARGET_MY;
2253     }
2254     if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2255         OP *newleft;
2256
2257         right->op_flags |= OPf_STACKED;
2258         if (rtype != OP_MATCH &&
2259             ! (rtype == OP_TRANS &&
2260                right->op_private & OPpTRANS_IDENTICAL) &&
2261             ! (rtype == OP_SUBST &&
2262                (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2263             newleft = mod(left, rtype);
2264         else
2265             newleft = left;
2266         if (right->op_type == OP_TRANS)
2267             o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2268         else
2269             o = prepend_elem(rtype, scalar(newleft), right);
2270         if (type == OP_NOT)
2271             return newUNOP(OP_NOT, 0, scalar(o));
2272         return o;
2273     }
2274     else
2275         return bind_match(type, left,
2276                 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
2277 }
2278
2279 OP *
2280 Perl_invert(pTHX_ OP *o)
2281 {
2282     if (!o)
2283         return NULL;
2284     return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2285 }
2286
2287 OP *
2288 Perl_scope(pTHX_ OP *o)
2289 {
2290     dVAR;
2291     if (o) {
2292         if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2293             o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2294             o->op_type = OP_LEAVE;
2295             o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2296         }
2297         else if (o->op_type == OP_LINESEQ) {
2298             OP *kid;
2299             o->op_type = OP_SCOPE;
2300             o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2301             kid = ((LISTOP*)o)->op_first;
2302             if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2303                 op_null(kid);
2304
2305                 /* The following deals with things like 'do {1 for 1}' */
2306                 kid = kid->op_sibling;
2307                 if (kid &&
2308                     (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2309                     op_null(kid);
2310             }
2311         }
2312         else
2313             o = newLISTOP(OP_SCOPE, 0, o, NULL);
2314     }
2315     return o;
2316 }
2317
2318 int
2319 Perl_block_start(pTHX_ int full)
2320 {
2321     dVAR;
2322     const int retval = PL_savestack_ix;
2323
2324     pad_block_start(full);
2325     SAVEHINTS();
2326     PL_hints &= ~HINT_BLOCK_SCOPE;
2327     SAVECOMPILEWARNINGS();
2328     PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2329
2330     CALL_BLOCK_HOOKS(start, full);
2331
2332     return retval;
2333 }
2334
2335 OP*
2336 Perl_block_end(pTHX_ I32 floor, OP *seq)
2337 {
2338     dVAR;
2339     const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2340     OP* retval = scalarseq(seq);
2341
2342     CALL_BLOCK_HOOKS(pre_end, &retval);
2343
2344     LEAVE_SCOPE(floor);
2345     CopHINTS_set(&PL_compiling, PL_hints);
2346     if (needblockscope)
2347         PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2348     pad_leavemy();
2349
2350     CALL_BLOCK_HOOKS(post_end, &retval);
2351
2352     return retval;
2353 }
2354
2355 /*
2356 =head1 Compile-time scope hooks
2357
2358 =for apidoc Ao||blockhook_register
2359
2360 Register a set of hooks to be called when the Perl lexical scope changes
2361 at compile time. See L<perlguts/"Compile-time scope hooks">.
2362
2363 =cut
2364 */
2365
2366 void
2367 Perl_blockhook_register(pTHX_ BHK *hk)
2368 {
2369     PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
2370
2371     Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
2372 }
2373
2374 STATIC OP *
2375 S_newDEFSVOP(pTHX)
2376 {
2377     dVAR;
2378     const PADOFFSET offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0);
2379     if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2380         return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2381     }
2382     else {
2383         OP * const o = newOP(OP_PADSV, 0);
2384         o->op_targ = offset;
2385         return o;
2386     }
2387 }
2388
2389 void
2390 Perl_newPROG(pTHX_ OP *o)
2391 {
2392     dVAR;
2393
2394     PERL_ARGS_ASSERT_NEWPROG;
2395
2396     if (PL_in_eval) {
2397         if (PL_eval_root)
2398                 return;
2399         PL_eval_root = newUNOP(OP_LEAVEEVAL,
2400                                ((PL_in_eval & EVAL_KEEPERR)
2401                                 ? OPf_SPECIAL : 0), o);
2402         PL_eval_start = linklist(PL_eval_root);
2403         PL_eval_root->op_private |= OPpREFCOUNTED;
2404         OpREFCNT_set(PL_eval_root, 1);
2405         PL_eval_root->op_next = 0;
2406         CALL_PEEP(PL_eval_start);
2407     }
2408     else {
2409         if (o->op_type == OP_STUB) {
2410             PL_comppad_name = 0;
2411             PL_compcv = 0;
2412             S_op_destroy(aTHX_ o);
2413             return;
2414         }
2415         PL_main_root = scope(sawparens(scalarvoid(o)));
2416         PL_curcop = &PL_compiling;
2417         PL_main_start = LINKLIST(PL_main_root);
2418         PL_main_root->op_private |= OPpREFCOUNTED;
2419         OpREFCNT_set(PL_main_root, 1);
2420         PL_main_root->op_next = 0;
2421         CALL_PEEP(PL_main_start);
2422         PL_compcv = 0;
2423
2424         /* Register with debugger */
2425         if (PERLDB_INTER) {
2426             CV * const cv = get_cvs("DB::postponed", 0);
2427             if (cv) {
2428                 dSP;
2429                 PUSHMARK(SP);
2430                 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2431                 PUTBACK;
2432                 call_sv(MUTABLE_SV(cv), G_DISCARD);
2433             }
2434         }
2435     }
2436 }
2437
2438 OP *
2439 Perl_localize(pTHX_ OP *o, I32 lex)
2440 {
2441     dVAR;
2442
2443     PERL_ARGS_ASSERT_LOCALIZE;
2444
2445     if (o->op_flags & OPf_PARENS)
2446 /* [perl #17376]: this appears to be premature, and results in code such as
2447    C< our(%x); > executing in list mode rather than void mode */
2448 #if 0
2449         list(o);
2450 #else
2451         NOOP;
2452 #endif
2453     else {
2454         if ( PL_parser->bufptr > PL_parser->oldbufptr
2455             && PL_parser->bufptr[-1] == ','
2456             && ckWARN(WARN_PARENTHESIS))
2457         {
2458             char *s = PL_parser->bufptr;
2459             bool sigil = FALSE;
2460
2461             /* some heuristics to detect a potential error */
2462             while (*s && (strchr(", \t\n", *s)))
2463                 s++;
2464
2465             while (1) {
2466                 if (*s && strchr("@$%*", *s) && *++s
2467                        && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2468                     s++;
2469                     sigil = TRUE;
2470                     while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2471                         s++;
2472                     while (*s && (strchr(", \t\n", *s)))
2473                         s++;
2474                 }
2475                 else
2476                     break;
2477             }
2478             if (sigil && (*s == ';' || *s == '=')) {
2479                 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2480                                 "Parentheses missing around \"%s\" list",
2481                                 lex
2482                                     ? (PL_parser->in_my == KEY_our
2483                                         ? "our"
2484                                         : PL_parser->in_my == KEY_state
2485                                             ? "state"
2486                                             : "my")
2487                                     : "local");
2488             }
2489         }
2490     }
2491     if (lex)
2492         o = my(o);
2493     else
2494         o = mod(o, OP_NULL);            /* a bit kludgey */
2495     PL_parser->in_my = FALSE;
2496     PL_parser->in_my_stash = NULL;
2497     return o;
2498 }
2499
2500 OP *
2501 Perl_jmaybe(pTHX_ OP *o)
2502 {
2503     PERL_ARGS_ASSERT_JMAYBE;
2504
2505     if (o->op_type == OP_LIST) {
2506         OP * const o2
2507             = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2508         o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2509     }
2510     return o;
2511 }
2512
2513 static OP *
2514 S_fold_constants(pTHX_ register OP *o)
2515 {
2516     dVAR;
2517     register OP * VOL curop;
2518     OP *newop;
2519     VOL I32 type = o->op_type;
2520     SV * VOL sv = NULL;
2521     int ret = 0;
2522     I32 oldscope;
2523     OP *old_next;
2524     SV * const oldwarnhook = PL_warnhook;
2525     SV * const olddiehook  = PL_diehook;
2526     COP not_compiling;
2527     dJMPENV;
2528
2529     PERL_ARGS_ASSERT_FOLD_CONSTANTS;
2530
2531     if (PL_opargs[type] & OA_RETSCALAR)
2532         scalar(o);
2533     if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2534         o->op_targ = pad_alloc(type, SVs_PADTMP);
2535
2536     /* integerize op, unless it happens to be C<-foo>.
2537      * XXX should pp_i_negate() do magic string negation instead? */
2538     if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2539         && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2540              && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2541     {
2542         o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2543     }
2544
2545     if (!(PL_opargs[type] & OA_FOLDCONST))
2546         goto nope;
2547
2548     switch (type) {
2549     case OP_NEGATE:
2550         /* XXX might want a ck_negate() for this */
2551         cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2552         break;
2553     case OP_UCFIRST:
2554     case OP_LCFIRST:
2555     case OP_UC:
2556     case OP_LC:
2557     case OP_SLT:
2558     case OP_SGT:
2559     case OP_SLE:
2560     case OP_SGE:
2561     case OP_SCMP:
2562         /* XXX what about the numeric ops? */
2563         if (PL_hints & HINT_LOCALE)
2564             goto nope;
2565         break;
2566     }
2567
2568     if (PL_parser && PL_parser->error_count)
2569         goto nope;              /* Don't try to run w/ errors */
2570
2571     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2572         const OPCODE type = curop->op_type;
2573         if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2574             type != OP_LIST &&
2575             type != OP_SCALAR &&
2576             type != OP_NULL &&
2577             type != OP_PUSHMARK)
2578         {
2579             goto nope;
2580         }
2581     }
2582
2583     curop = LINKLIST(o);
2584     old_next = o->op_next;
2585     o->op_next = 0;
2586     PL_op = curop;
2587
2588     oldscope = PL_scopestack_ix;
2589     create_eval_scope(G_FAKINGEVAL);
2590
2591     /* Verify that we don't need to save it:  */
2592     assert(PL_curcop == &PL_compiling);
2593     StructCopy(&PL_compiling, &not_compiling, COP);
2594     PL_curcop = &not_compiling;
2595     /* The above ensures that we run with all the correct hints of the
2596        currently compiling COP, but that IN_PERL_RUNTIME is not true. */
2597     assert(IN_PERL_RUNTIME);
2598     PL_warnhook = PERL_WARNHOOK_FATAL;
2599     PL_diehook  = NULL;
2600     JMPENV_PUSH(ret);
2601
2602     switch (ret) {
2603     case 0:
2604         CALLRUNOPS(aTHX);
2605         sv = *(PL_stack_sp--);
2606         if (o->op_targ && sv == PAD_SV(o->op_targ))     /* grab pad temp? */
2607             pad_swipe(o->op_targ,  FALSE);
2608         else if (SvTEMP(sv)) {                  /* grab mortal temp? */
2609             SvREFCNT_inc_simple_void(sv);
2610             SvTEMP_off(sv);
2611         }
2612         break;
2613     case 3:
2614         /* Something tried to die.  Abandon constant folding.  */
2615         /* Pretend the error never happened.  */
2616         CLEAR_ERRSV();
2617         o->op_next = old_next;
2618         break;
2619     default:
2620         JMPENV_POP;
2621         /* Don't expect 1 (setjmp failed) or 2 (something called my_exit)  */
2622         PL_warnhook = oldwarnhook;
2623         PL_diehook  = olddiehook;
2624         /* XXX note that this croak may fail as we've already blown away
2625          * the stack - eg any nested evals */
2626         Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
2627     }
2628     JMPENV_POP;
2629     PL_warnhook = oldwarnhook;
2630     PL_diehook  = olddiehook;
2631     PL_curcop = &PL_compiling;
2632
2633     if (PL_scopestack_ix > oldscope)
2634         delete_eval_scope();
2635
2636     if (ret)
2637         goto nope;
2638
2639 #ifndef PERL_MAD
2640     op_free(o);
2641 #endif
2642     assert(sv);
2643     if (type == OP_RV2GV)
2644         newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
2645     else
2646         newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
2647     op_getmad(o,newop,'f');
2648     return newop;
2649
2650  nope:
2651     return o;
2652 }
2653
2654 static OP *
2655 S_gen_constant_list(pTHX_ register OP *o)
2656 {
2657     dVAR;
2658     register OP *curop;
2659     const I32 oldtmps_floor = PL_tmps_floor;
2660
2661     list(o);
2662     if (PL_parser && PL_parser->error_count)
2663         return o;               /* Don't attempt to run with errors */
2664
2665     PL_op = curop = LINKLIST(o);
2666     o->op_next = 0;
2667     CALL_PEEP(curop);
2668     pp_pushmark();
2669     CALLRUNOPS(aTHX);
2670     PL_op = curop;
2671     assert (!(curop->op_flags & OPf_SPECIAL));
2672     assert(curop->op_type == OP_RANGE);
2673     pp_anonlist();
2674     PL_tmps_floor = oldtmps_floor;
2675
2676     o->op_type = OP_RV2AV;
2677     o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2678     o->op_flags &= ~OPf_REF;    /* treat \(1..2) like an ordinary list */
2679     o->op_flags |= OPf_PARENS;  /* and flatten \(1..2,3) */
2680     o->op_opt = 0;              /* needs to be revisited in peep() */
2681     curop = ((UNOP*)o)->op_first;
2682     ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
2683 #ifdef PERL_MAD
2684     op_getmad(curop,o,'O');
2685 #else
2686     op_free(curop);
2687 #endif
2688     linklist(o);
2689     return list(o);
2690 }
2691
2692 OP *
2693 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2694 {
2695     dVAR;
2696     if (!o || o->op_type != OP_LIST)
2697         o = newLISTOP(OP_LIST, 0, o, NULL);
2698     else
2699         o->op_flags &= ~OPf_WANT;
2700
2701     if (!(PL_opargs[type] & OA_MARK))
2702         op_null(cLISTOPo->op_first);
2703
2704     o->op_type = (OPCODE)type;
2705     o->op_ppaddr = PL_ppaddr[type];
2706     o->op_flags |= flags;
2707
2708     o = CHECKOP(type, o);
2709     if (o->op_type != (unsigned)type)
2710         return o;
2711
2712     return fold_constants(o);
2713 }
2714
2715 /* List constructors */
2716
2717 OP *
2718 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2719 {
2720     if (!first)
2721         return last;
2722
2723     if (!last)
2724         return first;
2725
2726     if (first->op_type != (unsigned)type
2727         || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2728     {
2729         return newLISTOP(type, 0, first, last);
2730     }
2731
2732     if (first->op_flags & OPf_KIDS)
2733         ((LISTOP*)first)->op_last->op_sibling = last;
2734     else {
2735         first->op_flags |= OPf_KIDS;
2736         ((LISTOP*)first)->op_first = last;
2737     }
2738     ((LISTOP*)first)->op_last = last;
2739     return first;
2740 }
2741
2742 OP *
2743 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2744 {
2745     if (!first)
2746         return (OP*)last;
2747
2748     if (!last)
2749         return (OP*)first;
2750
2751     if (first->op_type != (unsigned)type)
2752         return prepend_elem(type, (OP*)first, (OP*)last);
2753
2754     if (last->op_type != (unsigned)type)
2755         return append_elem(type, (OP*)first, (OP*)last);
2756
2757     first->op_last->op_sibling = last->op_first;
2758     first->op_last = last->op_last;
2759     first->op_flags |= (last->op_flags & OPf_KIDS);
2760
2761 #ifdef PERL_MAD
2762     if (last->op_first && first->op_madprop) {
2763         MADPROP *mp = last->op_first->op_madprop;
2764         if (mp) {
2765             while (mp->mad_next)
2766                 mp = mp->mad_next;
2767             mp->mad_next = first->op_madprop;
2768         }
2769         else {
2770             last->op_first->op_madprop = first->op_madprop;
2771         }
2772     }
2773     first->op_madprop = last->op_madprop;
2774     last->op_madprop = 0;
2775 #endif
2776
2777     S_op_destroy(aTHX_ (OP*)last);
2778
2779     return (OP*)first;
2780 }
2781
2782 OP *
2783 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2784 {
2785     if (!first)
2786         return last;
2787
2788     if (!last)
2789         return first;
2790
2791     if (last->op_type == (unsigned)type) {
2792         if (type == OP_LIST) {  /* already a PUSHMARK there */
2793             first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2794             ((LISTOP*)last)->op_first->op_sibling = first;
2795             if (!(first->op_flags & OPf_PARENS))
2796                 last->op_flags &= ~OPf_PARENS;
2797         }
2798         else {
2799             if (!(last->op_flags & OPf_KIDS)) {
2800                 ((LISTOP*)last)->op_last = first;
2801                 last->op_flags |= OPf_KIDS;
2802             }
2803             first->op_sibling = ((LISTOP*)last)->op_first;
2804             ((LISTOP*)last)->op_first = first;
2805         }
2806         last->op_flags |= OPf_KIDS;
2807         return last;
2808     }
2809
2810     return newLISTOP(type, 0, first, last);
2811 }
2812
2813 /* Constructors */
2814
2815 #ifdef PERL_MAD
2816  
2817 TOKEN *
2818 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
2819 {
2820     TOKEN *tk;
2821     Newxz(tk, 1, TOKEN);
2822     tk->tk_type = (OPCODE)optype;
2823     tk->tk_type = 12345;
2824     tk->tk_lval = lval;
2825     tk->tk_mad = madprop;
2826     return tk;
2827 }
2828
2829 void
2830 Perl_token_free(pTHX_ TOKEN* tk)
2831 {
2832     PERL_ARGS_ASSERT_TOKEN_FREE;
2833
2834     if (tk->tk_type != 12345)
2835         return;
2836     mad_free(tk->tk_mad);
2837     Safefree(tk);
2838 }
2839
2840 void
2841 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
2842 {
2843     MADPROP* mp;
2844     MADPROP* tm;
2845
2846     PERL_ARGS_ASSERT_TOKEN_GETMAD;
2847
2848     if (tk->tk_type != 12345) {
2849         Perl_warner(aTHX_ packWARN(WARN_MISC),
2850              "Invalid TOKEN object ignored");
2851         return;
2852     }
2853     tm = tk->tk_mad;
2854     if (!tm)
2855         return;
2856
2857     /* faked up qw list? */
2858     if (slot == '(' &&
2859         tm->mad_type == MAD_SV &&
2860         SvPVX((SV *)tm->mad_val)[0] == 'q')
2861             slot = 'x';
2862
2863     if (o) {
2864         mp = o->op_madprop;
2865         if (mp) {
2866             for (;;) {
2867                 /* pretend constant fold didn't happen? */
2868                 if (mp->mad_key == 'f' &&
2869                     (o->op_type == OP_CONST ||
2870                      o->op_type == OP_GV) )
2871                 {
2872                     token_getmad(tk,(OP*)mp->mad_val,slot);
2873                     return;
2874                 }
2875                 if (!mp->mad_next)
2876                     break;
2877                 mp = mp->mad_next;
2878             }
2879             mp->mad_next = tm;
2880             mp = mp->mad_next;
2881         }
2882         else {
2883             o->op_madprop = tm;
2884             mp = o->op_madprop;
2885         }
2886         if (mp->mad_key == 'X')
2887             mp->mad_key = slot; /* just change the first one */
2888
2889         tk->tk_mad = 0;
2890     }
2891     else
2892         mad_free(tm);
2893     Safefree(tk);
2894 }
2895
2896 void
2897 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
2898 {
2899     MADPROP* mp;
2900     if (!from)
2901         return;
2902     if (o) {
2903         mp = o->op_madprop;
2904         if (mp) {
2905             for (;;) {
2906                 /* pretend constant fold didn't happen? */
2907                 if (mp->mad_key == 'f' &&
2908                     (o->op_type == OP_CONST ||
2909                      o->op_type == OP_GV) )
2910                 {
2911                     op_getmad(from,(OP*)mp->mad_val,slot);
2912                     return;
2913                 }
2914                 if (!mp->mad_next)
2915                     break;
2916                 mp = mp->mad_next;
2917             }
2918             mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
2919         }
2920         else {
2921             o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
2922         }
2923     }
2924 }
2925
2926 void
2927 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
2928 {
2929     MADPROP* mp;
2930     if (!from)
2931         return;
2932     if (o) {
2933         mp = o->op_madprop;
2934         if (mp) {
2935             for (;;) {
2936                 /* pretend constant fold didn't happen? */
2937                 if (mp->mad_key == 'f' &&
2938                     (o->op_type == OP_CONST ||
2939                      o->op_type == OP_GV) )
2940                 {
2941                     op_getmad(from,(OP*)mp->mad_val,slot);
2942                     return;
2943                 }
2944                 if (!mp->mad_next)
2945                     break;
2946                 mp = mp->mad_next;
2947             }
2948             mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
2949         }
2950         else {
2951             o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
2952         }
2953     }
2954     else {
2955         PerlIO_printf(PerlIO_stderr(),
2956                       "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
2957         op_free(from);
2958     }
2959 }
2960
2961 void
2962 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
2963 {
2964     MADPROP* tm;
2965     if (!mp || !o)
2966         return;
2967     if (slot)
2968         mp->mad_key = slot;
2969     tm = o->op_madprop;
2970     o->op_madprop = mp;
2971     for (;;) {
2972         if (!mp->mad_next)
2973             break;
2974         mp = mp->mad_next;
2975     }
2976     mp->mad_next = tm;
2977 }
2978
2979 void
2980 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
2981 {
2982     if (!o)
2983         return;
2984     addmad(tm, &(o->op_madprop), slot);
2985 }
2986
2987 void
2988 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
2989 {
2990     MADPROP* mp;
2991     if (!tm || !root)
2992         return;
2993     if (slot)
2994         tm->mad_key = slot;
2995     mp = *root;
2996     if (!mp) {
2997         *root = tm;
2998         return;
2999     }
3000     for (;;) {
3001         if (!mp->mad_next)
3002             break;
3003         mp = mp->mad_next;
3004     }
3005     mp->mad_next = tm;
3006 }
3007
3008 MADPROP *
3009 Perl_newMADsv(pTHX_ char key, SV* sv)
3010 {
3011     PERL_ARGS_ASSERT_NEWMADSV;
3012
3013     return newMADPROP(key, MAD_SV, sv, 0);
3014 }
3015
3016 MADPROP *
3017 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3018 {
3019     MADPROP *mp;
3020     Newxz(mp, 1, MADPROP);
3021     mp->mad_next = 0;
3022     mp->mad_key = key;
3023     mp->mad_vlen = vlen;
3024     mp->mad_type = type;
3025     mp->mad_val = val;
3026 /*    PerlIO_printf(PerlIO_stderr(), "NEW  mp = %0x\n", mp);  */
3027     return mp;
3028 }
3029
3030 void
3031 Perl_mad_free(pTHX_ MADPROP* mp)
3032 {
3033 /*    PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3034     if (!mp)
3035         return;
3036     if (mp->mad_next)
3037         mad_free(mp->mad_next);
3038 /*    if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3039         PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3040     switch (mp->mad_type) {
3041     case MAD_NULL:
3042         break;
3043     case MAD_PV:
3044         Safefree((char*)mp->mad_val);
3045         break;
3046     case MAD_OP:
3047         if (mp->mad_vlen)       /* vlen holds "strong/weak" boolean */
3048             op_free((OP*)mp->mad_val);
3049         break;
3050     case MAD_SV:
3051         sv_free(MUTABLE_SV(mp->mad_val));
3052         break;
3053     default:
3054         PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3055         break;
3056     }
3057     Safefree(mp);
3058 }
3059
3060 #endif
3061
3062 OP *
3063 Perl_newNULLLIST(pTHX)
3064 {
3065     return newOP(OP_STUB, 0);
3066 }
3067
3068 static OP *
3069 S_force_list(pTHX_ OP *o)
3070 {
3071     if (!o || o->op_type != OP_LIST)
3072         o = newLISTOP(OP_LIST, 0, o, NULL);
3073     op_null(o);
3074     return o;
3075 }
3076
3077 OP *
3078 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3079 {
3080     dVAR;
3081     LISTOP *listop;
3082
3083     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3084
3085     NewOp(1101, listop, 1, LISTOP);
3086
3087     listop->op_type = (OPCODE)type;
3088     listop->op_ppaddr = PL_ppaddr[type];
3089     if (first || last)
3090         flags |= OPf_KIDS;
3091     listop->op_flags = (U8)flags;
3092
3093     if (!last && first)
3094         last = first;
3095     else if (!first && last)
3096         first = last;
3097     else if (first)
3098         first->op_sibling = last;
3099     listop->op_first = first;
3100     listop->op_last = last;
3101     if (type == OP_LIST) {
3102         OP* const pushop = newOP(OP_PUSHMARK, 0);
3103         pushop->op_sibling = first;
3104         listop->op_first = pushop;
3105         listop->op_flags |= OPf_KIDS;
3106         if (!last)
3107             listop->op_last = pushop;
3108     }
3109
3110     return CHECKOP(type, listop);
3111 }
3112
3113 OP *
3114 Perl_newOP(pTHX_ I32 type, I32 flags)
3115 {
3116     dVAR;
3117     OP *o;
3118
3119     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3120         || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3121         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3122         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3123
3124     NewOp(1101, o, 1, OP);
3125     o->op_type = (OPCODE)type;
3126     o->op_ppaddr = PL_ppaddr[type];
3127     o->op_flags = (U8)flags;
3128     o->op_latefree = 0;
3129     o->op_latefreed = 0;
3130     o->op_attached = 0;
3131
3132     o->op_next = o;
3133     o->op_private = (U8)(0 | (flags >> 8));
3134     if (PL_opargs[type] & OA_RETSCALAR)
3135         scalar(o);
3136     if (PL_opargs[type] & OA_TARGET)
3137         o->op_targ = pad_alloc(type, SVs_PADTMP);
3138     return CHECKOP(type, o);
3139 }
3140
3141 OP *
3142 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3143 {
3144     dVAR;
3145     UNOP *unop;
3146
3147     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3148         || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3149         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3150         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3151         || type == OP_SASSIGN
3152         || type == OP_ENTERTRY
3153         || type == OP_NULL );
3154
3155     if (!first)
3156         first = newOP(OP_STUB, 0);
3157     if (PL_opargs[type] & OA_MARK)
3158         first = force_list(first);
3159
3160     NewOp(1101, unop, 1, UNOP);
3161     unop->op_type = (OPCODE)type;
3162     unop->op_ppaddr = PL_ppaddr[type];
3163     unop->op_first = first;
3164     unop->op_flags = (U8)(flags | OPf_KIDS);
3165     unop->op_private = (U8)(1 | (flags >> 8));
3166     unop = (UNOP*) CHECKOP(type, unop);
3167     if (unop->op_next)
3168         return (OP*)unop;
3169
3170     return fold_constants((OP *) unop);
3171 }
3172
3173 OP *
3174 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3175 {
3176     dVAR;
3177     BINOP *binop;
3178
3179     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3180         || type == OP_SASSIGN || type == OP_NULL );
3181
3182     NewOp(1101, binop, 1, BINOP);
3183
3184     if (!first)
3185         first = newOP(OP_NULL, 0);
3186
3187     binop->op_type = (OPCODE)type;
3188     binop->op_ppaddr = PL_ppaddr[type];
3189     binop->op_first = first;
3190     binop->op_flags = (U8)(flags | OPf_KIDS);
3191     if (!last) {
3192         last = first;
3193         binop->op_private = (U8)(1 | (flags >> 8));
3194     }
3195     else {
3196         binop->op_private = (U8)(2 | (flags >> 8));
3197         first->op_sibling = last;
3198     }
3199
3200     binop = (BINOP*)CHECKOP(type, binop);
3201     if (binop->op_next || binop->op_type != (OPCODE)type)
3202         return (OP*)binop;
3203
3204     binop->op_last = binop->op_first->op_sibling;
3205
3206     return fold_constants((OP *)binop);
3207 }
3208
3209 static int uvcompare(const void *a, const void *b)
3210     __attribute__nonnull__(1)
3211     __attribute__nonnull__(2)
3212     __attribute__pure__;
3213 static int uvcompare(const void *a, const void *b)
3214 {
3215     if (*((const UV *)a) < (*(const UV *)b))
3216         return -1;
3217     if (*((const UV *)a) > (*(const UV *)b))
3218         return 1;
3219     if (*((const UV *)a+1) < (*(const UV *)b+1))
3220         return -1;
3221     if (*((const UV *)a+1) > (*(const UV *)b+1))
3222         return 1;
3223     return 0;
3224 }
3225
3226 static OP *
3227 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3228 {
3229     dVAR;
3230     SV * const tstr = ((SVOP*)expr)->op_sv;
3231     SV * const rstr =
3232 #ifdef PERL_MAD
3233                         (repl->op_type == OP_NULL)
3234                             ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3235 #endif
3236                               ((SVOP*)repl)->op_sv;
3237     STRLEN tlen;
3238     STRLEN rlen;
3239     const U8 *t = (U8*)SvPV_const(tstr, tlen);
3240     const U8 *r = (U8*)SvPV_const(rstr, rlen);
3241     register I32 i;
3242     register I32 j;
3243     I32 grows = 0;
3244     register short *tbl;
3245
3246     const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3247     const I32 squash     = o->op_private & OPpTRANS_SQUASH;
3248     I32 del              = o->op_private & OPpTRANS_DELETE;
3249     SV* swash;
3250
3251     PERL_ARGS_ASSERT_PMTRANS;
3252
3253     PL_hints |= HINT_BLOCK_SCOPE;
3254
3255     if (SvUTF8(tstr))
3256         o->op_private |= OPpTRANS_FROM_UTF;
3257
3258     if (SvUTF8(rstr))
3259         o->op_private |= OPpTRANS_TO_UTF;
3260
3261     if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3262         SV* const listsv = newSVpvs("# comment\n");
3263         SV* transv = NULL;
3264         const U8* tend = t + tlen;
3265         const U8* rend = r + rlen;
3266         STRLEN ulen;
3267         UV tfirst = 1;
3268         UV tlast = 0;
3269         IV tdiff;
3270         UV rfirst = 1;
3271         UV rlast = 0;
3272         IV rdiff;
3273         IV diff;
3274         I32 none = 0;
3275         U32 max = 0;
3276         I32 bits;
3277         I32 havefinal = 0;
3278         U32 final = 0;
3279         const I32 from_utf  = o->op_private & OPpTRANS_FROM_UTF;
3280         const I32 to_utf    = o->op_private & OPpTRANS_TO_UTF;
3281         U8* tsave = NULL;
3282         U8* rsave = NULL;
3283         const U32 flags = UTF8_ALLOW_DEFAULT;
3284
3285         if (!from_utf) {
3286             STRLEN len = tlen;
3287             t = tsave = bytes_to_utf8(t, &len);
3288             tend = t + len;
3289         }
3290         if (!to_utf && rlen) {
3291             STRLEN len = rlen;
3292             r = rsave = bytes_to_utf8(r, &len);
3293             rend = r + len;
3294         }
3295
3296 /* There are several snags with this code on EBCDIC:
3297    1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
3298    2. scan_const() in toke.c has encoded chars in native encoding which makes
3299       ranges at least in EBCDIC 0..255 range the bottom odd.
3300 */
3301
3302         if (complement) {
3303             U8 tmpbuf[UTF8_MAXBYTES+1];
3304             UV *cp;
3305             UV nextmin = 0;
3306             Newx(cp, 2*tlen, UV);
3307             i = 0;
3308             transv = newSVpvs("");
3309             while (t < tend) {
3310                 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3311                 t += ulen;
3312                 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
3313                     t++;
3314                     cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3315                     t += ulen;
3316                 }
3317                 else {
3318                  cp[2*i+1] = cp[2*i];
3319                 }
3320                 i++;
3321             }
3322             qsort(cp, i, 2*sizeof(UV), uvcompare);
3323             for (j = 0; j < i; j++) {
3324                 UV  val = cp[2*j];
3325                 diff = val - nextmin;
3326                 if (diff > 0) {
3327                     t = uvuni_to_utf8(tmpbuf,nextmin);
3328                     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3329                     if (diff > 1) {
3330                         U8  range_mark = UTF_TO_NATIVE(0xff);
3331                         t = uvuni_to_utf8(tmpbuf, val - 1);
3332                         sv_catpvn(transv, (char *)&range_mark, 1);
3333                         sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3334                     }
3335                 }
3336                 val = cp[2*j+1];
3337                 if (val >= nextmin)
3338                     nextmin = val + 1;
3339             }
3340             t = uvuni_to_utf8(tmpbuf,nextmin);
3341             sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3342             {
3343                 U8 range_mark = UTF_TO_NATIVE(0xff);
3344                 sv_catpvn(transv, (char *)&range_mark, 1);
3345             }
3346             t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
3347                                     UNICODE_ALLOW_SUPER);
3348             sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3349             t = (const U8*)SvPVX_const(transv);
3350             tlen = SvCUR(transv);
3351             tend = t + tlen;
3352             Safefree(cp);
3353         }
3354         else if (!rlen && !del) {
3355             r = t; rlen = tlen; rend = tend;
3356         }
3357         if (!squash) {
3358                 if ((!rlen && !del) || t == r ||
3359                     (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
3360                 {
3361                     o->op_private |= OPpTRANS_IDENTICAL;
3362                 }
3363         }
3364
3365         while (t < tend || tfirst <= tlast) {
3366             /* see if we need more "t" chars */
3367             if (tfirst > tlast) {
3368                 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3369                 t += ulen;
3370                 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {    /* illegal utf8 val indicates range */
3371                     t++;
3372                     tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3373                     t += ulen;
3374                 }
3375                 else
3376                     tlast = tfirst;
3377             }
3378
3379             /* now see if we need more "r" chars */
3380             if (rfirst > rlast) {
3381                 if (r < rend) {
3382                     rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3383                     r += ulen;
3384                     if (r < rend && NATIVE_TO_UTF(*r) == 0xff) {        /* illegal utf8 val indicates range */
3385                         r++;
3386                         rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3387                         r += ulen;
3388                     }
3389                     else
3390                         rlast = rfirst;
3391                 }
3392                 else {
3393                     if (!havefinal++)
3394                         final = rlast;
3395                     rfirst = rlast = 0xffffffff;
3396                 }
3397             }
3398
3399             /* now see which range will peter our first, if either. */
3400             tdiff = tlast - tfirst;
3401             rdiff = rlast - rfirst;
3402
3403             if (tdiff <= rdiff)
3404                 diff = tdiff;
3405             else
3406                 diff = rdiff;
3407
3408             if (rfirst == 0xffffffff) {
3409                 diff = tdiff;   /* oops, pretend rdiff is infinite */
3410                 if (diff > 0)
3411                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
3412                                    (long)tfirst, (long)tlast);
3413                 else
3414                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
3415             }
3416             else {
3417                 if (diff > 0)
3418                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
3419                                    (long)tfirst, (long)(tfirst + diff),
3420                                    (long)rfirst);
3421                 else
3422                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
3423                                    (long)tfirst, (long)rfirst);
3424
3425                 if (rfirst + diff > max)
3426                     max = rfirst + diff;
3427                 if (!grows)
3428                     grows = (tfirst < rfirst &&
3429                              UNISKIP(tfirst) < UNISKIP(rfirst + diff));
3430                 rfirst += diff + 1;
3431             }
3432             tfirst += diff + 1;
3433         }
3434
3435         none = ++max;
3436         if (del)
3437             del = ++max;
3438
3439         if (max > 0xffff)
3440             bits = 32;
3441         else if (max > 0xff)
3442             bits = 16;
3443         else
3444             bits = 8;
3445
3446         PerlMemShared_free(cPVOPo->op_pv);
3447         cPVOPo->op_pv = NULL;
3448
3449         swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
3450 #ifdef USE_ITHREADS
3451         cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
3452         SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
3453         PAD_SETSV(cPADOPo->op_padix, swash);
3454         SvPADTMP_on(swash);
3455         SvREADONLY_on(swash);
3456 #else
3457         cSVOPo->op_sv = swash;
3458 #endif
3459         SvREFCNT_dec(listsv);
3460         SvREFCNT_dec(transv);
3461
3462         if (!del && havefinal && rlen)
3463             (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
3464                            newSVuv((UV)final), 0);
3465
3466         if (grows)
3467             o->op_private |= OPpTRANS_GROWS;
3468
3469         Safefree(tsave);
3470         Safefree(rsave);
3471
3472 #ifdef PERL_MAD
3473         op_getmad(expr,o,'e');
3474         op_getmad(repl,o,'r');
3475 #else
3476         op_free(expr);
3477         op_free(repl);
3478 #endif
3479         return o;
3480     }
3481
3482     tbl = (short*)cPVOPo->op_pv;
3483     if (complement) {
3484         Zero(tbl, 256, short);
3485         for (i = 0; i < (I32)tlen; i++)
3486             tbl[t[i]] = -1;
3487         for (i = 0, j = 0; i < 256; i++) {
3488             if (!tbl[i]) {
3489                 if (j >= (I32)rlen) {
3490                     if (del)
3491                         tbl[i] = -2;
3492                     else if (rlen)
3493                         tbl[i] = r[j-1];
3494                     else
3495                         tbl[i] = (short)i;
3496                 }
3497                 else {
3498                     if (i < 128 && r[j] >= 128)
3499                         grows = 1;
3500                     tbl[i] = r[j++];
3501                 }
3502             }
3503         }
3504         if (!del) {
3505             if (!rlen) {
3506                 j = rlen;
3507                 if (!squash)
3508                     o->op_private |= OPpTRANS_IDENTICAL;
3509             }
3510             else if (j >= (I32)rlen)
3511                 j = rlen - 1;
3512             else {
3513                 tbl = 
3514                     (short *)
3515                     PerlMemShared_realloc(tbl,
3516                                           (0x101+rlen-j) * sizeof(short));
3517                 cPVOPo->op_pv = (char*)tbl;
3518             }
3519             tbl[0x100] = (short)(rlen - j);
3520             for (i=0; i < (I32)rlen - j; i++)
3521                 tbl[0x101+i] = r[j+i];
3522         }
3523     }
3524     else {
3525         if (!rlen && !del) {
3526             r = t; rlen = tlen;
3527             if (!squash)
3528                 o->op_private |= OPpTRANS_IDENTICAL;
3529         }
3530         else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
3531             o->op_private |= OPpTRANS_IDENTICAL;
3532         }
3533         for (i = 0; i < 256; i++)
3534             tbl[i] = -1;
3535         for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
3536             if (j >= (I32)rlen) {
3537                 if (del) {
3538                     if (tbl[t[i]] == -1)
3539                         tbl[t[i]] = -2;
3540                     continue;
3541                 }
3542                 --j;
3543             }
3544             if (tbl[t[i]] == -1) {
3545                 if (t[i] < 128 && r[j] >= 128)
3546                     grows = 1;
3547                 tbl[t[i]] = r[j];
3548             }
3549         }
3550     }
3551
3552     if(del && rlen == tlen) {
3553         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); 
3554     } else if(rlen > tlen) {
3555         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
3556     }
3557
3558     if (grows)
3559         o->op_private |= OPpTRANS_GROWS;
3560 #ifdef PERL_MAD
3561     op_getmad(expr,o,'e');
3562     op_getmad(repl,o,'r');
3563 #else
3564     op_free(expr);
3565     op_free(repl);
3566 #endif
3567
3568     return o;
3569 }
3570
3571 OP *
3572 Perl_newPMOP(pTHX_ I32 type, I32 flags)
3573 {
3574     dVAR;
3575     PMOP *pmop;
3576
3577     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
3578
3579     NewOp(1101, pmop, 1, PMOP);
3580     pmop->op_type = (OPCODE)type;
3581     pmop->op_ppaddr = PL_ppaddr[type];
3582     pmop->op_flags = (U8)flags;
3583     pmop->op_private = (U8)(0 | (flags >> 8));
3584
3585     if (PL_hints & HINT_RE_TAINT)
3586         pmop->op_pmflags |= PMf_RETAINT;
3587     if (PL_hints & HINT_LOCALE)
3588         pmop->op_pmflags |= PMf_LOCALE;
3589
3590
3591 #ifdef USE_ITHREADS
3592     assert(SvPOK(PL_regex_pad[0]));
3593     if (SvCUR(PL_regex_pad[0])) {
3594         /* Pop off the "packed" IV from the end.  */
3595         SV *const repointer_list = PL_regex_pad[0];
3596         const char *p = SvEND(repointer_list) - sizeof(IV);
3597         const IV offset = *((IV*)p);
3598
3599         assert(SvCUR(repointer_list) % sizeof(IV) == 0);
3600
3601         SvEND_set(repointer_list, p);
3602
3603         pmop->op_pmoffset = offset;
3604         /* This slot should be free, so assert this:  */
3605         assert(PL_regex_pad[offset] == &PL_sv_undef);
3606     } else {
3607         SV * const repointer = &PL_sv_undef;
3608         av_push(PL_regex_padav, repointer);
3609         pmop->op_pmoffset = av_len(PL_regex_padav);
3610         PL_regex_pad = AvARRAY(PL_regex_padav);
3611     }
3612 #endif
3613
3614     return CHECKOP(type, pmop);
3615 }
3616
3617 /* Given some sort of match op o, and an expression expr containing a
3618  * pattern, either compile expr into a regex and attach it to o (if it's
3619  * constant), or convert expr into a runtime regcomp op sequence (if it's
3620  * not)
3621  *
3622  * isreg indicates that the pattern is part of a regex construct, eg
3623  * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
3624  * split "pattern", which aren't. In the former case, expr will be a list
3625  * if the pattern contains more than one term (eg /a$b/) or if it contains
3626  * a replacement, ie s/// or tr///.
3627  */
3628
3629 OP *
3630 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
3631 {
3632     dVAR;
3633     PMOP *pm;
3634     LOGOP *rcop;
3635     I32 repl_has_vars = 0;
3636     OP* repl = NULL;
3637     bool reglist;
3638
3639     PERL_ARGS_ASSERT_PMRUNTIME;
3640
3641     if (o->op_type == OP_SUBST || o->op_type == OP_TRANS) {
3642         /* last element in list is the replacement; pop it */
3643         OP* kid;
3644         repl = cLISTOPx(expr)->op_last;
3645         kid = cLISTOPx(expr)->op_first;
3646         while (kid->op_sibling != repl)
3647             kid = kid->op_sibling;
3648         kid->op_sibling = NULL;
3649         cLISTOPx(expr)->op_last = kid;
3650     }
3651
3652     if (isreg && expr->op_type == OP_LIST &&
3653         cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last)
3654     {
3655         /* convert single element list to element */
3656         OP* const oe = expr;
3657         expr = cLISTOPx(oe)->op_first->op_sibling;
3658         cLISTOPx(oe)->op_first->op_sibling = NULL;
3659         cLISTOPx(oe)->op_last = NULL;
3660         op_free(oe);
3661     }
3662
3663     if (o->op_type == OP_TRANS) {
3664         return pmtrans(o, expr, repl);
3665     }
3666
3667     reglist = isreg && expr->op_type == OP_LIST;
3668     if (reglist)
3669         op_null(expr);
3670
3671     PL_hints |= HINT_BLOCK_SCOPE;
3672     pm = (PMOP*)o;
3673
3674     if (expr->op_type == OP_CONST) {
3675         SV *pat = ((SVOP*)expr)->op_sv;
3676         U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
3677
3678         if (o->op_flags & OPf_SPECIAL)
3679             pm_flags |= RXf_SPLIT;
3680
3681         if (DO_UTF8(pat)) {
3682             assert (SvUTF8(pat));
3683         } else if (SvUTF8(pat)) {
3684             /* Not doing UTF-8, despite what the SV says. Is this only if we're
3685                trapped in use 'bytes'?  */
3686             /* Make a copy of the octet sequence, but without the flag on, as
3687                the compiler now honours the SvUTF8 flag on pat.  */
3688             STRLEN len;
3689             const char *const p = SvPV(pat, len);
3690             pat = newSVpvn_flags(p, len, SVs_TEMP);
3691         }
3692
3693         PM_SETRE(pm, CALLREGCOMP(pat, pm_flags));
3694
3695 #ifdef PERL_MAD
3696         op_getmad(expr,(OP*)pm,'e');
3697 #else
3698         op_free(expr);
3699 #endif
3700     }
3701     else {
3702         if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
3703             expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
3704                             ? OP_REGCRESET
3705                             : OP_REGCMAYBE),0,expr);
3706
3707         NewOp(1101, rcop, 1, LOGOP);
3708         rcop->op_type = OP_REGCOMP;
3709         rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
3710         rcop->op_first = scalar(expr);
3711         rcop->op_flags |= OPf_KIDS
3712                             | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
3713                             | (reglist ? OPf_STACKED : 0);
3714         rcop->op_private = 1;
3715         rcop->op_other = o;
3716         if (reglist)
3717             rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
3718
3719         /* /$x/ may cause an eval, since $x might be qr/(?{..})/  */
3720         PL_cv_has_eval = 1;
3721
3722         /* establish postfix order */
3723         if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
3724             LINKLIST(expr);
3725             rcop->op_next = expr;
3726             ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
3727         }
3728         else {
3729             rcop->op_next = LINKLIST(expr);
3730             expr->op_next = (OP*)rcop;
3731         }
3732
3733         prepend_elem(o->op_type, scalar((OP*)rcop), o);
3734     }
3735
3736     if (repl) {
3737         OP *curop;
3738         if (pm->op_pmflags & PMf_EVAL) {
3739             curop = NULL;
3740             if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
3741                 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
3742         }
3743         else if (repl->op_type == OP_CONST)
3744             curop = repl;
3745         else {
3746             OP *lastop = NULL;
3747             for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
3748                 if (curop->op_type == OP_SCOPE
3749                         || curop->op_type == OP_LEAVE
3750                         || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
3751                     if (curop->op_type == OP_GV) {
3752                         GV * const gv = cGVOPx_gv(curop);
3753                         repl_has_vars = 1;
3754                         if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
3755                             break;
3756                     }
3757                     else if (curop->op_type == OP_RV2CV)
3758                         break;
3759                     else if (curop->op_type == OP_RV2SV ||
3760                              curop->op_type == OP_RV2AV ||
3761                              curop->op_type == OP_RV2HV ||
3762                              curop->op_type == OP_RV2GV) {
3763                         if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3764                             break;
3765                     }
3766                     else if (curop->op_type == OP_PADSV ||
3767                              curop->op_type == OP_PADAV ||
3768                              curop->op_type == OP_PADHV ||
3769                              curop->op_type == OP_PADANY)
3770                     {
3771                         repl_has_vars = 1;
3772                     }
3773                     else if (curop->op_type == OP_PUSHRE)
3774                         NOOP; /* Okay here, dangerous in newASSIGNOP */
3775                     else
3776                         break;
3777                 }
3778                 lastop = curop;
3779             }
3780         }
3781         if (curop == repl
3782             && !(repl_has_vars
3783                  && (!PM_GETRE(pm)
3784                      || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
3785         {
3786             pm->op_pmflags |= PMf_CONST;        /* const for long enough */
3787             prepend_elem(o->op_type, scalar(repl), o);
3788         }
3789         else {
3790             if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
3791                 pm->op_pmflags |= PMf_MAYBE_CONST;
3792             }
3793             NewOp(1101, rcop, 1, LOGOP);
3794             rcop->op_type = OP_SUBSTCONT;
3795             rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3796             rcop->op_first = scalar(repl);
3797             rcop->op_flags |= OPf_KIDS;
3798             rcop->op_private = 1;
3799             rcop->op_other = o;
3800
3801             /* establish postfix order */
3802             rcop->op_next = LINKLIST(repl);
3803             repl->op_next = (OP*)rcop;
3804
3805             pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
3806             assert(!(pm->op_pmflags & PMf_ONCE));
3807             pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
3808             rcop->op_next = 0;
3809         }
3810     }
3811
3812     return (OP*)pm;
3813 }
3814
3815 OP *
3816 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3817 {
3818     dVAR;
3819     SVOP *svop;
3820
3821     PERL_ARGS_ASSERT_NEWSVOP;
3822
3823     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
3824         || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3825         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
3826
3827     NewOp(1101, svop, 1, SVOP);
3828     svop->op_type = (OPCODE)type;
3829     svop->op_ppaddr = PL_ppaddr[type];
3830     svop->op_sv = sv;
3831     svop->op_next = (OP*)svop;
3832     svop->op_flags = (U8)flags;
3833     if (PL_opargs[type] & OA_RETSCALAR)
3834         scalar((OP*)svop);
3835     if (PL_opargs[type] & OA_TARGET)
3836         svop->op_targ = pad_alloc(type, SVs_PADTMP);
3837     return CHECKOP(type, svop);
3838 }
3839
3840 #ifdef USE_ITHREADS
3841 OP *
3842 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3843 {
3844     dVAR;
3845     PADOP *padop;
3846
3847     PERL_ARGS_ASSERT_NEWPADOP;
3848
3849     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
3850         || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3851         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
3852
3853     NewOp(1101, padop, 1, PADOP);
3854     padop->op_type = (OPCODE)type;
3855     padop->op_ppaddr = PL_ppaddr[type];
3856     padop->op_padix = pad_alloc(type, SVs_PADTMP);
3857     SvREFCNT_dec(PAD_SVl(padop->op_padix));
3858     PAD_SETSV(padop->op_padix, sv);
3859     assert(sv);
3860     SvPADTMP_on(sv);
3861     padop->op_next = (OP*)padop;
3862     padop->op_flags = (U8)flags;
3863     if (PL_opargs[type] & OA_RETSCALAR)
3864         scalar((OP*)padop);
3865     if (PL_opargs[type] & OA_TARGET)
3866         padop->op_targ = pad_alloc(type, SVs_PADTMP);
3867     return CHECKOP(type, padop);
3868 }
3869 #endif
3870
3871 OP *
3872 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3873 {
3874     dVAR;
3875
3876     PERL_ARGS_ASSERT_NEWGVOP;
3877
3878 #ifdef USE_ITHREADS
3879     GvIN_PAD_on(gv);
3880     return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
3881 #else
3882     return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
3883 #endif
3884 }
3885
3886 OP *
3887 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3888 {
3889     dVAR;
3890     PVOP *pvop;
3891
3892     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3893         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3894
3895     NewOp(1101, pvop, 1, PVOP);
3896     pvop->op_type = (OPCODE)type;
3897     pvop->op_ppaddr = PL_ppaddr[type];
3898     pvop->op_pv = pv;
3899     pvop->op_next = (OP*)pvop;
3900     pvop->op_flags = (U8)flags;
3901     if (PL_opargs[type] & OA_RETSCALAR)
3902         scalar((OP*)pvop);
3903     if (PL_opargs[type] & OA_TARGET)
3904         pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3905     return CHECKOP(type, pvop);
3906 }
3907
3908 #ifdef PERL_MAD
3909 OP*
3910 #else
3911 void
3912 #endif
3913 Perl_package(pTHX_ OP *o)
3914 {
3915     dVAR;
3916     SV *const sv = cSVOPo->op_sv;
3917 #ifdef PERL_MAD
3918     OP *pegop;
3919 #endif
3920
3921     PERL_ARGS_ASSERT_PACKAGE;
3922
3923     save_hptr(&PL_curstash);
3924     save_item(PL_curstname);
3925
3926     PL_curstash = gv_stashsv(sv, GV_ADD);
3927
3928     sv_setsv(PL_curstname, sv);
3929
3930     PL_hints |= HINT_BLOCK_SCOPE;
3931     PL_parser->copline = NOLINE;
3932     PL_parser->expect = XSTATE;
3933
3934 #ifndef PERL_MAD
3935     op_free(o);
3936 #else
3937     if (!PL_madskills) {
3938         op_free(o);
3939         return NULL;
3940     }
3941
3942     pegop = newOP(OP_NULL,0);
3943     op_getmad(o,pegop,'P');
3944     return pegop;
3945 #endif
3946 }
3947
3948 void
3949 Perl_package_version( pTHX_ OP *v )
3950 {
3951     dVAR;
3952     U32 savehints = PL_hints;
3953     PERL_ARGS_ASSERT_PACKAGE_VERSION;
3954     PL_hints &= ~HINT_STRICT_VARS;
3955     sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
3956     PL_hints = savehints;
3957     op_free(v);
3958 }
3959
3960 #ifdef PERL_MAD
3961 OP*
3962 #else
3963 void
3964 #endif
3965 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3966 {
3967     dVAR;
3968     OP *pack;
3969     OP *imop;
3970     OP *veop;
3971 #ifdef PERL_MAD
3972     OP *pegop = newOP(OP_NULL,0);
3973 #endif
3974
3975     PERL_ARGS_ASSERT_UTILIZE;
3976
3977     if (idop->op_type != OP_CONST)
3978         Perl_croak(aTHX_ "Module name must be constant");
3979
3980     if (PL_madskills)
3981         op_getmad(idop,pegop,'U');
3982
3983     veop = NULL;
3984
3985     if (version) {
3986         SV * const vesv = ((SVOP*)version)->op_sv;
3987
3988         if (PL_madskills)
3989             op_getmad(version,pegop,'V');
3990         if (!arg && !SvNIOKp(vesv)) {
3991             arg = version;
3992         }
3993         else {
3994             OP *pack;
3995             SV *meth;
3996
3997             if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3998                 Perl_croak(aTHX_ "Version number must be a constant number");
3999
4000             /* Make copy of idop so we don't free it twice */
4001             pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4002
4003             /* Fake up a method call to VERSION */
4004             meth = newSVpvs_share("VERSION");
4005             veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4006                             append_elem(OP_LIST,
4007                                         prepend_elem(OP_LIST, pack, list(version)),
4008                                         newSVOP(OP_METHOD_NAMED, 0, meth)));
4009         }
4010     }
4011
4012     /* Fake up an import/unimport */
4013     if (arg && arg->op_type == OP_STUB) {
4014         if (PL_madskills)
4015             op_getmad(arg,pegop,'S');
4016         imop = arg;             /* no import on explicit () */
4017     }
4018     else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
4019         imop = NULL;            /* use 5.0; */
4020         if (!aver)
4021             idop->op_private |= OPpCONST_NOVER;
4022     }
4023     else {
4024         SV *meth;
4025
4026         if (PL_madskills)
4027             op_getmad(arg,pegop,'A');
4028
4029         /* Make copy of idop so we don't free it twice */
4030         pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4031
4032         /* Fake up a method call to import/unimport */
4033         meth = aver
4034             ? newSVpvs_share("import") : newSVpvs_share("unimport");
4035         imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4036                        append_elem(OP_LIST,
4037                                    prepend_elem(OP_LIST, pack, list(arg)),
4038                                    newSVOP(OP_METHOD_NAMED, 0, meth)));
4039     }
4040
4041     /* Fake up the BEGIN {}, which does its thing immediately. */
4042     newATTRSUB(floor,
4043         newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
4044         NULL,
4045         NULL,
4046         append_elem(OP_LINESEQ,
4047             append_elem(OP_LINESEQ,
4048                 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
4049                 newSTATEOP(0, NULL, veop)),
4050             newSTATEOP(0, NULL, imop) ));
4051
4052     /* The "did you use incorrect case?" warning used to be here.
4053      * The problem is that on case-insensitive filesystems one
4054      * might get false positives for "use" (and "require"):
4055      * "use Strict" or "require CARP" will work.  This causes
4056      * portability problems for the script: in case-strict
4057      * filesystems the script will stop working.
4058      *
4059      * The "incorrect case" warning checked whether "use Foo"
4060      * imported "Foo" to your namespace, but that is wrong, too:
4061      * there is no requirement nor promise in the language that
4062      * a Foo.pm should or would contain anything in package "Foo".
4063      *
4064      * There is very little Configure-wise that can be done, either:
4065      * the case-sensitivity of the build filesystem of Perl does not
4066      * help in guessing the case-sensitivity of the runtime environment.
4067      */
4068
4069     PL_hints |= HINT_BLOCK_SCOPE;
4070     PL_parser->copline = NOLINE;
4071     PL_parser->expect = XSTATE;
4072     PL_cop_seqmax++; /* Purely for B::*'s benefit */
4073
4074 #ifdef PERL_MAD
4075     if (!PL_madskills) {
4076         /* FIXME - don't allocate pegop if !PL_madskills */
4077         op_free(pegop);
4078         return NULL;
4079     }
4080     return pegop;
4081 #endif
4082 }
4083
4084 /*
4085 =head1 Embedding Functions
4086
4087 =for apidoc load_module
4088
4089 Loads the module whose name is pointed to by the string part of name.
4090 Note that the actual module name, not its filename, should be given.
4091 Eg, "Foo::Bar" instead of "Foo/Bar.pm".  flags can be any of
4092 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
4093 (or 0 for no flags). ver, if specified, provides version semantics
4094 similar to C<use Foo::Bar VERSION>.  The optional trailing SV*
4095 arguments can be used to specify arguments to the module's import()
4096 method, similar to C<use Foo::Bar VERSION LIST>.  They must be
4097 terminated with a final NULL pointer.  Note that this list can only
4098 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
4099 Otherwise at least a single NULL pointer to designate the default
4100 import list is required.
4101
4102 =cut */
4103
4104 void
4105 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
4106 {
4107     va_list args;
4108
4109     PERL_ARGS_ASSERT_LOAD_MODULE;
4110
4111     va_start(args, ver);
4112     vload_module(flags, name, ver, &args);
4113     va_end(args);
4114 }
4115
4116 #ifdef PERL_IMPLICIT_CONTEXT
4117 void
4118 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
4119 {
4120     dTHX;
4121     va_list args;
4122     PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
4123     va_start(args, ver);
4124     vload_module(flags, name, ver, &args);
4125     va_end(args);
4126 }
4127 #endif
4128
4129 void
4130 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
4131 {
4132     dVAR;
4133     OP *veop, *imop;
4134     OP * const modname = newSVOP(OP_CONST, 0, name);
4135
4136     PERL_ARGS_ASSERT_VLOAD_MODULE;
4137
4138     modname->op_private |= OPpCONST_BARE;
4139     if (ver) {
4140         veop = newSVOP(OP_CONST, 0, ver);
4141     }
4142     else
4143         veop = NULL;
4144     if (flags & PERL_LOADMOD_NOIMPORT) {
4145         imop = sawparens(newNULLLIST());
4146     }
4147     else if (flags & PERL_LOADMOD_IMPORT_OPS) {
4148         imop = va_arg(*args, OP*);
4149     }
4150     else {
4151         SV *sv;
4152         imop = NULL;
4153         sv = va_arg(*args, SV*);
4154         while (sv) {
4155             imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
4156             sv = va_arg(*args, SV*);
4157         }
4158     }
4159
4160     /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
4161      * that it has a PL_parser to play with while doing that, and also
4162      * that it doesn't mess with any existing parser, by creating a tmp
4163      * new parser with lex_start(). This won't actually be used for much,
4164      * since pp_require() will create another parser for the real work. */
4165
4166     ENTER;
4167     SAVEVPTR(PL_curcop);
4168     lex_start(NULL, NULL, FALSE);
4169     utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
4170             veop, modname, imop);
4171     LEAVE;
4172 }
4173
4174 OP *
4175 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
4176 {
4177     dVAR;
4178     OP *doop;
4179     GV *gv = NULL;
4180
4181     PERL_ARGS_ASSERT_DOFILE;
4182
4183     if (!force_builtin) {
4184         gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
4185         if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
4186             GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
4187             gv = gvp ? *gvp : NULL;
4188         }
4189     }
4190
4191     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
4192         doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
4193                                append_elem(OP_LIST, term,
4194                                            scalar(newUNOP(OP_RV2CV, 0,
4195                                                           newGVOP(OP_GV, 0, gv))))));
4196     }
4197     else {
4198         doop = newUNOP(OP_DOFILE, 0, scalar(term));
4199     }
4200     return doop;
4201 }
4202
4203 OP *
4204 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
4205 {
4206     return newBINOP(OP_LSLICE, flags,
4207             list(force_list(subscript)),
4208             list(force_list(listval)) );
4209 }
4210
4211 STATIC I32
4212 S_is_list_assignment(pTHX_ register const OP *o)
4213 {
4214     unsigned type;
4215     U8 flags;
4216
4217     if (!o)
4218         return TRUE;
4219
4220     if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
4221         o = cUNOPo->op_first;
4222
4223     flags = o->op_flags;
4224     type = o->op_type;
4225     if (type == OP_COND_EXPR) {
4226         const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
4227         const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
4228
4229         if (t && f)
4230             return TRUE;
4231         if (t || f)
4232             yyerror("Assignment to both a list and a scalar");
4233         return FALSE;
4234     }
4235
4236     if (type == OP_LIST &&
4237         (flags & OPf_WANT) == OPf_WANT_SCALAR &&
4238         o->op_private & OPpLVAL_INTRO)
4239         return FALSE;
4240
4241     if (type == OP_LIST || flags & OPf_PARENS ||
4242         type == OP_RV2AV || type == OP_RV2HV ||
4243         type == OP_ASLICE || type == OP_HSLICE)
4244         return TRUE;
4245
4246     if (type == OP_PADAV || type == OP_PADHV)
4247         return TRUE;
4248
4249     if (type == OP_RV2SV)
4250         return FALSE;
4251
4252     return FALSE;
4253 }
4254
4255 OP *
4256 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
4257 {
4258     dVAR;
4259     OP *o;
4260
4261     if (optype) {
4262         if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
4263             return newLOGOP(optype, 0,
4264                 mod(scalar(left), optype),
4265                 newUNOP(OP_SASSIGN, 0, scalar(right)));
4266         }
4267         else {
4268             return newBINOP(optype, OPf_STACKED,
4269                 mod(scalar(left), optype), scalar(right));
4270         }
4271     }
4272
4273     if (is_list_assignment(left)) {
4274         static const char no_list_state[] = "Initialization of state variables"
4275             " in list context currently forbidden";
4276         OP *curop;
4277         bool maybe_common_vars = TRUE;
4278
4279         PL_modcount = 0;
4280         /* Grandfathering $[ assignment here.  Bletch.*/
4281         /* Only simple assignments like C<< ($[) = 1 >> are allowed */
4282         PL_eval_start = (left->op_type == OP_CONST) ? right : NULL;
4283         left = mod(left, OP_AASSIGN);
4284         if (PL_eval_start)
4285             PL_eval_start = 0;
4286         else if (left->op_type == OP_CONST) {
4287             /* FIXME for MAD */
4288             /* Result of assignment is always 1 (or we'd be dead already) */
4289             return newSVOP(OP_CONST, 0, newSViv(1));
4290         }
4291         curop = list(force_list(left));
4292         o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
4293         o->op_private = (U8)(0 | (flags >> 8));
4294
4295         if ((left->op_type == OP_LIST
4296              || (left->op_type == OP_NULL && left->op_targ == OP_LIST)))
4297         {
4298             OP* lop = ((LISTOP*)left)->op_first;
4299             maybe_common_vars = FALSE;
4300             while (lop) {
4301                 if (lop->op_type == OP_PADSV ||
4302                     lop->op_type == OP_PADAV ||
4303                     lop->op_type == OP_PADHV ||
4304                     lop->op_type == OP_PADANY) {
4305                     if (!(lop->op_private & OPpLVAL_INTRO))
4306                         maybe_common_vars = TRUE;
4307
4308                     if (lop->op_private & OPpPAD_STATE) {
4309                         if (left->op_private & OPpLVAL_INTRO) {
4310                             /* Each variable in state($a, $b, $c) = ... */
4311                         }
4312                         else {
4313                             /* Each state variable in
4314                                (state $a, my $b, our $c, $d, undef) = ... */
4315                         }
4316                         yyerror(no_list_state);
4317                     } else {
4318                         /* Each my variable in
4319                            (state $a, my $b, our $c, $d, undef) = ... */
4320                     }
4321                 } else if (lop->op_type == OP_UNDEF ||
4322                            lop->op_type == OP_PUSHMARK) {
4323                     /* undef may be interesting in
4324                        (state $a, undef, state $c) */
4325                 } else {
4326                     /* Other ops in the list. */
4327                     maybe_common_vars = TRUE;
4328                 }
4329                 lop = lop->op_sibling;
4330             }
4331         }
4332         else if ((left->op_private & OPpLVAL_INTRO)
4333                 && (   left->op_type == OP_PADSV
4334                     || left->op_type == OP_PADAV
4335                     || left->op_type == OP_PADHV
4336                     || left->op_type == OP_PADANY))
4337         {
4338             if (left->op_type == OP_PADSV) maybe_common_vars = FALSE;
4339             if (left->op_private & OPpPAD_STATE) {
4340                 /* All single variable list context state assignments, hence
4341                    state ($a) = ...
4342                    (state $a) = ...
4343                    state @a = ...
4344                    state (@a) = ...
4345                    (state @a) = ...
4346                    state %a = ...
4347                    state (%a) = ...
4348                    (state %a) = ...
4349                 */
4350                 yyerror(no_list_state);
4351             }
4352         }
4353
4354         /* PL_generation sorcery:
4355          * an assignment like ($a,$b) = ($c,$d) is easier than
4356          * ($a,$b) = ($c,$a), since there is no need for temporary vars.
4357          * To detect whether there are common vars, the global var
4358          * PL_generation is incremented for each assign op we compile.
4359          * Then, while compiling the assign op, we run through all the
4360          * variables on both sides of the assignment, setting a spare slot
4361          * in each of them to PL_generation. If any of them already have
4362          * that value, we know we've got commonality.  We could use a
4363          * single bit marker, but then we'd have to make 2 passes, first
4364          * to clear the flag, then to test and set it.  To find somewhere
4365          * to store these values, evil chicanery is done with SvUVX().
4366          */
4367
4368         if (maybe_common_vars) {
4369             OP *lastop = o;
4370             PL_generation++;
4371             for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4372                 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
4373                     if (curop->op_type == OP_GV) {
4374                         GV *gv = cGVOPx_gv(curop);
4375                         if (gv == PL_defgv
4376                             || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4377                             break;
4378                         GvASSIGN_GENERATION_set(gv, PL_generation);
4379                     }
4380                     else if (curop->op_type == OP_PADSV ||
4381                              curop->op_type == OP_PADAV ||
4382                              curop->op_type == OP_PADHV ||
4383                              curop->op_type == OP_PADANY)
4384                     {
4385                         if (PAD_COMPNAME_GEN(curop->op_targ)
4386                                                     == (STRLEN)PL_generation)
4387                             break;
4388                         PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
4389
4390                     }
4391                     else if (curop->op_type == OP_RV2CV)
4392                         break;
4393                     else if (curop->op_type == OP_RV2SV ||
4394                              curop->op_type == OP_RV2AV ||
4395                              curop->op_type == OP_RV2HV ||
4396                              curop->op_type == OP_RV2GV) {
4397                         if (lastop->op_type != OP_GV)   /* funny deref? */
4398                             break;
4399                     }
4400                     else if (curop->op_type == OP_PUSHRE) {
4401 #ifdef USE_ITHREADS
4402                         if (((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff) {
4403                             GV *const gv = MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff));
4404                             if (gv == PL_defgv
4405                                 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4406                                 break;
4407                             GvASSIGN_GENERATION_set(gv, PL_generation);
4408                         }
4409 #else
4410                         GV *const gv
4411                             = ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv;
4412                         if (gv) {
4413                             if (gv == PL_defgv
4414                                 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4415                                 break;
4416                             GvASSIGN_GENERATION_set(gv, PL_generation);
4417                         }
4418 #endif
4419                     }
4420                     else
4421                         break;
4422                 }
4423                 lastop = curop;
4424             }
4425             if (curop != o)
4426                 o->op_private |= OPpASSIGN_COMMON;
4427         }
4428
4429         if (right && right->op_type == OP_SPLIT && !PL_madskills) {
4430             OP* tmpop = ((LISTOP*)right)->op_first;
4431             if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
4432                 PMOP * const pm = (PMOP*)tmpop;
4433                 if (left->op_type == OP_RV2AV &&
4434                     !(left->op_private & OPpLVAL_INTRO) &&
4435                     !(o->op_private & OPpASSIGN_COMMON) )
4436                 {
4437                     tmpop = ((UNOP*)left)->op_first;
4438                     if (tmpop->op_type == OP_GV
4439 #ifdef USE_ITHREADS
4440                         && !pm->op_pmreplrootu.op_pmtargetoff
4441 #else
4442                         && !pm->op_pmreplrootu.op_pmtargetgv
4443 #endif
4444                         ) {
4445 #ifdef USE_ITHREADS
4446                         pm->op_pmreplrootu.op_pmtargetoff
4447                             = cPADOPx(tmpop)->op_padix;
4448                         cPADOPx(tmpop)->op_padix = 0;   /* steal it */
4449 #else
4450                         pm->op_pmreplrootu.op_pmtargetgv
4451                             = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
4452                         cSVOPx(tmpop)->op_sv = NULL;    /* steal it */
4453 #endif
4454                         pm->op_pmflags |= PMf_ONCE;
4455                         tmpop = cUNOPo->op_first;       /* to list (nulled) */
4456                         tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
4457                         tmpop->op_sibling = NULL;       /* don't free split */
4458                         right->op_next = tmpop->op_next;  /* fix starting loc */
4459                         op_free(o);                     /* blow off assign */
4460                         right->op_flags &= ~OPf_WANT;
4461                                 /* "I don't know and I don't care." */
4462                         return right;
4463                     }
4464                 }
4465                 else {
4466                    if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
4467                       ((LISTOP*)right)->op_last->op_type == OP_CONST)
4468                     {
4469                         SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
4470                         if (SvIOK(sv) && SvIVX(sv) == 0)
4471                             sv_setiv(sv, PL_modcount+1);
4472                     }
4473                 }
4474             }
4475         }
4476         return o;
4477     }
4478     if (!right)
4479         right = newOP(OP_UNDEF, 0);
4480     if (right->op_type == OP_READLINE) {
4481         right->op_flags |= OPf_STACKED;
4482         return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
4483     }
4484     else {
4485         PL_eval_start = right;  /* Grandfathering $[ assignment here.  Bletch.*/
4486         o = newBINOP(OP_SASSIGN, flags,
4487             scalar(right), mod(scalar(left), OP_SASSIGN) );
4488         if (PL_eval_start)
4489             PL_eval_start = 0;
4490         else {
4491             if (!PL_madskills) { /* assignment to $[ is ignored when making a mad dump */
4492                 deprecate("assignment to $[");
4493                 op_free(o);
4494                 o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling)));
4495                 o->op_private |= OPpCONST_ARYBASE;
4496             }
4497         }
4498     }
4499     return o;
4500 }
4501
4502 OP *
4503 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
4504 {
4505     dVAR;
4506     const U32 seq = intro_my();
4507     register COP *cop;
4508
4509     NewOp(1101, cop, 1, COP);
4510     if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
4511         cop->op_type = OP_DBSTATE;
4512         cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
4513     }
4514     else {
4515         cop->op_type = OP_NEXTSTATE;
4516         cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
4517     }
4518     cop->op_flags = (U8)flags;
4519     CopHINTS_set(cop, PL_hints);
4520 #ifdef NATIVE_HINTS
4521     cop->op_private |= NATIVE_HINTS;
4522 #endif
4523     CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
4524     cop->op_next = (OP*)cop;
4525
4526     cop->cop_seq = seq;
4527     /* CopARYBASE is now "virtual", in that it's stored as a flag bit in
4528        CopHINTS and a possible value in cop_hints_hash, so no need to copy it.
4529     */
4530     cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
4531     cop->cop_hints_hash = PL_curcop->cop_hints_hash;
4532     if (cop->cop_hints_hash) {
4533         HINTS_REFCNT_LOCK;
4534         cop->cop_hints_hash->refcounted_he_refcnt++;
4535         HINTS_REFCNT_UNLOCK;
4536     }
4537     if (label) {
4538         cop->cop_hints_hash
4539             = Perl_store_cop_label(aTHX_ cop->cop_hints_hash, label);
4540                                                      
4541         PL_hints |= HINT_BLOCK_SCOPE;
4542         /* It seems that we need to defer freeing this pointer, as other parts
4543            of the grammar end up wanting to copy it after this op has been
4544            created. */
4545         SAVEFREEPV(label);
4546     }
4547
4548     if (PL_parser && PL_parser->copline == NOLINE)
4549         CopLINE_set(cop, CopLINE(PL_curcop));
4550     else {
4551         CopLINE_set(cop, PL_parser->copline);
4552         if (PL_parser)
4553             PL_parser->copline = NOLINE;
4554     }
4555 #ifdef USE_ITHREADS
4556     CopFILE_set(cop, CopFILE(PL_curcop));       /* XXX share in a pvtable? */
4557 #else
4558     CopFILEGV_set(cop, CopFILEGV(PL_curcop));
4559 #endif
4560     CopSTASH_set(cop, PL_curstash);
4561
4562     if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash) {
4563         /* this line can have a breakpoint - store the cop in IV */
4564         AV *av = CopFILEAVx(PL_curcop);
4565         if (av) {
4566             SV * const * const svp = av_fetch(av, (I32)CopLINE(cop), FALSE);
4567             if (svp && *svp != &PL_sv_undef ) {
4568                 (void)SvIOK_on(*svp);
4569                 SvIV_set(*svp, PTR2IV(cop));
4570             }
4571         }
4572     }
4573
4574     if (flags & OPf_SPECIAL)
4575         op_null((OP*)cop);
4576     return prepend_elem(OP_LINESEQ, (OP*)cop, o);
4577 }
4578
4579
4580 OP *
4581 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
4582 {
4583     dVAR;
4584
4585     PERL_ARGS_ASSERT_NEWLOGOP;
4586
4587     return new_logop(type, flags, &first, &other);
4588 }
4589
4590 STATIC OP *
4591 S_search_const(pTHX_ OP *o)
4592 {
4593     PERL_ARGS_ASSERT_SEARCH_CONST;
4594
4595     switch (o->op_type) {
4596         case OP_CONST:
4597             return o;
4598         case OP_NULL:
4599             if (o->op_flags & OPf_KIDS)
4600                 return search_const(cUNOPo->op_first);
4601             break;
4602         case OP_LEAVE:
4603         case OP_SCOPE:
4604         case OP_LINESEQ:
4605         {
4606             OP *kid;
4607             if (!(o->op_flags & OPf_KIDS))
4608                 return NULL;
4609             kid = cLISTOPo->op_first;
4610             do {
4611                 switch (kid->op_type) {
4612                     case OP_ENTER:
4613                     case OP_NULL:
4614                     case OP_NEXTSTATE:
4615                         kid = kid->op_sibling;
4616                         break;
4617                     default:
4618                         if (kid != cLISTOPo->op_last)
4619                             return NULL;
4620                         goto last;
4621                 }
4622             } while (kid);
4623             if (!kid)
4624                 kid = cLISTOPo->op_last;
4625 last:
4626             return search_const(kid);
4627         }
4628     }
4629
4630     return NULL;
4631 }
4632
4633 STATIC OP *
4634 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
4635 {
4636     dVAR;
4637     LOGOP *logop;
4638     OP *o;
4639     OP *first;
4640     OP *other;
4641     OP *cstop = NULL;
4642     int prepend_not = 0;
4643
4644     PERL_ARGS_ASSERT_NEW_LOGOP;
4645
4646     first = *firstp;
4647     other = *otherp;
4648
4649     if (type == OP_XOR)         /* Not short circuit, but here by precedence. */
4650         return newBINOP(type, flags, scalar(first), scalar(other));
4651
4652     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP);
4653
4654     scalarboolean(first);
4655     /* optimize AND and OR ops that have NOTs as children */
4656     if (first->op_type == OP_NOT
4657         && (first->op_flags & OPf_KIDS)
4658         && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
4659             || (other->op_type == OP_NOT))  /* if (!$x && !$y) { } */
4660         && !PL_madskills) {
4661         if (type == OP_AND || type == OP_OR) {
4662             if (type == OP_AND)
4663                 type = OP_OR;
4664             else
4665                 type = OP_AND;
4666             op_null(first);
4667             if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
4668                 op_null(other);
4669                 prepend_not = 1; /* prepend a NOT op later */
4670             }
4671         }
4672     }
4673     /* search for a constant op that could let us fold the test */
4674     if ((cstop = search_const(first))) {
4675         if (cstop->op_private & OPpCONST_STRICT)
4676             no_bareword_allowed(cstop);
4677         else if ((cstop->op_private & OPpCONST_BARE))
4678                 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
4679         if ((type == OP_AND &&  SvTRUE(((SVOP*)cstop)->op_sv)) ||
4680             (type == OP_OR  && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
4681             (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
4682             *firstp = NULL;
4683             if (other->op_type == OP_CONST)
4684                 other->op_private |= OPpCONST_SHORTCIRCUIT;
4685             if (PL_madskills) {
4686                 OP *newop = newUNOP(OP_NULL, 0, other);
4687                 op_getmad(first, newop, '1');
4688                 newop->op_targ = type;  /* set "was" field */
4689                 return newop;
4690             }
4691             op_free(first);
4692             if (other->op_type == OP_LEAVE)
4693                 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
4694             return other;
4695         }
4696         else {
4697             /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
4698             const OP *o2 = other;
4699             if ( ! (o2->op_type == OP_LIST
4700                     && (( o2 = cUNOPx(o2)->op_first))
4701                     && o2->op_type == OP_PUSHMARK
4702                     && (( o2 = o2->op_sibling)) )
4703             )
4704                 o2 = other;
4705             if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
4706                         || o2->op_type == OP_PADHV)
4707                 && o2->op_private & OPpLVAL_INTRO
4708                 && !(o2->op_private & OPpPAD_STATE))
4709             {
4710                 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
4711                                  "Deprecated use of my() in false conditional");
4712             }
4713
4714             *otherp = NULL;
4715             if (first->op_type == OP_CONST)
4716                 first->op_private |= OPpCONST_SHORTCIRCUIT;
4717             if (PL_madskills) {
4718                 first = newUNOP(OP_NULL, 0, first);
4719                 op_getmad(other, first, '2');
4720                 first->op_targ = type;  /* set "was" field */
4721             }
4722             else
4723                 op_free(other);
4724             return first;
4725         }
4726     }
4727     else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
4728         && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
4729     {
4730         const OP * const k1 = ((UNOP*)first)->op_first;
4731         const OP * const k2 = k1->op_sibling;
4732         OPCODE warnop = 0;
4733         switch (first->op_type)
4734         {
4735         case OP_NULL:
4736             if (k2 && k2->op_type == OP_READLINE
4737                   && (k2->op_flags & OPf_STACKED)
4738                   && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4739             {
4740                 warnop = k2->op_type;
4741             }
4742             break;
4743
4744         case OP_SASSIGN:
4745             if (k1->op_type == OP_READDIR
4746                   || k1->op_type == OP_GLOB
4747                   || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4748                   || k1->op_type == OP_EACH)
4749             {
4750                 warnop = ((k1->op_type == OP_NULL)
4751                           ? (OPCODE)k1->op_targ : k1->op_type);
4752             }
4753             break;
4754         }
4755         if (warnop) {
4756             const line_t oldline = CopLINE(PL_curcop);
4757             CopLINE_set(PL_curcop, PL_parser->copline);
4758             Perl_warner(aTHX_ packWARN(WARN_MISC),
4759                  "Value of %s%s can be \"0\"; test with defined()",
4760                  PL_op_desc[warnop],
4761                  ((warnop == OP_READLINE || warnop == OP_GLOB)
4762                   ? " construct" : "() operator"));
4763             CopLINE_set(PL_curcop, oldline);
4764         }
4765     }
4766
4767     if (!other)
4768         return first;
4769
4770     if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
4771         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
4772
4773     NewOp(1101, logop, 1, LOGOP);
4774
4775     logop->op_type = (OPCODE)type;
4776     logop->op_ppaddr = PL_ppaddr[type];
4777     logop->op_first = first;
4778     logop->op_flags = (U8)(flags | OPf_KIDS);
4779     logop->op_other = LINKLIST(other);
4780     logop->op_private = (U8)(1 | (flags >> 8));
4781
4782     /* establish postfix order */
4783     logop->op_next = LINKLIST(first);
4784     first->op_next = (OP*)logop;
4785     first->op_sibling = other;
4786
4787     CHECKOP(type,logop);
4788
4789     o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 0, (OP*)logop);
4790     other->op_next = o;
4791
4792     return o;
4793 }
4794
4795 OP *
4796 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
4797 {
4798     dVAR;
4799     LOGOP *logop;
4800     OP *start;
4801     OP *o;
4802     OP *cstop;
4803
4804     PERL_ARGS_ASSERT_NEWCONDOP;
4805
4806     if (!falseop)
4807         return newLOGOP(OP_AND, 0, first, trueop);
4808     if (!trueop)
4809         return newLOGOP(OP_OR, 0, first, falseop);
4810
4811     scalarboolean(first);
4812     if ((cstop = search_const(first))) {
4813         /* Left or right arm of the conditional?  */
4814         const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
4815         OP *live = left ? trueop : falseop;
4816         OP *const dead = left ? falseop : trueop;
4817         if (cstop->op_private & OPpCONST_BARE &&
4818             cstop->op_private & OPpCONST_STRICT) {
4819             no_bareword_allowed(cstop);
4820         }
4821         if (PL_madskills) {
4822             /* This is all dead code when PERL_MAD is not defined.  */
4823             live = newUNOP(OP_NULL, 0, live);
4824             op_getmad(first, live, 'C');
4825             op_getmad(dead, live, left ? 'e' : 't');
4826         } else {
4827             op_free(first);
4828             op_free(dead);
4829         }
4830         if (live->op_type == OP_LEAVE)
4831             live = newUNOP(OP_NULL, OPf_SPECIAL, live);
4832         return live;
4833     }
4834     NewOp(1101, logop, 1, LOGOP);
4835     logop->op_type = OP_COND_EXPR;
4836     logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
4837     logop->op_first = first;
4838     logop->op_flags = (U8)(flags | OPf_KIDS);
4839     logop->op_private = (U8)(1 | (flags >> 8));
4840     logop->op_other = LINKLIST(trueop);
4841     logop->op_next = LINKLIST(falseop);
4842
4843     CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
4844             logop);
4845
4846     /* establish postfix order */
4847     start = LINKLIST(first);
4848     first->op_next = (OP*)logop;
4849
4850     first->op_sibling = trueop;
4851     trueop->op_sibling = falseop;
4852     o = newUNOP(OP_NULL, 0, (OP*)logop);
4853
4854     trueop->op_next = falseop->op_next = o;
4855
4856     o->op_next = start;
4857     return o;
4858 }
4859
4860 OP *
4861 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
4862 {
4863     dVAR;
4864     LOGOP *range;
4865     OP *flip;
4866     OP *flop;
4867     OP *leftstart;
4868     OP *o;
4869
4870     PERL_ARGS_ASSERT_NEWRANGE;
4871
4872     NewOp(1101, range, 1, LOGOP);
4873
4874     range->op_type = OP_RANGE;
4875     range->op_ppaddr = PL_ppaddr[OP_RANGE];
4876     range->op_first = left;
4877     range->op_flags = OPf_KIDS;
4878     leftstart = LINKLIST(left);
4879     range->op_other = LINKLIST(right);
4880     range->op_private = (U8)(1 | (flags >> 8));
4881
4882     left->op_sibling = right;
4883
4884     range->op_next = (OP*)range;
4885     flip = newUNOP(OP_FLIP, flags, (OP*)range);
4886     flop = newUNOP(OP_FLOP, 0, flip);
4887     o = newUNOP(OP_NULL, 0, flop);
4888     linklist(flop);
4889     range->op_next = leftstart;
4890
4891     left->op_next = flip;
4892     right->op_next = flop;
4893
4894     range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
4895     sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
4896     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
4897     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
4898
4899     flip->op_private =  left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
4900     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
4901
4902     flip->op_next = o;
4903     if (!flip->op_private || !flop->op_private)
4904         linklist(o);            /* blow off optimizer unless constant */
4905
4906     return o;
4907 }
4908
4909 OP *
4910 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
4911 {
4912     dVAR;
4913     OP* listop;
4914     OP* o;
4915     const bool once = block && block->op_flags & OPf_SPECIAL &&
4916       (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
4917
4918     PERL_UNUSED_ARG(debuggable);
4919
4920     if (expr) {
4921         if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
4922             return block;       /* do {} while 0 does once */
4923         if (expr->op_type == OP_READLINE
4924             || expr->op_type == OP_READDIR
4925             || expr->op_type == OP_GLOB
4926             || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
4927             expr = newUNOP(OP_DEFINED, 0,
4928                 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
4929         } else if (expr->op_flags & OPf_KIDS) {
4930             const OP * const k1 = ((UNOP*)expr)->op_first;
4931             const OP * const k2 = k1 ? k1->op_sibling : NULL;
4932             switch (expr->op_type) {
4933               case OP_NULL:
4934                 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
4935                       && (k2->op_flags & OPf_STACKED)
4936                       && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4937                     expr = newUNOP(OP_DEFINED, 0, expr);
4938                 break;
4939
4940               case OP_SASSIGN:
4941                 if (k1 && (k1->op_type == OP_READDIR
4942                       || k1->op_type == OP_GLOB
4943                       || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4944                       || k1->op_type == OP_EACH))
4945                     expr = newUNOP(OP_DEFINED, 0, expr);
4946                 break;
4947             }
4948         }
4949     }
4950
4951     /* if block is null, the next append_elem() would put UNSTACK, a scalar
4952      * op, in listop. This is wrong. [perl #27024] */
4953     if (!block)
4954         block = newOP(OP_NULL, 0);
4955     listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
4956     o = new_logop(OP_AND, 0, &expr, &listop);
4957
4958     if (listop)
4959         ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
4960
4961     if (once && o != listop)
4962         o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
4963
4964     if (o == listop)
4965         o = newUNOP(OP_NULL, 0, o);     /* or do {} while 1 loses outer block */
4966
4967     o->op_flags |= flags;
4968     o = scope(o);
4969     o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
4970     return o;
4971 }
4972
4973 OP *
4974 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32
4975 whileline, OP *expr, OP *block, OP *cont, I32 has_my)
4976 {
4977     dVAR;
4978     OP *redo;
4979     OP *next = NULL;
4980     OP *listop;
4981     OP *o;
4982     U8 loopflags = 0;
4983
4984     PERL_UNUSED_ARG(debuggable);
4985
4986     if (expr) {
4987         if (expr->op_type == OP_READLINE
4988          || expr->op_type == OP_READDIR
4989          || expr->op_type == OP_GLOB
4990                      || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
4991             expr = newUNOP(OP_DEFINED, 0,
4992                 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
4993         } else if (expr->op_flags & OPf_KIDS) {
4994             const OP * const k1 = ((UNOP*)expr)->op_first;
4995             const OP * const k2 = (k1) ? k1->op_sibling : NULL;
4996             switch (expr->op_type) {
4997               case OP_NULL:
4998                 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
4999                       && (k2->op_flags & OPf_STACKED)
5000                       && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
5001                     expr = newUNOP(OP_DEFINED, 0, expr);
5002                 break;
5003
5004               case OP_SASSIGN:
5005                 if (k1 && (k1->op_type == OP_READDIR
5006                       || k1->op_type == OP_GLOB
5007                       || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
5008                       || k1->op_type == OP_EACH))
5009                     expr = newUNOP(OP_DEFINED, 0, expr);
5010                 break;
5011             }
5012         }
5013     }
5014
5015     if (!block)
5016         block = newOP(OP_NULL, 0);
5017     else if (cont || has_my) {
5018         block = scope(block);
5019     }
5020
5021     if (cont) {
5022         next = LINKLIST(cont);
5023     }
5024     if (expr) {
5025         OP * const unstack = newOP(OP_UNSTACK, 0);
5026         if (!next)
5027             next = unstack;
5028         cont = append_elem(OP_LINESEQ, cont, unstack);
5029     }
5030
5031     assert(block);
5032     listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
5033     assert(listop);
5034     redo = LINKLIST(listop);
5035
5036     if (expr) {
5037         PL_parser->copline = (line_t)whileline;
5038         scalar(listop);
5039         o = new_logop(OP_AND, 0, &expr, &listop);
5040         if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
5041             op_free(expr);              /* oops, it's a while (0) */
5042             op_free((OP*)loop);
5043             return NULL;                /* listop already freed by new_logop */
5044         }
5045         if (listop)
5046             ((LISTOP*)listop)->op_last->op_next =
5047                 (o == listop ? redo : LINKLIST(o));
5048     }
5049     else
5050         o = listop;
5051
5052     if (!loop) {
5053         NewOp(1101,loop,1,LOOP);
5054         loop->op_type = OP_ENTERLOOP;
5055         loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
5056         loop->op_private = 0;
5057         loop->op_next = (OP*)loop;
5058     }
5059
5060     o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
5061
5062     loop->op_redoop = redo;
5063     loop->op_lastop = o;
5064     o->op_private |= loopflags;
5065
5066     if (next)
5067         loop->op_nextop = next;
5068     else
5069         loop->op_nextop = o;
5070
5071     o->op_flags |= flags;
5072     o->op_private |= (flags >> 8);
5073     return o;
5074 }
5075
5076 OP *
5077 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
5078 {
5079     dVAR;
5080     LOOP *loop;
5081     OP *wop;
5082     PADOFFSET padoff = 0;
5083     I32 iterflags = 0;
5084     I32 iterpflags = 0;
5085     OP *madsv = NULL;
5086
5087     PERL_ARGS_ASSERT_NEWFOROP;
5088
5089     if (sv) {
5090         if (sv->op_type == OP_RV2SV) {  /* symbol table variable */
5091             iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
5092             sv->op_type = OP_RV2GV;
5093             sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
5094
5095             /* The op_type check is needed to prevent a possible segfault
5096              * if the loop variable is undeclared and 'strict vars' is in
5097              * effect. This is illegal but is nonetheless parsed, so we
5098              * may reach this point with an OP_CONST where we're expecting
5099              * an OP_GV.
5100              */
5101             if (cUNOPx(sv)->op_first->op_type == OP_GV
5102              && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
5103                 iterpflags |= OPpITER_DEF;
5104         }
5105         else if (sv->op_type == OP_PADSV) { /* private variable */
5106             iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
5107             padoff = sv->op_targ;
5108             if (PL_madskills)
5109                 madsv = sv;
5110             else {
5111                 sv->op_targ = 0;
5112                 op_free(sv);
5113             }
5114             sv = NULL;
5115         }
5116         else
5117             Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
5118         if (padoff) {
5119             SV *const namesv = PAD_COMPNAME_SV(padoff);
5120             STRLEN len;
5121             const char *const name = SvPV_const(namesv, len);
5122
5123             if (len == 2 && name[0] == '$' && name[1] == '_')
5124                 iterpflags |= OPpITER_DEF;
5125         }
5126     }
5127     else {
5128         const PADOFFSET offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0);
5129         if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
5130             sv = newGVOP(OP_GV, 0, PL_defgv);
5131         }
5132         else {
5133             padoff = offset;
5134         }
5135         iterpflags |= OPpITER_DEF;
5136     }
5137     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
5138         expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
5139         iterflags |= OPf_STACKED;
5140     }
5141     else if (expr->op_type == OP_NULL &&
5142              (expr->op_flags & OPf_KIDS) &&
5143              ((BINOP*)expr)->op_first->op_type == OP_FLOP)
5144     {
5145         /* Basically turn for($x..$y) into the same as for($x,$y), but we
5146          * set the STACKED flag to indicate that these values are to be
5147          * treated as min/max values by 'pp_iterinit'.
5148          */
5149         const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
5150         LOGOP* const range = (LOGOP*) flip->op_first;
5151         OP* const left  = range->op_first;
5152         OP* const right = left->op_sibling;
5153         LISTOP* listop;
5154
5155         range->op_flags &= ~OPf_KIDS;
5156         range->op_first = NULL;
5157
5158         listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
5159         listop->op_first->op_next = range->op_next;
5160         left->op_next = range->op_other;
5161         right->op_next = (OP*)listop;
5162         listop->op_next = listop->op_first;
5163
5164 #ifdef PERL_MAD
5165         op_getmad(expr,(OP*)listop,'O');
5166 #else
5167         op_free(expr);
5168 #endif
5169         expr = (OP*)(listop);
5170         op_null(expr);
5171         iterflags |= OPf_STACKED;
5172     }
5173     else {
5174         expr = mod(force_list(expr), OP_GREPSTART);
5175     }
5176
5177     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
5178                                append_elem(OP_LIST, expr, scalar(sv))));
5179     assert(!loop->op_next);
5180     /* for my  $x () sets OPpLVAL_INTRO;
5181      * for our $x () sets OPpOUR_INTRO */
5182     loop->op_private = (U8)iterpflags;
5183 #ifdef PL_OP_SLAB_ALLOC
5184     {
5185         LOOP *tmp;
5186         NewOp(1234,tmp,1,LOOP);
5187         Copy(loop,tmp,1,LISTOP);
5188         S_op_destroy(aTHX_ (OP*)loop);
5189         loop = tmp;
5190     }
5191 #else
5192     loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
5193 #endif
5194     loop->op_targ = padoff;
5195     wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont, 0);
5196     if (madsv)
5197         op_getmad(madsv, (OP*)loop, 'v');
5198     PL_parser->copline = forline;
5199     return newSTATEOP(0, label, wop);
5200 }
5201
5202 OP*
5203 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
5204 {
5205     dVAR;
5206     OP *o;
5207
5208     PERL_ARGS_ASSERT_NEWLOOPEX;
5209
5210     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
5211
5212     if (type != OP_GOTO || label->op_type == OP_CONST) {
5213         /* "last()" means "last" */
5214         if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
5215             o = newOP(type, OPf_SPECIAL);
5216         else {
5217             o = newPVOP(type, 0, savesharedpv(label->op_type == OP_CONST
5218                                         ? SvPV_nolen_const(((SVOP*)label)->op_sv)
5219                                         : ""));
5220         }
5221 #ifdef PERL_MAD
5222         op_getmad(label,o,'L');
5223 #else
5224         op_free(label);
5225 #endif
5226     }
5227     else {
5228         /* Check whether it's going to be a goto &function */
5229         if (label->op_type == OP_ENTERSUB
5230                 && !(label->op_flags & OPf_STACKED))
5231             label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
5232         o = newUNOP(type, OPf_STACKED, label);
5233     }
5234     PL_hints |= HINT_BLOCK_SCOPE;
5235     return o;
5236 }
5237
5238 /* if the condition is a literal array or hash
5239    (or @{ ... } etc), make a reference to it.
5240  */
5241 STATIC OP *
5242 S_ref_array_or_hash(pTHX_ OP *cond)
5243 {
5244     if (cond
5245     && (cond->op_type == OP_RV2AV
5246     ||  cond->op_type == OP_PADAV
5247     ||  cond->op_type == OP_RV2HV
5248     ||  cond->op_type == OP_PADHV))
5249
5250         return newUNOP(OP_REFGEN,
5251             0, mod(cond, OP_REFGEN));
5252
5253     else
5254         return cond;
5255 }
5256
5257 /* These construct the optree fragments representing given()
5258    and when() blocks.
5259
5260    entergiven and enterwhen are LOGOPs; the op_other pointer
5261    points up to the associated leave op. We need this so we
5262    can put it in the context and make break/continue work.
5263    (Also, of course, pp_enterwhen will jump straight to
5264    op_other if the match fails.)
5265  */
5266
5267 STATIC OP *
5268 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
5269                    I32 enter_opcode, I32 leave_opcode,
5270                    PADOFFSET entertarg)
5271 {
5272     dVAR;
5273     LOGOP *enterop;
5274     OP *o;
5275
5276     PERL_ARGS_ASSERT_NEWGIVWHENOP;
5277
5278     NewOp(1101, enterop, 1, LOGOP);
5279     enterop->op_type = (Optype)enter_opcode;
5280     enterop->op_ppaddr = PL_ppaddr[enter_opcode];
5281     enterop->op_flags =  (U8) OPf_KIDS;
5282     enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg);
5283     enterop->op_private = 0;
5284
5285     o = newUNOP(leave_opcode, 0, (OP *) enterop);
5286
5287     if (cond) {
5288         enterop->op_first = scalar(cond);
5289         cond->op_sibling = block;
5290
5291         o->op_next = LINKLIST(cond);
5292         cond->op_next = (OP *) enterop;
5293     }
5294     else {
5295         /* This is a default {} block */
5296         enterop->op_first = block;
5297         enterop->op_flags |= OPf_SPECIAL;
5298
5299         o->op_next = (OP *) enterop;
5300     }
5301
5302     CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
5303                                        entergiven and enterwhen both
5304                                        use ck_null() */
5305
5306     enterop->op_next = LINKLIST(block);
5307     block->op_next = enterop->op_other = o;
5308
5309     return o;
5310 }
5311
5312 /* Does this look like a boolean operation? For these purposes
5313    a boolean operation is:
5314      - a subroutine call [*]
5315      - a logical connective
5316      - a comparison operator
5317      - a filetest operator, with the exception of -s -M -A -C
5318      - defined(), exists() or eof()
5319      - /$re/ or $foo =~ /$re/
5320    
5321    [*] possibly surprising
5322  */
5323 STATIC bool
5324 S_looks_like_bool(pTHX_ const OP *o)
5325 {
5326     dVAR;
5327
5328     PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
5329
5330     switch(o->op_type) {
5331         case OP_OR:
5332         case OP_DOR:
5333             return looks_like_bool(cLOGOPo->op_first);
5334
5335         case OP_AND:
5336             return (
5337                 looks_like_bool(cLOGOPo->op_first)
5338              && looks_like_bool(cLOGOPo->op_first->op_sibling));
5339
5340         case OP_NULL:
5341         case OP_SCALAR:
5342             return (
5343                 o->op_flags & OPf_KIDS
5344             && looks_like_bool(cUNOPo->op_first));
5345
5346         case OP_ENTERSUB:
5347
5348         case OP_NOT:    case OP_XOR:
5349
5350         case OP_EQ:     case OP_NE:     case OP_LT:
5351         case OP_GT:     case OP_LE:     case OP_GE:
5352
5353         case OP_I_EQ:   case OP_I_NE:   case OP_I_LT:
5354         case OP_I_GT:   case OP_I_LE:   case OP_I_GE:
5355
5356         case OP_SEQ:    case OP_SNE:    case OP_SLT:
5357         case OP_SGT:    case OP_SLE:    case OP_SGE:
5358         
5359         case OP_SMARTMATCH:
5360         
5361         case OP_FTRREAD:  case OP_FTRWRITE: case OP_FTREXEC:
5362         case OP_FTEREAD:  case OP_FTEWRITE: case OP_FTEEXEC:
5363         case OP_FTIS:     case OP_FTEOWNED: case OP_FTROWNED:
5364         case OP_FTZERO:   case OP_FTSOCK:   case OP_FTCHR:
5365         case OP_FTBLK:    case OP_FTFILE:   case OP_FTDIR:
5366         case OP_FTPIPE:   case OP_FTLINK:   case OP_FTSUID:
5367         case OP_FTSGID:   case OP_FTSVTX:   case OP_FTTTY:
5368         case OP_FTTEXT:   case OP_FTBINARY:
5369         
5370         case OP_DEFINED: case OP_EXISTS:
5371         case OP_MATCH:   case OP_EOF:
5372
5373         case OP_FLOP:
5374
5375             return TRUE;
5376         
5377         case OP_CONST:
5378             /* Detect comparisons that have been optimized away */
5379             if (cSVOPo->op_sv == &PL_sv_yes
5380             ||  cSVOPo->op_sv == &PL_sv_no)
5381             
5382                 return TRUE;
5383             else
5384                 return FALSE;
5385
5386         /* FALL THROUGH */
5387         default:
5388             return FALSE;
5389     }
5390 }
5391
5392 OP *
5393 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
5394 {
5395     dVAR;
5396     PERL_ARGS_ASSERT_NEWGIVENOP;
5397     return newGIVWHENOP(
5398         ref_array_or_hash(cond),
5399         block,
5400         OP_ENTERGIVEN, OP_LEAVEGIVEN,
5401         defsv_off);
5402 }
5403
5404 /* If cond is null, this is a default {} block */
5405 OP *
5406 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
5407 {
5408     const bool cond_llb = (!cond || looks_like_bool(cond));
5409     OP *cond_op;
5410
5411     PERL_ARGS_ASSERT_NEWWHENOP;
5412
5413     if (cond_llb)
5414         cond_op = cond;
5415     else {
5416         cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
5417                 newDEFSVOP(),
5418                 scalar(ref_array_or_hash(cond)));
5419     }
5420     
5421     return newGIVWHENOP(
5422         cond_op,
5423         append_elem(block->op_type, block, newOP(OP_BREAK, OPf_SPECIAL)),
5424         OP_ENTERWHEN, OP_LEAVEWHEN, 0);
5425 }
5426
5427 /*
5428 =for apidoc cv_undef
5429
5430 Clear out all the active components of a CV. This can happen either
5431 by an explicit C<undef &foo>, or by the reference count going to zero.
5432 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
5433 children can still follow the full lexical scope chain.
5434
5435 =cut
5436 */
5437
5438 void
5439 Perl_cv_undef(pTHX_ CV *cv)
5440 {
5441     dVAR;
5442
5443     PERL_ARGS_ASSERT_CV_UNDEF;
5444
5445     DEBUG_X(PerlIO_printf(Perl_debug_log,
5446           "CV undef: cv=0x%"UVxf" comppad=0x%"UVxf"\n",
5447             PTR2UV(cv), PTR2UV(PL_comppad))
5448     );
5449
5450 #ifdef USE_ITHREADS
5451     if (CvFILE(cv) && !CvISXSUB(cv)) {
5452         /* for XSUBs CvFILE point directly to static memory; __FILE__ */
5453         Safefree(CvFILE(cv));
5454     }
5455     CvFILE(cv) = NULL;
5456 #endif
5457
5458     if (!CvISXSUB(cv) && CvROOT(cv)) {
5459         if (SvTYPE(cv) == SVt_PVCV && CvDEPTH(cv))
5460             Perl_croak(aTHX_ "Can't undef active subroutine");
5461         ENTER;
5462
5463         PAD_SAVE_SETNULLPAD();
5464
5465         op_free(CvROOT(cv));
5466         CvROOT(cv) = NULL;
5467         CvSTART(cv) = NULL;
5468         LEAVE;
5469     }
5470     SvPOK_off(MUTABLE_SV(cv));          /* forget prototype */
5471     CvGV_set(cv, NULL);
5472
5473     pad_undef(cv);
5474
5475     /* remove CvOUTSIDE unless this is an undef rather than a free */
5476     if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
5477         if (!CvWEAKOUTSIDE(cv))
5478             SvREFCNT_dec(CvOUTSIDE(cv));
5479         CvOUTSIDE(cv) = NULL;
5480     }
5481     if (CvCONST(cv)) {
5482         SvREFCNT_dec(MUTABLE_SV(CvXSUBANY(cv).any_ptr));
5483         CvCONST_off(cv);
5484     }
5485     if (CvISXSUB(cv) && CvXSUB(cv)) {
5486         CvXSUB(cv) = NULL;
5487     }
5488     /* delete all flags except WEAKOUTSIDE and CVGV_RC, which indicate the
5489      * ref status of CvOUTSIDE and CvGV */
5490     CvFLAGS(cv) &= (CVf_WEAKOUTSIDE|CVf_CVGV_RC);
5491 }
5492
5493 void
5494 Perl_cv_ckproto_len(pTHX_ const CV *cv, const GV *gv, const char *p,
5495                     const STRLEN len)
5496 {
5497     PERL_ARGS_ASSERT_CV_CKPROTO_LEN;
5498
5499     /* Can't just use a strcmp on the prototype, as CONSTSUBs "cheat" by
5500        relying on SvCUR, and doubling up the buffer to hold CvFILE().  */
5501     if (((!p != !SvPOK(cv)) /* One has prototype, one has not.  */
5502          || (p && (len != SvCUR(cv) /* Not the same length.  */
5503                    || memNE(p, SvPVX_const(cv), len))))
5504          && ckWARN_d(WARN_PROTOTYPE)) {
5505         SV* const msg = sv_newmortal();
5506         SV* name = NULL;
5507
5508         if (gv)
5509             gv_efullname3(name = sv_newmortal(), gv, NULL);
5510         sv_setpvs(msg, "Prototype mismatch:");
5511         if (name)
5512             Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name));
5513         if (SvPOK(cv))
5514             Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", SVfARG(cv));
5515         else
5516             sv_catpvs(msg, ": none");
5517         sv_catpvs(msg, " vs ");
5518         if (p)
5519             Perl_sv_catpvf(aTHX_ msg, "(%.*s)", (int) len, p);
5520         else
5521             sv_catpvs(msg, "none");
5522         Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg));
5523     }
5524 }
5525
5526 static void const_sv_xsub(pTHX_ CV* cv);
5527
5528 /*
5529
5530 =head1 Optree Manipulation Functions
5531
5532 =for apidoc cv_const_sv
5533
5534 If C<cv> is a constant sub eligible for inlining. returns the constant
5535 value returned by the sub.  Otherwise, returns NULL.
5536
5537 Constant subs can be created with C<newCONSTSUB> or as described in
5538 L<perlsub/"Constant Functions">.
5539
5540 =cut
5541 */
5542 SV *
5543 Perl_cv_const_sv(pTHX_ const CV *const cv)
5544 {
5545     PERL_UNUSED_CONTEXT;
5546     if (!cv)
5547         return NULL;
5548     if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
5549         return NULL;
5550     return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
5551 }
5552
5553 /* op_const_sv:  examine an optree to determine whether it's in-lineable.
5554  * Can be called in 3 ways:
5555  *
5556  * !cv
5557  *      look for a single OP_CONST with attached value: return the value
5558  *
5559  * cv && CvCLONE(cv) && !CvCONST(cv)
5560  *
5561  *      examine the clone prototype, and if contains only a single
5562  *      OP_CONST referencing a pad const, or a single PADSV referencing
5563  *      an outer lexical, return a non-zero value to indicate the CV is
5564  *      a candidate for "constizing" at clone time
5565  *
5566  * cv && CvCONST(cv)
5567  *
5568  *      We have just cloned an anon prototype that was marked as a const
5569  *      candidiate. Try to grab the current value, and in the case of
5570  *      PADSV, ignore it if it has multiple references. Return the value.
5571  */
5572
5573 SV *
5574 Perl_op_const_sv(pTHX_ const OP *o, CV *cv)
5575 {
5576     dVAR;
5577     SV *sv = NULL;
5578
5579     if (PL_madskills)
5580         return NULL;
5581
5582     if (!o)
5583         return NULL;
5584
5585     if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
5586         o = cLISTOPo->op_first->op_sibling;
5587
5588     for (; o; o = o->op_next) {
5589         const OPCODE type = o->op_type;
5590
5591         if (sv && o->op_next == o)
5592             return sv;
5593         if (o->op_next != o) {
5594             if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
5595                 continue;
5596             if (type == OP_DBSTATE)
5597                 continue;
5598         }
5599         if (type == OP_LEAVESUB || type == OP_RETURN)
5600             break;
5601         if (sv)
5602             return NULL;
5603         if (type == OP_CONST && cSVOPo->op_sv)
5604             sv = cSVOPo->op_sv;
5605         else if (cv && type == OP_CONST) {
5606             sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
5607             if (!sv)
5608                 return NULL;
5609         }
5610         else if (cv && type == OP_PADSV) {
5611             if (CvCONST(cv)) { /* newly cloned anon */
5612                 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
5613                 /* the candidate should have 1 ref from this pad and 1 ref
5614                  * from the parent */
5615                 if (!sv || SvREFCNT(sv) != 2)
5616                     return NULL;
5617                 sv = newSVsv(sv);
5618                 SvREADONLY_on(sv);
5619                 return sv;
5620             }
5621             else {
5622                 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
5623                     sv = &PL_sv_undef; /* an arbitrary non-null value */
5624             }
5625         }
5626         else {
5627             return NULL;
5628         }
5629     }
5630     return sv;
5631 }
5632
5633 #ifdef PERL_MAD
5634 OP *
5635 #else
5636 void
5637 #endif
5638 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
5639 {
5640 #if 0
5641     /* This would be the return value, but the return cannot be reached.  */
5642     OP* pegop = newOP(OP_NULL, 0);
5643 #endif
5644
5645     PERL_UNUSED_ARG(floor);
5646
5647     if (o)
5648         SAVEFREEOP(o);
5649     if (proto)
5650         SAVEFREEOP(proto);
5651     if (attrs)
5652         SAVEFREEOP(attrs);
5653     if (block)
5654         SAVEFREEOP(block);
5655     Perl_croak(aTHX_ "\"my sub\" not yet implemented");
5656 #ifdef PERL_MAD
5657     NORETURN_FUNCTION_END;
5658 #endif
5659 }
5660
5661 CV *
5662 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
5663 {
5664     return Perl_newATTRSUB(aTHX_ floor, o, proto, NULL, block);
5665 }
5666
5667 CV *
5668 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
5669 {
5670     dVAR;
5671     GV *gv;
5672     const char *ps;
5673     STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
5674     register CV *cv = NULL;
5675     SV *const_sv;
5676     /* If the subroutine has no body, no attributes, and no builtin attributes
5677        then it's just a sub declaration, and we may be able to get away with
5678        storing with a placeholder scalar in the symbol table, rather than a
5679        full GV and CV.  If anything is present then it will take a full CV to
5680        store it.  */
5681     const I32 gv_fetch_flags
5682         = (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
5683            || PL_madskills)
5684         ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
5685     const char * const name = o ? SvPV_nolen_const(cSVOPo->op_sv) : NULL;
5686     bool has_name;
5687
5688     if (proto) {
5689         assert(proto->op_type == OP_CONST);
5690         ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
5691     }
5692     else
5693         ps = NULL;
5694
5695     if (name) {
5696         gv = gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV);
5697         has_name = TRUE;
5698     } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
5699         SV * const sv = sv_newmortal();
5700         Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
5701                        PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
5702                        CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
5703         gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
5704         has_name = TRUE;
5705     } else if (PL_curstash) {
5706         gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
5707         has_name = FALSE;
5708     } else {
5709         gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
5710         has_name = FALSE;
5711     }
5712
5713     if (!PL_madskills) {
5714         if (o)
5715             SAVEFREEOP(o);
5716         if (proto)
5717             SAVEFREEOP(proto);
5718         if (attrs)
5719             SAVEFREEOP(attrs);
5720     }
5721
5722     if (SvTYPE(gv) != SVt_PVGV) {       /* Maybe prototype now, and had at
5723                                            maximum a prototype before. */
5724         if (SvTYPE(gv) > SVt_NULL) {
5725             if (!SvPOK((const SV *)gv)
5726                 && !(SvIOK((const SV *)gv) && SvIVX((const SV *)gv) == -1))
5727             {
5728                 Perl_ck_warner_d(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
5729             }
5730             cv_ckproto_len((const CV *)gv, NULL, ps, ps_len);
5731         }
5732         if (ps)
5733             sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
5734         else
5735             sv_setiv(MUTABLE_SV(gv), -1);
5736
5737         SvREFCNT_dec(PL_compcv);
5738         cv = PL_compcv = NULL;
5739         goto done;
5740     }
5741
5742     cv = (!name || GvCVGEN(gv)) ? NULL : GvCV(gv);
5743
5744     if (!block || !ps || *ps || attrs
5745         || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
5746 #ifdef PERL_MAD
5747         || block->op_type == OP_NULL
5748 #endif
5749         )
5750         const_sv = NULL;
5751     else
5752         const_sv = op_const_sv(block, NULL);
5753
5754     if (cv) {
5755         const bool exists = CvROOT(cv) || CvXSUB(cv);
5756
5757         /* if the subroutine doesn't exist and wasn't pre-declared
5758          * with a prototype, assume it will be AUTOLOADed,
5759          * skipping the prototype check
5760          */
5761         if (exists || SvPOK(cv))
5762             cv_ckproto_len(cv, gv, ps, ps_len);
5763         /* already defined (or promised)? */
5764         if (exists || GvASSUMECV(gv)) {
5765             if ((!block
5766 #ifdef PERL_MAD
5767                  || block->op_type == OP_NULL
5768 #endif
5769                  )&& !attrs) {
5770                 if (CvFLAGS(PL_compcv)) {
5771                     /* might have had built-in attrs applied */
5772                     if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && ckWARN(WARN_MISC))
5773                         Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
5774                     CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS & ~CVf_LVALUE);
5775                 }
5776                 /* just a "sub foo;" when &foo is already defined */
5777                 SAVEFREESV(PL_compcv);
5778                 goto done;
5779             }
5780             if (block
5781 #ifdef PERL_MAD
5782                 && block->op_type != OP_NULL
5783 #endif
5784                 ) {
5785                 if (ckWARN(WARN_REDEFINE)
5786                     || (CvCONST(cv)
5787                         && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
5788                 {
5789                     const line_t oldline = CopLINE(PL_curcop);
5790                     if (PL_parser && PL_parser->copline != NOLINE)
5791                         CopLINE_set(PL_curcop, PL_parser->copline);
5792                     Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
5793                         CvCONST(cv) ? "Constant subroutine %s redefined"
5794                                     : "Subroutine %s redefined", name);
5795                     CopLINE_set(PL_curcop, oldline);
5796                 }
5797 #ifdef PERL_MAD
5798                 if (!PL_minus_c)        /* keep old one around for madskills */
5799 #endif
5800                     {
5801                         /* (PL_madskills unset in used file.) */
5802                         SvREFCNT_dec(cv);
5803                     }
5804                 cv = NULL;
5805             }
5806         }
5807     }
5808     if (const_sv) {
5809         SvREFCNT_inc_simple_void_NN(const_sv);
5810         if (cv) {
5811             assert(!CvROOT(cv) && !CvCONST(cv));
5812             sv_setpvs(MUTABLE_SV(cv), "");  /* prototype is "" */
5813             CvXSUBANY(cv).any_ptr = const_sv;
5814             CvXSUB(cv) = const_sv_xsub;
5815             CvCONST_on(cv);
5816             CvISXSUB_on(cv);
5817         }
5818         else {
5819             GvCV(gv) = NULL;
5820             cv = newCONSTSUB(NULL, name, const_sv);
5821         }
5822         mro_method_changed_in( /* sub Foo::Bar () { 123 } */
5823             (CvGV(cv) && GvSTASH(CvGV(cv)))
5824                 ? GvSTASH(CvGV(cv))
5825                 : CvSTASH(cv)
5826                     ? CvSTASH(cv)
5827                     : PL_curstash
5828         );
5829         if (PL_madskills)
5830             goto install_block;
5831         op_free(block);
5832         SvREFCNT_dec(PL_compcv);
5833         PL_compcv = NULL;
5834         goto done;
5835     }
5836     if (cv) {                           /* must reuse cv if autoloaded */
5837         /* transfer PL_compcv to cv */
5838         if (block
5839 #ifdef PERL_MAD
5840                   && block->op_type != OP_NULL
5841 #endif
5842         ) {
5843             cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
5844             cv_undef(cv);
5845             CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs;
5846             if (!CvWEAKOUTSIDE(cv))
5847                 SvREFCNT_dec(CvOUTSIDE(cv));
5848             CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
5849             CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
5850             CvOUTSIDE(PL_compcv) = 0;
5851             CvPADLIST(cv) = CvPADLIST(PL_compcv);
5852             CvPADLIST(PL_compcv) = 0;
5853             /* inner references to PL_compcv must be fixed up ... */
5854             pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
5855             if (PERLDB_INTER)/* Advice debugger on the new sub. */
5856               ++PL_sub_generation;
5857             if (CvSTASH(cv))
5858                 sv_del_backref(MUTABLE_SV(CvSTASH(cv)), MUTABLE_SV(cv));
5859         }
5860         else {
5861             /* Might have had built-in attributes applied -- propagate them. */
5862             CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
5863         }
5864         /* ... before we throw it away */
5865         SvREFCNT_dec(PL_compcv);
5866         PL_compcv = cv;
5867     }
5868     else {
5869         cv = PL_compcv;
5870         if (name) {
5871             GvCV(gv) = cv;
5872             if (PL_madskills) {
5873                 if (strEQ(name, "import")) {
5874                     PL_formfeed = MUTABLE_SV(cv);
5875                     /* diag_listed_as: SKIPME */
5876                     Perl_warner(aTHX_ packWARN(WARN_VOID), "0x%"UVxf"\n", PTR2UV(cv));
5877                 }
5878             }
5879             GvCVGEN(gv) = 0;
5880             mro_method_changed_in(GvSTASH(gv)); /* sub Foo::bar { (shift)+1 } */
5881         }
5882     }
5883     if (!CvGV(cv)) {
5884         CvGV_set(cv, gv);
5885         CvFILE_set_from_cop(cv, PL_curcop);
5886         CvSTASH(cv) = PL_curstash;
5887         if (PL_curstash)
5888             Perl_sv_add_backref(aTHX_ MUTABLE_SV(PL_curstash), MUTABLE_SV(cv));
5889     }
5890     if (attrs) {
5891         /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
5892         HV *stash = name && GvSTASH(CvGV(cv)) ? GvSTASH(CvGV(cv)) : PL_curstash;
5893         apply_attrs(stash, MUTABLE_SV(cv), attrs, FALSE);
5894     }
5895
5896     if (ps)
5897         sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
5898
5899     if (PL_parser && PL_parser->error_count) {
5900         op_free(block);
5901         block = NULL;
5902         if (name) {
5903             const char *s = strrchr(name, ':');
5904             s = s ? s+1 : name;
5905             if (strEQ(s, "BEGIN")) {
5906                 const char not_safe[] =
5907                     "BEGIN not safe after errors--compilation aborted";
5908                 if (PL_in_eval & EVAL_KEEPERR)
5909                     Perl_croak(aTHX_ not_safe);
5910                 else {
5911                     /* force display of errors found but not reported */
5912                     sv_catpv(ERRSV, not_safe);
5913                     Perl_croak(aTHX_ "%"SVf, SVfARG(ERRSV));
5914                 }
5915             }
5916         }
5917     }
5918  install_block:
5919     if (!block)
5920         goto done;
5921
5922     /* If we assign an optree to a PVCV, then we've defined a subroutine that
5923        the debugger could be able to set a breakpoint in, so signal to
5924        pp_entereval that it should not throw away any saved lines at scope
5925        exit.  */
5926        
5927     PL_breakable_sub_gen++;
5928     if (CvLVALUE(cv)) {
5929         CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
5930                              mod(scalarseq(block), OP_LEAVESUBLV));
5931         block->op_attached = 1;
5932     }
5933     else {
5934         /* This makes sub {}; work as expected.  */
5935         if (block->op_type == OP_STUB) {
5936             OP* const newblock = newSTATEOP(0, NULL, 0);
5937 #ifdef PERL_MAD
5938             op_getmad(block,newblock,'B');
5939 #else
5940             op_free(block);
5941 #endif
5942             block = newblock;
5943         }
5944         else
5945             block->op_attached = 1;
5946         CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
5947     }
5948     CvROOT(cv)->op_private |= OPpREFCOUNTED;
5949     OpREFCNT_set(CvROOT(cv), 1);
5950     CvSTART(cv) = LINKLIST(CvROOT(cv));
5951     CvROOT(cv)->op_next = 0;
5952     CALL_PEEP(CvSTART(cv));
5953
5954     /* now that optimizer has done its work, adjust pad values */
5955
5956     pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
5957
5958     if (CvCLONE(cv)) {
5959         assert(!CvCONST(cv));
5960         if (ps && !*ps && op_const_sv(block, cv))
5961             CvCONST_on(cv);
5962     }
5963
5964     if (has_name) {
5965         if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
5966             SV * const tmpstr = sv_newmortal();
5967             GV * const db_postponed = gv_fetchpvs("DB::postponed",
5968                                                   GV_ADDMULTI, SVt_PVHV);
5969             HV *hv;
5970             SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
5971                                           CopFILE(PL_curcop),
5972                                           (long)PL_subline,
5973                                           (long)CopLINE(PL_curcop));
5974             gv_efullname3(tmpstr, gv, NULL);
5975             (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
5976                     SvCUR(tmpstr), sv, 0);
5977             hv = GvHVn(db_postponed);
5978             if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))) {
5979                 CV * const pcv = GvCV(db_postponed);
5980                 if (pcv) {
5981                     dSP;
5982                     PUSHMARK(SP);
5983                     XPUSHs(tmpstr);
5984                     PUTBACK;
5985                     call_sv(MUTABLE_SV(pcv), G_DISCARD);
5986                 }
5987             }
5988         }
5989
5990         if (name && ! (PL_parser && PL_parser->error_count))
5991             process_special_blocks(name, gv, cv);
5992     }
5993
5994   done:
5995     if (PL_parser)
5996         PL_parser->copline = NOLINE;
5997     LEAVE_SCOPE(floor);
5998     return cv;
5999 }
6000
6001 STATIC void
6002 S_process_special_blocks(pTHX_ const char *const fullname, GV *const gv,
6003                          CV *const cv)
6004 {
6005     const char *const colon = strrchr(fullname,':');
6006     const char *const name = colon ? colon + 1 : fullname;
6007
6008     PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
6009
6010     if (*name == 'B') {
6011         if (strEQ(name, "BEGIN")) {
6012             const I32 oldscope = PL_scopestack_ix;
6013             ENTER;
6014             SAVECOPFILE(&PL_compiling);
6015             SAVECOPLINE(&PL_compiling);
6016
6017             DEBUG_x( dump_sub(gv) );
6018             Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
6019             GvCV(gv) = 0;               /* cv has been hijacked */
6020             call_list(oldscope, PL_beginav);
6021
6022             PL_curcop = &PL_compiling;
6023             CopHINTS_set(&PL_compiling, PL_hints);
6024             LEAVE;
6025         }
6026         else
6027             return;
6028     } else {
6029         if (*name == 'E') {
6030             if strEQ(name, "END") {
6031                 DEBUG_x( dump_sub(gv) );
6032                 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
6033             } else
6034                 return;
6035         } else if (*name == 'U') {
6036             if (strEQ(name, "UNITCHECK")) {
6037                 /* It's never too late to run a unitcheck block */
6038                 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
6039             }
6040             else
6041                 return;
6042         } else if (*name == 'C') {
6043             if (strEQ(name, "CHECK")) {
6044                 if (PL_main_start)
6045                     Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
6046                                    "Too late to run CHECK block");
6047                 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
6048             }
6049             else
6050                 return;
6051         } else if (*name == 'I') {
6052             if (strEQ(name, "INIT")) {
6053                 if (PL_main_start)
6054                     Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
6055                                    "Too late to run INIT block");
6056                 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
6057             }
6058             else
6059                 return;
6060         } else
6061             return;
6062         DEBUG_x( dump_sub(gv) );
6063         GvCV(gv) = 0;           /* cv has been hijacked */
6064     }
6065 }
6066
6067 /*
6068 =for apidoc newCONSTSUB
6069
6070 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
6071 eligible for inlining at compile-time.
6072
6073 Passing NULL for SV creates a constant sub equivalent to C<sub BAR () {}>,
6074 which won't be called if used as a destructor, but will suppress the overhead
6075 of a call to C<AUTOLOAD>.  (This form, however, isn't eligible for inlining at
6076 compile time.)
6077
6078 =cut
6079 */
6080
6081 CV *
6082 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
6083 {
6084     dVAR;
6085     CV* cv;
6086 #ifdef USE_ITHREADS
6087     const char *const file = CopFILE(PL_curcop);
6088 #else
6089     SV *const temp_sv = CopFILESV(PL_curcop);
6090     const char *const file = temp_sv ? SvPV_nolen_const(temp_sv) : NULL;
6091 #endif
6092
6093     ENTER;
6094
6095     if (IN_PERL_RUNTIME) {
6096         /* at runtime, it's not safe to manipulate PL_curcop: it may be
6097          * an op shared between threads. Use a non-shared COP for our
6098          * dirty work */
6099          SAVEVPTR(PL_curcop);
6100          PL_curcop = &PL_compiling;
6101     }
6102     SAVECOPLINE(PL_curcop);
6103     CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
6104
6105     SAVEHINTS();
6106     PL_hints &= ~HINT_BLOCK_SCOPE;
6107
6108     if (stash) {
6109         SAVESPTR(PL_curstash);
6110         SAVECOPSTASH(PL_curcop);
6111         PL_curstash = stash;
6112         CopSTASH_set(PL_curcop,stash);
6113     }
6114
6115     /* file becomes the CvFILE. For an XS, it's supposed to be static storage,
6116        and so doesn't get free()d.  (It's expected to be from the C pre-
6117        processor __FILE__ directive). But we need a dynamically allocated one,
6118        and we need it to get freed.  */
6119     cv = newXS_flags(name, const_sv_xsub, file ? file : "", "",
6120                      XS_DYNAMIC_FILENAME);
6121     CvXSUBANY(cv).any_ptr = sv;
6122     CvCONST_on(cv);
6123
6124 #ifdef USE_ITHREADS
6125     if (stash)
6126         CopSTASH_free(PL_curcop);
6127 #endif
6128     LEAVE;
6129
6130     return cv;
6131 }
6132
6133 CV *
6134 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
6135                  const char *const filename, const char *const proto,
6136                  U32 flags)
6137 {
6138     CV *cv = newXS(name, subaddr, filename);
6139
6140     PERL_ARGS_ASSERT_NEWXS_FLAGS;
6141
6142     if (flags & XS_DYNAMIC_FILENAME) {
6143         /* We need to "make arrangements" (ie cheat) to ensure that the
6144            filename lasts as long as the PVCV we just created, but also doesn't
6145            leak  */
6146         STRLEN filename_len = strlen(filename);
6147         STRLEN proto_and_file_len = filename_len;
6148         char *proto_and_file;
6149         STRLEN proto_len;
6150
6151         if (proto) {
6152             proto_len = strlen(proto);
6153             proto_and_file_len += proto_len;
6154
6155             Newx(proto_and_file, proto_and_file_len + 1, char);
6156             Copy(proto, proto_and_file, proto_len, char);
6157             Copy(filename, proto_and_file + proto_len, filename_len + 1, char);
6158         } else {
6159             proto_len = 0;
6160             proto_and_file = savepvn(filename, filename_len);
6161         }
6162
6163         /* This gets free()d.  :-)  */
6164         sv_usepvn_flags(MUTABLE_SV(cv), proto_and_file, proto_and_file_len,
6165                         SV_HAS_TRAILING_NUL);
6166         if (proto) {
6167             /* This gives us the correct prototype, rather than one with the
6168                file name appended.  */
6169             SvCUR_set(cv, proto_len);
6170         } else {
6171             SvPOK_off(cv);
6172         }
6173         CvFILE(cv) = proto_and_file + proto_len;
6174     } else {
6175         sv_setpv(MUTABLE_SV(cv), proto);
6176     }
6177     return cv;
6178 }
6179
6180 /*
6181 =for apidoc U||newXS
6182
6183 Used by C<xsubpp> to hook up XSUBs as Perl subs.  I<filename> needs to be
6184 static storage, as it is used directly as CvFILE(), without a copy being made.
6185
6186 =cut
6187 */
6188
6189 CV *
6190 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
6191 {
6192     dVAR;
6193     GV * const gv = gv_fetchpv(name ? name :
6194                         (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
6195                         GV_ADDMULTI, SVt_PVCV);
6196     register CV *cv;
6197
6198     PERL_ARGS_ASSERT_NEWXS;
6199
6200     if (!subaddr)
6201         Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
6202
6203     if ((cv = (name ? GvCV(gv) : NULL))) {
6204         if (GvCVGEN(gv)) {
6205             /* just a cached method */
6206             SvREFCNT_dec(cv);
6207             cv = NULL;
6208         }
6209         else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
6210             /* already defined (or promised) */
6211             /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
6212             if (ckWARN(WARN_REDEFINE)) {
6213                 GV * const gvcv = CvGV(cv);
6214                 if (gvcv) {
6215                     HV * const stash = GvSTASH(gvcv);
6216                     if (stash) {
6217                         const char *redefined_name = HvNAME_get(stash);
6218                         if ( strEQ(redefined_name,"autouse") ) {
6219                             const line_t oldline = CopLINE(PL_curcop);
6220                             if (PL_parser && PL_parser->copline != NOLINE)
6221                                 CopLINE_set(PL_curcop, PL_parser->copline);
6222                             Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
6223                                         CvCONST(cv) ? "Constant subroutine %s redefined"
6224                                                     : "Subroutine %s redefined"
6225                                         ,name);
6226                             CopLINE_set(PL_curcop, oldline);
6227                         }
6228                     }
6229                 }
6230             }
6231             SvREFCNT_dec(cv);
6232             cv = NULL;
6233         }
6234     }
6235
6236     if (cv)                             /* must reuse cv if autoloaded */
6237         cv_undef(cv);
6238     else {
6239         cv = MUTABLE_CV(newSV_type(SVt_PVCV));
6240         if (name) {
6241             GvCV(gv) = cv;
6242             GvCVGEN(gv) = 0;
6243             mro_method_changed_in(GvSTASH(gv)); /* newXS */
6244         }
6245     }
6246     if (!name)
6247         CvANON_on(cv);
6248     CvGV_set(cv, gv);
6249     (void)gv_fetchfile(filename);
6250     CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
6251                                    an external constant string */
6252     CvISXSUB_on(cv);
6253     CvXSUB(cv) = subaddr;
6254
6255     if (name)
6256         process_special_blocks(name, gv, cv);
6257
6258     return cv;
6259 }
6260
6261 #ifdef PERL_MAD
6262 OP *
6263 #else
6264 void
6265 #endif
6266 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
6267 {
6268     dVAR;
6269     register CV *cv;
6270 #ifdef PERL_MAD
6271     OP* pegop = newOP(OP_NULL, 0);
6272 #endif
6273
6274     GV * const gv = o
6275         ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
6276         : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
6277
6278     GvMULTI_on(gv);
6279     if ((cv = GvFORM(gv))) {
6280         if (ckWARN(WARN_REDEFINE)) {
6281             const line_t oldline = CopLINE(PL_curcop);
6282             if (PL_parser && PL_parser->copline != NOLINE)
6283                 CopLINE_set(PL_curcop, PL_parser->copline);
6284             if (o) {
6285                 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
6286                             "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv));
6287             } else {
6288                 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
6289                             "Format STDOUT redefined");
6290             }
6291             CopLINE_set(PL_curcop, oldline);
6292         }
6293         SvREFCNT_dec(cv);
6294     }
6295     cv = PL_compcv;
6296     GvFORM(gv) = cv;
6297     CvGV_set(cv, gv);
6298     CvFILE_set_from_cop(cv, PL_curcop);
6299
6300
6301     pad_tidy(padtidy_FORMAT);
6302     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
6303     CvROOT(cv)->op_private |= OPpREFCOUNTED;
6304     OpREFCNT_set(CvROOT(cv), 1);
6305     CvSTART(cv) = LINKLIST(CvROOT(cv));
6306     CvROOT(cv)->op_next = 0;
6307     CALL_PEEP(CvSTART(cv));
6308 #ifdef PERL_MAD
6309     op_getmad(o,pegop,'n');
6310     op_getmad_weak(block, pegop, 'b');
6311 #else
6312     op_free(o);
6313 #endif
6314     if (PL_parser)
6315         PL_parser->copline = NOLINE;
6316     LEAVE_SCOPE(floor);
6317 #ifdef PERL_MAD
6318     return pegop;
6319 #endif
6320 }
6321
6322 OP *
6323 Perl_newANONLIST(pTHX_ OP *o)
6324 {
6325     return convert(OP_ANONLIST, OPf_SPECIAL, o);
6326 }
6327
6328 OP *
6329 Perl_newANONHASH(pTHX_ OP *o)
6330 {
6331     return convert(OP_ANONHASH, OPf_SPECIAL, o);
6332 }
6333
6334 OP *
6335 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
6336 {
6337     return newANONATTRSUB(floor, proto, NULL, block);
6338 }
6339
6340 OP *
6341 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
6342 {
6343     return newUNOP(OP_REFGEN, 0,
6344         newSVOP(OP_ANONCODE, 0,
6345                 MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block))));
6346 }
6347
6348 OP *
6349 Perl_oopsAV(pTHX_ OP *o)
6350 {
6351     dVAR;
6352
6353     PERL_ARGS_ASSERT_OOPSAV;
6354
6355     switch (o->op_type) {
6356     case OP_PADSV:
6357         o->op_type = OP_PADAV;
6358         o->op_ppaddr = PL_ppaddr[OP_PADAV];
6359         return ref(o, OP_RV2AV);
6360
6361     case OP_RV2SV:
6362         o->op_type = OP_RV2AV;
6363         o->op_ppaddr = PL_ppaddr[OP_RV2AV];
6364         ref(o, OP_RV2AV);
6365         break;
6366
6367     default:
6368         Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
6369         break;
6370     }
6371     return o;
6372 }
6373
6374 OP *
6375 Perl_oopsHV(pTHX_ OP *o)
6376 {
6377     dVAR;
6378
6379     PERL_ARGS_ASSERT_OOPSHV;
6380
6381     switch (o->op_type) {
6382     case OP_PADSV:
6383     case OP_PADAV:
6384         o->op_type = OP_PADHV;
6385         o->op_ppaddr = PL_ppaddr[OP_PADHV];
6386         return ref(o, OP_RV2HV);
6387
6388     case OP_RV2SV:
6389     case OP_RV2AV:
6390         o->op_type = OP_RV2HV;
6391         o->op_ppaddr = PL_ppaddr[OP_RV2HV];
6392         ref(o, OP_RV2HV);
6393         break;
6394
6395     default:
6396         Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
6397         break;
6398     }
6399     return o;
6400 }
6401
6402 OP *
6403 Perl_newAVREF(pTHX_ OP *o)
6404 {
6405     dVAR;
6406
6407     PERL_ARGS_ASSERT_NEWAVREF;
6408
6409     if (o->op_type == OP_PADANY) {
6410         o->op_type = OP_PADAV;
6411         o->op_ppaddr = PL_ppaddr[OP_PADAV];
6412         return o;
6413     }
6414     else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
6415         Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6416                        "Using an array as a reference is deprecated");
6417     }
6418     return newUNOP(OP_RV2AV, 0, scalar(o));
6419 }
6420
6421 OP *
6422 Perl_newGVREF(pTHX_ I32 type, OP *o)
6423 {
6424     if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
6425         return newUNOP(OP_NULL, 0, o);
6426     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
6427 }
6428
6429 OP *
6430 Perl_newHVREF(pTHX_ OP *o)
6431 {
6432     dVAR;
6433
6434     PERL_ARGS_ASSERT_NEWHVREF;
6435
6436     if (o->op_type == OP_PADANY) {
6437         o->op_type = OP_PADHV;
6438         o->op_ppaddr = PL_ppaddr[OP_PADHV];
6439         return o;
6440     }
6441     else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
6442         Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6443                        "Using a hash as a reference is deprecated");
6444     }
6445     return newUNOP(OP_RV2HV, 0, scalar(o));
6446 }
6447
6448 OP *
6449 Perl_newCVREF(pTHX_ I32 flags, OP *o)
6450 {
6451     return newUNOP(OP_RV2CV, flags, scalar(o));
6452 }
6453
6454 OP *
6455 Perl_newSVREF(pTHX_ OP *o)
6456 {
6457     dVAR;
6458
6459     PERL_ARGS_ASSERT_NEWSVREF;
6460
6461     if (o->op_type == OP_PADANY) {
6462         o->op_type = OP_PADSV;
6463         o->op_ppaddr = PL_ppaddr[OP_PADSV];
6464         return o;
6465     }
6466     return newUNOP(OP_RV2SV, 0, scalar(o));
6467 }
6468
6469 /* Check routines. See the comments at the top of this file for details
6470  * on when these are called */
6471
6472 OP *
6473 Perl_ck_anoncode(pTHX_ OP *o)
6474 {
6475     PERL_ARGS_ASSERT_CK_ANONCODE;
6476
6477     cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
6478     if (!PL_madskills)
6479         cSVOPo->op_sv = NULL;
6480     return o;
6481 }
6482
6483 OP *
6484 Perl_ck_bitop(pTHX_ OP *o)
6485 {
6486     dVAR;
6487
6488     PERL_ARGS_ASSERT_CK_BITOP;
6489
6490 #define OP_IS_NUMCOMPARE(op) \
6491         ((op) == OP_LT   || (op) == OP_I_LT || \
6492          (op) == OP_GT   || (op) == OP_I_GT || \
6493          (op) == OP_LE   || (op) == OP_I_LE || \
6494          (op) == OP_GE   || (op) == OP_I_GE || \
6495          (op) == OP_EQ   || (op) == OP_I_EQ || \
6496          (op) == OP_NE   || (op) == OP_I_NE || \
6497          (op) == OP_NCMP || (op) == OP_I_NCMP)
6498     o->op_private = (U8)(PL_hints & HINT_INTEGER);
6499     if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
6500             && (o->op_type == OP_BIT_OR
6501              || o->op_type == OP_BIT_AND
6502              || o->op_type == OP_BIT_XOR))
6503     {
6504         const OP * const left = cBINOPo->op_first;
6505         const OP * const right = left->op_sibling;
6506         if ((OP_IS_NUMCOMPARE(left->op_type) &&
6507                 (left->op_flags & OPf_PARENS) == 0) ||
6508             (OP_IS_NUMCOMPARE(right->op_type) &&
6509                 (right->op_flags & OPf_PARENS) == 0))
6510             Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
6511                            "Possible precedence problem on bitwise %c operator",
6512                            o->op_type == OP_BIT_OR ? '|'
6513                            : o->op_type == OP_BIT_AND ? '&' : '^'
6514                            );
6515     }
6516     return o;
6517 }
6518
6519 OP *
6520 Perl_ck_concat(pTHX_ OP *o)
6521 {
6522     const OP * const kid = cUNOPo->op_first;
6523
6524     PERL_ARGS_ASSERT_CK_CONCAT;
6525     PERL_UNUSED_CONTEXT;
6526
6527     if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
6528             !(kUNOP->op_first->op_flags & OPf_MOD))
6529         o->op_flags |= OPf_STACKED;
6530     return o;
6531 }
6532
6533 OP *
6534 Perl_ck_spair(pTHX_ OP *o)
6535 {
6536     dVAR;
6537
6538     PERL_ARGS_ASSERT_CK_SPAIR;
6539
6540     if (o->op_flags & OPf_KIDS) {
6541         OP* newop;
6542         OP* kid;
6543         const OPCODE type = o->op_type;
6544         o = modkids(ck_fun(o), type);
6545         kid = cUNOPo->op_first;
6546         newop = kUNOP->op_first->op_sibling;
6547         if (newop) {
6548             const OPCODE type = newop->op_type;
6549             if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
6550                     type == OP_PADAV || type == OP_PADHV ||
6551                     type == OP_RV2AV || type == OP_RV2HV)
6552                 return o;
6553         }
6554 #ifdef PERL_MAD
6555         op_getmad(kUNOP->op_first,newop,'K');
6556 #else
6557         op_free(kUNOP->op_first);
6558 #endif
6559         kUNOP->op_first = newop;
6560     }
6561     o->op_ppaddr = PL_ppaddr[++o->op_type];
6562     return ck_fun(o);
6563 }
6564
6565 OP *
6566 Perl_ck_delete(pTHX_ OP *o)
6567 {
6568     PERL_ARGS_ASSERT_CK_DELETE;
6569
6570     o = ck_fun(o);
6571     o->op_private = 0;
6572     if (o->op_flags & OPf_KIDS) {
6573         OP * const kid = cUNOPo->op_first;
6574         switch (kid->op_type) {
6575         case OP_ASLICE:
6576             o->op_flags |= OPf_SPECIAL;
6577             /* FALL THROUGH */
6578         case OP_HSLICE:
6579             o->op_private |= OPpSLICE;
6580             break;
6581         case OP_AELEM:
6582             o->op_flags |= OPf_SPECIAL;
6583             /* FALL THROUGH */
6584         case OP_HELEM:
6585             break;
6586         default:
6587             Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
6588                   OP_DESC(o));
6589         }
6590         if (kid->op_private & OPpLVAL_INTRO)
6591             o->op_private |= OPpLVAL_INTRO;
6592         op_null(kid);
6593     }
6594     return o;
6595 }
6596
6597 OP *
6598 Perl_ck_die(pTHX_ OP *o)
6599 {
6600     PERL_ARGS_ASSERT_CK_DIE;
6601
6602 #ifdef VMS
6603     if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
6604 #endif
6605     return ck_fun(o);
6606 }
6607
6608 OP *
6609 Perl_ck_eof(pTHX_ OP *o)
6610 {
6611     dVAR;
6612
6613     PERL_ARGS_ASSERT_CK_EOF;
6614
6615     if (o->op_flags & OPf_KIDS) {
6616         if (cLISTOPo->op_first->op_type == OP_STUB) {
6617             OP * const newop
6618                 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
6619 #ifdef PERL_MAD
6620             op_getmad(o,newop,'O');
6621 #else
6622             op_free(o);
6623 #endif
6624             o = newop;
6625         }
6626         return ck_fun(o);
6627     }
6628     return o;
6629 }
6630
6631 OP *
6632 Perl_ck_eval(pTHX_ OP *o)
6633 {
6634     dVAR;
6635
6636     PERL_ARGS_ASSERT_CK_EVAL;
6637
6638     PL_hints |= HINT_BLOCK_SCOPE;
6639     if (o->op_flags & OPf_KIDS) {
6640         SVOP * const kid = (SVOP*)cUNOPo->op_first;
6641
6642         if (!kid) {
6643             o->op_flags &= ~OPf_KIDS;
6644             op_null(o);
6645         }
6646         else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
6647             LOGOP *enter;
6648 #ifdef PERL_MAD
6649             OP* const oldo = o;
6650 #endif
6651
6652             cUNOPo->op_first = 0;
6653 #ifndef PERL_MAD
6654             op_free(o);
6655 #endif
6656
6657             NewOp(1101, enter, 1, LOGOP);
6658             enter->op_type = OP_ENTERTRY;
6659             enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
6660             enter->op_private = 0;
6661
6662             /* establish postfix order */
6663             enter->op_next = (OP*)enter;
6664
6665             o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
6666             o->op_type = OP_LEAVETRY;
6667             o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
6668             enter->op_other = o;
6669             op_getmad(oldo,o,'O');
6670             return o;
6671         }
6672         else {
6673             scalar((OP*)kid);
6674             PL_cv_has_eval = 1;
6675         }
6676     }
6677     else {
6678 #ifdef PERL_MAD
6679         OP* const oldo = o;
6680 #else
6681         op_free(o);
6682 #endif
6683         o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
6684         op_getmad(oldo,o,'O');
6685     }
6686     o->op_targ = (PADOFFSET)PL_hints;
6687     if ((PL_hints & HINT_LOCALIZE_HH) != 0 && GvHV(PL_hintgv)) {
6688         /* Store a copy of %^H that pp_entereval can pick up. */
6689         OP *hhop = newSVOP(OP_HINTSEVAL, 0,
6690                            MUTABLE_SV(Perl_hv_copy_hints_hv(aTHX_ GvHV(PL_hintgv))));
6691         cUNOPo->op_first->op_sibling = hhop;
6692         o->op_private |= OPpEVAL_HAS_HH;
6693     }
6694     return o;
6695 }
6696
6697 OP *
6698 Perl_ck_exit(pTHX_ OP *o)
6699 {
6700     PERL_ARGS_ASSERT_CK_EXIT;
6701
6702 #ifdef VMS
6703     HV * const table = GvHV(PL_hintgv);
6704     if (table) {
6705        SV * const * const svp = hv_fetchs(table, "vmsish_exit", FALSE);
6706        if (svp && *svp && SvTRUE(*svp))
6707            o->op_private |= OPpEXIT_VMSISH;
6708     }
6709     if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
6710 #endif
6711     return ck_fun(o);
6712 }
6713
6714 OP *
6715 Perl_ck_exec(pTHX_ OP *o)
6716 {
6717     PERL_ARGS_ASSERT_CK_EXEC;
6718
6719     if (o->op_flags & OPf_STACKED) {
6720         OP *kid;
6721         o = ck_fun(o);
6722         kid = cUNOPo->op_first->op_sibling;
6723         if (kid->op_type == OP_RV2GV)
6724             op_null(kid);
6725     }
6726     else
6727         o = listkids(o);
6728     return o;
6729 }
6730
6731 OP *
6732 Perl_ck_exists(pTHX_ OP *o)
6733 {
6734     dVAR;
6735
6736     PERL_ARGS_ASSERT_CK_EXISTS;
6737
6738     o = ck_fun(o);
6739     if (o->op_flags & OPf_KIDS) {
6740         OP * const kid = cUNOPo->op_first;
6741         if (kid->op_type == OP_ENTERSUB) {
6742             (void) ref(kid, o->op_type);
6743             if (kid->op_type != OP_RV2CV
6744                         && !(PL_parser && PL_parser->error_count))
6745                 Perl_croak(aTHX_ "%s argument is not a subroutine name",
6746                             OP_DESC(o));
6747             o->op_private |= OPpEXISTS_SUB;
6748         }
6749         else if (kid->op_type == OP_AELEM)
6750             o->op_flags |= OPf_SPECIAL;
6751         else if (kid->op_type != OP_HELEM)
6752             Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or a subroutine",
6753                         OP_DESC(o));
6754         op_null(kid);
6755     }
6756     return o;
6757 }
6758
6759 OP *
6760 Perl_ck_rvconst(pTHX_ register OP *o)
6761 {
6762     dVAR;
6763     SVOP * const kid = (SVOP*)cUNOPo->op_first;
6764
6765     PERL_ARGS_ASSERT_CK_RVCONST;
6766
6767     o->op_private |= (PL_hints & HINT_STRICT_REFS);
6768     if (o->op_type == OP_RV2CV)
6769         o->op_private &= ~1;
6770
6771     if (kid->op_type == OP_CONST) {
6772         int iscv;
6773         GV *gv;
6774         SV * const kidsv = kid->op_sv;
6775
6776         /* Is it a constant from cv_const_sv()? */
6777         if (SvROK(kidsv) && SvREADONLY(kidsv)) {
6778             SV * const rsv = SvRV(kidsv);
6779             const svtype type = SvTYPE(rsv);
6780             const char *badtype = NULL;
6781
6782             switch (o->op_type) {
6783             case OP_RV2SV:
6784                 if (type > SVt_PVMG)
6785                     badtype = "a SCALAR";
6786                 break;
6787             case OP_RV2AV:
6788                 if (type != SVt_PVAV)
6789                     badtype = "an ARRAY";
6790                 break;
6791             case OP_RV2HV:
6792                 if (type != SVt_PVHV)
6793                     badtype = "a HASH";
6794                 break;
6795             case OP_RV2CV:
6796                 if (type != SVt_PVCV)
6797                     badtype = "a CODE";
6798                 break;
6799             }
6800             if (badtype)
6801                 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
6802             return o;
6803         }
6804         if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
6805             const char *badthing;
6806             switch (o->op_type) {
6807             case OP_RV2SV:
6808                 badthing = "a SCALAR";
6809                 break;
6810             case OP_RV2AV:
6811                 badthing = "an ARRAY";
6812                 break;
6813             case OP_RV2HV:
6814                 badthing = "a HASH";
6815                 break;
6816             default:
6817                 badthing = NULL;
6818                 break;
6819             }
6820             if (badthing)
6821                 Perl_croak(aTHX_
6822                            "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
6823                            SVfARG(kidsv), badthing);
6824         }
6825         /*
6826          * This is a little tricky.  We only want to add the symbol if we
6827          * didn't add it in the lexer.  Otherwise we get duplicate strict
6828          * warnings.  But if we didn't add it in the lexer, we must at
6829          * least pretend like we wanted to add it even if it existed before,
6830          * or we get possible typo warnings.  OPpCONST_ENTERED says
6831          * whether the lexer already added THIS instance of this symbol.
6832          */
6833         iscv = (o->op_type == OP_RV2CV) * 2;
6834         do {
6835             gv = gv_fetchsv(kidsv,
6836                 iscv | !(kid->op_private & OPpCONST_ENTERED),
6837                 iscv
6838                     ? SVt_PVCV
6839                     : o->op_type == OP_RV2SV
6840                         ? SVt_PV
6841                         : o->op_type == OP_RV2AV
6842                             ? SVt_PVAV
6843                             : o->op_type == OP_RV2HV
6844                                 ? SVt_PVHV
6845                                 : SVt_PVGV);
6846         } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
6847         if (gv) {
6848             kid->op_type = OP_GV;
6849             SvREFCNT_dec(kid->op_sv);
6850 #ifdef USE_ITHREADS
6851             /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
6852             kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
6853             SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
6854             GvIN_PAD_on(gv);
6855             PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
6856 #else
6857             kid->op_sv = SvREFCNT_inc_simple_NN(gv);
6858 #endif
6859             kid->op_private = 0;
6860             kid->op_ppaddr = PL_ppaddr[OP_GV];
6861         }
6862     }
6863     return o;
6864 }
6865
6866 OP *
6867 Perl_ck_ftst(pTHX_ OP *o)
6868 {
6869     dVAR;
6870     const I32 type = o->op_type;
6871
6872     PERL_ARGS_ASSERT_CK_FTST;
6873
6874     if (o->op_flags & OPf_REF) {
6875         NOOP;
6876     }
6877     else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
6878         SVOP * const kid = (SVOP*)cUNOPo->op_first;
6879         const OPCODE kidtype = kid->op_type;
6880
6881         if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6882             OP * const newop = newGVOP(type, OPf_REF,
6883                 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
6884 #ifdef PERL_MAD
6885             op_getmad(o,newop,'O');
6886 #else
6887             op_free(o);
6888 #endif
6889             return newop;
6890         }
6891         if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
6892             o->op_private |= OPpFT_ACCESS;
6893         if (PL_check[kidtype] == MEMBER_TO_FPTR(Perl_ck_ftst)
6894                 && kidtype != OP_STAT && kidtype != OP_LSTAT)
6895             o->op_private |= OPpFT_STACKED;
6896     }
6897     else {
6898 #ifdef PERL_MAD
6899         OP* const oldo = o;
6900 #else
6901         op_free(o);
6902 #endif
6903         if (type == OP_FTTTY)
6904             o = newGVOP(type, OPf_REF, PL_stdingv);
6905         else
6906             o = newUNOP(type, 0, newDEFSVOP());
6907         op_getmad(oldo,o,'O');
6908     }
6909     return o;
6910 }
6911
6912 OP *
6913 Perl_ck_fun(pTHX_ OP *o)
6914 {
6915     dVAR;
6916     const int type = o->op_type;
6917     register I32 oa = PL_opargs[type] >> OASHIFT;
6918
6919     PERL_ARGS_ASSERT_CK_FUN;
6920
6921     if (o->op_flags & OPf_STACKED) {
6922         if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
6923             oa &= ~OA_OPTIONAL;
6924         else
6925             return no_fh_allowed(o);
6926     }
6927
6928     if (o->op_flags & OPf_KIDS) {
6929         OP **tokid = &cLISTOPo->op_first;
6930         register OP *kid = cLISTOPo->op_first;
6931         OP *sibl;
6932         I32 numargs = 0;
6933
6934         if (kid->op_type == OP_PUSHMARK ||
6935             (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
6936         {
6937             tokid = &kid->op_sibling;
6938             kid = kid->op_sibling;
6939         }
6940         if (!kid && PL_opargs[type] & OA_DEFGV)
6941             *tokid = kid = newDEFSVOP();
6942
6943         while (oa && kid) {
6944             numargs++;
6945             sibl = kid->op_sibling;
6946 #ifdef PERL_MAD
6947             if (!sibl && kid->op_type == OP_STUB) {
6948                 numargs--;
6949                 break;
6950             }
6951 #endif
6952             switch (oa & 7) {
6953             case OA_SCALAR:
6954                 /* list seen where single (scalar) arg expected? */
6955                 if (numargs == 1 && !(oa >> 4)
6956                     && kid->op_type == OP_LIST && type != OP_SCALAR)
6957                 {
6958                     return too_many_arguments(o,PL_op_desc[type]);
6959                 }
6960                 scalar(kid);
6961                 break;
6962             case OA_LIST:
6963                 if (oa < 16) {
6964                     kid = 0;
6965                     continue;
6966                 }
6967                 else
6968                     list(kid);
6969                 break;
6970             case OA_AVREF:
6971                 if ((type == OP_PUSH || type == OP_UNSHIFT)
6972                     && !kid->op_sibling)
6973                     Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
6974                                    "Useless use of %s with no values",
6975                                    PL_op_desc[type]);
6976
6977                 if (kid->op_type == OP_CONST &&
6978                     (kid->op_private & OPpCONST_BARE))
6979                 {
6980                     OP * const newop = newAVREF(newGVOP(OP_GV, 0,
6981                         gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
6982                     Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6983                                    "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
6984                                    SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
6985 #ifdef PERL_MAD
6986                     op_getmad(kid,newop,'K');
6987 #else
6988                     op_free(kid);
6989 #endif
6990                     kid = newop;
6991                     kid->op_sibling = sibl;
6992                     *tokid = kid;
6993                 }
6994                 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
6995                     bad_type(numargs, "array", PL_op_desc[type], kid);
6996                 mod(kid, type);
6997                 break;
6998             case OA_HVREF:
6999                 if (kid->op_type == OP_CONST &&
7000                     (kid->op_private & OPpCONST_BARE))
7001                 {
7002                     OP * const newop = newHVREF(newGVOP(OP_GV, 0,
7003                         gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
7004                     Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7005                                    "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
7006                                    SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
7007 #ifdef PERL_MAD
7008                     op_getmad(kid,newop,'K');
7009 #else
7010                     op_free(kid);
7011 #endif
7012                     kid = newop;
7013                     kid->op_sibling = sibl;
7014                     *tokid = kid;
7015                 }
7016                 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
7017                     bad_type(numargs, "hash", PL_op_desc[type], kid);
7018                 mod(kid, type);
7019                 break;
7020             case OA_CVREF:
7021                 {
7022                     OP * const newop = newUNOP(OP_NULL, 0, kid);
7023                     kid->op_sibling = 0;
7024                     linklist(kid);
7025                     newop->op_next = newop;
7026                     kid = newop;
7027                     kid->op_sibling = sibl;
7028                     *tokid = kid;
7029                 }
7030                 break;
7031             case OA_FILEREF:
7032                 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
7033                     if (kid->op_type == OP_CONST &&
7034                         (kid->op_private & OPpCONST_BARE))
7035                     {
7036                         OP * const newop = newGVOP(OP_GV, 0,
7037                             gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
7038                         if (!(o->op_private & 1) && /* if not unop */
7039                             kid == cLISTOPo->op_last)
7040                             cLISTOPo->op_last = newop;
7041 #ifdef PERL_MAD
7042                         op_getmad(kid,newop,'K');
7043 #else
7044                         op_free(kid);
7045 #endif
7046                         kid = newop;
7047                     }
7048                     else if (kid->op_type == OP_READLINE) {
7049                         /* neophyte patrol: open(<FH>), close(<FH>) etc. */
7050                         bad_type(numargs, "HANDLE", OP_DESC(o), kid);
7051                     }
7052                     else {
7053                         I32 flags = OPf_SPECIAL;
7054                         I32 priv = 0;
7055                         PADOFFSET targ = 0;
7056
7057                         /* is this op a FH constructor? */
7058                         if (is_handle_constructor(o,numargs)) {
7059                             const char *name = NULL;
7060                             STRLEN len = 0;
7061
7062                             flags = 0;
7063                             /* Set a flag to tell rv2gv to vivify
7064                              * need to "prove" flag does not mean something
7065                              * else already - NI-S 1999/05/07
7066                              */
7067                             priv = OPpDEREF;
7068                             if (kid->op_type == OP_PADSV) {
7069                                 SV *const namesv
7070                                     = PAD_COMPNAME_SV(kid->op_targ);
7071                                 name = SvPV_const(namesv, len);
7072                             }
7073                             else if (kid->op_type == OP_RV2SV
7074                                      && kUNOP->op_first->op_type == OP_GV)
7075                             {
7076                                 GV * const gv = cGVOPx_gv(kUNOP->op_first);
7077                                 name = GvNAME(gv);
7078                                 len = GvNAMELEN(gv);
7079                             }
7080                             else if (kid->op_type == OP_AELEM
7081                                      || kid->op_type == OP_HELEM)
7082                             {
7083                                  OP *firstop;
7084                                  OP *op = ((BINOP*)kid)->op_first;
7085                                  name = NULL;
7086                                  if (op) {
7087                                       SV *tmpstr = NULL;
7088                                       const char * const a =
7089                                            kid->op_type == OP_AELEM ?
7090                                            "[]" : "{}";
7091                                       if (((op->op_type == OP_RV2AV) ||
7092                                            (op->op_type == OP_RV2HV)) &&
7093                                           (firstop = ((UNOP*)op)->op_first) &&
7094                                           (firstop->op_type == OP_GV)) {
7095                                            /* packagevar $a[] or $h{} */
7096                                            GV * const gv = cGVOPx_gv(firstop);
7097                                            if (gv)
7098                                                 tmpstr =
7099                                                      Perl_newSVpvf(aTHX_
7100                                                                    "%s%c...%c",
7101                                                                    GvNAME(gv),
7102                                                                    a[0], a[1]);
7103                                       }
7104                                       else if (op->op_type == OP_PADAV
7105                                                || op->op_type == OP_PADHV) {
7106                                            /* lexicalvar $a[] or $h{} */
7107                                            const char * const padname =
7108                                                 PAD_COMPNAME_PV(op->op_targ);
7109                                            if (padname)
7110                                                 tmpstr =
7111                                                      Perl_newSVpvf(aTHX_
7112                                                                    "%s%c...%c",
7113                                                                    padname + 1,
7114                                                                    a[0], a[1]);
7115                                       }
7116                                       if (tmpstr) {
7117                                            name = SvPV_const(tmpstr, len);
7118                                            sv_2mortal(tmpstr);
7119                                       }
7120                                  }
7121                                  if (!name) {
7122                                       name = "__ANONIO__";
7123                                       len = 10;
7124                                  }
7125                                  mod(kid, type);
7126                             }
7127                             if (name) {
7128                                 SV *namesv;
7129                                 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
7130                                 namesv = PAD_SVl(targ);
7131                                 SvUPGRADE(namesv, SVt_PV);
7132                                 if (*name != '$')
7133                                     sv_setpvs(namesv, "$");
7134                                 sv_catpvn(namesv, name, len);
7135                             }
7136                         }
7137                         kid->op_sibling = 0;
7138                         kid = newUNOP(OP_RV2GV, flags, scalar(kid));
7139                         kid->op_targ = targ;
7140                         kid->op_private |= priv;
7141                     }
7142                     kid->op_sibling = sibl;
7143                     *tokid = kid;
7144                 }
7145                 scalar(kid);
7146                 break;
7147             case OA_SCALARREF:
7148                 mod(scalar(kid), type);
7149                 break;
7150             }
7151             oa >>= 4;
7152             tokid = &kid->op_sibling;
7153             kid = kid->op_sibling;
7154         }
7155 #ifdef PERL_MAD
7156         if (kid && kid->op_type != OP_STUB)
7157             return too_many_arguments(o,OP_DESC(o));
7158         o->op_private |= numargs;
7159 #else
7160         /* FIXME - should the numargs move as for the PERL_MAD case?  */
7161         o->op_private |= numargs;
7162         if (kid)
7163             return too_many_arguments(o,OP_DESC(o));
7164 #endif
7165         listkids(o);
7166     }
7167     else if (PL_opargs[type] & OA_DEFGV) {
7168 #ifdef PERL_MAD
7169         OP *newop = newUNOP(type, 0, newDEFSVOP());
7170         op_getmad(o,newop,'O');
7171         return newop;
7172 #else
7173         /* Ordering of these two is important to keep f_map.t passing.  */
7174         op_free(o);
7175         return newUNOP(type, 0, newDEFSVOP());
7176 #endif
7177     }
7178
7179     if (oa) {
7180         while (oa & OA_OPTIONAL)
7181             oa >>= 4;
7182         if (oa && oa != OA_LIST)
7183             return too_few_arguments(o,OP_DESC(o));
7184     }
7185     return o;
7186 }
7187
7188 OP *
7189 Perl_ck_glob(pTHX_ OP *o)
7190 {
7191     dVAR;
7192     GV *gv;
7193
7194     PERL_ARGS_ASSERT_CK_GLOB;
7195
7196     o = ck_fun(o);
7197     if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
7198         append_elem(OP_GLOB, o, newDEFSVOP());
7199
7200     if (!((gv = gv_fetchpvs("glob", GV_NOTQUAL, SVt_PVCV))
7201           && GvCVu(gv) && GvIMPORTED_CV(gv)))
7202     {
7203         gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
7204     }
7205
7206 #if !defined(PERL_EXTERNAL_GLOB)
7207     /* XXX this can be tightened up and made more failsafe. */
7208     if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
7209         GV *glob_gv;
7210         ENTER;
7211         Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
7212                 newSVpvs("File::Glob"), NULL, NULL, NULL);
7213         if((glob_gv = gv_fetchpvs("File::Glob::csh_glob", 0, SVt_PVCV))) {
7214             gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
7215             GvCV(gv) = GvCV(glob_gv);
7216             SvREFCNT_inc_void(MUTABLE_SV(GvCV(gv)));
7217             GvIMPORTED_CV_on(gv);
7218         }
7219         LEAVE;
7220     }
7221 #endif /* PERL_EXTERNAL_GLOB */
7222
7223     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
7224         append_elem(OP_GLOB, o,
7225                     newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
7226         o->op_type = OP_LIST;
7227         o->op_ppaddr = PL_ppaddr[OP_LIST];
7228         cLISTOPo->op_first->op_type = OP_PUSHMARK;
7229         cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
7230         cLISTOPo->op_first->op_targ = 0;
7231         o = newUNOP(OP_ENTERSUB, OPf_STACKED,
7232                     append_elem(OP_LIST, o,
7233                                 scalar(newUNOP(OP_RV2CV, 0,
7234                                                newGVOP(OP_GV, 0, gv)))));
7235         o = newUNOP(OP_NULL, 0, ck_subr(o));
7236         o->op_targ = OP_GLOB;           /* hint at what it used to be */
7237         return o;
7238     }
7239     gv = newGVgen("main");
7240     gv_IOadd(gv);
7241     append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
7242     scalarkids(o);
7243     return o;
7244 }
7245
7246 OP *
7247 Perl_ck_grep(pTHX_ OP *o)
7248 {
7249     dVAR;
7250     LOGOP *gwop = NULL;
7251     OP *kid;
7252     const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
7253     PADOFFSET offset;
7254
7255     PERL_ARGS_ASSERT_CK_GREP;
7256
7257     o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
7258     /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
7259
7260     if (o->op_flags & OPf_STACKED) {
7261         OP* k;
7262         o = ck_sort(o);
7263         kid = cUNOPx(cLISTOPo->op_first->op_sibling)->op_first;
7264         if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
7265             return no_fh_allowed(o);
7266         for (k = kid; k; k = k->op_next) {
7267             kid = k;
7268         }
7269         NewOp(1101, gwop, 1, LOGOP);
7270         kid->op_next = (OP*)gwop;
7271         o->op_flags &= ~OPf_STACKED;
7272     }
7273     kid = cLISTOPo->op_first->op_sibling;
7274     if (type == OP_MAPWHILE)
7275         list(kid);
7276     else
7277         scalar(kid);
7278     o = ck_fun(o);
7279     if (PL_parser && PL_parser->error_count)
7280         return o;
7281     kid = cLISTOPo->op_first->op_sibling;
7282     if (kid->op_type != OP_NULL)
7283         Perl_croak(aTHX_ "panic: ck_grep");
7284     kid = kUNOP->op_first;
7285
7286     if (!gwop)
7287         NewOp(1101, gwop, 1, LOGOP);
7288     gwop->op_type = type;
7289     gwop->op_ppaddr = PL_ppaddr[type];
7290     gwop->op_first = listkids(o);
7291     gwop->op_flags |= OPf_KIDS;
7292     gwop->op_other = LINKLIST(kid);
7293     kid->op_next = (OP*)gwop;
7294     offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0);
7295     if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
7296         o->op_private = gwop->op_private = 0;
7297         gwop->op_targ = pad_alloc(type, SVs_PADTMP);
7298     }
7299     else {
7300         o->op_private = gwop->op_private = OPpGREP_LEX;
7301         gwop->op_targ = o->op_targ = offset;
7302     }
7303
7304     kid = cLISTOPo->op_first->op_sibling;
7305     if (!kid || !kid->op_sibling)
7306         return too_few_arguments(o,OP_DESC(o));
7307     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
7308         mod(kid, OP_GREPSTART);
7309
7310     return (OP*)gwop;
7311 }
7312
7313 OP *
7314 Perl_ck_index(pTHX_ OP *o)
7315 {
7316     PERL_ARGS_ASSERT_CK_INDEX;
7317
7318     if (o->op_flags & OPf_KIDS) {
7319         OP *kid = cLISTOPo->op_first->op_sibling;       /* get past pushmark */
7320         if (kid)
7321             kid = kid->op_sibling;                      /* get past "big" */
7322         if (kid && kid->op_type == OP_CONST)
7323             fbm_compile(((SVOP*)kid)->op_sv, 0);
7324     }
7325     return ck_fun(o);
7326 }
7327
7328 OP *
7329 Perl_ck_lfun(pTHX_ OP *o)
7330 {
7331     const OPCODE type = o->op_type;
7332
7333     PERL_ARGS_ASSERT_CK_LFUN;
7334
7335     return modkids(ck_fun(o), type);
7336 }
7337
7338 OP *
7339 Perl_ck_defined(pTHX_ OP *o)            /* 19990527 MJD */
7340 {
7341     PERL_ARGS_ASSERT_CK_DEFINED;
7342
7343     if ((o->op_flags & OPf_KIDS)) {
7344         switch (cUNOPo->op_first->op_type) {
7345         case OP_RV2AV:
7346             /* This is needed for
7347                if (defined %stash::)
7348                to work.   Do not break Tk.
7349                */
7350             break;                      /* Globals via GV can be undef */
7351         case OP_PADAV:
7352         case OP_AASSIGN:                /* Is this a good idea? */
7353             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7354                            "defined(@array) is deprecated");
7355             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7356                            "\t(Maybe you should just omit the defined()?)\n");
7357         break;
7358         case OP_RV2HV:
7359         case OP_PADHV:
7360             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7361                            "defined(%%hash) is deprecated");
7362             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7363                            "\t(Maybe you should just omit the defined()?)\n");
7364             break;
7365         default:
7366             /* no warning */
7367             break;
7368         }
7369     }
7370     return ck_rfun(o);
7371 }
7372
7373 OP *
7374 Perl_ck_readline(pTHX_ OP *o)
7375 {
7376     PERL_ARGS_ASSERT_CK_READLINE;
7377
7378     if (!(o->op_flags & OPf_KIDS)) {
7379         OP * const newop
7380             = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
7381 #ifdef PERL_MAD
7382         op_getmad(o,newop,'O');
7383 #else
7384         op_free(o);
7385 #endif
7386         return newop;
7387     }
7388     return o;
7389 }
7390
7391 OP *
7392 Perl_ck_rfun(pTHX_ OP *o)
7393 {
7394     const OPCODE type = o->op_type;
7395
7396     PERL_ARGS_ASSERT_CK_RFUN;
7397
7398     return refkids(ck_fun(o), type);
7399 }
7400
7401 OP *
7402 Perl_ck_listiob(pTHX_ OP *o)
7403 {
7404     register OP *kid;
7405
7406     PERL_ARGS_ASSERT_CK_LISTIOB;
7407
7408     kid = cLISTOPo->op_first;
7409     if (!kid) {
7410         o = force_list(o);
7411         kid = cLISTOPo->op_first;
7412     }
7413     if (kid->op_type == OP_PUSHMARK)
7414         kid = kid->op_sibling;
7415     if (kid && o->op_flags & OPf_STACKED)
7416         kid = kid->op_sibling;
7417     else if (kid && !kid->op_sibling) {         /* print HANDLE; */
7418         if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
7419             o->op_flags |= OPf_STACKED; /* make it a filehandle */
7420             kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
7421             cLISTOPo->op_first->op_sibling = kid;
7422             cLISTOPo->op_last = kid;
7423             kid = kid->op_sibling;
7424         }
7425     }
7426
7427     if (!kid)
7428         append_elem(o->op_type, o, newDEFSVOP());
7429
7430     return listkids(o);
7431 }
7432
7433 OP *
7434 Perl_ck_smartmatch(pTHX_ OP *o)
7435 {
7436     dVAR;
7437     if (0 == (o->op_flags & OPf_SPECIAL)) {
7438         OP *first  = cBINOPo->op_first;
7439         OP *second = first->op_sibling;
7440         
7441         /* Implicitly take a reference to an array or hash */
7442         first->op_sibling = NULL;
7443         first = cBINOPo->op_first = ref_array_or_hash(first);
7444         second = first->op_sibling = ref_array_or_hash(second);
7445         
7446         /* Implicitly take a reference to a regular expression */
7447         if (first->op_type == OP_MATCH) {
7448             first->op_type = OP_QR;
7449             first->op_ppaddr = PL_ppaddr[OP_QR];
7450         }
7451         if (second->op_type == OP_MATCH) {
7452             second->op_type = OP_QR;
7453             second->op_ppaddr = PL_ppaddr[OP_QR];
7454         }
7455     }
7456     
7457     return o;
7458 }
7459
7460
7461 OP *
7462 Perl_ck_sassign(pTHX_ OP *o)
7463 {
7464     dVAR;
7465     OP * const kid = cLISTOPo->op_first;
7466
7467     PERL_ARGS_ASSERT_CK_SASSIGN;
7468
7469     /* has a disposable target? */
7470     if ((PL_opargs[kid->op_type] & OA_TARGLEX)
7471         && !(kid->op_flags & OPf_STACKED)
7472         /* Cannot steal the second time! */
7473         && !(kid->op_private & OPpTARGET_MY)
7474         /* Keep the full thing for madskills */
7475         && !PL_madskills
7476         )
7477     {
7478         OP * const kkid = kid->op_sibling;
7479
7480         /* Can just relocate the target. */
7481         if (kkid && kkid->op_type == OP_PADSV
7482             && !(kkid->op_private & OPpLVAL_INTRO))
7483         {
7484             kid->op_targ = kkid->op_targ;
7485             kkid->op_targ = 0;
7486             /* Now we do not need PADSV and SASSIGN. */
7487             kid->op_sibling = o->op_sibling;    /* NULL */
7488             cLISTOPo->op_first = NULL;
7489             op_free(o);
7490             op_free(kkid);
7491             kid->op_private |= OPpTARGET_MY;    /* Used for context settings */
7492             return kid;
7493         }
7494     }
7495     if (kid->op_sibling) {
7496         OP *kkid = kid->op_sibling;
7497         if (kkid->op_type == OP_PADSV
7498                 && (kkid->op_private & OPpLVAL_INTRO)
7499                 && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
7500             const PADOFFSET target = kkid->op_targ;
7501             OP *const other = newOP(OP_PADSV,
7502                                     kkid->op_flags
7503                                     | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
7504             OP *const first = newOP(OP_NULL, 0);
7505             OP *const nullop = newCONDOP(0, first, o, other);
7506             OP *const condop = first->op_next;
7507             /* hijacking PADSTALE for uninitialized state variables */
7508             SvPADSTALE_on(PAD_SVl(target));
7509
7510             condop->op_type = OP_ONCE;
7511             condop->op_ppaddr = PL_ppaddr[OP_ONCE];
7512             condop->op_targ = target;
7513             other->op_targ = target;
7514
7515             /* Because we change the type of the op here, we will skip the
7516                assinment binop->op_last = binop->op_first->op_sibling; at the
7517                end of Perl_newBINOP(). So need to do it here. */
7518             cBINOPo->op_last = cBINOPo->op_first->op_sibling;
7519
7520             return nullop;
7521         }
7522     }
7523     return o;
7524 }
7525
7526 OP *
7527 Perl_ck_match(pTHX_ OP *o)
7528 {
7529     dVAR;
7530
7531     PERL_ARGS_ASSERT_CK_MATCH;
7532
7533     if (o->op_type != OP_QR && PL_compcv) {
7534         const PADOFFSET offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0);
7535         if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) {
7536             o->op_targ = offset;
7537             o->op_private |= OPpTARGET_MY;
7538         }
7539     }
7540     if (o->op_type == OP_MATCH || o->op_type == OP_QR)
7541         o->op_private |= OPpRUNTIME;
7542     return o;
7543 }
7544
7545 OP *
7546 Perl_ck_method(pTHX_ OP *o)
7547 {
7548     OP * const kid = cUNOPo->op_first;
7549
7550     PERL_ARGS_ASSERT_CK_METHOD;
7551
7552     if (kid->op_type == OP_CONST) {
7553         SV* sv = kSVOP->op_sv;
7554         const char * const method = SvPVX_const(sv);
7555         if (!(strchr(method, ':') || strchr(method, '\''))) {
7556             OP *cmop;
7557             if (!SvREADONLY(sv) || !SvFAKE(sv)) {
7558                 sv = newSVpvn_share(method, SvCUR(sv), 0);
7559             }
7560             else {
7561                 kSVOP->op_sv = NULL;
7562             }
7563             cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
7564 #ifdef PERL_MAD
7565             op_getmad(o,cmop,'O');
7566 #else
7567             op_free(o);
7568 #endif
7569             return cmop;
7570         }
7571     }
7572     return o;
7573 }
7574
7575 OP *
7576 Perl_ck_null(pTHX_ OP *o)
7577 {
7578     PERL_ARGS_ASSERT_CK_NULL;
7579     PERL_UNUSED_CONTEXT;
7580     return o;
7581 }
7582
7583 OP *
7584 Perl_ck_open(pTHX_ OP *o)
7585 {
7586     dVAR;
7587     HV * const table = GvHV(PL_hintgv);
7588
7589     PERL_ARGS_ASSERT_CK_OPEN;
7590
7591     if (table) {
7592         SV **svp = hv_fetchs(table, "open_IN", FALSE);
7593         if (svp && *svp) {
7594             STRLEN len = 0;
7595             const char *d = SvPV_const(*svp, len);
7596             const I32 mode = mode_from_discipline(d, len);
7597             if (mode & O_BINARY)
7598                 o->op_private |= OPpOPEN_IN_RAW;
7599             else if (mode & O_TEXT)
7600                 o->op_private |= OPpOPEN_IN_CRLF;
7601         }
7602
7603         svp = hv_fetchs(table, "open_OUT", FALSE);
7604         if (svp && *svp) {
7605             STRLEN len = 0;
7606             const char *d = SvPV_const(*svp, len);
7607             const I32 mode = mode_from_discipline(d, len);
7608             if (mode & O_BINARY)
7609                 o->op_private |= OPpOPEN_OUT_RAW;
7610             else if (mode & O_TEXT)
7611                 o->op_private |= OPpOPEN_OUT_CRLF;
7612         }
7613     }
7614     if (o->op_type == OP_BACKTICK) {
7615         if (!(o->op_flags & OPf_KIDS)) {
7616             OP * const newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
7617 #ifdef PERL_MAD
7618             op_getmad(o,newop,'O');
7619 #else
7620             op_free(o);
7621 #endif
7622             return newop;
7623         }
7624         return o;
7625     }
7626     {
7627          /* In case of three-arg dup open remove strictness
7628           * from the last arg if it is a bareword. */
7629          OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
7630          OP * const last  = cLISTOPx(o)->op_last;  /* The bareword. */
7631          OP *oa;
7632          const char *mode;
7633
7634          if ((last->op_type == OP_CONST) &&             /* The bareword. */
7635              (last->op_private & OPpCONST_BARE) &&
7636              (last->op_private & OPpCONST_STRICT) &&
7637              (oa = first->op_sibling) &&                /* The fh. */
7638              (oa = oa->op_sibling) &&                   /* The mode. */
7639              (oa->op_type == OP_CONST) &&
7640              SvPOK(((SVOP*)oa)->op_sv) &&
7641              (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
7642              mode[0] == '>' && mode[1] == '&' &&        /* A dup open. */
7643              (last == oa->op_sibling))                  /* The bareword. */
7644               last->op_private &= ~OPpCONST_STRICT;
7645     }
7646     return ck_fun(o);
7647 }
7648
7649 OP *
7650 Perl_ck_repeat(pTHX_ OP *o)
7651 {
7652     PERL_ARGS_ASSERT_CK_REPEAT;
7653
7654     if (cBINOPo->op_first->op_flags & OPf_PARENS) {
7655         o->op_private |= OPpREPEAT_DOLIST;
7656         cBINOPo->op_first = force_list(cBINOPo->op_first);
7657     }
7658     else
7659         scalar(o);
7660     return o;
7661 }
7662
7663 OP *
7664 Perl_ck_require(pTHX_ OP *o)
7665 {
7666     dVAR;
7667     GV* gv = NULL;
7668
7669     PERL_ARGS_ASSERT_CK_REQUIRE;
7670
7671     if (o->op_flags & OPf_KIDS) {       /* Shall we supply missing .pm? */
7672         SVOP * const kid = (SVOP*)cUNOPo->op_first;
7673
7674         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
7675             SV * const sv = kid->op_sv;
7676             U32 was_readonly = SvREADONLY(sv);
7677             char *s;
7678             STRLEN len;
7679             const char *end;
7680
7681             if (was_readonly) {
7682                 if (SvFAKE(sv)) {
7683                     sv_force_normal_flags(sv, 0);
7684                     assert(!SvREADONLY(sv));
7685                     was_readonly = 0;
7686                 } else {
7687                     SvREADONLY_off(sv);
7688                 }
7689             }   
7690
7691             s = SvPVX(sv);
7692             len = SvCUR(sv);
7693             end = s + len;
7694             for (; s < end; s++) {
7695                 if (*s == ':' && s[1] == ':') {
7696                     *s = '/';
7697                     Move(s+2, s+1, end - s - 1, char);
7698                     --end;
7699                 }
7700             }
7701             SvEND_set(sv, end);
7702             sv_catpvs(sv, ".pm");
7703             SvFLAGS(sv) |= was_readonly;
7704         }
7705     }
7706
7707     if (!(o->op_flags & OPf_SPECIAL)) { /* Wasn't written as CORE::require */
7708         /* handle override, if any */
7709         gv = gv_fetchpvs("require", GV_NOTQUAL, SVt_PVCV);
7710         if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
7711             GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "require", FALSE);
7712             gv = gvp ? *gvp : NULL;
7713         }
7714     }
7715
7716     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
7717         OP * const kid = cUNOPo->op_first;
7718         OP * newop;
7719
7720         cUNOPo->op_first = 0;
7721 #ifndef PERL_MAD
7722         op_free(o);
7723 #endif
7724         newop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
7725                                 append_elem(OP_LIST, kid,
7726                                             scalar(newUNOP(OP_RV2CV, 0,
7727                                                            newGVOP(OP_GV, 0,
7728                                                                    gv))))));
7729         op_getmad(o,newop,'O');
7730         return newop;
7731     }
7732
7733     return scalar(ck_fun(o));
7734 }
7735
7736 OP *
7737 Perl_ck_return(pTHX_ OP *o)
7738 {
7739     dVAR;
7740     OP *kid;
7741
7742     PERL_ARGS_ASSERT_CK_RETURN;
7743
7744     kid = cLISTOPo->op_first->op_sibling;
7745     if (CvLVALUE(PL_compcv)) {
7746         for (; kid; kid = kid->op_sibling)
7747             mod(kid, OP_LEAVESUBLV);
7748     } else {
7749         for (; kid; kid = kid->op_sibling)
7750             if ((kid->op_type == OP_NULL)
7751                 && ((kid->op_flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS))) {
7752                 /* This is a do block */
7753                 OP *op = kUNOP->op_first;
7754                 if (op->op_type == OP_LEAVE && op->op_flags & OPf_KIDS) {
7755                     op = cUNOPx(op)->op_first;
7756                     assert(op->op_type == OP_ENTER && !(op->op_flags & OPf_SPECIAL));
7757                     /* Force the use of the caller's context */
7758                     op->op_flags |= OPf_SPECIAL;
7759                 }
7760             }
7761     }
7762
7763     return o;
7764 }
7765
7766 OP *
7767 Perl_ck_select(pTHX_ OP *o)
7768 {
7769     dVAR;
7770     OP* kid;
7771
7772     PERL_ARGS_ASSERT_CK_SELECT;
7773
7774     if (o->op_flags & OPf_KIDS) {
7775         kid = cLISTOPo->op_first->op_sibling;   /* get past pushmark */
7776         if (kid && kid->op_sibling) {
7777             o->op_type = OP_SSELECT;
7778             o->op_ppaddr = PL_ppaddr[OP_SSELECT];
7779             o = ck_fun(o);
7780             return fold_constants(o);
7781         }
7782     }
7783     o = ck_fun(o);
7784     kid = cLISTOPo->op_first->op_sibling;    /* get past pushmark */
7785     if (kid && kid->op_type == OP_RV2GV)
7786         kid->op_private &= ~HINT_STRICT_REFS;
7787     return o;
7788 }
7789
7790 OP *
7791 Perl_ck_shift(pTHX_ OP *o)
7792 {
7793     dVAR;
7794     const I32 type = o->op_type;
7795
7796     PERL_ARGS_ASSERT_CK_SHIFT;
7797
7798     if (!(o->op_flags & OPf_KIDS)) {
7799         OP *argop;
7800
7801         if (!CvUNIQUE(PL_compcv)) {
7802             o->op_flags |= OPf_SPECIAL;
7803             return o;
7804         }
7805
7806         argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
7807 #ifdef PERL_MAD
7808         OP * const oldo = o;
7809         o = newUNOP(type, 0, scalar(argop));
7810         op_getmad(oldo,o,'O');
7811         return o;
7812 #else
7813         op_free(o);
7814         return newUNOP(type, 0, scalar(argop));
7815 #endif
7816     }
7817     return scalar(modkids(ck_fun(o), type));
7818 }
7819
7820 OP *
7821 Perl_ck_sort(pTHX_ OP *o)
7822 {
7823     dVAR;
7824     OP *firstkid;
7825
7826     PERL_ARGS_ASSERT_CK_SORT;
7827
7828     if (o->op_type == OP_SORT && (PL_hints & HINT_LOCALIZE_HH) != 0) {
7829         HV * const hinthv = GvHV(PL_hintgv);
7830         if (hinthv) {
7831             SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
7832             if (svp) {
7833                 const I32 sorthints = (I32)SvIV(*svp);
7834                 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
7835                     o->op_private |= OPpSORT_QSORT;
7836                 if ((sorthints & HINT_SORT_STABLE) != 0)
7837                     o->op_private |= OPpSORT_STABLE;
7838             }
7839         }
7840     }
7841
7842     if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
7843         simplify_sort(o);
7844     firstkid = cLISTOPo->op_first->op_sibling;          /* get past pushmark */
7845     if (o->op_flags & OPf_STACKED) {                    /* may have been cleared */
7846         OP *k = NULL;
7847         OP *kid = cUNOPx(firstkid)->op_first;           /* get past null */
7848
7849         if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
7850             linklist(kid);
7851             if (kid->op_type == OP_SCOPE) {
7852                 k = kid->op_next;
7853                 kid->op_next = 0;
7854             }
7855             else if (kid->op_type == OP_LEAVE) {
7856                 if (o->op_type == OP_SORT) {
7857                     op_null(kid);                       /* wipe out leave */
7858                     kid->op_next = kid;
7859
7860                     for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
7861                         if (k->op_next == kid)
7862                             k->op_next = 0;
7863                         /* don't descend into loops */
7864                         else if (k->op_type == OP_ENTERLOOP
7865                                  || k->op_type == OP_ENTERITER)
7866                         {
7867                             k = cLOOPx(k)->op_lastop;
7868                         }
7869                     }
7870                 }
7871                 else
7872                     kid->op_next = 0;           /* just disconnect the leave */
7873                 k = kLISTOP->op_first;
7874             }
7875             CALL_PEEP(k);
7876
7877             kid = firstkid;
7878             if (o->op_type == OP_SORT) {
7879                 /* provide scalar context for comparison function/block */
7880                 kid = scalar(kid);
7881                 kid->op_next = kid;
7882             }
7883             else
7884                 kid->op_next = k;
7885             o->op_flags |= OPf_SPECIAL;
7886         }
7887         else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
7888             op_null(firstkid);
7889
7890         firstkid = firstkid->op_sibling;
7891     }
7892
7893     /* provide list context for arguments */
7894     if (o->op_type == OP_SORT)
7895         list(firstkid);
7896
7897     return o;
7898 }
7899
7900 STATIC void
7901 S_simplify_sort(pTHX_ OP *o)
7902 {
7903     dVAR;
7904     register OP *kid = cLISTOPo->op_first->op_sibling;  /* get past pushmark */
7905     OP *k;
7906     int descending;
7907     GV *gv;
7908     const char *gvname;
7909
7910     PERL_ARGS_ASSERT_SIMPLIFY_SORT;
7911
7912     if (!(o->op_flags & OPf_STACKED))
7913         return;
7914     GvMULTI_on(gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV));
7915     GvMULTI_on(gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV));
7916     kid = kUNOP->op_first;                              /* get past null */
7917     if (kid->op_type != OP_SCOPE)
7918         return;
7919     kid = kLISTOP->op_last;                             /* get past scope */
7920     switch(kid->op_type) {
7921         case OP_NCMP:
7922         case OP_I_NCMP:
7923         case OP_SCMP:
7924             break;
7925         default:
7926             return;
7927     }
7928     k = kid;                                            /* remember this node*/
7929     if (kBINOP->op_first->op_type != OP_RV2SV)
7930         return;
7931     kid = kBINOP->op_first;                             /* get past cmp */
7932     if (kUNOP->op_first->op_type != OP_GV)
7933         return;
7934     kid = kUNOP->op_first;                              /* get past rv2sv */
7935     gv = kGVOP_gv;
7936     if (GvSTASH(gv) != PL_curstash)
7937         return;
7938     gvname = GvNAME(gv);
7939     if (*gvname == 'a' && gvname[1] == '\0')
7940         descending = 0;
7941     else if (*gvname == 'b' && gvname[1] == '\0')
7942         descending = 1;
7943     else
7944         return;
7945
7946     kid = k;                                            /* back to cmp */
7947     if (kBINOP->op_last->op_type != OP_RV2SV)
7948         return;
7949     kid = kBINOP->op_last;                              /* down to 2nd arg */
7950     if (kUNOP->op_first->op_type != OP_GV)
7951         return;
7952     kid = kUNOP->op_first;                              /* get past rv2sv */
7953     gv = kGVOP_gv;
7954     if (GvSTASH(gv) != PL_curstash)
7955         return;
7956     gvname = GvNAME(gv);
7957     if ( descending
7958          ? !(*gvname == 'a' && gvname[1] == '\0')
7959          : !(*gvname == 'b' && gvname[1] == '\0'))
7960         return;
7961     o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
7962     if (descending)
7963         o->op_private |= OPpSORT_DESCEND;
7964     if (k->op_type == OP_NCMP)
7965         o->op_private |= OPpSORT_NUMERIC;
7966     if (k->op_type == OP_I_NCMP)
7967         o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
7968     kid = cLISTOPo->op_first->op_sibling;
7969     cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
7970 #ifdef PERL_MAD
7971     op_getmad(kid,o,'S');                             /* then delete it */
7972 #else
7973     op_free(kid);                                     /* then delete it */
7974 #endif
7975 }
7976
7977 OP *
7978 Perl_ck_split(pTHX_ OP *o)
7979 {
7980     dVAR;
7981     register OP *kid;
7982
7983     PERL_ARGS_ASSERT_CK_SPLIT;
7984
7985     if (o->op_flags & OPf_STACKED)
7986         return no_fh_allowed(o);
7987
7988     kid = cLISTOPo->op_first;
7989     if (kid->op_type != OP_NULL)
7990         Perl_croak(aTHX_ "panic: ck_split");
7991     kid = kid->op_sibling;
7992     op_free(cLISTOPo->op_first);
7993     cLISTOPo->op_first = kid;
7994     if (!kid) {
7995         cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
7996         cLISTOPo->op_last = kid; /* There was only one element previously */
7997     }
7998
7999     if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
8000         OP * const sibl = kid->op_sibling;
8001         kid->op_sibling = 0;
8002         kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0);
8003         if (cLISTOPo->op_first == cLISTOPo->op_last)
8004             cLISTOPo->op_last = kid;
8005         cLISTOPo->op_first = kid;
8006         kid->op_sibling = sibl;
8007     }
8008
8009     kid->op_type = OP_PUSHRE;
8010     kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
8011     scalar(kid);
8012     if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
8013       Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
8014                      "Use of /g modifier is meaningless in split");
8015     }
8016
8017     if (!kid->op_sibling)
8018         append_elem(OP_SPLIT, o, newDEFSVOP());
8019
8020     kid = kid->op_sibling;
8021     scalar(kid);
8022
8023     if (!kid->op_sibling)
8024         append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
8025     assert(kid->op_sibling);
8026
8027     kid = kid->op_sibling;
8028     scalar(kid);
8029
8030     if (kid->op_sibling)
8031         return too_many_arguments(o,OP_DESC(o));
8032
8033     return o;
8034 }
8035
8036 OP *
8037 Perl_ck_join(pTHX_ OP *o)
8038 {
8039     const OP * const kid = cLISTOPo->op_first->op_sibling;
8040
8041     PERL_ARGS_ASSERT_CK_JOIN;
8042
8043     if (kid && kid->op_type == OP_MATCH) {
8044         if (ckWARN(WARN_SYNTAX)) {
8045             const REGEXP *re = PM_GETRE(kPMOP);
8046             const char *pmstr = re ? RX_PRECOMP_const(re) : "STRING";
8047             const STRLEN len = re ? RX_PRELEN(re) : 6;
8048             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
8049                         "/%.*s/ should probably be written as \"%.*s\"",
8050                         (int)len, pmstr, (int)len, pmstr);
8051         }
8052     }
8053     return ck_fun(o);
8054 }
8055
8056 OP *
8057 Perl_ck_subr(pTHX_ OP *o)
8058 {
8059     dVAR;
8060     OP *prev = ((cUNOPo->op_first->op_sibling)
8061              ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
8062     OP *o2 = prev->op_sibling;
8063     OP *cvop;
8064     const char *proto = NULL;
8065     const char *proto_end = NULL;
8066     CV *cv = NULL;
8067     GV *namegv = NULL;
8068     int optional = 0;
8069     I32 arg = 0;
8070     I32 contextclass = 0;
8071     const char *e = NULL;
8072     bool delete_op = 0;
8073
8074     PERL_ARGS_ASSERT_CK_SUBR;
8075
8076     o->op_private |= OPpENTERSUB_HASTARG;
8077     for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
8078     if (cvop->op_type == OP_RV2CV) {
8079         o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
8080         op_null(cvop);          /* disable rv2cv */
8081         if (!(o->op_private & OPpENTERSUB_AMPER)) {
8082             SVOP *tmpop = (SVOP*)((UNOP*)cvop)->op_first;
8083             GV *gv = NULL;
8084             switch (tmpop->op_type) {
8085                 case OP_GV: {
8086                     gv = cGVOPx_gv(tmpop);
8087                     cv = GvCVu(gv);
8088                     if (!cv)
8089                         tmpop->op_private |= OPpEARLY_CV;
8090                 } break;
8091                 case OP_CONST: {
8092                     SV *sv = cSVOPx_sv(tmpop);
8093                     if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)
8094                         cv = (CV*)SvRV(sv);
8095                 } break;
8096             }
8097             if (cv && SvPOK(cv)) {
8098                 STRLEN len;
8099                 namegv = gv && CvANON(cv) ? gv : CvGV(cv);
8100                 proto = SvPV(MUTABLE_SV(cv), len);
8101                 proto_end = proto + len;
8102             }
8103         }
8104     }
8105     else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
8106         if (o2->op_type == OP_CONST)
8107             o2->op_private &= ~OPpCONST_STRICT;
8108         else if (o2->op_type == OP_LIST) {
8109             OP * const sib = ((UNOP*)o2)->op_first->op_sibling;
8110             if (sib && sib->op_type == OP_CONST)
8111                 sib->op_private &= ~OPpCONST_STRICT;
8112         }
8113     }
8114     o->op_private |= (PL_hints & HINT_STRICT_REFS);
8115     if (PERLDB_SUB && PL_curstash != PL_debstash)
8116         o->op_private |= OPpENTERSUB_DB;
8117     while (o2 != cvop) {
8118         OP* o3;
8119         if (PL_madskills && o2->op_type == OP_STUB) {
8120             o2 = o2->op_sibling;
8121             continue;
8122         }
8123         if (PL_madskills && o2->op_type == OP_NULL)
8124             o3 = ((UNOP*)o2)->op_first;
8125         else
8126             o3 = o2;
8127         if (proto) {
8128             if (proto >= proto_end)
8129                 return too_many_arguments(o, gv_ename(namegv));
8130
8131             switch (*proto) {
8132             case ';':
8133                 optional = 1;
8134                 proto++;
8135                 continue;
8136             case '_':
8137                 /* _ must be at the end */
8138                 if (proto[1] && proto[1] != ';')
8139                     goto oops;
8140             case '$':
8141                 proto++;
8142                 arg++;
8143                 scalar(o2);
8144                 break;
8145             case '%':
8146             case '@':
8147                 list(o2);
8148                 arg++;
8149                 break;
8150             case '&':
8151                 proto++;
8152                 arg++;
8153                 if (o3->op_type != OP_REFGEN && o3->op_type != OP_UNDEF)
8154                     bad_type(arg,
8155                         arg == 1 ? "block or sub {}" : "sub {}",
8156                         gv_ename(namegv), o3);
8157                 break;
8158             case '*':
8159                 /* '*' allows any scalar type, including bareword */
8160                 proto++;
8161                 arg++;
8162                 if (o3->op_type == OP_RV2GV)
8163                     goto wrapref;       /* autoconvert GLOB -> GLOBref */
8164                 else if (o3->op_type == OP_CONST)
8165                     o3->op_private &= ~OPpCONST_STRICT;
8166                 else if (o3->op_type == OP_ENTERSUB) {
8167                     /* accidental subroutine, revert to bareword */
8168                     OP *gvop = ((UNOP*)o3)->op_first;
8169                     if (gvop && gvop->op_type == OP_NULL) {
8170                         gvop = ((UNOP*)gvop)->op_first;
8171                         if (gvop) {
8172                             for (; gvop->op_sibling; gvop = gvop->op_sibling)
8173                                 ;
8174                             if (gvop &&
8175                                 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
8176                                 (gvop = ((UNOP*)gvop)->op_first) &&
8177                                 gvop->op_type == OP_GV)
8178                             {
8179                                 GV * const gv = cGVOPx_gv(gvop);
8180                                 OP * const sibling = o2->op_sibling;
8181                                 SV * const n = newSVpvs("");
8182 #ifdef PERL_MAD
8183                                 OP * const oldo2 = o2;
8184 #else
8185                                 op_free(o2);
8186 #endif
8187                                 gv_fullname4(n, gv, "", FALSE);
8188                                 o2 = newSVOP(OP_CONST, 0, n);
8189                                 op_getmad(oldo2,o2,'O');
8190                                 prev->op_sibling = o2;
8191                                 o2->op_sibling = sibling;
8192                             }
8193                         }
8194                     }
8195                 }
8196                 scalar(o2);
8197                 break;
8198             case '[': case ']':
8199                  goto oops;
8200                  break;
8201             case '\\':
8202                 proto++;
8203                 arg++;
8204             again:
8205                 switch (*proto++) {
8206                 case '[':
8207                      if (contextclass++ == 0) {
8208                           e = strchr(proto, ']');
8209                           if (!e || e == proto)
8210                                goto oops;
8211                      }
8212                      else
8213                           goto oops;
8214                      goto again;
8215                      break;
8216                 case ']':
8217                      if (contextclass) {
8218                          const char *p = proto;
8219                          const char *const end = proto;
8220                          contextclass = 0;
8221                          while (*--p != '[') {}
8222                          bad_type(arg, Perl_form(aTHX_ "one of %.*s",
8223                                                  (int)(end - p), p),
8224                                   gv_ename(namegv), o3);
8225                      } else
8226                           goto oops;
8227                      break;
8228                 case '*':
8229                      if (o3->op_type == OP_RV2GV)
8230                           goto wrapref;
8231                      if (!contextclass)
8232                           bad_type(arg, "symbol", gv_ename(namegv), o3);
8233                      break;
8234                 case '&':
8235                      if (o3->op_type == OP_ENTERSUB)
8236                           goto wrapref;
8237                      if (!contextclass)
8238                           bad_type(arg, "subroutine entry", gv_ename(namegv),
8239                                    o3);
8240                      break;
8241                 case '$':
8242                     if (o3->op_type == OP_RV2SV ||
8243                         o3->op_type == OP_PADSV ||
8244                         o3->op_type == OP_HELEM ||
8245                         o3->op_type == OP_AELEM)
8246                          goto wrapref;
8247                     if (!contextclass)
8248                         bad_type(arg, "scalar", gv_ename(namegv), o3);
8249                      break;
8250                 case '@':
8251                     if (o3->op_type == OP_RV2AV ||
8252                         o3->op_type == OP_PADAV)
8253                          goto wrapref;
8254                     if (!contextclass)
8255                         bad_type(arg, "array", gv_ename(namegv), o3);
8256                     break;
8257                 case '%':
8258                     if (o3->op_type == OP_RV2HV ||
8259                         o3->op_type == OP_PADHV)
8260                          goto wrapref;
8261                     if (!contextclass)
8262                          bad_type(arg, "hash", gv_ename(namegv), o3);
8263                     break;
8264                 wrapref:
8265                     {
8266                         OP* const kid = o2;
8267                         OP* const sib = kid->op_sibling;
8268                         kid->op_sibling = 0;
8269                         o2 = newUNOP(OP_REFGEN, 0, kid);
8270                         o2->op_sibling = sib;
8271                         prev->op_sibling = o2;
8272                     }
8273                     if (contextclass && e) {
8274                          proto = e + 1;
8275                          contextclass = 0;
8276                     }
8277                     break;
8278                 default: goto oops;
8279                 }
8280                 if (contextclass)
8281                      goto again;
8282                 break;
8283             case ' ':
8284                 proto++;
8285                 continue;
8286             default:
8287               oops:
8288                 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
8289                            gv_ename(namegv), SVfARG(cv));
8290             }
8291         }
8292         else
8293             list(o2);
8294         mod(o2, OP_ENTERSUB);
8295         prev = o2;
8296         o2 = o2->op_sibling;
8297     } /* while */
8298     if (o2 == cvop && proto && *proto == '_') {
8299         /* generate an access to $_ */
8300         o2 = newDEFSVOP();
8301         o2->op_sibling = prev->op_sibling;
8302         prev->op_sibling = o2; /* instead of cvop */
8303     }
8304     if (proto && !optional && proto_end > proto &&
8305         (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
8306         return too_few_arguments(o, gv_ename(namegv));
8307     if(delete_op) {
8308 #ifdef PERL_MAD
8309         OP * const oldo = o;
8310 #else
8311         op_free(o);
8312 #endif
8313         o=newSVOP(OP_CONST, 0, newSViv(0));
8314         op_getmad(oldo,o,'O');
8315     }
8316     return o;
8317 }
8318
8319 OP *
8320 Perl_ck_svconst(pTHX_ OP *o)
8321 {
8322     PERL_ARGS_ASSERT_CK_SVCONST;
8323     PERL_UNUSED_CONTEXT;
8324     SvREADONLY_on(cSVOPo->op_sv);
8325     return o;
8326 }
8327
8328 OP *
8329 Perl_ck_chdir(pTHX_ OP *o)
8330 {
8331     if (o->op_flags & OPf_KIDS) {
8332         SVOP * const kid = (SVOP*)cUNOPo->op_first;
8333
8334         if (kid && kid->op_type == OP_CONST &&
8335             (kid->op_private & OPpCONST_BARE))
8336         {
8337             o->op_flags |= OPf_SPECIAL;
8338             kid->op_private &= ~OPpCONST_STRICT;
8339         }
8340     }
8341     return ck_fun(o);
8342 }
8343
8344 OP *
8345 Perl_ck_trunc(pTHX_ OP *o)
8346 {
8347     PERL_ARGS_ASSERT_CK_TRUNC;
8348
8349     if (o->op_flags & OPf_KIDS) {
8350         SVOP *kid = (SVOP*)cUNOPo->op_first;
8351
8352         if (kid->op_type == OP_NULL)
8353             kid = (SVOP*)kid->op_sibling;
8354         if (kid && kid->op_type == OP_CONST &&
8355             (kid->op_private & OPpCONST_BARE))
8356         {
8357             o->op_flags |= OPf_SPECIAL;
8358             kid->op_private &= ~OPpCONST_STRICT;
8359         }
8360     }
8361     return ck_fun(o);
8362 }
8363
8364 OP *
8365 Perl_ck_unpack(pTHX_ OP *o)
8366 {
8367     OP *kid = cLISTOPo->op_first;
8368
8369     PERL_ARGS_ASSERT_CK_UNPACK;
8370
8371     if (kid->op_sibling) {
8372         kid = kid->op_sibling;
8373         if (!kid->op_sibling)
8374             kid->op_sibling = newDEFSVOP();
8375     }
8376     return ck_fun(o);
8377 }
8378
8379 OP *
8380 Perl_ck_substr(pTHX_ OP *o)
8381 {
8382     PERL_ARGS_ASSERT_CK_SUBSTR;
8383
8384     o = ck_fun(o);
8385     if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
8386         OP *kid = cLISTOPo->op_first;
8387
8388         if (kid->op_type == OP_NULL)
8389             kid = kid->op_sibling;
8390         if (kid)
8391             kid->op_flags |= OPf_MOD;
8392
8393     }
8394     return o;
8395 }
8396
8397 OP *
8398 Perl_ck_each(pTHX_ OP *o)
8399 {
8400     dVAR;
8401     OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
8402
8403     PERL_ARGS_ASSERT_CK_EACH;
8404
8405     if (kid) {
8406         if (kid->op_type == OP_PADAV || kid->op_type == OP_RV2AV) {
8407             const unsigned new_type = o->op_type == OP_EACH ? OP_AEACH
8408                 : o->op_type == OP_KEYS ? OP_AKEYS : OP_AVALUES;
8409             o->op_type = new_type;
8410             o->op_ppaddr = PL_ppaddr[new_type];
8411         }
8412         else if (!(kid->op_type == OP_PADHV || kid->op_type == OP_RV2HV
8413                     || (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE)
8414                   )) {
8415             bad_type(1, "hash or array", PL_op_desc[o->op_type], kid);
8416             return o;
8417         }
8418     }
8419     return ck_fun(o);
8420 }
8421
8422 /* caller is supposed to assign the return to the 
8423    container of the rep_op var */
8424 STATIC OP *
8425 S_opt_scalarhv(pTHX_ OP *rep_op) {
8426     dVAR;
8427     UNOP *unop;
8428
8429     PERL_ARGS_ASSERT_OPT_SCALARHV;
8430
8431     NewOp(1101, unop, 1, UNOP);
8432     unop->op_type = (OPCODE)OP_BOOLKEYS;
8433     unop->op_ppaddr = PL_ppaddr[OP_BOOLKEYS];
8434     unop->op_flags = (U8)(OPf_WANT_SCALAR | OPf_KIDS );
8435     unop->op_private = (U8)(1 | ((OPf_WANT_SCALAR | OPf_KIDS) >> 8));
8436     unop->op_first = rep_op;
8437     unop->op_next = rep_op->op_next;
8438     rep_op->op_next = (OP*)unop;
8439     rep_op->op_flags|=(OPf_REF | OPf_MOD);
8440     unop->op_sibling = rep_op->op_sibling;
8441     rep_op->op_sibling = NULL;
8442     /* unop->op_targ = pad_alloc(OP_BOOLKEYS, SVs_PADTMP); */
8443     if (rep_op->op_type == OP_PADHV) { 
8444         rep_op->op_flags &= ~OPf_WANT_SCALAR;
8445         rep_op->op_flags |= OPf_WANT_LIST;
8446     }
8447     return (OP*)unop;
8448 }                        
8449
8450 /* Checks if o acts as an in-place operator on an array. oright points to the
8451  * beginning of the right-hand side. Returns the left-hand side of the
8452  * assignment if o acts in-place, or NULL otherwise. */
8453
8454 STATIC OP *
8455 S_is_inplace_av(pTHX_ OP *o, OP *oright) {
8456     OP *o2;
8457     OP *oleft = NULL;
8458
8459     PERL_ARGS_ASSERT_IS_INPLACE_AV;
8460
8461     if (!oright ||
8462         (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
8463         || oright->op_next != o
8464         || (oright->op_private & OPpLVAL_INTRO)
8465     )
8466         return NULL;
8467
8468     /* o2 follows the chain of op_nexts through the LHS of the
8469      * assign (if any) to the aassign op itself */
8470     o2 = o->op_next;
8471     if (!o2 || o2->op_type != OP_NULL)
8472         return NULL;
8473     o2 = o2->op_next;
8474     if (!o2 || o2->op_type != OP_PUSHMARK)
8475         return NULL;
8476     o2 = o2->op_next;
8477     if (o2 && o2->op_type == OP_GV)
8478         o2 = o2->op_next;
8479     if (!o2
8480         || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
8481         || (o2->op_private & OPpLVAL_INTRO)
8482     )
8483         return NULL;
8484     oleft = o2;
8485     o2 = o2->op_next;
8486     if (!o2 || o2->op_type != OP_NULL)
8487         return NULL;
8488     o2 = o2->op_next;
8489     if (!o2 || o2->op_type != OP_AASSIGN
8490             || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
8491         return NULL;
8492
8493     /* check that the sort is the first arg on RHS of assign */
8494
8495     o2 = cUNOPx(o2)->op_first;
8496     if (!o2 || o2->op_type != OP_NULL)
8497         return NULL;
8498     o2 = cUNOPx(o2)->op_first;
8499     if (!o2 || o2->op_type != OP_PUSHMARK)
8500         return NULL;
8501     if (o2->op_sibling != o)
8502         return NULL;
8503
8504     /* check the array is the same on both sides */
8505     if (oleft->op_type == OP_RV2AV) {
8506         if (oright->op_type != OP_RV2AV
8507             || !cUNOPx(oright)->op_first
8508             || cUNOPx(oright)->op_first->op_type != OP_GV
8509             || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
8510                cGVOPx_gv(cUNOPx(oright)->op_first)
8511         )
8512             return NULL;
8513     }
8514     else if (oright->op_type != OP_PADAV
8515         || oright->op_targ != oleft->op_targ
8516     )
8517         return NULL;
8518
8519     return oleft;
8520 }
8521
8522 /* A peephole optimizer.  We visit the ops in the order they're to execute.
8523  * See the comments at the top of this file for more details about when
8524  * peep() is called */
8525
8526 void
8527 Perl_peep(pTHX_ register OP *o, peep_next_t *next_peep)
8528 {
8529     dVAR;
8530     register OP* oldop = NULL;
8531
8532     PERL_ARGS_ASSERT_PEEP;
8533
8534     if (!o || o->op_opt)
8535         return;
8536     ENTER;
8537     SAVEOP();
8538     SAVEVPTR(PL_curcop);
8539     for (; o; o = o->op_next) {
8540         if (o->op_opt)
8541             break;
8542         /* By default, this op has now been optimised. A couple of cases below
8543            clear this again.  */
8544         o->op_opt = 1;
8545         PL_op = o;
8546         switch (o->op_type) {
8547         case OP_NEXTSTATE:
8548         case OP_DBSTATE:
8549             PL_curcop = ((COP*)o);              /* for warnings */
8550             break;
8551
8552         case OP_CONST:
8553             if (cSVOPo->op_private & OPpCONST_STRICT)
8554                 no_bareword_allowed(o);
8555 #ifdef USE_ITHREADS
8556         case OP_HINTSEVAL:
8557         case OP_METHOD_NAMED:
8558             /* Relocate sv to the pad for thread safety.
8559              * Despite being a "constant", the SV is written to,
8560              * for reference counts, sv_upgrade() etc. */
8561             if (cSVOP->op_sv) {
8562                 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
8563                 if (o->op_type != OP_METHOD_NAMED && SvPADTMP(cSVOPo->op_sv)) {
8564                     /* If op_sv is already a PADTMP then it is being used by
8565                      * some pad, so make a copy. */
8566                     sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
8567                     SvREADONLY_on(PAD_SVl(ix));
8568                     SvREFCNT_dec(cSVOPo->op_sv);
8569                 }
8570                 else if (o->op_type != OP_METHOD_NAMED
8571                          && cSVOPo->op_sv == &PL_sv_undef) {
8572                     /* PL_sv_undef is hack - it's unsafe to store it in the
8573                        AV that is the pad, because av_fetch treats values of
8574                        PL_sv_undef as a "free" AV entry and will merrily
8575                        replace them with a new SV, causing pad_alloc to think
8576                        that this pad slot is free. (When, clearly, it is not)
8577                     */
8578                     SvOK_off(PAD_SVl(ix));
8579                     SvPADTMP_on(PAD_SVl(ix));
8580                     SvREADONLY_on(PAD_SVl(ix));
8581                 }
8582                 else {
8583                     SvREFCNT_dec(PAD_SVl(ix));
8584                     SvPADTMP_on(cSVOPo->op_sv);
8585                     PAD_SETSV(ix, cSVOPo->op_sv);
8586                     /* XXX I don't know how this isn't readonly already. */
8587                     SvREADONLY_on(PAD_SVl(ix));
8588                 }
8589                 cSVOPo->op_sv = NULL;
8590                 o->op_targ = ix;
8591             }
8592 #endif
8593             break;
8594
8595         case OP_CONCAT:
8596             if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
8597                 if (o->op_next->op_private & OPpTARGET_MY) {
8598                     if (o->op_flags & OPf_STACKED) /* chained concats */
8599                         break; /* ignore_optimization */
8600                     else {
8601                         /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
8602                         o->op_targ = o->op_next->op_targ;
8603                         o->op_next->op_targ = 0;
8604                         o->op_private |= OPpTARGET_MY;
8605                     }
8606                 }
8607                 op_null(o->op_next);
8608             }
8609             break;
8610         case OP_STUB:
8611             if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
8612                 break; /* Scalar stub must produce undef.  List stub is noop */
8613             }
8614             goto nothin;
8615         case OP_NULL:
8616             if (o->op_targ == OP_NEXTSTATE
8617                 || o->op_targ == OP_DBSTATE)
8618             {
8619                 PL_curcop = ((COP*)o);
8620             }
8621             /* XXX: We avoid setting op_seq here to prevent later calls
8622                to peep() from mistakenly concluding that optimisation
8623                has already occurred. This doesn't fix the real problem,
8624                though (See 20010220.007). AMS 20010719 */
8625             /* op_seq functionality is now replaced by op_opt */
8626             o->op_opt = 0;
8627             /* FALL THROUGH */
8628         case OP_SCALAR:
8629         case OP_LINESEQ:
8630         case OP_SCOPE:
8631         nothin:
8632             if (oldop && o->op_next) {
8633                 oldop->op_next = o->op_next;
8634                 o->op_opt = 0;
8635                 continue;
8636             }
8637             break;
8638
8639         case OP_PADAV:
8640         case OP_GV:
8641             if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
8642                 OP* const pop = (o->op_type == OP_PADAV) ?
8643                             o->op_next : o->op_next->op_next;
8644                 IV i;
8645                 if (pop && pop->op_type == OP_CONST &&
8646                     ((PL_op = pop->op_next)) &&
8647                     pop->op_next->op_type == OP_AELEM &&
8648                     !(pop->op_next->op_private &
8649                       (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
8650                     (i = SvIV(((SVOP*)pop)->op_sv) - CopARYBASE_get(PL_curcop))
8651                                 <= 255 &&
8652                     i >= 0)
8653                 {
8654                     GV *gv;
8655                     if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
8656                         no_bareword_allowed(pop);
8657                     if (o->op_type == OP_GV)
8658                         op_null(o->op_next);
8659                     op_null(pop->op_next);
8660                     op_null(pop);
8661                     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
8662                     o->op_next = pop->op_next->op_next;
8663                     o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
8664                     o->op_private = (U8)i;
8665                     if (o->op_type == OP_GV) {
8666                         gv = cGVOPo_gv;
8667                         GvAVn(gv);
8668                     }
8669                     else
8670                         o->op_flags |= OPf_SPECIAL;
8671                     o->op_type = OP_AELEMFAST;
8672                 }
8673                 break;
8674             }
8675
8676             if (o->op_next->op_type == OP_RV2SV) {
8677                 if (!(o->op_next->op_private & OPpDEREF)) {
8678                     op_null(o->op_next);
8679                     o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
8680                                                                | OPpOUR_INTRO);
8681                     o->op_next = o->op_next->op_next;
8682                     o->op_type = OP_GVSV;
8683                     o->op_ppaddr = PL_ppaddr[OP_GVSV];
8684                 }
8685             }
8686             else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
8687                 GV * const gv = cGVOPo_gv;
8688                 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
8689                     /* XXX could check prototype here instead of just carping */
8690                     SV * const sv = sv_newmortal();
8691                     gv_efullname3(sv, gv, NULL);
8692                     Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
8693                                 "%"SVf"() called too early to check prototype",
8694                                 SVfARG(sv));
8695                 }
8696             }
8697             else if (o->op_next->op_type == OP_READLINE
8698                     && o->op_next->op_next->op_type == OP_CONCAT
8699                     && (o->op_next->op_next->op_flags & OPf_STACKED))
8700             {
8701                 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
8702                 o->op_type   = OP_RCATLINE;
8703                 o->op_flags |= OPf_STACKED;
8704                 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
8705                 op_null(o->op_next->op_next);
8706                 op_null(o->op_next);
8707             }
8708
8709             break;
8710         
8711         {
8712             OP *fop;
8713             OP *sop;
8714             
8715         case OP_NOT:
8716             fop = cUNOP->op_first;
8717             sop = NULL;
8718             goto stitch_keys;
8719             break;
8720
8721         case OP_AND:
8722         case OP_OR:
8723         case OP_DOR:
8724             fop = cLOGOP->op_first;
8725             sop = fop->op_sibling;
8726             while (cLOGOP->op_other->op_type == OP_NULL)
8727                 cLOGOP->op_other = cLOGOP->op_other->op_next;
8728             CALL_A_PEEP(next_peep, cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
8729           
8730           stitch_keys:      
8731             o->op_opt = 1;
8732             if ((fop->op_type == OP_PADHV || fop->op_type == OP_RV2HV)
8733                 || ( sop && 
8734                      (sop->op_type == OP_PADHV || sop->op_type == OP_RV2HV)
8735                     )
8736             ){  
8737                 OP * nop = o;
8738                 OP * lop = o;
8739                 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
8740                     while (nop && nop->op_next) {
8741                         switch (nop->op_next->op_type) {
8742                             case OP_NOT:
8743                             case OP_AND:
8744                             case OP_OR:
8745                             case OP_DOR:
8746                                 lop = nop = nop->op_next;
8747                                 break;
8748                             case OP_NULL:
8749                                 nop = nop->op_next;
8750                                 break;
8751                             default:
8752                                 nop = NULL;
8753                                 break;
8754                         }
8755                     }            
8756                 }
8757                 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) {
8758                     if (fop->op_type == OP_PADHV || fop->op_type == OP_RV2HV) 
8759                         cLOGOP->op_first = opt_scalarhv(fop);
8760                     if (sop && (sop->op_type == OP_PADHV || sop->op_type == OP_RV2HV)) 
8761                         cLOGOP->op_first->op_sibling = opt_scalarhv(sop);
8762                 }                                        
8763             }                  
8764             
8765             
8766             break;
8767         }    
8768         
8769         case OP_MAPWHILE:
8770         case OP_GREPWHILE:
8771         case OP_ANDASSIGN:
8772         case OP_ORASSIGN:
8773         case OP_DORASSIGN:
8774         case OP_COND_EXPR:
8775         case OP_RANGE:
8776         case OP_ONCE:
8777             while (cLOGOP->op_other->op_type == OP_NULL)
8778                 cLOGOP->op_other = cLOGOP->op_other->op_next;
8779             CALL_A_PEEP(next_peep, cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
8780             break;
8781
8782         case OP_ENTERLOOP:
8783         case OP_ENTERITER:
8784             while (cLOOP->op_redoop->op_type == OP_NULL)
8785                 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
8786             CALL_A_PEEP(next_peep, cLOOP->op_redoop);
8787             while (cLOOP->op_nextop->op_type == OP_NULL)
8788                 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
8789             CALL_A_PEEP(next_peep, cLOOP->op_nextop);
8790             while (cLOOP->op_lastop->op_type == OP_NULL)
8791                 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
8792             CALL_A_PEEP(next_peep, cLOOP->op_lastop);
8793             break;
8794
8795         case OP_SUBST:
8796             assert(!(cPMOP->op_pmflags & PMf_ONCE));
8797             while (cPMOP->op_pmstashstartu.op_pmreplstart &&
8798                    cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
8799                 cPMOP->op_pmstashstartu.op_pmreplstart
8800                     = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
8801             CALL_A_PEEP(next_peep, cPMOP->op_pmstashstartu.op_pmreplstart);
8802             break;
8803
8804         case OP_EXEC:
8805             if (o->op_next && o->op_next->op_type == OP_NEXTSTATE
8806                 && ckWARN(WARN_SYNTAX))
8807             {
8808                 if (o->op_next->op_sibling) {
8809                     const OPCODE type = o->op_next->op_sibling->op_type;
8810                     if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
8811                         const line_t oldline = CopLINE(PL_curcop);
8812                         CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
8813                         Perl_warner(aTHX_ packWARN(WARN_EXEC),
8814                                     "Statement unlikely to be reached");
8815                         Perl_warner(aTHX_ packWARN(WARN_EXEC),
8816                                     "\t(Maybe you meant system() when you said exec()?)\n");
8817                         CopLINE_set(PL_curcop, oldline);
8818                     }
8819                 }
8820             }
8821             break;
8822
8823         case OP_HELEM: {
8824             UNOP *rop;
8825             SV *lexname;
8826             GV **fields;
8827             SV **svp, *sv;
8828             const char *key = NULL;
8829             STRLEN keylen;
8830
8831             if (((BINOP*)o)->op_last->op_type != OP_CONST)
8832                 break;
8833
8834             /* Make the CONST have a shared SV */
8835             svp = cSVOPx_svp(((BINOP*)o)->op_last);
8836             if (!SvFAKE(sv = *svp) || !SvREADONLY(sv)) {
8837                 key = SvPV_const(sv, keylen);
8838                 lexname = newSVpvn_share(key,
8839                                          SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
8840                                          0);
8841                 SvREFCNT_dec(sv);
8842                 *svp = lexname;
8843             }
8844
8845             if ((o->op_private & (OPpLVAL_INTRO)))
8846                 break;
8847
8848             rop = (UNOP*)((BINOP*)o)->op_first;
8849             if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
8850                 break;
8851             lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
8852             if (!SvPAD_TYPED(lexname))
8853                 break;
8854             fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
8855             if (!fields || !GvHV(*fields))
8856                 break;
8857             key = SvPV_const(*svp, keylen);
8858             if (!hv_fetch(GvHV(*fields), key,
8859                         SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE))
8860             {
8861                 Perl_croak(aTHX_ "No such class field \"%s\" " 
8862                            "in variable %s of type %s", 
8863                       key, SvPV_nolen_const(lexname), HvNAME_get(SvSTASH(lexname)));
8864             }
8865
8866             break;
8867         }
8868
8869         case OP_HSLICE: {
8870             UNOP *rop;
8871             SV *lexname;
8872             GV **fields;
8873             SV **svp;
8874             const char *key;
8875             STRLEN keylen;
8876             SVOP *first_key_op, *key_op;
8877
8878             if ((o->op_private & (OPpLVAL_INTRO))
8879                 /* I bet there's always a pushmark... */
8880                 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
8881                 /* hmmm, no optimization if list contains only one key. */
8882                 break;
8883             rop = (UNOP*)((LISTOP*)o)->op_last;
8884             if (rop->op_type != OP_RV2HV)
8885                 break;
8886             if (rop->op_first->op_type == OP_PADSV)
8887                 /* @$hash{qw(keys here)} */
8888                 rop = (UNOP*)rop->op_first;
8889             else {
8890                 /* @{$hash}{qw(keys here)} */
8891                 if (rop->op_first->op_type == OP_SCOPE 
8892                     && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
8893                 {
8894                     rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
8895                 }
8896                 else
8897                     break;
8898             }
8899                     
8900             lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
8901             if (!SvPAD_TYPED(lexname))
8902                 break;
8903             fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
8904             if (!fields || !GvHV(*fields))
8905                 break;
8906             /* Again guessing that the pushmark can be jumped over.... */
8907             first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
8908                 ->op_first->op_sibling;
8909             for (key_op = first_key_op; key_op;
8910                  key_op = (SVOP*)key_op->op_sibling) {
8911                 if (key_op->op_type != OP_CONST)
8912                     continue;
8913                 svp = cSVOPx_svp(key_op);
8914                 key = SvPV_const(*svp, keylen);
8915                 if (!hv_fetch(GvHV(*fields), key, 
8916                             SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE))
8917                 {
8918                     Perl_croak(aTHX_ "No such class field \"%s\" "
8919                                "in variable %s of type %s",
8920                           key, SvPV_nolen(lexname), HvNAME_get(SvSTASH(lexname)));
8921                 }
8922             }
8923             break;
8924         }
8925         case OP_RV2SV:
8926         case OP_RV2AV:
8927         case OP_RV2HV:
8928             if (oldop
8929                  && (  oldop->op_type == OP_AELEM
8930                     || oldop->op_type == OP_PADSV
8931                     || oldop->op_type == OP_RV2SV
8932                     || oldop->op_type == OP_RV2GV
8933                     || oldop->op_type == OP_HELEM
8934                     )
8935                  && (oldop->op_private & OPpDEREF)
8936             ) {
8937                 o->op_private |= OPpDEREFed;
8938             }
8939
8940         case OP_SORT: {
8941             /* will point to RV2AV or PADAV op on LHS/RHS of assign */
8942             OP *oleft;
8943             OP *o2;
8944
8945             /* check that RHS of sort is a single plain array */
8946             OP *oright = cUNOPo->op_first;
8947             if (!oright || oright->op_type != OP_PUSHMARK)
8948                 break;
8949
8950             /* reverse sort ... can be optimised.  */
8951             if (!cUNOPo->op_sibling) {
8952                 /* Nothing follows us on the list. */
8953                 OP * const reverse = o->op_next;
8954
8955                 if (reverse->op_type == OP_REVERSE &&
8956                     (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
8957                     OP * const pushmark = cUNOPx(reverse)->op_first;
8958                     if (pushmark && (pushmark->op_type == OP_PUSHMARK)
8959                         && (cUNOPx(pushmark)->op_sibling == o)) {
8960                         /* reverse -> pushmark -> sort */
8961                         o->op_private |= OPpSORT_REVERSE;
8962                         op_null(reverse);
8963                         pushmark->op_next = oright->op_next;
8964                         op_null(oright);
8965                     }
8966                 }
8967             }
8968
8969             /* make @a = sort @a act in-place */
8970
8971             oright = cUNOPx(oright)->op_sibling;
8972             if (!oright)
8973                 break;
8974             if (oright->op_type == OP_NULL) { /* skip sort block/sub */
8975                 oright = cUNOPx(oright)->op_sibling;
8976             }
8977
8978             oleft = is_inplace_av(o, oright);
8979             if (!oleft)
8980                 break;
8981
8982             /* transfer MODishness etc from LHS arg to RHS arg */
8983             oright->op_flags = oleft->op_flags;
8984             o->op_private |= OPpSORT_INPLACE;
8985
8986             /* excise push->gv->rv2av->null->aassign */
8987             o2 = o->op_next->op_next;
8988             op_null(o2); /* PUSHMARK */
8989             o2 = o2->op_next;
8990             if (o2->op_type == OP_GV) {
8991                 op_null(o2); /* GV */
8992                 o2 = o2->op_next;
8993             }
8994             op_null(o2); /* RV2AV or PADAV */
8995             o2 = o2->op_next->op_next;
8996             op_null(o2); /* AASSIGN */
8997
8998             o->op_next = o2->op_next;
8999
9000             break;
9001         }
9002
9003         case OP_REVERSE: {
9004             OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
9005             OP *gvop = NULL;
9006             OP *oleft, *oright;
9007             LISTOP *enter, *exlist;
9008
9009             /* @a = reverse @a */
9010             if ((oright = cLISTOPo->op_first)
9011                     && (oright->op_type == OP_PUSHMARK)
9012                     && (oright = oright->op_sibling)
9013                     && (oleft = is_inplace_av(o, oright))) {
9014                 OP *o2;
9015
9016                 /* transfer MODishness etc from LHS arg to RHS arg */
9017                 oright->op_flags = oleft->op_flags;
9018                 o->op_private |= OPpREVERSE_INPLACE;
9019
9020                 /* excise push->gv->rv2av->null->aassign */
9021                 o2 = o->op_next->op_next;
9022                 op_null(o2); /* PUSHMARK */
9023                 o2 = o2->op_next;
9024                 if (o2->op_type == OP_GV) {
9025                     op_null(o2); /* GV */
9026                     o2 = o2->op_next;
9027                 }
9028                 op_null(o2); /* RV2AV or PADAV */
9029                 o2 = o2->op_next->op_next;
9030                 op_null(o2); /* AASSIGN */
9031
9032                 o->op_next = o2->op_next;
9033                 break;
9034             }
9035
9036             enter = (LISTOP *) o->op_next;
9037             if (!enter)
9038                 break;
9039             if (enter->op_type == OP_NULL) {
9040                 enter = (LISTOP *) enter->op_next;
9041                 if (!enter)
9042                     break;
9043             }
9044             /* for $a (...) will have OP_GV then OP_RV2GV here.
9045                for (...) just has an OP_GV.  */
9046             if (enter->op_type == OP_GV) {
9047                 gvop = (OP *) enter;
9048                 enter = (LISTOP *) enter->op_next;
9049                 if (!enter)
9050                     break;
9051                 if (enter->op_type == OP_RV2GV) {
9052                   enter = (LISTOP *) enter->op_next;
9053                   if (!enter)
9054                     break;
9055                 }
9056             }
9057
9058             if (enter->op_type != OP_ENTERITER)
9059                 break;
9060
9061             iter = enter->op_next;
9062             if (!iter || iter->op_type != OP_ITER)
9063                 break;
9064             
9065             expushmark = enter->op_first;
9066             if (!expushmark || expushmark->op_type != OP_NULL
9067                 || expushmark->op_targ != OP_PUSHMARK)
9068                 break;
9069
9070             exlist = (LISTOP *) expushmark->op_sibling;
9071             if (!exlist || exlist->op_type != OP_NULL
9072                 || exlist->op_targ != OP_LIST)
9073                 break;
9074
9075             if (exlist->op_last != o) {
9076                 /* Mmm. Was expecting to point back to this op.  */
9077                 break;
9078             }
9079             theirmark = exlist->op_first;
9080             if (!theirmark || theirmark->op_type != OP_PUSHMARK)
9081                 break;
9082
9083             if (theirmark->op_sibling != o) {
9084                 /* There's something between the mark and the reverse, eg
9085                    for (1, reverse (...))
9086                    so no go.  */
9087                 break;
9088             }
9089
9090             ourmark = ((LISTOP *)o)->op_first;
9091             if (!ourmark || ourmark->op_type != OP_PUSHMARK)
9092                 break;
9093
9094             ourlast = ((LISTOP *)o)->op_last;
9095             if (!ourlast || ourlast->op_next != o)
9096                 break;
9097
9098             rv2av = ourmark->op_sibling;
9099             if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
9100                 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
9101                 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
9102                 /* We're just reversing a single array.  */
9103                 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
9104                 enter->op_flags |= OPf_STACKED;
9105             }
9106
9107             /* We don't have control over who points to theirmark, so sacrifice
9108                ours.  */
9109             theirmark->op_next = ourmark->op_next;
9110             theirmark->op_flags = ourmark->op_flags;
9111             ourlast->op_next = gvop ? gvop : (OP *) enter;
9112             op_null(ourmark);
9113             op_null(o);
9114             enter->op_private |= OPpITER_REVERSED;
9115             iter->op_private |= OPpITER_REVERSED;
9116             
9117             break;
9118         }
9119
9120         case OP_SASSIGN: {
9121             OP *rv2gv;
9122             UNOP *refgen, *rv2cv;
9123             LISTOP *exlist;
9124
9125             if ((o->op_flags & OPf_WANT) != OPf_WANT_VOID)
9126                 break;
9127
9128             if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
9129                 break;
9130
9131             rv2gv = ((BINOP *)o)->op_last;
9132             if (!rv2gv || rv2gv->op_type != OP_RV2GV)
9133                 break;
9134
9135             refgen = (UNOP *)((BINOP *)o)->op_first;
9136
9137             if (!refgen || refgen->op_type != OP_REFGEN)
9138                 break;
9139
9140             exlist = (LISTOP *)refgen->op_first;
9141             if (!exlist || exlist->op_type != OP_NULL
9142                 || exlist->op_targ != OP_LIST)
9143                 break;
9144
9145             if (exlist->op_first->op_type != OP_PUSHMARK)
9146                 break;
9147
9148             rv2cv = (UNOP*)exlist->op_last;
9149
9150             if (rv2cv->op_type != OP_RV2CV)
9151                 break;
9152
9153             assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
9154             assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
9155             assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
9156
9157             o->op_private |= OPpASSIGN_CV_TO_GV;
9158             rv2gv->op_private |= OPpDONT_INIT_GV;
9159             rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
9160
9161             break;
9162         }
9163
9164         
9165         case OP_QR:
9166         case OP_MATCH:
9167             if (!(cPMOP->op_pmflags & PMf_ONCE)) {
9168                 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
9169             }
9170             break;
9171         }
9172         oldop = o;
9173     }
9174     LEAVE;
9175 }
9176
9177 const char*
9178 Perl_custom_op_name(pTHX_ const OP* o)
9179 {
9180     dVAR;
9181     const IV index = PTR2IV(o->op_ppaddr);
9182     SV* keysv;
9183     HE* he;
9184
9185     PERL_ARGS_ASSERT_CUSTOM_OP_NAME;
9186
9187     if (!PL_custom_op_names) /* This probably shouldn't happen */
9188         return (char *)PL_op_name[OP_CUSTOM];
9189
9190     keysv = sv_2mortal(newSViv(index));
9191
9192     he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
9193     if (!he)
9194         return (char *)PL_op_name[OP_CUSTOM]; /* Don't know who you are */
9195
9196     return SvPV_nolen(HeVAL(he));
9197 }
9198
9199 const char*
9200 Perl_custom_op_desc(pTHX_ const OP* o)
9201 {
9202     dVAR;
9203     const IV index = PTR2IV(o->op_ppaddr);
9204     SV* keysv;
9205     HE* he;
9206
9207     PERL_ARGS_ASSERT_CUSTOM_OP_DESC;
9208
9209     if (!PL_custom_op_descs)
9210         return (char *)PL_op_desc[OP_CUSTOM];
9211
9212     keysv = sv_2mortal(newSViv(index));
9213
9214     he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
9215     if (!he)
9216         return (char *)PL_op_desc[OP_CUSTOM];
9217
9218     return SvPV_nolen(HeVAL(he));
9219 }
9220
9221 #include "XSUB.h"
9222
9223 /* Efficient sub that returns a constant scalar value. */
9224 static void
9225 const_sv_xsub(pTHX_ CV* cv)
9226 {
9227     dVAR;
9228     dXSARGS;
9229     SV *const sv = MUTABLE_SV(XSANY.any_ptr);
9230     if (items != 0) {
9231         NOOP;
9232 #if 0
9233         /* diag_listed_as: SKIPME */
9234         Perl_croak(aTHX_ "usage: %s::%s()",
9235                    HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
9236 #endif
9237     }
9238     if (!sv) {
9239         XSRETURN(0);
9240     }
9241     EXTEND(sp, 1);
9242     ST(0) = sv;
9243     XSRETURN(1);
9244 }
9245
9246 /*
9247  * Local variables:
9248  * c-indentation-style: bsd
9249  * c-basic-offset: 4
9250  * indent-tabs-mode: t
9251  * End:
9252  *
9253  * ex: set ts=8 sts=4 sw=4 noet:
9254  */