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