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