Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1 | // 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. |
| 33 | function 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | // All possible bitwise OR trees with six leaves, i.e. CatalanNumber[5] = 42, |
| 59 | // see http://mathworld.wolfram.com/CatalanNumber.html. |
| 60 | var identifiers = ['a','b','c','d','e','f']; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 61 | var or_trees = makeTrees("|", identifiers); |
| 62 | var and_trees = makeTrees("&", identifiers); |
| 63 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | // Set up leaf masks to set 6 least-significant bits. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 65 | var a = 1 << 0; |
| 66 | var b = 1 << 1; |
| 67 | var c = 1 << 2; |
| 68 | var d = 1 << 3; |
| 69 | var e = 1 << 4; |
| 70 | var f = 1 << 5; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 71 | |
| 72 | for (var i = 0; i < or_trees.length; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | for (var j = 0; j < 6; ++j) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 74 | var or_fun = new Function("return " + or_trees[i]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | if (j == 0) assertEquals(63, or_fun()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 76 | |
| 77 | // Set the j'th variable to a string to force a bailout. |
| 78 | eval(identifiers[j] + "+= ''"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | assertEquals(63, or_fun()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 80 | // Set it back to a number for the next iteration. |
| 81 | eval(identifiers[j] + "= +" + identifiers[j]); |
| 82 | } |
| 83 | } |
| 84 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | // Set up leaf masks to clear 6 least-significant bits. |
| 86 | a ^= 63; |
| 87 | b ^= 63; |
| 88 | c ^= 63; |
| 89 | d ^= 63; |
| 90 | e ^= 63; |
| 91 | f ^= 63; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 92 | |
| 93 | for (i = 0; i < and_trees.length; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | for (var j = 0; j < 6; ++j) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 95 | 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 | } |