This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: tests for change #22539
[perl5.git] / ext / B / t / optree_samples.t
1 #!perl
2
3 BEGIN {
4     chdir 't';
5     @INC = ('../lib', '../ext/B/t');
6     require './test.pl';
7 }
8 use OptreeCheck;
9
10 plan tests      => 13;
11
12 pass("GENERAL OPTREE EXAMPLES");
13
14 pass("IF,THEN,ELSE, ?:");
15
16 checkOptree ( name      => '-basic sub {if shift print then,else}',
17               bcopts    => '-basic',
18               code      => sub { if (shift) { print "then" }
19                                  else       { print "else" }
20                              },
21               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
22 # B::Concise::compile(CODE(0x81a77b4))
23 # 9  <1> leavesub[1 ref] K/REFC,1 ->(end)
24 # -     <@> lineseq KP ->9
25 # 1        <;> nextstate(main 426 optree.t:16) v ->2
26 # -        <1> null K/1 ->-
27 # 5           <|> cond_expr(other->6) K/1 ->a
28 # 4              <1> shift sK/1 ->5
29 # 3                 <1> rv2av[t2] sKRM/1 ->4
30 # 2                    <#> gv[*_] s ->3
31 # -              <@> scope K ->-
32 # -                 <0> ex-nextstate v ->6
33 # 8                 <@> print sK ->9
34 # 6                    <0> pushmark s ->7
35 # 7                    <$> const[PV "then"] s ->8
36 # f              <@> leave KP ->9
37 # a                 <0> enter ->b
38 # b                 <;> nextstate(main 424 optree.t:17) v ->c
39 # e                 <@> print sK ->f
40 # c                    <0> pushmark s ->d
41 # d                    <$> const[PV "else"] s ->e
42 EOT_EOT
43 # 9  <1> leavesub[1 ref] K/REFC,1 ->(end)
44 # -     <@> lineseq KP ->9
45 # 1        <;> nextstate(main 427 optree_samples.t:18) v ->2
46 # -        <1> null K/1 ->-
47 # 5           <|> cond_expr(other->6) K/1 ->a
48 # 4              <1> shift sK/1 ->5
49 # 3                 <1> rv2av[t1] sKRM/1 ->4
50 # 2                    <$> gv(*_) s ->3
51 # -              <@> scope K ->-
52 # -                 <0> ex-nextstate v ->6
53 # 8                 <@> print sK ->9
54 # 6                    <0> pushmark s ->7
55 # 7                    <$> const(PV "then") s ->8
56 # f              <@> leave KP ->9
57 # a                 <0> enter ->b
58 # b                 <;> nextstate(main 425 optree_samples.t:19) v ->c
59 # e                 <@> print sK ->f
60 # c                    <0> pushmark s ->d
61 # d                    <$> const(PV "else") s ->e
62 EONT_EONT
63
64 checkOptree ( name      => '-basic (see above, with my $a = shift)',
65               bcopts    => '-basic',
66               code      => sub { my $a = shift;
67                                  if ($a) { print "foo" }
68                                  else    { print "bar" }
69                              },
70               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
71 # d  <1> leavesub[1 ref] K/REFC,1 ->(end)
72 # -     <@> lineseq KP ->d
73 # 1        <;> nextstate(main 431 optree.t:68) v ->2
74 # 6        <2> sassign vKS/2 ->7
75 # 4           <1> shift sK/1 ->5
76 # 3              <1> rv2av[t3] sKRM/1 ->4
77 # 2                 <#> gv[*_] s ->3
78 # 5           <0> padsv[$a:431,435] sRM*/LVINTRO ->6
79 # 7        <;> nextstate(main 435 optree.t:69) v ->8
80 # -        <1> null K/1 ->-
81 # 9           <|> cond_expr(other->a) K/1 ->e
82 # 8              <0> padsv[$a:431,435] s ->9
83 # -              <@> scope K ->-
84 # -                 <0> ex-nextstate v ->a
85 # c                 <@> print sK ->d
86 # a                    <0> pushmark s ->b
87 # b                    <$> const[PV "foo"] s ->c
88 # j              <@> leave KP ->d
89 # e                 <0> enter ->f
90 # f                 <;> nextstate(main 433 optree.t:70) v ->g
91 # i                 <@> print sK ->j
92 # g                    <0> pushmark s ->h
93 # h                    <$> const[PV "bar"] s ->i
94 EOT_EOT
95 # 1  <;> nextstate(main 45 optree.t:23) v
96 # 2  <0> padsv[$a:45,46] M/LVINTRO
97 # 3  <1> leavesub[1 ref] K/REFC,1
98 # d  <1> leavesub[1 ref] K/REFC,1 ->(end)
99 # -     <@> lineseq KP ->d
100 # 1        <;> nextstate(main 428 optree_samples.t:48) v ->2
101 # 6        <2> sassign vKS/2 ->7
102 # 4           <1> shift sK/1 ->5
103 # 3              <1> rv2av[t2] sKRM/1 ->4
104 # 2                 <$> gv(*_) s ->3
105 # 5           <0> padsv[$a:428,432] sRM*/LVINTRO ->6
106 # 7        <;> nextstate(main 432 optree_samples.t:49) v ->8
107 # -        <1> null K/1 ->-
108 # 9           <|> cond_expr(other->a) K/1 ->e
109 # 8              <0> padsv[$a:428,432] s ->9
110 # -              <@> scope K ->-
111 # -                 <0> ex-nextstate v ->a
112 # c                 <@> print sK ->d
113 # a                    <0> pushmark s ->b
114 # b                    <$> const(PV "foo") s ->c
115 # j              <@> leave KP ->d
116 # e                 <0> enter ->f
117 # f                 <;> nextstate(main 430 optree_samples.t:50) v ->g
118 # i                 <@> print sK ->j
119 # g                    <0> pushmark s ->h
120 # h                    <$> const(PV "bar") s ->i
121 EONT_EONT
122
123 checkOptree ( name      => '-exec sub {if shift print then,else}',
124               bcopts    => '-exec',
125               code      => sub { if (shift) { print "then" }
126                                  else       { print "else" }
127                              },
128               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
129 # B::Concise::compile(CODE(0x81a77b4))
130 # 1  <;> nextstate(main 426 optree.t:16) v
131 # 2  <#> gv[*_] s
132 # 3  <1> rv2av[t2] sKRM/1
133 # 4  <1> shift sK/1
134 # 5  <|> cond_expr(other->6) K/1
135 # 6      <0> pushmark s
136 # 7      <$> const[PV "then"] s
137 # 8      <@> print sK
138 #            goto 9
139 # a  <0> enter 
140 # b  <;> nextstate(main 424 optree.t:17) v
141 # c  <0> pushmark s
142 # d  <$> const[PV "else"] s
143 # e  <@> print sK
144 # f  <@> leave KP
145 # 9  <1> leavesub[1 ref] K/REFC,1
146 EOT_EOT
147 # 1  <;> nextstate(main 436 optree_samples.t:123) v
148 # 2  <$> gv(*_) s
149 # 3  <1> rv2av[t1] sKRM/1
150 # 4  <1> shift sK/1
151 # 5  <|> cond_expr(other->6) K/1
152 # 6      <0> pushmark s
153 # 7      <$> const(PV "then") s
154 # 8      <@> print sK
155 #            goto 9
156 # a  <0> enter 
157 # b  <;> nextstate(main 434 optree_samples.t:124) v
158 # c  <0> pushmark s
159 # d  <$> const(PV "else") s
160 # e  <@> print sK
161 # f  <@> leave KP
162 # 9  <1> leavesub[1 ref] K/REFC,1
163 EONT_EONT
164
165 checkOptree ( name      => '-exec (see above, with my $a = shift)',
166               bcopts    => '-exec',
167               code      => sub { my $a = shift;
168                                  if ($a) { print "foo" }
169                                  else    { print "bar" }
170                              },
171               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
172 # 1  <;> nextstate(main 423 optree.t:16) v
173 # 2  <#> gv[*_] s
174 # 3  <1> rv2av[t3] sKRM/1
175 # 4  <1> shift sK/1
176 # 5  <0> padsv[$a:423,427] sRM*/LVINTRO
177 # 6  <2> sassign vKS/2
178 # 7  <;> nextstate(main 427 optree.t:17) v
179 # 8  <0> padsv[$a:423,427] s
180 # 9  <|> cond_expr(other->a) K/1
181 # a      <0> pushmark s
182 # b      <$> const[PV "foo"] s
183 # c      <@> print sK
184 #            goto d
185 # e  <0> enter 
186 # f  <;> nextstate(main 425 optree.t:18) v
187 # g  <0> pushmark s
188 # h  <$> const[PV "bar"] s
189 # i  <@> print sK
190 # j  <@> leave KP
191 # d  <1> leavesub[1 ref] K/REFC,1
192 EOT_EOT
193 # 1  <;> nextstate(main 437 optree_samples.t:112) v
194 # 2  <$> gv(*_) s
195 # 3  <1> rv2av[t2] sKRM/1
196 # 4  <1> shift sK/1
197 # 5  <0> padsv[$a:437,441] sRM*/LVINTRO
198 # 6  <2> sassign vKS/2
199 # 7  <;> nextstate(main 441 optree_samples.t:113) v
200 # 8  <0> padsv[$a:437,441] s
201 # 9  <|> cond_expr(other->a) K/1
202 # a      <0> pushmark s
203 # b      <$> const(PV "foo") s
204 # c      <@> print sK
205 #            goto d
206 # e  <0> enter 
207 # f  <;> nextstate(main 439 optree_samples.t:114) v
208 # g  <0> pushmark s
209 # h  <$> const(PV "bar") s
210 # i  <@> print sK
211 # j  <@> leave KP
212 # d  <1> leavesub[1 ref] K/REFC,1
213 EONT_EONT
214
215 checkOptree ( name      => '-exec sub { print (shift) ? "foo" : "bar" }',
216               code      => sub { print (shift) ? "foo" : "bar" },
217               bcopts    => '-exec',
218               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
219 # 1  <;> nextstate(main 428 optree.t:31) v
220 # 2  <0> pushmark s
221 # 3  <#> gv[*_] s
222 # 4  <1> rv2av[t2] sKRM/1
223 # 5  <1> shift sK/1
224 # 6  <@> print sK
225 # 7  <|> cond_expr(other->8) K/1
226 # 8      <$> const[PV "foo"] s
227 #            goto 9
228 # a  <$> const[PV "bar"] s
229 # 9  <1> leavesub[1 ref] K/REFC,1
230 EOT_EOT
231 # 1  <;> nextstate(main 442 optree_samples.t:144) v
232 # 2  <0> pushmark s
233 # 3  <$> gv(*_) s
234 # 4  <1> rv2av[t1] sKRM/1
235 # 5  <1> shift sK/1
236 # 6  <@> print sK
237 # 7  <|> cond_expr(other->8) K/1
238 # 8      <$> const(PV "foo") s
239 #            goto 9
240 # a  <$> const(PV "bar") s
241 # 9  <1> leavesub[1 ref] K/REFC,1
242 EONT_EONT
243
244 pass ("FOREACH");
245
246 checkOptree ( name      => '-exec sub { foreach (1..10) {print "foo $_"} }',
247               code      => sub { foreach (1..10) {print "foo $_"} },
248               bcopts    => '-exec',
249               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
250 # 1  <;> nextstate(main 443 optree.t:158) v
251 # 2  <0> pushmark s
252 # 3  <$> const[IV 1] s
253 # 4  <$> const[IV 10] s
254 # 5  <#> gv[*_] s
255 # 6  <{> enteriter(next->d last->g redo->7) lKS
256 # e  <0> iter s
257 # f  <|> and(other->7) K/1
258 # 7      <;> nextstate(main 442 optree.t:158) v
259 # 8      <0> pushmark s
260 # 9      <$> const[PV "foo "] s
261 # a      <#> gvsv[*_] s
262 # b      <2> concat[t4] sK/2
263 # c      <@> print vK
264 # d      <0> unstack s
265 #            goto e
266 # g  <2> leaveloop K/2
267 # h  <1> leavesub[1 ref] K/REFC,1
268 # '
269 EOT_EOT
270 # 1  <;> nextstate(main 444 optree_samples.t:182) v
271 # 2  <0> pushmark s
272 # 3  <$> const(IV 1) s
273 # 4  <$> const(IV 10) s
274 # 5  <$> gv(*_) s
275 # 6  <{> enteriter(next->d last->g redo->7) lKS
276 # e  <0> iter s
277 # f  <|> and(other->7) K/1
278 # 7      <;> nextstate(main 443 optree_samples.t:182) v
279 # 8      <0> pushmark s
280 # 9      <$> const(PV "foo ") s
281 # a      <$> gvsv(*_) s
282 # b      <2> concat[t3] sK/2
283 # c      <@> print vK
284 # d      <0> unstack s
285 #            goto e
286 # g  <2> leaveloop K/2
287 # h  <1> leavesub[1 ref] K/REFC,1
288 EONT_EONT
289
290 checkOptree ( name      => '-basic sub { print "foo $_" foreach (1..10) }',
291               code      => sub { print "foo $_" foreach (1..10) }, 
292               bcopts    => '-basic',
293               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
294 # h  <1> leavesub[1 ref] K/REFC,1 ->(end)
295 # -     <@> lineseq KP ->h
296 # 1        <;> nextstate(main 445 optree.t:167) v ->2
297 # 2        <;> nextstate(main 445 optree.t:167) v ->3
298 # g        <2> leaveloop K/2 ->h
299 # 7           <{> enteriter(next->d last->g redo->8) lKS ->e
300 # -              <0> ex-pushmark s ->3
301 # -              <1> ex-list lK ->6
302 # 3                 <0> pushmark s ->4
303 # 4                 <$> const[IV 1] s ->5
304 # 5                 <$> const[IV 10] s ->6
305 # 6              <#> gv[*_] s ->7
306 # -           <1> null K/1 ->g
307 # f              <|> and(other->8) K/1 ->g
308 # e                 <0> iter s ->f
309 # -                 <@> lineseq sK ->-
310 # c                    <@> print vK ->d
311 # 8                       <0> pushmark s ->9
312 # -                       <1> ex-stringify sK/1 ->c
313 # -                          <0> ex-pushmark s ->9
314 # b                          <2> concat[t2] sK/2 ->c
315 # 9                             <$> const[PV "foo "] s ->a
316 # -                             <1> ex-rv2sv sK/1 ->b
317 # a                                <#> gvsv[*_] s ->b
318 # d                    <0> unstack s ->e
319 EOT_EOT
320 # h  <1> leavesub[1 ref] K/REFC,1 ->(end)
321 # -     <@> lineseq KP ->h
322 # 1        <;> nextstate(main 446 optree_samples.t:192) v ->2
323 # 2        <;> nextstate(main 446 optree_samples.t:192) v ->3
324 # g        <2> leaveloop K/2 ->h
325 # 7           <{> enteriter(next->d last->g redo->8) lKS ->e
326 # -              <0> ex-pushmark s ->3
327 # -              <1> ex-list lK ->6
328 # 3                 <0> pushmark s ->4
329 # 4                 <$> const(IV 1) s ->5
330 # 5                 <$> const(IV 10) s ->6
331 # 6              <$> gv(*_) s ->7
332 # -           <1> null K/1 ->g
333 # f              <|> and(other->8) K/1 ->g
334 # e                 <0> iter s ->f
335 # -                 <@> lineseq sK ->-
336 # c                    <@> print vK ->d
337 # 8                       <0> pushmark s ->9
338 # -                       <1> ex-stringify sK/1 ->c
339 # -                          <0> ex-pushmark s ->9
340 # b                          <2> concat[t1] sK/2 ->c
341 # 9                             <$> const(PV "foo ") s ->a
342 # -                             <1> ex-rv2sv sK/1 ->b
343 # a                                <$> gvsv(*_) s ->b
344 # d                    <0> unstack s ->e
345 EONT_EONT
346
347 checkOptree ( name      => '-exec -e foreach (1..10) {print "foo $_"}',
348               prog      => 'foreach (1..10) {print "foo $_"}',
349               bcopts    => '-exec',
350               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
351 # 1  <0> enter 
352 # 2  <;> nextstate(main 2 -e:1) v
353 # 3  <0> pushmark s
354 # 4  <$> const[IV 1] s
355 # 5  <$> const[IV 10] s
356 # 6  <#> gv[*_] s
357 # 7  <{> enteriter(next->e last->h redo->8) lKS
358 # f  <0> iter s
359 # g  <|> and(other->8) vK/1
360 # 8      <;> nextstate(main 1 -e:1) v
361 # 9      <0> pushmark s
362 # a      <$> const[PV "foo "] s
363 # b      <#> gvsv[*_] s
364 # c      <2> concat[t4] sK/2
365 # d      <@> print vK
366 # e      <0> unstack v
367 #            goto f
368 # h  <2> leaveloop vK/2
369 # i  <@> leave[1 ref] vKP/REFC
370 EOT_EOT
371 # 1  <0> enter 
372 # 2  <;> nextstate(main 2 -e:1) v
373 # 3  <0> pushmark s
374 # 4  <$> const(IV 1) s
375 # 5  <$> const(IV 10) s
376 # 6  <$> gv(*_) s
377 # 7  <{> enteriter(next->e last->h redo->8) lKS
378 # f  <0> iter s
379 # g  <|> and(other->8) vK/1
380 # 8      <;> nextstate(main 1 -e:1) v
381 # 9      <0> pushmark s
382 # a      <$> const(PV "foo ") s
383 # b      <$> gvsv(*_) s
384 # c      <2> concat[t3] sK/2
385 # d      <@> print vK
386 # e      <0> unstack v
387 #            goto f
388 # h  <2> leaveloop vK/2
389 # i  <@> leave[1 ref] vKP/REFC
390
391 EONT_EONT
392
393 checkOptree ( name      => '-exec sub { print "foo $_" foreach (1..10) }',
394               code      => sub { print "foo $_" foreach (1..10) }, 
395               bcopts    => '-exec',
396               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
397 # B::Concise::compile(CODE(0x8332b20))
398 #            goto -
399 # 1  <;> nextstate(main 445 optree.t:167) v
400 # 2  <;> nextstate(main 445 optree.t:167) v
401 # 3  <0> pushmark s
402 # 4  <$> const[IV 1] s
403 # 5  <$> const[IV 10] s
404 # 6  <#> gv[*_] s
405 # 7  <{> enteriter(next->d last->g redo->8) lKS
406 # e  <0> iter s
407 # f  <|> and(other->8) K/1
408 # 8      <0> pushmark s
409 # 9      <$> const[PV "foo "] s
410 # a      <#> gvsv[*_] s
411 # b      <2> concat[t2] sK/2
412 # c      <@> print vK
413 # d      <0> unstack s
414 #            goto e
415 # g  <2> leaveloop K/2
416 # h  <1> leavesub[1 ref] K/REFC,1
417 EOT_EOT
418 # 1  <;> nextstate(main 447 optree_samples.t:252) v
419 # 2  <;> nextstate(main 447 optree_samples.t:252) v
420 # 3  <0> pushmark s
421 # 4  <$> const(IV 1) s
422 # 5  <$> const(IV 10) s
423 # 6  <$> gv(*_) s
424 # 7  <{> enteriter(next->d last->g redo->8) lKS
425 # e  <0> iter s
426 # f  <|> and(other->8) K/1
427 # 8      <0> pushmark s
428 # 9      <$> const(PV "foo ") s
429 # a      <$> gvsv(*_) s
430 # b      <2> concat[t1] sK/2
431 # c      <@> print vK
432 # d      <0> unstack s
433 #            goto e
434 # g  <2> leaveloop K/2
435 # h  <1> leavesub[1 ref] K/REFC,1
436 EONT_EONT
437
438 checkOptree ( name      => '-e use constant j => "junk"; print j',
439               prog      => 'use constant j => "junk"; print j',
440               bcopts    => '-exec',
441               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
442 # 1  <0> enter 
443 # 2  <;> nextstate(main 71 -e:1) v
444 # 3  <0> pushmark s
445 # 4  <$> const[PV "junk"] s
446 # 5  <@> print vK
447 # 6  <@> leave[1 ref] vKP/REFC
448 EOT_EOT
449 # 1  <0> enter 
450 # 2  <;> nextstate(main 71 -e:1) v
451 # 3  <0> pushmark s
452 # 4  <$> const(PV "junk") s
453 # 5  <@> print vK
454 # 6  <@> leave[1 ref] vKP/REFC
455 EONT_EONT
456
457 __END__
458
459 #######################################################################
460
461 checkOptree ( name      => '-exec sub a { print (shift) ? "foo" : "bar" }',
462               code      => sub { print (shift) ? "foo" : "bar" },
463               bcopts    => '-exec',
464               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
465    insert threaded reference here
466 EOT_EOT
467    insert non-threaded reference here
468 EONT_EONT
469