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