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