Add missing syms to global.sym; update magic doc
[p5sagit/p5-mst-13.2.git] / op.c
1 /*    op.c
2  *
3  *    Copyright (c) 1991-1994, 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 #include "EXTERN.h"
19 #include "perl.h"
20
21 #define USE_OP_MASK  /* Turned on by default in 5.002beta1h */
22
23 #ifdef USE_OP_MASK
24 /*
25  * In the following definition, the ", (OP *) op" is just to make the compiler
26  * think the expression is of the right type: croak actually does a Siglongjmp.
27  */
28 #define CHECKOP(type,op) \
29     ((op_mask && op_mask[type]) \
30      ? (croak("%s trapped by operation mask", op_desc[type]), (OP*)op) \
31      : (*check[type])((OP*)op))
32 #else
33 #define CHECKOP(type,op) (*check[type])(op)
34 #endif /* USE_OP_MASK */
35
36 static I32 list_assignment _((OP *op));
37 static OP *bad_type _((I32 n, char *t, char *name, OP *kid));
38 static OP *modkids _((OP *op, I32 type));
39 static OP *no_fh_allowed _((OP *op));
40 static OP *scalarboolean _((OP *op));
41 static OP *too_few_arguments _((OP *op, char* name));
42 static OP *too_many_arguments _((OP *op, char* name));
43 static void null _((OP* op));
44 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, U32 seq,
45         CV* startcv, I32 cx_ix));
46
47 static char*
48 CvNAME(cv)
49 CV* cv;
50 {
51     SV* tmpsv = sv_newmortal();
52     gv_efullname3(tmpsv, CvGV(cv), Nullch);
53     return SvPV(tmpsv,na);
54 }
55
56 static OP *
57 no_fh_allowed(op)
58 OP *op;
59 {
60     sprintf(tokenbuf,"Missing comma after first argument to %s function",
61         op_desc[op->op_type]);
62     yyerror(tokenbuf);
63     return op;
64 }
65
66 static OP *
67 too_few_arguments(op, name)
68 OP* op;
69 char* name;
70 {
71     sprintf(tokenbuf,"Not enough arguments for %s", name);
72     yyerror(tokenbuf);
73     return op;
74 }
75
76 static OP *
77 too_many_arguments(op, name)
78 OP *op;
79 char* name;
80 {
81     sprintf(tokenbuf,"Too many arguments for %s", name);
82     yyerror(tokenbuf);
83     return op;
84 }
85
86 static OP *
87 bad_type(n, t, name, kid)
88 I32 n;
89 char *t;
90 char *name;
91 OP *kid;
92 {
93     sprintf(tokenbuf, "Type of arg %d to %s must be %s (not %s)",
94         (int) n, name, t, op_desc[kid->op_type]);
95     yyerror(tokenbuf);
96     return op;
97 }
98
99 void
100 assertref(op)
101 OP *op;
102 {
103     int type = op->op_type;
104     if (type != OP_AELEM && type != OP_HELEM) {
105         sprintf(tokenbuf, "Can't use subscript on %s", op_desc[type]);
106         yyerror(tokenbuf);
107         if (type == OP_RV2HV || type == OP_ENTERSUB)
108             warn("(Did you mean $ or @ instead of %c?)\n",
109                 type == OP_RV2HV ? '%' : '&');
110     }
111 }
112
113 /* "register" allocation */
114
115 PADOFFSET
116 pad_allocmy(name)
117 char *name;
118 {
119     PADOFFSET off;
120     SV *sv;
121
122     if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
123         if (!isPRINT(name[1]))
124             sprintf(name+1, "^%c", toCTRL(name[1])); /* XXX tokenbuf, really */
125         croak("Can't use global %s in \"my\"",name);
126     }
127     if (AvFILL(comppad_name) >= 0) {
128         SV **svp = AvARRAY(comppad_name);
129         for (off = AvFILL(comppad_name); off > comppad_name_floor; off--) {
130             if ((sv = svp[off])
131                 && sv != &sv_undef
132                 && SvIVX(sv) == 999999999       /* var is in open scope */
133                 && strEQ(name, SvPVX(sv)))
134             {
135                 warn("\"my\" variable %s masks earlier declaration in same scope", name);
136                 break;
137             }
138         }
139     }
140     off = pad_alloc(OP_PADSV, SVs_PADMY);
141     sv = NEWSV(1102,0);
142     sv_upgrade(sv, SVt_PVNV);
143     sv_setpv(sv, name);
144     av_store(comppad_name, off, sv);
145     SvNVX(sv) = (double)999999999;
146     SvIVX(sv) = 0;                      /* Not yet introduced--see newSTATEOP */
147     if (!min_intro_pending)
148         min_intro_pending = off;
149     max_intro_pending = off;
150     if (*name == '@')
151         av_store(comppad, off, (SV*)newAV());
152     else if (*name == '%')
153         av_store(comppad, off, (SV*)newHV());
154     SvPADMY_on(curpad[off]);
155     return off;
156 }
157
158 static PADOFFSET
159 #ifndef CAN_PROTOTYPE
160 pad_findlex(name, newoff, seq, startcv, cx_ix)
161 char *name;
162 PADOFFSET newoff;
163 U32 seq;
164 CV* startcv;
165 I32 cx_ix;
166 #else
167 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
168 #endif
169 {
170     CV *cv;
171     I32 off;
172     SV *sv;
173     register I32 i;
174     register CONTEXT *cx;
175     int saweval;
176
177     for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
178         AV* curlist = CvPADLIST(cv);
179         SV** svp = av_fetch(curlist, 0, FALSE);
180         AV *curname;
181         if (!svp || *svp == &sv_undef)
182             continue;
183         curname = (AV*)*svp;
184         svp = AvARRAY(curname);
185         for (off = AvFILL(curname); off > 0; off--) {
186             if ((sv = svp[off]) &&
187                 sv != &sv_undef &&
188                 seq <= SvIVX(sv) &&
189                 seq > I_32(SvNVX(sv)) &&
190                 strEQ(SvPVX(sv), name))
191             {
192                 I32 depth;
193                 AV *oldpad;
194                 SV *oldsv;
195
196                 depth = CvDEPTH(cv);
197                 if (!depth) {
198                     if (newoff && !CvUNIQUE(cv))
199                         return 0; /* don't clone inactive sub's stack frame */
200                     depth = 1;
201                 }
202                 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
203                 oldsv = *av_fetch(oldpad, off, TRUE);
204                 if (!newoff) {          /* Not a mere clone operation. */
205                     SV *sv = NEWSV(1103,0);
206                     newoff = pad_alloc(OP_PADSV, SVs_PADMY);
207                     sv_upgrade(sv, SVt_PVNV);
208                     sv_setpv(sv, name);
209                     av_store(comppad_name, newoff, sv);
210                     SvNVX(sv) = (double)curcop->cop_seq;
211                     SvIVX(sv) = 999999999;      /* A ref, intro immediately */
212                     SvFLAGS(sv) |= SVf_FAKE;
213                     if (!CvUNIQUE(cv)) {
214                         /* "It's closures all the way down." */
215                         CvCLONE_on(compcv);
216                         if (cv != startcv) {
217                             CV *bcv;
218                             for (bcv = startcv;
219                                  bcv && bcv != cv && !CvCLONE(bcv);
220                                  bcv = CvOUTSIDE(bcv))
221                                 CvCLONE_on(bcv);
222                         }
223                     }
224                 }
225                 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
226                 return newoff;
227             }
228         }
229     }
230
231     /* Nothing in current lexical context--try eval's context, if any.
232      * This is necessary to let the perldb get at lexically scoped variables.
233      * XXX This will also probably interact badly with eval tree caching.
234      */
235
236     saweval = 0;
237     for (i = cx_ix; i >= 0; i--) {
238         cx = &cxstack[i];
239         switch (cx->cx_type) {
240         default:
241             if (i == 0 && saweval) {
242                 seq = cxstack[saweval].blk_oldcop->cop_seq;
243                 return pad_findlex(name, newoff, seq, main_cv, 0);
244             }
245             break;
246         case CXt_EVAL:
247             if (cx->blk_eval.old_op_type != OP_ENTEREVAL &&
248                 cx->blk_eval.old_op_type != OP_ENTERTRY)
249                 return 0;       /* require must have its own scope */
250             saweval = i;
251             break;
252         case CXt_SUB:
253             if (!saweval)
254                 return 0;
255             cv = cx->blk_sub.cv;
256             if (debstash && CvSTASH(cv) == debstash) {  /* ignore DB'* scope */
257                 saweval = i;    /* so we know where we were called from */
258                 continue;
259             }
260             seq = cxstack[saweval].blk_oldcop->cop_seq;
261             return pad_findlex(name, newoff, seq, cv, i-1);
262         }
263     }
264
265     return 0;
266 }
267
268 PADOFFSET
269 pad_findmy(name)
270 char *name;
271 {
272     I32 off;
273     SV *sv;
274     SV **svp = AvARRAY(comppad_name);
275     U32 seq = cop_seqmax;
276
277     /* The one we're looking for is probably just before comppad_name_fill. */
278     for (off = AvFILL(comppad_name); off > 0; off--) {
279         if ((sv = svp[off]) &&
280             sv != &sv_undef &&
281             seq <= SvIVX(sv) &&
282             seq > I_32(SvNVX(sv)) &&
283             strEQ(SvPVX(sv), name))
284         {
285             return (PADOFFSET)off;
286         }
287     }
288
289     /* See if it's in a nested scope */
290     off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
291     if (off)
292         return off;
293
294     return 0;
295 }
296
297 void
298 pad_leavemy(fill)
299 I32 fill;
300 {
301     I32 off;
302     SV **svp = AvARRAY(comppad_name);
303     SV *sv;
304     if (min_intro_pending && fill < min_intro_pending) {
305         for (off = max_intro_pending; off >= min_intro_pending; off--) {
306             if ((sv = svp[off]) && sv != &sv_undef)
307                 warn("%s never introduced", SvPVX(sv));
308         }
309     }
310     /* "Deintroduce" my variables that are leaving with this scope. */
311     for (off = AvFILL(comppad_name); off > fill; off--) {
312         if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
313             SvIVX(sv) = cop_seqmax;
314     }
315 }
316
317 PADOFFSET
318 pad_alloc(optype,tmptype)       
319 I32 optype;
320 U32 tmptype;
321 {
322     SV *sv;
323     I32 retval;
324
325     if (AvARRAY(comppad) != curpad)
326         croak("panic: pad_alloc");
327     if (pad_reset_pending)
328         pad_reset();
329     if (tmptype & SVs_PADMY) {
330         do {
331             sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
332         } while (SvPADBUSY(sv));                /* need a fresh one */
333         retval = AvFILL(comppad);
334     }
335     else {
336         SV **names = AvARRAY(comppad_name);
337         SSize_t names_fill = AvFILL(comppad_name);
338         for (;;) {
339             /*
340              * "foreach" index vars temporarily become aliases to non-"my"
341              * values.  Thus we must skip, not just pad values that are
342              * marked as current pad values, but also those with names.
343              */
344             if (++padix <= names_fill &&
345                    (sv = names[padix]) && sv != &sv_undef)
346                 continue;
347             sv = *av_fetch(comppad, padix, TRUE);
348             if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
349                 break;
350         }
351         retval = padix;
352     }
353     SvFLAGS(sv) |= tmptype;
354     curpad = AvARRAY(comppad);
355     DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad alloc %ld for %s\n", (long) retval, op_name[optype]));
356     return (PADOFFSET)retval;
357 }
358
359 SV *
360 #ifndef CAN_PROTOTYPE
361 pad_sv(po)
362 PADOFFSET po;
363 #else
364 pad_sv(PADOFFSET po)
365 #endif /* CAN_PROTOTYPE */
366 {
367     if (!po)
368         croak("panic: pad_sv po");
369     DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad sv %d\n", po));
370     return curpad[po];          /* eventually we'll turn this into a macro */
371 }
372
373 void
374 #ifndef CAN_PROTOTYPE
375 pad_free(po)
376 PADOFFSET po;
377 #else
378 pad_free(PADOFFSET po)
379 #endif /* CAN_PROTOTYPE */
380 {
381     if (!curpad)
382         return;
383     if (AvARRAY(comppad) != curpad)
384         croak("panic: pad_free curpad");
385     if (!po)
386         croak("panic: pad_free po");
387     DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad free %d\n", po));
388     if (curpad[po] && curpad[po] != &sv_undef)
389         SvPADTMP_off(curpad[po]);
390     if ((I32)po < padix)
391         padix = po - 1;
392 }
393
394 void
395 #ifndef CAN_PROTOTYPE
396 pad_swipe(po)
397 PADOFFSET po;
398 #else
399 pad_swipe(PADOFFSET po)
400 #endif /* CAN_PROTOTYPE */
401 {
402     if (AvARRAY(comppad) != curpad)
403         croak("panic: pad_swipe curpad");
404     if (!po)
405         croak("panic: pad_swipe po");
406     DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad swipe %d\n", po));
407     SvPADTMP_off(curpad[po]);
408     curpad[po] = NEWSV(1107,0);
409     SvPADTMP_on(curpad[po]);
410     if ((I32)po < padix)
411         padix = po - 1;
412 }
413
414 void
415 pad_reset()
416 {
417     register I32 po;
418
419     if (AvARRAY(comppad) != curpad)
420         croak("panic: pad_reset curpad");
421     DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad reset\n"));
422     if (!tainting) {    /* Can't mix tainted and non-tainted temporaries. */
423         for (po = AvMAX(comppad); po > padix_floor; po--) {
424             if (curpad[po] && curpad[po] != &sv_undef)
425                 SvPADTMP_off(curpad[po]);
426         }
427         padix = padix_floor;
428     }
429     pad_reset_pending = FALSE;
430 }
431
432 /* Destructor */
433
434 void
435 op_free(op)
436 OP *op;
437 {
438     register OP *kid, *nextkid;
439
440     if (!op || op->op_seq == (U16)-1)
441         return;
442
443     if (op->op_flags & OPf_KIDS) {
444         for (kid = cUNOP->op_first; kid; kid = nextkid) {
445             nextkid = kid->op_sibling; /* Get before next freeing kid */
446             op_free(kid);
447         }
448     }
449
450     switch (op->op_type) {
451     case OP_NULL:
452         op->op_targ = 0;        /* Was holding old type, if any. */
453         break;
454     case OP_ENTEREVAL:
455         op->op_targ = 0;        /* Was holding hints. */
456         break;
457     case OP_GVSV:
458     case OP_GV:
459         SvREFCNT_dec(cGVOP->op_gv);
460         break;
461     case OP_NEXTSTATE:
462     case OP_DBSTATE:
463         Safefree(cCOP->cop_label);
464         SvREFCNT_dec(cCOP->cop_filegv);
465         break;
466     case OP_CONST:
467         SvREFCNT_dec(cSVOP->op_sv);
468         break;
469     case OP_GOTO:
470     case OP_NEXT:
471     case OP_LAST:
472     case OP_REDO:
473         if (op->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
474             break;
475         /* FALL THROUGH */
476     case OP_TRANS:
477         Safefree(cPVOP->op_pv);
478         break;
479     case OP_SUBST:
480         op_free(cPMOP->op_pmreplroot);
481         /* FALL THROUGH */
482     case OP_PUSHRE:
483     case OP_MATCH:
484         pregfree(cPMOP->op_pmregexp);
485         SvREFCNT_dec(cPMOP->op_pmshort);
486         break;
487     default:
488         break;
489     }
490
491     if (op->op_targ > 0)
492         pad_free(op->op_targ);
493
494     Safefree(op);
495 }
496
497 static void
498 null(op)
499 OP* op;
500 {
501     if (op->op_type != OP_NULL && op->op_targ > 0)
502         pad_free(op->op_targ);
503     op->op_targ = op->op_type;
504     op->op_type = OP_NULL;
505     op->op_ppaddr = ppaddr[OP_NULL];
506 }
507
508 /* Contextualizers */
509
510 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
511
512 OP *
513 linklist(op)
514 OP *op;
515 {
516     register OP *kid;
517
518     if (op->op_next)
519         return op->op_next;
520
521     /* establish postfix order */
522     if (cUNOP->op_first) {
523         op->op_next = LINKLIST(cUNOP->op_first);
524         for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
525             if (kid->op_sibling)
526                 kid->op_next = LINKLIST(kid->op_sibling);
527             else
528                 kid->op_next = op;
529         }
530     }
531     else
532         op->op_next = op;
533
534     return op->op_next;
535 }
536
537 OP *
538 scalarkids(op)
539 OP *op;
540 {
541     OP *kid;
542     if (op && op->op_flags & OPf_KIDS) {
543         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
544             scalar(kid);
545     }
546     return op;
547 }
548
549 static OP *
550 scalarboolean(op)
551 OP *op;
552 {
553     if (dowarn &&
554         op->op_type == OP_SASSIGN && cBINOP->op_first->op_type == OP_CONST) {
555         line_t oldline = curcop->cop_line;
556
557         if (copline != NOLINE)
558             curcop->cop_line = copline;
559         warn("Found = in conditional, should be ==");
560         curcop->cop_line = oldline;
561     }
562     return scalar(op);
563 }
564
565 OP *
566 scalar(op)
567 OP *op;
568 {
569     OP *kid;
570
571     /* assumes no premature commitment */
572     if (!op || (op->op_flags & OPf_KNOW) || op->op_type == OP_RETURN
573          || error_count)
574         return op;
575
576     op->op_flags &= ~OPf_LIST;
577     op->op_flags |= OPf_KNOW;
578
579     switch (op->op_type) {
580     case OP_REPEAT:
581         if (op->op_private & OPpREPEAT_DOLIST)
582             null(((LISTOP*)cBINOP->op_first)->op_first);
583         scalar(cBINOP->op_first);
584         break;
585     case OP_OR:
586     case OP_AND:
587     case OP_COND_EXPR:
588         for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
589             scalar(kid);
590         break;
591     case OP_SPLIT:
592         if ((kid = ((LISTOP*)op)->op_first) && kid->op_type == OP_PUSHRE) {
593             if (!kPMOP->op_pmreplroot)
594                 deprecate("implicit split to @_");
595         }
596         /* FALL THROUGH */
597     case OP_MATCH:
598     case OP_SUBST:
599     case OP_NULL:
600     default:
601         if (op->op_flags & OPf_KIDS) {
602             for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
603                 scalar(kid);
604         }
605         break;
606     case OP_LEAVE:
607     case OP_LEAVETRY:
608         scalar(cLISTOP->op_first);
609         /* FALL THROUGH */
610     case OP_SCOPE:
611     case OP_LINESEQ:
612     case OP_LIST:
613         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
614             if (kid->op_sibling)
615                 scalarvoid(kid);
616             else
617                 scalar(kid);
618         }
619         curcop = &compiling;
620         break;
621     }
622     return op;
623 }
624
625 OP *
626 scalarvoid(op)
627 OP *op;
628 {
629     OP *kid;
630     char* useless = 0;
631     SV* sv;
632
633     if (!op || error_count)
634         return op;
635     if (op->op_flags & OPf_LIST)
636         return op;
637
638     op->op_flags |= OPf_KNOW;
639
640     switch (op->op_type) {
641     default:
642         if (!(opargs[op->op_type] & OA_FOLDCONST))
643             break;
644         /* FALL THROUGH */
645     case OP_REPEAT:
646         if (op->op_flags & OPf_STACKED)
647             break;
648         /* FALL THROUGH */
649     case OP_GVSV:
650     case OP_WANTARRAY:
651     case OP_GV:
652     case OP_PADSV:
653     case OP_PADAV:
654     case OP_PADHV:
655     case OP_PADANY:
656     case OP_AV2ARYLEN:
657     case OP_REF:
658     case OP_REFGEN:
659     case OP_SREFGEN:
660     case OP_DEFINED:
661     case OP_HEX:
662     case OP_OCT:
663     case OP_LENGTH:
664     case OP_SUBSTR:
665     case OP_VEC:
666     case OP_INDEX:
667     case OP_RINDEX:
668     case OP_SPRINTF:
669     case OP_AELEM:
670     case OP_AELEMFAST:
671     case OP_ASLICE:
672     case OP_VALUES:
673     case OP_KEYS:
674     case OP_HELEM:
675     case OP_HSLICE:
676     case OP_UNPACK:
677     case OP_PACK:
678     case OP_JOIN:
679     case OP_LSLICE:
680     case OP_ANONLIST:
681     case OP_ANONHASH:
682     case OP_SORT:
683     case OP_REVERSE:
684     case OP_RANGE:
685     case OP_FLIP:
686     case OP_FLOP:
687     case OP_CALLER:
688     case OP_FILENO:
689     case OP_EOF:
690     case OP_TELL:
691     case OP_GETSOCKNAME:
692     case OP_GETPEERNAME:
693     case OP_READLINK:
694     case OP_TELLDIR:
695     case OP_GETPPID:
696     case OP_GETPGRP:
697     case OP_GETPRIORITY:
698     case OP_TIME:
699     case OP_TMS:
700     case OP_LOCALTIME:
701     case OP_GMTIME:
702     case OP_GHBYNAME:
703     case OP_GHBYADDR:
704     case OP_GHOSTENT:
705     case OP_GNBYNAME:
706     case OP_GNBYADDR:
707     case OP_GNETENT:
708     case OP_GPBYNAME:
709     case OP_GPBYNUMBER:
710     case OP_GPROTOENT:
711     case OP_GSBYNAME:
712     case OP_GSBYPORT:
713     case OP_GSERVENT:
714     case OP_GPWNAM:
715     case OP_GPWUID:
716     case OP_GGRNAM:
717     case OP_GGRGID:
718     case OP_GETLOGIN:
719         if (!(op->op_private & OPpLVAL_INTRO))
720             useless = op_desc[op->op_type];
721         break;
722
723     case OP_RV2GV:
724     case OP_RV2SV:
725     case OP_RV2AV:
726     case OP_RV2HV:
727         if (!(op->op_private & OPpLVAL_INTRO) &&
728                 (!op->op_sibling || op->op_sibling->op_type != OP_READLINE))
729             useless = "a variable";
730         break;
731
732     case OP_NEXTSTATE:
733     case OP_DBSTATE:
734         curcop = ((COP*)op);            /* for warning below */
735         break;
736
737     case OP_CONST:
738         sv = cSVOP->op_sv;
739         if (dowarn) {
740             useless = "a constant";
741             if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
742                 useless = 0;
743             else if (SvPOK(sv)) {
744                 if (strnEQ(SvPVX(sv), "di", 2) ||
745                     strnEQ(SvPVX(sv), "ds", 2) ||
746                     strnEQ(SvPVX(sv), "ig", 2))
747                         useless = 0;
748             }
749         }
750         null(op);               /* don't execute a constant */
751         SvREFCNT_dec(sv);       /* don't even remember it */
752         break;
753
754     case OP_POSTINC:
755         op->op_type = OP_PREINC;                /* pre-increment is faster */
756         op->op_ppaddr = ppaddr[OP_PREINC];
757         break;
758
759     case OP_POSTDEC:
760         op->op_type = OP_PREDEC;                /* pre-decrement is faster */
761         op->op_ppaddr = ppaddr[OP_PREDEC];
762         break;
763
764     case OP_OR:
765     case OP_AND:
766     case OP_COND_EXPR:
767         for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
768             scalarvoid(kid);
769         break;
770     case OP_NULL:
771         if (op->op_targ == OP_NEXTSTATE || op->op_targ == OP_DBSTATE)
772             curcop = ((COP*)op);                /* for warning below */
773         if (op->op_flags & OPf_STACKED)
774             break;
775     case OP_ENTERTRY:
776     case OP_ENTER:
777     case OP_SCALAR:
778         if (!(op->op_flags & OPf_KIDS))
779             break;
780     case OP_SCOPE:
781     case OP_LEAVE:
782     case OP_LEAVETRY:
783     case OP_LEAVELOOP:
784         op->op_private |= OPpLEAVE_VOID;
785     case OP_LINESEQ:
786     case OP_LIST:
787         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
788             scalarvoid(kid);
789         break;
790     case OP_SPLIT:
791         if ((kid = ((LISTOP*)op)->op_first) && kid->op_type == OP_PUSHRE) {
792             if (!kPMOP->op_pmreplroot)
793                 deprecate("implicit split to @_");
794         }
795         break;
796     case OP_DELETE:
797         op->op_private |= OPpLEAVE_VOID;
798         break;
799     }
800     if (useless && dowarn)
801         warn("Useless use of %s in void context", useless);
802     return op;
803 }
804
805 OP *
806 listkids(op)
807 OP *op;
808 {
809     OP *kid;
810     if (op && op->op_flags & OPf_KIDS) {
811         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
812             list(kid);
813     }
814     return op;
815 }
816
817 OP *
818 list(op)
819 OP *op;
820 {
821     OP *kid;
822
823     /* assumes no premature commitment */
824     if (!op || (op->op_flags & OPf_KNOW) || op->op_type == OP_RETURN
825          || error_count)
826         return op;
827
828     op->op_flags |= (OPf_KNOW | OPf_LIST);
829
830     switch (op->op_type) {
831     case OP_FLOP:
832     case OP_REPEAT:
833         list(cBINOP->op_first);
834         break;
835     case OP_OR:
836     case OP_AND:
837     case OP_COND_EXPR:
838         for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
839             list(kid);
840         break;
841     default:
842     case OP_MATCH:
843     case OP_SUBST:
844     case OP_NULL:
845         if (!(op->op_flags & OPf_KIDS))
846             break;
847         if (!op->op_next && cUNOP->op_first->op_type == OP_FLOP) {
848             list(cBINOP->op_first);
849             return gen_constant_list(op);
850         }
851     case OP_LIST:
852         listkids(op);
853         break;
854     case OP_LEAVE:
855     case OP_LEAVETRY:
856         list(cLISTOP->op_first);
857         /* FALL THROUGH */
858     case OP_SCOPE:
859     case OP_LINESEQ:
860         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
861             if (kid->op_sibling)
862                 scalarvoid(kid);
863             else
864                 list(kid);
865         }
866         curcop = &compiling;
867         break;
868     }
869     return op;
870 }
871
872 OP *
873 scalarseq(op)
874 OP *op;
875 {
876     OP *kid;
877
878     if (op) {
879         if (op->op_type == OP_LINESEQ ||
880              op->op_type == OP_SCOPE ||
881              op->op_type == OP_LEAVE ||
882              op->op_type == OP_LEAVETRY)
883         {
884             for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
885                 if (kid->op_sibling) {
886                     scalarvoid(kid);
887                 }
888             }
889             curcop = &compiling;
890         }
891         op->op_flags &= ~OPf_PARENS;
892         if (hints & HINT_BLOCK_SCOPE)
893             op->op_flags |= OPf_PARENS;
894     }
895     else
896         op = newOP(OP_STUB, 0);
897     return op;
898 }
899
900 static OP *
901 modkids(op, type)
902 OP *op;
903 I32 type;
904 {
905     OP *kid;
906     if (op && op->op_flags & OPf_KIDS) {
907         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
908             mod(kid, type);
909     }
910     return op;
911 }
912
913 static I32 modcount;
914
915 OP *
916 mod(op, type)
917 OP *op;
918 I32 type;
919 {
920     OP *kid;
921     SV *sv;
922
923     if (!op || error_count)
924         return op;
925
926     switch (op->op_type) {
927     case OP_CONST:
928         if (!(op->op_private & (OPpCONST_ARYBASE)))
929             goto nomod;
930         if (eval_start && eval_start->op_type == OP_CONST) {
931             compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
932             eval_start = 0;
933         }
934         else if (!type) {
935             SAVEI32(compiling.cop_arybase);
936             compiling.cop_arybase = 0;
937         }
938         else if (type == OP_REFGEN)
939             goto nomod;
940         else
941             croak("That use of $[ is unsupported");
942         break;
943     case OP_STUB:
944         if (op->op_flags & OPf_PARENS)
945             break;
946         goto nomod;
947     case OP_ENTERSUB:
948         if ((type == OP_UNDEF || type == OP_REFGEN) &&
949             !(op->op_flags & OPf_STACKED)) {
950             op->op_type = OP_RV2CV;             /* entersub => rv2cv */
951             op->op_ppaddr = ppaddr[OP_RV2CV];
952             assert(cUNOP->op_first->op_type == OP_NULL);
953             null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
954             break;
955         }
956         /* FALL THROUGH */
957     default:
958       nomod:
959         /* grep, foreach, subcalls, refgen */
960         if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
961             break;
962         sprintf(tokenbuf, "Can't modify %s in %s",
963             op_desc[op->op_type],
964             type ? op_desc[type] : "local");
965         yyerror(tokenbuf);
966         return op;
967
968     case OP_PREINC:
969     case OP_PREDEC:
970     case OP_POW:
971     case OP_MULTIPLY:
972     case OP_DIVIDE:
973     case OP_MODULO:
974     case OP_REPEAT:
975     case OP_ADD:
976     case OP_SUBTRACT:
977     case OP_CONCAT:
978     case OP_LEFT_SHIFT:
979     case OP_RIGHT_SHIFT:
980     case OP_BIT_AND:
981     case OP_BIT_XOR:
982     case OP_BIT_OR:
983     case OP_I_MULTIPLY:
984     case OP_I_DIVIDE:
985     case OP_I_MODULO:
986     case OP_I_ADD:
987     case OP_I_SUBTRACT:
988         if (!(op->op_flags & OPf_STACKED))
989             goto nomod;
990         modcount++;
991         break;
992         
993     case OP_COND_EXPR:
994         for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
995             mod(kid, type);
996         break;
997
998     case OP_RV2AV:
999     case OP_RV2HV:
1000         if (type == OP_REFGEN && op->op_flags & OPf_PARENS) {
1001             modcount = 10000;
1002             return op;          /* Treat \(@foo) like ordinary list. */
1003         }
1004         /* FALL THROUGH */
1005     case OP_RV2GV:
1006         ref(cUNOP->op_first, op->op_type);
1007         /* FALL THROUGH */
1008     case OP_AASSIGN:
1009     case OP_ASLICE:
1010     case OP_HSLICE:
1011     case OP_NEXTSTATE:
1012     case OP_DBSTATE:
1013     case OP_REFGEN:
1014     case OP_CHOMP:
1015         modcount = 10000;
1016         break;
1017     case OP_RV2SV:
1018         if (!type && cUNOP->op_first->op_type != OP_GV)
1019             croak("Can't localize a reference");
1020         ref(cUNOP->op_first, op->op_type); 
1021         /* FALL THROUGH */
1022     case OP_UNDEF:
1023     case OP_GV:
1024     case OP_AV2ARYLEN:
1025     case OP_SASSIGN:
1026     case OP_AELEMFAST:
1027         modcount++;
1028         break;
1029
1030     case OP_PADAV:
1031     case OP_PADHV:
1032         modcount = 10000;
1033         if (type == OP_REFGEN && op->op_flags & OPf_PARENS)
1034             return op;          /* Treat \(@foo) like ordinary list. */
1035         /* FALL THROUGH */
1036     case OP_PADSV:
1037         modcount++;
1038         if (!type)
1039             croak("Can't localize lexical variable %s",
1040                 SvPV(*av_fetch(comppad_name, op->op_targ, 4), na));
1041         break;
1042
1043     case OP_PUSHMARK:
1044         break;
1045         
1046     case OP_KEYS:
1047         if (type != OP_SASSIGN)
1048             goto nomod;
1049         /* FALL THROUGH */
1050     case OP_POS:
1051     case OP_VEC:
1052     case OP_SUBSTR:
1053         pad_free(op->op_targ);
1054         op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
1055         assert(SvTYPE(PAD_SV(op->op_targ)) == SVt_NULL);
1056         if (op->op_flags & OPf_KIDS)
1057             mod(cBINOP->op_first->op_sibling, type);
1058         break;
1059
1060     case OP_AELEM:
1061     case OP_HELEM:
1062         ref(cBINOP->op_first, op->op_type);
1063         modcount++;
1064         break;
1065
1066     case OP_SCOPE:
1067     case OP_LEAVE:
1068     case OP_ENTER:
1069         if (op->op_flags & OPf_KIDS)
1070             mod(cLISTOP->op_last, type);
1071         break;
1072
1073     case OP_NULL:
1074         if (!(op->op_flags & OPf_KIDS))
1075             break;
1076         if (op->op_targ != OP_LIST) {
1077             mod(cBINOP->op_first, type);
1078             break;
1079         }
1080         /* FALL THROUGH */
1081     case OP_LIST:
1082         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1083             mod(kid, type);
1084         break;
1085     }
1086     op->op_flags |= OPf_MOD;
1087
1088     if (type == OP_AASSIGN || type == OP_SASSIGN)
1089         op->op_flags |= OPf_SPECIAL|OPf_REF;
1090     else if (!type) {
1091         op->op_private |= OPpLVAL_INTRO;
1092         op->op_flags &= ~OPf_SPECIAL;
1093     }
1094     else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1095         op->op_flags |= OPf_REF;
1096     return op;
1097 }
1098
1099 OP *
1100 refkids(op, type)
1101 OP *op;
1102 I32 type;
1103 {
1104     OP *kid;
1105     if (op && op->op_flags & OPf_KIDS) {
1106         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1107             ref(kid, type);
1108     }
1109     return op;
1110 }
1111
1112 OP *
1113 ref(op, type)
1114 OP *op;
1115 I32 type;
1116 {
1117     OP *kid;
1118
1119     if (!op || error_count)
1120         return op;
1121
1122     switch (op->op_type) {
1123     case OP_ENTERSUB:
1124         if ((type == OP_DEFINED) &&
1125             !(op->op_flags & OPf_STACKED)) {
1126             op->op_type = OP_RV2CV;             /* entersub => rv2cv */
1127             op->op_ppaddr = ppaddr[OP_RV2CV];
1128             assert(cUNOP->op_first->op_type == OP_NULL);
1129             null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
1130             op->op_flags |= OPf_SPECIAL;
1131         }
1132         break;
1133       
1134     case OP_COND_EXPR:
1135         for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
1136             ref(kid, type);
1137         break;
1138     case OP_RV2SV:
1139         ref(cUNOP->op_first, op->op_type);
1140         /* FALL THROUGH */
1141     case OP_PADSV:
1142         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1143             op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1144                                : type == OP_RV2HV ? OPpDEREF_HV
1145                                : OPpDEREF_SV);
1146             op->op_flags |= OPf_MOD;
1147         }
1148         break;
1149       
1150     case OP_RV2AV:
1151     case OP_RV2HV:
1152         op->op_flags |= OPf_REF; 
1153         /* FALL THROUGH */
1154     case OP_RV2GV:
1155         ref(cUNOP->op_first, op->op_type);
1156         break;
1157
1158     case OP_PADAV:
1159     case OP_PADHV:
1160         op->op_flags |= OPf_REF; 
1161         break;
1162       
1163     case OP_SCALAR:
1164     case OP_NULL:
1165         if (!(op->op_flags & OPf_KIDS))
1166             break;
1167         ref(cBINOP->op_first, type);
1168         break;
1169     case OP_AELEM:
1170     case OP_HELEM:
1171         ref(cBINOP->op_first, op->op_type);
1172         if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1173             op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1174                                : type == OP_RV2HV ? OPpDEREF_HV
1175                                : OPpDEREF_SV);
1176             op->op_flags |= OPf_MOD;
1177         }
1178         break;
1179
1180     case OP_SCOPE:
1181     case OP_LEAVE:
1182     case OP_ENTER:
1183     case OP_LIST:
1184         if (!(op->op_flags & OPf_KIDS))
1185             break;
1186         ref(cLISTOP->op_last, type);
1187         break;
1188     default:
1189         break;
1190     }
1191     return scalar(op);
1192
1193 }
1194
1195 OP *
1196 my(op)
1197 OP *op;
1198 {
1199     OP *kid;
1200     I32 type;
1201
1202     if (!op || error_count)
1203         return op;
1204
1205     type = op->op_type;
1206     if (type == OP_LIST) {
1207         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1208             my(kid);
1209     }
1210     else if (type != OP_PADSV &&
1211              type != OP_PADAV &&
1212              type != OP_PADHV &&
1213              type != OP_PUSHMARK)
1214     {
1215         sprintf(tokenbuf, "Can't declare %s in my", op_desc[op->op_type]);
1216         yyerror(tokenbuf);
1217         return op;
1218     }
1219     op->op_flags |= OPf_MOD;
1220     op->op_private |= OPpLVAL_INTRO;
1221     return op;
1222 }
1223
1224 OP *
1225 sawparens(o)
1226 OP *o;
1227 {
1228     if (o)
1229         o->op_flags |= OPf_PARENS;
1230     return o;
1231 }
1232
1233 OP *
1234 bind_match(type, left, right)
1235 I32 type;
1236 OP *left;
1237 OP *right;
1238 {
1239     OP *op;
1240
1241     if (right->op_type == OP_MATCH ||
1242         right->op_type == OP_SUBST ||
1243         right->op_type == OP_TRANS) {
1244         right->op_flags |= OPf_STACKED;
1245         if (right->op_type != OP_MATCH)
1246             left = mod(left, right->op_type);
1247         if (right->op_type == OP_TRANS)
1248             op = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1249         else
1250             op = prepend_elem(right->op_type, scalar(left), right);
1251         if (type == OP_NOT)
1252             return newUNOP(OP_NOT, 0, scalar(op));
1253         return op;
1254     }
1255     else
1256         return bind_match(type, left,
1257                 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1258 }
1259
1260 OP *
1261 invert(op)
1262 OP *op;
1263 {
1264     if (!op)
1265         return op;
1266     /* XXX need to optimize away NOT NOT here?  Or do we let optimizer do it? */
1267     return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
1268 }
1269
1270 OP *
1271 scope(o)
1272 OP *o;
1273 {
1274     if (o) {
1275         if (o->op_flags & OPf_PARENS || perldb || tainting) {
1276             o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1277             o->op_type = OP_LEAVE;
1278             o->op_ppaddr = ppaddr[OP_LEAVE];
1279         }
1280         else {
1281             if (o->op_type == OP_LINESEQ) {
1282                 OP *kid;
1283                 o->op_type = OP_SCOPE;
1284                 o->op_ppaddr = ppaddr[OP_SCOPE];
1285                 kid = ((LISTOP*)o)->op_first;
1286                 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1287                     SvREFCNT_dec(((COP*)kid)->cop_filegv);
1288                     null(kid);
1289                 }
1290             }
1291             else
1292                 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1293         }
1294     }
1295     return o;
1296 }
1297
1298 int
1299 block_start(full)
1300 int full;
1301 {
1302     int retval = savestack_ix;
1303     SAVEI32(comppad_name_floor);
1304     if (full) {
1305         if ((comppad_name_fill = AvFILL(comppad_name)) > 0)
1306             comppad_name_floor = comppad_name_fill;
1307         else
1308             comppad_name_floor = 0;
1309     }
1310     SAVEI32(min_intro_pending);
1311     SAVEI32(max_intro_pending);
1312     min_intro_pending = 0;
1313     SAVEI32(comppad_name_fill);
1314     SAVEI32(padix_floor);
1315     padix_floor = padix;
1316     pad_reset_pending = FALSE;
1317     SAVEI32(hints);
1318     hints &= ~HINT_BLOCK_SCOPE;
1319     return retval;
1320 }
1321
1322 OP*
1323 block_end(floor, seq)
1324 I32 floor;
1325 OP* seq;
1326 {
1327     int needblockscope = hints & HINT_BLOCK_SCOPE;
1328     OP* retval = scalarseq(seq);
1329     LEAVE_SCOPE(floor);
1330     pad_reset_pending = FALSE;
1331     if (needblockscope)
1332         hints |= HINT_BLOCK_SCOPE; /* propagate out */
1333     pad_leavemy(comppad_name_fill);
1334     cop_seqmax++;
1335     return retval;
1336 }
1337
1338 void
1339 newPROG(op)
1340 OP *op;
1341 {
1342     if (in_eval) {
1343         eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), op);
1344         eval_start = linklist(eval_root);
1345         eval_root->op_next = 0;
1346         peep(eval_start);
1347     }
1348     else {
1349         if (!op) {
1350             main_start = 0;
1351             return;
1352         }
1353         main_root = scope(sawparens(scalarvoid(op)));
1354         curcop = &compiling;
1355         main_start = LINKLIST(main_root);
1356         main_root->op_next = 0;
1357         peep(main_start);
1358         main_cv = compcv;
1359         compcv = 0;
1360     }
1361 }
1362
1363 OP *
1364 localize(o, lex)
1365 OP *o;
1366 I32 lex;
1367 {
1368     if (o->op_flags & OPf_PARENS)
1369         list(o);
1370     else {
1371         scalar(o);
1372         if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1373             char *s;
1374             for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1375             if (*s == ';' || *s == '=')
1376                 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1377         }
1378     }
1379     in_my = FALSE;
1380     if (lex)
1381         return my(o);
1382     else
1383         return mod(o, OP_NULL);         /* a bit kludgey */
1384 }
1385
1386 OP *
1387 jmaybe(o)
1388 OP *o;
1389 {
1390     if (o->op_type == OP_LIST) {
1391         o = convert(OP_JOIN, 0,
1392                 prepend_elem(OP_LIST,
1393                     newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1394                     o));
1395     }
1396     return o;
1397 }
1398
1399 OP *
1400 fold_constants(o)
1401 register OP *o;
1402 {
1403     register OP *curop;
1404     I32 type = o->op_type;
1405     SV *sv;
1406
1407     if (opargs[type] & OA_RETSCALAR)
1408         scalar(o);
1409     if (opargs[type] & OA_TARGET)
1410         o->op_targ = pad_alloc(type, SVs_PADTMP);
1411
1412     if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1413         o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1414
1415     if (!(opargs[type] & OA_FOLDCONST))
1416         goto nope;
1417
1418     if (error_count)
1419         goto nope;              /* Don't try to run w/ errors */
1420
1421     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1422         if (curop->op_type != OP_CONST &&
1423                 curop->op_type != OP_LIST &&
1424                 curop->op_type != OP_SCALAR &&
1425                 curop->op_type != OP_NULL &&
1426                 curop->op_type != OP_PUSHMARK) {
1427             goto nope;
1428         }
1429     }
1430
1431     curop = LINKLIST(o);
1432     o->op_next = 0;
1433     op = curop;
1434     runops();
1435     sv = *(stack_sp--);
1436     if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1437         pad_swipe(o->op_targ);
1438     else if (SvTEMP(sv)) {                      /* grab mortal temp? */
1439         (void)SvREFCNT_inc(sv);
1440         SvTEMP_off(sv);
1441     }
1442     op_free(o);
1443     if (type == OP_RV2GV)
1444         return newGVOP(OP_GV, 0, (GV*)sv);
1445     else {
1446         if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1447             IV iv = SvIV(sv);
1448             if ((double)iv == SvNV(sv)) {       /* can we smush double to int */
1449                 SvREFCNT_dec(sv);
1450                 sv = newSViv(iv);
1451             }
1452             else
1453                 SvIOK_off(sv);                  /* undo SvIV() damage */
1454         }
1455         return newSVOP(OP_CONST, 0, sv);
1456     }
1457     
1458   nope:
1459     if (!(opargs[type] & OA_OTHERINT))
1460         return o;
1461
1462     if (!(hints & HINT_INTEGER)) {
1463         if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1464             return o;
1465
1466         for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1467             if (curop->op_type == OP_CONST) {
1468                 if (SvIOK(((SVOP*)curop)->op_sv))
1469                     continue;
1470                 return o;
1471             }
1472             if (opargs[curop->op_type] & OA_RETINTEGER)
1473                 continue;
1474             return o;
1475         }
1476         o->op_ppaddr = ppaddr[++(o->op_type)];
1477     }
1478
1479     return o;
1480 }
1481
1482 OP *
1483 gen_constant_list(o)
1484 register OP *o;
1485 {
1486     register OP *curop;
1487     I32 oldtmps_floor = tmps_floor;
1488
1489     list(o);
1490     if (error_count)
1491         return o;               /* Don't attempt to run with errors */
1492
1493     op = curop = LINKLIST(o);
1494     o->op_next = 0;
1495     pp_pushmark();
1496     runops();
1497     op = curop;
1498     pp_anonlist();
1499     tmps_floor = oldtmps_floor;
1500
1501     o->op_type = OP_RV2AV;
1502     o->op_ppaddr = ppaddr[OP_RV2AV];
1503     curop = ((UNOP*)o)->op_first;
1504     ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1505     op_free(curop);
1506     linklist(o);
1507     return list(o);
1508 }
1509
1510 OP *
1511 convert(type, flags, op)
1512 I32 type;
1513 I32 flags;
1514 OP* op;
1515 {
1516     OP *kid;
1517     OP *last = 0;
1518
1519     if (!op || op->op_type != OP_LIST)
1520         op = newLISTOP(OP_LIST, 0, op, Nullop);
1521     else
1522         op->op_flags &= ~(OPf_KNOW|OPf_LIST);
1523
1524     if (!(opargs[type] & OA_MARK))
1525         null(cLISTOP->op_first);
1526
1527     op->op_type = type;
1528     op->op_ppaddr = ppaddr[type];
1529     op->op_flags |= flags;
1530
1531     op = CHECKOP(type, op);
1532     if (op->op_type != type)
1533         return op;
1534
1535     if (cLISTOP->op_children < 7) {
1536         /* XXX do we really need to do this if we're done appending?? */
1537         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1538             last = kid;
1539         cLISTOP->op_last = last;        /* in case check substituted last arg */
1540     }
1541
1542     return fold_constants(op);
1543 }
1544
1545 /* List constructors */
1546
1547 OP *
1548 append_elem(type, first, last)
1549 I32 type;
1550 OP* first;
1551 OP* last;
1552 {
1553     if (!first)
1554         return last;
1555
1556     if (!last)
1557         return first;
1558
1559     if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1560             return newLISTOP(type, 0, first, last);
1561
1562     if (first->op_flags & OPf_KIDS)
1563         ((LISTOP*)first)->op_last->op_sibling = last;
1564     else {
1565         first->op_flags |= OPf_KIDS;
1566         ((LISTOP*)first)->op_first = last;
1567     }
1568     ((LISTOP*)first)->op_last = last;
1569     ((LISTOP*)first)->op_children++;
1570     return first;
1571 }
1572
1573 OP *
1574 append_list(type, first, last)
1575 I32 type;
1576 LISTOP* first;
1577 LISTOP* last;
1578 {
1579     if (!first)
1580         return (OP*)last;
1581
1582     if (!last)
1583         return (OP*)first;
1584
1585     if (first->op_type != type)
1586         return prepend_elem(type, (OP*)first, (OP*)last);
1587
1588     if (last->op_type != type)
1589         return append_elem(type, (OP*)first, (OP*)last);
1590
1591     first->op_last->op_sibling = last->op_first;
1592     first->op_last = last->op_last;
1593     first->op_children += last->op_children;
1594     if (first->op_children)
1595         last->op_flags |= OPf_KIDS;
1596
1597     Safefree(last);
1598     return (OP*)first;
1599 }
1600
1601 OP *
1602 prepend_elem(type, first, last)
1603 I32 type;
1604 OP* first;
1605 OP* last;
1606 {
1607     if (!first)
1608         return last;
1609
1610     if (!last)
1611         return first;
1612
1613     if (last->op_type == type) {
1614         if (type == OP_LIST) {  /* already a PUSHMARK there */
1615             first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1616             ((LISTOP*)last)->op_first->op_sibling = first;
1617         }
1618         else {
1619             if (!(last->op_flags & OPf_KIDS)) {
1620                 ((LISTOP*)last)->op_last = first;
1621                 last->op_flags |= OPf_KIDS;
1622             }
1623             first->op_sibling = ((LISTOP*)last)->op_first;
1624             ((LISTOP*)last)->op_first = first;
1625         }
1626         ((LISTOP*)last)->op_children++;
1627         return last;
1628     }
1629
1630     return newLISTOP(type, 0, first, last);
1631 }
1632
1633 /* Constructors */
1634
1635 OP *
1636 newNULLLIST()
1637 {
1638     return newOP(OP_STUB, 0);
1639 }
1640
1641 OP *
1642 force_list(op)
1643 OP* op;
1644 {
1645     if (!op || op->op_type != OP_LIST)
1646         op = newLISTOP(OP_LIST, 0, op, Nullop);
1647     null(op);
1648     return op;
1649 }
1650
1651 OP *
1652 newLISTOP(type, flags, first, last)
1653 I32 type;
1654 I32 flags;
1655 OP* first;
1656 OP* last;
1657 {
1658     LISTOP *listop;
1659
1660     Newz(1101, listop, 1, LISTOP);
1661
1662     listop->op_type = type;
1663     listop->op_ppaddr = ppaddr[type];
1664     listop->op_children = (first != 0) + (last != 0);
1665     listop->op_flags = flags;
1666
1667     if (!last && first)
1668         last = first;
1669     else if (!first && last)
1670         first = last;
1671     else if (first)
1672         first->op_sibling = last;
1673     listop->op_first = first;
1674     listop->op_last = last;
1675     if (type == OP_LIST) {
1676         OP* pushop;
1677         pushop = newOP(OP_PUSHMARK, 0);
1678         pushop->op_sibling = first;
1679         listop->op_first = pushop;
1680         listop->op_flags |= OPf_KIDS;
1681         if (!last)
1682             listop->op_last = pushop;
1683     }
1684     else if (listop->op_children)
1685         listop->op_flags |= OPf_KIDS;
1686
1687     return (OP*)listop;
1688 }
1689
1690 OP *
1691 newOP(type, flags)
1692 I32 type;
1693 I32 flags;
1694 {
1695     OP *op;
1696     Newz(1101, op, 1, OP);
1697     op->op_type = type;
1698     op->op_ppaddr = ppaddr[type];
1699     op->op_flags = flags;
1700
1701     op->op_next = op;
1702     op->op_private = 0 + (flags >> 8);
1703     if (opargs[type] & OA_RETSCALAR)
1704         scalar(op);
1705     if (opargs[type] & OA_TARGET)
1706         op->op_targ = pad_alloc(type, SVs_PADTMP);
1707     return CHECKOP(type, op);
1708 }
1709
1710 OP *
1711 newUNOP(type, flags, first)
1712 I32 type;
1713 I32 flags;
1714 OP* first;
1715 {
1716     UNOP *unop;
1717
1718     if (!first)
1719         first = newOP(OP_STUB, 0); 
1720     if (opargs[type] & OA_MARK)
1721         first = force_list(first);
1722
1723     Newz(1101, unop, 1, UNOP);
1724     unop->op_type = type;
1725     unop->op_ppaddr = ppaddr[type];
1726     unop->op_first = first;
1727     unop->op_flags = flags | OPf_KIDS;
1728     unop->op_private = 1 | (flags >> 8);
1729
1730     unop = (UNOP*) CHECKOP(type, unop);
1731     if (unop->op_next)
1732         return (OP*)unop;
1733
1734     return fold_constants((OP *) unop);
1735 }
1736
1737 OP *
1738 newBINOP(type, flags, first, last)
1739 I32 type;
1740 I32 flags;
1741 OP* first;
1742 OP* last;
1743 {
1744     BINOP *binop;
1745     Newz(1101, binop, 1, BINOP);
1746
1747     if (!first)
1748         first = newOP(OP_NULL, 0);
1749
1750     binop->op_type = type;
1751     binop->op_ppaddr = ppaddr[type];
1752     binop->op_first = first;
1753     binop->op_flags = flags | OPf_KIDS;
1754     if (!last) {
1755         last = first;
1756         binop->op_private = 1 | (flags >> 8);
1757     }
1758     else {
1759         binop->op_private = 2 | (flags >> 8);
1760         first->op_sibling = last;
1761     }
1762
1763     binop = (BINOP*)CHECKOP(type, binop);
1764     if (binop->op_next)
1765         return (OP*)binop;
1766
1767     binop->op_last = last = binop->op_first->op_sibling;
1768
1769     return fold_constants((OP *)binop);
1770 }
1771
1772 OP *
1773 pmtrans(op, expr, repl)
1774 OP *op;
1775 OP *expr;
1776 OP *repl;
1777 {
1778     SV *tstr = ((SVOP*)expr)->op_sv;
1779     SV *rstr = ((SVOP*)repl)->op_sv;
1780     STRLEN tlen;
1781     STRLEN rlen;
1782     register U8 *t = (U8*)SvPV(tstr, tlen);
1783     register U8 *r = (U8*)SvPV(rstr, rlen);
1784     register I32 i;
1785     register I32 j;
1786     I32 delete;
1787     I32 complement;
1788     register short *tbl;
1789
1790     tbl = (short*)cPVOP->op_pv;
1791     complement  = op->op_private & OPpTRANS_COMPLEMENT;
1792     delete      = op->op_private & OPpTRANS_DELETE;
1793     /* squash   = op->op_private & OPpTRANS_SQUASH; */
1794
1795     if (complement) {
1796         Zero(tbl, 256, short);
1797         for (i = 0; i < tlen; i++)
1798             tbl[t[i]] = -1;
1799         for (i = 0, j = 0; i < 256; i++) {
1800             if (!tbl[i]) {
1801                 if (j >= rlen) {
1802                     if (delete)
1803                         tbl[i] = -2;
1804                     else if (rlen)
1805                         tbl[i] = r[j-1];
1806                     else
1807                         tbl[i] = i;
1808                 }
1809                 else
1810                     tbl[i] = r[j++];
1811             }
1812         }
1813     }
1814     else {
1815         if (!rlen && !delete) {
1816             r = t; rlen = tlen;
1817         }
1818         for (i = 0; i < 256; i++)
1819             tbl[i] = -1;
1820         for (i = 0, j = 0; i < tlen; i++,j++) {
1821             if (j >= rlen) {
1822                 if (delete) {
1823                     if (tbl[t[i]] == -1)
1824                         tbl[t[i]] = -2;
1825                     continue;
1826                 }
1827                 --j;
1828             }
1829             if (tbl[t[i]] == -1)
1830                 tbl[t[i]] = r[j];
1831         }
1832     }
1833     op_free(expr);
1834     op_free(repl);
1835
1836     return op;
1837 }
1838
1839 OP *
1840 newPMOP(type, flags)
1841 I32 type;
1842 I32 flags;
1843 {
1844     PMOP *pmop;
1845
1846     Newz(1101, pmop, 1, PMOP);
1847     pmop->op_type = type;
1848     pmop->op_ppaddr = ppaddr[type];
1849     pmop->op_flags = flags;
1850     pmop->op_private = 0 | (flags >> 8);
1851
1852     if (hints & HINT_LOCALE)
1853         pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
1854
1855     /* link into pm list */
1856     if (type != OP_TRANS && curstash) {
1857         pmop->op_pmnext = HvPMROOT(curstash);
1858         HvPMROOT(curstash) = pmop;
1859     }
1860
1861     return (OP*)pmop;
1862 }
1863
1864 OP *
1865 pmruntime(op, expr, repl)
1866 OP *op;
1867 OP *expr;
1868 OP *repl;
1869 {
1870     PMOP *pm;
1871     LOGOP *rcop;
1872
1873     if (op->op_type == OP_TRANS)
1874         return pmtrans(op, expr, repl);
1875
1876     pm = (PMOP*)op;
1877
1878     if (expr->op_type == OP_CONST) {
1879         STRLEN plen;
1880         SV *pat = ((SVOP*)expr)->op_sv;
1881         char *p = SvPV(pat, plen);
1882         if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
1883             sv_setpvn(pat, "\\s+", 3);
1884             p = SvPV(pat, plen);
1885             pm->op_pmflags |= PMf_SKIPWHITE;
1886         }
1887         pm->op_pmregexp = pregcomp(p, p + plen, pm);
1888         if (strEQ("\\s+", pm->op_pmregexp->precomp)) 
1889             pm->op_pmflags |= PMf_WHITE;
1890         hoistmust(pm);
1891         op_free(expr);
1892     }
1893     else {
1894         if (pm->op_pmflags & PMf_KEEP)
1895             expr = newUNOP(OP_REGCMAYBE,0,expr);
1896
1897         Newz(1101, rcop, 1, LOGOP);
1898         rcop->op_type = OP_REGCOMP;
1899         rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1900         rcop->op_first = scalar(expr);
1901         rcop->op_flags |= OPf_KIDS;
1902         rcop->op_private = 1;
1903         rcop->op_other = op;
1904
1905         /* establish postfix order */
1906         if (pm->op_pmflags & PMf_KEEP) {
1907             LINKLIST(expr);
1908             rcop->op_next = expr;
1909             ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1910         }
1911         else {
1912             rcop->op_next = LINKLIST(expr);
1913             expr->op_next = (OP*)rcop;
1914         }
1915
1916         prepend_elem(op->op_type, scalar((OP*)rcop), op);
1917     }
1918
1919     if (repl) {
1920         OP *curop;
1921         if (pm->op_pmflags & PMf_EVAL)
1922             curop = 0;
1923         else if (repl->op_type == OP_CONST)
1924             curop = repl;
1925         else {
1926             OP *lastop = 0;
1927             for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1928                 if (opargs[curop->op_type] & OA_DANGEROUS) {
1929                     if (curop->op_type == OP_GV) {
1930                         GV *gv = ((GVOP*)curop)->op_gv;
1931                         if (strchr("&`'123456789+", *GvENAME(gv)))
1932                             break;
1933                     }
1934                     else if (curop->op_type == OP_RV2CV)
1935                         break;
1936                     else if (curop->op_type == OP_RV2SV ||
1937                              curop->op_type == OP_RV2AV ||
1938                              curop->op_type == OP_RV2HV ||
1939                              curop->op_type == OP_RV2GV) {
1940                         if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1941                             break;
1942                     }
1943                     else if (curop->op_type == OP_PADSV ||
1944                              curop->op_type == OP_PADAV ||
1945                              curop->op_type == OP_PADHV ||
1946                              curop->op_type == OP_PADANY) {
1947                              /* is okay */
1948                     }
1949                     else
1950                         break;
1951                 }
1952                 lastop = curop;
1953             }
1954         }
1955         if (curop == repl) {
1956             pm->op_pmflags |= PMf_CONST;        /* const for long enough */
1957             pm->op_pmpermflags |= PMf_CONST;    /* const for long enough */
1958             prepend_elem(op->op_type, scalar(repl), op);
1959         }
1960         else {
1961             Newz(1101, rcop, 1, LOGOP);
1962             rcop->op_type = OP_SUBSTCONT;
1963             rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1964             rcop->op_first = scalar(repl);
1965             rcop->op_flags |= OPf_KIDS;
1966             rcop->op_private = 1;
1967             rcop->op_other = op;
1968
1969             /* establish postfix order */
1970             rcop->op_next = LINKLIST(repl);
1971             repl->op_next = (OP*)rcop;
1972
1973             pm->op_pmreplroot = scalar((OP*)rcop);
1974             pm->op_pmreplstart = LINKLIST(rcop);
1975             rcop->op_next = 0;
1976         }
1977     }
1978
1979     return (OP*)pm;
1980 }
1981
1982 OP *
1983 newSVOP(type, flags, sv)
1984 I32 type;
1985 I32 flags;
1986 SV *sv;
1987 {
1988     SVOP *svop;
1989     Newz(1101, svop, 1, SVOP);
1990     svop->op_type = type;
1991     svop->op_ppaddr = ppaddr[type];
1992     svop->op_sv = sv;
1993     svop->op_next = (OP*)svop;
1994     svop->op_flags = flags;
1995     if (opargs[type] & OA_RETSCALAR)
1996         scalar((OP*)svop);
1997     if (opargs[type] & OA_TARGET)
1998         svop->op_targ = pad_alloc(type, SVs_PADTMP);
1999     return CHECKOP(type, svop);
2000 }
2001
2002 OP *
2003 newGVOP(type, flags, gv)
2004 I32 type;
2005 I32 flags;
2006 GV *gv;
2007 {
2008     GVOP *gvop;
2009     Newz(1101, gvop, 1, GVOP);
2010     gvop->op_type = type;
2011     gvop->op_ppaddr = ppaddr[type];
2012     gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2013     gvop->op_next = (OP*)gvop;
2014     gvop->op_flags = flags;
2015     if (opargs[type] & OA_RETSCALAR)
2016         scalar((OP*)gvop);
2017     if (opargs[type] & OA_TARGET)
2018         gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2019     return CHECKOP(type, gvop);
2020 }
2021
2022 OP *
2023 newPVOP(type, flags, pv)
2024 I32 type;
2025 I32 flags;
2026 char *pv;
2027 {
2028     PVOP *pvop;
2029     Newz(1101, pvop, 1, PVOP);
2030     pvop->op_type = type;
2031     pvop->op_ppaddr = ppaddr[type];
2032     pvop->op_pv = pv;
2033     pvop->op_next = (OP*)pvop;
2034     pvop->op_flags = flags;
2035     if (opargs[type] & OA_RETSCALAR)
2036         scalar((OP*)pvop);
2037     if (opargs[type] & OA_TARGET)
2038         pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2039     return CHECKOP(type, pvop);
2040 }
2041
2042 void
2043 package(op)
2044 OP *op;
2045 {
2046     SV *sv;
2047
2048     save_hptr(&curstash);
2049     save_item(curstname);
2050     if (op) {
2051         STRLEN len;
2052         char *name;
2053         sv = cSVOP->op_sv;
2054         name = SvPV(sv, len);
2055         curstash = gv_stashpvn(name,len,TRUE);
2056         sv_setpvn(curstname, name, len);
2057         op_free(op);
2058     }
2059     else {
2060         sv_setpv(curstname,"<none>");
2061         curstash = Nullhv;
2062     }
2063     copline = NOLINE;
2064     expect = XSTATE;
2065 }
2066
2067 void
2068 utilize(aver, floor, version, id, arg)
2069 int aver;
2070 I32 floor;
2071 OP *version;
2072 OP *id;
2073 OP *arg;
2074 {
2075     OP *pack;
2076     OP *meth;
2077     OP *rqop;
2078     OP *imop;
2079     OP *veop;
2080
2081     if (id->op_type != OP_CONST)
2082         croak("Module name must be constant");
2083
2084     veop = Nullop;
2085
2086     if(version != Nullop) {
2087         SV *vesv = ((SVOP*)version)->op_sv;
2088
2089         if (arg == Nullop && !SvNIOK(vesv)) {
2090             arg = version;
2091         }
2092         else {
2093             OP *pack;
2094             OP *meth;
2095
2096             if (version->op_type != OP_CONST || !SvNIOK(vesv))
2097                 croak("Version number must be constant number");
2098
2099             /* Make copy of id so we don't free it twice */
2100             pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2101
2102             /* Fake up a method call to VERSION */
2103             meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2104             veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2105                             append_elem(OP_LIST,
2106                             prepend_elem(OP_LIST, pack, list(version)),
2107                             newUNOP(OP_METHOD, 0, meth)));
2108         }
2109     }
2110      
2111     /* Fake up an import/unimport */
2112     if (arg && arg->op_type == OP_STUB)
2113         imop = arg;             /* no import on explicit () */
2114     else if(SvNIOK(((SVOP*)id)->op_sv)) {
2115         imop = Nullop;          /* use 5.0; */
2116     }
2117     else {
2118         /* Make copy of id so we don't free it twice */
2119         pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2120         meth = newSVOP(OP_CONST, 0,
2121             aver
2122                 ? newSVpv("import", 6)
2123                 : newSVpv("unimport", 8)
2124             );
2125         imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2126                     append_elem(OP_LIST,
2127                         prepend_elem(OP_LIST, pack, list(arg)),
2128                         newUNOP(OP_METHOD, 0, meth)));
2129     }
2130
2131     /* Fake up a require */
2132     rqop = newUNOP(OP_REQUIRE, 0, id);
2133
2134     /* Fake up the BEGIN {}, which does its thing immediately. */
2135     newSUB(floor,
2136         newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2137         Nullop,
2138         append_elem(OP_LINESEQ,
2139             append_elem(OP_LINESEQ,
2140                 newSTATEOP(0, Nullch, rqop),
2141                 newSTATEOP(0, Nullch, veop)),
2142             newSTATEOP(0, Nullch, imop) ));
2143
2144     copline = NOLINE;
2145     expect = XSTATE;
2146 }
2147
2148 OP *
2149 newSLICEOP(flags, subscript, listval)
2150 I32 flags;
2151 OP *subscript;
2152 OP *listval;
2153 {
2154     return newBINOP(OP_LSLICE, flags,
2155             list(force_list(subscript)),
2156             list(force_list(listval)) );
2157 }
2158
2159 static I32
2160 list_assignment(op)
2161 register OP *op;
2162 {
2163     if (!op)
2164         return TRUE;
2165
2166     if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
2167         op = cUNOP->op_first;
2168
2169     if (op->op_type == OP_COND_EXPR) {
2170         I32 t = list_assignment(cCONDOP->op_first->op_sibling);
2171         I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
2172
2173         if (t && f)
2174             return TRUE;
2175         if (t || f)
2176             yyerror("Assignment to both a list and a scalar");
2177         return FALSE;
2178     }
2179
2180     if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
2181         op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
2182         op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
2183         return TRUE;
2184
2185     if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
2186         return TRUE;
2187
2188     if (op->op_type == OP_RV2SV)
2189         return FALSE;
2190
2191     return FALSE;
2192 }
2193
2194 OP *
2195 newASSIGNOP(flags, left, optype, right)
2196 I32 flags;
2197 OP *left;
2198 I32 optype;
2199 OP *right;
2200 {
2201     OP *op;
2202
2203     if (optype) {
2204         if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2205             return newLOGOP(optype, 0,
2206                 mod(scalar(left), optype),
2207                 newUNOP(OP_SASSIGN, 0, scalar(right)));
2208         }
2209         else {
2210             return newBINOP(optype, OPf_STACKED,
2211                 mod(scalar(left), optype), scalar(right));
2212         }
2213     }
2214
2215     if (list_assignment(left)) {
2216         modcount = 0;
2217         eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
2218         left = mod(left, OP_AASSIGN);
2219         if (eval_start)
2220             eval_start = 0;
2221         else {
2222             op_free(left);
2223             op_free(right);
2224             return Nullop;
2225         }
2226         op = newBINOP(OP_AASSIGN, flags,
2227                 list(force_list(right)),
2228                 list(force_list(left)) );
2229         op->op_private = 0 | (flags >> 8);
2230         if (!(left->op_private & OPpLVAL_INTRO)) {
2231             static int generation = 100;
2232             OP *curop;
2233             OP *lastop = op;
2234             generation++;
2235             for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
2236                 if (opargs[curop->op_type] & OA_DANGEROUS) {
2237                     if (curop->op_type == OP_GV) {
2238                         GV *gv = ((GVOP*)curop)->op_gv;
2239                         if (gv == defgv || SvCUR(gv) == generation)
2240                             break;
2241                         SvCUR(gv) = generation;
2242                     }
2243                     else if (curop->op_type == OP_PADSV ||
2244                              curop->op_type == OP_PADAV ||
2245                              curop->op_type == OP_PADHV ||
2246                              curop->op_type == OP_PADANY) {
2247                         SV **svp = AvARRAY(comppad_name);
2248                         SV *sv = svp[curop->op_targ];
2249                         if (SvCUR(sv) == generation)
2250                             break;
2251                         SvCUR(sv) = generation; /* (SvCUR not used any more) */
2252                     }
2253                     else if (curop->op_type == OP_RV2CV)
2254                         break;
2255                     else if (curop->op_type == OP_RV2SV ||
2256                              curop->op_type == OP_RV2AV ||
2257                              curop->op_type == OP_RV2HV ||
2258                              curop->op_type == OP_RV2GV) {
2259                         if (lastop->op_type != OP_GV)   /* funny deref? */
2260                             break;
2261                     }
2262                     else
2263                         break;
2264                 }
2265                 lastop = curop;
2266             }
2267             if (curop != op)
2268                 op->op_private = OPpASSIGN_COMMON;
2269         }
2270         if (right && right->op_type == OP_SPLIT) {
2271             OP* tmpop;
2272             if ((tmpop = ((LISTOP*)right)->op_first) &&
2273                 tmpop->op_type == OP_PUSHRE)
2274             {
2275                 PMOP *pm = (PMOP*)tmpop;
2276                 if (left->op_type == OP_RV2AV &&
2277                     !(left->op_private & OPpLVAL_INTRO) &&
2278                     !(op->op_private & OPpASSIGN_COMMON) )
2279                 {
2280                     tmpop = ((UNOP*)left)->op_first;
2281                     if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2282                         pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2283                         pm->op_pmflags |= PMf_ONCE;
2284                         tmpop = ((UNOP*)op)->op_first;  /* to list (nulled) */
2285                         tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2286                         tmpop->op_sibling = Nullop;     /* don't free split */
2287                         right->op_next = tmpop->op_next;  /* fix starting loc */
2288                         op_free(op);                    /* blow off assign */
2289                         right->op_flags &= ~(OPf_KNOW|OPf_LIST);
2290                                 /* "I don't know and I don't care." */
2291                         return right;
2292                     }
2293                 }
2294                 else {
2295                     if (modcount < 10000 &&
2296                       ((LISTOP*)right)->op_last->op_type == OP_CONST)
2297                     {
2298                         SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2299                         if (SvIVX(sv) == 0)
2300                             sv_setiv(sv, modcount+1);
2301                     }
2302                 }
2303             }
2304         }
2305         return op;
2306     }
2307     if (!right)
2308         right = newOP(OP_UNDEF, 0);
2309     if (right->op_type == OP_READLINE) {
2310         right->op_flags |= OPf_STACKED;
2311         return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2312     }
2313     else {
2314         eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
2315         op = newBINOP(OP_SASSIGN, flags,
2316             scalar(right), mod(scalar(left), OP_SASSIGN) );
2317         if (eval_start)
2318             eval_start = 0;
2319         else {
2320             op_free(op);
2321             return Nullop;
2322         }
2323     }
2324     return op;
2325 }
2326
2327 OP *
2328 newSTATEOP(flags, label, op)
2329 I32 flags;
2330 char *label;
2331 OP *op;
2332 {
2333     U32 seq = intro_my();
2334     register COP *cop;
2335
2336     Newz(1101, cop, 1, COP);
2337     if (perldb && curcop->cop_line && curstash != debstash) {
2338         cop->op_type = OP_DBSTATE;
2339         cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2340     }
2341     else {
2342         cop->op_type = OP_NEXTSTATE;
2343         cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2344     }
2345     cop->op_flags = flags;
2346     cop->op_private = 0 | (flags >> 8);
2347     cop->op_next = (OP*)cop;
2348
2349     if (label) {
2350         cop->cop_label = label;
2351         hints |= HINT_BLOCK_SCOPE;
2352     }
2353     cop->cop_seq = seq;
2354     cop->cop_arybase = curcop->cop_arybase;
2355
2356     if (copline == NOLINE)
2357         cop->cop_line = curcop->cop_line;
2358     else {
2359         cop->cop_line = copline;
2360         copline = NOLINE;
2361     }
2362     cop->cop_filegv = GvREFCNT_inc(curcop->cop_filegv);
2363     cop->cop_stash = curstash;
2364
2365     if (perldb && curstash != debstash) {
2366         SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2367         if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2368             (void)SvIOK_on(*svp);
2369             SvIVX(*svp) = 1;
2370             SvSTASH(*svp) = (HV*)cop;
2371         }
2372     }
2373
2374     return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2375 }
2376
2377 /* "Introduce" my variables to visible status. */
2378 U32
2379 intro_my()
2380 {
2381     SV **svp;
2382     SV *sv;
2383     I32 i;
2384
2385     if (! min_intro_pending)
2386         return cop_seqmax;
2387
2388     svp = AvARRAY(comppad_name);
2389     for (i = min_intro_pending; i <= max_intro_pending; i++) {
2390         if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2391             SvIVX(sv) = 999999999;      /* Don't know scope end yet. */
2392             SvNVX(sv) = (double)cop_seqmax;
2393         }
2394     }
2395     min_intro_pending = 0;
2396     comppad_name_fill = max_intro_pending;      /* Needn't search higher */
2397     return cop_seqmax++;
2398 }
2399
2400 OP *
2401 newLOGOP(type, flags, first, other)
2402 I32 type;
2403 I32 flags;
2404 OP* first;
2405 OP* other;
2406 {
2407     LOGOP *logop;
2408     OP *op;
2409
2410     if (type == OP_XOR)         /* Not short circuit, but here by precedence. */
2411         return newBINOP(type, flags, scalar(first), scalar(other));
2412
2413     scalarboolean(first);
2414     /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2415     if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2416         if (type == OP_AND || type == OP_OR) {
2417             if (type == OP_AND)
2418                 type = OP_OR;
2419             else
2420                 type = OP_AND;
2421             op = first;
2422             first = cUNOP->op_first;
2423             if (op->op_next)
2424                 first->op_next = op->op_next;
2425             cUNOP->op_first = Nullop;
2426             op_free(op);
2427         }
2428     }
2429     if (first->op_type == OP_CONST) {
2430         if (dowarn && (first->op_private & OPpCONST_BARE))
2431             warn("Probable precedence problem on %s", op_desc[type]);
2432         if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2433             op_free(first);
2434             return other;
2435         }
2436         else {
2437             op_free(other);
2438             return first;
2439         }
2440     }
2441     else if (first->op_type == OP_WANTARRAY) {
2442         if (type == OP_AND)
2443             list(other);
2444         else
2445             scalar(other);
2446     }
2447
2448     if (!other)
2449         return first;
2450
2451     if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2452         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
2453
2454     Newz(1101, logop, 1, LOGOP);
2455
2456     logop->op_type = type;
2457     logop->op_ppaddr = ppaddr[type];
2458     logop->op_first = first;
2459     logop->op_flags = flags | OPf_KIDS;
2460     logop->op_other = LINKLIST(other);
2461     logop->op_private = 1 | (flags >> 8);
2462
2463     /* establish postfix order */
2464     logop->op_next = LINKLIST(first);
2465     first->op_next = (OP*)logop;
2466     first->op_sibling = other;
2467
2468     op = newUNOP(OP_NULL, 0, (OP*)logop);
2469     other->op_next = op;
2470
2471     return op;
2472 }
2473
2474 OP *
2475 newCONDOP(flags, first, trueop, falseop)
2476 I32 flags;
2477 OP* first;
2478 OP* trueop;
2479 OP* falseop;
2480 {
2481     CONDOP *condop;
2482     OP *op;
2483
2484     if (!falseop)
2485         return newLOGOP(OP_AND, 0, first, trueop);
2486     if (!trueop)
2487         return newLOGOP(OP_OR, 0, first, falseop);
2488
2489     scalarboolean(first);
2490     if (first->op_type == OP_CONST) {
2491         if (SvTRUE(((SVOP*)first)->op_sv)) {
2492             op_free(first);
2493             op_free(falseop);
2494             return trueop;
2495         }
2496         else {
2497             op_free(first);
2498             op_free(trueop);
2499             return falseop;
2500         }
2501     }
2502     else if (first->op_type == OP_WANTARRAY) {
2503         list(trueop);
2504         scalar(falseop);
2505     }
2506     Newz(1101, condop, 1, CONDOP);
2507
2508     condop->op_type = OP_COND_EXPR;
2509     condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2510     condop->op_first = first;
2511     condop->op_flags = flags | OPf_KIDS;
2512     condop->op_true = LINKLIST(trueop);
2513     condop->op_false = LINKLIST(falseop);
2514     condop->op_private = 1 | (flags >> 8);
2515
2516     /* establish postfix order */
2517     condop->op_next = LINKLIST(first);
2518     first->op_next = (OP*)condop;
2519
2520     first->op_sibling = trueop;
2521     trueop->op_sibling = falseop;
2522     op = newUNOP(OP_NULL, 0, (OP*)condop);
2523
2524     trueop->op_next = op;
2525     falseop->op_next = op;
2526
2527     return op;
2528 }
2529
2530 OP *
2531 newRANGE(flags, left, right)
2532 I32 flags;
2533 OP *left;
2534 OP *right;
2535 {
2536     CONDOP *condop;
2537     OP *flip;
2538     OP *flop;
2539     OP *op;
2540
2541     Newz(1101, condop, 1, CONDOP);
2542
2543     condop->op_type = OP_RANGE;
2544     condop->op_ppaddr = ppaddr[OP_RANGE];
2545     condop->op_first = left;
2546     condop->op_flags = OPf_KIDS;
2547     condop->op_true = LINKLIST(left);
2548     condop->op_false = LINKLIST(right);
2549     condop->op_private = 1 | (flags >> 8);
2550
2551     left->op_sibling = right;
2552
2553     condop->op_next = (OP*)condop;
2554     flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2555     flop = newUNOP(OP_FLOP, 0, flip);
2556     op = newUNOP(OP_NULL, 0, flop);
2557     linklist(flop);
2558
2559     left->op_next = flip;
2560     right->op_next = flop;
2561
2562     condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2563     sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2564     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2565     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2566
2567     flip->op_private =  left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2568     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2569
2570     flip->op_next = op;
2571     if (!flip->op_private || !flop->op_private)
2572         linklist(op);           /* blow off optimizer unless constant */
2573
2574     return op;
2575 }
2576
2577 OP *
2578 newLOOPOP(flags, debuggable, expr, block)
2579 I32 flags;
2580 I32 debuggable;
2581 OP *expr;
2582 OP *block;
2583 {
2584     OP* listop;
2585     OP* op;
2586     int once = block && block->op_flags & OPf_SPECIAL &&
2587       (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2588
2589     if (expr) {
2590         if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2591             return block;       /* do {} while 0 does once */
2592         else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
2593             expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr);
2594     }
2595
2596     listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2597     op = newLOGOP(OP_AND, 0, expr, listop);
2598
2599     ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2600
2601     if (once && op != listop)
2602         op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2603
2604     if (op == listop)
2605         op = newUNOP(OP_NULL, 0, op);   /* or do {} while 1 loses outer block */
2606
2607     op->op_flags |= flags;
2608     op = scope(op);
2609     op->op_flags |= OPf_SPECIAL;        /* suppress POPBLOCK curpm restoration*/
2610     return op;
2611 }
2612
2613 OP *
2614 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2615 I32 flags;
2616 I32 debuggable;
2617 LOOP *loop;
2618 OP *expr;
2619 OP *block;
2620 OP *cont;
2621 {
2622     OP *redo;
2623     OP *next = 0;
2624     OP *listop;
2625     OP *op;
2626     OP *condop;
2627
2628     if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2629         expr = newUNOP(OP_DEFINED, 0,
2630             newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2631     }
2632
2633     if (!block)
2634         block = newOP(OP_NULL, 0);
2635
2636     if (cont)
2637         next = LINKLIST(cont);
2638     if (expr)
2639         cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2640
2641     listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2642     redo = LINKLIST(listop);
2643
2644     if (expr) {
2645         op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2646         if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2647             op_free(expr);              /* oops, it's a while (0) */
2648             op_free((OP*)loop);
2649             return Nullop;              /* (listop already freed by newLOGOP) */
2650         }
2651         ((LISTOP*)listop)->op_last->op_next = condop = 
2652             (op == listop ? redo : LINKLIST(op));
2653         if (!next)
2654             next = condop;
2655     }
2656     else
2657         op = listop;
2658
2659     if (!loop) {
2660         Newz(1101,loop,1,LOOP);
2661         loop->op_type = OP_ENTERLOOP;
2662         loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2663         loop->op_private = 0;
2664         loop->op_next = (OP*)loop;
2665     }
2666
2667     op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2668
2669     loop->op_redoop = redo;
2670     loop->op_lastop = op;
2671
2672     if (next)
2673         loop->op_nextop = next;
2674     else
2675         loop->op_nextop = op;
2676
2677     op->op_flags |= flags;
2678     op->op_private |= (flags >> 8);
2679     return op;
2680 }
2681
2682 OP *
2683 #ifndef CAN_PROTOTYPE
2684 newFOROP(flags,label,forline,sv,expr,block,cont)
2685 I32 flags;
2686 char *label;
2687 line_t forline;
2688 OP* sv;
2689 OP* expr;
2690 OP*block;
2691 OP*cont;
2692 #else
2693 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2694 #endif /* CAN_PROTOTYPE */
2695 {
2696     LOOP *loop;
2697     int padoff = 0;
2698     I32 iterflags = 0;
2699
2700     copline = forline;
2701     if (sv) {
2702         if (sv->op_type == OP_RV2SV) {  /* symbol table variable */
2703             sv->op_type = OP_RV2GV;
2704             sv->op_ppaddr = ppaddr[OP_RV2GV];
2705         }
2706         else if (sv->op_type == OP_PADSV) { /* private variable */
2707             padoff = sv->op_targ;
2708             op_free(sv);
2709             sv = Nullop;
2710         }
2711         else
2712             croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2713     }
2714     else {
2715         sv = newGVOP(OP_GV, 0, defgv);
2716     }
2717     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2718         expr = scalar(ref(expr, OP_ITER));
2719         iterflags |= OPf_STACKED;
2720     }
2721     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2722         append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2723                     scalar(sv))));
2724     assert(!loop->op_next);
2725     Renew(loop, 1, LOOP);
2726     loop->op_targ = padoff;
2727     return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2728         newOP(OP_ITER, 0), block, cont));
2729 }
2730
2731 OP*
2732 newLOOPEX(type, label)
2733 I32 type;
2734 OP* label;
2735 {
2736     OP *op;
2737     if (type != OP_GOTO || label->op_type == OP_CONST) {
2738         op = newPVOP(type, 0, savepv(
2739                 label->op_type == OP_CONST
2740                     ? SvPVx(((SVOP*)label)->op_sv, na)
2741                     : "" ));
2742         op_free(label);
2743     }
2744     else {
2745         if (label->op_type == OP_ENTERSUB)
2746             label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2747         op = newUNOP(type, OPf_STACKED, label);
2748     }
2749     hints |= HINT_BLOCK_SCOPE;
2750     return op;
2751 }
2752
2753 void
2754 cv_undef(cv)
2755 CV *cv;
2756 {
2757     if (!CvXSUB(cv) && CvROOT(cv)) {
2758         if (CvDEPTH(cv))
2759             croak("Can't undef active subroutine");
2760         ENTER;
2761
2762         SAVESPTR(curpad);
2763         curpad = 0;
2764
2765         if (!CvCLONED(cv))
2766             op_free(CvROOT(cv));
2767         CvROOT(cv) = Nullop;
2768         LEAVE;
2769     }
2770     SvREFCNT_dec(CvGV(cv));
2771     CvGV(cv) = Nullgv;
2772     SvREFCNT_dec(CvOUTSIDE(cv));
2773     CvOUTSIDE(cv) = Nullcv;
2774     if (CvPADLIST(cv)) {
2775         I32 i = AvFILL(CvPADLIST(cv));
2776         while (i >= 0) {
2777             SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2778             if (svp)
2779                 SvREFCNT_dec(*svp);
2780         }
2781         SvREFCNT_dec((SV*)CvPADLIST(cv));
2782         CvPADLIST(cv) = Nullav;
2783     }
2784 }
2785
2786 #ifdef DEBUG_CLOSURES
2787 static void
2788 cv_dump(cv)
2789 CV* cv;
2790 {
2791     CV *outside = CvOUTSIDE(cv);
2792     AV* padlist = CvPADLIST(cv);
2793     AV* pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
2794     AV* pad = (AV*)*av_fetch(padlist, 1, FALSE);
2795     SV** pname = AvARRAY(pad_name);
2796     SV** ppad = AvARRAY(pad);
2797     I32 ix;
2798
2799     PerlIO_printf(Perl_debug_log, "\tCV=0x%p (%s), OUTSIDE=0x%p (%s)\n",
2800                   cv,
2801                   (CvANON(cv) ? "ANON"
2802                    : (cv == main_cv) ? "MAIN"
2803                    : CvUNIQUE(outside) ? "UNIQUE"
2804                    : CvGV(cv) ? GvNAME(CvGV(cv)) : "?mystery?"),
2805                   outside,
2806                   (!outside ? "null"
2807                    : CvANON(outside) ? "ANON"
2808                    : (outside == main_cv) ? "MAIN"
2809                    : CvUNIQUE(outside) ? "UNIQUE"
2810                    : CvGV(outside) ? GvNAME(CvGV(outside)) : "?mystery?"));
2811
2812     for (ix = 1; ix <= AvFILL(pad); ix++) {
2813         if (SvPOK(pname[ix]))
2814             PerlIO_printf(Perl_debug_log, "\t%4d. 0x%p (\"%s\" %ld-%ld)\n",
2815                           ix, ppad[ix], SvPVX(pname[ix]),
2816                           (long)I_32(SvNVX(pname[ix])),
2817                           (long)SvIVX(pname[ix]));
2818     }
2819 }
2820 #endif /* DEBUG_CLOSURES */
2821
2822 static CV *
2823 cv_clone2(proto, outside)
2824 CV* proto;
2825 CV* outside;
2826 {
2827     AV* av;
2828     I32 ix;
2829     AV* protopadlist = CvPADLIST(proto);
2830     AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
2831     AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
2832     SV** pname = AvARRAY(protopad_name);
2833     SV** ppad = AvARRAY(protopad);
2834     AV* comppadlist;
2835     CV* cv;
2836
2837     assert(!CvUNIQUE(proto));
2838
2839     ENTER;
2840     SAVESPTR(curpad);
2841     SAVESPTR(comppad);
2842     SAVESPTR(compcv);
2843
2844     cv = compcv = (CV*)NEWSV(1104,0);
2845     sv_upgrade((SV *)cv, SVt_PVCV);
2846     CvCLONED_on(cv);
2847     if (CvANON(proto))
2848         CvANON_on(cv);
2849
2850     CvFILEGV(cv)        = CvFILEGV(proto);
2851     CvGV(cv)            = GvREFCNT_inc(CvGV(proto));
2852     CvSTASH(cv)         = CvSTASH(proto);
2853     CvROOT(cv)          = CvROOT(proto);
2854     CvSTART(cv)         = CvSTART(proto);
2855     if (outside)
2856         CvOUTSIDE(cv)   = (CV*)SvREFCNT_inc(outside);
2857
2858     comppad = newAV();
2859
2860     comppadlist = newAV();
2861     AvREAL_off(comppadlist);
2862     av_store(comppadlist, 0, SvREFCNT_inc((SV*)protopad_name));
2863     av_store(comppadlist, 1, (SV*)comppad);
2864     CvPADLIST(cv) = comppadlist;
2865     av_fill(comppad, AvFILL(protopad));
2866     curpad = AvARRAY(comppad);
2867
2868     av = newAV();           /* will be @_ */
2869     av_extend(av, 0);
2870     av_store(comppad, 0, (SV*)av);
2871     AvFLAGS(av) = AVf_REIFY;
2872
2873     for (ix = AvFILL(protopad); ix > 0; ix--) {
2874         SV* sv;
2875         if (pname[ix] != &sv_undef) {
2876             char *name = SvPVX(pname[ix]);    /* XXX */
2877             if (SvFLAGS(pname[ix]) & SVf_FAKE) {   /* lexical from outside? */
2878                 I32 off = pad_findlex(name, ix, SvIVX(pname[ix]),
2879                                       CvOUTSIDE(cv), cxstack_ix);
2880                 if (!off)
2881                     curpad[ix] = SvREFCNT_inc(ppad[ix]);
2882                 else if (off != ix)
2883                     croak("panic: cv_clone: %s", name);
2884             }
2885             else {                              /* our own lexical */
2886                 if (*name == '&') {
2887                     /* anon code -- we'll come back for it */
2888                     sv = SvREFCNT_inc(ppad[ix]);
2889                 }
2890                 else if (*name == '@')
2891                     sv = (SV*)newAV();
2892                 else if (*name == '%')
2893                     sv = (SV*)newHV();
2894                 else
2895                     sv = NEWSV(0,0);
2896                 if (!SvPADBUSY(sv))
2897                     SvPADMY_on(sv);
2898                 curpad[ix] = sv;
2899             }
2900         }
2901         else {
2902             sv = NEWSV(0,0);
2903             SvPADTMP_on(sv);
2904             curpad[ix] = sv;
2905         }
2906     }
2907
2908     /* Now that vars are all in place, clone nested closures. */
2909
2910     for (ix = AvFILL(protopad); ix > 0; ix--) {
2911         if (pname[ix] != &sv_undef
2912             && !(SvFLAGS(pname[ix]) & SVf_FAKE)
2913             && *SvPVX(pname[ix]) == '&'
2914             && CvCLONE(ppad[ix]))
2915         {
2916             CV *kid = cv_clone2((CV*)ppad[ix], cv);
2917             SvREFCNT_dec(ppad[ix]);
2918             CvCLONE_on(kid);
2919             SvPADMY_on(kid);
2920             curpad[ix] = (SV*)kid;
2921         }
2922     }
2923
2924 #ifdef DEBUG_CLOSURES
2925     PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
2926     cv_dump(outside);
2927     PerlIO_printf(Perl_debug_log, "  from:\n");
2928     cv_dump(proto);
2929     PerlIO_printf(Perl_debug_log, "   to:\n");
2930     cv_dump(cv);
2931 #endif
2932
2933     LEAVE;
2934     return cv;
2935 }
2936
2937 CV *
2938 cv_clone(proto)
2939 CV* proto;
2940 {
2941     return cv_clone2(proto, CvOUTSIDE(proto));
2942 }
2943
2944 SV *
2945 cv_const_sv(cv)
2946 CV *cv;
2947 {
2948     OP *o;
2949     SV *sv = Nullsv;
2950     
2951     if(cv && SvPOK(cv) && !SvCUR(cv)) {
2952         for (o = CvSTART(cv); o; o = o->op_next) {
2953             OPCODE type = o->op_type;
2954         
2955             if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
2956                 continue;
2957             if (type == OP_LEAVESUB || type == OP_RETURN)
2958                 break;
2959             if (type != OP_CONST || sv)
2960                 return Nullsv;
2961
2962             sv = ((SVOP*)o)->op_sv;
2963         }
2964     }
2965     return sv;
2966 }
2967
2968 CV *
2969 newSUB(floor,op,proto,block)
2970 I32 floor;
2971 OP *op;
2972 OP *proto;
2973 OP *block;
2974 {
2975     register CV *cv;
2976     char *name = op ? SvPVx(cSVOP->op_sv, na) : "__ANON__";
2977     GV* gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV);
2978     AV* av;
2979     char *s;
2980     I32 ix;
2981
2982     if (op)
2983         sub_generation++;
2984     if (cv = GvCV(gv)) {
2985         if (GvCVGEN(gv))
2986             cv = 0;                     /* just a cached method */
2987         else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
2988             SV* const_sv = cv_const_sv(cv);
2989
2990             char *p = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
2991
2992             if((!proto != !SvPOK(cv)) || (p && strNE(SvPV((SV*)cv,na), p))) {
2993                 warn("Prototype mismatch: (%s) vs (%s)",
2994                         SvPOK(cv) ? SvPV((SV*)cv,na) : "none",
2995                         p ? p : "none");
2996             }
2997
2998             if ((const_sv || dowarn) && strNE(name, "BEGIN")) {/* already defined (or promised)? */
2999                 line_t oldline = curcop->cop_line;
3000
3001                 curcop->cop_line = copline;
3002                 warn(const_sv ? "Constant subroutine %s redefined"
3003                               : "Subroutine %s redefined",name);
3004                 curcop->cop_line = oldline;
3005             }
3006             SvREFCNT_dec(cv);
3007             cv = 0;
3008         }
3009     }
3010     if (cv) {                           /* must reuse cv if autoloaded */
3011         cv_undef(cv);
3012         CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3013         CvOUTSIDE(compcv) = 0;
3014         CvPADLIST(cv) = CvPADLIST(compcv);
3015         CvPADLIST(compcv) = 0;
3016         if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3017             CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3018         SvREFCNT_dec(compcv);
3019     }
3020     else {
3021         cv = compcv;
3022     }
3023     GvCV(gv) = cv;
3024     GvCVGEN(gv) = 0;
3025     CvFILEGV(cv) = curcop->cop_filegv;
3026     CvGV(cv) = GvREFCNT_inc(gv);
3027     CvSTASH(cv) = curstash;
3028
3029     if (proto) {
3030         char *p = SvPVx(((SVOP*)proto)->op_sv, na);
3031         sv_setpv((SV*)cv, p);
3032         op_free(proto);
3033     }
3034
3035     if (error_count) {
3036         op_free(block);
3037         block = Nullop;
3038     }
3039     if (!block) {
3040         CvROOT(cv) = 0;
3041         op_free(op);
3042         copline = NOLINE;
3043         LEAVE_SCOPE(floor);
3044         return cv;
3045     }
3046
3047     av = newAV();                       /* Will be @_ */
3048     av_extend(av, 0);
3049     av_store(comppad, 0, (SV*)av);
3050     AvFLAGS(av) = AVf_REIFY;
3051
3052     for (ix = AvFILL(comppad); ix > 0; ix--) {
3053         if (!SvPADMY(curpad[ix]))
3054             SvPADTMP_on(curpad[ix]);
3055     }
3056
3057     if (AvFILL(comppad_name) < AvFILL(comppad))
3058         av_store(comppad_name, AvFILL(comppad), Nullsv);
3059
3060     CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3061     CvSTART(cv) = LINKLIST(CvROOT(cv));
3062     CvROOT(cv)->op_next = 0;
3063     peep(CvSTART(cv));
3064     if (s = strrchr(name,':'))
3065         s++;
3066     else
3067         s = name;
3068     if (strEQ(s, "BEGIN") && !error_count) {
3069         line_t oldline = compiling.cop_line;
3070         SV *oldrs = rs;
3071
3072         ENTER;
3073         SAVESPTR(compiling.cop_filegv);
3074         SAVEI32(perldb);
3075         if (!beginav)
3076             beginav = newAV();
3077         av_push(beginav, (SV *)cv);
3078         DEBUG_x( dump_sub(gv) );
3079         rs = SvREFCNT_inc(nrs);
3080         SvREFCNT_inc(cv);
3081         calllist(beginav);
3082         if (GvCV(gv) == cv) {           /* Detach it. */
3083             SvREFCNT_dec(cv);
3084             GvCV(gv) = 0;               /* Was above calllist, why? IZ */
3085         }
3086         SvREFCNT_dec(rs);
3087         rs = oldrs;
3088         curcop = &compiling;
3089         curcop->cop_line = oldline;     /* might have recursed to yylex */
3090         LEAVE;
3091     }
3092     else if (strEQ(s, "END") && !error_count) {
3093         if (!endav)
3094             endav = newAV();
3095         av_unshift(endav, 1);
3096         av_store(endav, 0, SvREFCNT_inc(cv));
3097     }
3098     if (perldb && curstash != debstash) {
3099         SV *sv;
3100         SV *tmpstr = sv_newmortal();
3101         static GV *db_postponed;
3102         CV *cv;
3103         HV *hv;
3104
3105         sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), (long)subline);
3106         sv = newSVpv(buf,0);
3107         sv_catpv(sv,"-");
3108         sprintf(buf,"%ld",(long)curcop->cop_line);
3109         sv_catpv(sv,buf);
3110         gv_efullname3(tmpstr, gv, Nullch);
3111         hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3112         if (!db_postponed) {
3113             db_postponed = gv_fetchpv("DB::postponed", TRUE, SVt_PVHV);
3114         }
3115         hv = GvHVn(db_postponed);
3116         if (HvFILL(hv) >= 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3117             && (cv = GvCV(db_postponed))) {
3118             dSP;
3119             PUSHMARK(sp);
3120             XPUSHs(tmpstr);
3121             PUTBACK;
3122             perl_call_sv((SV*)cv, G_DISCARD);
3123         }
3124     }
3125     op_free(op);
3126     copline = NOLINE;
3127     LEAVE_SCOPE(floor);
3128     if (!op) {
3129         GvCV(gv) = 0;   /* Will remember in SVOP instead. */
3130         CvANON_on(cv);
3131     }
3132     return cv;
3133 }
3134
3135 #ifdef DEPRECATED
3136 CV *
3137 newXSUB(name, ix, subaddr, filename)
3138 char *name;
3139 I32 ix;
3140 I32 (*subaddr)();
3141 char *filename;
3142 {
3143     CV* cv = newXS(name, (void(*)())subaddr, filename);
3144     CvOLDSTYLE_on(cv);
3145     CvXSUBANY(cv).any_i32 = ix;
3146     return cv;
3147 }
3148 #endif
3149
3150 CV *
3151 newXS(name, subaddr, filename)
3152 char *name;
3153 void (*subaddr) _((CV*));
3154 char *filename;
3155 {
3156     register CV *cv;
3157     GV *gv = gv_fetchpv((name ? name : "__ANON__"), GV_ADDMULTI, SVt_PVCV);
3158     char *s;
3159
3160     if (name)
3161         sub_generation++;
3162     if (cv = GvCV(gv)) {
3163         if (GvCVGEN(gv))
3164             cv = 0;                     /* just a cached method */
3165         else if (CvROOT(cv) || CvXSUB(cv)) {    /* already defined? */
3166             if (dowarn) {
3167                 line_t oldline = curcop->cop_line;
3168
3169                 curcop->cop_line = copline;
3170                 warn("Subroutine %s redefined",name);
3171                 curcop->cop_line = oldline;
3172             }
3173             SvREFCNT_dec(cv);
3174             cv = 0;
3175         }
3176     }
3177     if (cv) {                           /* must reuse cv if autoloaded */
3178         assert(SvREFCNT(CvGV(cv)) > 1);
3179         SvREFCNT_dec(CvGV(cv));
3180     }
3181     else {
3182         cv = (CV*)NEWSV(1105,0);
3183         sv_upgrade((SV *)cv, SVt_PVCV);
3184     }
3185     GvCV(gv) = cv;
3186     CvGV(cv) = GvREFCNT_inc(gv);
3187     GvCVGEN(gv) = 0;
3188     CvFILEGV(cv) = gv_fetchfile(filename);
3189     CvXSUB(cv) = subaddr;
3190     if (!name)
3191         s = "__ANON__";
3192     else if (s = strrchr(name,':'))
3193         s++;
3194     else
3195         s = name;
3196     if (strEQ(s, "BEGIN")) {
3197         if (!beginav)
3198             beginav = newAV();
3199         av_push(beginav, SvREFCNT_inc(gv));
3200     }
3201     else if (strEQ(s, "END")) {
3202         if (!endav)
3203             endav = newAV();
3204         av_unshift(endav, 1);
3205         av_store(endav, 0, SvREFCNT_inc(gv));
3206     }
3207     if (!name) {
3208         GvCV(gv) = 0;   /* Will remember elsewhere instead. */
3209         CvANON_on(cv);
3210     }
3211     return cv;
3212 }
3213
3214 void
3215 newFORM(floor,op,block)
3216 I32 floor;
3217 OP *op;
3218 OP *block;
3219 {
3220     register CV *cv;
3221     char *name;
3222     GV *gv;
3223     I32 ix;
3224
3225     if (op)
3226         name = SvPVx(cSVOP->op_sv, na);
3227     else
3228         name = "STDOUT";
3229     gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3230     GvMULTI_on(gv);
3231     if (cv = GvFORM(gv)) {
3232         if (dowarn) {
3233             line_t oldline = curcop->cop_line;
3234
3235             curcop->cop_line = copline;
3236             warn("Format %s redefined",name);
3237             curcop->cop_line = oldline;
3238         }
3239         SvREFCNT_dec(cv);
3240     }
3241     cv = compcv;
3242     GvFORM(gv) = cv;
3243     CvGV(cv) = GvREFCNT_inc(gv);
3244     CvFILEGV(cv) = curcop->cop_filegv;
3245
3246     for (ix = AvFILL(comppad); ix > 0; ix--) {
3247         if (!SvPADMY(curpad[ix]))
3248             SvPADTMP_on(curpad[ix]);
3249     }
3250
3251     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3252     CvSTART(cv) = LINKLIST(CvROOT(cv));
3253     CvROOT(cv)->op_next = 0;
3254     peep(CvSTART(cv));
3255     FmLINES(cv) = 0;
3256     op_free(op);
3257     copline = NOLINE;
3258     LEAVE_SCOPE(floor);
3259 }
3260
3261 OP *
3262 newANONLIST(op)
3263 OP* op;
3264 {
3265     return newUNOP(OP_REFGEN, 0,
3266         mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
3267 }
3268
3269 OP *
3270 newANONHASH(op)
3271 OP* op;
3272 {
3273     return newUNOP(OP_REFGEN, 0,
3274         mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3275 }
3276
3277 OP *
3278 newANONSUB(floor, proto, block)
3279 I32 floor;
3280 OP *proto;
3281 OP *block;
3282 {
3283     return newUNOP(OP_REFGEN, 0,
3284         newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3285 }
3286
3287 OP *
3288 oopsAV(o)
3289 OP *o;
3290 {
3291     switch (o->op_type) {
3292     case OP_PADSV:
3293         o->op_type = OP_PADAV;
3294         o->op_ppaddr = ppaddr[OP_PADAV];
3295         return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3296         
3297     case OP_RV2SV:
3298         o->op_type = OP_RV2AV;
3299         o->op_ppaddr = ppaddr[OP_RV2AV];
3300         ref(o, OP_RV2AV);
3301         break;
3302
3303     default:
3304         warn("oops: oopsAV");
3305         break;
3306     }
3307     return o;
3308 }
3309
3310 OP *
3311 oopsHV(o)
3312 OP *o;
3313 {
3314     switch (o->op_type) {
3315     case OP_PADSV:
3316     case OP_PADAV:
3317         o->op_type = OP_PADHV;
3318         o->op_ppaddr = ppaddr[OP_PADHV];
3319         return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3320
3321     case OP_RV2SV:
3322     case OP_RV2AV:
3323         o->op_type = OP_RV2HV;
3324         o->op_ppaddr = ppaddr[OP_RV2HV];
3325         ref(o, OP_RV2HV);
3326         break;
3327
3328     default:
3329         warn("oops: oopsHV");
3330         break;
3331     }
3332     return o;
3333 }
3334
3335 OP *
3336 newAVREF(o)
3337 OP *o;
3338 {
3339     if (o->op_type == OP_PADANY) {
3340         o->op_type = OP_PADAV;
3341         o->op_ppaddr = ppaddr[OP_PADAV];
3342         return o;
3343     }
3344     return newUNOP(OP_RV2AV, 0, scalar(o));
3345 }
3346
3347 OP *
3348 newGVREF(type,o)
3349 I32 type;
3350 OP *o;
3351 {
3352     if (type == OP_MAPSTART)
3353         return newUNOP(OP_NULL, 0, o);
3354     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3355 }
3356
3357 OP *
3358 newHVREF(o)
3359 OP *o;
3360 {
3361     if (o->op_type == OP_PADANY) {
3362         o->op_type = OP_PADHV;
3363         o->op_ppaddr = ppaddr[OP_PADHV];
3364         return o;
3365     }
3366     return newUNOP(OP_RV2HV, 0, scalar(o));
3367 }
3368
3369 OP *
3370 oopsCV(o)
3371 OP *o;
3372 {
3373     croak("NOT IMPL LINE %d",__LINE__);
3374     /* STUB */
3375     return o;
3376 }
3377
3378 OP *
3379 newCVREF(flags, o)
3380 I32 flags;
3381 OP *o;
3382 {
3383     return newUNOP(OP_RV2CV, flags, scalar(o));
3384 }
3385
3386 OP *
3387 newSVREF(o)
3388 OP *o;
3389 {
3390     if (o->op_type == OP_PADANY) {
3391         o->op_type = OP_PADSV;
3392         o->op_ppaddr = ppaddr[OP_PADSV];
3393         return o;
3394     }
3395     return newUNOP(OP_RV2SV, 0, scalar(o));
3396 }
3397
3398 /* Check routines. */
3399
3400 OP *
3401 ck_anoncode(op)
3402 OP *op;
3403 {
3404     PADOFFSET ix;
3405     SV* name;
3406
3407     name = NEWSV(1106,0);
3408     sv_upgrade(name, SVt_PVNV);
3409     sv_setpvn(name, "&", 1);
3410     SvIVX(name) = -1;
3411     SvNVX(name) = 1;
3412     ix = pad_alloc(op->op_type, SVs_PADMY);
3413     av_store(comppad_name, ix, name);
3414     av_store(comppad, ix, cSVOP->op_sv);
3415     SvPADMY_on(cSVOP->op_sv);
3416     cSVOP->op_sv = Nullsv;
3417     cSVOP->op_targ = ix;
3418     return op;
3419 }
3420
3421 OP *
3422 ck_bitop(op)
3423 OP *op;
3424 {
3425     op->op_private = hints;
3426     return op;
3427 }
3428
3429 OP *
3430 ck_concat(op)
3431 OP *op;
3432 {
3433     if (cUNOP->op_first->op_type == OP_CONCAT)
3434         op->op_flags |= OPf_STACKED;
3435     return op;
3436 }
3437
3438 OP *
3439 ck_spair(op)
3440 OP *op;
3441 {
3442     if (op->op_flags & OPf_KIDS) {
3443         OP* newop;
3444         OP* kid;
3445         op = modkids(ck_fun(op), op->op_type);
3446         kid = cUNOP->op_first;
3447         newop = kUNOP->op_first->op_sibling;
3448         if (newop &&
3449             (newop->op_sibling ||
3450              !(opargs[newop->op_type] & OA_RETSCALAR) ||
3451              newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3452              newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3453             
3454             return op;
3455         }
3456         op_free(kUNOP->op_first);
3457         kUNOP->op_first = newop;
3458     }
3459     op->op_ppaddr = ppaddr[++op->op_type];
3460     return ck_fun(op);
3461 }
3462
3463 OP *
3464 ck_delete(op)
3465 OP *op;
3466 {
3467     op = ck_fun(op);
3468     op->op_private = 0;
3469     if (op->op_flags & OPf_KIDS) {
3470         OP *kid = cUNOP->op_first;
3471         if (kid->op_type == OP_HSLICE)
3472             op->op_private |= OPpSLICE;
3473         else if (kid->op_type != OP_HELEM)
3474             croak("%s argument is not a HASH element or slice",
3475                   op_desc[op->op_type]);
3476         null(kid);
3477     }
3478     return op;
3479 }
3480
3481 OP *
3482 ck_eof(op)
3483 OP *op;
3484 {
3485     I32 type = op->op_type;
3486
3487     if (op->op_flags & OPf_KIDS) {
3488         if (cLISTOP->op_first->op_type == OP_STUB) {
3489             op_free(op);
3490             op = newUNOP(type, OPf_SPECIAL,
3491                 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
3492         }
3493         return ck_fun(op);
3494     }
3495     return op;
3496 }
3497
3498 OP *
3499 ck_eval(op)
3500 OP *op;
3501 {
3502     hints |= HINT_BLOCK_SCOPE;
3503     if (op->op_flags & OPf_KIDS) {
3504         SVOP *kid = (SVOP*)cUNOP->op_first;
3505
3506         if (!kid) {
3507             op->op_flags &= ~OPf_KIDS;
3508             null(op);
3509         }
3510         else if (kid->op_type == OP_LINESEQ) {
3511             LOGOP *enter;
3512
3513             kid->op_next = op->op_next;
3514             cUNOP->op_first = 0;
3515             op_free(op);
3516
3517             Newz(1101, enter, 1, LOGOP);
3518             enter->op_type = OP_ENTERTRY;
3519             enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3520             enter->op_private = 0;
3521
3522             /* establish postfix order */
3523             enter->op_next = (OP*)enter;
3524
3525             op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3526             op->op_type = OP_LEAVETRY;
3527             op->op_ppaddr = ppaddr[OP_LEAVETRY];
3528             enter->op_other = op;
3529             return op;
3530         }
3531     }
3532     else {
3533         op_free(op);
3534         op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3535     }
3536     op->op_targ = (PADOFFSET)hints;
3537     return op;
3538 }
3539
3540 OP *
3541 ck_exec(op)
3542 OP *op;
3543 {
3544     OP *kid;
3545     if (op->op_flags & OPf_STACKED) {
3546         op = ck_fun(op);
3547         kid = cUNOP->op_first->op_sibling;
3548         if (kid->op_type == OP_RV2GV)
3549             null(kid);
3550     }
3551     else
3552         op = listkids(op);
3553     return op;
3554 }
3555
3556 OP *
3557 ck_exists(op)
3558 OP *op;
3559 {
3560     op = ck_fun(op);
3561     if (op->op_flags & OPf_KIDS) {
3562         OP *kid = cUNOP->op_first;
3563         if (kid->op_type != OP_HELEM)
3564             croak("%s argument is not a HASH element", op_desc[op->op_type]);
3565         null(kid);
3566     }
3567     return op;
3568 }
3569
3570 OP *
3571 ck_gvconst(o)
3572 register OP *o;
3573 {
3574     o = fold_constants(o);
3575     if (o->op_type == OP_CONST)
3576         o->op_type = OP_GV;
3577     return o;
3578 }
3579
3580 OP *
3581 ck_rvconst(op)
3582 register OP *op;
3583 {
3584     SVOP *kid = (SVOP*)cUNOP->op_first;
3585
3586     op->op_private |= (hints & HINT_STRICT_REFS);
3587     if (kid->op_type == OP_CONST) {
3588         int iscv = (op->op_type==OP_RV2CV)*2;
3589         GV *gv = 0;
3590         kid->op_type = OP_GV;
3591         for (gv = 0; !gv; iscv++) {
3592             /*
3593              * This is a little tricky.  We only want to add the symbol if we
3594              * didn't add it in the lexer.  Otherwise we get duplicate strict
3595              * warnings.  But if we didn't add it in the lexer, we must at
3596              * least pretend like we wanted to add it even if it existed before,
3597              * or we get possible typo warnings.  OPpCONST_ENTERED says
3598              * whether the lexer already added THIS instance of this symbol.
3599              */
3600             gv = gv_fetchpv(SvPVx(kid->op_sv, na),
3601                 iscv | !(kid->op_private & OPpCONST_ENTERED),
3602                 iscv
3603                     ? SVt_PVCV
3604                     : op->op_type == OP_RV2SV
3605                         ? SVt_PV
3606                         : op->op_type == OP_RV2AV
3607                             ? SVt_PVAV
3608                             : op->op_type == OP_RV2HV
3609                                 ? SVt_PVHV
3610                                 : SVt_PVGV);
3611         }
3612         SvREFCNT_dec(kid->op_sv);
3613         kid->op_sv = SvREFCNT_inc(gv);
3614     }
3615     return op;
3616 }
3617
3618 OP *
3619 ck_ftst(op)
3620 OP *op;
3621 {
3622     I32 type = op->op_type;
3623
3624     if (op->op_flags & OPf_REF)
3625         return op;
3626
3627     if (op->op_flags & OPf_KIDS) {
3628         SVOP *kid = (SVOP*)cUNOP->op_first;
3629
3630         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3631             OP *newop = newGVOP(type, OPf_REF,
3632                 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3633             op_free(op);
3634             return newop;
3635         }
3636     }
3637     else {
3638         op_free(op);
3639         if (type == OP_FTTTY)
3640             return newGVOP(type, OPf_REF, gv_fetchpv("main'STDIN", TRUE,
3641                                 SVt_PVIO));
3642         else
3643             return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3644     }
3645     return op;
3646 }
3647
3648 OP *
3649 ck_fun(op)
3650 OP *op;
3651 {
3652     register OP *kid;
3653     OP **tokid;
3654     OP *sibl;
3655     I32 numargs = 0;
3656     int type = op->op_type;
3657     register I32 oa = opargs[type] >> OASHIFT;
3658     
3659     if (op->op_flags & OPf_STACKED) {
3660         if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
3661             oa &= ~OA_OPTIONAL;
3662         else
3663             return no_fh_allowed(op);
3664     }
3665
3666     if (op->op_flags & OPf_KIDS) {
3667         tokid = &cLISTOP->op_first;
3668         kid = cLISTOP->op_first;
3669         if (kid->op_type == OP_PUSHMARK ||
3670             kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
3671         {
3672             tokid = &kid->op_sibling;
3673             kid = kid->op_sibling;
3674         }
3675         if (!kid && opargs[type] & OA_DEFGV)
3676             *tokid = kid = newSVREF(newGVOP(OP_GV, 0, defgv));
3677
3678         while (oa && kid) {
3679             numargs++;
3680             sibl = kid->op_sibling;
3681             switch (oa & 7) {
3682             case OA_SCALAR:
3683                 scalar(kid);
3684                 break;
3685             case OA_LIST:
3686                 if (oa < 16) {
3687                     kid = 0;
3688                     continue;
3689                 }
3690                 else
3691                     list(kid);
3692                 break;
3693             case OA_AVREF:
3694                 if (kid->op_type == OP_CONST &&
3695                   (kid->op_private & OPpCONST_BARE)) {
3696                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3697                     OP *newop = newAVREF(newGVOP(OP_GV, 0,
3698                         gv_fetchpv(name, TRUE, SVt_PVAV) ));
3699                     if (dowarn)
3700                         warn("Array @%s missing the @ in argument %d of %s()",
3701                             name, numargs, op_desc[type]);
3702                     op_free(kid);
3703                     kid = newop;
3704                     kid->op_sibling = sibl;
3705                     *tokid = kid;
3706                 }
3707                 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
3708                     bad_type(numargs, "array", op_desc[op->op_type], kid);
3709                 mod(kid, type);
3710                 break;
3711             case OA_HVREF:
3712                 if (kid->op_type == OP_CONST &&
3713                   (kid->op_private & OPpCONST_BARE)) {
3714                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3715                     OP *newop = newHVREF(newGVOP(OP_GV, 0,
3716                         gv_fetchpv(name, TRUE, SVt_PVHV) ));
3717                     if (dowarn)
3718                         warn("Hash %%%s missing the %% in argument %d of %s()",
3719                             name, numargs, op_desc[type]);
3720                     op_free(kid);
3721                     kid = newop;
3722                     kid->op_sibling = sibl;
3723                     *tokid = kid;
3724                 }
3725                 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
3726                     bad_type(numargs, "hash", op_desc[op->op_type], kid);
3727                 mod(kid, type);
3728                 break;
3729             case OA_CVREF:
3730                 {
3731                     OP *newop = newUNOP(OP_NULL, 0, kid);
3732                     kid->op_sibling = 0;
3733                     linklist(kid);
3734                     newop->op_next = newop;
3735                     kid = newop;
3736                     kid->op_sibling = sibl;
3737                     *tokid = kid;
3738                 }
3739                 break;
3740             case OA_FILEREF:
3741                 if (kid->op_type != OP_GV) {
3742                     if (kid->op_type == OP_CONST &&
3743                       (kid->op_private & OPpCONST_BARE)) {
3744                         OP *newop = newGVOP(OP_GV, 0,
3745                             gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3746                                         SVt_PVIO) );
3747                         op_free(kid);
3748                         kid = newop;
3749                     }
3750                     else {
3751                         kid->op_sibling = 0;
3752                         kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3753                     }
3754                     kid->op_sibling = sibl;
3755                     *tokid = kid;
3756                 }
3757                 scalar(kid);
3758                 break;
3759             case OA_SCALARREF:
3760                 mod(scalar(kid), type);
3761                 break;
3762             }
3763             oa >>= 4;
3764             tokid = &kid->op_sibling;
3765             kid = kid->op_sibling;
3766         }
3767         op->op_private |= numargs;
3768         if (kid)
3769             return too_many_arguments(op,op_desc[op->op_type]);
3770         listkids(op);
3771     }
3772     else if (opargs[type] & OA_DEFGV) {
3773         op_free(op);
3774         return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3775     }
3776
3777     if (oa) {
3778         while (oa & OA_OPTIONAL)
3779             oa >>= 4;
3780         if (oa && oa != OA_LIST)
3781             return too_few_arguments(op,op_desc[op->op_type]);
3782     }
3783     return op;
3784 }
3785
3786 OP *
3787 ck_glob(op)
3788 OP *op;
3789 {
3790     GV *gv = gv_fetchpv("glob", FALSE, SVt_PVCV);
3791
3792     if (gv && GvIMPORTED_CV(gv)) {
3793         op->op_type = OP_LIST;
3794         op->op_ppaddr = ppaddr[OP_LIST];
3795         op = newUNOP(OP_ENTERSUB, OPf_STACKED,
3796                      append_elem(OP_LIST, op, 
3797                                  scalar(newUNOP(OP_RV2CV, 0,
3798                                                 newGVOP(OP_GV, 0, gv)))));
3799         return ck_subr(op);
3800     }
3801     gv = newGVgen("main");
3802     gv_IOadd(gv);
3803     append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3804     scalarkids(op);
3805     return ck_fun(op);
3806 }
3807
3808 OP *
3809 ck_grep(op)
3810 OP *op;
3811 {
3812     LOGOP *gwop;
3813     OP *kid;
3814     OPCODE type = op->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
3815
3816     op->op_ppaddr = ppaddr[OP_GREPSTART];
3817     Newz(1101, gwop, 1, LOGOP);
3818     
3819     if (op->op_flags & OPf_STACKED) {
3820         OP* k;
3821         op = ck_sort(op);
3822         kid = cLISTOP->op_first->op_sibling;
3823         for (k = cLISTOP->op_first->op_sibling->op_next; k; k = k->op_next) {
3824             kid = k;
3825         }
3826         kid->op_next = (OP*)gwop;
3827         op->op_flags &= ~OPf_STACKED;
3828     }
3829     kid = cLISTOP->op_first->op_sibling;
3830     if (type == OP_MAPWHILE)
3831         list(kid);
3832     else
3833         scalar(kid);
3834     op = ck_fun(op);
3835     if (error_count)
3836         return op;
3837     kid = cLISTOP->op_first->op_sibling; 
3838     if (kid->op_type != OP_NULL)
3839         croak("panic: ck_grep");
3840     kid = kUNOP->op_first;
3841
3842     gwop->op_type = type;
3843     gwop->op_ppaddr = ppaddr[type];
3844     gwop->op_first = listkids(op);
3845     gwop->op_flags |= OPf_KIDS;
3846     gwop->op_private = 1;
3847     gwop->op_other = LINKLIST(kid);
3848     gwop->op_targ = pad_alloc(type, SVs_PADTMP);
3849     kid->op_next = (OP*)gwop;
3850
3851     kid = cLISTOP->op_first->op_sibling;
3852     if (!kid || !kid->op_sibling)
3853         return too_few_arguments(op,op_desc[op->op_type]);
3854     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
3855         mod(kid, OP_GREPSTART);
3856
3857     return (OP*)gwop;
3858 }
3859
3860 OP *
3861 ck_index(op)
3862 OP *op;
3863 {
3864     if (op->op_flags & OPf_KIDS) {
3865         OP *kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
3866         if (kid && kid->op_type == OP_CONST)
3867             fbm_compile(((SVOP*)kid)->op_sv);
3868     }
3869     return ck_fun(op);
3870 }
3871
3872 OP *
3873 ck_lengthconst(op)
3874 OP *op;
3875 {
3876     /* XXX length optimization goes here */
3877     return ck_fun(op);
3878 }
3879
3880 OP *
3881 ck_lfun(op)
3882 OP *op;
3883 {
3884     return modkids(ck_fun(op), op->op_type);
3885 }
3886
3887 OP *
3888 ck_rfun(op)
3889 OP *op;
3890 {
3891     return refkids(ck_fun(op), op->op_type);
3892 }
3893
3894 OP *
3895 ck_listiob(op)
3896 OP *op;
3897 {
3898     register OP *kid;
3899     
3900     kid = cLISTOP->op_first;
3901     if (!kid) {
3902         op = force_list(op);
3903         kid = cLISTOP->op_first;
3904     }
3905     if (kid->op_type == OP_PUSHMARK)
3906         kid = kid->op_sibling;
3907     if (kid && op->op_flags & OPf_STACKED)
3908         kid = kid->op_sibling;
3909     else if (kid && !kid->op_sibling) {         /* print HANDLE; */
3910         if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
3911             op->op_flags |= OPf_STACKED;        /* make it a filehandle */
3912             kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
3913             cLISTOP->op_first->op_sibling = kid;
3914             cLISTOP->op_last = kid;
3915             kid = kid->op_sibling;
3916         }
3917     }
3918         
3919     if (!kid)
3920         append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3921
3922     op = listkids(op);
3923
3924     op->op_private = 0;
3925 #ifdef USE_LOCALE
3926     if (hints & HINT_LOCALE)
3927         op->op_private |= OPpLOCALE;
3928 #endif
3929
3930     return op;
3931 }
3932
3933 OP *
3934 ck_fun_locale(op)
3935 OP *op;
3936 {
3937     op = ck_fun(op);
3938
3939     op->op_private = 0;
3940 #ifdef USE_LOCALE
3941     if (hints & HINT_LOCALE)
3942         op->op_private |= OPpLOCALE;
3943 #endif
3944
3945     return op;
3946 }
3947
3948 OP *
3949 ck_scmp(op)
3950 OP *op;
3951 {
3952     op->op_private = 0;
3953 #ifdef USE_LOCALE
3954     if (hints & HINT_LOCALE)
3955         op->op_private |= OPpLOCALE;
3956 #endif
3957
3958     return op;
3959 }
3960
3961 OP *
3962 ck_match(op)
3963 OP *op;
3964 {
3965     cPMOP->op_pmflags |= PMf_RUNTIME;
3966     cPMOP->op_pmpermflags |= PMf_RUNTIME;
3967     return op;
3968 }
3969
3970 OP *
3971 ck_null(op)
3972 OP *op;
3973 {
3974     return op;
3975 }
3976
3977 OP *
3978 ck_repeat(op)
3979 OP *op;
3980 {
3981     if (cBINOP->op_first->op_flags & OPf_PARENS) {
3982         op->op_private |= OPpREPEAT_DOLIST;
3983         cBINOP->op_first = force_list(cBINOP->op_first);
3984     }
3985     else
3986         scalar(op);
3987     return op;
3988 }
3989
3990 OP *
3991 ck_require(op)
3992 OP *op;
3993 {
3994     if (op->op_flags & OPf_KIDS) {      /* Shall we supply missing .pm? */
3995         SVOP *kid = (SVOP*)cUNOP->op_first;
3996
3997         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3998             char *s;
3999             for (s = SvPVX(kid->op_sv); *s; s++) {
4000                 if (*s == ':' && s[1] == ':') {
4001                     *s = '/';
4002                     Move(s+2, s+1, strlen(s+2)+1, char);
4003                     --SvCUR(kid->op_sv);
4004                 }
4005             }
4006             sv_catpvn(kid->op_sv, ".pm", 3);
4007         }
4008     }
4009     return ck_fun(op);
4010 }
4011
4012 OP *
4013 ck_retarget(op)
4014 OP *op;
4015 {
4016     croak("NOT IMPL LINE %d",__LINE__);
4017     /* STUB */
4018     return op;
4019 }
4020
4021 OP *
4022 ck_select(op)
4023 OP *op;
4024 {
4025     OP* kid;
4026     if (op->op_flags & OPf_KIDS) {
4027         kid = cLISTOP->op_first->op_sibling;    /* get past pushmark */
4028         if (kid && kid->op_sibling) {
4029             op->op_type = OP_SSELECT;
4030             op->op_ppaddr = ppaddr[OP_SSELECT];
4031             op = ck_fun(op);
4032             return fold_constants(op);
4033         }
4034     }
4035     op = ck_fun(op);
4036     kid = cLISTOP->op_first->op_sibling;    /* get past pushmark */
4037     if (kid && kid->op_type == OP_RV2GV)
4038         kid->op_private &= ~HINT_STRICT_REFS;
4039     return op;
4040 }
4041
4042 OP *
4043 ck_shift(op)
4044 OP *op;
4045 {
4046     I32 type = op->op_type;
4047
4048     if (!(op->op_flags & OPf_KIDS)) {
4049         op_free(op);
4050         return newUNOP(type, 0,
4051             scalar(newUNOP(OP_RV2AV, 0,
4052                 scalar(newGVOP(OP_GV, 0, subline 
4053                                ? defgv 
4054                                : gv_fetchpv("ARGV", TRUE, SVt_PVAV) )))));
4055     }
4056     return scalar(modkids(ck_fun(op), type));
4057 }
4058
4059 OP *
4060 ck_sort(op)
4061 OP *op;
4062 {
4063     op->op_private = 0;
4064 #ifdef USE_LOCALE
4065     if (hints & HINT_LOCALE)
4066         op->op_private |= OPpLOCALE;
4067 #endif
4068
4069     if (op->op_flags & OPf_STACKED) {
4070         OP *kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
4071         OP *k;
4072         kid = kUNOP->op_first;                          /* get past rv2gv */
4073
4074         if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4075             linklist(kid);
4076             if (kid->op_type == OP_SCOPE) {
4077                 k = kid->op_next;
4078                 kid->op_next = 0;
4079             }
4080             else if (kid->op_type == OP_LEAVE) {
4081                 if (op->op_type == OP_SORT) {
4082                     null(kid);                  /* wipe out leave */
4083                     kid->op_next = kid;
4084
4085                     for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4086                         if (k->op_next == kid)
4087                             k->op_next = 0;
4088                     }
4089                 }
4090                 else
4091                     kid->op_next = 0;           /* just disconnect the leave */
4092                 k = kLISTOP->op_first;
4093             }
4094             peep(k);
4095
4096             kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
4097             null(kid);                                  /* wipe out rv2gv */
4098             if (op->op_type == OP_SORT)
4099                 kid->op_next = kid;
4100             else
4101                 kid->op_next = k;
4102             op->op_flags |= OPf_SPECIAL;
4103         }
4104     }
4105
4106     return op;
4107 }
4108
4109 OP *
4110 ck_split(op)
4111 OP *op;
4112 {
4113     register OP *kid;
4114     PMOP* pm;
4115     
4116     if (op->op_flags & OPf_STACKED)
4117         return no_fh_allowed(op);
4118
4119     kid = cLISTOP->op_first;
4120     if (kid->op_type != OP_NULL)
4121         croak("panic: ck_split");
4122     kid = kid->op_sibling;
4123     op_free(cLISTOP->op_first);
4124     cLISTOP->op_first = kid;
4125     if (!kid) {
4126         cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4127         cLISTOP->op_last = kid; /* There was only one element previously */
4128     }
4129
4130     if (kid->op_type != OP_MATCH) {
4131         OP *sibl = kid->op_sibling;
4132         kid->op_sibling = 0;
4133         kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4134         if (cLISTOP->op_first == cLISTOP->op_last)
4135             cLISTOP->op_last = kid;
4136         cLISTOP->op_first = kid;
4137         kid->op_sibling = sibl;
4138     }
4139     pm = (PMOP*)kid;
4140     if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
4141         SvREFCNT_dec(pm->op_pmshort);   /* can't use substring to optimize */
4142         pm->op_pmshort = 0;
4143     }
4144
4145     kid->op_type = OP_PUSHRE;
4146     kid->op_ppaddr = ppaddr[OP_PUSHRE];
4147     scalar(kid);
4148
4149     if (!kid->op_sibling)
4150         append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
4151
4152     kid = kid->op_sibling;
4153     scalar(kid);
4154
4155     if (!kid->op_sibling)
4156         append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
4157
4158     kid = kid->op_sibling;
4159     scalar(kid);
4160
4161     if (kid->op_sibling)
4162         return too_many_arguments(op,op_desc[op->op_type]);
4163
4164     return op;
4165 }
4166
4167 OP *
4168 ck_subr(op)
4169 OP *op;
4170 {
4171     OP *prev = ((cUNOP->op_first->op_sibling)
4172              ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first;
4173     OP *o = prev->op_sibling;
4174     OP *cvop;
4175     char *proto = 0;
4176     CV *cv = 0;
4177     int optional = 0;
4178     I32 arg = 0;
4179
4180     for (cvop = o; cvop->op_sibling; cvop = cvop->op_sibling) ;
4181     if (cvop->op_type == OP_RV2CV) {
4182         SVOP* tmpop;
4183         op->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4184         null(cvop);             /* disable rv2cv */
4185         tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4186         if (tmpop->op_type == OP_GV) {
4187             cv = GvCV(tmpop->op_sv);
4188             if (cv && SvPOK(cv) && !(op->op_private & OPpENTERSUB_AMPER))
4189                 proto = SvPV((SV*)cv,na);
4190         }
4191     }
4192     op->op_private |= (hints & HINT_STRICT_REFS);
4193     if (perldb && curstash != debstash)
4194         op->op_private |= OPpENTERSUB_DB;
4195     while (o != cvop) {
4196         if (proto) {
4197             switch (*proto) {
4198             case '\0':
4199                 return too_many_arguments(op, CvNAME(cv));
4200             case ';':
4201                 optional = 1;
4202                 proto++;
4203                 continue;
4204             case '$':
4205                 proto++;
4206                 arg++;
4207                 scalar(o);
4208                 break;
4209             case '%':
4210             case '@':
4211                 list(o);
4212                 arg++;
4213                 break;
4214             case '&':
4215                 proto++;
4216                 arg++;
4217                 if (o->op_type != OP_REFGEN && o->op_type != OP_UNDEF)
4218                     bad_type(arg, "block", CvNAME(cv), o);
4219                 break;
4220             case '*':
4221                 proto++;
4222                 arg++;
4223                 if (o->op_type == OP_RV2GV)
4224                     goto wrapref;
4225                 {
4226                     OP* kid = o;
4227                     o = newUNOP(OP_RV2GV, 0, kid);
4228                     o->op_sibling = kid->op_sibling;
4229                     kid->op_sibling = 0;
4230                     prev->op_sibling = o;
4231                 }
4232                 goto wrapref;
4233             case '\\':
4234                 proto++;
4235                 arg++;
4236                 switch (*proto++) {
4237                 case '*':
4238                     if (o->op_type != OP_RV2GV)
4239                         bad_type(arg, "symbol", CvNAME(cv), o);
4240                     goto wrapref;
4241                 case '&':
4242                     if (o->op_type != OP_RV2CV)
4243                         bad_type(arg, "sub", CvNAME(cv), o);
4244                     goto wrapref;
4245                 case '$':
4246                     if (o->op_type != OP_RV2SV && o->op_type != OP_PADSV)
4247                         bad_type(arg, "scalar", CvNAME(cv), o);
4248                     goto wrapref;
4249                 case '@':
4250                     if (o->op_type != OP_RV2AV && o->op_type != OP_PADAV)
4251                         bad_type(arg, "array", CvNAME(cv), o);
4252                     goto wrapref;
4253                 case '%':
4254                     if (o->op_type != OP_RV2HV && o->op_type != OP_PADHV)
4255                         bad_type(arg, "hash", CvNAME(cv), o);
4256                   wrapref:
4257                     {
4258                         OP* kid = o;
4259                         o = newUNOP(OP_REFGEN, 0, kid);
4260                         o->op_sibling = kid->op_sibling;
4261                         kid->op_sibling = 0;
4262                         prev->op_sibling = o;
4263                     }
4264                     break;
4265                 default: goto oops;
4266                 }
4267                 break;
4268             case ' ':
4269                 proto++;
4270                 continue;
4271             default:
4272               oops:
4273                 croak("Malformed prototype for %s: %s",
4274                         CvNAME(cv),SvPV((SV*)cv,na));
4275             }
4276         }
4277         else
4278             list(o);
4279         mod(o, OP_ENTERSUB);
4280         prev = o;
4281         o = o->op_sibling;
4282     }
4283     if (proto && !optional && *proto == '$')
4284         return too_few_arguments(op, CvNAME(cv));
4285     return op;
4286 }
4287
4288 OP *
4289 ck_svconst(op)
4290 OP *op;
4291 {
4292     SvREADONLY_on(cSVOP->op_sv);
4293     return op;
4294 }
4295
4296 OP *
4297 ck_trunc(op)
4298 OP *op;
4299 {
4300     if (op->op_flags & OPf_KIDS) {
4301         SVOP *kid = (SVOP*)cUNOP->op_first;
4302
4303         if (kid->op_type == OP_NULL)
4304             kid = (SVOP*)kid->op_sibling;
4305         if (kid &&
4306           kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4307             op->op_flags |= OPf_SPECIAL;
4308     }
4309     return ck_fun(op);
4310 }
4311
4312 /* A peephole optimizer.  We visit the ops in the order they're to execute. */
4313
4314 void
4315 peep(o)
4316 register OP* o;
4317 {
4318     register OP* oldop = 0;
4319     if (!o || o->op_seq)
4320         return;
4321     ENTER;
4322     SAVESPTR(op);
4323     SAVESPTR(curcop);
4324     for (; o; o = o->op_next) {
4325         if (o->op_seq)
4326             break;
4327         if (!op_seqmax)
4328             op_seqmax++;
4329         op = o;
4330         switch (o->op_type) {
4331         case OP_NEXTSTATE:
4332         case OP_DBSTATE:
4333             curcop = ((COP*)o);         /* for warnings */
4334             o->op_seq = op_seqmax++;
4335             break;
4336
4337         case OP_CONCAT:
4338         case OP_CONST:
4339         case OP_JOIN:
4340         case OP_UC:
4341         case OP_UCFIRST:
4342         case OP_LC:
4343         case OP_LCFIRST:
4344         case OP_QUOTEMETA:
4345             if (o->op_next->op_type == OP_STRINGIFY)
4346                 null(o->op_next);
4347             o->op_seq = op_seqmax++;
4348             break;
4349         case OP_STUB:
4350             if ((o->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
4351                 o->op_seq = op_seqmax++;
4352                 break;  /* Scalar stub must produce undef.  List stub is noop */
4353             }
4354             goto nothin;
4355         case OP_NULL:
4356             if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4357                 curcop = ((COP*)op);
4358             goto nothin;
4359         case OP_SCALAR:
4360         case OP_LINESEQ:
4361         case OP_SCOPE:
4362           nothin:
4363             if (oldop && o->op_next) {
4364                 oldop->op_next = o->op_next;
4365                 continue;
4366             }
4367             o->op_seq = op_seqmax++;
4368             break;
4369
4370         case OP_GV:
4371             if (o->op_next->op_type == OP_RV2SV) {
4372                 if (!(o->op_next->op_private & OPpDEREF)) {
4373                     null(o->op_next);
4374                     o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4375                     o->op_next = o->op_next->op_next;
4376                     o->op_type = OP_GVSV;
4377                     o->op_ppaddr = ppaddr[OP_GVSV];
4378                 }
4379             }
4380             else if (o->op_next->op_type == OP_RV2AV) {
4381                 OP* pop = o->op_next->op_next;
4382                 IV i;
4383                 if (pop->op_type == OP_CONST &&
4384                     (op = pop->op_next) &&
4385                     pop->op_next->op_type == OP_AELEM &&
4386                     !(pop->op_next->op_private & (OPpDEREF|OPpLVAL_INTRO)) &&
4387                     (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4388                                 <= 255 &&
4389                     i >= 0)
4390                 {
4391                     SvREFCNT_dec(((SVOP*)pop)->op_sv);
4392                     null(o->op_next);
4393                     null(pop->op_next);
4394                     null(pop);
4395                     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4396                     o->op_next = pop->op_next->op_next;
4397                     o->op_type = OP_AELEMFAST;
4398                     o->op_ppaddr = ppaddr[OP_AELEMFAST];
4399                     o->op_private = (U8)i;
4400                     GvAVn((GV*)(((SVOP*)o)->op_sv));
4401                 }
4402             }
4403             o->op_seq = op_seqmax++;
4404             break;
4405
4406         case OP_MAPWHILE:
4407         case OP_GREPWHILE:
4408         case OP_AND:
4409         case OP_OR:
4410             o->op_seq = op_seqmax++;
4411             peep(cLOGOP->op_other);
4412             break;
4413
4414         case OP_COND_EXPR:
4415             o->op_seq = op_seqmax++;
4416             peep(cCONDOP->op_true);
4417             peep(cCONDOP->op_false);
4418             break;
4419
4420         case OP_ENTERLOOP:
4421             o->op_seq = op_seqmax++;
4422             peep(cLOOP->op_redoop);
4423             peep(cLOOP->op_nextop);
4424             peep(cLOOP->op_lastop);
4425             break;
4426
4427         case OP_MATCH:
4428         case OP_SUBST:
4429             o->op_seq = op_seqmax++;
4430             peep(cPMOP->op_pmreplstart);
4431             break;
4432
4433         case OP_EXEC:
4434             o->op_seq = op_seqmax++;
4435             if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4436                 if (o->op_next->op_sibling &&
4437                         o->op_next->op_sibling->op_type != OP_DIE) {
4438                     line_t oldline = curcop->cop_line;
4439
4440                     curcop->cop_line = ((COP*)o->op_next)->cop_line;
4441                     warn("Statement unlikely to be reached");
4442                     warn("(Maybe you meant system() when you said exec()?)\n");
4443                     curcop->cop_line = oldline;
4444                 }
4445             }
4446             break;
4447         default:
4448             o->op_seq = op_seqmax++;
4449             break;
4450         }
4451         oldop = o;
4452     }
4453     LEAVE;
4454 }