1// Copyright 2013 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: --allow-natives-syntax
29
30// A regression test for a register allocation bug triggered by
31// http://n3emscripten.appspot.com/dragons_asmjs.html. Note that the test is
32// very fragile, but without a proper testing infrastructure for the register
33// allocator we can't really do much better.
34//
35// The code below is a slighty modified version of Emscripten-translated C++
36// code for the standard textbook algorithm for frustum culling of an object
37// with a given bounding box.
38//
39// The key problem for the underlying bug was a value with a long live range
40// which is used often (a context) and a lot of live ranges starting at the same
41// point. The bug was that none of the ranges were allowed to be spilled, so the
42// allocator was splitting a live range at its start and re-added the very same
43// range into the list of unallocated ranges, making no progress.
44
45(function () {
46  function __ZNK4Math5plane3dotERKNS_6float4E(i1, i2) {
47    i1 = i1 | 0;
48    i2 = i2 | 0;
49    return +(+HEAPF32[i1 >> 2] * +HEAPF32[i2 >> 2] + +HEAPF32[i1 + 4 >> 2] * +HEAPF32[i2 + 4 >> 2] + +HEAPF32[i1 + 8 >> 2] * +HEAPF32[i2 + 8 >> 2] + +HEAPF32[i1 + 12 >> 2] * +HEAPF32[i2 + 12 >> 2]);
50  }
51
52  function __ZNK4Math7frustum8clipmaskERKNS_5pointE(i1, i2) {
53    i1 = i1 | 0;
54    i2 = i2 | 0;
55    var i3 = 0, i4 = 0;
56    i3 = i2 | 0;
57    i2 = +__ZNK4Math5plane3dotERKNS_6float4E(i1 | 0, i3) > 0.0 & 1;
58    i4 = +__ZNK4Math5plane3dotERKNS_6float4E(i1 + 16 | 0, i3) > 0.0 ? i2 | 2 : i2;
59    i2 = +__ZNK4Math5plane3dotERKNS_6float4E(i1 + 32 | 0, i3) > 0.0 ? i4 | 4 : i4;
60    i4 = +__ZNK4Math5plane3dotERKNS_6float4E(i1 + 48 | 0, i3) > 0.0 ? i2 | 8 : i2;
61    i2 = +__ZNK4Math5plane3dotERKNS_6float4E(i1 + 64 | 0, i3) > 0.0 ? i4 | 16 : i4;
62    return (+__ZNK4Math5plane3dotERKNS_6float4E(i1 + 80 | 0, i3) > 0.0 ? i2 | 32 : i2) | 0;
63  }
64
65  function __ZNK4Math7frustum10clipstatusERKNS_4bboxE(i1, i2) {
66    i1 = i1 | 0;
67    i2 = i2 | 0;
68    var i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0, i8 = 0, i9 = 0, i10 = 0, i11 = 0, i12 = 0, i13 = 0, i14 = 0, i15 = 0, i16 = 0, d17 = 0.0, d18 = 0.0, i19 = 0, i20 = 0, i21 = 0, i22 = 0;
69    i3 = STACKTOP;
70    STACKTOP = STACKTOP + 16 | 0;
71    i4 = i3 | 0;
72    i5 = i4 | 0;
73    HEAPF32[i5 >> 2] = 0.0;
74    i6 = i4 + 4 | 0;
75    HEAPF32[i6 >> 2] = 0.0;
76    i7 = i4 + 8 | 0;
77    HEAPF32[i7 >> 2] = 0.0;
78    i8 = i4 + 12 | 0;
79    HEAPF32[i8 >> 2] = 1.0;
80    i9 = i2 | 0;
81    i10 = i2 + 4 | 0;
82    i11 = i2 + 8 | 0;
83    i12 = i2 + 20 | 0;
84    i13 = i2 + 16 | 0;
85    i14 = i2 + 24 | 0;
86    i2 = 65535;
87    i15 = 0;
88    i16 = 0;
89    while (1) {
90      if ((i16 | 0) == 1) {
91        d17 = +HEAPF32[i12 >> 2];
92        d18 = +HEAPF32[i11 >> 2];
93        HEAPF32[i5 >> 2] = +HEAPF32[i9 >> 2];
94        HEAPF32[i6 >> 2] = d17;
95        HEAPF32[i7 >> 2] = d18;
96        HEAPF32[i8 >> 2] = 1.0;
97      } else if ((i16 | 0) == 4) {
98        HEAPF32[i5 >> 2] = +HEAPF32[i13 >> 2];
99        HEAPF32[i6 >> 2] = +HEAPF32[i12 >> 2];
100        HEAPF32[i7 >> 2] = +HEAPF32[i14 >> 2];
101        HEAPF32[i8 >> 2] = 1.0;
102      } else if ((i16 | 0) == 6) {
103        d18 = +HEAPF32[i10 >> 2];
104        d17 = +HEAPF32[i14 >> 2];
105        HEAPF32[i5 >> 2] = +HEAPF32[i9 >> 2];
106        HEAPF32[i6 >> 2] = d18;
107        HEAPF32[i7 >> 2] = d17;
108        HEAPF32[i8 >> 2] = 1.0;
109      } else if ((i16 | 0) == 5) {
110        d17 = +HEAPF32[i12 >> 2];
111        d18 = +HEAPF32[i14 >> 2];
112        HEAPF32[i5 >> 2] = +HEAPF32[i9 >> 2];
113        HEAPF32[i6 >> 2] = d17;
114        HEAPF32[i7 >> 2] = d18;
115        HEAPF32[i8 >> 2] = 1.0;
116      } else if ((i16 | 0) == 3) {
117        d18 = +HEAPF32[i10 >> 2];
118        d17 = +HEAPF32[i11 >> 2];
119        HEAPF32[i5 >> 2] = +HEAPF32[i13 >> 2];
120        HEAPF32[i6 >> 2] = d18;
121        HEAPF32[i7 >> 2] = d17;
122        HEAPF32[i8 >> 2] = 1.0;
123      } else if ((i16 | 0) == 0) {
124        HEAPF32[i5 >> 2] = +HEAPF32[i9 >> 2];
125        HEAPF32[i6 >> 2] = +HEAPF32[i10 >> 2];
126        HEAPF32[i7 >> 2] = +HEAPF32[i11 >> 2];
127        HEAPF32[i8 >> 2] = 1.0;
128      } else if ((i16 | 0) == 2) {
129        d17 = +HEAPF32[i12 >> 2];
130        d18 = +HEAPF32[i11 >> 2];
131        HEAPF32[i5 >> 2] = +HEAPF32[i13 >> 2];
132        HEAPF32[i6 >> 2] = d17;
133        HEAPF32[i7 >> 2] = d18;
134        HEAPF32[i8 >> 2] = 1.0;
135      } else if ((i16 | 0) == 7) {
136        d18 = +HEAPF32[i10 >> 2];
137        d17 = +HEAPF32[i14 >> 2];
138        HEAPF32[i5 >> 2] = +HEAPF32[i13 >> 2];
139        HEAPF32[i6 >> 2] = d18;
140        HEAPF32[i7 >> 2] = d17;
141        HEAPF32[i8 >> 2] = 1.0;
142      }
143      i19 = __ZNK4Math7frustum8clipmaskERKNS_5pointE(i1, i4) | 0;
144      i20 = i19 & i2;
145      i21 = i19 | i15;
146      i19 = i16 + 1 | 0;
147      if ((i19 | 0) == 8) {
148        break;
149      } else {
150        i2 = i20;
151        i15 = i21;
152        i16 = i19;
153      }
154    }
155    if ((i21 | 0) == 0) {
156      i22 = 0;
157      STACKTOP = i3;
158      return i22 | 0;
159    }
160    i22 = (i20 | 0) == 0 ? 2 : 1;
161    STACKTOP = i3;
162    return i22 | 0;
163  }
164
165  // -----------------------------------------------------------------
166
167  var STACKTOP = 0;
168  var HEAPF32 = new Float32Array(1000);
169
170  for (var i = 0; i < HEAPF32.length; i++) {
171    HEAPF32[i] = 1.0;
172  }
173
174  __ZNK4Math7frustum10clipstatusERKNS_4bboxE(0, 0);
175  __ZNK4Math7frustum10clipstatusERKNS_4bboxE(0, 0);
176  __ZNK4Math7frustum10clipstatusERKNS_4bboxE(0, 0);
177  %OptimizeFunctionOnNextCall(__ZNK4Math7frustum10clipstatusERKNS_4bboxE);
178  __ZNK4Math7frustum10clipstatusERKNS_4bboxE(0, 0);
179})();
180