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};
27 #################################
29 use constant { # see also t/op/gv.t line 282
40 => qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
43 sub pi () { 3.14159 };
44 $::{napier} = \2.71828; # counter-example (doesn't get optimized).
45 eval "sub napier ();";
48 # should be able to undefine constant::import here ???
50 # eval 'sub constant::import () {}';
51 # undef *constant::import::{CODE};
54 #################################
55 pass("CONSTANT SUBS RETURNING SCALARS");
57 checkOptree ( name => 'myint() as coderef',
60 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
61 is a constant sub, optimized to a IV
63 is a constant sub, optimized to a IV
67 checkOptree ( name => 'mystr() as coderef',
70 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
71 is a constant sub, optimized to a PV
73 is a constant sub, optimized to a PV
77 checkOptree ( name => 'myfl() as coderef',
80 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
81 is a constant sub, optimized to a NV
83 is a constant sub, optimized to a NV
87 checkOptree ( name => 'myrex() as coderef',
89 todo => '- currently renders as XS code',
91 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
98 checkOptree ( name => 'myglob() as coderef',
100 todo => '- currently renders as XS code',
102 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
109 checkOptree ( name => 'myaref() as coderef',
111 todo => '- currently renders as XS code',
113 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
120 checkOptree ( name => 'myhref() as coderef',
122 todo => '- currently renders as XS code',
124 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
133 checkOptree ( name => 'call myint',
135 bc_opts => '-nobanner',
136 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
137 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
139 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
140 2 <$> const[IV 42] s ->3
142 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
144 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
145 2 <$> const(IV 42) s ->3
149 checkOptree ( name => 'call mystr',
151 bc_opts => '-nobanner',
152 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
153 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
155 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
156 2 <$> const[PV "hithere"] s ->3
158 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
160 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
161 2 <$> const(PV "hithere") s ->3
165 checkOptree ( name => 'call myfl',
167 bc_opts => '-nobanner',
168 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
169 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
171 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
172 2 <$> const[NV 3.14159] s ->3
174 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
176 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
177 2 <$> const(NV 3.14159) s ->3
181 checkOptree ( name => 'call myrex',
183 todo => '- RV value is bare backslash',
185 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
186 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
187 # - <@> lineseq KP ->3
188 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
189 # 2 <$> const[RV \\] s ->3
191 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
192 # - <@> lineseq KP ->3
193 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
194 # 2 <$> const(RV \\) s ->3
198 checkOptree ( name => 'call myglob',
200 todo => '- RV value is bare backslash',
202 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
203 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
204 # - <@> lineseq KP ->3
205 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
206 # 2 <$> const[RV \\] s ->3
208 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
209 # - <@> lineseq KP ->3
210 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
211 # 2 <$> const(RV \\) s ->3
215 checkOptree ( name => 'call myaref',
217 todo => '- RV value is bare backslash',
219 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
220 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
221 # - <@> lineseq KP ->3
222 # 1 <;> nextstate(main 758 (eval 29):1) v ->2
223 # 2 <$> const[RV \\] s ->3
225 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
226 # - <@> lineseq KP ->3
227 # 1 <;> nextstate(main 758 (eval 29):1) v ->2
228 # 2 <$> const(RV \\) s ->3
232 checkOptree ( name => 'call myhref',
235 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
236 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
237 # - <@> lineseq KP ->3
238 # 1 <;> nextstate(main 763 (eval 31):1) v ->2
239 # 2 <$> const[RV \\HASH] s ->3
241 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
242 # - <@> lineseq KP ->3
243 # 1 <;> nextstate(main 763 (eval 31):1) v ->2
244 # 2 <$> const(RV \\HASH) s ->3
250 # test constant sub defined w/o 'use constant'
252 checkOptree ( name => "pi(), defined w/o 'use constant'",
255 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
256 is a constant sub, optimized to a NV
258 is a constant sub, optimized to a NV
262 checkOptree ( name => 'constant subs returning lists are not optimized',
265 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
266 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
267 # - <@> lineseq K ->3
268 # 1 <;> nextstate(constant 685 constant.pm:121) v ->2
269 # 2 <0> padav[@list:FAKE:m:102] ->3
271 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
272 # - <@> lineseq K ->3
273 # 1 <;> nextstate(constant 685 constant.pm:121) v ->2
274 # 2 <0> padav[@list:FAKE:m:76] ->3
279 printf "myint %d mystr %s myfl %f pi %f\n"
280 , myint, mystr, myfl, pi;
283 checkOptree ( name => 'call em all in a print statement',
285 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
286 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
287 # - <@> lineseq KP ->9
288 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
290 # 2 <0> pushmark s ->3
291 # 3 <$> const[PV "myint %d mystr %s myfl %f pi %f\n"] s ->4
292 # 4 <$> const[IV 42] s ->5
293 # 5 <$> const[PV "hithere"] s ->6
294 # 6 <$> const[NV 3.14159] s ->7
295 # 7 <$> const[NV 3.14159] s ->8
297 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
298 # - <@> lineseq KP ->9
299 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
301 # 2 <0> pushmark s ->3
302 # 3 <$> const(PV "myint %d mystr %s myfl %f pi %f\n") s ->4
303 # 4 <$> const(IV 42) s ->5
304 # 5 <$> const(PV "hithere") s ->6
305 # 6 <$> const(NV 3.14159) s ->7
306 # 7 <$> const(NV 3.14159) s ->8
316 Optimized constant subs are stored as bare scalars in the stash
317 (package hash), which formerly held only GVs (typeglobs).
319 But you cant create them manually - you cant assign a scalar to a
320 stash element, and expect it to work like a constant-sub, even if you
323 This is a feature; alternative is too much action-at-a-distance. The
324 following test demonstrates - napier is not seen as a function at all,
325 much less an optimized one.
329 checkOptree ( name => 'not evertnapier',
332 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');