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