This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make given() statements return the last evaluated expression
[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_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
107 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) CALL_FPTR(PL_opfreehook)(aTHX_ o)
108
109 #if defined(PL_OP_SLAB_ALLOC)
110
111 #ifdef PERL_DEBUG_READONLY_OPS
112 #  define PERL_SLAB_SIZE 4096
113 #  include <sys/mman.h>
114 #endif
115
116 #ifndef PERL_SLAB_SIZE
117 #define PERL_SLAB_SIZE 2048
118 #endif
119
120 void *
121 Perl_Slab_Alloc(pTHX_ size_t sz)
122 {
123     dVAR;
124     /*
125      * To make incrementing use count easy PL_OpSlab is an I32 *
126      * To make inserting the link to slab PL_OpPtr is I32 **
127      * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
128      * Add an overhead for pointer to slab and round up as a number of pointers
129      */
130     sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
131     if ((PL_OpSpace -= sz) < 0) {
132 #ifdef PERL_DEBUG_READONLY_OPS
133         /* We need to allocate chunk by chunk so that we can control the VM
134            mapping */
135         PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE,
136                         MAP_ANON|MAP_PRIVATE, -1, 0);
137
138         DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
139                               (unsigned long) PERL_SLAB_SIZE*sizeof(I32*),
140                               PL_OpPtr));
141         if(PL_OpPtr == MAP_FAILED) {
142             perror("mmap failed");
143             abort();
144         }
145 #else
146
147         PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*)); 
148 #endif
149         if (!PL_OpPtr) {
150             return NULL;
151         }
152         /* We reserve the 0'th I32 sized chunk as a use count */
153         PL_OpSlab = (I32 *) PL_OpPtr;
154         /* Reduce size by the use count word, and by the size we need.
155          * Latter is to mimic the '-=' in the if() above
156          */
157         PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
158         /* Allocation pointer starts at the top.
159            Theory: because we build leaves before trunk allocating at end
160            means that at run time access is cache friendly upward
161          */
162         PL_OpPtr += PERL_SLAB_SIZE;
163
164 #ifdef PERL_DEBUG_READONLY_OPS
165         /* We remember this slab.  */
166         /* This implementation isn't efficient, but it is simple. */
167         PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1));
168         PL_slabs[PL_slab_count++] = PL_OpSlab;
169         DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab));
170 #endif
171     }
172     assert( PL_OpSpace >= 0 );
173     /* Move the allocation pointer down */
174     PL_OpPtr   -= sz;
175     assert( PL_OpPtr > (I32 **) PL_OpSlab );
176     *PL_OpPtr   = PL_OpSlab;    /* Note which slab it belongs to */
177     (*PL_OpSlab)++;             /* Increment use count of slab */
178     assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
179     assert( *PL_OpSlab > 0 );
180     return (void *)(PL_OpPtr + 1);
181 }
182
183 #ifdef PERL_DEBUG_READONLY_OPS
184 void
185 Perl_pending_Slabs_to_ro(pTHX) {
186     /* Turn all the allocated op slabs read only.  */
187     U32 count = PL_slab_count;
188     I32 **const slabs = PL_slabs;
189
190     /* Reset the array of pending OP slabs, as we're about to turn this lot
191        read only. Also, do it ahead of the loop in case the warn triggers,
192        and a warn handler has an eval */
193
194     PL_slabs = NULL;
195     PL_slab_count = 0;
196
197     /* Force a new slab for any further allocation.  */
198     PL_OpSpace = 0;
199
200     while (count--) {
201         void *const start = slabs[count];
202         const size_t size = PERL_SLAB_SIZE* sizeof(I32*);
203         if(mprotect(start, size, PROT_READ)) {
204             Perl_warn(aTHX_ "mprotect for %p %lu failed with %d",
205                       start, (unsigned long) size, errno);
206         }
207     }
208
209     free(slabs);
210 }
211
212 STATIC void
213 S_Slab_to_rw(pTHX_ void *op)
214 {
215     I32 * const * const ptr = (I32 **) op;
216     I32 * const slab = ptr[-1];
217
218     PERL_ARGS_ASSERT_SLAB_TO_RW;
219
220     assert( ptr-1 > (I32 **) slab );
221     assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
222     assert( *slab > 0 );
223     if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) {
224         Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d",
225                   slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno);
226     }
227 }
228
229 OP *
230 Perl_op_refcnt_inc(pTHX_ OP *o)
231 {
232     if(o) {
233         Slab_to_rw(o);
234         ++o->op_targ;
235     }
236     return o;
237
238 }
239
240 PADOFFSET
241 Perl_op_refcnt_dec(pTHX_ OP *o)
242 {
243     PERL_ARGS_ASSERT_OP_REFCNT_DEC;
244     Slab_to_rw(o);
245     return --o->op_targ;
246 }
247 #else
248 #  define Slab_to_rw(op)
249 #endif
250
251 void
252 Perl_Slab_Free(pTHX_ void *op)
253 {
254     I32 * const * const ptr = (I32 **) op;
255     I32 * const slab = ptr[-1];
256     PERL_ARGS_ASSERT_SLAB_FREE;
257     assert( ptr-1 > (I32 **) slab );
258     assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
259     assert( *slab > 0 );
260     Slab_to_rw(op);
261     if (--(*slab) == 0) {
262 #  ifdef NETWARE
263 #    define PerlMemShared PerlMem
264 #  endif
265         
266 #ifdef PERL_DEBUG_READONLY_OPS
267         U32 count = PL_slab_count;
268         /* Need to remove this slab from our list of slabs */
269         if (count) {
270             while (count--) {
271                 if (PL_slabs[count] == slab) {
272                     dVAR;
273                     /* Found it. Move the entry at the end to overwrite it.  */
274                     DEBUG_m(PerlIO_printf(Perl_debug_log,
275                                           "Deallocate %p by moving %p from %lu to %lu\n",
276                                           PL_OpSlab,
277                                           PL_slabs[PL_slab_count - 1],
278                                           PL_slab_count, count));
279                     PL_slabs[count] = PL_slabs[--PL_slab_count];
280                     /* Could realloc smaller at this point, but probably not
281                        worth it.  */
282                     if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) {
283                         perror("munmap failed");
284                         abort();
285                     }
286                     break;
287                 }
288             }
289         }
290 #else
291     PerlMemShared_free(slab);
292 #endif
293         if (slab == PL_OpSlab) {
294             PL_OpSpace = 0;
295         }
296     }
297 }
298 #endif
299 /*
300  * In the following definition, the ", (OP*)0" is just to make the compiler
301  * think the expression is of the right type: croak actually does a Siglongjmp.
302  */
303 #define CHECKOP(type,o) \
304     ((PL_op_mask && PL_op_mask[type])                           \
305      ? ( op_free((OP*)o),                                       \
306          Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]),  \
307          (OP*)0 )                                               \
308      : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
309
310 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
311
312 STATIC const char*
313 S_gv_ename(pTHX_ GV *gv)
314 {
315     SV* const tmpsv = sv_newmortal();
316
317     PERL_ARGS_ASSERT_GV_ENAME;
318
319     gv_efullname3(tmpsv, gv, NULL);
320     return SvPV_nolen_const(tmpsv);
321 }
322
323 STATIC OP *
324 S_no_fh_allowed(pTHX_ OP *o)
325 {
326     PERL_ARGS_ASSERT_NO_FH_ALLOWED;
327
328     yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
329                  OP_DESC(o)));
330     return o;
331 }
332
333 STATIC OP *
334 S_too_few_arguments(pTHX_ OP *o, const char *name)
335 {
336     PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS;
337
338     yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
339     return o;
340 }
341
342 STATIC OP *
343 S_too_many_arguments(pTHX_ OP *o, const char *name)
344 {
345     PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS;
346
347     yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
348     return o;
349 }
350
351 STATIC void
352 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
353 {
354     PERL_ARGS_ASSERT_BAD_TYPE;
355
356     yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
357                  (int)n, name, t, OP_DESC(kid)));
358 }
359
360 STATIC void
361 S_no_bareword_allowed(pTHX_ const OP *o)
362 {
363     PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
364
365     if (PL_madskills)
366         return;         /* various ok barewords are hidden in extra OP_NULL */
367     qerror(Perl_mess(aTHX_
368                      "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
369                      SVfARG(cSVOPo_sv)));
370 }
371
372 /* "register" allocation */
373
374 PADOFFSET
375 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
376 {
377     dVAR;
378     PADOFFSET off;
379     const bool is_our = (PL_parser->in_my == KEY_our);
380
381     PERL_ARGS_ASSERT_ALLOCMY;
382
383     if (flags)
384         Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
385                    (UV)flags);
386
387     /* Until we're using the length for real, cross check that we're being
388        told the truth.  */
389     assert(strlen(name) == len);
390
391     /* complain about "my $<special_var>" etc etc */
392     if (len &&
393         !(is_our ||
394           isALPHA(name[1]) ||
395           (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
396           (name[1] == '_' && (*name == '$' || len > 2))))
397     {
398         /* name[2] is true if strlen(name) > 2  */
399         if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
400             yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
401                               name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
402                               PL_parser->in_my == KEY_state ? "state" : "my"));
403         } else {
404             yyerror(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
405                               PL_parser->in_my == KEY_state ? "state" : "my"));
406         }
407     }
408
409     /* allocate a spare slot and store the name in that slot */
410
411     off = pad_add_name(name, len,
412                        is_our ? padadd_OUR :
413                        PL_parser->in_my == KEY_state ? padadd_STATE : 0,
414                     PL_parser->in_my_stash,
415                     (is_our
416                         /* $_ is always in main::, even with our */
417                         ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
418                         : NULL
419                     )
420     );
421     /* anon sub prototypes contains state vars should always be cloned,
422      * otherwise the state var would be shared between anon subs */
423
424     if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
425         CvCLONE_on(PL_compcv);
426
427     return off;
428 }
429
430 /* free the body of an op without examining its contents.
431  * Always use this rather than FreeOp directly */
432
433 static void
434 S_op_destroy(pTHX_ OP *o)
435 {
436     if (o->op_latefree) {
437         o->op_latefreed = 1;
438         return;
439     }
440     FreeOp(o);
441 }
442
443 #ifdef USE_ITHREADS
444 #  define forget_pmop(a,b)      S_forget_pmop(aTHX_ a,b)
445 #else
446 #  define forget_pmop(a,b)      S_forget_pmop(aTHX_ a)
447 #endif
448
449 /* Destructor */
450
451 void
452 Perl_op_free(pTHX_ OP *o)
453 {
454     dVAR;
455     OPCODE type;
456
457     if (!o)
458         return;
459     if (o->op_latefreed) {
460         if (o->op_latefree)
461             return;
462         goto do_free;
463     }
464
465     type = o->op_type;
466     if (o->op_private & OPpREFCOUNTED) {
467         switch (type) {
468         case OP_LEAVESUB:
469         case OP_LEAVESUBLV:
470         case OP_LEAVEEVAL:
471         case OP_LEAVE:
472         case OP_SCOPE:
473         case OP_LEAVEWRITE:
474             {
475             PADOFFSET refcnt;
476             OP_REFCNT_LOCK;
477             refcnt = OpREFCNT_dec(o);
478             OP_REFCNT_UNLOCK;
479             if (refcnt) {
480                 /* Need to find and remove any pattern match ops from the list
481                    we maintain for reset().  */
482                 find_and_forget_pmops(o);
483                 return;
484             }
485             }
486             break;
487         default:
488             break;
489         }
490     }
491
492     /* Call the op_free hook if it has been set. Do it now so that it's called
493      * at the right time for refcounted ops, but still before all of the kids
494      * are freed. */
495     CALL_OPFREEHOOK(o);
496
497     if (o->op_flags & OPf_KIDS) {
498         register OP *kid, *nextkid;
499         for (kid = cUNOPo->op_first; kid; kid = nextkid) {
500             nextkid = kid->op_sibling; /* Get before next freeing kid */
501             op_free(kid);
502         }
503     }
504
505 #ifdef PERL_DEBUG_READONLY_OPS
506     Slab_to_rw(o);
507 #endif
508
509     /* COP* is not cleared by op_clear() so that we may track line
510      * numbers etc even after null() */
511     if (type == OP_NEXTSTATE || type == OP_DBSTATE
512             || (type == OP_NULL /* the COP might have been null'ed */
513                 && ((OPCODE)o->op_targ == OP_NEXTSTATE
514                     || (OPCODE)o->op_targ == OP_DBSTATE))) {
515         cop_free((COP*)o);
516     }
517
518     if (type == OP_NULL)
519         type = (OPCODE)o->op_targ;
520
521     op_clear(o);
522     if (o->op_latefree) {
523         o->op_latefreed = 1;
524         return;
525     }
526   do_free:
527     FreeOp(o);
528 #ifdef DEBUG_LEAKING_SCALARS
529     if (PL_op == o)
530         PL_op = NULL;
531 #endif
532 }
533
534 void
535 Perl_op_clear(pTHX_ OP *o)
536 {
537
538     dVAR;
539
540     PERL_ARGS_ASSERT_OP_CLEAR;
541
542 #ifdef PERL_MAD
543     /* if (o->op_madprop && o->op_madprop->mad_next)
544        abort(); */
545     /* FIXME for MAD - if I uncomment these two lines t/op/pack.t fails with
546        "modification of a read only value" for a reason I can't fathom why.
547        It's the "" stringification of $_, where $_ was set to '' in a foreach
548        loop, but it defies simplification into a small test case.
549        However, commenting them out has caused ext/List/Util/t/weak.t to fail
550        the last test.  */
551     /*
552       mad_free(o->op_madprop);
553       o->op_madprop = 0;
554     */
555 #endif    
556
557  retry:
558     switch (o->op_type) {
559     case OP_NULL:       /* Was holding old type, if any. */
560         if (PL_madskills && o->op_targ != OP_NULL) {
561             o->op_type = (Optype)o->op_targ;
562             o->op_targ = 0;
563             goto retry;
564         }
565     case OP_ENTEREVAL:  /* Was holding hints. */
566         o->op_targ = 0;
567         break;
568     default:
569         if (!(o->op_flags & OPf_REF)
570             || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
571             break;
572         /* FALL THROUGH */
573     case OP_GVSV:
574     case OP_GV:
575     case OP_AELEMFAST:
576         if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
577             /* not an OP_PADAV replacement */
578             GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
579 #ifdef USE_ITHREADS
580                         && PL_curpad
581 #endif
582                         ? cGVOPo_gv : NULL;
583             /* It's possible during global destruction that the GV is freed
584                before the optree. Whilst the SvREFCNT_inc is happy to bump from
585                0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
586                will trigger an assertion failure, because the entry to sv_clear
587                checks that the scalar is not already freed.  A check of for
588                !SvIS_FREED(gv) turns out to be invalid, because during global
589                destruction the reference count can be forced down to zero
590                (with SVf_BREAK set).  In which case raising to 1 and then
591                dropping to 0 triggers cleanup before it should happen.  I
592                *think* that this might actually be a general, systematic,
593                weakness of the whole idea of SVf_BREAK, in that code *is*
594                allowed to raise and lower references during global destruction,
595                so any *valid* code that happens to do this during global
596                destruction might well trigger premature cleanup.  */
597             bool still_valid = gv && SvREFCNT(gv);
598
599             if (still_valid)
600                 SvREFCNT_inc_simple_void(gv);
601 #ifdef USE_ITHREADS
602             if (cPADOPo->op_padix > 0) {
603                 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
604                  * may still exist on the pad */
605                 pad_swipe(cPADOPo->op_padix, TRUE);
606                 cPADOPo->op_padix = 0;
607             }
608 #else
609             SvREFCNT_dec(cSVOPo->op_sv);
610             cSVOPo->op_sv = NULL;
611 #endif
612             if (still_valid) {
613                 int try_downgrade = SvREFCNT(gv) == 2;
614                 SvREFCNT_dec(gv);
615                 if (try_downgrade)
616                     gv_try_downgrade(gv);
617             }
618         }
619         break;
620     case OP_METHOD_NAMED:
621     case OP_CONST:
622     case OP_HINTSEVAL:
623         SvREFCNT_dec(cSVOPo->op_sv);
624         cSVOPo->op_sv = NULL;
625 #ifdef USE_ITHREADS
626         /** Bug #15654
627           Even if op_clear does a pad_free for the target of the op,
628           pad_free doesn't actually remove the sv that exists in the pad;
629           instead it lives on. This results in that it could be reused as 
630           a target later on when the pad was reallocated.
631         **/
632         if(o->op_targ) {
633           pad_swipe(o->op_targ,1);
634           o->op_targ = 0;
635         }
636 #endif
637         break;
638     case OP_GOTO:
639     case OP_NEXT:
640     case OP_LAST:
641     case OP_REDO:
642         if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
643             break;
644         /* FALL THROUGH */
645     case OP_TRANS:
646         if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
647 #ifdef USE_ITHREADS
648             if (cPADOPo->op_padix > 0) {
649                 pad_swipe(cPADOPo->op_padix, TRUE);
650                 cPADOPo->op_padix = 0;
651             }
652 #else
653             SvREFCNT_dec(cSVOPo->op_sv);
654             cSVOPo->op_sv = NULL;
655 #endif
656         }
657         else {
658             PerlMemShared_free(cPVOPo->op_pv);
659             cPVOPo->op_pv = NULL;
660         }
661         break;
662     case OP_SUBST:
663         op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
664         goto clear_pmop;
665     case OP_PUSHRE:
666 #ifdef USE_ITHREADS
667         if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
668             /* No GvIN_PAD_off here, because other references may still
669              * exist on the pad */
670             pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
671         }
672 #else
673         SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
674 #endif
675         /* FALL THROUGH */
676     case OP_MATCH:
677     case OP_QR:
678 clear_pmop:
679         forget_pmop(cPMOPo, 1);
680         cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
681         /* we use the same protection as the "SAFE" version of the PM_ macros
682          * here since sv_clean_all might release some PMOPs
683          * after PL_regex_padav has been cleared
684          * and the clearing of PL_regex_padav needs to
685          * happen before sv_clean_all
686          */
687 #ifdef USE_ITHREADS
688         if(PL_regex_pad) {        /* We could be in destruction */
689             const IV offset = (cPMOPo)->op_pmoffset;
690             ReREFCNT_dec(PM_GETRE(cPMOPo));
691             PL_regex_pad[offset] = &PL_sv_undef;
692             sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
693                            sizeof(offset));
694         }
695 #else
696         ReREFCNT_dec(PM_GETRE(cPMOPo));
697         PM_SETRE(cPMOPo, NULL);
698 #endif
699
700         break;
701     }
702
703     if (o->op_targ > 0) {
704         pad_free(o->op_targ);
705         o->op_targ = 0;
706     }
707 }
708
709 STATIC void
710 S_cop_free(pTHX_ COP* cop)
711 {
712     PERL_ARGS_ASSERT_COP_FREE;
713
714     CopFILE_free(cop);
715     CopSTASH_free(cop);
716     if (! specialWARN(cop->cop_warnings))
717         PerlMemShared_free(cop->cop_warnings);
718     Perl_refcounted_he_free(aTHX_ cop->cop_hints_hash);
719 }
720
721 STATIC void
722 S_forget_pmop(pTHX_ PMOP *const o
723 #ifdef USE_ITHREADS
724               , U32 flags
725 #endif
726               )
727 {
728     HV * const pmstash = PmopSTASH(o);
729
730     PERL_ARGS_ASSERT_FORGET_PMOP;
731
732     if (pmstash && !SvIS_FREED(pmstash)) {
733         MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
734         if (mg) {
735             PMOP **const array = (PMOP**) mg->mg_ptr;
736             U32 count = mg->mg_len / sizeof(PMOP**);
737             U32 i = count;
738
739             while (i--) {
740                 if (array[i] == o) {
741                     /* Found it. Move the entry at the end to overwrite it.  */
742                     array[i] = array[--count];
743                     mg->mg_len = count * sizeof(PMOP**);
744                     /* Could realloc smaller at this point always, but probably
745                        not worth it. Probably worth free()ing if we're the
746                        last.  */
747                     if(!count) {
748                         Safefree(mg->mg_ptr);
749                         mg->mg_ptr = NULL;
750                     }
751                     break;
752                 }
753             }
754         }
755     }
756     if (PL_curpm == o) 
757         PL_curpm = NULL;
758 #ifdef USE_ITHREADS
759     if (flags)
760         PmopSTASH_free(o);
761 #endif
762 }
763
764 STATIC void
765 S_find_and_forget_pmops(pTHX_ OP *o)
766 {
767     PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
768
769     if (o->op_flags & OPf_KIDS) {
770         OP *kid = cUNOPo->op_first;
771         while (kid) {
772             switch (kid->op_type) {
773             case OP_SUBST:
774             case OP_PUSHRE:
775             case OP_MATCH:
776             case OP_QR:
777                 forget_pmop((PMOP*)kid, 0);
778             }
779             find_and_forget_pmops(kid);
780             kid = kid->op_sibling;
781         }
782     }
783 }
784
785 void
786 Perl_op_null(pTHX_ OP *o)
787 {
788     dVAR;
789
790     PERL_ARGS_ASSERT_OP_NULL;
791
792     if (o->op_type == OP_NULL)
793         return;
794     if (!PL_madskills)
795         op_clear(o);
796     o->op_targ = o->op_type;
797     o->op_type = OP_NULL;
798     o->op_ppaddr = PL_ppaddr[OP_NULL];
799 }
800
801 void
802 Perl_op_refcnt_lock(pTHX)
803 {
804     dVAR;
805     PERL_UNUSED_CONTEXT;
806     OP_REFCNT_LOCK;
807 }
808
809 void
810 Perl_op_refcnt_unlock(pTHX)
811 {
812     dVAR;
813     PERL_UNUSED_CONTEXT;
814     OP_REFCNT_UNLOCK;
815 }
816
817 /* Contextualizers */
818
819 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
820
821 static OP *
822 S_linklist(pTHX_ OP *o)
823 {
824     OP *first;
825
826     PERL_ARGS_ASSERT_LINKLIST;
827
828     if (o->op_next)
829         return o->op_next;
830
831     /* establish postfix order */
832     first = cUNOPo->op_first;
833     if (first) {
834         register OP *kid;
835         o->op_next = LINKLIST(first);
836         kid = first;
837         for (;;) {
838             if (kid->op_sibling) {
839                 kid->op_next = LINKLIST(kid->op_sibling);
840                 kid = kid->op_sibling;
841             } else {
842                 kid->op_next = o;
843                 break;
844             }
845         }
846     }
847     else
848         o->op_next = o;
849
850     return o->op_next;
851 }
852
853 static OP *
854 S_scalarkids(pTHX_ OP *o)
855 {
856     if (o && o->op_flags & OPf_KIDS) {
857         OP *kid;
858         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
859             scalar(kid);
860     }
861     return o;
862 }
863
864 STATIC OP *
865 S_scalarboolean(pTHX_ OP *o)
866 {
867     dVAR;
868
869     PERL_ARGS_ASSERT_SCALARBOOLEAN;
870
871     if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
872         if (ckWARN(WARN_SYNTAX)) {
873             const line_t oldline = CopLINE(PL_curcop);
874
875             if (PL_parser && PL_parser->copline != NOLINE)
876                 CopLINE_set(PL_curcop, PL_parser->copline);
877             Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
878             CopLINE_set(PL_curcop, oldline);
879         }
880     }
881     return scalar(o);
882 }
883
884 OP *
885 Perl_scalar(pTHX_ OP *o)
886 {
887     dVAR;
888     OP *kid;
889
890     /* assumes no premature commitment */
891     if (!o || (PL_parser && PL_parser->error_count)
892          || (o->op_flags & OPf_WANT)
893          || o->op_type == OP_RETURN)
894     {
895         return o;
896     }
897
898     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
899
900     switch (o->op_type) {
901     case OP_REPEAT:
902         scalar(cBINOPo->op_first);
903         break;
904     case OP_OR:
905     case OP_AND:
906     case OP_COND_EXPR:
907         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
908             scalar(kid);
909         break;
910         /* FALL THROUGH */
911     case OP_SPLIT:
912     case OP_MATCH:
913     case OP_QR:
914     case OP_SUBST:
915     case OP_NULL:
916     default:
917         if (o->op_flags & OPf_KIDS) {
918             for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
919                 scalar(kid);
920         }
921         break;
922     case OP_LEAVE:
923     case OP_LEAVETRY:
924         kid = cLISTOPo->op_first;
925         scalar(kid);
926         kid = kid->op_sibling;
927     do_kids:
928         while (kid) {
929             OP *sib = kid->op_sibling;
930             if (sib && kid->op_type != OP_LEAVEWHEN) {
931                 if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) {
932                     scalar(kid);
933                     scalarvoid(sib);
934                     break;
935                 } else
936                     scalarvoid(kid);
937             } else
938                 scalar(kid);
939             kid = sib;
940         }
941         PL_curcop = &PL_compiling;
942         break;
943     case OP_SCOPE:
944     case OP_LINESEQ:
945     case OP_LIST:
946         kid = cLISTOPo->op_first;
947         goto do_kids;
948     case OP_SORT:
949         Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
950         break;
951     }
952     return o;
953 }
954
955 OP *
956 Perl_scalarvoid(pTHX_ OP *o)
957 {
958     dVAR;
959     OP *kid;
960     const char* useless = NULL;
961     SV* sv;
962     U8 want;
963
964     PERL_ARGS_ASSERT_SCALARVOID;
965
966     /* trailing mad null ops don't count as "there" for void processing */
967     if (PL_madskills &&
968         o->op_type != OP_NULL &&
969         o->op_sibling &&
970         o->op_sibling->op_type == OP_NULL)
971     {
972         OP *sib;
973         for (sib = o->op_sibling;
974                 sib && sib->op_type == OP_NULL;
975                 sib = sib->op_sibling) ;
976         
977         if (!sib)
978             return o;
979     }
980
981     if (o->op_type == OP_NEXTSTATE
982         || o->op_type == OP_DBSTATE
983         || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
984                                       || o->op_targ == OP_DBSTATE)))
985         PL_curcop = (COP*)o;            /* for warning below */
986
987     /* assumes no premature commitment */
988     want = o->op_flags & OPf_WANT;
989     if ((want && want != OPf_WANT_SCALAR)
990          || (PL_parser && PL_parser->error_count)
991          || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
992     {
993         return o;
994     }
995
996     if ((o->op_private & OPpTARGET_MY)
997         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
998     {
999         return scalar(o);                       /* As if inside SASSIGN */
1000     }
1001
1002     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1003
1004     switch (o->op_type) {
1005     default:
1006         if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1007             break;
1008         /* FALL THROUGH */
1009     case OP_REPEAT:
1010         if (o->op_flags & OPf_STACKED)
1011             break;
1012         goto func_ops;
1013     case OP_SUBSTR:
1014         if (o->op_private == 4)
1015             break;
1016         /* FALL THROUGH */
1017     case OP_GVSV:
1018     case OP_WANTARRAY:
1019     case OP_GV:
1020     case OP_SMARTMATCH:
1021     case OP_PADSV:
1022     case OP_PADAV:
1023     case OP_PADHV:
1024     case OP_PADANY:
1025     case OP_AV2ARYLEN:
1026     case OP_REF:
1027     case OP_REFGEN:
1028     case OP_SREFGEN:
1029     case OP_DEFINED:
1030     case OP_HEX:
1031     case OP_OCT:
1032     case OP_LENGTH:
1033     case OP_VEC:
1034     case OP_INDEX:
1035     case OP_RINDEX:
1036     case OP_SPRINTF:
1037     case OP_AELEM:
1038     case OP_AELEMFAST:
1039     case OP_ASLICE:
1040     case OP_HELEM:
1041     case OP_HSLICE:
1042     case OP_UNPACK:
1043     case OP_PACK:
1044     case OP_JOIN:
1045     case OP_LSLICE:
1046     case OP_ANONLIST:
1047     case OP_ANONHASH:
1048     case OP_SORT:
1049     case OP_REVERSE:
1050     case OP_RANGE:
1051     case OP_FLIP:
1052     case OP_FLOP:
1053     case OP_CALLER:
1054     case OP_FILENO:
1055     case OP_EOF:
1056     case OP_TELL:
1057     case OP_GETSOCKNAME:
1058     case OP_GETPEERNAME:
1059     case OP_READLINK:
1060     case OP_TELLDIR:
1061     case OP_GETPPID:
1062     case OP_GETPGRP:
1063     case OP_GETPRIORITY:
1064     case OP_TIME:
1065     case OP_TMS:
1066     case OP_LOCALTIME:
1067     case OP_GMTIME:
1068     case OP_GHBYNAME:
1069     case OP_GHBYADDR:
1070     case OP_GHOSTENT:
1071     case OP_GNBYNAME:
1072     case OP_GNBYADDR:
1073     case OP_GNETENT:
1074     case OP_GPBYNAME:
1075     case OP_GPBYNUMBER:
1076     case OP_GPROTOENT:
1077     case OP_GSBYNAME:
1078     case OP_GSBYPORT:
1079     case OP_GSERVENT:
1080     case OP_GPWNAM:
1081     case OP_GPWUID:
1082     case OP_GGRNAM:
1083     case OP_GGRGID:
1084     case OP_GETLOGIN:
1085     case OP_PROTOTYPE:
1086       func_ops:
1087         if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1088             /* Otherwise it's "Useless use of grep iterator" */
1089             useless = OP_DESC(o);
1090         break;
1091
1092     case OP_SPLIT:
1093         kid = cLISTOPo->op_first;
1094         if (kid && kid->op_type == OP_PUSHRE
1095 #ifdef USE_ITHREADS
1096                 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1097 #else
1098                 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1099 #endif
1100             useless = OP_DESC(o);
1101         break;
1102
1103     case OP_NOT:
1104        kid = cUNOPo->op_first;
1105        if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1106            kid->op_type != OP_TRANS) {
1107                 goto func_ops;
1108        }
1109        useless = "negative pattern binding (!~)";
1110        break;
1111
1112     case OP_RV2GV:
1113     case OP_RV2SV:
1114     case OP_RV2AV:
1115     case OP_RV2HV:
1116         if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1117                 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1118             useless = "a variable";
1119         break;
1120
1121     case OP_CONST:
1122         sv = cSVOPo_sv;
1123         if (cSVOPo->op_private & OPpCONST_STRICT)
1124             no_bareword_allowed(o);
1125         else {
1126             if (ckWARN(WARN_VOID)) {
1127                 if (SvOK(sv)) {
1128                     SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1129                                 "a constant (%"SVf")", sv));
1130                     useless = SvPV_nolen(msv);
1131                 }
1132                 else
1133                     useless = "a constant (undef)";
1134                 if (o->op_private & OPpCONST_ARYBASE)
1135                     useless = NULL;
1136                 /* don't warn on optimised away booleans, eg 
1137                  * use constant Foo, 5; Foo || print; */
1138                 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1139                     useless = NULL;
1140                 /* the constants 0 and 1 are permitted as they are
1141                    conventionally used as dummies in constructs like
1142                         1 while some_condition_with_side_effects;  */
1143                 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1144                     useless = NULL;
1145                 else if (SvPOK(sv)) {
1146                   /* perl4's way of mixing documentation and code
1147                      (before the invention of POD) was based on a
1148                      trick to mix nroff and perl code. The trick was
1149                      built upon these three nroff macros being used in
1150                      void context. The pink camel has the details in
1151                      the script wrapman near page 319. */
1152                     const char * const maybe_macro = SvPVX_const(sv);
1153                     if (strnEQ(maybe_macro, "di", 2) ||
1154                         strnEQ(maybe_macro, "ds", 2) ||
1155                         strnEQ(maybe_macro, "ig", 2))
1156                             useless = NULL;
1157                 }
1158             }
1159         }
1160         op_null(o);             /* don't execute or even remember it */
1161         break;
1162
1163     case OP_POSTINC:
1164         o->op_type = OP_PREINC;         /* pre-increment is faster */
1165         o->op_ppaddr = PL_ppaddr[OP_PREINC];
1166         break;
1167
1168     case OP_POSTDEC:
1169         o->op_type = OP_PREDEC;         /* pre-decrement is faster */
1170         o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1171         break;
1172
1173     case OP_I_POSTINC:
1174         o->op_type = OP_I_PREINC;       /* pre-increment is faster */
1175         o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1176         break;
1177
1178     case OP_I_POSTDEC:
1179         o->op_type = OP_I_PREDEC;       /* pre-decrement is faster */
1180         o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1181         break;
1182
1183     case OP_OR:
1184     case OP_AND:
1185         kid = cLOGOPo->op_first;
1186         if (kid->op_type == OP_NOT
1187             && (kid->op_flags & OPf_KIDS)
1188             && !PL_madskills) {
1189             if (o->op_type == OP_AND) {
1190                 o->op_type = OP_OR;
1191                 o->op_ppaddr = PL_ppaddr[OP_OR];
1192             } else {
1193                 o->op_type = OP_AND;
1194                 o->op_ppaddr = PL_ppaddr[OP_AND];
1195             }
1196             op_null(kid);
1197         }
1198
1199     case OP_DOR:
1200     case OP_COND_EXPR:
1201     case OP_ENTERGIVEN:
1202     case OP_ENTERWHEN:
1203         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1204             scalarvoid(kid);
1205         break;
1206
1207     case OP_NULL:
1208         if (o->op_flags & OPf_STACKED)
1209             break;
1210         /* FALL THROUGH */
1211     case OP_NEXTSTATE:
1212     case OP_DBSTATE:
1213     case OP_ENTERTRY:
1214     case OP_ENTER:
1215         if (!(o->op_flags & OPf_KIDS))
1216             break;
1217         /* FALL THROUGH */
1218     case OP_SCOPE:
1219     case OP_LEAVE:
1220     case OP_LEAVETRY:
1221     case OP_LEAVELOOP:
1222     case OP_LINESEQ:
1223     case OP_LIST:
1224     case OP_LEAVEGIVEN:
1225     case OP_LEAVEWHEN:
1226         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1227             scalarvoid(kid);
1228         break;
1229     case OP_ENTEREVAL:
1230         scalarkids(o);
1231         break;
1232     case OP_SCALAR:
1233         return scalar(o);
1234     }
1235     if (useless)
1236         Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
1237     return o;
1238 }
1239
1240 static OP *
1241 S_listkids(pTHX_ OP *o)
1242 {
1243     if (o && o->op_flags & OPf_KIDS) {
1244         OP *kid;
1245         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1246             list(kid);
1247     }
1248     return o;
1249 }
1250
1251 OP *
1252 Perl_list(pTHX_ OP *o)
1253 {
1254     dVAR;
1255     OP *kid;
1256
1257     /* assumes no premature commitment */
1258     if (!o || (o->op_flags & OPf_WANT)
1259          || (PL_parser && PL_parser->error_count)
1260          || o->op_type == OP_RETURN)
1261     {
1262         return o;
1263     }
1264
1265     if ((o->op_private & OPpTARGET_MY)
1266         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1267     {
1268         return o;                               /* As if inside SASSIGN */
1269     }
1270
1271     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1272
1273     switch (o->op_type) {
1274     case OP_FLOP:
1275     case OP_REPEAT:
1276         list(cBINOPo->op_first);
1277         break;
1278     case OP_OR:
1279     case OP_AND:
1280     case OP_COND_EXPR:
1281         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1282             list(kid);
1283         break;
1284     default:
1285     case OP_MATCH:
1286     case OP_QR:
1287     case OP_SUBST:
1288     case OP_NULL:
1289         if (!(o->op_flags & OPf_KIDS))
1290             break;
1291         if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1292             list(cBINOPo->op_first);
1293             return gen_constant_list(o);
1294         }
1295     case OP_LIST:
1296         listkids(o);
1297         break;
1298     case OP_LEAVE:
1299     case OP_LEAVETRY:
1300         kid = cLISTOPo->op_first;
1301         list(kid);
1302         kid = kid->op_sibling;
1303     do_kids:
1304         while (kid) {
1305             OP *sib = kid->op_sibling;
1306             if (sib && kid->op_type != OP_LEAVEWHEN) {
1307                 if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) {
1308                     list(kid);
1309                     scalarvoid(sib);
1310                     break;
1311                 } else
1312                     scalarvoid(kid);
1313             } else
1314                 list(kid);
1315             kid = sib;
1316         }
1317         PL_curcop = &PL_compiling;
1318         break;
1319     case OP_SCOPE:
1320     case OP_LINESEQ:
1321         kid = cLISTOPo->op_first;
1322         goto do_kids;
1323     }
1324     return o;
1325 }
1326
1327 static OP *
1328 S_scalarseq(pTHX_ OP *o)
1329 {
1330     dVAR;
1331     if (o) {
1332         const OPCODE type = o->op_type;
1333
1334         if (type == OP_LINESEQ || type == OP_SCOPE ||
1335             type == OP_LEAVE || type == OP_LEAVETRY)
1336         {
1337             OP *kid;
1338             for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1339                 if (kid->op_sibling) {
1340                     scalarvoid(kid);
1341                 }
1342             }
1343             PL_curcop = &PL_compiling;
1344         }
1345         o->op_flags &= ~OPf_PARENS;
1346         if (PL_hints & HINT_BLOCK_SCOPE)
1347             o->op_flags |= OPf_PARENS;
1348     }
1349     else
1350         o = newOP(OP_STUB, 0);
1351     return o;
1352 }
1353
1354 STATIC OP *
1355 S_modkids(pTHX_ OP *o, I32 type)
1356 {
1357     if (o && o->op_flags & OPf_KIDS) {
1358         OP *kid;
1359         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1360             mod(kid, type);
1361     }
1362     return o;
1363 }
1364
1365 /* Propagate lvalue ("modifiable") context to an op and its children.
1366  * 'type' represents the context type, roughly based on the type of op that
1367  * would do the modifying, although local() is represented by OP_NULL.
1368  * It's responsible for detecting things that can't be modified,  flag
1369  * things that need to behave specially in an lvalue context (e.g., "$$x = 5"
1370  * might have to vivify a reference in $x), and so on.
1371  *
1372  * For example, "$a+1 = 2" would cause mod() to be called with o being
1373  * OP_ADD and type being OP_SASSIGN, and would output an error.
1374  */
1375
1376 OP *
1377 Perl_mod(pTHX_ OP *o, I32 type)
1378 {
1379     dVAR;
1380     OP *kid;
1381     /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1382     int localize = -1;
1383
1384     if (!o || (PL_parser && PL_parser->error_count))
1385         return o;
1386
1387     if ((o->op_private & OPpTARGET_MY)
1388         && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1389     {
1390         return o;
1391     }
1392
1393     switch (o->op_type) {
1394     case OP_UNDEF:
1395         localize = 0;
1396         PL_modcount++;
1397         return o;
1398     case OP_CONST:
1399         if (!(o->op_private & OPpCONST_ARYBASE))
1400             goto nomod;
1401         localize = 0;
1402         if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1403             CopARYBASE_set(&PL_compiling,
1404                            (I32)SvIV(cSVOPx(PL_eval_start)->op_sv));
1405             PL_eval_start = 0;
1406         }
1407         else if (!type) {
1408             SAVECOPARYBASE(&PL_compiling);
1409             CopARYBASE_set(&PL_compiling, 0);
1410         }
1411         else if (type == OP_REFGEN)
1412             goto nomod;
1413         else
1414             Perl_croak(aTHX_ "That use of $[ is unsupported");
1415         break;
1416     case OP_STUB:
1417         if ((o->op_flags & OPf_PARENS) || PL_madskills)
1418             break;
1419         goto nomod;
1420     case OP_ENTERSUB:
1421         if ((type == OP_UNDEF || type == OP_REFGEN) &&
1422             !(o->op_flags & OPf_STACKED)) {
1423             o->op_type = OP_RV2CV;              /* entersub => rv2cv */
1424             /* The default is to set op_private to the number of children,
1425                which for a UNOP such as RV2CV is always 1. And w're using
1426                the bit for a flag in RV2CV, so we need it clear.  */
1427             o->op_private &= ~1;
1428             o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1429             assert(cUNOPo->op_first->op_type == OP_NULL);
1430             op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1431             break;
1432         }
1433         else if (o->op_private & OPpENTERSUB_NOMOD)
1434             return o;
1435         else {                          /* lvalue subroutine call */
1436             o->op_private |= OPpLVAL_INTRO;
1437             PL_modcount = RETURN_UNLIMITED_NUMBER;
1438             if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1439                 /* Backward compatibility mode: */
1440                 o->op_private |= OPpENTERSUB_INARGS;
1441                 break;
1442             }
1443             else {                      /* Compile-time error message: */
1444                 OP *kid = cUNOPo->op_first;
1445                 CV *cv;
1446                 OP *okid;
1447
1448                 if (kid->op_type != OP_PUSHMARK) {
1449                     if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1450                         Perl_croak(aTHX_
1451                                 "panic: unexpected lvalue entersub "
1452                                 "args: type/targ %ld:%"UVuf,
1453                                 (long)kid->op_type, (UV)kid->op_targ);
1454                     kid = kLISTOP->op_first;
1455                 }
1456                 while (kid->op_sibling)
1457                     kid = kid->op_sibling;
1458                 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1459                     /* Indirect call */
1460                     if (kid->op_type == OP_METHOD_NAMED
1461                         || kid->op_type == OP_METHOD)
1462                     {
1463                         UNOP *newop;
1464
1465                         NewOp(1101, newop, 1, UNOP);
1466                         newop->op_type = OP_RV2CV;
1467                         newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1468                         newop->op_first = NULL;
1469                         newop->op_next = (OP*)newop;
1470                         kid->op_sibling = (OP*)newop;
1471                         newop->op_private |= OPpLVAL_INTRO;
1472                         newop->op_private &= ~1;
1473                         break;
1474                     }
1475
1476                     if (kid->op_type != OP_RV2CV)
1477                         Perl_croak(aTHX_
1478                                    "panic: unexpected lvalue entersub "
1479                                    "entry via type/targ %ld:%"UVuf,
1480                                    (long)kid->op_type, (UV)kid->op_targ);
1481                     kid->op_private |= OPpLVAL_INTRO;
1482                     break;      /* Postpone until runtime */
1483                 }
1484
1485                 okid = kid;
1486                 kid = kUNOP->op_first;
1487                 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1488                     kid = kUNOP->op_first;
1489                 if (kid->op_type == OP_NULL)
1490                     Perl_croak(aTHX_
1491                                "Unexpected constant lvalue entersub "
1492                                "entry via type/targ %ld:%"UVuf,
1493                                (long)kid->op_type, (UV)kid->op_targ);
1494                 if (kid->op_type != OP_GV) {
1495                     /* Restore RV2CV to check lvalueness */
1496                   restore_2cv:
1497                     if (kid->op_next && kid->op_next != kid) { /* Happens? */
1498                         okid->op_next = kid->op_next;
1499                         kid->op_next = okid;
1500                     }
1501                     else
1502                         okid->op_next = NULL;
1503                     okid->op_type = OP_RV2CV;
1504                     okid->op_targ = 0;
1505                     okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1506                     okid->op_private |= OPpLVAL_INTRO;
1507                     okid->op_private &= ~1;
1508                     break;
1509                 }
1510
1511                 cv = GvCV(kGVOP_gv);
1512                 if (!cv)
1513                     goto restore_2cv;
1514                 if (CvLVALUE(cv))
1515                     break;
1516             }
1517         }
1518         /* FALL THROUGH */
1519     default:
1520       nomod:
1521         /* grep, foreach, subcalls, refgen */
1522         if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1523             break;
1524         yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1525                      (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1526                       ? "do block"
1527                       : (o->op_type == OP_ENTERSUB
1528                         ? "non-lvalue subroutine call"
1529                         : OP_DESC(o))),
1530                      type ? PL_op_desc[type] : "local"));
1531         return o;
1532
1533     case OP_PREINC:
1534     case OP_PREDEC:
1535     case OP_POW:
1536     case OP_MULTIPLY:
1537     case OP_DIVIDE:
1538     case OP_MODULO:
1539     case OP_REPEAT:
1540     case OP_ADD:
1541     case OP_SUBTRACT:
1542     case OP_CONCAT:
1543     case OP_LEFT_SHIFT:
1544     case OP_RIGHT_SHIFT:
1545     case OP_BIT_AND:
1546     case OP_BIT_XOR:
1547     case OP_BIT_OR:
1548     case OP_I_MULTIPLY:
1549     case OP_I_DIVIDE:
1550     case OP_I_MODULO:
1551     case OP_I_ADD:
1552     case OP_I_SUBTRACT:
1553         if (!(o->op_flags & OPf_STACKED))
1554             goto nomod;
1555         PL_modcount++;
1556         break;
1557
1558     case OP_COND_EXPR:
1559         localize = 1;
1560         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1561             mod(kid, type);
1562         break;
1563
1564     case OP_RV2AV:
1565     case OP_RV2HV:
1566         if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1567            PL_modcount = RETURN_UNLIMITED_NUMBER;
1568             return o;           /* Treat \(@foo) like ordinary list. */
1569         }
1570         /* FALL THROUGH */
1571     case OP_RV2GV:
1572         if (scalar_mod_type(o, type))
1573             goto nomod;
1574         ref(cUNOPo->op_first, o->op_type);
1575         /* FALL THROUGH */
1576     case OP_ASLICE:
1577     case OP_HSLICE:
1578         if (type == OP_LEAVESUBLV)
1579             o->op_private |= OPpMAYBE_LVSUB;
1580         localize = 1;
1581         /* FALL THROUGH */
1582     case OP_AASSIGN:
1583     case OP_NEXTSTATE:
1584     case OP_DBSTATE:
1585        PL_modcount = RETURN_UNLIMITED_NUMBER;
1586         break;
1587     case OP_AV2ARYLEN:
1588         PL_hints |= HINT_BLOCK_SCOPE;
1589         if (type == OP_LEAVESUBLV)
1590             o->op_private |= OPpMAYBE_LVSUB;
1591         PL_modcount++;
1592         break;
1593     case OP_RV2SV:
1594         ref(cUNOPo->op_first, o->op_type);
1595         localize = 1;
1596         /* FALL THROUGH */
1597     case OP_GV:
1598         PL_hints |= HINT_BLOCK_SCOPE;
1599     case OP_SASSIGN:
1600     case OP_ANDASSIGN:
1601     case OP_ORASSIGN:
1602     case OP_DORASSIGN:
1603         PL_modcount++;
1604         break;
1605
1606     case OP_AELEMFAST:
1607         localize = -1;
1608         PL_modcount++;
1609         break;
1610
1611     case OP_PADAV:
1612     case OP_PADHV:
1613        PL_modcount = RETURN_UNLIMITED_NUMBER;
1614         if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1615             return o;           /* Treat \(@foo) like ordinary list. */
1616         if (scalar_mod_type(o, type))
1617             goto nomod;
1618         if (type == OP_LEAVESUBLV)
1619             o->op_private |= OPpMAYBE_LVSUB;
1620         /* FALL THROUGH */
1621     case OP_PADSV:
1622         PL_modcount++;
1623         if (!type) /* local() */
1624             Perl_croak(aTHX_ "Can't localize lexical variable %s",
1625                  PAD_COMPNAME_PV(o->op_targ));
1626         break;
1627
1628     case OP_PUSHMARK:
1629         localize = 0;
1630         break;
1631
1632     case OP_KEYS:
1633         if (type != OP_SASSIGN)
1634             goto nomod;
1635         goto lvalue_func;
1636     case OP_SUBSTR:
1637         if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1638             goto nomod;
1639         /* FALL THROUGH */
1640     case OP_POS:
1641     case OP_VEC:
1642         if (type == OP_LEAVESUBLV)
1643             o->op_private |= OPpMAYBE_LVSUB;
1644       lvalue_func:
1645         pad_free(o->op_targ);
1646         o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1647         assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1648         if (o->op_flags & OPf_KIDS)
1649             mod(cBINOPo->op_first->op_sibling, type);
1650         break;
1651
1652     case OP_AELEM:
1653     case OP_HELEM:
1654         ref(cBINOPo->op_first, o->op_type);
1655         if (type == OP_ENTERSUB &&
1656              !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1657             o->op_private |= OPpLVAL_DEFER;
1658         if (type == OP_LEAVESUBLV)
1659             o->op_private |= OPpMAYBE_LVSUB;
1660         localize = 1;
1661         PL_modcount++;
1662         break;
1663
1664     case OP_SCOPE:
1665     case OP_LEAVE:
1666     case OP_ENTER:
1667     case OP_LINESEQ:
1668         localize = 0;
1669         if (o->op_flags & OPf_KIDS)
1670             mod(cLISTOPo->op_last, type);
1671         break;
1672
1673     case OP_NULL:
1674         localize = 0;
1675         if (o->op_flags & OPf_SPECIAL)          /* do BLOCK */
1676             goto nomod;
1677         else if (!(o->op_flags & OPf_KIDS))
1678             break;
1679         if (o->op_targ != OP_LIST) {
1680             mod(cBINOPo->op_first, type);
1681             break;
1682         }
1683         /* FALL THROUGH */
1684     case OP_LIST:
1685         localize = 0;
1686         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1687             mod(kid, type);
1688         break;
1689
1690     case OP_RETURN:
1691         if (type != OP_LEAVESUBLV)
1692             goto nomod;
1693         break; /* mod()ing was handled by ck_return() */
1694     }
1695
1696     /* [20011101.069] File test operators interpret OPf_REF to mean that
1697        their argument is a filehandle; thus \stat(".") should not set
1698        it. AMS 20011102 */
1699     if (type == OP_REFGEN &&
1700         PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1701         return o;
1702
1703     if (type != OP_LEAVESUBLV)
1704         o->op_flags |= OPf_MOD;
1705
1706     if (type == OP_AASSIGN || type == OP_SASSIGN)
1707         o->op_flags |= OPf_SPECIAL|OPf_REF;
1708     else if (!type) { /* local() */
1709         switch (localize) {
1710         case 1:
1711             o->op_private |= OPpLVAL_INTRO;
1712             o->op_flags &= ~OPf_SPECIAL;
1713             PL_hints |= HINT_BLOCK_SCOPE;
1714             break;
1715         case 0:
1716             break;
1717         case -1:
1718             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
1719                            "Useless localization of %s", OP_DESC(o));
1720         }
1721     }
1722     else if (type != OP_GREPSTART && type != OP_ENTERSUB
1723              && type != OP_LEAVESUBLV)
1724         o->op_flags |= OPf_REF;
1725     return o;
1726 }
1727
1728 STATIC bool
1729 S_scalar_mod_type(const OP *o, I32 type)
1730 {
1731     PERL_ARGS_ASSERT_SCALAR_MOD_TYPE;
1732
1733     switch (type) {
1734     case OP_SASSIGN:
1735         if (o->op_type == OP_RV2GV)
1736             return FALSE;
1737         /* FALL THROUGH */
1738     case OP_PREINC:
1739     case OP_PREDEC:
1740     case OP_POSTINC:
1741     case OP_POSTDEC:
1742     case OP_I_PREINC:
1743     case OP_I_PREDEC:
1744     case OP_I_POSTINC:
1745     case OP_I_POSTDEC:
1746     case OP_POW:
1747     case OP_MULTIPLY:
1748     case OP_DIVIDE:
1749     case OP_MODULO:
1750     case OP_REPEAT:
1751     case OP_ADD:
1752     case OP_SUBTRACT:
1753     case OP_I_MULTIPLY:
1754     case OP_I_DIVIDE:
1755     case OP_I_MODULO:
1756     case OP_I_ADD:
1757     case OP_I_SUBTRACT:
1758     case OP_LEFT_SHIFT:
1759     case OP_RIGHT_SHIFT:
1760     case OP_BIT_AND:
1761     case OP_BIT_XOR:
1762     case OP_BIT_OR:
1763     case OP_CONCAT:
1764     case OP_SUBST:
1765     case OP_TRANS:
1766     case OP_READ:
1767     case OP_SYSREAD:
1768     case OP_RECV:
1769     case OP_ANDASSIGN:
1770     case OP_ORASSIGN:
1771     case OP_DORASSIGN:
1772         return TRUE;
1773     default:
1774         return FALSE;
1775     }
1776 }
1777
1778 STATIC bool
1779 S_is_handle_constructor(const OP *o, I32 numargs)
1780 {
1781     PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
1782
1783     switch (o->op_type) {
1784     case OP_PIPE_OP:
1785     case OP_SOCKPAIR:
1786         if (numargs == 2)
1787             return TRUE;
1788         /* FALL THROUGH */
1789     case OP_SYSOPEN:
1790     case OP_OPEN:
1791     case OP_SELECT:             /* XXX c.f. SelectSaver.pm */
1792     case OP_SOCKET:
1793     case OP_OPEN_DIR:
1794     case OP_ACCEPT:
1795         if (numargs == 1)
1796             return TRUE;
1797         /* FALLTHROUGH */
1798     default:
1799         return FALSE;
1800     }
1801 }
1802
1803 static OP *
1804 S_refkids(pTHX_ OP *o, I32 type)
1805 {
1806     if (o && o->op_flags & OPf_KIDS) {
1807         OP *kid;
1808         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1809             ref(kid, type);
1810     }
1811     return o;
1812 }
1813
1814 OP *
1815 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
1816 {
1817     dVAR;
1818     OP *kid;
1819
1820     PERL_ARGS_ASSERT_DOREF;
1821
1822     if (!o || (PL_parser && PL_parser->error_count))
1823         return o;
1824
1825     switch (o->op_type) {
1826     case OP_ENTERSUB:
1827         if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1828             !(o->op_flags & OPf_STACKED)) {
1829             o->op_type = OP_RV2CV;             /* entersub => rv2cv */
1830             o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1831             assert(cUNOPo->op_first->op_type == OP_NULL);
1832             op_null(((LISTOP*)cUNOPo->op_first)->op_first);     /* disable pushmark */
1833             o->op_flags |= OPf_SPECIAL;
1834             o->op_private &= ~1;
1835         }
1836         break;
1837
1838     case OP_COND_EXPR:
1839         for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1840             doref(kid, type, set_op_ref);
1841         break;
1842     case OP_RV2SV:
1843         if (type == OP_DEFINED)
1844             o->op_flags |= OPf_SPECIAL;         /* don't create GV */
1845         doref(cUNOPo->op_first, o->op_type, set_op_ref);
1846         /* FALL THROUGH */
1847     case OP_PADSV:
1848         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1849             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1850                               : type == OP_RV2HV ? OPpDEREF_HV
1851                               : OPpDEREF_SV);
1852             o->op_flags |= OPf_MOD;
1853         }
1854         break;
1855
1856     case OP_RV2AV:
1857     case OP_RV2HV:
1858         if (set_op_ref)
1859             o->op_flags |= OPf_REF;
1860         /* FALL THROUGH */
1861     case OP_RV2GV:
1862         if (type == OP_DEFINED)
1863             o->op_flags |= OPf_SPECIAL;         /* don't create GV */
1864         doref(cUNOPo->op_first, o->op_type, set_op_ref);
1865         break;
1866
1867     case OP_PADAV:
1868     case OP_PADHV:
1869         if (set_op_ref)
1870             o->op_flags |= OPf_REF;
1871         break;
1872
1873     case OP_SCALAR:
1874     case OP_NULL:
1875         if (!(o->op_flags & OPf_KIDS))
1876             break;
1877         doref(cBINOPo->op_first, type, set_op_ref);
1878         break;
1879     case OP_AELEM:
1880     case OP_HELEM:
1881         doref(cBINOPo->op_first, o->op_type, set_op_ref);
1882         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1883             o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1884                               : type == OP_RV2HV ? OPpDEREF_HV
1885                               : OPpDEREF_SV);
1886             o->op_flags |= OPf_MOD;
1887         }
1888         break;
1889
1890     case OP_SCOPE:
1891     case OP_LEAVE:
1892         set_op_ref = FALSE;
1893         /* FALL THROUGH */
1894     case OP_ENTER:
1895     case OP_LIST:
1896         if (!(o->op_flags & OPf_KIDS))
1897             break;
1898         doref(cLISTOPo->op_last, type, set_op_ref);
1899         break;
1900     default:
1901         break;
1902     }
1903     return scalar(o);
1904
1905 }
1906
1907 STATIC OP *
1908 S_dup_attrlist(pTHX_ OP *o)
1909 {
1910     dVAR;
1911     OP *rop;
1912
1913     PERL_ARGS_ASSERT_DUP_ATTRLIST;
1914
1915     /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1916      * where the first kid is OP_PUSHMARK and the remaining ones
1917      * are OP_CONST.  We need to push the OP_CONST values.
1918      */
1919     if (o->op_type == OP_CONST)
1920         rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
1921 #ifdef PERL_MAD
1922     else if (o->op_type == OP_NULL)
1923         rop = NULL;
1924 #endif
1925     else {
1926         assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1927         rop = NULL;
1928         for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1929             if (o->op_type == OP_CONST)
1930                 rop = append_elem(OP_LIST, rop,
1931                                   newSVOP(OP_CONST, o->op_flags,
1932                                           SvREFCNT_inc_NN(cSVOPo->op_sv)));
1933         }
1934     }
1935     return rop;
1936 }
1937
1938 STATIC void
1939 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1940 {
1941     dVAR;
1942     SV *stashsv;
1943
1944     PERL_ARGS_ASSERT_APPLY_ATTRS;
1945
1946     /* fake up C<use attributes $pkg,$rv,@attrs> */
1947     ENTER;              /* need to protect against side-effects of 'use' */
1948     stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1949
1950 #define ATTRSMODULE "attributes"
1951 #define ATTRSMODULE_PM "attributes.pm"
1952
1953     if (for_my) {
1954         /* Don't force the C<use> if we don't need it. */
1955         SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
1956         if (svp && *svp != &PL_sv_undef)
1957             NOOP;       /* already in %INC */
1958         else
1959             Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1960                              newSVpvs(ATTRSMODULE), NULL);
1961     }
1962     else {
1963         Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1964                          newSVpvs(ATTRSMODULE),
1965                          NULL,
1966                          prepend_elem(OP_LIST,
1967                                       newSVOP(OP_CONST, 0, stashsv),
1968                                       prepend_elem(OP_LIST,
1969                                                    newSVOP(OP_CONST, 0,
1970                                                            newRV(target)),
1971                                                    dup_attrlist(attrs))));
1972     }
1973     LEAVE;
1974 }
1975
1976 STATIC void
1977 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1978 {
1979     dVAR;
1980     OP *pack, *imop, *arg;
1981     SV *meth, *stashsv;
1982
1983     PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
1984
1985     if (!attrs)
1986         return;
1987
1988     assert(target->op_type == OP_PADSV ||
1989            target->op_type == OP_PADHV ||
1990            target->op_type == OP_PADAV);
1991
1992     /* Ensure that attributes.pm is loaded. */
1993     apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1994
1995     /* Need package name for method call. */
1996     pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
1997
1998     /* Build up the real arg-list. */
1999     stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2000
2001     arg = newOP(OP_PADSV, 0);
2002     arg->op_targ = target->op_targ;
2003     arg = prepend_elem(OP_LIST,
2004                        newSVOP(OP_CONST, 0, stashsv),
2005                        prepend_elem(OP_LIST,
2006                                     newUNOP(OP_REFGEN, 0,
2007                                             mod(arg, OP_REFGEN)),
2008                                     dup_attrlist(attrs)));
2009
2010     /* Fake up a method call to import */
2011     meth = newSVpvs_share("import");
2012     imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2013                    append_elem(OP_LIST,
2014                                prepend_elem(OP_LIST, pack, list(arg)),
2015                                newSVOP(OP_METHOD_NAMED, 0, meth)));
2016     imop->op_private |= OPpENTERSUB_NOMOD;
2017
2018     /* Combine the ops. */
2019     *imopsp = append_elem(OP_LIST, *imopsp, imop);
2020 }
2021
2022 /*
2023 =notfor apidoc apply_attrs_string
2024
2025 Attempts to apply a list of attributes specified by the C<attrstr> and
2026 C<len> arguments to the subroutine identified by the C<cv> argument which
2027 is expected to be associated with the package identified by the C<stashpv>
2028 argument (see L<attributes>).  It gets this wrong, though, in that it
2029 does not correctly identify the boundaries of the individual attribute
2030 specifications within C<attrstr>.  This is not really intended for the
2031 public API, but has to be listed here for systems such as AIX which
2032 need an explicit export list for symbols.  (It's called from XS code
2033 in support of the C<ATTRS:> keyword from F<xsubpp>.)  Patches to fix it
2034 to respect attribute syntax properly would be welcome.
2035
2036 =cut
2037 */
2038
2039 void
2040 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2041                         const char *attrstr, STRLEN len)
2042 {
2043     OP *attrs = NULL;
2044
2045     PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2046
2047     if (!len) {
2048         len = strlen(attrstr);
2049     }
2050
2051     while (len) {
2052         for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2053         if (len) {
2054             const char * const sstr = attrstr;
2055             for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2056             attrs = append_elem(OP_LIST, attrs,
2057                                 newSVOP(OP_CONST, 0,
2058                                         newSVpvn(sstr, attrstr-sstr)));
2059         }
2060     }
2061
2062     Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2063                      newSVpvs(ATTRSMODULE),
2064                      NULL, prepend_elem(OP_LIST,
2065                                   newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2066                                   prepend_elem(OP_LIST,
2067                                                newSVOP(OP_CONST, 0,
2068                                                        newRV(MUTABLE_SV(cv))),
2069                                                attrs)));
2070 }
2071
2072 STATIC OP *
2073 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2074 {
2075     dVAR;
2076     I32 type;
2077
2078     PERL_ARGS_ASSERT_MY_KID;
2079
2080     if (!o || (PL_parser && PL_parser->error_count))
2081         return o;
2082
2083     type = o->op_type;
2084     if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2085         (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2086         return o;
2087     }
2088
2089     if (type == OP_LIST) {
2090         OP *kid;
2091         for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2092             my_kid(kid, attrs, imopsp);
2093     } else if (type == OP_UNDEF
2094 #ifdef PERL_MAD
2095                || type == OP_STUB
2096 #endif
2097                ) {
2098         return o;
2099     } else if (type == OP_RV2SV ||      /* "our" declaration */
2100                type == OP_RV2AV ||
2101                type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2102         if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2103             yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2104                         OP_DESC(o),
2105                         PL_parser->in_my == KEY_our
2106                             ? "our"
2107                             : PL_parser->in_my == KEY_state ? "state" : "my"));
2108         } else if (attrs) {
2109             GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2110             PL_parser->in_my = FALSE;
2111             PL_parser->in_my_stash = NULL;
2112             apply_attrs(GvSTASH(gv),
2113                         (type == OP_RV2SV ? GvSV(gv) :
2114                          type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2115                          type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2116                         attrs, FALSE);
2117         }
2118         o->op_private |= OPpOUR_INTRO;
2119         return o;
2120     }
2121     else if (type != OP_PADSV &&
2122              type != OP_PADAV &&
2123              type != OP_PADHV &&
2124              type != OP_PUSHMARK)
2125     {
2126         yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2127                           OP_DESC(o),
2128                           PL_parser->in_my == KEY_our
2129                             ? "our"
2130                             : PL_parser->in_my == KEY_state ? "state" : "my"));
2131         return o;
2132     }
2133     else if (attrs && type != OP_PUSHMARK) {
2134         HV *stash;
2135
2136         PL_parser->in_my = FALSE;
2137         PL_parser->in_my_stash = NULL;
2138
2139         /* check for C<my Dog $spot> when deciding package */
2140         stash = PAD_COMPNAME_TYPE(o->op_targ);
2141         if (!stash)
2142             stash = PL_curstash;
2143         apply_attrs_my(stash, o, attrs, imopsp);
2144     }
2145     o->op_flags |= OPf_MOD;
2146     o->op_private |= OPpLVAL_INTRO;
2147     if (PL_parser->in_my == KEY_state)
2148         o->op_private |= OPpPAD_STATE;
2149     return o;
2150 }
2151
2152 OP *
2153 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2154 {
2155     dVAR;
2156     OP *rops;
2157     int maybe_scalar = 0;
2158
2159     PERL_ARGS_ASSERT_MY_ATTRS;
2160
2161 /* [perl #17376]: this appears to be premature, and results in code such as
2162    C< our(%x); > executing in list mode rather than void mode */
2163 #if 0
2164     if (o->op_flags & OPf_PARENS)
2165         list(o);
2166     else
2167         maybe_scalar = 1;
2168 #else
2169     maybe_scalar = 1;
2170 #endif
2171     if (attrs)
2172         SAVEFREEOP(attrs);
2173     rops = NULL;
2174     o = my_kid(o, attrs, &rops);
2175     if (rops) {
2176         if (maybe_scalar && o->op_type == OP_PADSV) {
2177             o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
2178             o->op_private |= OPpLVAL_INTRO;
2179         }
2180         else
2181             o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
2182     }
2183     PL_parser->in_my = FALSE;
2184     PL_parser->in_my_stash = NULL;
2185     return o;
2186 }
2187
2188 OP *
2189 Perl_sawparens(pTHX_ OP *o)
2190 {
2191     PERL_UNUSED_CONTEXT;
2192     if (o)
2193         o->op_flags |= OPf_PARENS;
2194     return o;
2195 }
2196
2197 OP *
2198 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2199 {
2200     OP *o;
2201     bool ismatchop = 0;
2202     const OPCODE ltype = left->op_type;
2203     const OPCODE rtype = right->op_type;
2204
2205     PERL_ARGS_ASSERT_BIND_MATCH;
2206
2207     if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2208           || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2209     {
2210       const char * const desc
2211           = PL_op_desc[(rtype == OP_SUBST || rtype == OP_TRANS)
2212                        ? (int)rtype : OP_MATCH];
2213       const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV)
2214              ? "@array" : "%hash");
2215       Perl_warner(aTHX_ packWARN(WARN_MISC),
2216              "Applying %s to %s will act on scalar(%s)",
2217              desc, sample, sample);
2218     }
2219
2220     if (rtype == OP_CONST &&
2221         cSVOPx(right)->op_private & OPpCONST_BARE &&
2222         cSVOPx(right)->op_private & OPpCONST_STRICT)
2223     {
2224         no_bareword_allowed(right);
2225     }
2226
2227     ismatchop = rtype == OP_MATCH ||
2228                 rtype == OP_SUBST ||
2229                 rtype == OP_TRANS;
2230     if (ismatchop && right->op_private & OPpTARGET_MY) {
2231         right->op_targ = 0;
2232         right->op_private &= ~OPpTARGET_MY;
2233     }
2234     if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2235         OP *newleft;
2236
2237         right->op_flags |= OPf_STACKED;
2238         if (rtype != OP_MATCH &&
2239             ! (rtype == OP_TRANS &&
2240                right->op_private & OPpTRANS_IDENTICAL))
2241             newleft = mod(left, rtype);
2242         else
2243             newleft = left;
2244         if (right->op_type == OP_TRANS)
2245             o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2246         else
2247             o = prepend_elem(rtype, scalar(newleft), right);
2248         if (type == OP_NOT)
2249             return newUNOP(OP_NOT, 0, scalar(o));
2250         return o;
2251     }
2252     else
2253         return bind_match(type, left,
2254                 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
2255 }
2256
2257 OP *
2258 Perl_invert(pTHX_ OP *o)
2259 {
2260     if (!o)
2261         return NULL;
2262     return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2263 }
2264
2265 OP *
2266 Perl_scope(pTHX_ OP *o)
2267 {
2268     dVAR;
2269     if (o) {
2270         if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2271             o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2272             o->op_type = OP_LEAVE;
2273             o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2274         }
2275         else if (o->op_type == OP_LINESEQ) {
2276             OP *kid;
2277             o->op_type = OP_SCOPE;
2278             o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2279             kid = ((LISTOP*)o)->op_first;
2280             if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2281                 op_null(kid);
2282
2283                 /* The following deals with things like 'do {1 for 1}' */
2284                 kid = kid->op_sibling;
2285                 if (kid &&
2286                     (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2287                     op_null(kid);
2288             }
2289         }
2290         else
2291             o = newLISTOP(OP_SCOPE, 0, o, NULL);
2292     }
2293     return o;
2294 }
2295         
2296 int
2297 Perl_block_start(pTHX_ int full)
2298 {
2299     dVAR;
2300     const int retval = PL_savestack_ix;
2301     pad_block_start(full);
2302     SAVEHINTS();
2303     PL_hints &= ~HINT_BLOCK_SCOPE;
2304     SAVECOMPILEWARNINGS();
2305     PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2306     return retval;
2307 }
2308
2309 OP*
2310 Perl_block_end(pTHX_ I32 floor, OP *seq)
2311 {
2312     dVAR;
2313     const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2314     OP* const retval = scalarseq(seq);
2315     LEAVE_SCOPE(floor);
2316     CopHINTS_set(&PL_compiling, PL_hints);
2317     if (needblockscope)
2318         PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2319     pad_leavemy();
2320     return retval;
2321 }
2322
2323 STATIC OP *
2324 S_newDEFSVOP(pTHX)
2325 {
2326     dVAR;
2327     const PADOFFSET offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0);
2328     if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2329         return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2330     }
2331     else {
2332         OP * const o = newOP(OP_PADSV, 0);
2333         o->op_targ = offset;
2334         return o;
2335     }
2336 }
2337
2338 void
2339 Perl_newPROG(pTHX_ OP *o)
2340 {
2341     dVAR;
2342
2343     PERL_ARGS_ASSERT_NEWPROG;
2344
2345     if (PL_in_eval) {
2346         if (PL_eval_root)
2347                 return;
2348         PL_eval_root = newUNOP(OP_LEAVEEVAL,
2349                                ((PL_in_eval & EVAL_KEEPERR)
2350                                 ? OPf_SPECIAL : 0), o);
2351         PL_eval_start = linklist(PL_eval_root);
2352         PL_eval_root->op_private |= OPpREFCOUNTED;
2353         OpREFCNT_set(PL_eval_root, 1);
2354         PL_eval_root->op_next = 0;
2355         CALL_PEEP(PL_eval_start);
2356     }
2357     else {
2358         if (o->op_type == OP_STUB) {
2359             PL_comppad_name = 0;
2360             PL_compcv = 0;
2361             S_op_destroy(aTHX_ o);
2362             return;
2363         }
2364         PL_main_root = scope(sawparens(scalarvoid(o)));
2365         PL_curcop = &PL_compiling;
2366         PL_main_start = LINKLIST(PL_main_root);
2367         PL_main_root->op_private |= OPpREFCOUNTED;
2368         OpREFCNT_set(PL_main_root, 1);
2369         PL_main_root->op_next = 0;
2370         CALL_PEEP(PL_main_start);
2371         PL_compcv = 0;
2372
2373         /* Register with debugger */
2374         if (PERLDB_INTER) {
2375             CV * const cv = get_cvs("DB::postponed", 0);
2376             if (cv) {
2377                 dSP;
2378                 PUSHMARK(SP);
2379                 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2380                 PUTBACK;
2381                 call_sv(MUTABLE_SV(cv), G_DISCARD);
2382             }
2383         }
2384     }
2385 }
2386
2387 OP *
2388 Perl_localize(pTHX_ OP *o, I32 lex)
2389 {
2390     dVAR;
2391
2392     PERL_ARGS_ASSERT_LOCALIZE;
2393
2394     if (o->op_flags & OPf_PARENS)
2395 /* [perl #17376]: this appears to be premature, and results in code such as
2396    C< our(%x); > executing in list mode rather than void mode */
2397 #if 0
2398         list(o);
2399 #else
2400         NOOP;
2401 #endif
2402     else {
2403         if ( PL_parser->bufptr > PL_parser->oldbufptr
2404             && PL_parser->bufptr[-1] == ','
2405             && ckWARN(WARN_PARENTHESIS))
2406         {
2407             char *s = PL_parser->bufptr;
2408             bool sigil = FALSE;
2409
2410             /* some heuristics to detect a potential error */
2411             while (*s && (strchr(", \t\n", *s)))
2412                 s++;
2413
2414             while (1) {
2415                 if (*s && strchr("@$%*", *s) && *++s
2416                        && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2417                     s++;
2418                     sigil = TRUE;
2419                     while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2420                         s++;
2421                     while (*s && (strchr(", \t\n", *s)))
2422                         s++;
2423                 }
2424                 else
2425                     break;
2426             }
2427             if (sigil && (*s == ';' || *s == '=')) {
2428                 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2429                                 "Parentheses missing around \"%s\" list",
2430                                 lex
2431                                     ? (PL_parser->in_my == KEY_our
2432                                         ? "our"
2433                                         : PL_parser->in_my == KEY_state
2434                                             ? "state"
2435                                             : "my")
2436                                     : "local");
2437             }
2438         }
2439     }
2440     if (lex)
2441         o = my(o);
2442     else
2443         o = mod(o, OP_NULL);            /* a bit kludgey */
2444     PL_parser->in_my = FALSE;
2445     PL_parser->in_my_stash = NULL;
2446     return o;
2447 }
2448
2449 OP *
2450 Perl_jmaybe(pTHX_ OP *o)
2451 {
2452     PERL_ARGS_ASSERT_JMAYBE;
2453
2454     if (o->op_type == OP_LIST) {
2455         OP * const o2
2456             = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2457         o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2458     }
2459     return o;
2460 }
2461
2462 static OP *
2463 S_fold_constants(pTHX_ register OP *o)
2464 {
2465     dVAR;
2466     register OP * VOL curop;
2467     OP *newop;
2468     VOL I32 type = o->op_type;
2469     SV * VOL sv = NULL;
2470     int ret = 0;
2471     I32 oldscope;
2472     OP *old_next;
2473     SV * const oldwarnhook = PL_warnhook;
2474     SV * const olddiehook  = PL_diehook;
2475     COP not_compiling;
2476     dJMPENV;
2477
2478     PERL_ARGS_ASSERT_FOLD_CONSTANTS;
2479
2480     if (PL_opargs[type] & OA_RETSCALAR)
2481         scalar(o);
2482     if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2483         o->op_targ = pad_alloc(type, SVs_PADTMP);
2484
2485     /* integerize op, unless it happens to be C<-foo>.
2486      * XXX should pp_i_negate() do magic string negation instead? */
2487     if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2488         && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2489              && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2490     {
2491         o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2492     }
2493
2494     if (!(PL_opargs[type] & OA_FOLDCONST))
2495         goto nope;
2496
2497     switch (type) {
2498     case OP_NEGATE:
2499         /* XXX might want a ck_negate() for this */
2500         cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2501         break;
2502     case OP_UCFIRST:
2503     case OP_LCFIRST:
2504     case OP_UC:
2505     case OP_LC:
2506     case OP_SLT:
2507     case OP_SGT:
2508     case OP_SLE:
2509     case OP_SGE:
2510     case OP_SCMP:
2511         /* XXX what about the numeric ops? */
2512         if (PL_hints & HINT_LOCALE)
2513             goto nope;
2514         break;
2515     }
2516
2517     if (PL_parser && PL_parser->error_count)
2518         goto nope;              /* Don't try to run w/ errors */
2519
2520     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2521         const OPCODE type = curop->op_type;
2522         if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2523             type != OP_LIST &&
2524             type != OP_SCALAR &&
2525             type != OP_NULL &&
2526             type != OP_PUSHMARK)
2527         {
2528             goto nope;
2529         }
2530     }
2531
2532     curop = LINKLIST(o);
2533     old_next = o->op_next;
2534     o->op_next = 0;
2535     PL_op = curop;
2536
2537     oldscope = PL_scopestack_ix;
2538     create_eval_scope(G_FAKINGEVAL);
2539
2540     /* Verify that we don't need to save it:  */
2541     assert(PL_curcop == &PL_compiling);
2542     StructCopy(&PL_compiling, &not_compiling, COP);
2543     PL_curcop = &not_compiling;
2544     /* The above ensures that we run with all the correct hints of the
2545        currently compiling COP, but that IN_PERL_RUNTIME is not true. */
2546     assert(IN_PERL_RUNTIME);
2547     PL_warnhook = PERL_WARNHOOK_FATAL;
2548     PL_diehook  = NULL;
2549     JMPENV_PUSH(ret);
2550
2551     switch (ret) {
2552     case 0:
2553         CALLRUNOPS(aTHX);
2554         sv = *(PL_stack_sp--);
2555         if (o->op_targ && sv == PAD_SV(o->op_targ))     /* grab pad temp? */
2556             pad_swipe(o->op_targ,  FALSE);
2557         else if (SvTEMP(sv)) {                  /* grab mortal temp? */
2558             SvREFCNT_inc_simple_void(sv);
2559             SvTEMP_off(sv);
2560         }
2561         break;
2562     case 3:
2563         /* Something tried to die.  Abandon constant folding.  */
2564         /* Pretend the error never happened.  */
2565         CLEAR_ERRSV();
2566         o->op_next = old_next;
2567         break;
2568     default:
2569         JMPENV_POP;
2570         /* Don't expect 1 (setjmp failed) or 2 (something called my_exit)  */
2571         PL_warnhook = oldwarnhook;
2572         PL_diehook  = olddiehook;
2573         /* XXX note that this croak may fail as we've already blown away
2574          * the stack - eg any nested evals */
2575         Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
2576     }
2577     JMPENV_POP;
2578     PL_warnhook = oldwarnhook;
2579     PL_diehook  = olddiehook;
2580     PL_curcop = &PL_compiling;
2581
2582     if (PL_scopestack_ix > oldscope)
2583         delete_eval_scope();
2584
2585     if (ret)
2586         goto nope;
2587
2588 #ifndef PERL_MAD
2589     op_free(o);
2590 #endif
2591     assert(sv);
2592     if (type == OP_RV2GV)
2593         newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
2594     else
2595         newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
2596     op_getmad(o,newop,'f');
2597     return newop;
2598
2599  nope:
2600     return o;
2601 }
2602
2603 static OP *
2604 S_gen_constant_list(pTHX_ register OP *o)
2605 {
2606     dVAR;
2607     register OP *curop;
2608     const I32 oldtmps_floor = PL_tmps_floor;
2609
2610     list(o);
2611     if (PL_parser && PL_parser->error_count)
2612         return o;               /* Don't attempt to run with errors */
2613
2614     PL_op = curop = LINKLIST(o);
2615     o->op_next = 0;
2616     CALL_PEEP(curop);
2617     pp_pushmark();
2618     CALLRUNOPS(aTHX);
2619     PL_op = curop;
2620     assert (!(curop->op_flags & OPf_SPECIAL));
2621     assert(curop->op_type == OP_RANGE);
2622     pp_anonlist();
2623     PL_tmps_floor = oldtmps_floor;
2624
2625     o->op_type = OP_RV2AV;
2626     o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2627     o->op_flags &= ~OPf_REF;    /* treat \(1..2) like an ordinary list */
2628     o->op_flags |= OPf_PARENS;  /* and flatten \(1..2,3) */
2629     o->op_opt = 0;              /* needs to be revisited in peep() */
2630     curop = ((UNOP*)o)->op_first;
2631     ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
2632 #ifdef PERL_MAD
2633     op_getmad(curop,o,'O');
2634 #else
2635     op_free(curop);
2636 #endif
2637     linklist(o);
2638     return list(o);
2639 }
2640
2641 OP *
2642 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2643 {
2644     dVAR;
2645     if (!o || o->op_type != OP_LIST)
2646         o = newLISTOP(OP_LIST, 0, o, NULL);
2647     else
2648         o->op_flags &= ~OPf_WANT;
2649
2650     if (!(PL_opargs[type] & OA_MARK))
2651         op_null(cLISTOPo->op_first);
2652
2653     o->op_type = (OPCODE)type;
2654     o->op_ppaddr = PL_ppaddr[type];
2655     o->op_flags |= flags;
2656
2657     o = CHECKOP(type, o);
2658     if (o->op_type != (unsigned)type)
2659         return o;
2660
2661     return fold_constants(o);
2662 }
2663
2664 /* List constructors */
2665
2666 OP *
2667 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2668 {
2669     if (!first)
2670         return last;
2671
2672     if (!last)
2673         return first;
2674
2675     if (first->op_type != (unsigned)type
2676         || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2677     {
2678         return newLISTOP(type, 0, first, last);
2679     }
2680
2681     if (first->op_flags & OPf_KIDS)
2682         ((LISTOP*)first)->op_last->op_sibling = last;
2683     else {
2684         first->op_flags |= OPf_KIDS;
2685         ((LISTOP*)first)->op_first = last;
2686     }
2687     ((LISTOP*)first)->op_last = last;
2688     return first;
2689 }
2690
2691 OP *
2692 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2693 {
2694     if (!first)
2695         return (OP*)last;
2696
2697     if (!last)
2698         return (OP*)first;
2699
2700     if (first->op_type != (unsigned)type)
2701         return prepend_elem(type, (OP*)first, (OP*)last);
2702
2703     if (last->op_type != (unsigned)type)
2704         return append_elem(type, (OP*)first, (OP*)last);
2705
2706     first->op_last->op_sibling = last->op_first;
2707     first->op_last = last->op_last;
2708     first->op_flags |= (last->op_flags & OPf_KIDS);
2709
2710 #ifdef PERL_MAD
2711     if (last->op_first && first->op_madprop) {
2712         MADPROP *mp = last->op_first->op_madprop;
2713         if (mp) {
2714             while (mp->mad_next)
2715                 mp = mp->mad_next;
2716             mp->mad_next = first->op_madprop;
2717         }
2718         else {
2719             last->op_first->op_madprop = first->op_madprop;
2720         }
2721     }
2722     first->op_madprop = last->op_madprop;
2723     last->op_madprop = 0;
2724 #endif
2725
2726     S_op_destroy(aTHX_ (OP*)last);
2727
2728     return (OP*)first;
2729 }
2730
2731 OP *
2732 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2733 {
2734     if (!first)
2735         return last;
2736
2737     if (!last)
2738         return first;
2739
2740     if (last->op_type == (unsigned)type) {
2741         if (type == OP_LIST) {  /* already a PUSHMARK there */
2742             first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2743             ((LISTOP*)last)->op_first->op_sibling = first;
2744             if (!(first->op_flags & OPf_PARENS))
2745                 last->op_flags &= ~OPf_PARENS;
2746         }
2747         else {
2748             if (!(last->op_flags & OPf_KIDS)) {
2749                 ((LISTOP*)last)->op_last = first;
2750                 last->op_flags |= OPf_KIDS;
2751             }
2752             first->op_sibling = ((LISTOP*)last)->op_first;
2753             ((LISTOP*)last)->op_first = first;
2754         }
2755         last->op_flags |= OPf_KIDS;
2756         return last;
2757     }
2758
2759     return newLISTOP(type, 0, first, last);
2760 }
2761
2762 /* Constructors */
2763
2764 #ifdef PERL_MAD
2765  
2766 TOKEN *
2767 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
2768 {
2769     TOKEN *tk;
2770     Newxz(tk, 1, TOKEN);
2771     tk->tk_type = (OPCODE)optype;
2772     tk->tk_type = 12345;
2773     tk->tk_lval = lval;
2774     tk->tk_mad = madprop;
2775     return tk;
2776 }
2777
2778 void
2779 Perl_token_free(pTHX_ TOKEN* tk)
2780 {
2781     PERL_ARGS_ASSERT_TOKEN_FREE;
2782
2783     if (tk->tk_type != 12345)
2784         return;
2785     mad_free(tk->tk_mad);
2786     Safefree(tk);
2787 }
2788
2789 void
2790 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
2791 {
2792     MADPROP* mp;
2793     MADPROP* tm;
2794
2795     PERL_ARGS_ASSERT_TOKEN_GETMAD;
2796
2797     if (tk->tk_type != 12345) {
2798         Perl_warner(aTHX_ packWARN(WARN_MISC),
2799              "Invalid TOKEN object ignored");
2800         return;
2801     }
2802     tm = tk->tk_mad;
2803     if (!tm)
2804         return;
2805
2806     /* faked up qw list? */
2807     if (slot == '(' &&
2808         tm->mad_type == MAD_SV &&
2809         SvPVX((SV *)tm->mad_val)[0] == 'q')
2810             slot = 'x';
2811
2812     if (o) {
2813         mp = o->op_madprop;
2814         if (mp) {
2815             for (;;) {
2816                 /* pretend constant fold didn't happen? */
2817                 if (mp->mad_key == 'f' &&
2818                     (o->op_type == OP_CONST ||
2819                      o->op_type == OP_GV) )
2820                 {
2821                     token_getmad(tk,(OP*)mp->mad_val,slot);
2822                     return;
2823                 }
2824                 if (!mp->mad_next)
2825                     break;
2826                 mp = mp->mad_next;
2827             }
2828             mp->mad_next = tm;
2829             mp = mp->mad_next;
2830         }
2831         else {
2832             o->op_madprop = tm;
2833             mp = o->op_madprop;
2834         }
2835         if (mp->mad_key == 'X')
2836             mp->mad_key = slot; /* just change the first one */
2837
2838         tk->tk_mad = 0;
2839     }
2840     else
2841         mad_free(tm);
2842     Safefree(tk);
2843 }
2844
2845 void
2846 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
2847 {
2848     MADPROP* mp;
2849     if (!from)
2850         return;
2851     if (o) {
2852         mp = o->op_madprop;
2853         if (mp) {
2854             for (;;) {
2855                 /* pretend constant fold didn't happen? */
2856                 if (mp->mad_key == 'f' &&
2857                     (o->op_type == OP_CONST ||
2858                      o->op_type == OP_GV) )
2859                 {
2860                     op_getmad(from,(OP*)mp->mad_val,slot);
2861                     return;
2862                 }
2863                 if (!mp->mad_next)
2864                     break;
2865                 mp = mp->mad_next;
2866             }
2867             mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
2868         }
2869         else {
2870             o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
2871         }
2872     }
2873 }
2874
2875 void
2876 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
2877 {
2878     MADPROP* mp;
2879     if (!from)
2880         return;
2881     if (o) {
2882         mp = o->op_madprop;
2883         if (mp) {
2884             for (;;) {
2885                 /* pretend constant fold didn't happen? */
2886                 if (mp->mad_key == 'f' &&
2887                     (o->op_type == OP_CONST ||
2888                      o->op_type == OP_GV) )
2889                 {
2890                     op_getmad(from,(OP*)mp->mad_val,slot);
2891                     return;
2892                 }
2893                 if (!mp->mad_next)
2894                     break;
2895                 mp = mp->mad_next;
2896             }
2897             mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
2898         }
2899         else {
2900             o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
2901         }
2902     }
2903     else {
2904         PerlIO_printf(PerlIO_stderr(),
2905                       "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
2906         op_free(from);
2907     }
2908 }
2909
2910 void
2911 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
2912 {
2913     MADPROP* tm;
2914     if (!mp || !o)
2915         return;
2916     if (slot)
2917         mp->mad_key = slot;
2918     tm = o->op_madprop;
2919     o->op_madprop = mp;
2920     for (;;) {
2921         if (!mp->mad_next)
2922             break;
2923         mp = mp->mad_next;
2924     }
2925     mp->mad_next = tm;
2926 }
2927
2928 void
2929 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
2930 {
2931     if (!o)
2932         return;
2933     addmad(tm, &(o->op_madprop), slot);
2934 }
2935
2936 void
2937 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
2938 {
2939     MADPROP* mp;
2940     if (!tm || !root)
2941         return;
2942     if (slot)
2943         tm->mad_key = slot;
2944     mp = *root;
2945     if (!mp) {
2946         *root = tm;
2947         return;
2948     }
2949     for (;;) {
2950         if (!mp->mad_next)
2951             break;
2952         mp = mp->mad_next;
2953     }
2954     mp->mad_next = tm;
2955 }
2956
2957 MADPROP *
2958 Perl_newMADsv(pTHX_ char key, SV* sv)
2959 {
2960     PERL_ARGS_ASSERT_NEWMADSV;
2961
2962     return newMADPROP(key, MAD_SV, sv, 0);
2963 }
2964
2965 MADPROP *
2966 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
2967 {
2968     MADPROP *mp;
2969     Newxz(mp, 1, MADPROP);
2970     mp->mad_next = 0;
2971     mp->mad_key = key;
2972     mp->mad_vlen = vlen;
2973     mp->mad_type = type;
2974     mp->mad_val = val;
2975 /*    PerlIO_printf(PerlIO_stderr(), "NEW  mp = %0x\n", mp);  */
2976     return mp;
2977 }
2978
2979 void
2980 Perl_mad_free(pTHX_ MADPROP* mp)
2981 {
2982 /*    PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
2983     if (!mp)
2984         return;
2985     if (mp->mad_next)
2986         mad_free(mp->mad_next);
2987 /*    if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
2988         PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
2989     switch (mp->mad_type) {
2990     case MAD_NULL:
2991         break;
2992     case MAD_PV:
2993         Safefree((char*)mp->mad_val);
2994         break;
2995     case MAD_OP:
2996         if (mp->mad_vlen)       /* vlen holds "strong/weak" boolean */
2997             op_free((OP*)mp->mad_val);
2998         break;
2999     case MAD_SV:
3000         sv_free(MUTABLE_SV(mp->mad_val));
3001         break;
3002     default:
3003         PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3004         break;
3005     }
3006     Safefree(mp);
3007 }
3008
3009 #endif
3010
3011 OP *
3012 Perl_newNULLLIST(pTHX)
3013 {
3014     return newOP(OP_STUB, 0);
3015 }
3016
3017 static OP *
3018 S_force_list(pTHX_ OP *o)
3019 {
3020     if (!o || o->op_type != OP_LIST)
3021         o = newLISTOP(OP_LIST, 0, o, NULL);
3022     op_null(o);
3023     return o;
3024 }
3025
3026 OP *
3027 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3028 {
3029     dVAR;
3030     LISTOP *listop;
3031
3032     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3033
3034     NewOp(1101, listop, 1, LISTOP);
3035
3036     listop->op_type = (OPCODE)type;
3037     listop->op_ppaddr = PL_ppaddr[type];
3038     if (first || last)
3039         flags |= OPf_KIDS;
3040     listop->op_flags = (U8)flags;
3041
3042     if (!last && first)
3043         last = first;
3044     else if (!first && last)
3045         first = last;
3046     else if (first)
3047         first->op_sibling = last;
3048     listop->op_first = first;
3049     listop->op_last = last;
3050     if (type == OP_LIST) {
3051         OP* const pushop = newOP(OP_PUSHMARK, 0);
3052         pushop->op_sibling = first;
3053         listop->op_first = pushop;
3054         listop->op_flags |= OPf_KIDS;
3055         if (!last)
3056             listop->op_last = pushop;
3057     }
3058
3059     return CHECKOP(type, listop);
3060 }
3061
3062 OP *
3063 Perl_newOP(pTHX_ I32 type, I32 flags)
3064 {
3065     dVAR;
3066     OP *o;
3067
3068     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3069         || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3070         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3071         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3072
3073     NewOp(1101, o, 1, OP);
3074     o->op_type = (OPCODE)type;
3075     o->op_ppaddr = PL_ppaddr[type];
3076     o->op_flags = (U8)flags;
3077     o->op_latefree = 0;
3078     o->op_latefreed = 0;
3079     o->op_attached = 0;
3080
3081     o->op_next = o;
3082     o->op_private = (U8)(0 | (flags >> 8));
3083     if (PL_opargs[type] & OA_RETSCALAR)
3084         scalar(o);
3085     if (PL_opargs[type] & OA_TARGET)
3086         o->op_targ = pad_alloc(type, SVs_PADTMP);
3087     return CHECKOP(type, o);
3088 }
3089
3090 OP *
3091 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3092 {
3093     dVAR;
3094     UNOP *unop;
3095
3096     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3097         || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3098         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3099         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3100         || type == OP_SASSIGN
3101         || type == OP_ENTERTRY
3102         || type == OP_NULL );
3103
3104     if (!first)
3105         first = newOP(OP_STUB, 0);
3106     if (PL_opargs[type] & OA_MARK)
3107         first = force_list(first);
3108
3109     NewOp(1101, unop, 1, UNOP);
3110     unop->op_type = (OPCODE)type;
3111     unop->op_ppaddr = PL_ppaddr[type];
3112     unop->op_first = first;
3113     unop->op_flags = (U8)(flags | OPf_KIDS);
3114     unop->op_private = (U8)(1 | (flags >> 8));
3115     unop = (UNOP*) CHECKOP(type, unop);
3116     if (unop->op_next)
3117         return (OP*)unop;
3118
3119     return fold_constants((OP *) unop);
3120 }
3121
3122 OP *
3123 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3124 {
3125     dVAR;
3126     BINOP *binop;
3127
3128     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3129         || type == OP_SASSIGN || type == OP_NULL );
3130
3131     NewOp(1101, binop, 1, BINOP);
3132
3133     if (!first)
3134         first = newOP(OP_NULL, 0);
3135
3136     binop->op_type = (OPCODE)type;
3137     binop->op_ppaddr = PL_ppaddr[type];
3138     binop->op_first = first;
3139     binop->op_flags = (U8)(flags | OPf_KIDS);
3140     if (!last) {
3141         last = first;
3142         binop->op_private = (U8)(1 | (flags >> 8));
3143     }
3144     else {
3145         binop->op_private = (U8)(2 | (flags >> 8));
3146         first->op_sibling = last;
3147     }
3148
3149     binop = (BINOP*)CHECKOP(type, binop);
3150     if (binop->op_next || binop->op_type != (OPCODE)type)
3151         return (OP*)binop;
3152
3153     binop->op_last = binop->op_first->op_sibling;
3154
3155     return fold_constants((OP *)binop);
3156 }
3157
3158 static int uvcompare(const void *a, const void *b)
3159     __attribute__nonnull__(1)
3160     __attribute__nonnull__(2)
3161     __attribute__pure__;
3162 static int uvcompare(const void *a, const void *b)
3163 {
3164     if (*((const UV *)a) < (*(const UV *)b))
3165         return -1;
3166     if (*((const UV *)a) > (*(const UV *)b))
3167         return 1;
3168     if (*((const UV *)a+1) < (*(const UV *)b+1))
3169         return -1;
3170     if (*((const UV *)a+1) > (*(const UV *)b+1))
3171         return 1;
3172     return 0;
3173 }
3174
3175 static OP *
3176 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3177 {
3178     dVAR;
3179     SV * const tstr = ((SVOP*)expr)->op_sv;
3180     SV * const rstr =
3181 #ifdef PERL_MAD
3182                         (repl->op_type == OP_NULL)
3183                             ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3184 #endif
3185                               ((SVOP*)repl)->op_sv;
3186     STRLEN tlen;
3187     STRLEN rlen;
3188     const U8 *t = (U8*)SvPV_const(tstr, tlen);
3189     const U8 *r = (U8*)SvPV_const(rstr, rlen);
3190     register I32 i;
3191     register I32 j;
3192     I32 grows = 0;
3193     register short *tbl;
3194
3195     const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3196     const I32 squash     = o->op_private & OPpTRANS_SQUASH;
3197     I32 del              = o->op_private & OPpTRANS_DELETE;
3198     SV* swash;
3199
3200     PERL_ARGS_ASSERT_PMTRANS;
3201
3202     PL_hints |= HINT_BLOCK_SCOPE;
3203
3204     if (SvUTF8(tstr))
3205         o->op_private |= OPpTRANS_FROM_UTF;
3206
3207     if (SvUTF8(rstr))
3208         o->op_private |= OPpTRANS_TO_UTF;
3209
3210     if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3211         SV* const listsv = newSVpvs("# comment\n");
3212         SV* transv = NULL;
3213         const U8* tend = t + tlen;
3214         const U8* rend = r + rlen;
3215         STRLEN ulen;
3216         UV tfirst = 1;
3217         UV tlast = 0;
3218         IV tdiff;
3219         UV rfirst = 1;
3220         UV rlast = 0;
3221         IV rdiff;
3222         IV diff;
3223         I32 none = 0;
3224         U32 max = 0;
3225         I32 bits;
3226         I32 havefinal = 0;
3227         U32 final = 0;
3228         const I32 from_utf  = o->op_private & OPpTRANS_FROM_UTF;
3229         const I32 to_utf    = o->op_private & OPpTRANS_TO_UTF;
3230         U8* tsave = NULL;
3231         U8* rsave = NULL;
3232         const U32 flags = UTF8_ALLOW_DEFAULT;
3233
3234         if (!from_utf) {
3235             STRLEN len = tlen;
3236             t = tsave = bytes_to_utf8(t, &len);
3237             tend = t + len;
3238         }
3239         if (!to_utf && rlen) {
3240             STRLEN len = rlen;
3241             r = rsave = bytes_to_utf8(r, &len);
3242             rend = r + len;
3243         }
3244
3245 /* There are several snags with this code on EBCDIC:
3246    1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
3247    2. scan_const() in toke.c has encoded chars in native encoding which makes
3248       ranges at least in EBCDIC 0..255 range the bottom odd.
3249 */
3250
3251         if (complement) {
3252             U8 tmpbuf[UTF8_MAXBYTES+1];
3253             UV *cp;
3254             UV nextmin = 0;
3255             Newx(cp, 2*tlen, UV);
3256             i = 0;
3257             transv = newSVpvs("");
3258             while (t < tend) {
3259                 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3260                 t += ulen;
3261                 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
3262                     t++;
3263                     cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3264                     t += ulen;
3265                 }
3266                 else {
3267                  cp[2*i+1] = cp[2*i];
3268                 }
3269                 i++;
3270             }
3271             qsort(cp, i, 2*sizeof(UV), uvcompare);
3272             for (j = 0; j < i; j++) {
3273                 UV  val = cp[2*j];
3274                 diff = val - nextmin;
3275                 if (diff > 0) {
3276                     t = uvuni_to_utf8(tmpbuf,nextmin);
3277                     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3278                     if (diff > 1) {
3279                         U8  range_mark = UTF_TO_NATIVE(0xff);
3280                         t = uvuni_to_utf8(tmpbuf, val - 1);
3281                         sv_catpvn(transv, (char *)&range_mark, 1);
3282                         sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3283                     }
3284                 }
3285                 val = cp[2*j+1];
3286                 if (val >= nextmin)
3287                     nextmin = val + 1;
3288             }
3289             t = uvuni_to_utf8(tmpbuf,nextmin);
3290             sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3291             {
3292                 U8 range_mark = UTF_TO_NATIVE(0xff);
3293                 sv_catpvn(transv, (char *)&range_mark, 1);
3294             }
3295             t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
3296                                     UNICODE_ALLOW_SUPER);
3297             sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3298             t = (const U8*)SvPVX_const(transv);
3299             tlen = SvCUR(transv);
3300             tend = t + tlen;
3301             Safefree(cp);
3302         }
3303         else if (!rlen && !del) {
3304             r = t; rlen = tlen; rend = tend;
3305         }
3306         if (!squash) {
3307                 if ((!rlen && !del) || t == r ||
3308                     (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
3309                 {
3310                     o->op_private |= OPpTRANS_IDENTICAL;
3311                 }
3312         }
3313
3314         while (t < tend || tfirst <= tlast) {
3315             /* see if we need more "t" chars */
3316             if (tfirst > tlast) {
3317                 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3318                 t += ulen;
3319                 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {    /* illegal utf8 val indicates range */
3320                     t++;
3321                     tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3322                     t += ulen;
3323                 }
3324                 else
3325                     tlast = tfirst;
3326             }
3327
3328             /* now see if we need more "r" chars */
3329             if (rfirst > rlast) {
3330                 if (r < rend) {
3331                     rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3332                     r += ulen;
3333                     if (r < rend && NATIVE_TO_UTF(*r) == 0xff) {        /* illegal utf8 val indicates range */
3334                         r++;
3335                         rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3336                         r += ulen;
3337                     }
3338                     else
3339                         rlast = rfirst;
3340                 }
3341                 else {
3342                     if (!havefinal++)
3343                         final = rlast;
3344                     rfirst = rlast = 0xffffffff;
3345                 }
3346             }
3347
3348             /* now see which range will peter our first, if either. */
3349             tdiff = tlast - tfirst;
3350             rdiff = rlast - rfirst;
3351
3352             if (tdiff <= rdiff)
3353                 diff = tdiff;
3354             else
3355                 diff = rdiff;
3356
3357             if (rfirst == 0xffffffff) {
3358                 diff = tdiff;   /* oops, pretend rdiff is infinite */
3359                 if (diff > 0)
3360                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
3361                                    (long)tfirst, (long)tlast);
3362                 else
3363                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
3364             }
3365             else {
3366                 if (diff > 0)
3367                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
3368                                    (long)tfirst, (long)(tfirst + diff),
3369                                    (long)rfirst);
3370                 else
3371                     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
3372                                    (long)tfirst, (long)rfirst);
3373
3374                 if (rfirst + diff > max)
3375                     max = rfirst + diff;
3376                 if (!grows)
3377                     grows = (tfirst < rfirst &&
3378                              UNISKIP(tfirst) < UNISKIP(rfirst + diff));
3379                 rfirst += diff + 1;
3380             }
3381             tfirst += diff + 1;
3382         }
3383
3384         none = ++max;
3385         if (del)
3386             del = ++max;
3387
3388         if (max > 0xffff)
3389             bits = 32;
3390         else if (max > 0xff)
3391             bits = 16;
3392         else
3393             bits = 8;
3394
3395         PerlMemShared_free(cPVOPo->op_pv);
3396         cPVOPo->op_pv = NULL;
3397
3398         swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
3399 #ifdef USE_ITHREADS
3400         cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
3401         SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
3402         PAD_SETSV(cPADOPo->op_padix, swash);
3403         SvPADTMP_on(swash);
3404         SvREADONLY_on(swash);
3405 #else
3406         cSVOPo->op_sv = swash;
3407 #endif
3408         SvREFCNT_dec(listsv);
3409         SvREFCNT_dec(transv);
3410
3411         if (!del && havefinal && rlen)
3412             (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
3413                            newSVuv((UV)final), 0);
3414
3415         if (grows)
3416             o->op_private |= OPpTRANS_GROWS;
3417
3418         Safefree(tsave);
3419         Safefree(rsave);
3420
3421 #ifdef PERL_MAD
3422         op_getmad(expr,o,'e');
3423         op_getmad(repl,o,'r');
3424 #else
3425         op_free(expr);
3426         op_free(repl);
3427 #endif
3428         return o;
3429     }
3430
3431     tbl = (short*)cPVOPo->op_pv;
3432     if (complement) {
3433         Zero(tbl, 256, short);
3434         for (i = 0; i < (I32)tlen; i++)
3435             tbl[t[i]] = -1;
3436         for (i = 0, j = 0; i < 256; i++) {
3437             if (!tbl[i]) {
3438                 if (j >= (I32)rlen) {
3439                     if (del)
3440                         tbl[i] = -2;
3441                     else if (rlen)
3442                         tbl[i] = r[j-1];
3443                     else
3444                         tbl[i] = (short)i;
3445                 }
3446                 else {
3447                     if (i < 128 && r[j] >= 128)
3448                         grows = 1;
3449                     tbl[i] = r[j++];
3450                 }
3451             }
3452         }
3453         if (!del) {
3454             if (!rlen) {
3455                 j = rlen;
3456                 if (!squash)
3457                     o->op_private |= OPpTRANS_IDENTICAL;
3458             }
3459             else if (j >= (I32)rlen)
3460                 j = rlen - 1;
3461             else {
3462                 tbl = 
3463                     (short *)
3464                     PerlMemShared_realloc(tbl,
3465                                           (0x101+rlen-j) * sizeof(short));
3466                 cPVOPo->op_pv = (char*)tbl;
3467             }
3468             tbl[0x100] = (short)(rlen - j);
3469             for (i=0; i < (I32)rlen - j; i++)
3470                 tbl[0x101+i] = r[j+i];
3471         }
3472     }
3473     else {
3474         if (!rlen && !del) {
3475             r = t; rlen = tlen;
3476             if (!squash)
3477                 o->op_private |= OPpTRANS_IDENTICAL;
3478         }
3479         else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
3480             o->op_private |= OPpTRANS_IDENTICAL;
3481         }
3482         for (i = 0; i < 256; i++)
3483             tbl[i] = -1;
3484         for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
3485             if (j >= (I32)rlen) {
3486                 if (del) {
3487                     if (tbl[t[i]] == -1)
3488                         tbl[t[i]] = -2;
3489                     continue;
3490                 }
3491                 --j;
3492             }
3493             if (tbl[t[i]] == -1) {
3494                 if (t[i] < 128 && r[j] >= 128)
3495                     grows = 1;
3496                 tbl[t[i]] = r[j];
3497             }
3498         }
3499     }
3500
3501     if(del && rlen == tlen) {
3502         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); 
3503     } else if(rlen > tlen) {
3504         Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
3505     }
3506
3507     if (grows)
3508         o->op_private |= OPpTRANS_GROWS;
3509 #ifdef PERL_MAD
3510     op_getmad(expr,o,'e');
3511     op_getmad(repl,o,'r');
3512 #else
3513     op_free(expr);
3514     op_free(repl);
3515 #endif
3516
3517     return o;
3518 }
3519
3520 OP *
3521 Perl_newPMOP(pTHX_ I32 type, I32 flags)
3522 {
3523     dVAR;
3524     PMOP *pmop;
3525
3526     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
3527
3528     NewOp(1101, pmop, 1, PMOP);
3529     pmop->op_type = (OPCODE)type;
3530     pmop->op_ppaddr = PL_ppaddr[type];
3531     pmop->op_flags = (U8)flags;
3532     pmop->op_private = (U8)(0 | (flags >> 8));
3533
3534     if (PL_hints & HINT_RE_TAINT)
3535         pmop->op_pmflags |= PMf_RETAINT;
3536     if (PL_hints & HINT_LOCALE)
3537         pmop->op_pmflags |= PMf_LOCALE;
3538
3539
3540 #ifdef USE_ITHREADS
3541     assert(SvPOK(PL_regex_pad[0]));
3542     if (SvCUR(PL_regex_pad[0])) {
3543         /* Pop off the "packed" IV from the end.  */
3544         SV *const repointer_list = PL_regex_pad[0];
3545         const char *p = SvEND(repointer_list) - sizeof(IV);
3546         const IV offset = *((IV*)p);
3547
3548         assert(SvCUR(repointer_list) % sizeof(IV) == 0);
3549
3550         SvEND_set(repointer_list, p);
3551
3552         pmop->op_pmoffset = offset;
3553         /* This slot should be free, so assert this:  */
3554         assert(PL_regex_pad[offset] == &PL_sv_undef);
3555     } else {
3556         SV * const repointer = &PL_sv_undef;
3557         av_push(PL_regex_padav, repointer);
3558         pmop->op_pmoffset = av_len(PL_regex_padav);
3559         PL_regex_pad = AvARRAY(PL_regex_padav);
3560     }
3561 #endif
3562
3563     return CHECKOP(type, pmop);
3564 }
3565
3566 /* Given some sort of match op o, and an expression expr containing a
3567  * pattern, either compile expr into a regex and attach it to o (if it's
3568  * constant), or convert expr into a runtime regcomp op sequence (if it's
3569  * not)
3570  *
3571  * isreg indicates that the pattern is part of a regex construct, eg
3572  * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
3573  * split "pattern", which aren't. In the former case, expr will be a list
3574  * if the pattern contains more than one term (eg /a$b/) or if it contains
3575  * a replacement, ie s/// or tr///.
3576  */
3577
3578 OP *
3579 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
3580 {
3581     dVAR;
3582     PMOP *pm;
3583     LOGOP *rcop;
3584     I32 repl_has_vars = 0;
3585     OP* repl = NULL;
3586     bool reglist;
3587
3588     PERL_ARGS_ASSERT_PMRUNTIME;
3589
3590     if (o->op_type == OP_SUBST || o->op_type == OP_TRANS) {
3591         /* last element in list is the replacement; pop it */
3592         OP* kid;
3593         repl = cLISTOPx(expr)->op_last;
3594         kid = cLISTOPx(expr)->op_first;
3595         while (kid->op_sibling != repl)
3596             kid = kid->op_sibling;
3597         kid->op_sibling = NULL;
3598         cLISTOPx(expr)->op_last = kid;
3599     }
3600
3601     if (isreg && expr->op_type == OP_LIST &&
3602         cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last)
3603     {
3604         /* convert single element list to element */
3605         OP* const oe = expr;
3606         expr = cLISTOPx(oe)->op_first->op_sibling;
3607         cLISTOPx(oe)->op_first->op_sibling = NULL;
3608         cLISTOPx(oe)->op_last = NULL;
3609         op_free(oe);
3610     }
3611
3612     if (o->op_type == OP_TRANS) {
3613         return pmtrans(o, expr, repl);
3614     }
3615
3616     reglist = isreg && expr->op_type == OP_LIST;
3617     if (reglist)
3618         op_null(expr);
3619
3620     PL_hints |= HINT_BLOCK_SCOPE;
3621     pm = (PMOP*)o;
3622
3623     if (expr->op_type == OP_CONST) {
3624         SV *pat = ((SVOP*)expr)->op_sv;
3625         U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
3626
3627         if (o->op_flags & OPf_SPECIAL)
3628             pm_flags |= RXf_SPLIT;
3629
3630         if (DO_UTF8(pat)) {
3631             assert (SvUTF8(pat));
3632         } else if (SvUTF8(pat)) {
3633             /* Not doing UTF-8, despite what the SV says. Is this only if we're
3634                trapped in use 'bytes'?  */
3635             /* Make a copy of the octet sequence, but without the flag on, as
3636                the compiler now honours the SvUTF8 flag on pat.  */
3637             STRLEN len;
3638             const char *const p = SvPV(pat, len);
3639             pat = newSVpvn_flags(p, len, SVs_TEMP);
3640         }
3641
3642         PM_SETRE(pm, CALLREGCOMP(pat, pm_flags));
3643
3644 #ifdef PERL_MAD
3645         op_getmad(expr,(OP*)pm,'e');
3646 #else
3647         op_free(expr);
3648 #endif
3649     }
3650     else {
3651         if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
3652             expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
3653                             ? OP_REGCRESET
3654                             : OP_REGCMAYBE),0,expr);
3655
3656         NewOp(1101, rcop, 1, LOGOP);
3657         rcop->op_type = OP_REGCOMP;
3658         rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
3659         rcop->op_first = scalar(expr);
3660         rcop->op_flags |= OPf_KIDS
3661                             | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
3662                             | (reglist ? OPf_STACKED : 0);
3663         rcop->op_private = 1;
3664         rcop->op_other = o;
3665         if (reglist)
3666             rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
3667
3668         /* /$x/ may cause an eval, since $x might be qr/(?{..})/  */
3669         PL_cv_has_eval = 1;
3670
3671         /* establish postfix order */
3672         if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
3673             LINKLIST(expr);
3674             rcop->op_next = expr;
3675             ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
3676         }
3677         else {
3678             rcop->op_next = LINKLIST(expr);
3679             expr->op_next = (OP*)rcop;
3680         }
3681
3682         prepend_elem(o->op_type, scalar((OP*)rcop), o);
3683     }
3684
3685     if (repl) {
3686         OP *curop;
3687         if (pm->op_pmflags & PMf_EVAL) {
3688             curop = NULL;
3689             if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
3690                 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
3691         }
3692         else if (repl->op_type == OP_CONST)
3693             curop = repl;
3694         else {
3695             OP *lastop = NULL;
3696             for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
3697                 if (curop->op_type == OP_SCOPE
3698                         || curop->op_type == OP_LEAVE
3699                         || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
3700                     if (curop->op_type == OP_GV) {
3701                         GV * const gv = cGVOPx_gv(curop);
3702                         repl_has_vars = 1;
3703                         if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
3704                             break;
3705                     }
3706                     else if (curop->op_type == OP_RV2CV)
3707                         break;
3708                     else if (curop->op_type == OP_RV2SV ||
3709                              curop->op_type == OP_RV2AV ||
3710                              curop->op_type == OP_RV2HV ||
3711                              curop->op_type == OP_RV2GV) {
3712                         if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3713                             break;
3714                     }
3715                     else if (curop->op_type == OP_PADSV ||
3716                              curop->op_type == OP_PADAV ||
3717                              curop->op_type == OP_PADHV ||
3718                              curop->op_type == OP_PADANY)
3719                     {
3720                         repl_has_vars = 1;
3721                     }
3722                     else if (curop->op_type == OP_PUSHRE)
3723                         NOOP; /* Okay here, dangerous in newASSIGNOP */
3724                     else
3725                         break;
3726                 }
3727                 lastop = curop;
3728             }
3729         }
3730         if (curop == repl
3731             && !(repl_has_vars
3732                  && (!PM_GETRE(pm)
3733                      || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
3734         {
3735             pm->op_pmflags |= PMf_CONST;        /* const for long enough */
3736             prepend_elem(o->op_type, scalar(repl), o);
3737         }
3738         else {
3739             if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
3740                 pm->op_pmflags |= PMf_MAYBE_CONST;
3741             }
3742             NewOp(1101, rcop, 1, LOGOP);
3743             rcop->op_type = OP_SUBSTCONT;
3744             rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3745             rcop->op_first = scalar(repl);
3746             rcop->op_flags |= OPf_KIDS;
3747             rcop->op_private = 1;
3748             rcop->op_other = o;
3749
3750             /* establish postfix order */
3751             rcop->op_next = LINKLIST(repl);
3752             repl->op_next = (OP*)rcop;
3753
3754             pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
3755             assert(!(pm->op_pmflags & PMf_ONCE));
3756             pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
3757             rcop->op_next = 0;
3758         }
3759     }
3760
3761     return (OP*)pm;
3762 }
3763
3764 OP *
3765 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3766 {
3767     dVAR;
3768     SVOP *svop;
3769
3770     PERL_ARGS_ASSERT_NEWSVOP;
3771
3772     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
3773         || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3774         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
3775
3776     NewOp(1101, svop, 1, SVOP);
3777     svop->op_type = (OPCODE)type;
3778     svop->op_ppaddr = PL_ppaddr[type];
3779     svop->op_sv = sv;
3780     svop->op_next = (OP*)svop;
3781     svop->op_flags = (U8)flags;
3782     if (PL_opargs[type] & OA_RETSCALAR)
3783         scalar((OP*)svop);
3784     if (PL_opargs[type] & OA_TARGET)
3785         svop->op_targ = pad_alloc(type, SVs_PADTMP);
3786     return CHECKOP(type, svop);
3787 }
3788
3789 #ifdef USE_ITHREADS
3790 OP *
3791 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3792 {
3793     dVAR;
3794     PADOP *padop;
3795
3796     PERL_ARGS_ASSERT_NEWPADOP;
3797
3798     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
3799         || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3800         || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
3801
3802     NewOp(1101, padop, 1, PADOP);
3803     padop->op_type = (OPCODE)type;
3804     padop->op_ppaddr = PL_ppaddr[type];
3805     padop->op_padix = pad_alloc(type, SVs_PADTMP);
3806     SvREFCNT_dec(PAD_SVl(padop->op_padix));
3807     PAD_SETSV(padop->op_padix, sv);
3808     assert(sv);
3809     SvPADTMP_on(sv);
3810     padop->op_next = (OP*)padop;
3811     padop->op_flags = (U8)flags;
3812     if (PL_opargs[type] & OA_RETSCALAR)
3813         scalar((OP*)padop);
3814     if (PL_opargs[type] & OA_TARGET)
3815         padop->op_targ = pad_alloc(type, SVs_PADTMP);
3816     return CHECKOP(type, padop);
3817 }
3818 #endif
3819
3820 OP *
3821 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3822 {
3823     dVAR;
3824
3825     PERL_ARGS_ASSERT_NEWGVOP;
3826
3827 #ifdef USE_ITHREADS
3828     GvIN_PAD_on(gv);
3829     return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
3830 #else
3831     return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
3832 #endif
3833 }
3834
3835 OP *
3836 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3837 {
3838     dVAR;
3839     PVOP *pvop;
3840
3841     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3842         || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3843
3844     NewOp(1101, pvop, 1, PVOP);
3845     pvop->op_type = (OPCODE)type;
3846     pvop->op_ppaddr = PL_ppaddr[type];
3847     pvop->op_pv = pv;
3848     pvop->op_next = (OP*)pvop;
3849     pvop->op_flags = (U8)flags;
3850     if (PL_opargs[type] & OA_RETSCALAR)
3851         scalar((OP*)pvop);
3852     if (PL_opargs[type] & OA_TARGET)
3853         pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3854     return CHECKOP(type, pvop);
3855 }
3856
3857 #ifdef PERL_MAD
3858 OP*
3859 #else
3860 void
3861 #endif
3862 Perl_package(pTHX_ OP *o)
3863 {
3864     dVAR;
3865     SV *const sv = cSVOPo->op_sv;
3866 #ifdef PERL_MAD
3867     OP *pegop;
3868 #endif
3869
3870     PERL_ARGS_ASSERT_PACKAGE;
3871
3872     save_hptr(&PL_curstash);
3873     save_item(PL_curstname);
3874
3875     PL_curstash = gv_stashsv(sv, GV_ADD);
3876
3877     sv_setsv(PL_curstname, sv);
3878
3879     PL_hints |= HINT_BLOCK_SCOPE;
3880     PL_parser->copline = NOLINE;
3881     PL_parser->expect = XSTATE;
3882
3883 #ifndef PERL_MAD
3884     op_free(o);
3885 #else
3886     if (!PL_madskills) {
3887         op_free(o);
3888         return NULL;
3889     }
3890
3891     pegop = newOP(OP_NULL,0);
3892     op_getmad(o,pegop,'P');
3893     return pegop;
3894 #endif
3895 }
3896
3897 void
3898 Perl_package_version( pTHX_ OP *v )
3899 {
3900     dVAR;
3901     U32 savehints = PL_hints;
3902     PERL_ARGS_ASSERT_PACKAGE_VERSION;
3903     PL_hints &= ~HINT_STRICT_VARS;
3904     sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
3905     PL_hints = savehints;
3906     op_free(v);
3907 }
3908
3909 #ifdef PERL_MAD
3910 OP*
3911 #else
3912 void
3913 #endif
3914 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3915 {
3916     dVAR;
3917     OP *pack;
3918     OP *imop;
3919     OP *veop;
3920 #ifdef PERL_MAD
3921     OP *pegop = newOP(OP_NULL,0);
3922 #endif
3923
3924     PERL_ARGS_ASSERT_UTILIZE;
3925
3926     if (idop->op_type != OP_CONST)
3927         Perl_croak(aTHX_ "Module name must be constant");
3928
3929     if (PL_madskills)
3930         op_getmad(idop,pegop,'U');
3931
3932     veop = NULL;
3933
3934     if (version) {
3935         SV * const vesv = ((SVOP*)version)->op_sv;
3936
3937         if (PL_madskills)
3938             op_getmad(version,pegop,'V');
3939         if (!arg && !SvNIOKp(vesv)) {
3940             arg = version;
3941         }
3942         else {
3943             OP *pack;
3944             SV *meth;
3945
3946             if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3947                 Perl_croak(aTHX_ "Version number must be a constant number");
3948
3949             /* Make copy of idop so we don't free it twice */
3950             pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3951
3952             /* Fake up a method call to VERSION */
3953             meth = newSVpvs_share("VERSION");
3954             veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3955                             append_elem(OP_LIST,
3956                                         prepend_elem(OP_LIST, pack, list(version)),
3957                                         newSVOP(OP_METHOD_NAMED, 0, meth)));
3958         }
3959     }
3960
3961     /* Fake up an import/unimport */
3962     if (arg && arg->op_type == OP_STUB) {
3963         if (PL_madskills)
3964             op_getmad(arg,pegop,'S');
3965         imop = arg;             /* no import on explicit () */
3966     }
3967     else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3968         imop = NULL;            /* use 5.0; */
3969         if (!aver)
3970             idop->op_private |= OPpCONST_NOVER;
3971     }
3972     else {
3973         SV *meth;
3974
3975         if (PL_madskills)
3976             op_getmad(arg,pegop,'A');
3977
3978         /* Make copy of idop so we don't free it twice */
3979         pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3980
3981         /* Fake up a method call to import/unimport */
3982         meth = aver
3983             ? newSVpvs_share("import") : newSVpvs_share("unimport");
3984         imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3985                        append_elem(OP_LIST,
3986                                    prepend_elem(OP_LIST, pack, list(arg)),
3987                                    newSVOP(OP_METHOD_NAMED, 0, meth)));
3988     }
3989
3990     /* Fake up the BEGIN {}, which does its thing immediately. */
3991     newATTRSUB(floor,
3992         newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
3993         NULL,
3994         NULL,
3995         append_elem(OP_LINESEQ,
3996             append_elem(OP_LINESEQ,
3997                 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
3998                 newSTATEOP(0, NULL, veop)),
3999             newSTATEOP(0, NULL, imop) ));
4000
4001     /* The "did you use incorrect case?" warning used to be here.
4002      * The problem is that on case-insensitive filesystems one
4003      * might get false positives for "use" (and "require"):
4004      * "use Strict" or "require CARP" will work.  This causes
4005      * portability problems for the script: in case-strict
4006      * filesystems the script will stop working.
4007      *
4008      * The "incorrect case" warning checked whether "use Foo"
4009      * imported "Foo" to your namespace, but that is wrong, too:
4010      * there is no requirement nor promise in the language that
4011      * a Foo.pm should or would contain anything in package "Foo".
4012      *
4013      * There is very little Configure-wise that can be done, either:
4014      * the case-sensitivity of the build filesystem of Perl does not
4015      * help in guessing the case-sensitivity of the runtime environment.
4016      */
4017
4018     PL_hints |= HINT_BLOCK_SCOPE;
4019     PL_parser->copline = NOLINE;
4020     PL_parser->expect = XSTATE;
4021     PL_cop_seqmax++; /* Purely for B::*'s benefit */
4022
4023 #ifdef PERL_MAD
4024     if (!PL_madskills) {
4025         /* FIXME - don't allocate pegop if !PL_madskills */
4026         op_free(pegop);
4027         return NULL;
4028     }
4029     return pegop;
4030 #endif
4031 }
4032
4033 /*
4034 =head1 Embedding Functions
4035
4036 =for apidoc load_module
4037
4038 Loads the module whose name is pointed to by the string part of name.
4039 Note that the actual module name, not its filename, should be given.
4040 Eg, "Foo::Bar" instead of "Foo/Bar.pm".  flags can be any of
4041 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
4042 (or 0 for no flags). ver, if specified, provides version semantics
4043 similar to C<use Foo::Bar VERSION>.  The optional trailing SV*
4044 arguments can be used to specify arguments to the module's import()
4045 method, similar to C<use Foo::Bar VERSION LIST>.  They must be
4046 terminated with a final NULL pointer.  Note that this list can only
4047 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
4048 Otherwise at least a single NULL pointer to designate the default
4049 import list is required.
4050
4051 =cut */
4052
4053 void
4054 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
4055 {
4056     va_list args;
4057
4058     PERL_ARGS_ASSERT_LOAD_MODULE;
4059
4060     va_start(args, ver);
4061     vload_module(flags, name, ver, &args);
4062     va_end(args);
4063 }
4064
4065 #ifdef PERL_IMPLICIT_CONTEXT
4066 void
4067 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
4068 {
4069     dTHX;
4070     va_list args;
4071     PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
4072     va_start(args, ver);
4073     vload_module(flags, name, ver, &args);
4074     va_end(args);
4075 }
4076 #endif
4077
4078 void
4079 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
4080 {
4081     dVAR;
4082     OP *veop, *imop;
4083     OP * const modname = newSVOP(OP_CONST, 0, name);
4084
4085     PERL_ARGS_ASSERT_VLOAD_MODULE;
4086
4087     modname->op_private |= OPpCONST_BARE;
4088     if (ver) {
4089         veop = newSVOP(OP_CONST, 0, ver);
4090     }
4091     else
4092         veop = NULL;
4093     if (flags & PERL_LOADMOD_NOIMPORT) {
4094         imop = sawparens(newNULLLIST());
4095     }
4096     else if (flags & PERL_LOADMOD_IMPORT_OPS) {
4097         imop = va_arg(*args, OP*);
4098     }
4099     else {
4100         SV *sv;
4101         imop = NULL;
4102         sv = va_arg(*args, SV*);
4103         while (sv) {
4104             imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
4105             sv = va_arg(*args, SV*);
4106         }
4107     }
4108
4109     /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
4110      * that it has a PL_parser to play with while doing that, and also
4111      * that it doesn't mess with any existing parser, by creating a tmp
4112      * new parser with lex_start(). This won't actually be used for much,
4113      * since pp_require() will create another parser for the real work. */
4114
4115     ENTER;
4116     SAVEVPTR(PL_curcop);
4117     lex_start(NULL, NULL, FALSE);
4118     utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
4119             veop, modname, imop);
4120     LEAVE;
4121 }
4122
4123 OP *
4124 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
4125 {
4126     dVAR;
4127     OP *doop;
4128     GV *gv = NULL;
4129
4130     PERL_ARGS_ASSERT_DOFILE;
4131
4132     if (!force_builtin) {
4133         gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
4134         if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
4135             GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
4136             gv = gvp ? *gvp : NULL;
4137         }
4138     }
4139
4140     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
4141         doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
4142                                append_elem(OP_LIST, term,
4143                                            scalar(newUNOP(OP_RV2CV, 0,
4144                                                           newGVOP(OP_GV, 0, gv))))));
4145     }
4146     else {
4147         doop = newUNOP(OP_DOFILE, 0, scalar(term));
4148     }
4149     return doop;
4150 }
4151
4152 OP *
4153 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
4154 {
4155     return newBINOP(OP_LSLICE, flags,
4156             list(force_list(subscript)),
4157             list(force_list(listval)) );
4158 }
4159
4160 STATIC I32
4161 S_is_list_assignment(pTHX_ register const OP *o)
4162 {
4163     unsigned type;
4164     U8 flags;
4165
4166     if (!o)
4167         return TRUE;
4168
4169     if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
4170         o = cUNOPo->op_first;
4171
4172     flags = o->op_flags;
4173     type = o->op_type;
4174     if (type == OP_COND_EXPR) {
4175         const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
4176         const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
4177
4178         if (t && f)
4179             return TRUE;
4180         if (t || f)
4181             yyerror("Assignment to both a list and a scalar");
4182         return FALSE;
4183     }
4184
4185     if (type == OP_LIST &&
4186         (flags & OPf_WANT) == OPf_WANT_SCALAR &&
4187         o->op_private & OPpLVAL_INTRO)
4188         return FALSE;
4189
4190     if (type == OP_LIST || flags & OPf_PARENS ||
4191         type == OP_RV2AV || type == OP_RV2HV ||
4192         type == OP_ASLICE || type == OP_HSLICE)
4193         return TRUE;
4194
4195     if (type == OP_PADAV || type == OP_PADHV)
4196         return TRUE;
4197
4198     if (type == OP_RV2SV)
4199         return FALSE;
4200
4201     return FALSE;
4202 }
4203
4204 OP *
4205 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
4206 {
4207     dVAR;
4208     OP *o;
4209
4210     if (optype) {
4211         if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
4212             return newLOGOP(optype, 0,
4213                 mod(scalar(left), optype),
4214                 newUNOP(OP_SASSIGN, 0, scalar(right)));
4215         }
4216         else {
4217             return newBINOP(optype, OPf_STACKED,
4218                 mod(scalar(left), optype), scalar(right));
4219         }
4220     }
4221
4222     if (is_list_assignment(left)) {
4223         static const char no_list_state[] = "Initialization of state variables"
4224             " in list context currently forbidden";
4225         OP *curop;
4226         bool maybe_common_vars = TRUE;
4227
4228         PL_modcount = 0;
4229         /* Grandfathering $[ assignment here.  Bletch.*/
4230         /* Only simple assignments like C<< ($[) = 1 >> are allowed */
4231         PL_eval_start = (left->op_type == OP_CONST) ? right : NULL;
4232         left = mod(left, OP_AASSIGN);
4233         if (PL_eval_start)
4234             PL_eval_start = 0;
4235         else if (left->op_type == OP_CONST) {
4236             /* FIXME for MAD */
4237             /* Result of assignment is always 1 (or we'd be dead already) */
4238             return newSVOP(OP_CONST, 0, newSViv(1));
4239         }
4240         curop = list(force_list(left));
4241         o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
4242         o->op_private = (U8)(0 | (flags >> 8));
4243
4244         if ((left->op_type == OP_LIST
4245              || (left->op_type == OP_NULL && left->op_targ == OP_LIST)))
4246         {
4247             OP* lop = ((LISTOP*)left)->op_first;
4248             maybe_common_vars = FALSE;
4249             while (lop) {
4250                 if (lop->op_type == OP_PADSV ||
4251                     lop->op_type == OP_PADAV ||
4252                     lop->op_type == OP_PADHV ||
4253                     lop->op_type == OP_PADANY) {
4254                     if (!(lop->op_private & OPpLVAL_INTRO))
4255                         maybe_common_vars = TRUE;
4256
4257                     if (lop->op_private & OPpPAD_STATE) {
4258                         if (left->op_private & OPpLVAL_INTRO) {
4259                             /* Each variable in state($a, $b, $c) = ... */
4260                         }
4261                         else {
4262                             /* Each state variable in
4263                                (state $a, my $b, our $c, $d, undef) = ... */
4264                         }
4265                         yyerror(no_list_state);
4266                     } else {
4267                         /* Each my variable in
4268                            (state $a, my $b, our $c, $d, undef) = ... */
4269                     }
4270                 } else if (lop->op_type == OP_UNDEF ||
4271                            lop->op_type == OP_PUSHMARK) {
4272                     /* undef may be interesting in
4273                        (state $a, undef, state $c) */
4274                 } else {
4275                     /* Other ops in the list. */
4276                     maybe_common_vars = TRUE;
4277                 }
4278                 lop = lop->op_sibling;
4279             }
4280         }
4281         else if ((left->op_private & OPpLVAL_INTRO)
4282                 && (   left->op_type == OP_PADSV
4283                     || left->op_type == OP_PADAV
4284                     || left->op_type == OP_PADHV
4285                     || left->op_type == OP_PADANY))
4286         {
4287             if (left->op_type == OP_PADSV) maybe_common_vars = FALSE;
4288             if (left->op_private & OPpPAD_STATE) {
4289                 /* All single variable list context state assignments, hence
4290                    state ($a) = ...
4291                    (state $a) = ...
4292                    state @a = ...
4293                    state (@a) = ...
4294                    (state @a) = ...
4295                    state %a = ...
4296                    state (%a) = ...
4297                    (state %a) = ...
4298                 */
4299                 yyerror(no_list_state);
4300             }
4301         }
4302
4303         /* PL_generation sorcery:
4304          * an assignment like ($a,$b) = ($c,$d) is easier than
4305          * ($a,$b) = ($c,$a), since there is no need for temporary vars.
4306          * To detect whether there are common vars, the global var
4307          * PL_generation is incremented for each assign op we compile.
4308          * Then, while compiling the assign op, we run through all the
4309          * variables on both sides of the assignment, setting a spare slot
4310          * in each of them to PL_generation. If any of them already have
4311          * that value, we know we've got commonality.  We could use a
4312          * single bit marker, but then we'd have to make 2 passes, first
4313          * to clear the flag, then to test and set it.  To find somewhere
4314          * to store these values, evil chicanery is done with SvUVX().
4315          */
4316
4317         if (maybe_common_vars) {
4318             OP *lastop = o;
4319             PL_generation++;
4320             for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4321                 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
4322                     if (curop->op_type == OP_GV) {
4323                         GV *gv = cGVOPx_gv(curop);
4324                         if (gv == PL_defgv
4325                             || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4326                             break;
4327                         GvASSIGN_GENERATION_set(gv, PL_generation);
4328                     }
4329                     else if (curop->op_type == OP_PADSV ||
4330                              curop->op_type == OP_PADAV ||
4331                              curop->op_type == OP_PADHV ||
4332                              curop->op_type == OP_PADANY)
4333                     {
4334                         if (PAD_COMPNAME_GEN(curop->op_targ)
4335                                                     == (STRLEN)PL_generation)
4336                             break;
4337                         PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
4338
4339                     }
4340                     else if (curop->op_type == OP_RV2CV)
4341                         break;
4342                     else if (curop->op_type == OP_RV2SV ||
4343                              curop->op_type == OP_RV2AV ||
4344                              curop->op_type == OP_RV2HV ||
4345                              curop->op_type == OP_RV2GV) {
4346                         if (lastop->op_type != OP_GV)   /* funny deref? */
4347                             break;
4348                     }
4349                     else if (curop->op_type == OP_PUSHRE) {
4350 #ifdef USE_ITHREADS
4351                         if (((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff) {
4352                             GV *const gv = MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff));
4353                             if (gv == PL_defgv
4354                                 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4355                                 break;
4356                             GvASSIGN_GENERATION_set(gv, PL_generation);
4357                         }
4358 #else
4359                         GV *const gv
4360                             = ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv;
4361                         if (gv) {
4362                             if (gv == PL_defgv
4363                                 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4364                                 break;
4365                             GvASSIGN_GENERATION_set(gv, PL_generation);
4366                         }
4367 #endif
4368                     }
4369                     else
4370                         break;
4371                 }
4372                 lastop = curop;
4373             }
4374             if (curop != o)
4375                 o->op_private |= OPpASSIGN_COMMON;
4376         }
4377
4378         if (right && right->op_type == OP_SPLIT && !PL_madskills) {
4379             OP* tmpop = ((LISTOP*)right)->op_first;
4380             if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
4381                 PMOP * const pm = (PMOP*)tmpop;
4382                 if (left->op_type == OP_RV2AV &&
4383                     !(left->op_private & OPpLVAL_INTRO) &&
4384                     !(o->op_private & OPpASSIGN_COMMON) )
4385                 {
4386                     tmpop = ((UNOP*)left)->op_first;
4387                     if (tmpop->op_type == OP_GV
4388 #ifdef USE_ITHREADS
4389                         && !pm->op_pmreplrootu.op_pmtargetoff
4390 #else
4391                         && !pm->op_pmreplrootu.op_pmtargetgv
4392 #endif
4393                         ) {
4394 #ifdef USE_ITHREADS
4395                         pm->op_pmreplrootu.op_pmtargetoff
4396                             = cPADOPx(tmpop)->op_padix;
4397                         cPADOPx(tmpop)->op_padix = 0;   /* steal it */
4398 #else
4399                         pm->op_pmreplrootu.op_pmtargetgv
4400                             = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
4401                         cSVOPx(tmpop)->op_sv = NULL;    /* steal it */
4402 #endif
4403                         pm->op_pmflags |= PMf_ONCE;
4404                         tmpop = cUNOPo->op_first;       /* to list (nulled) */
4405                         tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
4406                         tmpop->op_sibling = NULL;       /* don't free split */
4407                         right->op_next = tmpop->op_next;  /* fix starting loc */
4408                         op_free(o);                     /* blow off assign */
4409                         right->op_flags &= ~OPf_WANT;
4410                                 /* "I don't know and I don't care." */
4411                         return right;
4412                     }
4413                 }
4414                 else {
4415                    if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
4416                       ((LISTOP*)right)->op_last->op_type == OP_CONST)
4417                     {
4418                         SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
4419                         if (SvIOK(sv) && SvIVX(sv) == 0)
4420                             sv_setiv(sv, PL_modcount+1);
4421                     }
4422                 }
4423             }
4424         }
4425         return o;
4426     }
4427     if (!right)
4428         right = newOP(OP_UNDEF, 0);
4429     if (right->op_type == OP_READLINE) {
4430         right->op_flags |= OPf_STACKED;
4431         return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
4432     }
4433     else {
4434         PL_eval_start = right;  /* Grandfathering $[ assignment here.  Bletch.*/
4435         o = newBINOP(OP_SASSIGN, flags,
4436             scalar(right), mod(scalar(left), OP_SASSIGN) );
4437         if (PL_eval_start)
4438             PL_eval_start = 0;
4439         else {
4440             if (!PL_madskills) { /* assignment to $[ is ignored when making a mad dump */
4441                 deprecate("assignment to $[");
4442                 op_free(o);
4443                 o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling)));
4444                 o->op_private |= OPpCONST_ARYBASE;
4445             }
4446         }
4447     }
4448     return o;
4449 }
4450
4451 OP *
4452 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
4453 {
4454     dVAR;
4455     const U32 seq = intro_my();
4456     register COP *cop;
4457
4458     NewOp(1101, cop, 1, COP);
4459     if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
4460         cop->op_type = OP_DBSTATE;
4461         cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
4462     }
4463     else {
4464         cop->op_type = OP_NEXTSTATE;
4465         cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
4466     }
4467     cop->op_flags = (U8)flags;
4468     CopHINTS_set(cop, PL_hints);
4469 #ifdef NATIVE_HINTS
4470     cop->op_private |= NATIVE_HINTS;
4471 #endif
4472     CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
4473     cop->op_next = (OP*)cop;
4474
4475     cop->cop_seq = seq;
4476     /* CopARYBASE is now "virtual", in that it's stored as a flag bit in
4477        CopHINTS and a possible value in cop_hints_hash, so no need to copy it.
4478     */
4479     cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
4480     cop->cop_hints_hash = PL_curcop->cop_hints_hash;
4481     if (cop->cop_hints_hash) {
4482         HINTS_REFCNT_LOCK;
4483         cop->cop_hints_hash->refcounted_he_refcnt++;
4484         HINTS_REFCNT_UNLOCK;
4485     }
4486     if (label) {
4487         cop->cop_hints_hash
4488             = Perl_store_cop_label(aTHX_ cop->cop_hints_hash, label);
4489                                                      
4490         PL_hints |= HINT_BLOCK_SCOPE;
4491         /* It seems that we need to defer freeing this pointer, as other parts
4492            of the grammar end up wanting to copy it after this op has been
4493            created. */
4494         SAVEFREEPV(label);
4495     }
4496
4497     if (PL_parser && PL_parser->copline == NOLINE)
4498         CopLINE_set(cop, CopLINE(PL_curcop));
4499     else {
4500         CopLINE_set(cop, PL_parser->copline);
4501         if (PL_parser)
4502             PL_parser->copline = NOLINE;
4503     }
4504 #ifdef USE_ITHREADS
4505     CopFILE_set(cop, CopFILE(PL_curcop));       /* XXX share in a pvtable? */
4506 #else
4507     CopFILEGV_set(cop, CopFILEGV(PL_curcop));
4508 #endif
4509     CopSTASH_set(cop, PL_curstash);
4510
4511     if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash) {
4512         /* this line can have a breakpoint - store the cop in IV */
4513         AV *av = CopFILEAVx(PL_curcop);
4514         if (av) {
4515             SV * const * const svp = av_fetch(av, (I32)CopLINE(cop), FALSE);
4516             if (svp && *svp != &PL_sv_undef ) {
4517                 (void)SvIOK_on(*svp);
4518                 SvIV_set(*svp, PTR2IV(cop));
4519             }
4520         }
4521     }
4522
4523     if (flags & OPf_SPECIAL)
4524         op_null((OP*)cop);
4525     return prepend_elem(OP_LINESEQ, (OP*)cop, o);
4526 }
4527
4528
4529 OP *
4530 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
4531 {
4532     dVAR;
4533
4534     PERL_ARGS_ASSERT_NEWLOGOP;
4535
4536     return new_logop(type, flags, &first, &other);
4537 }
4538
4539 STATIC OP *
4540 S_search_const(pTHX_ OP *o)
4541 {
4542     PERL_ARGS_ASSERT_SEARCH_CONST;
4543
4544     switch (o->op_type) {
4545         case OP_CONST:
4546             return o;
4547         case OP_NULL:
4548             if (o->op_flags & OPf_KIDS)
4549                 return search_const(cUNOPo->op_first);
4550             break;
4551         case OP_LEAVE:
4552         case OP_SCOPE:
4553         case OP_LINESEQ:
4554         {
4555             OP *kid;
4556             if (!(o->op_flags & OPf_KIDS))
4557                 return NULL;
4558             kid = cLISTOPo->op_first;
4559             do {
4560                 switch (kid->op_type) {
4561                     case OP_ENTER:
4562                     case OP_NULL:
4563                     case OP_NEXTSTATE:
4564                         kid = kid->op_sibling;
4565                         break;
4566                     default:
4567                         if (kid != cLISTOPo->op_last)
4568                             return NULL;
4569                         goto last;
4570                 }
4571             } while (kid);
4572             if (!kid)
4573                 kid = cLISTOPo->op_last;
4574 last:
4575             return search_const(kid);
4576         }
4577     }
4578
4579     return NULL;
4580 }
4581
4582 STATIC OP *
4583 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
4584 {
4585     dVAR;
4586     LOGOP *logop;
4587     OP *o;
4588     OP *first;
4589     OP *other;
4590     OP *cstop = NULL;
4591     int prepend_not = 0;
4592
4593     PERL_ARGS_ASSERT_NEW_LOGOP;
4594
4595     first = *firstp;
4596     other = *otherp;
4597
4598     if (type == OP_XOR)         /* Not short circuit, but here by precedence. */
4599         return newBINOP(type, flags, scalar(first), scalar(other));
4600
4601     assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP);
4602
4603     scalarboolean(first);
4604     /* optimize AND and OR ops that have NOTs as children */
4605     if (first->op_type == OP_NOT
4606         && (first->op_flags & OPf_KIDS)
4607         && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
4608             || (other->op_type == OP_NOT))  /* if (!$x && !$y) { } */
4609         && !PL_madskills) {
4610         if (type == OP_AND || type == OP_OR) {
4611             if (type == OP_AND)
4612                 type = OP_OR;
4613             else
4614                 type = OP_AND;
4615             op_null(first);
4616             if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
4617                 op_null(other);
4618                 prepend_not = 1; /* prepend a NOT op later */
4619             }
4620         }
4621     }
4622     /* search for a constant op that could let us fold the test */
4623     if ((cstop = search_const(first))) {
4624         if (cstop->op_private & OPpCONST_STRICT)
4625             no_bareword_allowed(cstop);
4626         else if ((cstop->op_private & OPpCONST_BARE))
4627                 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
4628         if ((type == OP_AND &&  SvTRUE(((SVOP*)cstop)->op_sv)) ||
4629             (type == OP_OR  && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
4630             (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
4631             *firstp = NULL;
4632             if (other->op_type == OP_CONST)
4633                 other->op_private |= OPpCONST_SHORTCIRCUIT;
4634             if (PL_madskills) {
4635                 OP *newop = newUNOP(OP_NULL, 0, other);
4636                 op_getmad(first, newop, '1');
4637                 newop->op_targ = type;  /* set "was" field */
4638                 return newop;
4639             }
4640             op_free(first);
4641             if (other->op_type == OP_LEAVE)
4642                 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
4643             return other;
4644         }
4645         else {
4646             /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
4647             const OP *o2 = other;
4648             if ( ! (o2->op_type == OP_LIST
4649                     && (( o2 = cUNOPx(o2)->op_first))
4650                     && o2->op_type == OP_PUSHMARK
4651                     && (( o2 = o2->op_sibling)) )
4652             )
4653                 o2 = other;
4654             if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
4655                         || o2->op_type == OP_PADHV)
4656                 && o2->op_private & OPpLVAL_INTRO
4657                 && !(o2->op_private & OPpPAD_STATE))
4658             {
4659                 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
4660                                  "Deprecated use of my() in false conditional");
4661             }
4662
4663             *otherp = NULL;
4664             if (first->op_type == OP_CONST)
4665                 first->op_private |= OPpCONST_SHORTCIRCUIT;
4666             if (PL_madskills) {
4667                 first = newUNOP(OP_NULL, 0, first);
4668                 op_getmad(other, first, '2');
4669                 first->op_targ = type;  /* set "was" field */
4670             }
4671             else
4672                 op_free(other);
4673             return first;
4674         }
4675     }
4676     else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
4677         && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
4678     {
4679         const OP * const k1 = ((UNOP*)first)->op_first;
4680         const OP * const k2 = k1->op_sibling;
4681         OPCODE warnop = 0;
4682         switch (first->op_type)
4683         {
4684         case OP_NULL:
4685             if (k2 && k2->op_type == OP_READLINE
4686                   && (k2->op_flags & OPf_STACKED)
4687                   && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4688             {
4689                 warnop = k2->op_type;
4690             }
4691             break;
4692
4693         case OP_SASSIGN:
4694             if (k1->op_type == OP_READDIR
4695                   || k1->op_type == OP_GLOB
4696                   || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4697                   || k1->op_type == OP_EACH)
4698             {
4699                 warnop = ((k1->op_type == OP_NULL)
4700                           ? (OPCODE)k1->op_targ : k1->op_type);
4701             }
4702             break;
4703         }
4704         if (warnop) {
4705             const line_t oldline = CopLINE(PL_curcop);
4706             CopLINE_set(PL_curcop, PL_parser->copline);
4707             Perl_warner(aTHX_ packWARN(WARN_MISC),
4708                  "Value of %s%s can be \"0\"; test with defined()",
4709                  PL_op_desc[warnop],
4710                  ((warnop == OP_READLINE || warnop == OP_GLOB)
4711                   ? " construct" : "() operator"));
4712             CopLINE_set(PL_curcop, oldline);
4713         }
4714     }
4715
4716     if (!other)
4717         return first;
4718
4719     if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
4720         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
4721
4722     NewOp(1101, logop, 1, LOGOP);
4723
4724     logop->op_type = (OPCODE)type;
4725     logop->op_ppaddr = PL_ppaddr[type];
4726     logop->op_first = first;
4727     logop->op_flags = (U8)(flags | OPf_KIDS);
4728     logop->op_other = LINKLIST(other);
4729     logop->op_private = (U8)(1 | (flags >> 8));
4730
4731     /* establish postfix order */
4732     logop->op_next = LINKLIST(first);
4733     first->op_next = (OP*)logop;
4734     first->op_sibling = other;
4735
4736     CHECKOP(type,logop);
4737
4738     o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 0, (OP*)logop);
4739     other->op_next = o;
4740
4741     return o;
4742 }
4743
4744 OP *
4745 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
4746 {
4747     dVAR;
4748     LOGOP *logop;
4749     OP *start;
4750     OP *o;
4751     OP *cstop;
4752
4753     PERL_ARGS_ASSERT_NEWCONDOP;
4754
4755     if (!falseop)
4756         return newLOGOP(OP_AND, 0, first, trueop);
4757     if (!trueop)
4758         return newLOGOP(OP_OR, 0, first, falseop);
4759
4760     scalarboolean(first);
4761     if ((cstop = search_const(first))) {
4762         /* Left or right arm of the conditional?  */
4763         const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
4764         OP *live = left ? trueop : falseop;
4765         OP *const dead = left ? falseop : trueop;
4766         if (cstop->op_private & OPpCONST_BARE &&
4767             cstop->op_private & OPpCONST_STRICT) {
4768             no_bareword_allowed(cstop);
4769         }
4770         if (PL_madskills) {
4771             /* This is all dead code when PERL_MAD is not defined.  */
4772             live = newUNOP(OP_NULL, 0, live);
4773             op_getmad(first, live, 'C');
4774             op_getmad(dead, live, left ? 'e' : 't');
4775         } else {
4776             op_free(first);
4777             op_free(dead);
4778         }
4779         if (live->op_type == OP_LEAVE)
4780             live = newUNOP(OP_NULL, OPf_SPECIAL, live);
4781         return live;
4782     }
4783     NewOp(1101, logop, 1, LOGOP);
4784     logop->op_type = OP_COND_EXPR;
4785     logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
4786     logop->op_first = first;
4787     logop->op_flags = (U8)(flags | OPf_KIDS);
4788     logop->op_private = (U8)(1 | (flags >> 8));
4789     logop->op_other = LINKLIST(trueop);
4790     logop->op_next = LINKLIST(falseop);
4791
4792     CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
4793             logop);
4794
4795     /* establish postfix order */
4796     start = LINKLIST(first);
4797     first->op_next = (OP*)logop;
4798
4799     first->op_sibling = trueop;
4800     trueop->op_sibling = falseop;
4801     o = newUNOP(OP_NULL, 0, (OP*)logop);
4802
4803     trueop->op_next = falseop->op_next = o;
4804
4805     o->op_next = start;
4806     return o;
4807 }
4808
4809 OP *
4810 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
4811 {
4812     dVAR;
4813     LOGOP *range;
4814     OP *flip;
4815     OP *flop;
4816     OP *leftstart;
4817     OP *o;
4818