6 @INC = ('.', '../lib', '../ext/B/t');
12 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13 print "1..0 # Skip -- Perl configured without B module\n";
16 # require 'test.pl'; # now done by OptreeCheck
19 use OptreeCheck; # ALSO DOES @ARGV HANDLING !!!!!!
25 skip "no perlio in this build", $tests unless $Config::Config{useperlio};
29 if (((caller 0)[10]||{})->{open}) {
30 @open_todo = (skip => "\$^OPEN is set");
35 #################################
37 use constant { # see also t/op/gv.t line 282
51 sub myno () { return 1!=1 }
52 sub pi () { 3.14159 };
54 my $want = { # expected types, how value renders in-line, todos (maybe)
55 myfl => [ 'NV', myfl ],
56 myint => [ 'IV', myint ],
57 mystr => [ 'PV', '"'.mystr.'"' ],
58 myhref => [ 'RV', '\\\\HASH'],
59 myundef => [ 'NULL', ],
61 myaref => [ 'RV', '\\\\' ],
62 myglob => [ 'RV', '\\\\' ],
63 myrex => [ 'RV', '\\\\' ],
64 mysub => [ 'RV', '\\\\' ],
65 myunsub => [ 'RV', '\\\\' ],
66 # these are not inlined, at least not per BC::Concise
72 => qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
75 $::{napier} = \2.71828; # counter-example (doesn't get optimized).
76 eval "sub napier ();";
79 # should be able to undefine constant::import here ???
81 # eval 'sub constant::import () {}';
82 # undef *constant::import::{CODE};
85 #################################
86 pass("RENDER CONSTANT SUBS RETURNING SCALARS");
88 for $func (sort keys %$want) {
89 # no strict 'refs'; # why not needed ?
90 checkOptree ( name => "$func() as a coderef",
93 expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
94 is a constant sub, optimized to a $want->{$func}[0]
96 is a constant sub, optimized to a $want->{$func}[0]
101 pass("RENDER CALLS TO THOSE CONSTANT SUBS");
103 for $func (sort keys %$want) {
104 # print "# doing $func\n";
105 checkOptree ( name => "call $func",
107 ($want->{$func}[2]) ? ( todo => $want->{$func}[2]) : (),
108 bc_opts => '-nobanner',
109 expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
110 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
111 - <\@> lineseq KP ->3
112 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
113 2 <\$> const[$want->{$func}[0] $want->{$func}[1]] s ->3
115 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
116 - <\@> lineseq KP ->3
117 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
118 2 <\$> const($want->{$func}[0] $want->{$func}[1]) s ->3
126 checkOptree ( name => 'myyes() as coderef',
127 code => sub () { 1==1 },
129 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
130 is a constant sub, optimized to a SPECIAL
132 is a constant sub, optimized to a SPECIAL
136 checkOptree ( name => 'myyes() as coderef',
137 code => 'sub a() { 1==1 }; print a',
139 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
140 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
141 # - <@> lineseq KP ->5
142 # 1 <;> nextstate(main 810 (eval 47):1) v ->2
144 # 2 <0> pushmark s ->3
145 # 3 <$> const[SPECIAL sv_yes] s ->4
147 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
148 # - <@> lineseq KP ->5
149 # 1 <;> nextstate(main 810 (eval 47):1) v ->2
151 # 2 <0> pushmark s ->3
152 # 3 <$> const(SPECIAL sv_yes) s ->4
156 checkOptree ( name => 'myno() as coderef',
157 code => 'sub a() { 1!=1 }; print a',
159 todo => '- SPECIAL sv_no renders as PVNV 0',
160 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
161 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
162 # - <@> lineseq KP ->5
163 # 1 <;> nextstate(main 810 (eval 47):1) v ->2
165 # 2 <0> pushmark s ->3
166 # 3 <$> const[PVNV 0] s ->4
168 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
169 # - <@> lineseq KP ->5
170 # 1 <;> nextstate(main 810 (eval 47):1) v ->2
172 # 2 <0> pushmark s ->3
173 # 3 <$> const(PVNV 0) s ->4
177 checkOptree ( name => 'constant sub returning list',
180 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
181 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
182 # - <@> lineseq K ->3
183 # 1 <;> nextstate(constant 685 constant.pm:121) v:*,& ->2
184 # 2 <0> padav[@list:FAKE:m:102] ->3
186 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
187 # - <@> lineseq K ->3
188 # 1 <;> nextstate(constant 685 constant.pm:121) v:*,& ->2
189 # 2 <0> padav[@list:FAKE:m:76] ->3
194 printf "myint %d mystr %s myfl %f pi %f\n"
195 , myint, mystr, myfl, pi;
198 checkOptree ( name => 'call many in a print statement',
201 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
202 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
203 # - <@> lineseq KP ->9
204 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
206 # 2 <0> pushmark s ->3
207 # 3 <$> const[PV "myint %d mystr %s myfl %f pi %f\n"] s ->4
208 # 4 <$> const[IV 42] s ->5
209 # 5 <$> const[PV "hithere"] s ->6
210 # 6 <$> const[NV 1.414213] s ->7
211 # 7 <$> const[NV 3.14159] s ->8
213 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
214 # - <@> lineseq KP ->9
215 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
217 # 2 <0> pushmark s ->3
218 # 3 <$> const(PV "myint %d mystr %s myfl %f pi %f\n") s ->4
219 # 4 <$> const(IV 42) s ->5
220 # 5 <$> const(PV "hithere") s ->6
221 # 6 <$> const(NV 1.414213) s ->7
222 # 7 <$> const(NV 3.14159) s ->8
232 Optimized constant subs are stored as bare scalars in the stash
233 (package hash), which formerly held only GVs (typeglobs).
235 But you cant create them manually - you cant assign a scalar to a
236 stash element, and expect it to work like a constant-sub, even if you
239 This is a feature; alternative is too much action-at-a-distance. The
240 following test demonstrates - napier is not seen as a function at all,
241 much less an optimized one.
245 checkOptree ( name => 'not evertnapier',
248 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');