f27aa94360f9470ddc6c1f58d8664825d74b6ebd
[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 (right->op_type == OP_MATCH ||
1268         right->op_type == OP_SUBST ||
1269         right->op_type == OP_TRANS) {
1270         right->op_flags |= OPf_STACKED;
1271         if (right->op_type != OP_MATCH)
1272             left = mod(left, right->op_type);
1273         if (right->op_type == OP_TRANS)
1274             op = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1275         else
1276             op = prepend_elem(right->op_type, scalar(left), right);
1277         if (type == OP_NOT)
1278             return newUNOP(OP_NOT, 0, scalar(op));
1279         return op;
1280     }
1281     else
1282         return bind_match(type, left,
1283                 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1284 }
1285
1286 OP *
1287 invert(op)
1288 OP *op;
1289 {
1290     if (!op)
1291         return op;
1292     /* XXX need to optimize away NOT NOT here?  Or do we let optimizer do it? */
1293     return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
1294 }
1295
1296 OP *
1297 scope(o)
1298 OP *o;
1299 {
1300     if (o) {
1301         if (o->op_flags & OPf_PARENS || perldb || tainting) {
1302             o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1303             o->op_type = OP_LEAVE;
1304             o->op_ppaddr = ppaddr[OP_LEAVE];
1305         }
1306         else {
1307             if (o->op_type == OP_LINESEQ) {
1308                 OP *kid;
1309                 o->op_type = OP_SCOPE;
1310                 o->op_ppaddr = ppaddr[OP_SCOPE];
1311                 kid = ((LISTOP*)o)->op_first;
1312                 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1313                     SvREFCNT_dec(((COP*)kid)->cop_filegv);
1314                     null(kid);
1315                 }
1316             }
1317             else
1318                 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1319         }
1320     }
1321     return o;
1322 }
1323
1324 int
1325 block_start(full)
1326 int full;
1327 {
1328     int retval = savestack_ix;
1329     SAVEI32(comppad_name_floor);
1330     if (full) {
1331         if ((comppad_name_fill = AvFILL(comppad_name)) > 0)
1332             comppad_name_floor = comppad_name_fill;
1333         else
1334             comppad_name_floor = 0;
1335     }
1336     SAVEI32(min_intro_pending);
1337     SAVEI32(max_intro_pending);
1338     min_intro_pending = 0;
1339     SAVEI32(comppad_name_fill);
1340     SAVEI32(padix_floor);
1341     padix_floor = padix;
1342     pad_reset_pending = FALSE;
1343     SAVEI32(hints);
1344     hints &= ~HINT_BLOCK_SCOPE;
1345     return retval;
1346 }
1347
1348 OP*
1349 block_end(floor, seq)
1350 I32 floor;
1351 OP* seq;
1352 {
1353     int needblockscope = hints & HINT_BLOCK_SCOPE;
1354     OP* retval = scalarseq(seq);
1355     LEAVE_SCOPE(floor);
1356     pad_reset_pending = FALSE;
1357     if (needblockscope)
1358         hints |= HINT_BLOCK_SCOPE; /* propagate out */
1359     pad_leavemy(comppad_name_fill);
1360     cop_seqmax++;
1361     return retval;
1362 }
1363
1364 void
1365 newPROG(op)
1366 OP *op;
1367 {
1368     if (in_eval) {
1369         eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), op);
1370         eval_start = linklist(eval_root);
1371         eval_root->op_next = 0;
1372         peep(eval_start);
1373     }
1374     else {
1375         if (!op)
1376             return;
1377         main_root = scope(sawparens(scalarvoid(op)));
1378         curcop = &compiling;
1379         main_start = LINKLIST(main_root);
1380         main_root->op_next = 0;
1381         peep(main_start);
1382         compcv = 0;
1383
1384         /* Register with debugger */
1385         if (perldb) {
1386             CV *cv = perl_get_cv("DB::postponed", FALSE);
1387             if (cv) {
1388                 dSP;
1389                 PUSHMARK(sp);
1390                 XPUSHs((SV*)compiling.cop_filegv);
1391                 PUTBACK;
1392                 perl_call_sv((SV*)cv, G_DISCARD);
1393             }
1394         }
1395     }
1396 }
1397
1398 OP *
1399 localize(o, lex)
1400 OP *o;
1401 I32 lex;
1402 {
1403     if (o->op_flags & OPf_PARENS)
1404         list(o);
1405     else {
1406         scalar(o);
1407         if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1408             char *s;
1409             for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1410             if (*s == ';' || *s == '=')
1411                 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1412         }
1413     }
1414     in_my = FALSE;
1415     if (lex)
1416         return my(o);
1417     else
1418         return mod(o, OP_NULL);         /* a bit kludgey */
1419 }
1420
1421 OP *
1422 jmaybe(o)
1423 OP *o;
1424 {
1425     if (o->op_type == OP_LIST) {
1426         o = convert(OP_JOIN, 0,
1427                 prepend_elem(OP_LIST,
1428                     newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1429                     o));
1430     }
1431     return o;
1432 }
1433
1434 OP *
1435 fold_constants(o)
1436 register OP *o;
1437 {
1438     register OP *curop;
1439     I32 type = o->op_type;
1440     SV *sv;
1441
1442     if (opargs[type] & OA_RETSCALAR)
1443         scalar(o);
1444     if (opargs[type] & OA_TARGET)
1445         o->op_targ = pad_alloc(type, SVs_PADTMP);
1446
1447     if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1448         o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1449
1450     if (!(opargs[type] & OA_FOLDCONST))
1451         goto nope;
1452
1453     if (error_count)
1454         goto nope;              /* Don't try to run w/ errors */
1455
1456     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1457         if (curop->op_type != OP_CONST &&
1458                 curop->op_type != OP_LIST &&
1459                 curop->op_type != OP_SCALAR &&
1460                 curop->op_type != OP_NULL &&
1461                 curop->op_type != OP_PUSHMARK) {
1462             goto nope;
1463         }
1464     }
1465
1466     curop = LINKLIST(o);
1467     o->op_next = 0;
1468     op = curop;
1469     runops();
1470     sv = *(stack_sp--);
1471     if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1472         pad_swipe(o->op_targ);
1473     else if (SvTEMP(sv)) {                      /* grab mortal temp? */
1474         (void)SvREFCNT_inc(sv);
1475         SvTEMP_off(sv);
1476     }
1477     op_free(o);
1478     if (type == OP_RV2GV)
1479         return newGVOP(OP_GV, 0, (GV*)sv);
1480     else {
1481         if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1482             IV iv = SvIV(sv);
1483             if ((double)iv == SvNV(sv)) {       /* can we smush double to int */
1484                 SvREFCNT_dec(sv);
1485                 sv = newSViv(iv);
1486             }
1487             else
1488                 SvIOK_off(sv);                  /* undo SvIV() damage */
1489         }
1490         return newSVOP(OP_CONST, 0, sv);
1491     }
1492     
1493   nope:
1494     if (!(opargs[type] & OA_OTHERINT))
1495         return o;
1496
1497     if (!(hints & HINT_INTEGER)) {
1498         if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1499             return o;
1500
1501         for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1502             if (curop->op_type == OP_CONST) {
1503                 if (SvIOK(((SVOP*)curop)->op_sv))
1504                     continue;
1505                 return o;
1506             }
1507             if (opargs[curop->op_type] & OA_RETINTEGER)
1508                 continue;
1509             return o;
1510         }
1511         o->op_ppaddr = ppaddr[++(o->op_type)];
1512     }
1513
1514     return o;
1515 }
1516
1517 OP *
1518 gen_constant_list(o)
1519 register OP *o;
1520 {
1521     register OP *curop;
1522     I32 oldtmps_floor = tmps_floor;
1523
1524     list(o);
1525     if (error_count)
1526         return o;               /* Don't attempt to run with errors */
1527
1528     op = curop = LINKLIST(o);
1529     o->op_next = 0;
1530     pp_pushmark();
1531     runops();
1532     op = curop;
1533     pp_anonlist();
1534     tmps_floor = oldtmps_floor;
1535
1536     o->op_type = OP_RV2AV;
1537     o->op_ppaddr = ppaddr[OP_RV2AV];
1538     curop = ((UNOP*)o)->op_first;
1539     ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1540     op_free(curop);
1541     linklist(o);
1542     return list(o);
1543 }
1544
1545 OP *
1546 convert(type, flags, op)
1547 I32 type;
1548 I32 flags;
1549 OP* op;
1550 {
1551     OP *kid;
1552     OP *last = 0;
1553
1554     if (!op || op->op_type != OP_LIST)
1555         op = newLISTOP(OP_LIST, 0, op, Nullop);
1556     else
1557         op->op_flags &= ~(OPf_KNOW|OPf_LIST);
1558
1559     if (!(opargs[type] & OA_MARK))
1560         null(cLISTOP->op_first);
1561
1562     op->op_type = type;
1563     op->op_ppaddr = ppaddr[type];
1564     op->op_flags |= flags;
1565
1566     op = CHECKOP(type, op);
1567     if (op->op_type != type)
1568         return op;
1569
1570     if (cLISTOP->op_children < 7) {
1571         /* XXX do we really need to do this if we're done appending?? */
1572         for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1573             last = kid;
1574         cLISTOP->op_last = last;        /* in case check substituted last arg */
1575     }
1576
1577     return fold_constants(op);
1578 }
1579
1580 /* List constructors */
1581
1582 OP *
1583 append_elem(type, first, last)
1584 I32 type;
1585 OP* first;
1586 OP* last;
1587 {
1588     if (!first)
1589         return last;
1590
1591     if (!last)
1592         return first;
1593
1594     if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1595             return newLISTOP(type, 0, first, last);
1596
1597     if (first->op_flags & OPf_KIDS)
1598         ((LISTOP*)first)->op_last->op_sibling = last;
1599     else {
1600         first->op_flags |= OPf_KIDS;
1601         ((LISTOP*)first)->op_first = last;
1602     }
1603     ((LISTOP*)first)->op_last = last;
1604     ((LISTOP*)first)->op_children++;
1605     return first;
1606 }
1607
1608 OP *
1609 append_list(type, first, last)
1610 I32 type;
1611 LISTOP* first;
1612 LISTOP* last;
1613 {
1614     if (!first)
1615         return (OP*)last;
1616
1617     if (!last)
1618         return (OP*)first;
1619
1620     if (first->op_type != type)
1621         return prepend_elem(type, (OP*)first, (OP*)last);
1622
1623     if (last->op_type != type)
1624         return append_elem(type, (OP*)first, (OP*)last);
1625
1626     first->op_last->op_sibling = last->op_first;
1627     first->op_last = last->op_last;
1628     first->op_children += last->op_children;
1629     if (first->op_children)
1630         last->op_flags |= OPf_KIDS;
1631
1632     Safefree(last);
1633     return (OP*)first;
1634 }
1635
1636 OP *
1637 prepend_elem(type, first, last)
1638 I32 type;
1639 OP* first;
1640 OP* last;
1641 {
1642     if (!first)
1643         return last;
1644
1645     if (!last)
1646         return first;
1647
1648     if (last->op_type == type) {
1649         if (type == OP_LIST) {  /* already a PUSHMARK there */
1650             first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1651             ((LISTOP*)last)->op_first->op_sibling = first;
1652         }
1653         else {
1654             if (!(last->op_flags & OPf_KIDS)) {
1655                 ((LISTOP*)last)->op_last = first;
1656                 last->op_flags |= OPf_KIDS;
1657             }
1658             first->op_sibling = ((LISTOP*)last)->op_first;
1659             ((LISTOP*)last)->op_first = first;
1660         }
1661         ((LISTOP*)last)->op_children++;
1662         return last;
1663     }
1664
1665     return newLISTOP(type, 0, first, last);
1666 }
1667
1668 /* Constructors */
1669
1670 OP *
1671 newNULLLIST()
1672 {
1673     return newOP(OP_STUB, 0);
1674 }
1675
1676 OP *
1677 force_list(op)
1678 OP* op;
1679 {
1680     if (!op || op->op_type != OP_LIST)
1681         op = newLISTOP(OP_LIST, 0, op, Nullop);
1682     null(op);
1683     return op;
1684 }
1685
1686 OP *
1687 newLISTOP(type, flags, first, last)
1688 I32 type;
1689 I32 flags;
1690 OP* first;
1691 OP* last;
1692 {
1693     LISTOP *listop;
1694
1695     Newz(1101, listop, 1, LISTOP);
1696
1697     listop->op_type = type;
1698     listop->op_ppaddr = ppaddr[type];
1699     listop->op_children = (first != 0) + (last != 0);
1700     listop->op_flags = flags;
1701
1702     if (!last && first)
1703         last = first;
1704     else if (!first && last)
1705         first = last;
1706     else if (first)
1707         first->op_sibling = last;
1708     listop->op_first = first;
1709     listop->op_last = last;
1710     if (type == OP_LIST) {
1711         OP* pushop;
1712         pushop = newOP(OP_PUSHMARK, 0);
1713         pushop->op_sibling = first;
1714         listop->op_first = pushop;
1715         listop->op_flags |= OPf_KIDS;
1716         if (!last)
1717             listop->op_last = pushop;
1718     }
1719     else if (listop->op_children)
1720         listop->op_flags |= OPf_KIDS;
1721
1722     return (OP*)listop;
1723 }
1724
1725 OP *
1726 newOP(type, flags)
1727 I32 type;
1728 I32 flags;
1729 {
1730     OP *op;
1731     Newz(1101, op, 1, OP);
1732     op->op_type = type;
1733     op->op_ppaddr = ppaddr[type];
1734     op->op_flags = flags;
1735
1736     op->op_next = op;
1737     op->op_private = 0 + (flags >> 8);
1738     if (opargs[type] & OA_RETSCALAR)
1739         scalar(op);
1740     if (opargs[type] & OA_TARGET)
1741         op->op_targ = pad_alloc(type, SVs_PADTMP);
1742     return CHECKOP(type, op);
1743 }
1744
1745 OP *
1746 newUNOP(type, flags, first)
1747 I32 type;
1748 I32 flags;
1749 OP* first;
1750 {
1751     UNOP *unop;
1752
1753     if (!first)
1754         first = newOP(OP_STUB, 0); 
1755     if (opargs[type] & OA_MARK)
1756         first = force_list(first);
1757
1758     Newz(1101, unop, 1, UNOP);
1759     unop->op_type = type;
1760     unop->op_ppaddr = ppaddr[type];
1761     unop->op_first = first;
1762     unop->op_flags = flags | OPf_KIDS;
1763     unop->op_private = 1 | (flags >> 8);
1764
1765     unop = (UNOP*) CHECKOP(type, unop);
1766     if (unop->op_next)
1767         return (OP*)unop;
1768
1769     return fold_constants((OP *) unop);
1770 }
1771
1772 OP *
1773 newBINOP(type, flags, first, last)
1774 I32 type;
1775 I32 flags;
1776 OP* first;
1777 OP* last;
1778 {
1779     BINOP *binop;
1780     Newz(1101, binop, 1, BINOP);
1781
1782     if (!first)
1783         first = newOP(OP_NULL, 0);
1784
1785     binop->op_type = type;
1786     binop->op_ppaddr = ppaddr[type];
1787     binop->op_first = first;
1788     binop->op_flags = flags | OPf_KIDS;
1789     if (!last) {
1790         last = first;
1791         binop->op_private = 1 | (flags >> 8);
1792     }
1793     else {
1794         binop->op_private = 2 | (flags >> 8);
1795         first->op_sibling = last;
1796     }
1797
1798     binop = (BINOP*)CHECKOP(type, binop);
1799     if (binop->op_next)
1800         return (OP*)binop;
1801
1802     binop->op_last = last = binop->op_first->op_sibling;
1803
1804     return fold_constants((OP *)binop);
1805 }
1806
1807 OP *
1808 pmtrans(op, expr, repl)
1809 OP *op;
1810 OP *expr;
1811 OP *repl;
1812 {
1813     SV *tstr = ((SVOP*)expr)->op_sv;
1814     SV *rstr = ((SVOP*)repl)->op_sv;
1815     STRLEN tlen;
1816     STRLEN rlen;
1817     register U8 *t = (U8*)SvPV(tstr, tlen);
1818     register U8 *r = (U8*)SvPV(rstr, rlen);
1819     register I32 i;
1820     register I32 j;
1821     I32 delete;
1822     I32 complement;
1823     register short *tbl;
1824
1825     tbl = (short*)cPVOP->op_pv;
1826     complement  = op->op_private & OPpTRANS_COMPLEMENT;
1827     delete      = op->op_private & OPpTRANS_DELETE;
1828     /* squash   = op->op_private & OPpTRANS_SQUASH; */
1829
1830     if (complement) {
1831         Zero(tbl, 256, short);
1832         for (i = 0; i < tlen; i++)
1833             tbl[t[i]] = -1;
1834         for (i = 0, j = 0; i < 256; i++) {
1835             if (!tbl[i]) {
1836                 if (j >= rlen) {
1837                     if (delete)
1838                         tbl[i] = -2;
1839                     else if (rlen)
1840                         tbl[i] = r[j-1];
1841                     else
1842                         tbl[i] = i;
1843                 }
1844                 else
1845                     tbl[i] = r[j++];
1846             }
1847         }
1848     }
1849     else {
1850         if (!rlen && !delete) {
1851             r = t; rlen = tlen;
1852         }
1853         for (i = 0; i < 256; i++)
1854             tbl[i] = -1;
1855         for (i = 0, j = 0; i < tlen; i++,j++) {
1856             if (j >= rlen) {
1857                 if (delete) {
1858                     if (tbl[t[i]] == -1)
1859                         tbl[t[i]] = -2;
1860                     continue;
1861                 }
1862                 --j;
1863             }
1864             if (tbl[t[i]] == -1)
1865                 tbl[t[i]] = r[j];
1866         }
1867     }
1868     op_free(expr);
1869     op_free(repl);
1870
1871     return op;
1872 }
1873
1874 OP *
1875 newPMOP(type, flags)
1876 I32 type;
1877 I32 flags;
1878 {
1879     PMOP *pmop;
1880
1881     Newz(1101, pmop, 1, PMOP);
1882     pmop->op_type = type;
1883     pmop->op_ppaddr = ppaddr[type];
1884     pmop->op_flags = flags;
1885     pmop->op_private = 0 | (flags >> 8);
1886
1887     if (hints & HINT_LOCALE)
1888         pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
1889
1890     /* link into pm list */
1891     if (type != OP_TRANS && curstash) {
1892         pmop->op_pmnext = HvPMROOT(curstash);
1893         HvPMROOT(curstash) = pmop;
1894     }
1895
1896     return (OP*)pmop;
1897 }
1898
1899 OP *
1900 pmruntime(op, expr, repl)
1901 OP *op;
1902 OP *expr;
1903 OP *repl;
1904 {
1905     PMOP *pm;
1906     LOGOP *rcop;
1907
1908     if (op->op_type == OP_TRANS)
1909         return pmtrans(op, expr, repl);
1910
1911     pm = (PMOP*)op;
1912
1913     if (expr->op_type == OP_CONST) {
1914         STRLEN plen;
1915         SV *pat = ((SVOP*)expr)->op_sv;
1916         char *p = SvPV(pat, plen);
1917         if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
1918             sv_setpvn(pat, "\\s+", 3);
1919             p = SvPV(pat, plen);
1920             pm->op_pmflags |= PMf_SKIPWHITE;
1921         }
1922         pm->op_pmregexp = pregcomp(p, p + plen, pm);
1923         if (strEQ("\\s+", pm->op_pmregexp->precomp)) 
1924             pm->op_pmflags |= PMf_WHITE;
1925         hoistmust(pm);
1926         op_free(expr);
1927     }
1928     else {
1929         if (pm->op_pmflags & PMf_KEEP)
1930             expr = newUNOP(OP_REGCMAYBE,0,expr);
1931
1932         Newz(1101, rcop, 1, LOGOP);
1933         rcop->op_type = OP_REGCOMP;
1934         rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1935         rcop->op_first = scalar(expr);
1936         rcop->op_flags |= OPf_KIDS;
1937         rcop->op_private = 1;
1938         rcop->op_other = op;
1939
1940         /* establish postfix order */
1941         if (pm->op_pmflags & PMf_KEEP) {
1942             LINKLIST(expr);
1943             rcop->op_next = expr;
1944             ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1945         }
1946         else {
1947             rcop->op_next = LINKLIST(expr);
1948             expr->op_next = (OP*)rcop;
1949         }
1950
1951         prepend_elem(op->op_type, scalar((OP*)rcop), op);
1952     }
1953
1954     if (repl) {
1955         OP *curop;
1956         if (pm->op_pmflags & PMf_EVAL)
1957             curop = 0;
1958         else if (repl->op_type == OP_CONST)
1959             curop = repl;
1960         else {
1961             OP *lastop = 0;
1962             for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1963                 if (opargs[curop->op_type] & OA_DANGEROUS) {
1964                     if (curop->op_type == OP_GV) {
1965                         GV *gv = ((GVOP*)curop)->op_gv;
1966                         if (strchr("&`'123456789+", *GvENAME(gv)))
1967                             break;
1968                     }
1969                     else if (curop->op_type == OP_RV2CV)
1970                         break;
1971                     else if (curop->op_type == OP_RV2SV ||
1972                              curop->op_type == OP_RV2AV ||
1973                              curop->op_type == OP_RV2HV ||
1974                              curop->op_type == OP_RV2GV) {
1975                         if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1976                             break;
1977                     }
1978                     else if (curop->op_type == OP_PADSV ||
1979                              curop->op_type == OP_PADAV ||
1980                              curop->op_type == OP_PADHV ||
1981                              curop->op_type == OP_PADANY) {
1982                              /* is okay */
1983                     }
1984                     else
1985                         break;
1986                 }
1987                 lastop = curop;
1988             }
1989         }
1990         if (curop == repl) {
1991             pm->op_pmflags |= PMf_CONST;        /* const for long enough */
1992             pm->op_pmpermflags |= PMf_CONST;    /* const for long enough */
1993             prepend_elem(op->op_type, scalar(repl), op);
1994         }
1995         else {
1996             Newz(1101, rcop, 1, LOGOP);
1997             rcop->op_type = OP_SUBSTCONT;
1998             rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1999             rcop->op_first = scalar(repl);
2000             rcop->op_flags |= OPf_KIDS;
2001             rcop->op_private = 1;
2002             rcop->op_other = op;
2003
2004             /* establish postfix order */
2005             rcop->op_next = LINKLIST(repl);
2006             repl->op_next = (OP*)rcop;
2007
2008             pm->op_pmreplroot = scalar((OP*)rcop);
2009             pm->op_pmreplstart = LINKLIST(rcop);
2010             rcop->op_next = 0;
2011         }
2012     }
2013
2014     return (OP*)pm;
2015 }
2016
2017 OP *
2018 newSVOP(type, flags, sv)
2019 I32 type;
2020 I32 flags;
2021 SV *sv;
2022 {
2023     SVOP *svop;
2024     Newz(1101, svop, 1, SVOP);
2025     svop->op_type = type;
2026     svop->op_ppaddr = ppaddr[type];
2027     svop->op_sv = sv;
2028     svop->op_next = (OP*)svop;
2029     svop->op_flags = flags;
2030     if (opargs[type] & OA_RETSCALAR)
2031         scalar((OP*)svop);
2032     if (opargs[type] & OA_TARGET)
2033         svop->op_targ = pad_alloc(type, SVs_PADTMP);
2034     return CHECKOP(type, svop);
2035 }
2036
2037 OP *
2038 newGVOP(type, flags, gv)
2039 I32 type;
2040 I32 flags;
2041 GV *gv;
2042 {
2043     GVOP *gvop;
2044     Newz(1101, gvop, 1, GVOP);
2045     gvop->op_type = type;
2046     gvop->op_ppaddr = ppaddr[type];
2047     gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2048     gvop->op_next = (OP*)gvop;
2049     gvop->op_flags = flags;
2050     if (opargs[type] & OA_RETSCALAR)
2051         scalar((OP*)gvop);
2052     if (opargs[type] & OA_TARGET)
2053         gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2054     return CHECKOP(type, gvop);
2055 }
2056
2057 OP *
2058 newPVOP(type, flags, pv)
2059 I32 type;
2060 I32 flags;
2061 char *pv;
2062 {
2063     PVOP *pvop;
2064     Newz(1101, pvop, 1, PVOP);
2065     pvop->op_type = type;
2066     pvop->op_ppaddr = ppaddr[type];
2067     pvop->op_pv = pv;
2068     pvop->op_next = (OP*)pvop;
2069     pvop->op_flags = flags;
2070     if (opargs[type] & OA_RETSCALAR)
2071         scalar((OP*)pvop);
2072     if (opargs[type] & OA_TARGET)
2073         pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2074     return CHECKOP(type, pvop);
2075 }
2076
2077 void
2078 package(op)
2079 OP *op;
2080 {
2081     SV *sv;
2082
2083     save_hptr(&curstash);
2084     save_item(curstname);
2085     if (op) {
2086         STRLEN len;
2087         char *name;
2088         sv = cSVOP->op_sv;
2089         name = SvPV(sv, len);
2090         curstash = gv_stashpvn(name,len,TRUE);
2091         sv_setpvn(curstname, name, len);
2092         op_free(op);
2093     }
2094     else {
2095         sv_setpv(curstname,"<none>");
2096         curstash = Nullhv;
2097     }
2098     copline = NOLINE;
2099     expect = XSTATE;
2100 }
2101
2102 void
2103 utilize(aver, floor, version, id, arg)
2104 int aver;
2105 I32 floor;
2106 OP *version;
2107 OP *id;
2108 OP *arg;
2109 {
2110     OP *pack;
2111     OP *meth;
2112     OP *rqop;
2113     OP *imop;
2114     OP *veop;
2115
2116     if (id->op_type != OP_CONST)
2117         croak("Module name must be constant");
2118
2119     veop = Nullop;
2120
2121     if(version != Nullop) {
2122         SV *vesv = ((SVOP*)version)->op_sv;
2123
2124         if (arg == Nullop && !SvNIOK(vesv)) {
2125             arg = version;
2126         }
2127         else {
2128             OP *pack;
2129             OP *meth;
2130
2131             if (version->op_type != OP_CONST || !SvNIOK(vesv))
2132                 croak("Version number must be constant number");
2133
2134             /* Make copy of id so we don't free it twice */
2135             pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2136
2137             /* Fake up a method call to VERSION */
2138             meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2139             veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2140                             append_elem(OP_LIST,
2141                             prepend_elem(OP_LIST, pack, list(version)),
2142                             newUNOP(OP_METHOD, 0, meth)));
2143         }
2144     }
2145      
2146     /* Fake up an import/unimport */
2147     if (arg && arg->op_type == OP_STUB)
2148         imop = arg;             /* no import on explicit () */
2149     else if(SvNIOK(((SVOP*)id)->op_sv)) {
2150         imop = Nullop;          /* use 5.0; */
2151     }
2152     else {
2153         /* Make copy of id so we don't free it twice */
2154         pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2155         meth = newSVOP(OP_CONST, 0,
2156             aver
2157                 ? newSVpv("import", 6)
2158                 : newSVpv("unimport", 8)
2159             );
2160         imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2161                     append_elem(OP_LIST,
2162                         prepend_elem(OP_LIST, pack, list(arg)),
2163                         newUNOP(OP_METHOD, 0, meth)));
2164     }
2165
2166     /* Fake up a require */
2167     rqop = newUNOP(OP_REQUIRE, 0, id);
2168
2169     /* Fake up the BEGIN {}, which does its thing immediately. */
2170     newSUB(floor,
2171         newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2172         Nullop,
2173         append_elem(OP_LINESEQ,
2174             append_elem(OP_LINESEQ,
2175                 newSTATEOP(0, Nullch, rqop),
2176                 newSTATEOP(0, Nullch, veop)),
2177             newSTATEOP(0, Nullch, imop) ));
2178
2179     copline = NOLINE;
2180     expect = XSTATE;
2181 }
2182
2183 OP *
2184 newSLICEOP(flags, subscript, listval)
2185 I32 flags;
2186 OP *subscript;
2187 OP *listval;
2188 {
2189     return newBINOP(OP_LSLICE, flags,
2190             list(force_list(subscript)),
2191             list(force_list(listval)) );
2192 }
2193
2194 static I32
2195 list_assignment(op)
2196 register OP *op;
2197 {
2198     if (!op)
2199         return TRUE;
2200
2201     if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
2202         op = cUNOP->op_first;
2203
2204     if (op->op_type == OP_COND_EXPR) {
2205         I32 t = list_assignment(cCONDOP->op_first->op_sibling);
2206         I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
2207
2208         if (t && f)
2209             return TRUE;
2210         if (t || f)
2211             yyerror("Assignment to both a list and a scalar");
2212         return FALSE;
2213     }
2214
2215     if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
2216         op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
2217         op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
2218         return TRUE;
2219
2220     if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
2221         return TRUE;
2222
2223     if (op->op_type == OP_RV2SV)
2224         return FALSE;
2225
2226     return FALSE;
2227 }
2228
2229 OP *
2230 newASSIGNOP(flags, left, optype, right)
2231 I32 flags;
2232 OP *left;
2233 I32 optype;
2234 OP *right;
2235 {
2236     OP *op;
2237
2238     if (optype) {
2239         if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2240             return newLOGOP(optype, 0,
2241                 mod(scalar(left), optype),
2242                 newUNOP(OP_SASSIGN, 0, scalar(right)));
2243         }
2244         else {
2245             return newBINOP(optype, OPf_STACKED,
2246                 mod(scalar(left), optype), scalar(right));
2247         }
2248     }
2249
2250     if (list_assignment(left)) {
2251         modcount = 0;
2252         eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
2253         left = mod(left, OP_AASSIGN);
2254         if (eval_start)
2255             eval_start = 0;
2256         else {
2257             op_free(left);
2258             op_free(right);
2259             return Nullop;
2260         }
2261         op = newBINOP(OP_AASSIGN, flags,
2262                 list(force_list(right)),
2263                 list(force_list(left)) );
2264         op->op_private = 0 | (flags >> 8);
2265         if (!(left->op_private & OPpLVAL_INTRO)) {
2266             static int generation = 100;
2267             OP *curop;
2268             OP *lastop = op;
2269             generation++;
2270             for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
2271                 if (opargs[curop->op_type] & OA_DANGEROUS) {
2272                     if (curop->op_type == OP_GV) {
2273                         GV *gv = ((GVOP*)curop)->op_gv;
2274                         if (gv == defgv || SvCUR(gv) == generation)
2275                             break;
2276                         SvCUR(gv) = generation;
2277                     }
2278                     else if (curop->op_type == OP_PADSV ||
2279                              curop->op_type == OP_PADAV ||
2280                              curop->op_type == OP_PADHV ||
2281                              curop->op_type == OP_PADANY) {
2282                         SV **svp = AvARRAY(comppad_name);
2283                         SV *sv = svp[curop->op_targ];
2284                         if (SvCUR(sv) == generation)
2285                             break;
2286                         SvCUR(sv) = generation; /* (SvCUR not used any more) */
2287                     }
2288                     else if (curop->op_type == OP_RV2CV)
2289                         break;
2290                     else if (curop->op_type == OP_RV2SV ||
2291                              curop->op_type == OP_RV2AV ||
2292                              curop->op_type == OP_RV2HV ||
2293                              curop->op_type == OP_RV2GV) {
2294                         if (lastop->op_type != OP_GV)   /* funny deref? */
2295                             break;
2296                     }
2297                     else
2298                         break;
2299                 }
2300                 lastop = curop;
2301             }
2302             if (curop != op)
2303                 op->op_private = OPpASSIGN_COMMON;
2304         }
2305         if (right && right->op_type == OP_SPLIT) {
2306             OP* tmpop;
2307             if ((tmpop = ((LISTOP*)right)->op_first) &&
2308                 tmpop->op_type == OP_PUSHRE)
2309             {
2310                 PMOP *pm = (PMOP*)tmpop;
2311                 if (left->op_type == OP_RV2AV &&
2312                     !(left->op_private & OPpLVAL_INTRO) &&
2313                     !(op->op_private & OPpASSIGN_COMMON) )
2314                 {
2315                     tmpop = ((UNOP*)left)->op_first;
2316                     if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2317                         pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2318                         pm->op_pmflags |= PMf_ONCE;
2319                         tmpop = ((UNOP*)op)->op_first;  /* to list (nulled) */
2320                         tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2321                         tmpop->op_sibling = Nullop;     /* don't free split */
2322                         right->op_next = tmpop->op_next;  /* fix starting loc */
2323                         op_free(op);                    /* blow off assign */
2324                         right->op_flags &= ~(OPf_KNOW|OPf_LIST);
2325                                 /* "I don't know and I don't care." */
2326                         return right;
2327                     }
2328                 }
2329                 else {
2330                     if (modcount < 10000 &&
2331                       ((LISTOP*)right)->op_last->op_type == OP_CONST)
2332                     {
2333                         SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2334                         if (SvIVX(sv) == 0)
2335                             sv_setiv(sv, modcount+1);
2336                     }
2337                 }
2338             }
2339         }
2340         return op;
2341     }
2342     if (!right)
2343         right = newOP(OP_UNDEF, 0);
2344     if (right->op_type == OP_READLINE) {
2345         right->op_flags |= OPf_STACKED;
2346         return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2347     }
2348     else {
2349         eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
2350         op = newBINOP(OP_SASSIGN, flags,
2351             scalar(right), mod(scalar(left), OP_SASSIGN) );
2352         if (eval_start)
2353             eval_start = 0;
2354         else {
2355             op_free(op);
2356             return Nullop;
2357         }
2358     }
2359     return op;
2360 }
2361
2362 OP *
2363 newSTATEOP(flags, label, op)
2364 I32 flags;
2365 char *label;
2366 OP *op;
2367 {
2368     U32 seq = intro_my();
2369     register COP *cop;
2370
2371     Newz(1101, cop, 1, COP);
2372     if (perldb && curcop->cop_line && curstash != debstash) {
2373         cop->op_type = OP_DBSTATE;
2374         cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2375     }
2376     else {
2377         cop->op_type = OP_NEXTSTATE;
2378         cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2379     }
2380     cop->op_flags = flags;
2381     cop->op_private = 0 | (flags >> 8);
2382 #ifdef NATIVE_HINTS
2383     cop->op_private |= NATIVE_HINTS;
2384 #endif
2385     cop->op_next = (OP*)cop;
2386
2387     if (label) {
2388         cop->cop_label = label;
2389         hints |= HINT_BLOCK_SCOPE;
2390     }
2391     cop->cop_seq = seq;
2392     cop->cop_arybase = curcop->cop_arybase;
2393
2394     if (copline == NOLINE)
2395         cop->cop_line = curcop->cop_line;
2396     else {
2397         cop->cop_line = copline;
2398         copline = NOLINE;
2399     }
2400     cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2401     cop->cop_stash = curstash;
2402
2403     if (perldb && curstash != debstash) {
2404         SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2405         if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2406             (void)SvIOK_on(*svp);
2407             SvIVX(*svp) = 1;
2408             SvSTASH(*svp) = (HV*)cop;
2409         }
2410     }
2411
2412     return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2413 }
2414
2415 /* "Introduce" my variables to visible status. */
2416 U32
2417 intro_my()
2418 {
2419     SV **svp;
2420     SV *sv;
2421     I32 i;
2422
2423     if (! min_intro_pending)
2424         return cop_seqmax;
2425
2426     svp = AvARRAY(comppad_name);
2427     for (i = min_intro_pending; i <= max_intro_pending; i++) {
2428         if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2429             SvIVX(sv) = 999999999;      /* Don't know scope end yet. */
2430             SvNVX(sv) = (double)cop_seqmax;
2431         }
2432     }
2433     min_intro_pending = 0;
2434     comppad_name_fill = max_intro_pending;      /* Needn't search higher */
2435     return cop_seqmax++;
2436 }
2437
2438 OP *
2439 newLOGOP(type, flags, first, other)
2440 I32 type;
2441 I32 flags;
2442 OP* first;
2443 OP* other;
2444 {
2445     LOGOP *logop;
2446     OP *op;
2447
2448     if (type == OP_XOR)         /* Not short circuit, but here by precedence. */
2449         return newBINOP(type, flags, scalar(first), scalar(other));
2450
2451     scalarboolean(first);
2452     /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2453     if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2454         if (type == OP_AND || type == OP_OR) {
2455             if (type == OP_AND)
2456                 type = OP_OR;
2457             else
2458                 type = OP_AND;
2459             op = first;
2460             first = cUNOP->op_first;
2461             if (op->op_next)
2462                 first->op_next = op->op_next;
2463             cUNOP->op_first = Nullop;
2464             op_free(op);
2465         }
2466     }
2467     if (first->op_type == OP_CONST) {
2468         if (dowarn && (first->op_private & OPpCONST_BARE))
2469             warn("Probable precedence problem on %s", op_desc[type]);
2470         if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2471             op_free(first);
2472             return other;
2473         }
2474         else {
2475             op_free(other);
2476             return first;
2477         }
2478     }
2479     else if (first->op_type == OP_WANTARRAY) {
2480         if (type == OP_AND)
2481             list(other);
2482         else
2483             scalar(other);
2484     }
2485     else if (dowarn && (first->op_flags & OPf_KIDS)) {
2486         OP *k1 = ((UNOP*)first)->op_first;
2487         OP *k2 = k1->op_sibling;
2488         OPCODE warnop = 0;
2489         switch (first->op_type)
2490         {
2491         case OP_NULL:
2492             if (k2 && k2->op_type == OP_READLINE
2493                   && (k2->op_flags & OPf_STACKED)
2494                   && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2495                 warnop = k2->op_type;
2496             break;
2497
2498         case OP_SASSIGN:
2499             if (k1->op_type == OP_READDIR || k1->op_type == OP_GLOB)
2500                 warnop = k1->op_type;
2501             break;
2502         }
2503         if (warnop) {
2504             line_t oldline = curcop->cop_line;
2505             curcop->cop_line = copline;
2506             warn("Value of %s construct can be \"0\"; test with defined()",
2507                  op_desc[warnop]);
2508                 curcop->cop_line = oldline;
2509         }
2510     }
2511
2512     if (!other)
2513         return first;
2514
2515     if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2516         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
2517
2518     Newz(1101, logop, 1, LOGOP);
2519
2520     logop->op_type = type;
2521     logop->op_ppaddr = ppaddr[type];
2522     logop->op_first = first;
2523     logop->op_flags = flags | OPf_KIDS;
2524     logop->op_other = LINKLIST(other);
2525     logop->op_private = 1 | (flags >> 8);
2526
2527     /* establish postfix order */
2528     logop->op_next = LINKLIST(first);
2529     first->op_next = (OP*)logop;
2530     first->op_sibling = other;
2531
2532     op = newUNOP(OP_NULL, 0, (OP*)logop);
2533     other->op_next = op;
2534
2535     return op;
2536 }
2537
2538 OP *
2539 newCONDOP(flags, first, trueop, falseop)
2540 I32 flags;
2541 OP* first;
2542 OP* trueop;
2543 OP* falseop;
2544 {
2545     CONDOP *condop;
2546     OP *op;
2547
2548     if (!falseop)
2549         return newLOGOP(OP_AND, 0, first, trueop);
2550     if (!trueop)
2551         return newLOGOP(OP_OR, 0, first, falseop);
2552
2553     scalarboolean(first);
2554     if (first->op_type == OP_CONST) {
2555         if (SvTRUE(((SVOP*)first)->op_sv)) {
2556             op_free(first);
2557             op_free(falseop);
2558             return trueop;
2559         }
2560         else {
2561             op_free(first);
2562             op_free(trueop);
2563             return falseop;
2564         }
2565     }
2566     else if (first->op_type == OP_WANTARRAY) {
2567         list(trueop);
2568         scalar(falseop);
2569     }
2570     Newz(1101, condop, 1, CONDOP);
2571
2572     condop->op_type = OP_COND_EXPR;
2573     condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2574     condop->op_first = first;
2575     condop->op_flags = flags | OPf_KIDS;
2576     condop->op_true = LINKLIST(trueop);
2577     condop->op_false = LINKLIST(falseop);
2578     condop->op_private = 1 | (flags >> 8);
2579
2580     /* establish postfix order */
2581     condop->op_next = LINKLIST(first);
2582     first->op_next = (OP*)condop;
2583
2584     first->op_sibling = trueop;
2585     trueop->op_sibling = falseop;
2586     op = newUNOP(OP_NULL, 0, (OP*)condop);
2587
2588     trueop->op_next = op;
2589     falseop->op_next = op;
2590
2591     return op;
2592 }
2593
2594 OP *
2595 newRANGE(flags, left, right)
2596 I32 flags;
2597 OP *left;
2598 OP *right;
2599 {
2600     CONDOP *condop;
2601     OP *flip;
2602     OP *flop;
2603     OP *op;
2604
2605     Newz(1101, condop, 1, CONDOP);
2606
2607     condop->op_type = OP_RANGE;
2608     condop->op_ppaddr = ppaddr[OP_RANGE];
2609     condop->op_first = left;
2610     condop->op_flags = OPf_KIDS;
2611     condop->op_true = LINKLIST(left);
2612     condop->op_false = LINKLIST(right);
2613     condop->op_private = 1 | (flags >> 8);
2614
2615     left->op_sibling = right;
2616
2617     condop->op_next = (OP*)condop;
2618     flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2619     flop = newUNOP(OP_FLOP, 0, flip);
2620     op = newUNOP(OP_NULL, 0, flop);
2621     linklist(flop);
2622
2623     left->op_next = flip;
2624     right->op_next = flop;
2625
2626     condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2627     sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2628     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2629     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2630
2631     flip->op_private =  left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2632     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2633
2634     flip->op_next = op;
2635     if (!flip->op_private || !flop->op_private)
2636         linklist(op);           /* blow off optimizer unless constant */
2637
2638     return op;
2639 }
2640
2641 OP *
2642 newLOOPOP(flags, debuggable, expr, block)
2643 I32 flags;
2644 I32 debuggable;
2645 OP *expr;
2646 OP *block;
2647 {
2648     OP* listop;
2649     OP* op;
2650     int once = block && block->op_flags & OPf_SPECIAL &&
2651       (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2652
2653     if (expr) {
2654         if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2655             return block;       /* do {} while 0 does once */
2656         if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB) {
2657             expr = newUNOP(OP_DEFINED, 0,
2658                 newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2659         }
2660     }
2661
2662     listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2663     op = newLOGOP(OP_AND, 0, expr, listop);
2664
2665     ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2666
2667     if (once && op != listop)
2668         op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2669
2670     if (op == listop)
2671         op = newUNOP(OP_NULL, 0, op);   /* or do {} while 1 loses outer block */
2672
2673     op->op_flags |= flags;
2674     op = scope(op);
2675     op->op_flags |= OPf_SPECIAL;        /* suppress POPBLOCK curpm restoration*/
2676     return op;
2677 }
2678
2679 OP *
2680 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2681 I32 flags;
2682 I32 debuggable;
2683 LOOP *loop;
2684 OP *expr;
2685 OP *block;
2686 OP *cont;
2687 {
2688     OP *redo;
2689     OP *next = 0;
2690     OP *listop;
2691     OP *op;
2692     OP *condop;
2693
2694     if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2695         expr = newUNOP(OP_DEFINED, 0,
2696             newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2697     }
2698
2699     if (!block)
2700         block = newOP(OP_NULL, 0);
2701
2702     if (cont)
2703         next = LINKLIST(cont);
2704     if (expr)
2705         cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2706
2707     listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2708     redo = LINKLIST(listop);
2709
2710     if (expr) {
2711         op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2712         if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2713             op_free(expr);              /* oops, it's a while (0) */
2714             op_free((OP*)loop);
2715             return Nullop;              /* (listop already freed by newLOGOP) */
2716         }
2717         ((LISTOP*)listop)->op_last->op_next = condop = 
2718             (op == listop ? redo : LINKLIST(op));
2719         if (!next)
2720             next = condop;
2721     }
2722     else
2723         op = listop;
2724
2725     if (!loop) {
2726         Newz(1101,loop,1,LOOP);
2727         loop->op_type = OP_ENTERLOOP;
2728         loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2729         loop->op_private = 0;
2730         loop->op_next = (OP*)loop;
2731     }
2732
2733     op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2734
2735     loop->op_redoop = redo;
2736     loop->op_lastop = op;
2737
2738     if (next)
2739         loop->op_nextop = next;
2740     else
2741         loop->op_nextop = op;
2742
2743     op->op_flags |= flags;
2744     op->op_private |= (flags >> 8);
2745     return op;
2746 }
2747
2748 OP *
2749 #ifndef CAN_PROTOTYPE
2750 newFOROP(flags,label,forline,sv,expr,block,cont)
2751 I32 flags;
2752 char *label;
2753 line_t forline;
2754 OP* sv;
2755 OP* expr;
2756 OP*block;
2757 OP*cont;
2758 #else
2759 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2760 #endif /* CAN_PROTOTYPE */
2761 {
2762     LOOP *loop;
2763     int padoff = 0;
2764     I32 iterflags = 0;
2765
2766     copline = forline;
2767     if (sv) {
2768         if (sv->op_type == OP_RV2SV) {  /* symbol table variable */
2769             sv->op_type = OP_RV2GV;
2770             sv->op_ppaddr = ppaddr[OP_RV2GV];
2771         }
2772         else if (sv->op_type == OP_PADSV) { /* private variable */
2773             padoff = sv->op_targ;
2774             op_free(sv);
2775             sv = Nullop;
2776         }
2777         else
2778             croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2779     }
2780     else {
2781         sv = newGVOP(OP_GV, 0, defgv);
2782     }
2783     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2784         expr = scalar(ref(expr, OP_ITER));
2785         iterflags |= OPf_STACKED;
2786     }
2787     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2788         append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2789                     scalar(sv))));
2790     assert(!loop->op_next);
2791     Renew(loop, 1, LOOP);
2792     loop->op_targ = padoff;
2793     return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2794         newOP(OP_ITER, 0), block, cont));
2795 }
2796
2797 OP*
2798 newLOOPEX(type, label)
2799 I32 type;
2800 OP* label;
2801 {
2802     OP *op;
2803     if (type != OP_GOTO || label->op_type == OP_CONST) {
2804         op = newPVOP(type, 0, savepv(
2805                 label->op_type == OP_CONST
2806                     ? SvPVx(((SVOP*)label)->op_sv, na)
2807                     : "" ));
2808         op_free(label);
2809     }
2810     else {
2811         if (label->op_type == OP_ENTERSUB)
2812             label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2813         op = newUNOP(type, OPf_STACKED, label);
2814     }
2815     hints |= HINT_BLOCK_SCOPE;
2816     return op;
2817 }
2818
2819 void
2820 cv_undef(cv)
2821 CV *cv;
2822 {
2823     if (!CvXSUB(cv) && CvROOT(cv)) {
2824         if (CvDEPTH(cv))
2825             croak("Can't undef active subroutine");
2826         ENTER;
2827
2828         SAVESPTR(curpad);
2829         curpad = 0;
2830
2831         if (!CvCLONED(cv))
2832             op_free(CvROOT(cv));
2833         CvROOT(cv) = Nullop;
2834         LEAVE;
2835     }
2836     CvFLAGS(cv) = 0;
2837     SvREFCNT_dec(CvGV(cv));
2838     CvGV(cv) = Nullgv;
2839     SvREFCNT_dec(CvOUTSIDE(cv));
2840     CvOUTSIDE(cv) = Nullcv;
2841     if (CvPADLIST(cv)) {
2842         /* may be during global destruction */
2843         if (SvREFCNT(CvPADLIST(cv))) {
2844             I32 i = AvFILL(CvPADLIST(cv));
2845             while (i >= 0) {
2846                 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2847                 if (svp)
2848                     SvREFCNT_dec(*svp);
2849             }
2850             SvREFCNT_dec((SV*)CvPADLIST(cv));
2851         }
2852         CvPADLIST(cv) = Nullav;
2853     }
2854 }
2855
2856 #ifdef DEBUG_CLOSURES
2857 static void
2858 cv_dump(cv)
2859 CV* cv;
2860 {
2861     CV *outside = CvOUTSIDE(cv);
2862     AV* padlist = CvPADLIST(cv);
2863     AV* pad_name;
2864     AV* pad;
2865     SV** pname;
2866     SV** ppad;
2867     I32 ix;
2868
2869     PerlIO_printf(Perl_debug_log, "\tCV=0x%p (%s), OUTSIDE=0x%p (%s)\n",
2870                   cv,
2871                   (CvANON(cv) ? "ANON"
2872                    : (cv == main_cv) ? "MAIN"
2873                    : CvUNIQUE(outside) ? "UNIQUE"
2874                    : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
2875                   outside,
2876                   (!outside ? "null"
2877                    : CvANON(outside) ? "ANON"
2878                    : (outside == main_cv) ? "MAIN"
2879                    : CvUNIQUE(outside) ? "UNIQUE"
2880                    : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
2881
2882     if (!padlist)
2883         return;
2884
2885     pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
2886     pad = (AV*)*av_fetch(padlist, 1, FALSE);
2887     pname = AvARRAY(pad_name);
2888     ppad = AvARRAY(pad);
2889
2890     for (ix = 1; ix <= AvFILL(pad_name); ix++) {
2891         if (SvPOK(pname[ix]))
2892             PerlIO_printf(Perl_debug_log, "\t%4d. 0x%p (%s\"%s\" %ld-%ld)\n",
2893                           ix, ppad[ix],
2894                           SvFAKE(pname[ix]) ? "FAKE " : "",
2895                           SvPVX(pname[ix]),
2896                           (long)I_32(SvNVX(pname[ix])),
2897                           (long)SvIVX(pname[ix]));
2898     }
2899 }
2900 #endif /* DEBUG_CLOSURES */
2901
2902 static CV *
2903 cv_clone2(proto, outside)
2904 CV* proto;
2905 CV* outside;
2906 {
2907     AV* av;
2908     I32 ix;
2909     AV* protopadlist = CvPADLIST(proto);
2910     AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
2911     AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
2912     SV** pname = AvARRAY(protopad_name);
2913     SV** ppad = AvARRAY(protopad);
2914     I32 fname = AvFILL(protopad_name);
2915     I32 fpad = AvFILL(protopad);
2916     AV* comppadlist;
2917     CV* cv;
2918
2919     assert(!CvUNIQUE(proto));
2920
2921     ENTER;
2922     SAVESPTR(curpad);
2923     SAVESPTR(comppad);
2924     SAVESPTR(compcv);
2925
2926     cv = compcv = (CV*)NEWSV(1104,0);
2927     sv_upgrade((SV *)cv, SvTYPE(proto));
2928     CvCLONED_on(cv);
2929     if (CvANON(proto))
2930         CvANON_on(cv);
2931
2932     CvFILEGV(cv)        = CvFILEGV(proto);
2933     CvGV(cv)            = (GV*)SvREFCNT_inc(CvGV(proto));
2934     CvSTASH(cv)         = CvSTASH(proto);
2935     CvROOT(cv)          = CvROOT(proto);
2936     CvSTART(cv)         = CvSTART(proto);
2937     if (outside)
2938         CvOUTSIDE(cv)   = (CV*)SvREFCNT_inc(outside);
2939
2940     comppad = newAV();
2941
2942     comppadlist = newAV();
2943     AvREAL_off(comppadlist);
2944     av_store(comppadlist, 0, SvREFCNT_inc((SV*)protopad_name));
2945     av_store(comppadlist, 1, (SV*)comppad);
2946     CvPADLIST(cv) = comppadlist;
2947     av_fill(comppad, AvFILL(protopad));
2948     curpad = AvARRAY(comppad);
2949
2950     av = newAV();           /* will be @_ */
2951     av_extend(av, 0);
2952     av_store(comppad, 0, (SV*)av);
2953     AvFLAGS(av) = AVf_REIFY;
2954
2955     for (ix = fpad; ix > 0; ix--) {
2956         SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
2957         if (namesv && namesv != &sv_undef) {
2958             char *name = SvPVX(namesv);    /* XXX */
2959             if (SvFLAGS(namesv) & SVf_FAKE) {   /* lexical from outside? */
2960                 I32 off = pad_findlex(name, ix, SvIVX(namesv),
2961                                       CvOUTSIDE(cv), cxstack_ix);
2962                 if (!off)
2963                     curpad[ix] = SvREFCNT_inc(ppad[ix]);
2964                 else if (off != ix)
2965                     croak("panic: cv_clone: %s", name);
2966             }
2967             else {                              /* our own lexical */
2968                 SV* sv;
2969                 if (*name == '&') {
2970                     /* anon code -- we'll come back for it */
2971                     sv = SvREFCNT_inc(ppad[ix]);
2972                 }
2973                 else if (*name == '@')
2974                     sv = (SV*)newAV();
2975                 else if (*name == '%')
2976                     sv = (SV*)newHV();
2977                 else
2978                     sv = NEWSV(0,0);
2979                 if (!SvPADBUSY(sv))
2980                     SvPADMY_on(sv);
2981                 curpad[ix] = sv;
2982             }
2983         }
2984         else {
2985             SV* sv = NEWSV(0,0);
2986             SvPADTMP_on(sv);
2987             curpad[ix] = sv;
2988         }
2989     }
2990
2991     /* Now that vars are all in place, clone nested closures. */
2992
2993     for (ix = fpad; ix > 0; ix--) {
2994         SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
2995         if (namesv
2996             && namesv != &sv_undef
2997             && !(SvFLAGS(namesv) & SVf_FAKE)
2998             && *SvPVX(namesv) == '&'
2999             && CvCLONE(ppad[ix]))
3000         {
3001             CV *kid = cv_clone2((CV*)ppad[ix], cv);
3002             SvREFCNT_dec(ppad[ix]);
3003             CvCLONE_on(kid);
3004             SvPADMY_on(kid);
3005             curpad[ix] = (SV*)kid;
3006         }
3007     }
3008
3009 #ifdef DEBUG_CLOSURES
3010     PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3011     cv_dump(outside);
3012     PerlIO_printf(Perl_debug_log, "  from:\n");
3013     cv_dump(proto);
3014     PerlIO_printf(Perl_debug_log, "   to:\n");
3015     cv_dump(cv);
3016 #endif
3017
3018     LEAVE;
3019     return cv;
3020 }
3021
3022 CV *
3023 cv_clone(proto)
3024 CV* proto;
3025 {
3026     return cv_clone2(proto, CvOUTSIDE(proto));
3027 }
3028
3029 SV *
3030 cv_const_sv(cv)
3031 CV *cv;
3032 {
3033     OP *o;
3034     SV *sv = Nullsv;
3035     
3036     if(cv && SvPOK(cv) && !SvCUR(cv)) {
3037         for (o = CvSTART(cv); o; o = o->op_next) {
3038             OPCODE type = o->op_type;
3039         
3040             if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3041                 continue;
3042             if (type == OP_LEAVESUB || type == OP_RETURN)
3043                 break;
3044             if (type != OP_CONST || sv)
3045                 return Nullsv;
3046
3047             sv = ((SVOP*)o)->op_sv;
3048         }
3049     }
3050     return sv;
3051 }
3052
3053 CV *
3054 newSUB(floor,op,proto,block)
3055 I32 floor;
3056 OP *op;
3057 OP *proto;
3058 OP *block;
3059 {
3060     char *name = op ? SvPVx(cSVOP->op_sv, na) : Nullch;
3061     GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3062     register CV *cv;
3063     AV *av;
3064     I32 ix;
3065
3066     if (op)
3067         SAVEFREEOP(op);
3068     if (cv = (name ? GvCV(gv) : Nullcv)) {
3069         if (GvCVGEN(gv)) {
3070             /* just a cached method */
3071             SvREFCNT_dec(cv);
3072             cv = 0;
3073         }
3074         else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3075             /* already defined (or promised) */
3076
3077             SV* const_sv = cv_const_sv(cv);
3078             char *p = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3079
3080             if((!proto != !SvPOK(cv)) || (p && strNE(SvPV((SV*)cv,na), p))) {
3081                 warn("Prototype mismatch: (%s) vs (%s)",
3082                         SvPOK(cv) ? SvPV((SV*)cv,na) : "none",
3083                         p ? p : "none");
3084             }
3085             if (!block) {
3086                 /* just a "sub foo;" when &foo is already defined */
3087                 SAVEFREESV(compcv);
3088                 goto done;
3089             }
3090             if (const_sv || dowarn) {
3091                 line_t oldline = curcop->cop_line;
3092                 curcop->cop_line = copline;
3093                 warn(const_sv ? "Constant subroutine %s redefined"
3094                               : "Subroutine %s redefined",name);
3095                 curcop->cop_line = oldline;
3096             }
3097             SvREFCNT_dec(cv);
3098             cv = 0;
3099         }
3100     }
3101     if (cv) {                           /* must reuse cv if autoloaded */
3102         cv_undef(cv);
3103         CvFLAGS(cv) = CvFLAGS(compcv);
3104         CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3105         CvOUTSIDE(compcv) = 0;
3106         CvPADLIST(cv) = CvPADLIST(compcv);
3107         CvPADLIST(compcv) = 0;
3108         if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3109             CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3110         SvREFCNT_dec(compcv);
3111     }
3112     else {
3113         cv = compcv;
3114         if (name) {
3115             GvCV(gv) = cv;
3116             GvCVGEN(gv) = 0;
3117             sub_generation++;
3118         }
3119     }
3120     CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3121     CvFILEGV(cv) = curcop->cop_filegv;
3122     CvSTASH(cv) = curstash;
3123
3124     if (proto) {
3125         char *p = SvPVx(((SVOP*)proto)->op_sv, na);
3126         sv_setpv((SV*)cv, p);
3127         op_free(proto);
3128     }
3129
3130     if (error_count) {
3131         op_free(block);
3132         block = Nullop;
3133     }
3134     if (!block) {
3135         copline = NOLINE;
3136         LEAVE_SCOPE(floor);
3137         return cv;
3138     }
3139
3140     av = newAV();                       /* Will be @_ */
3141     av_extend(av, 0);
3142     av_store(comppad, 0, (SV*)av);
3143     AvFLAGS(av) = AVf_REIFY;
3144
3145     for (ix = AvFILL(comppad); ix > 0; ix--) {
3146         if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3147             SvPADTMP_on(curpad[ix]);
3148     }
3149
3150     if (AvFILL(comppad_name) < AvFILL(comppad))
3151         av_store(comppad_name, AvFILL(comppad), Nullsv);
3152
3153     CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3154     CvSTART(cv) = LINKLIST(CvROOT(cv));
3155     CvROOT(cv)->op_next = 0;
3156     peep(CvSTART(cv));
3157
3158     if (name) {
3159         char *s;
3160
3161         if (perldb && curstash != debstash) {
3162             SV *sv;
3163             SV *tmpstr = sv_newmortal();
3164             static GV *db_postponed;
3165             CV *cv;
3166             HV *hv;
3167
3168             sprintf(buf, "%s:%ld",
3169                     SvPVX(GvSV(curcop->cop_filegv)), (long)subline);
3170             sv = newSVpv(buf,0);
3171             sv_catpv(sv,"-");
3172             sprintf(buf,"%ld",(long)curcop->cop_line);
3173             sv_catpv(sv,buf);
3174             gv_efullname3(tmpstr, gv, Nullch);
3175             hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3176             if (!db_postponed) {
3177                 db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3178             }
3179             hv = GvHVn(db_postponed);
3180             if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3181                   && (cv = GvCV(db_postponed))) {
3182                 dSP;
3183                 PUSHMARK(sp);
3184                 XPUSHs(tmpstr);
3185                 PUTBACK;
3186                 perl_call_sv((SV*)cv, G_DISCARD);
3187             }
3188         }
3189
3190         if ((s = strrchr(name,':')))
3191             s++;
3192         else
3193             s = name;
3194         if (strEQ(s, "BEGIN") && !error_count) {
3195             ENTER;
3196             SAVESPTR(compiling.cop_filegv);
3197             SAVEI16(compiling.cop_line);
3198             SAVEI32(perldb);
3199             save_svref(&rs);
3200             sv_setsv(rs, nrs);
3201
3202             if (!beginav)
3203                 beginav = newAV();
3204             DEBUG_x( dump_sub(gv) );
3205             av_push(beginav, (SV *)cv);
3206             GvCV(gv) = 0;
3207             calllist(beginav);
3208
3209             curcop = &compiling;
3210             LEAVE;
3211         }
3212         else if (strEQ(s, "END") && !error_count) {
3213             if (!endav)
3214                 endav = newAV();
3215             av_unshift(endav, 1);
3216             av_store(endav, 0, (SV *)cv);
3217             GvCV(gv) = 0;
3218         }
3219     }
3220
3221   done:
3222     copline = NOLINE;
3223     LEAVE_SCOPE(floor);
3224     return cv;
3225 }
3226
3227 #ifdef DEPRECATED
3228 CV *
3229 newXSUB(name, ix, subaddr, filename)
3230 char *name;
3231 I32 ix;
3232 I32 (*subaddr)();
3233 char *filename;
3234 {
3235     CV* cv = newXS(name, (void(*)())subaddr, filename);
3236     CvOLDSTYLE_on(cv);
3237     CvXSUBANY(cv).any_i32 = ix;
3238     return cv;
3239 }
3240 #endif
3241
3242 CV *
3243 newXS(name, subaddr, filename)
3244 char *name;
3245 void (*subaddr) _((CV*));
3246 char *filename;
3247 {
3248     GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3249     register CV *cv;
3250
3251     if (cv = (name ? GvCV(gv) : Nullcv)) {
3252         if (GvCVGEN(gv)) {
3253             /* just a cached method */
3254             SvREFCNT_dec(cv);
3255             cv = 0;
3256         }
3257         else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3258             /* already defined (or promised) */
3259             if (dowarn) {
3260                 line_t oldline = curcop->cop_line;
3261                 curcop->cop_line = copline;
3262                 warn("Subroutine %s redefined",name);
3263                 curcop->cop_line = oldline;
3264             }
3265             SvREFCNT_dec(cv);
3266             cv = 0;
3267         }
3268     }
3269
3270     if (cv)                             /* must reuse cv if autoloaded */
3271         cv_undef(cv);
3272     else {
3273         cv = (CV*)NEWSV(1105,0);
3274         sv_upgrade((SV *)cv, SVt_PVCV);
3275         if (name) {
3276             GvCV(gv) = cv;
3277             GvCVGEN(gv) = 0;
3278             sub_generation++;
3279         }
3280     }
3281     CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3282     CvFILEGV(cv) = gv_fetchfile(filename);
3283     CvXSUB(cv) = subaddr;
3284
3285     if (name) {
3286         char *s = strrchr(name,':');
3287         if (s)
3288             s++;
3289         else
3290             s = name;
3291         if (strEQ(s, "BEGIN")) {
3292             if (!beginav)
3293                 beginav = newAV();
3294             av_push(beginav, (SV *)cv);
3295             GvCV(gv) = 0;
3296         }
3297         else if (strEQ(s, "END")) {
3298             if (!endav)
3299                 endav = newAV();
3300             av_unshift(endav, 1);
3301             av_store(endav, 0, (SV *)cv);
3302             GvCV(gv) = 0;
3303         }
3304     }
3305     else
3306         CvANON_on(cv);
3307
3308     return cv;
3309 }
3310
3311 void
3312 newFORM(floor,op,block)
3313 I32 floor;
3314 OP *op;
3315 OP *block;
3316 {
3317     register CV *cv;
3318     char *name;
3319     GV *gv;
3320     I32 ix;
3321
3322     if (op)
3323         name = SvPVx(cSVOP->op_sv, na);
3324     else
3325         name = "STDOUT";
3326     gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3327     GvMULTI_on(gv);
3328     if (cv = GvFORM(gv)) {
3329         if (dowarn) {
3330             line_t oldline = curcop->cop_line;
3331
3332             curcop->cop_line = copline;
3333             warn("Format %s redefined",name);
3334             curcop->cop_line = oldline;
3335         }
3336         SvREFCNT_dec(cv);
3337     }
3338     cv = compcv;
3339     GvFORM(gv) = cv;
3340     CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3341     CvFILEGV(cv) = curcop->cop_filegv;
3342
3343     for (ix = AvFILL(comppad); ix > 0; ix--) {
3344         if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3345             SvPADTMP_on(curpad[ix]);
3346     }
3347
3348     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3349     CvSTART(cv) = LINKLIST(CvROOT(cv));
3350     CvROOT(cv)->op_next = 0;
3351     peep(CvSTART(cv));
3352     op_free(op);
3353     copline = NOLINE;
3354     LEAVE_SCOPE(floor);
3355 }
3356
3357 OP *
3358 newANONLIST(op)
3359 OP* op;
3360 {
3361     return newUNOP(OP_REFGEN, 0,
3362         mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
3363 }
3364
3365 OP *
3366 newANONHASH(op)
3367 OP* op;
3368 {
3369     return newUNOP(OP_REFGEN, 0,
3370         mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3371 }
3372
3373 OP *
3374 newANONSUB(floor, proto, block)
3375 I32 floor;
3376 OP *proto;
3377 OP *block;
3378 {
3379     return newUNOP(OP_REFGEN, 0,
3380         newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3381 }
3382
3383 OP *
3384 oopsAV(o)
3385 OP *o;
3386 {
3387     switch (o->op_type) {
3388     case OP_PADSV:
3389         o->op_type = OP_PADAV;
3390         o->op_ppaddr = ppaddr[OP_PADAV];
3391         return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3392         
3393     case OP_RV2SV:
3394         o->op_type = OP_RV2AV;
3395         o->op_ppaddr = ppaddr[OP_RV2AV];
3396         ref(o, OP_RV2AV);
3397         break;
3398
3399     default:
3400         warn("oops: oopsAV");
3401         break;
3402     }
3403     return o;
3404 }
3405
3406 OP *
3407 oopsHV(o)
3408 OP *o;
3409 {
3410     switch (o->op_type) {
3411     case OP_PADSV:
3412     case OP_PADAV:
3413         o->op_type = OP_PADHV;
3414         o->op_ppaddr = ppaddr[OP_PADHV];
3415         return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3416
3417     case OP_RV2SV:
3418     case OP_RV2AV:
3419         o->op_type = OP_RV2HV;
3420         o->op_ppaddr = ppaddr[OP_RV2HV];
3421         ref(o, OP_RV2HV);
3422         break;
3423
3424     default:
3425         warn("oops: oopsHV");
3426         break;
3427     }
3428     return o;
3429 }
3430
3431 OP *
3432 newAVREF(o)
3433 OP *o;
3434 {
3435     if (o->op_type == OP_PADANY) {
3436         o->op_type = OP_PADAV;
3437         o->op_ppaddr = ppaddr[OP_PADAV];
3438         return o;
3439     }
3440     return newUNOP(OP_RV2AV, 0, scalar(o));
3441 }
3442
3443 OP *
3444 newGVREF(type,o)
3445 I32 type;
3446 OP *o;
3447 {
3448     if (type == OP_MAPSTART)
3449         return newUNOP(OP_NULL, 0, o);
3450     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3451 }
3452
3453 OP *
3454 newHVREF(o)
3455 OP *o;
3456 {
3457     if (o->op_type == OP_PADANY) {
3458         o->op_type = OP_PADHV;
3459         o->op_ppaddr = ppaddr[OP_PADHV];
3460         return o;
3461     }
3462     return newUNOP(OP_RV2HV, 0, scalar(o));
3463 }
3464
3465 OP *
3466 oopsCV(o)
3467 OP *o;
3468 {
3469     croak("NOT IMPL LINE %d",__LINE__);
3470     /* STUB */
3471     return o;
3472 }
3473
3474 OP *
3475 newCVREF(flags, o)
3476 I32 flags;
3477 OP *o;
3478 {
3479     return newUNOP(OP_RV2CV, flags, scalar(o));
3480 }
3481
3482 OP *
3483 newSVREF(o)
3484 OP *o;
3485 {
3486     if (o->op_type == OP_PADANY) {
3487         o->op_type = OP_PADSV;
3488         o->op_ppaddr = ppaddr[OP_PADSV];
3489         return o;
3490     }
3491     return newUNOP(OP_RV2SV, 0, scalar(o));
3492 }
3493
3494 /* Check routines. */
3495
3496 OP *
3497 ck_anoncode(op)
3498 OP *op;
3499 {
3500     PADOFFSET ix;
3501     SV* name;
3502
3503     name = NEWSV(1106,0);
3504     sv_upgrade(name, SVt_PVNV);
3505     sv_setpvn(name, "&", 1);
3506     SvIVX(name) = -1;
3507     SvNVX(name) = 1;
3508     ix = pad_alloc(op->op_type, SVs_PADMY);
3509     av_store(comppad_name, ix, name);
3510     av_store(comppad, ix, cSVOP->op_sv);
3511     SvPADMY_on(cSVOP->op_sv);
3512     cSVOP->op_sv = Nullsv;
3513     cSVOP->op_targ = ix;
3514     return op;
3515 }
3516
3517 OP *
3518 ck_bitop(op)
3519 OP *op;
3520 {
3521     op->op_private = hints;
3522     return op;
3523 }
3524
3525 OP *
3526 ck_concat(op)
3527 OP *op;
3528 {
3529     if (cUNOP->op_first->op_type == OP_CONCAT)
3530         op->op_flags |= OPf_STACKED;
3531     return op;
3532 }
3533
3534 OP *
3535 ck_spair(op)
3536 OP *op;
3537 {
3538     if (op->op_flags & OPf_KIDS) {
3539         OP* newop;
3540         OP* kid;
3541         OPCODE type = op->op_type;
3542         op = modkids(ck_fun(op), type);
3543         kid = cUNOP->op_first;
3544         newop = kUNOP->op_first->op_sibling;
3545         if (newop &&
3546             (newop->op_sibling ||
3547              !(opargs[newop->op_type] & OA_RETSCALAR) ||
3548              newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3549              newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3550             
3551             return op;
3552         }
3553         op_free(kUNOP->op_first);
3554         kUNOP->op_first = newop;
3555     }
3556     op->op_ppaddr = ppaddr[++op->op_type];
3557     return ck_fun(op);
3558 }
3559
3560 OP *
3561 ck_delete(op)
3562 OP *op;
3563 {
3564     op = ck_fun(op);
3565     op->op_private = 0;
3566     if (op->op_flags & OPf_KIDS) {
3567         OP *kid = cUNOP->op_first;
3568         if (kid->op_type == OP_HSLICE)
3569             op->op_private |= OPpSLICE;
3570         else if (kid->op_type != OP_HELEM)
3571             croak("%s argument is not a HASH element or slice",
3572                   op_desc[op->op_type]);
3573         null(kid);
3574     }
3575     return op;
3576 }
3577
3578 OP *
3579 ck_eof(op)
3580 OP *op;
3581 {
3582     I32 type = op->op_type;
3583
3584     if (op->op_flags & OPf_KIDS) {
3585         if (cLISTOP->op_first->op_type == OP_STUB) {
3586             op_free(op);
3587             op = newUNOP(type, OPf_SPECIAL,
3588                 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
3589         }
3590         return ck_fun(op);
3591     }
3592     return op;
3593 }
3594
3595 OP *
3596 ck_eval(op)
3597 OP *op;
3598 {
3599     hints |= HINT_BLOCK_SCOPE;
3600     if (op->op_flags & OPf_KIDS) {
3601         SVOP *kid = (SVOP*)cUNOP->op_first;
3602
3603         if (!kid) {
3604             op->op_flags &= ~OPf_KIDS;
3605             null(op);
3606         }
3607         else if (kid->op_type == OP_LINESEQ) {
3608             LOGOP *enter;
3609
3610             kid->op_next = op->op_next;
3611             cUNOP->op_first = 0;
3612             op_free(op);
3613
3614             Newz(1101, enter, 1, LOGOP);
3615             enter->op_type = OP_ENTERTRY;
3616             enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3617             enter->op_private = 0;
3618
3619             /* establish postfix order */
3620             enter->op_next = (OP*)enter;
3621
3622             op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3623             op->op_type = OP_LEAVETRY;
3624             op->op_ppaddr = ppaddr[OP_LEAVETRY];
3625             enter->op_other = op;
3626             return op;
3627         }
3628     }
3629     else {
3630         op_free(op);
3631         op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3632     }
3633     op->op_targ = (PADOFFSET)hints;
3634     return op;
3635 }
3636
3637 OP *
3638 ck_exec(op)
3639 OP *op;
3640 {
3641     OP *kid;
3642     if (op->op_flags & OPf_STACKED) {
3643         op = ck_fun(op);
3644         kid = cUNOP->op_first->op_sibling;
3645         if (kid->op_type == OP_RV2GV)
3646             null(kid);
3647     }
3648     else
3649         op = listkids(op);
3650     return op;
3651 }
3652
3653 OP *
3654 ck_exists(op)
3655 OP *op;
3656 {
3657     op = ck_fun(op);
3658     if (op->op_flags & OPf_KIDS) {
3659         OP *kid = cUNOP->op_first;
3660         if (kid->op_type != OP_HELEM)
3661             croak("%s argument is not a HASH element", op_desc[op->op_type]);
3662         null(kid);
3663     }
3664     return op;
3665 }
3666
3667 OP *
3668 ck_gvconst(o)
3669 register OP *o;
3670 {
3671     o = fold_constants(o);
3672     if (o->op_type == OP_CONST)
3673         o->op_type = OP_GV;
3674     return o;
3675 }
3676
3677 OP *
3678 ck_rvconst(op)
3679 register OP *op;
3680 {
3681     SVOP *kid = (SVOP*)cUNOP->op_first;
3682
3683     op->op_private |= (hints & HINT_STRICT_REFS);
3684     if (kid->op_type == OP_CONST) {
3685         char *name;
3686         int iscv;
3687         GV *gv;
3688
3689         name = SvPV(kid->op_sv, na);
3690         if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3691             char *badthing = Nullch;
3692             switch (op->op_type) {
3693             case OP_RV2SV:
3694                 badthing = "a SCALAR";
3695                 break;
3696             case OP_RV2AV:
3697                 badthing = "an ARRAY";
3698                 break;
3699             case OP_RV2HV:
3700                 badthing = "a HASH";
3701                 break;
3702             }
3703             if (badthing)
3704                 croak(
3705           "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3706                       name, badthing);
3707         }
3708         kid->op_type = OP_GV;
3709         iscv = (op->op_type == OP_RV2CV) * 2;
3710         for (gv = 0; !gv; iscv++) {
3711             /*
3712              * This is a little tricky.  We only want to add the symbol if we
3713              * didn't add it in the lexer.  Otherwise we get duplicate strict
3714              * warnings.  But if we didn't add it in the lexer, we must at
3715              * least pretend like we wanted to add it even if it existed before,
3716              * or we get possible typo warnings.  OPpCONST_ENTERED says
3717              * whether the lexer already added THIS instance of this symbol.
3718              */
3719             gv = gv_fetchpv(name,
3720                 iscv | !(kid->op_private & OPpCONST_ENTERED),
3721                 iscv
3722                     ? SVt_PVCV
3723                     : op->op_type == OP_RV2SV
3724                         ? SVt_PV
3725                         : op->op_type == OP_RV2AV
3726                             ? SVt_PVAV
3727                             : op->op_type == OP_RV2HV
3728                                 ? SVt_PVHV
3729                                 : SVt_PVGV);
3730         }
3731         SvREFCNT_dec(kid->op_sv);
3732         kid->op_sv = SvREFCNT_inc(gv);
3733     }
3734     return op;
3735 }
3736
3737 OP *
3738 ck_ftst(op)
3739 OP *op;
3740 {
3741     I32 type = op->op_type;
3742
3743     if (op->op_flags & OPf_REF)
3744         return op;
3745
3746     if (op->op_flags & OPf_KIDS) {
3747         SVOP *kid = (SVOP*)cUNOP->op_first;
3748
3749         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3750             OP *newop = newGVOP(type, OPf_REF,
3751                 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3752             op_free(op);
3753             return newop;
3754         }
3755     }
3756     else {
3757         op_free(op);
3758         if (type == OP_FTTTY)
3759             return newGVOP(type, OPf_REF, gv_fetchpv("main'STDIN", TRUE,
3760                                 SVt_PVIO));
3761         else
3762             return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3763     }
3764     return op;
3765 }
3766
3767 OP *
3768 ck_fun(op)
3769 OP *op;
3770 {
3771     register OP *kid;
3772     OP **tokid;
3773     OP *sibl;
3774     I32 numargs = 0;
3775     int type = op->op_type;
3776     register I32 oa = opargs[type] >> OASHIFT;
3777     
3778     if (op->op_flags & OPf_STACKED) {
3779         if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
3780             oa &= ~OA_OPTIONAL;
3781         else
3782             return no_fh_allowed(op);
3783     }
3784
3785     if (op->op_flags & OPf_KIDS) {
3786         tokid = &cLISTOP->op_first;
3787         kid = cLISTOP->op_first;
3788         if (kid->op_type == OP_PUSHMARK ||
3789             kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
3790         {
3791             tokid = &kid->op_sibling;
3792             kid = kid->op_sibling;
3793         }
3794         if (!kid && opargs[type] & OA_DEFGV)
3795             *tokid = kid = newSVREF(newGVOP(OP_GV, 0, defgv));
3796
3797         while (oa && kid) {
3798             numargs++;
3799             sibl = kid->op_sibling;
3800             switch (oa & 7) {
3801             case OA_SCALAR:
3802                 scalar(kid);
3803                 break;
3804             case OA_LIST:
3805                 if (oa < 16) {
3806                     kid = 0;
3807                     continue;
3808                 }
3809                 else
3810                     list(kid);
3811                 break;
3812             case OA_AVREF:
3813                 if (kid->op_type == OP_CONST &&
3814                   (kid->op_private & OPpCONST_BARE)) {
3815                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3816                     OP *newop = newAVREF(newGVOP(OP_GV, 0,
3817                         gv_fetchpv(name, TRUE, SVt_PVAV) ));
3818                     if (dowarn)
3819                         warn("Array @%s missing the @ in argument %ld of %s()",
3820                             name, (long)numargs, op_desc[type]);
3821                     op_free(kid);
3822                     kid = newop;
3823                     kid->op_sibling = sibl;
3824                     *tokid = kid;
3825                 }
3826                 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
3827                     bad_type(numargs, "array", op_desc[op->op_type], kid);
3828                 mod(kid, type);
3829                 break;
3830             case OA_HVREF:
3831                 if (kid->op_type == OP_CONST &&
3832                   (kid->op_private & OPpCONST_BARE)) {
3833                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3834                     OP *newop = newHVREF(newGVOP(OP_GV, 0,
3835                         gv_fetchpv(name, TRUE, SVt_PVHV) ));
3836                     if (dowarn)
3837                         warn("Hash %%%s missing the %% in argument %ld of %s()",
3838                             name, (long)numargs, op_desc[type]);
3839                     op_free(kid);
3840                     kid = newop;
3841                     kid->op_sibling = sibl;
3842                     *tokid = kid;
3843                 }
3844                 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
3845                     bad_type(numargs, "hash", op_desc[op->op_type], kid);
3846                 mod(kid, type);
3847                 break;
3848             case OA_CVREF:
3849                 {
3850                     OP *newop = newUNOP(OP_NULL, 0, kid);
3851                     kid->op_sibling = 0;
3852                     linklist(kid);
3853                     newop->op_next = newop;
3854                     kid = newop;
3855                     kid->op_sibling = sibl;
3856                     *tokid = kid;
3857                 }
3858                 break;
3859             case OA_FILEREF:
3860                 if (kid->op_type != OP_GV) {
3861                     if (kid->op_type == OP_CONST &&
3862                       (kid->op_private & OPpCONST_BARE)) {
3863                         OP *newop = newGVOP(OP_GV, 0,
3864                             gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3865                                         SVt_PVIO) );
3866                         op_free(kid);
3867                         kid = newop;
3868                     }
3869                     else {
3870                         kid->op_sibling = 0;
3871                         kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3872                     }
3873                     kid->op_sibling = sibl;
3874                     *tokid = kid;
3875                 }
3876                 scalar(kid);
3877                 break;
3878             case OA_SCALARREF:
3879                 mod(scalar(kid), type);
3880                 break;
3881             }
3882             oa >>= 4;
3883             tokid = &kid->op_sibling;
3884             kid = kid->op_sibling;
3885         }
3886         op->op_private |= numargs;
3887         if (kid)
3888             return too_many_arguments(op,op_desc[op->op_type]);
3889         listkids(op);
3890     }
3891     else if (opargs[type] & OA_DEFGV) {
3892         op_free(op);
3893         return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3894     }
3895
3896     if (oa) {
3897         while (oa & OA_OPTIONAL)
3898             oa >>= 4;
3899         if (oa && oa != OA_LIST)
3900             return too_few_arguments(op,op_desc[op->op_type]);
3901     }
3902     return op;
3903 }
3904
3905 OP *
3906 ck_glob(op)
3907 OP *op;
3908 {
3909     GV *gv = gv_fetchpv("glob", FALSE, SVt_PVCV);
3910
3911     if (gv && GvIMPORTED_CV(gv)) {
3912         op->op_type = OP_LIST;
3913         op->op_ppaddr = ppaddr[OP_LIST];
3914         op = newUNOP(OP_ENTERSUB, OPf_STACKED,
3915                      append_elem(OP_LIST, op, 
3916                                  scalar(newUNOP(OP_RV2CV, 0,
3917                                                 newGVOP(OP_GV, 0, gv)))));
3918         return ck_subr(op);
3919     }
3920     if ((op->op_flags & OPf_KIDS) && !cLISTOP->op_first->op_sibling)
3921         append_elem(OP_GLOB, op, newSVREF(newGVOP(OP_GV, 0, defgv)));
3922     gv = newGVgen("main");
3923     gv_IOadd(gv);
3924     append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3925     scalarkids(op);
3926     return ck_fun(op);
3927 }
3928
3929 OP *
3930 ck_grep(op)
3931 OP *op;
3932 {
3933     LOGOP *gwop;
3934     OP *kid;
3935     OPCODE type = op->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
3936
3937     op->op_ppaddr = ppaddr[OP_GREPSTART];
3938     Newz(1101, gwop, 1, LOGOP);
3939     
3940     if (op->op_flags & OPf_STACKED) {
3941         OP* k;
3942         op = ck_sort(op);
3943         kid = cLISTOP->op_first->op_sibling;
3944         for (k = cLISTOP->op_first->op_sibling->op_next; k; k = k->op_next) {
3945             kid = k;
3946         }
3947         kid->op_next = (OP*)gwop;
3948         op->op_flags &= ~OPf_STACKED;
3949     }
3950     kid = cLISTOP->op_first->op_sibling;
3951     if (type == OP_MAPWHILE)
3952         list(kid);
3953     else
3954         scalar(kid);
3955     op = ck_fun(op);
3956     if (error_count)
3957         return op;
3958     kid = cLISTOP->op_first->op_sibling; 
3959     if (kid->op_type != OP_NULL)
3960         croak("panic: ck_grep");
3961     kid = kUNOP->op_first;
3962
3963     gwop->op_type = type;
3964     gwop->op_ppaddr = ppaddr[type];
3965     gwop->op_first = listkids(op);
3966     gwop->op_flags |= OPf_KIDS;
3967     gwop->op_private = 1;
3968     gwop->op_other = LINKLIST(kid);
3969     gwop->op_targ = pad_alloc(type, SVs_PADTMP);
3970     kid->op_next = (OP*)gwop;
3971
3972     kid = cLISTOP->op_first->op_sibling;
3973     if (!kid || !kid->op_sibling)
3974         return too_few_arguments(op,op_desc[op->op_type]);
3975     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
3976         mod(kid, OP_GREPSTART);
3977
3978     return (OP*)gwop;
3979 }
3980
3981 OP *
3982 ck_index(op)
3983 OP *op;
3984 {
3985     if (op->op_flags & OPf_KIDS) {
3986         OP *kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
3987         if (kid && kid->op_type == OP_CONST)
3988             fbm_compile(((SVOP*)kid)->op_sv);
3989     }
3990     return ck_fun(op);
3991 }
3992
3993 OP *
3994 ck_lengthconst(op)
3995 OP *op;
3996 {
3997     /* XXX length optimization goes here */
3998     return ck_fun(op);
3999 }
4000
4001 OP *
4002 ck_lfun(op)
4003 OP *op;
4004 {
4005     OPCODE type = op->op_type;
4006     return modkids(ck_fun(op), type);
4007 }
4008
4009 OP *
4010 ck_rfun(op)
4011 OP *op;
4012 {
4013     OPCODE type = op->op_type;
4014     return refkids(ck_fun(op), type);
4015 }
4016
4017 OP *
4018 ck_listiob(op)
4019 OP *op;
4020 {
4021     register OP *kid;
4022     
4023     kid = cLISTOP->op_first;
4024     if (!kid) {
4025         op = force_list(op);
4026         kid = cLISTOP->op_first;
4027     }
4028     if (kid->op_type == OP_PUSHMARK)
4029         kid = kid->op_sibling;
4030     if (kid && op->op_flags & OPf_STACKED)
4031         kid = kid->op_sibling;
4032     else if (kid && !kid->op_sibling) {         /* print HANDLE; */
4033         if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4034             op->op_flags |= OPf_STACKED;        /* make it a filehandle */
4035             kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4036             cLISTOP->op_first->op_sibling = kid;
4037             cLISTOP->op_last = kid;
4038             kid = kid->op_sibling;
4039         }
4040     }
4041         
4042     if (!kid)
4043         append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
4044
4045     op = listkids(op);
4046
4047     op->op_private = 0;
4048 #ifdef USE_LOCALE
4049     if (hints & HINT_LOCALE)
4050         op->op_private |= OPpLOCALE;
4051 #endif
4052
4053     return op;
4054 }
4055
4056 OP *
4057 ck_fun_locale(op)
4058 OP *op;
4059 {
4060     op = ck_fun(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_scmp(op)
4073 OP *op;
4074 {
4075     op->op_private = 0;
4076 #ifdef USE_LOCALE
4077     if (hints & HINT_LOCALE)
4078         op->op_private |= OPpLOCALE;
4079 #endif
4080
4081     return op;
4082 }
4083
4084 OP *
4085 ck_match(op)
4086 OP *op;
4087 {
4088     cPMOP->op_pmflags |= PMf_RUNTIME;
4089     cPMOP->op_pmpermflags |= PMf_RUNTIME;
4090     return op;
4091 }
4092
4093 OP *
4094 ck_null(op)
4095 OP *op;
4096 {
4097     return op;
4098 }
4099
4100 OP *
4101 ck_repeat(op)
4102 OP *op;
4103 {
4104     if (cBINOP->op_first->op_flags & OPf_PARENS) {
4105         op->op_private |= OPpREPEAT_DOLIST;
4106         cBINOP->op_first = force_list(cBINOP->op_first);
4107     }
4108     else
4109         scalar(op);
4110     return op;
4111 }
4112
4113 OP *
4114 ck_require(op)
4115 OP *op;
4116 {
4117     if (op->op_flags & OPf_KIDS) {      /* Shall we supply missing .pm? */
4118         SVOP *kid = (SVOP*)cUNOP->op_first;
4119
4120         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4121             char *s;
4122             for (s = SvPVX(kid->op_sv); *s; s++) {
4123                 if (*s == ':' && s[1] == ':') {
4124                     *s = '/';
4125                     Move(s+2, s+1, strlen(s+2)+1, char);
4126                     --SvCUR(kid->op_sv);
4127                 }
4128             }
4129             sv_catpvn(kid->op_sv, ".pm", 3);
4130         }
4131     }
4132     return ck_fun(op);
4133 }
4134
4135 OP *
4136 ck_retarget(op)
4137 OP *op;
4138 {
4139     croak("NOT IMPL LINE %d",__LINE__);
4140     /* STUB */
4141     return op;
4142 }
4143
4144 OP *
4145 ck_select(op)
4146 OP *op;
4147 {
4148     OP* kid;
4149     if (op->op_flags & OPf_KIDS) {
4150         kid = cLISTOP->op_first->op_sibling;    /* get past pushmark */
4151         if (kid && kid->op_sibling) {
4152             op->op_type = OP_SSELECT;
4153             op->op_ppaddr = ppaddr[OP_SSELECT];
4154             op = ck_fun(op);
4155             return fold_constants(op);
4156         }
4157     }
4158     op = ck_fun(op);
4159     kid = cLISTOP->op_first->op_sibling;    /* get past pushmark */
4160     if (kid && kid->op_type == OP_RV2GV)
4161         kid->op_private &= ~HINT_STRICT_REFS;
4162     return op;
4163 }
4164
4165 OP *
4166 ck_shift(op)
4167 OP *op;
4168 {
4169     I32 type = op->op_type;
4170
4171     if (!(op->op_flags & OPf_KIDS)) {
4172         op_free(op);
4173         return newUNOP(type, 0,
4174             scalar(newUNOP(OP_RV2AV, 0,
4175                 scalar(newGVOP(OP_GV, 0, subline 
4176                                ? defgv 
4177                                : gv_fetchpv("ARGV", TRUE, SVt_PVAV) )))));
4178     }
4179     return scalar(modkids(ck_fun(op), type));
4180 }
4181
4182 OP *
4183 ck_sort(op)
4184 OP *op;
4185 {
4186     op->op_private = 0;
4187 #ifdef USE_LOCALE
4188     if (hints & HINT_LOCALE)
4189         op->op_private |= OPpLOCALE;
4190 #endif
4191
4192     if (op->op_flags & OPf_STACKED) {
4193         OP *kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
4194         OP *k;
4195         kid = kUNOP->op_first;                          /* get past rv2gv */
4196
4197         if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4198             linklist(kid);
4199             if (kid->op_type == OP_SCOPE) {
4200                 k = kid->op_next;
4201                 kid->op_next = 0;
4202             }
4203             else if (kid->op_type == OP_LEAVE) {
4204                 if (op->op_type == OP_SORT) {
4205                     null(kid);                  /* wipe out leave */
4206                     kid->op_next = kid;
4207
4208                     for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4209                         if (k->op_next == kid)
4210                             k->op_next = 0;
4211                     }
4212                 }
4213                 else
4214                     kid->op_next = 0;           /* just disconnect the leave */
4215                 k = kLISTOP->op_first;
4216             }
4217             peep(k);
4218
4219             kid = cLISTOP->op_first->op_sibling;        /* get past pushmark */
4220             null(kid);                                  /* wipe out rv2gv */
4221             if (op->op_type == OP_SORT)
4222                 kid->op_next = kid;
4223             else
4224                 kid->op_next = k;
4225             op->op_flags |= OPf_SPECIAL;
4226         }
4227     }
4228
4229     return op;
4230 }
4231
4232 OP *
4233 ck_split(op)
4234 OP *op;
4235 {
4236     register OP *kid;
4237     PMOP* pm;
4238     
4239     if (op->op_flags & OPf_STACKED)
4240         return no_fh_allowed(op);
4241
4242     kid = cLISTOP->op_first;
4243     if (kid->op_type != OP_NULL)
4244         croak("panic: ck_split");
4245     kid = kid->op_sibling;
4246     op_free(cLISTOP->op_first);
4247     cLISTOP->op_first = kid;
4248     if (!kid) {
4249         cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4250         cLISTOP->op_last = kid; /* There was only one element previously */
4251     }
4252
4253     if (kid->op_type != OP_MATCH) {
4254         OP *sibl = kid->op_sibling;
4255         kid->op_sibling = 0;
4256         kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4257         if (cLISTOP->op_first == cLISTOP->op_last)
4258             cLISTOP->op_last = kid;
4259         cLISTOP->op_first = kid;
4260         kid->op_sibling = sibl;
4261     }
4262     pm = (PMOP*)kid;
4263     if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
4264         SvREFCNT_dec(pm->op_pmshort);   /* can't use substring to optimize */
4265         pm->op_pmshort = 0;
4266     }
4267
4268     kid->op_type = OP_PUSHRE;
4269     kid->op_ppaddr = ppaddr[OP_PUSHRE];
4270     scalar(kid);
4271
4272     if (!kid->op_sibling)
4273         append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
4274
4275     kid = kid->op_sibling;
4276     scalar(kid);
4277
4278     if (!kid->op_sibling)
4279         append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
4280
4281     kid = kid->op_sibling;
4282     scalar(kid);
4283
4284     if (kid->op_sibling)
4285         return too_many_arguments(op,op_desc[op->op_type]);
4286
4287     return op;
4288 }
4289
4290 OP *
4291 ck_subr(op)
4292 OP *op;
4293 {
4294     OP *prev = ((cUNOP->op_first->op_sibling)
4295              ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first;
4296     OP *o = prev->op_sibling;
4297     OP *cvop;
4298     char *proto = 0;
4299     CV *cv = 0;
4300     int optional = 0;
4301     I32 arg = 0;
4302
4303     for (cvop = o; cvop->op_sibling; cvop = cvop->op_sibling) ;
4304     if (cvop->op_type == OP_RV2CV) {
4305         SVOP* tmpop;
4306         op->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4307         null(cvop);             /* disable rv2cv */
4308         tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4309         if (tmpop->op_type == OP_GV) {
4310             cv = GvCVu(tmpop->op_sv);
4311             if (cv && SvPOK(cv) && !(op->op_private & OPpENTERSUB_AMPER))
4312                 proto = SvPV((SV*)cv,na);
4313         }
4314     }
4315     op->op_private |= (hints & HINT_STRICT_REFS);
4316     if (perldb && curstash != debstash)
4317         op->op_private |= OPpENTERSUB_DB;
4318     while (o != cvop) {
4319         if (proto) {
4320             switch (*proto) {
4321             case '\0':
4322                 return too_many_arguments(op, CvNAME(cv));
4323             case ';':
4324                 optional = 1;
4325                 proto++;
4326                 continue;
4327             case '$':
4328                 proto++;
4329                 arg++;
4330                 scalar(o);
4331                 break;
4332             case '%':
4333             case '@':
4334                 list(o);
4335                 arg++;
4336                 break;
4337             case '&':
4338                 proto++;
4339                 arg++;
4340                 if (o->op_type != OP_REFGEN && o->op_type != OP_UNDEF)
4341                     bad_type(arg, "block", CvNAME(cv), o);
4342                 break;
4343             case '*':
4344                 proto++;
4345                 arg++;
4346                 if (o->op_type == OP_RV2GV)
4347                     goto wrapref;
4348                 {
4349                     OP* kid = o;
4350                     o = newUNOP(OP_RV2GV, 0, kid);
4351                     o->op_sibling = kid->op_sibling;
4352                     kid->op_sibling = 0;
4353                     prev->op_sibling = o;
4354                 }
4355                 goto wrapref;
4356             case '\\':
4357                 proto++;
4358                 arg++;
4359                 switch (*proto++) {
4360                 case '*':
4361                     if (o->op_type != OP_RV2GV)
4362                         bad_type(arg, "symbol", CvNAME(cv), o);
4363                     goto wrapref;
4364                 case '&':
4365                     if (o->op_type != OP_RV2CV)
4366                         bad_type(arg, "sub", CvNAME(cv), o);
4367                     goto wrapref;
4368                 case '$':
4369                     if (o->op_type != OP_RV2SV && o->op_type != OP_PADSV)
4370                         bad_type(arg, "scalar", CvNAME(cv), o);
4371                     goto wrapref;
4372                 case '@':
4373                     if (o->op_type != OP_RV2AV && o->op_type != OP_PADAV)
4374                         bad_type(arg, "array", CvNAME(cv), o);
4375                     goto wrapref;
4376                 case '%':
4377                     if (o->op_type != OP_RV2HV && o->op_type != OP_PADHV)
4378                         bad_type(arg, "hash", CvNAME(cv), o);
4379                   wrapref:
4380                     {
4381                         OP* kid = o;
4382                         o = newUNOP(OP_REFGEN, 0, kid);
4383                         o->op_sibling = kid->op_sibling;
4384                         kid->op_sibling = 0;
4385                         prev->op_sibling = o;
4386                     }
4387                     break;
4388                 default: goto oops;
4389                 }
4390                 break;
4391             case ' ':
4392                 proto++;
4393                 continue;
4394             default:
4395               oops:
4396                 croak("Malformed prototype for %s: %s",
4397                         CvNAME(cv),SvPV((SV*)cv,na));
4398             }
4399         }
4400         else
4401             list(o);
4402         mod(o, OP_ENTERSUB);
4403         prev = o;
4404         o = o->op_sibling;
4405     }
4406     if (proto && !optional && *proto == '$')
4407         return too_few_arguments(op, CvNAME(cv));
4408     return op;
4409 }
4410
4411 OP *
4412 ck_svconst(op)
4413 OP *op;
4414 {
4415     SvREADONLY_on(cSVOP->op_sv);
4416     return op;
4417 }
4418
4419 OP *
4420 ck_trunc(op)
4421 OP *op;
4422 {
4423     if (op->op_flags & OPf_KIDS) {
4424         SVOP *kid = (SVOP*)cUNOP->op_first;
4425
4426         if (kid->op_type == OP_NULL)
4427             kid = (SVOP*)kid->op_sibling;
4428         if (kid &&
4429           kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4430             op->op_flags |= OPf_SPECIAL;
4431     }
4432     return ck_fun(op);
4433 }
4434
4435 /* A peephole optimizer.  We visit the ops in the order they're to execute. */
4436
4437 void
4438 peep(o)
4439 register OP* o;
4440 {
4441     register OP* oldop = 0;
4442     if (!o || o->op_seq)
4443         return;
4444     ENTER;
4445     SAVESPTR(op);
4446     SAVESPTR(curcop);
4447     for (; o; o = o->op_next) {
4448         if (o->op_seq)
4449             break;
4450         if (!op_seqmax)
4451             op_seqmax++;
4452         op = o;
4453         switch (o->op_type) {
4454         case OP_NEXTSTATE:
4455         case OP_DBSTATE:
4456             curcop = ((COP*)o);         /* for warnings */
4457             o->op_seq = op_seqmax++;
4458             break;
4459
4460         case OP_CONCAT:
4461         case OP_CONST:
4462         case OP_JOIN:
4463         case OP_UC:
4464         case OP_UCFIRST:
4465         case OP_LC:
4466         case OP_LCFIRST:
4467         case OP_QUOTEMETA:
4468             if (o->op_next->op_type == OP_STRINGIFY)
4469                 null(o->op_next);
4470             o->op_seq = op_seqmax++;
4471             break;
4472         case OP_STUB:
4473             if ((o->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
4474                 o->op_seq = op_seqmax++;
4475                 break;  /* Scalar stub must produce undef.  List stub is noop */
4476             }
4477             goto nothin;
4478         case OP_NULL:
4479             if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4480                 curcop = ((COP*)op);
4481             goto nothin;
4482         case OP_SCALAR:
4483         case OP_LINESEQ:
4484         case OP_SCOPE:
4485           nothin:
4486             if (oldop && o->op_next) {
4487                 oldop->op_next = o->op_next;
4488                 continue;
4489             }
4490             o->op_seq = op_seqmax++;
4491             break;
4492
4493         case OP_GV:
4494             if (o->op_next->op_type == OP_RV2SV) {
4495                 if (!(o->op_next->op_private & OPpDEREF)) {
4496                     null(o->op_next);
4497                     o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4498                     o->op_next = o->op_next->op_next;
4499                     o->op_type = OP_GVSV;
4500                     o->op_ppaddr = ppaddr[OP_GVSV];
4501                 }
4502             }
4503             else if (o->op_next->op_type == OP_RV2AV) {
4504                 OP* pop = o->op_next->op_next;
4505                 IV i;
4506                 if (pop->op_type == OP_CONST &&
4507                     (op = pop->op_next) &&
4508                     pop->op_next->op_type == OP_AELEM &&
4509                     !(pop->op_next->op_private & (OPpDEREF|OPpLVAL_INTRO)) &&
4510                     (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4511                                 <= 255 &&
4512                     i >= 0)
4513                 {
4514                     SvREFCNT_dec(((SVOP*)pop)->op_sv);
4515                     null(o->op_next);
4516                     null(pop->op_next);
4517                     null(pop);
4518                     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4519                     o->op_next = pop->op_next->op_next;
4520                     o->op_type = OP_AELEMFAST;
4521                     o->op_ppaddr = ppaddr[OP_AELEMFAST];
4522                     o->op_private = (U8)i;
4523                     GvAVn(((GVOP*)o)->op_gv);
4524                 }
4525             }
4526             o->op_seq = op_seqmax++;
4527             break;
4528
4529         case OP_MAPWHILE:
4530         case OP_GREPWHILE:
4531         case OP_AND:
4532         case OP_OR:
4533             o->op_seq = op_seqmax++;
4534             peep(cLOGOP->op_other);
4535             break;
4536
4537         case OP_COND_EXPR:
4538             o->op_seq = op_seqmax++;
4539             peep(cCONDOP->op_true);
4540             peep(cCONDOP->op_false);
4541             break;
4542
4543         case OP_ENTERLOOP:
4544             o->op_seq = op_seqmax++;
4545             peep(cLOOP->op_redoop);
4546             peep(cLOOP->op_nextop);
4547             peep(cLOOP->op_lastop);
4548             break;
4549
4550         case OP_MATCH:
4551         case OP_SUBST:
4552             o->op_seq = op_seqmax++;
4553             peep(cPMOP->op_pmreplstart);
4554             break;
4555
4556         case OP_EXEC:
4557             o->op_seq = op_seqmax++;
4558             if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4559                 if (o->op_next->op_sibling &&
4560                         o->op_next->op_sibling->op_type != OP_DIE) {
4561                     line_t oldline = curcop->cop_line;
4562
4563                     curcop->cop_line = ((COP*)o->op_next)->cop_line;
4564                     warn("Statement unlikely to be reached");
4565                     warn("(Maybe you meant system() when you said exec()?)\n");
4566                     curcop->cop_line = oldline;
4567                 }
4568             }
4569             break;
4570         default:
4571             o->op_seq = op_seqmax++;
4572             break;
4573         }
4574         oldop = o;
4575     }
4576     LEAVE;
4577 }