6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
10 if (!$Config::Config{useperlio}) {
11 print "1..0 # Skip -- need perlio to walk the optree\n";
16 use OptreeCheck; # ALSO DOES @ARGV HANDLING !!!!!!
21 #################################
23 use constant { # see also t/op/gv.t line 358
37 sub myno () { return 1!=1 }
38 sub pi () { 3.14159 };
40 my $RV_class = $] >= 5.011 ? 'IV' : 'RV';
42 my $want = { # expected types, how value renders in-line, todos (maybe)
43 mystr => [ 'PV', '"'.mystr.'"' ],
44 myhref => [ $RV_class, '\\\\HASH'],
46 myglob => [ $RV_class, '\\\\' ],
47 mysub => [ $RV_class, '\\\\' ],
48 myunsub => [ $RV_class, '\\\\' ],
49 # these are not inlined, at least not per BC::Concise
50 #myyes => [ $RV_class, ],
51 #myno => [ $RV_class, ],
52 myaref => [ $RV_class, '\\\\' ],
53 myfl => [ 'NV', myfl ],
54 myint => [ 'IV', myint ],
56 myrex => [ $RV_class, '\\\\"\\(?^:Foo\\)"' ],
58 myrex => [ $RV_class, '\\\\' ],
60 myundef => [ 'NULL', ],
64 => qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
67 $::{napier} = \2.71828; # counter-example (doesn't get optimized).
68 eval "sub napier ();";
71 # should be able to undefine constant::import here ???
73 # eval 'sub constant::import () {}';
74 # undef *constant::import::{CODE};
77 #################################
78 pass("RENDER CONSTANT SUBS RETURNING SCALARS");
80 for $func (sort keys %$want) {
81 # no strict 'refs'; # why not needed ?
82 checkOptree ( name => "$func() as a coderef",
85 expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
86 is a constant sub, optimized to a $want->{$func}[0]
88 is a constant sub, optimized to a $want->{$func}[0]
93 pass("RENDER CALLS TO THOSE CONSTANT SUBS");
95 for $func (sort keys %$want) {
96 # print "# doing $func\n";
97 checkOptree ( name => "call $func",
99 ($want->{$func}[2]) ? ( todo => $want->{$func}[2]) : (),
100 bc_opts => '-nobanner',
101 expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
102 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
103 - <\@> lineseq KP ->3
104 1 <;> dbstate(main 833 (eval 44):1) v ->2
105 2 <\$> const[$want->{$func}[0] $want->{$func}[1]] s* ->3 < 5.017002
106 2 <\$> const[$want->{$func}[0] $want->{$func}[1]] s*/FOLD ->3 >=5.017002
108 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
109 - <\@> lineseq KP ->3
110 1 <;> dbstate(main 833 (eval 44):1) v ->2
111 2 <\$> const($want->{$func}[0] $want->{$func}[1]) s* ->3 < 5.017002
112 2 <\$> const($want->{$func}[0] $want->{$func}[1]) s*/FOLD ->3 >=5.017002
120 checkOptree ( name => 'myyes() as coderef',
121 code => sub () { 1==1 },
123 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
124 is a constant sub, optimized to a SPECIAL
126 is a constant sub, optimized to a SPECIAL
130 checkOptree ( name => 'myyes() as coderef',
131 prog => 'sub a() { 1==1 }; print a',
133 strip_open_hints => 1,
134 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
135 # 6 <@> leave[1 ref] vKP/REFC ->(end)
137 # 2 <;> nextstate(main 2 -e:1) v:>,<,%,{ ->3
139 # 3 <0> pushmark s ->4
140 # 4 <$> const[SPECIAL sv_yes] s* ->5 < 5.017002
141 # 4 <$> const[SPECIAL sv_yes] s*/FOLD ->5 >=5.017002
143 # 6 <@> leave[1 ref] vKP/REFC ->(end)
145 # 2 <;> nextstate(main 2 -e:1) v:>,<,%,{ ->3
147 # 3 <0> pushmark s ->4
148 # 4 <$> const(SPECIAL sv_yes) s* ->5 < 5.017002
149 # 4 <$> const(SPECIAL sv_yes) s*/FOLD ->5 >=5.017002
153 # Need to do this as a prog, not code, as only the first constant to use
154 # PL_sv_no actually gets to use the real thing - every one following is
156 checkOptree ( name => 'myno() as coderef',
157 prog => 'sub a() { 1!=1 }; print a',
159 strip_open_hints => 1,
160 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
161 # 6 <@> leave[1 ref] vKP/REFC ->(end)
163 # 2 <;> nextstate(main 2 -e:1) v:>,<,%,{ ->3
165 # 3 <0> pushmark s ->4
166 # 4 <$> const[SPECIAL sv_no] s* ->5 < 5.017002
167 # 4 <$> const[SPECIAL sv_no] s*/FOLD ->5 >=5.017002
169 # 6 <@> leave[1 ref] vKP/REFC ->(end)
171 # 2 <;> nextstate(main 2 -e:1) v:>,<,%,{ ->3
173 # 3 <0> pushmark s ->4
174 # 4 <$> const(SPECIAL sv_no) s* ->5 < 5.017002
175 # 4 <$> const(SPECIAL sv_no) s*/FOLD ->5 >=5.017002
179 my ($expect, $expect_nt) =
181 ? (" is a constant sub, optimized to a AV\n") x 2
182 : (<<'EOT_EOT', <<'EONT_EONT');
183 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
184 # - <@> lineseq K ->3
185 # 1 <;> nextstate(constant 61 constant.pm:118) v:*,&,x*,x&,x$ ->2
186 # 2 <0> padav[@list:FAKE:m:96] ->3
188 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
189 # - <@> lineseq K ->3
190 # 1 <;> nextstate(constant 61 constant.pm:118) v:*,&,x*,x&,x$ ->2
191 # 2 <0> padav[@list:FAKE:m:71] ->3
195 checkOptree ( name => 'constant sub returning list',
198 expect => $expect, expect_nt => $expect_nt);
202 printf "myint %d mystr %s myfl %f pi %f\n"
203 , myint, mystr, myfl, pi;
206 my ($expect, $expect_nt) = (<<'EOT_EOT', <<'EONT_EONT');
207 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
208 # - <@> lineseq KP ->9
209 # 1 <;> nextstate(main 635 optree_constants.t:163) v:>,<,% ->2
211 # 2 <0> pushmark sM ->3
212 # 3 <$> const[PV "myint %d mystr %s myfl %f pi %f\n"] sM ->4 < 5.017002
213 # 4 <$> const[IV 42] sM* ->5 < 5.017002
214 # 5 <$> const[PV "hithere"] sM* ->6 < 5.017002
215 # 6 <$> const[NV 1.414213] sM* ->7 < 5.017002
216 # 7 <$> const[NV 3.14159] sM* ->8 < 5.017002
217 # 3 <$> const[PV "myint %d mystr %s myfl %f pi %f\n"] sM/FOLD ->4 >= 5.017002
218 # 4 <$> const[IV 42] sM*/FOLD ->5 >=5.017002
219 # 5 <$> const[PV "hithere"] sM*/FOLD ->6 >=5.017002
220 # 6 <$> const[NV 1.414213] sM*/FOLD ->7 >=5.017002
221 # 7 <$> const[NV 3.14159] sM*/FOLD ->8 >=5.017002
223 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
224 # - <@> lineseq KP ->9
225 # 1 <;> nextstate(main 635 optree_constants.t:163) v:>,<,% ->2
227 # 2 <0> pushmark sM ->3
228 # 3 <$> const(PV "myint %d mystr %s myfl %f pi %f\n") sM ->4 < 5.017002
229 # 4 <$> const(IV 42) sM* ->5 < 5.017002
230 # 5 <$> const(PV "hithere") sM* ->6 < 5.017002
231 # 6 <$> const(NV 1.414213) sM* ->7 < 5.017002
232 # 7 <$> const(NV 3.14159) sM* ->8 < 5.017002
233 # 3 <$> const(PV "myint %d mystr %s myfl %f pi %f\n") sM/FOLD ->4 >= 5.017002
234 # 4 <$> const(IV 42) sM*/FOLD ->5 >=5.017002
235 # 5 <$> const(PV "hithere") sM*/FOLD ->6 >=5.017002
236 # 6 <$> const(NV 1.414213) sM*/FOLD ->7 >=5.017002
237 # 7 <$> const(NV 3.14159) sM*/FOLD ->8 >=5.017002
241 s/M(?=\*? ->)//g for $expect, $expect_nt;
244 checkOptree ( name => 'call many in a print statement',
246 strip_open_hints => 1,
247 expect => $expect, expect_nt => $expect_nt);
249 # test constant expression folding
251 checkOptree ( name => 'arithmetic constant folding in print',
252 code => 'print 1+2+3',
253 strip_open_hints => 1,
254 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
255 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
256 # - <@> lineseq KP ->5
257 # 1 <;> nextstate(main 937 (eval 53):1) v ->2
259 # 2 <0> pushmark s ->3
260 # 3 <$> const[IV 6] s ->4 < 5.017002
261 # 3 <$> const[IV 6] s/FOLD ->4 >=5.017002
263 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
264 # - <@> lineseq KP ->5
265 # 1 <;> nextstate(main 937 (eval 53):1) v ->2
267 # 2 <0> pushmark s ->3
268 # 3 <$> const(IV 6) s ->4 < 5.017002
269 # 3 <$> const(IV 6) s/FOLD ->4 >=5.017002
272 checkOptree ( name => 'string constant folding in print',
273 code => 'print "foo"."bar"',
274 strip_open_hints => 1,
275 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
276 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
277 # - <@> lineseq KP ->5
278 # 1 <;> nextstate(main 942 (eval 55):1) v ->2
280 # 2 <0> pushmark s ->3
281 # 3 <$> const[PV "foobar"] s ->4 < 5.017002
282 # 3 <$> const[PV "foobar"] s/FOLD ->4 >=5.017002
284 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
285 # - <@> lineseq KP ->5
286 # 1 <;> nextstate(main 942 (eval 55):1) v ->2
288 # 2 <0> pushmark s ->3
289 # 3 <$> const(PV "foobar") s ->4 < 5.017002
290 # 3 <$> const(PV "foobar") s/FOLD ->4 >=5.017002
293 checkOptree ( name => 'boolean or folding',
294 code => 'print "foobar" if 1 or 0',
295 strip_open_hints => 1,
296 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
297 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
298 # - <@> lineseq KP ->5
299 # 1 <;> nextstate(main 942 (eval 55):1) v ->2
301 # 2 <0> pushmark s ->3
302 # 3 <$> const[PV "foobar"] s ->4
304 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
305 # - <@> lineseq KP ->5
306 # 1 <;> nextstate(main 942 (eval 55):1) v ->2
308 # 2 <0> pushmark s ->3
309 # 3 <$> const(PV "foobar") s ->4
312 checkOptree ( name => 'lc*,uc*,gt,lt,ge,le,cmp',
314 $s = uc('foo.').ucfirst('bar.').lc('LOW.').lcfirst('LOW');
315 print "a-lt-b" if "a" lt "b";
316 print "b-gt-a" if "b" gt "a";
317 print "a-le-b" if "a" le "b";
318 print "b-ge-a" if "b" ge "a";
319 print "b-cmp-a" if "b" cmp "a";
320 print "a-gt-b" if "a" gt "b"; # should be suppressed
322 strip_open_hints => 1,
323 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
324 # r <1> leavesub[1 ref] K/REFC,1 ->(end)
325 # - <@> lineseq KP ->r
326 # 1 <;> nextstate(main 916 optree_constants.t:307) v:>,<,%,{ ->2
327 # 4 <2> sassign vKS/2 ->5
328 # 2 <$> const[PV "FOO.Bar.low.lOW"] s ->3 < 5.017002
329 # 2 <$> const[PV "FOO.Bar.low.lOW"] s/FOLD ->3 >=5.017002
330 # - <1> ex-rv2sv sKRM*/1 ->4
331 # 3 <#> gvsv[*s] s ->4
332 # 5 <;> nextstate(main 916 optree_constants.t:308) v:>,<,%,{ ->6
334 # 6 <0> pushmark s ->7
335 # 7 <$> const[PV "a-lt-b"] s ->8
336 # 9 <;> nextstate(main 916 optree_constants.t:309) v:>,<,%,{ ->a
338 # a <0> pushmark s ->b
339 # b <$> const[PV "b-gt-a"] s ->c
340 # d <;> nextstate(main 916 optree_constants.t:310) v:>,<,%,{ ->e
342 # e <0> pushmark s ->f
343 # f <$> const[PV "a-le-b"] s ->g
344 # h <;> nextstate(main 916 optree_constants.t:311) v:>,<,%,{ ->i
346 # i <0> pushmark s ->j
347 # j <$> const[PV "b-ge-a"] s ->k
348 # l <;> nextstate(main 916 optree_constants.t:312) v:>,<,%,{ ->m
350 # m <0> pushmark s ->n
351 # n <$> const[PV "b-cmp-a"] s ->o
352 # p <;> nextstate(main 916 optree_constants.t:313) v:>,<,%,{ ->q
353 # q <$> const[PVNV 0] s/SHORT ->r < 5.017002
354 # q <$> const[PVNV 0] s/FOLD,SHORT ->r >=5.017002 < 5.019003
355 # q <$> const[SPECIAL sv_no] s/FOLD,SHORT ->r >=5.019003
357 # r <1> leavesub[1 ref] K/REFC,1 ->(end)
358 # - <@> lineseq KP ->r
359 # 1 <;> nextstate(main 916 optree_constants.t:307) v:>,<,%,{ ->2
360 # 4 <2> sassign vKS/2 ->5
361 # 2 <$> const(PV "FOO.Bar.low.lOW") s ->3 < 5.017002
362 # 2 <$> const(PV "FOO.Bar.low.lOW") s/FOLD ->3 >=5.017002
363 # - <1> ex-rv2sv sKRM*/1 ->4
364 # 3 <$> gvsv(*s) s ->4
365 # 5 <;> nextstate(main 916 optree_constants.t:308) v:>,<,%,{ ->6
367 # 6 <0> pushmark s ->7
368 # 7 <$> const(PV "a-lt-b") s ->8
369 # 9 <;> nextstate(main 916 optree_constants.t:309) v:>,<,%,{ ->a
371 # a <0> pushmark s ->b
372 # b <$> const(PV "b-gt-a") s ->c
373 # d <;> nextstate(main 916 optree_constants.t:310) v:>,<,%,{ ->e
375 # e <0> pushmark s ->f
376 # f <$> const(PV "a-le-b") s ->g
377 # h <;> nextstate(main 916 optree_constants.t:311) v:>,<,%,{ ->i
379 # i <0> pushmark s ->j
380 # j <$> const(PV "b-ge-a") s ->k
381 # l <;> nextstate(main 916 optree_constants.t:312) v:>,<,%,{ ->m
383 # m <0> pushmark s ->n
384 # n <$> const(PV "b-cmp-a") s ->o
385 # p <;> nextstate(main 916 optree_constants.t:313) v:>,<,%,{ ->q
386 # q <$> const(SPECIAL sv_no) s/SHORT ->r < 5.017002
387 # q <$> const(SPECIAL sv_no) s/FOLD,SHORT ->r >=5.017002
390 checkOptree ( name => 'mixed constant folding, with explicit braces',
391 code => 'print "foo"."bar".(2+3)',
392 strip_open_hints => 1,
393 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
394 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
395 # - <@> lineseq KP ->5
396 # 1 <;> nextstate(main 977 (eval 28):1) v ->2
398 # 2 <0> pushmark s ->3
399 # 3 <$> const[PV "foobar5"] s ->4 < 5.017002
400 # 3 <$> const[PV "foobar5"] s/FOLD ->4 >=5.017002
402 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
403 # - <@> lineseq KP ->5
404 # 1 <;> nextstate(main 977 (eval 28):1) v ->2
406 # 2 <0> pushmark s ->3
407 # 3 <$> const(PV "foobar5") s ->4 < 5.017002
408 # 3 <$> const(PV "foobar5") s/FOLD ->4 >=5.017002
415 Optimized constant subs are stored as bare scalars in the stash
416 (package hash), which formerly held only GVs (typeglobs).
418 But you cant create them manually - you cant assign a scalar to a
419 stash element, and expect it to work like a constant-sub, even if you
422 This is a feature; alternative is too much action-at-a-distance. The
423 following test demonstrates - napier is not seen as a function at all,
424 much less an optimized one.
428 checkOptree ( name => 'not evertnapier',
431 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');