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