Home | History | Annotate | Download | only in compiler
      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 function Test() {
     29   this.result = 0;
     30   this.x = 0;
     31   this.y = 0;
     32   this.z = 0;
     33 }
     34 var a = 1;
     35 var b = 2;
     36 var c = 4;
     37 var d = 8;
     38 
     39 // Test operations expected to stay on the fast path.  Enumerate all binary
     40 // trees with <= 4 leaves.
     41 Test.prototype.test0 = function () {
     42   this.result = a | b;
     43 };
     44 
     45 Test.prototype.test1 = function() {
     46   this.result = (a | b) | c;
     47 };
     48 
     49 Test.prototype.test2 = function() {
     50   this.result = a | (b | c);
     51 };
     52 
     53 Test.prototype.test3 = function() {
     54   this.result = ((a | b) | c) | d;
     55 };
     56 
     57 Test.prototype.test4 = function() {
     58   this.result = (a | (b | c)) | d;
     59 };
     60 
     61 Test.prototype.test5 = function() {
     62   this.result = (a | b) | (c | d);
     63 };
     64 
     65 Test.prototype.test6 = function() {
     66   this.result = a | ((b | c) | d);
     67 };
     68 
     69 Test.prototype.test7 = function() {
     70   this.result = a | (b | (c | d));
     71 };
     72 
     73 // These tests should fail if we bailed out to the beginning of the full
     74 // code.
     75 Test.prototype.test8 = function () {
     76   // If this.x = 1 and a = 1.1:
     77   this.y = this.x | b;  // Should be (1 | 2) == 3.
     78   this.x = c;           // Should be 4.
     79   this.z = this.x | a;  // Should be (4 | 1.1) == 5.
     80 };
     81 
     82 Test.prototype.test9 = function() {
     83   // If this.x = 2 and a = 1.1:
     84   this.z =              // (14 | 1.1) == 15
     85       (this.x =         // (6 | 8) == 14
     86            (this.y =    // (2 | 4) == 6
     87                 this.x  // 2
     88                 | c)    // 4
     89             | d)        // 8
     90        | a;             // 1.1
     91 }
     92 
     93 Test.prototype.test10 = function() {
     94   this.z = (a >> b) | (c >> c);
     95 }
     96 
     97 Test.prototype.test11 = function(x) {
     98   this.z = x >> x;
     99 }
    100 
    101 var t = new Test();
    102 
    103 t.test0();
    104 assertEquals(3, t.result);
    105 
    106 t.test1();
    107 assertEquals(7, t.result);
    108 t.test2();
    109 assertEquals(7, t.result);
    110 
    111 t.test3();
    112 assertEquals(15, t.result);
    113 t.test4();
    114 assertEquals(15, t.result);
    115 t.test5();
    116 assertEquals(15, t.result);
    117 t.test6();
    118 assertEquals(15, t.result);
    119 t.test7();
    120 assertEquals(15, t.result);
    121 
    122 a = 1.1;
    123 t.x = 1;
    124 t.test8();
    125 assertEquals(4, t.x);
    126 assertEquals(3, t.y);
    127 assertEquals(5, t.z);
    128 
    129 t.x = 2;
    130 t.test9();
    131 assertEquals(14, t.x);
    132 assertEquals(6, t.y);
    133 assertEquals(15, t.z);
    134 
    135 a = "2";
    136 t.test11(a);
    137 assertEquals(0, t.z);
    138 
    139 a = 4;
    140 b = "1";
    141 c = 2;
    142 t.test10();
    143 assertEquals(2, t.z);
    144