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