This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.0 alpha 5
[perl5.git] / op.c
CommitLineData
79072805
LW
1/* $RCSfile: cmd.h,v $$Revision: 4.1 $$Date: 92/08/07 17:19:19 $
2 *
3 * Copyright (c) 1991, 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 * $Log: cmd.h,v $
9 */
10
11#include "EXTERN.h"
12#include "perl.h"
13
79072805
LW
14/* Lowest byte of opargs */
15#define OA_MARK 1
16#define OA_FOLDCONST 2
17#define OA_RETSCALAR 4
18#define OA_TARGET 8
19#define OA_RETINTEGER 16
20#define OA_OTHERINT 32
21#define OA_DANGEROUS 64
22
23/* Remaining nybbles of opargs */
24#define OA_SCALAR 1
25#define OA_LIST 2
26#define OA_AVREF 3
27#define OA_HVREF 4
28#define OA_CVREF 5
29#define OA_FILEREF 6
30#define OA_SCALARREF 7
31#define OA_OPTIONAL 8
32
79072805
LW
33void
34cpy7bit(d,s,l)
35register char *d;
36register char *s;
37register I32 l;
38{
39 while (l--)
40 *d++ = *s++ & 127;
41 *d = '\0';
42}
43
79072805
LW
44OP *
45no_fh_allowed(op)
46OP *op;
47{
48 sprintf(tokenbuf,"Missing comma after first argument to %s function",
49 op_name[op->op_type]);
50 yyerror(tokenbuf);
51 return op;
52}
53
54OP *
55too_few_arguments(op)
56OP *op;
57{
58 sprintf(tokenbuf,"Not enough arguments for %s", op_name[op->op_type]);
59 yyerror(tokenbuf);
60 return op;
61}
62
63OP *
64too_many_arguments(op)
65OP *op;
66{
67 sprintf(tokenbuf,"Too many arguments for %s", op_name[op->op_type]);
68 yyerror(tokenbuf);
69 return op;
70}
71
72/* "register" allocation */
73
74PADOFFSET
93a17b20
LW
75pad_allocmy(name)
76char *name;
77{
ed6116ce 78 PADOFFSET off = pad_alloc(OP_PADSV, SVs_PADMY);
93a17b20
LW
79 SV *sv = NEWSV(0,0);
80 sv_upgrade(sv, SVt_PVNV);
81 sv_setpv(sv, name);
82 av_store(comppadname, off, sv);
463ee0b2
LW
83 SvNVX(sv) = (double)cop_seqmax;
84 SvIVX(sv) = 99999999;
93a17b20 85 if (*name == '@')
463ee0b2 86 av_store(comppad, off, (SV*)newAV());
93a17b20 87 else if (*name == '%')
463ee0b2 88 av_store(comppad, off, (SV*)newHV());
ed6116ce 89 SvPADMY_on(curpad[off]);
93a17b20
LW
90 return off;
91}
92
93PADOFFSET
94pad_findmy(name)
95char *name;
96{
97 I32 off;
98 SV *sv;
99 SV **svp = AvARRAY(comppadname);
100 register I32 i;
101 register CONTEXT *cx;
102 bool saweval;
103 AV *curlist;
104 AV *curname;
105 CV *cv;
463ee0b2 106 I32 seq = cop_seqmax;
93a17b20
LW
107
108 for (off = comppadnamefill; off > 0; off--) {
109 if ((sv = svp[off]) &&
463ee0b2
LW
110 seq <= SvIVX(sv) &&
111 seq > (I32)SvNVX(sv) &&
112 strEQ(SvPVX(sv), name))
93a17b20
LW
113 {
114 return (PADOFFSET)off;
115 }
116 }
117
118 /* Nothing in current lexical context--try eval's context, if any.
119 * This is necessary to let the perldb get at lexically scoped variables.
120 * XXX This will also probably interact badly with eval tree caching.
121 */
122
123 saweval = FALSE;
124 for (i = cxstack_ix; i >= 0; i--) {
125 cx = &cxstack[i];
126 switch (cx->cx_type) {
127 default:
128 break;
129 case CXt_EVAL:
130 saweval = TRUE;
131 break;
132 case CXt_SUB:
133 if (!saweval)
134 return 0;
135 cv = cx->blk_sub.cv;
136 if (debstash && CvSTASH(cv) == debstash) /* ignore DB'* scope */
137 continue;
138 seq = cxstack[i+1].blk_oldcop->cop_seq;
139 curlist = CvPADLIST(cv);
140 curname = (AV*)*av_fetch(curlist, 0, FALSE);
141 svp = AvARRAY(curname);
142 for (off = AvFILL(curname); off > 0; off--) {
143 if ((sv = svp[off]) &&
463ee0b2
LW
144 seq <= SvIVX(sv) &&
145 seq > (I32)SvNVX(sv) &&
146 strEQ(SvPVX(sv), name))
93a17b20 147 {
ed6116ce 148 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
93a17b20
LW
149 AV *oldpad = (AV*)*av_fetch(curlist, CvDEPTH(cv), FALSE);
150 SV *oldsv = *av_fetch(oldpad, off, TRUE);
151 SV *sv = NEWSV(0,0);
152 sv_upgrade(sv, SVt_PVNV);
153 sv_setpv(sv, name);
154 av_store(comppadname, newoff, sv);
463ee0b2
LW
155 SvNVX(sv) = (double)curcop->cop_seq;
156 SvIVX(sv) = 99999999;
93a17b20
LW
157 av_store(comppad, newoff, sv_ref(oldsv));
158 return newoff;
159 }
160 }
161 return 0;
162 }
163 }
164
165 return 0;
166}
167
168void
169pad_leavemy(fill)
170I32 fill;
171{
172 I32 off;
173 SV **svp = AvARRAY(comppadname);
174 SV *sv;
175 for (off = AvFILL(comppadname); off > fill; off--) {
176 if (sv = svp[off])
463ee0b2 177 SvIVX(sv) = cop_seqmax;
93a17b20
LW
178 }
179}
180
181PADOFFSET
79072805
LW
182pad_alloc(optype,tmptype)
183I32 optype;
ed6116ce 184U32 tmptype;
79072805
LW
185{
186 SV *sv;
187 I32 retval;
188
189 if (AvARRAY(comppad) != curpad)
463ee0b2 190 croak("panic: pad_alloc");
ed6116ce 191 if (tmptype & SVs_PADMY) {
79072805
LW
192 do {
193 sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
ed6116ce 194 } while (SvPADBUSY(sv)); /* need a fresh one */
79072805
LW
195 retval = AvFILL(comppad);
196 }
197 else {
198 do {
199 sv = *av_fetch(comppad, ++padix, TRUE);
ed6116ce 200 } while (SvSTORAGE(sv) & (SVs_PADTMP|SVs_PADMY));
79072805
LW
201 retval = padix;
202 }
ed6116ce 203 SvSTORAGE(sv) |= tmptype;
79072805
LW
204 curpad = AvARRAY(comppad);
205 DEBUG_X(fprintf(stderr, "Pad alloc %d for %s\n", retval, op_name[optype]));
206 return (PADOFFSET)retval;
207}
208
209SV *
210pad_sv(po)
211PADOFFSET po;
212{
213 if (!po)
463ee0b2 214 croak("panic: pad_sv po");
79072805
LW
215 DEBUG_X(fprintf(stderr, "Pad sv %d\n", po));
216 return curpad[po]; /* eventually we'll turn this into a macro */
217}
218
219void
220pad_free(po)
221PADOFFSET po;
222{
223 if (AvARRAY(comppad) != curpad)
463ee0b2 224 croak("panic: pad_free curpad");
79072805 225 if (!po)
463ee0b2 226 croak("panic: pad_free po");
79072805
LW
227 DEBUG_X(fprintf(stderr, "Pad free %d\n", po));
228 if (curpad[po])
ed6116ce 229 SvPADTMP_off(curpad[po]);
79072805
LW
230 if (po < padix)
231 padix = po - 1;
232}
233
234void
235pad_swipe(po)
236PADOFFSET po;
237{
238 if (AvARRAY(comppad) != curpad)
463ee0b2 239 croak("panic: pad_swipe curpad");
79072805 240 if (!po)
463ee0b2 241 croak("panic: pad_swipe po");
79072805
LW
242 DEBUG_X(fprintf(stderr, "Pad swipe %d\n", po));
243 curpad[po] = NEWSV(0,0);
ed6116ce 244 SvPADTMP_off(curpad[po]);
79072805
LW
245 if (po < padix)
246 padix = po - 1;
247}
248
249void
250pad_reset()
251{
252 register I32 po;
253
254 if (AvARRAY(comppad) != curpad)
463ee0b2 255 croak("panic: pad_reset curpad");
79072805
LW
256 DEBUG_X(fprintf(stderr, "Pad reset\n"));
257 for (po = AvMAX(comppad); po > 0; po--) {
ed6116ce
LW
258 if (curpad[po])
259 SvPADTMP_off(curpad[po]);
79072805
LW
260 }
261 padix = 0;
262}
263
264/* Destructor */
265
266void
267op_free(op)
268OP *op;
269{
270 register OP *kid;
271
272 if (!op)
273 return;
274
275 if (op->op_flags & OPf_KIDS) {
276 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
277 op_free(kid);
278 }
279
280 if (op->op_targ > 0)
281 pad_free(op->op_targ);
282
283 switch (op->op_type) {
463ee0b2 284 case OP_GVSV:
79072805 285 case OP_GV:
463ee0b2 286 sv_free((SV*)cGVOP->op_gv);
79072805
LW
287 break;
288 case OP_CONST:
289 sv_free(cSVOP->op_sv);
290 break;
291 }
292
293 Safefree(op);
294}
295
296/* Contextualizers */
297
463ee0b2 298#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
79072805
LW
299
300OP *
301linklist(op)
302OP *op;
303{
304 register OP *kid;
305
306 if (op->op_next)
307 return op->op_next;
308
309 /* establish postfix order */
310 if (cUNOP->op_first) {
311 op->op_next = LINKLIST(cUNOP->op_first);
312 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
313 if (kid->op_sibling)
314 kid->op_next = LINKLIST(kid->op_sibling);
315 else
316 kid->op_next = op;
317 }
318 }
319 else
320 op->op_next = op;
321
322 return op->op_next;
323}
324
325OP *
326scalarkids(op)
327OP *op;
328{
329 OP *kid;
330 if (op && op->op_flags & OPf_KIDS) {
331 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
332 scalar(kid);
333 }
334 return op;
335}
336
337OP *
338scalar(op)
339OP *op;
340{
341 OP *kid;
342
343 if (!op || (op->op_flags & OPf_KNOW)) /* assumes no premature commitment */
344 return op;
345
346 op->op_flags &= ~OPf_LIST;
347 op->op_flags |= OPf_KNOW;
348
349 switch (op->op_type) {
350 case OP_REPEAT:
351 scalar(cBINOP->op_first);
352 return op;
353 case OP_OR:
354 case OP_AND:
355 case OP_COND_EXPR:
356 break;
357 default:
358 case OP_MATCH:
359 case OP_SUBST:
360 case OP_NULL:
361 if (!(op->op_flags & OPf_KIDS))
362 return op;
363 break;
463ee0b2 364 case OP_SCOPE:
79072805
LW
365 case OP_LEAVE:
366 case OP_LEAVETRY:
367 case OP_LINESEQ:
368 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
369 if (kid->op_sibling)
370 scalarvoid(kid);
371 else
372 scalar(kid);
373 }
93a17b20 374 curcop = &compiling;
79072805
LW
375 return op;
376 case OP_LIST:
377 op = prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), op);
378 break;
379 }
380 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
381 scalar(kid);
382 return op;
383}
384
385OP *
386scalarvoid(op)
387OP *op;
388{
389 OP *kid;
390
391 if (!op)
392 return op;
393 if (op->op_flags & OPf_LIST)
394 return op;
395
396 op->op_flags |= OPf_KNOW;
397
398 switch (op->op_type) {
399 default:
463ee0b2
LW
400 if (dowarn && (opargs[op->op_type] & OA_FOLDCONST) &&
401 !(op->op_flags & OPf_STACKED))
93a17b20 402 warn("Useless use of %s", op_name[op->op_type]);
79072805
LW
403 return op;
404
93a17b20
LW
405 case OP_NEXTSTATE:
406 curcop = ((COP*)op); /* for warning above */
407 break;
408
79072805
LW
409 case OP_CONST:
410 op->op_type = OP_NULL; /* don't execute a constant */
411 sv_free(cSVOP->op_sv); /* don't even remember it */
412 break;
413
414 case OP_POSTINC:
415 op->op_type = OP_PREINC;
416 op->op_ppaddr = ppaddr[OP_PREINC];
417 break;
418
419 case OP_POSTDEC:
420 op->op_type = OP_PREDEC;
421 op->op_ppaddr = ppaddr[OP_PREDEC];
422 break;
423
424 case OP_REPEAT:
425 scalarvoid(cBINOP->op_first);
426 break;
427 case OP_OR:
428 case OP_AND:
429 case OP_COND_EXPR:
430 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
431 scalarvoid(kid);
432 break;
433 case OP_ENTERTRY:
434 case OP_ENTER:
435 case OP_SCALAR:
436 case OP_NULL:
437 if (!(op->op_flags & OPf_KIDS))
438 break;
463ee0b2 439 case OP_SCOPE:
79072805
LW
440 case OP_LEAVE:
441 case OP_LEAVETRY:
442 case OP_LINESEQ:
443 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
444 scalarvoid(kid);
445 break;
446 case OP_LIST:
447 op = prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), op);
448 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
449 scalarvoid(kid);
450 break;
451 }
452 return op;
453}
454
455OP *
456listkids(op)
457OP *op;
458{
459 OP *kid;
460 if (op && op->op_flags & OPf_KIDS) {
461 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
462 list(kid);
463 }
464 return op;
465}
466
467OP *
468list(op)
469OP *op;
470{
471 OP *kid;
472
473 if (!op || (op->op_flags & OPf_KNOW)) /* assumes no premature commitment */
474 return op;
475
476 op->op_flags |= (OPf_KNOW | OPf_LIST);
477
478 switch (op->op_type) {
479 case OP_FLOP:
480 case OP_REPEAT:
481 list(cBINOP->op_first);
482 break;
483 case OP_OR:
484 case OP_AND:
485 case OP_COND_EXPR:
486 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
487 list(kid);
488 break;
489 default:
490 case OP_MATCH:
491 case OP_SUBST:
492 case OP_NULL:
493 if (!(op->op_flags & OPf_KIDS))
494 break;
495 if (!op->op_next && cUNOP->op_first->op_type == OP_FLOP) {
496 list(cBINOP->op_first);
497 return gen_constant_list(op);
498 }
499 case OP_LIST:
500 listkids(op);
501 break;
463ee0b2 502 case OP_SCOPE:
79072805
LW
503 case OP_LEAVE:
504 case OP_LEAVETRY:
505 case OP_LINESEQ:
506 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
507 if (kid->op_sibling)
508 scalarvoid(kid);
509 else
510 list(kid);
511 }
93a17b20 512 curcop = &compiling;
79072805
LW
513 break;
514 }
515 return op;
516}
517
ed6116ce
LW
518static OP *
519guess_mark(op)
520OP *op;
521{
522 if (op->op_type == OP_LIST &&
523 (!cLISTOP->op_first ||
524 cLISTOP->op_first->op_type != OP_PUSHMARK))
525 {
526 op = prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), op);
527 op->op_private |= OPpLIST_GUESSED;
528 }
529 return op;
530}
531
79072805
LW
532OP *
533scalarseq(op)
534OP *op;
535{
536 OP *kid;
ed6116ce 537 OP **prev;
79072805 538
463ee0b2
LW
539 if (op) {
540 if (op->op_type == OP_LINESEQ ||
541 op->op_type == OP_SCOPE ||
79072805 542 op->op_type == OP_LEAVE ||
463ee0b2
LW
543 op->op_type == OP_LEAVETRY)
544 {
ed6116ce 545 prev = &cLISTOP->op_first;
463ee0b2 546 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
ed6116ce 547 if (kid->op_sibling) {
463ee0b2 548 scalarvoid(kid);
ed6116ce
LW
549 prev = &kid->op_sibling;
550 }
551 else
552 *prev = guess_mark(kid);
463ee0b2
LW
553 }
554 curcop = &compiling;
79072805 555 }
463ee0b2
LW
556 op->op_flags &= ~OPf_PARENS;
557 if (needblockscope)
558 op->op_flags |= OPf_PARENS;
79072805
LW
559 }
560 return op;
561}
562
563OP *
463ee0b2 564modkids(op, type)
79072805
LW
565OP *op;
566I32 type;
567{
568 OP *kid;
569 if (op && op->op_flags & OPf_KIDS) {
570 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
463ee0b2 571 mod(kid, type);
79072805
LW
572 }
573 return op;
574}
575
463ee0b2 576static I32 modcount;
79072805
LW
577
578OP *
463ee0b2 579mod(op, type)
79072805
LW
580OP *op;
581I32 type;
582{
583 OP *kid;
584 SV *sv;
585
586 if (!op)
587 return op;
588
589 switch (op->op_type) {
590 case OP_ENTERSUBR:
591 if ((type == OP_DEFINED || type == OP_UNDEF || type == OP_REFGEN) &&
592 !(op->op_flags & OPf_STACKED)) {
93a17b20
LW
593 op->op_type = OP_RV2CV; /* entersubr => rv2cv */
594 op->op_ppaddr = ppaddr[OP_RV2CV];
595 cUNOP->op_first->op_type = OP_NULL; /* disable pushmark */
596 cUNOP->op_first->op_ppaddr = ppaddr[OP_NULL];
79072805
LW
597 break;
598 }
599 /* FALL THROUGH */
600 default:
601 if (type == OP_DEFINED)
602 return scalar(op); /* ordinary expression, not lvalue */
603 sprintf(tokenbuf, "Can't %s %s in %s",
604 type == OP_REFGEN ? "refer to" : "modify",
605 op_name[op->op_type],
606 type ? op_name[type] : "local");
607 yyerror(tokenbuf);
608 return op;
609
610 case OP_COND_EXPR:
611 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 612 mod(kid, type);
79072805
LW
613 break;
614
615 case OP_RV2AV:
616 case OP_RV2HV:
617 case OP_RV2GV:
93a17b20 618 ref(cUNOP->op_first, op->op_type);
79072805
LW
619 /* FALL THROUGH */
620 case OP_AASSIGN:
621 case OP_ASLICE:
622 case OP_HSLICE:
93a17b20
LW
623 case OP_NEXTSTATE:
624 case OP_DBSTATE:
463ee0b2 625 modcount = 10000;
79072805 626 break;
463ee0b2
LW
627 case OP_RV2SV:
628 if (type == OP_RV2AV || type == OP_RV2HV)
629 op->op_private = type;
630 /* FALL THROUGH */
93a17b20
LW
631 case OP_PADSV:
632 case OP_PADAV:
633 case OP_PADHV:
79072805
LW
634 case OP_UNDEF:
635 case OP_GV:
463ee0b2
LW
636 case OP_AV2ARYLEN:
637 case OP_SASSIGN:
638 case OP_REFGEN:
639 case OP_ANONLIST:
640 case OP_ANONHASH:
641 modcount++;
642 break;
643
644 case OP_PUSHMARK:
645 break;
646
647 case OP_SUBSTR:
648 case OP_VEC:
ed6116ce 649 op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
463ee0b2
LW
650 sv = PAD_SV(op->op_targ);
651 sv_upgrade(sv, SVt_PVLV);
652 sv_magic(sv, 0, op->op_type == OP_VEC ? 'v' : 'x', 0, 0);
653 curpad[op->op_targ] = sv;
654 /* FALL THROUGH */
655 case OP_NULL:
656 if (!(op->op_flags & OPf_KIDS))
657 croak("panic: mod");
658 mod(cBINOP->op_first, type ? type : op->op_type);
659 break;
660 case OP_AELEM:
661 case OP_HELEM:
662 mod(cBINOP->op_first, type ? type : op->op_type);
663 if (type == OP_RV2AV || type == OP_RV2HV)
664 op->op_private = type;
665 break;
666
667 case OP_SCOPE:
668 case OP_LEAVE:
669 case OP_ENTER:
670 if (type != OP_RV2HV && type != OP_RV2AV)
671 break;
672 if (!(op->op_flags & OPf_KIDS))
673 break;
674 /* FALL THROUGH */
675 case OP_LIST:
676 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
677 mod(kid, type);
678 break;
679 }
680 op->op_flags |= OPf_LVAL;
681 if (!type) {
682 op->op_flags &= ~OPf_SPECIAL;
683 op->op_flags |= OPf_INTRO;
684 }
685 else if (type == OP_AASSIGN || type == OP_SASSIGN)
686 op->op_flags |= OPf_SPECIAL;
687 return op;
688}
689
690OP *
691refkids(op, type)
692OP *op;
693I32 type;
694{
695 OP *kid;
696 if (op && op->op_flags & OPf_KIDS) {
697 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
698 ref(kid, type);
699 }
700 return op;
701}
702
703OP *
704ref(op, type)
705OP *op;
706I32 type;
707{
708 OP *kid;
709 SV *sv;
710
711 if (!op)
712 return op;
713
714 switch (op->op_type) {
715 default:
716 sprintf(tokenbuf, "Can't use %s as reference in %s",
717 op_name[op->op_type],
718 type ? op_name[type] : "local");
719 yyerror(tokenbuf);
720 return op;
721
722 case OP_COND_EXPR:
723 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
724 ref(kid, type);
725 break;
726
727 case OP_RV2AV:
728 case OP_RV2HV:
729 case OP_RV2GV:
730 ref(cUNOP->op_first, op->op_type);
731 /* FALL THROUGH */
732 case OP_AASSIGN:
733 case OP_ASLICE:
734 case OP_HSLICE:
735 case OP_NEXTSTATE:
736 case OP_DBSTATE:
737 case OP_ENTERSUBR:
738 break;
79072805 739 case OP_RV2SV:
463ee0b2
LW
740 if (type == OP_RV2AV || type == OP_RV2HV)
741 op->op_private = type;
742 /* FALL THROUGH */
743 case OP_PADSV:
744 case OP_PADAV:
745 case OP_PADHV:
746 case OP_UNDEF:
747 case OP_GV:
79072805
LW
748 case OP_AV2ARYLEN:
749 case OP_SASSIGN:
750 case OP_REFGEN:
751 case OP_ANONLIST:
752 case OP_ANONHASH:
79072805
LW
753 break;
754
755 case OP_PUSHMARK:
756 break;
757
758 case OP_SUBSTR:
759 case OP_VEC:
ed6116ce 760 op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
79072805
LW
761 sv = PAD_SV(op->op_targ);
762 sv_upgrade(sv, SVt_PVLV);
763 sv_magic(sv, 0, op->op_type == OP_VEC ? 'v' : 'x', 0, 0);
764 curpad[op->op_targ] = sv;
765 /* FALL THROUGH */
766 case OP_NULL:
767 if (!(op->op_flags & OPf_KIDS))
463ee0b2 768 break;
79072805
LW
769 ref(cBINOP->op_first, type ? type : op->op_type);
770 break;
771 case OP_AELEM:
772 case OP_HELEM:
773 ref(cBINOP->op_first, type ? type : op->op_type);
93a17b20
LW
774 if (type == OP_RV2AV || type == OP_RV2HV)
775 op->op_private = type;
79072805
LW
776 break;
777
463ee0b2 778 case OP_SCOPE:
79072805
LW
779 case OP_LEAVE:
780 case OP_ENTER:
781 if (type != OP_RV2HV && type != OP_RV2AV)
782 break;
783 if (!(op->op_flags & OPf_KIDS))
784 break;
785 /* FALL THROUGH */
786 case OP_LIST:
787 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
788 ref(kid, type);
789 break;
790 }
791 op->op_flags |= OPf_LVAL;
792 if (!type) {
793 op->op_flags &= ~OPf_SPECIAL;
93a17b20 794 op->op_flags |= OPf_INTRO;
79072805
LW
795 }
796 else if (type == OP_AASSIGN || type == OP_SASSIGN)
797 op->op_flags |= OPf_SPECIAL;
798 return op;
799}
800
801OP *
93a17b20
LW
802my(op)
803OP *op;
804{
805 OP *kid;
806 SV *sv;
807 I32 type;
808
809 if (!op)
810 return op;
811
812 type = op->op_type;
813 if (type == OP_LIST) {
814 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
815 my(kid);
816 }
817 else if (type != OP_PADSV &&
818 type != OP_PADAV &&
819 type != OP_PADHV &&
820 type != OP_PUSHMARK)
821 {
822 sprintf(tokenbuf, "Can't declare %s in my", op_name[op->op_type]);
823 yyerror(tokenbuf);
824 return op;
825 }
826 op->op_flags |= OPf_LVAL|OPf_INTRO;
827 return op;
828}
829
830OP *
79072805
LW
831sawparens(o)
832OP *o;
833{
834 if (o)
835 o->op_flags |= OPf_PARENS;
836 return o;
837}
838
839OP *
840bind_match(type, left, right)
841I32 type;
842OP *left;
843OP *right;
844{
845 OP *op;
846
847 if (right->op_type == OP_MATCH ||
848 right->op_type == OP_SUBST ||
849 right->op_type == OP_TRANS) {
850 right->op_flags |= OPf_STACKED;
851 if (right->op_type != OP_MATCH)
463ee0b2 852 left = mod(left, right->op_type);
79072805
LW
853 if (right->op_type == OP_TRANS)
854 op = newBINOP(OP_NULL, 0, scalar(left), right);
855 else
856 op = prepend_elem(right->op_type, scalar(left), right);
857 if (type == OP_NOT)
858 return newUNOP(OP_NOT, 0, scalar(op));
859 return op;
860 }
861 else
862 return bind_match(type, left,
863 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
864}
865
866OP *
867invert(op)
868OP *op;
869{
870 if (!op)
871 return op;
872 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
873 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
874}
875
876OP *
877scope(o)
878OP *o;
879{
880 if (o) {
463ee0b2
LW
881 if (o->op_flags & OPf_PARENS) {
882 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
883 o->op_type = OP_LEAVE;
884 o->op_ppaddr = ppaddr[OP_LEAVE];
885 }
886 else {
887 if (o->op_type == OP_LINESEQ) {
888 OP *kid;
889 o->op_type = OP_SCOPE;
890 o->op_ppaddr = ppaddr[OP_SCOPE];
891 kid = ((LISTOP*)o)->op_first;
892 if (kid->op_type == OP_NEXTSTATE) {
893 kid->op_type = OP_NULL;
894 kid->op_ppaddr = ppaddr[OP_NULL];
895 }
896 }
897 else
898 o = newUNOP(OP_SCOPE, 0, o);
899 }
79072805
LW
900 }
901 return o;
902}
903
904OP *
905block_head(o, startp)
906OP *o;
907OP **startp;
908{
909 if (!o) {
910 *startp = 0;
911 return o;
912 }
463ee0b2 913 o = scope(scalarseq(o));
79072805
LW
914 *startp = LINKLIST(o);
915 o->op_next = 0;
916 peep(*startp);
917 return o;
918}
919
920OP *
93a17b20 921localize(o, lex)
79072805 922OP *o;
93a17b20 923I32 lex;
79072805
LW
924{
925 if (o->op_flags & OPf_PARENS)
926 list(o);
927 else
928 scalar(o);
93a17b20
LW
929 in_my = FALSE;
930 if (lex)
931 return my(o);
932 else
463ee0b2 933 return mod(o, OP_NULL); /* a bit kludgey */
79072805
LW
934}
935
936OP *
937jmaybe(o)
938OP *o;
939{
940 if (o->op_type == OP_LIST) {
941 o = convert(OP_JOIN, 0,
942 prepend_elem(OP_LIST,
943 newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE))),
944 o));
945 }
946 return o;
947}
948
949OP *
950fold_constants(o)
951register OP *o;
952{
953 register OP *curop;
954 I32 type = o->op_type;
955 SV *sv;
956
957 if (opargs[type] & OA_RETSCALAR)
958 scalar(o);
959 if (opargs[type] & OA_TARGET)
ed6116ce 960 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805
LW
961
962 if (!(opargs[type] & OA_FOLDCONST))
963 goto nope;
964
965 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
93a17b20
LW
966 if (curop->op_type != OP_CONST &&
967 curop->op_type != OP_LIST &&
968 curop->op_type != OP_SCALAR &&
969 curop->op_type != OP_PUSHMARK) {
79072805
LW
970 goto nope;
971 }
972 }
973
974 curop = LINKLIST(o);
975 o->op_next = 0;
976 op = curop;
977 run();
978 if (o->op_targ && *stack_sp == PAD_SV(o->op_targ))
979 pad_swipe(o->op_targ);
980 op_free(o);
981 if (type == OP_RV2GV)
982 return newGVOP(OP_GV, 0, *(stack_sp--));
983 else
984 return newSVOP(OP_CONST, 0, *(stack_sp--));
985
986 nope:
987 if (!(opargs[type] & OA_OTHERINT))
988 return o;
989 if (!(o->op_flags & OPf_KIDS))
990 return o;
991
992 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
993 if (curop->op_type == OP_CONST) {
994 if (SvIOK(((SVOP*)curop)->op_sv))
995 continue;
996 return o;
997 }
998 if (opargs[curop->op_type] & OA_RETINTEGER)
999 continue;
1000 return o;
1001 }
1002
1003 o->op_ppaddr = ppaddr[++(o->op_type)];
1004 return o;
1005}
1006
1007OP *
1008gen_constant_list(o)
1009register OP *o;
1010{
1011 register OP *curop;
1012 OP *anonop;
1013 I32 tmpmark;
1014 I32 tmpsp;
1015 I32 oldtmps_floor = tmps_floor;
1016 AV *av;
1017 GV *gv;
1018
1019 tmpmark = stack_sp - stack_base;
1020 anonop = newANONLIST(o);
1021 curop = LINKLIST(anonop);
1022 anonop->op_next = 0;
1023 op = curop;
1024 run();
1025 tmpsp = stack_sp - stack_base;
1026 tmps_floor = oldtmps_floor;
1027 stack_sp = stack_base + tmpmark;
1028
1029 o->op_type = OP_RV2AV;
1030 o->op_ppaddr = ppaddr[OP_RV2AV];
1031 o->op_sibling = 0;
1032 curop = ((UNOP*)o)->op_first;
1033 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, newSVsv(stack_sp[1]));
1034 op_free(curop);
1035 curop = ((UNOP*)anonop)->op_first;
1036 curop = ((UNOP*)curop)->op_first;
1037 curop->op_sibling = 0;
1038 op_free(anonop);
1039 o->op_next = 0;
1040 linklist(o);
1041 return list(o);
1042}
1043
1044OP *
1045convert(type, flags, op)
1046I32 type;
1047I32 flags;
1048OP* op;
1049{
1050 OP *kid;
1051 OP *last;
1052
1053 if (opargs[type] & OA_MARK)
1054 op = prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), op);
1055
1056 if (!op || op->op_type != OP_LIST)
1057 op = newLISTOP(OP_LIST, 0, op, Nullop);
1058
1059 op->op_type = type;
1060 op->op_ppaddr = ppaddr[type];
1061 op->op_flags |= flags;
1062
1063 op = (*check[type])(op);
1064 if (op->op_type != type)
1065 return op;
1066
1067 if (cLISTOP->op_children < 7) {
1068 /* XXX do we really need to do this if we're done appending?? */
1069 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1070 last = kid;
1071 cLISTOP->op_last = last; /* in case check substituted last arg */
1072 }
1073
1074 return fold_constants(op);
1075}
1076
1077/* List constructors */
1078
1079OP *
1080append_elem(type, first, last)
1081I32 type;
1082OP* first;
1083OP* last;
1084{
1085 if (!first)
1086 return last;
1087 else if (!last)
1088 return first;
1089 else if (first->op_type == type) {
1090 if (first->op_flags & OPf_KIDS)
1091 ((LISTOP*)first)->op_last->op_sibling = last;
1092 else {
1093 first->op_flags |= OPf_KIDS;
1094 ((LISTOP*)first)->op_first = last;
1095 }
1096 ((LISTOP*)first)->op_last = last;
1097 ((LISTOP*)first)->op_children++;
1098 return first;
1099 }
1100
1101 return newLISTOP(type, 0, first, last);
1102}
1103
1104OP *
1105append_list(type, first, last)
1106I32 type;
1107LISTOP* first;
1108LISTOP* last;
1109{
1110 if (!first)
1111 return (OP*)last;
1112 else if (!last)
1113 return (OP*)first;
1114 else if (first->op_type != type)
1115 return prepend_elem(type, (OP*)first, (OP*)last);
1116 else if (last->op_type != type)
1117 return append_elem(type, (OP*)first, (OP*)last);
1118
1119 first->op_last->op_sibling = last->op_first;
1120 first->op_last = last->op_last;
1121 first->op_children += last->op_children;
1122 if (first->op_children)
1123 last->op_flags |= OPf_KIDS;
1124
1125 Safefree(last);
1126 return (OP*)first;
1127}
1128
1129OP *
1130prepend_elem(type, first, last)
1131I32 type;
1132OP* first;
1133OP* last;
1134{
1135 if (!first)
1136 return last;
1137 else if (!last)
1138 return first;
1139 else if (last->op_type == type) {
1140 if (!(last->op_flags & OPf_KIDS)) {
1141 ((LISTOP*)last)->op_last = first;
1142 last->op_flags |= OPf_KIDS;
1143 }
1144 first->op_sibling = ((LISTOP*)last)->op_first;
1145 ((LISTOP*)last)->op_first = first;
1146 ((LISTOP*)last)->op_children++;
1147 return last;
1148 }
1149
1150 return newLISTOP(type, 0, first, last);
1151}
1152
1153/* Constructors */
1154
1155OP *
1156newNULLLIST()
1157{
1158 return Nullop;
1159}
1160
1161OP *
1162newLISTOP(type, flags, first, last)
1163I32 type;
1164I32 flags;
1165OP* first;
1166OP* last;
1167{
1168 LISTOP *listop;
1169
1170 Newz(1101, listop, 1, LISTOP);
1171
1172 listop->op_type = type;
1173 listop->op_ppaddr = ppaddr[type];
1174 listop->op_children = (first != 0) + (last != 0);
1175 listop->op_flags = flags;
1176 if (listop->op_children)
1177 listop->op_flags |= OPf_KIDS;
1178
1179 if (!last && first)
1180 last = first;
1181 else if (!first && last)
1182 first = last;
1183 listop->op_first = first;
1184 listop->op_last = last;
1185 if (first && first != last)
1186 first->op_sibling = last;
1187
1188 return (OP*)listop;
1189}
1190
1191OP *
1192newOP(type, flags)
1193I32 type;
1194I32 flags;
1195{
1196 OP *op;
1197 Newz(1101, op, 1, OP);
1198 op->op_type = type;
1199 op->op_ppaddr = ppaddr[type];
1200 op->op_flags = flags;
1201
1202 op->op_next = op;
1203 /* op->op_private = 0; */
1204 if (opargs[type] & OA_RETSCALAR)
1205 scalar(op);
1206 if (opargs[type] & OA_TARGET)
ed6116ce 1207 op->op_targ = pad_alloc(type, SVs_PADTMP);
79072805
LW
1208 return (*check[type])(op);
1209}
1210
1211OP *
1212newUNOP(type, flags, first)
1213I32 type;
1214I32 flags;
1215OP* first;
1216{
1217 UNOP *unop;
1218
1219 if (opargs[type] & OA_MARK) {
1220 if (first->op_type == OP_LIST)
1221 prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), first);
1222 else
1223 return newBINOP(type, flags, newOP(OP_PUSHMARK, 0), first);
1224 }
1225
93a17b20
LW
1226 if (!first)
1227 first = newOP(OP_STUB, 0);
1228
79072805
LW
1229 Newz(1101, unop, 1, UNOP);
1230 unop->op_type = type;
1231 unop->op_ppaddr = ppaddr[type];
1232 unop->op_first = first;
1233 unop->op_flags = flags | OPf_KIDS;
1234 unop->op_private = 1;
1235
1236 unop = (UNOP*)(*check[type])((OP*)unop);
1237 if (unop->op_next)
1238 return (OP*)unop;
1239
1240 return fold_constants(unop);
1241}
1242
1243OP *
1244newBINOP(type, flags, first, last)
1245I32 type;
1246I32 flags;
1247OP* first;
1248OP* last;
1249{
1250 BINOP *binop;
1251 Newz(1101, binop, 1, BINOP);
1252
1253 if (!first)
1254 first = newOP(OP_NULL, 0);
1255
1256 binop->op_type = type;
1257 binop->op_ppaddr = ppaddr[type];
1258 binop->op_first = first;
1259 binop->op_flags = flags | OPf_KIDS;
1260 if (!last) {
1261 last = first;
1262 binop->op_private = 1;
1263 }
1264 else {
1265 binop->op_private = 2;
1266 first->op_sibling = last;
1267 }
1268
1269 binop = (BINOP*)(*check[type])((OP*)binop);
1270 if (binop->op_next)
1271 return (OP*)binop;
1272
1273 binop->op_last = last = binop->op_first->op_sibling;
1274
1275 return fold_constants(binop);
1276}
1277
1278OP *
1279pmtrans(op, expr, repl)
1280OP *op;
1281OP *expr;
1282OP *repl;
1283{
1284 PMOP *pm = (PMOP*)op;
1285 SV *tstr = ((SVOP*)expr)->op_sv;
1286 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
1287 STRLEN tlen;
1288 STRLEN rlen;
1289 register char *t = SvPV(tstr, tlen);
1290 register char *r = SvPV(rstr, rlen);
79072805
LW
1291 register I32 i;
1292 register I32 j;
1293 I32 squash;
1294 I32 delete;
1295 I32 complement;
1296 register short *tbl;
1297
1298 tbl = (short*)cPVOP->op_pv;
1299 complement = op->op_private & OPpTRANS_COMPLEMENT;
1300 delete = op->op_private & OPpTRANS_DELETE;
1301 squash = op->op_private & OPpTRANS_SQUASH;
1302
1303 if (complement) {
1304 Zero(tbl, 256, short);
1305 for (i = 0; i < tlen; i++)
1306 tbl[t[i] & 0377] = -1;
1307 for (i = 0, j = 0; i < 256; i++) {
1308 if (!tbl[i]) {
1309 if (j >= rlen) {
1310 if (delete)
1311 tbl[i] = -2;
1312 else if (rlen)
1313 tbl[i] = r[j-1] & 0377;
1314 else
1315 tbl[i] = i;
1316 }
1317 else
1318 tbl[i] = r[j++] & 0377;
1319 }
1320 }
1321 }
1322 else {
1323 if (!rlen && !delete) {
1324 r = t; rlen = tlen;
1325 }
1326 for (i = 0; i < 256; i++)
1327 tbl[i] = -1;
1328 for (i = 0, j = 0; i < tlen; i++,j++) {
1329 if (j >= rlen) {
1330 if (delete) {
1331 if (tbl[t[i] & 0377] == -1)
1332 tbl[t[i] & 0377] = -2;
1333 continue;
1334 }
1335 --j;
1336 }
1337 if (tbl[t[i] & 0377] == -1)
1338 tbl[t[i] & 0377] = r[j] & 0377;
1339 }
1340 }
1341 op_free(expr);
1342 op_free(repl);
1343
1344 return op;
1345}
1346
1347OP *
1348newPMOP(type, flags)
1349I32 type;
1350I32 flags;
1351{
1352 PMOP *pmop;
1353
1354 Newz(1101, pmop, 1, PMOP);
1355 pmop->op_type = type;
1356 pmop->op_ppaddr = ppaddr[type];
1357 pmop->op_flags = flags;
1358 pmop->op_private = 0;
1359
1360 /* link into pm list */
1361 if (type != OP_TRANS) {
1362 pmop->op_pmnext = HvPMROOT(curstash);
1363 HvPMROOT(curstash) = pmop;
1364 }
1365
1366 return (OP*)pmop;
1367}
1368
1369OP *
1370pmruntime(op, expr, repl)
1371OP *op;
1372OP *expr;
1373OP *repl;
1374{
1375 PMOP *pm;
1376 LOGOP *rcop;
1377
1378 if (op->op_type == OP_TRANS)
1379 return pmtrans(op, expr, repl);
1380
1381 pm = (PMOP*)op;
1382
1383 if (expr->op_type == OP_CONST) {
463ee0b2 1384 STRLEN plen;
79072805 1385 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 1386 char *p = SvPV(pat, plen);
79072805 1387 if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 1388 sv_setpvn(pat, "\\s+", 3);
463ee0b2 1389 p = SvPV(pat, plen);
79072805
LW
1390 pm->op_pmflags |= PMf_SKIPWHITE;
1391 }
463ee0b2 1392 scan_prefix(pm, p, plen);
79072805
LW
1393 if (pm->op_pmshort && (pm->op_pmflags & PMf_SCANFIRST))
1394 fbm_compile(pm->op_pmshort, pm->op_pmflags & PMf_FOLD);
463ee0b2 1395 pm->op_pmregexp = regcomp(p, p + plen, pm->op_pmflags & PMf_FOLD);
79072805
LW
1396 hoistmust(pm);
1397 op_free(expr);
1398 }
1399 else {
463ee0b2
LW
1400 if (pm->op_pmflags & PMf_KEEP)
1401 expr = newUNOP(OP_REGCMAYBE,0,expr);
1402
79072805
LW
1403 Newz(1101, rcop, 1, LOGOP);
1404 rcop->op_type = OP_REGCOMP;
1405 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1406 rcop->op_first = scalar(expr);
1407 rcop->op_flags |= OPf_KIDS;
1408 rcop->op_private = 1;
1409 rcop->op_other = op;
1410
1411 /* establish postfix order */
463ee0b2
LW
1412 if (pm->op_pmflags & PMf_KEEP) {
1413 LINKLIST(expr);
1414 rcop->op_next = expr;
1415 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1416 }
1417 else {
1418 rcop->op_next = LINKLIST(expr);
1419 expr->op_next = (OP*)rcop;
1420 }
79072805 1421
463ee0b2 1422 prepend_elem(op->op_type, scalar((OP*)rcop), op);
79072805
LW
1423 }
1424
1425 if (repl) {
1426 if (repl->op_type == OP_CONST) {
1427 pm->op_pmflags |= PMf_CONST;
1428 prepend_elem(op->op_type, scalar(repl), op);
1429 }
1430 else {
1431 OP *curop;
1432 OP *lastop = 0;
1433 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1434 if (opargs[curop->op_type] & OA_DANGEROUS) {
1435 if (curop->op_type == OP_GV) {
1436 GV *gv = ((GVOP*)curop)->op_gv;
93a17b20 1437 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
1438 break;
1439 }
1440 else if (curop->op_type == OP_RV2CV)
1441 break;
1442 else if (curop->op_type == OP_RV2SV ||
1443 curop->op_type == OP_RV2AV ||
1444 curop->op_type == OP_RV2HV ||
1445 curop->op_type == OP_RV2GV) {
1446 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1447 break;
1448 }
1449 else
1450 break;
1451 }
1452 lastop = curop;
1453 }
1454 if (curop == repl) {
1455 pm->op_pmflags |= PMf_CONST; /* const for long enough */
1456 prepend_elem(op->op_type, scalar(repl), op);
1457 }
1458 else {
1459 Newz(1101, rcop, 1, LOGOP);
1460 rcop->op_type = OP_SUBSTCONT;
1461 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1462 rcop->op_first = scalar(repl);
1463 rcop->op_flags |= OPf_KIDS;
1464 rcop->op_private = 1;
1465 rcop->op_other = op;
1466
1467 /* establish postfix order */
1468 rcop->op_next = LINKLIST(repl);
1469 repl->op_next = (OP*)rcop;
1470
463ee0b2 1471 pm->op_pmreplroot = scalar((OP*)rcop);
79072805
LW
1472 pm->op_pmreplstart = LINKLIST(rcop);
1473 rcop->op_next = 0;
1474 }
1475 }
1476 }
1477
1478 return (OP*)pm;
1479}
1480
1481OP *
1482newSVOP(type, flags, sv)
1483I32 type;
1484I32 flags;
1485SV *sv;
1486{
1487 SVOP *svop;
1488 Newz(1101, svop, 1, SVOP);
1489 svop->op_type = type;
1490 svop->op_ppaddr = ppaddr[type];
1491 svop->op_sv = sv;
1492 svop->op_next = (OP*)svop;
1493 svop->op_flags = flags;
1494 if (opargs[type] & OA_RETSCALAR)
463ee0b2 1495 scalar((OP*)svop);
79072805 1496 if (opargs[type] & OA_TARGET)
ed6116ce 1497 svop->op_targ = pad_alloc(type, SVs_PADTMP);
79072805
LW
1498 return (*check[type])((OP*)svop);
1499}
1500
1501OP *
1502newGVOP(type, flags, gv)
1503I32 type;
1504I32 flags;
1505GV *gv;
1506{
1507 GVOP *gvop;
1508 Newz(1101, gvop, 1, GVOP);
1509 gvop->op_type = type;
1510 gvop->op_ppaddr = ppaddr[type];
1511 gvop->op_gv = (GV*)sv_ref(gv);
1512 gvop->op_next = (OP*)gvop;
1513 gvop->op_flags = flags;
1514 if (opargs[type] & OA_RETSCALAR)
463ee0b2 1515 scalar((OP*)gvop);
79072805 1516 if (opargs[type] & OA_TARGET)
ed6116ce 1517 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
79072805
LW
1518 return (*check[type])((OP*)gvop);
1519}
1520
1521OP *
1522newPVOP(type, flags, pv)
1523I32 type;
1524I32 flags;
1525char *pv;
1526{
1527 PVOP *pvop;
1528 Newz(1101, pvop, 1, PVOP);
1529 pvop->op_type = type;
1530 pvop->op_ppaddr = ppaddr[type];
1531 pvop->op_pv = pv;
1532 pvop->op_next = (OP*)pvop;
1533 pvop->op_flags = flags;
1534 if (opargs[type] & OA_RETSCALAR)
463ee0b2 1535 scalar((OP*)pvop);
79072805 1536 if (opargs[type] & OA_TARGET)
ed6116ce 1537 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
79072805
LW
1538 return (*check[type])((OP*)pvop);
1539}
1540
1541OP *
1542newCVOP(type, flags, cv, cont)
1543I32 type;
1544I32 flags;
1545CV *cv;
1546OP *cont;
1547{
1548 CVOP *cvop;
1549 Newz(1101, cvop, 1, CVOP);
1550 cvop->op_type = type;
1551 cvop->op_ppaddr = ppaddr[type];
1552 cvop->op_cv = cv;
1553 cvop->op_cont = cont;
1554 cvop->op_next = (OP*)cvop;
1555 cvop->op_flags = flags;
1556 if (opargs[type] & OA_RETSCALAR)
463ee0b2 1557 scalar((OP*)cvop);
79072805 1558 if (opargs[type] & OA_TARGET)
ed6116ce 1559 cvop->op_targ = pad_alloc(type, SVs_PADTMP);
79072805
LW
1560 return (*check[type])((OP*)cvop);
1561}
1562
1563void
1564package(op)
1565OP *op;
1566{
93a17b20 1567 SV *sv;
79072805
LW
1568
1569 save_hptr(&curstash);
1570 save_item(curstname);
93a17b20 1571 if (op) {
463ee0b2
LW
1572 STRLEN len;
1573 char *name;
93a17b20 1574 sv = cSVOP->op_sv;
463ee0b2
LW
1575 curstash = fetch_stash(sv,TRUE);
1576 name = SvPV(sv, len);
1577 sv_setpvn(curstname, name, len);
93a17b20
LW
1578 op_free(op);
1579 }
1580 else {
1581 sv_setpv(curstname,"<none>");
1582 curstash = Nullhv;
1583 }
79072805
LW
1584 copline = NOLINE;
1585 expect = XBLOCK;
1586}
1587
463ee0b2
LW
1588HV*
1589fetch_stash(sv,create)
1590SV *sv;
1591I32 create;
1592{
1593 char tmpbuf[256];
1594 HV *stash;
1595 GV *tmpgv;
1596 char *name = SvPV(sv, na);
1597 sprintf(tmpbuf,"%s::",name);
1598 tmpgv = gv_fetchpv(tmpbuf,create);
1599 if (!tmpgv)
1600 return 0;
1601 if (!GvHV(tmpgv))
1602 GvHV(tmpgv) = newHV();
1603 stash = GvHV(tmpgv);
1604 if (!HvNAME(stash))
1605 HvNAME(stash) = savestr(name);
1606 return stash;
1607}
1608
79072805
LW
1609OP *
1610newSLICEOP(flags, subscript, listval)
1611I32 flags;
1612OP *subscript;
1613OP *listval;
1614{
1615 return newBINOP(OP_LSLICE, flags,
1616 list(prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), subscript)),
1617 list(prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), listval)) );
1618}
1619
1620static I32
1621list_assignment(op)
1622register OP *op;
1623{
1624 if (!op)
1625 return TRUE;
1626
1627 if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
1628 op = cUNOP->op_first;
1629
1630 if (op->op_type == OP_COND_EXPR) {
1631 I32 t = list_assignment(cCONDOP->op_first->op_sibling);
1632 I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
1633
1634 if (t && f)
1635 return TRUE;
1636 if (t || f)
1637 yyerror("Assignment to both a list and a scalar");
1638 return FALSE;
1639 }
1640
1641 if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
1642 op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
1643 op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
1644 return TRUE;
1645
93a17b20
LW
1646 if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
1647 return TRUE;
1648
79072805
LW
1649 if (op->op_type == OP_RV2SV)
1650 return FALSE;
1651
1652 return FALSE;
1653}
1654
1655OP *
1656newASSIGNOP(flags, left, right)
1657I32 flags;
1658OP *left;
1659OP *right;
1660{
1661 OP *op;
1662
1663 if (list_assignment(left)) {
463ee0b2
LW
1664 modcount = 0;
1665 left = mod(left, OP_AASSIGN);
79072805
LW
1666 if (right && right->op_type == OP_SPLIT) {
1667 if ((op = ((LISTOP*)right)->op_first) && op->op_type == OP_PUSHRE) {
1668 PMOP *pm = (PMOP*)op;
1669 if (left->op_type == OP_RV2AV) {
1670 op = ((UNOP*)left)->op_first;
1671 if (op->op_type == OP_GV && !pm->op_pmreplroot) {
1672 pm->op_pmreplroot = (OP*)((GVOP*)op)->op_gv;
1673 pm->op_pmflags |= PMf_ONCE;
1674 op_free(left);
1675 return right;
1676 }
1677 }
1678 else {
463ee0b2 1679 if (modcount < 10000) {
79072805 1680 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
463ee0b2
LW
1681 if (SvIVX(sv) == 0)
1682 sv_setiv(sv, modcount+1);
79072805
LW
1683 }
1684 }
1685 }
1686 }
1687 op = newBINOP(OP_AASSIGN, flags,
1688 list(prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), right)),
1689 list(prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), left)) );
1690 op->op_private = 0;
93a17b20 1691 if (!(left->op_flags & OPf_INTRO)) {
79072805
LW
1692 static int generation = 0;
1693 OP *curop;
1694 OP *lastop = op;
1695 generation++;
1696 for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
1697 if (opargs[curop->op_type] & OA_DANGEROUS) {
1698 if (curop->op_type == OP_GV) {
1699 GV *gv = ((GVOP*)curop)->op_gv;
1700 if (gv == defgv || SvCUR(gv) == generation)
1701 break;
1702 SvCUR(gv) = generation;
1703 }
1704 else if (curop->op_type == OP_RV2CV)
1705 break;
1706 else if (curop->op_type == OP_RV2SV ||
1707 curop->op_type == OP_RV2AV ||
1708 curop->op_type == OP_RV2HV ||
1709 curop->op_type == OP_RV2GV) {
1710 if (lastop->op_type != OP_GV) /* funny deref? */
1711 break;
1712 }
1713 else
1714 break;
1715 }
1716 lastop = curop;
1717 }
1718 if (curop != op)
1719 op->op_private = OPpASSIGN_COMMON;
1720 }
ed6116ce 1721 op->op_targ = pad_alloc(OP_AASSIGN, SVs_PADTMP); /* for scalar context */
79072805
LW
1722 return op;
1723 }
1724 if (!right)
1725 right = newOP(OP_UNDEF, 0);
1726 if (right->op_type == OP_READLINE) {
1727 right->op_flags |= OPf_STACKED;
463ee0b2 1728 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805
LW
1729 }
1730 else
1731 op = newBINOP(OP_SASSIGN, flags,
463ee0b2 1732 scalar(right), mod(scalar(left), OP_SASSIGN) );
79072805
LW
1733 return op;
1734}
1735
1736OP *
1737newSTATEOP(flags, label, op)
1738I32 flags;
1739char *label;
1740OP *op;
1741{
1742 register COP *cop;
1743
93a17b20
LW
1744 comppadnamefill = AvFILL(comppadname); /* introduce my variables */
1745
79072805 1746 Newz(1101, cop, 1, COP);
93a17b20
LW
1747 cop->op_type = OP_NEXTSTATE;
1748 cop->op_ppaddr = ppaddr[ perldb ? OP_DBSTATE : OP_NEXTSTATE ];
79072805
LW
1749 cop->op_flags = flags;
1750 cop->op_private = 0;
1751 cop->op_next = (OP*)cop;
1752
463ee0b2
LW
1753 if (label) {
1754 cop->cop_label = label;
1755 needblockscope = TRUE;
1756 }
1757 cop->cop_seq = cop_seqmax++;
79072805
LW
1758
1759 if (copline == NOLINE)
1760 cop->cop_line = curcop->cop_line;
1761 else {
1762 cop->cop_line = copline;
1763 copline = NOLINE;
1764 }
1765 cop->cop_filegv = curcop->cop_filegv;
1766 cop->cop_stash = curstash;
1767
93a17b20
LW
1768 if (perldb) {
1769 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
1770 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
463ee0b2 1771 SvIVX(*svp) = 1;
93a17b20
LW
1772 SvIOK_on(*svp);
1773 SvSTASH(*svp) = (HV*)cop;
1774 }
1775 }
1776
79072805
LW
1777 return prepend_elem(OP_LINESEQ, (OP*)cop, op);
1778}
1779
1780OP *
1781newLOGOP(type, flags, first, other)
1782I32 type;
1783I32 flags;
1784OP* first;
1785OP* other;
1786{
1787 LOGOP *logop;
1788 OP *op;
1789
1790 scalar(first);
1791 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
1792 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
1793 if (type == OP_AND || type == OP_OR) {
1794 if (type == OP_AND)
1795 type = OP_OR;
1796 else
1797 type = OP_AND;
1798 op = first;
1799 first = cUNOP->op_first;
1800 if (op->op_next)
1801 first->op_next = op->op_next;
1802 cUNOP->op_first = Nullop;
1803 op_free(op);
1804 }
1805 }
1806 if (first->op_type == OP_CONST) {
93a17b20
LW
1807 if (dowarn && (first->op_private & OPpCONST_BARE))
1808 warn("Probable precedence problem on %s", op_name[type]);
79072805
LW
1809 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
1810 op_free(first);
1811 return other;
1812 }
1813 else {
1814 op_free(other);
1815 return first;
1816 }
1817 }
1818 else if (first->op_type == OP_WANTARRAY) {
1819 if (type == OP_AND)
1820 list(other);
1821 else
1822 scalar(other);
1823 }
1824
1825 if (!other)
1826 return first;
1827
1828 Newz(1101, logop, 1, LOGOP);
1829
1830 logop->op_type = type;
1831 logop->op_ppaddr = ppaddr[type];
1832 logop->op_first = first;
1833 logop->op_flags = flags | OPf_KIDS;
1834 logop->op_other = LINKLIST(other);
1835 logop->op_private = 1;
1836
1837 /* establish postfix order */
1838 logop->op_next = LINKLIST(first);
1839 first->op_next = (OP*)logop;
1840 first->op_sibling = other;
1841
1842 op = newUNOP(OP_NULL, 0, (OP*)logop);
1843 other->op_next = op;
1844
1845 return op;
1846}
1847
1848OP *
1849newCONDOP(flags, first, true, false)
1850I32 flags;
1851OP* first;
1852OP* true;
1853OP* false;
1854{
1855 CONDOP *condop;
1856 OP *op;
1857
1858 if (!false)
1859 return newLOGOP(OP_AND, 0, first, true);
463ee0b2
LW
1860 if (!true)
1861 return newLOGOP(OP_OR, 0, first, false);
79072805
LW
1862
1863 scalar(first);
1864 if (first->op_type == OP_CONST) {
1865 if (SvTRUE(((SVOP*)first)->op_sv)) {
1866 op_free(first);
1867 op_free(false);
1868 return true;
1869 }
1870 else {
1871 op_free(first);
1872 op_free(true);
1873 return false;
1874 }
1875 }
1876 else if (first->op_type == OP_WANTARRAY) {
1877 list(true);
1878 scalar(false);
1879 }
1880 Newz(1101, condop, 1, CONDOP);
1881
1882 condop->op_type = OP_COND_EXPR;
1883 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
1884 condop->op_first = first;
1885 condop->op_flags = flags | OPf_KIDS;
1886 condop->op_true = LINKLIST(true);
1887 condop->op_false = LINKLIST(false);
1888 condop->op_private = 1;
1889
1890 /* establish postfix order */
1891 condop->op_next = LINKLIST(first);
1892 first->op_next = (OP*)condop;
1893
1894 first->op_sibling = true;
1895 true->op_sibling = false;
1896 op = newUNOP(OP_NULL, 0, (OP*)condop);
1897
1898 true->op_next = op;
1899 false->op_next = op;
1900
1901 return op;
1902}
1903
1904OP *
1905newRANGE(flags, left, right)
1906I32 flags;
1907OP *left;
1908OP *right;
1909{
1910 CONDOP *condop;
1911 OP *flip;
1912 OP *flop;
1913 OP *op;
1914
1915 Newz(1101, condop, 1, CONDOP);
1916
1917 condop->op_type = OP_RANGE;
1918 condop->op_ppaddr = ppaddr[OP_RANGE];
1919 condop->op_first = left;
1920 condop->op_flags = OPf_KIDS;
1921 condop->op_true = LINKLIST(left);
1922 condop->op_false = LINKLIST(right);
1923 condop->op_private = 1;
1924
1925 left->op_sibling = right;
1926
1927 condop->op_next = (OP*)condop;
1928 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
1929 flop = newUNOP(OP_FLOP, 0, flip);
1930 op = newUNOP(OP_NULL, 0, flop);
1931 linklist(flop);
1932
1933 left->op_next = flip;
1934 right->op_next = flop;
1935
ed6116ce 1936 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805 1937 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
ed6116ce 1938 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805
LW
1939 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
1940
1941 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
1942 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
1943
1944 flip->op_next = op;
1945 if (!flip->op_private || !flop->op_private)
1946 linklist(op); /* blow off optimizer unless constant */
1947
1948 return op;
1949}
1950
1951OP *
1952newLOOPOP(flags, debuggable, expr, block)
1953I32 flags;
1954I32 debuggable;
1955OP *expr;
1956OP *block;
1957{
463ee0b2 1958 OP* listop;
93a17b20 1959 OP* op;
463ee0b2
LW
1960 int once = block && block->op_flags & OPf_SPECIAL &&
1961 (block->op_type == OP_ENTERSUBR || block->op_type == OP_NULL);
93a17b20 1962
463ee0b2
LW
1963 if (expr) {
1964 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
1965 return block; /* do {} while 0 does once */
1966 else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
1967 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), expr);
1968 }
93a17b20 1969
ed6116ce 1970 listop = append_elem(OP_LINESEQ, guess_mark(block), newOP(OP_UNSTACK, 0));
93a17b20 1971 op = newLOGOP(OP_AND, 0, expr, listop);
463ee0b2 1972
79072805
LW
1973 ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
1974
463ee0b2 1975 if (once && op != listop)
79072805
LW
1976 op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
1977
1978 op->op_flags |= flags;
463ee0b2 1979 return scope(op);
79072805
LW
1980}
1981
1982OP *
1983newWHILEOP(flags, debuggable, loop, expr, block, cont)
1984I32 flags;
1985I32 debuggable;
1986LOOP *loop;
1987OP *expr;
1988OP *block;
1989OP *cont;
1990{
1991 OP *redo;
1992 OP *next = 0;
1993 OP *listop;
1994 OP *op;
1995 OP *condop;
1996
1997 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB))
1998 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), expr);
1999
2000 if (!block)
2001 block = newOP(OP_NULL, 0);
2002
2003 if (cont)
2004 next = LINKLIST(cont);
2005 if (expr)
2006 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2007
463ee0b2 2008 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
79072805
LW
2009 redo = LINKLIST(listop);
2010
2011 if (expr) {
2012 op = newLOGOP(OP_AND, 0, expr, scalar(listop));
463ee0b2
LW
2013 if (op == expr) { /* oops, it's a while (0) */
2014 op_free(expr);
2015 op_free((OP*)loop);
2016 return Nullop; /* (listop already freed by newLOGOP) */
2017 }
79072805
LW
2018 ((LISTOP*)listop)->op_last->op_next = condop =
2019 (op == listop ? redo : LINKLIST(op));
2020 if (!next)
2021 next = condop;
2022 }
2023 else
2024 op = listop;
2025
2026 if (!loop) {
2027 Newz(1101,loop,1,LOOP);
2028 loop->op_type = OP_ENTERLOOP;
2029 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2030 loop->op_private = 0;
2031 loop->op_next = (OP*)loop;
2032 }
2033
463ee0b2 2034 op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
79072805
LW
2035
2036 loop->op_redoop = redo;
2037 loop->op_lastop = op;
2038
2039 if (next)
2040 loop->op_nextop = next;
2041 else
2042 loop->op_nextop = op;
2043
2044 op->op_flags |= flags;
2045 return op;
2046}
2047
2048OP *
2049newFOROP(flags,label,forline,sv,expr,block,cont)
2050I32 flags;
2051char *label;
2052line_t forline;
2053OP* sv;
2054OP* expr;
2055OP*block;
2056OP*cont;
2057{
2058 LOOP *loop;
2059
2060 copline = forline;
2061 if (sv) {
2062 if (sv->op_type == OP_RV2SV) {
2063 OP *op = sv;
2064 sv = cUNOP->op_first;
2065 sv->op_next = sv;
2066 cUNOP->op_first = Nullop;
2067 op_free(op);
2068 }
2069 else
463ee0b2 2070 croak("Can't use %s for loop variable", op_name[sv->op_type]);
79072805
LW
2071 }
2072 else {
2073 sv = newGVOP(OP_GV, 0, defgv);
2074 }
2075 loop = (LOOP*)list(convert(OP_ENTERITER, 0,
2076 append_elem(OP_LIST,
2077 prepend_elem(OP_LIST, newOP(OP_PUSHMARK, 0), expr),
2078 scalar(sv))));
2079 return newSTATEOP(0, label, newWHILEOP(flags, 1,
93a17b20 2080 loop, newOP(OP_ITER, 0), block, cont));
79072805
LW
2081}
2082
2083void
463ee0b2 2084cv_clear(cv)
79072805
LW
2085CV *cv;
2086{
2087 if (!CvUSERSUB(cv) && CvROOT(cv)) {
2088 op_free(CvROOT(cv));
2089 CvROOT(cv) = Nullop;
2090 if (CvDEPTH(cv))
2091 warn("Deleting active subroutine"); /* XXX */
2092 if (CvPADLIST(cv)) {
2093 I32 i = AvFILL(CvPADLIST(cv));
2094 while (i > 0) {
2095 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2096 if (svp)
463ee0b2 2097 av_free((AV*)*svp);
79072805 2098 }
463ee0b2 2099 av_free((AV*)CvPADLIST(cv));
79072805
LW
2100 }
2101 }
79072805
LW
2102}
2103
2104void
2105newSUB(floor,op,block)
2106I32 floor;
2107OP *op;
2108OP *block;
2109{
2110 register CV *cv;
463ee0b2
LW
2111 char *name = SvPVx(cSVOP->op_sv, na);
2112 GV *gv = gv_fetchpv(name,2);
79072805
LW
2113 AV* av;
2114
463ee0b2
LW
2115 sub_generation++;
2116 if ((cv = GvCV(gv)) && !GvCVGEN(gv)) {
79072805
LW
2117 if (CvDEPTH(cv))
2118 CvDELETED(cv) = TRUE; /* probably an autoloader */
2119 else {
93a17b20 2120 if (dowarn && CvROOT(cv)) {
79072805
LW
2121 line_t oldline = curcop->cop_line;
2122
2123 curcop->cop_line = copline;
2124 warn("Subroutine %s redefined",name);
2125 curcop->cop_line = oldline;
2126 }
463ee0b2 2127 sv_free((SV*)cv);
79072805
LW
2128 }
2129 }
2130 Newz(101,cv,1,CV);
2131 sv_upgrade(cv, SVt_PVCV);
463ee0b2 2132 SvREFCNT(cv) = 1;
79072805 2133 GvCV(gv) = cv;
463ee0b2 2134 GvCVGEN(gv) = 0;
79072805
LW
2135 CvFILEGV(cv) = curcop->cop_filegv;
2136
2137 av = newAV();
2138 AvREAL_off(av);
93a17b20
LW
2139 if (AvFILL(comppadname) < AvFILL(comppad))
2140 av_store(comppadname, AvFILL(comppad), Nullsv);
2141 av_store(av, 0, (SV*)comppadname);
79072805
LW
2142 av_store(av, 1, (SV*)comppad);
2143 AvFILL(av) = 1;
2144 CvPADLIST(cv) = av;
93a17b20 2145 comppadname = newAV();
79072805 2146
93a17b20
LW
2147 if (!block) {
2148 CvROOT(cv) = 0;
2149 op_free(op);
2150 copline = NOLINE;
2151 leave_scope(floor);
2152 return;
2153 }
79072805
LW
2154 CvROOT(cv) = newUNOP(OP_LEAVESUBR, 0, scalarseq(block));
2155 CvSTART(cv) = LINKLIST(CvROOT(cv));
2156 CvROOT(cv)->op_next = 0;
93a17b20 2157 CvSTASH(cv) = curstash;
79072805
LW
2158 peep(CvSTART(cv));
2159 CvDELETED(cv) = FALSE;
93a17b20
LW
2160 if (strEQ(name, "BEGIN")) {
2161 line_t oldline = curcop->cop_line;
2162 GV* oldfile = curcop->cop_filegv;
2163
2164 if (!beginav)
2165 beginav = newAV();
2166 av_push(beginav, sv_ref(gv));
2167 DEBUG_x( dump_sub(gv) );
2168 rs = nrs;
2169 rslen = nrslen;
2170 rschar = nrschar;
2171 rspara = (nrslen == 2);
2172 calllist(beginav);
463ee0b2 2173 sv_free((SV*)cv);
93a17b20
LW
2174 rs = "\n";
2175 rslen = 1;
2176 rschar = '\n';
2177 rspara = 0;
2178 GvCV(gv) = 0;
2179 curcop = &compiling;
2180 curcop->cop_line = oldline; /* might have compiled something */
2181 curcop->cop_filegv = oldfile; /* recursively, clobbering these */
2182 }
2183 else if (strEQ(name, "END")) {
2184 if (!endav)
2185 endav = newAV();
2186 av_unshift(endav, 1);
2187 av_store(endav, 0, sv_ref(gv));
2188 }
79072805
LW
2189 if (perldb) {
2190 SV *sv;
2191 SV *tmpstr = sv_mortalcopy(&sv_undef);
2192
463ee0b2 2193 sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), subline);
79072805
LW
2194 sv = newSVpv(buf,0);
2195 sv_catpv(sv,"-");
2196 sprintf(buf,"%ld",(long)curcop->cop_line);
2197 sv_catpv(sv,buf);
2198 gv_efullname(tmpstr,gv);
463ee0b2 2199 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
79072805
LW
2200 }
2201 op_free(op);
2202 copline = NOLINE;
2203 leave_scope(floor);
2204}
2205
2206void
463ee0b2 2207newXSUB(name, ix, subaddr, filename)
79072805
LW
2208char *name;
2209I32 ix;
2210I32 (*subaddr)();
2211char *filename;
2212{
2213 register CV *cv;
463ee0b2 2214 GV *gv = gv_fetchpv(name,2);
79072805 2215
463ee0b2
LW
2216 sub_generation++;
2217 if ((cv = GvCV(gv)) && !GvCVGEN(gv)) {
79072805
LW
2218 if (dowarn)
2219 warn("Subroutine %s redefined",name);
2220 if (!CvUSERSUB(cv) && CvROOT(cv)) {
2221 op_free(CvROOT(cv));
2222 CvROOT(cv) = Nullop;
2223 }
2224 Safefree(cv);
2225 }
2226 Newz(101,cv,1,CV);
2227 sv_upgrade(cv, SVt_PVCV);
463ee0b2 2228 SvREFCNT(cv) = 1;
79072805 2229 GvCV(gv) = cv;
463ee0b2 2230 GvCVGEN(gv) = 0;
79072805
LW
2231 CvFILEGV(cv) = gv_fetchfile(filename);
2232 CvUSERSUB(cv) = subaddr;
2233 CvUSERINDEX(cv) = ix;
2234 CvDELETED(cv) = FALSE;
93a17b20
LW
2235 if (strEQ(name, "BEGIN")) {
2236 if (!beginav)
2237 beginav = newAV();
2238 av_push(beginav, sv_ref(gv));
2239 }
2240 else if (strEQ(name, "END")) {
2241 if (!endav)
2242 endav = newAV();
2243 av_unshift(endav, 1);
2244 av_store(endav, 0, sv_ref(gv));
2245 }
79072805
LW
2246}
2247
2248void
2249newFORM(floor,op,block)
2250I32 floor;
2251OP *op;
2252OP *block;
2253{
2254 register CV *cv;
2255 char *name;
2256 GV *gv;
2257 AV* av;
2258
2259 if (op)
463ee0b2 2260 name = SvPVx(cSVOP->op_sv, na);
79072805
LW
2261 else
2262 name = "STDOUT";
2263 gv = gv_fetchpv(name,TRUE);
2264 if (cv = GvFORM(gv)) {
2265 if (dowarn) {
2266 line_t oldline = curcop->cop_line;
2267
2268 curcop->cop_line = copline;
2269 warn("Format %s redefined",name);
2270 curcop->cop_line = oldline;
2271 }
463ee0b2 2272 sv_free((SV*)cv);
79072805
LW
2273 }
2274 Newz(101,cv,1,CV);
2275 sv_upgrade(cv, SVt_PVFM);
463ee0b2 2276 SvREFCNT(cv) = 1;
79072805
LW
2277 GvFORM(gv) = cv;
2278 CvFILEGV(cv) = curcop->cop_filegv;
2279
2280 CvPADLIST(cv) = av = newAV();
2281 AvREAL_off(av);
2282 av_store(av, 1, (SV*)comppad);
2283 AvFILL(av) = 1;
2284
2285 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
2286 CvSTART(cv) = LINKLIST(CvROOT(cv));
2287 CvROOT(cv)->op_next = 0;
2288 peep(CvSTART(cv));
2289 CvDELETED(cv) = FALSE;
2290 FmLINES(cv) = 0;
2291 op_free(op);
2292 copline = NOLINE;
2293 leave_scope(floor);
2294}
2295
2296OP *
2297newMETHOD(ref,name)
2298OP *ref;
2299OP *name;
2300{
2301 LOGOP* mop;
2302 Newz(1101, mop, 1, LOGOP);
2303 mop->op_type = OP_METHOD;
2304 mop->op_ppaddr = ppaddr[OP_METHOD];
2305 mop->op_first = scalar(ref);
2306 mop->op_flags |= OPf_KIDS;
2307 mop->op_private = 1;
2308 mop->op_other = LINKLIST(name);
ed6116ce 2309 mop->op_targ = pad_alloc(OP_METHOD, SVs_PADTMP);
79072805
LW
2310 mop->op_next = LINKLIST(ref);
2311 ref->op_next = (OP*)mop;
2312 return (OP*)mop;
2313}
2314
2315OP *
2316newANONLIST(op)
2317OP* op;
2318{
93a17b20
LW
2319 return newUNOP(OP_REFGEN, 0,
2320 ref(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
79072805
LW
2321}
2322
2323OP *
2324newANONHASH(op)
2325OP* op;
2326{
93a17b20
LW
2327 return newUNOP(OP_REFGEN, 0,
2328 ref(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
79072805
LW
2329}
2330
2331OP *
2332oopsAV(o)
2333OP *o;
2334{
ed6116ce
LW
2335 switch (o->op_type) {
2336 case OP_PADSV:
2337 o->op_type = OP_PADAV;
2338 o->op_ppaddr = ppaddr[OP_PADAV];
2339 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
2340
2341 case OP_RV2SV:
79072805
LW
2342 o->op_type = OP_RV2AV;
2343 o->op_ppaddr = ppaddr[OP_RV2AV];
2344 ref(o, OP_RV2AV);
ed6116ce
LW
2345 break;
2346
2347 default:
79072805 2348 warn("oops: oopsAV");
ed6116ce
LW
2349 break;
2350 }
79072805
LW
2351 return o;
2352}
2353
2354OP *
2355oopsHV(o)
2356OP *o;
2357{
ed6116ce
LW
2358 switch (o->op_type) {
2359 case OP_PADSV:
2360 case OP_PADAV:
2361 o->op_type = OP_PADHV;
2362 o->op_ppaddr = ppaddr[OP_PADHV];
2363 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
2364
2365 case OP_RV2SV:
2366 case OP_RV2AV:
79072805
LW
2367 o->op_type = OP_RV2HV;
2368 o->op_ppaddr = ppaddr[OP_RV2HV];
2369 ref(o, OP_RV2HV);
ed6116ce
LW
2370 break;
2371
2372 default:
79072805 2373 warn("oops: oopsHV");
ed6116ce
LW
2374 break;
2375 }
79072805
LW
2376 return o;
2377}
2378
2379OP *
2380newAVREF(o)
2381OP *o;
2382{
ed6116ce
LW
2383 if (o->op_type == OP_PADANY) {
2384 o->op_type = OP_PADAV;
2385 o->op_ppaddr = ppaddr[OP_PADAV];
93a17b20 2386 return o;
ed6116ce 2387 }
79072805
LW
2388 return newUNOP(OP_RV2AV, 0, scalar(o));
2389}
2390
2391OP *
2392newGVREF(o)
2393OP *o;
2394{
2395 return newUNOP(OP_RV2GV, 0, scalar(o));
2396}
2397
2398OP *
2399newHVREF(o)
2400OP *o;
2401{
ed6116ce
LW
2402 if (o->op_type == OP_PADANY) {
2403 o->op_type = OP_PADHV;
2404 o->op_ppaddr = ppaddr[OP_PADHV];
93a17b20 2405 return o;
ed6116ce 2406 }
79072805
LW
2407 return newUNOP(OP_RV2HV, 0, scalar(o));
2408}
2409
2410OP *
2411oopsCV(o)
2412OP *o;
2413{
463ee0b2 2414 croak("NOT IMPL LINE %d",__LINE__);
79072805
LW
2415 /* STUB */
2416 return o;
2417}
2418
2419OP *
2420newCVREF(o)
2421OP *o;
2422{
2423 return newUNOP(OP_RV2CV, 0, scalar(o));
2424}
2425
2426OP *
2427newSVREF(o)
2428OP *o;
2429{
ed6116ce
LW
2430 if (o->op_type == OP_PADANY) {
2431 o->op_type = OP_PADSV;
2432 o->op_ppaddr = ppaddr[OP_PADSV];
93a17b20 2433 return o;
ed6116ce 2434 }
79072805
LW
2435 return newUNOP(OP_RV2SV, 0, scalar(o));
2436}
2437
2438/* Check routines. */
2439
2440OP *
2441ck_aelem(op)
2442OP *op;
2443{
2444 /* XXX need to optimize constant subscript here. */
2445 return op;
2446}
2447
2448OP *
2449ck_concat(op)
2450OP *op;
2451{
2452 if (cUNOP->op_first->op_type == OP_CONCAT)
2453 op->op_flags |= OPf_STACKED;
2454 return op;
2455}
2456
2457OP *
2458ck_chop(op)
2459OP *op;
2460{
2461 if (op->op_flags & OPf_KIDS) {
2462 OP* newop;
463ee0b2 2463 op = modkids(ck_fun(op), op->op_type);
79072805
LW
2464 if (op->op_private != 1)
2465 return op;
2466 newop = cUNOP->op_first->op_sibling;
2467 if (!newop || newop->op_type != OP_RV2SV)
2468 return op;
2469 op_free(cUNOP->op_first);
2470 cUNOP->op_first = newop;
2471 }
2472 op->op_type = OP_SCHOP;
2473 op->op_ppaddr = ppaddr[OP_SCHOP];
2474 return op;
2475}
2476
2477OP *
2478ck_eof(op)
2479OP *op;
2480{
2481 I32 type = op->op_type;
2482
2483 if (op->op_flags & OPf_KIDS)
2484 return ck_fun(op);
2485
2486 if (op->op_flags & OPf_SPECIAL) {
2487 op_free(op);
2488 op = newUNOP(type, 0, newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE)));
2489 }
2490 return op;
2491}
2492
2493OP *
2494ck_eval(op)
2495OP *op;
2496{
463ee0b2 2497 needblockscope = TRUE;
79072805
LW
2498 if (op->op_flags & OPf_KIDS) {
2499 SVOP *kid = (SVOP*)cUNOP->op_first;
2500
93a17b20
LW
2501 if (!kid) {
2502 op->op_flags &= ~OPf_KIDS;
2503 op->op_type = OP_NULL;
2504 op->op_ppaddr = ppaddr[OP_NULL];
79072805
LW
2505 }
2506 else if (kid->op_type == OP_LINESEQ) {
2507 LOGOP *enter;
2508
2509 kid->op_next = op->op_next;
2510 cUNOP->op_first = 0;
2511 op_free(op);
2512
2513 Newz(1101, enter, 1, LOGOP);
2514 enter->op_type = OP_ENTERTRY;
2515 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
2516 enter->op_private = 0;
2517
2518 /* establish postfix order */
2519 enter->op_next = (OP*)enter;
2520
463ee0b2 2521 op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
79072805
LW
2522 op->op_type = OP_LEAVETRY;
2523 op->op_ppaddr = ppaddr[OP_LEAVETRY];
2524 enter->op_other = op;
2525 return op;
2526 }
2527 }
2528 else {
2529 op_free(op);
2530 op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
2531 }
2532 return op;
2533}
2534
2535OP *
2536ck_exec(op)
2537OP *op;
2538{
2539 OP *kid;
79072805 2540 if (op->op_flags & OPf_STACKED) {
463ee0b2 2541 op = ck_fun(op);
79072805
LW
2542 kid = cUNOP->op_first->op_sibling;
2543 if (kid->op_type == OP_RV2GV) {
2544 kid->op_type = OP_NULL;
2545 kid->op_ppaddr = ppaddr[OP_NULL];
2546 }
2547 }
463ee0b2
LW
2548 else
2549 op = listkids(op);
79072805
LW
2550 return op;
2551}
2552
2553OP *
2554ck_gvconst(o)
2555register OP *o;
2556{
2557 o = fold_constants(o);
2558 if (o->op_type == OP_CONST)
2559 o->op_type = OP_GV;
2560 return o;
2561}
2562
2563OP *
2564ck_rvconst(op)
2565register OP *op;
2566{
2567 SVOP *kid = (SVOP*)cUNOP->op_first;
2568 if (kid->op_type == OP_CONST) {
2569 kid->op_type = OP_GV;
463ee0b2
LW
2570 kid->op_sv = sv_ref((SV*)gv_fetchpv(SvPVx(kid->op_sv, na),
2571 1+(op->op_type==OP_RV2CV)));
79072805
LW
2572 }
2573 return op;
2574}
2575
2576OP *
2577ck_formline(op)
2578OP *op;
2579{
2580 return ck_fun(op);
2581}
2582
2583OP *
2584ck_ftst(op)
2585OP *op;
2586{
2587 I32 type = op->op_type;
2588
2589 if (op->op_flags & OPf_SPECIAL)
2590 return op;
2591
2592 if (op->op_flags & OPf_KIDS) {
2593 SVOP *kid = (SVOP*)cUNOP->op_first;
2594
2595 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
2596 OP *newop = newGVOP(type, OPf_SPECIAL,
463ee0b2 2597 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE));
79072805
LW
2598 op_free(op);
2599 return newop;
2600 }
2601 }
2602 else {
2603 op_free(op);
2604 if (type == OP_FTTTY)
2605 return newGVOP(type, OPf_SPECIAL, gv_fetchpv("main'STDIN", TRUE));
2606 else
2607 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
2608 }
2609 return op;
2610}
2611
2612OP *
2613ck_fun(op)
2614OP *op;
2615{
2616 register OP *kid;
2617 OP **tokid;
2618 OP *sibl;
2619 I32 numargs = 0;
2620 register I32 oa = opargs[op->op_type] >> 8;
2621
2622 if (op->op_flags & OPf_STACKED) {
2623 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
2624 oa &= ~OA_OPTIONAL;
2625 else
2626 return no_fh_allowed(op);
2627 }
2628
2629 if (op->op_flags & OPf_KIDS) {
2630 tokid = &cLISTOP->op_first;
2631 kid = cLISTOP->op_first;
2632 if (kid->op_type == OP_PUSHMARK) {
2633 tokid = &kid->op_sibling;
2634 kid = kid->op_sibling;
2635 }
2636
2637 while (oa && kid) {
2638 numargs++;
2639 sibl = kid->op_sibling;
2640 switch (oa & 7) {
2641 case OA_SCALAR:
2642 scalar(kid);
2643 break;
2644 case OA_LIST:
2645 if (oa < 16) {
2646 kid = 0;
2647 continue;
2648 }
2649 else
2650 list(kid);
2651 break;
2652 case OA_AVREF:
2653 if (kid->op_type == OP_CONST &&
2654 (kid->op_private & OPpCONST_BARE)) {
463ee0b2 2655 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
79072805 2656 OP *newop = newAVREF(newGVOP(OP_GV, 0,
463ee0b2
LW
2657 gv_fetchpv(name, TRUE) ));
2658 if (dowarn)
2659 warn("Array @%s missing the @ in argument %d of %s()",
2660 name, numargs, op_name[op->op_type]);
79072805
LW
2661 op_free(kid);
2662 kid = newop;
2663 kid->op_sibling = sibl;
2664 *tokid = kid;
2665 }
463ee0b2 2666 mod(kid, op->op_type);
79072805
LW
2667 break;
2668 case OA_HVREF:
2669 if (kid->op_type == OP_CONST &&
2670 (kid->op_private & OPpCONST_BARE)) {
463ee0b2 2671 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
79072805 2672 OP *newop = newHVREF(newGVOP(OP_GV, 0,
463ee0b2
LW
2673 gv_fetchpv(name, TRUE) ));
2674 if (dowarn)
2675 warn("Hash %%%s missing the %% in argument %d of %s()",
2676 name, numargs, op_name[op->op_type]);
79072805
LW
2677 op_free(kid);
2678 kid = newop;
2679 kid->op_sibling = sibl;
2680 *tokid = kid;
2681 }
463ee0b2 2682 mod(kid, op->op_type);
79072805
LW
2683 break;
2684 case OA_CVREF:
2685 {
2686 OP *newop = newUNOP(OP_NULL, 0, scalar(kid));
2687 kid->op_sibling = 0;
2688 linklist(kid);
2689 newop->op_next = newop;
2690 kid = newop;
2691 kid->op_sibling = sibl;
2692 *tokid = kid;
2693 }
2694 break;
2695 case OA_FILEREF:
2696 if (kid->op_type != OP_GV) {
2697 if (kid->op_type == OP_CONST &&
2698 (kid->op_private & OPpCONST_BARE)) {
2699 OP *newop = newGVOP(OP_GV, 0,
463ee0b2 2700 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE) );
79072805
LW
2701 op_free(kid);
2702 kid = newop;
2703 }
2704 else {
2705 kid->op_sibling = 0;
2706 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
2707 }
2708 kid->op_sibling = sibl;
2709 *tokid = kid;
2710 }
2711 scalar(kid);
2712 break;
2713 case OA_SCALARREF:
463ee0b2 2714 mod(scalar(kid), op->op_type);
79072805
LW
2715 break;
2716 }
2717 oa >>= 4;
2718 tokid = &kid->op_sibling;
2719 kid = kid->op_sibling;
2720 }
2721 op->op_private = numargs;
2722 if (kid)
2723 return too_many_arguments(op);
2724 listkids(op);
2725 }
2726 if (oa) {
2727 while (oa & OA_OPTIONAL)
2728 oa >>= 4;
2729 if (oa && oa != OA_LIST)
2730 return too_few_arguments(op);
2731 }
2732 return op;
2733}
2734
2735OP *
2736ck_glob(op)
2737OP *op;
2738{
2739 GV *gv = newGVgen();
2740 GvIOn(gv);
2741 append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
2742 scalarkids(op);
2743 return op;
2744}
2745
2746OP *
2747ck_grep(op)
2748OP *op;
2749{
2750 LOGOP *gwop;
2751 OP *kid;
2752
93a17b20
LW
2753 if (op->op_flags & OPf_STACKED) {
2754 op = ck_sort(op);
2755 op->op_flags &= ~OPf_STACKED;
2756 }
79072805
LW
2757 op = ck_fun(op);
2758 if (error_count)
2759 return op;
2760 kid = cLISTOP->op_first->op_sibling;
2761 if (kid->op_type != OP_NULL)
463ee0b2 2762 croak("panic: ck_grep");
79072805
LW
2763 kid = kUNOP->op_first;
2764
2765 Newz(1101, gwop, 1, LOGOP);
2766 gwop->op_type = OP_GREPWHILE;
2767 gwop->op_ppaddr = ppaddr[OP_GREPWHILE];
2768 gwop->op_first = list(op);
2769 gwop->op_flags |= OPf_KIDS;
2770 gwop->op_private = 1;
2771 gwop->op_other = LINKLIST(kid);
ed6116ce 2772 gwop->op_targ = pad_alloc(OP_GREPWHILE, SVs_PADTMP);
79072805
LW
2773 kid->op_next = (OP*)gwop;
2774
2775 return (OP*)gwop;
2776}
2777
2778OP *
2779ck_index(op)
2780OP *op;
2781{
2782 if (op->op_flags & OPf_KIDS) {
2783 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
2784 if (kid && kid->op_type == OP_CONST)
2785 fbm_compile(((SVOP*)kid)->op_sv, 0);
2786 }
2787 return ck_fun(op);
2788}
2789
2790OP *
2791ck_lengthconst(op)
2792OP *op;
2793{
2794 /* XXX length optimization goes here */
2795 return op;
2796}
2797
2798OP *
2799ck_lfun(op)
2800OP *op;
2801{
463ee0b2 2802 return modkids(ck_fun(op), op->op_type);
79072805
LW
2803}
2804
2805OP *
2806ck_listiob(op)
2807OP *op;
2808{
2809 register OP *kid;
2810
2811 kid = cLISTOP->op_first;
2812 if (!kid) {
93a17b20 2813 prepend_elem(op->op_type, newOP(OP_PUSHMARK, 0), op);
79072805
LW
2814 kid = cLISTOP->op_first;
2815 }
2816 if (kid->op_type == OP_PUSHMARK)
2817 kid = kid->op_sibling;
2818 if (kid && op->op_flags & OPf_STACKED)
2819 kid = kid->op_sibling;
2820 else if (kid && !kid->op_sibling) { /* print HANDLE; */
2821 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
2822 op->op_flags |= OPf_STACKED; /* make it a filehandle */
2823 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
2824 cLISTOP->op_first->op_sibling = kid;
2825 cLISTOP->op_last = kid;
2826 kid = kid->op_sibling;
2827 }
2828 }
2829
2830 if (!kid)
2831 append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
2832
2833 return listkids(op);
2834}
2835
2836OP *
2837ck_match(op)
2838OP *op;
2839{
2840 cPMOP->op_pmflags |= PMf_RUNTIME;
2841 return op;
2842}
2843
2844OP *
2845ck_null(op)
2846OP *op;
2847{
2848 return op;
2849}
2850
2851OP *
2852ck_repeat(op)
2853OP *op;
2854{
2855 if (cBINOP->op_first->op_flags & OPf_PARENS) {
2856 op->op_private = OPpREPEAT_DOLIST;
2857 cBINOP->op_first =
2858 prepend_elem(OP_NULL, newOP(OP_PUSHMARK, 0), cBINOP->op_first);
2859 }
2860 else
2861 scalar(op);
2862 return op;
2863}
2864
2865OP *
2866ck_retarget(op)
2867OP *op;
2868{
463ee0b2 2869 croak("NOT IMPL LINE %d",__LINE__);
79072805
LW
2870 /* STUB */
2871 return op;
2872}
2873
2874OP *
2875ck_select(op)
2876OP *op;
2877{
2878 if (op->op_flags & OPf_KIDS) {
2879 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
2880 if (kid) {
2881 op->op_type = OP_SSELECT;
2882 op->op_ppaddr = ppaddr[OP_SSELECT];
2883 op = ck_fun(op);
2884 return fold_constants(op);
2885 }
2886 }
2887 return ck_fun(op);
2888}
2889
2890OP *
2891ck_shift(op)
2892OP *op;
2893{
2894 I32 type = op->op_type;
2895
2896 if (!(op->op_flags & OPf_KIDS)) {
2897 op_free(op);
2898 return newUNOP(type, 0,
2899 scalar(newUNOP(OP_RV2AV, 0,
2900 scalar(newGVOP(OP_GV, 0,
2901 gv_fetchpv((subline ? "_" : "ARGV"), TRUE) )))));
2902 }
463ee0b2 2903 return scalar(modkids(ck_fun(op), type));
79072805
LW
2904}
2905
2906OP *
2907ck_sort(op)
2908OP *op;
2909{
2910 if (op->op_flags & OPf_STACKED) {
2911 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
463ee0b2
LW
2912 OP *k;
2913 kid = kUNOP->op_first; /* get past rv2gv */
79072805 2914
463ee0b2 2915 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
79072805 2916 linklist(kid);
463ee0b2
LW
2917 if (kid->op_type == OP_SCOPE) {
2918 k = kid->op_next;
2919 kid->op_next = 0;
2920 peep(k);
79072805 2921 }
463ee0b2
LW
2922 else if (kid->op_type == OP_LEAVE) {
2923 kid->op_type = OP_NULL; /* wipe out leave */
2924 kid->op_ppaddr = ppaddr[OP_NULL];
2925 kid->op_next = kid;
2926
2927 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
2928 if (k->op_next == kid)
2929 k->op_next = 0;
2930 }
2931 peep(kLISTOP->op_first);
2932 }
2933 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
2934 kid->op_type = OP_NULL; /* wipe out rv2gv */
79072805
LW
2935 kid->op_ppaddr = ppaddr[OP_NULL];
2936 kid->op_next = kid;
79072805
LW
2937 op->op_flags |= OPf_SPECIAL;
2938 }
2939 }
2940 return op;
2941}
2942
2943OP *
2944ck_split(op)
2945OP *op;
2946{
2947 register OP *kid;
ed6116ce 2948 PMOP* pm;
79072805
LW
2949
2950 if (op->op_flags & OPf_STACKED)
2951 return no_fh_allowed(op);
2952
2953 if (!(op->op_flags & OPf_KIDS))
2954 op = prepend_elem(OP_SPLIT,
2955 pmruntime(
2956 newPMOP(OP_MATCH, OPf_SPECIAL),
2957 newSVOP(OP_CONST, 0, newSVpv(" ", 1)),
2958 Nullop),
2959 op);
2960
2961 kid = cLISTOP->op_first;
2962 if (kid->op_type == OP_PUSHMARK)
463ee0b2 2963 croak("panic: ck_split");
79072805
LW
2964
2965 if (kid->op_type != OP_MATCH) {
2966 OP *sibl = kid->op_sibling;
463ee0b2 2967 kid->op_sibling = 0;
79072805
LW
2968 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
2969 if (cLISTOP->op_first == cLISTOP->op_last)
2970 cLISTOP->op_last = kid;
2971 cLISTOP->op_first = kid;
2972 kid->op_sibling = sibl;
2973 }
ed6116ce
LW
2974 pm = (PMOP*)kid;
2975 if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
2976 sv_free(pm->op_pmshort); /* can't use substring to optimize */
2977 pm->op_pmshort = 0;
2978 }
79072805
LW
2979
2980 kid->op_type = OP_PUSHRE;
2981 kid->op_ppaddr = ppaddr[OP_PUSHRE];
2982 scalar(kid);
2983
2984 if (!kid->op_sibling)
2985 append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
2986
2987 kid = kid->op_sibling;
2988 scalar(kid);
2989
2990 if (!kid->op_sibling)
2991 append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
2992
2993 kid = kid->op_sibling;
2994 scalar(kid);
2995
2996 if (kid->op_sibling)
2997 return too_many_arguments(op);
2998
2999 return op;
3000}
3001
3002OP *
3003ck_subr(op)
3004OP *op;
3005{
93a17b20
LW
3006 OP *o = ((cUNOP->op_first->op_sibling)
3007 ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first->op_sibling;
3008
3009 if (o->op_type == OP_RV2CV) {
3010 o->op_type = OP_NULL; /* disable rv2cv */
3011 o->op_ppaddr = ppaddr[OP_NULL];
3012 }
79072805 3013 op->op_private = 0;
93a17b20
LW
3014 if (perldb)
3015 op->op_private |= OPpSUBR_DB;
79072805
LW
3016 return op;
3017}
3018
3019OP *
3020ck_trunc(op)
3021OP *op;
3022{
3023 if (op->op_flags & OPf_KIDS) {
3024 SVOP *kid = (SVOP*)cUNOP->op_first;
3025
3026 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
3027 op->op_flags |= OPf_SPECIAL;
3028 }
3029 return ck_fun(op);
3030}
3031
463ee0b2
LW
3032/* A peephole optimizer. We visit the ops in the order they're to execute. */
3033
79072805
LW
3034void
3035peep(op)
3036register OP* op;
3037{
3038 register OP* oldop = 0;
3039 if (!op || op->op_seq)
3040 return;
3041 for (; op; op = op->op_next) {
3042 if (op->op_seq)
3043 return;
3044 switch (op->op_type) {
3045 case OP_NULL:
3046 case OP_SCALAR:
93a17b20 3047 case OP_LINESEQ:
463ee0b2 3048 case OP_SCOPE:
79072805
LW
3049 if (oldop) {
3050 oldop->op_next = op->op_next;
3051 continue;
3052 }
463ee0b2 3053 op->op_seq = ++op_seqmax;
79072805
LW
3054 break;
3055
3056 case OP_GV:
463ee0b2
LW
3057 if (op->op_next->op_type == OP_RV2SV &&
3058 op->op_next->op_private < OP_RV2GV)
3059 {
79072805
LW
3060 op->op_next->op_type = OP_NULL;
3061 op->op_next->op_ppaddr = ppaddr[OP_NULL];
93a17b20 3062 op->op_flags |= op->op_next->op_flags & OPf_INTRO;
79072805
LW
3063 op->op_next = op->op_next->op_next;
3064 op->op_type = OP_GVSV;
3065 op->op_ppaddr = ppaddr[OP_GVSV];
3066 }
463ee0b2 3067 op->op_seq = ++op_seqmax;
79072805
LW
3068 break;
3069
3070 case OP_GREPWHILE:
3071 case OP_AND:
3072 case OP_OR:
463ee0b2 3073 op->op_seq = ++op_seqmax;
79072805
LW
3074 peep(cLOGOP->op_other);
3075 break;
3076
3077 case OP_COND_EXPR:
463ee0b2 3078 op->op_seq = ++op_seqmax;
79072805
LW
3079 peep(cCONDOP->op_true);
3080 peep(cCONDOP->op_false);
3081 break;
3082
3083 case OP_ENTERLOOP:
463ee0b2 3084 op->op_seq = ++op_seqmax;
79072805
LW
3085 peep(cLOOP->op_redoop);
3086 peep(cLOOP->op_nextop);
3087 peep(cLOOP->op_lastop);
3088 break;
3089
3090 case OP_MATCH:
3091 case OP_SUBST:
463ee0b2 3092 op->op_seq = ++op_seqmax;
79072805
LW
3093 peep(cPMOP->op_pmreplroot);
3094 break;
3095
3096 default:
463ee0b2 3097 op->op_seq = ++op_seqmax;
79072805
LW
3098 break;
3099 }
3100 oldop = op;
3101 }
3102}