xref: /openbsd-src/gnu/usr.bin/perl/ext/B/t/optree_samples.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!perl
2
3BEGIN {
4    unshift @INC, 't';
5    require Config;
6    if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7        print "1..0 # Skip -- Perl configured without B module\n";
8        exit 0;
9    }
10    if (!$Config::Config{useperlio}) {
11        print "1..0 # Skip -- need perlio to walk the optree\n";
12        exit 0;
13    }
14}
15use OptreeCheck;
16use Config;
17plan tests	=> 46;
18
19pass("GENERAL OPTREE EXAMPLES");
20
21pass("IF,THEN,ELSE, ?:");
22
23checkOptree ( name	=> '-basic sub {if shift print then,else}',
24	      bcopts	=> '-basic',
25	      code	=> sub { if (shift) { print "then" }
26				 else       { print "else" }
27			     },
28	      strip_open_hints => 1,
29	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
30# 7  <1> leavesub[1 ref] K/REFC,1 ->(end)
31# -     <@> lineseq KP ->7
32# 1        <;> nextstate(main 665 optree_samples.t:24) v:>,<,% ->2
33# -        <1> null K/1 ->-
34# 3           <|> cond_expr(other->4) K/1 ->8
35# 2              <0> shift s* ->3
36# -              <@> scope K ->-
37# -                 <0> ex-nextstate v ->4
38# 6                 <@> print sK ->7
39# 4                    <0> pushmark s ->5
40# 5                    <$> const[PV "then"] s ->6
41# d              <@> leave KP ->7
42# 8                 <0> enter ->9
43# 9                 <;> nextstate(main 663 optree_samples.t:25) v:>,<,% ->a
44# c                 <@> print sK ->d
45# a                    <0> pushmark s ->b
46# b                    <$> const[PV "else"] s ->c
47EOT_EOT
48# 7  <1> leavesub[1 ref] K/REFC,1 ->(end)
49# -     <@> lineseq KP ->7
50# 1        <;> nextstate(main 665 optree_samples.t:24) v:>,<,% ->2
51# -        <1> null K/1 ->-
52# 3           <|> cond_expr(other->4) K/1 ->8
53# 2              <0> shift s* ->3
54# -              <@> scope K ->-
55# -                 <0> ex-nextstate v ->4
56# 6                 <@> print sK ->7
57# 4                    <0> pushmark s ->5
58# 5                    <$> const(PV "then") s ->6
59# d              <@> leave KP ->7
60# 8                 <0> enter ->9
61# 9                 <;> nextstate(main 663 optree_samples.t:25) v:>,<,% ->a
62# c                 <@> print sK ->d
63# a                    <0> pushmark s ->b
64# b                    <$> const(PV "else") s ->c
65EONT_EONT
66
67checkOptree ( name	=> '-basic (see above, with my $a = shift)',
68	      bcopts	=> '-basic',
69	      code	=> sub { my $a = shift;
70				 if ($a) { print "foo" }
71				 else    { print "bar" }
72			     },
73	      strip_open_hints => 1,
74	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
75# b  <1> leavesub[1 ref] K/REFC,1 ->(end)
76# -     <@> lineseq KP ->b
77# 1        <;> nextstate(main 666 optree_samples.t:70) v:>,<,% ->2
78# 4        <2> sassign vKS/2 ->5
79# 2           <0> shift s* ->3
80# 3           <0> padsv[$a:666,670] sRM*/LVINTRO ->4
81# 5        <;> nextstate(main 670 optree_samples.t:71) v:>,<,% ->6
82# -        <1> null K/1 ->-
83# 7           <|> cond_expr(other->8) K/1 ->c
84# 6              <0> padsv[$a:666,670] s ->7
85# -              <@> scope K ->-
86# -                 <0> ex-nextstate v ->8
87# a                 <@> print sK ->b
88# 8                    <0> pushmark s ->9
89# 9                    <$> const[PV "foo"] s ->a
90# h              <@> leave KP ->b
91# c                 <0> enter ->d
92# d                 <;> nextstate(main 668 optree_samples.t:72) v:>,<,% ->e
93# g                 <@> print sK ->h
94# e                    <0> pushmark s ->f
95# f                    <$> const[PV "bar"] s ->g
96EOT_EOT
97# b  <1> leavesub[1 ref] K/REFC,1 ->(end)
98# -     <@> lineseq KP ->b
99# 1        <;> nextstate(main 666 optree_samples.t:72) v:>,<,% ->2
100# 4        <2> sassign vKS/2 ->5
101# 2           <0> shift s* ->3
102# 3           <0> padsv[$a:666,670] sRM*/LVINTRO ->4
103# 5        <;> nextstate(main 670 optree_samples.t:73) v:>,<,% ->6
104# -        <1> null K/1 ->-
105# 7           <|> cond_expr(other->8) K/1 ->c
106# 6              <0> padsv[$a:666,670] s ->7
107# -              <@> scope K ->-
108# -                 <0> ex-nextstate v ->8
109# a                 <@> print sK ->b
110# 8                    <0> pushmark s ->9
111# 9                    <$> const(PV "foo") s ->a
112# h              <@> leave KP ->b
113# c                 <0> enter ->d
114# d                 <;> nextstate(main 668 optree_samples.t:74) v:>,<,% ->e
115# g                 <@> print sK ->h
116# e                    <0> pushmark s ->f
117# f                    <$> const(PV "bar") s ->g
118EONT_EONT
119
120checkOptree ( name	=> '-exec sub {if shift print then,else}',
121	      bcopts	=> '-exec',
122	      code	=> sub { if (shift) { print "then" }
123				 else       { print "else" }
124			     },
125	      strip_open_hints => 1,
126	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
127# 1  <;> nextstate(main 674 optree_samples.t:125) v:>,<,%
128# 2  <0> shift s*
129# 3  <|> cond_expr(other->4) K/1
130# 4      <0> pushmark s
131# 5      <$> const[PV "then"] s
132# 6      <@> print sK
133#            goto 7
134# 8  <0> enter
135# 9  <;> nextstate(main 672 optree_samples.t:126) v:>,<,%
136# a  <0> pushmark s
137# b  <$> const[PV "else"] s
138# c  <@> print sK
139# d  <@> leave KP
140# 7  <1> leavesub[1 ref] K/REFC,1
141EOT_EOT
142# 1  <;> nextstate(main 674 optree_samples.t:129) v:>,<,%
143# 2  <0> shift s*
144# 3  <|> cond_expr(other->4) K/1
145# 4      <0> pushmark s
146# 5      <$> const(PV "then") s
147# 6      <@> print sK
148#            goto 7
149# 8  <0> enter
150# 9  <;> nextstate(main 672 optree_samples.t:130) v:>,<,%
151# a  <0> pushmark s
152# b  <$> const(PV "else") s
153# c  <@> print sK
154# d  <@> leave KP
155# 7  <1> leavesub[1 ref] K/REFC,1
156EONT_EONT
157
158checkOptree ( name	=> '-exec (see above, with my $a = shift)',
159	      bcopts	=> '-exec',
160	      code	=> sub { my $a = shift;
161				 if ($a) { print "foo" }
162				 else    { print "bar" }
163			     },
164	      strip_open_hints => 1,
165	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
166# 1  <;> nextstate(main 675 optree_samples.t:165) v:>,<,%
167# 2  <0> shift s*
168# 3  <0> padsv[$a:675,679] sRM*/LVINTRO
169# 4  <2> sassign vKS/2
170# 5  <;> nextstate(main 679 optree_samples.t:166) v:>,<,%
171# 6  <0> padsv[$a:675,679] s
172# 7  <|> cond_expr(other->8) K/1
173# 8      <0> pushmark s
174# 9      <$> const[PV "foo"] s
175# a      <@> print sK
176#            goto b
177# c  <0> enter
178# d  <;> nextstate(main 677 optree_samples.t:167) v:>,<,%
179# e  <0> pushmark s
180# f  <$> const[PV "bar"] s
181# g  <@> print sK
182# h  <@> leave KP
183# b  <1> leavesub[1 ref] K/REFC,1
184EOT_EOT
185# 1  <;> nextstate(main 675 optree_samples.t:171) v:>,<,%
186# 2  <0> shift s*
187# 3  <0> padsv[$a:675,679] sRM*/LVINTRO
188# 4  <2> sassign vKS/2
189# 5  <;> nextstate(main 679 optree_samples.t:172) v:>,<,%
190# 6  <0> padsv[$a:675,679] s
191# 7  <|> cond_expr(other->8) K/1
192# 8      <0> pushmark s
193# 9      <$> const(PV "foo") s
194# a      <@> print sK
195#            goto b
196# c  <0> enter
197# d  <;> nextstate(main 677 optree_samples.t:173) v:>,<,%
198# e  <0> pushmark s
199# f  <$> const(PV "bar") s
200# g  <@> print sK
201# h  <@> leave KP
202# b  <1> leavesub[1 ref] K/REFC,1
203EONT_EONT
204
205checkOptree ( name	=> '-exec sub { print (shift) ? "foo" : "bar" }',
206	      code	=> sub { print (shift) ? "foo" : "bar" },
207	      bcopts	=> '-exec',
208	      strip_open_hints => 1,
209	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
210# 1  <;> nextstate(main 680 optree_samples.t:213) v:>,<,%
211# 2  <0> pushmark s
212# 3  <0> shift s*
213# 4  <@> print sK
214# 5  <|> cond_expr(other->6) K/1
215# 6      <$> const[PV "foo"] s
216#            goto 7
217# 8  <$> const[PV "bar"] s
218# 7  <1> leavesub[1 ref] K/REFC,1
219EOT_EOT
220# 1  <;> nextstate(main 680 optree_samples.t:221) v:>,<,%
221# 2  <0> pushmark s
222# 3  <0> shift s*
223# 4  <@> print sK
224# 5  <|> cond_expr(other->6) K/1
225# 6      <$> const(PV "foo") s
226#            goto 7
227# 8  <$> const(PV "bar") s
228# 7  <1> leavesub[1 ref] K/REFC,1
229EONT_EONT
230
231pass ("FOREACH");
232
233checkOptree ( name	=> '-exec sub { foreach (1..10) {print "foo $_"} }',
234	      code	=> sub { foreach (1..10) {print "foo $_"} },
235	      bcopts	=> '-exec',
236	      strip_open_hints => 1,
237	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
238# 1  <;> nextstate(main 443 optree.t:158) v:>,<,%
239# 2  <0> pushmark s
240# 3  <$> const[IV 1] s
241# 4  <$> const[IV 10] s
242# 5  <#> gv[*_] s
243# 6  <{> enteriter(next->d last->g redo->7) lKS/8
244# e  <0> iter s
245# f  <|> and(other->7) K/1
246# 7      <;> nextstate(main 442 optree.t:158) v:>,<,%
247# 8      <0> pushmark s
248# 9      <$> const[PV "foo "] s
249# a      <#> gvsv[*_] s
250# b      <2> concat[t4] sK/2
251# c      <@> print vK
252# d      <0> unstack s
253#            goto e
254# g  <2> leaveloop K/2
255# h  <1> leavesub[1 ref] K/REFC,1
256EOT_EOT
257# 1  <;> nextstate(main 444 optree_samples.t:182) v:>,<,%
258# 2  <0> pushmark s
259# 3  <$> const(IV 1) s
260# 4  <$> const(IV 10) s
261# 5  <$> gv(*_) s
262# 6  <{> enteriter(next->d last->g redo->7) lKS/8
263# e  <0> iter s
264# f  <|> and(other->7) K/1
265# 7      <;> nextstate(main 443 optree_samples.t:182) v:>,<,%
266# 8      <0> pushmark s
267# 9      <$> const(PV "foo ") s
268# a      <$> gvsv(*_) s
269# b      <2> concat[t3] sK/2
270# c      <@> print vK
271# d      <0> unstack s
272#            goto e
273# g  <2> leaveloop K/2
274# h  <1> leavesub[1 ref] K/REFC,1
275EONT_EONT
276
277checkOptree ( name	=> '-basic sub { print "foo $_" foreach (1..10) }',
278	      code	=> sub { print "foo $_" foreach (1..10) },
279	      bcopts	=> '-basic',
280	      strip_open_hints => 1,
281	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
282# g  <1> leavesub[1 ref] K/REFC,1 ->(end)
283# -     <@> lineseq KP ->g
284# 1        <;> nextstate(main 445 optree.t:167) v:>,<,% ->2
285# f        <2> leaveloop K/2 ->g
286# 6           <{> enteriter(next->c last->f redo->7) lKS/8 ->d
287# -              <0> ex-pushmark s ->2
288# -              <1> ex-list lK ->5
289# 2                 <0> pushmark s ->3
290# 3                 <$> const[IV 1] s ->4
291# 4                 <$> const[IV 10] s ->5
292# 5              <#> gv[*_] s ->6
293# -           <1> null K/1 ->f
294# e              <|> and(other->7) K/1 ->f
295# d                 <0> iter s ->e
296# -                 <@> lineseq sK ->-
297# b                    <@> print vK ->c
298# 7                       <0> pushmark s ->8
299# -                       <1> ex-stringify sK/1 ->b
300# -                          <0> ex-pushmark s ->8
301# a                          <2> concat[t2] sK/2 ->b
302# 8                             <$> const[PV "foo "] s ->9
303# -                             <1> ex-rv2sv sK/1 ->a
304# 9                                <#> gvsv[*_] s ->a
305# c                    <0> unstack s ->d
306EOT_EOT
307# g  <1> leavesub[1 ref] K/REFC,1 ->(end)
308# -     <@> lineseq KP ->g
309# 1        <;> nextstate(main 446 optree_samples.t:192) v:>,<,% ->2
310# f        <2> leaveloop K/2 ->g
311# 6           <{> enteriter(next->c last->f redo->7) lKS/8 ->d
312# -              <0> ex-pushmark s ->2
313# -              <1> ex-list lK ->5
314# 2                 <0> pushmark s ->3
315# 3                 <$> const(IV 1) s ->4
316# 4                 <$> const(IV 10) s ->5
317# 5              <$> gv(*_) s ->6
318# -           <1> null K/1 ->f
319# e              <|> and(other->7) K/1 ->f
320# d                 <0> iter s ->e
321# -                 <@> lineseq sK ->-
322# b                    <@> print vK ->c
323# 7                       <0> pushmark s ->8
324# -                       <1> ex-stringify sK/1 ->b
325# -                          <0> ex-pushmark s ->8
326# a                          <2> concat[t1] sK/2 ->b
327# 8                             <$> const(PV "foo ") s ->9
328# -                             <1> ex-rv2sv sK/1 ->a
329# 9                                <$> gvsv(*_) s ->a
330# c                    <0> unstack s ->d
331EONT_EONT
332
333checkOptree ( name	=> '-exec -e foreach (1..10) {print qq{foo $_}}',
334	      prog	=> 'foreach (1..10) {print qq{foo $_}}',
335	      bcopts	=> '-exec',
336	      strip_open_hints => 1,
337	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
338# 1  <0> enter
339# 2  <;> nextstate(main 2 -e:1) v:>,<,%,{
340# 3  <0> pushmark s
341# 4  <$> const[IV 1] s
342# 5  <$> const[IV 10] s
343# 6  <#> gv[*_] s
344# 7  <{> enteriter(next->e last->h redo->8) lKS/8
345# f  <0> iter s
346# g  <|> and(other->8) vK/1
347# 8      <;> nextstate(main 1 -e:1) v:>,<,%
348# 9      <0> pushmark s
349# a      <$> const[PV "foo "] s
350# b      <#> gvsv[*_] s
351# c      <2> concat[t4] sK/2
352# d      <@> print vK
353# e      <0> unstack v
354#            goto f
355# h  <2> leaveloop vK/2
356# i  <@> leave[1 ref] vKP/REFC
357EOT_EOT
358# 1  <0> enter
359# 2  <;> nextstate(main 2 -e:1) v:>,<,%,{
360# 3  <0> pushmark s
361# 4  <$> const(IV 1) s
362# 5  <$> const(IV 10) s
363# 6  <$> gv(*_) s
364# 7  <{> enteriter(next->e last->h redo->8) lKS/8
365# f  <0> iter s
366# g  <|> and(other->8) vK/1
367# 8      <;> nextstate(main 1 -e:1) v:>,<,%
368# 9      <0> pushmark s
369# a      <$> const(PV "foo ") s
370# b      <$> gvsv(*_) s
371# c      <2> concat[t3] sK/2
372# d      <@> print vK
373# e      <0> unstack v
374#            goto f
375# h  <2> leaveloop vK/2
376# i  <@> leave[1 ref] vKP/REFC
377EONT_EONT
378
379checkOptree ( name	=> '-exec sub { print "foo $_" foreach (1..10) }',
380	      code	=> sub { print "foo $_" foreach (1..10) },
381	      bcopts	=> '-exec',
382	      strip_open_hints => 1,
383	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
384# 1  <;> nextstate(main 445 optree.t:167) v:>,<,%
385# 2  <0> pushmark s
386# 3  <$> const[IV 1] s
387# 4  <$> const[IV 10] s
388# 5  <#> gv[*_] s
389# 6  <{> enteriter(next->c last->f redo->7) lKS/8
390# d  <0> iter s
391# e  <|> and(other->7) K/1
392# 7      <0> pushmark s
393# 8      <$> const[PV "foo "] s
394# 9      <#> gvsv[*_] s
395# a      <2> concat[t2] sK/2
396# b      <@> print vK
397# c      <0> unstack s
398#            goto d
399# f  <2> leaveloop K/2
400# g  <1> leavesub[1 ref] K/REFC,1
401EOT_EOT
402# 1  <;> nextstate(main 447 optree_samples.t:252) v:>,<,%
403# 2  <0> pushmark s
404# 3  <$> const(IV 1) s
405# 4  <$> const(IV 10) s
406# 5  <$> gv(*_) s
407# 6  <{> enteriter(next->c last->f redo->7) lKS/8
408# d  <0> iter s
409# e  <|> and(other->7) K/1
410# 7      <0> pushmark s
411# 8      <$> const(PV "foo ") s
412# 9      <$> gvsv(*_) s
413# a      <2> concat[t1] sK/2
414# b      <@> print vK
415# c      <0> unstack s
416#            goto d
417# f  <2> leaveloop K/2
418# g  <1> leavesub[1 ref] K/REFC,1
419EONT_EONT
420
421pass("GREP: SAMPLES FROM PERLDOC -F GREP");
422
423checkOptree ( name	=> '@foo = grep(!/^\#/, @bar)',
424	      code	=> '@foo = grep(!/^\#/, @bar)',
425	      bcopts	=> '-exec',
426	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
427# 1  <;> nextstate(main 496 (eval 20):1) v:{
428# 2  <0> pushmark s
429# 3  <0> pushmark s
430# 4  <#> gv[*bar] s
431# 5  <1> rv2av[t4] lKM/1
432# 6  <@> grepstart lK
433# 7  <|> grepwhile(other->8)[t5] lK
434# 8      </> match(/"^#"/) s/RTIME
435# 9      <1> not sK/1
436#            goto 7
437# a  <0> pushmark s
438# b  <#> gv[*foo] s
439# c  <1> rv2av[t2] lKRM*/1
440# d  <2> aassign[t6] KS/COMMON
441# e  <1> leavesub[1 ref] K/REFC,1
442EOT_EOT
443# 1  <;> nextstate(main 496 (eval 20):1) v:{
444# 2  <0> pushmark s
445# 3  <0> pushmark s
446# 4  <$> gv(*bar) s
447# 5  <1> rv2av[t2] lKM/1
448# 6  <@> grepstart lK
449# 7  <|> grepwhile(other->8)[t3] lK
450# 8      </> match(/"^\\#"/) s/RTIME
451# 9      <1> not sK/1
452#            goto 7
453# a  <0> pushmark s
454# b  <$> gv(*foo) s
455# c  <1> rv2av[t1] lKRM*/1
456# d  <2> aassign[t4] KS/COMMON
457# e  <1> leavesub[1 ref] K/REFC,1
458EONT_EONT
459
460
461pass("MAP: SAMPLES FROM PERLDOC -F MAP");
462
463checkOptree ( name	=> '%h = map { getkey($_) => $_ } @a',
464	      code	=> '%h = map { getkey($_) => $_ } @a',
465	      bcopts	=> '-exec',
466	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
467# 1  <;> nextstate(main 501 (eval 22):1) v:{
468# 2  <0> pushmark s
469# 3  <0> pushmark s
470# 4  <#> gv[*a] s
471# 5  <1> rv2av[t8] lKM/1
472# 6  <@> mapstart lK*                 < 5.017002
473# 6  <@> mapstart lK                  >=5.017002
474# 7  <|> mapwhile(other->8)[t9] lK
475# 8      <0> enter l
476# 9      <;> nextstate(main 500 (eval 22):1) v:{
477# a      <0> pushmark s
478# b      <#> gvsv[*_] s
479# c      <#> gv[*getkey] s/EARLYCV
480# d      <1> entersub[t5] lKS/TARG
481# e      <#> gvsv[*_] s
482# f      <@> leave lKP
483#            goto 7
484# g  <0> pushmark s
485# h  <#> gv[*h] s
486# i  <1> rv2hv[t2] lKRM*/1         < 5.019006
487# i  <1> rv2hv lKRM*/1             >=5.019006
488# j  <2> aassign[t10] KS/COMMON
489# k  <1> leavesub[1 ref] K/REFC,1
490EOT_EOT
491# 1  <;> nextstate(main 501 (eval 22):1) v:{
492# 2  <0> pushmark s
493# 3  <0> pushmark s
494# 4  <$> gv(*a) s
495# 5  <1> rv2av[t3] lKM/1
496# 6  <@> mapstart lK*                 < 5.017002
497# 6  <@> mapstart lK                  >=5.017002
498# 7  <|> mapwhile(other->8)[t4] lK
499# 8      <0> enter l
500# 9      <;> nextstate(main 500 (eval 22):1) v:{
501# a      <0> pushmark s
502# b      <$> gvsv(*_) s
503# c      <$> gv(*getkey) s/EARLYCV
504# d      <1> entersub[t2] lKS/TARG
505# e      <$> gvsv(*_) s
506# f      <@> leave lKP
507#            goto 7
508# g  <0> pushmark s
509# h  <$> gv(*h) s
510# i  <1> rv2hv[t1] lKRM*/1         < 5.019006
511# i  <1> rv2hv lKRM*/1             >=5.019006
512# j  <2> aassign[t5] KS/COMMON
513# k  <1> leavesub[1 ref] K/REFC,1
514EONT_EONT
515
516checkOptree ( name	=> '%h=(); for $_(@a){$h{getkey($_)} = $_}',
517	      code	=> '%h=(); for $_(@a){$h{getkey($_)} = $_}',
518	      bcopts	=> '-exec',
519	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
520# 1  <;> nextstate(main 505 (eval 24):1) v
521# 2  <0> pushmark s
522# 3  <0> pushmark s
523# 4  <#> gv[*h] s
524# 5  <1> rv2hv[t2] lKRM*/1         < 5.019006
525# 5  <1> rv2hv lKRM*/1             >=5.019006
526# 6  <2> aassign[t3] vKS
527# 7  <;> nextstate(main 506 (eval 24):1) v:{
528# 8  <0> pushmark sM
529# 9  <#> gv[*a] s
530# a  <1> rv2av[t6] sKRM/1
531# b  <#> gv[*_] s
532# c  <1> rv2gv sKRM/1
533# d  <{> enteriter(next->o last->r redo->e) lKS/8
534# p  <0> iter s
535# q  <|> and(other->e) K/1
536# e      <;> nextstate(main 505 (eval 24):1) v:{
537# f      <#> gvsv[*_] s
538# g      <#> gv[*h] s
539# h      <1> rv2hv sKR/1
540# i      <0> pushmark s
541# j      <#> gvsv[*_] s
542# k      <#> gv[*getkey] s/EARLYCV
543# l      <1> entersub[t10] sKS/TARG
544# m      <2> helem sKRM*/2
545# n      <2> sassign vKS/2
546# o      <0> unstack s
547#            goto p
548# r  <2> leaveloop KP/2
549# s  <1> leavesub[1 ref] K/REFC,1
550EOT_EOT
551# 1  <;> nextstate(main 505 (eval 24):1) v
552# 2  <0> pushmark s
553# 3  <0> pushmark s
554# 4  <$> gv(*h) s
555# 5  <1> rv2hv[t1] lKRM*/1         < 5.019006
556# 5  <1> rv2hv lKRM*/1             >=5.019006
557# 6  <2> aassign[t2] vKS
558# 7  <;> nextstate(main 506 (eval 24):1) v:{
559# 8  <0> pushmark sM
560# 9  <$> gv(*a) s
561# a  <1> rv2av[t3] sKRM/1
562# b  <$> gv(*_) s
563# c  <1> rv2gv sKRM/1
564# d  <{> enteriter(next->o last->r redo->e) lKS/8
565# p  <0> iter s
566# q  <|> and(other->e) K/1
567# e      <;> nextstate(main 505 (eval 24):1) v:{
568# f      <$> gvsv(*_) s
569# g      <$> gv(*h) s
570# h      <1> rv2hv sKR/1
571# i      <0> pushmark s
572# j      <$> gvsv(*_) s
573# k      <$> gv(*getkey) s/EARLYCV
574# l      <1> entersub[t4] sKS/TARG
575# m      <2> helem sKRM*/2
576# n      <2> sassign vKS/2
577# o      <0> unstack s
578#            goto p
579# r  <2> leaveloop KP/2
580# s  <1> leavesub[1 ref] K/REFC,1
581EONT_EONT
582
583checkOptree ( name	=> 'map $_+42, 10..20',
584	      code	=> 'map $_+42, 10..20',
585	      bcopts	=> '-exec',
586	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
587# 1  <;> nextstate(main 497 (eval 20):1) v
588# 2  <0> pushmark s
589# 3  <$> const[AV ] s
590# 4  <1> rv2av lKPM/1
591# 5  <@> mapstart K
592# 6  <|> mapwhile(other->7)[t5] K
593# 7      <#> gvsv[*_] s
594# 8      <$> const[IV 42] s
595# 9      <2> add[t2] sK/2
596#            goto 6
597# a  <1> leavesub[1 ref] K/REFC,1
598EOT_EOT
599# 1  <;> nextstate(main 511 (eval 26):1) v
600# 2  <0> pushmark s
601# 3  <$> const(AV ) s
602# 4  <1> rv2av lKPM/1
603# 5  <@> mapstart K
604# 6  <|> mapwhile(other->7)[t4] K
605# 7      <$> gvsv(*_) s
606# 8      <$> const(IV 42) s
607# 9      <2> add[t1] sK/2
608#            goto 6
609# a  <1> leavesub[1 ref] K/REFC,1
610EONT_EONT
611
612pass("CONSTANTS");
613
614checkOptree ( name	=> '-e use constant j => qq{junk}; print j',
615	      prog	=> 'use constant j => qq{junk}; print j',
616	      bcopts	=> '-exec',
617	      strip_open_hints => 1,
618	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
619# 1  <0> enter
620# 2  <;> nextstate(main 71 -e:1) v:>,<,%,{
621# 3  <0> pushmark s
622# 4  <$> const[PV "junk"] s*      < 5.017002
623# 4  <$> const[PV "junk"] s*/FOLD >=5.017002
624# 5  <@> print vK
625# 6  <@> leave[1 ref] vKP/REFC
626EOT_EOT
627# 1  <0> enter
628# 2  <;> nextstate(main 71 -e:1) v:>,<,%,{
629# 3  <0> pushmark s
630# 4  <$> const(PV "junk") s*      < 5.017002
631# 4  <$> const(PV "junk") s*/FOLD >=5.017002
632# 5  <@> print vK
633# 6  <@> leave[1 ref] vKP/REFC
634EONT_EONT
635
636pass("rpeep - return \$x at end of sub");
637
638checkOptree ( name	=> '-exec sub { return 1 }',
639	      code	=> sub { return 1 },
640	      bcopts	=> '-exec',
641	      strip_open_hints => 1,
642	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
643# 1  <;> nextstate(main 1 -e:1) v:>,<,%
644# 2  <$> const[IV 1] s
645# 3  <1> leavesub[1 ref] K/REFC,1
646EOT_EOT
647# 1  <;> nextstate(main 1 -e:1) v:>,<,%
648# 2  <$> const(IV 1) s
649# 3  <1> leavesub[1 ref] K/REFC,1
650EONT_EONT
651
652pass("rpeep - if ($a || $b)");
653
654checkOptree ( name	=> 'if ($a || $b) { } return 1',
655	      code	=> 'if ($a || $b) { } return 1',
656	      bcopts	=> '-exec',
657	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
658# 1  <;> nextstate(main 997 (eval 15):1) v
659# 2  <#> gvsv[*a] s
660# 3  <|> or(other->4) sK/1
661# 4      <#> gvsv[*b] s
662# 5      <|> and(other->6) vK/1
663# 6  <0> stub v
664# 7  <;> nextstate(main 997 (eval 15):1) v
665# 8  <$> const[IV 1] s
666# 9  <1> leavesub[1 ref] K/REFC,1
667EOT_EOT
668# 1  <;> nextstate(main 997 (eval 15):1) v
669# 2  <$> gvsv(*a) s
670# 3  <|> or(other->4) sK/1
671# 4      <$> gvsv(*b) s
672# 5      <|> and(other->6) vK/1
673# 6  <0> stub v
674# 7  <;> nextstate(main 3 (eval 3):1) v
675# 8  <$> const(IV 1) s
676# 9  <1> leavesub[1 ref] K/REFC,1
677EONT_EONT
678
679pass("rpeep - unless ($a && $b)");
680
681checkOptree ( name	=> 'unless ($a && $b) { } return 1',
682	      code	=> 'unless ($a && $b) { } return 1',
683	      bcopts	=> '-exec',
684	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
685# 1  <;> nextstate(main 997 (eval 15):1) v
686# 2  <#> gvsv[*a] s
687# 3  <|> and(other->4) sK/1
688# 4      <#> gvsv[*b] s
689# 5      <|> or(other->6) vK/1
690# 6  <0> stub v
691# 7  <;> nextstate(main 997 (eval 15):1) v
692# 8  <$> const[IV 1] s
693# 9  <1> leavesub[1 ref] K/REFC,1
694EOT_EOT
695# 1  <;> nextstate(main 997 (eval 15):1) v
696# 2  <$> gvsv(*a) s
697# 3  <|> and(other->4) sK/1
698# 4      <$> gvsv(*b) s
699# 5      <|> or(other->6) vK/1
700# 6  <0> stub v
701# 7  <;> nextstate(main 3 (eval 3):1) v
702# 8  <$> const(IV 1) s
703# 9  <1> leavesub[1 ref] K/REFC,1
704EONT_EONT
705
706pass("rpeep - my $a; my @b; my %c; print 'f'");
707
708checkOptree ( name	=> 'my $a; my @b; my %c; return 1',
709	      code	=> 'my $a; my @b; my %c; return 1',
710	      bcopts	=> '-exec',
711	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
712# 1  <;> nextstate(main 991 (eval 17):1) v
713# 2  <0> padrange[$a:991,994; @b:992,994; %c:993,994] vM/LVINTRO,3
714# 3  <;> nextstate(main 994 (eval 17):1) v:{
715# 4  <$> const[IV 1] s
716# 5  <1> leavesub[1 ref] K/REFC,1
717EOT_EOT
718# 1  <;> nextstate(main 991 (eval 17):1) v
719# 2  <0> padrange[$a:991,994; @b:992,994; %c:993,994] vM/LVINTRO,3
720# 3  <;> nextstate(main 994 (eval 17):1) v:{
721# 4  <$> const(IV 1) s
722# 5  <1> leavesub[1 ref] K/REFC,1
723EONT_EONT
724
725__END__
726
727#######################################################################
728
729checkOptree ( name	=> '-exec sub a { print (shift) ? "foo" : "bar" }',
730	      code	=> sub { print (shift) ? "foo" : "bar" },
731	      bcopts	=> '-exec',
732	      expect	=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
733   insert threaded reference here
734EOT_EOT
735   insert non-threaded reference here
736EONT_EONT
737
738