3 # Use B to test that optimisations are not inadvertently removed,
4 # by examining particular nodes in the optree.
12 skip_all_if_miniperl("No B under miniperl");
19 use B qw(svref_2object
20 OPpASSIGN_COMMON_SCALAR
28 # Test that OP_AASSIGN gets the appropriate
29 # OPpASSIGN_COMMON* flags set.
31 # Too few flags set is likely to cause code to misbehave;
32 # too many flags set unnecessarily slows things down.
33 # See also the tests in t/op/aassign.t
36 # Each anon array contains:
39 # a 3 char string, each char showing whether we expect a
40 # particular flag to be set:
41 # '-' indicates any char not set, while
42 # 'S': char 0: OPpASSIGN_COMMON_SCALAR,
43 # 'R': char 1: OPpASSIGN_COMMON_RC1,
44 # 'A' char 2: OPpASSIGN_COMMON_AGG,
49 [ "---", '() = (1, $x, my $y, @z, f($p))', 'no LHS' ],
50 [ "---", '(undef, $x, my $y, @z, ($a ? $b : $c)) = ()', 'no RHS' ],
51 [ "---", '(undef, $x, my $y, @z, ($a ? $b : $c)) = (1,2)', 'safe RHS' ],
52 [ "---", 'my @a = (1,2)', 'safe RHS: my array' ],
53 [ "---", 'my %h = (1,2)', 'safe RHS: my hash' ],
54 [ "---", 'my ($a,$b,$c,$d) = 1..6; ($a,$b) = ($c,$d);', 'non-common lex' ],
55 [ "---", '($x,$y) = (1,2)', 'pkg var LHS only' ],
56 [ "---", 'my $p; my ($x,$y) = ($p, $p)', 'my; dup lex var on RHS' ],
57 [ "---", 'my $p; my ($x,$y); ($x,$y) = ($p, $p)', 'dup lex var on RHS' ],
58 [ "---", 'my ($self) = @_', 'LHS lex scalar only' ],
59 [ "--A", 'my ($self, @rest) = @_', 'LHS lex mixed' ],
60 [ "-R-", 'my ($x,$y) = ($p, $q)', 'pkg var RHS only' ],
61 [ "S--", '($x,$y) = ($p, $q)', 'pkg scalar both sides' ],
62 [ "--A", 'my (@a, @b); @a = @b', 'lex ary both sides' ],
63 [ "-R-", 'my ($x,$y,$z,@a); ($x,$y,$z) = @a ', 'lex vars to lex ary' ],
64 [ "--A", '@a = @b', 'pkg ary both sides' ],
65 [ "--A", 'my (%a,%b); %a = %b', 'lex hash both sides' ],
66 [ "--A", '%a = %b', 'pkg hash both sides' ],
67 [ "--A", 'my $x; @a = ($a[0], $a[$x])', 'common ary' ],
68 [ "--A", 'my ($x,@a); @a = ($a[0], $a[$x])', 'common lex ary' ],
69 [ "S-A", 'my $x; ($a[$x], $a[0]) = ($a[0], $a[$x])', 'common ary elems' ],
70 [ "S-A", 'my ($x,@a); ($a[$x], $a[0]) = ($a[0], $a[$x])',
71 'common lex ary elems' ],
72 [ "--A", 'my $x; my @a = @$x', 'lex ary may have stuff' ],
73 [ "-RA", 'my $x; my ($b, @a) = @$x', 'lex ary may have stuff' ],
74 [ "--A", 'my $x; my %a = @$x', 'lex hash may have stuff' ],
75 [ "-RA", 'my $x; my ($b, %a) = @$x', 'lex hash may have stuff' ],
76 [ "--A", 'my (@a,@b); @a = ($b[0])', 'lex ary and elem' ],
77 [ "S-A", 'my @a; ($a[1],$a[0]) = @a', 'lex ary and elem' ],
78 [ "--A", 'my @x; @y = $x[0]', 'pkg ary from lex elem' ],
79 [ "---", '(undef,$x) = f()', 'single scalar on LHS' ],
80 [ "---", '($x,$y) = ($x)', 'single scalar on RHS, no AGG' ],
81 [ "--A", '($x,@b) = ($x)', 'single scalar on RHS' ],
82 [ "--A", 'my @a; @a = (@a = split())', 'split a/a' ],
83 [ "--A", 'my (@a,@b); @a = (@b = split())', 'split a/b' ],
84 [ "---", 'my @a; @a = (split(), 1)', '(split(),1)' ],
85 [ "---", '@a = (split(//, @a), 1)', 'split(@a)' ],
86 [ "--A", 'my @a; my $ar = @a; @a = (@$ar = split())', 'a/ar split' ],
89 my ($exp, $code, $desc) = @$test;
92 # package vars used in code snippets
93 our (@a, %a, @b, %b, $c, $p, $q, $x, $y, @y, @z);
95 $sub = eval "sub { $code }"
97 "aassign eval('$code') failed: this test needs"
98 . "to be rewritten:\n$@"
101 my $last_expr = svref_2object($sub)->ROOT->first->last;
102 if ($last_expr->name ne 'aassign') {
103 die "Expected aassign but found ", $last_expr->name,
104 "; this test needs to be rewritten"
107 (($last_expr->private & OPpASSIGN_COMMON_SCALAR) ? 'S' : '-')
108 . (($last_expr->private & OPpASSIGN_COMMON_RC1) ? 'R' : '-')
109 . (($last_expr->private & OPpASSIGN_COMMON_AGG) ? 'A' : '-');
110 is $got, $exp, "OPpASSIGN_COMMON: $desc: '$code'";
114 # join -> stringify/const
116 for (['CONSTANT', sub { join "foo", $_ }],
117 ['$var' , sub { join $_ , $_ }],
118 ['$myvar' , sub { my $var; join $var, $_ }],
121 my $last_expr = svref_2object($sub)->ROOT->first->last;
122 is $last_expr->name, 'stringify',
123 "join($sep, \$scalar) optimised to stringify";
126 for (['CONSTANT', sub { join "foo", "bar" }, 0, "bar" ],
127 ['CONSTANT', sub { join "foo", "bar", 3 }, 1, "barfoo3"],
128 ['$var' , sub { join $_ , "bar" }, 0, "bar" ],
129 ['$myvar' , sub { my $var; join $var, "bar" }, 0, "bar" ],
131 my($sep,$sub,$is_list,$expect) = @$_;
132 my $last_expr = svref_2object($sub)->ROOT->first->last;
133 my $tn = "join($sep, " . ($is_list?'list of constants':'const') . ")";
134 is $last_expr->name, 'const', "$tn optimised to constant";
135 is $sub->(), $expect, "$tn folded correctly";
139 # list+pushmark in list context elided out of the execution chain
140 is svref_2object(sub { () = ($_, ($_, $_)) })
144 ->next # should be gvsv, not pushmark
146 "list+pushmark in list context where list's elder sibling is a null";
149 # nextstate multiple times becoming one nextstate
151 is svref_2object(sub { 0;0;0;0;0;0;time })->START->next->name, 'time',
152 'multiple nextstates become one';
155 # pad[ahs]v state declarations in void context
157 is svref_2object(sub{state($foo,@fit,%far);state $bar;state($a,$b); time})
158 ->START->next->name, 'time',
159 'pad[ahs]v state declarations in void context';
162 # pushmark-padsv-padav-padhv in list context --> padrange
166 my $sub = sub { \my( $f, @f, %f ) };
167 my $op = svref_2object($sub)->START;
168 push(@ops, $op->name), $op = $op->next while $$op;
169 is "@ops", "nextstate padrange refgen leavesub", 'multi-type padrange'
173 # rv2[ahs]v in void context
175 is svref_2object(sub { our($foo,@fit,%far); our $bar; our($a,$b); time })
176 ->START->next->name, 'time',
177 'rv2[ahs]v in void context';
182 for(['@pkgary' , '@_' ],
183 ['@lexary' , 'my @a; @a'],
184 ['my(@array)' , 'my(@a)' ],
185 ['local(@array)', 'local(@_)'],
186 ['@{...}' , '@{\@_}' ],
189 my $sub = eval "sub { $code = split }";
190 my $split = svref_2object($sub)->ROOT->first->last;
191 is $split->name, 'split', "$tn = split swallows up the assignment";
195 # stringify with join kid --> join
196 is svref_2object(sub { "@_" })->ROOT->first->last->name, 'join',
197 'qq"@_" optimised from stringify(join(...)) to join(...)';
200 # Check that certain ops, when in boolean context, have the
201 # right private "is boolean" or "maybe boolean" flags set.
203 # A maybe flag is set when the context at the end of a chain of and/or/dor
204 # ops isn't known till runtime, e.g.
205 # sub f { ....; ((%h || $x) || $y)) }
206 # If f() is called in void context, then %h can return a boolean value;
207 # if in scalar context, %h must return a key count.
209 # With (op && other), its ok to treat op as in bool cxt even when the &&
210 # is in scalar cxt, as long as whatever op returns as a false boolean value
211 # matches what it returns as a false scalar value (IV(0) in the case of
212 # rv2hv etc). This is because in (A && B), A is returned only when A is
216 # op code op path flag maybe flag
217 [ 'rv2hv', '%pkg', [], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
218 [ 'rv2hv', 'scalar(%pkg)', [0], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
219 [ 'padhv', '%lex', [], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
220 [ 'padhv', 'scalar(%lex)', [0], OPpTRUEBOOL, OPpMAYBE_TRUEBOOL ],
222 my ($op_name, $op_code, $post_op_path, $bool_flag, $maybe_flag) = @$ops;
225 # 1st column: what to expect for each $context (void, scalar, unknown),
227 # 1: expect bool flag
228 # 2: expect maybe bool flag
230 # 2nd column: path though the op subtree to the flagged op:
231 # 0 is first child, 1 is second child etc.
232 # Will have @$post_op_path from above appended.
233 # 3rd column: code to execute: %s holds the code for the op
239 [ [0,0,0], [], '%s' ],
240 [ [1,9,1], [0,0], 'if (%s) {$x}' ],
241 [ [1,9,1], [0,0], 'if (%s) {$x} else {$y}' ],
242 [ [1,9,2], [0,0], 'unless (%s) {$x}' ],
246 [ [1,1,1], [0], '!%s' ],
247 [ [1,9,1], [0,0,0], 'if (!%s) {$x}' ],
248 [ [1,9,1], [0,0,0], 'if (!%s) {$x} else {$y}' ],
249 [ [1,9,1], [0,0,0], 'unless (!%s) {$x}' ],
253 [ [1,1,1], [0,0,], '%s ? $p : $q' ],
254 [ [1,9,1], [0,0,0,0], 'if (%s ? $p : $q) {$x}' ],
255 [ [1,9,1], [0,0,0,0], 'if (%s ? $p : $q) {$x} else {$y}' ],
256 [ [1,9,1], [0,0,0,0], 'unless (%s ? $p : $q) {$x}' ],
261 [ [1,0,2], [0,0], '%s || $x' ],
262 [ [1,1,1], [0,0,0], '!(%s || $x)' ],
263 [ [1,0,2], [0,1,0,0], '$y && (%s || $x)' ],
264 [ [1,9,1], [0,0,0,0], 'if (%s || $x) {$x}' ],
265 [ [1,9,1], [0,0,0,0], 'if (%s || $x) {$x} else {$y}' ],
266 [ [1,9,2], [0,0,0,0], 'unless (%s || $x) {$x}' ],
270 [ [0,0,0], [0,1], '$x || %s' ],
271 [ [1,1,1], [0,0,1], '!($x || %s)' ],
272 [ [0,0,0], [0,1,0,1], '$y && ($x || %s)' ],
273 [ [1,9,1], [0,0,0,1], 'if ($x || %s) {$x}' ],
274 [ [1,9,1], [0,0,0,1], 'if ($x || %s) {$x} else {$y}' ],
275 [ [1,9,2], [0,0,0,1], 'unless ($x || %s) {$x}' ],
279 [ [1,0,2], [0,0], '%s // $x' ],
280 [ [1,1,1], [0,0,0], '!(%s // $x)' ],
281 [ [1,0,2], [0,1,0,0], '$y && (%s // $x)' ],
282 [ [1,9,1], [0,0,0,0], 'if (%s // $x) {$x}' ],
283 [ [1,9,2], [0,0,0,0], 'unless (%s // $x) {$x}' ],
284 [ [1,9,1], [0,0,0,0], 'if (%s // $x) {$x}' ],
285 [ [1,9,1], [0,0,0,0], 'if (%s // $x) {$x} else {$y}' ],
286 [ [1,9,2], [0,0,0,0], 'unless (%s // $x) {$x}' ],
290 [ [0,0,0], [0,1], '$x // %s' ],
291 [ [1,1,1], [0,0,1], '!($x // %s)' ],
292 [ [0,0,0], [0,1,0,1], '$y && ($x // %s)' ],
293 [ [1,9,1], [0,0,0,1], 'if ($x // %s) {$x}' ],
294 [ [1,9,1], [0,0,0,1], 'if ($x // %s) {$x} else {$y}' ],
295 [ [1,9,2], [0,0,0,1], 'unless ($x // %s) {$x}' ],
299 [ [1,1,1], [0,0], '%s && $x' ],
300 [ [1,1,1], [0,0,0], '!(%s && $x)' ],
301 [ [1,1,1], [0,1,0,0], '$y || (%s && $x)' ],
302 [ [1,9,1], [0,0,0,0], 'if (%s && $x) {$x}' ],
303 [ [1,9,1], [0,0,0,0], 'if (%s && $x) {$x} else {$y}' ],
304 [ [1,9,1], [0,0,0,0], 'unless (%s && $x) {$x}' ],
308 [ [0,0,0], [0,1], '$x && %s' ],
309 [ [1,1,1], [0,0,1], '!($x && %s)' ],
310 [ [0,0,0], [0,1,0,1], '$y || ($x && %s)' ],
311 [ [1,9,1], [0,0,0,1], 'if ($x && %s) {$x}' ],
312 [ [1,9,1], [0,0,0,1], 'if ($x && %s) {$x} else {$y}' ],
313 [ [1,9,2], [0,0,0,1], 'unless ($x && %s) {$x}' ],
316 my ($expects, $op_path, $code_fmt) = @$test;
318 for my $context (0,1,2) {
322 # 9: skip test (principally if() can't be in scalar context)
324 next if $expects->[$context] == 9;
326 my $base_code = sprintf $code_fmt, $op_code;
327 my $code = $base_code;
328 my @op_path = @$op_path;
329 push @op_path, @$post_op_path;
331 # where to find the expression in the top-level lineseq
338 elsif ($context == 1) {
339 $code = "\$r = ($code)";
347 my (%lex, $p, $q, $r, $x, $y);
350 $sub = eval "sub { $code }"
352 "eval'$code' failed: this test needs to be rewritten;\n"
353 . "Errors were:\n$@";
356 # find the expression subtree in the main lineseq of the sub
357 my $expr = svref_2object($sub)->ROOT->first;
359 my $next = $expr->first;
362 $next = $next->sibling;
364 $expr = $ops[$seq_offset];
366 # search through the expr subtree looking for the named op -
367 # this assumes that for all the code examples above, the
368 # op is always in the LH branch
369 while (defined (my $p = shift @op_path)) {
370 $expr = $expr->first;
371 $expr = $expr->sibling while $p--;
374 if (!$expr || $expr->name ne $op_name) {
375 die "Can't find $op_name op in optree for '$code'; "
376 . "this test needs to be rewritten"
379 my $exp = $expects->[$context];
381 : $exp == 1 ? $bool_flag
384 my $got = ($expr->private & ($bool_flag | $maybe_flag));
385 my $cxt_name = ('void ', 'scalar ', 'unknown')[$context];
386 is $got, $exp, "boolean: $op_name $cxt_name '$base_code'";