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