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