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// Regression test that stresses the register allocator gap instruction.
29
30function small_select(n, v1, v2) {
31  for (var i = 0; i < n; ++i) {
32    var tmp = v1;
33    v1 = v2;
34    v2 = tmp;
35  }
36  return v1;
37}
38
39function select(n, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) {
40  for (var i = 0; i < n; ++i) {
41    var tmp = v1;
42    v1 = v2;
43    v2 = v3;
44    v3 = v4;
45    v4 = v5;
46    v5 = v6;
47    v6 = v7;
48    v7 = v8;
49    v8 = v9;
50    v9 = v10;
51    v10 = tmp;
52  }
53  return v1;
54}
55
56function select_while(n, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) {
57  var i = 0;
58  while (i < n) {
59    var tmp = v1;
60    v1 = v2;
61    v2 = v3;
62    v3 = v4;
63    v4 = v5;
64    v5 = v6;
65    v6 = v7;
66    v7 = v8;
67    v8 = v9;
68    v9 = v10;
69    v10 = tmp;
70    i++;
71  }
72  return v1;
73}
74
75function two_cycles(n, v1, v2, v3, v4, v5, x1, x2, x3, x4, x5) {
76  for (var i = 0; i < n; ++i) {
77    var tmp = v1;
78    v1 = v2;
79    v2 = v3;
80    v3 = v4;
81    v4 = v5;
82    v5 = tmp;
83    tmp = x1;
84    x1 = x2;
85    x2 = x3;
86    x3 = x4;
87    x4 = x5;
88    x5 = tmp;
89  }
90  return v1 + x1;
91}
92
93function two_cycles_while(n, v1, v2, v3, v4, v5, x1, x2, x3, x4, x5) {
94  var i = 0;
95  while (i < n) {
96    var tmp = v1;
97    v1 = v2;
98    v2 = v3;
99    v3 = v4;
100    v4 = v5;
101    v5 = tmp;
102    tmp = x1;
103    x1 = x2;
104    x2 = x3;
105    x3 = x4;
106    x4 = x5;
107    x5 = tmp;
108    i++;
109  }
110  return v1 + x1;
111}
112assertEquals(1, small_select(0, 1, 2));
113assertEquals(2, small_select(1, 1, 2));
114assertEquals(1, small_select(10, 1, 2));
115
116assertEquals(1, select(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
117assertEquals(4, select(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
118assertEquals(10, select(9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
119
120assertEquals(1 + 6, two_cycles(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
121assertEquals(4 + 9, two_cycles(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
122assertEquals(5 + 10, two_cycles(9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
123
124assertEquals(1, select_while(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
125assertEquals(4, select_while(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
126assertEquals(10, select_while(9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
127
128assertEquals(1 + 6, two_cycles_while(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
129assertEquals(4 + 9, two_cycles_while(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
130assertEquals(5 + 10, two_cycles_while(9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
131