This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Turn off gcc-2.95's strict-alias optimization.
[perl5.git] / op.c
CommitLineData
a0d0e21e 1/* op.c
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805
LW
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 *
a0d0e21e
LW
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
79072805
LW
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_OP_C
79072805
LW
20#include "perl.h"
21
b7dc083c
NIS
22/* #define PL_OP_SLAB_ALLOC */
23
24#ifdef PL_OP_SLAB_ALLOC
25#define SLAB_SIZE 8192
26static char *PL_OpPtr = NULL;
27static int PL_OpSpace = 0;
28#define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
29 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
30 else \
31 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
32 } while (0)
33
864dbfa3 34STATIC void *
cea2e8a9 35S_Slab_Alloc(pTHX_ int m, size_t sz)
b7dc083c
NIS
36{
37 Newz(m,PL_OpPtr,SLAB_SIZE,char);
38 PL_OpSpace = SLAB_SIZE - sz;
39 return PL_OpPtr += PL_OpSpace;
40}
76e3520e 41
b7dc083c
NIS
42#else
43#define NewOp(m, var, c, type) Newz(m, var, c, type)
44#endif
e50aee73 45/*
5dc0d613 46 * In the following definition, the ", Nullop" is just to make the compiler
a5f75d66 47 * think the expression is of the right type: croak actually does a Siglongjmp.
e50aee73 48 */
11343788 49#define CHECKOP(type,o) \
3280af22 50 ((PL_op_mask && PL_op_mask[type]) \
5dc0d613 51 ? ( op_free((OP*)o), \
cea2e8a9 52 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
28757baa 53 Nullop ) \
fc0dc3b3 54 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
e50aee73 55
c53d7c7d
HS
56#define PAD_MAX 999999999
57
76e3520e 58STATIC char*
cea2e8a9 59S_gv_ename(pTHX_ GV *gv)
4633a7c4 60{
2d8e6c8d 61 STRLEN n_a;
4633a7c4 62 SV* tmpsv = sv_newmortal();
46fc3d4c 63 gv_efullname3(tmpsv, gv, Nullch);
2d8e6c8d 64 return SvPV(tmpsv,n_a);
4633a7c4
LW
65}
66
76e3520e 67STATIC OP *
cea2e8a9 68S_no_fh_allowed(pTHX_ OP *o)
79072805 69{
cea2e8a9 70 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
22c35a8c 71 PL_op_desc[o->op_type]));
11343788 72 return o;
79072805
LW
73}
74
76e3520e 75STATIC OP *
cea2e8a9 76S_too_few_arguments(pTHX_ OP *o, char *name)
79072805 77{
cea2e8a9 78 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
11343788 79 return o;
79072805
LW
80}
81
76e3520e 82STATIC OP *
cea2e8a9 83S_too_many_arguments(pTHX_ OP *o, char *name)
79072805 84{
cea2e8a9 85 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
11343788 86 return o;
79072805
LW
87}
88
76e3520e 89STATIC void
cea2e8a9 90S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
8990e307 91{
cea2e8a9 92 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
22c35a8c 93 (int)n, name, t, PL_op_desc[kid->op_type]));
8990e307
LW
94}
95
7a52d87a 96STATIC void
cea2e8a9 97S_no_bareword_allowed(pTHX_ OP *o)
7a52d87a 98{
cea2e8a9 99 Perl_warn(aTHX_ "Bareword \"%s\" not allowed while \"strict subs\" in use",
7a52d87a
GS
100 SvPV_nolen(cSVOPo->op_sv));
101 ++PL_error_count;
102}
103
79072805
LW
104/* "register" allocation */
105
106PADOFFSET
864dbfa3 107Perl_pad_allocmy(pTHX_ char *name)
93a17b20 108{
11343788 109 dTHR;
a0d0e21e
LW
110 PADOFFSET off;
111 SV *sv;
112
834a4ddd
LW
113 if (!(
114 isALPHA(name[1]) ||
115 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
116 name[1] == '_' && (int)strlen(name) > 2))
117 {
c4d0567e 118 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
2b92dfce
GS
119 /* 1999-02-27 mjd@plover.com */
120 char *p;
121 p = strchr(name, '\0');
122 /* The next block assumes the buffer is at least 205 chars
123 long. At present, it's always at least 256 chars. */
124 if (p-name > 200) {
125 strcpy(name+200, "...");
126 p = name+199;
127 }
128 else {
129 p[1] = '\0';
130 }
131 /* Move everything else down one character */
132 for (; p-name > 2; p--)
133 *p = *(p-1);
46fc3d4c
PP
134 name[2] = toCTRL(name[1]);
135 name[1] = '^';
136 }
cea2e8a9 137 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
a0d0e21e 138 }
599cee73 139 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
3280af22
NIS
140 SV **svp = AvARRAY(PL_comppad_name);
141 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
b1cb66bf 142 if ((sv = svp[off])
3280af22 143 && sv != &PL_sv_undef
c53d7c7d 144 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
b1cb66bf
PP
145 && strEQ(name, SvPVX(sv)))
146 {
cea2e8a9 147 Perl_warner(aTHX_ WARN_UNSAFE,
9fbbe825 148 "\"my\" variable %s masks earlier declaration in same %s",
c53d7c7d 149 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
b1cb66bf
PP
150 break;
151 }
152 }
153 }
a0d0e21e
LW
154 off = pad_alloc(OP_PADSV, SVs_PADMY);
155 sv = NEWSV(1102,0);
93a17b20
LW
156 sv_upgrade(sv, SVt_PVNV);
157 sv_setpv(sv, name);
3280af22 158 if (PL_in_my_stash) {
c750a3ec 159 if (*name != '$')
cea2e8a9 160 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
dce40276 161 name));
c750a3ec
MB
162 SvOBJECT_on(sv);
163 (void)SvUPGRADE(sv, SVt_PVMG);
3280af22
NIS
164 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
165 PL_sv_objcount++;
c750a3ec 166 }
3280af22 167 av_store(PL_comppad_name, off, sv);
65202027 168 SvNVX(sv) = (NV)PAD_MAX;
8990e307 169 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
3280af22
NIS
170 if (!PL_min_intro_pending)
171 PL_min_intro_pending = off;
172 PL_max_intro_pending = off;
93a17b20 173 if (*name == '@')
3280af22 174 av_store(PL_comppad, off, (SV*)newAV());
93a17b20 175 else if (*name == '%')
3280af22
NIS
176 av_store(PL_comppad, off, (SV*)newHV());
177 SvPADMY_on(PL_curpad[off]);
93a17b20
LW
178 return off;
179}
180
2680586e
GS
181#define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
182
76e3520e 183STATIC PADOFFSET
cea2e8a9 184S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
864dbfa3 185 I32 cx_ix, I32 saweval, U32 flags)
93a17b20 186{
11343788 187 dTHR;
748a9306 188 CV *cv;
93a17b20
LW
189 I32 off;
190 SV *sv;
93a17b20 191 register I32 i;
c09156bb 192 register PERL_CONTEXT *cx;
93a17b20 193
748a9306 194 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
4fdae800
PP
195 AV *curlist = CvPADLIST(cv);
196 SV **svp = av_fetch(curlist, 0, FALSE);
748a9306 197 AV *curname;
4fdae800 198
3280af22 199 if (!svp || *svp == &PL_sv_undef)
4633a7c4 200 continue;
748a9306
LW
201 curname = (AV*)*svp;
202 svp = AvARRAY(curname);
93965878 203 for (off = AvFILLp(curname); off > 0; off--) {
748a9306 204 if ((sv = svp[off]) &&
3280af22 205 sv != &PL_sv_undef &&
748a9306 206 seq <= SvIVX(sv) &&
13826f2c 207 seq > I_32(SvNVX(sv)) &&
748a9306
LW
208 strEQ(SvPVX(sv), name))
209 {
5f05dabc
PP
210 I32 depth;
211 AV *oldpad;
212 SV *oldsv;
213
214 depth = CvDEPTH(cv);
215 if (!depth) {
9607fc9c
PP
216 if (newoff) {
217 if (SvFAKE(sv))
218 continue;
4fdae800 219 return 0; /* don't clone from inactive stack frame */
9607fc9c 220 }
5f05dabc
PP
221 depth = 1;
222 }
223 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
224 oldsv = *av_fetch(oldpad, off, TRUE);
748a9306 225 if (!newoff) { /* Not a mere clone operation. */
9607fc9c 226 SV *namesv = NEWSV(1103,0);
748a9306 227 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
9607fc9c
PP
228 sv_upgrade(namesv, SVt_PVNV);
229 sv_setpv(namesv, name);
3280af22 230 av_store(PL_comppad_name, newoff, namesv);
65202027 231 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
c53d7c7d 232 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
9607fc9c 233 SvFAKE_on(namesv); /* A ref, not a real var */
87671ffc 234 if (SvOBJECT(sv)) { /* A typed var */
3d93dc8b
GS
235 SvOBJECT_on(namesv);
236 (void)SvUPGRADE(namesv, SVt_PVMG);
87671ffc 237 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
3d93dc8b
GS
238 PL_sv_objcount++;
239 }
3280af22 240 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
28757baa 241 /* "It's closures all the way down." */
3280af22 242 CvCLONE_on(PL_compcv);
54310121 243 if (cv == startcv) {
3280af22 244 if (CvANON(PL_compcv))
54310121
PP
245 oldsv = Nullsv; /* no need to keep ref */
246 }
247 else {
28757baa
PP
248 CV *bcv;
249 for (bcv = startcv;
250 bcv && bcv != cv && !CvCLONE(bcv);
6b35e009
GS
251 bcv = CvOUTSIDE(bcv))
252 {
28757baa
PP
253 if (CvANON(bcv))
254 CvCLONE_on(bcv);
255 else {
6b35e009
GS
256 if (ckWARN(WARN_CLOSURE)
257 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
258 {
cea2e8a9 259 Perl_warner(aTHX_ WARN_CLOSURE,
44a8e56a 260 "Variable \"%s\" may be unavailable",
28757baa 261 name);
6b35e009 262 }
28757baa
PP
263 break;
264 }
265 }
266 }
267 }
3280af22 268 else if (!CvUNIQUE(PL_compcv)) {
599cee73 269 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
cea2e8a9 270 Perl_warner(aTHX_ WARN_CLOSURE,
599cee73 271 "Variable \"%s\" will not stay shared", name);
5f05dabc 272 }
748a9306 273 }
3280af22 274 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
748a9306
LW
275 return newoff;
276 }
93a17b20
LW
277 }
278 }
279
2680586e
GS
280 if (flags & FINDLEX_NOSEARCH)
281 return 0;
282
93a17b20
LW
283 /* Nothing in current lexical context--try eval's context, if any.
284 * This is necessary to let the perldb get at lexically scoped variables.
285 * XXX This will also probably interact badly with eval tree caching.
286 */
287
748a9306 288 for (i = cx_ix; i >= 0; i--) {
93a17b20 289 cx = &cxstack[i];
6b35e009 290 switch (CxTYPE(cx)) {
93a17b20 291 default:
748a9306
LW
292 if (i == 0 && saweval) {
293 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 294 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
748a9306 295 }
93a17b20
LW
296 break;
297 case CXt_EVAL:
44a8e56a
PP
298 switch (cx->blk_eval.old_op_type) {
299 case OP_ENTEREVAL:
6b35e009
GS
300 if (CxREALEVAL(cx))
301 saweval = i;
44a8e56a
PP
302 break;
303 case OP_REQUIRE:
304 /* require must have its own scope */
305 return 0;
306 }
93a17b20
LW
307 break;
308 case CXt_SUB:
309 if (!saweval)
310 return 0;
311 cv = cx->blk_sub.cv;
3280af22 312 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
748a9306 313 saweval = i; /* so we know where we were called from */
93a17b20 314 continue;
93a17b20 315 }
748a9306 316 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 317 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
93a17b20
LW
318 }
319 }
320
748a9306
LW
321 return 0;
322}
a0d0e21e 323
748a9306 324PADOFFSET
864dbfa3 325Perl_pad_findmy(pTHX_ char *name)
748a9306 326{
11343788 327 dTHR;
748a9306 328 I32 off;
54310121 329 I32 pendoff = 0;
748a9306 330 SV *sv;
3280af22
NIS
331 SV **svp = AvARRAY(PL_comppad_name);
332 U32 seq = PL_cop_seqmax;
6b35e009 333 PERL_CONTEXT *cx;
33b8ce05 334 CV *outside;
748a9306 335
11343788
MB
336#ifdef USE_THREADS
337 /*
338 * Special case to get lexical (and hence per-thread) @_.
339 * XXX I need to find out how to tell at parse-time whether use
340 * of @_ should refer to a lexical (from a sub) or defgv (global
341 * scope and maybe weird sub-ish things like formats). See
342 * startsub in perly.y. It's possible that @_ could be lexical
343 * (at least from subs) even in non-threaded perl.
344 */
345 if (strEQ(name, "@_"))
346 return 0; /* success. (NOT_IN_PAD indicates failure) */
347#endif /* USE_THREADS */
348
748a9306 349 /* The one we're looking for is probably just before comppad_name_fill. */
3280af22 350 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
a0d0e21e 351 if ((sv = svp[off]) &&
3280af22 352 sv != &PL_sv_undef &&
54310121
PP
353 (!SvIVX(sv) ||
354 (seq <= SvIVX(sv) &&
355 seq > I_32(SvNVX(sv)))) &&
a0d0e21e
LW
356 strEQ(SvPVX(sv), name))
357 {
54310121
PP
358 if (SvIVX(sv))
359 return (PADOFFSET)off;
360 pendoff = off; /* this pending def. will override import */
a0d0e21e
LW
361 }
362 }
748a9306 363
33b8ce05
GS
364 outside = CvOUTSIDE(PL_compcv);
365
366 /* Check if if we're compiling an eval'', and adjust seq to be the
367 * eval's seq number. This depends on eval'' having a non-null
368 * CvOUTSIDE() while it is being compiled. The eval'' itself is
1aff0e91
GS
369 * identified by CvEVAL being true and CvGV being null. */
370 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
6b35e009
GS
371 cx = &cxstack[cxstack_ix];
372 if (CxREALEVAL(cx))
373 seq = cx->blk_oldcop->cop_seq;
374 }
375
748a9306 376 /* See if it's in a nested scope */
2680586e 377 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
54310121
PP
378 if (off) {
379 /* If there is a pending local definition, this new alias must die */
380 if (pendoff)
3280af22 381 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
11343788 382 return off; /* pad_findlex returns 0 for failure...*/
54310121 383 }
11343788 384 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
93a17b20
LW
385}
386
387void
864dbfa3 388Perl_pad_leavemy(pTHX_ I32 fill)
93a17b20 389{
0453d815 390 dTHR;
93a17b20 391 I32 off;
3280af22 392 SV **svp = AvARRAY(PL_comppad_name);
93a17b20 393 SV *sv;
3280af22
NIS
394 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
395 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
0453d815
PM
396 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
397 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
8990e307
LW
398 }
399 }
400 /* "Deintroduce" my variables that are leaving with this scope. */
3280af22 401 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
c53d7c7d 402 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
3280af22 403 SvIVX(sv) = PL_cop_seqmax;
93a17b20
LW
404 }
405}
406
407PADOFFSET
864dbfa3 408Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
79072805 409{
11343788 410 dTHR;
79072805
LW
411 SV *sv;
412 I32 retval;
413
3280af22 414 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 415 Perl_croak(aTHX_ "panic: pad_alloc");
3280af22 416 if (PL_pad_reset_pending)
a0d0e21e 417 pad_reset();
ed6116ce 418 if (tmptype & SVs_PADMY) {
79072805 419 do {
3280af22 420 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
ed6116ce 421 } while (SvPADBUSY(sv)); /* need a fresh one */
3280af22 422 retval = AvFILLp(PL_comppad);
79072805
LW
423 }
424 else {
3280af22
NIS
425 SV **names = AvARRAY(PL_comppad_name);
426 SSize_t names_fill = AvFILLp(PL_comppad_name);
bbce6d69
PP
427 for (;;) {
428 /*
429 * "foreach" index vars temporarily become aliases to non-"my"
430 * values. Thus we must skip, not just pad values that are
431 * marked as current pad values, but also those with names.
432 */
3280af22
NIS
433 if (++PL_padix <= names_fill &&
434 (sv = names[PL_padix]) && sv != &PL_sv_undef)
bbce6d69 435 continue;
3280af22 436 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
bbce6d69
PP
437 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
438 break;
439 }
3280af22 440 retval = PL_padix;
79072805 441 }
8990e307 442 SvFLAGS(sv) |= tmptype;
3280af22 443 PL_curpad = AvARRAY(PL_comppad);
11343788 444#ifdef USE_THREADS
5dc0d613 445 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
533c011a 446 (unsigned long) thr, (unsigned long) PL_curpad,
22c35a8c 447 (long) retval, PL_op_name[optype]));
11343788 448#else
d9bb4600 449 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
3280af22 450 (unsigned long) PL_curpad,
22c35a8c 451 (long) retval, PL_op_name[optype]));
11343788 452#endif /* USE_THREADS */
79072805
LW
453 return (PADOFFSET)retval;
454}
455
456SV *
864dbfa3 457Perl_pad_sv(pTHX_ PADOFFSET po)
79072805 458{
11343788
MB
459 dTHR;
460#ifdef USE_THREADS
5dc0d613 461 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
533c011a 462 (unsigned long) thr, (unsigned long) PL_curpad, po));
11343788 463#else
79072805 464 if (!po)
cea2e8a9 465 Perl_croak(aTHX_ "panic: pad_sv po");
d9bb4600 466 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
3280af22 467 (unsigned long) PL_curpad, po));
11343788 468#endif /* USE_THREADS */
3280af22 469 return PL_curpad[po]; /* eventually we'll turn this into a macro */
79072805
LW
470}
471
472void
864dbfa3 473Perl_pad_free(pTHX_ PADOFFSET po)
79072805 474{
11343788 475 dTHR;
3280af22 476 if (!PL_curpad)
a0d0e21e 477 return;
3280af22 478 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 479 Perl_croak(aTHX_ "panic: pad_free curpad");
79072805 480 if (!po)
cea2e8a9 481 Perl_croak(aTHX_ "panic: pad_free po");
11343788 482#ifdef USE_THREADS
5dc0d613 483 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
533c011a 484 (unsigned long) thr, (unsigned long) PL_curpad, po));
11343788 485#else
d9bb4600 486 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
3280af22 487 (unsigned long) PL_curpad, po));
11343788 488#endif /* USE_THREADS */
3280af22
NIS
489 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
490 SvPADTMP_off(PL_curpad[po]);
491 if ((I32)po < PL_padix)
492 PL_padix = po - 1;
79072805
LW
493}
494
495void
864dbfa3 496Perl_pad_swipe(pTHX_ PADOFFSET po)
79072805 497{
11343788 498 dTHR;
3280af22 499 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 500 Perl_croak(aTHX_ "panic: pad_swipe curpad");
79072805 501 if (!po)
cea2e8a9 502 Perl_croak(aTHX_ "panic: pad_swipe po");
11343788 503#ifdef USE_THREADS
5dc0d613 504 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
533c011a 505 (unsigned long) thr, (unsigned long) PL_curpad, po));
11343788 506#else
d9bb4600 507 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
3280af22 508 (unsigned long) PL_curpad, po));
11343788 509#endif /* USE_THREADS */
3280af22
NIS
510 SvPADTMP_off(PL_curpad[po]);
511 PL_curpad[po] = NEWSV(1107,0);
512 SvPADTMP_on(PL_curpad[po]);
513 if ((I32)po < PL_padix)
514 PL_padix = po - 1;
79072805
LW
515}
516
d9bb4600
GS
517/* XXX pad_reset() is currently disabled because it results in serious bugs.
518 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
519 * on the stack by OPs that use them, there are several ways to get an alias
520 * to a shared TARG. Such an alias will change randomly and unpredictably.
521 * We avoid doing this until we can think of a Better Way.
522 * GSAR 97-10-29 */
79072805 523void
864dbfa3 524Perl_pad_reset(pTHX)
79072805 525{
d9bb4600 526#ifdef USE_BROKEN_PAD_RESET
11343788 527 dTHR;
79072805
LW
528 register I32 po;
529
6b88bc9c 530 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 531 Perl_croak(aTHX_ "panic: pad_reset curpad");
11343788 532#ifdef USE_THREADS
5dc0d613 533 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
6b88bc9c 534 (unsigned long) thr, (unsigned long) PL_curpad));
11343788 535#else
d9bb4600 536 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
6b88bc9c 537 (unsigned long) PL_curpad));
11343788 538#endif /* USE_THREADS */
6b88bc9c
GS
539 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
540 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
541 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
542 SvPADTMP_off(PL_curpad[po]);
748a9306 543 }
6b88bc9c 544 PL_padix = PL_padix_floor;
79072805 545 }
d9bb4600 546#endif
3280af22 547 PL_pad_reset_pending = FALSE;
79072805
LW
548}
549
a863c7d1 550#ifdef USE_THREADS
54b9620d 551/* find_threadsv is not reentrant */
a863c7d1 552PADOFFSET
864dbfa3 553Perl_find_threadsv(pTHX_ const char *name)
a863c7d1
MB
554{
555 dTHR;
556 char *p;
557 PADOFFSET key;
554b3eca 558 SV **svp;
54b9620d 559 /* We currently only handle names of a single character */
533c011a 560 p = strchr(PL_threadsv_names, *name);
a863c7d1
MB
561 if (!p)
562 return NOT_IN_PAD;
533c011a 563 key = p - PL_threadsv_names;
2d8e6c8d 564 MUTEX_LOCK(&thr->mutex);
54b9620d 565 svp = av_fetch(thr->threadsv, key, FALSE);
2d8e6c8d
GS
566 if (svp)
567 MUTEX_UNLOCK(&thr->mutex);
568 else {
554b3eca 569 SV *sv = NEWSV(0, 0);
54b9620d 570 av_store(thr->threadsv, key, sv);
940cb80d 571 thr->threadsvp = AvARRAY(thr->threadsv);
2d8e6c8d 572 MUTEX_UNLOCK(&thr->mutex);
554b3eca
MB
573 /*
574 * Some magic variables used to be automagically initialised
575 * in gv_fetchpv. Those which are now per-thread magicals get
576 * initialised here instead.
577 */
578 switch (*name) {
54b9620d
MB
579 case '_':
580 break;
554b3eca
MB
581 case ';':
582 sv_setpv(sv, "\034");
54b9620d 583 sv_magic(sv, 0, 0, name, 1);
554b3eca 584 break;
c277df42
IZ
585 case '&':
586 case '`':
587 case '\'':
533c011a 588 PL_sawampersand = TRUE;
a3f914c5
GS
589 /* FALL THROUGH */
590 case '1':
591 case '2':
592 case '3':
593 case '4':
594 case '5':
595 case '6':
596 case '7':
597 case '8':
598 case '9':
c277df42 599 SvREADONLY_on(sv);
d8b5173a 600 /* FALL THROUGH */
067391ea
GS
601
602 /* XXX %! tied to Errno.pm needs to be added here.
603 * See gv_fetchpv(). */
604 /* case '!': */
605
54b9620d
MB
606 default:
607 sv_magic(sv, 0, 0, name, 1);
554b3eca 608 }
8b73bbec 609 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
54b9620d 610 "find_threadsv: new SV %p for $%s%c\n",
554b3eca
MB
611 sv, (*name < 32) ? "^" : "",
612 (*name < 32) ? toCTRL(*name) : *name));
a863c7d1
MB
613 }
614 return key;
615}
616#endif /* USE_THREADS */
617
79072805
LW
618/* Destructor */
619
620void
864dbfa3 621Perl_op_free(pTHX_ OP *o)
79072805 622{
85e6fe83 623 register OP *kid, *nextkid;
acb36ea4 624 OPCODE type;
79072805 625
5dc0d613 626 if (!o || o->op_seq == (U16)-1)
79072805
LW
627 return;
628
11343788
MB
629 if (o->op_flags & OPf_KIDS) {
630 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
85e6fe83 631 nextkid = kid->op_sibling; /* Get before next freeing kid */
79072805 632 op_free(kid);
85e6fe83 633 }
79072805 634 }
acb36ea4
GS
635 type = o->op_type;
636 if (type == OP_NULL)
637 type = o->op_targ;
638
639 /* COP* is not cleared by op_clear() so that we may track line
640 * numbers etc even after null() */
641 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
642 cop_free((COP*)o);
643
644 op_clear(o);
645
646#ifdef PL_OP_SLAB_ALLOC
647 if ((char *) o == PL_OpPtr)
648 {
649 }
650#else
651 Safefree(o);
652#endif
653}
79072805 654
acb36ea4
GS
655STATIC void
656S_op_clear(pTHX_ OP *o)
657{
11343788 658 switch (o->op_type) {
acb36ea4
GS
659 case OP_NULL: /* Was holding old type, if any. */
660 case OP_ENTEREVAL: /* Was holding hints. */
661#ifdef USE_THREADS
662 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
663#endif
664 o->op_targ = 0;
a0d0e21e 665 break;
554b3eca 666#ifdef USE_THREADS
8dd3ba40
SM
667 case OP_ENTERITER:
668 if (!(o->op_flags & OPf_SPECIAL))
669 break;
670 /* FALL THROUGH */
554b3eca 671#endif /* USE_THREADS */
a6006777 672 default:
ac4c12e7 673 if (!(o->op_flags & OPf_REF)
0b94c7bb 674 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
a6006777
PP
675 break;
676 /* FALL THROUGH */
463ee0b2 677 case OP_GVSV:
79072805 678 case OP_GV:
a6006777 679 case OP_AELEMFAST:
11343788 680 SvREFCNT_dec(cGVOPo->op_gv);
acb36ea4 681 cGVOPo->op_gv = Nullgv;
79072805
LW
682 break;
683 case OP_CONST:
11343788 684 SvREFCNT_dec(cSVOPo->op_sv);
acb36ea4 685 cSVOPo->op_sv = Nullsv;
79072805 686 break;
748a9306
LW
687 case OP_GOTO:
688 case OP_NEXT:
689 case OP_LAST:
690 case OP_REDO:
11343788 691 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
748a9306
LW
692 break;
693 /* FALL THROUGH */
a0d0e21e 694 case OP_TRANS:
acb36ea4 695 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
a0ed51b3 696 SvREFCNT_dec(cSVOPo->op_sv);
acb36ea4
GS
697 cSVOPo->op_sv = Nullsv;
698 }
699 else {
a0ed51b3 700 Safefree(cPVOPo->op_pv);
acb36ea4
GS
701 cPVOPo->op_pv = Nullch;
702 }
a0d0e21e
LW
703 break;
704 case OP_SUBST:
11343788 705 op_free(cPMOPo->op_pmreplroot);
acb36ea4 706 cPMOPo->op_pmreplroot = Nullop;
a0d0e21e 707 /* FALL THROUGH */
748a9306 708 case OP_PUSHRE:
a0d0e21e 709 case OP_MATCH:
8782bef2 710 case OP_QR:
c277df42 711 ReREFCNT_dec(cPMOPo->op_pmregexp);
acb36ea4 712 cPMOPo->op_pmregexp = (REGEXP*)NULL;
a0d0e21e 713 break;
79072805
LW
714 }
715
11343788
MB
716 if (o->op_targ > 0)
717 pad_free(o->op_targ);
79072805
LW
718}
719
76e3520e 720STATIC void
3eb57f73
HS
721S_cop_free(pTHX_ COP* cop)
722{
723 Safefree(cop->cop_label);
724 SvREFCNT_dec(cop->cop_filegv);
0453d815 725 if (! specialWARN(cop->cop_warnings))
3eb57f73
HS
726 SvREFCNT_dec(cop->cop_warnings);
727}
728
729STATIC void
cea2e8a9 730S_null(pTHX_ OP *o)
8990e307 731{
acb36ea4
GS
732 if (o->op_type == OP_NULL)
733 return;
734 op_clear(o);
11343788
MB
735 o->op_targ = o->op_type;
736 o->op_type = OP_NULL;
22c35a8c 737 o->op_ppaddr = PL_ppaddr[OP_NULL];
8990e307
LW
738}
739
79072805
LW
740/* Contextualizers */
741
463ee0b2 742#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
79072805
LW
743
744OP *
864dbfa3 745Perl_linklist(pTHX_ OP *o)
79072805
LW
746{
747 register OP *kid;
748
11343788
MB
749 if (o->op_next)
750 return o->op_next;
79072805
LW
751
752 /* establish postfix order */
11343788
MB
753 if (cUNOPo->op_first) {
754 o->op_next = LINKLIST(cUNOPo->op_first);
755 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
756 if (kid->op_sibling)
757 kid->op_next = LINKLIST(kid->op_sibling);
758 else
11343788 759 kid->op_next = o;
79072805
LW
760 }
761 }
762 else
11343788 763 o->op_next = o;
79072805 764
11343788 765 return o->op_next;
79072805
LW
766}
767
768OP *
864dbfa3 769Perl_scalarkids(pTHX_ OP *o)
79072805
LW
770{
771 OP *kid;
11343788
MB
772 if (o && o->op_flags & OPf_KIDS) {
773 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
774 scalar(kid);
775 }
11343788 776 return o;
79072805
LW
777}
778
76e3520e 779STATIC OP *
cea2e8a9 780S_scalarboolean(pTHX_ OP *o)
8990e307 781{
d008e5eb 782 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
0f15f207 783 dTHR;
d008e5eb
GS
784 if (ckWARN(WARN_SYNTAX)) {
785 line_t oldline = PL_curcop->cop_line;
a0d0e21e 786
d008e5eb
GS
787 if (PL_copline != NOLINE)
788 PL_curcop->cop_line = PL_copline;
cea2e8a9 789 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
d008e5eb
GS
790 PL_curcop->cop_line = oldline;
791 }
a0d0e21e 792 }
11343788 793 return scalar(o);
8990e307
LW
794}
795
796OP *
864dbfa3 797Perl_scalar(pTHX_ OP *o)
79072805
LW
798{
799 OP *kid;
800
a0d0e21e 801 /* assumes no premature commitment */
3280af22 802 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 803 || o->op_type == OP_RETURN)
11343788 804 return o;
79072805 805
b162f9ea
IZ
806 if ((o->op_private & OPpTARGET_MY)
807 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
808 return scalar(o); /* As if inside SASSIGN */
809
5dc0d613 810 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
79072805 811
11343788 812 switch (o->op_type) {
79072805 813 case OP_REPEAT:
11343788
MB
814 if (o->op_private & OPpREPEAT_DOLIST)
815 null(((LISTOP*)cBINOPo->op_first)->op_first);
816 scalar(cBINOPo->op_first);
8990e307 817 break;
79072805
LW
818 case OP_OR:
819 case OP_AND:
820 case OP_COND_EXPR:
11343788 821 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
8990e307 822 scalar(kid);
79072805 823 break;
a0d0e21e 824 case OP_SPLIT:
11343788 825 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
826 if (!kPMOP->op_pmreplroot)
827 deprecate("implicit split to @_");
828 }
829 /* FALL THROUGH */
79072805 830 case OP_MATCH:
8782bef2 831 case OP_QR:
79072805
LW
832 case OP_SUBST:
833 case OP_NULL:
8990e307 834 default:
11343788
MB
835 if (o->op_flags & OPf_KIDS) {
836 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
8990e307
LW
837 scalar(kid);
838 }
79072805
LW
839 break;
840 case OP_LEAVE:
841 case OP_LEAVETRY:
5dc0d613 842 kid = cLISTOPo->op_first;
54310121
PP
843 scalar(kid);
844 while (kid = kid->op_sibling) {
845 if (kid->op_sibling)
846 scalarvoid(kid);
847 else
848 scalar(kid);
849 }
3280af22 850 WITH_THR(PL_curcop = &PL_compiling);
54310121 851 break;
748a9306 852 case OP_SCOPE:
79072805 853 case OP_LINESEQ:
8990e307 854 case OP_LIST:
11343788 855 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
856 if (kid->op_sibling)
857 scalarvoid(kid);
858 else
859 scalar(kid);
860 }
3280af22 861 WITH_THR(PL_curcop = &PL_compiling);
79072805
LW
862 break;
863 }
11343788 864 return o;
79072805
LW
865}
866
867OP *
864dbfa3 868Perl_scalarvoid(pTHX_ OP *o)
79072805
LW
869{
870 OP *kid;
8990e307
LW
871 char* useless = 0;
872 SV* sv;
2ebea0a1
GS
873 U8 want;
874
acb36ea4
GS
875 if (o->op_type == OP_NEXTSTATE
876 || o->op_type == OP_SETSTATE
877 || o->op_type == OP_DBSTATE
878 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
879 || o->op_targ == OP_SETSTATE
880 || o->op_targ == OP_DBSTATE)))
2ebea0a1
GS
881 {
882 dTHR;
883 PL_curcop = (COP*)o; /* for warning below */
884 }
79072805 885
54310121 886 /* assumes no premature commitment */
2ebea0a1
GS
887 want = o->op_flags & OPf_WANT;
888 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
5dc0d613 889 || o->op_type == OP_RETURN)
11343788 890 return o;
79072805 891
b162f9ea
IZ
892 if ((o->op_private & OPpTARGET_MY)
893 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
894 return scalar(o); /* As if inside SASSIGN */
895
5dc0d613 896 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
79072805 897
11343788 898 switch (o->op_type) {
79072805 899 default:
22c35a8c 900 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
8990e307 901 break;
36477c24
PP
902 /* FALL THROUGH */
903 case OP_REPEAT:
11343788 904 if (o->op_flags & OPf_STACKED)
8990e307 905 break;
5d82c453
GA
906 goto func_ops;
907 case OP_SUBSTR:
908 if (o->op_private == 4)
909 break;
8990e307
LW
910 /* FALL THROUGH */
911 case OP_GVSV:
912 case OP_WANTARRAY:
913 case OP_GV:
914 case OP_PADSV:
915 case OP_PADAV:
916 case OP_PADHV:
917 case OP_PADANY:
918 case OP_AV2ARYLEN:
8990e307 919 case OP_REF:
a0d0e21e
LW
920 case OP_REFGEN:
921 case OP_SREFGEN:
8990e307
LW
922 case OP_DEFINED:
923 case OP_HEX:
924 case OP_OCT:
925 case OP_LENGTH:
8990e307
LW
926 case OP_VEC:
927 case OP_INDEX:
928 case OP_RINDEX:
929 case OP_SPRINTF:
930 case OP_AELEM:
931 case OP_AELEMFAST:
932 case OP_ASLICE:
8990e307
LW
933 case OP_HELEM:
934 case OP_HSLICE:
935 case OP_UNPACK:
936 case OP_PACK:
8990e307
LW
937 case OP_JOIN:
938 case OP_LSLICE:
939 case OP_ANONLIST:
940 case OP_ANONHASH:
941 case OP_SORT:
942 case OP_REVERSE:
943 case OP_RANGE:
944 case OP_FLIP:
945 case OP_FLOP:
946 case OP_CALLER:
947 case OP_FILENO:
948 case OP_EOF:
949 case OP_TELL:
950 case OP_GETSOCKNAME:
951 case OP_GETPEERNAME:
952 case OP_READLINK:
953 case OP_TELLDIR:
954 case OP_GETPPID:
955 case OP_GETPGRP:
956 case OP_GETPRIORITY:
957 case OP_TIME:
958 case OP_TMS:
959 case OP_LOCALTIME:
960 case OP_GMTIME:
961 case OP_GHBYNAME:
962 case OP_GHBYADDR:
963 case OP_GHOSTENT:
964 case OP_GNBYNAME:
965 case OP_GNBYADDR:
966 case OP_GNETENT:
967 case OP_GPBYNAME:
968 case OP_GPBYNUMBER:
969 case OP_GPROTOENT:
970 case OP_GSBYNAME:
971 case OP_GSBYPORT:
972 case OP_GSERVENT:
973 case OP_GPWNAM:
974 case OP_GPWUID:
975 case OP_GGRNAM:
976 case OP_GGRGID:
977 case OP_GETLOGIN:
5d82c453 978 func_ops:
11343788 979 if (!(o->op_private & OPpLVAL_INTRO))
22c35a8c 980 useless = PL_op_desc[o->op_type];
8990e307
LW
981 break;
982
983 case OP_RV2GV:
984 case OP_RV2SV:
985 case OP_RV2AV:
986 case OP_RV2HV:
11343788
MB
987 if (!(o->op_private & OPpLVAL_INTRO) &&
988 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
8990e307
LW
989 useless = "a variable";
990 break;
79072805
LW
991
992 case OP_CONST:
11343788 993 sv = cSVOPo->op_sv;
7a52d87a
GS
994 if (cSVOPo->op_private & OPpCONST_STRICT)
995 no_bareword_allowed(o);
996 else {
d008e5eb
GS
997 dTHR;
998 if (ckWARN(WARN_VOID)) {
999 useless = "a constant";
1000 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1001 useless = 0;
1002 else if (SvPOK(sv)) {
1003 if (strnEQ(SvPVX(sv), "di", 2) ||
1004 strnEQ(SvPVX(sv), "ds", 2) ||
1005 strnEQ(SvPVX(sv), "ig", 2))
1006 useless = 0;
1007 }
8990e307
LW
1008 }
1009 }
acb36ea4 1010 null(o); /* don't execute or even remember it */
79072805
LW
1011 break;
1012
1013 case OP_POSTINC:
11343788 1014 o->op_type = OP_PREINC; /* pre-increment is faster */
22c35a8c 1015 o->op_ppaddr = PL_ppaddr[OP_PREINC];
79072805
LW
1016 break;
1017
1018 case OP_POSTDEC:
11343788 1019 o->op_type = OP_PREDEC; /* pre-decrement is faster */
22c35a8c 1020 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
79072805
LW
1021 break;
1022
79072805
LW
1023 case OP_OR:
1024 case OP_AND:
1025 case OP_COND_EXPR:
11343788 1026 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1027 scalarvoid(kid);
1028 break;
5aabfad6 1029
a0d0e21e 1030 case OP_NULL:
11343788 1031 if (o->op_flags & OPf_STACKED)
a0d0e21e 1032 break;
5aabfad6 1033 /* FALL THROUGH */
2ebea0a1
GS
1034 case OP_NEXTSTATE:
1035 case OP_DBSTATE:
79072805
LW
1036 case OP_ENTERTRY:
1037 case OP_ENTER:
1038 case OP_SCALAR:
11343788 1039 if (!(o->op_flags & OPf_KIDS))
79072805 1040 break;
54310121 1041 /* FALL THROUGH */
463ee0b2 1042 case OP_SCOPE:
79072805
LW
1043 case OP_LEAVE:
1044 case OP_LEAVETRY:
a0d0e21e 1045 case OP_LEAVELOOP:
79072805 1046 case OP_LINESEQ:
79072805 1047 case OP_LIST:
11343788 1048 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1049 scalarvoid(kid);
1050 break;
c90c0ff4 1051 case OP_ENTEREVAL:
5196be3e 1052 scalarkids(o);
c90c0ff4 1053 break;
5aabfad6 1054 case OP_REQUIRE:
c90c0ff4 1055 /* all requires must return a boolean value */
5196be3e
MB
1056 o->op_flags &= ~OPf_WANT;
1057 return scalar(o);
a0d0e21e 1058 case OP_SPLIT:
11343788 1059 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
1060 if (!kPMOP->op_pmreplroot)
1061 deprecate("implicit split to @_");
1062 }
1063 break;
79072805 1064 }
d008e5eb
GS
1065 if (useless) {
1066 dTHR;
1067 if (ckWARN(WARN_VOID))
cea2e8a9 1068 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
d008e5eb 1069 }
11343788 1070 return o;
79072805
LW
1071}
1072
1073OP *
864dbfa3 1074Perl_listkids(pTHX_ OP *o)
79072805
LW
1075{
1076 OP *kid;
11343788
MB
1077 if (o && o->op_flags & OPf_KIDS) {
1078 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1079 list(kid);
1080 }
11343788 1081 return o;
79072805
LW
1082}
1083
1084OP *
864dbfa3 1085Perl_list(pTHX_ OP *o)
79072805
LW
1086{
1087 OP *kid;
1088
a0d0e21e 1089 /* assumes no premature commitment */
3280af22 1090 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 1091 || o->op_type == OP_RETURN)
11343788 1092 return o;
79072805 1093
b162f9ea
IZ
1094 if ((o->op_private & OPpTARGET_MY)
1095 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1096 return o; /* As if inside SASSIGN */
1097
5dc0d613 1098 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
79072805 1099
11343788 1100 switch (o->op_type) {
79072805
LW
1101 case OP_FLOP:
1102 case OP_REPEAT:
11343788 1103 list(cBINOPo->op_first);
79072805
LW
1104 break;
1105 case OP_OR:
1106 case OP_AND:
1107 case OP_COND_EXPR:
11343788 1108 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1109 list(kid);
1110 break;
1111 default:
1112 case OP_MATCH:
8782bef2 1113 case OP_QR:
79072805
LW
1114 case OP_SUBST:
1115 case OP_NULL:
11343788 1116 if (!(o->op_flags & OPf_KIDS))
79072805 1117 break;
11343788
MB
1118 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1119 list(cBINOPo->op_first);
1120 return gen_constant_list(o);
79072805
LW
1121 }
1122 case OP_LIST:
11343788 1123 listkids(o);
79072805
LW
1124 break;
1125 case OP_LEAVE:
1126 case OP_LEAVETRY:
5dc0d613 1127 kid = cLISTOPo->op_first;
54310121
PP
1128 list(kid);
1129 while (kid = kid->op_sibling) {
1130 if (kid->op_sibling)
1131 scalarvoid(kid);
1132 else
1133 list(kid);
1134 }
3280af22 1135 WITH_THR(PL_curcop = &PL_compiling);
54310121 1136 break;
748a9306 1137 case OP_SCOPE:
79072805 1138 case OP_LINESEQ:
11343788 1139 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
1140 if (kid->op_sibling)
1141 scalarvoid(kid);
1142 else
1143 list(kid);
1144 }
3280af22 1145 WITH_THR(PL_curcop = &PL_compiling);
79072805 1146 break;
c90c0ff4
PP
1147 case OP_REQUIRE:
1148 /* all requires must return a boolean value */
5196be3e
MB
1149 o->op_flags &= ~OPf_WANT;
1150 return scalar(o);
79072805 1151 }
11343788 1152 return o;
79072805
LW
1153}
1154
1155OP *
864dbfa3 1156Perl_scalarseq(pTHX_ OP *o)
79072805
LW
1157{
1158 OP *kid;
1159
11343788
MB
1160 if (o) {
1161 if (o->op_type == OP_LINESEQ ||
1162 o->op_type == OP_SCOPE ||
1163 o->op_type == OP_LEAVE ||
1164 o->op_type == OP_LEAVETRY)
463ee0b2 1165 {
0f15f207 1166 dTHR;
11343788 1167 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
ed6116ce 1168 if (kid->op_sibling) {
463ee0b2 1169 scalarvoid(kid);
ed6116ce 1170 }
463ee0b2 1171 }
3280af22 1172 PL_curcop = &PL_compiling;
79072805 1173 }
11343788 1174 o->op_flags &= ~OPf_PARENS;
3280af22 1175 if (PL_hints & HINT_BLOCK_SCOPE)
11343788 1176 o->op_flags |= OPf_PARENS;
79072805 1177 }
8990e307 1178 else
11343788
MB
1179 o = newOP(OP_STUB, 0);
1180 return o;
79072805
LW
1181}
1182
76e3520e 1183STATIC OP *
cea2e8a9 1184S_modkids(pTHX_ OP *o, I32 type)
79072805
LW
1185{
1186 OP *kid;
11343788
MB
1187 if (o && o->op_flags & OPf_KIDS) {
1188 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2 1189 mod(kid, type);
79072805 1190 }
11343788 1191 return o;
79072805
LW
1192}
1193
79072805 1194OP *
864dbfa3 1195Perl_mod(pTHX_ OP *o, I32 type)
79072805 1196{
11343788 1197 dTHR;
79072805
LW
1198 OP *kid;
1199 SV *sv;
2d8e6c8d 1200 STRLEN n_a;
79072805 1201
3280af22 1202 if (!o || PL_error_count)
11343788 1203 return o;
79072805 1204
b162f9ea
IZ
1205 if ((o->op_private & OPpTARGET_MY)
1206 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1207 return o;
1208
11343788 1209 switch (o->op_type) {
68dc0745 1210 case OP_UNDEF:
3280af22 1211 PL_modcount++;
5dc0d613 1212 return o;
a0d0e21e 1213 case OP_CONST:
11343788 1214 if (!(o->op_private & (OPpCONST_ARYBASE)))
a0d0e21e 1215 goto nomod;
3280af22
NIS
1216 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1217 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1218 PL_eval_start = 0;
a0d0e21e
LW
1219 }
1220 else if (!type) {
3280af22
NIS
1221 SAVEI32(PL_compiling.cop_arybase);
1222 PL_compiling.cop_arybase = 0;
a0d0e21e
LW
1223 }
1224 else if (type == OP_REFGEN)
1225 goto nomod;
1226 else
cea2e8a9 1227 Perl_croak(aTHX_ "That use of $[ is unsupported");
a0d0e21e 1228 break;
5f05dabc 1229 case OP_STUB:
5196be3e 1230 if (o->op_flags & OPf_PARENS)
5f05dabc
PP
1231 break;
1232 goto nomod;
a0d0e21e
LW
1233 case OP_ENTERSUB:
1234 if ((type == OP_UNDEF || type == OP_REFGEN) &&
11343788
MB
1235 !(o->op_flags & OPf_STACKED)) {
1236 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1237 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1238 assert(cUNOPo->op_first->op_type == OP_NULL);
1239 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
79072805
LW
1240 break;
1241 }
1242 /* FALL THROUGH */
1243 default:
a0d0e21e
LW
1244 nomod:
1245 /* grep, foreach, subcalls, refgen */
1246 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1247 break;
cea2e8a9 1248 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
638bc118 1249 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
22c35a8c
GS
1250 ? "do block" : PL_op_desc[o->op_type]),
1251 type ? PL_op_desc[type] : "local"));
11343788 1252 return o;
79072805 1253
a0d0e21e
LW
1254 case OP_PREINC:
1255 case OP_PREDEC:
1256 case OP_POW:
1257 case OP_MULTIPLY:
1258 case OP_DIVIDE:
1259 case OP_MODULO:
1260 case OP_REPEAT:
1261 case OP_ADD:
1262 case OP_SUBTRACT:
1263 case OP_CONCAT:
1264 case OP_LEFT_SHIFT:
1265 case OP_RIGHT_SHIFT:
1266 case OP_BIT_AND:
1267 case OP_BIT_XOR:
1268 case OP_BIT_OR:
1269 case OP_I_MULTIPLY:
1270 case OP_I_DIVIDE:
1271 case OP_I_MODULO:
1272 case OP_I_ADD:
1273 case OP_I_SUBTRACT:
11343788 1274 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 1275 goto nomod;
3280af22 1276 PL_modcount++;
a0d0e21e
LW
1277 break;
1278
79072805 1279 case OP_COND_EXPR:
11343788 1280 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 1281 mod(kid, type);
79072805
LW
1282 break;
1283
1284 case OP_RV2AV:
1285 case OP_RV2HV:
93af7a87 1286 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1287 Perl_croak(aTHX_ "Can't localize through a reference");
11343788 1288 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3280af22 1289 PL_modcount = 10000;
11343788 1290 return o; /* Treat \(@foo) like ordinary list. */
748a9306
LW
1291 }
1292 /* FALL THROUGH */
79072805 1293 case OP_RV2GV:
5dc0d613 1294 if (scalar_mod_type(o, type))
3fe9a6f1 1295 goto nomod;
11343788 1296 ref(cUNOPo->op_first, o->op_type);
79072805
LW
1297 /* FALL THROUGH */
1298 case OP_AASSIGN:
1299 case OP_ASLICE:
1300 case OP_HSLICE:
93a17b20
LW
1301 case OP_NEXTSTATE:
1302 case OP_DBSTATE:
a0d0e21e
LW
1303 case OP_REFGEN:
1304 case OP_CHOMP:
3280af22 1305 PL_modcount = 10000;
79072805 1306 break;
463ee0b2 1307 case OP_RV2SV:
11343788 1308 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1309 Perl_croak(aTHX_ "Can't localize through a reference");
aeea060c 1310 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1311 /* FALL THROUGH */
79072805 1312 case OP_GV:
463ee0b2 1313 case OP_AV2ARYLEN:
3280af22 1314 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1315 case OP_SASSIGN:
bf4b1e52
GS
1316 case OP_ANDASSIGN:
1317 case OP_ORASSIGN:
8990e307 1318 case OP_AELEMFAST:
3280af22 1319 PL_modcount++;
8990e307
LW
1320 break;
1321
748a9306
LW
1322 case OP_PADAV:
1323 case OP_PADHV:
3280af22 1324 PL_modcount = 10000;
5196be3e
MB
1325 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1326 return o; /* Treat \(@foo) like ordinary list. */
1327 if (scalar_mod_type(o, type))
3fe9a6f1 1328 goto nomod;
748a9306
LW
1329 /* FALL THROUGH */
1330 case OP_PADSV:
3280af22 1331 PL_modcount++;
748a9306 1332 if (!type)
cea2e8a9 1333 Perl_croak(aTHX_ "Can't localize lexical variable %s",
2d8e6c8d 1334 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
463ee0b2
LW
1335 break;
1336
554b3eca 1337#ifdef USE_THREADS
2faa37cc 1338 case OP_THREADSV:
533c011a 1339 PL_modcount++; /* XXX ??? */
554b3eca
MB
1340 break;
1341#endif /* USE_THREADS */
1342
748a9306
LW
1343 case OP_PUSHMARK:
1344 break;
a0d0e21e 1345
69969c6f
SB
1346 case OP_KEYS:
1347 if (type != OP_SASSIGN)
1348 goto nomod;
5d82c453
GA
1349 goto lvalue_func;
1350 case OP_SUBSTR:
1351 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1352 goto nomod;
5f05dabc 1353 /* FALL THROUGH */
a0d0e21e 1354 case OP_POS:
463ee0b2 1355 case OP_VEC:
5d82c453 1356 lvalue_func:
11343788
MB
1357 pad_free(o->op_targ);
1358 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
5dc0d613 1359 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
11343788
MB
1360 if (o->op_flags & OPf_KIDS)
1361 mod(cBINOPo->op_first->op_sibling, type);
463ee0b2 1362 break;
a0d0e21e 1363
463ee0b2
LW
1364 case OP_AELEM:
1365 case OP_HELEM:
11343788 1366 ref(cBINOPo->op_first, o->op_type);
68dc0745 1367 if (type == OP_ENTERSUB &&
5dc0d613
MB
1368 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1369 o->op_private |= OPpLVAL_DEFER;
3280af22 1370 PL_modcount++;
463ee0b2
LW
1371 break;
1372
1373 case OP_SCOPE:
1374 case OP_LEAVE:
1375 case OP_ENTER:
11343788
MB
1376 if (o->op_flags & OPf_KIDS)
1377 mod(cLISTOPo->op_last, type);
a0d0e21e
LW
1378 break;
1379
1380 case OP_NULL:
638bc118
GS
1381 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1382 goto nomod;
1383 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 1384 break;
11343788
MB
1385 if (o->op_targ != OP_LIST) {
1386 mod(cBINOPo->op_first, type);
a0d0e21e
LW
1387 break;
1388 }
1389 /* FALL THROUGH */
463ee0b2 1390 case OP_LIST:
11343788 1391 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1392 mod(kid, type);
1393 break;
1394 }
11343788 1395 o->op_flags |= OPf_MOD;
a0d0e21e
LW
1396
1397 if (type == OP_AASSIGN || type == OP_SASSIGN)
11343788 1398 o->op_flags |= OPf_SPECIAL|OPf_REF;
a0d0e21e 1399 else if (!type) {
11343788
MB
1400 o->op_private |= OPpLVAL_INTRO;
1401 o->op_flags &= ~OPf_SPECIAL;
3280af22 1402 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1403 }
a0d0e21e 1404 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
11343788
MB
1405 o->op_flags |= OPf_REF;
1406 return o;
463ee0b2
LW
1407}
1408
864dbfa3 1409STATIC bool
cea2e8a9 1410S_scalar_mod_type(pTHX_ OP *o, I32 type)
3fe9a6f1
PP
1411{
1412 switch (type) {
1413 case OP_SASSIGN:
5196be3e 1414 if (o->op_type == OP_RV2GV)
3fe9a6f1
PP
1415 return FALSE;
1416 /* FALL THROUGH */
1417 case OP_PREINC:
1418 case OP_PREDEC:
1419 case OP_POSTINC:
1420 case OP_POSTDEC:
1421 case OP_I_PREINC:
1422 case OP_I_PREDEC:
1423 case OP_I_POSTINC:
1424 case OP_I_POSTDEC:
1425 case OP_POW:
1426 case OP_MULTIPLY:
1427 case OP_DIVIDE:
1428 case OP_MODULO:
1429 case OP_REPEAT:
1430 case OP_ADD:
1431 case OP_SUBTRACT:
1432 case OP_I_MULTIPLY:
1433 case OP_I_DIVIDE:
1434 case OP_I_MODULO:
1435 case OP_I_ADD:
1436 case OP_I_SUBTRACT:
1437 case OP_LEFT_SHIFT:
1438 case OP_RIGHT_SHIFT:
1439 case OP_BIT_AND:
1440 case OP_BIT_XOR:
1441 case OP_BIT_OR:
1442 case OP_CONCAT:
1443 case OP_SUBST:
1444 case OP_TRANS:
49e9fbe6
GS
1445 case OP_READ:
1446 case OP_SYSREAD:
1447 case OP_RECV:
bf4b1e52
GS
1448 case OP_ANDASSIGN:
1449 case OP_ORASSIGN:
3fe9a6f1
PP
1450 return TRUE;
1451 default:
1452 return FALSE;
1453 }
1454}
1455
35cd451c 1456STATIC bool
cea2e8a9 1457S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
35cd451c
GS
1458{
1459 switch (o->op_type) {
1460 case OP_PIPE_OP:
1461 case OP_SOCKPAIR:
1462 if (argnum == 2)
1463 return TRUE;
1464 /* FALL THROUGH */
1465 case OP_SYSOPEN:
1466 case OP_OPEN:
ded8aa31 1467 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
1468 case OP_SOCKET:
1469 case OP_OPEN_DIR:
1470 case OP_ACCEPT:
1471 if (argnum == 1)
1472 return TRUE;
1473 /* FALL THROUGH */
1474 default:
1475 return FALSE;
1476 }
1477}
1478
463ee0b2 1479OP *
864dbfa3 1480Perl_refkids(pTHX_ OP *o, I32 type)
463ee0b2
LW
1481{
1482 OP *kid;
11343788
MB
1483 if (o && o->op_flags & OPf_KIDS) {
1484 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1485 ref(kid, type);
1486 }
11343788 1487 return o;
463ee0b2
LW
1488}
1489
1490OP *
864dbfa3 1491Perl_ref(pTHX_ OP *o, I32 type)
463ee0b2
LW
1492{
1493 OP *kid;
463ee0b2 1494
3280af22 1495 if (!o || PL_error_count)
11343788 1496 return o;
463ee0b2 1497
11343788 1498 switch (o->op_type) {
a0d0e21e 1499 case OP_ENTERSUB:
e55aaa0e 1500 if ((type == OP_DEFINED || type == OP_LOCK) &&
11343788
MB
1501 !(o->op_flags & OPf_STACKED)) {
1502 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1503 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1504 assert(cUNOPo->op_first->op_type == OP_NULL);
1505 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1506 o->op_flags |= OPf_SPECIAL;
8990e307
LW
1507 }
1508 break;
aeea060c 1509
463ee0b2 1510 case OP_COND_EXPR:
11343788 1511 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2
LW
1512 ref(kid, type);
1513 break;
8990e307 1514 case OP_RV2SV:
35cd451c
GS
1515 if (type == OP_DEFINED)
1516 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1517 ref(cUNOPo->op_first, o->op_type);
4633a7c4
LW
1518 /* FALL THROUGH */
1519 case OP_PADSV:
5f05dabc 1520 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1521 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1522 : type == OP_RV2HV ? OPpDEREF_HV
1523 : OPpDEREF_SV);
11343788 1524 o->op_flags |= OPf_MOD;
a0d0e21e 1525 }
8990e307
LW
1526 break;
1527
2faa37cc 1528 case OP_THREADSV:
a863c7d1
MB
1529 o->op_flags |= OPf_MOD; /* XXX ??? */
1530 break;
1531
463ee0b2
LW
1532 case OP_RV2AV:
1533 case OP_RV2HV:
aeea060c 1534 o->op_flags |= OPf_REF;
8990e307 1535 /* FALL THROUGH */
463ee0b2 1536 case OP_RV2GV:
35cd451c
GS
1537 if (type == OP_DEFINED)
1538 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1539 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1540 break;
8990e307 1541
463ee0b2
LW
1542 case OP_PADAV:
1543 case OP_PADHV:
aeea060c 1544 o->op_flags |= OPf_REF;
79072805 1545 break;
aeea060c 1546
8990e307 1547 case OP_SCALAR:
79072805 1548 case OP_NULL:
11343788 1549 if (!(o->op_flags & OPf_KIDS))
463ee0b2 1550 break;
11343788 1551 ref(cBINOPo->op_first, type);
79072805
LW
1552 break;
1553 case OP_AELEM:
1554 case OP_HELEM:
11343788 1555 ref(cBINOPo->op_first, o->op_type);
5f05dabc 1556 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1557 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1558 : type == OP_RV2HV ? OPpDEREF_HV
1559 : OPpDEREF_SV);
11343788 1560 o->op_flags |= OPf_MOD;
8990e307 1561 }
79072805
LW
1562 break;
1563
463ee0b2 1564 case OP_SCOPE:
79072805
LW
1565 case OP_LEAVE:
1566 case OP_ENTER:
8990e307 1567 case OP_LIST:
11343788 1568 if (!(o->op_flags & OPf_KIDS))
79072805 1569 break;
11343788 1570 ref(cLISTOPo->op_last, type);
79072805 1571 break;
a0d0e21e
LW
1572 default:
1573 break;
79072805 1574 }
11343788 1575 return scalar(o);
8990e307 1576
79072805
LW
1577}
1578
1579OP *
864dbfa3 1580Perl_my(pTHX_ OP *o)
93a17b20
LW
1581{
1582 OP *kid;
93a17b20
LW
1583 I32 type;
1584
3280af22 1585 if (!o || PL_error_count)
11343788 1586 return o;
93a17b20 1587
11343788 1588 type = o->op_type;
93a17b20 1589 if (type == OP_LIST) {
11343788 1590 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
93a17b20 1591 my(kid);
dab48698 1592 } else if (type == OP_UNDEF) {
7766148a 1593 return o;
dab48698 1594 } else if (type != OP_PADSV &&
93a17b20
LW
1595 type != OP_PADAV &&
1596 type != OP_PADHV &&
1597 type != OP_PUSHMARK)
1598 {
cea2e8a9 1599 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
11343788 1600 return o;
93a17b20 1601 }
11343788
MB
1602 o->op_flags |= OPf_MOD;
1603 o->op_private |= OPpLVAL_INTRO;
1604 return o;
93a17b20
LW
1605}
1606
1607OP *
864dbfa3 1608Perl_sawparens(pTHX_ OP *o)
79072805
LW
1609{
1610 if (o)
1611 o->op_flags |= OPf_PARENS;
1612 return o;
1613}
1614
1615OP *
864dbfa3 1616Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 1617{
d008e5eb 1618 dTHR;
11343788 1619 OP *o;
79072805 1620
599cee73
PM
1621 if (ckWARN(WARN_UNSAFE) &&
1622 (left->op_type == OP_RV2AV ||
1623 left->op_type == OP_RV2HV ||
1624 left->op_type == OP_PADAV ||
1625 left->op_type == OP_PADHV)) {
22c35a8c 1626 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
1627 right->op_type == OP_TRANS)
1628 ? right->op_type : OP_MATCH];
1629 char *sample = ((left->op_type == OP_RV2AV ||
1630 left->op_type == OP_PADAV)
1631 ? "@array" : "%hash");
cea2e8a9 1632 Perl_warner(aTHX_ WARN_UNSAFE,
599cee73
PM
1633 "Applying %s to %s will act on scalar(%s)",
1634 desc, sample, sample);
2ae324a7
PP
1635 }
1636
79072805
LW
1637 if (right->op_type == OP_MATCH ||
1638 right->op_type == OP_SUBST ||
1639 right->op_type == OP_TRANS) {
1640 right->op_flags |= OPf_STACKED;
1641 if (right->op_type != OP_MATCH)
463ee0b2 1642 left = mod(left, right->op_type);
79072805 1643 if (right->op_type == OP_TRANS)
11343788 1644 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 1645 else
11343788 1646 o = prepend_elem(right->op_type, scalar(left), right);
79072805 1647 if (type == OP_NOT)
11343788
MB
1648 return newUNOP(OP_NOT, 0, scalar(o));
1649 return o;
79072805
LW
1650 }
1651 else
1652 return bind_match(type, left,
1653 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1654}
1655
1656OP *
864dbfa3 1657Perl_invert(pTHX_ OP *o)
79072805 1658{
11343788
MB
1659 if (!o)
1660 return o;
79072805 1661 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 1662 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
1663}
1664
1665OP *
864dbfa3 1666Perl_scope(pTHX_ OP *o)
79072805
LW
1667{
1668 if (o) {
3280af22 1669 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
1670 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1671 o->op_type = OP_LEAVE;
22c35a8c 1672 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
1673 }
1674 else {
1675 if (o->op_type == OP_LINESEQ) {
1676 OP *kid;
1677 o->op_type = OP_SCOPE;
22c35a8c 1678 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
463ee0b2 1679 kid = ((LISTOP*)o)->op_first;
748a9306 1680 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
7399586d
HS
1681 kid->op_type = OP_SETSTATE;
1682 kid->op_ppaddr = PL_ppaddr[OP_SETSTATE];
748a9306 1683 }
463ee0b2
LW
1684 }
1685 else
748a9306 1686 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 1687 }
79072805
LW
1688 }
1689 return o;
1690}
1691
b3ac6de7 1692void
864dbfa3 1693Perl_save_hints(pTHX)
b3ac6de7 1694{
3280af22
NIS
1695 SAVEI32(PL_hints);
1696 SAVESPTR(GvHV(PL_hintgv));
1697 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1698 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
1699}
1700
a0d0e21e 1701int
864dbfa3 1702Perl_block_start(pTHX_ int full)
79072805 1703{
11343788 1704 dTHR;
3280af22 1705 int retval = PL_savestack_ix;
b3ac6de7 1706
3280af22 1707 SAVEI32(PL_comppad_name_floor);
55497cff 1708 if (full) {
3280af22
NIS
1709 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1710 PL_comppad_name_floor = PL_comppad_name_fill;
55497cff 1711 else
3280af22
NIS
1712 PL_comppad_name_floor = 0;
1713 }
1714 SAVEI32(PL_min_intro_pending);
1715 SAVEI32(PL_max_intro_pending);
1716 PL_min_intro_pending = 0;
1717 SAVEI32(PL_comppad_name_fill);
1718 SAVEI32(PL_padix_floor);
1719 PL_padix_floor = PL_padix;
1720 PL_pad_reset_pending = FALSE;
b3ac6de7 1721 SAVEHINTS();
3280af22 1722 PL_hints &= ~HINT_BLOCK_SCOPE;
e24b16f9 1723 SAVEPPTR(PL_compiling.cop_warnings);
0453d815 1724 if (! specialWARN(PL_compiling.cop_warnings)) {
599cee73
PM
1725 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1726 SAVEFREESV(PL_compiling.cop_warnings) ;
1727 }
1728
1729
a0d0e21e
LW
1730 return retval;
1731}
1732
1733OP*
864dbfa3 1734Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 1735{
11343788 1736 dTHR;
3280af22 1737 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 1738 OP* retval = scalarseq(seq);
a0d0e21e 1739 LEAVE_SCOPE(floor);
3280af22 1740 PL_pad_reset_pending = FALSE;
e24b16f9 1741 PL_compiling.op_private = PL_hints;
a0d0e21e 1742 if (needblockscope)
3280af22
NIS
1743 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1744 pad_leavemy(PL_comppad_name_fill);
1745 PL_cop_seqmax++;
a0d0e21e
LW
1746 return retval;
1747}
1748
76e3520e 1749STATIC OP *
cea2e8a9 1750S_newDEFSVOP(pTHX)
54b9620d
MB
1751{
1752#ifdef USE_THREADS
1753 OP *o = newOP(OP_THREADSV, 0);
1754 o->op_targ = find_threadsv("_");
1755 return o;
1756#else
3280af22 1757 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
1758#endif /* USE_THREADS */
1759}
1760
a0d0e21e 1761void
864dbfa3 1762Perl_newPROG(pTHX_ OP *o)
a0d0e21e 1763{
11343788 1764 dTHR;
3280af22 1765 if (PL_in_eval) {
b295d113
TH
1766 if (PL_eval_root)
1767 return;
faef0170
HS
1768 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1769 ((PL_in_eval & EVAL_KEEPERR)
1770 ? OPf_SPECIAL : 0), o);
3280af22
NIS
1771 PL_eval_start = linklist(PL_eval_root);
1772 PL_eval_root->op_next = 0;
1773 peep(PL_eval_start);
a0d0e21e
LW
1774 }
1775 else {
5dc0d613 1776 if (!o)
a0d0e21e 1777 return;
3280af22
NIS
1778 PL_main_root = scope(sawparens(scalarvoid(o)));
1779 PL_curcop = &PL_compiling;
1780 PL_main_start = LINKLIST(PL_main_root);
1781 PL_main_root->op_next = 0;
1782 peep(PL_main_start);
1783 PL_compcv = 0;
3841441e 1784
4fdae800 1785 /* Register with debugger */
84902520 1786 if (PERLDB_INTER) {
864dbfa3 1787 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
1788 if (cv) {
1789 dSP;
924508f0 1790 PUSHMARK(SP);
3280af22 1791 XPUSHs((SV*)PL_compiling.cop_filegv);
3841441e 1792 PUTBACK;
864dbfa3 1793 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
1794 }
1795 }
79072805 1796 }
79072805
LW
1797}
1798
1799OP *
864dbfa3 1800Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
1801{
1802 if (o->op_flags & OPf_PARENS)
1803 list(o);
8990e307 1804 else {
d008e5eb 1805 dTHR;
599cee73 1806 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 1807 char *s;
834a4ddd 1808 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 1809 if (*s == ';' || *s == '=')
cea2e8a9 1810 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
599cee73 1811 lex ? "my" : "local");
8990e307
LW
1812 }
1813 }
3280af22
NIS
1814 PL_in_my = FALSE;
1815 PL_in_my_stash = Nullhv;
93a17b20
LW
1816 if (lex)
1817 return my(o);
1818 else
463ee0b2 1819 return mod(o, OP_NULL); /* a bit kludgey */
79072805
LW
1820}
1821
1822OP *
864dbfa3 1823Perl_jmaybe(pTHX_ OP *o)
79072805
LW
1824{
1825 if (o->op_type == OP_LIST) {
554b3eca
MB
1826 OP *o2;
1827#ifdef USE_THREADS
2faa37cc 1828 o2 = newOP(OP_THREADSV, 0);
54b9620d 1829 o2->op_targ = find_threadsv(";");
554b3eca
MB
1830#else
1831 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1832#endif /* USE_THREADS */
1833 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
1834 }
1835 return o;
1836}
1837
1838OP *
864dbfa3 1839Perl_fold_constants(pTHX_ register OP *o)
79072805 1840{
11343788 1841 dTHR;
79072805
LW
1842 register OP *curop;
1843 I32 type = o->op_type;
748a9306 1844 SV *sv;
79072805 1845
22c35a8c 1846 if (PL_opargs[type] & OA_RETSCALAR)
79072805 1847 scalar(o);
b162f9ea 1848 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
ed6116ce 1849 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 1850
eac055e9
GS
1851 /* integerize op, unless it happens to be C<-foo>.
1852 * XXX should pp_i_negate() do magic string negation instead? */
1853 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
1854 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
1855 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
1856 {
22c35a8c 1857 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
eac055e9 1858 }
85e6fe83 1859
22c35a8c 1860 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
1861 goto nope;
1862
de939608 1863 switch (type) {
7a52d87a
GS
1864 case OP_NEGATE:
1865 /* XXX might want a ck_negate() for this */
1866 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
1867 break;
de939608
CS
1868 case OP_SPRINTF:
1869 case OP_UCFIRST:
1870 case OP_LCFIRST:
1871 case OP_UC:
1872 case OP_LC:
69dcf70c
MB
1873 case OP_SLT:
1874 case OP_SGT:
1875 case OP_SLE:
1876 case OP_SGE:
1877 case OP_SCMP:
1878
de939608
CS
1879 if (o->op_private & OPpLOCALE)
1880 goto nope;
1881 }
1882
3280af22 1883 if (PL_error_count)
a0d0e21e
LW
1884 goto nope; /* Don't try to run w/ errors */
1885
79072805 1886 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
11fa937b
GS
1887 if ((curop->op_type != OP_CONST ||
1888 (curop->op_private & OPpCONST_BARE)) &&
7a52d87a
GS
1889 curop->op_type != OP_LIST &&
1890 curop->op_type != OP_SCALAR &&
1891 curop->op_type != OP_NULL &&
1892 curop->op_type != OP_PUSHMARK)
1893 {
79072805
LW
1894 goto nope;
1895 }
1896 }
1897
1898 curop = LINKLIST(o);
1899 o->op_next = 0;
533c011a 1900 PL_op = curop;
cea2e8a9 1901 CALLRUNOPS(aTHX);
3280af22 1902 sv = *(PL_stack_sp--);
748a9306 1903 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 1904 pad_swipe(o->op_targ);
748a9306
LW
1905 else if (SvTEMP(sv)) { /* grab mortal temp? */
1906 (void)SvREFCNT_inc(sv);
1907 SvTEMP_off(sv);
85e6fe83 1908 }
79072805
LW
1909 op_free(o);
1910 if (type == OP_RV2GV)
b1cb66bf 1911 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 1912 else {
ee580363
GS
1913 /* try to smush double to int, but don't smush -2.0 to -2 */
1914 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1915 type != OP_NEGATE)
1916 {
748a9306 1917 IV iv = SvIV(sv);
65202027 1918 if ((NV)iv == SvNV(sv)) {
748a9306
LW
1919 SvREFCNT_dec(sv);
1920 sv = newSViv(iv);
1921 }
b1cb66bf
PP
1922 else
1923 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
1924 }
1925 return newSVOP(OP_CONST, 0, sv);
1926 }
aeea060c 1927
79072805 1928 nope:
22c35a8c 1929 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 1930 return o;
79072805 1931
3280af22 1932 if (!(PL_hints & HINT_INTEGER)) {
a0d0e21e 1933 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
85e6fe83
LW
1934 return o;
1935
1936 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1937 if (curop->op_type == OP_CONST) {
b1cb66bf 1938 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
1939 continue;
1940 return o;
1941 }
22c35a8c 1942 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
1943 continue;
1944 return o;
1945 }
22c35a8c 1946 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
1947 }
1948
79072805
LW
1949 return o;
1950}
1951
1952OP *
864dbfa3 1953Perl_gen_constant_list(pTHX_ register OP *o)
79072805 1954{
11343788 1955 dTHR;
79072805 1956 register OP *curop;
3280af22 1957 I32 oldtmps_floor = PL_tmps_floor;
79072805 1958
a0d0e21e 1959 list(o);
3280af22 1960 if (PL_error_count)
a0d0e21e
LW
1961 return o; /* Don't attempt to run with errors */
1962
533c011a 1963 PL_op = curop = LINKLIST(o);
a0d0e21e 1964 o->op_next = 0;
cea2e8a9
GS
1965 pp_pushmark();
1966 CALLRUNOPS(aTHX);
533c011a 1967 PL_op = curop;
cea2e8a9 1968 pp_anonlist();
3280af22 1969 PL_tmps_floor = oldtmps_floor;
79072805
LW
1970
1971 o->op_type = OP_RV2AV;
22c35a8c 1972 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 1973 curop = ((UNOP*)o)->op_first;
3280af22 1974 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 1975 op_free(curop);
79072805
LW
1976 linklist(o);
1977 return list(o);
1978}
1979
1980OP *
864dbfa3 1981Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805
LW
1982{
1983 OP *kid;
a0d0e21e 1984 OP *last = 0;
79072805 1985
11343788
MB
1986 if (!o || o->op_type != OP_LIST)
1987 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 1988 else
5dc0d613 1989 o->op_flags &= ~OPf_WANT;
79072805 1990
22c35a8c 1991 if (!(PL_opargs[type] & OA_MARK))
11343788 1992 null(cLISTOPo->op_first);
8990e307 1993
11343788 1994 o->op_type = type;
22c35a8c 1995 o->op_ppaddr = PL_ppaddr[type];
11343788 1996 o->op_flags |= flags;
79072805 1997
11343788
MB
1998 o = CHECKOP(type, o);
1999 if (o->op_type != type)
2000 return o;
79072805 2001
11343788 2002 if (cLISTOPo->op_children < 7) {
79072805 2003 /* XXX do we really need to do this if we're done appending?? */
11343788 2004 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805 2005 last = kid;
11343788 2006 cLISTOPo->op_last = last; /* in case check substituted last arg */
79072805
LW
2007 }
2008
11343788 2009 return fold_constants(o);
79072805
LW
2010}
2011
2012/* List constructors */
2013
2014OP *
864dbfa3 2015Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2016{
2017 if (!first)
2018 return last;
8990e307
LW
2019
2020 if (!last)
79072805 2021 return first;
8990e307 2022
a0d0e21e
LW
2023 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2024 return newLISTOP(type, 0, first, last);
79072805 2025
a0d0e21e
LW
2026 if (first->op_flags & OPf_KIDS)
2027 ((LISTOP*)first)->op_last->op_sibling = last;
2028 else {
2029 first->op_flags |= OPf_KIDS;
2030 ((LISTOP*)first)->op_first = last;
2031 }
2032 ((LISTOP*)first)->op_last = last;
2033 ((LISTOP*)first)->op_children++;
2034 return first;
79072805
LW
2035}
2036
2037OP *
864dbfa3 2038Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2039{
2040 if (!first)
2041 return (OP*)last;
8990e307
LW
2042
2043 if (!last)
79072805 2044 return (OP*)first;
8990e307
LW
2045
2046 if (first->op_type != type)
79072805 2047 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2048
2049 if (last->op_type != type)
79072805
LW
2050 return append_elem(type, (OP*)first, (OP*)last);
2051
2052 first->op_last->op_sibling = last->op_first;
2053 first->op_last = last->op_last;
2054 first->op_children += last->op_children;
2055 if (first->op_children)
acb74605 2056 first->op_flags |= OPf_KIDS;
b7dc083c
NIS
2057
2058#ifdef PL_OP_SLAB_ALLOC
2059#else
2060 Safefree(last);
2061#endif
79072805
LW
2062 return (OP*)first;
2063}
2064
2065OP *
864dbfa3 2066Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2067{
2068 if (!first)
2069 return last;
8990e307
LW
2070
2071 if (!last)
79072805 2072 return first;
8990e307
LW
2073
2074 if (last->op_type == type) {
2075 if (type == OP_LIST) { /* already a PUSHMARK there */
2076 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2077 ((LISTOP*)last)->op_first->op_sibling = first;
2078 }
2079 else {
2080 if (!(last->op_flags & OPf_KIDS)) {
2081 ((LISTOP*)last)->op_last = first;
2082 last->op_flags |= OPf_KIDS;
2083 }
2084 first->op_sibling = ((LISTOP*)last)->op_first;
2085 ((LISTOP*)last)->op_first = first;
79072805 2086 }
79072805
LW
2087 ((LISTOP*)last)->op_children++;
2088 return last;
2089 }
2090
2091 return newLISTOP(type, 0, first, last);
2092}
2093
2094/* Constructors */
2095
2096OP *
864dbfa3 2097Perl_newNULLLIST(pTHX)
79072805 2098{
8990e307
LW
2099 return newOP(OP_STUB, 0);
2100}
2101
2102OP *
864dbfa3 2103Perl_force_list(pTHX_ OP *o)
8990e307 2104{
11343788
MB
2105 if (!o || o->op_type != OP_LIST)
2106 o = newLISTOP(OP_LIST, 0, o, Nullop);
2107 null(o);
2108 return o;
79072805
LW
2109}
2110
2111OP *
864dbfa3 2112Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2113{
2114 LISTOP *listop;
2115
b7dc083c 2116 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2117
2118 listop->op_type = type;
22c35a8c 2119 listop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2120 listop->op_children = (first != 0) + (last != 0);
2121 listop->op_flags = flags;
79072805
LW
2122
2123 if (!last && first)
2124 last = first;
2125 else if (!first && last)
2126 first = last;
8990e307
LW
2127 else if (first)
2128 first->op_sibling = last;
79072805
LW
2129 listop->op_first = first;
2130 listop->op_last = last;
8990e307
LW
2131 if (type == OP_LIST) {
2132 OP* pushop;
2133 pushop = newOP(OP_PUSHMARK, 0);
2134 pushop->op_sibling = first;
2135 listop->op_first = pushop;
2136 listop->op_flags |= OPf_KIDS;
2137 if (!last)
2138 listop->op_last = pushop;
2139 }
2140 else if (listop->op_children)
2141 listop->op_flags |= OPf_KIDS;
79072805
LW
2142
2143 return (OP*)listop;
2144}
2145
2146OP *
864dbfa3 2147Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2148{
11343788 2149 OP *o;
b7dc083c 2150 NewOp(1101, o, 1, OP);
11343788 2151 o->op_type = type;
22c35a8c 2152 o->op_ppaddr = PL_ppaddr[type];
11343788 2153 o->op_flags = flags;
79072805 2154
11343788
MB
2155 o->op_next = o;
2156 o->op_private = 0 + (flags >> 8);
22c35a8c 2157 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2158 scalar(o);
22c35a8c 2159 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2160 o->op_targ = pad_alloc(type, SVs_PADTMP);
2161 return CHECKOP(type, o);
79072805
LW
2162}
2163
2164OP *
864dbfa3 2165Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2166{
2167 UNOP *unop;
2168
93a17b20 2169 if (!first)
aeea060c 2170 first = newOP(OP_STUB, 0);
22c35a8c 2171 if (PL_opargs[type] & OA_MARK)
8990e307 2172 first = force_list(first);
93a17b20 2173
b7dc083c 2174 NewOp(1101, unop, 1, UNOP);
79072805 2175 unop->op_type = type;
22c35a8c 2176 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2177 unop->op_first = first;
2178 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2179 unop->op_private = 1 | (flags >> 8);
e50aee73 2180 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2181 if (unop->op_next)
2182 return (OP*)unop;
2183
a0d0e21e 2184 return fold_constants((OP *) unop);
79072805
LW
2185}
2186
2187OP *
864dbfa3 2188Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2189{
2190 BINOP *binop;
b7dc083c 2191 NewOp(1101, binop, 1, BINOP);
79072805
LW
2192
2193 if (!first)
2194 first = newOP(OP_NULL, 0);
2195
2196 binop->op_type = type;
22c35a8c 2197 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2198 binop->op_first = first;
2199 binop->op_flags = flags | OPf_KIDS;
2200 if (!last) {
2201 last = first;
c07a80fd 2202 binop->op_private = 1 | (flags >> 8);
79072805
LW
2203 }
2204 else {
c07a80fd 2205 binop->op_private = 2 | (flags >> 8);
79072805
LW
2206 first->op_sibling = last;
2207 }
2208
e50aee73 2209 binop = (BINOP*)CHECKOP(type, binop);
b162f9ea 2210 if (binop->op_next || binop->op_type != type)
79072805
LW
2211 return (OP*)binop;
2212
7284ab6f 2213 binop->op_last = binop->op_first->op_sibling;
79072805 2214
a0d0e21e 2215 return fold_constants((OP *)binop);
79072805
LW
2216}
2217
a0ed51b3
LW
2218static int
2219utf8compare(const void *a, const void *b)
2220{
2221 int i;
2222 for (i = 0; i < 10; i++) {
2223 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2224 return -1;
2225 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2226 return 1;
2227 }
2228 return 0;
2229}
2230
79072805 2231OP *
864dbfa3 2232Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2233{
79072805
LW
2234 SV *tstr = ((SVOP*)expr)->op_sv;
2235 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2236 STRLEN tlen;
2237 STRLEN rlen;
ec49126f
PP
2238 register U8 *t = (U8*)SvPV(tstr, tlen);
2239 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2240 register I32 i;
2241 register I32 j;
a0ed51b3 2242 I32 del;
79072805 2243 I32 complement;
5d06d08e 2244 I32 squash;
79072805
LW
2245 register short *tbl;
2246
11343788 2247 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2248 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2249 squash = o->op_private & OPpTRANS_SQUASH;
79072805 2250
a0ed51b3 2251 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2252 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2253 SV* transv = 0;
2254 U8* tend = t + tlen;
2255 U8* rend = r + rlen;
2256 I32 ulen;
2257 U32 tfirst = 1;
2258 U32 tlast = 0;
2259 I32 tdiff;
2260 U32 rfirst = 1;
2261 U32 rlast = 0;
2262 I32 rdiff;
2263 I32 diff;
2264 I32 none = 0;
2265 U32 max = 0;
2266 I32 bits;
2267 I32 grows = 0;
2268 I32 havefinal = 0;
2269 U32 final;
2270 HV *hv;
2271 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2272 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2273
2274 if (complement) {
2275 U8 tmpbuf[10];
2276 U8** cp;
2277 UV nextmin = 0;
2278 New(1109, cp, tlen, U8*);
2279 i = 0;
79cb57f6 2280 transv = newSVpvn("",0);
a0ed51b3
LW
2281 while (t < tend) {
2282 cp[i++] = t;
2283 t += UTF8SKIP(t);
2284 if (*t == 0xff) {
2285 t++;
2286 t += UTF8SKIP(t);
2287 }
2288 }
2289 qsort(cp, i, sizeof(U8*), utf8compare);
2290 for (j = 0; j < i; j++) {
2291 U8 *s = cp[j];
2292 UV val = utf8_to_uv(s, &ulen);
2293 s += ulen;
2294 diff = val - nextmin;
2295 if (diff > 0) {
2296 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2297 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2298 if (diff > 1) {
2299 t = uv_to_utf8(tmpbuf, val - 1);
2300 sv_catpvn(transv, "\377", 1);
dfe13c55 2301 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2302 }
2303 }
2304 if (*s == 0xff)
2305 val = utf8_to_uv(s+1, &ulen);
2306 if (val >= nextmin)
2307 nextmin = val + 1;
2308 }
2309 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2310 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2311 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2312 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2313 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2314 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2315 tlen = SvCUR(transv);
2316 tend = t + tlen;
2317 }
2318 else if (!rlen && !del) {
2319 r = t; rlen = tlen; rend = tend;
4757a243
LW
2320 }
2321 if (!squash) {
2322 if (to_utf && from_utf) { /* only counting characters */
2323 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2324 o->op_private |= OPpTRANS_IDENTICAL;
2325 }
2326 else { /* straight latin-1 translation */
2327 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2328 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2329 o->op_private |= OPpTRANS_IDENTICAL;
2330 }
a0ed51b3
LW
2331 }
2332
2333 while (t < tend || tfirst <= tlast) {
2334 /* see if we need more "t" chars */
2335 if (tfirst > tlast) {
2336 tfirst = (I32)utf8_to_uv(t, &ulen);
2337 t += ulen;
2338 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2339 tlast = (I32)utf8_to_uv(++t, &ulen);
2340 t += ulen;
2341 }
2342 else
2343 tlast = tfirst;
2344 }
2345
2346 /* now see if we need more "r" chars */
2347 if (rfirst > rlast) {
2348 if (r < rend) {
2349 rfirst = (I32)utf8_to_uv(r, &ulen);
2350 r += ulen;
2351 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2352 rlast = (I32)utf8_to_uv(++r, &ulen);
2353 r += ulen;
2354 }
2355 else
2356 rlast = rfirst;
2357 }
2358 else {
2359 if (!havefinal++)
2360 final = rlast;
2361 rfirst = rlast = 0xffffffff;
2362 }
2363 }
2364
2365 /* now see which range will peter our first, if either. */
2366 tdiff = tlast - tfirst;
2367 rdiff = rlast - rfirst;
2368
2369 if (tdiff <= rdiff)
2370 diff = tdiff;
2371 else
2372 diff = rdiff;
2373
2374 if (rfirst == 0xffffffff) {
2375 diff = tdiff; /* oops, pretend rdiff is infinite */
2376 if (diff > 0)
cea2e8a9 2377 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
a0ed51b3 2378 else
cea2e8a9 2379 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
a0ed51b3
LW
2380 }
2381 else {
2382 if (diff > 0)
cea2e8a9 2383 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
a0ed51b3 2384 else
cea2e8a9 2385 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
a0ed51b3
LW
2386
2387 if (rfirst + diff > max)
2388 max = rfirst + diff;
2389 rfirst += diff + 1;
2390 if (!grows) {
2391 if (rfirst <= 0x80)
2392 ;
2393 else if (rfirst <= 0x800)
2394 grows |= (tfirst < 0x80);
2395 else if (rfirst <= 0x10000)
2396 grows |= (tfirst < 0x800);
2397 else if (rfirst <= 0x200000)
2398 grows |= (tfirst < 0x10000);
2399 else if (rfirst <= 0x4000000)
2400 grows |= (tfirst < 0x200000);
2401 else if (rfirst <= 0x80000000)
2402 grows |= (tfirst < 0x4000000);
2403 }
2404 }
2405 tfirst += diff + 1;
2406 }
2407
2408 none = ++max;
2409 if (del)
2410 del = ++max;
2411
2412 if (max > 0xffff)
2413 bits = 32;
2414 else if (max > 0xff)
2415 bits = 16;
2416 else
2417 bits = 8;
2418
2419 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2420 SvREFCNT_dec(listsv);
2421 if (transv)
2422 SvREFCNT_dec(transv);
2423
2424 if (!del && havefinal)
2425 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2426
2427 if (grows && to_utf)
2428 o->op_private |= OPpTRANS_GROWS;
2429
2430 op_free(expr);
2431 op_free(repl);
2432 return o;
2433 }
2434
2435 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2436 if (complement) {
2437 Zero(tbl, 256, short);
2438 for (i = 0; i < tlen; i++)
ec49126f 2439 tbl[t[i]] = -1;
79072805
LW
2440 for (i = 0, j = 0; i < 256; i++) {
2441 if (!tbl[i]) {
2442 if (j >= rlen) {
a0ed51b3 2443 if (del)
79072805
LW
2444 tbl[i] = -2;
2445 else if (rlen)
ec49126f 2446 tbl[i] = r[j-1];
79072805
LW
2447 else
2448 tbl[i] = i;
2449 }
2450 else
ec49126f 2451 tbl[i] = r[j++];
79072805
LW
2452 }
2453 }
2454 }
2455 else {
a0ed51b3 2456 if (!rlen && !del) {
79072805 2457 r = t; rlen = tlen;
5d06d08e 2458 if (!squash)
4757a243 2459 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2460 }
2461 for (i = 0; i < 256; i++)
2462 tbl[i] = -1;
2463 for (i = 0, j = 0; i < tlen; i++,j++) {
2464 if (j >= rlen) {
a0ed51b3 2465 if (del) {
ec49126f
PP
2466 if (tbl[t[i]] == -1)
2467 tbl[t[i]] = -2;
79072805
LW
2468 continue;
2469 }
2470 --j;
2471 }
ec49126f
PP
2472 if (tbl[t[i]] == -1)
2473 tbl[t[i]] = r[j];
79072805
LW
2474 }
2475 }
2476 op_free(expr);
2477 op_free(repl);
2478
11343788 2479 return o;
79072805
LW
2480}
2481
2482OP *
864dbfa3 2483Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805 2484{
11343788 2485 dTHR;
79072805
LW
2486 PMOP *pmop;
2487
b7dc083c 2488 NewOp(1101, pmop, 1, PMOP);
79072805 2489 pmop->op_type = type;
22c35a8c 2490 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2491 pmop->op_flags = flags;
c07a80fd 2492 pmop->op_private = 0 | (flags >> 8);
79072805 2493
3280af22 2494 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2495 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2496 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2497 pmop->op_pmpermflags |= PMf_LOCALE;
2498 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2499
79072805 2500 /* link into pm list */
3280af22
NIS
2501 if (type != OP_TRANS && PL_curstash) {
2502 pmop->op_pmnext = HvPMROOT(PL_curstash);
2503 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2504 }
2505
2506 return (OP*)pmop;
2507}
2508
2509OP *
864dbfa3 2510Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2511{
5c0ca799 2512 dTHR;
79072805
LW
2513 PMOP *pm;
2514 LOGOP *rcop;
ce862d02 2515 I32 repl_has_vars = 0;
79072805 2516
11343788
MB
2517 if (o->op_type == OP_TRANS)
2518 return pmtrans(o, expr, repl);
79072805 2519
3280af22 2520 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2521 pm = (PMOP*)o;
79072805
LW
2522
2523 if (expr->op_type == OP_CONST) {
463ee0b2 2524 STRLEN plen;
79072805 2525 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2526 char *p = SvPV(pat, plen);
11343788 2527 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2528 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2529 p = SvPV(pat, plen);
79072805
LW
2530 pm->op_pmflags |= PMf_SKIPWHITE;
2531 }
cea2e8a9 2532 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
aeea060c 2533 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2534 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2535 op_free(expr);
2536 }
2537 else {
3280af22
NIS
2538 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2539 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2540 ? OP_REGCRESET
2541 : OP_REGCMAYBE),0,expr);
463ee0b2 2542
b7dc083c 2543 NewOp(1101, rcop, 1, LOGOP);
79072805 2544 rcop->op_type = OP_REGCOMP;
22c35a8c 2545 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2546 rcop->op_first = scalar(expr);
3280af22 2547 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2548 ? (OPf_SPECIAL | OPf_KIDS)
2549 : OPf_KIDS);
79072805 2550 rcop->op_private = 1;
11343788 2551 rcop->op_other = o;
79072805
LW
2552
2553 /* establish postfix order */
3280af22 2554 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2555 LINKLIST(expr);
2556 rcop->op_next = expr;
2557 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2558 }
2559 else {
2560 rcop->op_next = LINKLIST(expr);
2561 expr->op_next = (OP*)rcop;
2562 }
79072805 2563
11343788 2564 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
2565 }
2566
2567 if (repl) {
748a9306 2568 OP *curop;
0244c3a4 2569 if (pm->op_pmflags & PMf_EVAL) {
748a9306 2570 curop = 0;
0244c3a4
GS
2571 if (PL_curcop->cop_line < PL_multi_end)
2572 PL_curcop->cop_line = PL_multi_end;
2573 }
554b3eca 2574#ifdef USE_THREADS
2faa37cc 2575 else if (repl->op_type == OP_THREADSV
554b3eca 2576 && strchr("&`'123456789+",
533c011a 2577 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
2578 {
2579 curop = 0;
2580 }
2581#endif /* USE_THREADS */
748a9306
LW
2582 else if (repl->op_type == OP_CONST)
2583 curop = repl;
79072805 2584 else {
79072805
LW
2585 OP *lastop = 0;
2586 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 2587 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 2588#ifdef USE_THREADS
ce862d02
IZ
2589 if (curop->op_type == OP_THREADSV) {
2590 repl_has_vars = 1;
be949f6f 2591 if (strchr("&`'123456789+", curop->op_private))
ce862d02 2592 break;
554b3eca
MB
2593 }
2594#else
79072805
LW
2595 if (curop->op_type == OP_GV) {
2596 GV *gv = ((GVOP*)curop)->op_gv;
ce862d02 2597 repl_has_vars = 1;
93a17b20 2598 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2599 break;
2600 }
554b3eca 2601#endif /* USE_THREADS */
79072805
LW
2602 else if (curop->op_type == OP_RV2CV)
2603 break;
2604 else if (curop->op_type == OP_RV2SV ||
2605 curop->op_type == OP_RV2AV ||
2606 curop->op_type == OP_RV2HV ||
2607 curop->op_type == OP_RV2GV) {
2608 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2609 break;
2610 }
748a9306
LW
2611 else if (curop->op_type == OP_PADSV ||
2612 curop->op_type == OP_PADAV ||
2613 curop->op_type == OP_PADHV ||
554b3eca 2614 curop->op_type == OP_PADANY) {
ce862d02 2615 repl_has_vars = 1;
748a9306 2616 }
1167e5da
SM
2617 else if (curop->op_type == OP_PUSHRE)
2618 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
2619 else
2620 break;
2621 }
2622 lastop = curop;
2623 }
748a9306 2624 }
ce862d02
IZ
2625 if (curop == repl
2626 && !(repl_has_vars
2627 && (!pm->op_pmregexp
2628 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 2629 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 2630 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 2631 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
2632 }
2633 else {
ce862d02
IZ
2634 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2635 pm->op_pmflags |= PMf_MAYBE_CONST;
2636 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2637 }
b7dc083c 2638 NewOp(1101, rcop, 1, LOGOP);
748a9306 2639 rcop->op_type = OP_SUBSTCONT;
22c35a8c 2640 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
2641 rcop->op_first = scalar(repl);
2642 rcop->op_flags |= OPf_KIDS;
2643 rcop->op_private = 1;
11343788 2644 rcop->op_other = o;
748a9306
LW
2645
2646 /* establish postfix order */
2647 rcop->op_next = LINKLIST(repl);
2648 repl->op_next = (OP*)rcop;
2649
2650 pm->op_pmreplroot = scalar((OP*)rcop);
2651 pm->op_pmreplstart = LINKLIST(rcop);
2652 rcop->op_next = 0;
79072805
LW
2653 }
2654 }
2655
2656 return (OP*)pm;
2657}
2658
2659OP *
864dbfa3 2660Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
2661{
2662 SVOP *svop;
b7dc083c 2663 NewOp(1101, svop, 1, SVOP);
79072805 2664 svop->op_type = type;
22c35a8c 2665 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2666 svop->op_sv = sv;
2667 svop->op_next = (OP*)svop;
2668 svop->op_flags = flags;
22c35a8c 2669 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2670 scalar((OP*)svop);
22c35a8c 2671 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2672 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2673 return CHECKOP(type, svop);
79072805
LW
2674}
2675
2676OP *
864dbfa3 2677Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 2678{
11343788 2679 dTHR;
79072805 2680 GVOP *gvop;
b7dc083c 2681 NewOp(1101, gvop, 1, GVOP);
79072805 2682 gvop->op_type = type;
22c35a8c 2683 gvop->op_ppaddr = PL_ppaddr[type];
8990e307 2684 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
79072805
LW
2685 gvop->op_next = (OP*)gvop;
2686 gvop->op_flags = flags;
22c35a8c 2687 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2688 scalar((OP*)gvop);
22c35a8c 2689 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2690 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2691 return CHECKOP(type, gvop);
79072805
LW
2692}
2693
2694OP *
864dbfa3 2695Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
2696{
2697 PVOP *pvop;
b7dc083c 2698 NewOp(1101, pvop, 1, PVOP);
79072805 2699 pvop->op_type = type;
22c35a8c 2700 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2701 pvop->op_pv = pv;
2702 pvop->op_next = (OP*)pvop;
2703 pvop->op_flags = flags;
22c35a8c 2704 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2705 scalar((OP*)pvop);
22c35a8c 2706 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2707 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2708 return CHECKOP(type, pvop);
79072805
LW
2709}
2710
79072805 2711void
864dbfa3 2712Perl_package(pTHX_ OP *o)
79072805 2713{
11343788 2714 dTHR;
93a17b20 2715 SV *sv;
79072805 2716
3280af22
NIS
2717 save_hptr(&PL_curstash);
2718 save_item(PL_curstname);
11343788 2719 if (o) {
463ee0b2
LW
2720 STRLEN len;
2721 char *name;
11343788 2722 sv = cSVOPo->op_sv;
463ee0b2 2723 name = SvPV(sv, len);
3280af22
NIS
2724 PL_curstash = gv_stashpvn(name,len,TRUE);
2725 sv_setpvn(PL_curstname, name, len);
11343788 2726 op_free(o);
93a17b20
LW
2727 }
2728 else {
3280af22
NIS
2729 sv_setpv(PL_curstname,"<none>");
2730 PL_curstash = Nullhv;
93a17b20 2731 }
7ad382f4 2732 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
2733 PL_copline = NOLINE;
2734 PL_expect = XSTATE;
79072805
LW
2735}
2736
85e6fe83 2737void
864dbfa3 2738Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 2739{
a0d0e21e
LW
2740 OP *pack;
2741 OP *meth;
2742 OP *rqop;
2743 OP *imop;
b1cb66bf 2744 OP *veop;
78ca652e 2745 GV *gv;
85e6fe83 2746
a0d0e21e 2747 if (id->op_type != OP_CONST)
cea2e8a9 2748 Perl_croak(aTHX_ "Module name must be constant");
85e6fe83 2749
b1cb66bf
PP
2750 veop = Nullop;
2751
2752 if(version != Nullop) {
2753 SV *vesv = ((SVOP*)version)->op_sv;
2754
2755 if (arg == Nullop && !SvNIOK(vesv)) {
2756 arg = version;
2757 }
2758 else {
2759 OP *pack;
b1cb66bf
PP
2760
2761 if (version->op_type != OP_CONST || !SvNIOK(vesv))
cea2e8a9 2762 Perl_croak(aTHX_ "Version number must be constant number");
b1cb66bf
PP
2763
2764 /* Make copy of id so we don't free it twice */
2765 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2766
2767 /* Fake up a method call to VERSION */
b1cb66bf
PP
2768 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2769 append_elem(OP_LIST,
2770 prepend_elem(OP_LIST, pack, list(version)),
f5d5a27c
CS
2771 newSVOP(OP_METHOD_NAMED, 0,
2772 newSVpvn("VERSION", 7))));
b1cb66bf
PP
2773 }
2774 }
aeea060c 2775
a0d0e21e 2776 /* Fake up an import/unimport */
4633a7c4
LW
2777 if (arg && arg->op_type == OP_STUB)
2778 imop = arg; /* no import on explicit () */
b1cb66bf
PP
2779 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2780 imop = Nullop; /* use 5.0; */
2781 }
4633a7c4
LW
2782 else {
2783 /* Make copy of id so we don't free it twice */
2784 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
4633a7c4 2785 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
a0d0e21e
LW
2786 append_elem(OP_LIST,
2787 prepend_elem(OP_LIST, pack, list(arg)),
f5d5a27c
CS
2788 newSVOP(OP_METHOD_NAMED, 0,
2789 aver ? newSVpvn("import", 6)
2790 : newSVpvn("unimport", 8))));
4633a7c4
LW
2791 }
2792
78ca652e
GS
2793 /* Fake up a require, handle override, if any */
2794 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2795 if (!(gv && GvIMPORTED_CV(gv)))
2796 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2797
2798 if (gv && GvIMPORTED_CV(gv)) {
2799 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2800 append_elem(OP_LIST, id,
2801 scalar(newUNOP(OP_RV2CV, 0,
2802 newGVOP(OP_GV, 0,
2803 gv))))));
2804 }
2805 else {
2806 rqop = newUNOP(OP_REQUIRE, 0, id);
2807 }
a0d0e21e
LW
2808
2809 /* Fake up the BEGIN {}, which does its thing immediately. */
c07a80fd 2810 newSUB(floor,
79cb57f6 2811 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 2812 Nullop,
a0d0e21e 2813 append_elem(OP_LINESEQ,
b1cb66bf
PP
2814 append_elem(OP_LINESEQ,
2815 newSTATEOP(0, Nullch, rqop),
2816 newSTATEOP(0, Nullch, veop)),
a0d0e21e 2817 newSTATEOP(0, Nullch, imop) ));
85e6fe83 2818
3280af22
NIS
2819 PL_copline = NOLINE;
2820 PL_expect = XSTATE;
85e6fe83
LW
2821}
2822
79072805 2823OP *
864dbfa3 2824Perl_dofile(pTHX_ OP *term)
78ca652e
GS
2825{
2826 OP *doop;
2827 GV *gv;
2828
2829 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2830 if (!(gv && GvIMPORTED_CV(gv)))
2831 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2832
2833 if (gv && GvIMPORTED_CV(gv)) {
2834 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2835 append_elem(OP_LIST, term,
2836 scalar(newUNOP(OP_RV2CV, 0,
2837 newGVOP(OP_GV, 0,
2838 gv))))));
2839 }
2840 else {
2841 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2842 }
2843 return doop;
2844}
2845
2846OP *
864dbfa3 2847Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
2848{
2849 return newBINOP(OP_LSLICE, flags,
8990e307
LW
2850 list(force_list(subscript)),
2851 list(force_list(listval)) );
79072805
LW
2852}
2853
76e3520e 2854STATIC I32
cea2e8a9 2855S_list_assignment(pTHX_ register OP *o)
79072805 2856{
11343788 2857 if (!o)
79072805
LW
2858 return TRUE;
2859
11343788
MB
2860 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2861 o = cUNOPo->op_first;
79072805 2862
11343788 2863 if (o->op_type == OP_COND_EXPR) {
1a67a97c
SM
2864 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
2865 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
79072805
LW
2866
2867 if (t && f)
2868 return TRUE;
2869 if (t || f)
2870 yyerror("Assignment to both a list and a scalar");
2871 return FALSE;
2872 }
2873
11343788
MB
2874 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2875 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2876 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
2877 return TRUE;
2878
11343788 2879 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
2880 return TRUE;
2881
11343788 2882 if (o->op_type == OP_RV2SV)
79072805
LW
2883 return FALSE;
2884
2885 return FALSE;
2886}
2887
2888OP *
864dbfa3 2889Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 2890{
11343788 2891 OP *o;
79072805 2892
a0d0e21e
LW
2893 if (optype) {
2894 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2895 return newLOGOP(optype, 0,
2896 mod(scalar(left), optype),
2897 newUNOP(OP_SASSIGN, 0, scalar(right)));
2898 }
2899 else {
2900 return newBINOP(optype, OPf_STACKED,
2901 mod(scalar(left), optype), scalar(right));
2902 }
2903 }
2904
79072805 2905 if (list_assignment(left)) {
6ee623d5 2906 dTHR;
3280af22
NIS
2907 PL_modcount = 0;
2908 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 2909 left = mod(left, OP_AASSIGN);
3280af22
NIS
2910 if (PL_eval_start)
2911 PL_eval_start = 0;
748a9306 2912 else {
a0d0e21e
LW
2913 op_free(left);
2914 op_free(right);
2915 return Nullop;
2916 }
11343788 2917 o = newBINOP(OP_AASSIGN, flags,
8990e307
LW
2918 list(force_list(right)),
2919 list(force_list(left)) );
11343788 2920 o->op_private = 0 | (flags >> 8);
a0d0e21e 2921 if (!(left->op_private & OPpLVAL_INTRO)) {
79072805 2922 OP *curop;
11343788 2923 OP *lastop = o;
3280af22 2924 PL_generation++;
11343788 2925 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 2926 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805
LW
2927 if (curop->op_type == OP_GV) {
2928 GV *gv = ((GVOP*)curop)->op_gv;
3280af22 2929 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 2930 break;
3280af22 2931 SvCUR(gv) = PL_generation;
79072805 2932 }
748a9306
LW
2933 else if (curop->op_type == OP_PADSV ||
2934 curop->op_type == OP_PADAV ||
2935 curop->op_type == OP_PADHV ||
2936 curop->op_type == OP_PADANY) {
3280af22 2937 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 2938 SV *sv = svp[curop->op_targ];
3280af22 2939 if (SvCUR(sv) == PL_generation)
748a9306 2940 break;
3280af22 2941 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 2942 }
79072805
LW
2943 else if (curop->op_type == OP_RV2CV)
2944 break;
2945 else if (curop->op_type == OP_RV2SV ||
2946 curop->op_type == OP_RV2AV ||
2947 curop->op_type == OP_RV2HV ||
2948 curop->op_type == OP_RV2GV) {
2949 if (lastop->op_type != OP_GV) /* funny deref? */
2950 break;
2951 }
1167e5da
SM
2952 else if (curop->op_type == OP_PUSHRE) {
2953 if (((PMOP*)curop)->op_pmreplroot) {
2954 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3280af22 2955 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 2956 break;
3280af22 2957 SvCUR(gv) = PL_generation;
1167e5da
SM
2958 }
2959 }
79072805
LW
2960 else
2961 break;
2962 }
2963 lastop = curop;
2964 }
11343788
MB
2965 if (curop != o)
2966 o->op_private = OPpASSIGN_COMMON;
79072805 2967 }
c07a80fd
PP
2968 if (right && right->op_type == OP_SPLIT) {
2969 OP* tmpop;
2970 if ((tmpop = ((LISTOP*)right)->op_first) &&
2971 tmpop->op_type == OP_PUSHRE)
2972 {
2973 PMOP *pm = (PMOP*)tmpop;
2974 if (left->op_type == OP_RV2AV &&
2975 !(left->op_private & OPpLVAL_INTRO) &&
11343788 2976 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd
PP
2977 {
2978 tmpop = ((UNOP*)left)->op_first;
2979 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2980 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2981 pm->op_pmflags |= PMf_ONCE;
11343788 2982 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd
PP
2983 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2984 tmpop->op_sibling = Nullop; /* don't free split */
2985 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 2986 op_free(o); /* blow off assign */
54310121 2987 right->op_flags &= ~OPf_WANT;
a5f75d66 2988 /* "I don't know and I don't care." */
c07a80fd
PP
2989 return right;
2990 }
2991 }
2992 else {
3280af22 2993 if (PL_modcount < 10000 &&
c07a80fd
PP
2994 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2995 {
2996 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2997 if (SvIVX(sv) == 0)
3280af22 2998 sv_setiv(sv, PL_modcount+1);
c07a80fd
PP
2999 }
3000 }
3001 }
3002 }
11343788 3003 return o;
79072805
LW
3004 }
3005 if (!right)
3006 right = newOP(OP_UNDEF, 0);
3007 if (right->op_type == OP_READLINE) {
3008 right->op_flags |= OPf_STACKED;
463ee0b2 3009 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 3010 }
a0d0e21e 3011 else {
3280af22 3012 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 3013 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 3014 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
3015 if (PL_eval_start)
3016 PL_eval_start = 0;
748a9306 3017 else {
11343788 3018 op_free(o);
a0d0e21e
LW
3019 return Nullop;
3020 }
3021 }
11343788 3022 return o;
79072805
LW
3023}
3024
3025OP *
864dbfa3 3026Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
79072805 3027{
11343788 3028 dTHR;
bbce6d69 3029 U32 seq = intro_my();
79072805
LW
3030 register COP *cop;
3031
b7dc083c 3032 NewOp(1101, cop, 1, COP);
3280af22 3033 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
8990e307 3034 cop->op_type = OP_DBSTATE;
22c35a8c 3035 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
8990e307
LW
3036 }
3037 else {
3038 cop->op_type = OP_NEXTSTATE;
22c35a8c 3039 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
8990e307 3040 }
79072805 3041 cop->op_flags = flags;
a0ed51b3 3042 cop->op_private = (PL_hints & HINT_UTF8);
ff0cee69
PP
3043#ifdef NATIVE_HINTS
3044 cop->op_private |= NATIVE_HINTS;
3045#endif
e24b16f9 3046 PL_compiling.op_private = cop->op_private;
79072805
LW
3047 cop->op_next = (OP*)cop;
3048
463ee0b2
LW
3049 if (label) {
3050 cop->cop_label = label;
3280af22 3051 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 3052 }
bbce6d69 3053 cop->cop_seq = seq;
3280af22 3054 cop->cop_arybase = PL_curcop->cop_arybase;
0453d815 3055 if (specialWARN(PL_curcop->cop_warnings))
599cee73
PM
3056 cop->cop_warnings = PL_curcop->cop_warnings ;
3057 else
3058 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3059
79072805 3060
3280af22
NIS
3061 if (PL_copline == NOLINE)
3062 cop->cop_line = PL_curcop->cop_line;
79072805 3063 else {
3280af22
NIS
3064 cop->cop_line = PL_copline;
3065 PL_copline = NOLINE;
79072805 3066 }
3280af22
NIS
3067 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3068 cop->cop_stash = PL_curstash;
79072805 3069
3280af22
NIS
3070 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3071 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3072 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
a0d0e21e 3073 (void)SvIOK_on(*svp);
a5f75d66 3074 SvIVX(*svp) = 1;
93a17b20
LW
3075 SvSTASH(*svp) = (HV*)cop;
3076 }
3077 }
3078
11343788 3079 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
79072805
LW
3080}
3081
bbce6d69
PP
3082/* "Introduce" my variables to visible status. */
3083U32
864dbfa3 3084Perl_intro_my(pTHX)
bbce6d69
PP
3085{
3086 SV **svp;
3087 SV *sv;
3088 I32 i;
3089
3280af22
NIS
3090 if (! PL_min_intro_pending)
3091 return PL_cop_seqmax;
bbce6d69 3092
3280af22
NIS
3093 svp = AvARRAY(PL_comppad_name);
3094 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3095 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
c53d7c7d 3096 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
65202027 3097 SvNVX(sv) = (NV)PL_cop_seqmax;
bbce6d69
PP
3098 }
3099 }
3280af22
NIS
3100 PL_min_intro_pending = 0;
3101 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3102 return PL_cop_seqmax++;
bbce6d69
PP
3103}
3104
79072805 3105OP *
864dbfa3 3106Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
79072805 3107{
883ffac3
CS
3108 return new_logop(type, flags, &first, &other);
3109}
3110
3bd495df 3111STATIC OP *
cea2e8a9 3112S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
883ffac3 3113{
11343788 3114 dTHR;
79072805 3115 LOGOP *logop;
11343788 3116 OP *o;
883ffac3
CS
3117 OP *first = *firstp;
3118 OP *other = *otherp;
79072805 3119
a0d0e21e
LW
3120 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3121 return newBINOP(type, flags, scalar(first), scalar(other));
3122
8990e307 3123 scalarboolean(first);
79072805
LW
3124 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3125 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3126 if (type == OP_AND || type == OP_OR) {
3127 if (type == OP_AND)
3128 type = OP_OR;
3129 else
3130 type = OP_AND;
11343788 3131 o = first;
883ffac3 3132 first = *firstp = cUNOPo->op_first;
11343788
MB
3133 if (o->op_next)
3134 first->op_next = o->op_next;
3135 cUNOPo->op_first = Nullop;
3136 op_free(o);
79072805
LW
3137 }
3138 }
3139 if (first->op_type == OP_CONST) {
599cee73 3140 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
cea2e8a9 3141 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
22c35a8c 3142 PL_op_desc[type]);
79072805
LW
3143 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3144 op_free(first);
883ffac3 3145 *firstp = Nullop;
79072805
LW
3146 return other;
3147 }
3148 else {
3149 op_free(other);
883ffac3 3150 *otherp = Nullop;
79072805
LW
3151 return first;
3152 }
3153 }
3154 else if (first->op_type == OP_WANTARRAY) {
3155 if (type == OP_AND)
3156 list(other);
3157 else
3158 scalar(other);
3159 }
599cee73 3160 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
a6006777
PP
3161 OP *k1 = ((UNOP*)first)->op_first;
3162 OP *k2 = k1->op_sibling;
3163 OPCODE warnop = 0;
3164 switch (first->op_type)
3165 {
3166 case OP_NULL:
3167 if (k2 && k2->op_type == OP_READLINE
3168 && (k2->op_flags & OPf_STACKED)
55d729e4 3169 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
a6006777
PP
3170 warnop = k2->op_type;
3171 break;
3172
3173 case OP_SASSIGN:
68dc0745
PP
3174 if (k1->op_type == OP_READDIR
3175 || k1->op_type == OP_GLOB
3176 || k1->op_type == OP_EACH)
a6006777
PP
3177 warnop = k1->op_type;
3178 break;
3179 }
8ebc5c01 3180 if (warnop) {
3280af22
NIS
3181 line_t oldline = PL_curcop->cop_line;
3182 PL_curcop->cop_line = PL_copline;
cea2e8a9 3183 Perl_warner(aTHX_ WARN_UNSAFE,
599cee73 3184 "Value of %s%s can be \"0\"; test with defined()",
22c35a8c 3185 PL_op_desc[warnop],
68dc0745
PP
3186 ((warnop == OP_READLINE || warnop == OP_GLOB)
3187 ? " construct" : "() operator"));
3280af22 3188 PL_curcop->cop_line = oldline;
8ebc5c01 3189 }
a6006777 3190 }
79072805
LW
3191
3192 if (!other)
3193 return first;
3194
a0d0e21e
LW
3195 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3196 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3197
b7dc083c 3198 NewOp(1101, logop, 1, LOGOP);
79072805
LW
3199
3200 logop->op_type = type;
22c35a8c 3201 logop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3202 logop->op_first = first;
3203 logop->op_flags = flags | OPf_KIDS;
3204 logop->op_other = LINKLIST(other);
c07a80fd 3205 logop->op_private = 1 | (flags >> 8);
79072805
LW
3206
3207 /* establish postfix order */
3208 logop->op_next = LINKLIST(first);
3209 first->op_next = (OP*)logop;
3210 first->op_sibling = other;
3211
11343788
MB
3212 o = newUNOP(OP_NULL, 0, (OP*)logop);
3213 other->op_next = o;
79072805 3214
11343788 3215 return o;
79072805
LW
3216}
3217
3218OP *
864dbfa3 3219Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
79072805 3220{
11343788 3221 dTHR;
1a67a97c
SM
3222 LOGOP *logop;
3223 OP *start;
11343788 3224 OP *o;
79072805 3225
b1cb66bf
PP
3226 if (!falseop)
3227 return newLOGOP(OP_AND, 0, first, trueop);
3228 if (!trueop)
3229 return newLOGOP(OP_OR, 0, first, falseop);
79072805 3230
8990e307 3231 scalarboolean(first);
79072805
LW
3232 if (first->op_type == OP_CONST) {
3233 if (SvTRUE(((SVOP*)first)->op_sv)) {
3234 op_free(first);
b1cb66bf
PP
3235 op_free(falseop);
3236 return trueop;
79072805
LW
3237 }
3238 else {
3239 op_free(first);
b1cb66bf
PP
3240 op_free(trueop);
3241 return falseop;
79072805
LW
3242 }
3243 }
3244 else if (first->op_type == OP_WANTARRAY) {
b1cb66bf
PP
3245 list(trueop);
3246 scalar(falseop);
79072805 3247 }
1a67a97c
SM
3248 NewOp(1101, logop, 1, LOGOP);
3249 logop->op_type = OP_COND_EXPR;
3250 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3251 logop->op_first = first;
3252 logop->op_flags = flags | OPf_KIDS;
3253 logop->op_private = 1 | (flags >> 8);
3254 logop->op_other = LINKLIST(trueop);
3255 logop->op_next = LINKLIST(falseop);
79072805 3256
79072805
LW
3257
3258 /* establish postfix order */
1a67a97c
SM
3259 start = LINKLIST(first);
3260 first->op_next = (OP*)logop;
79072805 3261
b1cb66bf
PP
3262 first->op_sibling = trueop;
3263 trueop->op_sibling = falseop;
1a67a97c 3264 o = newUNOP(OP_NULL, 0, (OP*)logop);
79072805 3265
1a67a97c 3266 trueop->op_next = falseop->op_next = o;
79072805 3267
1a67a97c 3268 o->op_next = start;
11343788 3269 return o;
79072805
LW
3270}
3271
3272OP *
864dbfa3 3273Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
79072805 3274{
b35b2403 3275 dTHR;
1a67a97c 3276 LOGOP *range;
79072805
LW
3277 OP *flip;
3278 OP *flop;
1a67a97c 3279 OP *leftstart;
11343788 3280 OP *o;
79072805 3281
1a67a97c 3282 NewOp(1101, range, 1, LOGOP);
79072805 3283
1a67a97c
SM
3284 range->op_type = OP_RANGE;
3285 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3286 range->op_first = left;
3287 range->op_flags = OPf_KIDS;
3288 leftstart = LINKLIST(left);
3289 range->op_other = LINKLIST(right);
3290 range->op_private = 1 | (flags >> 8);
79072805
LW
3291
3292 left->op_sibling = right;
3293
1a67a97c
SM
3294 range->op_next = (OP*)range;
3295 flip = newUNOP(OP_FLIP, flags, (OP*)range);
79072805 3296 flop = newUNOP(OP_FLOP, 0, flip);
11343788 3297 o = newUNOP(OP_NULL, 0, flop);
79072805 3298 linklist(flop);
1a67a97c 3299 range->op_next = leftstart;
79072805
LW
3300
3301 left->op_next = flip;
3302 right->op_next = flop;
3303
1a67a97c
SM
3304 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3305 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
ed6116ce 3306 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805
LW
3307 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3308
3309 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3310 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3311
11343788 3312 flip->op_next = o;
79072805 3313 if (!flip->op_private || !flop->op_private)
11343788 3314 linklist(o); /* blow off optimizer unless constant */
79072805 3315
11343788 3316 return o;
79072805
LW
3317}
3318
3319OP *
864dbfa3 3320Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
79072805 3321{
11343788 3322 dTHR;
463ee0b2 3323 OP* listop;
11343788 3324 OP* o;
463ee0b2 3325 int once = block && block->op_flags & OPf_SPECIAL &&
a0d0e21e 3326 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
93a17b20 3327
463ee0b2
LW
3328 if (expr) {
3329 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3330 return block; /* do {} while 0 does once */
fb73857a
PP
3331 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3332 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
774d564b 3333 expr = newUNOP(OP_DEFINED, 0,
54b9620d 3334 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
55d729e4
GS
3335 } else if (expr->op_flags & OPf_KIDS) {
3336 OP *k1 = ((UNOP*)expr)->op_first;
3337 OP *k2 = (k1) ? k1->op_sibling : NULL;
3338 switch (expr->op_type) {
3339 case OP_NULL:
3340 if (k2 && k2->op_type == OP_READLINE
3341 && (k2->op_flags & OPf_STACKED)
3342 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3343 expr = newUNOP(OP_DEFINED, 0, expr);
3344 break;
3345
3346 case OP_SASSIGN:
3347 if (k1->op_type == OP_READDIR
3348 || k1->op_type == OP_GLOB
3349 || k1->op_type == OP_EACH)
3350 expr = newUNOP(OP_DEFINED, 0, expr);
3351 break;
3352 }
774d564b 3353 }
463ee0b2 3354 }
93a17b20 3355
8990e307 3356 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
883ffac3 3357 o = new_logop(OP_AND, 0, &expr, &listop);
463ee0b2 3358
883ffac3
CS
3359 if (listop)
3360 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
79072805 3361
11343788
MB
3362 if (once && o != listop)
3363 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
79072805 3364
11343788
MB
3365 if (o == listop)
3366 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
748a9306 3367
11343788
MB
3368 o->op_flags |= flags;
3369 o = scope(o);
3370 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3371 return o;
79072805
LW
3372}
3373
3374OP *
864dbfa3 3375Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
79072805 3376{
11343788 3377 dTHR;
79072805
LW
3378 OP *redo;
3379 OP *next = 0;
3380 OP *listop;
11343788 3381 OP *o;
79072805
LW
3382 OP *condop;
3383
fb73857a
PP
3384 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3385 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
748a9306 3386 expr = newUNOP(OP_DEFINED, 0,
54b9620d 3387 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
55d729e4
GS
3388 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3389 OP *k1 = ((UNOP*)expr)->op_first;
3390 OP *k2 = (k1) ? k1->op_sibling : NULL;
3391 switch (expr->op_type) {
3392 case OP_NULL:
3393 if (k2 && k2->op_type == OP_READLINE
3394 && (k2->op_flags & OPf_STACKED)
3395 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3396 expr = newUNOP(OP_DEFINED, 0, expr);
3397 break;
3398
3399 case OP_SASSIGN:
3400 if (k1->op_type == OP_READDIR
3401 || k1->op_type == OP_GLOB
3402 || k1->op_type == OP_EACH)
3403 expr = newUNOP(OP_DEFINED, 0, expr);
3404 break;
3405 }
748a9306 3406 }
79072805
LW
3407
3408 if (!block)
3409 block = newOP(OP_NULL, 0);
3410
3411 if (cont)
3412 next = LINKLIST(cont);
fb73857a 3413 if (expr) {
79072805 3414 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
fb73857a 3415 if ((line_t)whileline != NOLINE) {
3280af22 3416 PL_copline = whileline;
fb73857a
PP
3417 cont = append_elem(OP_LINESEQ, cont,
3418 newSTATEOP(0, Nullch, Nullop));
3419 }
3420 }
79072805 3421
463ee0b2 3422 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
79072805
LW
3423 redo = LINKLIST(listop);
3424
3425 if (expr) {
3280af22 3426 PL_copline = whileline;
883ffac3
CS
3427 scalar(listop);
3428 o = new_logop(OP_AND, 0, &expr, &listop);
11343788 3429 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
85e6fe83 3430 op_free(expr); /* oops, it's a while (0) */
463ee0b2 3431 op_free((OP*)loop);
883ffac3 3432 return Nullop; /* listop already freed by new_logop */
463ee0b2 3433 }
883ffac3
CS
3434 if (listop)
3435 ((LISTOP*)listop)->op_last->op_next = condop =
3436 (o == listop ? redo : LINKLIST(o));
79072805
LW
3437 if (!next)
3438 next = condop;
3439 }
3440 else
11343788 3441 o = listop;
79072805
LW
3442
3443 if (!loop) {
b7dc083c 3444 NewOp(1101,loop,1,LOOP);
79072805 3445 loop->op_type = OP_ENTERLOOP;
22c35a8c 3446 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
79072805
LW
3447 loop->op_private = 0;
3448 loop->op_next = (OP*)loop;
3449 }
3450
11343788 3451 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
79072805
LW
3452
3453 loop->op_redoop = redo;
11343788 3454 loop->op_lastop = o;
79072805
LW
3455
3456 if (next)
3457 loop->op_nextop = next;
3458 else
11343788 3459 loop->op_nextop = o;
79072805 3460
11343788
MB
3461 o->op_flags |= flags;
3462 o->op_private |= (flags >> 8);
3463 return o;
79072805
LW
3464}
3465
3466OP *
864dbfa3 3467Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
79072805
LW
3468{
3469 LOOP *loop;
b7dc083c 3470 LOOP *tmp;
fb73857a 3471 OP *wop;
85e6fe83 3472 int padoff = 0;
4633a7c4 3473 I32 iterflags = 0;
79072805 3474
79072805 3475 if (sv) {
85e6fe83 3476 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
748a9306 3477 sv->op_type = OP_RV2GV;
22c35a8c 3478 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
79072805 3479 }
85e6fe83
LW
3480 else if (sv->op_type == OP_PADSV) { /* private variable */
3481 padoff = sv->op_targ;
3482 op_free(sv);
3483 sv = Nullop;
3484 }
54b9620d
MB
3485 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3486 padoff = sv->op_targ;
3487 iterflags |= OPf_SPECIAL;
3488 op_free(sv);
3489 sv = Nullop;
3490 }
79072805 3491 else
cea2e8a9 3492 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
79072805
LW
3493 }
3494 else {
54b9620d
MB
3495#ifdef USE_THREADS
3496 padoff = find_threadsv("_");
3497 iterflags |= OPf_SPECIAL;
3498#else
3280af22 3499 sv = newGVOP(OP_GV, 0, PL_defgv);
54b9620d 3500#endif
79072805 3501 }
5f05dabc 3502 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
89ea2908 3503 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4633a7c4
LW
3504 iterflags |= OPf_STACKED;
3505 }
89ea2908
GA
3506 else if (expr->op_ty