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