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