blob: 0d971a95b25ef986e2909ea059bb38a6e883055e [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --always-opt --nocompilation-cache
29
30// Given a binary operation string and an ordered array of leaf
31// strings, return an array of all binary tree strings with the leaves
32// (in order) as the fringe.
33function makeTrees(op, leaves) {
34 var len = leaves.length;
35 if (len == 1) {
36 // One leaf is a leaf.
37 return leaves;
38 } else {
39 // More than one leaf requires an interior node.
40 var result = [];
41 // Split the leaves into left and right subtrees in all possible
42 // ways. For each split recursively compute all possible subtrees.
43 for (var i = 1; i < len; ++i) {
44 var leftTrees = makeTrees(op, leaves.slice(0, i));
45 var rightTrees = makeTrees(op, leaves.slice(i, len));
46 // Adjoin every possible left and right subtree.
47 for (var j = 0; j < leftTrees.length; ++j) {
48 for (var k = 0; k < rightTrees.length; ++k) {
49 var string = "(" + leftTrees[j] + op + rightTrees[k] + ")";
50 result.push(string);
51 }
52 }
53 }
54 return result;
55 }
56}
57
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058// All possible bitwise OR trees with six leaves, i.e. CatalanNumber[5] = 42,
59// see http://mathworld.wolfram.com/CatalanNumber.html.
60var identifiers = ['a','b','c','d','e','f'];
Ben Murdochb0fe1622011-05-05 13:52:32 +010061var or_trees = makeTrees("|", identifiers);
62var and_trees = makeTrees("&", identifiers);
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064// Set up leaf masks to set 6 least-significant bits.
Ben Murdochb0fe1622011-05-05 13:52:32 +010065var a = 1 << 0;
66var b = 1 << 1;
67var c = 1 << 2;
68var d = 1 << 3;
69var e = 1 << 4;
70var f = 1 << 5;
Ben Murdochb0fe1622011-05-05 13:52:32 +010071
72for (var i = 0; i < or_trees.length; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 for (var j = 0; j < 6; ++j) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010074 var or_fun = new Function("return " + or_trees[i]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 if (j == 0) assertEquals(63, or_fun());
Ben Murdochb0fe1622011-05-05 13:52:32 +010076
77 // Set the j'th variable to a string to force a bailout.
78 eval(identifiers[j] + "+= ''");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 assertEquals(63, or_fun());
Ben Murdochb0fe1622011-05-05 13:52:32 +010080 // Set it back to a number for the next iteration.
81 eval(identifiers[j] + "= +" + identifiers[j]);
82 }
83}
84
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085// Set up leaf masks to clear 6 least-significant bits.
86a ^= 63;
87b ^= 63;
88c ^= 63;
89d ^= 63;
90e ^= 63;
91f ^= 63;
Ben Murdochb0fe1622011-05-05 13:52:32 +010092
93for (i = 0; i < and_trees.length; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 for (var j = 0; j < 6; ++j) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010095 var and_fun = new Function("return " + and_trees[i]);
96 if (j == 0) assertEquals(0, and_fun());
97
98 // Set the j'th variable to a string to force a bailout.
99 eval(identifiers[j] + "+= ''");
100 assertEquals(0, and_fun());
101 // Set it back to a number for the next iteration.
102 eval(identifiers[j] + "= +" + identifiers[j]);
103 }
104}