327ea8a567a7d3586357d6e35d40d71ef1bdf702
[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_ENTERSUB || type == OP_RV2HV || type == OP_PADHV)
108             warn("(Did you mean $ or @ instead of %c?)\n",
109                  type == OP_ENTERSUB ? '&' : '%');
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             line_t oldline = curcop->cop_line;
2468             curcop->cop_line = copline;
2469             warn("Value of %s construct can be \"0\"; test with defined()",
2470                  op_desc[warnop]);
2471                 curcop->cop_line = oldline;
2472         }
2473     }
2474
2475     if (!other)
2476         return first;
2477
2478     if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2479         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
2480
2481     Newz(1101, logop, 1, LOGOP);
2482
2483     logop->op_type = type;
2484     logop->op_ppaddr = ppaddr[type];
2485     logop->op_first = first;
2486     logop->op_flags = flags | OPf_KIDS;
2487     logop->op_other = LINKLIST(other);
2488     logop->op_private = 1 | (flags >> 8);
2489
2490     /* establish postfix order */
2491     logop->op_next = LINKLIST(first);
2492     first->op_next = (OP*)logop;
2493     first->op_sibling = other;
2494
2495     op = newUNOP(OP_NULL, 0, (OP*)logop);
2496     other->op_next = op;
2497
2498     return op;
2499 }
2500
2501 OP *
2502 newCONDOP(flags, first, trueop, falseop)
2503 I32 flags;
2504 OP* first;
2505 OP* trueop;
2506 OP* falseop;
2507 {
2508     CONDOP *condop;
2509     OP *op;
2510
2511     if (!falseop)
2512         return newLOGOP(OP_AND, 0, first, trueop);
2513     if (!trueop)
2514         return newLOGOP(OP_OR, 0, first, falseop);
2515
2516     scalarboolean(first);
2517     if (first->op_type == OP_CONST) {
2518         if (SvTRUE(((SVOP*)first)->op_sv)) {
2519             op_free(first);
2520             op_free(falseop);
2521             return trueop;
2522         }
2523         else {
2524             op_free(first);
2525             op_free(trueop);
2526             return falseop;
2527         }
2528     }
2529     else if (first->op_type == OP_WANTARRAY) {
2530         list(trueop);
2531         scalar(falseop);
2532     }
2533     Newz(1101, condop, 1, CONDOP);
2534
2535     condop->op_type = OP_COND_EXPR;
2536     condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2537     condop->op_first = first;
2538     condop->op_flags = flags | OPf_KIDS;
2539     condop->op_true = LINKLIST(trueop);
2540     condop->op_false = LINKLIST(falseop);
2541     condop->op_private = 1 | (flags >> 8);
2542
2543     /* establish postfix order */
2544     condop->op_next = LINKLIST(first);
2545     first->op_next = (OP*)condop;
2546
2547     first->op_sibling = trueop;
2548     trueop->op_sibling = falseop;
2549     op = newUNOP(OP_NULL, 0, (OP*)condop);
2550
2551     trueop->op_next = op;
2552     falseop->op_next = op;
2553
2554     return op;
2555 }
2556
2557 OP *
2558 newRANGE(flags, left, right)
2559 I32 flags;
2560 OP *left;
2561 OP *right;
2562 {
2563     CONDOP *condop;
2564     OP *flip;
2565     OP *flop;
2566     OP *op;
2567
2568     Newz(1101, condop, 1, CONDOP);
2569
2570     condop->op_type = OP_RANGE;
2571     condop->op_ppaddr = ppaddr[OP_RANGE];
2572     condop->op_first = left;
2573     condop->op_flags = OPf_KIDS;
2574     condop->op_true = LINKLIST(left);
2575     condop->op_false = LINKLIST(right);
2576     condop->op_private = 1 | (flags >> 8);
2577
2578     left->op_sibling = right;
2579
2580     condop->op_next = (OP*)condop;
2581     flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2582     flop = newUNOP(OP_FLOP, 0, flip);
2583     op = newUNOP(OP_NULL, 0, flop);
2584     linklist(flop);
2585
2586     left->op_next = flip;
2587     right->op_next = flop;
2588
2589     condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2590     sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2591     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2592     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2593
2594     flip->op_private =  left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2595     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2596
2597     flip->op_next = op;
2598     if (!flip->op_private || !flop->op_private)
2599         linklist(op);           /* blow off optimizer unless constant */
2600
2601     return op;
2602 }
2603
2604 OP *
2605 newLOOPOP(flags, debuggable, expr, block)
2606 I32 flags;
2607 I32 debuggable;
2608 OP *expr;
2609 OP *block;
2610 {
2611     OP* listop;
2612     OP* op;
2613     int once = block && block->op_flags & OPf_SPECIAL &&
2614       (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2615
2616     if (expr) {
2617         if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2618             return block;       /* do {} while 0 does once */
2619         else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
2620             expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr);
2621     }
2622
2623     listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2624     op = newLOGOP(OP_AND, 0, expr, listop);
2625
2626     ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2627
2628     if (once && op != listop)
2629         op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2630
2631     if (op == listop)
2632         op = newUNOP(OP_NULL, 0, op);   /* or do {} while 1 loses outer block */
2633
2634     op->op_flags |= flags;
2635     op = scope(op);
2636     op->op_flags |= OPf_SPECIAL;        /* suppress POPBLOCK curpm restoration*/
2637     return op;
2638 }
2639
2640 OP *
2641 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2642 I32 flags;
2643 I32 debuggable;
2644 LOOP *loop;
2645 OP *expr;
2646 OP *block;
2647 OP *cont;
2648 {
2649     OP *redo;
2650     OP *next = 0;
2651     OP *listop;
2652     OP *op;
2653     OP *condop;
2654
2655     if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2656         expr = newUNOP(OP_DEFINED, 0,
2657             newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2658     }
2659
2660     if (!block)
2661         block = newOP(OP_NULL, 0);
2662
2663     if (cont)
2664         next = LINKLIST(cont);
2665     if (expr)
2666         cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2667
2668     listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2669     redo = LINKLIST(listop);
2670
2671     if (expr) {
2672         op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2673         if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2674             op_free(expr);              /* oops, it's a while (0) */
2675             op_free((OP*)loop);
2676             return Nullop;              /* (listop already freed by newLOGOP) */
2677         }
2678         ((LISTOP*)listop)->op_last->op_next = condop = 
2679             (op == listop ? redo : LINKLIST(op));
2680         if (!next)
2681             next = condop;
2682     }
2683     else
2684         op = listop;
2685
2686     if (!loop) {
2687         Newz(1101,loop,1,LOOP);
2688         loop->op_type = OP_ENTERLOOP;
2689         loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2690         loop->op_private = 0;
2691         loop->op_next = (OP*)loop;
2692     }
2693
2694     op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2695
2696     loop->op_redoop = redo;
2697     loop->op_lastop = op;
2698
2699     if (next)
2700         loop->op_nextop = next;
2701     else
2702         loop->op_nextop = op;
2703
2704     op->op_flags |= flags;
2705     op->op_private |= (flags >> 8);
2706     return op;
2707 }
2708
2709 OP *
2710 #ifndef CAN_PROTOTYPE
2711 newFOROP(flags,label,forline,sv,expr,block,cont)
2712 I32 flags;
2713 char *label;
2714 line_t forline;
2715 OP* sv;
2716 OP* expr;
2717 OP*block;
2718 OP*cont;
2719 #else
2720 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2721 #endif /* CAN_PROTOTYPE */
2722 {
2723     LOOP *loop;
2724     int padoff = 0;
2725     I32 iterflags = 0;
2726
2727     copline = forline;
2728     if (sv) {
2729         if (sv->op_type == OP_RV2SV) {  /* symbol table variable */
2730             sv->op_type = OP_RV2GV;
2731             sv->op_ppaddr = ppaddr[OP_RV2GV];
2732         }
2733         else if (sv->op_type == OP_PADSV) { /* private variable */
2734             padoff = sv->op_targ;
2735             op_free(sv);
2736             sv = Nullop;
2737         }
2738         else
2739             croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2740     }
2741     else {
2742         sv = newGVOP(OP_GV, 0, defgv);
2743     }
2744     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2745         expr = scalar(ref(expr, OP_ITER));
2746         iterflags |= OPf_STACKED;
2747     }
2748     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2749         append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2750                     scalar(sv))));
2751     assert(!loop->op_next);
2752     Renew(loop, 1, LOOP);
2753     loop->op_targ = padoff;
2754     return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2755         newOP(OP_ITER, 0), block, cont));
2756 }
2757
2758 OP*
2759 newLOOPEX(type, label)
2760 I32 type;
2761 OP* label;
2762 {
2763     OP *op;
2764     if (type != OP_GOTO || label->op_type == OP_CONST) {
2765         op = newPVOP(type, 0, savepv(
2766                 label->op_type == OP_CONST
2767                     ? SvPVx(((SVOP*)label)->op_sv, na)
2768                     : "" ));
2769         op_free(label);
2770     }
2771     else {
2772         if (label->op_type == OP_ENTERSUB)
2773             label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2774         op = newUNOP(type, OPf_STACKED, label);
2775     }
2776     hints |= HINT_BLOCK_SCOPE;
2777     return op;
2778 }
2779
2780 void
2781 cv_undef(cv)
2782 CV *cv;
2783 {
2784     if (!CvXSUB(cv) && CvROOT(cv)) {
2785         if (CvDEPTH(cv))
2786             croak("Can't undef active subroutine");
2787         ENTER;
2788
2789         SAVESPTR(curpad);
2790         curpad = 0;
2791
2792         if (!CvCLONED(cv))
2793             op_free(CvROOT(cv));
2794         CvROOT(cv) = Nullop;
2795         LEAVE;
2796     }
2797     SvREFCNT_dec(CvGV(cv));
2798     CvGV(cv) = Nullgv;
2799     SvREFCNT_dec(CvOUTSIDE(cv));
2800     CvOUTSIDE(cv) = Nullcv;
2801     if (CvPADLIST(cv)) {
2802         /* may be during global destruction */
2803         if (SvREFCNT(CvPADLIST(cv))) {
2804             I32 i = AvFILL(CvPADLIST(cv));
2805             while (i >= 0) {
2806                 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2807                 if (svp)
2808                     SvREFCNT_dec(*svp);
2809             }
2810             SvREFCNT_dec((SV*)CvPADLIST(cv));
2811         }
2812         CvPADLIST(cv) = Nullav;
2813     }
2814 }
2815
2816 #ifdef DEBUG_CLOSURES
2817 static void
2818 cv_dump(cv)
2819 CV* cv;
2820 {
2821     CV *outside = CvOUTSIDE(cv);
2822     AV* padlist = CvPADLIST(cv);
2823     AV* pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
2824     AV* pad = (AV*)*av_fetch(padlist, 1, FALSE);
2825     SV** pname = AvARRAY(pad_name);
2826     SV** ppad = AvARRAY(pad);
2827     I32 ix;
2828
2829     PerlIO_printf(Perl_debug_log, "\tCV=0x%p (%s), OUTSIDE=0x%p (%s)\n",
2830                   cv,
2831                   (CvANON(cv) ? "ANON"
2832                    : (cv == main_cv) ? "MAIN"
2833                    : CvUNIQUE(outside) ? "UNIQUE"
2834                    : CvGV(cv) ? GvNAME(CvGV(cv)) : "?mystery?"),
2835                   outside,
2836                   (!outside ? "null"
2837                    : CvANON(outside) ? "ANON"
2838                    : (outside == main_cv) ? "MAIN"
2839                    : CvUNIQUE(outside) ? "UNIQUE"
2840                    : CvGV(outside) ? GvNAME(CvGV(outside)) : "?mystery?"));
2841
2842     for (ix = 1; ix <= AvFILL(pad); ix++) {
2843         if (SvPOK(pname[ix]))
2844             PerlIO_printf(Perl_debug_log, "\t%4d. 0x%p (\"%s\" %ld-%ld)\n",
2845                           ix, ppad[ix], SvPVX(pname[ix]),
2846                           (long)I_32(SvNVX(pname[ix])),
2847                           (long)SvIVX(pname[ix]));
2848     }
2849 }
2850 #endif /* DEBUG_CLOSURES */
2851
2852 static CV *
2853 cv_clone2(proto, outside)
2854 CV* proto;
2855 CV* outside;
2856 {
2857     AV* av;
2858     I32 ix;
2859     AV* protopadlist = CvPADLIST(proto);
2860     AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
2861     AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
2862     SV** pname = AvARRAY(protopad_name);
2863     SV** ppad = AvARRAY(protopad);
2864     AV* comppadlist;
2865     CV* cv;
2866
2867     assert(!CvUNIQUE(proto));
2868
2869     ENTER;
2870     SAVESPTR(curpad);
2871     SAVESPTR(comppad);
2872     SAVESPTR(compcv);
2873
2874     cv = compcv = (CV*)NEWSV(1104,0);
2875     sv_upgrade((SV *)cv, SVt_PVCV);
2876     CvCLONED_on(cv);
2877     if (CvANON(proto))
2878         CvANON_on(cv);
2879
2880     CvFILEGV(cv)        = CvFILEGV(proto);
2881     CvGV(cv)            = GvREFCNT_inc(CvGV(proto));
2882     CvSTASH(cv)         = CvSTASH(proto);
2883     CvROOT(cv)          = CvROOT(proto);
2884     CvSTART(cv)         = CvSTART(proto);
2885     if (outside)
2886         CvOUTSIDE(cv)   = (CV*)SvREFCNT_inc(outside);
2887
2888     comppad = newAV();
2889
2890     comppadlist = newAV();
2891     AvREAL_off(comppadlist);
2892     av_store(comppadlist, 0, SvREFCNT_inc((SV*)protopad_name));
2893     av_store(comppadlist, 1, (SV*)comppad);
2894     CvPADLIST(cv) = comppadlist;
2895     av_fill(comppad, AvFILL(protopad));
2896     curpad = AvARRAY(comppad);
2897
2898     av = newAV();           /* will be @_ */
2899     av_extend(av, 0);
2900     av_store(comppad, 0, (SV*)av);
2901     AvFLAGS(av) = AVf_REIFY;
2902
2903     for (ix = AvFILL(protopad); ix > 0; ix--) {
2904         SV* sv;
2905         if (pname[ix] != &sv_undef) {
2906             char *name = SvPVX(pname[ix]);    /* XXX */
2907             if (SvFLAGS(pname[ix]) & SVf_FAKE) {   /* lexical from outside? */
2908                 I32 off = pad_findlex(name, ix, SvIVX(pname[ix]),
2909                                       CvOUTSIDE(cv), cxstack_ix);
2910                 if (!off)
2911                     curpad[ix] = SvREFCNT_inc(ppad[ix]);
2912                 else if (off != ix)
2913                     croak("panic: cv_clone: %s", name);
2914             }
2915             else {                              /* our own lexical */
2916                 if (*name == '&') {
2917                     /* anon code -- we'll come back for it */
2918                     sv = SvREFCNT_inc(ppad[ix]);
2919                 }
2920                 else if (*name == '@')
2921                     sv = (SV*)newAV();
2922                 else if (*name == '%')
2923                     sv = (SV*)newHV();
2924                 else
2925                     sv = NEWSV(0,0);
2926                 if (!SvPADBUSY(sv))
2927                     SvPADMY_on(sv);
2928                 curpad[ix] = sv;
2929             }
2930         }
2931         else {
2932             sv = NEWSV(0,0);
2933             SvPADTMP_on(sv);
2934             curpad[ix] = sv;
2935         }
2936     }
2937
2938     /* Now that vars are all in place, clone nested closures. */
2939
2940     for (ix = AvFILL(protopad); ix > 0; ix--) {
2941         if (pname[ix] != &sv_undef
2942             && !(SvFLAGS(pname[ix]) & SVf_FAKE)
2943             && *SvPVX(pname[ix]) == '&'
2944             && CvCLONE(ppad[ix]))
2945         {
2946             CV *kid = cv_clone2((CV*)ppad[ix], cv);
2947             SvREFCNT_dec(ppad[ix]);
2948             CvCLONE_on(kid);
2949             SvPADMY_on(kid);
2950             curpad[ix] = (SV*)kid;
2951         }
2952     }
2953
2954 #ifdef DEBUG_CLOSURES
2955     PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
2956     cv_dump(outside);
2957     PerlIO_printf(Perl_debug_log, "  from:\n");
2958     cv_dump(proto);
2959     PerlIO_printf(Perl_debug_log, "   to:\n");
2960     cv_dump(cv);
2961 #endif
2962
2963     LEAVE;
2964     return cv;
2965 }
2966
2967 CV *
2968 cv_clone(proto)
2969 CV* proto;
2970 {
2971     return cv_clone2(proto, CvOUTSIDE(proto));
2972 }
2973
2974 SV *
2975 cv_const_sv(cv)
2976 CV *cv;
2977 {
2978     OP *o;
2979     SV *sv = Nullsv;
2980     
2981     if(cv && SvPOK(cv) && !SvCUR(cv)) {
2982         for (o = CvSTART(cv); o; o = o->op_next) {
2983             OPCODE type = o->op_type;
2984         
2985             if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
2986                 continue;
2987             if (type == OP_LEAVESUB || type == OP_RETURN)
2988                 break;
2989             if (type != OP_CONST || sv)
2990                 return Nullsv;
2991
2992             sv = ((SVOP*)o)->op_sv;
2993         }
2994     }
2995     return sv;
2996 }
2997
2998 CV *
2999 newSUB(floor,op,proto,block)
3000 I32 floor;
3001 OP *op;
3002 OP *proto;
3003 OP *block;
3004 {
3005     register CV *cv;
3006     char *name = op ? SvPVx(cSVOP->op_sv, na) : "__ANON__";
3007     GV* gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV);
3008     AV* av;
3009     char *s;
3010     I32 ix;
3011
3012     if (op) {
3013         SAVEFREEOP(op);
3014         sub_generation++;
3015     }
3016     if (cv = GvCV(gv)) {
3017         if (GvCVGEN(gv)) {
3018             /* just a cached method */
3019             SvREFCNT_dec(cv);
3020             cv = 0;
3021         }
3022         else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3023             SV* const_sv = cv_const_sv(cv);
3024
3025             char *p = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3026
3027             if((!proto != !SvPOK(cv)) || (p && strNE(SvPV((SV*)cv,na), p))) {
3028                 warn("Prototype mismatch: (%s) vs (%s)",
3029                         SvPOK(cv) ? SvPV((SV*)cv,na) : "none",
3030                         p ? p : "none");
3031             }
3032
3033             if ((const_sv || dowarn) && strNE(name, "BEGIN")) {/* already defined (or promised)? */
3034                 line_t oldline = curcop->cop_line;
3035
3036                 curcop->cop_line = copline;
3037                 warn(const_sv ? "Constant subroutine %s redefined"
3038                               : "Subroutine %s redefined",name);
3039                 curcop->cop_line = oldline;
3040             }
3041             SvREFCNT_dec(cv);
3042             cv = 0;
3043         }
3044     }
3045     if (cv) {                           /* must reuse cv if autoloaded */
3046         cv_undef(cv);
3047         CvFLAGS(cv) = (CvFLAGS(cv)&~CVf_CLONE) | (CvFLAGS(compcv)&CVf_CLONE);
3048         CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3049         CvOUTSIDE(compcv) = 0;
3050         CvPADLIST(cv) = CvPADLIST(compcv);
3051         CvPADLIST(compcv) = 0;
3052         if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3053             CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3054         SvREFCNT_dec(compcv);
3055     }
3056     else {
3057         cv = compcv;
3058     }
3059     GvCV(gv) = cv;
3060     GvCVGEN(gv) = 0;
3061     CvFILEGV(cv) = curcop->cop_filegv;
3062     CvGV(cv) = GvREFCNT_inc(gv);
3063     CvSTASH(cv) = curstash;
3064
3065     if (proto) {
3066         char *p = SvPVx(((SVOP*)proto)->op_sv, na);
3067         sv_setpv((SV*)cv, p);
3068         op_free(proto);
3069     }
3070
3071     if (error_count) {
3072         op_free(block);
3073         block = Nullop;
3074     }
3075     if (!block) {
3076         CvROOT(cv) = 0;
3077         copline = NOLINE;
3078         LEAVE_SCOPE(floor);
3079         return cv;
3080     }
3081
3082     /* XXX: Named functions at file scope cannot be closures */
3083     if (op && CvUNIQUE(CvOUTSIDE(cv)))
3084         CvCLONE_off(cv);
3085
3086     av = newAV();                       /* Will be @_ */
3087     av_extend(av, 0);
3088     av_store(comppad, 0, (SV*)av);
3089     AvFLAGS(av) = AVf_REIFY;
3090
3091     for (ix = AvFILL(comppad); ix > 0; ix--) {
3092         if (!SvPADMY(curpad[ix]))
3093             SvPADTMP_on(curpad[ix]);
3094     }
3095
3096     if (AvFILL(comppad_name) < AvFILL(comppad))
3097         av_store(comppad_name, AvFILL(comppad), Nullsv);
3098
3099     CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3100     CvSTART(cv) = LINKLIST(CvROOT(cv));
3101     CvROOT(cv)->op_next = 0;
3102     peep(CvSTART(cv));
3103
3104     if (s = strrchr(name,':'))
3105         s++;
3106     else
3107         s = name;
3108     if (strEQ(s, "BEGIN") && !error_count) {
3109         ENTER;
3110         SAVESPTR(compiling.cop_filegv);
3111         SAVEI16(compiling.cop_line);
3112         SAVEI32(perldb);
3113         save_svref(&rs);
3114         sv_setsv(rs, nrs);
3115
3116         if (!beginav)
3117             beginav = newAV();
3118         DEBUG_x( dump_sub(gv) );
3119         av_push(beginav, (SV *)cv);
3120         GvCV(gv) = 0;
3121         calllist(beginav);
3122
3123         curcop = &compiling;
3124         LEAVE;
3125     }
3126     else if (strEQ(s, "END") && !error_count) {
3127         if (!endav)
3128             endav = newAV();
3129         av_unshift(endav, 1);
3130         av_store(endav, 0, (SV *)cv);
3131         GvCV(gv) = 0;
3132     }
3133
3134     if (perldb && curstash != debstash) {
3135         SV *sv;
3136         SV *tmpstr = sv_newmortal();
3137         static GV *db_postponed;
3138         CV *cv;
3139         HV *hv;
3140
3141         sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), (long)subline);
3142         sv = newSVpv(buf,0);
3143         sv_catpv(sv,"-");
3144         sprintf(buf,"%ld",(long)curcop->cop_line);
3145         sv_catpv(sv,buf);
3146         gv_efullname3(tmpstr, gv, Nullch);
3147         hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3148         if (!db_postponed) {
3149             db_postponed = gv_fetchpv("DB::postponed", TRUE, SVt_PVHV);
3150         }
3151         hv = GvHVn(db_postponed);
3152         if (HvFILL(hv) >= 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3153             && (cv = GvCV(db_postponed))) {
3154             dSP;
3155             PUSHMARK(sp);
3156             XPUSHs(tmpstr);
3157             PUTBACK;
3158             perl_call_sv((SV*)cv, G_DISCARD);
3159         }
3160     }
3161
3162     if (!op) {
3163         GvCV(gv) = 0;   /* Will remember in SVOP instead. */
3164         CvANON_on(cv);
3165     }
3166     copline = NOLINE;
3167     LEAVE_SCOPE(floor);
3168     return cv;
3169 }
3170
3171 #ifdef DEPRECATED
3172 CV *
3173 newXSUB(name, ix, subaddr, filename)
3174 char *name;
3175 I32 ix;
3176 I32 (*subaddr)();
3177 char *filename;
3178 {
3179     CV* cv = newXS(name, (void(*)())subaddr, filename);
3180     CvOLDSTYLE_on(cv);
3181     CvXSUBANY(cv).any_i32 = ix;
3182     return cv;
3183 }
3184 #endif
3185
3186 CV *
3187 newXS(name, subaddr, filename)
3188 char *name;
3189 void (*subaddr) _((CV*));
3190 char *filename;
3191 {
3192     register CV *cv;
3193     GV *gv = gv_fetchpv((name ? name : "__ANON__"), GV_ADDMULTI, SVt_PVCV);
3194     char *s;
3195
3196     if (name)
3197         sub_generation++;
3198     if (cv = GvCV(gv)) {
3199         if (GvCVGEN(gv))
3200             cv = 0;                     /* just a cached method */
3201         else if (CvROOT(cv) || CvXSUB(cv)) {    /* already defined? */
3202             if (dowarn) {
3203                 line_t oldline = curcop->cop_line;
3204
3205                 curcop->cop_line = copline;
3206                 warn("Subroutine %s redefined",name);
3207                 curcop->cop_line = oldline;
3208             }
3209             SvREFCNT_dec(cv);
3210             cv = 0;
3211         }
3212     }
3213     if (cv) {                           /* must reuse cv if autoloaded */
3214         assert(SvREFCNT(CvGV(cv)) > 1);
3215         SvREFCNT_dec(CvGV(cv));
3216     }
3217     else {
3218         cv = (CV*)NEWSV(1105,0);
3219         sv_upgrade((SV *)cv, SVt_PVCV);
3220     }
3221     GvCV(gv) = cv;
3222     CvGV(cv) = GvREFCNT_inc(gv);
3223     GvCVGEN(gv) = 0;
3224     CvFILEGV(cv) = gv_fetchfile(filename);
3225     CvXSUB(cv) = subaddr;
3226     if (!name)
3227         s = "__ANON__";
3228     else if (s = strrchr(name,':'))
3229         s++;
3230     else
3231         s = name;
3232     if (strEQ(s, "BEGIN")) {
3233         if (!beginav)
3234             beginav = newAV();
3235         av_push(beginav, SvREFCNT_inc(gv));
3236     }
3237     else if (strEQ(s, "END")) {
3238         if (!endav)
3239             endav = newAV();
3240         av_unshift(endav, 1);
3241         av_store(endav, 0, SvREFCNT_inc(gv));
3242     }
3243     if (!name) {
3244         GvCV(gv) = 0;   /* Will remember elsewhere instead. */
3245         CvANON_on(cv);
3246     }
3247     return cv;
3248 }
3249
3250 void
3251 newFORM(floor,op,block)
3252 I32 floor;
3253 OP *op;
3254 OP *block;
3255 {
3256     register CV *cv;
3257     char *name;
3258     GV *gv;
3259     I32 ix;
3260
3261     if (op)
3262         name = SvPVx(cSVOP->op_sv, na);
3263     else
3264         name = "STDOUT";
3265     gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3266     GvMULTI_on(gv);
3267     if (cv = GvFORM(gv)) {
3268         if (dowarn) {
3269             line_t oldline = curcop->cop_line;
3270
3271             curcop->cop_line = copline;
3272             warn("Format %s redefined",name);
3273             curcop->cop_line = oldline;
3274         }
3275         SvREFCNT_dec(cv);
3276     }
3277     cv = compcv;
3278     GvFORM(gv) = cv;
3279     CvGV(cv) = GvREFCNT_inc(gv);
3280     CvFILEGV(cv) = curcop->cop_filegv;
3281
3282     for (ix = AvFILL(comppad); ix > 0; ix--) {
3283         if (!SvPADMY(curpad[ix]))
3284             SvPADTMP_on(curpad[ix]);
3285     }
3286
3287     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3288     CvSTART(cv) = LINKLIST(CvROOT(cv));
3289     CvROOT(cv)->op_next = 0;
3290     peep(CvSTART(cv));
3291     FmLINES(cv) = 0;
3292     op_free(op);
3293     copline = NOLINE;
3294     LEAVE_SCOPE(floor);
3295 }
3296
3297 OP *
3298 newANONLIST(op)
3299 OP* op;
3300 {
3301     return newUNOP(OP_REFGEN, 0,
3302         mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
3303 }
3304
3305 OP *
3306 newANONHASH(op)
3307 OP* op;
3308 {
3309     return newUNOP(OP_REFGEN, 0,
3310         mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3311 }
3312
3313 OP *
3314 newANONSUB(floor, proto, block)
3315 I32 floor;
3316 OP *proto;
3317 OP *block;
3318 {
3319     return newUNOP(OP_REFGEN, 0,
3320         newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3321 }
3322
3323 OP *
3324 oopsAV(o)
3325 OP *o;
3326 {
3327     switch (o->op_type) {
3328     case OP_PADSV:
3329         o->op_type = OP_PADAV;
3330         o->op_ppaddr = ppaddr[OP_PADAV];
3331         return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3332         
3333     case OP_RV2SV:
3334         o->op_type = OP_RV2AV;
3335         o->op_ppaddr = ppaddr[OP_RV2AV];
3336         ref(o, OP_RV2AV);
3337         break;
3338
3339     default:
3340         warn("oops: oopsAV");
3341         break;
3342     }
3343     return o;
3344 }
3345
3346 OP *
3347 oopsHV(o)
3348 OP *o;
3349 {
3350     switch (o->op_type) {
3351     case OP_PADSV:
3352     case OP_PADAV:
3353         o->op_type = OP_PADHV;
3354         o->op_ppaddr = ppaddr[OP_PADHV];
3355         return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3356
3357     case OP_RV2SV:
3358     case OP_RV2AV:
3359         o->op_type = OP_RV2HV;
3360         o->op_ppaddr = ppaddr[OP_RV2HV];
3361         ref(o, OP_RV2HV);
3362         break;
3363
3364     default:
3365         warn("oops: oopsHV");
3366         break;
3367     }
3368     return o;
3369 }
3370
3371 OP *
3372 newAVREF(o)
3373 OP *o;
3374 {
3375     if (o->op_type == OP_PADANY) {
3376         o->op_type = OP_PADAV;
3377         o->op_ppaddr = ppaddr[OP_PADAV];
3378         return o;
3379     }
3380     return newUNOP(OP_RV2AV, 0, scalar(o));
3381 }
3382
3383 OP *
3384 newGVREF(type,o)
3385 I32 type;
3386 OP *o;
3387 {
3388     if (type == OP_MAPSTART)
3389         return newUNOP(OP_NULL, 0, o);
3390     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3391 }
3392
3393 OP *
3394 newHVREF(o)
3395 OP *o;
3396 {
3397     if (o->op_type == OP_PADANY) {
3398         o->op_type = OP_PADHV;
3399         o->op_ppaddr = ppaddr[OP_PADHV];
3400         return o;
3401     }
3402     return newUNOP(OP_RV2HV, 0, scalar(o));
3403 }
3404
3405 OP *
3406 oopsCV(o)
3407 OP *o;
3408 {
3409     croak("NOT IMPL LINE %d",__LINE__);
3410     /* STUB */
3411     return o;
3412 }
3413
3414 OP *
3415 newCVREF(flags, o)
3416 I32 flags;
3417 OP *o;
3418 {
3419     return newUNOP(OP_RV2CV, flags, scalar(o));
3420 }
3421
3422 OP *
3423 newSVREF(o)
3424 OP *o;
3425 {
3426     if (o->op_type == OP_PADANY) {
3427         o->op_type = OP_PADSV;
3428         o->op_ppaddr = ppaddr[OP_PADSV];
3429         return o;
3430     }
3431     return newUNOP(OP_RV2SV, 0, scalar(o));
3432 }
3433
3434 /* Check routines. */
3435
3436 OP *
3437 ck_anoncode(op)
3438 OP *op;
3439 {
3440     PADOFFSET ix;
3441     SV* name;
3442
3443     name = NEWSV(1106,0);
3444     sv_upgrade(name, SVt_PVNV);
3445     sv_setpvn(name, "&", 1);
3446     SvIVX(name) = -1;
3447     SvNVX(name) = 1;
3448     ix = pad_alloc(op->op_type, SVs_PADMY);
3449     av_store(comppad_name, ix, name);
3450     av_store(comppad, ix, cSVOP->op_sv);
3451     SvPADMY_on(cSVOP->op_sv);
3452     cSVOP->op_sv = Nullsv;
3453     cSVOP->op_targ = ix;
3454     return op;
3455 }
3456
3457 OP *
3458 ck_bitop(op)
3459 OP *op;
3460 {
3461     op->op_private = hints;
3462     return op;
3463 }
3464
3465 OP *
3466 ck_concat(op)
3467 OP *op;
3468 {
3469     if (cUNOP->op_first->op_type == OP_CONCAT)
3470         op->op_flags |= OPf_STACKED;
3471     return op;
3472 }
3473
3474 OP *
3475 ck_spair(op)
3476 OP *op;
3477 {
3478     if (op->op_flags & OPf_KIDS) {
3479         OP* newop;
3480         OP* kid;
3481         OPCODE type = op->op_type;
3482         op = modkids(ck_fun(op), type);
3483         kid = cUNOP->op_first;
3484         newop = kUNOP->op_first->op_sibling;
3485         if (newop &&
3486             (newop->op_sibling ||
3487              !(opargs[newop->op_type] & OA_RETSCALAR) ||
3488              newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3489              newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3490             
3491             return op;
3492         }
3493         op_free(kUNOP->op_first);
3494         kUNOP->op_first = newop;
3495     }
3496     op->op_ppaddr = ppaddr[++op->op_type];
3497     return ck_fun(op);
3498 }
3499
3500 OP *
3501 ck_delete(op)
3502 OP *op;
3503 {
3504     op = ck_fun(op);
3505     op->op_private = 0;
3506     if (op->op_flags & OPf_KIDS) {
3507         OP *kid = cUNOP->op_first;
3508         if (kid->op_type == OP_HSLICE)
3509             op->op_private |= OPpSLICE;
3510         else if (kid->op_type != OP_HELEM)
3511             croak("%s argument is not a HASH element or slice",
3512                   op_desc[op->op_type]);
3513         null(kid);
3514     }
3515     return op;
3516 }
3517
3518 OP *
3519 ck_eof(op)
3520 OP *op;
3521 {
3522     I32 type = op->op_type;
3523
3524     if (op->op_flags & OPf_KIDS) {
3525         if (cLISTOP->op_first->op_type == OP_STUB) {
3526             op_free(op);
3527             op = newUNOP(type, OPf_SPECIAL,
3528                 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
3529         }
3530         return ck_fun(op);
3531     }
3532     return op;
3533 }
3534
3535 OP *
3536 ck_eval(op)
3537 OP *op;
3538 {
3539     hints |= HINT_BLOCK_SCOPE;
3540     if (op->op_flags & OPf_KIDS) {
3541         SVOP *kid = (SVOP*)cUNOP->op_first;
3542
3543         if (!kid) {
3544             op->op_flags &= ~OPf_KIDS;
3545             null(op);
3546         }
3547         else if (kid->op_type == OP_LINESEQ) {
3548             LOGOP *enter;
3549
3550             kid->op_next = op->op_next;
3551             cUNOP->op_first = 0;
3552             op_free(op);
3553
3554             Newz(1101, enter, 1, LOGOP);
3555             enter->op_type = OP_ENTERTRY;
3556             enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3557             enter->op_private = 0;
3558
3559             /* establish postfix order */
3560             enter->op_next = (OP*)enter;
3561
3562             op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3563             op->op_type = OP_LEAVETRY;
3564             op->op_ppaddr = ppaddr[OP_LEAVETRY];
3565             enter->op_other = op;
3566             return op;
3567         }
3568     }
3569     else {
3570         op_free(op);
3571         op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3572     }
3573     op->op_targ = (PADOFFSET)hints;
3574     return op;
3575 }
3576
3577 OP *
3578 ck_exec(op)
3579 OP *op;
3580 {
3581     OP *kid;
3582     if (op->op_flags & OPf_STACKED) {
3583         op = ck_fun(op);
3584         kid = cUNOP->op_first->op_sibling;
3585         if (kid->op_type == OP_RV2GV)
3586             null(kid);
3587     }
3588     else
3589         op = listkids(op);
3590     return op;
3591 }
3592
3593 OP *
3594 ck_exists(op)
3595 OP *op;
3596 {
3597     op = ck_fun(op);
3598     if (op->op_flags & OPf_KIDS) {
3599         OP *kid = cUNOP->op_first;
3600         if (kid->op_type != OP_HELEM)
3601             croak("%s argument is not a HASH element", op_desc[op->op_type]);
3602         null(kid);
3603     }
3604     return op;
3605 }
3606
3607 OP *
3608 ck_gvconst(o)
3609 register OP *o;
3610 {
3611     o = fold_constants(o);
3612     if (o->op_type == OP_CONST)
3613         o->op_type = OP_GV;
3614     return o;
3615 }
3616
3617 OP *
3618 ck_rvconst(op)
3619 register OP *op;
3620 {
3621     SVOP *kid = (SVOP*)cUNOP->op_first;
3622
3623     op->op_private |= (hints & HINT_STRICT_REFS);
3624     if (kid->op_type == OP_CONST) {
3625         int iscv = (op->op_type==OP_RV2CV)*2;
3626         GV *gv = 0;
3627         kid->op_type = OP_GV;
3628         for (gv = 0; !gv; iscv++) {
3629             /*
3630              * This is a little tricky.  We only want to add the symbol if we
3631              * didn't add it in the lexer.  Otherwise we get duplicate strict
3632              * warnings.  But if we didn't add it in the lexer, we must at
3633              * least pretend like we wanted to add it even if it existed before,
3634              * or we get possible typo warnings.  OPpCONST_ENTERED says
3635              * whether the lexer already added THIS instance of this symbol.
3636              */
3637             gv = gv_fetchpv(SvPVx(kid->op_sv, na),
3638                 iscv | !(kid->op_private & OPpCONST_ENTERED),
3639                 iscv
3640                     ? SVt_PVCV
3641                     : op->op_type == OP_RV2SV
3642                         ? SVt_PV
3643                         : op->op_type == OP_RV2AV
3644                             ? SVt_PVAV
3645                             : op->op_type == OP_RV2HV
3646                                 ? SVt_PVHV
3647                                 : SVt_PVGV);
3648         }
3649         SvREFCNT_dec(kid->op_sv);
3650         kid->op_sv = SvREFCNT_inc(gv);
3651     }
3652     return op;
3653 }
3654
3655 OP *
3656 ck_ftst(op)
3657 OP *op;
3658 {
3659     I32 type = op->op_type;
3660
3661     if (op->op_flags & OPf_REF)
3662         return op;
3663
3664     if (op->op_flags & OPf_KIDS) {
3665         SVOP *kid = (SVOP*)cUNOP->op_first;
3666
3667         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3668             OP *newop = newGVOP(type, OPf_REF,
3669                 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3670             op_free(op);
3671             return newop;
3672         }
3673     }
3674     else {
3675         op_free(op);
3676         if (type == OP_FTTTY)
3677             return newGVOP(type, OPf_REF, gv_fetchpv("main'STDIN", TRUE,
3678                                 SVt_PVIO));
3679         else
3680             return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3681     }
3682     return op;
3683 }
3684
3685 OP *
3686 ck_fun(op)
3687 OP *op;
3688 {
3689     register OP *kid;
3690     OP **tokid;
3691     OP *sibl;
3692     I32 numargs = 0;
3693     int type = op->op_type;
3694     register I32 oa = opargs[type] >> OASHIFT;
3695     
3696     if (op->op_flags & OPf_STACKED) {
3697         if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
3698             oa &= ~OA_OPTIONAL;
3699         else
3700             return no_fh_allowed(op);
3701     }
3702
3703     if (op->op_flags & OPf_KIDS) {
3704         tokid = &cLISTOP->op_first;
3705         kid = cLISTOP->op_first;
3706         if (kid->op_type == OP_PUSHMARK ||
3707             kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
3708         {
3709             tokid = &kid->op_sibling;
3710             kid = kid->op_sibling;
3711         }
3712         if (!kid && opargs[type] & OA_DEFGV)
3713             *tokid = kid = newSVREF(newGVOP(OP_GV, 0, defgv));
3714
3715         while (oa && kid) {
3716             numargs++;
3717             sibl = kid->op_sibling;
3718             switch (oa & 7) {
3719             case OA_SCALAR:
3720                 scalar(kid);
3721                 break;
3722             case OA_LIST:
3723                 if (oa < 16) {
3724                     kid = 0;
3725                     continue;
3726                 }
3727                 else
3728                     list(kid);
3729                 break;
3730             case OA_AVREF:
3731                 if (kid->op_type == OP_CONST &&
3732                   (kid->op_private & OPpCONST_BARE)) {
3733                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3734                     OP *newop = newAVREF(newGVOP(OP_GV, 0,
3735                         gv_fetchpv(name, TRUE, SVt_PVAV) ));
3736                     if (dowarn)
3737                         warn("Array @%s missing the @ in argument %d of %s()",
3738                             name, numargs, op_desc[type]);
3739                     op_free(kid);
3740                     kid = newop;
3741                     kid->op_sibling = sibl;
3742                     *tokid = kid;
3743                 }
3744                 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
3745                     bad_type(numargs, "array", op_desc[op->op_type], kid);
3746                 mod(kid, type);
3747                 break;
3748             case OA_HVREF:
3749                 if (kid->op_type == OP_CONST &&
3750                   (kid->op_private & OPpCONST_BARE)) {
3751                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3752                     OP *newop = newHVREF(newGVOP(OP_GV, 0,
3753                         gv_fetchpv(name, TRUE, SVt_PVHV) ));
3754                     if (dowarn)
3755                         warn("Hash %%%s missing the %% in argument %d of %s()",
3756                             name, numargs, op_desc[type]);
3757                     op_free(kid);
3758                     kid = newop;
3759                     kid->op_sibling = sibl;
3760                     *tokid = kid;
3761                 }
3762                 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
3763                     bad_type(numargs, "hash", op_desc[op->op_type], kid);
3764                 mod(kid, type);
3765                 break;
3766             case OA_CVREF:
3767                 {
3768                     OP *newop = newUNOP(OP_NULL, 0, kid);
3769                     kid->op_sibling = 0;
3770                     linklist(kid);
3771                     newop->op_next = newop;
3772                     kid = newop;
3773                     kid->op_sibling = sibl;
3774                     *tokid = kid;
3775                 }
3776                 break;
3777             case OA_FILEREF:
3778                 if (kid->op_type != OP_GV) {
3779                     if (kid->op_type == OP_CONST &&
3780                       (kid->op_private & OPpCONST_BARE)) {
3781                         OP *newop = newGVOP(OP_GV, 0,
3782                             gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3783                                         SVt_PVIO) );
3784                         op_free(kid);
3785                         kid = newop;
3786                     }
3787                     else {
3788                         kid->op_sibling = 0;
3789                         kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3790                     }
3791                     kid->op_sibling = sibl;
3792                     *tokid = kid;
3793                 }
3794                 scalar(kid);
3795                 break;
3796             case OA_SCALARREF:
3797                 mod(scalar(kid), type);
3798                 break;
3799             }
3800             oa >>= 4;
3801             tokid = &kid->op_sibling;
3802             kid = kid->op_sibling;
3803         }
3804         op->op_private |= numargs;
3805         if (kid)
3806             return too_many_arguments(op,op_desc[op->op_type]);
3807         listkids(op);
3808     }
3809     else if (opargs[type] & OA_DEFGV) {
3810         op_free(op);
3811         return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3812     }
3813
3814     if (oa) {
3815         while (oa & OA_OPTIONAL)
3816             oa >>= 4;
3817         if (oa && oa != OA_LIST)
3818             return too_few_arguments(op,op_desc[op->op_type]);
3819     }
3820     return op;
3821 }
3822
3823 OP *
3824 ck_glob(op)
3825 OP *op;
3826 {
3827     GV *gv = gv_fetchpv("glob", FALSE, SVt_PVCV);
3828
3829     if (gv && GvIMPORTED_CV(gv)) {
3830         op->op_type = OP_LIST;
3831         op->op_ppaddr = ppaddr[OP_LIST];
3832         op = newUNOP(OP_ENTERSUB, OPf_STACKED,
3833                      append_elem(OP_LIST, op, 
3834                                  scalar(newUNOP(OP_RV2CV, 0,
3835                                                 newGVOP(OP_GV, 0, gv)))));
3836         return ck_subr(op);
3837     }
3838     gv = newGVgen("main");
3839     gv_IOadd(gv);
3840     append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3841     scalarkids(op);
3842     return ck_fun(op);
3843 }
3844
3845 OP *
3846 ck_grep(op)
3847 OP *op;
3848 {
3849     LOGOP *gwop;
3850     OP *kid;
3851     OPCODE type = op->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
3852
3853     op->op_ppaddr = ppaddr[OP_GREPSTART];
3854     Newz(1101, gwop, 1, LOGOP);
3855     
3856     if (op->op_flags & OPf_STACKED) {
3857         OP* k;
3858         op = ck_sort(op);
3859         kid = cLISTOP->op_first->op_sibling;
3860         for (k = cLISTOP->op_first->op_sibling->op_next; k; k = k->op_next) {
3861             kid = k;
3862         }
3863         kid->op_next = (OP*)gwop;
3864         op->op_flags &= ~OPf_STACKED;
3865     }
3866     kid = cLISTOP->op_first->op_sibling;
3867     if (type == OP_MAPWHILE)
3868         list(kid);
3869     else
3870         scalar(kid);
3871     op = ck_fun(op);
3872     if (error_count)
3873         return op;
3874     kid = cLISTOP->op_first->op_sibling; 
3875     if (kid->op_type != OP_NULL)
3876         croak("panic: ck_grep");
3877     kid = kUNOP->op_first;
3878
3879     gwop->op_type = type;
3880     gwop->op_ppaddr = ppaddr[type];
3881     gwop->op_first = listkids(op);
3882     gwop->op_flags |= OPf_KIDS;
3883     gwop->op_private = 1;
3884     gwop->op_other = LINKLIST(kid);
3885     gwop->op_targ = pad_alloc(type, SVs_PADTMP);
3886     kid->op_next = (OP*)gwop;
3887
3888     kid = cLISTOP->op_first->op_sibling;
3889     if (!kid || !kid->op_sibling)
3890         return too_few_arguments(op,op_desc[op->op_type]);
3891     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
3892         mod(kid, OP_GREPSTART);
3893
3894     return (OP*)gwop;
3895 }
3896
3897 OP *
3898 ck_index(op)
3899 OP *op;
3900 {
3901     if (op->op_flags & OPf_KIDS) {
3902         OP *kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
3903         if (kid && kid->op_type == OP_CONST)
3904             fbm_compile(((SVOP*)kid)->op_sv);
3905     }
3906     return ck_fun(op);
3907 }
3908
3909 OP *
3910 ck_lengthconst(op)
3911 OP *op;
3912 {
3913     /* XXX length optimization goes here */
3914     return ck_fun(op);
3915 }
3916
3917 OP *
3918 ck_lfun(op)
3919 OP *op;
3920 {
3921     OPCODE type = op->op_type;
3922     return modkids(ck_fun(op), type);
3923 }
3924
3925 OP *
3926 ck_rfun(op)
3927 OP *op;
3928 {
3929     OPCODE type = op->op_type;
3930     return refkids(ck_fun(op), type);
3931 }
3932
3933 OP *
3934 ck_listiob(op)
3935 OP *op;
3936 {
3937     register OP *kid;
3938     
3939     kid = cLISTOP->op_first;
3940     if (!kid) {
3941         op = force_list(op);
3942         kid = cLISTOP->op_first;
3943     }
3944     if (kid->op_type == OP_PUSHMARK)
3945         kid = kid->op_sibling;
3946     if (kid && op->op_flags & OPf_STACKED)
3947         kid = kid->op_sibling;
3948     else if (kid && !kid->op_sibling) {         /* print HANDLE; */
3949         if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
3950             op->op_flags |= OPf_STACKED;        /* make it a filehandle */
3951             kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
3952             cLISTOP->op_first->op_sibling = kid;
3953             cLISTOP->op_last = kid;
3954             kid = kid->op_sibling;
3955         }
3956     }
3957         
3958     if (!kid)
3959         append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3960
3961     op = listkids(op);
3962
3963     op->op_private = 0;
3964 #ifdef USE_LOCALE
3965     if (hints & HINT_LOCALE)
3966         op->op_private |= OPpLOCALE;
3967 #endif
3968
3969     return op;
3970 }
3971
3972 OP *
3973 ck_fun_locale(op)
3974 OP *op;
3975 {
3976     op = ck_fun(op);
3977
3978     op->op_private = 0;
3979 #ifdef USE_LOCALE
3980     if (hints & HINT_LOCALE)
3981         op->op_private |= OPpLOCALE;
3982 #endif
3983
3984     return op;
3985 }
3986
3987 OP *
3988 ck_scmp(op)
3989 OP *op;
3990 {
3991     op->op_private = 0;
3992 #ifdef USE_LOCALE
3993     if (hints & HINT_LOCALE)
3994         op->op_private |= OPpLOCALE;
3995 #endif
3996
3997     return op;
3998 }
3999
4000 OP *
4001 ck_match(op)
4002 OP *op;
4003 {
4004     cPMOP->op_pmflags |= PMf_RUNTIME;
4005     cPMOP->op_pmpermflags |= PMf_RUNTIME;
4006     return op;
4007 }
4008
4009 OP *
4010 ck_null(op)
4011 OP *op;
4012 {
4013     return op;
4014 }
4015
4016 OP *
4017 ck_repeat(op)
4018 OP *op;
4019 {
4020     if (cBINOP->op_first->op_flags & OPf_PARENS) {
4021         op->op_private |= OPpREPEAT_DOLIST;
4022         cBINOP->op_first = force_list(cBINOP->op_first);
4023     }
4024     else
4025         scalar(op);
4026     return op;
4027 }
4028
4029 OP *
4030 ck_require(op)
4031 OP *op;
4032 {
4033     if (op->op_flags & OPf_KIDS) {      /* Shall we supply missing .pm? */
4034         SVOP *kid = (SVOP*)cUNOP->op_first;
4035
4036         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4037             char *s;
4038             for (s = SvPVX(kid->op_sv); *s; s++) {
4039                 if (*s == ':' && s[1] == ':') {
4040                     *s = '/';
4041                     Move(s+2, s+1, strlen(s+2)+1, char);
4042                     --SvCUR(kid->op_sv);
4043                 }
4044             }
4045             sv_catpvn(kid->op_sv, ".pm", 3);
4046         }
4047     }
4048     return ck_fun(op);
4049 }
4050
4051 OP *
4052 ck_retarget(op)
4053 OP *op;
4054 {
4055     croak("NOT IMPL LINE %d",__LINE__);
4056     /* STUB */
4057     return op;
4058 }
4059
4060 OP *
4061 ck_select(op)
4062 OP *op;
4063 {
4064     OP* kid;
4065     if (op->op_flags & OPf_KIDS) {
4066         kid = cLISTOP->op_first->op_sibling;    /* get past pushmark */
4067         if (kid && kid->op_sibling) {
4068             op->op_type = OP_SSELECT;
4069             op->op_ppaddr = ppaddr[OP_SSELECT];
4070             op = ck_fun(op);
4071             return fold_constants(op);
4072         }
4073     }
4074     op = ck_fun(op);
4075     kid = cLISTOP->op_first->op_sibling;    /* get past pushmark */
4076     if (kid && kid->op_type == OP_RV2GV)
4077         kid->op_private &= ~HINT_STRICT_REFS;
4078     return op;
4079 }
4080
4081 OP *
4082 ck_shift(op)
4083 OP *op;
4084 {
4085     I32 type = op->op_type;
4086
4087     if (!(op->op_flags & OPf_KIDS)) {
4088         op_free(op);
4089         return newUNOP(type, 0,
4090             scalar(newUNOP(OP_RV2AV, 0,
4091                 scalar(newGVOP(OP_GV, 0, subline 
4092                                ? defgv 
4093                                : gv_fetchpv("ARGV", TRUE, SVt_PVAV) )))));
4094     }
4095     return scalar(modkids(ck_fun(op), type));
4096 }
4097
4098 OP *
4099 ck_sort(op)
4100 OP *op;
4101 {
4102     op->op_private = 0;
4103 #ifdef USE_LOCALE
4104     if (hints & HINT_LOCALE)
4105         op->op_private |= OPpLOCALE;
4106 #endif
4107
4108     if (op->op_flags & OPf_STACKED) {
4109         OP *kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
4110         OP *k;
4111         kid = kUNOP->op_first;                          /* get past rv2gv */
4112
4113         if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4114             linklist(kid);
4115             if (kid->op_type == OP_SCOPE) {
4116                 k = kid->op_next;
4117                 kid->op_next = 0;
4118             }
4119             else if (kid->op_type == OP_LEAVE) {
4120                 if (op->op_type == OP_SORT) {
4121                     null(kid);                  /* wipe out leave */
4122                     kid->op_next = kid;
4123
4124                     for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4125                         if (k->op_next == kid)
4126                             k->op_next = 0;
4127                     }
4128                 }
4129                 else
4130                     kid->op_next = 0;           /* just disconnect the leave */
4131                 k = kLISTOP->op_first;
4132             }
4133             peep(k);
4134
4135             kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
4136             null(kid);                                  /* wipe out rv2gv */
4137             if (op->op_type == OP_SORT)
4138                 kid->op_next = kid;
4139             else
4140                 kid->op_next = k;
4141             op->op_flags |= OPf_SPECIAL;
4142         }
4143     }
4144
4145     return op;
4146 }
4147
4148 OP *
4149 ck_split(op)
4150 OP *op;
4151 {
4152     register OP *kid;
4153     PMOP* pm;
4154     
4155     if (op->op_flags & OPf_STACKED)
4156         return no_fh_allowed(op);
4157
4158     kid = cLISTOP->op_first;
4159     if (kid->op_type != OP_NULL)
4160         croak("panic: ck_split");
4161     kid = kid->op_sibling;
4162     op_free(cLISTOP->op_first);
4163     cLISTOP->op_first = kid;
4164     if (!kid) {
4165         cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4166         cLISTOP->op_last = kid; /* There was only one element previously */
4167     }
4168
4169     if (kid->op_type != OP_MATCH) {
4170         OP *sibl = kid->op_sibling;
4171         kid->op_sibling = 0;
4172         kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4173         if (cLISTOP->op_first == cLISTOP->op_last)
4174             cLISTOP->op_last = kid;
4175         cLISTOP->op_first = kid;
4176         kid->op_sibling = sibl;
4177     }
4178     pm = (PMOP*)kid;
4179     if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
4180         SvREFCNT_dec(pm->op_pmshort);   /* can't use substring to optimize */
4181         pm->op_pmshort = 0;
4182     }
4183
4184     kid->op_type = OP_PUSHRE;
4185     kid->op_ppaddr = ppaddr[OP_PUSHRE];
4186     scalar(kid);
4187
4188     if (!kid->op_sibling)
4189         append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
4190
4191     kid = kid->op_sibling;
4192     scalar(kid);
4193
4194     if (!kid->op_sibling)
4195         append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
4196
4197     kid = kid->op_sibling;
4198     scalar(kid);
4199
4200     if (kid->op_sibling)
4201         return too_many_arguments(op,op_desc[op->op_type]);
4202
4203     return op;
4204 }
4205
4206 OP *
4207 ck_subr(op)
4208 OP *op;
4209 {
4210     OP *prev = ((cUNOP->op_first->op_sibling)
4211              ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first;
4212     OP *o = prev->op_sibling;
4213     OP *cvop;
4214     char *proto = 0;
4215     CV *cv = 0;
4216     int optional = 0;
4217     I32 arg = 0;
4218
4219     for (cvop = o; cvop->op_sibling; cvop = cvop->op_sibling) ;
4220     if (cvop->op_type == OP_RV2CV) {
4221         SVOP* tmpop;
4222         op->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4223         null(cvop);             /* disable rv2cv */
4224         tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4225         if (tmpop->op_type == OP_GV) {
4226             cv = GvCVu(tmpop->op_sv);
4227             if (cv && SvPOK(cv) && !(op->op_private & OPpENTERSUB_AMPER))
4228                 proto = SvPV((SV*)cv,na);
4229         }
4230     }
4231     op->op_private |= (hints & HINT_STRICT_REFS);
4232     if (perldb && curstash != debstash)
4233         op->op_private |= OPpENTERSUB_DB;
4234     while (o != cvop) {
4235         if (proto) {
4236             switch (*proto) {
4237             case '\0':
4238                 return too_many_arguments(op, CvNAME(cv));
4239             case ';':
4240                 optional = 1;
4241                 proto++;
4242                 continue;
4243             case '$':
4244                 proto++;
4245                 arg++;
4246                 scalar(o);
4247                 break;
4248             case '%':
4249             case '@':
4250                 list(o);
4251                 arg++;
4252                 break;
4253             case '&':
4254                 proto++;
4255                 arg++;
4256                 if (o->op_type != OP_REFGEN && o->op_type != OP_UNDEF)
4257                     bad_type(arg, "block", CvNAME(cv), o);
4258                 break;
4259             case '*':
4260                 proto++;
4261                 arg++;
4262                 if (o->op_type == OP_RV2GV)
4263                     goto wrapref;
4264                 {
4265                     OP* kid = o;
4266                     o = newUNOP(OP_RV2GV, 0, kid);
4267                     o->op_sibling = kid->op_sibling;
4268                     kid->op_sibling = 0;
4269                     prev->op_sibling = o;
4270                 }
4271                 goto wrapref;
4272             case '\\':
4273                 proto++;
4274                 arg++;
4275                 switch (*proto++) {
4276                 case '*':
4277                     if (o->op_type != OP_RV2GV)
4278                         bad_type(arg, "symbol", CvNAME(cv), o);
4279                     goto wrapref;
4280                 case '&':
4281                     if (o->op_type != OP_RV2CV)
4282                         bad_type(arg, "sub", CvNAME(cv), o);
4283                     goto wrapref;
4284                 case '$':
4285                     if (o->op_type != OP_RV2SV && o->op_type != OP_PADSV)
4286                         bad_type(arg, "scalar", CvNAME(cv), o);
4287                     goto wrapref;
4288                 case '@':
4289                     if (o->op_type != OP_RV2AV && o->op_type != OP_PADAV)
4290                         bad_type(arg, "array", CvNAME(cv), o);
4291                     goto wrapref;
4292                 case '%':
4293                     if (o->op_type != OP_RV2HV && o->op_type != OP_PADHV)
4294                         bad_type(arg, "hash", CvNAME(cv), o);
4295                   wrapref:
4296                     {
4297                         OP* kid = o;
4298                         o = newUNOP(OP_REFGEN, 0, kid);
4299                         o->op_sibling = kid->op_sibling;
4300                         kid->op_sibling = 0;
4301                         prev->op_sibling = o;
4302                     }
4303                     break;
4304                 default: goto oops;
4305                 }
4306                 break;
4307             case ' ':
4308                 proto++;
4309                 continue;
4310             default:
4311               oops:
4312                 croak("Malformed prototype for %s: %s",
4313                         CvNAME(cv),SvPV((SV*)cv,na));
4314             }
4315         }
4316         else
4317             list(o);
4318         mod(o, OP_ENTERSUB);
4319         prev = o;
4320         o = o->op_sibling;
4321     }
4322     if (proto && !optional && *proto == '$')
4323         return too_few_arguments(op, CvNAME(cv));
4324     return op;
4325 }
4326
4327 OP *
4328 ck_svconst(op)
4329 OP *op;
4330 {
4331     SvREADONLY_on(cSVOP->op_sv);
4332     return op;
4333 }
4334
4335 OP *
4336 ck_trunc(op)
4337 OP *op;
4338 {
4339     if (op->op_flags & OPf_KIDS) {
4340         SVOP *kid = (SVOP*)cUNOP->op_first;
4341
4342         if (kid->op_type == OP_NULL)
4343             kid = (SVOP*)kid->op_sibling;
4344         if (kid &&
4345           kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4346             op->op_flags |= OPf_SPECIAL;
4347     }
4348     return ck_fun(op);
4349 }
4350
4351 /* A peephole optimizer.  We visit the ops in the order they're to execute. */
4352
4353 void
4354 peep(o)
4355 register OP* o;
4356 {
4357     register OP* oldop = 0;
4358     if (!o || o->op_seq)
4359         return;
4360     ENTER;
4361     SAVESPTR(op);
4362     SAVESPTR(curcop);
4363     for (; o; o = o->op_next) {
4364         if (o->op_seq)
4365             break;
4366         if (!op_seqmax)
4367             op_seqmax++;
4368         op = o;
4369         switch (o->op_type) {
4370         case OP_NEXTSTATE:
4371         case OP_DBSTATE:
4372             curcop = ((COP*)o);         /* for warnings */
4373             o->op_seq = op_seqmax++;
4374             break;
4375
4376         case OP_CONCAT:
4377         case OP_CONST:
4378         case OP_JOIN:
4379         case OP_UC:
4380         case OP_UCFIRST:
4381         case OP_LC:
4382         case OP_LCFIRST:
4383         case OP_QUOTEMETA:
4384             if (o->op_next->op_type == OP_STRINGIFY)
4385                 null(o->op_next);
4386             o->op_seq = op_seqmax++;
4387             break;
4388         case OP_STUB:
4389             if ((o->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
4390                 o->op_seq = op_seqmax++;
4391                 break;  /* Scalar stub must produce undef.  List stub is noop */
4392             }
4393             goto nothin;
4394         case OP_NULL:
4395             if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4396                 curcop = ((COP*)op);
4397             goto nothin;
4398         case OP_SCALAR:
4399         case OP_LINESEQ:
4400         case OP_SCOPE:
4401           nothin:
4402             if (oldop && o->op_next) {
4403                 oldop->op_next = o->op_next;
4404                 continue;
4405             }
4406             o->op_seq = op_seqmax++;
4407             break;
4408
4409         case OP_GV:
4410             if (o->op_next->op_type == OP_RV2SV) {
4411                 if (!(o->op_next->op_private & OPpDEREF)) {
4412                     null(o->op_next);
4413                     o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4414                     o->op_next = o->op_next->op_next;
4415                     o->op_type = OP_GVSV;
4416                     o->op_ppaddr = ppaddr[OP_GVSV];
4417                 }
4418             }
4419             else if (o->op_next->op_type == OP_RV2AV) {
4420                 OP* pop = o->op_next->op_next;
4421                 IV i;
4422                 if (pop->op_type == OP_CONST &&
4423                     (op = pop->op_next) &&
4424                     pop->op_next->op_type == OP_AELEM &&
4425                     !(pop->op_next->op_private & (OPpDEREF|OPpLVAL_INTRO)) &&
4426                     (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4427                                 <= 255 &&
4428                     i >= 0)
4429                 {
4430                     SvREFCNT_dec(((SVOP*)pop)->op_sv);
4431                     null(o->op_next);
4432                     null(pop->op_next);
4433                     null(pop);
4434                     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4435                     o->op_next = pop->op_next->op_next;
4436                     o->op_type = OP_AELEMFAST;
4437                     o->op_ppaddr = ppaddr[OP_AELEMFAST];
4438                     o->op_private = (U8)i;
4439                     GvAVn(((GVOP*)o)->op_gv);
4440                 }
4441             }
4442             o->op_seq = op_seqmax++;
4443             break;
4444
4445         case OP_MAPWHILE:
4446         case OP_GREPWHILE:
4447         case OP_AND:
4448         case OP_OR:
4449             o->op_seq = op_seqmax++;
4450             peep(cLOGOP->op_other);
4451             break;
4452
4453         case OP_COND_EXPR:
4454             o->op_seq = op_seqmax++;
4455             peep(cCONDOP->op_true);
4456             peep(cCONDOP->op_false);
4457             break;
4458
4459         case OP_ENTERLOOP:
4460             o->op_seq = op_seqmax++;
4461             peep(cLOOP->op_redoop);
4462             peep(cLOOP->op_nextop);
4463             peep(cLOOP->op_lastop);
4464             break;
4465
4466         case OP_MATCH:
4467         case OP_SUBST:
4468             o->op_seq = op_seqmax++;
4469             peep(cPMOP->op_pmreplstart);
4470             break;
4471
4472         case OP_EXEC:
4473             o->op_seq = op_seqmax++;
4474             if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4475                 if (o->op_next->op_sibling &&
4476                         o->op_next->op_sibling->op_type != OP_DIE) {
4477                     line_t oldline = curcop->cop_line;
4478
4479                     curcop->cop_line = ((COP*)o->op_next)->cop_line;
4480                     warn("Statement unlikely to be reached");
4481                     warn("(Maybe you meant system() when you said exec()?)\n");
4482                     curcop->cop_line = oldline;
4483                 }
4484             }
4485             break;
4486         default:
4487             o->op_seq = op_seqmax++;
4488             break;
4489         }
4490         oldop = o;
4491     }
4492     LEAVE;
4493 }