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 sub myno () { return 1!=1 }
44 sub pi () { 3.14159 };
46 my $want = { # expected types, how value renders in-line, todos (maybe)
47 myfl => [ 'NV', myfl ],
48 myint => [ 'IV', myint ],
49 mystr => [ 'PV', '"'.mystr.'"' ],
50 myhref => [ 'RV', '\\\\HASH'],
51 myundef => [ 'NULL', ],
53 # these have todos, since they render as a bare backslash
54 myaref => [ 'RV', '\\\\', ' - should render as \\ARRAY' ],
55 myglob => [ 'RV', '\\\\', ' - should render as \\GV' ],
56 myrex => [ 'RV', '\\\\', ' - should render as ??' ],
57 mysub => [ 'RV', '\\\\', ' - should render as \\CV' ],
58 myunsub => [ 'RV', '\\\\', ' - should render as \\CV' ],
59 # these are not inlined, at least not per BC::Concise
65 => qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
68 $::{napier} = \2.71828; # counter-example (doesn't get optimized).
69 eval "sub napier ();";
72 # should be able to undefine constant::import here ???
74 # eval 'sub constant::import () {}';
75 # undef *constant::import::{CODE};
78 #################################
79 pass("RENDER CONSTANT SUBS RETURNING SCALARS");
81 for $func (sort keys %$want) {
82 # no strict 'refs'; # why not needed ?
83 checkOptree ( name => "$func() as a coderef",
86 expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
87 is a constant sub, optimized to a $want->{$func}[0]
89 is a constant sub, optimized to a $want->{$func}[0]
94 pass("RENDER CALLS TO THOSE CONSTANT SUBS");
96 for $func (sort keys %$want) {
97 # print "# doing $func\n";
98 checkOptree ( name => "call $func",
100 ($want->{$func}[2]) ? ( todo => $want->{$func}[2]) : (),
101 bc_opts => '-nobanner',
102 expect => <<EOT_EOT, expect_nt => <<EONT_EONT);
103 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
104 - <\@> lineseq KP ->3
105 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
106 2 <\$> const[$want->{$func}[0] $want->{$func}[1]] s ->3
108 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
109 - <\@> lineseq KP ->3
110 1 <;> dbstate(main 1163 OptreeCheck.pm:511]:1) v ->2
111 2 <\$> const($want->{$func}[0] $want->{$func}[1]) s ->3
119 checkOptree ( name => 'myyes() as coderef',
120 code => sub () { 1==1 },
122 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
123 is a constant sub, optimized to a SPECIAL
125 is a constant sub, optimized to a SPECIAL
129 checkOptree ( name => 'myyes() as coderef',
130 code => 'sub a() { 1==1 }; print a',
132 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
133 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
134 # - <@> lineseq KP ->5
135 # 1 <;> nextstate(main 810 (eval 47):1) v ->2
137 # 2 <0> pushmark s ->3
138 # 3 <$> const[SPECIAL sv_yes] s ->4
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
149 checkOptree ( name => 'myno() as coderef',
150 code => 'sub a() { 1!=1 }; print a',
152 todo => '- SPECIAL sv_no renders as PVNV 0',
153 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
154 # 5 <1> leavesub[1 ref] K/REFC,1 ->(end)
155 # - <@> lineseq KP ->5
156 # 1 <;> nextstate(main 810 (eval 47):1) v ->2
158 # 2 <0> pushmark s ->3
159 # 3 <$> const[PVNV 0] s ->4
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
170 checkOptree ( name => 'constant sub returning list',
173 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
174 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
175 # - <@> lineseq K ->3
176 # 1 <;> nextstate(constant 685 constant.pm:121) v:*,& ->2
177 # 2 <0> padav[@list:FAKE:m:102] ->3
179 # 3 <1> leavesub[2 refs] K/REFC,1 ->(end)
180 # - <@> lineseq K ->3
181 # 1 <;> nextstate(constant 685 constant.pm:121) v:*,& ->2
182 # 2 <0> padav[@list:FAKE:m:76] ->3
187 printf "myint %d mystr %s myfl %f pi %f\n"
188 , myint, mystr, myfl, pi;
191 checkOptree ( name => 'call many in a print statement',
193 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
194 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
195 # - <@> lineseq KP ->9
196 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
198 # 2 <0> pushmark s ->3
199 # 3 <$> const[PV "myint %d mystr %s myfl %f pi %f\n"] s ->4
200 # 4 <$> const[IV 42] s ->5
201 # 5 <$> const[PV "hithere"] s ->6
202 # 6 <$> const[NV 1.414213] s ->7
203 # 7 <$> const[NV 3.14159] s ->8
205 # 9 <1> leavesub[1 ref] K/REFC,1 ->(end)
206 # - <@> lineseq KP ->9
207 # 1 <;> nextstate(main 635 optree_constants.t:163) v ->2
209 # 2 <0> pushmark s ->3
210 # 3 <$> const(PV "myint %d mystr %s myfl %f pi %f\n") s ->4
211 # 4 <$> const(IV 42) s ->5
212 # 5 <$> const(PV "hithere") s ->6
213 # 6 <$> const(NV 1.414213) s ->7
214 # 7 <$> const(NV 3.14159) s ->8
224 Optimized constant subs are stored as bare scalars in the stash
225 (package hash), which formerly held only GVs (typeglobs).
227 But you cant create them manually - you cant assign a scalar to a
228 stash element, and expect it to work like a constant-sub, even if you
231 This is a feature; alternative is too much action-at-a-distance. The
232 following test demonstrates - napier is not seen as a function at all,
233 much less an optimized one.
237 checkOptree ( name => 'not evertnapier',
240 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');