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. 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 58// 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']; 61var or_trees = makeTrees("|", identifiers); 62var and_trees = makeTrees("&", identifiers); 63 64// Set up leaf masks to set 6 least-significant bits. 65var a = 1 << 0; 66var b = 1 << 1; 67var c = 1 << 2; 68var d = 1 << 3; 69var e = 1 << 4; 70var f = 1 << 5; 71 72for (var i = 0; i < or_trees.length; ++i) { 73 for (var j = 0; j < 6; ++j) { 74 var or_fun = new Function("return " + or_trees[i]); 75 if (j == 0) assertEquals(63, or_fun()); 76 77 // Set the j'th variable to a string to force a bailout. 78 eval(identifiers[j] + "+= ''"); 79 assertEquals(63, or_fun()); 80 // Set it back to a number for the next iteration. 81 eval(identifiers[j] + "= +" + identifiers[j]); 82 } 83} 84 85// Set up leaf masks to clear 6 least-significant bits. 86a ^= 63; 87b ^= 63; 88c ^= 63; 89d ^= 63; 90e ^= 63; 91f ^= 63; 92 93for (i = 0; i < and_trees.length; ++i) { 94 for (var j = 0; j < 6; ++j) { 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} 105