add patch, along with all the missing bits, and doc tweaks
[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     PMOP *pm;
2125     LOGOP *rcop;
2126     I32 repl_has_vars = 0;
2127
2128     if (o->op_type == OP_TRANS)
2129         return pmtrans(o, expr, repl);
2130
2131     hints |= HINT_BLOCK_SCOPE;
2132     pm = (PMOP*)o;
2133
2134     if (expr->op_type == OP_CONST) {
2135         STRLEN plen;
2136         SV *pat = ((SVOP*)expr)->op_sv;
2137         char *p = SvPV(pat, plen);
2138         if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2139             sv_setpvn(pat, "\\s+", 3);
2140             p = SvPV(pat, plen);
2141             pm->op_pmflags |= PMf_SKIPWHITE;
2142         }
2143         pm->op_pmregexp = CALLREGCOMP(p, p + plen, pm);
2144         if (strEQ("\\s+", pm->op_pmregexp->precomp))
2145             pm->op_pmflags |= PMf_WHITE;
2146         op_free(expr);
2147     }
2148     else {
2149         if (pm->op_pmflags & PMf_KEEP || !(hints & HINT_RE_EVAL))
2150             expr = newUNOP((!(hints & HINT_RE_EVAL) 
2151                             ? OP_REGCRESET
2152                             : OP_REGCMAYBE),0,expr);
2153
2154         Newz(1101, rcop, 1, LOGOP);
2155         rcop->op_type = OP_REGCOMP;
2156         rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2157         rcop->op_first = scalar(expr);
2158         rcop->op_flags |= ((hints & HINT_RE_EVAL) 
2159                            ? (OPf_SPECIAL | OPf_KIDS)
2160                            : OPf_KIDS);
2161         rcop->op_private = 1;
2162         rcop->op_other = o;
2163
2164         /* establish postfix order */
2165         if (pm->op_pmflags & PMf_KEEP || !(hints & HINT_RE_EVAL)) {
2166             LINKLIST(expr);
2167             rcop->op_next = expr;
2168             ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2169         }
2170         else {
2171             rcop->op_next = LINKLIST(expr);
2172             expr->op_next = (OP*)rcop;
2173         }
2174
2175         prepend_elem(o->op_type, scalar((OP*)rcop), o);
2176     }
2177
2178     if (repl) {
2179         OP *curop;
2180         if (pm->op_pmflags & PMf_EVAL)
2181             curop = 0;
2182 #ifdef USE_THREADS
2183         else if (repl->op_type == OP_THREADSV
2184                  && strchr("&`'123456789+",
2185                            threadsv_names[repl->op_targ]))
2186         {
2187             curop = 0;
2188         }
2189 #endif /* USE_THREADS */
2190         else if (repl->op_type == OP_CONST)
2191             curop = repl;
2192         else {
2193             OP *lastop = 0;
2194             for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2195                 if (opargs[curop->op_type] & OA_DANGEROUS) {
2196 #ifdef USE_THREADS
2197                     if (curop->op_type == OP_THREADSV) {
2198                         repl_has_vars = 1;
2199                         if (strchr("&`'123456789+", curop->op_private))
2200                             break;
2201                     }
2202 #else
2203                     if (curop->op_type == OP_GV) {
2204                         GV *gv = ((GVOP*)curop)->op_gv;
2205                         repl_has_vars = 1;
2206                         if (strchr("&`'123456789+", *GvENAME(gv)))
2207                             break;
2208                     }
2209 #endif /* USE_THREADS */
2210                     else if (curop->op_type == OP_RV2CV)
2211                         break;
2212                     else if (curop->op_type == OP_RV2SV ||
2213                              curop->op_type == OP_RV2AV ||
2214                              curop->op_type == OP_RV2HV ||
2215                              curop->op_type == OP_RV2GV) {
2216                         if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2217                             break;
2218                     }
2219                     else if (curop->op_type == OP_PADSV ||
2220                              curop->op_type == OP_PADAV ||
2221                              curop->op_type == OP_PADHV ||
2222                              curop->op_type == OP_PADANY) {
2223                         repl_has_vars = 1;
2224                     }
2225                     else
2226                         break;
2227                 }
2228                 lastop = curop;
2229             }
2230         }
2231         if (curop == repl
2232             && !(repl_has_vars 
2233                  && (!pm->op_pmregexp 
2234                      || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2235             pm->op_pmflags |= PMf_CONST;        /* const for long enough */
2236             pm->op_pmpermflags |= PMf_CONST;    /* const for long enough */
2237             prepend_elem(o->op_type, scalar(repl), o);
2238         }
2239         else {
2240             if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2241                 pm->op_pmflags |= PMf_MAYBE_CONST;
2242                 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2243             }
2244             Newz(1101, rcop, 1, LOGOP);
2245             rcop->op_type = OP_SUBSTCONT;
2246             rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2247             rcop->op_first = scalar(repl);
2248             rcop->op_flags |= OPf_KIDS;
2249             rcop->op_private = 1;
2250             rcop->op_other = o;
2251
2252             /* establish postfix order */
2253             rcop->op_next = LINKLIST(repl);
2254             repl->op_next = (OP*)rcop;
2255
2256             pm->op_pmreplroot = scalar((OP*)rcop);
2257             pm->op_pmreplstart = LINKLIST(rcop);
2258             rcop->op_next = 0;
2259         }
2260     }
2261
2262     return (OP*)pm;
2263 }
2264
2265 OP *
2266 newSVOP(I32 type, I32 flags, SV *sv)
2267 {
2268     SVOP *svop;
2269     Newz(1101, svop, 1, SVOP);
2270     svop->op_type = type;
2271     svop->op_ppaddr = ppaddr[type];
2272     svop->op_sv = sv;
2273     svop->op_next = (OP*)svop;
2274     svop->op_flags = flags;
2275     if (opargs[type] & OA_RETSCALAR)
2276         scalar((OP*)svop);
2277     if (opargs[type] & OA_TARGET)
2278         svop->op_targ = pad_alloc(type, SVs_PADTMP);
2279     return CHECKOP(type, svop);
2280 }
2281
2282 OP *
2283 newGVOP(I32 type, I32 flags, GV *gv)
2284 {
2285     dTHR;
2286     GVOP *gvop;
2287     Newz(1101, gvop, 1, GVOP);
2288     gvop->op_type = type;
2289     gvop->op_ppaddr = ppaddr[type];
2290     gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2291     gvop->op_next = (OP*)gvop;
2292     gvop->op_flags = flags;
2293     if (opargs[type] & OA_RETSCALAR)
2294         scalar((OP*)gvop);
2295     if (opargs[type] & OA_TARGET)
2296         gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2297     return CHECKOP(type, gvop);
2298 }
2299
2300 OP *
2301 newPVOP(I32 type, I32 flags, char *pv)
2302 {
2303     PVOP *pvop;
2304     Newz(1101, pvop, 1, PVOP);
2305     pvop->op_type = type;
2306     pvop->op_ppaddr = ppaddr[type];
2307     pvop->op_pv = pv;
2308     pvop->op_next = (OP*)pvop;
2309     pvop->op_flags = flags;
2310     if (opargs[type] & OA_RETSCALAR)
2311         scalar((OP*)pvop);
2312     if (opargs[type] & OA_TARGET)
2313         pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2314     return CHECKOP(type, pvop);
2315 }
2316
2317 void
2318 package(OP *o)
2319 {
2320     dTHR;
2321     SV *sv;
2322
2323     save_hptr(&curstash);
2324     save_item(curstname);
2325     if (o) {
2326         STRLEN len;
2327         char *name;
2328         sv = cSVOPo->op_sv;
2329         name = SvPV(sv, len);
2330         curstash = gv_stashpvn(name,len,TRUE);
2331         sv_setpvn(curstname, name, len);
2332         op_free(o);
2333     }
2334     else {
2335         sv_setpv(curstname,"<none>");
2336         curstash = Nullhv;
2337     }
2338     copline = NOLINE;
2339     expect = XSTATE;
2340 }
2341
2342 void
2343 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2344 {
2345     OP *pack;
2346     OP *meth;
2347     OP *rqop;
2348     OP *imop;
2349     OP *veop;
2350
2351     if (id->op_type != OP_CONST)
2352         croak("Module name must be constant");
2353
2354     veop = Nullop;
2355
2356     if(version != Nullop) {
2357         SV *vesv = ((SVOP*)version)->op_sv;
2358
2359         if (arg == Nullop && !SvNIOK(vesv)) {
2360             arg = version;
2361         }
2362         else {
2363             OP *pack;
2364             OP *meth;
2365
2366             if (version->op_type != OP_CONST || !SvNIOK(vesv))
2367                 croak("Version number must be constant number");
2368
2369             /* Make copy of id so we don't free it twice */
2370             pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2371
2372             /* Fake up a method call to VERSION */
2373             meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2374             veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2375                             append_elem(OP_LIST,
2376                             prepend_elem(OP_LIST, pack, list(version)),
2377                             newUNOP(OP_METHOD, 0, meth)));
2378         }
2379     }
2380
2381     /* Fake up an import/unimport */
2382     if (arg && arg->op_type == OP_STUB)
2383         imop = arg;             /* no import on explicit () */
2384     else if(SvNIOK(((SVOP*)id)->op_sv)) {
2385         imop = Nullop;          /* use 5.0; */
2386     }
2387     else {
2388         /* Make copy of id so we don't free it twice */
2389         pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2390         meth = newSVOP(OP_CONST, 0,
2391             aver
2392                 ? newSVpv("import", 6)
2393                 : newSVpv("unimport", 8)
2394             );
2395         imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2396                     append_elem(OP_LIST,
2397                         prepend_elem(OP_LIST, pack, list(arg)),
2398                         newUNOP(OP_METHOD, 0, meth)));
2399     }
2400
2401     /* Fake up a require */
2402     rqop = newUNOP(OP_REQUIRE, 0, id);
2403
2404     /* Fake up the BEGIN {}, which does its thing immediately. */
2405     newSUB(floor,
2406         newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2407         Nullop,
2408         append_elem(OP_LINESEQ,
2409             append_elem(OP_LINESEQ,
2410                 newSTATEOP(0, Nullch, rqop),
2411                 newSTATEOP(0, Nullch, veop)),
2412             newSTATEOP(0, Nullch, imop) ));
2413
2414     copline = NOLINE;
2415     expect = XSTATE;
2416 }
2417
2418 OP *
2419 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2420 {
2421     return newBINOP(OP_LSLICE, flags,
2422             list(force_list(subscript)),
2423             list(force_list(listval)) );
2424 }
2425
2426 STATIC I32
2427 list_assignment(register OP *o)
2428 {
2429     if (!o)
2430         return TRUE;
2431
2432     if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2433         o = cUNOPo->op_first;
2434
2435     if (o->op_type == OP_COND_EXPR) {
2436         I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2437         I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2438
2439         if (t && f)
2440             return TRUE;
2441         if (t || f)
2442             yyerror("Assignment to both a list and a scalar");
2443         return FALSE;
2444     }
2445
2446     if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2447         o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2448         o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2449         return TRUE;
2450
2451     if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2452         return TRUE;
2453
2454     if (o->op_type == OP_RV2SV)
2455         return FALSE;
2456
2457     return FALSE;
2458 }
2459
2460 OP *
2461 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2462 {
2463     OP *o;
2464
2465     if (optype) {
2466         if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2467             return newLOGOP(optype, 0,
2468                 mod(scalar(left), optype),
2469                 newUNOP(OP_SASSIGN, 0, scalar(right)));
2470         }
2471         else {
2472             return newBINOP(optype, OPf_STACKED,
2473                 mod(scalar(left), optype), scalar(right));
2474         }
2475     }
2476
2477     if (list_assignment(left)) {
2478         dTHR;
2479         modcount = 0;
2480         eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
2481         left = mod(left, OP_AASSIGN);
2482         if (eval_start)
2483             eval_start = 0;
2484         else {
2485             op_free(left);
2486             op_free(right);
2487             return Nullop;
2488         }
2489         o = newBINOP(OP_AASSIGN, flags,
2490                 list(force_list(right)),
2491                 list(force_list(left)) );
2492         o->op_private = 0 | (flags >> 8);
2493         if (!(left->op_private & OPpLVAL_INTRO)) {
2494             OP *curop;
2495             OP *lastop = o;
2496             generation++;
2497             for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2498                 if (opargs[curop->op_type] & OA_DANGEROUS) {
2499                     if (curop->op_type == OP_GV) {
2500                         GV *gv = ((GVOP*)curop)->op_gv;
2501                         if (gv == defgv || SvCUR(gv) == generation)
2502                             break;
2503                         SvCUR(gv) = generation;
2504                     }
2505                     else if (curop->op_type == OP_PADSV ||
2506                              curop->op_type == OP_PADAV ||
2507                              curop->op_type == OP_PADHV ||
2508                              curop->op_type == OP_PADANY) {
2509                         SV **svp = AvARRAY(comppad_name);
2510                         SV *sv = svp[curop->op_targ];
2511                         if (SvCUR(sv) == generation)
2512                             break;
2513                         SvCUR(sv) = generation; /* (SvCUR not used any more) */
2514                     }
2515                     else if (curop->op_type == OP_RV2CV)
2516                         break;
2517                     else if (curop->op_type == OP_RV2SV ||
2518                              curop->op_type == OP_RV2AV ||
2519                              curop->op_type == OP_RV2HV ||
2520                              curop->op_type == OP_RV2GV) {
2521                         if (lastop->op_type != OP_GV)   /* funny deref? */
2522                             break;
2523                     }
2524                     else
2525                         break;
2526                 }
2527                 lastop = curop;
2528             }
2529             if (curop != o)
2530                 o->op_private = OPpASSIGN_COMMON;
2531         }
2532         if (right && right->op_type == OP_SPLIT) {
2533             OP* tmpop;
2534             if ((tmpop = ((LISTOP*)right)->op_first) &&
2535                 tmpop->op_type == OP_PUSHRE)
2536             {
2537                 PMOP *pm = (PMOP*)tmpop;
2538                 if (left->op_type == OP_RV2AV &&
2539                     !(left->op_private & OPpLVAL_INTRO) &&
2540                     !(o->op_private & OPpASSIGN_COMMON) )
2541                 {
2542                     tmpop = ((UNOP*)left)->op_first;
2543                     if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2544                         pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2545                         pm->op_pmflags |= PMf_ONCE;
2546                         tmpop = cUNOPo->op_first;       /* to list (nulled) */
2547                         tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2548                         tmpop->op_sibling = Nullop;     /* don't free split */
2549                         right->op_next = tmpop->op_next;  /* fix starting loc */
2550                         op_free(o);                     /* blow off assign */
2551                         right->op_flags &= ~OPf_WANT;
2552                                 /* "I don't know and I don't care." */
2553                         return right;
2554                     }
2555                 }
2556                 else {
2557                     if (modcount < 10000 &&
2558                       ((LISTOP*)right)->op_last->op_type == OP_CONST)
2559                     {
2560                         SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2561                         if (SvIVX(sv) == 0)
2562                             sv_setiv(sv, modcount+1);
2563                     }
2564                 }
2565             }
2566         }
2567         return o;
2568     }
2569     if (!right)
2570         right = newOP(OP_UNDEF, 0);
2571     if (right->op_type == OP_READLINE) {
2572         right->op_flags |= OPf_STACKED;
2573         return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2574     }
2575     else {
2576         eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
2577         o = newBINOP(OP_SASSIGN, flags,
2578             scalar(right), mod(scalar(left), OP_SASSIGN) );
2579         if (eval_start)
2580             eval_start = 0;
2581         else {
2582             op_free(o);
2583             return Nullop;
2584         }
2585     }
2586     return o;
2587 }
2588
2589 OP *
2590 newSTATEOP(I32 flags, char *label, OP *o)
2591 {
2592     dTHR;
2593     U32 seq = intro_my();
2594     register COP *cop;
2595
2596     Newz(1101, cop, 1, COP);
2597     if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2598         cop->op_type = OP_DBSTATE;
2599         cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2600     }
2601     else {
2602         cop->op_type = OP_NEXTSTATE;
2603         cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2604     }
2605     cop->op_flags = flags;
2606     cop->op_private = 0 | (flags >> 8);
2607 #ifdef NATIVE_HINTS
2608     cop->op_private |= NATIVE_HINTS;
2609 #endif
2610     cop->op_next = (OP*)cop;
2611
2612     if (label) {
2613         cop->cop_label = label;
2614         hints |= HINT_BLOCK_SCOPE;
2615     }
2616     cop->cop_seq = seq;
2617     cop->cop_arybase = curcop->cop_arybase;
2618
2619     if (copline == NOLINE)
2620         cop->cop_line = curcop->cop_line;
2621     else {
2622         cop->cop_line = copline;
2623         copline = NOLINE;
2624     }
2625     cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2626     cop->cop_stash = curstash;
2627
2628     if (PERLDB_LINE && curstash != debstash) {
2629         SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2630         if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2631             (void)SvIOK_on(*svp);
2632             SvIVX(*svp) = 1;
2633             SvSTASH(*svp) = (HV*)cop;
2634         }
2635     }
2636
2637     return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2638 }
2639
2640 /* "Introduce" my variables to visible status. */
2641 U32
2642 intro_my(void)
2643 {
2644     SV **svp;
2645     SV *sv;
2646     I32 i;
2647
2648     if (! min_intro_pending)
2649         return cop_seqmax;
2650
2651     svp = AvARRAY(comppad_name);
2652     for (i = min_intro_pending; i <= max_intro_pending; i++) {
2653         if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2654             SvIVX(sv) = 999999999;      /* Don't know scope end yet. */
2655             SvNVX(sv) = (double)cop_seqmax;
2656         }
2657     }
2658     min_intro_pending = 0;
2659     comppad_name_fill = max_intro_pending;      /* Needn't search higher */
2660     return cop_seqmax++;
2661 }
2662
2663 OP *
2664 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2665 {
2666     return new_logop(type, flags, &first, &other);
2667 }
2668
2669 STATIC OP *
2670 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
2671 {
2672     dTHR;
2673     LOGOP *logop;
2674     OP *o;
2675     OP *first = *firstp;
2676     OP *other = *otherp;
2677
2678     if (type == OP_XOR)         /* Not short circuit, but here by precedence. */
2679         return newBINOP(type, flags, scalar(first), scalar(other));
2680
2681     scalarboolean(first);
2682     /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2683     if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2684         if (type == OP_AND || type == OP_OR) {
2685             if (type == OP_AND)
2686                 type = OP_OR;
2687             else
2688                 type = OP_AND;
2689             o = first;
2690             first = *firstp = cUNOPo->op_first;
2691             if (o->op_next)
2692                 first->op_next = o->op_next;
2693             cUNOPo->op_first = Nullop;
2694             op_free(o);
2695         }
2696     }
2697     if (first->op_type == OP_CONST) {
2698         if (dowarn && (first->op_private & OPpCONST_BARE))
2699             warn("Probable precedence problem on %s", op_desc[type]);
2700         if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2701             op_free(first);
2702             *firstp = Nullop;
2703             return other;
2704         }
2705         else {
2706             op_free(other);
2707             *otherp = Nullop;
2708             return first;
2709         }
2710     }
2711     else if (first->op_type == OP_WANTARRAY) {
2712         if (type == OP_AND)
2713             list(other);
2714         else
2715             scalar(other);
2716     }
2717     else if (dowarn && (first->op_flags & OPf_KIDS)) {
2718         OP *k1 = ((UNOP*)first)->op_first;
2719         OP *k2 = k1->op_sibling;
2720         OPCODE warnop = 0;
2721         switch (first->op_type)
2722         {
2723         case OP_NULL:
2724             if (k2 && k2->op_type == OP_READLINE
2725                   && (k2->op_flags & OPf_STACKED)
2726                   && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 
2727                 warnop = k2->op_type;
2728             break;
2729
2730         case OP_SASSIGN:
2731             if (k1->op_type == OP_READDIR
2732                   || k1->op_type == OP_GLOB
2733                   || k1->op_type == OP_EACH)
2734                 warnop = k1->op_type;
2735             break;
2736         }
2737         if (warnop) {
2738             line_t oldline = curcop->cop_line;
2739             curcop->cop_line = copline;
2740             warn("Value of %s%s can be \"0\"; test with defined()",
2741                  op_desc[warnop],
2742                  ((warnop == OP_READLINE || warnop == OP_GLOB)
2743                   ? " construct" : "() operator"));
2744             curcop->cop_line = oldline;
2745         }
2746     }
2747
2748     if (!other)
2749         return first;
2750
2751     if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2752         other->op_private |= OPpASSIGN_BACKWARDS;  /* other is an OP_SASSIGN */
2753
2754     Newz(1101, logop, 1, LOGOP);
2755
2756     logop->op_type = type;
2757     logop->op_ppaddr = ppaddr[type];
2758     logop->op_first = first;
2759     logop->op_flags = flags | OPf_KIDS;
2760     logop->op_other = LINKLIST(other);
2761     logop->op_private = 1 | (flags >> 8);
2762
2763     /* establish postfix order */
2764     logop->op_next = LINKLIST(first);
2765     first->op_next = (OP*)logop;
2766     first->op_sibling = other;
2767
2768     o = newUNOP(OP_NULL, 0, (OP*)logop);
2769     other->op_next = o;
2770
2771     return o;
2772 }
2773
2774 OP *
2775 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2776 {
2777     dTHR;
2778     CONDOP *condop;
2779     OP *o;
2780
2781     if (!falseop)
2782         return newLOGOP(OP_AND, 0, first, trueop);
2783     if (!trueop)
2784         return newLOGOP(OP_OR, 0, first, falseop);
2785
2786     scalarboolean(first);
2787     if (first->op_type == OP_CONST) {
2788         if (SvTRUE(((SVOP*)first)->op_sv)) {
2789             op_free(first);
2790             op_free(falseop);
2791             return trueop;
2792         }
2793         else {
2794             op_free(first);
2795             op_free(trueop);
2796             return falseop;
2797         }
2798     }
2799     else if (first->op_type == OP_WANTARRAY) {
2800         list(trueop);
2801         scalar(falseop);
2802     }
2803     Newz(1101, condop, 1, CONDOP);
2804
2805     condop->op_type = OP_COND_EXPR;
2806     condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2807     condop->op_first = first;
2808     condop->op_flags = flags | OPf_KIDS;
2809     condop->op_true = LINKLIST(trueop);
2810     condop->op_false = LINKLIST(falseop);
2811     condop->op_private = 1 | (flags >> 8);
2812
2813     /* establish postfix order */
2814     condop->op_next = LINKLIST(first);
2815     first->op_next = (OP*)condop;
2816
2817     first->op_sibling = trueop;
2818     trueop->op_sibling = falseop;
2819     o = newUNOP(OP_NULL, 0, (OP*)condop);
2820
2821     trueop->op_next = o;
2822     falseop->op_next = o;
2823
2824     return o;
2825 }
2826
2827 OP *
2828 newRANGE(I32 flags, OP *left, OP *right)
2829 {
2830     dTHR;
2831     CONDOP *condop;
2832     OP *flip;
2833     OP *flop;
2834     OP *o;
2835
2836     Newz(1101, condop, 1, CONDOP);
2837
2838     condop->op_type = OP_RANGE;
2839     condop->op_ppaddr = ppaddr[OP_RANGE];
2840     condop->op_first = left;
2841     condop->op_flags = OPf_KIDS;
2842     condop->op_true = LINKLIST(left);
2843     condop->op_false = LINKLIST(right);
2844     condop->op_private = 1 | (flags >> 8);
2845
2846     left->op_sibling = right;
2847
2848     condop->op_next = (OP*)condop;
2849     flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2850     flop = newUNOP(OP_FLOP, 0, flip);
2851     o = newUNOP(OP_NULL, 0, flop);
2852     linklist(flop);
2853
2854     left->op_next = flip;
2855     right->op_next = flop;
2856
2857     condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2858     sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2859     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2860     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2861
2862     flip->op_private =  left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2863     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2864
2865     flip->op_next = o;
2866     if (!flip->op_private || !flop->op_private)
2867         linklist(o);            /* blow off optimizer unless constant */
2868
2869     return o;
2870 }
2871
2872 OP *
2873 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2874 {
2875     dTHR;
2876     OP* listop;
2877     OP* o;
2878     int once = block && block->op_flags & OPf_SPECIAL &&
2879       (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2880
2881     if (expr) {
2882         if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2883             return block;       /* do {} while 0 does once */
2884         if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2885             || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2886             expr = newUNOP(OP_DEFINED, 0,
2887                 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2888         } else if (expr->op_flags & OPf_KIDS) {
2889             OP *k1 = ((UNOP*)expr)->op_first;
2890             OP *k2 = (k1) ? k1->op_sibling : NULL;
2891             switch (expr->op_type) {
2892               case OP_NULL: 
2893                 if (k2 && k2->op_type == OP_READLINE
2894                       && (k2->op_flags & OPf_STACKED)
2895                       && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 
2896                     expr = newUNOP(OP_DEFINED, 0, expr);
2897                 break;                                
2898
2899               case OP_SASSIGN:
2900                 if (k1->op_type == OP_READDIR
2901                       || k1->op_type == OP_GLOB
2902                       || k1->op_type == OP_EACH)
2903                     expr = newUNOP(OP_DEFINED, 0, expr);
2904                 break;
2905             }
2906         }
2907     }
2908
2909     listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2910     o = new_logop(OP_AND, 0, &expr, &listop);
2911
2912     if (listop)
2913         ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2914
2915     if (once && o != listop)
2916         o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2917
2918     if (o == listop)
2919         o = newUNOP(OP_NULL, 0, o);     /* or do {} while 1 loses outer block */
2920
2921     o->op_flags |= flags;
2922     o = scope(o);
2923     o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2924     return o;
2925 }
2926
2927 OP *
2928 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2929 {
2930     dTHR;
2931     OP *redo;
2932     OP *next = 0;
2933     OP *listop;
2934     OP *o;
2935     OP *condop;
2936
2937     if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2938                  || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2939         expr = newUNOP(OP_DEFINED, 0,
2940             newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2941     } else if (expr && (expr->op_flags & OPf_KIDS)) {
2942         OP *k1 = ((UNOP*)expr)->op_first;
2943         OP *k2 = (k1) ? k1->op_sibling : NULL;
2944         switch (expr->op_type) {
2945           case OP_NULL: 
2946             if (k2 && k2->op_type == OP_READLINE
2947                   && (k2->op_flags & OPf_STACKED)
2948                   && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 
2949                 expr = newUNOP(OP_DEFINED, 0, expr);
2950             break;                                
2951
2952           case OP_SASSIGN:
2953             if (k1->op_type == OP_READDIR
2954                   || k1->op_type == OP_GLOB
2955                   || k1->op_type == OP_EACH)
2956                 expr = newUNOP(OP_DEFINED, 0, expr);
2957             break;
2958         }
2959     }
2960
2961     if (!block)
2962         block = newOP(OP_NULL, 0);
2963
2964     if (cont)
2965         next = LINKLIST(cont);
2966     if (expr) {
2967         cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2968         if ((line_t)whileline != NOLINE) {
2969             copline = whileline;
2970             cont = append_elem(OP_LINESEQ, cont,
2971                                newSTATEOP(0, Nullch, Nullop));
2972         }
2973     }
2974
2975     listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2976     redo = LINKLIST(listop);
2977
2978     if (expr) {
2979         copline = whileline;
2980         scalar(listop);
2981         o = new_logop(OP_AND, 0, &expr, &listop);
2982         if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2983             op_free(expr);              /* oops, it's a while (0) */
2984             op_free((OP*)loop);
2985             return Nullop;              /* listop already freed by new_logop */
2986         }
2987         if (listop)
2988             ((LISTOP*)listop)->op_last->op_next = condop =
2989                 (o == listop ? redo : LINKLIST(o));
2990         if (!next)
2991             next = condop;
2992     }
2993     else
2994         o = listop;
2995
2996     if (!loop) {
2997         Newz(1101,loop,1,LOOP);
2998         loop->op_type = OP_ENTERLOOP;
2999         loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
3000         loop->op_private = 0;
3001         loop->op_next = (OP*)loop;
3002     }
3003
3004     o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3005
3006     loop->op_redoop = redo;
3007     loop->op_lastop = o;
3008
3009     if (next)
3010         loop->op_nextop = next;
3011     else
3012         loop->op_nextop = o;
3013
3014     o->op_flags |= flags;
3015     o->op_private |= (flags >> 8);
3016     return o;
3017 }
3018
3019 OP *
3020 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3021 {
3022     LOOP *loop;
3023     OP *wop;
3024     int padoff = 0;
3025     I32 iterflags = 0;
3026
3027     if (sv) {
3028         if (sv->op_type == OP_RV2SV) {  /* symbol table variable */
3029             sv->op_type = OP_RV2GV;
3030             sv->op_ppaddr = ppaddr[OP_RV2GV];
3031         }
3032         else if (sv->op_type == OP_PADSV) { /* private variable */
3033             padoff = sv->op_targ;
3034             op_free(sv);
3035             sv = Nullop;
3036         }
3037         else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3038             padoff = sv->op_targ;
3039             iterflags |= OPf_SPECIAL;
3040             op_free(sv);
3041             sv = Nullop;
3042         }
3043         else
3044             croak("Can't use %s for loop variable", op_desc[sv->op_type]);
3045     }
3046     else {
3047 #ifdef USE_THREADS
3048         padoff = find_threadsv("_");
3049         iterflags |= OPf_SPECIAL;
3050 #else
3051         sv = newGVOP(OP_GV, 0, defgv);
3052 #endif
3053     }
3054     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3055         expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3056         iterflags |= OPf_STACKED;
3057     }
3058     else if (expr->op_type == OP_NULL &&
3059              (expr->op_flags & OPf_KIDS) &&
3060              ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3061     {
3062         /* Basically turn for($x..$y) into the same as for($x,$y), but we
3063          * set the STACKED flag to indicate that these values are to be
3064          * treated as min/max values by 'pp_iterinit'.
3065          */
3066         UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3067         CONDOP* range = (CONDOP*) flip->op_first;
3068         OP* left  = range->op_first;
3069         OP* right = left->op_sibling;
3070         LISTOP* listop;
3071
3072         range->op_flags &= ~OPf_KIDS;
3073         range->op_first = Nullop;
3074
3075         listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3076         listop->op_first->op_next = range->op_true;
3077         left->op_next = range->op_false;
3078         right->op_next = (OP*)listop;
3079         listop->op_next = listop->op_first;
3080
3081         op_free(expr);
3082         expr = (OP*)(listop);
3083         null(expr);
3084         iterflags |= OPf_STACKED;
3085     }
3086     else {
3087         expr = mod(force_list(expr), OP_GREPSTART);
3088     }
3089
3090
3091     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3092                                append_elem(OP_LIST, expr, scalar(sv))));
3093     assert(!loop->op_next);
3094     Renew(loop, 1, LOOP);
3095     loop->op_targ = padoff;
3096     wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3097     copline = forline;
3098     return newSTATEOP(0, label, wop);
3099 }
3100
3101 OP*
3102 newLOOPEX(I32 type, OP *label)
3103 {
3104     dTHR;
3105     OP *o;
3106     if (type != OP_GOTO || label->op_type == OP_CONST) {
3107         /* "last()" means "last" */
3108         if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3109             o = newOP(type, OPf_SPECIAL);
3110         else {
3111             o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3112                                         ? SvPVx(((SVOP*)label)->op_sv, na)
3113                                         : ""));
3114         }
3115         op_free(label);
3116     }
3117     else {
3118         if (label->op_type == OP_ENTERSUB)
3119             label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3120         o = newUNOP(type, OPf_STACKED, label);
3121     }
3122     hints |= HINT_BLOCK_SCOPE;
3123     return o;
3124 }
3125
3126 void
3127 cv_undef(CV *cv)
3128 {
3129     dTHR;
3130 #ifdef USE_THREADS
3131     if (CvMUTEXP(cv)) {
3132         MUTEX_DESTROY(CvMUTEXP(cv));
3133         Safefree(CvMUTEXP(cv));
3134         CvMUTEXP(cv) = 0;
3135     }
3136 #endif /* USE_THREADS */
3137
3138     if (!CvXSUB(cv) && CvROOT(cv)) {
3139 #ifdef USE_THREADS
3140         if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3141             croak("Can't undef active subroutine");
3142 #else
3143         if (CvDEPTH(cv))
3144             croak("Can't undef active subroutine");
3145 #endif /* USE_THREADS */
3146         ENTER;
3147
3148         SAVESPTR(curpad);
3149         curpad = 0;
3150
3151         if (!CvCLONED(cv))
3152             op_free(CvROOT(cv));
3153         CvROOT(cv) = Nullop;
3154         LEAVE;
3155     }
3156     SvPOK_off((SV*)cv);         /* forget prototype */
3157     CvFLAGS(cv) = 0;
3158     SvREFCNT_dec(CvGV(cv));
3159     CvGV(cv) = Nullgv;
3160     SvREFCNT_dec(CvOUTSIDE(cv));
3161     CvOUTSIDE(cv) = Nullcv;
3162     if (CvPADLIST(cv)) {
3163         /* may be during global destruction */
3164         if (SvREFCNT(CvPADLIST(cv))) {
3165             I32 i = AvFILLp(CvPADLIST(cv));
3166             while (i >= 0) {
3167                 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3168                 SV* sv = svp ? *svp : Nullsv;
3169                 if (!sv)
3170                     continue;
3171                 if (sv == (SV*)comppad_name)
3172                     comppad_name = Nullav;
3173                 else if (sv == (SV*)comppad) {
3174                     comppad = Nullav;
3175                     curpad = Null(SV**);
3176                 }
3177                 SvREFCNT_dec(sv);
3178             }
3179             SvREFCNT_dec((SV*)CvPADLIST(cv));
3180         }
3181         CvPADLIST(cv) = Nullav;
3182     }
3183 }
3184
3185 #ifdef DEBUG_CLOSURES
3186 STATIC void
3187 cv_dump(cv)
3188 CV* cv;
3189 {
3190     CV *outside = CvOUTSIDE(cv);
3191     AV* padlist = CvPADLIST(cv);
3192     AV* pad_name;
3193     AV* pad;
3194     SV** pname;
3195     SV** ppad;
3196     I32 ix;
3197
3198     PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3199                   cv,
3200                   (CvANON(cv) ? "ANON"
3201                    : (cv == main_cv) ? "MAIN"
3202                    : CvUNIQUE(outside) ? "UNIQUE"
3203                    : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3204                   outside,
3205                   (!outside ? "null"
3206                    : CvANON(outside) ? "ANON"
3207                    : (outside == main_cv) ? "MAIN"
3208                    : CvUNIQUE(outside) ? "UNIQUE"
3209                    : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3210
3211     if (!padlist)
3212         return;
3213
3214     pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3215     pad = (AV*)*av_fetch(padlist, 1, FALSE);
3216     pname = AvARRAY(pad_name);
3217     ppad = AvARRAY(pad);
3218
3219     for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3220         if (SvPOK(pname[ix]))
3221             PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3222                           ix, ppad[ix],
3223                           SvFAKE(pname[ix]) ? "FAKE " : "",
3224                           SvPVX(pname[ix]),
3225                           (long)I_32(SvNVX(pname[ix])),
3226                           (long)SvIVX(pname[ix]));
3227     }
3228 }
3229 #endif /* DEBUG_CLOSURES */
3230
3231 STATIC CV *
3232 cv_clone2(CV *proto, CV *outside)
3233 {
3234     dTHR;
3235     AV* av;
3236     I32 ix;
3237     AV* protopadlist = CvPADLIST(proto);
3238     AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3239     AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3240     SV** pname = AvARRAY(protopad_name);
3241     SV** ppad = AvARRAY(protopad);
3242     I32 fname = AvFILLp(protopad_name);
3243     I32 fpad = AvFILLp(protopad);
3244     AV* comppadlist;
3245     CV* cv;
3246
3247     assert(!CvUNIQUE(proto));
3248
3249     ENTER;
3250     SAVESPTR(curpad);
3251     SAVESPTR(comppad);
3252     SAVESPTR(comppad_name);
3253     SAVESPTR(compcv);
3254
3255     cv = compcv = (CV*)NEWSV(1104,0);
3256     sv_upgrade((SV *)cv, SvTYPE(proto));
3257     CvCLONED_on(cv);
3258     if (CvANON(proto))
3259         CvANON_on(cv);
3260
3261 #ifdef USE_THREADS
3262     New(666, CvMUTEXP(cv), 1, perl_mutex);
3263     MUTEX_INIT(CvMUTEXP(cv));
3264     CvOWNER(cv)         = 0;
3265 #endif /* USE_THREADS */
3266     CvFILEGV(cv)        = CvFILEGV(proto);
3267     CvGV(cv)            = (GV*)SvREFCNT_inc(CvGV(proto));
3268     CvSTASH(cv)         = CvSTASH(proto);
3269     CvROOT(cv)          = CvROOT(proto);
3270     CvSTART(cv)         = CvSTART(proto);
3271     if (outside)
3272         CvOUTSIDE(cv)   = (CV*)SvREFCNT_inc(outside);
3273
3274     if (SvPOK(proto))
3275         sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3276
3277     comppad_name = newAV();
3278     for (ix = fname; ix >= 0; ix--)
3279         av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3280
3281     comppad = newAV();
3282
3283     comppadlist = newAV();
3284     AvREAL_off(comppadlist);
3285     av_store(comppadlist, 0, (SV*)comppad_name);
3286     av_store(comppadlist, 1, (SV*)comppad);
3287     CvPADLIST(cv) = comppadlist;
3288     av_fill(comppad, AvFILLp(protopad));
3289     curpad = AvARRAY(comppad);
3290
3291     av = newAV();           /* will be @_ */
3292     av_extend(av, 0);
3293     av_store(comppad, 0, (SV*)av);
3294     AvFLAGS(av) = AVf_REIFY;
3295
3296     for (ix = fpad; ix > 0; ix--) {
3297         SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3298         if (namesv && namesv != &sv_undef) {
3299             char *name = SvPVX(namesv);    /* XXX */
3300             if (SvFLAGS(namesv) & SVf_FAKE) {   /* lexical from outside? */
3301                 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3302                                       CvOUTSIDE(cv), cxstack_ix);
3303                 if (!off)
3304                     curpad[ix] = SvREFCNT_inc(ppad[ix]);
3305                 else if (off != ix)
3306                     croak("panic: cv_clone: %s", name);
3307             }
3308             else {                              /* our own lexical */
3309                 SV* sv;
3310                 if (*name == '&') {
3311                     /* anon code -- we'll come back for it */
3312                     sv = SvREFCNT_inc(ppad[ix]);
3313                 }
3314                 else if (*name == '@')
3315                     sv = (SV*)newAV();
3316                 else if (*name == '%')
3317                     sv = (SV*)newHV();
3318                 else
3319                     sv = NEWSV(0,0);
3320                 if (!SvPADBUSY(sv))
3321                     SvPADMY_on(sv);
3322                 curpad[ix] = sv;
3323             }
3324         }
3325         else {
3326             SV* sv = NEWSV(0,0);
3327             SvPADTMP_on(sv);
3328             curpad[ix] = sv;
3329         }
3330     }
3331
3332     /* Now that vars are all in place, clone nested closures. */
3333
3334     for (ix = fpad; ix > 0; ix--) {
3335         SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3336         if (namesv
3337             && namesv != &sv_undef
3338             && !(SvFLAGS(namesv) & SVf_FAKE)
3339             && *SvPVX(namesv) == '&'
3340             && CvCLONE(ppad[ix]))
3341         {
3342             CV *kid = cv_clone2((CV*)ppad[ix], cv);
3343             SvREFCNT_dec(ppad[ix]);
3344             CvCLONE_on(kid);
3345             SvPADMY_on(kid);
3346             curpad[ix] = (SV*)kid;
3347         }
3348     }
3349
3350 #ifdef DEBUG_CLOSURES
3351     PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3352     cv_dump(outside);
3353     PerlIO_printf(Perl_debug_log, "  from:\n");
3354     cv_dump(proto);
3355     PerlIO_printf(Perl_debug_log, "   to:\n");
3356     cv_dump(cv);
3357 #endif
3358
3359     LEAVE;
3360     return cv;
3361 }
3362
3363 CV *
3364 cv_clone(CV *proto)
3365 {
3366     return cv_clone2(proto, CvOUTSIDE(proto));
3367 }
3368
3369 void
3370 cv_ckproto(CV *cv, GV *gv, char *p)
3371 {
3372     if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3373         SV* msg = sv_newmortal();
3374         SV* name = Nullsv;
3375
3376         if (gv)
3377             gv_efullname3(name = sv_newmortal(), gv, Nullch);
3378         sv_setpv(msg, "Prototype mismatch:");
3379         if (name)
3380             sv_catpvf(msg, " sub %_", name);
3381         if (SvPOK(cv))
3382             sv_catpvf(msg, " (%s)", SvPVX(cv));
3383         sv_catpv(msg, " vs ");
3384         if (p)
3385             sv_catpvf(msg, "(%s)", p);
3386         else
3387             sv_catpv(msg, "none");
3388         warn("%_", msg);
3389     }
3390 }
3391
3392 SV *
3393 cv_const_sv(CV *cv)
3394 {
3395     if (!cv || !SvPOK(cv) || SvCUR(cv))
3396         return Nullsv;
3397     return op_const_sv(CvSTART(cv), cv);
3398 }
3399
3400 SV *
3401 op_const_sv(OP *o, CV *cv)
3402 {
3403     SV *sv = Nullsv;
3404
3405     if(!o)
3406         return Nullsv;
3407  
3408     if(o->op_type == OP_LINESEQ && cLISTOPo->op_first) 
3409         o = cLISTOPo->op_first->op_sibling;
3410
3411     for (; o; o = o->op_next) {
3412         OPCODE type = o->op_type;
3413
3414         if(sv && o->op_next == o) 
3415             return sv;
3416         if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3417             continue;
3418         if (type == OP_LEAVESUB || type == OP_RETURN)
3419             break;
3420         if (sv)
3421             return Nullsv;
3422         if (type == OP_CONST)
3423             sv = cSVOPo->op_sv;
3424         else if (type == OP_PADSV && cv) {
3425             AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3426             sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3427             if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3428                 return Nullsv;
3429         }
3430         else
3431             return Nullsv;
3432     }
3433     if (sv)
3434         SvREADONLY_on(sv);
3435     return sv;
3436 }
3437
3438 CV *
3439 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3440 {
3441     dTHR;
3442     char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3443     GV *gv = gv_fetchpv(name ? name : "__ANON__",
3444                         GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
3445     char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3446     register CV *cv=0;
3447     I32 ix;
3448
3449     if (o)
3450         SAVEFREEOP(o);
3451     if (proto)
3452         SAVEFREEOP(proto);
3453
3454     if (SvTYPE(gv) != SVt_PVGV) {       /* Prototype now, and had
3455                                            maximum a prototype before. */
3456         if (SvTYPE(gv) > SVt_NULL) {
3457             if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1))
3458                 warn("Runaway prototype");
3459             cv_ckproto((CV*)gv, NULL, ps);
3460         }
3461         if (ps)
3462             sv_setpv((SV*)gv, ps);
3463         else
3464             sv_setiv((SV*)gv, -1);
3465         SvREFCNT_dec(compcv);
3466         cv = compcv = NULL;
3467         sub_generation++;
3468         goto noblock;
3469     }
3470
3471     if (!name || GvCVGEN(gv))
3472         cv = Nullcv;
3473     else if (cv = GvCV(gv)) {
3474         cv_ckproto(cv, gv, ps);
3475         /* already defined (or promised)? */
3476         if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3477             SV* const_sv;
3478             bool const_changed = TRUE;
3479             if (!block) {
3480                 /* just a "sub foo;" when &foo is already defined */
3481                 SAVEFREESV(compcv);
3482                 goto done;
3483             }
3484             /* ahem, death to those who redefine active sort subs */
3485             if (curstackinfo->si_type == PERLSI_SORT && sortcop == CvSTART(cv))
3486                 croak("Can't redefine active sort subroutine %s", name);
3487             if(const_sv = cv_const_sv(cv))
3488                 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
3489             if ((const_sv && const_changed) || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3490                                         && HvNAME(GvSTASH(CvGV(cv)))
3491                                         && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3492                                                  "autouse"))) {
3493                 line_t oldline = curcop->cop_line;
3494                 curcop->cop_line = copline;
3495                 warn(const_sv ? "Constant subroutine %s redefined"
3496                      : "Subroutine %s redefined", name);
3497                 curcop->cop_line = oldline;
3498             }
3499             SvREFCNT_dec(cv);
3500             cv = Nullcv;
3501         }
3502     }
3503     if (cv) {                           /* must reuse cv if autoloaded */
3504         cv_undef(cv);
3505         CvFLAGS(cv) = CvFLAGS(compcv);
3506         CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3507         CvOUTSIDE(compcv) = 0;
3508         CvPADLIST(cv) = CvPADLIST(compcv);
3509         CvPADLIST(compcv) = 0;
3510         if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3511             CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3512         SvREFCNT_dec(compcv);
3513     }
3514     else {
3515         cv = compcv;
3516         if (name) {
3517             GvCV(gv) = cv;
3518             GvCVGEN(gv) = 0;
3519             sub_generation++;
3520         }
3521     }
3522     CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3523     CvFILEGV(cv) = curcop->cop_filegv;
3524     CvSTASH(cv) = curstash;
3525 #ifdef USE_THREADS
3526     CvOWNER(cv) = 0;
3527     New(666, CvMUTEXP(cv), 1, perl_mutex);
3528     MUTEX_INIT(CvMUTEXP(cv));
3529 #endif /* USE_THREADS */
3530
3531     if (ps)
3532         sv_setpv((SV*)cv, ps);
3533
3534     if (error_count) {
3535         op_free(block);
3536         block = Nullop;
3537         if (name) {
3538             char *s = strrchr(name, ':');
3539             s = s ? s+1 : name;
3540             if (strEQ(s, "BEGIN")) {
3541                 char *not_safe =
3542                     "BEGIN not safe after errors--compilation aborted";
3543                 if (in_eval & 4)
3544                     croak(not_safe);
3545                 else {
3546                     /* force display of errors found but not reported */
3547                     sv_catpv(ERRSV, not_safe);
3548                     croak("%s", SvPVx(ERRSV, na));
3549                 }
3550             }
3551         }
3552     }
3553     if (!block) {
3554       noblock:
3555         copline = NOLINE;
3556         LEAVE_SCOPE(floor);
3557         return cv;
3558     }
3559
3560     if (AvFILLp(comppad_name) < AvFILLp(comppad))
3561         av_store(comppad_name, AvFILLp(comppad), Nullsv);
3562
3563     if (CvCLONE(cv)) {
3564         SV **namep = AvARRAY(comppad_name);
3565         for (ix = AvFILLp(comppad); ix > 0; ix--) {
3566             SV *namesv;
3567
3568             if (SvIMMORTAL(curpad[ix]))
3569                 continue;
3570             /*
3571              * The only things that a clonable function needs in its
3572              * pad are references to outer lexicals and anonymous subs.
3573              * The rest are created anew during cloning.
3574              */
3575             if (!((namesv = namep[ix]) != Nullsv &&
3576                   namesv != &sv_undef &&
3577                   (SvFAKE(namesv) ||
3578                    *SvPVX(namesv) == '&')))
3579             {
3580                 SvREFCNT_dec(curpad[ix]);
3581                 curpad[ix] = Nullsv;
3582             }
3583         }
3584     }
3585     else {
3586         AV *av = newAV();                       /* Will be @_ */
3587         av_extend(av, 0);
3588         av_store(comppad, 0, (SV*)av);
3589         AvFLAGS(av) = AVf_REIFY;
3590
3591         for (ix = AvFILLp(comppad); ix > 0; ix--) {
3592             if (SvIMMORTAL(curpad[ix]))
3593                 continue;
3594             if (!SvPADMY(curpad[ix]))
3595                 SvPADTMP_on(curpad[ix]);
3596         }
3597     }
3598
3599     CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3600     CvSTART(cv) = LINKLIST(CvROOT(cv));
3601     CvROOT(cv)->op_next = 0;
3602     peep(CvSTART(cv));
3603
3604     if (name) {
3605         char *s;
3606
3607         if (PERLDB_SUBLINE && curstash != debstash) {
3608             SV *sv = NEWSV(0,0);
3609             SV *tmpstr = sv_newmortal();
3610             GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3611             CV *cv;
3612             HV *hv;
3613
3614             sv_setpvf(sv, "%_:%ld-%ld",
3615                     GvSV(curcop->cop_filegv),
3616                     (long)subline, (long)curcop->cop_line);
3617             gv_efullname3(tmpstr, gv, Nullch);
3618             hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3619             hv = GvHVn(db_postponed);
3620             if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3621                   && (cv = GvCV(db_postponed))) {
3622                 dSP;
3623                 PUSHMARK(SP);
3624                 XPUSHs(tmpstr);
3625                 PUTBACK;
3626                 perl_call_sv((SV*)cv, G_DISCARD);
3627             }
3628         }
3629
3630         if ((s = strrchr(name,':')))
3631             s++;
3632         else
3633             s = name;
3634         if (strEQ(s, "BEGIN")) {
3635             I32 oldscope = scopestack_ix;
3636             ENTER;
3637             SAVESPTR(compiling.cop_filegv);
3638             SAVEI16(compiling.cop_line);
3639             save_svref(&rs);
3640             sv_setsv(rs, nrs);
3641
3642             if (!beginav)
3643                 beginav = newAV();
3644             DEBUG_x( dump_sub(gv) );
3645             av_push(beginav, (SV *)cv);
3646             GvCV(gv) = 0;
3647             call_list(oldscope, beginav);
3648
3649             curcop = &compiling;
3650             LEAVE;
3651         }
3652         else if (strEQ(s, "END") && !error_count) {
3653             if (!endav)
3654                 endav = newAV();
3655             av_unshift(endav, 1);
3656             av_store(endav, 0, (SV *)cv);
3657             GvCV(gv) = 0;
3658         }
3659         else if (strEQ(s, "INIT") && !error_count) {
3660             if (!initav)
3661                 initav = newAV();
3662             av_push(initav, SvREFCNT_inc(cv));
3663         }
3664     }
3665
3666   done:
3667     copline = NOLINE;
3668     LEAVE_SCOPE(floor);
3669     return cv;
3670 }
3671
3672 void
3673 newCONSTSUB(HV *stash, char *name, SV *sv)
3674 {
3675     dTHR;
3676     U32 oldhints = hints;
3677     HV *old_cop_stash = curcop->cop_stash;
3678     HV *old_curstash = curstash;
3679     line_t oldline = curcop->cop_line;
3680     curcop->cop_line = copline;
3681
3682     hints &= ~HINT_BLOCK_SCOPE;
3683     if(stash)
3684         curstash = curcop->cop_stash = stash;
3685
3686     newSUB(
3687         start_subparse(FALSE, 0),
3688         newSVOP(OP_CONST, 0, newSVpv(name,0)),
3689         newSVOP(OP_CONST, 0, &sv_no),   /* SvPV(&sv_no) == "" -- GMB */
3690         newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
3691     );
3692
3693     hints = oldhints;
3694     curcop->cop_stash = old_cop_stash;
3695     curstash = old_curstash;
3696     curcop->cop_line = oldline;
3697 }
3698
3699 CV *
3700 newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
3701 {
3702     dTHR;
3703     GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3704     register CV *cv;
3705
3706     if (cv = (name ? GvCV(gv) : Nullcv)) {
3707         if (GvCVGEN(gv)) {
3708             /* just a cached method */
3709             SvREFCNT_dec(cv);
3710             cv = 0;
3711         }
3712         else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3713             /* already defined (or promised) */
3714             if (dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3715                             && HvNAME(GvSTASH(CvGV(cv)))
3716                             && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
3717                 line_t oldline = curcop->cop_line;
3718                 curcop->cop_line = copline;
3719                 warn("Subroutine %s redefined",name);
3720                 curcop->cop_line = oldline;
3721             }
3722             SvREFCNT_dec(cv);
3723             cv = 0;
3724         }
3725     }
3726
3727     if (cv)                             /* must reuse cv if autoloaded */
3728         cv_undef(cv);
3729     else {
3730         cv = (CV*)NEWSV(1105,0);
3731         sv_upgrade((SV *)cv, SVt_PVCV);
3732         if (name) {
3733             GvCV(gv) = cv;
3734             GvCVGEN(gv) = 0;
3735             sub_generation++;
3736         }
3737     }
3738     CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3739 #ifdef USE_THREADS
3740     New(666, CvMUTEXP(cv), 1, perl_mutex);
3741     MUTEX_INIT(CvMUTEXP(cv));
3742     CvOWNER(cv) = 0;
3743 #endif /* USE_THREADS */
3744     CvFILEGV(cv) = gv_fetchfile(filename);
3745     CvXSUB(cv) = subaddr;
3746
3747     if (name) {
3748         char *s = strrchr(name,':');
3749         if (s)
3750             s++;
3751         else
3752             s = name;
3753         if (strEQ(s, "BEGIN")) {
3754             if (!beginav)
3755                 beginav = newAV();
3756             av_push(beginav, (SV *)cv);
3757             GvCV(gv) = 0;
3758         }
3759         else if (strEQ(s, "END")) {
3760             if (!endav)
3761                 endav = newAV();
3762             av_unshift(endav, 1);
3763             av_store(endav, 0, (SV *)cv);
3764             GvCV(gv) = 0;
3765         }
3766         else if (strEQ(s, "INIT")) {
3767             if (!initav)
3768                 initav = newAV();
3769             av_push(initav, (SV *)cv);
3770         }
3771     }
3772     else
3773         CvANON_on(cv);
3774
3775     return cv;
3776 }
3777
3778 void
3779 newFORM(I32 floor, OP *o, OP *block)
3780 {
3781     dTHR;
3782     register CV *cv;
3783     char *name;
3784     GV *gv;
3785     I32 ix;
3786
3787     if (o)
3788         name = SvPVx(cSVOPo->op_sv, na);
3789     else
3790         name = "STDOUT";
3791     gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3792     GvMULTI_on(gv);
3793     if (cv = GvFORM(gv)) {
3794         if (dowarn) {
3795             line_t oldline = curcop->cop_line;
3796
3797             curcop->cop_line = copline;
3798             warn("Format %s redefined",name);
3799             curcop->cop_line = oldline;
3800         }
3801         SvREFCNT_dec(cv);
3802     }
3803     cv = compcv;
3804     GvFORM(gv) = cv;
3805     CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3806     CvFILEGV(cv) = curcop->cop_filegv;
3807
3808     for (ix = AvFILLp(comppad); ix > 0; ix--) {
3809         if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3810             SvPADTMP_on(curpad[ix]);
3811     }
3812
3813     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3814     CvSTART(cv) = LINKLIST(CvROOT(cv));
3815     CvROOT(cv)->op_next = 0;
3816     peep(CvSTART(cv));
3817     op_free(o);
3818     copline = NOLINE;
3819     LEAVE_SCOPE(floor);
3820 }
3821
3822 OP *
3823 newANONLIST(OP *o)
3824 {
3825     return newUNOP(OP_REFGEN, 0,
3826         mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3827 }
3828
3829 OP *
3830 newANONHASH(OP *o)
3831 {
3832     return newUNOP(OP_REFGEN, 0,
3833         mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3834 }
3835
3836 OP *
3837 newANONSUB(I32 floor, OP *proto, OP *block)
3838 {
3839     return newUNOP(OP_REFGEN, 0,
3840         newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3841 }
3842
3843 OP *
3844 oopsAV(OP *o)
3845 {
3846     switch (o->op_type) {
3847     case OP_PADSV:
3848         o->op_type = OP_PADAV;
3849         o->op_ppaddr = ppaddr[OP_PADAV];
3850         return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3851         
3852     case OP_RV2SV:
3853         o->op_type = OP_RV2AV;
3854         o->op_ppaddr = ppaddr[OP_RV2AV];
3855         ref(o, OP_RV2AV);
3856         break;
3857
3858     default:
3859         warn("oops: oopsAV");
3860         break;
3861     }
3862     return o;
3863 }
3864
3865 OP *
3866 oopsHV(OP *o)
3867 {
3868     switch (o->op_type) {
3869     case OP_PADSV:
3870     case OP_PADAV:
3871         o->op_type = OP_PADHV;
3872         o->op_ppaddr = ppaddr[OP_PADHV];
3873         return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3874
3875     case OP_RV2SV:
3876     case OP_RV2AV:
3877         o->op_type = OP_RV2HV;
3878         o->op_ppaddr = ppaddr[OP_RV2HV];
3879         ref(o, OP_RV2HV);
3880         break;
3881
3882     default:
3883         warn("oops: oopsHV");
3884         break;
3885     }
3886     return o;
3887 }
3888
3889 OP *
3890 newAVREF(OP *o)
3891 {
3892     if (o->op_type == OP_PADANY) {
3893         o->op_type = OP_PADAV;
3894         o->op_ppaddr = ppaddr[OP_PADAV];
3895         return o;
3896     }
3897     return newUNOP(OP_RV2AV, 0, scalar(o));
3898 }
3899
3900 OP *
3901 newGVREF(I32 type, OP *o)
3902 {
3903     if (type == OP_MAPSTART)
3904         return newUNOP(OP_NULL, 0, o);
3905     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3906 }
3907
3908 OP *
3909 newHVREF(OP *o)
3910 {
3911     if (o->op_type == OP_PADANY) {
3912         o->op_type = OP_PADHV;
3913         o->op_ppaddr = ppaddr[OP_PADHV];
3914         return o;
3915     }
3916     return newUNOP(OP_RV2HV, 0, scalar(o));
3917 }
3918
3919 OP *
3920 oopsCV(OP *o)
3921 {
3922     croak("NOT IMPL LINE %d",__LINE__);
3923     /* STUB */
3924     return o;
3925 }
3926
3927 OP *
3928 newCVREF(I32 flags, OP *o)
3929 {
3930     return newUNOP(OP_RV2CV, flags, scalar(o));
3931 }
3932
3933 OP *
3934 newSVREF(OP *o)
3935 {
3936     if (o->op_type == OP_PADANY) {
3937         o->op_type = OP_PADSV;
3938         o->op_ppaddr = ppaddr[OP_PADSV];
3939         return o;
3940     }
3941     else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3942         o->op_flags |= OPpDONE_SVREF;
3943         return o;
3944     }
3945     return newUNOP(OP_RV2SV, 0, scalar(o));
3946 }
3947
3948 /* Check routines. */
3949
3950 OP *
3951 ck_anoncode(OP *o)
3952 {
3953     PADOFFSET ix;
3954     SV* name;
3955
3956     name = NEWSV(1106,0);
3957     sv_upgrade(name, SVt_PVNV);
3958     sv_setpvn(name, "&", 1);
3959     SvIVX(name) = -1;
3960     SvNVX(name) = 1;
3961     ix = pad_alloc(o->op_type, SVs_PADMY);
3962     av_store(comppad_name, ix, name);
3963     av_store(comppad, ix, cSVOPo->op_sv);
3964     SvPADMY_on(cSVOPo->op_sv);
3965     cSVOPo->op_sv = Nullsv;
3966     cSVOPo->op_targ = ix;
3967     return o;
3968 }
3969
3970 OP *
3971 ck_bitop(OP *o)
3972 {
3973     o->op_private = hints;
3974     return o;
3975 }
3976
3977 OP *
3978 ck_concat(OP *o)
3979 {
3980     if (cUNOPo->op_first->op_type == OP_CONCAT)
3981         o->op_flags |= OPf_STACKED;
3982     return o;
3983 }
3984
3985 OP *
3986 ck_spair(OP *o)
3987 {
3988     if (o->op_flags & OPf_KIDS) {
3989         OP* newop;
3990         OP* kid;
3991         OPCODE type = o->op_type;
3992         o = modkids(ck_fun(o), type);
3993         kid = cUNOPo->op_first;
3994         newop = kUNOP->op_first->op_sibling;
3995         if (newop &&
3996             (newop->op_sibling ||
3997              !(opargs[newop->op_type] & OA_RETSCALAR) ||
3998              newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3999              newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4000         
4001             return o;
4002         }
4003         op_free(kUNOP->op_first);
4004         kUNOP->op_first = newop;
4005     }
4006     o->op_ppaddr = ppaddr[++o->op_type];
4007     return ck_fun(o);
4008 }
4009
4010 OP *
4011 ck_delete(OP *o)
4012 {
4013     o = ck_fun(o);
4014     o->op_private = 0;
4015     if (o->op_flags & OPf_KIDS) {
4016         OP *kid = cUNOPo->op_first;
4017         if (kid->op_type == OP_HSLICE)
4018             o->op_private |= OPpSLICE;
4019         else if (kid->op_type != OP_HELEM)
4020             croak("%s argument is not a HASH element or slice",
4021                   op_desc[o->op_type]);
4022         null(kid);
4023     }
4024     return o;
4025 }
4026
4027 OP *
4028 ck_eof(OP *o)
4029 {
4030     I32 type = o->op_type;
4031
4032     if (o->op_flags & OPf_KIDS) {
4033         if (cLISTOPo->op_first->op_type == OP_STUB) {
4034             op_free(o);
4035             o = newUNOP(type, OPf_SPECIAL,
4036                 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4037         }
4038         return ck_fun(o);
4039     }
4040     return o;
4041 }
4042
4043 OP *
4044 ck_eval(OP *o)
4045 {
4046     hints |= HINT_BLOCK_SCOPE;
4047     if (o->op_flags & OPf_KIDS) {
4048         SVOP *kid = (SVOP*)cUNOPo->op_first;
4049
4050         if (!kid) {
4051             o->op_flags &= ~OPf_KIDS;
4052             null(o);
4053         }
4054         else if (kid->op_type == OP_LINESEQ) {
4055             LOGOP *enter;
4056
4057             kid->op_next = o->op_next;
4058             cUNOPo->op_first = 0;
4059             op_free(o);
4060
4061             Newz(1101, enter, 1, LOGOP);
4062             enter->op_type = OP_ENTERTRY;
4063             enter->op_ppaddr = ppaddr[OP_ENTERTRY];
4064             enter->op_private = 0;
4065
4066             /* establish postfix order */
4067             enter->op_next = (OP*)enter;
4068
4069             o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4070             o->op_type = OP_LEAVETRY;
4071             o->op_ppaddr = ppaddr[OP_LEAVETRY];
4072             enter->op_other = o;
4073             return o;
4074         }
4075         else
4076             scalar((OP*)kid);
4077     }
4078     else {
4079         op_free(o);
4080         o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4081     }
4082     o->op_targ = (PADOFFSET)hints;
4083     return o;
4084 }
4085
4086 OP *
4087 ck_exec(OP *o)
4088 {
4089     OP *kid;
4090     if (o->op_flags & OPf_STACKED) {
4091         o = ck_fun(o);
4092         kid = cUNOPo->op_first->op_sibling;
4093         if (kid->op_type == OP_RV2GV)
4094             null(kid);
4095     }
4096     else
4097         o = listkids(o);
4098     return o;
4099 }
4100
4101 OP *
4102 ck_exists(OP *o)
4103 {
4104     o = ck_fun(o);
4105     if (o->op_flags & OPf_KIDS) {
4106         OP *kid = cUNOPo->op_first;
4107         if (kid->op_type != OP_HELEM)
4108             croak("%s argument is not a HASH element", op_desc[o->op_type]);
4109         null(kid);
4110     }
4111     return o;
4112 }
4113
4114 OP *
4115 ck_gvconst(register OP *o)
4116 {
4117     o = fold_constants(o);
4118     if (o->op_type == OP_CONST)
4119         o->op_type = OP_GV;
4120     return o;
4121 }
4122
4123 OP *
4124 ck_rvconst(register OP *o)
4125 {
4126     dTHR;
4127     SVOP *kid = (SVOP*)cUNOPo->op_first;
4128
4129     o->op_private |= (hints & HINT_STRICT_REFS);
4130     if (kid->op_type == OP_CONST) {
4131         char *name;
4132         int iscv;
4133         GV *gv;
4134
4135         name = SvPV(kid->op_sv, na);
4136         if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4137             char *badthing = Nullch;
4138             switch (o->op_type) {
4139             case OP_RV2SV:
4140                 badthing = "a SCALAR";
4141                 break;
4142             case OP_RV2AV:
4143                 badthing = "an ARRAY";
4144                 break;
4145             case OP_RV2HV:
4146                 badthing = "a HASH";
4147                 break;
4148             }
4149             if (badthing)
4150                 croak(
4151           "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4152                       name, badthing);
4153         }
4154         /*
4155          * This is a little tricky.  We only want to add the symbol if we
4156          * didn't add it in the lexer.  Otherwise we get duplicate strict
4157          * warnings.  But if we didn't add it in the lexer, we must at
4158          * least pretend like we wanted to add it even if it existed before,
4159          * or we get possible typo warnings.  OPpCONST_ENTERED says
4160          * whether the lexer already added THIS instance of this symbol.
4161          */
4162         iscv = (o->op_type == OP_RV2CV) * 2;
4163         do {
4164             gv = gv_fetchpv(name,
4165                 iscv | !(kid->op_private & OPpCONST_ENTERED),
4166                 iscv
4167                     ? SVt_PVCV
4168                     : o->op_type == OP_RV2SV
4169                         ? SVt_PV
4170                         : o->op_type == OP_RV2AV
4171                             ? SVt_PVAV
4172                             : o->op_type == OP_RV2HV
4173                                 ? SVt_PVHV
4174                                 : SVt_PVGV);
4175         } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4176         if (gv) {
4177             kid->op_type = OP_GV;
4178             SvREFCNT_dec(kid->op_sv);
4179             kid->op_sv = SvREFCNT_inc(gv);
4180         }
4181     }
4182     return o;
4183 }
4184
4185 OP *
4186 ck_ftst(OP *o)
4187 {
4188     dTHR;
4189     I32 type = o->op_type;
4190
4191     if (o->op_flags & OPf_REF)
4192         return o;
4193
4194     if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4195         SVOP *kid = (SVOP*)cUNOPo->op_first;
4196
4197         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4198             OP *newop = newGVOP(type, OPf_REF,
4199                 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
4200             op_free(o);
4201             return newop;
4202         }
4203     }
4204     else {
4205         op_free(o);
4206         if (type == OP_FTTTY)
4207            return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4208                                 SVt_PVIO));
4209         else
4210             return newUNOP(type, 0, newDEFSVOP());
4211     }
4212     return o;
4213 }
4214
4215 OP *
4216 ck_fun(OP *o)
4217 {
4218     dTHR;
4219     register OP *kid;
4220     OP **tokid;
4221     OP *sibl;
4222     I32 numargs = 0;
4223     int type = o->op_type;
4224     register I32 oa = opargs[type] >> OASHIFT;
4225
4226     if (o->op_flags & OPf_STACKED) {
4227         if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4228             oa &= ~OA_OPTIONAL;
4229         else
4230             return no_fh_allowed(o);
4231     }
4232
4233     if (o->op_flags & OPf_KIDS) {
4234         tokid = &cLISTOPo->op_first;
4235         kid = cLISTOPo->op_first;
4236         if (kid->op_type == OP_PUSHMARK ||
4237             kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4238         {
4239             tokid = &kid->op_sibling;
4240             kid = kid->op_sibling;
4241         }
4242         if (!kid && opargs[type] & OA_DEFGV)
4243             *tokid = kid = newDEFSVOP();
4244
4245         while (oa && kid) {
4246             numargs++;
4247             sibl = kid->op_sibling;
4248             switch (oa & 7) {
4249             case OA_SCALAR:
4250                 scalar(kid);
4251                 break;
4252             case OA_LIST:
4253                 if (oa < 16) {
4254                     kid = 0;
4255                     continue;
4256                 }
4257                 else
4258                     list(kid);
4259                 break;
4260             case OA_AVREF:
4261                 if (kid->op_type == OP_CONST &&
4262                   (kid->op_private & OPpCONST_BARE)) {
4263                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4264                     OP *newop = newAVREF(newGVOP(OP_GV, 0,
4265                         gv_fetchpv(name, TRUE, SVt_PVAV) ));
4266                     if (dowarn)
4267                         warn("Array @%s missing the @ in argument %ld of %s()",
4268                             name, (long)numargs, op_desc[type]);
4269                     op_free(kid);
4270                     kid = newop;
4271                     kid->op_sibling = sibl;
4272                     *tokid = kid;
4273                 }
4274                 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4275                     bad_type(numargs, "array", op_desc[o->op_type], kid);
4276                 mod(kid, type);
4277                 break;
4278             case OA_HVREF:
4279                 if (kid->op_type == OP_CONST &&
4280                   (kid->op_private & OPpCONST_BARE)) {
4281                     char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4282                     OP *newop = newHVREF(newGVOP(OP_GV, 0,
4283                         gv_fetchpv(name, TRUE, SVt_PVHV) ));
4284                     if (dowarn)
4285                         warn("Hash %%%s missing the %% in argument %ld of %s()",
4286                             name, (long)numargs, op_desc[type]);
4287                     op_free(kid);
4288                     kid = newop;
4289                     kid->op_sibling = sibl;
4290                     *tokid = kid;
4291                 }
4292                 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4293                     bad_type(numargs, "hash", op_desc[o->op_type], kid);
4294                 mod(kid, type);
4295                 break;
4296             case OA_CVREF:
4297                 {
4298                     OP *newop = newUNOP(OP_NULL, 0, kid);
4299                     kid->op_sibling = 0;
4300                     linklist(kid);
4301                     newop->op_next = newop;
4302                     kid = newop;
4303                     kid->op_sibling = sibl;
4304                     *tokid = kid;
4305                 }
4306                 break;
4307             case OA_FILEREF:
4308                 if (kid->op_type != OP_GV) {
4309                     if (kid->op_type == OP_CONST &&
4310                       (kid->op_private & OPpCONST_BARE)) {
4311                         OP *newop = newGVOP(OP_GV, 0,
4312                             gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4313                                         SVt_PVIO) );
4314                         op_free(kid);
4315                         kid = newop;
4316                     }
4317                     else {
4318                         kid->op_sibling = 0;
4319                         kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4320                     }
4321                     kid->op_sibling = sibl;
4322                     *tokid = kid;
4323                 }
4324                 scalar(kid);
4325                 break;
4326             case OA_SCALARREF:
4327                 mod(scalar(kid), type);
4328                 break;
4329             }
4330             oa >>= 4;
4331             tokid = &kid->op_sibling;
4332             kid = kid->op_sibling;
4333         }
4334         o->op_private |= numargs;
4335         if (kid)
4336             return too_many_arguments(o,op_desc[o->op_type]);
4337         listkids(o);
4338     }
4339     else if (opargs[type] & OA_DEFGV) {
4340         op_free(o);
4341         return newUNOP(type, 0, newDEFSVOP());
4342     }
4343
4344     if (oa) {
4345         while (oa & OA_OPTIONAL)
4346             oa >>= 4;
4347         if (oa && oa != OA_LIST)
4348             return too_few_arguments(o,op_desc[o->op_type]);
4349     }
4350     return o;
4351 }
4352
4353 OP *
4354 ck_glob(OP *o)
4355 {
4356     GV *gv;
4357
4358     if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4359         append_elem(OP_GLOB, o, newDEFSVOP());
4360
4361     if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4362         gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4363
4364     if (gv && GvIMPORTED_CV(gv)) {
4365         static int glob_index;
4366
4367         append_elem(OP_GLOB, o,
4368                     newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4369         o->op_type = OP_LIST;
4370         o->op_ppaddr = ppaddr[OP_LIST];
4371         cLISTOPo->op_first->op_type = OP_PUSHMARK;
4372         cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4373         o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4374                     append_elem(OP_LIST, o,
4375                                 scalar(newUNOP(OP_RV2CV, 0,
4376                                                newGVOP(OP_GV, 0, gv)))));
4377         o = newUNOP(OP_NULL, 0, ck_subr(o));
4378         o->op_targ = OP_GLOB;           /* hint at what it used to be */
4379         return o;
4380     }
4381     gv = newGVgen("main");
4382     gv_IOadd(gv);
4383     append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4384     scalarkids(o);
4385     return ck_fun(o);
4386 }
4387
4388 OP *
4389 ck_grep(OP *o)
4390 {
4391     LOGOP *gwop;
4392     OP *kid;
4393     OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4394
4395     o->op_ppaddr = ppaddr[OP_GREPSTART];
4396     Newz(1101, gwop, 1, LOGOP);
4397
4398     if (o->op_flags & OPf_STACKED) {
4399         OP* k;
4400         o = ck_sort(o);
4401         kid = cLISTOPo->op_first->op_sibling;
4402         for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4403             kid = k;
4404         }
4405         kid->op_next = (OP*)gwop;
4406         o->op_flags &= ~OPf_STACKED;
4407     }
4408     kid = cLISTOPo->op_first->op_sibling;
4409     if (type == OP_MAPWHILE)
4410         list(kid);
4411     else
4412         scalar(kid);
4413     o = ck_fun(o);
4414     if (error_count)
4415         return o;
4416     kid = cLISTOPo->op_first->op_sibling;
4417     if (kid->op_type != OP_NULL)
4418         croak("panic: ck_grep");
4419     kid = kUNOP->op_first;
4420
4421     gwop->op_type = type;
4422     gwop->op_ppaddr = ppaddr[type];
4423     gwop->op_first = listkids(o);
4424     gwop->op_flags |= OPf_KIDS;
4425     gwop->op_private = 1;
4426     gwop->op_other = LINKLIST(kid);
4427     gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4428     kid->op_next = (OP*)gwop;
4429
4430     kid = cLISTOPo->op_first->op_sibling;
4431     if (!kid || !kid->op_sibling)
4432         return too_few_arguments(o,op_desc[o->op_type]);
4433     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4434         mod(kid, OP_GREPSTART);
4435
4436     return (OP*)gwop;
4437 }
4438
4439 OP *
4440 ck_index(OP *o)
4441 {
4442     if (o->op_flags & OPf_KIDS) {
4443         OP *kid = cLISTOPo->op_first->op_sibling;       /* get past pushmark */
4444         if (kid && kid->op_type == OP_CONST)
4445             fbm_compile(((SVOP*)kid)->op_sv, 0);
4446     }
4447     return ck_fun(o);
4448 }
4449
4450 OP *
4451 ck_lengthconst(OP *o)
4452 {
4453     /* XXX length optimization goes here */
4454     return ck_fun(o);
4455 }
4456
4457 OP *
4458 ck_lfun(OP *o)
4459 {
4460     OPCODE type = o->op_type;
4461     return modkids(ck_fun(o), type);
4462 }
4463
4464 OP *
4465 ck_rfun(OP *o)
4466 {
4467     OPCODE type = o->op_type;
4468     return refkids(ck_fun(o), type);
4469 }
4470
4471 OP *
4472 ck_listiob(OP *o)
4473 {
4474     register OP *kid;
4475
4476     kid = cLISTOPo->op_first;
4477     if (!kid) {
4478         o = force_list(o);
4479         kid = cLISTOPo->op_first;
4480     }
4481     if (kid->op_type == OP_PUSHMARK)
4482         kid = kid->op_sibling;
4483     if (kid && o->op_flags & OPf_STACKED)
4484         kid = kid->op_sibling;
4485     else if (kid && !kid->op_sibling) {         /* print HANDLE; */
4486         if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4487             o->op_flags |= OPf_STACKED; /* make it a filehandle */
4488             kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4489             cLISTOPo->op_first->op_sibling = kid;
4490             cLISTOPo->op_last = kid;
4491             kid = kid->op_sibling;
4492         }
4493     }
4494         
4495     if (!kid)
4496         append_elem(o->op_type, o, newDEFSVOP());
4497
4498     o = listkids(o);
4499
4500     o->op_private = 0;
4501 #ifdef USE_LOCALE
4502     if (hints & HINT_LOCALE)
4503         o->op_private |= OPpLOCALE;
4504 #endif
4505
4506     return o;
4507 }
4508
4509 OP *
4510 ck_fun_locale(OP *o)
4511 {
4512     o = ck_fun(o);
4513
4514     o->op_private = 0;
4515 #ifdef USE_LOCALE
4516     if (hints & HINT_LOCALE)
4517         o->op_private |= OPpLOCALE;
4518 #endif
4519
4520     return o;
4521 }
4522
4523 OP *
4524 ck_scmp(OP *o)
4525 {
4526     o->op_private = 0;
4527 #ifdef USE_LOCALE
4528     if (hints & HINT_LOCALE)
4529         o->op_private |= OPpLOCALE;
4530 #endif
4531
4532     return o;
4533 }
4534
4535 OP *
4536 ck_match(OP *o)
4537 {
4538     o->op_private |= OPpRUNTIME;
4539     return o;
4540 }
4541
4542 OP *
4543 ck_null(OP *o)
4544 {
4545     return o;
4546 }
4547
4548 OP *
4549 ck_repeat(OP *o)
4550 {
4551     if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4552         o->op_private |= OPpREPEAT_DOLIST;
4553         cBINOPo->op_first = force_list(cBINOPo->op_first);
4554     }
4555     else
4556         scalar(o);
4557     return o;
4558 }
4559
4560 OP *
4561 ck_require(OP *o)
4562 {
4563     if (o->op_flags & OPf_KIDS) {       /* Shall we supply missing .pm? */
4564         SVOP *kid = (SVOP*)cUNOPo->op_first;
4565
4566         if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4567             char *s;
4568             for (s = SvPVX(kid->op_sv); *s; s++) {
4569                 if (*s == ':' && s[1] == ':') {
4570                     *s = '/';
4571                     Move(s+2, s+1, strlen(s+2)+1, char);
4572                     --SvCUR(kid->op_sv);
4573                 }
4574             }
4575             sv_catpvn(kid->op_sv, ".pm", 3);
4576         }
4577     }
4578     return ck_fun(o);
4579 }
4580
4581 OP *
4582 ck_retarget(OP *o)
4583 {
4584     croak("NOT IMPL LINE %d",__LINE__);
4585     /* STUB */
4586     return o;
4587 }
4588
4589 OP *
4590 ck_select(OP *o)
4591 {
4592     OP* kid;
4593     if (o->op_flags & OPf_KIDS) {
4594         kid = cLISTOPo->op_first->op_sibling;   /* get past pushmark */
4595         if (kid && kid->op_sibling) {
4596             o->op_type = OP_SSELECT;
4597             o->op_ppaddr = ppaddr[OP_SSELECT];
4598             o = ck_fun(o);
4599             return fold_constants(o);
4600         }
4601     }
4602     o = ck_fun(o);
4603     kid = cLISTOPo->op_first->op_sibling;    /* get past pushmark */
4604     if (kid && kid->op_type == OP_RV2GV)
4605         kid->op_private &= ~HINT_STRICT_REFS;
4606     return o;
4607 }
4608
4609 OP *
4610 ck_shift(OP *o)
4611 {
4612     I32 type = o->op_type;
4613
4614     if (!(o->op_flags & OPf_KIDS)) {
4615         OP *argop;
4616         
4617         op_free(o);
4618 #ifdef USE_THREADS
4619         if (!CvUNIQUE(compcv)) {
4620             argop = newOP(OP_PADAV, OPf_REF);
4621             argop->op_targ = 0;         /* curpad[0] is @_ */
4622         }
4623         else {
4624             argop = newUNOP(OP_RV2AV, 0,
4625                 scalar(newGVOP(OP_GV, 0,
4626                     gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4627         }
4628 #else
4629         argop = newUNOP(OP_RV2AV, 0,
4630             scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4631                            defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4632 #endif /* USE_THREADS */
4633         return newUNOP(type, 0, scalar(argop));
4634     }
4635     return scalar(modkids(ck_fun(o), type));
4636 }
4637
4638 OP *
4639 ck_sort(OP *o)
4640 {
4641     o->op_private = 0;
4642 #ifdef USE_LOCALE
4643     if (hints & HINT_LOCALE)
4644         o->op_private |= OPpLOCALE;
4645 #endif
4646
4647     if (o->op_flags & OPf_STACKED) {
4648         OP *kid = cLISTOPo->op_first->op_sibling;       /* get past pushmark */
4649         OP *k;
4650         kid = kUNOP->op_first;                          /* get past rv2gv */
4651
4652         if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4653             linklist(kid);
4654             if (kid->op_type == OP_SCOPE) {
4655                 k = kid->op_next;
4656                 kid->op_next = 0;
4657             }
4658             else if (kid->op_type == OP_LEAVE) {
4659                 if (o->op_type == OP_SORT) {
4660                     null(kid);                  /* wipe out leave */
4661                     kid->op_next = kid;
4662
4663                     for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4664                         if (k->op_next == kid)
4665                             k->op_next = 0;
4666                     }
4667                 }
4668                 else
4669                     kid->op_next = 0;           /* just disconnect the leave */
4670                 k = kLISTOP->op_first;
4671             }
4672             peep(k);
4673
4674             kid = cLISTOPo->op_first->op_sibling;       /* get past pushmark */
4675             null(kid);                                  /* wipe out rv2gv */
4676             if (o->op_type == OP_SORT)
4677                 kid->op_next = kid;
4678             else
4679                 kid->op_next = k;
4680             o->op_flags |= OPf_SPECIAL;
4681         }
4682     }
4683
4684     return o;
4685 }
4686
4687 OP *
4688 ck_split(OP *o)
4689 {
4690     register OP *kid;
4691
4692     if (o->op_flags & OPf_STACKED)
4693         return no_fh_allowed(o);
4694
4695     kid = cLISTOPo->op_first;
4696     if (kid->op_type != OP_NULL)
4697         croak("panic: ck_split");
4698     kid = kid->op_sibling;
4699     op_free(cLISTOPo->op_first);
4700     cLISTOPo->op_first = kid;
4701     if (!kid) {
4702         cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4703         cLISTOPo->op_last = kid; /* There was only one element previously */
4704     }
4705
4706     if (kid->op_type != OP_MATCH) {
4707         OP *sibl = kid->op_sibling;
4708         kid->op_sibling = 0;
4709         kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4710         if (cLISTOPo->op_first == cLISTOPo->op_last)
4711             cLISTOPo->op_last = kid;
4712         cLISTOPo->op_first = kid;
4713         kid->op_sibling = sibl;
4714     }
4715
4716     kid->op_type = OP_PUSHRE;
4717     kid->op_ppaddr = ppaddr[OP_PUSHRE];
4718     scalar(kid);
4719
4720     if (!kid->op_sibling)
4721         append_elem(OP_SPLIT, o, newDEFSVOP());
4722
4723     kid = kid->op_sibling;
4724     scalar(kid);
4725
4726     if (!kid->op_sibling)
4727         append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4728
4729     kid = kid->op_sibling;
4730     scalar(kid);
4731
4732     if (kid->op_sibling)
4733         return too_many_arguments(o,op_desc[o->op_type]);
4734
4735     return o;
4736 }
4737
4738 OP *
4739 ck_subr(OP *o)
4740 {
4741     dTHR;
4742     OP *prev = ((cUNOPo->op_first->op_sibling)
4743              ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4744     OP *o2 = prev->op_sibling;
4745     OP *cvop;
4746     char *proto = 0;
4747     CV *cv = 0;
4748     GV *namegv = 0;
4749     int optional = 0;
4750     I32 arg = 0;
4751
4752     for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4753     if (cvop->op_type == OP_RV2CV) {
4754         SVOP* tmpop;
4755         o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4756         null(cvop);             /* disable rv2cv */
4757         tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4758         if (tmpop->op_type == OP_GV) {
4759             cv = GvCVu(tmpop->op_sv);
4760             if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4761                 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4762                 proto = SvPV((SV*)cv, na);
4763             }
4764         }
4765     }
4766     o->op_private |= (hints & HINT_STRICT_REFS);
4767     if (PERLDB_SUB && curstash != debstash)
4768         o->op_private |= OPpENTERSUB_DB;
4769     while (o2 != cvop) {
4770         if (proto) {
4771             switch (*proto) {
4772             case '\0':
4773                 return too_many_arguments(o, gv_ename(namegv));
4774             case ';':
4775                 optional = 1;
4776                 proto++;
4777                 continue;
4778             case '$':
4779                 proto++;
4780                 arg++;
4781                 scalar(o2);
4782                 break;
4783             case '%':
4784             case '@':
4785                 list(o2);
4786                 arg++;
4787                 break;
4788             case '&':
4789                 proto++;
4790                 arg++;
4791                 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4792                     bad_type(arg, "block", gv_ename(namegv), o2);
4793                 break;
4794             case '*':
4795                 proto++;
4796                 arg++;
4797                 if (o2->op_type == OP_RV2GV)
4798                     goto wrapref;
4799                 {
4800                     OP* kid = o2;
4801                     OP* sib = kid->op_sibling;
4802                     kid->op_sibling = 0;
4803                     o2 = newUNOP(OP_RV2GV, 0, kid);
4804                     o2->op_sibling = sib;
4805                     prev->op_sibling = o2;
4806                 }
4807                 goto wrapref;
4808             case '\\':
4809                 proto++;
4810                 arg++;
4811                 switch (*proto++) {
4812                 case '*':
4813                     if (o2->op_type != OP_RV2GV)
4814                         bad_type(arg, "symbol", gv_ename(namegv), o2);
4815                     goto wrapref;
4816                 case '&':
4817                     if (o2->op_type != OP_RV2CV)
4818                         bad_type(arg, "sub", gv_ename(namegv), o2);
4819                     goto wrapref;
4820                 case '$':
4821                     if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4822                         bad_type(arg, "scalar", gv_ename(namegv), o2);
4823                     goto wrapref;
4824                 case '@':
4825                     if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4826                         bad_type(arg, "array", gv_ename(namegv), o2);
4827                     goto wrapref;
4828                 case '%':
4829                     if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4830                         bad_type(arg, "hash", gv_ename(namegv), o2);
4831                   wrapref:
4832                     {
4833                         OP* kid = o2;
4834                         OP* sib = kid->op_sibling;
4835                         kid->op_sibling = 0;
4836                         o2 = newUNOP(OP_REFGEN, 0, kid);
4837                         o2->op_sibling = sib;
4838                         prev->op_sibling = o2;
4839                     }
4840                     break;
4841                 default: goto oops;
4842                 }
4843                 break;
4844             case ' ':
4845                 proto++;
4846                 continue;
4847             default:
4848               oops:
4849                 croak("Malformed prototype for %s: %s",
4850                         gv_ename(namegv), SvPV((SV*)cv, na));
4851             }
4852         }
4853         else
4854             list(o2);
4855         mod(o2, OP_ENTERSUB);
4856         prev = o2;
4857         o2 = o2->op_sibling;
4858     }
4859     if (proto && !optional &&
4860           (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4861         return too_few_arguments(o, gv_ename(namegv));
4862     return o;
4863 }
4864
4865 OP *
4866 ck_svconst(OP *o)
4867 {
4868     SvREADONLY_on(cSVOPo->op_sv);
4869     return o;
4870 }
4871
4872 OP *
4873 ck_trunc(OP *o)
4874 {
4875     if (o->op_flags & OPf_KIDS) {
4876         SVOP *kid = (SVOP*)cUNOPo->op_first;
4877
4878         if (kid->op_type == OP_NULL)
4879             kid = (SVOP*)kid->op_sibling;
4880         if (kid &&
4881           kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4882             o->op_flags |= OPf_SPECIAL;
4883     }
4884     return ck_fun(o);
4885 }
4886
4887 /* A peephole optimizer.  We visit the ops in the order they're to execute. */
4888
4889 void
4890 peep(register OP *o)
4891 {
4892     dTHR;
4893     register OP* oldop = 0;
4894     if (!o || o->op_seq)
4895         return;
4896     ENTER;
4897     SAVEOP();
4898     SAVESPTR(curcop);
4899     for (; o; o = o->op_next) {
4900         if (o->op_seq)
4901             break;
4902         if (!op_seqmax)
4903             op_seqmax++;
4904         op = o;
4905         switch (o->op_type) {
4906         case OP_NEXTSTATE:
4907         case OP_DBSTATE:
4908             curcop = ((COP*)o);         /* for warnings */
4909             o->op_seq = op_seqmax++;
4910             break;
4911
4912         case OP_CONCAT:
4913         case OP_CONST:
4914         case OP_JOIN:
4915         case OP_UC:
4916         case OP_UCFIRST:
4917         case OP_LC:
4918         case OP_LCFIRST:
4919         case OP_QUOTEMETA:
4920             if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4921                 null(o->op_next);
4922             o->op_seq = op_seqmax++;
4923             break;
4924         case OP_STUB:
4925             if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4926                 o->op_seq = op_seqmax++;
4927                 break; /* Scalar stub must produce undef.  List stub is noop */
4928             }
4929             goto nothin;
4930         case OP_NULL:
4931             if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4932                 curcop = ((COP*)o);
4933             goto nothin;
4934         case OP_SCALAR:
4935         case OP_LINESEQ:
4936         case OP_SCOPE:
4937           nothin:
4938             if (oldop && o->op_next) {
4939                 oldop->op_next = o->op_next;
4940                 continue;
4941             }
4942             o->op_seq = op_seqmax++;
4943             break;
4944
4945         case OP_GV:
4946             if (o->op_next->op_type == OP_RV2SV) {
4947                 if (!(o->op_next->op_private & OPpDEREF)) {
4948                     null(o->op_next);
4949                     o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4950                     o->op_next = o->op_next->op_next;
4951                     o->op_type = OP_GVSV;
4952                     o->op_ppaddr = ppaddr[OP_GVSV];
4953                 }
4954             }
4955             else if (o->op_next->op_type == OP_RV2AV) {
4956                 OP* pop = o->op_next->op_next;
4957                 IV i;
4958                 if (pop->op_type == OP_CONST &&
4959                     (op = pop->op_next) &&
4960                     pop->op_next->op_type == OP_AELEM &&
4961                     !(pop->op_next->op_private &
4962                       (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4963                     (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4964                                 <= 255 &&
4965                     i >= 0)
4966                 {
4967                     SvREFCNT_dec(((SVOP*)pop)->op_sv);
4968                     null(o->op_next);
4969                     null(pop->op_next);
4970                     null(pop);
4971                     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4972                     o->op_next = pop->op_next->op_next;
4973                     o->op_type = OP_AELEMFAST;
4974                     o->op_ppaddr = ppaddr[OP_AELEMFAST];
4975                     o->op_private = (U8)i;
4976                     GvAVn(((GVOP*)o)->op_gv);
4977                 }
4978             }
4979             o->op_seq = op_seqmax++;
4980             break;
4981
4982         case OP_PADAV:
4983             if (o->op_next->op_type == OP_RV2AV
4984                 && (o->op_next->op_flags & OPf_REF))
4985             {
4986                 null(o->op_next);
4987                 o->op_next = o->op_next->op_next;
4988             }
4989             break;
4990         
4991         case OP_PADHV:
4992             if (o->op_next->op_type == OP_RV2HV
4993                 && (o->op_next->op_flags & OPf_REF))
4994             {
4995                 null(o->op_next);
4996                 o->op_next = o->op_next->op_next;
4997             }
4998             break;
4999
5000         case OP_MAPWHILE:
5001         case OP_GREPWHILE:
5002         case OP_AND:
5003         case OP_OR:
5004             o->op_seq = op_seqmax++;
5005             while (cLOGOP->op_other->op_type == OP_NULL)
5006                 cLOGOP->op_other = cLOGOP->op_other->op_next;
5007             peep(cLOGOP->op_other);
5008             break;
5009
5010         case OP_COND_EXPR:
5011             o->op_seq = op_seqmax++;
5012             peep(cCONDOP->op_true);
5013             peep(cCONDOP->op_false);
5014             break;
5015
5016         case OP_ENTERLOOP:
5017             o->op_seq = op_seqmax++;
5018             peep(cLOOP->op_redoop);
5019             peep(cLOOP->op_nextop);
5020             peep(cLOOP->op_lastop);
5021             break;
5022
5023         case OP_MATCH:
5024         case OP_SUBST:
5025             o->op_seq = op_seqmax++;
5026             peep(cPMOP->op_pmreplstart);
5027             break;
5028
5029         case OP_EXEC:
5030             o->op_seq = op_seqmax++;
5031             if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
5032                 if (o->op_next->op_sibling &&
5033                         o->op_next->op_sibling->op_type != OP_EXIT &&
5034                         o->op_next->op_sibling->op_type != OP_WARN &&
5035                         o->op_next->op_sibling->op_type != OP_DIE) {
5036                     line_t oldline = curcop->cop_line;
5037
5038                     curcop->cop_line = ((COP*)o->op_next)->cop_line;
5039                     warn("Statement unlikely to be reached");
5040                     warn("(Maybe you meant system() when you said exec()?)\n");
5041                     curcop->cop_line = oldline;
5042                 }
5043             }
5044             break;
5045         
5046         case OP_HELEM: {
5047             UNOP *rop;
5048             SV *lexname;
5049             GV **fields;
5050             SV **svp, **indsvp;
5051             I32 ind;
5052             char *key;
5053             STRLEN keylen;
5054         
5055             if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
5056                 || ((BINOP*)o)->op_last->op_type != OP_CONST)
5057                 break;
5058             rop = (UNOP*)((BINOP*)o)->op_first;
5059             if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
5060                 break;
5061             lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
5062             if (!SvOBJECT(lexname))
5063                 break;
5064             fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
5065             if (!fields || !GvHV(*fields))
5066                 break;
5067             svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
5068             key = SvPV(*svp, keylen);
5069             indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
5070             if (!indsvp) {
5071                 croak("No such field \"%s\" in variable %s of type %s",
5072                       key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
5073             }
5074             ind = SvIV(*indsvp);
5075             if (ind < 1)
5076                 croak("Bad index while coercing array into hash");
5077             rop->op_type = OP_RV2AV;
5078             rop->op_ppaddr = ppaddr[OP_RV2AV];
5079             o->op_type = OP_AELEM;
5080             o->op_ppaddr = ppaddr[OP_AELEM];
5081             SvREFCNT_dec(*svp);
5082             *svp = newSViv(ind);
5083             break;
5084         }
5085
5086         default:
5087             o->op_seq = op_seqmax++;
5088             break;
5089         }
5090         oldop = o;
5091     }
5092     LEAVE;
5093 }