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
43 => qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
46 sub pi () { 3.14159 };
47 $::{napier} = \2.71828; # counter-example (doesn't get optimized).
48 eval "sub napier ();";
51 # should be able to undefine constant::import here ???
53 # eval 'sub constant::import () {}';
54 # undef *constant::import::{CODE};
57 #################################
58 pass("CONSTANT SUBS RETURNING SCALARS");
60 checkOptree ( name => 'myint() as coderef',
63 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
64 is a constant sub, optimized to a IV
66 is a constant sub, optimized to a IV
70 checkOptree ( name => 'mystr() as coderef',
73 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
74 is a constant sub, optimized to a PV
76 is a constant sub, optimized to a PV
80 checkOptree ( name => 'myfl() as coderef',
83 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
84 is a constant sub, optimized to a NV
86 is a constant sub, optimized to a NV
90 checkOptree ( name => 'myrex() as coderef',
93 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
94 is a constant sub, optimized to a RV
96 is a constant sub, optimized to a RV
100 checkOptree ( name => 'myglob() as coderef',
103 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
104 is a constant sub, optimized to a RV
106 is a constant sub, optimized to a RV
110 checkOptree ( name => 'myaref() as coderef',
113 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
114 is a constant sub, optimized to a RV
116 is a constant sub, optimized to a RV
120 checkOptree ( name => 'myhref() as coderef',
123 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
124 is a constant sub, optimized to a RV
126 is a constant sub, optimized to a RV
130 checkOptree ( name => 'myundef() as coderef',
133 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
134 is a constant sub, optimized to a NULL
136 is a constant sub, optimized to a NULL
140 checkOptree ( name => 'mysub() as coderef',
143 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
144 is a constant sub, optimized to a RV
146 is a constant sub, optimized to a RV
150 checkOptree ( name => 'myunsub() as coderef',
151 todo => '- may prove only that sub is unformed',
154 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
163 checkOptree ( name => 'call myint',
165 bc_opts => '-nobanner',
166 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
167 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
169 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
170 2 <$> const[IV 42] s ->3
172 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
174 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
175 2 <$> const(IV 42) s ->3
179 checkOptree ( name => 'call mystr',
181 bc_opts => '-nobanner',
182 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
183 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
185 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
186 2 <$> const[PV "hithere"] s ->3
188 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
190 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
191 2 <$> const(PV "hithere") s ->3
195 checkOptree ( name => 'call myfl',
197 bc_opts => '-nobanner',
198 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
199 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
201 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
202 2 <$> const[NV 3.14159] s ->3
204 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
206 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
207 2 <$> const(NV 3.14159) s ->3
211 checkOptree ( name => 'call myrex',
213 todo => '- RV value is bare backslash',
215 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
216 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
217 # - <@> lineseq KP ->3
218 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
219 # 2 <$> const[RV \\] s ->3
221 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
222 # - <@> lineseq KP ->3
223 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
224 # 2 <$> const(RV \\) s ->3
228 checkOptree ( name => 'call myglob',
230 todo => '- RV value is bare backslash',
232 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
233 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
234 # - <@> lineseq KP ->3
235 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
236 # 2 <$> const[RV \\] s ->3
238 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
239 # - <@> lineseq KP ->3
240 # 1 <;> nextstate(main 753 (eval 27):1) v ->2
241 # 2 <$> const(RV \\) s ->3
245 checkOptree ( name => 'call myaref',
247 todo => '- RV value is bare backslash',
249 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
250 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
251 # - <@> lineseq KP ->3
252 # 1 <;> nextstate(main 758 (eval 29):1) v ->2
253 # 2 <$> const[RV \\] s ->3
255 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
256 # - <@> lineseq KP ->3
257 # 1 <;> nextstate(main 758 (eval 29):1) v ->2
258 # 2 <$> const(RV \\) s ->3
262 checkOptree ( name => 'call myhref',
265 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
266 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
267 # - <@> lineseq KP ->3
268 # 1 <;> nextstate(main 763 (eval 31):1) v ->2
269 # 2 <$> const[RV \\HASH] s ->3
271 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
272 # - <@> lineseq KP ->3
273 # 1 <;> nextstate(main 763 (eval 31):1) v ->2
274 # 2 <$> const(RV \\HASH) s ->3
278 checkOptree ( name => 'call myundef',
281 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
282 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
283 # - <@> lineseq KP ->3
284 # 1 <;> nextstate(main 771 (eval 35):1) v ->2
285 # 2 <$> const[NULL ] s ->3
287 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
288 # - <@> lineseq KP ->3
289 # 1 <;> nextstate(main 771 (eval 35):1) v ->2
290 # 2 <$> const(NULL ) s ->3
294 checkOptree ( name => 'call mysub',
297 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
298 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
299 # - <@> lineseq KP ->3
300 # 1 <;> nextstate(main 771 (eval 35):1) v ->2
301 # 2 <$> const[RV \\] s ->3
303 # 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
304 # - <@> lineseq KP ->3
305 # 1 <;> nextstate(main 771 (eval 35):1) v ->2
306 # 2 <$> const(RV \\) s ->3
311 # test constant sub defined w/o 'use constant'
313 checkOptree ( name => "pi(), defined w/o 'use constant'",
316 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
317 is a constant sub, optimized to a NV
319 is a constant sub, optimized to a NV
323 checkOptree ( name => 'constant sub returning list',
326 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
327 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
328 # - <@> lineseq K ->3
329 # 1 <;> nextstate(constant 685 constant.pm:121) v ->2
330 # 2 <0> padav[@list:FAKE:m:102] ->3
332 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
333 # - <@> lineseq K ->3
334 # 1 <;> nextstate(constant 685 constant.pm:121) v ->2
335 # 2 <0> padav[@list:FAKE:m:76] ->3
340 printf "myint %d mystr %s myfl %f pi %f\n"
341 , myint, mystr, myfl, pi;
344 checkOptree ( name => 'call many in a print statement',
346 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
347 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
348 # - <@> lineseq KP ->9
349 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
351 # 2 <0> pushmark s ->3
352 # 3 <$> const[PV "myint %d mystr %s myfl %f pi %f\n"] s ->4
353 # 4 <$> const[IV 42] s ->5
354 # 5 <$> const[PV "hithere"] s ->6
355 # 6 <$> const[NV 3.14159] s ->7
356 # 7 <$> const[NV 3.14159] s ->8
358 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
359 # - <@> lineseq KP ->9
360 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
362 # 2 <0> pushmark s ->3
363 # 3 <$> const(PV "myint %d mystr %s myfl %f pi %f\n") s ->4
364 # 4 <$> const(IV 42) s ->5
365 # 5 <$> const(PV "hithere") s ->6
366 # 6 <$> const(NV 3.14159) s ->7
367 # 7 <$> const(NV 3.14159) s ->8
377 Optimized constant subs are stored as bare scalars in the stash
378 (package hash), which formerly held only GVs (typeglobs).
380 But you cant create them manually - you cant assign a scalar to a
381 stash element, and expect it to work like a constant-sub, even if you
384 This is a feature; alternative is too much action-at-a-distance. The
385 following test demonstrates - napier is not seen as a function at all,
386 much less an optimized one.
390 checkOptree ( name => 'not evertnapier',
393 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');