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