summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/compiler/expression-trees.js
blob: 0d971a95b25ef986e2909ea059bb38a6e883055e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Flags: --always-opt --nocompilation-cache

// Given a binary operation string and an ordered array of leaf
// strings, return an array of all binary tree strings with the leaves
// (in order) as the fringe.
function makeTrees(op, leaves) {
  var len = leaves.length;
  if (len == 1) {
    // One leaf is a leaf.
    return leaves;
  } else {
    // More than one leaf requires an interior node.
    var result = [];
    // Split the leaves into left and right subtrees in all possible
    // ways.  For each split recursively compute all possible subtrees.
    for (var i = 1; i < len; ++i) {
      var leftTrees = makeTrees(op, leaves.slice(0, i));
      var rightTrees = makeTrees(op, leaves.slice(i, len));
      // Adjoin every possible left and right subtree.
      for (var j = 0; j < leftTrees.length; ++j) {
        for (var k = 0; k < rightTrees.length; ++k) {
          var string = "(" + leftTrees[j] + op + rightTrees[k] + ")";
          result.push(string);
        }
      }
    }
    return result;
  }
}

// All possible bitwise OR trees with six leaves, i.e. CatalanNumber[5] = 42,
// see http://mathworld.wolfram.com/CatalanNumber.html.
var identifiers = ['a','b','c','d','e','f'];
var or_trees = makeTrees("|", identifiers);
var and_trees = makeTrees("&", identifiers);

// Set up leaf masks to set 6 least-significant bits.
var a = 1 << 0;
var b = 1 << 1;
var c = 1 << 2;
var d = 1 << 3;
var e = 1 << 4;
var f = 1 << 5;

for (var i = 0; i < or_trees.length; ++i) {
  for (var j = 0; j < 6; ++j) {
    var or_fun = new Function("return " + or_trees[i]);
    if (j == 0) assertEquals(63, or_fun());

    // Set the j'th variable to a string to force a bailout.
    eval(identifiers[j] + "+= ''");
    assertEquals(63, or_fun());
    // Set it back to a number for the next iteration.
    eval(identifiers[j] + "= +" + identifiers[j]);
  }
}

// Set up leaf masks to clear 6 least-significant bits.
a ^= 63;
b ^= 63;
c ^= 63;
d ^= 63;
e ^= 63;
f ^= 63;

for (i = 0; i < and_trees.length; ++i) {
  for (var j = 0; j < 6; ++j) {
    var and_fun = new Function("return " + and_trees[i]);
    if (j == 0) assertEquals(0, and_fun());

    // Set the j'th variable to a string to force a bailout.
    eval(identifiers[j] + "+= ''");
    assertEquals(0, and_fun());
    // Set it back to a number for the next iteration.
    eval(identifiers[j] + "= +" + identifiers[j]);
  }
}