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 429 possible bitwise OR trees with eight leaves.
59var identifiers = ['a','b','c','d','e','f','g','h'];
60var or_trees = makeTrees("|", identifiers);
61var and_trees = makeTrees("&", identifiers);
62
63// Set up leaf masks to set 8 least-significant bits.
64var a = 1 << 0;
65var b = 1 << 1;
66var c = 1 << 2;
67var d = 1 << 3;
68var e = 1 << 4;
69var f = 1 << 5;
70var g = 1 << 6;
71var h = 1 << 7;
72
73for (var i = 0; i < or_trees.length; ++i) {
74  for (var j = 0; j < 8; ++j) {
75    var or_fun = new Function("return " + or_trees[i]);
76    if (j == 0) assertEquals(255, or_fun());
77
78    // Set the j'th variable to a string to force a bailout.
79    eval(identifiers[j] + "+= ''");
80    assertEquals(255, or_fun());
81    // Set it back to a number for the next iteration.
82    eval(identifiers[j] + "= +" + identifiers[j]);
83  }
84}
85
86// Set up leaf masks to clear 8 least-significant bits.
87a ^= 255;
88b ^= 255;
89c ^= 255;
90d ^= 255;
91e ^= 255;
92f ^= 255;
93g ^= 255;
94h ^= 255;
95
96for (i = 0; i < and_trees.length; ++i) {
97  for (var j = 0; j < 8; ++j) {
98    var and_fun = new Function("return " + and_trees[i]);
99    if (j == 0) assertEquals(0, and_fun());
100
101    // Set the j'th variable to a string to force a bailout.
102    eval(identifiers[j] + "+= ''");
103    assertEquals(0, and_fun());
104    // Set it back to a number for the next iteration.
105    eval(identifiers[j] + "= +" + identifiers[j]);
106  }
107}
108